isci: Removed struct sci_base_object from state machine.
[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 "isci.h"
57 #include "port.h"
58 #include "remote_device.h"
59 #include "request.h"
60 #include "scic_controller.h"
61 #include "scic_io_request.h"
62 #include "scic_phy.h"
63 #include "scic_port.h"
64 #include "scic_sds_controller.h"
65 #include "scic_sds_phy.h"
66 #include "scic_sds_port.h"
67 #include "remote_node_context.h"
68 #include "scic_sds_request.h"
69 #include "sci_environment.h"
70 #include "sci_util.h"
71 #include "scu_event_codes.h"
72 #include "task.h"
73
74 enum sci_status scic_remote_device_stop(
75         struct scic_sds_remote_device *sci_dev,
76         u32 timeout)
77 {
78         return sci_dev->state_handlers->stop_handler(sci_dev);
79 }
80
81
82 enum sci_status scic_remote_device_reset(
83         struct scic_sds_remote_device *sci_dev)
84 {
85         return sci_dev->state_handlers->reset_handler(sci_dev);
86 }
87
88
89 enum sci_status scic_remote_device_reset_complete(
90         struct scic_sds_remote_device *sci_dev)
91 {
92         return sci_dev->state_handlers->reset_complete_handler(sci_dev);
93 }
94
95
96 enum sas_linkrate scic_remote_device_get_connection_rate(
97         struct scic_sds_remote_device *sci_dev)
98 {
99         return sci_dev->connection_rate;
100 }
101
102 /**
103  *
104  *
105  * Remote device timer requirements
106  */
107 #define SCIC_SDS_REMOTE_DEVICE_MINIMUM_TIMER_COUNT (0)
108 #define SCIC_SDS_REMOTE_DEVICE_MAXIMUM_TIMER_COUNT (SCI_MAX_REMOTE_DEVICES)
109
110
111 /**
112  *
113  * @sci_dev: The remote device for which the suspend is being requested.
114  *
115  * This method invokes the remote device suspend state handler. enum sci_status
116  */
117 enum sci_status scic_sds_remote_device_suspend(
118         struct scic_sds_remote_device *sci_dev,
119         u32 suspend_type)
120 {
121         return sci_dev->state_handlers->suspend_handler(sci_dev, suspend_type);
122 }
123
124 /**
125  *
126  * @sci_dev: The remote device for which the event handling is being
127  *    requested.
128  * @frame_index: This is the frame index that is being processed.
129  *
130  * This method invokes the frame handler for the remote device state machine
131  * enum sci_status
132  */
133 enum sci_status scic_sds_remote_device_frame_handler(
134         struct scic_sds_remote_device *sci_dev,
135         u32 frame_index)
136 {
137         return sci_dev->state_handlers->frame_handler(sci_dev, frame_index);
138 }
139
140 /**
141  *
142  * @sci_dev: The remote device for which the event handling is being
143  *    requested.
144  * @event_code: This is the event code that is to be processed.
145  *
146  * This method invokes the remote device event handler. enum sci_status
147  */
148 enum sci_status scic_sds_remote_device_event_handler(
149         struct scic_sds_remote_device *sci_dev,
150         u32 event_code)
151 {
152         return sci_dev->state_handlers->event_handler(sci_dev, event_code);
153 }
154
155 /**
156  *
157  * @controller: The controller that is starting the io request.
158  * @sci_dev: The remote device for which the start io handling is being
159  *    requested.
160  * @io_request: The io request that is being started.
161  *
162  * This method invokes the remote device start io handler. enum sci_status
163  */
164 enum sci_status scic_sds_remote_device_start_io(
165         struct scic_sds_controller *controller,
166         struct scic_sds_remote_device *sci_dev,
167         struct scic_sds_request *io_request)
168 {
169         return sci_dev->state_handlers->start_io_handler(
170                        sci_dev, io_request);
171 }
172
173 /**
174  *
175  * @controller: The controller that is completing the io request.
176  * @sci_dev: The remote device for which the complete io handling is being
177  *    requested.
178  * @io_request: The io request that is being completed.
179  *
180  * This method invokes the remote device complete io handler. enum sci_status
181  */
182 enum sci_status scic_sds_remote_device_complete_io(
183         struct scic_sds_controller *controller,
184         struct scic_sds_remote_device *sci_dev,
185         struct scic_sds_request *io_request)
186 {
187         return sci_dev->state_handlers->complete_io_handler(
188                        sci_dev, io_request);
189 }
190
191 /**
192  *
193  * @controller: The controller that is starting the task request.
194  * @sci_dev: The remote device for which the start task handling is being
195  *    requested.
196  * @io_request: The task request that is being started.
197  *
198  * This method invokes the remote device start task handler. enum sci_status
199  */
200 enum sci_status scic_sds_remote_device_start_task(
201         struct scic_sds_controller *controller,
202         struct scic_sds_remote_device *sci_dev,
203         struct scic_sds_request *io_request)
204 {
205         return sci_dev->state_handlers->start_task_handler(
206                        sci_dev, io_request);
207 }
208
209 /**
210  *
211  * @sci_dev:
212  * @request:
213  *
214  * This method takes the request and bulids an appropriate SCU context for the
215  * request and then requests the controller to post the request. none
216  */
217 void scic_sds_remote_device_post_request(
218         struct scic_sds_remote_device *sci_dev,
219         u32 request)
220 {
221         u32 context;
222
223         context = scic_sds_remote_device_build_command_context(sci_dev, request);
224
225         scic_sds_controller_post_request(
226                 scic_sds_remote_device_get_controller(sci_dev),
227                 context
228                 );
229 }
230
231 /**
232  *
233  * @user_parameter: This is cast to a remote device object.
234  *
235  * This method is called once the remote node context is ready to be freed.
236  * The remote device can now report that its stop operation is complete. none
237  */
238 static void scic_sds_cb_remote_device_rnc_destruct_complete(
239         void *user_parameter)
240 {
241         struct scic_sds_remote_device *sci_dev;
242
243         sci_dev = (struct scic_sds_remote_device *)user_parameter;
244
245         BUG_ON(sci_dev->started_request_count != 0);
246
247         sci_base_state_machine_change_state(&sci_dev->state_machine,
248                                             SCI_BASE_REMOTE_DEVICE_STATE_STOPPED);
249 }
250
251 /**
252  *
253  * @user_parameter: This is cast to a remote device object.
254  *
255  * This method is called once the remote node context has transisitioned to a
256  * ready state.  This is the indication that the remote device object can also
257  * transition to ready. none
258  */
259 static void scic_sds_remote_device_resume_complete_handler(
260         void *user_parameter)
261 {
262         struct scic_sds_remote_device *sci_dev;
263
264         sci_dev = (struct scic_sds_remote_device *)user_parameter;
265
266         if (
267                 sci_base_state_machine_get_state(&sci_dev->state_machine)
268                 != SCI_BASE_REMOTE_DEVICE_STATE_READY
269                 ) {
270                 sci_base_state_machine_change_state(
271                         &sci_dev->state_machine,
272                         SCI_BASE_REMOTE_DEVICE_STATE_READY
273                         );
274         }
275 }
276
277 /**
278  *
279  * @device: This parameter specifies the device for which the request is being
280  *    started.
281  * @request: This parameter specifies the request being started.
282  * @status: This parameter specifies the current start operation status.
283  *
284  * This method will perform the STP request start processing common to IO
285  * requests and task requests of all types. none
286  */
287 void scic_sds_remote_device_start_request(
288         struct scic_sds_remote_device *sci_dev,
289         struct scic_sds_request *sci_req,
290         enum sci_status status)
291 {
292         /* We still have a fault in starting the io complete it on the port */
293         if (status == SCI_SUCCESS)
294                 scic_sds_remote_device_increment_request_count(sci_dev);
295         else{
296                 sci_dev->owning_port->state_handlers->complete_io_handler(
297                         sci_dev->owning_port, sci_dev, sci_req
298                         );
299         }
300 }
301
302
303 /**
304  *
305  * @request: This parameter specifies the request being continued.
306  *
307  * This method will continue to post tc for a STP request. This method usually
308  * serves as a callback when RNC gets resumed during a task management
309  * sequence. none
310  */
311 void scic_sds_remote_device_continue_request(void *dev)
312 {
313         struct scic_sds_remote_device *sci_dev = dev;
314
315         /* we need to check if this request is still valid to continue. */
316         if (sci_dev->working_request)
317                 scic_controller_continue_io(sci_dev->working_request);
318 }
319
320 /**
321  * This method will terminate all of the IO requests in the controllers IO
322  *    request table that were targeted for this device.
323  * @sci_dev: This parameter specifies the remote device for which to
324  *    attempt to terminate all requests.
325  *
326  * This method returns an indication as to whether all requests were
327  * successfully terminated.  If a single request fails to be terminated, then
328  * this method will return the failure.
329  */
330 static enum sci_status scic_sds_remote_device_terminate_requests(
331         struct scic_sds_remote_device *sci_dev)
332 {
333         enum sci_status status           = SCI_SUCCESS;
334         enum sci_status terminate_status = SCI_SUCCESS;
335         struct scic_sds_request *sci_req;
336         u32 index;
337         u32 request_count    = sci_dev->started_request_count;
338
339         for (index = 0;
340              (index < SCI_MAX_IO_REQUESTS) && (request_count > 0);
341              index++) {
342                 sci_req = sci_dev->owning_port->owning_controller->io_request_table[index];
343
344                 if ((sci_req != NULL) && (sci_req->target_device == sci_dev)) {
345                         terminate_status = scic_controller_terminate_request(
346                                 sci_dev->owning_port->owning_controller,
347                                 sci_dev,
348                                 sci_req
349                                 );
350
351                         if (terminate_status != SCI_SUCCESS)
352                                 status = terminate_status;
353
354                         request_count--;
355                 }
356         }
357
358         return status;
359 }
360
361 static enum sci_status
362 default_device_handler(struct scic_sds_remote_device *sci_dev,
363                        const char *func)
364 {
365         dev_warn(scirdev_to_dev(sci_dev),
366                  "%s: in wrong state: %d\n", func,
367                  sci_base_state_machine_get_state(&sci_dev->state_machine));
368         return SCI_FAILURE_INVALID_STATE;
369 }
370
371 enum sci_status scic_sds_remote_device_default_start_handler(
372         struct scic_sds_remote_device *sci_dev)
373 {
374         return default_device_handler(sci_dev, __func__);
375 }
376
377 static enum sci_status scic_sds_remote_device_default_stop_handler(
378         struct scic_sds_remote_device *sci_dev)
379 {
380         return default_device_handler(sci_dev, __func__);
381 }
382
383 enum sci_status scic_sds_remote_device_default_fail_handler(
384         struct scic_sds_remote_device *sci_dev)
385 {
386         return default_device_handler(sci_dev, __func__);
387 }
388
389 enum sci_status scic_sds_remote_device_default_destruct_handler(
390         struct scic_sds_remote_device *sci_dev)
391 {
392         return default_device_handler(sci_dev, __func__);
393 }
394
395 enum sci_status scic_sds_remote_device_default_reset_handler(
396         struct scic_sds_remote_device *sci_dev)
397 {
398         return default_device_handler(sci_dev, __func__);
399 }
400
401 enum sci_status scic_sds_remote_device_default_reset_complete_handler(
402         struct scic_sds_remote_device *sci_dev)
403 {
404         return default_device_handler(sci_dev, __func__);
405 }
406
407 enum sci_status scic_sds_remote_device_default_suspend_handler(
408         struct scic_sds_remote_device *sci_dev, u32 suspend_type)
409 {
410         return default_device_handler(sci_dev, __func__);
411 }
412
413 enum sci_status scic_sds_remote_device_default_resume_handler(
414         struct scic_sds_remote_device *sci_dev)
415 {
416         return default_device_handler(sci_dev, __func__);
417 }
418
419 /**
420  *
421  * @device: The struct scic_sds_remote_device which is then cast into a
422  *    struct scic_sds_remote_device.
423  * @event_code: The event code that the struct scic_sds_controller wants the device
424  *    object to process.
425  *
426  * This method is the default event handler.  It will call the RNC state
427  * machine handler for any RNC events otherwise it will log a warning and
428  * returns a failure. enum sci_status SCI_FAILURE_INVALID_STATE
429  */
430 static enum sci_status  scic_sds_remote_device_core_event_handler(
431         struct scic_sds_remote_device *sci_dev,
432         u32 event_code,
433         bool is_ready_state)
434 {
435         enum sci_status status;
436
437         switch (scu_get_event_type(event_code)) {
438         case SCU_EVENT_TYPE_RNC_OPS_MISC:
439         case SCU_EVENT_TYPE_RNC_SUSPEND_TX:
440         case SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX:
441                 status = scic_sds_remote_node_context_event_handler(&sci_dev->rnc, event_code);
442                 break;
443         case SCU_EVENT_TYPE_PTX_SCHEDULE_EVENT:
444
445                 if (scu_get_event_code(event_code) == SCU_EVENT_IT_NEXUS_TIMEOUT) {
446                         status = SCI_SUCCESS;
447
448                         /* Suspend the associated RNC */
449                         scic_sds_remote_node_context_suspend(&sci_dev->rnc,
450                                                               SCI_SOFTWARE_SUSPENSION,
451                                                               NULL, NULL);
452
453                         dev_dbg(scirdev_to_dev(sci_dev),
454                                 "%s: device: %p event code: %x: %s\n",
455                                 __func__, sci_dev, event_code,
456                                 (is_ready_state)
457                                 ? "I_T_Nexus_Timeout event"
458                                 : "I_T_Nexus_Timeout event in wrong state");
459
460                         break;
461                 }
462         /* Else, fall through and treat as unhandled... */
463
464         default:
465                 dev_dbg(scirdev_to_dev(sci_dev),
466                         "%s: device: %p event code: %x: %s\n",
467                         __func__, sci_dev, event_code,
468                         (is_ready_state)
469                         ? "unexpected event"
470                         : "unexpected event in wrong state");
471                 status = SCI_FAILURE_INVALID_STATE;
472                 break;
473         }
474
475         return status;
476 }
477 /**
478  *
479  * @device: The struct scic_sds_remote_device which is then cast into a
480  *    struct scic_sds_remote_device.
481  * @event_code: The event code that the struct scic_sds_controller wants the device
482  *    object to process.
483  *
484  * This method is the default event handler.  It will call the RNC state
485  * machine handler for any RNC events otherwise it will log a warning and
486  * returns a failure. enum sci_status SCI_FAILURE_INVALID_STATE
487  */
488 static enum sci_status  scic_sds_remote_device_default_event_handler(
489         struct scic_sds_remote_device *sci_dev,
490         u32 event_code)
491 {
492         return scic_sds_remote_device_core_event_handler(sci_dev,
493                                                           event_code,
494                                                           false);
495 }
496
497 /**
498  *
499  * @device: The struct scic_sds_remote_device which is then cast into a
500  *    struct scic_sds_remote_device.
501  * @frame_index: The frame index for which the struct scic_sds_controller wants this
502  *    device object to process.
503  *
504  * This method is the default unsolicited frame handler.  It logs a warning,
505  * releases the frame and returns a failure. enum sci_status
506  * SCI_FAILURE_INVALID_STATE
507  */
508 enum sci_status scic_sds_remote_device_default_frame_handler(
509         struct scic_sds_remote_device *sci_dev,
510         u32 frame_index)
511 {
512         dev_warn(scirdev_to_dev(sci_dev),
513                  "%s: SCIC Remote Device requested to handle frame %x "
514                  "while in wrong state %d\n",
515                  __func__,
516                  frame_index,
517                  sci_base_state_machine_get_state(
518                          &sci_dev->state_machine));
519
520         /* Return the frame back to the controller */
521         scic_sds_controller_release_frame(
522                 scic_sds_remote_device_get_controller(sci_dev), frame_index
523                 );
524
525         return SCI_FAILURE_INVALID_STATE;
526 }
527
528 enum sci_status scic_sds_remote_device_default_start_request_handler(
529         struct scic_sds_remote_device *sci_dev,
530         struct scic_sds_request *request)
531 {
532         return default_device_handler(sci_dev, __func__);
533 }
534
535 enum sci_status scic_sds_remote_device_default_complete_request_handler(
536         struct scic_sds_remote_device *sci_dev,
537         struct scic_sds_request *request)
538 {
539         return default_device_handler(sci_dev, __func__);
540 }
541
542 enum sci_status scic_sds_remote_device_default_continue_request_handler(
543         struct scic_sds_remote_device *sci_dev,
544         struct scic_sds_request *request)
545 {
546         return default_device_handler(sci_dev, __func__);
547 }
548
549 /**
550  *
551  * @device: The struct scic_sds_remote_device which is then cast into a
552  *    struct scic_sds_remote_device.
553  * @frame_index: The frame index for which the struct scic_sds_controller wants this
554  *    device object to process.
555  *
556  * This method is a general ssp frame handler.  In most cases the device object
557  * needs to route the unsolicited frame processing to the io request object.
558  * This method decodes the tag for the io request object and routes the
559  * unsolicited frame to that object. enum sci_status SCI_FAILURE_INVALID_STATE
560  */
561 enum sci_status scic_sds_remote_device_general_frame_handler(
562         struct scic_sds_remote_device *sci_dev,
563         u32 frame_index)
564 {
565         enum sci_status result;
566         struct sci_ssp_frame_header *frame_header;
567         struct scic_sds_request *io_request;
568
569         result = scic_sds_unsolicited_frame_control_get_header(
570                 &(scic_sds_remote_device_get_controller(sci_dev)->uf_control),
571                 frame_index,
572                 (void **)&frame_header
573                 );
574
575         if (SCI_SUCCESS == result) {
576                 io_request = scic_sds_controller_get_io_request_from_tag(
577                         scic_sds_remote_device_get_controller(sci_dev), frame_header->tag);
578
579                 if ((io_request == NULL)
580                     || (io_request->target_device != sci_dev)) {
581                         /*
582                          * We could not map this tag to a valid IO request
583                          * Just toss the frame and continue */
584                         scic_sds_controller_release_frame(
585                                 scic_sds_remote_device_get_controller(sci_dev), frame_index
586                                 );
587                 } else {
588                         /* The IO request is now in charge of releasing the frame */
589                         result = io_request->state_handlers->frame_handler(
590                                 io_request, frame_index);
591                 }
592         }
593
594         return result;
595 }
596
597 /**
598  *
599  * @[in]: sci_dev This is the device object that is receiving the event.
600  * @[in]: event_code The event code to process.
601  *
602  * This is a common method for handling events reported to the remote device
603  * from the controller object. enum sci_status
604  */
605 enum sci_status scic_sds_remote_device_general_event_handler(
606         struct scic_sds_remote_device *sci_dev,
607         u32 event_code)
608 {
609         return scic_sds_remote_device_core_event_handler(sci_dev,
610                                                           event_code,
611                                                           true);
612 }
613
614 /*
615  * *****************************************************************************
616  * *  STOPPED STATE HANDLERS
617  * ***************************************************************************** */
618
619 /**
620  *
621  * @device:
622  *
623  * This method takes the struct scic_sds_remote_device from a stopped state and
624  * attempts to start it.   The RNC buffer for the device is constructed and the
625  * device state machine is transitioned to the
626  * SCIC_BASE_REMOTE_DEVICE_STATE_STARTING. enum sci_status SCI_SUCCESS if there is
627  * an RNC buffer available to construct the remote device.
628  * SCI_FAILURE_INSUFFICIENT_RESOURCES if there is no RNC buffer available in
629  * which to construct the remote device.
630  */
631 static enum sci_status scic_sds_remote_device_stopped_state_start_handler(
632         struct scic_sds_remote_device *sci_dev)
633 {
634         enum sci_status status;
635
636         status = scic_sds_remote_node_context_resume(&sci_dev->rnc,
637                         scic_sds_remote_device_resume_complete_handler, sci_dev);
638
639         if (status == SCI_SUCCESS)
640                 sci_base_state_machine_change_state(&sci_dev->state_machine,
641                                                     SCI_BASE_REMOTE_DEVICE_STATE_STARTING);
642
643         return status;
644 }
645
646 static enum sci_status scic_sds_remote_device_stopped_state_stop_handler(
647         struct scic_sds_remote_device *sci_dev)
648 {
649         return SCI_SUCCESS;
650 }
651
652 /**
653  *
654  * @sci_dev: The struct scic_sds_remote_device which is cast into a
655  *    struct scic_sds_remote_device.
656  *
657  * This method will destruct a struct scic_sds_remote_device that is in a stopped
658  * state.  This is the only state from which a destruct request will succeed.
659  * The RNi for this struct scic_sds_remote_device is returned to the free pool and the
660  * device object transitions to the SCI_BASE_REMOTE_DEVICE_STATE_FINAL.
661  * enum sci_status SCI_SUCCESS
662  */
663 static enum sci_status scic_sds_remote_device_stopped_state_destruct_handler(
664         struct scic_sds_remote_device *sci_dev)
665 {
666         struct scic_sds_controller *scic;
667
668         scic = scic_sds_remote_device_get_controller(sci_dev);
669         scic_sds_controller_free_remote_node_context(scic, sci_dev,
670                                                      sci_dev->rnc.remote_node_index);
671         sci_dev->rnc.remote_node_index = SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX;
672
673         sci_base_state_machine_change_state(&sci_dev->state_machine,
674                                             SCI_BASE_REMOTE_DEVICE_STATE_FINAL);
675
676         return SCI_SUCCESS;
677 }
678
679 /*
680  * *****************************************************************************
681  * *  STARTING STATE HANDLERS
682  * ***************************************************************************** */
683
684 static enum sci_status scic_sds_remote_device_starting_state_stop_handler(
685         struct scic_sds_remote_device *sci_dev)
686 {
687         /*
688          * This device has not yet started so there had better be no IO requests
689          */
690         BUG_ON(sci_dev->started_request_count != 0);
691
692         /*
693          * Destroy the remote node context
694          */
695         scic_sds_remote_node_context_destruct(&sci_dev->rnc,
696                 scic_sds_cb_remote_device_rnc_destruct_complete, sci_dev);
697
698         /*
699          * Transition to the stopping state and wait for the remote node to
700          * complete being posted and invalidated.
701          */
702         sci_base_state_machine_change_state(&sci_dev->state_machine,
703                                             SCI_BASE_REMOTE_DEVICE_STATE_STOPPING);
704
705         return SCI_SUCCESS;
706 }
707
708 enum sci_status scic_sds_remote_device_ready_state_stop_handler(
709         struct scic_sds_remote_device *sci_dev)
710 {
711         enum sci_status status = SCI_SUCCESS;
712
713         /* Request the parent state machine to transition to the stopping state */
714         sci_base_state_machine_change_state(&sci_dev->state_machine,
715                                             SCI_BASE_REMOTE_DEVICE_STATE_STOPPING);
716
717         if (sci_dev->started_request_count == 0) {
718                 scic_sds_remote_node_context_destruct(&sci_dev->rnc,
719                         scic_sds_cb_remote_device_rnc_destruct_complete,
720                         sci_dev);
721         } else
722                 status = scic_sds_remote_device_terminate_requests(sci_dev);
723
724         return status;
725 }
726
727 /**
728  *
729  * @device: The struct scic_sds_remote_device object which is cast to a
730  *    struct scic_sds_remote_device object.
731  *
732  * This is the ready state device reset handler enum sci_status
733  */
734 enum sci_status scic_sds_remote_device_ready_state_reset_handler(
735         struct scic_sds_remote_device *sci_dev)
736 {
737         /* Request the parent state machine to transition to the stopping state */
738         sci_base_state_machine_change_state(&sci_dev->state_machine,
739                                             SCI_BASE_REMOTE_DEVICE_STATE_RESETTING);
740
741         return SCI_SUCCESS;
742 }
743
744 /*
745  * This method will attempt to start a task request for this device object. The
746  * remote device object will issue the start request for the task and if
747  * successful it will start the request for the port object then increment its
748  * own requet count. enum sci_status SCI_SUCCESS if the task request is started for
749  * this device object. SCI_FAILURE_INSUFFICIENT_RESOURCES if the io request
750  * object could not get the resources to start.
751  */
752 static enum sci_status scic_sds_remote_device_ready_state_start_task_handler(
753         struct scic_sds_remote_device *sci_dev,
754         struct scic_sds_request *request)
755 {
756         enum sci_status result;
757
758         /* See if the port is in a state where we can start the IO request */
759         result = scic_sds_port_start_io(
760                 scic_sds_remote_device_get_port(sci_dev), sci_dev, request);
761
762         if (result == SCI_SUCCESS) {
763                 result = scic_sds_remote_node_context_start_task(&sci_dev->rnc,
764                                                                  request);
765                 if (result == SCI_SUCCESS)
766                         result = scic_sds_request_start(request);
767
768                 scic_sds_remote_device_start_request(sci_dev, request, result);
769         }
770
771         return result;
772 }
773
774 /*
775  * This method will attempt to start an io request for this device object. The
776  * remote device object will issue the start request for the io and if
777  * successful it will start the request for the port object then increment its
778  * own requet count. enum sci_status SCI_SUCCESS if the io request is started for
779  * this device object. SCI_FAILURE_INSUFFICIENT_RESOURCES if the io request
780  * object could not get the resources to start.
781  */
782 static enum sci_status scic_sds_remote_device_ready_state_start_io_handler(
783         struct scic_sds_remote_device *sci_dev,
784         struct scic_sds_request *request)
785 {
786         enum sci_status result;
787
788         /* See if the port is in a state where we can start the IO request */
789         result = scic_sds_port_start_io(
790                 scic_sds_remote_device_get_port(sci_dev), sci_dev, request);
791
792         if (result == SCI_SUCCESS) {
793                 result = scic_sds_remote_node_context_start_io(&sci_dev->rnc, request);
794                 if (result == SCI_SUCCESS)
795                         result = scic_sds_request_start(request);
796
797                 scic_sds_remote_device_start_request(sci_dev, request, result);
798         }
799
800         return result;
801 }
802
803 /*
804  * This method will complete the request for the remote device object.  The
805  * method will call the completion handler for the request object and if
806  * successful it will complete the request on the port object then decrement
807  * its own started_request_count. enum sci_status
808  */
809 static enum sci_status scic_sds_remote_device_ready_state_complete_request_handler(
810         struct scic_sds_remote_device *sci_dev,
811         struct scic_sds_request *request)
812 {
813         enum sci_status result;
814
815         result = scic_sds_request_complete(request);
816
817         if (result != SCI_SUCCESS)
818                 return result;
819
820         /* See if the port is in a state
821          * where we can start the IO request */
822         result = scic_sds_port_complete_io(
823                         scic_sds_remote_device_get_port(sci_dev),
824                         sci_dev, request);
825
826         if (result == SCI_SUCCESS)
827                 scic_sds_remote_device_decrement_request_count(sci_dev);
828
829         return result;
830 }
831
832 /*
833  * *****************************************************************************
834  * *  STOPPING STATE HANDLERS
835  * ***************************************************************************** */
836
837 /**
838  *
839  * @sci_dev: The struct scic_sds_remote_device which is cast into a
840  *    struct scic_sds_remote_device.
841  *
842  * This method will stop a struct scic_sds_remote_device that is already in the
843  * SCI_BASE_REMOTE_DEVICE_STATE_STOPPING state. This is not considered an error
844  * since we allow a stop request on a device that is alreay stopping or
845  * stopped. enum sci_status SCI_SUCCESS
846  */
847 static enum sci_status scic_sds_remote_device_stopping_state_stop_handler(
848         struct scic_sds_remote_device *device)
849 {
850         /*
851          * All requests should have been terminated, but if there is an
852          * attempt to stop a device already in the stopping state, then
853          * try again to terminate. */
854         return scic_sds_remote_device_terminate_requests(device);
855 }
856
857
858 /**
859  *
860  * @device: The device object for which the request is completing.
861  * @request: The task request that is being completed.
862  *
863  * This method completes requests for this struct scic_sds_remote_device while it is
864  * in the SCI_BASE_REMOTE_DEVICE_STATE_STOPPING state. This method calls the
865  * complete method for the request object and if that is successful the port
866  * object is called to complete the task request. Then the device object itself
867  * completes the task request. If struct scic_sds_remote_device started_request_count
868  * goes to 0 and the invalidate RNC request has completed the device object can
869  * transition to the SCI_BASE_REMOTE_DEVICE_STATE_STOPPED. enum sci_status
870  */
871 static enum sci_status scic_sds_remote_device_stopping_state_complete_request_handler(
872         struct scic_sds_remote_device *sci_dev,
873         struct scic_sds_request *request)
874 {
875         enum sci_status status = SCI_SUCCESS;
876
877         status = scic_sds_request_complete(request);
878
879         if (status != SCI_SUCCESS)
880                 return status;
881
882         status = scic_sds_port_complete_io(scic_sds_remote_device_get_port(sci_dev),
883                                            sci_dev, request);
884         if (status != SCI_SUCCESS)
885                 return status;
886
887         scic_sds_remote_device_decrement_request_count(sci_dev);
888
889         if (scic_sds_remote_device_get_request_count(sci_dev) == 0)
890                 scic_sds_remote_node_context_destruct(&sci_dev->rnc,
891                                                       scic_sds_cb_remote_device_rnc_destruct_complete,
892                                                       sci_dev);
893         return SCI_SUCCESS;
894 }
895
896 /**
897  *
898  * @device: The struct scic_sds_remote_device which is to be cast into a
899  *    struct scic_sds_remote_device object.
900  *
901  * This method will complete the reset operation when the device is in the
902  * resetting state. enum sci_status
903  */
904 static enum sci_status scic_sds_remote_device_resetting_state_reset_complete_handler(
905         struct scic_sds_remote_device *sci_dev)
906 {
907
908         sci_base_state_machine_change_state(
909                 &sci_dev->state_machine,
910                 SCI_BASE_REMOTE_DEVICE_STATE_READY
911                 );
912
913         return SCI_SUCCESS;
914 }
915
916 /**
917  *
918  * @device: The struct scic_sds_remote_device which is to be cast into a
919  *    struct scic_sds_remote_device object.
920  *
921  * This method will stop the remote device while in the resetting state.
922  * enum sci_status
923  */
924 static enum sci_status scic_sds_remote_device_resetting_state_stop_handler(
925         struct scic_sds_remote_device *sci_dev)
926 {
927         sci_base_state_machine_change_state(
928                 &sci_dev->state_machine,
929                 SCI_BASE_REMOTE_DEVICE_STATE_STOPPING
930                 );
931
932         return SCI_SUCCESS;
933 }
934
935 /*
936  * This method completes requests for this struct scic_sds_remote_device while it is
937  * in the SCI_BASE_REMOTE_DEVICE_STATE_RESETTING state. This method calls the
938  * complete method for the request object and if that is successful the port
939  * object is called to complete the task request. Then the device object itself
940  * completes the task request. enum sci_status
941  */
942 static enum sci_status scic_sds_remote_device_resetting_state_complete_request_handler(
943         struct scic_sds_remote_device *sci_dev,
944         struct scic_sds_request *request)
945 {
946         enum sci_status status = SCI_SUCCESS;
947
948         status = scic_sds_request_complete(request);
949
950         if (status == SCI_SUCCESS) {
951                 status = scic_sds_port_complete_io(
952                                 scic_sds_remote_device_get_port(sci_dev),
953                                 sci_dev, request);
954
955                 if (status == SCI_SUCCESS) {
956                         scic_sds_remote_device_decrement_request_count(sci_dev);
957                 }
958         }
959
960         return status;
961 }
962
963 static const struct scic_sds_remote_device_state_handler scic_sds_remote_device_state_handler_table[] = {
964         [SCI_BASE_REMOTE_DEVICE_STATE_INITIAL] = {
965                 .start_handler          = scic_sds_remote_device_default_start_handler,
966                 .stop_handler           = scic_sds_remote_device_default_stop_handler,
967                 .fail_handler           = scic_sds_remote_device_default_fail_handler,
968                 .destruct_handler       = scic_sds_remote_device_default_destruct_handler,
969                 .reset_handler          = scic_sds_remote_device_default_reset_handler,
970                 .reset_complete_handler = scic_sds_remote_device_default_reset_complete_handler,
971                 .start_io_handler       = scic_sds_remote_device_default_start_request_handler,
972                 .complete_io_handler    = scic_sds_remote_device_default_complete_request_handler,
973                 .continue_io_handler    = scic_sds_remote_device_default_continue_request_handler,
974                 .start_task_handler     = scic_sds_remote_device_default_start_request_handler,
975                 .complete_task_handler  = scic_sds_remote_device_default_complete_request_handler,
976                 .suspend_handler        = scic_sds_remote_device_default_suspend_handler,
977                 .resume_handler         = scic_sds_remote_device_default_resume_handler,
978                 .event_handler          = scic_sds_remote_device_default_event_handler,
979                 .frame_handler          = scic_sds_remote_device_default_frame_handler
980         },
981         [SCI_BASE_REMOTE_DEVICE_STATE_STOPPED] = {
982                 .start_handler          = scic_sds_remote_device_stopped_state_start_handler,
983                 .stop_handler           = scic_sds_remote_device_stopped_state_stop_handler,
984                 .fail_handler           = scic_sds_remote_device_default_fail_handler,
985                 .destruct_handler       = scic_sds_remote_device_stopped_state_destruct_handler,
986                 .reset_handler          = scic_sds_remote_device_default_reset_handler,
987                 .reset_complete_handler = scic_sds_remote_device_default_reset_complete_handler,
988                 .start_io_handler       = scic_sds_remote_device_default_start_request_handler,
989                 .complete_io_handler    = scic_sds_remote_device_default_complete_request_handler,
990                 .continue_io_handler    = scic_sds_remote_device_default_continue_request_handler,
991                 .start_task_handler     = scic_sds_remote_device_default_start_request_handler,
992                 .complete_task_handler  = scic_sds_remote_device_default_complete_request_handler,
993                 .suspend_handler        = scic_sds_remote_device_default_suspend_handler,
994                 .resume_handler         = scic_sds_remote_device_default_resume_handler,
995                 .event_handler          = scic_sds_remote_device_default_event_handler,
996                 .frame_handler          = scic_sds_remote_device_default_frame_handler
997         },
998         [SCI_BASE_REMOTE_DEVICE_STATE_STARTING] = {
999                 .start_handler          = scic_sds_remote_device_default_start_handler,
1000                 .stop_handler           = scic_sds_remote_device_starting_state_stop_handler,
1001                 .fail_handler           = scic_sds_remote_device_default_fail_handler,
1002                 .destruct_handler       = scic_sds_remote_device_default_destruct_handler,
1003                 .reset_handler          = scic_sds_remote_device_default_reset_handler,
1004                 .reset_complete_handler = scic_sds_remote_device_default_reset_complete_handler,
1005                 .start_io_handler       = scic_sds_remote_device_default_start_request_handler,
1006                 .complete_io_handler    = scic_sds_remote_device_default_complete_request_handler,
1007                 .continue_io_handler    = scic_sds_remote_device_default_continue_request_handler,
1008                 .start_task_handler     = scic_sds_remote_device_default_start_request_handler,
1009                 .complete_task_handler  = scic_sds_remote_device_default_complete_request_handler,
1010                 .suspend_handler        = scic_sds_remote_device_default_suspend_handler,
1011                 .resume_handler         = scic_sds_remote_device_default_resume_handler,
1012                 .event_handler          = scic_sds_remote_device_general_event_handler,
1013                 .frame_handler          = scic_sds_remote_device_default_frame_handler
1014         },
1015         [SCI_BASE_REMOTE_DEVICE_STATE_READY] = {
1016                 .start_handler          = scic_sds_remote_device_default_start_handler,
1017                 .stop_handler           = scic_sds_remote_device_ready_state_stop_handler,
1018                 .fail_handler           = scic_sds_remote_device_default_fail_handler,
1019                 .destruct_handler       = scic_sds_remote_device_default_destruct_handler,
1020                 .reset_handler          = scic_sds_remote_device_ready_state_reset_handler,
1021                 .reset_complete_handler = scic_sds_remote_device_default_reset_complete_handler,
1022                 .start_io_handler       = scic_sds_remote_device_ready_state_start_io_handler,
1023                 .complete_io_handler    = scic_sds_remote_device_ready_state_complete_request_handler,
1024                 .continue_io_handler    = scic_sds_remote_device_default_continue_request_handler,
1025                 .start_task_handler     = scic_sds_remote_device_ready_state_start_task_handler,
1026                 .complete_task_handler  = scic_sds_remote_device_ready_state_complete_request_handler,
1027                 .suspend_handler        = scic_sds_remote_device_default_suspend_handler,
1028                 .resume_handler         = scic_sds_remote_device_default_resume_handler,
1029                 .event_handler          = scic_sds_remote_device_general_event_handler,
1030                 .frame_handler          = scic_sds_remote_device_general_frame_handler,
1031         },
1032         [SCI_BASE_REMOTE_DEVICE_STATE_STOPPING] = {
1033                 .start_handler          = scic_sds_remote_device_default_start_handler,
1034                 .stop_handler           = scic_sds_remote_device_stopping_state_stop_handler,
1035                 .fail_handler           = scic_sds_remote_device_default_fail_handler,
1036                 .destruct_handler       = scic_sds_remote_device_default_destruct_handler,
1037                 .reset_handler          = scic_sds_remote_device_default_reset_handler,
1038                 .reset_complete_handler = scic_sds_remote_device_default_reset_complete_handler,
1039                 .start_io_handler       = scic_sds_remote_device_default_start_request_handler,
1040                 .complete_io_handler    = scic_sds_remote_device_stopping_state_complete_request_handler,
1041                 .continue_io_handler    = scic_sds_remote_device_default_continue_request_handler,
1042                 .start_task_handler     = scic_sds_remote_device_default_start_request_handler,
1043                 .complete_task_handler  = scic_sds_remote_device_stopping_state_complete_request_handler,
1044                 .suspend_handler        = scic_sds_remote_device_default_suspend_handler,
1045                 .resume_handler         = scic_sds_remote_device_default_resume_handler,
1046                 .event_handler          = scic_sds_remote_device_general_event_handler,
1047                 .frame_handler          = scic_sds_remote_device_general_frame_handler
1048         },
1049         [SCI_BASE_REMOTE_DEVICE_STATE_FAILED] = {
1050                 .start_handler          = scic_sds_remote_device_default_start_handler,
1051                 .stop_handler           = scic_sds_remote_device_default_stop_handler,
1052                 .fail_handler           = scic_sds_remote_device_default_fail_handler,
1053                 .destruct_handler       = scic_sds_remote_device_default_destruct_handler,
1054                 .reset_handler          = scic_sds_remote_device_default_reset_handler,
1055                 .reset_complete_handler = scic_sds_remote_device_default_reset_complete_handler,
1056                 .start_io_handler       = scic_sds_remote_device_default_start_request_handler,
1057                 .complete_io_handler    = scic_sds_remote_device_default_complete_request_handler,
1058                 .continue_io_handler    = scic_sds_remote_device_default_continue_request_handler,
1059                 .start_task_handler     = scic_sds_remote_device_default_start_request_handler,
1060                 .complete_task_handler  = scic_sds_remote_device_default_complete_request_handler,
1061                 .suspend_handler        = scic_sds_remote_device_default_suspend_handler,
1062                 .resume_handler         = scic_sds_remote_device_default_resume_handler,
1063                 .event_handler          = scic_sds_remote_device_default_event_handler,
1064                 .frame_handler          = scic_sds_remote_device_general_frame_handler
1065         },
1066         [SCI_BASE_REMOTE_DEVICE_STATE_RESETTING] = {
1067                 .start_handler          = scic_sds_remote_device_default_start_handler,
1068                 .stop_handler           = scic_sds_remote_device_resetting_state_stop_handler,
1069                 .fail_handler           = scic_sds_remote_device_default_fail_handler,
1070                 .destruct_handler       = scic_sds_remote_device_default_destruct_handler,
1071                 .reset_handler          = scic_sds_remote_device_default_reset_handler,
1072                 .reset_complete_handler = scic_sds_remote_device_resetting_state_reset_complete_handler,
1073                 .start_io_handler       = scic_sds_remote_device_default_start_request_handler,
1074                 .complete_io_handler    = scic_sds_remote_device_resetting_state_complete_request_handler,
1075                 .continue_io_handler    = scic_sds_remote_device_default_continue_request_handler,
1076                 .start_task_handler     = scic_sds_remote_device_default_start_request_handler,
1077                 .complete_task_handler  = scic_sds_remote_device_resetting_state_complete_request_handler,
1078                 .suspend_handler        = scic_sds_remote_device_default_suspend_handler,
1079                 .resume_handler         = scic_sds_remote_device_default_resume_handler,
1080                 .event_handler          = scic_sds_remote_device_default_event_handler,
1081                 .frame_handler          = scic_sds_remote_device_general_frame_handler
1082         },
1083         [SCI_BASE_REMOTE_DEVICE_STATE_FINAL] = {
1084                 .start_handler          = scic_sds_remote_device_default_start_handler,
1085                 .stop_handler           = scic_sds_remote_device_default_stop_handler,
1086                 .fail_handler           = scic_sds_remote_device_default_fail_handler,
1087                 .destruct_handler       = scic_sds_remote_device_default_destruct_handler,
1088                 .reset_handler          = scic_sds_remote_device_default_reset_handler,
1089                 .reset_complete_handler = scic_sds_remote_device_default_reset_complete_handler,
1090                 .start_io_handler       = scic_sds_remote_device_default_start_request_handler,
1091                 .complete_io_handler    = scic_sds_remote_device_default_complete_request_handler,
1092                 .continue_io_handler    = scic_sds_remote_device_default_continue_request_handler,
1093                 .start_task_handler     = scic_sds_remote_device_default_start_request_handler,
1094                 .complete_task_handler  = scic_sds_remote_device_default_complete_request_handler,
1095                 .suspend_handler        = scic_sds_remote_device_default_suspend_handler,
1096                 .resume_handler         = scic_sds_remote_device_default_resume_handler,
1097                 .event_handler          = scic_sds_remote_device_default_event_handler,
1098                 .frame_handler          = scic_sds_remote_device_default_frame_handler
1099         }
1100 };
1101
1102 static void scic_sds_remote_device_initial_state_enter(void *object)
1103 {
1104         struct scic_sds_remote_device *sci_dev = (struct scic_sds_remote_device *)object;
1105
1106         sci_dev = container_of(object, typeof(*sci_dev), parent);
1107         SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1108                           SCI_BASE_REMOTE_DEVICE_STATE_INITIAL);
1109
1110         /* Initial state is a transitional state to the stopped state */
1111         sci_base_state_machine_change_state(&sci_dev->state_machine,
1112                                             SCI_BASE_REMOTE_DEVICE_STATE_STOPPED);
1113 }
1114
1115 /**
1116  * isci_remote_device_change_state() - This function gets the status of the
1117  *    remote_device object.
1118  * @isci_device: This parameter points to the isci_remote_device object
1119  *
1120  * status of the object as a isci_status enum.
1121  */
1122 void isci_remote_device_change_state(
1123         struct isci_remote_device *isci_device,
1124         enum isci_status status)
1125 {
1126         unsigned long flags;
1127
1128         spin_lock_irqsave(&isci_device->state_lock, flags);
1129         isci_device->status = status;
1130         spin_unlock_irqrestore(&isci_device->state_lock, flags);
1131 }
1132
1133 /**
1134  * scic_remote_device_destruct() - free remote node context and destruct
1135  * @remote_device: This parameter specifies the remote device to be destructed.
1136  *
1137  * Remote device objects are a limited resource.  As such, they must be
1138  * protected.  Thus calls to construct and destruct are mutually exclusive and
1139  * non-reentrant. The return value shall indicate if the device was
1140  * successfully destructed or if some failure occurred. enum sci_status This value
1141  * is returned if the device is successfully destructed.
1142  * SCI_FAILURE_INVALID_REMOTE_DEVICE This value is returned if the supplied
1143  * device isn't valid (e.g. it's already been destoryed, the handle isn't
1144  * valid, etc.).
1145  */
1146 static enum sci_status scic_remote_device_destruct(struct scic_sds_remote_device *sci_dev)
1147 {
1148         return sci_dev->state_handlers->destruct_handler(sci_dev);
1149 }
1150
1151 /**
1152  * isci_remote_device_deconstruct() - This function frees an isci_remote_device.
1153  * @ihost: This parameter specifies the isci host object.
1154  * @idev: This parameter specifies the remote device to be freed.
1155  *
1156  */
1157 static void isci_remote_device_deconstruct(struct isci_host *ihost, struct isci_remote_device *idev)
1158 {
1159         dev_dbg(&ihost->pdev->dev,
1160                 "%s: isci_device = %p\n", __func__, idev);
1161
1162         /* There should not be any outstanding io's. All paths to
1163          * here should go through isci_remote_device_nuke_requests.
1164          * If we hit this condition, we will need a way to complete
1165          * io requests in process */
1166         while (!list_empty(&idev->reqs_in_process)) {
1167
1168                 dev_err(&ihost->pdev->dev,
1169                         "%s: ** request list not empty! **\n", __func__);
1170                 BUG();
1171         }
1172
1173         scic_remote_device_destruct(&idev->sci);
1174         idev->domain_dev->lldd_dev = NULL;
1175         idev->domain_dev = NULL;
1176         idev->isci_port = NULL;
1177         list_del_init(&idev->node);
1178
1179         clear_bit(IDEV_START_PENDING, &idev->flags);
1180         clear_bit(IDEV_STOP_PENDING, &idev->flags);
1181         wake_up(&ihost->eventq);
1182 }
1183
1184 /**
1185  * isci_remote_device_stop_complete() - This function is called by the scic
1186  *    when the remote device stop has completed. We mark the isci device as not
1187  *    ready and remove the isci remote device.
1188  * @ihost: This parameter specifies the isci host object.
1189  * @idev: This parameter specifies the remote device.
1190  * @status: This parameter specifies status of the completion.
1191  *
1192  */
1193 static void isci_remote_device_stop_complete(struct isci_host *ihost,
1194                                              struct isci_remote_device *idev)
1195 {
1196         dev_dbg(&ihost->pdev->dev, "%s: complete idev = %p\n", __func__, idev);
1197
1198         isci_remote_device_change_state(idev, isci_stopped);
1199
1200         /* after stop, we can tear down resources. */
1201         isci_remote_device_deconstruct(ihost, idev);
1202 }
1203
1204 static void scic_sds_remote_device_stopped_state_enter(void *object)
1205 {
1206         struct scic_sds_remote_device *sci_dev;
1207         struct scic_sds_controller *scic;
1208         struct isci_remote_device *idev;
1209         struct isci_host *ihost;
1210         u32 prev_state;
1211
1212         sci_dev = container_of(object, typeof(*sci_dev), parent);
1213         scic = scic_sds_remote_device_get_controller(sci_dev);
1214         ihost = sci_object_get_association(scic);
1215         idev = sci_object_get_association(sci_dev);
1216
1217         SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1218                           SCI_BASE_REMOTE_DEVICE_STATE_STOPPED);
1219
1220         /* If we are entering from the stopping state let the SCI User know that
1221          * the stop operation has completed.
1222          */
1223         prev_state = sci_dev->state_machine.previous_state_id;
1224         if (prev_state == SCI_BASE_REMOTE_DEVICE_STATE_STOPPING)
1225                 isci_remote_device_stop_complete(ihost, idev);
1226
1227         scic_sds_controller_remote_device_stopped(scic, sci_dev);
1228 }
1229
1230 static void scic_sds_remote_device_starting_state_enter(void *object)
1231 {
1232         struct scic_sds_remote_device *sci_dev = container_of(object, typeof(*sci_dev),
1233                                                               parent);
1234         struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1235         struct isci_host *ihost = sci_object_get_association(scic);
1236         struct isci_remote_device *idev = sci_object_get_association(sci_dev);
1237
1238         SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1239                           SCI_BASE_REMOTE_DEVICE_STATE_STARTING);
1240
1241         isci_remote_device_not_ready(ihost, idev,
1242                                      SCIC_REMOTE_DEVICE_NOT_READY_START_REQUESTED);
1243 }
1244
1245 static void scic_sds_remote_device_ready_state_enter(void *object)
1246 {
1247         struct scic_sds_remote_device *sci_dev = container_of(object, typeof(*sci_dev),
1248                                                               parent);
1249         struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1250         struct isci_host *ihost = sci_object_get_association(scic);
1251         struct isci_remote_device *idev = sci_object_get_association(sci_dev);
1252
1253         SET_STATE_HANDLER(sci_dev,
1254                           scic_sds_remote_device_state_handler_table,
1255                           SCI_BASE_REMOTE_DEVICE_STATE_READY);
1256
1257         scic->remote_device_sequence[sci_dev->rnc.remote_node_index]++;
1258
1259         if (sci_dev->has_ready_substate_machine)
1260                 sci_base_state_machine_start(&sci_dev->ready_substate_machine);
1261         else
1262                 isci_remote_device_ready(ihost, idev);
1263 }
1264
1265 static void scic_sds_remote_device_ready_state_exit(void *object)
1266 {
1267         struct scic_sds_remote_device *sci_dev = container_of(object, typeof(*sci_dev),
1268                                                               parent);
1269         if (sci_dev->has_ready_substate_machine)
1270                 sci_base_state_machine_stop(&sci_dev->ready_substate_machine);
1271         else {
1272                 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1273                 struct isci_host *ihost = sci_object_get_association(scic);
1274                 struct isci_remote_device *idev = sci_object_get_association(sci_dev);
1275
1276                 isci_remote_device_not_ready(ihost, idev,
1277                                              SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED);
1278         }
1279 }
1280
1281 static void scic_sds_remote_device_stopping_state_enter(void *object)
1282 {
1283         struct scic_sds_remote_device *sci_dev = (struct scic_sds_remote_device *)object;
1284
1285         SET_STATE_HANDLER(
1286                 sci_dev,
1287                 scic_sds_remote_device_state_handler_table,
1288                 SCI_BASE_REMOTE_DEVICE_STATE_STOPPING
1289                 );
1290 }
1291
1292 static void scic_sds_remote_device_failed_state_enter(void *object)
1293 {
1294         struct scic_sds_remote_device *sci_dev = (struct scic_sds_remote_device *)object;
1295
1296         SET_STATE_HANDLER(
1297                 sci_dev,
1298                 scic_sds_remote_device_state_handler_table,
1299                 SCI_BASE_REMOTE_DEVICE_STATE_FAILED
1300                 );
1301 }
1302
1303 static void scic_sds_remote_device_resetting_state_enter(void *object)
1304 {
1305         struct scic_sds_remote_device *sci_dev = (struct scic_sds_remote_device *)object;
1306
1307         SET_STATE_HANDLER(
1308                 sci_dev,
1309                 scic_sds_remote_device_state_handler_table,
1310                 SCI_BASE_REMOTE_DEVICE_STATE_RESETTING
1311                 );
1312
1313         scic_sds_remote_node_context_suspend(
1314                 &sci_dev->rnc, SCI_SOFTWARE_SUSPENSION, NULL, NULL);
1315 }
1316
1317 static void scic_sds_remote_device_resetting_state_exit(void *object)
1318 {
1319         struct scic_sds_remote_device *sci_dev = (struct scic_sds_remote_device *)object;
1320
1321         scic_sds_remote_node_context_resume(&sci_dev->rnc, NULL, NULL);
1322 }
1323
1324 static void scic_sds_remote_device_final_state_enter(void *object)
1325 {
1326         struct scic_sds_remote_device *sci_dev = (struct scic_sds_remote_device *)object;
1327
1328         SET_STATE_HANDLER(
1329                 sci_dev,
1330                 scic_sds_remote_device_state_handler_table,
1331                 SCI_BASE_REMOTE_DEVICE_STATE_FINAL
1332                 );
1333 }
1334
1335
1336 static const struct sci_base_state scic_sds_remote_device_state_table[] = {
1337         [SCI_BASE_REMOTE_DEVICE_STATE_INITIAL] = {
1338                 .enter_state = scic_sds_remote_device_initial_state_enter,
1339         },
1340         [SCI_BASE_REMOTE_DEVICE_STATE_STOPPED] = {
1341                 .enter_state = scic_sds_remote_device_stopped_state_enter,
1342         },
1343         [SCI_BASE_REMOTE_DEVICE_STATE_STARTING] = {
1344                 .enter_state = scic_sds_remote_device_starting_state_enter,
1345         },
1346         [SCI_BASE_REMOTE_DEVICE_STATE_READY] = {
1347                 .enter_state = scic_sds_remote_device_ready_state_enter,
1348                 .exit_state  = scic_sds_remote_device_ready_state_exit
1349         },
1350         [SCI_BASE_REMOTE_DEVICE_STATE_STOPPING] = {
1351                 .enter_state = scic_sds_remote_device_stopping_state_enter,
1352         },
1353         [SCI_BASE_REMOTE_DEVICE_STATE_FAILED] = {
1354                 .enter_state = scic_sds_remote_device_failed_state_enter,
1355         },
1356         [SCI_BASE_REMOTE_DEVICE_STATE_RESETTING] = {
1357                 .enter_state = scic_sds_remote_device_resetting_state_enter,
1358                 .exit_state  = scic_sds_remote_device_resetting_state_exit
1359         },
1360         [SCI_BASE_REMOTE_DEVICE_STATE_FINAL] = {
1361                 .enter_state = scic_sds_remote_device_final_state_enter,
1362         },
1363 };
1364
1365 /**
1366  * scic_remote_device_construct() - common construction
1367  * @sci_port: SAS/SATA port through which this device is accessed.
1368  * @sci_dev: remote device to construct
1369  *
1370  * This routine just performs benign initialization and does not
1371  * allocate the remote_node_context which is left to
1372  * scic_remote_device_[de]a_construct().  scic_remote_device_destruct()
1373  * frees the remote_node_context(s) for the device.
1374  */
1375 static void scic_remote_device_construct(struct scic_sds_port *sci_port,
1376                                   struct scic_sds_remote_device *sci_dev)
1377 {
1378         sci_dev->owning_port = sci_port;
1379         sci_dev->started_request_count = 0;
1380         sci_dev->parent.private = NULL;
1381
1382         sci_base_state_machine_construct(
1383                 &sci_dev->state_machine,
1384                 &sci_dev->parent,
1385                 scic_sds_remote_device_state_table,
1386                 SCI_BASE_REMOTE_DEVICE_STATE_INITIAL
1387                 );
1388
1389         sci_base_state_machine_start(
1390                 &sci_dev->state_machine
1391                 );
1392
1393         scic_sds_remote_node_context_construct(&sci_dev->rnc,
1394                                                SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX);
1395
1396         sci_object_set_association(&sci_dev->rnc, sci_dev);
1397 }
1398
1399 /**
1400  * scic_remote_device_da_construct() - construct direct attached device.
1401  *
1402  * The information (e.g. IAF, Signature FIS, etc.) necessary to build
1403  * the device is known to the SCI Core since it is contained in the
1404  * scic_phy object.  Remote node context(s) is/are a global resource
1405  * allocated by this routine, freed by scic_remote_device_destruct().
1406  *
1407  * Returns:
1408  * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
1409  * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
1410  * sata-only controller instance.
1411  * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
1412  */
1413 static enum sci_status scic_remote_device_da_construct(struct scic_sds_port *sci_port,
1414                                                        struct scic_sds_remote_device *sci_dev)
1415 {
1416         enum sci_status status;
1417         u16 remote_node_index;
1418         struct domain_device *dev = sci_dev_to_domain(sci_dev);
1419
1420         scic_remote_device_construct(sci_port, sci_dev);
1421
1422         /*
1423          * This information is request to determine how many remote node context
1424          * entries will be needed to store the remote node.
1425          */
1426         sci_dev->is_direct_attached = true;
1427         status = scic_sds_controller_allocate_remote_node_context(sci_port->owning_controller,
1428                                                                   sci_dev,
1429                                                                   &remote_node_index);
1430
1431         if (status != SCI_SUCCESS)
1432                 return status;
1433
1434         sci_dev->rnc.remote_node_index = remote_node_index;
1435
1436         if (dev->dev_type == SAS_END_DEV)
1437                 sci_dev->has_ready_substate_machine = false;
1438         else if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP)) {
1439                 sci_dev->has_ready_substate_machine = true;
1440
1441                 sci_base_state_machine_construct(
1442                                 &sci_dev->ready_substate_machine,
1443                                 &sci_dev->parent,
1444                                 scic_sds_stp_remote_device_ready_substate_table,
1445                                 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
1446         } else if (dev_is_expander(dev)) {
1447                 sci_dev->has_ready_substate_machine = true;
1448
1449                 /* add the SMP ready substate machine construction here */
1450                 sci_base_state_machine_construct(
1451                                 &sci_dev->ready_substate_machine,
1452                                 &sci_dev->parent,
1453                                 scic_sds_smp_remote_device_ready_substate_table,
1454                                 SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
1455         } else
1456                 return SCI_FAILURE_UNSUPPORTED_PROTOCOL;
1457
1458         sci_dev->connection_rate = scic_sds_port_get_max_allowed_speed(sci_port);
1459
1460         /* / @todo Should I assign the port width by reading all of the phys on the port? */
1461         sci_dev->device_port_width = 1;
1462
1463         return SCI_SUCCESS;
1464 }
1465
1466 /**
1467  * scic_remote_device_ea_construct() - construct expander attached device
1468  *
1469  * Remote node context(s) is/are a global resource allocated by this
1470  * routine, freed by scic_remote_device_destruct().
1471  *
1472  * Returns:
1473  * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
1474  * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
1475  * sata-only controller instance.
1476  * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
1477  */
1478 static enum sci_status scic_remote_device_ea_construct(struct scic_sds_port *sci_port,
1479                                                        struct scic_sds_remote_device *sci_dev)
1480 {
1481         struct scic_sds_controller *scic = sci_port->owning_controller;
1482         struct domain_device *dev = sci_dev_to_domain(sci_dev);
1483         enum sci_status status;
1484
1485         scic_remote_device_construct(sci_port, sci_dev);
1486
1487         status = scic_sds_controller_allocate_remote_node_context(
1488                 scic, sci_dev, &sci_dev->rnc.remote_node_index);
1489         if (status != SCI_SUCCESS)
1490                 return status;
1491
1492         if (dev->dev_type == SAS_END_DEV)
1493                 sci_dev->has_ready_substate_machine = false;
1494         else if (dev_is_expander(dev)) {
1495                 sci_dev->has_ready_substate_machine = true;
1496
1497                 /* add the SMP ready substate machine construction here */
1498                 sci_base_state_machine_construct(
1499                                 &sci_dev->ready_substate_machine,
1500                                 &sci_dev->parent,
1501                                 scic_sds_smp_remote_device_ready_substate_table,
1502                                 SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
1503         } else if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP)) {
1504                 sci_dev->has_ready_substate_machine = true;
1505
1506                 sci_base_state_machine_construct(
1507                                 &sci_dev->ready_substate_machine,
1508                                 &sci_dev->parent,
1509                                 scic_sds_stp_remote_device_ready_substate_table,
1510                                 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
1511         }
1512
1513         /*
1514          * For SAS-2 the physical link rate is actually a logical link
1515          * rate that incorporates multiplexing.  The SCU doesn't
1516          * incorporate multiplexing and for the purposes of the
1517          * connection the logical link rate is that same as the
1518          * physical.  Furthermore, the SAS-2 and SAS-1.1 fields overlay
1519          * one another, so this code works for both situations. */
1520         sci_dev->connection_rate = min_t(u16, scic_sds_port_get_max_allowed_speed(sci_port),
1521                                          dev->linkrate);
1522
1523         /* / @todo Should I assign the port width by reading all of the phys on the port? */
1524         sci_dev->device_port_width = 1;
1525
1526         return status;
1527 }
1528
1529 /**
1530  * scic_remote_device_start() - This method will start the supplied remote
1531  *    device.  This method enables normal IO requests to flow through to the
1532  *    remote device.
1533  * @remote_device: This parameter specifies the device to be started.
1534  * @timeout: This parameter specifies the number of milliseconds in which the
1535  *    start operation should complete.
1536  *
1537  * An indication of whether the device was successfully started. SCI_SUCCESS
1538  * This value is returned if the device was successfully started.
1539  * SCI_FAILURE_INVALID_PHY This value is returned if the user attempts to start
1540  * the device when there have been no phys added to it.
1541  */
1542 static enum sci_status scic_remote_device_start(struct scic_sds_remote_device *sci_dev,
1543                                          u32 timeout)
1544 {
1545         return sci_dev->state_handlers->start_handler(sci_dev);
1546 }
1547
1548 static enum sci_status isci_remote_device_construct(struct isci_port *iport,
1549                                                     struct isci_remote_device *idev)
1550 {
1551         struct scic_sds_port *sci_port = iport->sci_port_handle;
1552         struct isci_host *ihost = iport->isci_host;
1553         struct domain_device *dev = idev->domain_dev;
1554         enum sci_status status;
1555
1556         if (dev->parent && dev_is_expander(dev->parent))
1557                 status = scic_remote_device_ea_construct(sci_port, &idev->sci);
1558         else
1559                 status = scic_remote_device_da_construct(sci_port, &idev->sci);
1560
1561         if (status != SCI_SUCCESS) {
1562                 dev_dbg(&ihost->pdev->dev, "%s: construct failed: %d\n",
1563                         __func__, status);
1564
1565                 return status;
1566         }
1567
1568         /* XXX will be killed with sci_base_object removal */
1569         sci_object_set_association(&idev->sci, idev);
1570
1571         /* start the device. */
1572         status = scic_remote_device_start(&idev->sci, ISCI_REMOTE_DEVICE_START_TIMEOUT);
1573
1574         if (status != SCI_SUCCESS)
1575                 dev_warn(&ihost->pdev->dev, "remote device start failed: %d\n",
1576                          status);
1577
1578         return status;
1579 }
1580
1581 void isci_remote_device_nuke_requests(struct isci_host *ihost, struct isci_remote_device *idev)
1582 {
1583         DECLARE_COMPLETION_ONSTACK(aborted_task_completion);
1584
1585         dev_dbg(&ihost->pdev->dev,
1586                 "%s: idev = %p\n", __func__, idev);
1587
1588         /* Cleanup all requests pending for this device. */
1589         isci_terminate_pending_requests(ihost, idev, terminating);
1590
1591         dev_dbg(&ihost->pdev->dev,
1592                 "%s: idev = %p, done\n", __func__, idev);
1593 }
1594
1595 /**
1596  * This function builds the isci_remote_device when a libsas dev_found message
1597  *    is received.
1598  * @isci_host: This parameter specifies the isci host object.
1599  * @port: This parameter specifies the isci_port conected to this device.
1600  *
1601  * pointer to new isci_remote_device.
1602  */
1603 static struct isci_remote_device *
1604 isci_remote_device_alloc(struct isci_host *ihost, struct isci_port *iport)
1605 {
1606         struct isci_remote_device *idev;
1607         int i;
1608
1609         for (i = 0; i < SCI_MAX_REMOTE_DEVICES; i++) {
1610                 idev = &ihost->devices[i];
1611                 if (!test_and_set_bit(IDEV_ALLOCATED, &idev->flags))
1612                         break;
1613         }
1614
1615         if (i >= SCI_MAX_REMOTE_DEVICES) {
1616                 dev_warn(&ihost->pdev->dev, "%s: failed\n", __func__);
1617                 return NULL;
1618         }
1619
1620         if (WARN_ONCE(!list_empty(&idev->reqs_in_process), "found requests in process\n"))
1621                 return NULL;
1622
1623         if (WARN_ONCE(!list_empty(&idev->node), "found non-idle remote device\n"))
1624                 return NULL;
1625
1626         isci_remote_device_change_state(idev, isci_freed);
1627
1628         return idev;
1629 }
1630
1631 /**
1632  * isci_remote_device_ready() - This function is called by the scic when the
1633  *    remote device is ready. We mark the isci device as ready and signal the
1634  *    waiting proccess.
1635  * @ihost: our valid isci_host
1636  * @idev: remote device
1637  *
1638  */
1639 void isci_remote_device_ready(struct isci_host *ihost, struct isci_remote_device *idev)
1640 {
1641         dev_dbg(&ihost->pdev->dev,
1642                 "%s: idev = %p\n", __func__, idev);
1643
1644         isci_remote_device_change_state(idev, isci_ready_for_io);
1645         if (test_and_clear_bit(IDEV_START_PENDING, &idev->flags))
1646                 wake_up(&ihost->eventq);
1647 }
1648
1649 /**
1650  * isci_remote_device_not_ready() - This function is called by the scic when
1651  *    the remote device is not ready. We mark the isci device as ready (not
1652  *    "ready_for_io") and signal the waiting proccess.
1653  * @isci_host: This parameter specifies the isci host object.
1654  * @isci_device: This parameter specifies the remote device
1655  *
1656  */
1657 void isci_remote_device_not_ready(struct isci_host *ihost,
1658                                   struct isci_remote_device *idev, u32 reason)
1659 {
1660         dev_dbg(&ihost->pdev->dev,
1661                 "%s: isci_device = %p\n", __func__, idev);
1662
1663         if (reason == SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED)
1664                 isci_remote_device_change_state(idev, isci_stopping);
1665         else
1666                 /* device ready is actually a "not ready for io" state. */
1667                 isci_remote_device_change_state(idev, isci_ready);
1668 }
1669
1670 /**
1671  * isci_remote_device_stop() - This function is called internally to stop the
1672  *    remote device.
1673  * @isci_host: This parameter specifies the isci host object.
1674  * @isci_device: This parameter specifies the remote device.
1675  *
1676  * The status of the scic request to stop.
1677  */
1678 enum sci_status isci_remote_device_stop(struct isci_host *ihost, struct isci_remote_device *idev)
1679 {
1680         enum sci_status status;
1681         unsigned long flags;
1682
1683         dev_dbg(&ihost->pdev->dev,
1684                 "%s: isci_device = %p\n", __func__, idev);
1685
1686         isci_remote_device_change_state(idev, isci_stopping);
1687
1688         /* Kill all outstanding requests. */
1689         isci_remote_device_nuke_requests(ihost, idev);
1690
1691         set_bit(IDEV_STOP_PENDING, &idev->flags);
1692
1693         spin_lock_irqsave(&ihost->scic_lock, flags);
1694         status = scic_remote_device_stop(&idev->sci, 50);
1695         spin_unlock_irqrestore(&ihost->scic_lock, flags);
1696
1697         /* Wait for the stop complete callback. */
1698         if (status == SCI_SUCCESS) {
1699                 wait_for_device_stop(ihost, idev);
1700                 clear_bit(IDEV_ALLOCATED, &idev->flags);
1701         }
1702
1703         dev_dbg(&ihost->pdev->dev,
1704                 "%s: idev = %p - after completion wait\n",
1705                 __func__, idev);
1706
1707         return status;
1708 }
1709
1710 /**
1711  * isci_remote_device_gone() - This function is called by libsas when a domain
1712  *    device is removed.
1713  * @domain_device: This parameter specifies the libsas domain device.
1714  *
1715  */
1716 void isci_remote_device_gone(struct domain_device *dev)
1717 {
1718         struct isci_host *ihost = dev_to_ihost(dev);
1719         struct isci_remote_device *idev = dev->lldd_dev;
1720
1721         dev_dbg(&ihost->pdev->dev,
1722                 "%s: domain_device = %p, isci_device = %p, isci_port = %p\n",
1723                 __func__, dev, idev, idev->isci_port);
1724
1725         isci_remote_device_stop(ihost, idev);
1726 }
1727
1728
1729 /**
1730  * isci_remote_device_found() - This function is called by libsas when a remote
1731  *    device is discovered. A remote device object is created and started. the
1732  *    function then sleeps until the sci core device started message is
1733  *    received.
1734  * @domain_device: This parameter specifies the libsas domain device.
1735  *
1736  * status, zero indicates success.
1737  */
1738 int isci_remote_device_found(struct domain_device *domain_dev)
1739 {
1740         struct isci_host *isci_host = dev_to_ihost(domain_dev);
1741         struct isci_port *isci_port;
1742         struct isci_phy *isci_phy;
1743         struct asd_sas_port *sas_port;
1744         struct asd_sas_phy *sas_phy;
1745         struct isci_remote_device *isci_device;
1746         enum sci_status status;
1747
1748         dev_dbg(&isci_host->pdev->dev,
1749                 "%s: domain_device = %p\n", __func__, domain_dev);
1750
1751         wait_for_start(isci_host);
1752
1753         sas_port = domain_dev->port;
1754         sas_phy = list_first_entry(&sas_port->phy_list, struct asd_sas_phy,
1755                                    port_phy_el);
1756         isci_phy = to_isci_phy(sas_phy);
1757         isci_port = isci_phy->isci_port;
1758
1759         /* we are being called for a device on this port,
1760          * so it has to come up eventually
1761          */
1762         wait_for_completion(&isci_port->start_complete);
1763
1764         if ((isci_stopping == isci_port_get_state(isci_port)) ||
1765             (isci_stopped == isci_port_get_state(isci_port)))
1766                 return -ENODEV;
1767
1768         isci_device = isci_remote_device_alloc(isci_host, isci_port);
1769         if (!isci_device)
1770                 return -ENODEV;
1771
1772         INIT_LIST_HEAD(&isci_device->node);
1773         domain_dev->lldd_dev = isci_device;
1774         isci_device->domain_dev = domain_dev;
1775         isci_device->isci_port = isci_port;
1776         isci_remote_device_change_state(isci_device, isci_starting);
1777
1778
1779         spin_lock_irq(&isci_host->scic_lock);
1780         list_add_tail(&isci_device->node, &isci_port->remote_dev_list);
1781
1782         set_bit(IDEV_START_PENDING, &isci_device->flags);
1783         status = isci_remote_device_construct(isci_port, isci_device);
1784         spin_unlock_irq(&isci_host->scic_lock);
1785
1786         dev_dbg(&isci_host->pdev->dev,
1787                 "%s: isci_device = %p\n",
1788                 __func__, isci_device);
1789
1790         if (status != SCI_SUCCESS) {
1791
1792                 spin_lock_irq(&isci_host->scic_lock);
1793                 isci_remote_device_deconstruct(
1794                         isci_host,
1795                         isci_device
1796                         );
1797                 spin_unlock_irq(&isci_host->scic_lock);
1798                 return -ENODEV;
1799         }
1800
1801         /* wait for the device ready callback. */
1802         wait_for_device_start(isci_host, isci_device);
1803
1804         return 0;
1805 }
1806 /**
1807  * isci_device_is_reset_pending() - This function will check if there is any
1808  *    pending reset condition on the device.
1809  * @request: This parameter is the isci_device object.
1810  *
1811  * true if there is a reset pending for the device.
1812  */
1813 bool isci_device_is_reset_pending(
1814         struct isci_host *isci_host,
1815         struct isci_remote_device *isci_device)
1816 {
1817         struct isci_request *isci_request;
1818         struct isci_request *tmp_req;
1819         bool reset_is_pending = false;
1820         unsigned long flags;
1821
1822         dev_dbg(&isci_host->pdev->dev,
1823                 "%s: isci_device = %p\n", __func__, isci_device);
1824
1825         spin_lock_irqsave(&isci_host->scic_lock, flags);
1826
1827         /* Check for reset on all pending requests. */
1828         list_for_each_entry_safe(isci_request, tmp_req,
1829                                  &isci_device->reqs_in_process, dev_node) {
1830                 dev_dbg(&isci_host->pdev->dev,
1831                         "%s: isci_device = %p request = %p\n",
1832                         __func__, isci_device, isci_request);
1833
1834                 if (isci_request->ttype == io_task) {
1835                         struct sas_task *task = isci_request_access_task(
1836                                 isci_request);
1837
1838                         spin_lock(&task->task_state_lock);
1839                         if (task->task_state_flags & SAS_TASK_NEED_DEV_RESET)
1840                                 reset_is_pending = true;
1841                         spin_unlock(&task->task_state_lock);
1842                 }
1843         }
1844
1845         spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1846
1847         dev_dbg(&isci_host->pdev->dev,
1848                 "%s: isci_device = %p reset_is_pending = %d\n",
1849                 __func__, isci_device, reset_is_pending);
1850
1851         return reset_is_pending;
1852 }
1853
1854 /**
1855  * isci_device_clear_reset_pending() - This function will clear if any pending
1856  *    reset condition flags on the device.
1857  * @request: This parameter is the isci_device object.
1858  *
1859  * true if there is a reset pending for the device.
1860  */
1861 void isci_device_clear_reset_pending(struct isci_host *ihost, struct isci_remote_device *idev)
1862 {
1863         struct isci_request *isci_request;
1864         struct isci_request *tmp_req;
1865         unsigned long flags = 0;
1866
1867         dev_dbg(&ihost->pdev->dev, "%s: idev=%p, ihost=%p\n",
1868                  __func__, idev, ihost);
1869
1870         spin_lock_irqsave(&ihost->scic_lock, flags);
1871
1872         /* Clear reset pending on all pending requests. */
1873         list_for_each_entry_safe(isci_request, tmp_req,
1874                                  &idev->reqs_in_process, dev_node) {
1875                 dev_dbg(&ihost->pdev->dev, "%s: idev = %p request = %p\n",
1876                          __func__, idev, isci_request);
1877
1878                 if (isci_request->ttype == io_task) {
1879
1880                         unsigned long flags2;
1881                         struct sas_task *task = isci_request_access_task(
1882                                 isci_request);
1883
1884                         spin_lock_irqsave(&task->task_state_lock, flags2);
1885                         task->task_state_flags &= ~SAS_TASK_NEED_DEV_RESET;
1886                         spin_unlock_irqrestore(&task->task_state_lock, flags2);
1887                 }
1888         }
1889         spin_unlock_irqrestore(&ihost->scic_lock, flags);
1890 }