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.
7 * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
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.
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.
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.
26 * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
27 * All rights reserved.
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
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
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.
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.
56 #include "intel_sas.h"
60 #include "remote_device.h"
62 #include "scic_controller.h"
63 #include "scic_io_request.h"
65 #include "scic_port.h"
66 #include "scic_sds_controller.h"
67 #include "scic_sds_phy.h"
68 #include "scic_sds_port.h"
69 #include "remote_node_context.h"
70 #include "scic_sds_request.h"
71 #include "sci_environment.h"
73 #include "scu_event_codes.h"
77 * isci_remote_device_change_state() - This function gets the status of the
78 * remote_device object.
79 * @isci_device: This parameter points to the isci_remote_device object
81 * status of the object as a isci_status enum.
83 void isci_remote_device_change_state(
84 struct isci_remote_device
*isci_device
,
85 enum isci_status status
)
89 spin_lock_irqsave(&isci_device
->state_lock
, flags
);
90 isci_device
->status
= status
;
91 spin_unlock_irqrestore(&isci_device
->state_lock
, flags
);
95 * isci_remote_device_not_ready() - This function is called by the scic when
96 * the remote device is not ready. We mark the isci device as ready (not
97 * "ready_for_io") and signal the waiting proccess.
98 * @isci_host: This parameter specifies the isci host object.
99 * @isci_device: This parameter specifies the remote device
102 static void isci_remote_device_not_ready(struct isci_host
*ihost
,
103 struct isci_remote_device
*idev
, u32 reason
)
105 dev_dbg(&ihost
->pdev
->dev
,
106 "%s: isci_device = %p\n", __func__
, idev
);
108 if (reason
== SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED
)
109 isci_remote_device_change_state(idev
, isci_stopping
);
111 /* device ready is actually a "not ready for io" state. */
112 isci_remote_device_change_state(idev
, isci_ready
);
116 * isci_remote_device_ready() - This function is called by the scic when the
117 * remote device is ready. We mark the isci device as ready and signal the
119 * @ihost: our valid isci_host
120 * @idev: remote device
123 static void isci_remote_device_ready(struct isci_host
*ihost
, struct isci_remote_device
*idev
)
125 dev_dbg(&ihost
->pdev
->dev
,
126 "%s: idev = %p\n", __func__
, idev
);
128 isci_remote_device_change_state(idev
, isci_ready_for_io
);
129 if (test_and_clear_bit(IDEV_START_PENDING
, &idev
->flags
))
130 wake_up(&ihost
->eventq
);
133 /* called once the remote node context is ready to be freed.
134 * The remote device can now report that its stop operation is complete. none
136 static void rnc_destruct_done(void *_dev
)
138 struct scic_sds_remote_device
*sci_dev
= _dev
;
140 BUG_ON(sci_dev
->started_request_count
!= 0);
141 sci_base_state_machine_change_state(&sci_dev
->state_machine
,
142 SCI_BASE_REMOTE_DEVICE_STATE_STOPPED
);
145 static enum sci_status
scic_sds_remote_device_terminate_requests(struct scic_sds_remote_device
*sci_dev
)
147 struct scic_sds_controller
*scic
= sci_dev
->owning_port
->owning_controller
;
148 u32 i
, request_count
= sci_dev
->started_request_count
;
149 enum sci_status status
= SCI_SUCCESS
;
151 for (i
= 0; i
< SCI_MAX_IO_REQUESTS
&& i
< request_count
; i
++) {
152 struct scic_sds_request
*sci_req
;
155 sci_req
= scic
->io_request_table
[i
];
156 if (!sci_req
|| sci_req
->target_device
!= sci_dev
)
158 s
= scic_controller_terminate_request(scic
, sci_dev
, sci_req
);
159 if (s
!= SCI_SUCCESS
)
166 enum sci_status
scic_remote_device_stop(struct scic_sds_remote_device
*sci_dev
,
169 struct sci_base_state_machine
*sm
= &sci_dev
->state_machine
;
170 enum scic_sds_remote_device_states state
= sm
->current_state_id
;
173 case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL
:
174 case SCI_BASE_REMOTE_DEVICE_STATE_FAILED
:
175 case SCI_BASE_REMOTE_DEVICE_STATE_FINAL
:
177 dev_warn(scirdev_to_dev(sci_dev
), "%s: in wrong state: %d\n",
179 return SCI_FAILURE_INVALID_STATE
;
180 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED
:
182 case SCI_BASE_REMOTE_DEVICE_STATE_STARTING
:
183 /* device not started so there had better be no requests */
184 BUG_ON(sci_dev
->started_request_count
!= 0);
185 scic_sds_remote_node_context_destruct(&sci_dev
->rnc
,
186 rnc_destruct_done
, sci_dev
);
187 /* Transition to the stopping state and wait for the
188 * remote node to complete being posted and invalidated.
190 sci_base_state_machine_change_state(sm
, SCI_BASE_REMOTE_DEVICE_STATE_STOPPING
);
192 case SCI_BASE_REMOTE_DEVICE_STATE_READY
:
193 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE
:
194 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD
:
195 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ
:
196 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR
:
197 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET
:
198 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE
:
199 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD
:
200 sci_base_state_machine_change_state(sm
, SCI_BASE_REMOTE_DEVICE_STATE_STOPPING
);
201 if (sci_dev
->started_request_count
== 0) {
202 scic_sds_remote_node_context_destruct(&sci_dev
->rnc
,
203 rnc_destruct_done
, sci_dev
);
206 return scic_sds_remote_device_terminate_requests(sci_dev
);
208 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING
:
209 /* All requests should have been terminated, but if there is an
210 * attempt to stop a device already in the stopping state, then
211 * try again to terminate.
213 return scic_sds_remote_device_terminate_requests(sci_dev
);
214 case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING
:
215 sci_base_state_machine_change_state(sm
, SCI_BASE_REMOTE_DEVICE_STATE_STOPPING
);
220 enum sci_status
scic_remote_device_reset(struct scic_sds_remote_device
*sci_dev
)
222 struct sci_base_state_machine
*sm
= &sci_dev
->state_machine
;
223 enum scic_sds_remote_device_states state
= sm
->current_state_id
;
226 case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL
:
227 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED
:
228 case SCI_BASE_REMOTE_DEVICE_STATE_STARTING
:
229 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE
:
230 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD
:
231 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING
:
232 case SCI_BASE_REMOTE_DEVICE_STATE_FAILED
:
233 case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING
:
234 case SCI_BASE_REMOTE_DEVICE_STATE_FINAL
:
236 dev_warn(scirdev_to_dev(sci_dev
), "%s: in wrong state: %d\n",
238 return SCI_FAILURE_INVALID_STATE
;
239 case SCI_BASE_REMOTE_DEVICE_STATE_READY
:
240 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE
:
241 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD
:
242 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ
:
243 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR
:
244 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET
:
245 sci_base_state_machine_change_state(sm
, SCI_BASE_REMOTE_DEVICE_STATE_RESETTING
);
250 enum sci_status
scic_remote_device_reset_complete(struct scic_sds_remote_device
*sci_dev
)
252 struct sci_base_state_machine
*sm
= &sci_dev
->state_machine
;
253 enum scic_sds_remote_device_states state
= sm
->current_state_id
;
255 if (state
!= SCI_BASE_REMOTE_DEVICE_STATE_RESETTING
) {
256 dev_warn(scirdev_to_dev(sci_dev
), "%s: in wrong state: %d\n",
258 return SCI_FAILURE_INVALID_STATE
;
261 sci_base_state_machine_change_state(sm
, SCI_BASE_REMOTE_DEVICE_STATE_READY
);
265 enum sci_status
scic_sds_remote_device_suspend(struct scic_sds_remote_device
*sci_dev
,
268 struct sci_base_state_machine
*sm
= &sci_dev
->state_machine
;
269 enum scic_sds_remote_device_states state
= sm
->current_state_id
;
271 if (state
!= SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD
) {
272 dev_warn(scirdev_to_dev(sci_dev
), "%s: in wrong state: %d\n",
274 return SCI_FAILURE_INVALID_STATE
;
277 return scic_sds_remote_node_context_suspend(&sci_dev
->rnc
,
278 suspend_type
, NULL
, NULL
);
281 enum sci_status
scic_sds_remote_device_frame_handler(struct scic_sds_remote_device
*sci_dev
,
284 struct sci_base_state_machine
*sm
= &sci_dev
->state_machine
;
285 enum scic_sds_remote_device_states state
= sm
->current_state_id
;
286 struct scic_sds_controller
*scic
= sci_dev
->owning_port
->owning_controller
;
287 enum sci_status status
;
290 case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL
:
291 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED
:
292 case SCI_BASE_REMOTE_DEVICE_STATE_STARTING
:
293 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE
:
294 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE
:
295 case SCI_BASE_REMOTE_DEVICE_STATE_FINAL
:
297 dev_warn(scirdev_to_dev(sci_dev
), "%s: in wrong state: %d\n",
299 /* Return the frame back to the controller */
300 scic_sds_controller_release_frame(scic
, frame_index
);
301 return SCI_FAILURE_INVALID_STATE
;
302 case SCI_BASE_REMOTE_DEVICE_STATE_READY
:
303 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR
:
304 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET
:
305 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING
:
306 case SCI_BASE_REMOTE_DEVICE_STATE_FAILED
:
307 case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING
: {
308 struct scic_sds_request
*sci_req
;
309 struct ssp_frame_hdr hdr
;
313 status
= scic_sds_unsolicited_frame_control_get_header(&scic
->uf_control
,
316 if (status
!= SCI_SUCCESS
)
319 word_cnt
= sizeof(hdr
) / sizeof(u32
);
320 sci_swab32_cpy(&hdr
, frame_header
, word_cnt
);
322 sci_req
= scic_request_by_tag(scic
, be16_to_cpu(hdr
.tag
));
323 if (sci_req
&& sci_req
->target_device
== sci_dev
) {
324 /* The IO request is now in charge of releasing the frame */
325 status
= sci_req
->state_handlers
->frame_handler(sci_req
,
328 /* We could not map this tag to a valid IO
329 * request Just toss the frame and continue
331 scic_sds_controller_release_frame(scic
, frame_index
);
335 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ
: {
336 struct dev_to_host_fis
*hdr
;
338 status
= scic_sds_unsolicited_frame_control_get_header(&scic
->uf_control
,
341 if (status
!= SCI_SUCCESS
)
344 if (hdr
->fis_type
== FIS_SETDEVBITS
&&
345 (hdr
->status
& ATA_ERR
)) {
346 sci_dev
->not_ready_reason
= SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED
;
348 /* TODO Check sactive and complete associated IO if any. */
349 sci_base_state_machine_change_state(sm
, SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR
);
350 } else if (hdr
->fis_type
== FIS_REGD2H
&&
351 (hdr
->status
& ATA_ERR
)) {
353 * Some devices return D2H FIS when an NCQ error is detected.
354 * Treat this like an SDB error FIS ready reason.
356 sci_dev
->not_ready_reason
= SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED
;
357 sci_base_state_machine_change_state(&sci_dev
->state_machine
,
358 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR
);
360 status
= SCI_FAILURE
;
362 scic_sds_controller_release_frame(scic
, frame_index
);
365 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD
:
366 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD
:
367 /* The device does not process any UF received from the hardware while
368 * in this state. All unsolicited frames are forwarded to the io request
371 status
= scic_sds_io_request_frame_handler(sci_dev
->working_request
, frame_index
);
378 static bool is_remote_device_ready(struct scic_sds_remote_device
*sci_dev
)
381 struct sci_base_state_machine
*sm
= &sci_dev
->state_machine
;
382 enum scic_sds_remote_device_states state
= sm
->current_state_id
;
385 case SCI_BASE_REMOTE_DEVICE_STATE_READY
:
386 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE
:
387 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD
:
388 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ
:
389 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR
:
390 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET
:
391 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE
:
392 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD
:
399 enum sci_status
scic_sds_remote_device_event_handler(struct scic_sds_remote_device
*sci_dev
,
402 struct sci_base_state_machine
*sm
= &sci_dev
->state_machine
;
403 enum scic_sds_remote_device_states state
= sm
->current_state_id
;
404 enum sci_status status
;
406 switch (scu_get_event_type(event_code
)) {
407 case SCU_EVENT_TYPE_RNC_OPS_MISC
:
408 case SCU_EVENT_TYPE_RNC_SUSPEND_TX
:
409 case SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX
:
410 status
= scic_sds_remote_node_context_event_handler(&sci_dev
->rnc
, event_code
);
412 case SCU_EVENT_TYPE_PTX_SCHEDULE_EVENT
:
413 if (scu_get_event_code(event_code
) == SCU_EVENT_IT_NEXUS_TIMEOUT
) {
414 status
= SCI_SUCCESS
;
416 /* Suspend the associated RNC */
417 scic_sds_remote_node_context_suspend(&sci_dev
->rnc
,
418 SCI_SOFTWARE_SUSPENSION
,
421 dev_dbg(scirdev_to_dev(sci_dev
),
422 "%s: device: %p event code: %x: %s\n",
423 __func__
, sci_dev
, event_code
,
424 is_remote_device_ready(sci_dev
)
425 ? "I_T_Nexus_Timeout event"
426 : "I_T_Nexus_Timeout event in wrong state");
430 /* Else, fall through and treat as unhandled... */
432 dev_dbg(scirdev_to_dev(sci_dev
),
433 "%s: device: %p event code: %x: %s\n",
434 __func__
, sci_dev
, event_code
,
435 is_remote_device_ready(sci_dev
)
437 : "unexpected event in wrong state");
438 status
= SCI_FAILURE_INVALID_STATE
;
442 if (status
!= SCI_SUCCESS
)
445 if (state
== SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE
) {
447 /* We pick up suspension events to handle specifically to this
448 * state. We resume the RNC right away.
450 if (scu_get_event_type(event_code
) == SCU_EVENT_TYPE_RNC_SUSPEND_TX
||
451 scu_get_event_type(event_code
) == SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX
)
452 status
= scic_sds_remote_node_context_resume(&sci_dev
->rnc
, NULL
, NULL
);
458 static void scic_sds_remote_device_start_request(struct scic_sds_remote_device
*sci_dev
,
459 struct scic_sds_request
*sci_req
,
460 enum sci_status status
)
462 struct scic_sds_port
*sci_port
= sci_dev
->owning_port
;
464 /* cleanup requests that failed after starting on the port */
465 if (status
!= SCI_SUCCESS
)
466 scic_sds_port_complete_io(sci_port
, sci_dev
, sci_req
);
468 scic_sds_remote_device_increment_request_count(sci_dev
);
471 enum sci_status
scic_sds_remote_device_start_io(struct scic_sds_controller
*scic
,
472 struct scic_sds_remote_device
*sci_dev
,
473 struct scic_sds_request
*sci_req
)
475 struct sci_base_state_machine
*sm
= &sci_dev
->state_machine
;
476 enum scic_sds_remote_device_states state
= sm
->current_state_id
;
477 struct scic_sds_port
*sci_port
= sci_dev
->owning_port
;
478 struct isci_request
*ireq
= sci_req
->ireq
;
479 enum sci_status status
;
482 case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL
:
483 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED
:
484 case SCI_BASE_REMOTE_DEVICE_STATE_STARTING
:
485 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR
:
486 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING
:
487 case SCI_BASE_REMOTE_DEVICE_STATE_FAILED
:
488 case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING
:
489 case SCI_BASE_REMOTE_DEVICE_STATE_FINAL
:
491 dev_warn(scirdev_to_dev(sci_dev
), "%s: in wrong state: %d\n",
493 return SCI_FAILURE_INVALID_STATE
;
494 case SCI_BASE_REMOTE_DEVICE_STATE_READY
:
495 /* attempt to start an io request for this device object. The remote
496 * device object will issue the start request for the io and if
497 * successful it will start the request for the port object then
498 * increment its own request count.
500 status
= scic_sds_port_start_io(sci_port
, sci_dev
, sci_req
);
501 if (status
!= SCI_SUCCESS
)
504 status
= scic_sds_remote_node_context_start_io(&sci_dev
->rnc
, sci_req
);
505 if (status
!= SCI_SUCCESS
)
508 status
= scic_sds_request_start(sci_req
);
510 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE
: {
511 /* handle the start io operation for a sata device that is in
512 * the command idle state. - Evalute the type of IO request to
513 * be started - If its an NCQ request change to NCQ substate -
514 * If its any other command change to the CMD substate
516 * If this is a softreset we may want to have a different
519 enum scic_sds_remote_device_states new_state
;
520 struct sas_task
*task
= isci_request_access_task(ireq
);
522 status
= scic_sds_port_start_io(sci_port
, sci_dev
, sci_req
);
523 if (status
!= SCI_SUCCESS
)
526 status
= scic_sds_remote_node_context_start_io(&sci_dev
->rnc
, sci_req
);
527 if (status
!= SCI_SUCCESS
)
530 status
= sci_req
->state_handlers
->start_handler(sci_req
);
531 if (status
!= SCI_SUCCESS
)
534 if (task
->ata_task
.use_ncq
)
535 new_state
= SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ
;
537 sci_dev
->working_request
= sci_req
;
538 new_state
= SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD
;
540 sci_base_state_machine_change_state(sm
, new_state
);
543 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ
: {
544 struct sas_task
*task
= isci_request_access_task(ireq
);
546 if (task
->ata_task
.use_ncq
) {
547 status
= scic_sds_port_start_io(sci_port
, sci_dev
, sci_req
);
548 if (status
!= SCI_SUCCESS
)
551 status
= scic_sds_remote_node_context_start_io(&sci_dev
->rnc
, sci_req
);
552 if (status
!= SCI_SUCCESS
)
555 status
= sci_req
->state_handlers
->start_handler(sci_req
);
557 return SCI_FAILURE_INVALID_STATE
;
560 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET
:
561 return SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED
;
562 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE
:
563 status
= scic_sds_port_start_io(sci_port
, sci_dev
, sci_req
);
564 if (status
!= SCI_SUCCESS
)
567 status
= scic_sds_remote_node_context_start_io(&sci_dev
->rnc
, sci_req
);
568 if (status
!= SCI_SUCCESS
)
571 status
= scic_sds_request_start(sci_req
);
572 if (status
!= SCI_SUCCESS
)
575 sci_dev
->working_request
= sci_req
;
576 sci_base_state_machine_change_state(&sci_dev
->state_machine
,
577 SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD
);
579 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD
:
580 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD
:
581 /* device is already handling a command it can not accept new commands
582 * until this one is complete.
584 return SCI_FAILURE_INVALID_STATE
;
587 scic_sds_remote_device_start_request(sci_dev
, sci_req
, status
);
591 static enum sci_status
common_complete_io(struct scic_sds_port
*sci_port
,
592 struct scic_sds_remote_device
*sci_dev
,
593 struct scic_sds_request
*sci_req
)
595 enum sci_status status
;
597 status
= scic_sds_request_complete(sci_req
);
598 if (status
!= SCI_SUCCESS
)
601 status
= scic_sds_port_complete_io(sci_port
, sci_dev
, sci_req
);
602 if (status
!= SCI_SUCCESS
)
605 scic_sds_remote_device_decrement_request_count(sci_dev
);
609 enum sci_status
scic_sds_remote_device_complete_io(struct scic_sds_controller
*scic
,
610 struct scic_sds_remote_device
*sci_dev
,
611 struct scic_sds_request
*sci_req
)
613 struct sci_base_state_machine
*sm
= &sci_dev
->state_machine
;
614 enum scic_sds_remote_device_states state
= sm
->current_state_id
;
615 struct scic_sds_port
*sci_port
= sci_dev
->owning_port
;
616 enum sci_status status
;
619 case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL
:
620 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED
:
621 case SCI_BASE_REMOTE_DEVICE_STATE_STARTING
:
622 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE
:
623 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE
:
624 case SCI_BASE_REMOTE_DEVICE_STATE_FAILED
:
625 case SCI_BASE_REMOTE_DEVICE_STATE_FINAL
:
627 dev_warn(scirdev_to_dev(sci_dev
), "%s: in wrong state: %d\n",
629 return SCI_FAILURE_INVALID_STATE
;
630 case SCI_BASE_REMOTE_DEVICE_STATE_READY
:
631 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET
:
632 case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING
:
633 status
= common_complete_io(sci_port
, sci_dev
, sci_req
);
635 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD
:
636 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ
:
637 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR
:
638 status
= common_complete_io(sci_port
, sci_dev
, sci_req
);
639 if (status
!= SCI_SUCCESS
)
642 if (sci_req
->sci_status
== SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED
) {
643 /* This request causes hardware error, device needs to be Lun Reset.
644 * So here we force the state machine to IDLE state so the rest IOs
645 * can reach RNC state handler, these IOs will be completed by RNC with
646 * status of "DEVICE_RESET_REQUIRED", instead of "INVALID STATE".
648 sci_base_state_machine_change_state(sm
, SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET
);
649 } else if (scic_sds_remote_device_get_request_count(sci_dev
) == 0)
650 sci_base_state_machine_change_state(sm
, SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE
);
652 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD
:
653 status
= common_complete_io(sci_port
, sci_dev
, sci_req
);
654 if (status
!= SCI_SUCCESS
)
656 sci_base_state_machine_change_state(sm
, SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE
);
658 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING
:
659 status
= common_complete_io(sci_port
, sci_dev
, sci_req
);
660 if (status
!= SCI_SUCCESS
)
663 if (scic_sds_remote_device_get_request_count(sci_dev
) == 0)
664 scic_sds_remote_node_context_destruct(&sci_dev
->rnc
,
670 if (status
!= SCI_SUCCESS
)
671 dev_err(scirdev_to_dev(sci_dev
),
672 "%s: Port:0x%p Device:0x%p Request:0x%p Status:0x%x "
673 "could not complete\n", __func__
, sci_port
,
674 sci_dev
, sci_req
, status
);
679 static void scic_sds_remote_device_continue_request(void *dev
)
681 struct scic_sds_remote_device
*sci_dev
= dev
;
683 /* we need to check if this request is still valid to continue. */
684 if (sci_dev
->working_request
)
685 scic_controller_continue_io(sci_dev
->working_request
);
688 enum sci_status
scic_sds_remote_device_start_task(struct scic_sds_controller
*scic
,
689 struct scic_sds_remote_device
*sci_dev
,
690 struct scic_sds_request
*sci_req
)
692 struct sci_base_state_machine
*sm
= &sci_dev
->state_machine
;
693 enum scic_sds_remote_device_states state
= sm
->current_state_id
;
694 struct scic_sds_port
*sci_port
= sci_dev
->owning_port
;
695 enum sci_status status
;
698 case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL
:
699 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED
:
700 case SCI_BASE_REMOTE_DEVICE_STATE_STARTING
:
701 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE
:
702 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD
:
703 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING
:
704 case SCI_BASE_REMOTE_DEVICE_STATE_FAILED
:
705 case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING
:
706 case SCI_BASE_REMOTE_DEVICE_STATE_FINAL
:
708 dev_warn(scirdev_to_dev(sci_dev
), "%s: in wrong state: %d\n",
710 return SCI_FAILURE_INVALID_STATE
;
711 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE
:
712 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD
:
713 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ
:
714 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR
:
715 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET
:
716 status
= scic_sds_port_start_io(sci_port
, sci_dev
, sci_req
);
717 if (status
!= SCI_SUCCESS
)
720 status
= scic_sds_remote_node_context_start_task(&sci_dev
->rnc
, sci_req
);
721 if (status
!= SCI_SUCCESS
)
724 status
= sci_req
->state_handlers
->start_handler(sci_req
);
725 if (status
!= SCI_SUCCESS
)
728 /* Note: If the remote device state is not IDLE this will
729 * replace the request that probably resulted in the task
730 * management request.
732 sci_dev
->working_request
= sci_req
;
733 sci_base_state_machine_change_state(sm
, SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD
);
735 /* The remote node context must cleanup the TCi to NCQ mapping
736 * table. The only way to do this correctly is to either write
737 * to the TLCR register or to invalidate and repost the RNC. In
738 * either case the remote node context state machine will take
739 * the correct action when the remote node context is suspended
742 scic_sds_remote_node_context_suspend(&sci_dev
->rnc
,
743 SCI_SOFTWARE_SUSPENSION
, NULL
, NULL
);
744 scic_sds_remote_node_context_resume(&sci_dev
->rnc
,
745 scic_sds_remote_device_continue_request
,
749 scic_sds_remote_device_start_request(sci_dev
, sci_req
, status
);
750 /* We need to let the controller start request handler know that
751 * it can't post TC yet. We will provide a callback function to
752 * post TC when RNC gets resumed.
754 return SCI_FAILURE_RESET_DEVICE_PARTIAL_SUCCESS
;
755 case SCI_BASE_REMOTE_DEVICE_STATE_READY
:
756 status
= scic_sds_port_start_io(sci_port
, sci_dev
, sci_req
);
757 if (status
!= SCI_SUCCESS
)
760 status
= scic_sds_remote_node_context_start_task(&sci_dev
->rnc
, sci_req
);
761 if (status
!= SCI_SUCCESS
)
764 status
= scic_sds_request_start(sci_req
);
767 scic_sds_remote_device_start_request(sci_dev
, sci_req
, status
);
777 * This method takes the request and bulids an appropriate SCU context for the
778 * request and then requests the controller to post the request. none
780 void scic_sds_remote_device_post_request(
781 struct scic_sds_remote_device
*sci_dev
,
786 context
= scic_sds_remote_device_build_command_context(sci_dev
, request
);
788 scic_sds_controller_post_request(
789 scic_sds_remote_device_get_controller(sci_dev
),
794 /* called once the remote node context has transisitioned to a
795 * ready state. This is the indication that the remote device object can also
796 * transition to ready.
798 static void remote_device_resume_done(void *_dev
)
800 struct scic_sds_remote_device
*sci_dev
= _dev
;
802 if (is_remote_device_ready(sci_dev
))
805 /* go 'ready' if we are not already in a ready state */
806 sci_base_state_machine_change_state(&sci_dev
->state_machine
,
807 SCI_BASE_REMOTE_DEVICE_STATE_READY
);
810 static void scic_sds_stp_remote_device_ready_idle_substate_resume_complete_handler(void *_dev
)
812 struct scic_sds_remote_device
*sci_dev
= _dev
;
813 struct isci_remote_device
*idev
= sci_dev_to_idev(sci_dev
);
814 struct scic_sds_controller
*scic
= sci_dev
->owning_port
->owning_controller
;
816 /* For NCQ operation we do not issue a isci_remote_device_not_ready().
817 * As a result, avoid sending the ready notification.
819 if (sci_dev
->state_machine
.previous_state_id
!= SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ
)
820 isci_remote_device_ready(scic
->ihost
, idev
);
823 static void scic_sds_remote_device_initial_state_enter(void *object
)
825 struct scic_sds_remote_device
*sci_dev
= object
;
827 /* Initial state is a transitional state to the stopped state */
828 sci_base_state_machine_change_state(&sci_dev
->state_machine
,
829 SCI_BASE_REMOTE_DEVICE_STATE_STOPPED
);
833 * scic_remote_device_destruct() - free remote node context and destruct
834 * @remote_device: This parameter specifies the remote device to be destructed.
836 * Remote device objects are a limited resource. As such, they must be
837 * protected. Thus calls to construct and destruct are mutually exclusive and
838 * non-reentrant. The return value shall indicate if the device was
839 * successfully destructed or if some failure occurred. enum sci_status This value
840 * is returned if the device is successfully destructed.
841 * SCI_FAILURE_INVALID_REMOTE_DEVICE This value is returned if the supplied
842 * device isn't valid (e.g. it's already been destoryed, the handle isn't
845 static enum sci_status
scic_remote_device_destruct(struct scic_sds_remote_device
*sci_dev
)
847 struct sci_base_state_machine
*sm
= &sci_dev
->state_machine
;
848 enum scic_sds_remote_device_states state
= sm
->current_state_id
;
849 struct scic_sds_controller
*scic
;
851 if (state
!= SCI_BASE_REMOTE_DEVICE_STATE_STOPPED
) {
852 dev_warn(scirdev_to_dev(sci_dev
), "%s: in wrong state: %d\n",
854 return SCI_FAILURE_INVALID_STATE
;
857 scic
= sci_dev
->owning_port
->owning_controller
;
858 scic_sds_controller_free_remote_node_context(scic
, sci_dev
,
859 sci_dev
->rnc
.remote_node_index
);
860 sci_dev
->rnc
.remote_node_index
= SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX
;
861 sci_base_state_machine_change_state(sm
, SCI_BASE_REMOTE_DEVICE_STATE_FINAL
);
867 * isci_remote_device_deconstruct() - This function frees an isci_remote_device.
868 * @ihost: This parameter specifies the isci host object.
869 * @idev: This parameter specifies the remote device to be freed.
872 static void isci_remote_device_deconstruct(struct isci_host
*ihost
, struct isci_remote_device
*idev
)
874 dev_dbg(&ihost
->pdev
->dev
,
875 "%s: isci_device = %p\n", __func__
, idev
);
877 /* There should not be any outstanding io's. All paths to
878 * here should go through isci_remote_device_nuke_requests.
879 * If we hit this condition, we will need a way to complete
880 * io requests in process */
881 while (!list_empty(&idev
->reqs_in_process
)) {
883 dev_err(&ihost
->pdev
->dev
,
884 "%s: ** request list not empty! **\n", __func__
);
888 scic_remote_device_destruct(&idev
->sci
);
889 idev
->domain_dev
->lldd_dev
= NULL
;
890 idev
->domain_dev
= NULL
;
891 idev
->isci_port
= NULL
;
892 list_del_init(&idev
->node
);
894 clear_bit(IDEV_START_PENDING
, &idev
->flags
);
895 clear_bit(IDEV_STOP_PENDING
, &idev
->flags
);
896 wake_up(&ihost
->eventq
);
900 * isci_remote_device_stop_complete() - This function is called by the scic
901 * when the remote device stop has completed. We mark the isci device as not
902 * ready and remove the isci remote device.
903 * @ihost: This parameter specifies the isci host object.
904 * @idev: This parameter specifies the remote device.
905 * @status: This parameter specifies status of the completion.
908 static void isci_remote_device_stop_complete(struct isci_host
*ihost
,
909 struct isci_remote_device
*idev
)
911 dev_dbg(&ihost
->pdev
->dev
, "%s: complete idev = %p\n", __func__
, idev
);
913 isci_remote_device_change_state(idev
, isci_stopped
);
915 /* after stop, we can tear down resources. */
916 isci_remote_device_deconstruct(ihost
, idev
);
919 static void scic_sds_remote_device_stopped_state_enter(void *object
)
921 struct scic_sds_remote_device
*sci_dev
= object
;
922 struct scic_sds_controller
*scic
;
923 struct isci_remote_device
*idev
;
924 struct isci_host
*ihost
;
927 scic
= scic_sds_remote_device_get_controller(sci_dev
);
929 idev
= sci_dev_to_idev(sci_dev
);
931 /* If we are entering from the stopping state let the SCI User know that
932 * the stop operation has completed.
934 prev_state
= sci_dev
->state_machine
.previous_state_id
;
935 if (prev_state
== SCI_BASE_REMOTE_DEVICE_STATE_STOPPING
)
936 isci_remote_device_stop_complete(ihost
, idev
);
938 scic_sds_controller_remote_device_stopped(scic
, sci_dev
);
941 static void scic_sds_remote_device_starting_state_enter(void *object
)
943 struct scic_sds_remote_device
*sci_dev
= object
;
944 struct scic_sds_controller
*scic
= scic_sds_remote_device_get_controller(sci_dev
);
945 struct isci_host
*ihost
= scic
->ihost
;
946 struct isci_remote_device
*idev
= sci_dev_to_idev(sci_dev
);
948 isci_remote_device_not_ready(ihost
, idev
,
949 SCIC_REMOTE_DEVICE_NOT_READY_START_REQUESTED
);
952 static void scic_sds_remote_device_ready_state_enter(void *object
)
954 struct scic_sds_remote_device
*sci_dev
= object
;
955 struct scic_sds_controller
*scic
= sci_dev
->owning_port
->owning_controller
;
956 struct domain_device
*dev
= sci_dev_to_domain(sci_dev
);
958 scic
->remote_device_sequence
[sci_dev
->rnc
.remote_node_index
]++;
960 if (dev
->dev_type
== SATA_DEV
|| (dev
->tproto
& SAS_PROTOCOL_SATA
)) {
961 sci_base_state_machine_change_state(&sci_dev
->state_machine
,
962 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE
);
963 } else if (dev_is_expander(dev
)) {
964 sci_base_state_machine_change_state(&sci_dev
->state_machine
,
965 SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE
);
967 isci_remote_device_ready(scic
->ihost
, sci_dev_to_idev(sci_dev
));
970 static void scic_sds_remote_device_ready_state_exit(void *object
)
972 struct scic_sds_remote_device
*sci_dev
= object
;
973 struct domain_device
*dev
= sci_dev_to_domain(sci_dev
);
975 if (dev
->dev_type
== SAS_END_DEV
) {
976 struct scic_sds_controller
*scic
= sci_dev
->owning_port
->owning_controller
;
977 struct isci_remote_device
*idev
= sci_dev_to_idev(sci_dev
);
979 isci_remote_device_not_ready(scic
->ihost
, idev
,
980 SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED
);
984 static void scic_sds_remote_device_resetting_state_enter(void *object
)
986 struct scic_sds_remote_device
*sci_dev
= object
;
988 scic_sds_remote_node_context_suspend(
989 &sci_dev
->rnc
, SCI_SOFTWARE_SUSPENSION
, NULL
, NULL
);
992 static void scic_sds_remote_device_resetting_state_exit(void *object
)
994 struct scic_sds_remote_device
*sci_dev
= object
;
996 scic_sds_remote_node_context_resume(&sci_dev
->rnc
, NULL
, NULL
);
999 static void scic_sds_stp_remote_device_ready_idle_substate_enter(void *object
)
1001 struct scic_sds_remote_device
*sci_dev
= object
;
1003 sci_dev
->working_request
= NULL
;
1004 if (scic_sds_remote_node_context_is_ready(&sci_dev
->rnc
)) {
1006 * Since the RNC is ready, it's alright to finish completion
1007 * processing (e.g. signal the remote device is ready). */
1008 scic_sds_stp_remote_device_ready_idle_substate_resume_complete_handler(sci_dev
);
1010 scic_sds_remote_node_context_resume(&sci_dev
->rnc
,
1011 scic_sds_stp_remote_device_ready_idle_substate_resume_complete_handler
,
1016 static void scic_sds_stp_remote_device_ready_cmd_substate_enter(void *object
)
1018 struct scic_sds_remote_device
*sci_dev
= object
;
1019 struct scic_sds_controller
*scic
= scic_sds_remote_device_get_controller(sci_dev
);
1021 BUG_ON(sci_dev
->working_request
== NULL
);
1023 isci_remote_device_not_ready(scic
->ihost
, sci_dev_to_idev(sci_dev
),
1024 SCIC_REMOTE_DEVICE_NOT_READY_SATA_REQUEST_STARTED
);
1027 static void scic_sds_stp_remote_device_ready_ncq_error_substate_enter(void *object
)
1029 struct scic_sds_remote_device
*sci_dev
= object
;
1030 struct scic_sds_controller
*scic
= scic_sds_remote_device_get_controller(sci_dev
);
1031 struct isci_remote_device
*idev
= sci_dev_to_idev(sci_dev
);
1033 if (sci_dev
->not_ready_reason
== SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED
)
1034 isci_remote_device_not_ready(scic
->ihost
, idev
,
1035 sci_dev
->not_ready_reason
);
1038 static void scic_sds_smp_remote_device_ready_idle_substate_enter(void *object
)
1040 struct scic_sds_remote_device
*sci_dev
= object
;
1041 struct scic_sds_controller
*scic
= scic_sds_remote_device_get_controller(sci_dev
);
1043 isci_remote_device_ready(scic
->ihost
, sci_dev_to_idev(sci_dev
));
1046 static void scic_sds_smp_remote_device_ready_cmd_substate_enter(void *object
)
1048 struct scic_sds_remote_device
*sci_dev
= object
;
1049 struct scic_sds_controller
*scic
= scic_sds_remote_device_get_controller(sci_dev
);
1051 BUG_ON(sci_dev
->working_request
== NULL
);
1053 isci_remote_device_not_ready(scic
->ihost
, sci_dev_to_idev(sci_dev
),
1054 SCIC_REMOTE_DEVICE_NOT_READY_SMP_REQUEST_STARTED
);
1057 static void scic_sds_smp_remote_device_ready_cmd_substate_exit(void *object
)
1059 struct scic_sds_remote_device
*sci_dev
= object
;
1061 sci_dev
->working_request
= NULL
;
1064 static const struct sci_base_state scic_sds_remote_device_state_table
[] = {
1065 [SCI_BASE_REMOTE_DEVICE_STATE_INITIAL
] = {
1066 .enter_state
= scic_sds_remote_device_initial_state_enter
,
1068 [SCI_BASE_REMOTE_DEVICE_STATE_STOPPED
] = {
1069 .enter_state
= scic_sds_remote_device_stopped_state_enter
,
1071 [SCI_BASE_REMOTE_DEVICE_STATE_STARTING
] = {
1072 .enter_state
= scic_sds_remote_device_starting_state_enter
,
1074 [SCI_BASE_REMOTE_DEVICE_STATE_READY
] = {
1075 .enter_state
= scic_sds_remote_device_ready_state_enter
,
1076 .exit_state
= scic_sds_remote_device_ready_state_exit
1078 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE
] = {
1079 .enter_state
= scic_sds_stp_remote_device_ready_idle_substate_enter
,
1081 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD
] = {
1082 .enter_state
= scic_sds_stp_remote_device_ready_cmd_substate_enter
,
1084 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ
] = { },
1085 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR
] = {
1086 .enter_state
= scic_sds_stp_remote_device_ready_ncq_error_substate_enter
,
1088 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET
] = { },
1089 [SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE
] = {
1090 .enter_state
= scic_sds_smp_remote_device_ready_idle_substate_enter
,
1092 [SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD
] = {
1093 .enter_state
= scic_sds_smp_remote_device_ready_cmd_substate_enter
,
1094 .exit_state
= scic_sds_smp_remote_device_ready_cmd_substate_exit
,
1096 [SCI_BASE_REMOTE_DEVICE_STATE_STOPPING
] = { },
1097 [SCI_BASE_REMOTE_DEVICE_STATE_FAILED
] = { },
1098 [SCI_BASE_REMOTE_DEVICE_STATE_RESETTING
] = {
1099 .enter_state
= scic_sds_remote_device_resetting_state_enter
,
1100 .exit_state
= scic_sds_remote_device_resetting_state_exit
1102 [SCI_BASE_REMOTE_DEVICE_STATE_FINAL
] = { },
1106 * scic_remote_device_construct() - common construction
1107 * @sci_port: SAS/SATA port through which this device is accessed.
1108 * @sci_dev: remote device to construct
1110 * This routine just performs benign initialization and does not
1111 * allocate the remote_node_context which is left to
1112 * scic_remote_device_[de]a_construct(). scic_remote_device_destruct()
1113 * frees the remote_node_context(s) for the device.
1115 static void scic_remote_device_construct(struct scic_sds_port
*sci_port
,
1116 struct scic_sds_remote_device
*sci_dev
)
1118 sci_dev
->owning_port
= sci_port
;
1119 sci_dev
->started_request_count
= 0;
1121 sci_base_state_machine_construct(
1122 &sci_dev
->state_machine
,
1124 scic_sds_remote_device_state_table
,
1125 SCI_BASE_REMOTE_DEVICE_STATE_INITIAL
1128 sci_base_state_machine_start(
1129 &sci_dev
->state_machine
1132 scic_sds_remote_node_context_construct(&sci_dev
->rnc
,
1133 SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX
);
1137 * scic_remote_device_da_construct() - construct direct attached device.
1139 * The information (e.g. IAF, Signature FIS, etc.) necessary to build
1140 * the device is known to the SCI Core since it is contained in the
1141 * scic_phy object. Remote node context(s) is/are a global resource
1142 * allocated by this routine, freed by scic_remote_device_destruct().
1145 * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
1146 * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
1147 * sata-only controller instance.
1148 * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
1150 static enum sci_status
scic_remote_device_da_construct(struct scic_sds_port
*sci_port
,
1151 struct scic_sds_remote_device
*sci_dev
)
1153 enum sci_status status
;
1154 struct domain_device
*dev
= sci_dev_to_domain(sci_dev
);
1156 scic_remote_device_construct(sci_port
, sci_dev
);
1159 * This information is request to determine how many remote node context
1160 * entries will be needed to store the remote node.
1162 sci_dev
->is_direct_attached
= true;
1163 status
= scic_sds_controller_allocate_remote_node_context(sci_port
->owning_controller
,
1165 &sci_dev
->rnc
.remote_node_index
);
1167 if (status
!= SCI_SUCCESS
)
1170 if (dev
->dev_type
== SAS_END_DEV
|| dev
->dev_type
== SATA_DEV
||
1171 (dev
->tproto
& SAS_PROTOCOL_STP
) || dev_is_expander(dev
))
1174 return SCI_FAILURE_UNSUPPORTED_PROTOCOL
;
1176 sci_dev
->connection_rate
= scic_sds_port_get_max_allowed_speed(sci_port
);
1178 /* / @todo Should I assign the port width by reading all of the phys on the port? */
1179 sci_dev
->device_port_width
= 1;
1185 * scic_remote_device_ea_construct() - construct expander attached device
1187 * Remote node context(s) is/are a global resource allocated by this
1188 * routine, freed by scic_remote_device_destruct().
1191 * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
1192 * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
1193 * sata-only controller instance.
1194 * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
1196 static enum sci_status
scic_remote_device_ea_construct(struct scic_sds_port
*sci_port
,
1197 struct scic_sds_remote_device
*sci_dev
)
1199 struct domain_device
*dev
= sci_dev_to_domain(sci_dev
);
1200 enum sci_status status
;
1202 scic_remote_device_construct(sci_port
, sci_dev
);
1204 status
= scic_sds_controller_allocate_remote_node_context(sci_port
->owning_controller
,
1206 &sci_dev
->rnc
.remote_node_index
);
1207 if (status
!= SCI_SUCCESS
)
1210 if (dev
->dev_type
== SAS_END_DEV
|| dev
->dev_type
== SATA_DEV
||
1211 (dev
->tproto
& SAS_PROTOCOL_STP
) || dev_is_expander(dev
))
1214 return SCI_FAILURE_UNSUPPORTED_PROTOCOL
;
1217 * For SAS-2 the physical link rate is actually a logical link
1218 * rate that incorporates multiplexing. The SCU doesn't
1219 * incorporate multiplexing and for the purposes of the
1220 * connection the logical link rate is that same as the
1221 * physical. Furthermore, the SAS-2 and SAS-1.1 fields overlay
1222 * one another, so this code works for both situations. */
1223 sci_dev
->connection_rate
= min_t(u16
, scic_sds_port_get_max_allowed_speed(sci_port
),
1226 /* / @todo Should I assign the port width by reading all of the phys on the port? */
1227 sci_dev
->device_port_width
= 1;
1233 * scic_remote_device_start() - This method will start the supplied remote
1234 * device. This method enables normal IO requests to flow through to the
1236 * @remote_device: This parameter specifies the device to be started.
1237 * @timeout: This parameter specifies the number of milliseconds in which the
1238 * start operation should complete.
1240 * An indication of whether the device was successfully started. SCI_SUCCESS
1241 * This value is returned if the device was successfully started.
1242 * SCI_FAILURE_INVALID_PHY This value is returned if the user attempts to start
1243 * the device when there have been no phys added to it.
1245 static enum sci_status
scic_remote_device_start(struct scic_sds_remote_device
*sci_dev
,
1248 struct sci_base_state_machine
*sm
= &sci_dev
->state_machine
;
1249 enum scic_sds_remote_device_states state
= sm
->current_state_id
;
1250 enum sci_status status
;
1252 if (state
!= SCI_BASE_REMOTE_DEVICE_STATE_STOPPED
) {
1253 dev_warn(scirdev_to_dev(sci_dev
), "%s: in wrong state: %d\n",
1255 return SCI_FAILURE_INVALID_STATE
;
1258 status
= scic_sds_remote_node_context_resume(&sci_dev
->rnc
,
1259 remote_device_resume_done
,
1261 if (status
!= SCI_SUCCESS
)
1264 sci_base_state_machine_change_state(sm
, SCI_BASE_REMOTE_DEVICE_STATE_STARTING
);
1269 static enum sci_status
isci_remote_device_construct(struct isci_port
*iport
,
1270 struct isci_remote_device
*idev
)
1272 struct scic_sds_port
*sci_port
= iport
->sci_port_handle
;
1273 struct isci_host
*ihost
= iport
->isci_host
;
1274 struct domain_device
*dev
= idev
->domain_dev
;
1275 enum sci_status status
;
1277 if (dev
->parent
&& dev_is_expander(dev
->parent
))
1278 status
= scic_remote_device_ea_construct(sci_port
, &idev
->sci
);
1280 status
= scic_remote_device_da_construct(sci_port
, &idev
->sci
);
1282 if (status
!= SCI_SUCCESS
) {
1283 dev_dbg(&ihost
->pdev
->dev
, "%s: construct failed: %d\n",
1289 /* start the device. */
1290 status
= scic_remote_device_start(&idev
->sci
, ISCI_REMOTE_DEVICE_START_TIMEOUT
);
1292 if (status
!= SCI_SUCCESS
)
1293 dev_warn(&ihost
->pdev
->dev
, "remote device start failed: %d\n",
1299 void isci_remote_device_nuke_requests(struct isci_host
*ihost
, struct isci_remote_device
*idev
)
1301 DECLARE_COMPLETION_ONSTACK(aborted_task_completion
);
1303 dev_dbg(&ihost
->pdev
->dev
,
1304 "%s: idev = %p\n", __func__
, idev
);
1306 /* Cleanup all requests pending for this device. */
1307 isci_terminate_pending_requests(ihost
, idev
, terminating
);
1309 dev_dbg(&ihost
->pdev
->dev
,
1310 "%s: idev = %p, done\n", __func__
, idev
);
1314 * This function builds the isci_remote_device when a libsas dev_found message
1316 * @isci_host: This parameter specifies the isci host object.
1317 * @port: This parameter specifies the isci_port conected to this device.
1319 * pointer to new isci_remote_device.
1321 static struct isci_remote_device
*
1322 isci_remote_device_alloc(struct isci_host
*ihost
, struct isci_port
*iport
)
1324 struct isci_remote_device
*idev
;
1327 for (i
= 0; i
< SCI_MAX_REMOTE_DEVICES
; i
++) {
1328 idev
= &ihost
->devices
[i
];
1329 if (!test_and_set_bit(IDEV_ALLOCATED
, &idev
->flags
))
1333 if (i
>= SCI_MAX_REMOTE_DEVICES
) {
1334 dev_warn(&ihost
->pdev
->dev
, "%s: failed\n", __func__
);
1338 if (WARN_ONCE(!list_empty(&idev
->reqs_in_process
), "found requests in process\n"))
1341 if (WARN_ONCE(!list_empty(&idev
->node
), "found non-idle remote device\n"))
1344 isci_remote_device_change_state(idev
, isci_freed
);
1350 * isci_remote_device_stop() - This function is called internally to stop the
1352 * @isci_host: This parameter specifies the isci host object.
1353 * @isci_device: This parameter specifies the remote device.
1355 * The status of the scic request to stop.
1357 enum sci_status
isci_remote_device_stop(struct isci_host
*ihost
, struct isci_remote_device
*idev
)
1359 enum sci_status status
;
1360 unsigned long flags
;
1362 dev_dbg(&ihost
->pdev
->dev
,
1363 "%s: isci_device = %p\n", __func__
, idev
);
1365 isci_remote_device_change_state(idev
, isci_stopping
);
1367 /* Kill all outstanding requests. */
1368 isci_remote_device_nuke_requests(ihost
, idev
);
1370 set_bit(IDEV_STOP_PENDING
, &idev
->flags
);
1372 spin_lock_irqsave(&ihost
->scic_lock
, flags
);
1373 status
= scic_remote_device_stop(&idev
->sci
, 50);
1374 spin_unlock_irqrestore(&ihost
->scic_lock
, flags
);
1376 /* Wait for the stop complete callback. */
1377 if (status
== SCI_SUCCESS
) {
1378 wait_for_device_stop(ihost
, idev
);
1379 clear_bit(IDEV_ALLOCATED
, &idev
->flags
);
1382 dev_dbg(&ihost
->pdev
->dev
,
1383 "%s: idev = %p - after completion wait\n",
1390 * isci_remote_device_gone() - This function is called by libsas when a domain
1391 * device is removed.
1392 * @domain_device: This parameter specifies the libsas domain device.
1395 void isci_remote_device_gone(struct domain_device
*dev
)
1397 struct isci_host
*ihost
= dev_to_ihost(dev
);
1398 struct isci_remote_device
*idev
= dev
->lldd_dev
;
1400 dev_dbg(&ihost
->pdev
->dev
,
1401 "%s: domain_device = %p, isci_device = %p, isci_port = %p\n",
1402 __func__
, dev
, idev
, idev
->isci_port
);
1404 isci_remote_device_stop(ihost
, idev
);
1409 * isci_remote_device_found() - This function is called by libsas when a remote
1410 * device is discovered. A remote device object is created and started. the
1411 * function then sleeps until the sci core device started message is
1413 * @domain_device: This parameter specifies the libsas domain device.
1415 * status, zero indicates success.
1417 int isci_remote_device_found(struct domain_device
*domain_dev
)
1419 struct isci_host
*isci_host
= dev_to_ihost(domain_dev
);
1420 struct isci_port
*isci_port
;
1421 struct isci_phy
*isci_phy
;
1422 struct asd_sas_port
*sas_port
;
1423 struct asd_sas_phy
*sas_phy
;
1424 struct isci_remote_device
*isci_device
;
1425 enum sci_status status
;
1427 dev_dbg(&isci_host
->pdev
->dev
,
1428 "%s: domain_device = %p\n", __func__
, domain_dev
);
1430 wait_for_start(isci_host
);
1432 sas_port
= domain_dev
->port
;
1433 sas_phy
= list_first_entry(&sas_port
->phy_list
, struct asd_sas_phy
,
1435 isci_phy
= to_isci_phy(sas_phy
);
1436 isci_port
= isci_phy
->isci_port
;
1438 /* we are being called for a device on this port,
1439 * so it has to come up eventually
1441 wait_for_completion(&isci_port
->start_complete
);
1443 if ((isci_stopping
== isci_port_get_state(isci_port
)) ||
1444 (isci_stopped
== isci_port_get_state(isci_port
)))
1447 isci_device
= isci_remote_device_alloc(isci_host
, isci_port
);
1451 INIT_LIST_HEAD(&isci_device
->node
);
1452 domain_dev
->lldd_dev
= isci_device
;
1453 isci_device
->domain_dev
= domain_dev
;
1454 isci_device
->isci_port
= isci_port
;
1455 isci_remote_device_change_state(isci_device
, isci_starting
);
1458 spin_lock_irq(&isci_host
->scic_lock
);
1459 list_add_tail(&isci_device
->node
, &isci_port
->remote_dev_list
);
1461 set_bit(IDEV_START_PENDING
, &isci_device
->flags
);
1462 status
= isci_remote_device_construct(isci_port
, isci_device
);
1463 spin_unlock_irq(&isci_host
->scic_lock
);
1465 dev_dbg(&isci_host
->pdev
->dev
,
1466 "%s: isci_device = %p\n",
1467 __func__
, isci_device
);
1469 if (status
!= SCI_SUCCESS
) {
1471 spin_lock_irq(&isci_host
->scic_lock
);
1472 isci_remote_device_deconstruct(
1476 spin_unlock_irq(&isci_host
->scic_lock
);
1480 /* wait for the device ready callback. */
1481 wait_for_device_start(isci_host
, isci_device
);
1486 * isci_device_is_reset_pending() - This function will check if there is any
1487 * pending reset condition on the device.
1488 * @request: This parameter is the isci_device object.
1490 * true if there is a reset pending for the device.
1492 bool isci_device_is_reset_pending(
1493 struct isci_host
*isci_host
,
1494 struct isci_remote_device
*isci_device
)
1496 struct isci_request
*isci_request
;
1497 struct isci_request
*tmp_req
;
1498 bool reset_is_pending
= false;
1499 unsigned long flags
;
1501 dev_dbg(&isci_host
->pdev
->dev
,
1502 "%s: isci_device = %p\n", __func__
, isci_device
);
1504 spin_lock_irqsave(&isci_host
->scic_lock
, flags
);
1506 /* Check for reset on all pending requests. */
1507 list_for_each_entry_safe(isci_request
, tmp_req
,
1508 &isci_device
->reqs_in_process
, dev_node
) {
1509 dev_dbg(&isci_host
->pdev
->dev
,
1510 "%s: isci_device = %p request = %p\n",
1511 __func__
, isci_device
, isci_request
);
1513 if (isci_request
->ttype
== io_task
) {
1514 struct sas_task
*task
= isci_request_access_task(
1517 spin_lock(&task
->task_state_lock
);
1518 if (task
->task_state_flags
& SAS_TASK_NEED_DEV_RESET
)
1519 reset_is_pending
= true;
1520 spin_unlock(&task
->task_state_lock
);
1524 spin_unlock_irqrestore(&isci_host
->scic_lock
, flags
);
1526 dev_dbg(&isci_host
->pdev
->dev
,
1527 "%s: isci_device = %p reset_is_pending = %d\n",
1528 __func__
, isci_device
, reset_is_pending
);
1530 return reset_is_pending
;
1534 * isci_device_clear_reset_pending() - This function will clear if any pending
1535 * reset condition flags on the device.
1536 * @request: This parameter is the isci_device object.
1538 * true if there is a reset pending for the device.
1540 void isci_device_clear_reset_pending(struct isci_host
*ihost
, struct isci_remote_device
*idev
)
1542 struct isci_request
*isci_request
;
1543 struct isci_request
*tmp_req
;
1544 unsigned long flags
= 0;
1546 dev_dbg(&ihost
->pdev
->dev
, "%s: idev=%p, ihost=%p\n",
1547 __func__
, idev
, ihost
);
1549 spin_lock_irqsave(&ihost
->scic_lock
, flags
);
1551 /* Clear reset pending on all pending requests. */
1552 list_for_each_entry_safe(isci_request
, tmp_req
,
1553 &idev
->reqs_in_process
, dev_node
) {
1554 dev_dbg(&ihost
->pdev
->dev
, "%s: idev = %p request = %p\n",
1555 __func__
, idev
, isci_request
);
1557 if (isci_request
->ttype
== io_task
) {
1559 unsigned long flags2
;
1560 struct sas_task
*task
= isci_request_access_task(
1563 spin_lock_irqsave(&task
->task_state_lock
, flags2
);
1564 task
->task_state_flags
&= ~SAS_TASK_NEED_DEV_RESET
;
1565 spin_unlock_irqrestore(&task
->task_state_lock
, flags2
);
1568 spin_unlock_irqrestore(&ihost
->scic_lock
, flags
);