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