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