isci: fix remote_device start_io regressions
[pandora-kernel.git] / drivers / scsi / isci / remote_device.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 "intel_sas.h"
56 #include "intel_ata.h"
57 #include "isci.h"
58 #include "port.h"
59 #include "remote_device.h"
60 #include "request.h"
61 #include "scic_controller.h"
62 #include "scic_io_request.h"
63 #include "scic_phy.h"
64 #include "scic_port.h"
65 #include "scic_sds_controller.h"
66 #include "scic_sds_phy.h"
67 #include "scic_sds_port.h"
68 #include "remote_node_context.h"
69 #include "scic_sds_request.h"
70 #include "sci_environment.h"
71 #include "sci_util.h"
72 #include "scu_event_codes.h"
73 #include "task.h"
74
75 /**
76  * isci_remote_device_change_state() - This function gets the status of the
77  *    remote_device object.
78  * @isci_device: This parameter points to the isci_remote_device object
79  *
80  * status of the object as a isci_status enum.
81  */
82 void isci_remote_device_change_state(
83         struct isci_remote_device *isci_device,
84         enum isci_status status)
85 {
86         unsigned long flags;
87
88         spin_lock_irqsave(&isci_device->state_lock, flags);
89         isci_device->status = status;
90         spin_unlock_irqrestore(&isci_device->state_lock, flags);
91 }
92
93 /**
94  * isci_remote_device_not_ready() - This function is called by the scic when
95  *    the remote device is not ready. We mark the isci device as ready (not
96  *    "ready_for_io") and signal the waiting proccess.
97  * @isci_host: This parameter specifies the isci host object.
98  * @isci_device: This parameter specifies the remote device
99  *
100  */
101 static void isci_remote_device_not_ready(struct isci_host *ihost,
102                                   struct isci_remote_device *idev, u32 reason)
103 {
104         dev_dbg(&ihost->pdev->dev,
105                 "%s: isci_device = %p\n", __func__, idev);
106
107         if (reason == SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED)
108                 isci_remote_device_change_state(idev, isci_stopping);
109         else
110                 /* device ready is actually a "not ready for io" state. */
111                 isci_remote_device_change_state(idev, isci_ready);
112 }
113
114 /**
115  * isci_remote_device_ready() - This function is called by the scic when the
116  *    remote device is ready. We mark the isci device as ready and signal the
117  *    waiting proccess.
118  * @ihost: our valid isci_host
119  * @idev: remote device
120  *
121  */
122 static void isci_remote_device_ready(struct isci_host *ihost, struct isci_remote_device *idev)
123 {
124         dev_dbg(&ihost->pdev->dev,
125                 "%s: idev = %p\n", __func__, idev);
126
127         isci_remote_device_change_state(idev, isci_ready_for_io);
128         if (test_and_clear_bit(IDEV_START_PENDING, &idev->flags))
129                 wake_up(&ihost->eventq);
130 }
131
132
133
134 enum sci_status scic_remote_device_stop(
135         struct scic_sds_remote_device *sci_dev,
136         u32 timeout)
137 {
138         return sci_dev->state_handlers->stop_handler(sci_dev);
139 }
140
141
142 enum sci_status scic_remote_device_reset(
143         struct scic_sds_remote_device *sci_dev)
144 {
145         return sci_dev->state_handlers->reset_handler(sci_dev);
146 }
147
148
149 enum sci_status scic_remote_device_reset_complete(
150         struct scic_sds_remote_device *sci_dev)
151 {
152         return sci_dev->state_handlers->reset_complete_handler(sci_dev);
153 }
154
155 #define SCIC_SDS_REMOTE_DEVICE_MINIMUM_TIMER_COUNT (0)
156 #define SCIC_SDS_REMOTE_DEVICE_MAXIMUM_TIMER_COUNT (SCI_MAX_REMOTE_DEVICES)
157
158 /**
159  *
160  * @sci_dev: The remote device for which the suspend is being requested.
161  *
162  * This method invokes the remote device suspend state handler. enum sci_status
163  */
164 enum sci_status scic_sds_remote_device_suspend(
165         struct scic_sds_remote_device *sci_dev,
166         u32 suspend_type)
167 {
168         return sci_dev->state_handlers->suspend_handler(sci_dev, suspend_type);
169 }
170
171 /**
172  *
173  * @sci_dev: The remote device for which the event handling is being
174  *    requested.
175  * @frame_index: This is the frame index that is being processed.
176  *
177  * This method invokes the frame handler for the remote device state machine
178  * enum sci_status
179  */
180 enum sci_status scic_sds_remote_device_frame_handler(
181         struct scic_sds_remote_device *sci_dev,
182         u32 frame_index)
183 {
184         return sci_dev->state_handlers->frame_handler(sci_dev, frame_index);
185 }
186
187 /**
188  *
189  * @sci_dev: The remote device for which the event handling is being
190  *    requested.
191  * @event_code: This is the event code that is to be processed.
192  *
193  * This method invokes the remote device event handler. enum sci_status
194  */
195 enum sci_status scic_sds_remote_device_event_handler(
196         struct scic_sds_remote_device *sci_dev,
197         u32 event_code)
198 {
199         return sci_dev->state_handlers->event_handler(sci_dev, event_code);
200 }
201
202 /**
203  *
204  * @controller: The controller that is starting the io request.
205  * @sci_dev: The remote device for which the start io handling is being
206  *    requested.
207  * @io_request: The io request that is being started.
208  *
209  * This method invokes the remote device start io handler. enum sci_status
210  */
211 enum sci_status scic_sds_remote_device_start_io(
212         struct scic_sds_controller *controller,
213         struct scic_sds_remote_device *sci_dev,
214         struct scic_sds_request *io_request)
215 {
216         return sci_dev->state_handlers->start_io_handler(
217                        sci_dev, io_request);
218 }
219
220 /**
221  *
222  * @controller: The controller that is completing the io request.
223  * @sci_dev: The remote device for which the complete io handling is being
224  *    requested.
225  * @io_request: The io request that is being completed.
226  *
227  * This method invokes the remote device complete io handler. enum sci_status
228  */
229 enum sci_status scic_sds_remote_device_complete_io(
230         struct scic_sds_controller *controller,
231         struct scic_sds_remote_device *sci_dev,
232         struct scic_sds_request *io_request)
233 {
234         return sci_dev->state_handlers->complete_io_handler(
235                        sci_dev, io_request);
236 }
237
238 /**
239  *
240  * @controller: The controller that is starting the task request.
241  * @sci_dev: The remote device for which the start task handling is being
242  *    requested.
243  * @io_request: The task request that is being started.
244  *
245  * This method invokes the remote device start task handler. enum sci_status
246  */
247 enum sci_status scic_sds_remote_device_start_task(
248         struct scic_sds_controller *controller,
249         struct scic_sds_remote_device *sci_dev,
250         struct scic_sds_request *io_request)
251 {
252         return sci_dev->state_handlers->start_task_handler(
253                        sci_dev, io_request);
254 }
255
256 /**
257  *
258  * @sci_dev:
259  * @request:
260  *
261  * This method takes the request and bulids an appropriate SCU context for the
262  * request and then requests the controller to post the request. none
263  */
264 void scic_sds_remote_device_post_request(
265         struct scic_sds_remote_device *sci_dev,
266         u32 request)
267 {
268         u32 context;
269
270         context = scic_sds_remote_device_build_command_context(sci_dev, request);
271
272         scic_sds_controller_post_request(
273                 scic_sds_remote_device_get_controller(sci_dev),
274                 context
275                 );
276 }
277
278 /* called once the remote node context is ready to be freed.
279  * The remote device can now report that its stop operation is complete. none
280  */
281 static void scic_sds_cb_remote_device_rnc_destruct_complete(void *_dev)
282 {
283         struct scic_sds_remote_device *sci_dev = _dev;
284
285         BUG_ON(sci_dev->started_request_count != 0);
286         sci_base_state_machine_change_state(&sci_dev->state_machine,
287                                             SCI_BASE_REMOTE_DEVICE_STATE_STOPPED);
288 }
289
290 /* called once the remote node context has transisitioned to a
291  * ready state.  This is the indication that the remote device object can also
292  * transition to ready.
293  */
294 static void scic_sds_remote_device_resume_complete_handler(void *_dev)
295 {
296         struct scic_sds_remote_device *sci_dev = _dev;
297         enum scic_sds_remote_device_states state;
298
299         state = sci_dev->state_machine.current_state_id;
300         switch (state) {
301         case SCI_BASE_REMOTE_DEVICE_STATE_READY:
302         case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
303         case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
304         case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ:
305         case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
306         case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
307         case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
308         case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
309                 break;
310         default:
311                 /* go 'ready' if we are not already in a ready state */
312                 sci_base_state_machine_change_state(&sci_dev->state_machine,
313                                                     SCI_BASE_REMOTE_DEVICE_STATE_READY);
314                 break;
315         }
316 }
317
318 /**
319  *
320  * @device: This parameter specifies the device for which the request is being
321  *    started.
322  * @request: This parameter specifies the request being started.
323  * @status: This parameter specifies the current start operation status.
324  *
325  * This method will perform the STP request start processing common to IO
326  * requests and task requests of all types. none
327  */
328 static void scic_sds_remote_device_start_request(
329         struct scic_sds_remote_device *sci_dev,
330         struct scic_sds_request *sci_req,
331         enum sci_status status)
332 {
333         /* We still have a fault in starting the io complete it on the port */
334         if (status == SCI_SUCCESS)
335                 scic_sds_remote_device_increment_request_count(sci_dev);
336         else{
337                 sci_dev->owning_port->state_handlers->complete_io_handler(
338                         sci_dev->owning_port, sci_dev, sci_req
339                         );
340         }
341 }
342
343
344 /**
345  *
346  * @request: This parameter specifies the request being continued.
347  *
348  * This method will continue to post tc for a STP request. This method usually
349  * serves as a callback when RNC gets resumed during a task management
350  * sequence. none
351  */
352 static void scic_sds_remote_device_continue_request(void *dev)
353 {
354         struct scic_sds_remote_device *sci_dev = dev;
355
356         /* we need to check if this request is still valid to continue. */
357         if (sci_dev->working_request)
358                 scic_controller_continue_io(sci_dev->working_request);
359 }
360
361 /**
362  * This method will terminate all of the IO requests in the controllers IO
363  *    request table that were targeted for this device.
364  * @sci_dev: This parameter specifies the remote device for which to
365  *    attempt to terminate all requests.
366  *
367  * This method returns an indication as to whether all requests were
368  * successfully terminated.  If a single request fails to be terminated, then
369  * this method will return the failure.
370  */
371 static enum sci_status scic_sds_remote_device_terminate_requests(
372         struct scic_sds_remote_device *sci_dev)
373 {
374         enum sci_status status           = SCI_SUCCESS;
375         enum sci_status terminate_status = SCI_SUCCESS;
376         struct scic_sds_request *sci_req;
377         u32 index;
378         u32 request_count    = sci_dev->started_request_count;
379
380         for (index = 0;
381              (index < SCI_MAX_IO_REQUESTS) && (request_count > 0);
382              index++) {
383                 sci_req = sci_dev->owning_port->owning_controller->io_request_table[index];
384
385                 if ((sci_req != NULL) && (sci_req->target_device == sci_dev)) {
386                         terminate_status = scic_controller_terminate_request(
387                                 sci_dev->owning_port->owning_controller,
388                                 sci_dev,
389                                 sci_req
390                                 );
391
392                         if (terminate_status != SCI_SUCCESS)
393                                 status = terminate_status;
394
395                         request_count--;
396                 }
397         }
398
399         return status;
400 }
401
402 static enum sci_status
403 default_device_handler(struct scic_sds_remote_device *sci_dev,
404                        const char *func)
405 {
406         dev_warn(scirdev_to_dev(sci_dev),
407                  "%s: in wrong state: %d\n", func,
408                  sci_base_state_machine_get_state(&sci_dev->state_machine));
409         return SCI_FAILURE_INVALID_STATE;
410 }
411
412 static enum sci_status scic_sds_remote_device_default_start_handler(
413         struct scic_sds_remote_device *sci_dev)
414 {
415         return default_device_handler(sci_dev, __func__);
416 }
417
418 static enum sci_status scic_sds_remote_device_default_stop_handler(
419         struct scic_sds_remote_device *sci_dev)
420 {
421         return default_device_handler(sci_dev, __func__);
422 }
423
424 static enum sci_status scic_sds_remote_device_default_fail_handler(
425         struct scic_sds_remote_device *sci_dev)
426 {
427         return default_device_handler(sci_dev, __func__);
428 }
429
430 static enum sci_status scic_sds_remote_device_default_destruct_handler(
431         struct scic_sds_remote_device *sci_dev)
432 {
433         return default_device_handler(sci_dev, __func__);
434 }
435
436 static enum sci_status scic_sds_remote_device_default_reset_handler(
437         struct scic_sds_remote_device *sci_dev)
438 {
439         return default_device_handler(sci_dev, __func__);
440 }
441
442 static enum sci_status scic_sds_remote_device_default_reset_complete_handler(
443         struct scic_sds_remote_device *sci_dev)
444 {
445         return default_device_handler(sci_dev, __func__);
446 }
447
448 static enum sci_status scic_sds_remote_device_default_suspend_handler(
449         struct scic_sds_remote_device *sci_dev, u32 suspend_type)
450 {
451         return default_device_handler(sci_dev, __func__);
452 }
453
454 static enum sci_status scic_sds_remote_device_default_resume_handler(
455         struct scic_sds_remote_device *sci_dev)
456 {
457         return default_device_handler(sci_dev, __func__);
458 }
459
460 /**
461  *
462  * @device: The struct scic_sds_remote_device which is then cast into a
463  *    struct scic_sds_remote_device.
464  * @event_code: The event code that the struct scic_sds_controller wants the device
465  *    object to process.
466  *
467  * This method is the default event handler.  It will call the RNC state
468  * machine handler for any RNC events otherwise it will log a warning and
469  * returns a failure. enum sci_status SCI_FAILURE_INVALID_STATE
470  */
471 static enum sci_status  scic_sds_remote_device_core_event_handler(
472         struct scic_sds_remote_device *sci_dev,
473         u32 event_code,
474         bool is_ready_state)
475 {
476         enum sci_status status;
477
478         switch (scu_get_event_type(event_code)) {
479         case SCU_EVENT_TYPE_RNC_OPS_MISC:
480         case SCU_EVENT_TYPE_RNC_SUSPEND_TX:
481         case SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX:
482                 status = scic_sds_remote_node_context_event_handler(&sci_dev->rnc, event_code);
483                 break;
484         case SCU_EVENT_TYPE_PTX_SCHEDULE_EVENT:
485
486                 if (scu_get_event_code(event_code) == SCU_EVENT_IT_NEXUS_TIMEOUT) {
487                         status = SCI_SUCCESS;
488
489                         /* Suspend the associated RNC */
490                         scic_sds_remote_node_context_suspend(&sci_dev->rnc,
491                                                               SCI_SOFTWARE_SUSPENSION,
492                                                               NULL, NULL);
493
494                         dev_dbg(scirdev_to_dev(sci_dev),
495                                 "%s: device: %p event code: %x: %s\n",
496                                 __func__, sci_dev, event_code,
497                                 (is_ready_state)
498                                 ? "I_T_Nexus_Timeout event"
499                                 : "I_T_Nexus_Timeout event in wrong state");
500
501                         break;
502                 }
503         /* Else, fall through and treat as unhandled... */
504
505         default:
506                 dev_dbg(scirdev_to_dev(sci_dev),
507                         "%s: device: %p event code: %x: %s\n",
508                         __func__, sci_dev, event_code,
509                         (is_ready_state)
510                         ? "unexpected event"
511                         : "unexpected event in wrong state");
512                 status = SCI_FAILURE_INVALID_STATE;
513                 break;
514         }
515
516         return status;
517 }
518 /**
519  *
520  * @device: The struct scic_sds_remote_device which is then cast into a
521  *    struct scic_sds_remote_device.
522  * @event_code: The event code that the struct scic_sds_controller wants the device
523  *    object to process.
524  *
525  * This method is the default event handler.  It will call the RNC state
526  * machine handler for any RNC events otherwise it will log a warning and
527  * returns a failure. enum sci_status SCI_FAILURE_INVALID_STATE
528  */
529 static enum sci_status  scic_sds_remote_device_default_event_handler(
530         struct scic_sds_remote_device *sci_dev,
531         u32 event_code)
532 {
533         return scic_sds_remote_device_core_event_handler(sci_dev,
534                                                           event_code,
535                                                           false);
536 }
537
538 /**
539  *
540  * @device: The struct scic_sds_remote_device which is then cast into a
541  *    struct scic_sds_remote_device.
542  * @frame_index: The frame index for which the struct scic_sds_controller wants this
543  *    device object to process.
544  *
545  * This method is the default unsolicited frame handler.  It logs a warning,
546  * releases the frame and returns a failure. enum sci_status
547  * SCI_FAILURE_INVALID_STATE
548  */
549 static enum sci_status scic_sds_remote_device_default_frame_handler(
550         struct scic_sds_remote_device *sci_dev,
551         u32 frame_index)
552 {
553         dev_warn(scirdev_to_dev(sci_dev),
554                  "%s: SCIC Remote Device requested to handle frame %x "
555                  "while in wrong state %d\n",
556                  __func__,
557                  frame_index,
558                  sci_base_state_machine_get_state(
559                          &sci_dev->state_machine));
560
561         /* Return the frame back to the controller */
562         scic_sds_controller_release_frame(
563                 scic_sds_remote_device_get_controller(sci_dev), frame_index
564                 );
565
566         return SCI_FAILURE_INVALID_STATE;
567 }
568
569 static enum sci_status scic_sds_remote_device_default_start_request_handler(
570         struct scic_sds_remote_device *sci_dev,
571         struct scic_sds_request *request)
572 {
573         return default_device_handler(sci_dev, __func__);
574 }
575
576 static enum sci_status scic_sds_remote_device_default_complete_request_handler(
577         struct scic_sds_remote_device *sci_dev,
578         struct scic_sds_request *request)
579 {
580         return default_device_handler(sci_dev, __func__);
581 }
582
583 static enum sci_status scic_sds_remote_device_default_continue_request_handler(
584         struct scic_sds_remote_device *sci_dev,
585         struct scic_sds_request *request)
586 {
587         return default_device_handler(sci_dev, __func__);
588 }
589
590 /**
591  *
592  * @device: The struct scic_sds_remote_device which is then cast into a
593  *    struct scic_sds_remote_device.
594  * @frame_index: The frame index for which the struct scic_sds_controller wants this
595  *    device object to process.
596  *
597  * This method is a general ssp frame handler.  In most cases the device object
598  * needs to route the unsolicited frame processing to the io request object.
599  * This method decodes the tag for the io request object and routes the
600  * unsolicited frame to that object. enum sci_status SCI_FAILURE_INVALID_STATE
601  */
602 static enum sci_status scic_sds_remote_device_general_frame_handler(
603         struct scic_sds_remote_device *sci_dev,
604         u32 frame_index)
605 {
606         enum sci_status result;
607         struct sci_ssp_frame_header *frame_header;
608         struct scic_sds_request *io_request;
609
610         result = scic_sds_unsolicited_frame_control_get_header(
611                 &(scic_sds_remote_device_get_controller(sci_dev)->uf_control),
612                 frame_index,
613                 (void **)&frame_header
614                 );
615
616         if (SCI_SUCCESS == result) {
617                 io_request = scic_sds_controller_get_io_request_from_tag(
618                         scic_sds_remote_device_get_controller(sci_dev), frame_header->tag);
619
620                 if ((io_request == NULL)
621                     || (io_request->target_device != sci_dev)) {
622                         /*
623                          * We could not map this tag to a valid IO request
624                          * Just toss the frame and continue */
625                         scic_sds_controller_release_frame(
626                                 scic_sds_remote_device_get_controller(sci_dev), frame_index
627                                 );
628                 } else {
629                         /* The IO request is now in charge of releasing the frame */
630                         result = io_request->state_handlers->frame_handler(
631                                 io_request, frame_index);
632                 }
633         }
634
635         return result;
636 }
637
638 /**
639  *
640  * @[in]: sci_dev This is the device object that is receiving the event.
641  * @[in]: event_code The event code to process.
642  *
643  * This is a common method for handling events reported to the remote device
644  * from the controller object. enum sci_status
645  */
646 static enum sci_status scic_sds_remote_device_general_event_handler(
647         struct scic_sds_remote_device *sci_dev,
648         u32 event_code)
649 {
650         return scic_sds_remote_device_core_event_handler(sci_dev,
651                                                           event_code,
652                                                           true);
653 }
654
655 /*
656  * *****************************************************************************
657  * *  STOPPED STATE HANDLERS
658  * ***************************************************************************** */
659
660 /**
661  *
662  * @device:
663  *
664  * This method takes the struct scic_sds_remote_device from a stopped state and
665  * attempts to start it.   The RNC buffer for the device is constructed and the
666  * device state machine is transitioned to the
667  * SCIC_BASE_REMOTE_DEVICE_STATE_STARTING. enum sci_status SCI_SUCCESS if there is
668  * an RNC buffer available to construct the remote device.
669  * SCI_FAILURE_INSUFFICIENT_RESOURCES if there is no RNC buffer available in
670  * which to construct the remote device.
671  */
672 static enum sci_status scic_sds_remote_device_stopped_state_start_handler(
673         struct scic_sds_remote_device *sci_dev)
674 {
675         enum sci_status status;
676
677         status = scic_sds_remote_node_context_resume(&sci_dev->rnc,
678                         scic_sds_remote_device_resume_complete_handler, sci_dev);
679
680         if (status == SCI_SUCCESS)
681                 sci_base_state_machine_change_state(&sci_dev->state_machine,
682                                                     SCI_BASE_REMOTE_DEVICE_STATE_STARTING);
683
684         return status;
685 }
686
687 static enum sci_status scic_sds_remote_device_stopped_state_stop_handler(
688         struct scic_sds_remote_device *sci_dev)
689 {
690         return SCI_SUCCESS;
691 }
692
693 /**
694  *
695  * @sci_dev: The struct scic_sds_remote_device which is cast into a
696  *    struct scic_sds_remote_device.
697  *
698  * This method will destruct a struct scic_sds_remote_device that is in a stopped
699  * state.  This is the only state from which a destruct request will succeed.
700  * The RNi for this struct scic_sds_remote_device is returned to the free pool and the
701  * device object transitions to the SCI_BASE_REMOTE_DEVICE_STATE_FINAL.
702  * enum sci_status SCI_SUCCESS
703  */
704 static enum sci_status scic_sds_remote_device_stopped_state_destruct_handler(
705         struct scic_sds_remote_device *sci_dev)
706 {
707         struct scic_sds_controller *scic;
708
709         scic = scic_sds_remote_device_get_controller(sci_dev);
710         scic_sds_controller_free_remote_node_context(scic, sci_dev,
711                                                      sci_dev->rnc.remote_node_index);
712         sci_dev->rnc.remote_node_index = SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX;
713
714         sci_base_state_machine_change_state(&sci_dev->state_machine,
715                                             SCI_BASE_REMOTE_DEVICE_STATE_FINAL);
716
717         return SCI_SUCCESS;
718 }
719
720 /*
721  * *****************************************************************************
722  * *  STARTING STATE HANDLERS
723  * ***************************************************************************** */
724
725 static enum sci_status scic_sds_remote_device_starting_state_stop_handler(
726         struct scic_sds_remote_device *sci_dev)
727 {
728         /*
729          * This device has not yet started so there had better be no IO requests
730          */
731         BUG_ON(sci_dev->started_request_count != 0);
732
733         /*
734          * Destroy the remote node context
735          */
736         scic_sds_remote_node_context_destruct(&sci_dev->rnc,
737                 scic_sds_cb_remote_device_rnc_destruct_complete, sci_dev);
738
739         /*
740          * Transition to the stopping state and wait for the remote node to
741          * complete being posted and invalidated.
742          */
743         sci_base_state_machine_change_state(&sci_dev->state_machine,
744                                             SCI_BASE_REMOTE_DEVICE_STATE_STOPPING);
745
746         return SCI_SUCCESS;
747 }
748
749 static enum sci_status scic_sds_remote_device_ready_state_stop_handler(
750         struct scic_sds_remote_device *sci_dev)
751 {
752         enum sci_status status = SCI_SUCCESS;
753
754         /* Request the parent state machine to transition to the stopping state */
755         sci_base_state_machine_change_state(&sci_dev->state_machine,
756                                             SCI_BASE_REMOTE_DEVICE_STATE_STOPPING);
757
758         if (sci_dev->started_request_count == 0) {
759                 scic_sds_remote_node_context_destruct(&sci_dev->rnc,
760                         scic_sds_cb_remote_device_rnc_destruct_complete,
761                         sci_dev);
762         } else
763                 status = scic_sds_remote_device_terminate_requests(sci_dev);
764
765         return status;
766 }
767
768 /**
769  *
770  * @device: The struct scic_sds_remote_device object which is cast to a
771  *    struct scic_sds_remote_device object.
772  *
773  * This is the ready state device reset handler enum sci_status
774  */
775 static enum sci_status scic_sds_remote_device_ready_state_reset_handler(
776         struct scic_sds_remote_device *sci_dev)
777 {
778         /* Request the parent state machine to transition to the stopping state */
779         sci_base_state_machine_change_state(&sci_dev->state_machine,
780                                             SCI_BASE_REMOTE_DEVICE_STATE_RESETTING);
781
782         return SCI_SUCCESS;
783 }
784
785 /*
786  * This method will attempt to start a task request for this device object. The
787  * remote device object will issue the start request for the task and if
788  * successful it will start the request for the port object then increment its
789  * own requet count. enum sci_status SCI_SUCCESS if the task request is started for
790  * this device object. SCI_FAILURE_INSUFFICIENT_RESOURCES if the io request
791  * object could not get the resources to start.
792  */
793 static enum sci_status scic_sds_remote_device_ready_state_start_task_handler(
794         struct scic_sds_remote_device *sci_dev,
795         struct scic_sds_request *request)
796 {
797         enum sci_status result;
798
799         /* See if the port is in a state where we can start the IO request */
800         result = scic_sds_port_start_io(
801                 scic_sds_remote_device_get_port(sci_dev), sci_dev, request);
802
803         if (result == SCI_SUCCESS) {
804                 result = scic_sds_remote_node_context_start_task(&sci_dev->rnc,
805                                                                  request);
806                 if (result == SCI_SUCCESS)
807                         result = scic_sds_request_start(request);
808
809                 scic_sds_remote_device_start_request(sci_dev, request, result);
810         }
811
812         return result;
813 }
814
815 /*
816  * This method will attempt to start an io request for this device object. The
817  * remote device object will issue the start request for the io and if
818  * successful it will start the request for the port object then increment its
819  * own requet count. enum sci_status SCI_SUCCESS if the io request is started for
820  * this device object. SCI_FAILURE_INSUFFICIENT_RESOURCES if the io request
821  * object could not get the resources to start.
822  */
823 static enum sci_status scic_sds_remote_device_ready_state_start_io_handler(
824         struct scic_sds_remote_device *sci_dev,
825         struct scic_sds_request *request)
826 {
827         enum sci_status result;
828
829         /* See if the port is in a state where we can start the IO request */
830         result = scic_sds_port_start_io(
831                 scic_sds_remote_device_get_port(sci_dev), sci_dev, request);
832
833         if (result == SCI_SUCCESS) {
834                 result = scic_sds_remote_node_context_start_io(&sci_dev->rnc, request);
835                 if (result == SCI_SUCCESS)
836                         result = scic_sds_request_start(request);
837
838                 scic_sds_remote_device_start_request(sci_dev, request, result);
839         }
840
841         return result;
842 }
843
844 /*
845  * This method will complete the request for the remote device object.  The
846  * method will call the completion handler for the request object and if
847  * successful it will complete the request on the port object then decrement
848  * its own started_request_count. enum sci_status
849  */
850 static enum sci_status scic_sds_remote_device_ready_state_complete_request_handler(
851         struct scic_sds_remote_device *sci_dev,
852         struct scic_sds_request *request)
853 {
854         enum sci_status result;
855
856         result = scic_sds_request_complete(request);
857
858         if (result != SCI_SUCCESS)
859                 return result;
860
861         /* See if the port is in a state
862          * where we can start the IO request */
863         result = scic_sds_port_complete_io(
864                         scic_sds_remote_device_get_port(sci_dev),
865                         sci_dev, request);
866
867         if (result == SCI_SUCCESS)
868                 scic_sds_remote_device_decrement_request_count(sci_dev);
869
870         return result;
871 }
872
873 /*
874  * *****************************************************************************
875  * *  STOPPING STATE HANDLERS
876  * ***************************************************************************** */
877
878 /**
879  *
880  * @sci_dev: The struct scic_sds_remote_device which is cast into a
881  *    struct scic_sds_remote_device.
882  *
883  * This method will stop a struct scic_sds_remote_device that is already in the
884  * SCI_BASE_REMOTE_DEVICE_STATE_STOPPING state. This is not considered an error
885  * since we allow a stop request on a device that is alreay stopping or
886  * stopped. enum sci_status SCI_SUCCESS
887  */
888 static enum sci_status scic_sds_remote_device_stopping_state_stop_handler(
889         struct scic_sds_remote_device *device)
890 {
891         /*
892          * All requests should have been terminated, but if there is an
893          * attempt to stop a device already in the stopping state, then
894          * try again to terminate. */
895         return scic_sds_remote_device_terminate_requests(device);
896 }
897
898
899 /**
900  *
901  * @device: The device object for which the request is completing.
902  * @request: The task request that is being completed.
903  *
904  * This method completes requests for this struct scic_sds_remote_device while it is
905  * in the SCI_BASE_REMOTE_DEVICE_STATE_STOPPING state. This method calls the
906  * complete method for the request object and if that is successful the port
907  * object is called to complete the task request. Then the device object itself
908  * completes the task request. If struct scic_sds_remote_device started_request_count
909  * goes to 0 and the invalidate RNC request has completed the device object can
910  * transition to the SCI_BASE_REMOTE_DEVICE_STATE_STOPPED. enum sci_status
911  */
912 static enum sci_status scic_sds_remote_device_stopping_state_complete_request_handler(
913         struct scic_sds_remote_device *sci_dev,
914         struct scic_sds_request *request)
915 {
916         enum sci_status status = SCI_SUCCESS;
917
918         status = scic_sds_request_complete(request);
919
920         if (status != SCI_SUCCESS)
921                 return status;
922
923         status = scic_sds_port_complete_io(scic_sds_remote_device_get_port(sci_dev),
924                                            sci_dev, request);
925         if (status != SCI_SUCCESS)
926                 return status;
927
928         scic_sds_remote_device_decrement_request_count(sci_dev);
929
930         if (scic_sds_remote_device_get_request_count(sci_dev) == 0)
931                 scic_sds_remote_node_context_destruct(&sci_dev->rnc,
932                                                       scic_sds_cb_remote_device_rnc_destruct_complete,
933                                                       sci_dev);
934         return SCI_SUCCESS;
935 }
936
937 static enum sci_status scic_sds_remote_device_resetting_state_reset_complete_handler(
938         struct scic_sds_remote_device *sci_dev)
939 {
940         sci_base_state_machine_change_state(&sci_dev->state_machine,
941                                             SCI_BASE_REMOTE_DEVICE_STATE_READY);
942
943         return SCI_SUCCESS;
944 }
945
946 static enum sci_status scic_sds_remote_device_resetting_state_stop_handler(
947         struct scic_sds_remote_device *sci_dev)
948 {
949         sci_base_state_machine_change_state(
950                 &sci_dev->state_machine,
951                 SCI_BASE_REMOTE_DEVICE_STATE_STOPPING
952                 );
953
954         return SCI_SUCCESS;
955 }
956
957 /* complete requests for this device while it is in the
958  * SCI_BASE_REMOTE_DEVICE_STATE_RESETTING state. This method calls the complete
959  * method for the request object and if that is successful the port object is
960  * called to complete the task request. Then the device object itself completes
961  * the task request. enum sci_status
962  */
963 static enum sci_status scic_sds_remote_device_resetting_state_complete_request_handler(
964         struct scic_sds_remote_device *sci_dev,
965         struct scic_sds_request *request)
966 {
967         enum sci_status status = SCI_SUCCESS;
968
969         status = scic_sds_request_complete(request);
970
971         if (status == SCI_SUCCESS) {
972                 status = scic_sds_port_complete_io(
973                                 scic_sds_remote_device_get_port(sci_dev),
974                                 sci_dev, request);
975
976                 if (status == SCI_SUCCESS) {
977                         scic_sds_remote_device_decrement_request_count(sci_dev);
978                 }
979         }
980
981         return status;
982 }
983
984 static enum sci_status scic_sds_stp_remote_device_complete_request(struct scic_sds_remote_device *sci_dev,
985                                                                    struct scic_sds_request *sci_req)
986 {
987         enum sci_status status;
988
989         status = scic_sds_io_request_complete(sci_req);
990         if (status != SCI_SUCCESS)
991                 goto out;
992
993         status = scic_sds_port_complete_io(sci_dev->owning_port, sci_dev, sci_req);
994         if (status != SCI_SUCCESS)
995                 goto out;
996
997         scic_sds_remote_device_decrement_request_count(sci_dev);
998         if (sci_req->sci_status == SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED) {
999                 /* This request causes hardware error, device needs to be Lun Reset.
1000                  * So here we force the state machine to IDLE state so the rest IOs
1001                  * can reach RNC state handler, these IOs will be completed by RNC with
1002                  * status of "DEVICE_RESET_REQUIRED", instead of "INVALID STATE".
1003                  */
1004                 sci_base_state_machine_change_state(&sci_dev->state_machine,
1005                                                     SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET);
1006         } else if (scic_sds_remote_device_get_request_count(sci_dev) == 0)
1007                 sci_base_state_machine_change_state(&sci_dev->state_machine,
1008                                                     SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
1009
1010
1011  out:
1012         if (status != SCI_SUCCESS)
1013                 dev_err(scirdev_to_dev(sci_dev),
1014                         "%s: Port:0x%p Device:0x%p Request:0x%p Status:0x%x "
1015                         "could not complete\n", __func__, sci_dev->owning_port,
1016                         sci_dev, sci_req, status);
1017
1018         return status;
1019 }
1020
1021 /* scic_sds_stp_remote_device_ready_substate_start_request_handler - start stp
1022  * @device: The target device a task management request towards to.
1023  * @request: The task request.
1024  *
1025  * This is the READY NCQ substate handler to start task management request. In
1026  * this routine, we suspend and resume the RNC.  enum sci_status Always return
1027  * SCI_FAILURE_RESET_DEVICE_PARTIAL_SUCCESS status to let
1028  * controller_start_task_handler know that the controller can't post TC for
1029  * task request yet, instead, when RNC gets resumed, a controller_continue_task
1030  * callback will be called.
1031  */
1032 static enum sci_status scic_sds_stp_remote_device_ready_substate_start_request_handler(
1033         struct scic_sds_remote_device *device,
1034         struct scic_sds_request *request)
1035 {
1036         enum sci_status status;
1037
1038         /* Will the port allow the io request to start? */
1039         status = device->owning_port->state_handlers->start_io_handler(
1040                 device->owning_port, device, request);
1041         if (status != SCI_SUCCESS)
1042                 return status;
1043
1044         status = scic_sds_remote_node_context_start_task(&device->rnc, request);
1045         if (status != SCI_SUCCESS)
1046                 goto out;
1047
1048         status = request->state_handlers->start_handler(request);
1049         if (status != SCI_SUCCESS)
1050                 goto out;
1051
1052         /*
1053          * Note: If the remote device state is not IDLE this will replace
1054          * the request that probably resulted in the task management request.
1055          */
1056         device->working_request = request;
1057         sci_base_state_machine_change_state(&device->state_machine,
1058                         SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD);
1059
1060         /*
1061          * The remote node context must cleanup the TCi to NCQ mapping table.
1062          * The only way to do this correctly is to either write to the TLCR
1063          * register or to invalidate and repost the RNC. In either case the
1064          * remote node context state machine will take the correct action when
1065          * the remote node context is suspended and later resumed.
1066          */
1067         scic_sds_remote_node_context_suspend(&device->rnc,
1068                         SCI_SOFTWARE_SUSPENSION, NULL, NULL);
1069         scic_sds_remote_node_context_resume(&device->rnc,
1070                         scic_sds_remote_device_continue_request,
1071                         device);
1072
1073 out:
1074         scic_sds_remote_device_start_request(device, request, status);
1075         /*
1076          * We need to let the controller start request handler know that it can't
1077          * post TC yet. We will provide a callback function to post TC when RNC gets
1078          * resumed.
1079          */
1080         return SCI_FAILURE_RESET_DEVICE_PARTIAL_SUCCESS;
1081 }
1082
1083 /* handle the start io operation for a sata device that is in the command idle
1084  * state. - Evalute the type of IO request to be started - If its an NCQ
1085  * request change to NCQ substate - If its any other command change to the CMD
1086  * substate
1087  *
1088  * If this is a softreset we may want to have a different substate.
1089  */
1090 static enum sci_status scic_sds_stp_remote_device_ready_idle_substate_start_io_handler(
1091         struct scic_sds_remote_device *sci_dev,
1092         struct scic_sds_request *request)
1093 {
1094         enum sci_status status;
1095         struct isci_request *isci_request = request->ireq;
1096         enum scic_sds_remote_device_states new_state;
1097
1098         /* Will the port allow the io request to start? */
1099         status = sci_dev->owning_port->state_handlers->start_io_handler(
1100                         sci_dev->owning_port, sci_dev, request);
1101         if (status != SCI_SUCCESS)
1102                 return status;
1103
1104         status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, request);
1105         if (status != SCI_SUCCESS)
1106                 goto out;
1107
1108         status = request->state_handlers->start_handler(request);
1109         if (status != SCI_SUCCESS)
1110                 goto out;
1111
1112         if (isci_sata_get_sat_protocol(isci_request) == SAT_PROTOCOL_FPDMA)
1113                 new_state = SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ;
1114         else {
1115                 sci_dev->working_request = request;
1116                 new_state = SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD;
1117         }
1118         sci_base_state_machine_change_state(&sci_dev->state_machine, new_state);
1119 out:
1120         scic_sds_remote_device_start_request(sci_dev, request, status);
1121         return status;
1122 }
1123
1124
1125 static enum sci_status scic_sds_stp_remote_device_ready_idle_substate_event_handler(
1126         struct scic_sds_remote_device *sci_dev,
1127         u32 event_code)
1128 {
1129         enum sci_status status;
1130
1131         status = scic_sds_remote_device_general_event_handler(sci_dev, event_code);
1132         if (status != SCI_SUCCESS)
1133                 return status;
1134
1135         /* We pick up suspension events to handle specifically to this state. We
1136          * resume the RNC right away. enum sci_status
1137          */
1138         if (scu_get_event_type(event_code) == SCU_EVENT_TYPE_RNC_SUSPEND_TX ||
1139             scu_get_event_type(event_code) == SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX)
1140                 status = scic_sds_remote_node_context_resume(&sci_dev->rnc, NULL, NULL);
1141
1142         return status;
1143 }
1144
1145 static enum sci_status scic_sds_stp_remote_device_ready_ncq_substate_start_io_handler(
1146         struct scic_sds_remote_device *sci_dev,
1147         struct scic_sds_request *request)
1148 {
1149         enum sci_status status;
1150         struct isci_request *isci_request = request->ireq;
1151         scic_sds_port_io_request_handler_t start_io;
1152
1153         if (isci_sata_get_sat_protocol(isci_request) == SAT_PROTOCOL_FPDMA) {
1154                 start_io = sci_dev->owning_port->state_handlers->start_io_handler;
1155                 status = start_io(sci_dev->owning_port, sci_dev, request);
1156                 if (status != SCI_SUCCESS)
1157                         return status;
1158
1159                 status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, request);
1160                 if (status == SCI_SUCCESS)
1161                         status = request->state_handlers->start_handler(request);
1162
1163                 scic_sds_remote_device_start_request(sci_dev, request, status);
1164         } else
1165                 status = SCI_FAILURE_INVALID_STATE;
1166
1167         return status;
1168 }
1169
1170 static enum sci_status scic_sds_stp_remote_device_ready_ncq_substate_frame_handler(struct scic_sds_remote_device *sci_dev,
1171                                                                                    u32 frame_index)
1172 {
1173         enum sci_status status;
1174         struct sata_fis_header *frame_header;
1175         struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
1176
1177         status = scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
1178                                                                frame_index,
1179                                                                (void **)&frame_header);
1180         if (status != SCI_SUCCESS)
1181                 return status;
1182
1183         if (frame_header->fis_type == SATA_FIS_TYPE_SETDEVBITS &&
1184             (frame_header->status & ATA_STATUS_REG_ERROR_BIT)) {
1185                 sci_dev->not_ready_reason = SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED;
1186
1187                 /* TODO Check sactive and complete associated IO if any. */
1188
1189                 sci_base_state_machine_change_state(&sci_dev->state_machine,
1190                                                     SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR);
1191         } else if (frame_header->fis_type == SATA_FIS_TYPE_REGD2H &&
1192                    (frame_header->status & ATA_STATUS_REG_ERROR_BIT)) {
1193                 /*
1194                  * Some devices return D2H FIS when an NCQ error is detected.
1195                  * Treat this like an SDB error FIS ready reason.
1196                  */
1197                 sci_dev->not_ready_reason = SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED;
1198
1199                 sci_base_state_machine_change_state(&sci_dev->state_machine,
1200                                                     SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR);
1201         } else
1202                 status = SCI_FAILURE;
1203
1204         scic_sds_controller_release_frame(scic, frame_index);
1205
1206         return status;
1207 }
1208
1209 static enum sci_status scic_sds_stp_remote_device_ready_cmd_substate_start_io_handler(
1210         struct scic_sds_remote_device *device,
1211         struct scic_sds_request *request)
1212 {
1213         /* device is already handling a command it can not accept new commands
1214          * until this one is complete.
1215          */
1216         return SCI_FAILURE_INVALID_STATE;
1217 }
1218
1219 static enum sci_status scic_sds_stp_remote_device_ready_cmd_substate_suspend_handler(
1220         struct scic_sds_remote_device *sci_dev,
1221         u32 suspend_type)
1222 {
1223         enum sci_status status;
1224
1225         status = scic_sds_remote_node_context_suspend(&sci_dev->rnc,
1226                                                       suspend_type, NULL, NULL);
1227
1228         return status;
1229 }
1230
1231 static enum sci_status scic_sds_stp_remote_device_ready_cmd_substate_frame_handler(
1232         struct scic_sds_remote_device *sci_dev,
1233         u32 frame_index)
1234 {
1235         /* The device doe not process any UF received from the hardware while
1236          * in this state.  All unsolicited frames are forwarded to the io
1237          * request object.
1238          */
1239         return scic_sds_io_request_frame_handler(sci_dev->working_request,
1240                                                  frame_index);
1241 }
1242
1243 static enum sci_status scic_sds_stp_remote_device_ready_await_reset_substate_start_io_handler(
1244         struct scic_sds_remote_device *device,
1245         struct scic_sds_request *request)
1246 {
1247         return SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED;
1248 }
1249
1250 static enum sci_status scic_sds_stp_remote_device_ready_await_reset_substate_complete_request_handler(
1251         struct scic_sds_remote_device *device,
1252         struct scic_sds_request *request)
1253 {
1254         struct scic_sds_request *sci_req = request;
1255         enum sci_status status;
1256
1257         status = scic_sds_io_request_complete(sci_req);
1258         if (status != SCI_SUCCESS)
1259                 goto out;
1260
1261         status = scic_sds_port_complete_io(device->owning_port, device, sci_req);
1262         if (status != SCI_SUCCESS)
1263                 goto out;
1264
1265         scic_sds_remote_device_decrement_request_count(device);
1266  out:
1267         if (status != SCI_SUCCESS)
1268                 dev_err(scirdev_to_dev(device),
1269                         "%s: Port:0x%p Device:0x%p Request:0x%p Status:0x%x "
1270                         "could not complete\n",
1271                         __func__, device->owning_port, device, sci_req, status);
1272
1273         return status;
1274 }
1275
1276 static void scic_sds_stp_remote_device_ready_idle_substate_resume_complete_handler(void *_dev)
1277 {
1278         struct scic_sds_remote_device *sci_dev = _dev;
1279         struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
1280         struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
1281
1282         /* For NCQ operation we do not issue a isci_remote_device_not_ready().
1283          * As a result, avoid sending the ready notification.
1284          */
1285         if (sci_dev->state_machine.previous_state_id != SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ)
1286                 isci_remote_device_ready(scic->ihost, idev);
1287 }
1288
1289 static enum sci_status scic_sds_smp_remote_device_ready_idle_substate_start_io_handler(
1290         struct scic_sds_remote_device *sci_dev,
1291         struct scic_sds_request *sci_req)
1292 {
1293         enum sci_status status;
1294
1295         /* Will the port allow the io request to start? */
1296         status = sci_dev->owning_port->state_handlers->start_io_handler(
1297                         sci_dev->owning_port, sci_dev, sci_req);
1298         if (status != SCI_SUCCESS)
1299                 return status;
1300
1301         status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, sci_req);
1302         if (status != SCI_SUCCESS)
1303                 goto out;
1304
1305         status = scic_sds_request_start(sci_req);
1306         if (status != SCI_SUCCESS)
1307                 goto out;
1308
1309         sci_dev->working_request = sci_req;
1310         sci_base_state_machine_change_state(&sci_dev->state_machine,
1311                                             SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD);
1312
1313  out:
1314         scic_sds_remote_device_start_request(sci_dev, sci_req, status);
1315
1316         return status;
1317 }
1318
1319 static enum sci_status scic_sds_smp_remote_device_ready_cmd_substate_start_io_handler(
1320         struct scic_sds_remote_device *device,
1321         struct scic_sds_request *request)
1322 {
1323         /* device is already handling a command it can not accept new commands
1324          * until this one is complete.
1325          */
1326         return SCI_FAILURE_INVALID_STATE;
1327 }
1328
1329 static enum sci_status
1330 scic_sds_smp_remote_device_ready_cmd_substate_complete_io_handler(struct scic_sds_remote_device *sci_dev,
1331                                                                   struct scic_sds_request *sci_req)
1332 {
1333         enum sci_status status;
1334
1335         status = scic_sds_io_request_complete(sci_req);
1336         if (status != SCI_SUCCESS)
1337                 return status;
1338
1339         status = scic_sds_port_complete_io(sci_dev->owning_port, sci_dev, sci_req);
1340         if (status != SCI_SUCCESS) {
1341                 dev_err(scirdev_to_dev(sci_dev),
1342                         "%s: SCIC SDS Remote Device 0x%p io request "
1343                         "0x%p could not be completd on the port 0x%p "
1344                         "failed with status %d.\n", __func__, sci_dev, sci_req,
1345                         sci_dev->owning_port, status);
1346                 return status;
1347         }
1348
1349         scic_sds_remote_device_decrement_request_count(sci_dev);
1350         sci_base_state_machine_change_state(&sci_dev->state_machine,
1351                                             SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
1352
1353         return status;
1354 }
1355
1356 static enum sci_status scic_sds_smp_remote_device_ready_cmd_substate_frame_handler(
1357         struct scic_sds_remote_device *sci_dev,
1358         u32 frame_index)
1359 {
1360         enum sci_status status;
1361
1362         /* The device does not process any UF received from the hardware while
1363          * in this state.  All unsolicited frames are forwarded to the io request
1364          * object.
1365          */
1366         status = scic_sds_io_request_frame_handler(
1367                 sci_dev->working_request,
1368                 frame_index
1369                 );
1370
1371         return status;
1372 }
1373
1374 static const struct scic_sds_remote_device_state_handler scic_sds_remote_device_state_handler_table[] = {
1375         [SCI_BASE_REMOTE_DEVICE_STATE_INITIAL] = {
1376                 .start_handler          = scic_sds_remote_device_default_start_handler,
1377                 .stop_handler           = scic_sds_remote_device_default_stop_handler,
1378                 .fail_handler           = scic_sds_remote_device_default_fail_handler,
1379                 .destruct_handler       = scic_sds_remote_device_default_destruct_handler,
1380                 .reset_handler          = scic_sds_remote_device_default_reset_handler,
1381                 .reset_complete_handler = scic_sds_remote_device_default_reset_complete_handler,
1382                 .start_io_handler       = scic_sds_remote_device_default_start_request_handler,
1383                 .complete_io_handler    = scic_sds_remote_device_default_complete_request_handler,
1384                 .continue_io_handler    = scic_sds_remote_device_default_continue_request_handler,
1385                 .start_task_handler     = scic_sds_remote_device_default_start_request_handler,
1386                 .complete_task_handler  = scic_sds_remote_device_default_complete_request_handler,
1387                 .suspend_handler        = scic_sds_remote_device_default_suspend_handler,
1388                 .resume_handler         = scic_sds_remote_device_default_resume_handler,
1389                 .event_handler          = scic_sds_remote_device_default_event_handler,
1390                 .frame_handler          = scic_sds_remote_device_default_frame_handler
1391         },
1392         [SCI_BASE_REMOTE_DEVICE_STATE_STOPPED] = {
1393                 .start_handler          = scic_sds_remote_device_stopped_state_start_handler,
1394                 .stop_handler           = scic_sds_remote_device_stopped_state_stop_handler,
1395                 .fail_handler           = scic_sds_remote_device_default_fail_handler,
1396                 .destruct_handler       = scic_sds_remote_device_stopped_state_destruct_handler,
1397                 .reset_handler          = scic_sds_remote_device_default_reset_handler,
1398                 .reset_complete_handler = scic_sds_remote_device_default_reset_complete_handler,
1399                 .start_io_handler       = scic_sds_remote_device_default_start_request_handler,
1400                 .complete_io_handler    = scic_sds_remote_device_default_complete_request_handler,
1401                 .continue_io_handler    = scic_sds_remote_device_default_continue_request_handler,
1402                 .start_task_handler     = scic_sds_remote_device_default_start_request_handler,
1403                 .complete_task_handler  = scic_sds_remote_device_default_complete_request_handler,
1404                 .suspend_handler        = scic_sds_remote_device_default_suspend_handler,
1405                 .resume_handler         = scic_sds_remote_device_default_resume_handler,
1406                 .event_handler          = scic_sds_remote_device_default_event_handler,
1407                 .frame_handler          = scic_sds_remote_device_default_frame_handler
1408         },
1409         [SCI_BASE_REMOTE_DEVICE_STATE_STARTING] = {
1410                 .start_handler          = scic_sds_remote_device_default_start_handler,
1411                 .stop_handler           = scic_sds_remote_device_starting_state_stop_handler,
1412                 .fail_handler           = scic_sds_remote_device_default_fail_handler,
1413                 .destruct_handler       = scic_sds_remote_device_default_destruct_handler,
1414                 .reset_handler          = scic_sds_remote_device_default_reset_handler,
1415                 .reset_complete_handler = scic_sds_remote_device_default_reset_complete_handler,
1416                 .start_io_handler       = scic_sds_remote_device_default_start_request_handler,
1417                 .complete_io_handler    = scic_sds_remote_device_default_complete_request_handler,
1418                 .continue_io_handler    = scic_sds_remote_device_default_continue_request_handler,
1419                 .start_task_handler     = scic_sds_remote_device_default_start_request_handler,
1420                 .complete_task_handler  = scic_sds_remote_device_default_complete_request_handler,
1421                 .suspend_handler        = scic_sds_remote_device_default_suspend_handler,
1422                 .resume_handler         = scic_sds_remote_device_default_resume_handler,
1423                 .event_handler          = scic_sds_remote_device_general_event_handler,
1424                 .frame_handler          = scic_sds_remote_device_default_frame_handler
1425         },
1426         [SCI_BASE_REMOTE_DEVICE_STATE_READY] = {
1427                 .start_handler          = scic_sds_remote_device_default_start_handler,
1428                 .stop_handler           = scic_sds_remote_device_ready_state_stop_handler,
1429                 .fail_handler           = scic_sds_remote_device_default_fail_handler,
1430                 .destruct_handler       = scic_sds_remote_device_default_destruct_handler,
1431                 .reset_handler          = scic_sds_remote_device_ready_state_reset_handler,
1432                 .reset_complete_handler = scic_sds_remote_device_default_reset_complete_handler,
1433                 .start_io_handler       = scic_sds_remote_device_ready_state_start_io_handler,
1434                 .complete_io_handler    = scic_sds_remote_device_ready_state_complete_request_handler,
1435                 .continue_io_handler    = scic_sds_remote_device_default_continue_request_handler,
1436                 .start_task_handler     = scic_sds_remote_device_ready_state_start_task_handler,
1437                 .complete_task_handler  = scic_sds_remote_device_ready_state_complete_request_handler,
1438                 .suspend_handler        = scic_sds_remote_device_default_suspend_handler,
1439                 .resume_handler         = scic_sds_remote_device_default_resume_handler,
1440                 .event_handler          = scic_sds_remote_device_general_event_handler,
1441                 .frame_handler          = scic_sds_remote_device_general_frame_handler,
1442         },
1443         [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE] = {
1444                 .start_handler          = scic_sds_remote_device_default_start_handler,
1445                 .stop_handler           = scic_sds_remote_device_ready_state_stop_handler,
1446                 .fail_handler           = scic_sds_remote_device_default_fail_handler,
1447                 .destruct_handler       = scic_sds_remote_device_default_destruct_handler,
1448                 .reset_handler          = scic_sds_remote_device_ready_state_reset_handler,
1449                 .reset_complete_handler = scic_sds_remote_device_default_reset_complete_handler,
1450                 .start_io_handler       = scic_sds_stp_remote_device_ready_idle_substate_start_io_handler,
1451                 .complete_io_handler    = scic_sds_remote_device_default_complete_request_handler,
1452                 .continue_io_handler    = scic_sds_remote_device_default_continue_request_handler,
1453                 .start_task_handler     = scic_sds_stp_remote_device_ready_substate_start_request_handler,
1454                 .complete_task_handler  = scic_sds_remote_device_default_complete_request_handler,
1455                 .suspend_handler                = scic_sds_remote_device_default_suspend_handler,
1456                 .resume_handler                 = scic_sds_remote_device_default_resume_handler,
1457                 .event_handler                  = scic_sds_stp_remote_device_ready_idle_substate_event_handler,
1458                 .frame_handler                  = scic_sds_remote_device_default_frame_handler
1459         },
1460         [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD] = {
1461                 .start_handler          = scic_sds_remote_device_default_start_handler,
1462                 .stop_handler           = scic_sds_remote_device_ready_state_stop_handler,
1463                 .fail_handler           = scic_sds_remote_device_default_fail_handler,
1464                 .destruct_handler       = scic_sds_remote_device_default_destruct_handler,
1465                 .reset_handler          = scic_sds_remote_device_ready_state_reset_handler,
1466                 .reset_complete_handler = scic_sds_remote_device_default_reset_complete_handler,
1467                 .start_io_handler       = scic_sds_stp_remote_device_ready_cmd_substate_start_io_handler,
1468                 .complete_io_handler    = scic_sds_stp_remote_device_complete_request,
1469                 .continue_io_handler    = scic_sds_remote_device_default_continue_request_handler,
1470                 .start_task_handler     = scic_sds_stp_remote_device_ready_substate_start_request_handler,
1471                 .complete_task_handler  = scic_sds_stp_remote_device_complete_request,
1472                 .suspend_handler                = scic_sds_stp_remote_device_ready_cmd_substate_suspend_handler,
1473                 .resume_handler                 = scic_sds_remote_device_default_resume_handler,
1474                 .event_handler                  = scic_sds_remote_device_general_event_handler,
1475                 .frame_handler                  = scic_sds_stp_remote_device_ready_cmd_substate_frame_handler
1476         },
1477         [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ] = {
1478                 .start_handler          = scic_sds_remote_device_default_start_handler,
1479                 .stop_handler           = scic_sds_remote_device_ready_state_stop_handler,
1480                 .fail_handler           = scic_sds_remote_device_default_fail_handler,
1481                 .destruct_handler       = scic_sds_remote_device_default_destruct_handler,
1482                 .reset_handler          = scic_sds_remote_device_ready_state_reset_handler,
1483                 .reset_complete_handler = scic_sds_remote_device_default_reset_complete_handler,
1484                 .start_io_handler       = scic_sds_stp_remote_device_ready_ncq_substate_start_io_handler,
1485                 .complete_io_handler    = scic_sds_stp_remote_device_complete_request,
1486                 .continue_io_handler    = scic_sds_remote_device_default_continue_request_handler,
1487                 .start_task_handler     = scic_sds_stp_remote_device_ready_substate_start_request_handler,
1488                 .complete_task_handler  = scic_sds_stp_remote_device_complete_request,
1489                 .suspend_handler                = scic_sds_remote_device_default_suspend_handler,
1490                 .resume_handler                 = scic_sds_remote_device_default_resume_handler,
1491                 .event_handler                  = scic_sds_remote_device_general_event_handler,
1492                 .frame_handler                  = scic_sds_stp_remote_device_ready_ncq_substate_frame_handler
1493         },
1494         [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR] = {
1495                 .start_handler          = scic_sds_remote_device_default_start_handler,
1496                 .stop_handler           = scic_sds_remote_device_ready_state_stop_handler,
1497                 .fail_handler           = scic_sds_remote_device_default_fail_handler,
1498                 .destruct_handler       = scic_sds_remote_device_default_destruct_handler,
1499                 .reset_handler          = scic_sds_remote_device_ready_state_reset_handler,
1500                 .reset_complete_handler = scic_sds_remote_device_default_reset_complete_handler,
1501                 .start_io_handler       = scic_sds_remote_device_default_start_request_handler,
1502                 .complete_io_handler    = scic_sds_stp_remote_device_complete_request,
1503                 .continue_io_handler    = scic_sds_remote_device_default_continue_request_handler,
1504                 .start_task_handler     = scic_sds_stp_remote_device_ready_substate_start_request_handler,
1505                 .complete_task_handler  = scic_sds_stp_remote_device_complete_request,
1506                 .suspend_handler                = scic_sds_remote_device_default_suspend_handler,
1507                 .resume_handler                 = scic_sds_remote_device_default_resume_handler,
1508                 .event_handler                  = scic_sds_remote_device_general_event_handler,
1509                 .frame_handler                  = scic_sds_remote_device_general_frame_handler
1510         },
1511         [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET] = {
1512                 .start_handler          = scic_sds_remote_device_default_start_handler,
1513                 .stop_handler           = scic_sds_remote_device_ready_state_stop_handler,
1514                 .fail_handler           = scic_sds_remote_device_default_fail_handler,
1515                 .destruct_handler       = scic_sds_remote_device_default_destruct_handler,
1516                 .reset_handler          = scic_sds_remote_device_ready_state_reset_handler,
1517                 .reset_complete_handler = scic_sds_remote_device_default_reset_complete_handler,
1518                 .start_io_handler       = scic_sds_stp_remote_device_ready_await_reset_substate_start_io_handler,
1519                 .complete_io_handler    = scic_sds_stp_remote_device_ready_await_reset_substate_complete_request_handler,
1520                 .continue_io_handler    = scic_sds_remote_device_default_continue_request_handler,
1521                 .start_task_handler     = scic_sds_stp_remote_device_ready_substate_start_request_handler,
1522                 .complete_task_handler  = scic_sds_stp_remote_device_complete_request,
1523                 .suspend_handler                = scic_sds_remote_device_default_suspend_handler,
1524                 .resume_handler                 = scic_sds_remote_device_default_resume_handler,
1525                 .event_handler                  = scic_sds_remote_device_general_event_handler,
1526                 .frame_handler                  = scic_sds_remote_device_general_frame_handler
1527         },
1528         [SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE] = {
1529                 .start_handler          = scic_sds_remote_device_default_start_handler,
1530                 .stop_handler           = scic_sds_remote_device_ready_state_stop_handler,
1531                 .fail_handler           = scic_sds_remote_device_default_fail_handler,
1532                 .destruct_handler       = scic_sds_remote_device_default_destruct_handler,
1533                 .reset_handler          = scic_sds_remote_device_default_reset_handler,
1534                 .reset_complete_handler = scic_sds_remote_device_default_reset_complete_handler,
1535                 .start_io_handler       = scic_sds_smp_remote_device_ready_idle_substate_start_io_handler,
1536                 .complete_io_handler    = scic_sds_remote_device_default_complete_request_handler,
1537                 .continue_io_handler    = scic_sds_remote_device_default_continue_request_handler,
1538                 .start_task_handler     = scic_sds_remote_device_default_start_request_handler,
1539                 .complete_task_handler  = scic_sds_remote_device_default_complete_request_handler,
1540                 .suspend_handler        = scic_sds_remote_device_default_suspend_handler,
1541                 .resume_handler         = scic_sds_remote_device_default_resume_handler,
1542                 .event_handler          = scic_sds_remote_device_general_event_handler,
1543                 .frame_handler          = scic_sds_remote_device_default_frame_handler
1544         },
1545         [SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD] = {
1546                 .start_handler          = scic_sds_remote_device_default_start_handler,
1547                 .stop_handler           = scic_sds_remote_device_ready_state_stop_handler,
1548                 .fail_handler           = scic_sds_remote_device_default_fail_handler,
1549                 .destruct_handler       = scic_sds_remote_device_default_destruct_handler,
1550                 .reset_handler          = scic_sds_remote_device_default_reset_handler,
1551                 .reset_complete_handler = scic_sds_remote_device_default_reset_complete_handler,
1552                 .start_io_handler       = scic_sds_smp_remote_device_ready_cmd_substate_start_io_handler,
1553                 .complete_io_handler    = scic_sds_smp_remote_device_ready_cmd_substate_complete_io_handler,
1554                 .continue_io_handler    = scic_sds_remote_device_default_continue_request_handler,
1555                 .start_task_handler     = scic_sds_remote_device_default_start_request_handler,
1556                 .complete_task_handler  = scic_sds_remote_device_default_complete_request_handler,
1557                 .suspend_handler        = scic_sds_remote_device_default_suspend_handler,
1558                 .resume_handler         = scic_sds_remote_device_default_resume_handler,
1559                 .event_handler          = scic_sds_remote_device_general_event_handler,
1560                 .frame_handler          = scic_sds_smp_remote_device_ready_cmd_substate_frame_handler
1561         },
1562         [SCI_BASE_REMOTE_DEVICE_STATE_STOPPING] = {
1563                 .start_handler          = scic_sds_remote_device_default_start_handler,
1564                 .stop_handler           = scic_sds_remote_device_stopping_state_stop_handler,
1565                 .fail_handler           = scic_sds_remote_device_default_fail_handler,
1566                 .destruct_handler       = scic_sds_remote_device_default_destruct_handler,
1567                 .reset_handler          = scic_sds_remote_device_default_reset_handler,
1568                 .reset_complete_handler = scic_sds_remote_device_default_reset_complete_handler,
1569                 .start_io_handler       = scic_sds_remote_device_default_start_request_handler,
1570                 .complete_io_handler    = scic_sds_remote_device_stopping_state_complete_request_handler,
1571                 .continue_io_handler    = scic_sds_remote_device_default_continue_request_handler,
1572                 .start_task_handler     = scic_sds_remote_device_default_start_request_handler,
1573                 .complete_task_handler  = scic_sds_remote_device_stopping_state_complete_request_handler,
1574                 .suspend_handler        = scic_sds_remote_device_default_suspend_handler,
1575                 .resume_handler         = scic_sds_remote_device_default_resume_handler,
1576                 .event_handler          = scic_sds_remote_device_general_event_handler,
1577                 .frame_handler          = scic_sds_remote_device_general_frame_handler
1578         },
1579         [SCI_BASE_REMOTE_DEVICE_STATE_FAILED] = {
1580                 .start_handler          = scic_sds_remote_device_default_start_handler,
1581                 .stop_handler           = scic_sds_remote_device_default_stop_handler,
1582                 .fail_handler           = scic_sds_remote_device_default_fail_handler,
1583                 .destruct_handler       = scic_sds_remote_device_default_destruct_handler,
1584                 .reset_handler          = scic_sds_remote_device_default_reset_handler,
1585                 .reset_complete_handler = scic_sds_remote_device_default_reset_complete_handler,
1586                 .start_io_handler       = scic_sds_remote_device_default_start_request_handler,
1587                 .complete_io_handler    = scic_sds_remote_device_default_complete_request_handler,
1588                 .continue_io_handler    = scic_sds_remote_device_default_continue_request_handler,
1589                 .start_task_handler     = scic_sds_remote_device_default_start_request_handler,
1590                 .complete_task_handler  = scic_sds_remote_device_default_complete_request_handler,
1591                 .suspend_handler        = scic_sds_remote_device_default_suspend_handler,
1592                 .resume_handler         = scic_sds_remote_device_default_resume_handler,
1593                 .event_handler          = scic_sds_remote_device_default_event_handler,
1594                 .frame_handler          = scic_sds_remote_device_general_frame_handler
1595         },
1596         [SCI_BASE_REMOTE_DEVICE_STATE_RESETTING] = {
1597                 .start_handler          = scic_sds_remote_device_default_start_handler,
1598                 .stop_handler           = scic_sds_remote_device_resetting_state_stop_handler,
1599                 .fail_handler           = scic_sds_remote_device_default_fail_handler,
1600                 .destruct_handler       = scic_sds_remote_device_default_destruct_handler,
1601                 .reset_handler          = scic_sds_remote_device_default_reset_handler,
1602                 .reset_complete_handler = scic_sds_remote_device_resetting_state_reset_complete_handler,
1603                 .start_io_handler       = scic_sds_remote_device_default_start_request_handler,
1604                 .complete_io_handler    = scic_sds_remote_device_resetting_state_complete_request_handler,
1605                 .continue_io_handler    = scic_sds_remote_device_default_continue_request_handler,
1606                 .start_task_handler     = scic_sds_remote_device_default_start_request_handler,
1607                 .complete_task_handler  = scic_sds_remote_device_resetting_state_complete_request_handler,
1608                 .suspend_handler        = scic_sds_remote_device_default_suspend_handler,
1609                 .resume_handler         = scic_sds_remote_device_default_resume_handler,
1610                 .event_handler          = scic_sds_remote_device_default_event_handler,
1611                 .frame_handler          = scic_sds_remote_device_general_frame_handler
1612         },
1613         [SCI_BASE_REMOTE_DEVICE_STATE_FINAL] = {
1614                 .start_handler          = scic_sds_remote_device_default_start_handler,
1615                 .stop_handler           = scic_sds_remote_device_default_stop_handler,
1616                 .fail_handler           = scic_sds_remote_device_default_fail_handler,
1617                 .destruct_handler       = scic_sds_remote_device_default_destruct_handler,
1618                 .reset_handler          = scic_sds_remote_device_default_reset_handler,
1619                 .reset_complete_handler = scic_sds_remote_device_default_reset_complete_handler,
1620                 .start_io_handler       = scic_sds_remote_device_default_start_request_handler,
1621                 .complete_io_handler    = scic_sds_remote_device_default_complete_request_handler,
1622                 .continue_io_handler    = scic_sds_remote_device_default_continue_request_handler,
1623                 .start_task_handler     = scic_sds_remote_device_default_start_request_handler,
1624                 .complete_task_handler  = scic_sds_remote_device_default_complete_request_handler,
1625                 .suspend_handler        = scic_sds_remote_device_default_suspend_handler,
1626                 .resume_handler         = scic_sds_remote_device_default_resume_handler,
1627                 .event_handler          = scic_sds_remote_device_default_event_handler,
1628                 .frame_handler          = scic_sds_remote_device_default_frame_handler
1629         }
1630 };
1631
1632 static void scic_sds_remote_device_initial_state_enter(void *object)
1633 {
1634         struct scic_sds_remote_device *sci_dev = object;
1635
1636         SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1637                           SCI_BASE_REMOTE_DEVICE_STATE_INITIAL);
1638
1639         /* Initial state is a transitional state to the stopped state */
1640         sci_base_state_machine_change_state(&sci_dev->state_machine,
1641                                             SCI_BASE_REMOTE_DEVICE_STATE_STOPPED);
1642 }
1643
1644 /**
1645  * scic_remote_device_destruct() - free remote node context and destruct
1646  * @remote_device: This parameter specifies the remote device to be destructed.
1647  *
1648  * Remote device objects are a limited resource.  As such, they must be
1649  * protected.  Thus calls to construct and destruct are mutually exclusive and
1650  * non-reentrant. The return value shall indicate if the device was
1651  * successfully destructed or if some failure occurred. enum sci_status This value
1652  * is returned if the device is successfully destructed.
1653  * SCI_FAILURE_INVALID_REMOTE_DEVICE This value is returned if the supplied
1654  * device isn't valid (e.g. it's already been destoryed, the handle isn't
1655  * valid, etc.).
1656  */
1657 static enum sci_status scic_remote_device_destruct(struct scic_sds_remote_device *sci_dev)
1658 {
1659         return sci_dev->state_handlers->destruct_handler(sci_dev);
1660 }
1661
1662 /**
1663  * isci_remote_device_deconstruct() - This function frees an isci_remote_device.
1664  * @ihost: This parameter specifies the isci host object.
1665  * @idev: This parameter specifies the remote device to be freed.
1666  *
1667  */
1668 static void isci_remote_device_deconstruct(struct isci_host *ihost, struct isci_remote_device *idev)
1669 {
1670         dev_dbg(&ihost->pdev->dev,
1671                 "%s: isci_device = %p\n", __func__, idev);
1672
1673         /* There should not be any outstanding io's. All paths to
1674          * here should go through isci_remote_device_nuke_requests.
1675          * If we hit this condition, we will need a way to complete
1676          * io requests in process */
1677         while (!list_empty(&idev->reqs_in_process)) {
1678
1679                 dev_err(&ihost->pdev->dev,
1680                         "%s: ** request list not empty! **\n", __func__);
1681                 BUG();
1682         }
1683
1684         scic_remote_device_destruct(&idev->sci);
1685         idev->domain_dev->lldd_dev = NULL;
1686         idev->domain_dev = NULL;
1687         idev->isci_port = NULL;
1688         list_del_init(&idev->node);
1689
1690         clear_bit(IDEV_START_PENDING, &idev->flags);
1691         clear_bit(IDEV_STOP_PENDING, &idev->flags);
1692         wake_up(&ihost->eventq);
1693 }
1694
1695 /**
1696  * isci_remote_device_stop_complete() - This function is called by the scic
1697  *    when the remote device stop has completed. We mark the isci device as not
1698  *    ready and remove the isci remote device.
1699  * @ihost: This parameter specifies the isci host object.
1700  * @idev: This parameter specifies the remote device.
1701  * @status: This parameter specifies status of the completion.
1702  *
1703  */
1704 static void isci_remote_device_stop_complete(struct isci_host *ihost,
1705                                              struct isci_remote_device *idev)
1706 {
1707         dev_dbg(&ihost->pdev->dev, "%s: complete idev = %p\n", __func__, idev);
1708
1709         isci_remote_device_change_state(idev, isci_stopped);
1710
1711         /* after stop, we can tear down resources. */
1712         isci_remote_device_deconstruct(ihost, idev);
1713 }
1714
1715 static void scic_sds_remote_device_stopped_state_enter(void *object)
1716 {
1717         struct scic_sds_remote_device *sci_dev = object;
1718         struct scic_sds_controller *scic;
1719         struct isci_remote_device *idev;
1720         struct isci_host *ihost;
1721         u32 prev_state;
1722
1723         scic = scic_sds_remote_device_get_controller(sci_dev);
1724         ihost = scic->ihost;
1725         idev = sci_dev_to_idev(sci_dev);
1726
1727         SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1728                           SCI_BASE_REMOTE_DEVICE_STATE_STOPPED);
1729
1730         /* If we are entering from the stopping state let the SCI User know that
1731          * the stop operation has completed.
1732          */
1733         prev_state = sci_dev->state_machine.previous_state_id;
1734         if (prev_state == SCI_BASE_REMOTE_DEVICE_STATE_STOPPING)
1735                 isci_remote_device_stop_complete(ihost, idev);
1736
1737         scic_sds_controller_remote_device_stopped(scic, sci_dev);
1738 }
1739
1740 static void scic_sds_remote_device_starting_state_enter(void *object)
1741 {
1742         struct scic_sds_remote_device *sci_dev = object;
1743         struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1744         struct isci_host *ihost = scic->ihost;
1745         struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
1746
1747         SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1748                           SCI_BASE_REMOTE_DEVICE_STATE_STARTING);
1749
1750         isci_remote_device_not_ready(ihost, idev,
1751                                      SCIC_REMOTE_DEVICE_NOT_READY_START_REQUESTED);
1752 }
1753
1754 static void scic_sds_remote_device_ready_state_enter(void *object)
1755 {
1756         struct scic_sds_remote_device *sci_dev = object;
1757         struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
1758         struct domain_device *dev = sci_dev_to_domain(sci_dev);
1759
1760         SET_STATE_HANDLER(sci_dev,
1761                           scic_sds_remote_device_state_handler_table,
1762                           SCI_BASE_REMOTE_DEVICE_STATE_READY);
1763
1764         scic->remote_device_sequence[sci_dev->rnc.remote_node_index]++;
1765
1766         if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_SATA)) {
1767                 sci_base_state_machine_change_state(&sci_dev->state_machine,
1768                                                     SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
1769         } else if (dev_is_expander(dev)) {
1770                 sci_base_state_machine_change_state(&sci_dev->state_machine,
1771                                                     SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
1772         } else
1773                 isci_remote_device_ready(scic->ihost, sci_dev_to_idev(sci_dev));
1774 }
1775
1776 static void scic_sds_remote_device_ready_state_exit(void *object)
1777 {
1778         struct scic_sds_remote_device *sci_dev = object;
1779         struct domain_device *dev = sci_dev_to_domain(sci_dev);
1780
1781         if (dev->dev_type == SAS_END_DEV) {
1782                 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
1783                 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
1784
1785                 isci_remote_device_not_ready(scic->ihost, idev,
1786                                              SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED);
1787         }
1788 }
1789
1790 static void scic_sds_remote_device_stopping_state_enter(void *object)
1791 {
1792         struct scic_sds_remote_device *sci_dev = object;
1793
1794         SET_STATE_HANDLER(
1795                 sci_dev,
1796                 scic_sds_remote_device_state_handler_table,
1797                 SCI_BASE_REMOTE_DEVICE_STATE_STOPPING
1798                 );
1799 }
1800
1801 static void scic_sds_remote_device_failed_state_enter(void *object)
1802 {
1803         struct scic_sds_remote_device *sci_dev = object;
1804
1805         SET_STATE_HANDLER(
1806                 sci_dev,
1807                 scic_sds_remote_device_state_handler_table,
1808                 SCI_BASE_REMOTE_DEVICE_STATE_FAILED
1809                 );
1810 }
1811
1812 static void scic_sds_remote_device_resetting_state_enter(void *object)
1813 {
1814         struct scic_sds_remote_device *sci_dev = object;
1815
1816         SET_STATE_HANDLER(
1817                 sci_dev,
1818                 scic_sds_remote_device_state_handler_table,
1819                 SCI_BASE_REMOTE_DEVICE_STATE_RESETTING
1820                 );
1821
1822         scic_sds_remote_node_context_suspend(
1823                 &sci_dev->rnc, SCI_SOFTWARE_SUSPENSION, NULL, NULL);
1824 }
1825
1826 static void scic_sds_remote_device_resetting_state_exit(void *object)
1827 {
1828         struct scic_sds_remote_device *sci_dev = object;
1829
1830         scic_sds_remote_node_context_resume(&sci_dev->rnc, NULL, NULL);
1831 }
1832
1833 static void scic_sds_remote_device_final_state_enter(void *object)
1834 {
1835         struct scic_sds_remote_device *sci_dev = object;
1836
1837         SET_STATE_HANDLER(
1838                 sci_dev,
1839                 scic_sds_remote_device_state_handler_table,
1840                 SCI_BASE_REMOTE_DEVICE_STATE_FINAL
1841                 );
1842 }
1843
1844 static void scic_sds_stp_remote_device_ready_idle_substate_enter(void *object)
1845 {
1846         struct scic_sds_remote_device *sci_dev = object;
1847
1848         SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1849                           SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
1850
1851         sci_dev->working_request = NULL;
1852         if (scic_sds_remote_node_context_is_ready(&sci_dev->rnc)) {
1853                 /*
1854                  * Since the RNC is ready, it's alright to finish completion
1855                  * processing (e.g. signal the remote device is ready). */
1856                 scic_sds_stp_remote_device_ready_idle_substate_resume_complete_handler(sci_dev);
1857         } else {
1858                 scic_sds_remote_node_context_resume(&sci_dev->rnc,
1859                         scic_sds_stp_remote_device_ready_idle_substate_resume_complete_handler,
1860                         sci_dev);
1861         }
1862 }
1863
1864 static void scic_sds_stp_remote_device_ready_cmd_substate_enter(void *object)
1865 {
1866         struct scic_sds_remote_device *sci_dev = object;
1867         struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1868
1869         BUG_ON(sci_dev->working_request == NULL);
1870
1871         SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1872                           SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD);
1873
1874         isci_remote_device_not_ready(scic->ihost, sci_dev_to_idev(sci_dev),
1875                                      SCIC_REMOTE_DEVICE_NOT_READY_SATA_REQUEST_STARTED);
1876 }
1877
1878 static void scic_sds_stp_remote_device_ready_ncq_substate_enter(void *object)
1879 {
1880         struct scic_sds_remote_device *sci_dev = object;
1881
1882         SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1883                           SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ);
1884 }
1885
1886 static void scic_sds_stp_remote_device_ready_ncq_error_substate_enter(void *object)
1887 {
1888         struct scic_sds_remote_device *sci_dev = object;
1889         struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1890         struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
1891
1892         SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1893                           SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR);
1894
1895         if (sci_dev->not_ready_reason == SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED)
1896                 isci_remote_device_not_ready(scic->ihost, idev,
1897                                              sci_dev->not_ready_reason);
1898 }
1899
1900 static void scic_sds_stp_remote_device_ready_await_reset_substate_enter(void *object)
1901 {
1902         struct scic_sds_remote_device *sci_dev = object;
1903
1904         SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1905                 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET);
1906 }
1907
1908 static void scic_sds_smp_remote_device_ready_idle_substate_enter(void *object)
1909 {
1910         struct scic_sds_remote_device *sci_dev = object;
1911         struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1912
1913         SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1914                           SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
1915
1916         isci_remote_device_ready(scic->ihost, sci_dev_to_idev(sci_dev));
1917 }
1918
1919 static void scic_sds_smp_remote_device_ready_cmd_substate_enter(void *object)
1920 {
1921         struct scic_sds_remote_device *sci_dev = object;
1922         struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1923
1924         BUG_ON(sci_dev->working_request == NULL);
1925
1926         SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1927                           SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD);
1928
1929         isci_remote_device_not_ready(scic->ihost, sci_dev_to_idev(sci_dev),
1930                                      SCIC_REMOTE_DEVICE_NOT_READY_SMP_REQUEST_STARTED);
1931 }
1932
1933 static void scic_sds_smp_remote_device_ready_cmd_substate_exit(void *object)
1934 {
1935         struct scic_sds_remote_device *sci_dev = object;
1936
1937         sci_dev->working_request = NULL;
1938 }
1939
1940 static const struct sci_base_state scic_sds_remote_device_state_table[] = {
1941         [SCI_BASE_REMOTE_DEVICE_STATE_INITIAL] = {
1942                 .enter_state = scic_sds_remote_device_initial_state_enter,
1943         },
1944         [SCI_BASE_REMOTE_DEVICE_STATE_STOPPED] = {
1945                 .enter_state = scic_sds_remote_device_stopped_state_enter,
1946         },
1947         [SCI_BASE_REMOTE_DEVICE_STATE_STARTING] = {
1948                 .enter_state = scic_sds_remote_device_starting_state_enter,
1949         },
1950         [SCI_BASE_REMOTE_DEVICE_STATE_READY] = {
1951                 .enter_state = scic_sds_remote_device_ready_state_enter,
1952                 .exit_state  = scic_sds_remote_device_ready_state_exit
1953         },
1954         [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE] = {
1955                 .enter_state = scic_sds_stp_remote_device_ready_idle_substate_enter,
1956         },
1957         [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD] = {
1958                 .enter_state = scic_sds_stp_remote_device_ready_cmd_substate_enter,
1959         },
1960         [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ] = {
1961                 .enter_state = scic_sds_stp_remote_device_ready_ncq_substate_enter,
1962         },
1963         [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR] = {
1964                 .enter_state = scic_sds_stp_remote_device_ready_ncq_error_substate_enter,
1965         },
1966         [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET] = {
1967                 .enter_state = scic_sds_stp_remote_device_ready_await_reset_substate_enter,
1968         },
1969         [SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE] = {
1970                 .enter_state = scic_sds_smp_remote_device_ready_idle_substate_enter,
1971         },
1972         [SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD] = {
1973                 .enter_state = scic_sds_smp_remote_device_ready_cmd_substate_enter,
1974                 .exit_state  = scic_sds_smp_remote_device_ready_cmd_substate_exit,
1975         },
1976         [SCI_BASE_REMOTE_DEVICE_STATE_STOPPING] = {
1977                 .enter_state = scic_sds_remote_device_stopping_state_enter,
1978         },
1979         [SCI_BASE_REMOTE_DEVICE_STATE_FAILED] = {
1980                 .enter_state = scic_sds_remote_device_failed_state_enter,
1981         },
1982         [SCI_BASE_REMOTE_DEVICE_STATE_RESETTING] = {
1983                 .enter_state = scic_sds_remote_device_resetting_state_enter,
1984                 .exit_state  = scic_sds_remote_device_resetting_state_exit
1985         },
1986         [SCI_BASE_REMOTE_DEVICE_STATE_FINAL] = {
1987                 .enter_state = scic_sds_remote_device_final_state_enter,
1988         },
1989 };
1990
1991 /**
1992  * scic_remote_device_construct() - common construction
1993  * @sci_port: SAS/SATA port through which this device is accessed.
1994  * @sci_dev: remote device to construct
1995  *
1996  * This routine just performs benign initialization and does not
1997  * allocate the remote_node_context which is left to
1998  * scic_remote_device_[de]a_construct().  scic_remote_device_destruct()
1999  * frees the remote_node_context(s) for the device.
2000  */
2001 static void scic_remote_device_construct(struct scic_sds_port *sci_port,
2002                                   struct scic_sds_remote_device *sci_dev)
2003 {
2004         sci_dev->owning_port = sci_port;
2005         sci_dev->started_request_count = 0;
2006
2007         sci_base_state_machine_construct(
2008                 &sci_dev->state_machine,
2009                 sci_dev,
2010                 scic_sds_remote_device_state_table,
2011                 SCI_BASE_REMOTE_DEVICE_STATE_INITIAL
2012                 );
2013
2014         sci_base_state_machine_start(
2015                 &sci_dev->state_machine
2016                 );
2017
2018         scic_sds_remote_node_context_construct(&sci_dev->rnc,
2019                                                SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX);
2020 }
2021
2022 /**
2023  * scic_remote_device_da_construct() - construct direct attached device.
2024  *
2025  * The information (e.g. IAF, Signature FIS, etc.) necessary to build
2026  * the device is known to the SCI Core since it is contained in the
2027  * scic_phy object.  Remote node context(s) is/are a global resource
2028  * allocated by this routine, freed by scic_remote_device_destruct().
2029  *
2030  * Returns:
2031  * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
2032  * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
2033  * sata-only controller instance.
2034  * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
2035  */
2036 static enum sci_status scic_remote_device_da_construct(struct scic_sds_port *sci_port,
2037                                                        struct scic_sds_remote_device *sci_dev)
2038 {
2039         enum sci_status status;
2040         struct domain_device *dev = sci_dev_to_domain(sci_dev);
2041
2042         scic_remote_device_construct(sci_port, sci_dev);
2043
2044         /*
2045          * This information is request to determine how many remote node context
2046          * entries will be needed to store the remote node.
2047          */
2048         sci_dev->is_direct_attached = true;
2049         status = scic_sds_controller_allocate_remote_node_context(sci_port->owning_controller,
2050                                                                   sci_dev,
2051                                                                   &sci_dev->rnc.remote_node_index);
2052
2053         if (status != SCI_SUCCESS)
2054                 return status;
2055
2056         if (dev->dev_type == SAS_END_DEV || dev->dev_type == SATA_DEV ||
2057             (dev->tproto & SAS_PROTOCOL_STP) || dev_is_expander(dev))
2058                 /* pass */;
2059         else
2060                 return SCI_FAILURE_UNSUPPORTED_PROTOCOL;
2061
2062         sci_dev->connection_rate = scic_sds_port_get_max_allowed_speed(sci_port);
2063
2064         /* / @todo Should I assign the port width by reading all of the phys on the port? */
2065         sci_dev->device_port_width = 1;
2066
2067         return SCI_SUCCESS;
2068 }
2069
2070 /**
2071  * scic_remote_device_ea_construct() - construct expander attached device
2072  *
2073  * Remote node context(s) is/are a global resource allocated by this
2074  * routine, freed by scic_remote_device_destruct().
2075  *
2076  * Returns:
2077  * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
2078  * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
2079  * sata-only controller instance.
2080  * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
2081  */
2082 static enum sci_status scic_remote_device_ea_construct(struct scic_sds_port *sci_port,
2083                                                        struct scic_sds_remote_device *sci_dev)
2084 {
2085         struct domain_device *dev = sci_dev_to_domain(sci_dev);
2086         enum sci_status status;
2087
2088         scic_remote_device_construct(sci_port, sci_dev);
2089
2090         status = scic_sds_controller_allocate_remote_node_context(sci_port->owning_controller,
2091                                                                   sci_dev,
2092                                                                   &sci_dev->rnc.remote_node_index);
2093         if (status != SCI_SUCCESS)
2094                 return status;
2095
2096         if (dev->dev_type == SAS_END_DEV || dev->dev_type == SATA_DEV ||
2097             (dev->tproto & SAS_PROTOCOL_STP) || dev_is_expander(dev))
2098                 /* pass */;
2099         else
2100                 return SCI_FAILURE_UNSUPPORTED_PROTOCOL;
2101
2102         /*
2103          * For SAS-2 the physical link rate is actually a logical link
2104          * rate that incorporates multiplexing.  The SCU doesn't
2105          * incorporate multiplexing and for the purposes of the
2106          * connection the logical link rate is that same as the
2107          * physical.  Furthermore, the SAS-2 and SAS-1.1 fields overlay
2108          * one another, so this code works for both situations. */
2109         sci_dev->connection_rate = min_t(u16, scic_sds_port_get_max_allowed_speed(sci_port),
2110                                          dev->linkrate);
2111
2112         /* / @todo Should I assign the port width by reading all of the phys on the port? */
2113         sci_dev->device_port_width = 1;
2114
2115         return SCI_SUCCESS;
2116 }
2117
2118 /**
2119  * scic_remote_device_start() - This method will start the supplied remote
2120  *    device.  This method enables normal IO requests to flow through to the
2121  *    remote device.
2122  * @remote_device: This parameter specifies the device to be started.
2123  * @timeout: This parameter specifies the number of milliseconds in which the
2124  *    start operation should complete.
2125  *
2126  * An indication of whether the device was successfully started. SCI_SUCCESS
2127  * This value is returned if the device was successfully started.
2128  * SCI_FAILURE_INVALID_PHY This value is returned if the user attempts to start
2129  * the device when there have been no phys added to it.
2130  */
2131 static enum sci_status scic_remote_device_start(struct scic_sds_remote_device *sci_dev,
2132                                          u32 timeout)
2133 {
2134         return sci_dev->state_handlers->start_handler(sci_dev);
2135 }
2136
2137 static enum sci_status isci_remote_device_construct(struct isci_port *iport,
2138                                                     struct isci_remote_device *idev)
2139 {
2140         struct scic_sds_port *sci_port = iport->sci_port_handle;
2141         struct isci_host *ihost = iport->isci_host;
2142         struct domain_device *dev = idev->domain_dev;
2143         enum sci_status status;
2144
2145         if (dev->parent && dev_is_expander(dev->parent))
2146                 status = scic_remote_device_ea_construct(sci_port, &idev->sci);
2147         else
2148                 status = scic_remote_device_da_construct(sci_port, &idev->sci);
2149
2150         if (status != SCI_SUCCESS) {
2151                 dev_dbg(&ihost->pdev->dev, "%s: construct failed: %d\n",
2152                         __func__, status);
2153
2154                 return status;
2155         }
2156
2157         /* start the device. */
2158         status = scic_remote_device_start(&idev->sci, ISCI_REMOTE_DEVICE_START_TIMEOUT);
2159
2160         if (status != SCI_SUCCESS)
2161                 dev_warn(&ihost->pdev->dev, "remote device start failed: %d\n",
2162                          status);
2163
2164         return status;
2165 }
2166
2167 void isci_remote_device_nuke_requests(struct isci_host *ihost, struct isci_remote_device *idev)
2168 {
2169         DECLARE_COMPLETION_ONSTACK(aborted_task_completion);
2170
2171         dev_dbg(&ihost->pdev->dev,
2172                 "%s: idev = %p\n", __func__, idev);
2173
2174         /* Cleanup all requests pending for this device. */
2175         isci_terminate_pending_requests(ihost, idev, terminating);
2176
2177         dev_dbg(&ihost->pdev->dev,
2178                 "%s: idev = %p, done\n", __func__, idev);
2179 }
2180
2181 /**
2182  * This function builds the isci_remote_device when a libsas dev_found message
2183  *    is received.
2184  * @isci_host: This parameter specifies the isci host object.
2185  * @port: This parameter specifies the isci_port conected to this device.
2186  *
2187  * pointer to new isci_remote_device.
2188  */
2189 static struct isci_remote_device *
2190 isci_remote_device_alloc(struct isci_host *ihost, struct isci_port *iport)
2191 {
2192         struct isci_remote_device *idev;
2193         int i;
2194
2195         for (i = 0; i < SCI_MAX_REMOTE_DEVICES; i++) {
2196                 idev = &ihost->devices[i];
2197                 if (!test_and_set_bit(IDEV_ALLOCATED, &idev->flags))
2198                         break;
2199         }
2200
2201         if (i >= SCI_MAX_REMOTE_DEVICES) {
2202                 dev_warn(&ihost->pdev->dev, "%s: failed\n", __func__);
2203                 return NULL;
2204         }
2205
2206         if (WARN_ONCE(!list_empty(&idev->reqs_in_process), "found requests in process\n"))
2207                 return NULL;
2208
2209         if (WARN_ONCE(!list_empty(&idev->node), "found non-idle remote device\n"))
2210                 return NULL;
2211
2212         isci_remote_device_change_state(idev, isci_freed);
2213
2214         return idev;
2215 }
2216
2217 /**
2218  * isci_remote_device_stop() - This function is called internally to stop the
2219  *    remote device.
2220  * @isci_host: This parameter specifies the isci host object.
2221  * @isci_device: This parameter specifies the remote device.
2222  *
2223  * The status of the scic request to stop.
2224  */
2225 enum sci_status isci_remote_device_stop(struct isci_host *ihost, struct isci_remote_device *idev)
2226 {
2227         enum sci_status status;
2228         unsigned long flags;
2229
2230         dev_dbg(&ihost->pdev->dev,
2231                 "%s: isci_device = %p\n", __func__, idev);
2232
2233         isci_remote_device_change_state(idev, isci_stopping);
2234
2235         /* Kill all outstanding requests. */
2236         isci_remote_device_nuke_requests(ihost, idev);
2237
2238         set_bit(IDEV_STOP_PENDING, &idev->flags);
2239
2240         spin_lock_irqsave(&ihost->scic_lock, flags);
2241         status = scic_remote_device_stop(&idev->sci, 50);
2242         spin_unlock_irqrestore(&ihost->scic_lock, flags);
2243
2244         /* Wait for the stop complete callback. */
2245         if (status == SCI_SUCCESS) {
2246                 wait_for_device_stop(ihost, idev);
2247                 clear_bit(IDEV_ALLOCATED, &idev->flags);
2248         }
2249
2250         dev_dbg(&ihost->pdev->dev,
2251                 "%s: idev = %p - after completion wait\n",
2252                 __func__, idev);
2253
2254         return status;
2255 }
2256
2257 /**
2258  * isci_remote_device_gone() - This function is called by libsas when a domain
2259  *    device is removed.
2260  * @domain_device: This parameter specifies the libsas domain device.
2261  *
2262  */
2263 void isci_remote_device_gone(struct domain_device *dev)
2264 {
2265         struct isci_host *ihost = dev_to_ihost(dev);
2266         struct isci_remote_device *idev = dev->lldd_dev;
2267
2268         dev_dbg(&ihost->pdev->dev,
2269                 "%s: domain_device = %p, isci_device = %p, isci_port = %p\n",
2270                 __func__, dev, idev, idev->isci_port);
2271
2272         isci_remote_device_stop(ihost, idev);
2273 }
2274
2275
2276 /**
2277  * isci_remote_device_found() - This function is called by libsas when a remote
2278  *    device is discovered. A remote device object is created and started. the
2279  *    function then sleeps until the sci core device started message is
2280  *    received.
2281  * @domain_device: This parameter specifies the libsas domain device.
2282  *
2283  * status, zero indicates success.
2284  */
2285 int isci_remote_device_found(struct domain_device *domain_dev)
2286 {
2287         struct isci_host *isci_host = dev_to_ihost(domain_dev);
2288         struct isci_port *isci_port;
2289         struct isci_phy *isci_phy;
2290         struct asd_sas_port *sas_port;
2291         struct asd_sas_phy *sas_phy;
2292         struct isci_remote_device *isci_device;
2293         enum sci_status status;
2294
2295         dev_dbg(&isci_host->pdev->dev,
2296                 "%s: domain_device = %p\n", __func__, domain_dev);
2297
2298         wait_for_start(isci_host);
2299
2300         sas_port = domain_dev->port;
2301         sas_phy = list_first_entry(&sas_port->phy_list, struct asd_sas_phy,
2302                                    port_phy_el);
2303         isci_phy = to_isci_phy(sas_phy);
2304         isci_port = isci_phy->isci_port;
2305
2306         /* we are being called for a device on this port,
2307          * so it has to come up eventually
2308          */
2309         wait_for_completion(&isci_port->start_complete);
2310
2311         if ((isci_stopping == isci_port_get_state(isci_port)) ||
2312             (isci_stopped == isci_port_get_state(isci_port)))
2313                 return -ENODEV;
2314
2315         isci_device = isci_remote_device_alloc(isci_host, isci_port);
2316         if (!isci_device)
2317                 return -ENODEV;
2318
2319         INIT_LIST_HEAD(&isci_device->node);
2320         domain_dev->lldd_dev = isci_device;
2321         isci_device->domain_dev = domain_dev;
2322         isci_device->isci_port = isci_port;
2323         isci_remote_device_change_state(isci_device, isci_starting);
2324
2325
2326         spin_lock_irq(&isci_host->scic_lock);
2327         list_add_tail(&isci_device->node, &isci_port->remote_dev_list);
2328
2329         set_bit(IDEV_START_PENDING, &isci_device->flags);
2330         status = isci_remote_device_construct(isci_port, isci_device);
2331         spin_unlock_irq(&isci_host->scic_lock);
2332
2333         dev_dbg(&isci_host->pdev->dev,
2334                 "%s: isci_device = %p\n",
2335                 __func__, isci_device);
2336
2337         if (status != SCI_SUCCESS) {
2338
2339                 spin_lock_irq(&isci_host->scic_lock);
2340                 isci_remote_device_deconstruct(
2341                         isci_host,
2342                         isci_device
2343                         );
2344                 spin_unlock_irq(&isci_host->scic_lock);
2345                 return -ENODEV;
2346         }
2347
2348         /* wait for the device ready callback. */
2349         wait_for_device_start(isci_host, isci_device);
2350
2351         return 0;
2352 }
2353 /**
2354  * isci_device_is_reset_pending() - This function will check if there is any
2355  *    pending reset condition on the device.
2356  * @request: This parameter is the isci_device object.
2357  *
2358  * true if there is a reset pending for the device.
2359  */
2360 bool isci_device_is_reset_pending(
2361         struct isci_host *isci_host,
2362         struct isci_remote_device *isci_device)
2363 {
2364         struct isci_request *isci_request;
2365         struct isci_request *tmp_req;
2366         bool reset_is_pending = false;
2367         unsigned long flags;
2368
2369         dev_dbg(&isci_host->pdev->dev,
2370                 "%s: isci_device = %p\n", __func__, isci_device);
2371
2372         spin_lock_irqsave(&isci_host->scic_lock, flags);
2373
2374         /* Check for reset on all pending requests. */
2375         list_for_each_entry_safe(isci_request, tmp_req,
2376                                  &isci_device->reqs_in_process, dev_node) {
2377                 dev_dbg(&isci_host->pdev->dev,
2378                         "%s: isci_device = %p request = %p\n",
2379                         __func__, isci_device, isci_request);
2380
2381                 if (isci_request->ttype == io_task) {
2382                         struct sas_task *task = isci_request_access_task(
2383                                 isci_request);
2384
2385                         spin_lock(&task->task_state_lock);
2386                         if (task->task_state_flags & SAS_TASK_NEED_DEV_RESET)
2387                                 reset_is_pending = true;
2388                         spin_unlock(&task->task_state_lock);
2389                 }
2390         }
2391
2392         spin_unlock_irqrestore(&isci_host->scic_lock, flags);
2393
2394         dev_dbg(&isci_host->pdev->dev,
2395                 "%s: isci_device = %p reset_is_pending = %d\n",
2396                 __func__, isci_device, reset_is_pending);
2397
2398         return reset_is_pending;
2399 }
2400
2401 /**
2402  * isci_device_clear_reset_pending() - This function will clear if any pending
2403  *    reset condition flags on the device.
2404  * @request: This parameter is the isci_device object.
2405  *
2406  * true if there is a reset pending for the device.
2407  */
2408 void isci_device_clear_reset_pending(struct isci_host *ihost, struct isci_remote_device *idev)
2409 {
2410         struct isci_request *isci_request;
2411         struct isci_request *tmp_req;
2412         unsigned long flags = 0;
2413
2414         dev_dbg(&ihost->pdev->dev, "%s: idev=%p, ihost=%p\n",
2415                  __func__, idev, ihost);
2416
2417         spin_lock_irqsave(&ihost->scic_lock, flags);
2418
2419         /* Clear reset pending on all pending requests. */
2420         list_for_each_entry_safe(isci_request, tmp_req,
2421                                  &idev->reqs_in_process, dev_node) {
2422                 dev_dbg(&ihost->pdev->dev, "%s: idev = %p request = %p\n",
2423                          __func__, idev, isci_request);
2424
2425                 if (isci_request->ttype == io_task) {
2426
2427                         unsigned long flags2;
2428                         struct sas_task *task = isci_request_access_task(
2429                                 isci_request);
2430
2431                         spin_lock_irqsave(&task->task_state_lock, flags2);
2432                         task->task_state_flags &= ~SAS_TASK_NEED_DEV_RESET;
2433                         spin_unlock_irqrestore(&task->task_state_lock, flags2);
2434                 }
2435         }
2436         spin_unlock_irqrestore(&ihost->scic_lock, flags);
2437 }