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