isci: removing intel_*.h headers
[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 <scsi/sas.h>
56 #include "isci.h"
57 #include "port.h"
58 #include "remote_device.h"
59 #include "request.h"
60 #include "scic_controller.h"
61 #include "scic_io_request.h"
62 #include "scic_phy.h"
63 #include "scic_port.h"
64 #include "scic_sds_controller.h"
65 #include "scic_sds_phy.h"
66 #include "scic_sds_port.h"
67 #include "remote_node_context.h"
68 #include "scic_sds_request.h"
69 #include "sci_environment.h"
70 #include "sci_util.h"
71 #include "scu_event_codes.h"
72 #include "task.h"
73
74 /**
75  * isci_remote_device_change_state() - This function gets the status of the
76  *    remote_device object.
77  * @isci_device: This parameter points to the isci_remote_device object
78  *
79  * status of the object as a isci_status enum.
80  */
81 void isci_remote_device_change_state(
82         struct isci_remote_device *isci_device,
83         enum isci_status status)
84 {
85         unsigned long flags;
86
87         spin_lock_irqsave(&isci_device->state_lock, flags);
88         isci_device->status = status;
89         spin_unlock_irqrestore(&isci_device->state_lock, flags);
90 }
91
92 /**
93  * isci_remote_device_not_ready() - This function is called by the scic when
94  *    the remote device is not ready. We mark the isci device as ready (not
95  *    "ready_for_io") and signal the waiting proccess.
96  * @isci_host: This parameter specifies the isci host object.
97  * @isci_device: This parameter specifies the remote device
98  *
99  */
100 static void isci_remote_device_not_ready(struct isci_host *ihost,
101                                   struct isci_remote_device *idev, u32 reason)
102 {
103         dev_dbg(&ihost->pdev->dev,
104                 "%s: isci_device = %p\n", __func__, idev);
105
106         if (reason == SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED)
107                 isci_remote_device_change_state(idev, isci_stopping);
108         else
109                 /* device ready is actually a "not ready for io" state. */
110                 isci_remote_device_change_state(idev, isci_ready);
111 }
112
113 /**
114  * isci_remote_device_ready() - This function is called by the scic when the
115  *    remote device is ready. We mark the isci device as ready and signal the
116  *    waiting proccess.
117  * @ihost: our valid isci_host
118  * @idev: remote device
119  *
120  */
121 static void isci_remote_device_ready(struct isci_host *ihost, struct isci_remote_device *idev)
122 {
123         dev_dbg(&ihost->pdev->dev,
124                 "%s: idev = %p\n", __func__, idev);
125
126         isci_remote_device_change_state(idev, isci_ready_for_io);
127         if (test_and_clear_bit(IDEV_START_PENDING, &idev->flags))
128                 wake_up(&ihost->eventq);
129 }
130
131 /* called once the remote node context is ready to be freed.
132  * The remote device can now report that its stop operation is complete. none
133  */
134 static void rnc_destruct_done(void *_dev)
135 {
136         struct scic_sds_remote_device *sci_dev = _dev;
137
138         BUG_ON(sci_dev->started_request_count != 0);
139         sci_base_state_machine_change_state(&sci_dev->state_machine,
140                                             SCI_BASE_REMOTE_DEVICE_STATE_STOPPED);
141 }
142
143 static enum sci_status scic_sds_remote_device_terminate_requests(struct scic_sds_remote_device *sci_dev)
144 {
145         struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
146         u32 i, request_count = sci_dev->started_request_count;
147         enum sci_status status  = SCI_SUCCESS;
148
149         for (i = 0; i < SCI_MAX_IO_REQUESTS && i < request_count; i++) {
150                 struct scic_sds_request *sci_req;
151                 enum sci_status s;
152
153                 sci_req = scic->io_request_table[i];
154                 if (!sci_req || sci_req->target_device != sci_dev)
155                         continue;
156                 s = scic_controller_terminate_request(scic, sci_dev, sci_req);
157                 if (s != SCI_SUCCESS)
158                         status = s;
159         }
160
161         return status;
162 }
163
164 enum sci_status scic_remote_device_stop(struct scic_sds_remote_device *sci_dev,
165                                         u32 timeout)
166 {
167         struct sci_base_state_machine *sm = &sci_dev->state_machine;
168         enum scic_sds_remote_device_states state = sm->current_state_id;
169
170         switch (state) {
171         case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL:
172         case SCI_BASE_REMOTE_DEVICE_STATE_FAILED:
173         case SCI_BASE_REMOTE_DEVICE_STATE_FINAL:
174         default:
175                 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
176                          __func__, state);
177                 return SCI_FAILURE_INVALID_STATE;
178         case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED:
179                 return SCI_SUCCESS;
180         case SCI_BASE_REMOTE_DEVICE_STATE_STARTING:
181                 /* device not started so there had better be no requests */
182                 BUG_ON(sci_dev->started_request_count != 0);
183                 scic_sds_remote_node_context_destruct(&sci_dev->rnc,
184                                                       rnc_destruct_done, sci_dev);
185                 /* Transition to the stopping state and wait for the
186                  * remote node to complete being posted and invalidated.
187                  */
188                 sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_STOPPING);
189                 return SCI_SUCCESS;
190         case SCI_BASE_REMOTE_DEVICE_STATE_READY:
191         case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
192         case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
193         case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ:
194         case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
195         case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
196         case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
197         case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
198                 sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_STOPPING);
199                 if (sci_dev->started_request_count == 0) {
200                         scic_sds_remote_node_context_destruct(&sci_dev->rnc,
201                                                               rnc_destruct_done, sci_dev);
202                         return SCI_SUCCESS;
203                 } else
204                         return scic_sds_remote_device_terminate_requests(sci_dev);
205                 break;
206         case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING:
207                 /* All requests should have been terminated, but if there is an
208                  * attempt to stop a device already in the stopping state, then
209                  * try again to terminate.
210                  */
211                 return scic_sds_remote_device_terminate_requests(sci_dev);
212         case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING:
213                 sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_STOPPING);
214                 return SCI_SUCCESS;
215         }
216 }
217
218 enum sci_status scic_remote_device_reset(struct scic_sds_remote_device *sci_dev)
219 {
220         struct sci_base_state_machine *sm = &sci_dev->state_machine;
221         enum scic_sds_remote_device_states state = sm->current_state_id;
222
223         switch (state) {
224         case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL:
225         case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED:
226         case SCI_BASE_REMOTE_DEVICE_STATE_STARTING:
227         case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
228         case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
229         case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING:
230         case SCI_BASE_REMOTE_DEVICE_STATE_FAILED:
231         case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING:
232         case SCI_BASE_REMOTE_DEVICE_STATE_FINAL:
233         default:
234                 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
235                          __func__, state);
236                 return SCI_FAILURE_INVALID_STATE;
237         case SCI_BASE_REMOTE_DEVICE_STATE_READY:
238         case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
239         case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
240         case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ:
241         case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
242         case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
243                 sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_RESETTING);
244                 return SCI_SUCCESS;
245         }
246 }
247
248 enum sci_status scic_remote_device_reset_complete(struct scic_sds_remote_device *sci_dev)
249 {
250         struct sci_base_state_machine *sm = &sci_dev->state_machine;
251         enum scic_sds_remote_device_states state = sm->current_state_id;
252
253         if (state != SCI_BASE_REMOTE_DEVICE_STATE_RESETTING) {
254                 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
255                          __func__, state);
256                 return SCI_FAILURE_INVALID_STATE;
257         }
258
259         sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_READY);
260         return SCI_SUCCESS;
261 }
262
263 enum sci_status scic_sds_remote_device_suspend(struct scic_sds_remote_device *sci_dev,
264                                                u32 suspend_type)
265 {
266         struct sci_base_state_machine *sm = &sci_dev->state_machine;
267         enum scic_sds_remote_device_states state = sm->current_state_id;
268
269         if (state != SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD) {
270                 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
271                          __func__, state);
272                 return SCI_FAILURE_INVALID_STATE;
273         }
274
275         return scic_sds_remote_node_context_suspend(&sci_dev->rnc,
276                                                     suspend_type, NULL, NULL);
277 }
278
279 enum sci_status scic_sds_remote_device_frame_handler(struct scic_sds_remote_device *sci_dev,
280                                                      u32 frame_index)
281 {
282         struct sci_base_state_machine *sm = &sci_dev->state_machine;
283         enum scic_sds_remote_device_states state = sm->current_state_id;
284         struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
285         enum sci_status status;
286
287         switch (state) {
288         case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL:
289         case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED:
290         case SCI_BASE_REMOTE_DEVICE_STATE_STARTING:
291         case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
292         case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
293         case SCI_BASE_REMOTE_DEVICE_STATE_FINAL:
294         default:
295                 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
296                          __func__, state);
297                 /* Return the frame back to the controller */
298                 scic_sds_controller_release_frame(scic, frame_index);
299                 return SCI_FAILURE_INVALID_STATE;
300         case SCI_BASE_REMOTE_DEVICE_STATE_READY:
301         case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
302         case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
303         case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING:
304         case SCI_BASE_REMOTE_DEVICE_STATE_FAILED:
305         case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING: {
306                 struct scic_sds_request *sci_req;
307                 struct ssp_frame_hdr hdr;
308                 void *frame_header;
309                 ssize_t word_cnt;
310
311                 status = scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
312                                                                        frame_index,
313                                                                        &frame_header);
314                 if (status != SCI_SUCCESS)
315                         return status;
316
317                 word_cnt = sizeof(hdr) / sizeof(u32);
318                 sci_swab32_cpy(&hdr, frame_header, word_cnt);
319
320                 sci_req = scic_request_by_tag(scic, be16_to_cpu(hdr.tag));
321                 if (sci_req && sci_req->target_device == sci_dev) {
322                         /* The IO request is now in charge of releasing the frame */
323                         status = sci_req->state_handlers->frame_handler(sci_req,
324                                                                         frame_index);
325                 } else {
326                         /* We could not map this tag to a valid IO
327                          * request Just toss the frame and continue
328                          */
329                         scic_sds_controller_release_frame(scic, frame_index);
330                 }
331                 break;
332         }
333         case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ: {
334                 struct dev_to_host_fis *hdr;
335
336                 status = scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
337                                                                        frame_index,
338                                                                        (void **)&hdr);
339                 if (status != SCI_SUCCESS)
340                         return status;
341
342                 if (hdr->fis_type == FIS_SETDEVBITS &&
343                     (hdr->status & ATA_ERR)) {
344                         sci_dev->not_ready_reason = SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED;
345
346                         /* TODO Check sactive and complete associated IO if any. */
347                         sci_base_state_machine_change_state(sm, SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR);
348                 } else if (hdr->fis_type == FIS_REGD2H &&
349                            (hdr->status & ATA_ERR)) {
350                         /*
351                          * Some devices return D2H FIS when an NCQ error is detected.
352                          * Treat this like an SDB error FIS ready reason.
353                          */
354                         sci_dev->not_ready_reason = SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED;
355                         sci_base_state_machine_change_state(&sci_dev->state_machine,
356                                                             SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR);
357                 } else
358                         status = SCI_FAILURE;
359
360                 scic_sds_controller_release_frame(scic, frame_index);
361                 break;
362         }
363         case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
364         case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
365                 /* The device does not process any UF received from the hardware while
366                  * in this state.  All unsolicited frames are forwarded to the io request
367                  * object.
368                  */
369                 status = scic_sds_io_request_frame_handler(sci_dev->working_request, frame_index);
370                 break;
371         }
372
373         return status;
374 }
375
376 static bool is_remote_device_ready(struct scic_sds_remote_device *sci_dev)
377 {
378
379         struct sci_base_state_machine *sm = &sci_dev->state_machine;
380         enum scic_sds_remote_device_states state = sm->current_state_id;
381
382         switch (state) {
383         case SCI_BASE_REMOTE_DEVICE_STATE_READY:
384         case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
385         case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
386         case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ:
387         case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
388         case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
389         case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
390         case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
391                 return true;
392         default:
393                 return false;
394         }
395 }
396
397 enum sci_status scic_sds_remote_device_event_handler(struct scic_sds_remote_device *sci_dev,
398                                                      u32 event_code)
399 {
400         struct sci_base_state_machine *sm = &sci_dev->state_machine;
401         enum scic_sds_remote_device_states state = sm->current_state_id;
402         enum sci_status status;
403
404         switch (scu_get_event_type(event_code)) {
405         case SCU_EVENT_TYPE_RNC_OPS_MISC:
406         case SCU_EVENT_TYPE_RNC_SUSPEND_TX:
407         case SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX:
408                 status = scic_sds_remote_node_context_event_handler(&sci_dev->rnc, event_code);
409                 break;
410         case SCU_EVENT_TYPE_PTX_SCHEDULE_EVENT:
411                 if (scu_get_event_code(event_code) == SCU_EVENT_IT_NEXUS_TIMEOUT) {
412                         status = SCI_SUCCESS;
413
414                         /* Suspend the associated RNC */
415                         scic_sds_remote_node_context_suspend(&sci_dev->rnc,
416                                                               SCI_SOFTWARE_SUSPENSION,
417                                                               NULL, NULL);
418
419                         dev_dbg(scirdev_to_dev(sci_dev),
420                                 "%s: device: %p event code: %x: %s\n",
421                                 __func__, sci_dev, event_code,
422                                 is_remote_device_ready(sci_dev)
423                                 ? "I_T_Nexus_Timeout event"
424                                 : "I_T_Nexus_Timeout event in wrong state");
425
426                         break;
427                 }
428         /* Else, fall through and treat as unhandled... */
429         default:
430                 dev_dbg(scirdev_to_dev(sci_dev),
431                         "%s: device: %p event code: %x: %s\n",
432                         __func__, sci_dev, event_code,
433                         is_remote_device_ready(sci_dev)
434                         ? "unexpected event"
435                         : "unexpected event in wrong state");
436                 status = SCI_FAILURE_INVALID_STATE;
437                 break;
438         }
439
440         if (status != SCI_SUCCESS)
441                 return status;
442
443         if (state == SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE) {
444
445                 /* We pick up suspension events to handle specifically to this
446                  * state. We resume the RNC right away.
447                  */
448                 if (scu_get_event_type(event_code) == SCU_EVENT_TYPE_RNC_SUSPEND_TX ||
449                     scu_get_event_type(event_code) == SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX)
450                         status = scic_sds_remote_node_context_resume(&sci_dev->rnc, NULL, NULL);
451         }
452
453         return status;
454 }
455
456 static void scic_sds_remote_device_start_request(struct scic_sds_remote_device *sci_dev,
457                                                  struct scic_sds_request *sci_req,
458                                                  enum sci_status status)
459 {
460         struct scic_sds_port *sci_port = sci_dev->owning_port;
461
462         /* cleanup requests that failed after starting on the port */
463         if (status != SCI_SUCCESS)
464                 scic_sds_port_complete_io(sci_port, sci_dev, sci_req);
465         else
466                 scic_sds_remote_device_increment_request_count(sci_dev);
467 }
468
469 enum sci_status scic_sds_remote_device_start_io(struct scic_sds_controller *scic,
470                                                 struct scic_sds_remote_device *sci_dev,
471                                                 struct scic_sds_request *sci_req)
472 {
473         struct sci_base_state_machine *sm = &sci_dev->state_machine;
474         enum scic_sds_remote_device_states state = sm->current_state_id;
475         struct scic_sds_port *sci_port = sci_dev->owning_port;
476         struct isci_request *ireq = sci_req->ireq;
477         enum sci_status status;
478
479         switch (state) {
480         case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL:
481         case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED:
482         case SCI_BASE_REMOTE_DEVICE_STATE_STARTING:
483         case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
484         case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING:
485         case SCI_BASE_REMOTE_DEVICE_STATE_FAILED:
486         case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING:
487         case SCI_BASE_REMOTE_DEVICE_STATE_FINAL:
488         default:
489                 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
490                          __func__, state);
491                 return SCI_FAILURE_INVALID_STATE;
492         case SCI_BASE_REMOTE_DEVICE_STATE_READY:
493                 /* attempt to start an io request for this device object. The remote
494                  * device object will issue the start request for the io and if
495                  * successful it will start the request for the port object then
496                  * increment its own request count.
497                  */
498                 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
499                 if (status != SCI_SUCCESS)
500                         return status;
501
502                 status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, sci_req);
503                 if (status != SCI_SUCCESS)
504                         break;
505
506                 status = scic_sds_request_start(sci_req);
507                 break;
508         case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE: {
509                 /* handle the start io operation for a sata device that is in
510                  * the command idle state. - Evalute the type of IO request to
511                  * be started - If its an NCQ request change to NCQ substate -
512                  * If its any other command change to the CMD substate
513                  *
514                  * If this is a softreset we may want to have a different
515                  * substate.
516                  */
517                 enum scic_sds_remote_device_states new_state;
518                 struct sas_task *task = isci_request_access_task(ireq);
519
520                 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
521                 if (status != SCI_SUCCESS)
522                         return status;
523
524                 status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, sci_req);
525                 if (status != SCI_SUCCESS)
526                         break;
527
528                 status = sci_req->state_handlers->start_handler(sci_req);
529                 if (status != SCI_SUCCESS)
530                         break;
531
532                 if (task->ata_task.use_ncq)
533                         new_state = SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ;
534                 else {
535                         sci_dev->working_request = sci_req;
536                         new_state = SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD;
537                 }
538                 sci_base_state_machine_change_state(sm, new_state);
539                 break;
540         }
541         case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ: {
542                 struct sas_task *task = isci_request_access_task(ireq);
543
544                 if (task->ata_task.use_ncq) {
545                         status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
546                         if (status != SCI_SUCCESS)
547                                 return status;
548
549                         status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, sci_req);
550                         if (status != SCI_SUCCESS)
551                                 break;
552
553                         status = sci_req->state_handlers->start_handler(sci_req);
554                 } else
555                         return SCI_FAILURE_INVALID_STATE;
556                 break;
557         }
558         case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
559                 return SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED;
560         case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
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_io(&sci_dev->rnc, sci_req);
566                 if (status != SCI_SUCCESS)
567                         break;
568
569                 status = scic_sds_request_start(sci_req);
570                 if (status != SCI_SUCCESS)
571                         break;
572
573                 sci_dev->working_request = sci_req;
574                 sci_base_state_machine_change_state(&sci_dev->state_machine,
575                                                     SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD);
576                 break;
577         case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
578         case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
579                 /* device is already handling a command it can not accept new commands
580                  * until this one is complete.
581                  */
582                 return SCI_FAILURE_INVALID_STATE;
583         }
584
585         scic_sds_remote_device_start_request(sci_dev, sci_req, status);
586         return status;
587 }
588
589 static enum sci_status common_complete_io(struct scic_sds_port *sci_port,
590                                           struct scic_sds_remote_device *sci_dev,
591                                           struct scic_sds_request *sci_req)
592 {
593         enum sci_status status;
594
595         status = scic_sds_request_complete(sci_req);
596         if (status != SCI_SUCCESS)
597                 return status;
598
599         status = scic_sds_port_complete_io(sci_port, sci_dev, sci_req);
600         if (status != SCI_SUCCESS)
601                 return status;
602
603         scic_sds_remote_device_decrement_request_count(sci_dev);
604         return status;
605 }
606
607 enum sci_status scic_sds_remote_device_complete_io(struct scic_sds_controller *scic,
608                                                    struct scic_sds_remote_device *sci_dev,
609                                                    struct scic_sds_request *sci_req)
610 {
611         struct sci_base_state_machine *sm = &sci_dev->state_machine;
612         enum scic_sds_remote_device_states state = sm->current_state_id;
613         struct scic_sds_port *sci_port = sci_dev->owning_port;
614         enum sci_status status;
615
616         switch (state) {
617         case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL:
618         case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED:
619         case SCI_BASE_REMOTE_DEVICE_STATE_STARTING:
620         case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
621         case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
622         case SCI_BASE_REMOTE_DEVICE_STATE_FAILED:
623         case SCI_BASE_REMOTE_DEVICE_STATE_FINAL:
624         default:
625                 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
626                          __func__, state);
627                 return SCI_FAILURE_INVALID_STATE;
628         case SCI_BASE_REMOTE_DEVICE_STATE_READY:
629         case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
630         case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING:
631                 status = common_complete_io(sci_port, sci_dev, sci_req);
632                 break;
633         case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
634         case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ:
635         case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
636                 status = common_complete_io(sci_port, sci_dev, sci_req);
637                 if (status != SCI_SUCCESS)
638                         break;
639
640                 if (sci_req->sci_status == SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED) {
641                         /* This request causes hardware error, device needs to be Lun Reset.
642                          * So here we force the state machine to IDLE state so the rest IOs
643                          * can reach RNC state handler, these IOs will be completed by RNC with
644                          * status of "DEVICE_RESET_REQUIRED", instead of "INVALID STATE".
645                          */
646                         sci_base_state_machine_change_state(sm, SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET);
647                 } else if (scic_sds_remote_device_get_request_count(sci_dev) == 0)
648                         sci_base_state_machine_change_state(sm, SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
649                 break;
650         case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
651                 status = common_complete_io(sci_port, sci_dev, sci_req);
652                 if (status != SCI_SUCCESS)
653                         break;
654                 sci_base_state_machine_change_state(sm, SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
655                 break;
656         case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING:
657                 status = common_complete_io(sci_port, sci_dev, sci_req);
658                 if (status != SCI_SUCCESS)
659                         break;
660
661                 if (scic_sds_remote_device_get_request_count(sci_dev) == 0)
662                         scic_sds_remote_node_context_destruct(&sci_dev->rnc,
663                                                               rnc_destruct_done,
664                                                               sci_dev);
665                 break;
666         }
667
668         if (status != SCI_SUCCESS)
669                 dev_err(scirdev_to_dev(sci_dev),
670                         "%s: Port:0x%p Device:0x%p Request:0x%p Status:0x%x "
671                         "could not complete\n", __func__, sci_port,
672                         sci_dev, sci_req, status);
673
674         return status;
675 }
676
677 static void scic_sds_remote_device_continue_request(void *dev)
678 {
679         struct scic_sds_remote_device *sci_dev = dev;
680
681         /* we need to check if this request is still valid to continue. */
682         if (sci_dev->working_request)
683                 scic_controller_continue_io(sci_dev->working_request);
684 }
685
686 enum sci_status scic_sds_remote_device_start_task(struct scic_sds_controller *scic,
687                                                   struct scic_sds_remote_device *sci_dev,
688                                                   struct scic_sds_request *sci_req)
689 {
690         struct sci_base_state_machine *sm = &sci_dev->state_machine;
691         enum scic_sds_remote_device_states state = sm->current_state_id;
692         struct scic_sds_port *sci_port = sci_dev->owning_port;
693         enum sci_status status;
694
695         switch (state) {
696         case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL:
697         case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED:
698         case SCI_BASE_REMOTE_DEVICE_STATE_STARTING:
699         case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
700         case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
701         case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING:
702         case SCI_BASE_REMOTE_DEVICE_STATE_FAILED:
703         case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING:
704         case SCI_BASE_REMOTE_DEVICE_STATE_FINAL:
705         default:
706                 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
707                          __func__, state);
708                 return SCI_FAILURE_INVALID_STATE;
709         case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
710         case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
711         case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ:
712         case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
713         case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
714                 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
715                 if (status != SCI_SUCCESS)
716                         return status;
717
718                 status = scic_sds_remote_node_context_start_task(&sci_dev->rnc, sci_req);
719                 if (status != SCI_SUCCESS)
720                         goto out;
721
722                 status = sci_req->state_handlers->start_handler(sci_req);
723                 if (status != SCI_SUCCESS)
724                         goto out;
725
726                 /* Note: If the remote device state is not IDLE this will
727                  * replace the request that probably resulted in the task
728                  * management request.
729                  */
730                 sci_dev->working_request = sci_req;
731                 sci_base_state_machine_change_state(sm, SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD);
732
733                 /* The remote node context must cleanup the TCi to NCQ mapping
734                  * table.  The only way to do this correctly is to either write
735                  * to the TLCR register or to invalidate and repost the RNC. In
736                  * either case the remote node context state machine will take
737                  * the correct action when the remote node context is suspended
738                  * and later resumed.
739                  */
740                 scic_sds_remote_node_context_suspend(&sci_dev->rnc,
741                                 SCI_SOFTWARE_SUSPENSION, NULL, NULL);
742                 scic_sds_remote_node_context_resume(&sci_dev->rnc,
743                                 scic_sds_remote_device_continue_request,
744                                                     sci_dev);
745
746         out:
747                 scic_sds_remote_device_start_request(sci_dev, sci_req, status);
748                 /* We need to let the controller start request handler know that
749                  * it can't post TC yet. We will provide a callback function to
750                  * post TC when RNC gets resumed.
751                  */
752                 return SCI_FAILURE_RESET_DEVICE_PARTIAL_SUCCESS;
753         case SCI_BASE_REMOTE_DEVICE_STATE_READY:
754                 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
755                 if (status != SCI_SUCCESS)
756                         return status;
757
758                 status = scic_sds_remote_node_context_start_task(&sci_dev->rnc, sci_req);
759                 if (status != SCI_SUCCESS)
760                         break;
761
762                 status = scic_sds_request_start(sci_req);
763                 break;
764         }
765         scic_sds_remote_device_start_request(sci_dev, sci_req, status);
766
767         return status;
768 }
769
770 /**
771  *
772  * @sci_dev:
773  * @request:
774  *
775  * This method takes the request and bulids an appropriate SCU context for the
776  * request and then requests the controller to post the request. none
777  */
778 void scic_sds_remote_device_post_request(
779         struct scic_sds_remote_device *sci_dev,
780         u32 request)
781 {
782         u32 context;
783
784         context = scic_sds_remote_device_build_command_context(sci_dev, request);
785
786         scic_sds_controller_post_request(
787                 scic_sds_remote_device_get_controller(sci_dev),
788                 context
789                 );
790 }
791
792 /* called once the remote node context has transisitioned to a
793  * ready state.  This is the indication that the remote device object can also
794  * transition to ready.
795  */
796 static void remote_device_resume_done(void *_dev)
797 {
798         struct scic_sds_remote_device *sci_dev = _dev;
799
800         if (is_remote_device_ready(sci_dev))
801                 return;
802
803         /* go 'ready' if we are not already in a ready state */
804         sci_base_state_machine_change_state(&sci_dev->state_machine,
805                                             SCI_BASE_REMOTE_DEVICE_STATE_READY);
806 }
807
808 static void scic_sds_stp_remote_device_ready_idle_substate_resume_complete_handler(void *_dev)
809 {
810         struct scic_sds_remote_device *sci_dev = _dev;
811         struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
812         struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
813
814         /* For NCQ operation we do not issue a isci_remote_device_not_ready().
815          * As a result, avoid sending the ready notification.
816          */
817         if (sci_dev->state_machine.previous_state_id != SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ)
818                 isci_remote_device_ready(scic->ihost, idev);
819 }
820
821 static void scic_sds_remote_device_initial_state_enter(void *object)
822 {
823         struct scic_sds_remote_device *sci_dev = object;
824
825         /* Initial state is a transitional state to the stopped state */
826         sci_base_state_machine_change_state(&sci_dev->state_machine,
827                                             SCI_BASE_REMOTE_DEVICE_STATE_STOPPED);
828 }
829
830 /**
831  * scic_remote_device_destruct() - free remote node context and destruct
832  * @remote_device: This parameter specifies the remote device to be destructed.
833  *
834  * Remote device objects are a limited resource.  As such, they must be
835  * protected.  Thus calls to construct and destruct are mutually exclusive and
836  * non-reentrant. The return value shall indicate if the device was
837  * successfully destructed or if some failure occurred. enum sci_status This value
838  * is returned if the device is successfully destructed.
839  * SCI_FAILURE_INVALID_REMOTE_DEVICE This value is returned if the supplied
840  * device isn't valid (e.g. it's already been destoryed, the handle isn't
841  * valid, etc.).
842  */
843 static enum sci_status scic_remote_device_destruct(struct scic_sds_remote_device *sci_dev)
844 {
845         struct sci_base_state_machine *sm = &sci_dev->state_machine;
846         enum scic_sds_remote_device_states state = sm->current_state_id;
847         struct scic_sds_controller *scic;
848
849         if (state != SCI_BASE_REMOTE_DEVICE_STATE_STOPPED) {
850                 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
851                          __func__, state);
852                 return SCI_FAILURE_INVALID_STATE;
853         }
854
855         scic = sci_dev->owning_port->owning_controller;
856         scic_sds_controller_free_remote_node_context(scic, sci_dev,
857                                                      sci_dev->rnc.remote_node_index);
858         sci_dev->rnc.remote_node_index = SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX;
859         sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_FINAL);
860
861         return SCI_SUCCESS;
862 }
863
864 /**
865  * isci_remote_device_deconstruct() - This function frees an isci_remote_device.
866  * @ihost: This parameter specifies the isci host object.
867  * @idev: This parameter specifies the remote device to be freed.
868  *
869  */
870 static void isci_remote_device_deconstruct(struct isci_host *ihost, struct isci_remote_device *idev)
871 {
872         dev_dbg(&ihost->pdev->dev,
873                 "%s: isci_device = %p\n", __func__, idev);
874
875         /* There should not be any outstanding io's. All paths to
876          * here should go through isci_remote_device_nuke_requests.
877          * If we hit this condition, we will need a way to complete
878          * io requests in process */
879         while (!list_empty(&idev->reqs_in_process)) {
880
881                 dev_err(&ihost->pdev->dev,
882                         "%s: ** request list not empty! **\n", __func__);
883                 BUG();
884         }
885
886         scic_remote_device_destruct(&idev->sci);
887         idev->domain_dev->lldd_dev = NULL;
888         idev->domain_dev = NULL;
889         idev->isci_port = NULL;
890         list_del_init(&idev->node);
891
892         clear_bit(IDEV_START_PENDING, &idev->flags);
893         clear_bit(IDEV_STOP_PENDING, &idev->flags);
894         wake_up(&ihost->eventq);
895 }
896
897 /**
898  * isci_remote_device_stop_complete() - This function is called by the scic
899  *    when the remote device stop has completed. We mark the isci device as not
900  *    ready and remove the isci remote device.
901  * @ihost: This parameter specifies the isci host object.
902  * @idev: This parameter specifies the remote device.
903  * @status: This parameter specifies status of the completion.
904  *
905  */
906 static void isci_remote_device_stop_complete(struct isci_host *ihost,
907                                              struct isci_remote_device *idev)
908 {
909         dev_dbg(&ihost->pdev->dev, "%s: complete idev = %p\n", __func__, idev);
910
911         isci_remote_device_change_state(idev, isci_stopped);
912
913         /* after stop, we can tear down resources. */
914         isci_remote_device_deconstruct(ihost, idev);
915 }
916
917 static void scic_sds_remote_device_stopped_state_enter(void *object)
918 {
919         struct scic_sds_remote_device *sci_dev = object;
920         struct scic_sds_controller *scic;
921         struct isci_remote_device *idev;
922         struct isci_host *ihost;
923         u32 prev_state;
924
925         scic = scic_sds_remote_device_get_controller(sci_dev);
926         ihost = scic->ihost;
927         idev = sci_dev_to_idev(sci_dev);
928
929         /* If we are entering from the stopping state let the SCI User know that
930          * the stop operation has completed.
931          */
932         prev_state = sci_dev->state_machine.previous_state_id;
933         if (prev_state == SCI_BASE_REMOTE_DEVICE_STATE_STOPPING)
934                 isci_remote_device_stop_complete(ihost, idev);
935
936         scic_sds_controller_remote_device_stopped(scic, sci_dev);
937 }
938
939 static void scic_sds_remote_device_starting_state_enter(void *object)
940 {
941         struct scic_sds_remote_device *sci_dev = object;
942         struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
943         struct isci_host *ihost = scic->ihost;
944         struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
945
946         isci_remote_device_not_ready(ihost, idev,
947                                      SCIC_REMOTE_DEVICE_NOT_READY_START_REQUESTED);
948 }
949
950 static void scic_sds_remote_device_ready_state_enter(void *object)
951 {
952         struct scic_sds_remote_device *sci_dev = object;
953         struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
954         struct domain_device *dev = sci_dev_to_domain(sci_dev);
955
956         scic->remote_device_sequence[sci_dev->rnc.remote_node_index]++;
957
958         if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_SATA)) {
959                 sci_base_state_machine_change_state(&sci_dev->state_machine,
960                                                     SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
961         } else if (dev_is_expander(dev)) {
962                 sci_base_state_machine_change_state(&sci_dev->state_machine,
963                                                     SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
964         } else
965                 isci_remote_device_ready(scic->ihost, sci_dev_to_idev(sci_dev));
966 }
967
968 static void scic_sds_remote_device_ready_state_exit(void *object)
969 {
970         struct scic_sds_remote_device *sci_dev = object;
971         struct domain_device *dev = sci_dev_to_domain(sci_dev);
972
973         if (dev->dev_type == SAS_END_DEV) {
974                 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
975                 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
976
977                 isci_remote_device_not_ready(scic->ihost, idev,
978                                              SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED);
979         }
980 }
981
982 static void scic_sds_remote_device_resetting_state_enter(void *object)
983 {
984         struct scic_sds_remote_device *sci_dev = object;
985
986         scic_sds_remote_node_context_suspend(
987                 &sci_dev->rnc, SCI_SOFTWARE_SUSPENSION, NULL, NULL);
988 }
989
990 static void scic_sds_remote_device_resetting_state_exit(void *object)
991 {
992         struct scic_sds_remote_device *sci_dev = object;
993
994         scic_sds_remote_node_context_resume(&sci_dev->rnc, NULL, NULL);
995 }
996
997 static void scic_sds_stp_remote_device_ready_idle_substate_enter(void *object)
998 {
999         struct scic_sds_remote_device *sci_dev = object;
1000
1001         sci_dev->working_request = NULL;
1002         if (scic_sds_remote_node_context_is_ready(&sci_dev->rnc)) {
1003                 /*
1004                  * Since the RNC is ready, it's alright to finish completion
1005                  * processing (e.g. signal the remote device is ready). */
1006                 scic_sds_stp_remote_device_ready_idle_substate_resume_complete_handler(sci_dev);
1007         } else {
1008                 scic_sds_remote_node_context_resume(&sci_dev->rnc,
1009                         scic_sds_stp_remote_device_ready_idle_substate_resume_complete_handler,
1010                         sci_dev);
1011         }
1012 }
1013
1014 static void scic_sds_stp_remote_device_ready_cmd_substate_enter(void *object)
1015 {
1016         struct scic_sds_remote_device *sci_dev = object;
1017         struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1018
1019         BUG_ON(sci_dev->working_request == NULL);
1020
1021         isci_remote_device_not_ready(scic->ihost, sci_dev_to_idev(sci_dev),
1022                                      SCIC_REMOTE_DEVICE_NOT_READY_SATA_REQUEST_STARTED);
1023 }
1024
1025 static void scic_sds_stp_remote_device_ready_ncq_error_substate_enter(void *object)
1026 {
1027         struct scic_sds_remote_device *sci_dev = object;
1028         struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1029         struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
1030
1031         if (sci_dev->not_ready_reason == SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED)
1032                 isci_remote_device_not_ready(scic->ihost, idev,
1033                                              sci_dev->not_ready_reason);
1034 }
1035
1036 static void scic_sds_smp_remote_device_ready_idle_substate_enter(void *object)
1037 {
1038         struct scic_sds_remote_device *sci_dev = object;
1039         struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1040
1041         isci_remote_device_ready(scic->ihost, sci_dev_to_idev(sci_dev));
1042 }
1043
1044 static void scic_sds_smp_remote_device_ready_cmd_substate_enter(void *object)
1045 {
1046         struct scic_sds_remote_device *sci_dev = object;
1047         struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1048
1049         BUG_ON(sci_dev->working_request == NULL);
1050
1051         isci_remote_device_not_ready(scic->ihost, sci_dev_to_idev(sci_dev),
1052                                      SCIC_REMOTE_DEVICE_NOT_READY_SMP_REQUEST_STARTED);
1053 }
1054
1055 static void scic_sds_smp_remote_device_ready_cmd_substate_exit(void *object)
1056 {
1057         struct scic_sds_remote_device *sci_dev = object;
1058
1059         sci_dev->working_request = NULL;
1060 }
1061
1062 static const struct sci_base_state scic_sds_remote_device_state_table[] = {
1063         [SCI_BASE_REMOTE_DEVICE_STATE_INITIAL] = {
1064                 .enter_state = scic_sds_remote_device_initial_state_enter,
1065         },
1066         [SCI_BASE_REMOTE_DEVICE_STATE_STOPPED] = {
1067                 .enter_state = scic_sds_remote_device_stopped_state_enter,
1068         },
1069         [SCI_BASE_REMOTE_DEVICE_STATE_STARTING] = {
1070                 .enter_state = scic_sds_remote_device_starting_state_enter,
1071         },
1072         [SCI_BASE_REMOTE_DEVICE_STATE_READY] = {
1073                 .enter_state = scic_sds_remote_device_ready_state_enter,
1074                 .exit_state  = scic_sds_remote_device_ready_state_exit
1075         },
1076         [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE] = {
1077                 .enter_state = scic_sds_stp_remote_device_ready_idle_substate_enter,
1078         },
1079         [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD] = {
1080                 .enter_state = scic_sds_stp_remote_device_ready_cmd_substate_enter,
1081         },
1082         [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ] = { },
1083         [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR] = {
1084                 .enter_state = scic_sds_stp_remote_device_ready_ncq_error_substate_enter,
1085         },
1086         [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET] = { },
1087         [SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE] = {
1088                 .enter_state = scic_sds_smp_remote_device_ready_idle_substate_enter,
1089         },
1090         [SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD] = {
1091                 .enter_state = scic_sds_smp_remote_device_ready_cmd_substate_enter,
1092                 .exit_state  = scic_sds_smp_remote_device_ready_cmd_substate_exit,
1093         },
1094         [SCI_BASE_REMOTE_DEVICE_STATE_STOPPING] = { },
1095         [SCI_BASE_REMOTE_DEVICE_STATE_FAILED] = { },
1096         [SCI_BASE_REMOTE_DEVICE_STATE_RESETTING] = {
1097                 .enter_state = scic_sds_remote_device_resetting_state_enter,
1098                 .exit_state  = scic_sds_remote_device_resetting_state_exit
1099         },
1100         [SCI_BASE_REMOTE_DEVICE_STATE_FINAL] = { },
1101 };
1102
1103 /**
1104  * scic_remote_device_construct() - common construction
1105  * @sci_port: SAS/SATA port through which this device is accessed.
1106  * @sci_dev: remote device to construct
1107  *
1108  * This routine just performs benign initialization and does not
1109  * allocate the remote_node_context which is left to
1110  * scic_remote_device_[de]a_construct().  scic_remote_device_destruct()
1111  * frees the remote_node_context(s) for the device.
1112  */
1113 static void scic_remote_device_construct(struct scic_sds_port *sci_port,
1114                                   struct scic_sds_remote_device *sci_dev)
1115 {
1116         sci_dev->owning_port = sci_port;
1117         sci_dev->started_request_count = 0;
1118
1119         sci_base_state_machine_construct(
1120                 &sci_dev->state_machine,
1121                 sci_dev,
1122                 scic_sds_remote_device_state_table,
1123                 SCI_BASE_REMOTE_DEVICE_STATE_INITIAL
1124                 );
1125
1126         sci_base_state_machine_start(
1127                 &sci_dev->state_machine
1128                 );
1129
1130         scic_sds_remote_node_context_construct(&sci_dev->rnc,
1131                                                SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX);
1132 }
1133
1134 /**
1135  * scic_remote_device_da_construct() - construct direct attached device.
1136  *
1137  * The information (e.g. IAF, Signature FIS, etc.) necessary to build
1138  * the device is known to the SCI Core since it is contained in the
1139  * scic_phy object.  Remote node context(s) is/are a global resource
1140  * allocated by this routine, freed by scic_remote_device_destruct().
1141  *
1142  * Returns:
1143  * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
1144  * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
1145  * sata-only controller instance.
1146  * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
1147  */
1148 static enum sci_status scic_remote_device_da_construct(struct scic_sds_port *sci_port,
1149                                                        struct scic_sds_remote_device *sci_dev)
1150 {
1151         enum sci_status status;
1152         struct domain_device *dev = sci_dev_to_domain(sci_dev);
1153
1154         scic_remote_device_construct(sci_port, sci_dev);
1155
1156         /*
1157          * This information is request to determine how many remote node context
1158          * entries will be needed to store the remote node.
1159          */
1160         sci_dev->is_direct_attached = true;
1161         status = scic_sds_controller_allocate_remote_node_context(sci_port->owning_controller,
1162                                                                   sci_dev,
1163                                                                   &sci_dev->rnc.remote_node_index);
1164
1165         if (status != SCI_SUCCESS)
1166                 return status;
1167
1168         if (dev->dev_type == SAS_END_DEV || dev->dev_type == SATA_DEV ||
1169             (dev->tproto & SAS_PROTOCOL_STP) || dev_is_expander(dev))
1170                 /* pass */;
1171         else
1172                 return SCI_FAILURE_UNSUPPORTED_PROTOCOL;
1173
1174         sci_dev->connection_rate = scic_sds_port_get_max_allowed_speed(sci_port);
1175
1176         /* / @todo Should I assign the port width by reading all of the phys on the port? */
1177         sci_dev->device_port_width = 1;
1178
1179         return SCI_SUCCESS;
1180 }
1181
1182 /**
1183  * scic_remote_device_ea_construct() - construct expander attached device
1184  *
1185  * Remote node context(s) is/are a global resource allocated by this
1186  * routine, freed by scic_remote_device_destruct().
1187  *
1188  * Returns:
1189  * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
1190  * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
1191  * sata-only controller instance.
1192  * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
1193  */
1194 static enum sci_status scic_remote_device_ea_construct(struct scic_sds_port *sci_port,
1195                                                        struct scic_sds_remote_device *sci_dev)
1196 {
1197         struct domain_device *dev = sci_dev_to_domain(sci_dev);
1198         enum sci_status status;
1199
1200         scic_remote_device_construct(sci_port, sci_dev);
1201
1202         status = scic_sds_controller_allocate_remote_node_context(sci_port->owning_controller,
1203                                                                   sci_dev,
1204                                                                   &sci_dev->rnc.remote_node_index);
1205         if (status != SCI_SUCCESS)
1206                 return status;
1207
1208         if (dev->dev_type == SAS_END_DEV || dev->dev_type == SATA_DEV ||
1209             (dev->tproto & SAS_PROTOCOL_STP) || dev_is_expander(dev))
1210                 /* pass */;
1211         else
1212                 return SCI_FAILURE_UNSUPPORTED_PROTOCOL;
1213
1214         /*
1215          * For SAS-2 the physical link rate is actually a logical link
1216          * rate that incorporates multiplexing.  The SCU doesn't
1217          * incorporate multiplexing and for the purposes of the
1218          * connection the logical link rate is that same as the
1219          * physical.  Furthermore, the SAS-2 and SAS-1.1 fields overlay
1220          * one another, so this code works for both situations. */
1221         sci_dev->connection_rate = min_t(u16, scic_sds_port_get_max_allowed_speed(sci_port),
1222                                          dev->linkrate);
1223
1224         /* / @todo Should I assign the port width by reading all of the phys on the port? */
1225         sci_dev->device_port_width = 1;
1226
1227         return SCI_SUCCESS;
1228 }
1229
1230 /**
1231  * scic_remote_device_start() - This method will start the supplied remote
1232  *    device.  This method enables normal IO requests to flow through to the
1233  *    remote device.
1234  * @remote_device: This parameter specifies the device to be started.
1235  * @timeout: This parameter specifies the number of milliseconds in which the
1236  *    start operation should complete.
1237  *
1238  * An indication of whether the device was successfully started. SCI_SUCCESS
1239  * This value is returned if the device was successfully started.
1240  * SCI_FAILURE_INVALID_PHY This value is returned if the user attempts to start
1241  * the device when there have been no phys added to it.
1242  */
1243 static enum sci_status scic_remote_device_start(struct scic_sds_remote_device *sci_dev,
1244                                                 u32 timeout)
1245 {
1246         struct sci_base_state_machine *sm = &sci_dev->state_machine;
1247         enum scic_sds_remote_device_states state = sm->current_state_id;
1248         enum sci_status status;
1249
1250         if (state != SCI_BASE_REMOTE_DEVICE_STATE_STOPPED) {
1251                 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
1252                          __func__, state);
1253                 return SCI_FAILURE_INVALID_STATE;
1254         }
1255
1256         status = scic_sds_remote_node_context_resume(&sci_dev->rnc,
1257                                                      remote_device_resume_done,
1258                                                      sci_dev);
1259         if (status != SCI_SUCCESS)
1260                 return status;
1261
1262         sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_STARTING);
1263
1264         return SCI_SUCCESS;
1265 }
1266
1267 static enum sci_status isci_remote_device_construct(struct isci_port *iport,
1268                                                     struct isci_remote_device *idev)
1269 {
1270         struct scic_sds_port *sci_port = iport->sci_port_handle;
1271         struct isci_host *ihost = iport->isci_host;
1272         struct domain_device *dev = idev->domain_dev;
1273         enum sci_status status;
1274
1275         if (dev->parent && dev_is_expander(dev->parent))
1276                 status = scic_remote_device_ea_construct(sci_port, &idev->sci);
1277         else
1278                 status = scic_remote_device_da_construct(sci_port, &idev->sci);
1279
1280         if (status != SCI_SUCCESS) {
1281                 dev_dbg(&ihost->pdev->dev, "%s: construct failed: %d\n",
1282                         __func__, status);
1283
1284                 return status;
1285         }
1286
1287         /* start the device. */
1288         status = scic_remote_device_start(&idev->sci, ISCI_REMOTE_DEVICE_START_TIMEOUT);
1289
1290         if (status != SCI_SUCCESS)
1291                 dev_warn(&ihost->pdev->dev, "remote device start failed: %d\n",
1292                          status);
1293
1294         return status;
1295 }
1296
1297 void isci_remote_device_nuke_requests(struct isci_host *ihost, struct isci_remote_device *idev)
1298 {
1299         DECLARE_COMPLETION_ONSTACK(aborted_task_completion);
1300
1301         dev_dbg(&ihost->pdev->dev,
1302                 "%s: idev = %p\n", __func__, idev);
1303
1304         /* Cleanup all requests pending for this device. */
1305         isci_terminate_pending_requests(ihost, idev, terminating);
1306
1307         dev_dbg(&ihost->pdev->dev,
1308                 "%s: idev = %p, done\n", __func__, idev);
1309 }
1310
1311 /**
1312  * This function builds the isci_remote_device when a libsas dev_found message
1313  *    is received.
1314  * @isci_host: This parameter specifies the isci host object.
1315  * @port: This parameter specifies the isci_port conected to this device.
1316  *
1317  * pointer to new isci_remote_device.
1318  */
1319 static struct isci_remote_device *
1320 isci_remote_device_alloc(struct isci_host *ihost, struct isci_port *iport)
1321 {
1322         struct isci_remote_device *idev;
1323         int i;
1324
1325         for (i = 0; i < SCI_MAX_REMOTE_DEVICES; i++) {
1326                 idev = &ihost->devices[i];
1327                 if (!test_and_set_bit(IDEV_ALLOCATED, &idev->flags))
1328                         break;
1329         }
1330
1331         if (i >= SCI_MAX_REMOTE_DEVICES) {
1332                 dev_warn(&ihost->pdev->dev, "%s: failed\n", __func__);
1333                 return NULL;
1334         }
1335
1336         if (WARN_ONCE(!list_empty(&idev->reqs_in_process), "found requests in process\n"))
1337                 return NULL;
1338
1339         if (WARN_ONCE(!list_empty(&idev->node), "found non-idle remote device\n"))
1340                 return NULL;
1341
1342         isci_remote_device_change_state(idev, isci_freed);
1343
1344         return idev;
1345 }
1346
1347 /**
1348  * isci_remote_device_stop() - This function is called internally to stop the
1349  *    remote device.
1350  * @isci_host: This parameter specifies the isci host object.
1351  * @isci_device: This parameter specifies the remote device.
1352  *
1353  * The status of the scic request to stop.
1354  */
1355 enum sci_status isci_remote_device_stop(struct isci_host *ihost, struct isci_remote_device *idev)
1356 {
1357         enum sci_status status;
1358         unsigned long flags;
1359
1360         dev_dbg(&ihost->pdev->dev,
1361                 "%s: isci_device = %p\n", __func__, idev);
1362
1363         isci_remote_device_change_state(idev, isci_stopping);
1364
1365         /* Kill all outstanding requests. */
1366         isci_remote_device_nuke_requests(ihost, idev);
1367
1368         set_bit(IDEV_STOP_PENDING, &idev->flags);
1369
1370         spin_lock_irqsave(&ihost->scic_lock, flags);
1371         status = scic_remote_device_stop(&idev->sci, 50);
1372         spin_unlock_irqrestore(&ihost->scic_lock, flags);
1373
1374         /* Wait for the stop complete callback. */
1375         if (status == SCI_SUCCESS) {
1376                 wait_for_device_stop(ihost, idev);
1377                 clear_bit(IDEV_ALLOCATED, &idev->flags);
1378         }
1379
1380         dev_dbg(&ihost->pdev->dev,
1381                 "%s: idev = %p - after completion wait\n",
1382                 __func__, idev);
1383
1384         return status;
1385 }
1386
1387 /**
1388  * isci_remote_device_gone() - This function is called by libsas when a domain
1389  *    device is removed.
1390  * @domain_device: This parameter specifies the libsas domain device.
1391  *
1392  */
1393 void isci_remote_device_gone(struct domain_device *dev)
1394 {
1395         struct isci_host *ihost = dev_to_ihost(dev);
1396         struct isci_remote_device *idev = dev->lldd_dev;
1397
1398         dev_dbg(&ihost->pdev->dev,
1399                 "%s: domain_device = %p, isci_device = %p, isci_port = %p\n",
1400                 __func__, dev, idev, idev->isci_port);
1401
1402         isci_remote_device_stop(ihost, idev);
1403 }
1404
1405
1406 /**
1407  * isci_remote_device_found() - This function is called by libsas when a remote
1408  *    device is discovered. A remote device object is created and started. the
1409  *    function then sleeps until the sci core device started message is
1410  *    received.
1411  * @domain_device: This parameter specifies the libsas domain device.
1412  *
1413  * status, zero indicates success.
1414  */
1415 int isci_remote_device_found(struct domain_device *domain_dev)
1416 {
1417         struct isci_host *isci_host = dev_to_ihost(domain_dev);
1418         struct isci_port *isci_port;
1419         struct isci_phy *isci_phy;
1420         struct asd_sas_port *sas_port;
1421         struct asd_sas_phy *sas_phy;
1422         struct isci_remote_device *isci_device;
1423         enum sci_status status;
1424
1425         dev_dbg(&isci_host->pdev->dev,
1426                 "%s: domain_device = %p\n", __func__, domain_dev);
1427
1428         wait_for_start(isci_host);
1429
1430         sas_port = domain_dev->port;
1431         sas_phy = list_first_entry(&sas_port->phy_list, struct asd_sas_phy,
1432                                    port_phy_el);
1433         isci_phy = to_isci_phy(sas_phy);
1434         isci_port = isci_phy->isci_port;
1435
1436         /* we are being called for a device on this port,
1437          * so it has to come up eventually
1438          */
1439         wait_for_completion(&isci_port->start_complete);
1440
1441         if ((isci_stopping == isci_port_get_state(isci_port)) ||
1442             (isci_stopped == isci_port_get_state(isci_port)))
1443                 return -ENODEV;
1444
1445         isci_device = isci_remote_device_alloc(isci_host, isci_port);
1446         if (!isci_device)
1447                 return -ENODEV;
1448
1449         INIT_LIST_HEAD(&isci_device->node);
1450         domain_dev->lldd_dev = isci_device;
1451         isci_device->domain_dev = domain_dev;
1452         isci_device->isci_port = isci_port;
1453         isci_remote_device_change_state(isci_device, isci_starting);
1454
1455
1456         spin_lock_irq(&isci_host->scic_lock);
1457         list_add_tail(&isci_device->node, &isci_port->remote_dev_list);
1458
1459         set_bit(IDEV_START_PENDING, &isci_device->flags);
1460         status = isci_remote_device_construct(isci_port, isci_device);
1461         spin_unlock_irq(&isci_host->scic_lock);
1462
1463         dev_dbg(&isci_host->pdev->dev,
1464                 "%s: isci_device = %p\n",
1465                 __func__, isci_device);
1466
1467         if (status != SCI_SUCCESS) {
1468
1469                 spin_lock_irq(&isci_host->scic_lock);
1470                 isci_remote_device_deconstruct(
1471                         isci_host,
1472                         isci_device
1473                         );
1474                 spin_unlock_irq(&isci_host->scic_lock);
1475                 return -ENODEV;
1476         }
1477
1478         /* wait for the device ready callback. */
1479         wait_for_device_start(isci_host, isci_device);
1480
1481         return 0;
1482 }
1483 /**
1484  * isci_device_is_reset_pending() - This function will check if there is any
1485  *    pending reset condition on the device.
1486  * @request: This parameter is the isci_device object.
1487  *
1488  * true if there is a reset pending for the device.
1489  */
1490 bool isci_device_is_reset_pending(
1491         struct isci_host *isci_host,
1492         struct isci_remote_device *isci_device)
1493 {
1494         struct isci_request *isci_request;
1495         struct isci_request *tmp_req;
1496         bool reset_is_pending = false;
1497         unsigned long flags;
1498
1499         dev_dbg(&isci_host->pdev->dev,
1500                 "%s: isci_device = %p\n", __func__, isci_device);
1501
1502         spin_lock_irqsave(&isci_host->scic_lock, flags);
1503
1504         /* Check for reset on all pending requests. */
1505         list_for_each_entry_safe(isci_request, tmp_req,
1506                                  &isci_device->reqs_in_process, dev_node) {
1507                 dev_dbg(&isci_host->pdev->dev,
1508                         "%s: isci_device = %p request = %p\n",
1509                         __func__, isci_device, isci_request);
1510
1511                 if (isci_request->ttype == io_task) {
1512                         struct sas_task *task = isci_request_access_task(
1513                                 isci_request);
1514
1515                         spin_lock(&task->task_state_lock);
1516                         if (task->task_state_flags & SAS_TASK_NEED_DEV_RESET)
1517                                 reset_is_pending = true;
1518                         spin_unlock(&task->task_state_lock);
1519                 }
1520         }
1521
1522         spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1523
1524         dev_dbg(&isci_host->pdev->dev,
1525                 "%s: isci_device = %p reset_is_pending = %d\n",
1526                 __func__, isci_device, reset_is_pending);
1527
1528         return reset_is_pending;
1529 }
1530
1531 /**
1532  * isci_device_clear_reset_pending() - This function will clear if any pending
1533  *    reset condition flags on the device.
1534  * @request: This parameter is the isci_device object.
1535  *
1536  * true if there is a reset pending for the device.
1537  */
1538 void isci_device_clear_reset_pending(struct isci_host *ihost, struct isci_remote_device *idev)
1539 {
1540         struct isci_request *isci_request;
1541         struct isci_request *tmp_req;
1542         unsigned long flags = 0;
1543
1544         dev_dbg(&ihost->pdev->dev, "%s: idev=%p, ihost=%p\n",
1545                  __func__, idev, ihost);
1546
1547         spin_lock_irqsave(&ihost->scic_lock, flags);
1548
1549         /* Clear reset pending on all pending requests. */
1550         list_for_each_entry_safe(isci_request, tmp_req,
1551                                  &idev->reqs_in_process, dev_node) {
1552                 dev_dbg(&ihost->pdev->dev, "%s: idev = %p request = %p\n",
1553                          __func__, idev, isci_request);
1554
1555                 if (isci_request->ttype == io_task) {
1556
1557                         unsigned long flags2;
1558                         struct sas_task *task = isci_request_access_task(
1559                                 isci_request);
1560
1561                         spin_lock_irqsave(&task->task_state_lock, flags2);
1562                         task->task_state_flags &= ~SAS_TASK_NEED_DEV_RESET;
1563                         spin_unlock_irqrestore(&task->task_state_lock, flags2);
1564                 }
1565         }
1566         spin_unlock_irqrestore(&ihost->scic_lock, flags);
1567 }