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