isci: Removed sci_base_object from scic_sds_remote_node_context.
[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 = object;
1105
1106         SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1107                           SCI_BASE_REMOTE_DEVICE_STATE_INITIAL);
1108
1109         /* Initial state is a transitional state to the stopped state */
1110         sci_base_state_machine_change_state(&sci_dev->state_machine,
1111                                             SCI_BASE_REMOTE_DEVICE_STATE_STOPPED);
1112 }
1113
1114 /**
1115  * isci_remote_device_change_state() - This function gets the status of the
1116  *    remote_device object.
1117  * @isci_device: This parameter points to the isci_remote_device object
1118  *
1119  * status of the object as a isci_status enum.
1120  */
1121 void isci_remote_device_change_state(
1122         struct isci_remote_device *isci_device,
1123         enum isci_status status)
1124 {
1125         unsigned long flags;
1126
1127         spin_lock_irqsave(&isci_device->state_lock, flags);
1128         isci_device->status = status;
1129         spin_unlock_irqrestore(&isci_device->state_lock, flags);
1130 }
1131
1132 /**
1133  * scic_remote_device_destruct() - free remote node context and destruct
1134  * @remote_device: This parameter specifies the remote device to be destructed.
1135  *
1136  * Remote device objects are a limited resource.  As such, they must be
1137  * protected.  Thus calls to construct and destruct are mutually exclusive and
1138  * non-reentrant. The return value shall indicate if the device was
1139  * successfully destructed or if some failure occurred. enum sci_status This value
1140  * is returned if the device is successfully destructed.
1141  * SCI_FAILURE_INVALID_REMOTE_DEVICE This value is returned if the supplied
1142  * device isn't valid (e.g. it's already been destoryed, the handle isn't
1143  * valid, etc.).
1144  */
1145 static enum sci_status scic_remote_device_destruct(struct scic_sds_remote_device *sci_dev)
1146 {
1147         return sci_dev->state_handlers->destruct_handler(sci_dev);
1148 }
1149
1150 /**
1151  * isci_remote_device_deconstruct() - This function frees an isci_remote_device.
1152  * @ihost: This parameter specifies the isci host object.
1153  * @idev: This parameter specifies the remote device to be freed.
1154  *
1155  */
1156 static void isci_remote_device_deconstruct(struct isci_host *ihost, struct isci_remote_device *idev)
1157 {
1158         dev_dbg(&ihost->pdev->dev,
1159                 "%s: isci_device = %p\n", __func__, idev);
1160
1161         /* There should not be any outstanding io's. All paths to
1162          * here should go through isci_remote_device_nuke_requests.
1163          * If we hit this condition, we will need a way to complete
1164          * io requests in process */
1165         while (!list_empty(&idev->reqs_in_process)) {
1166
1167                 dev_err(&ihost->pdev->dev,
1168                         "%s: ** request list not empty! **\n", __func__);
1169                 BUG();
1170         }
1171
1172         scic_remote_device_destruct(&idev->sci);
1173         idev->domain_dev->lldd_dev = NULL;
1174         idev->domain_dev = NULL;
1175         idev->isci_port = NULL;
1176         list_del_init(&idev->node);
1177
1178         clear_bit(IDEV_START_PENDING, &idev->flags);
1179         clear_bit(IDEV_STOP_PENDING, &idev->flags);
1180         wake_up(&ihost->eventq);
1181 }
1182
1183 /**
1184  * isci_remote_device_stop_complete() - This function is called by the scic
1185  *    when the remote device stop has completed. We mark the isci device as not
1186  *    ready and remove the isci remote device.
1187  * @ihost: This parameter specifies the isci host object.
1188  * @idev: This parameter specifies the remote device.
1189  * @status: This parameter specifies status of the completion.
1190  *
1191  */
1192 static void isci_remote_device_stop_complete(struct isci_host *ihost,
1193                                              struct isci_remote_device *idev)
1194 {
1195         dev_dbg(&ihost->pdev->dev, "%s: complete idev = %p\n", __func__, idev);
1196
1197         isci_remote_device_change_state(idev, isci_stopped);
1198
1199         /* after stop, we can tear down resources. */
1200         isci_remote_device_deconstruct(ihost, idev);
1201 }
1202
1203 static void scic_sds_remote_device_stopped_state_enter(void *object)
1204 {
1205         struct scic_sds_remote_device *sci_dev = object;
1206         struct scic_sds_controller *scic;
1207         struct isci_remote_device *idev;
1208         struct isci_host *ihost;
1209         u32 prev_state;
1210
1211         scic = scic_sds_remote_device_get_controller(sci_dev);
1212         ihost = scic->ihost;
1213         idev = sci_dev_to_idev(sci_dev);
1214
1215         SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1216                           SCI_BASE_REMOTE_DEVICE_STATE_STOPPED);
1217
1218         /* If we are entering from the stopping state let the SCI User know that
1219          * the stop operation has completed.
1220          */
1221         prev_state = sci_dev->state_machine.previous_state_id;
1222         if (prev_state == SCI_BASE_REMOTE_DEVICE_STATE_STOPPING)
1223                 isci_remote_device_stop_complete(ihost, idev);
1224
1225         scic_sds_controller_remote_device_stopped(scic, sci_dev);
1226 }
1227
1228 static void scic_sds_remote_device_starting_state_enter(void *object)
1229 {
1230         struct scic_sds_remote_device *sci_dev = object;
1231         struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1232         struct isci_host *ihost = scic->ihost;
1233         struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
1234
1235         SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1236                           SCI_BASE_REMOTE_DEVICE_STATE_STARTING);
1237
1238         isci_remote_device_not_ready(ihost, idev,
1239                                      SCIC_REMOTE_DEVICE_NOT_READY_START_REQUESTED);
1240 }
1241
1242 static void scic_sds_remote_device_ready_state_enter(void *object)
1243 {
1244         struct scic_sds_remote_device *sci_dev = object;
1245         struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1246         struct isci_host *ihost = scic->ihost;
1247         struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
1248
1249         SET_STATE_HANDLER(sci_dev,
1250                           scic_sds_remote_device_state_handler_table,
1251                           SCI_BASE_REMOTE_DEVICE_STATE_READY);
1252
1253         scic->remote_device_sequence[sci_dev->rnc.remote_node_index]++;
1254
1255         if (sci_dev->has_ready_substate_machine)
1256                 sci_base_state_machine_start(&sci_dev->ready_substate_machine);
1257         else
1258                 isci_remote_device_ready(ihost, idev);
1259 }
1260
1261 static void scic_sds_remote_device_ready_state_exit(void *object)
1262 {
1263         struct scic_sds_remote_device *sci_dev = object;
1264         if (sci_dev->has_ready_substate_machine)
1265                 sci_base_state_machine_stop(&sci_dev->ready_substate_machine);
1266         else {
1267                 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1268                 struct isci_host *ihost = scic->ihost;
1269                 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
1270
1271                 isci_remote_device_not_ready(ihost, idev,
1272                                              SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED);
1273         }
1274 }
1275
1276 static void scic_sds_remote_device_stopping_state_enter(void *object)
1277 {
1278         struct scic_sds_remote_device *sci_dev = object;
1279
1280         SET_STATE_HANDLER(
1281                 sci_dev,
1282                 scic_sds_remote_device_state_handler_table,
1283                 SCI_BASE_REMOTE_DEVICE_STATE_STOPPING
1284                 );
1285 }
1286
1287 static void scic_sds_remote_device_failed_state_enter(void *object)
1288 {
1289         struct scic_sds_remote_device *sci_dev = object;
1290
1291         SET_STATE_HANDLER(
1292                 sci_dev,
1293                 scic_sds_remote_device_state_handler_table,
1294                 SCI_BASE_REMOTE_DEVICE_STATE_FAILED
1295                 );
1296 }
1297
1298 static void scic_sds_remote_device_resetting_state_enter(void *object)
1299 {
1300         struct scic_sds_remote_device *sci_dev = object;
1301
1302         SET_STATE_HANDLER(
1303                 sci_dev,
1304                 scic_sds_remote_device_state_handler_table,
1305                 SCI_BASE_REMOTE_DEVICE_STATE_RESETTING
1306                 );
1307
1308         scic_sds_remote_node_context_suspend(
1309                 &sci_dev->rnc, SCI_SOFTWARE_SUSPENSION, NULL, NULL);
1310 }
1311
1312 static void scic_sds_remote_device_resetting_state_exit(void *object)
1313 {
1314         struct scic_sds_remote_device *sci_dev = object;
1315
1316         scic_sds_remote_node_context_resume(&sci_dev->rnc, NULL, NULL);
1317 }
1318
1319 static void scic_sds_remote_device_final_state_enter(void *object)
1320 {
1321         struct scic_sds_remote_device *sci_dev = object;
1322
1323         SET_STATE_HANDLER(
1324                 sci_dev,
1325                 scic_sds_remote_device_state_handler_table,
1326                 SCI_BASE_REMOTE_DEVICE_STATE_FINAL
1327                 );
1328 }
1329
1330
1331 static const struct sci_base_state scic_sds_remote_device_state_table[] = {
1332         [SCI_BASE_REMOTE_DEVICE_STATE_INITIAL] = {
1333                 .enter_state = scic_sds_remote_device_initial_state_enter,
1334         },
1335         [SCI_BASE_REMOTE_DEVICE_STATE_STOPPED] = {
1336                 .enter_state = scic_sds_remote_device_stopped_state_enter,
1337         },
1338         [SCI_BASE_REMOTE_DEVICE_STATE_STARTING] = {
1339                 .enter_state = scic_sds_remote_device_starting_state_enter,
1340         },
1341         [SCI_BASE_REMOTE_DEVICE_STATE_READY] = {
1342                 .enter_state = scic_sds_remote_device_ready_state_enter,
1343                 .exit_state  = scic_sds_remote_device_ready_state_exit
1344         },
1345         [SCI_BASE_REMOTE_DEVICE_STATE_STOPPING] = {
1346                 .enter_state = scic_sds_remote_device_stopping_state_enter,
1347         },
1348         [SCI_BASE_REMOTE_DEVICE_STATE_FAILED] = {
1349                 .enter_state = scic_sds_remote_device_failed_state_enter,
1350         },
1351         [SCI_BASE_REMOTE_DEVICE_STATE_RESETTING] = {
1352                 .enter_state = scic_sds_remote_device_resetting_state_enter,
1353                 .exit_state  = scic_sds_remote_device_resetting_state_exit
1354         },
1355         [SCI_BASE_REMOTE_DEVICE_STATE_FINAL] = {
1356                 .enter_state = scic_sds_remote_device_final_state_enter,
1357         },
1358 };
1359
1360 /**
1361  * scic_remote_device_construct() - common construction
1362  * @sci_port: SAS/SATA port through which this device is accessed.
1363  * @sci_dev: remote device to construct
1364  *
1365  * This routine just performs benign initialization and does not
1366  * allocate the remote_node_context which is left to
1367  * scic_remote_device_[de]a_construct().  scic_remote_device_destruct()
1368  * frees the remote_node_context(s) for the device.
1369  */
1370 static void scic_remote_device_construct(struct scic_sds_port *sci_port,
1371                                   struct scic_sds_remote_device *sci_dev)
1372 {
1373         sci_dev->owning_port = sci_port;
1374         sci_dev->started_request_count = 0;
1375
1376         sci_base_state_machine_construct(
1377                 &sci_dev->state_machine,
1378                 sci_dev,
1379                 scic_sds_remote_device_state_table,
1380                 SCI_BASE_REMOTE_DEVICE_STATE_INITIAL
1381                 );
1382
1383         sci_base_state_machine_start(
1384                 &sci_dev->state_machine
1385                 );
1386
1387         scic_sds_remote_node_context_construct(&sci_dev->rnc,
1388                                                SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX);
1389 }
1390
1391 /**
1392  * scic_remote_device_da_construct() - construct direct attached device.
1393  *
1394  * The information (e.g. IAF, Signature FIS, etc.) necessary to build
1395  * the device is known to the SCI Core since it is contained in the
1396  * scic_phy object.  Remote node context(s) is/are a global resource
1397  * allocated by this routine, freed by scic_remote_device_destruct().
1398  *
1399  * Returns:
1400  * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
1401  * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
1402  * sata-only controller instance.
1403  * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
1404  */
1405 static enum sci_status scic_remote_device_da_construct(struct scic_sds_port *sci_port,
1406                                                        struct scic_sds_remote_device *sci_dev)
1407 {
1408         enum sci_status status;
1409         u16 remote_node_index;
1410         struct domain_device *dev = sci_dev_to_domain(sci_dev);
1411
1412         scic_remote_device_construct(sci_port, sci_dev);
1413
1414         /*
1415          * This information is request to determine how many remote node context
1416          * entries will be needed to store the remote node.
1417          */
1418         sci_dev->is_direct_attached = true;
1419         status = scic_sds_controller_allocate_remote_node_context(sci_port->owning_controller,
1420                                                                   sci_dev,
1421                                                                   &remote_node_index);
1422
1423         if (status != SCI_SUCCESS)
1424                 return status;
1425
1426         sci_dev->rnc.remote_node_index = remote_node_index;
1427
1428         if (dev->dev_type == SAS_END_DEV)
1429                 sci_dev->has_ready_substate_machine = false;
1430         else if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP)) {
1431                 sci_dev->has_ready_substate_machine = true;
1432
1433                 sci_base_state_machine_construct(
1434                                 &sci_dev->ready_substate_machine,
1435                                 sci_dev,
1436                                 scic_sds_stp_remote_device_ready_substate_table,
1437                                 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
1438         } else if (dev_is_expander(dev)) {
1439                 sci_dev->has_ready_substate_machine = true;
1440
1441                 /* add the SMP ready substate machine construction here */
1442                 sci_base_state_machine_construct(
1443                                 &sci_dev->ready_substate_machine,
1444                                 sci_dev,
1445                                 scic_sds_smp_remote_device_ready_substate_table,
1446                                 SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
1447         } else
1448                 return SCI_FAILURE_UNSUPPORTED_PROTOCOL;
1449
1450         sci_dev->connection_rate = scic_sds_port_get_max_allowed_speed(sci_port);
1451
1452         /* / @todo Should I assign the port width by reading all of the phys on the port? */
1453         sci_dev->device_port_width = 1;
1454
1455         return SCI_SUCCESS;
1456 }
1457
1458 /**
1459  * scic_remote_device_ea_construct() - construct expander attached device
1460  *
1461  * Remote node context(s) is/are a global resource allocated by this
1462  * routine, freed by scic_remote_device_destruct().
1463  *
1464  * Returns:
1465  * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
1466  * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
1467  * sata-only controller instance.
1468  * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
1469  */
1470 static enum sci_status scic_remote_device_ea_construct(struct scic_sds_port *sci_port,
1471                                                        struct scic_sds_remote_device *sci_dev)
1472 {
1473         struct scic_sds_controller *scic = sci_port->owning_controller;
1474         struct domain_device *dev = sci_dev_to_domain(sci_dev);
1475         enum sci_status status;
1476
1477         scic_remote_device_construct(sci_port, sci_dev);
1478
1479         status = scic_sds_controller_allocate_remote_node_context(
1480                 scic, sci_dev, &sci_dev->rnc.remote_node_index);
1481         if (status != SCI_SUCCESS)
1482                 return status;
1483
1484         if (dev->dev_type == SAS_END_DEV)
1485                 sci_dev->has_ready_substate_machine = false;
1486         else if (dev_is_expander(dev)) {
1487                 sci_dev->has_ready_substate_machine = true;
1488
1489                 /* add the SMP ready substate machine construction here */
1490                 sci_base_state_machine_construct(
1491                                 &sci_dev->ready_substate_machine,
1492                                 sci_dev,
1493                                 scic_sds_smp_remote_device_ready_substate_table,
1494                                 SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
1495         } else if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP)) {
1496                 sci_dev->has_ready_substate_machine = true;
1497
1498                 sci_base_state_machine_construct(
1499                                 &sci_dev->ready_substate_machine,
1500                                 sci_dev,
1501                                 scic_sds_stp_remote_device_ready_substate_table,
1502                                 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
1503         }
1504
1505         /*
1506          * For SAS-2 the physical link rate is actually a logical link
1507          * rate that incorporates multiplexing.  The SCU doesn't
1508          * incorporate multiplexing and for the purposes of the
1509          * connection the logical link rate is that same as the
1510          * physical.  Furthermore, the SAS-2 and SAS-1.1 fields overlay
1511          * one another, so this code works for both situations. */
1512         sci_dev->connection_rate = min_t(u16, scic_sds_port_get_max_allowed_speed(sci_port),
1513                                          dev->linkrate);
1514
1515         /* / @todo Should I assign the port width by reading all of the phys on the port? */
1516         sci_dev->device_port_width = 1;
1517
1518         return status;
1519 }
1520
1521 /**
1522  * scic_remote_device_start() - This method will start the supplied remote
1523  *    device.  This method enables normal IO requests to flow through to the
1524  *    remote device.
1525  * @remote_device: This parameter specifies the device to be started.
1526  * @timeout: This parameter specifies the number of milliseconds in which the
1527  *    start operation should complete.
1528  *
1529  * An indication of whether the device was successfully started. SCI_SUCCESS
1530  * This value is returned if the device was successfully started.
1531  * SCI_FAILURE_INVALID_PHY This value is returned if the user attempts to start
1532  * the device when there have been no phys added to it.
1533  */
1534 static enum sci_status scic_remote_device_start(struct scic_sds_remote_device *sci_dev,
1535                                          u32 timeout)
1536 {
1537         return sci_dev->state_handlers->start_handler(sci_dev);
1538 }
1539
1540 static enum sci_status isci_remote_device_construct(struct isci_port *iport,
1541                                                     struct isci_remote_device *idev)
1542 {
1543         struct scic_sds_port *sci_port = iport->sci_port_handle;
1544         struct isci_host *ihost = iport->isci_host;
1545         struct domain_device *dev = idev->domain_dev;
1546         enum sci_status status;
1547
1548         if (dev->parent && dev_is_expander(dev->parent))
1549                 status = scic_remote_device_ea_construct(sci_port, &idev->sci);
1550         else
1551                 status = scic_remote_device_da_construct(sci_port, &idev->sci);
1552
1553         if (status != SCI_SUCCESS) {
1554                 dev_dbg(&ihost->pdev->dev, "%s: construct failed: %d\n",
1555                         __func__, status);
1556
1557                 return status;
1558         }
1559
1560         /* start the device. */
1561         status = scic_remote_device_start(&idev->sci, ISCI_REMOTE_DEVICE_START_TIMEOUT);
1562
1563         if (status != SCI_SUCCESS)
1564                 dev_warn(&ihost->pdev->dev, "remote device start failed: %d\n",
1565                          status);
1566
1567         return status;
1568 }
1569
1570 void isci_remote_device_nuke_requests(struct isci_host *ihost, struct isci_remote_device *idev)
1571 {
1572         DECLARE_COMPLETION_ONSTACK(aborted_task_completion);
1573
1574         dev_dbg(&ihost->pdev->dev,
1575                 "%s: idev = %p\n", __func__, idev);
1576
1577         /* Cleanup all requests pending for this device. */
1578         isci_terminate_pending_requests(ihost, idev, terminating);
1579
1580         dev_dbg(&ihost->pdev->dev,
1581                 "%s: idev = %p, done\n", __func__, idev);
1582 }
1583
1584 /**
1585  * This function builds the isci_remote_device when a libsas dev_found message
1586  *    is received.
1587  * @isci_host: This parameter specifies the isci host object.
1588  * @port: This parameter specifies the isci_port conected to this device.
1589  *
1590  * pointer to new isci_remote_device.
1591  */
1592 static struct isci_remote_device *
1593 isci_remote_device_alloc(struct isci_host *ihost, struct isci_port *iport)
1594 {
1595         struct isci_remote_device *idev;
1596         int i;
1597
1598         for (i = 0; i < SCI_MAX_REMOTE_DEVICES; i++) {
1599                 idev = &ihost->devices[i];
1600                 if (!test_and_set_bit(IDEV_ALLOCATED, &idev->flags))
1601                         break;
1602         }
1603
1604         if (i >= SCI_MAX_REMOTE_DEVICES) {
1605                 dev_warn(&ihost->pdev->dev, "%s: failed\n", __func__);
1606                 return NULL;
1607         }
1608
1609         if (WARN_ONCE(!list_empty(&idev->reqs_in_process), "found requests in process\n"))
1610                 return NULL;
1611
1612         if (WARN_ONCE(!list_empty(&idev->node), "found non-idle remote device\n"))
1613                 return NULL;
1614
1615         isci_remote_device_change_state(idev, isci_freed);
1616
1617         return idev;
1618 }
1619
1620 /**
1621  * isci_remote_device_ready() - This function is called by the scic when the
1622  *    remote device is ready. We mark the isci device as ready and signal the
1623  *    waiting proccess.
1624  * @ihost: our valid isci_host
1625  * @idev: remote device
1626  *
1627  */
1628 void isci_remote_device_ready(struct isci_host *ihost, struct isci_remote_device *idev)
1629 {
1630         dev_dbg(&ihost->pdev->dev,
1631                 "%s: idev = %p\n", __func__, idev);
1632
1633         isci_remote_device_change_state(idev, isci_ready_for_io);
1634         if (test_and_clear_bit(IDEV_START_PENDING, &idev->flags))
1635                 wake_up(&ihost->eventq);
1636 }
1637
1638 /**
1639  * isci_remote_device_not_ready() - This function is called by the scic when
1640  *    the remote device is not ready. We mark the isci device as ready (not
1641  *    "ready_for_io") and signal the waiting proccess.
1642  * @isci_host: This parameter specifies the isci host object.
1643  * @isci_device: This parameter specifies the remote device
1644  *
1645  */
1646 void isci_remote_device_not_ready(struct isci_host *ihost,
1647                                   struct isci_remote_device *idev, u32 reason)
1648 {
1649         dev_dbg(&ihost->pdev->dev,
1650                 "%s: isci_device = %p\n", __func__, idev);
1651
1652         if (reason == SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED)
1653                 isci_remote_device_change_state(idev, isci_stopping);
1654         else
1655                 /* device ready is actually a "not ready for io" state. */
1656                 isci_remote_device_change_state(idev, isci_ready);
1657 }
1658
1659 /**
1660  * isci_remote_device_stop() - This function is called internally to stop the
1661  *    remote device.
1662  * @isci_host: This parameter specifies the isci host object.
1663  * @isci_device: This parameter specifies the remote device.
1664  *
1665  * The status of the scic request to stop.
1666  */
1667 enum sci_status isci_remote_device_stop(struct isci_host *ihost, struct isci_remote_device *idev)
1668 {
1669         enum sci_status status;
1670         unsigned long flags;
1671
1672         dev_dbg(&ihost->pdev->dev,
1673                 "%s: isci_device = %p\n", __func__, idev);
1674
1675         isci_remote_device_change_state(idev, isci_stopping);
1676
1677         /* Kill all outstanding requests. */
1678         isci_remote_device_nuke_requests(ihost, idev);
1679
1680         set_bit(IDEV_STOP_PENDING, &idev->flags);
1681
1682         spin_lock_irqsave(&ihost->scic_lock, flags);
1683         status = scic_remote_device_stop(&idev->sci, 50);
1684         spin_unlock_irqrestore(&ihost->scic_lock, flags);
1685
1686         /* Wait for the stop complete callback. */
1687         if (status == SCI_SUCCESS) {
1688                 wait_for_device_stop(ihost, idev);
1689                 clear_bit(IDEV_ALLOCATED, &idev->flags);
1690         }
1691
1692         dev_dbg(&ihost->pdev->dev,
1693                 "%s: idev = %p - after completion wait\n",
1694                 __func__, idev);
1695
1696         return status;
1697 }
1698
1699 /**
1700  * isci_remote_device_gone() - This function is called by libsas when a domain
1701  *    device is removed.
1702  * @domain_device: This parameter specifies the libsas domain device.
1703  *
1704  */
1705 void isci_remote_device_gone(struct domain_device *dev)
1706 {
1707         struct isci_host *ihost = dev_to_ihost(dev);
1708         struct isci_remote_device *idev = dev->lldd_dev;
1709
1710         dev_dbg(&ihost->pdev->dev,
1711                 "%s: domain_device = %p, isci_device = %p, isci_port = %p\n",
1712                 __func__, dev, idev, idev->isci_port);
1713
1714         isci_remote_device_stop(ihost, idev);
1715 }
1716
1717
1718 /**
1719  * isci_remote_device_found() - This function is called by libsas when a remote
1720  *    device is discovered. A remote device object is created and started. the
1721  *    function then sleeps until the sci core device started message is
1722  *    received.
1723  * @domain_device: This parameter specifies the libsas domain device.
1724  *
1725  * status, zero indicates success.
1726  */
1727 int isci_remote_device_found(struct domain_device *domain_dev)
1728 {
1729         struct isci_host *isci_host = dev_to_ihost(domain_dev);
1730         struct isci_port *isci_port;
1731         struct isci_phy *isci_phy;
1732         struct asd_sas_port *sas_port;
1733         struct asd_sas_phy *sas_phy;
1734         struct isci_remote_device *isci_device;
1735         enum sci_status status;
1736
1737         dev_dbg(&isci_host->pdev->dev,
1738                 "%s: domain_device = %p\n", __func__, domain_dev);
1739
1740         wait_for_start(isci_host);
1741
1742         sas_port = domain_dev->port;
1743         sas_phy = list_first_entry(&sas_port->phy_list, struct asd_sas_phy,
1744                                    port_phy_el);
1745         isci_phy = to_isci_phy(sas_phy);
1746         isci_port = isci_phy->isci_port;
1747
1748         /* we are being called for a device on this port,
1749          * so it has to come up eventually
1750          */
1751         wait_for_completion(&isci_port->start_complete);
1752
1753         if ((isci_stopping == isci_port_get_state(isci_port)) ||
1754             (isci_stopped == isci_port_get_state(isci_port)))
1755                 return -ENODEV;
1756
1757         isci_device = isci_remote_device_alloc(isci_host, isci_port);
1758         if (!isci_device)
1759                 return -ENODEV;
1760
1761         INIT_LIST_HEAD(&isci_device->node);
1762         domain_dev->lldd_dev = isci_device;
1763         isci_device->domain_dev = domain_dev;
1764         isci_device->isci_port = isci_port;
1765         isci_remote_device_change_state(isci_device, isci_starting);
1766
1767
1768         spin_lock_irq(&isci_host->scic_lock);
1769         list_add_tail(&isci_device->node, &isci_port->remote_dev_list);
1770
1771         set_bit(IDEV_START_PENDING, &isci_device->flags);
1772         status = isci_remote_device_construct(isci_port, isci_device);
1773         spin_unlock_irq(&isci_host->scic_lock);
1774
1775         dev_dbg(&isci_host->pdev->dev,
1776                 "%s: isci_device = %p\n",
1777                 __func__, isci_device);
1778
1779         if (status != SCI_SUCCESS) {
1780
1781                 spin_lock_irq(&isci_host->scic_lock);
1782                 isci_remote_device_deconstruct(
1783                         isci_host,
1784                         isci_device
1785                         );
1786                 spin_unlock_irq(&isci_host->scic_lock);
1787                 return -ENODEV;
1788         }
1789
1790         /* wait for the device ready callback. */
1791         wait_for_device_start(isci_host, isci_device);
1792
1793         return 0;
1794 }
1795 /**
1796  * isci_device_is_reset_pending() - This function will check if there is any
1797  *    pending reset condition on the device.
1798  * @request: This parameter is the isci_device object.
1799  *
1800  * true if there is a reset pending for the device.
1801  */
1802 bool isci_device_is_reset_pending(
1803         struct isci_host *isci_host,
1804         struct isci_remote_device *isci_device)
1805 {
1806         struct isci_request *isci_request;
1807         struct isci_request *tmp_req;
1808         bool reset_is_pending = false;
1809         unsigned long flags;
1810
1811         dev_dbg(&isci_host->pdev->dev,
1812                 "%s: isci_device = %p\n", __func__, isci_device);
1813
1814         spin_lock_irqsave(&isci_host->scic_lock, flags);
1815
1816         /* Check for reset on all pending requests. */
1817         list_for_each_entry_safe(isci_request, tmp_req,
1818                                  &isci_device->reqs_in_process, dev_node) {
1819                 dev_dbg(&isci_host->pdev->dev,
1820                         "%s: isci_device = %p request = %p\n",
1821                         __func__, isci_device, isci_request);
1822
1823                 if (isci_request->ttype == io_task) {
1824                         struct sas_task *task = isci_request_access_task(
1825                                 isci_request);
1826
1827                         spin_lock(&task->task_state_lock);
1828                         if (task->task_state_flags & SAS_TASK_NEED_DEV_RESET)
1829                                 reset_is_pending = true;
1830                         spin_unlock(&task->task_state_lock);
1831                 }
1832         }
1833
1834         spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1835
1836         dev_dbg(&isci_host->pdev->dev,
1837                 "%s: isci_device = %p reset_is_pending = %d\n",
1838                 __func__, isci_device, reset_is_pending);
1839
1840         return reset_is_pending;
1841 }
1842
1843 /**
1844  * isci_device_clear_reset_pending() - This function will clear if any pending
1845  *    reset condition flags on the device.
1846  * @request: This parameter is the isci_device object.
1847  *
1848  * true if there is a reset pending for the device.
1849  */
1850 void isci_device_clear_reset_pending(struct isci_host *ihost, struct isci_remote_device *idev)
1851 {
1852         struct isci_request *isci_request;
1853         struct isci_request *tmp_req;
1854         unsigned long flags = 0;
1855
1856         dev_dbg(&ihost->pdev->dev, "%s: idev=%p, ihost=%p\n",
1857                  __func__, idev, ihost);
1858
1859         spin_lock_irqsave(&ihost->scic_lock, flags);
1860
1861         /* Clear reset pending on all pending requests. */
1862         list_for_each_entry_safe(isci_request, tmp_req,
1863                                  &idev->reqs_in_process, dev_node) {
1864                 dev_dbg(&ihost->pdev->dev, "%s: idev = %p request = %p\n",
1865                          __func__, idev, isci_request);
1866
1867                 if (isci_request->ttype == io_task) {
1868
1869                         unsigned long flags2;
1870                         struct sas_task *task = isci_request_access_task(
1871                                 isci_request);
1872
1873                         spin_lock_irqsave(&task->task_state_lock, flags2);
1874                         task->task_state_flags &= ~SAS_TASK_NEED_DEV_RESET;
1875                         spin_unlock_irqrestore(&task->task_state_lock, flags2);
1876                 }
1877         }
1878         spin_unlock_irqrestore(&ihost->scic_lock, flags);
1879 }