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.
58 #include "remote_device.h"
61 #include "scic_port.h"
62 #include "scic_sds_phy.h"
63 #include "scic_sds_port.h"
64 #include "remote_node_context.h"
65 #include "scu_event_codes.h"
69 * isci_remote_device_change_state() - This function gets the status of the
70 * remote_device object.
71 * @isci_device: This parameter points to the isci_remote_device object
73 * status of the object as a isci_status enum.
75 void isci_remote_device_change_state(
76 struct isci_remote_device
*isci_device
,
77 enum isci_status status
)
81 spin_lock_irqsave(&isci_device
->state_lock
, flags
);
82 isci_device
->status
= status
;
83 spin_unlock_irqrestore(&isci_device
->state_lock
, flags
);
87 * isci_remote_device_not_ready() - This function is called by the scic when
88 * the remote device is not ready. We mark the isci device as ready (not
89 * "ready_for_io") and signal the waiting proccess.
90 * @isci_host: This parameter specifies the isci host object.
91 * @isci_device: This parameter specifies the remote device
94 static void isci_remote_device_not_ready(struct isci_host
*ihost
,
95 struct isci_remote_device
*idev
, u32 reason
)
97 dev_dbg(&ihost
->pdev
->dev
,
98 "%s: isci_device = %p\n", __func__
, idev
);
100 if (reason
== SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED
)
101 isci_remote_device_change_state(idev
, isci_stopping
);
103 /* device ready is actually a "not ready for io" state. */
104 isci_remote_device_change_state(idev
, isci_ready
);
108 * isci_remote_device_ready() - This function is called by the scic when the
109 * remote device is ready. We mark the isci device as ready and signal the
111 * @ihost: our valid isci_host
112 * @idev: remote device
115 static void isci_remote_device_ready(struct isci_host
*ihost
, struct isci_remote_device
*idev
)
117 dev_dbg(&ihost
->pdev
->dev
,
118 "%s: idev = %p\n", __func__
, idev
);
120 isci_remote_device_change_state(idev
, isci_ready_for_io
);
121 if (test_and_clear_bit(IDEV_START_PENDING
, &idev
->flags
))
122 wake_up(&ihost
->eventq
);
125 /* called once the remote node context is ready to be freed.
126 * The remote device can now report that its stop operation is complete. none
128 static void rnc_destruct_done(void *_dev
)
130 struct scic_sds_remote_device
*sci_dev
= _dev
;
132 BUG_ON(sci_dev
->started_request_count
!= 0);
133 sci_base_state_machine_change_state(&sci_dev
->state_machine
,
134 SCI_BASE_REMOTE_DEVICE_STATE_STOPPED
);
137 static enum sci_status
scic_sds_remote_device_terminate_requests(struct scic_sds_remote_device
*sci_dev
)
139 struct scic_sds_controller
*scic
= sci_dev
->owning_port
->owning_controller
;
140 u32 i
, request_count
= sci_dev
->started_request_count
;
141 enum sci_status status
= SCI_SUCCESS
;
143 for (i
= 0; i
< SCI_MAX_IO_REQUESTS
&& i
< request_count
; i
++) {
144 struct scic_sds_request
*sci_req
;
147 sci_req
= scic
->io_request_table
[i
];
148 if (!sci_req
|| sci_req
->target_device
!= sci_dev
)
150 s
= scic_controller_terminate_request(scic
, sci_dev
, sci_req
);
151 if (s
!= SCI_SUCCESS
)
158 enum sci_status
scic_remote_device_stop(struct scic_sds_remote_device
*sci_dev
,
161 struct sci_base_state_machine
*sm
= &sci_dev
->state_machine
;
162 enum scic_sds_remote_device_states state
= sm
->current_state_id
;
165 case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL
:
166 case SCI_BASE_REMOTE_DEVICE_STATE_FAILED
:
167 case SCI_BASE_REMOTE_DEVICE_STATE_FINAL
:
169 dev_warn(scirdev_to_dev(sci_dev
), "%s: in wrong state: %d\n",
171 return SCI_FAILURE_INVALID_STATE
;
172 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED
:
174 case SCI_BASE_REMOTE_DEVICE_STATE_STARTING
:
175 /* device not started so there had better be no requests */
176 BUG_ON(sci_dev
->started_request_count
!= 0);
177 scic_sds_remote_node_context_destruct(&sci_dev
->rnc
,
178 rnc_destruct_done
, sci_dev
);
179 /* Transition to the stopping state and wait for the
180 * remote node to complete being posted and invalidated.
182 sci_base_state_machine_change_state(sm
, SCI_BASE_REMOTE_DEVICE_STATE_STOPPING
);
184 case SCI_BASE_REMOTE_DEVICE_STATE_READY
:
185 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE
:
186 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD
:
187 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ
:
188 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR
:
189 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET
:
190 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE
:
191 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD
:
192 sci_base_state_machine_change_state(sm
, SCI_BASE_REMOTE_DEVICE_STATE_STOPPING
);
193 if (sci_dev
->started_request_count
== 0) {
194 scic_sds_remote_node_context_destruct(&sci_dev
->rnc
,
195 rnc_destruct_done
, sci_dev
);
198 return scic_sds_remote_device_terminate_requests(sci_dev
);
200 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING
:
201 /* All requests should have been terminated, but if there is an
202 * attempt to stop a device already in the stopping state, then
203 * try again to terminate.
205 return scic_sds_remote_device_terminate_requests(sci_dev
);
206 case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING
:
207 sci_base_state_machine_change_state(sm
, SCI_BASE_REMOTE_DEVICE_STATE_STOPPING
);
212 enum sci_status
scic_remote_device_reset(struct scic_sds_remote_device
*sci_dev
)
214 struct sci_base_state_machine
*sm
= &sci_dev
->state_machine
;
215 enum scic_sds_remote_device_states state
= sm
->current_state_id
;
218 case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL
:
219 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED
:
220 case SCI_BASE_REMOTE_DEVICE_STATE_STARTING
:
221 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE
:
222 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD
:
223 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING
:
224 case SCI_BASE_REMOTE_DEVICE_STATE_FAILED
:
225 case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING
:
226 case SCI_BASE_REMOTE_DEVICE_STATE_FINAL
:
228 dev_warn(scirdev_to_dev(sci_dev
), "%s: in wrong state: %d\n",
230 return SCI_FAILURE_INVALID_STATE
;
231 case SCI_BASE_REMOTE_DEVICE_STATE_READY
:
232 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE
:
233 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD
:
234 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ
:
235 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR
:
236 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET
:
237 sci_base_state_machine_change_state(sm
, SCI_BASE_REMOTE_DEVICE_STATE_RESETTING
);
242 enum sci_status
scic_remote_device_reset_complete(struct scic_sds_remote_device
*sci_dev
)
244 struct sci_base_state_machine
*sm
= &sci_dev
->state_machine
;
245 enum scic_sds_remote_device_states state
= sm
->current_state_id
;
247 if (state
!= SCI_BASE_REMOTE_DEVICE_STATE_RESETTING
) {
248 dev_warn(scirdev_to_dev(sci_dev
), "%s: in wrong state: %d\n",
250 return SCI_FAILURE_INVALID_STATE
;
253 sci_base_state_machine_change_state(sm
, SCI_BASE_REMOTE_DEVICE_STATE_READY
);
257 enum sci_status
scic_sds_remote_device_suspend(struct scic_sds_remote_device
*sci_dev
,
260 struct sci_base_state_machine
*sm
= &sci_dev
->state_machine
;
261 enum scic_sds_remote_device_states state
= sm
->current_state_id
;
263 if (state
!= SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD
) {
264 dev_warn(scirdev_to_dev(sci_dev
), "%s: in wrong state: %d\n",
266 return SCI_FAILURE_INVALID_STATE
;
269 return scic_sds_remote_node_context_suspend(&sci_dev
->rnc
,
270 suspend_type
, NULL
, NULL
);
273 enum sci_status
scic_sds_remote_device_frame_handler(struct scic_sds_remote_device
*sci_dev
,
276 struct sci_base_state_machine
*sm
= &sci_dev
->state_machine
;
277 enum scic_sds_remote_device_states state
= sm
->current_state_id
;
278 struct scic_sds_controller
*scic
= sci_dev
->owning_port
->owning_controller
;
279 enum sci_status status
;
282 case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL
:
283 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED
:
284 case SCI_BASE_REMOTE_DEVICE_STATE_STARTING
:
285 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE
:
286 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE
:
287 case SCI_BASE_REMOTE_DEVICE_STATE_FINAL
:
289 dev_warn(scirdev_to_dev(sci_dev
), "%s: in wrong state: %d\n",
291 /* Return the frame back to the controller */
292 scic_sds_controller_release_frame(scic
, frame_index
);
293 return SCI_FAILURE_INVALID_STATE
;
294 case SCI_BASE_REMOTE_DEVICE_STATE_READY
:
295 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR
:
296 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET
:
297 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING
:
298 case SCI_BASE_REMOTE_DEVICE_STATE_FAILED
:
299 case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING
: {
300 struct scic_sds_request
*sci_req
;
301 struct ssp_frame_hdr hdr
;
305 status
= scic_sds_unsolicited_frame_control_get_header(&scic
->uf_control
,
308 if (status
!= SCI_SUCCESS
)
311 word_cnt
= sizeof(hdr
) / sizeof(u32
);
312 sci_swab32_cpy(&hdr
, frame_header
, word_cnt
);
314 sci_req
= scic_request_by_tag(scic
, be16_to_cpu(hdr
.tag
));
315 if (sci_req
&& sci_req
->target_device
== sci_dev
) {
316 /* The IO request is now in charge of releasing the frame */
317 status
= sci_req
->state_handlers
->frame_handler(sci_req
,
320 /* We could not map this tag to a valid IO
321 * request Just toss the frame and continue
323 scic_sds_controller_release_frame(scic
, frame_index
);
327 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ
: {
328 struct dev_to_host_fis
*hdr
;
330 status
= scic_sds_unsolicited_frame_control_get_header(&scic
->uf_control
,
333 if (status
!= SCI_SUCCESS
)
336 if (hdr
->fis_type
== FIS_SETDEVBITS
&&
337 (hdr
->status
& ATA_ERR
)) {
338 sci_dev
->not_ready_reason
= SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED
;
340 /* TODO Check sactive and complete associated IO if any. */
341 sci_base_state_machine_change_state(sm
, SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR
);
342 } else if (hdr
->fis_type
== FIS_REGD2H
&&
343 (hdr
->status
& ATA_ERR
)) {
345 * Some devices return D2H FIS when an NCQ error is detected.
346 * Treat this like an SDB error FIS ready reason.
348 sci_dev
->not_ready_reason
= SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED
;
349 sci_base_state_machine_change_state(&sci_dev
->state_machine
,
350 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR
);
352 status
= SCI_FAILURE
;
354 scic_sds_controller_release_frame(scic
, frame_index
);
357 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD
:
358 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD
:
359 /* The device does not process any UF received from the hardware while
360 * in this state. All unsolicited frames are forwarded to the io request
363 status
= scic_sds_io_request_frame_handler(sci_dev
->working_request
, frame_index
);
370 static bool is_remote_device_ready(struct scic_sds_remote_device
*sci_dev
)
373 struct sci_base_state_machine
*sm
= &sci_dev
->state_machine
;
374 enum scic_sds_remote_device_states state
= sm
->current_state_id
;
377 case SCI_BASE_REMOTE_DEVICE_STATE_READY
:
378 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE
:
379 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD
:
380 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ
:
381 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR
:
382 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET
:
383 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE
:
384 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD
:
391 enum sci_status
scic_sds_remote_device_event_handler(struct scic_sds_remote_device
*sci_dev
,
394 struct sci_base_state_machine
*sm
= &sci_dev
->state_machine
;
395 enum scic_sds_remote_device_states state
= sm
->current_state_id
;
396 enum sci_status status
;
398 switch (scu_get_event_type(event_code
)) {
399 case SCU_EVENT_TYPE_RNC_OPS_MISC
:
400 case SCU_EVENT_TYPE_RNC_SUSPEND_TX
:
401 case SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX
:
402 status
= scic_sds_remote_node_context_event_handler(&sci_dev
->rnc
, event_code
);
404 case SCU_EVENT_TYPE_PTX_SCHEDULE_EVENT
:
405 if (scu_get_event_code(event_code
) == SCU_EVENT_IT_NEXUS_TIMEOUT
) {
406 status
= SCI_SUCCESS
;
408 /* Suspend the associated RNC */
409 scic_sds_remote_node_context_suspend(&sci_dev
->rnc
,
410 SCI_SOFTWARE_SUSPENSION
,
413 dev_dbg(scirdev_to_dev(sci_dev
),
414 "%s: device: %p event code: %x: %s\n",
415 __func__
, sci_dev
, event_code
,
416 is_remote_device_ready(sci_dev
)
417 ? "I_T_Nexus_Timeout event"
418 : "I_T_Nexus_Timeout event in wrong state");
422 /* Else, fall through and treat as unhandled... */
424 dev_dbg(scirdev_to_dev(sci_dev
),
425 "%s: device: %p event code: %x: %s\n",
426 __func__
, sci_dev
, event_code
,
427 is_remote_device_ready(sci_dev
)
429 : "unexpected event in wrong state");
430 status
= SCI_FAILURE_INVALID_STATE
;
434 if (status
!= SCI_SUCCESS
)
437 if (state
== SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE
) {
439 /* We pick up suspension events to handle specifically to this
440 * state. We resume the RNC right away.
442 if (scu_get_event_type(event_code
) == SCU_EVENT_TYPE_RNC_SUSPEND_TX
||
443 scu_get_event_type(event_code
) == SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX
)
444 status
= scic_sds_remote_node_context_resume(&sci_dev
->rnc
, NULL
, NULL
);
450 static void scic_sds_remote_device_start_request(struct scic_sds_remote_device
*sci_dev
,
451 struct scic_sds_request
*sci_req
,
452 enum sci_status status
)
454 struct scic_sds_port
*sci_port
= sci_dev
->owning_port
;
456 /* cleanup requests that failed after starting on the port */
457 if (status
!= SCI_SUCCESS
)
458 scic_sds_port_complete_io(sci_port
, sci_dev
, sci_req
);
460 scic_sds_remote_device_increment_request_count(sci_dev
);
463 enum sci_status
scic_sds_remote_device_start_io(struct scic_sds_controller
*scic
,
464 struct scic_sds_remote_device
*sci_dev
,
465 struct scic_sds_request
*sci_req
)
467 struct sci_base_state_machine
*sm
= &sci_dev
->state_machine
;
468 enum scic_sds_remote_device_states state
= sm
->current_state_id
;
469 struct scic_sds_port
*sci_port
= sci_dev
->owning_port
;
470 struct isci_request
*ireq
= sci_req_to_ireq(sci_req
);
471 enum sci_status status
;
474 case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL
:
475 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED
:
476 case SCI_BASE_REMOTE_DEVICE_STATE_STARTING
:
477 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR
:
478 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING
:
479 case SCI_BASE_REMOTE_DEVICE_STATE_FAILED
:
480 case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING
:
481 case SCI_BASE_REMOTE_DEVICE_STATE_FINAL
:
483 dev_warn(scirdev_to_dev(sci_dev
), "%s: in wrong state: %d\n",
485 return SCI_FAILURE_INVALID_STATE
;
486 case SCI_BASE_REMOTE_DEVICE_STATE_READY
:
487 /* attempt to start an io request for this device object. The remote
488 * device object will issue the start request for the io and if
489 * successful it will start the request for the port object then
490 * increment its own request count.
492 status
= scic_sds_port_start_io(sci_port
, sci_dev
, sci_req
);
493 if (status
!= SCI_SUCCESS
)
496 status
= scic_sds_remote_node_context_start_io(&sci_dev
->rnc
, sci_req
);
497 if (status
!= SCI_SUCCESS
)
500 status
= scic_sds_request_start(sci_req
);
502 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE
: {
503 /* handle the start io operation for a sata device that is in
504 * the command idle state. - Evalute the type of IO request to
505 * be started - If its an NCQ request change to NCQ substate -
506 * If its any other command change to the CMD substate
508 * If this is a softreset we may want to have a different
511 enum scic_sds_remote_device_states new_state
;
512 struct sas_task
*task
= isci_request_access_task(ireq
);
514 status
= scic_sds_port_start_io(sci_port
, sci_dev
, sci_req
);
515 if (status
!= SCI_SUCCESS
)
518 status
= scic_sds_remote_node_context_start_io(&sci_dev
->rnc
, sci_req
);
519 if (status
!= SCI_SUCCESS
)
522 status
= sci_req
->state_handlers
->start_handler(sci_req
);
523 if (status
!= SCI_SUCCESS
)
526 if (task
->ata_task
.use_ncq
)
527 new_state
= SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ
;
529 sci_dev
->working_request
= sci_req
;
530 new_state
= SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD
;
532 sci_base_state_machine_change_state(sm
, new_state
);
535 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ
: {
536 struct sas_task
*task
= isci_request_access_task(ireq
);
538 if (task
->ata_task
.use_ncq
) {
539 status
= scic_sds_port_start_io(sci_port
, sci_dev
, sci_req
);
540 if (status
!= SCI_SUCCESS
)
543 status
= scic_sds_remote_node_context_start_io(&sci_dev
->rnc
, sci_req
);
544 if (status
!= SCI_SUCCESS
)
547 status
= sci_req
->state_handlers
->start_handler(sci_req
);
549 return SCI_FAILURE_INVALID_STATE
;
552 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET
:
553 return SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED
;
554 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE
:
555 status
= scic_sds_port_start_io(sci_port
, sci_dev
, sci_req
);
556 if (status
!= SCI_SUCCESS
)
559 status
= scic_sds_remote_node_context_start_io(&sci_dev
->rnc
, sci_req
);
560 if (status
!= SCI_SUCCESS
)
563 status
= scic_sds_request_start(sci_req
);
564 if (status
!= SCI_SUCCESS
)
567 sci_dev
->working_request
= sci_req
;
568 sci_base_state_machine_change_state(&sci_dev
->state_machine
,
569 SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD
);
571 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD
:
572 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD
:
573 /* device is already handling a command it can not accept new commands
574 * until this one is complete.
576 return SCI_FAILURE_INVALID_STATE
;
579 scic_sds_remote_device_start_request(sci_dev
, sci_req
, status
);
583 static enum sci_status
common_complete_io(struct scic_sds_port
*sci_port
,
584 struct scic_sds_remote_device
*sci_dev
,
585 struct scic_sds_request
*sci_req
)
587 enum sci_status status
;
589 status
= scic_sds_request_complete(sci_req
);
590 if (status
!= SCI_SUCCESS
)
593 status
= scic_sds_port_complete_io(sci_port
, sci_dev
, sci_req
);
594 if (status
!= SCI_SUCCESS
)
597 scic_sds_remote_device_decrement_request_count(sci_dev
);
601 enum sci_status
scic_sds_remote_device_complete_io(struct scic_sds_controller
*scic
,
602 struct scic_sds_remote_device
*sci_dev
,
603 struct scic_sds_request
*sci_req
)
605 struct sci_base_state_machine
*sm
= &sci_dev
->state_machine
;
606 enum scic_sds_remote_device_states state
= sm
->current_state_id
;
607 struct scic_sds_port
*sci_port
= sci_dev
->owning_port
;
608 enum sci_status status
;
611 case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL
:
612 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED
:
613 case SCI_BASE_REMOTE_DEVICE_STATE_STARTING
:
614 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE
:
615 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE
:
616 case SCI_BASE_REMOTE_DEVICE_STATE_FAILED
:
617 case SCI_BASE_REMOTE_DEVICE_STATE_FINAL
:
619 dev_warn(scirdev_to_dev(sci_dev
), "%s: in wrong state: %d\n",
621 return SCI_FAILURE_INVALID_STATE
;
622 case SCI_BASE_REMOTE_DEVICE_STATE_READY
:
623 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET
:
624 case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING
:
625 status
= common_complete_io(sci_port
, sci_dev
, sci_req
);
627 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD
:
628 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ
:
629 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR
:
630 status
= common_complete_io(sci_port
, sci_dev
, sci_req
);
631 if (status
!= SCI_SUCCESS
)
634 if (sci_req
->sci_status
== SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED
) {
635 /* This request causes hardware error, device needs to be Lun Reset.
636 * So here we force the state machine to IDLE state so the rest IOs
637 * can reach RNC state handler, these IOs will be completed by RNC with
638 * status of "DEVICE_RESET_REQUIRED", instead of "INVALID STATE".
640 sci_base_state_machine_change_state(sm
, SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET
);
641 } else if (scic_sds_remote_device_get_request_count(sci_dev
) == 0)
642 sci_base_state_machine_change_state(sm
, SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE
);
644 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD
:
645 status
= common_complete_io(sci_port
, sci_dev
, sci_req
);
646 if (status
!= SCI_SUCCESS
)
648 sci_base_state_machine_change_state(sm
, SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE
);
650 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING
:
651 status
= common_complete_io(sci_port
, sci_dev
, sci_req
);
652 if (status
!= SCI_SUCCESS
)
655 if (scic_sds_remote_device_get_request_count(sci_dev
) == 0)
656 scic_sds_remote_node_context_destruct(&sci_dev
->rnc
,
662 if (status
!= SCI_SUCCESS
)
663 dev_err(scirdev_to_dev(sci_dev
),
664 "%s: Port:0x%p Device:0x%p Request:0x%p Status:0x%x "
665 "could not complete\n", __func__
, sci_port
,
666 sci_dev
, sci_req
, status
);
671 static void scic_sds_remote_device_continue_request(void *dev
)
673 struct scic_sds_remote_device
*sci_dev
= dev
;
675 /* we need to check if this request is still valid to continue. */
676 if (sci_dev
->working_request
)
677 scic_controller_continue_io(sci_dev
->working_request
);
680 enum sci_status
scic_sds_remote_device_start_task(struct scic_sds_controller
*scic
,
681 struct scic_sds_remote_device
*sci_dev
,
682 struct scic_sds_request
*sci_req
)
684 struct sci_base_state_machine
*sm
= &sci_dev
->state_machine
;
685 enum scic_sds_remote_device_states state
= sm
->current_state_id
;
686 struct scic_sds_port
*sci_port
= sci_dev
->owning_port
;
687 enum sci_status status
;
690 case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL
:
691 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED
:
692 case SCI_BASE_REMOTE_DEVICE_STATE_STARTING
:
693 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE
:
694 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD
:
695 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING
:
696 case SCI_BASE_REMOTE_DEVICE_STATE_FAILED
:
697 case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING
:
698 case SCI_BASE_REMOTE_DEVICE_STATE_FINAL
:
700 dev_warn(scirdev_to_dev(sci_dev
), "%s: in wrong state: %d\n",
702 return SCI_FAILURE_INVALID_STATE
;
703 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE
:
704 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD
:
705 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ
:
706 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR
:
707 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET
:
708 status
= scic_sds_port_start_io(sci_port
, sci_dev
, sci_req
);
709 if (status
!= SCI_SUCCESS
)
712 status
= scic_sds_remote_node_context_start_task(&sci_dev
->rnc
, sci_req
);
713 if (status
!= SCI_SUCCESS
)
716 status
= sci_req
->state_handlers
->start_handler(sci_req
);
717 if (status
!= SCI_SUCCESS
)
720 /* Note: If the remote device state is not IDLE this will
721 * replace the request that probably resulted in the task
722 * management request.
724 sci_dev
->working_request
= sci_req
;
725 sci_base_state_machine_change_state(sm
, SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD
);
727 /* The remote node context must cleanup the TCi to NCQ mapping
728 * table. The only way to do this correctly is to either write
729 * to the TLCR register or to invalidate and repost the RNC. In
730 * either case the remote node context state machine will take
731 * the correct action when the remote node context is suspended
734 scic_sds_remote_node_context_suspend(&sci_dev
->rnc
,
735 SCI_SOFTWARE_SUSPENSION
, NULL
, NULL
);
736 scic_sds_remote_node_context_resume(&sci_dev
->rnc
,
737 scic_sds_remote_device_continue_request
,
741 scic_sds_remote_device_start_request(sci_dev
, sci_req
, status
);
742 /* We need to let the controller start request handler know that
743 * it can't post TC yet. We will provide a callback function to
744 * post TC when RNC gets resumed.
746 return SCI_FAILURE_RESET_DEVICE_PARTIAL_SUCCESS
;
747 case SCI_BASE_REMOTE_DEVICE_STATE_READY
:
748 status
= scic_sds_port_start_io(sci_port
, sci_dev
, sci_req
);
749 if (status
!= SCI_SUCCESS
)
752 status
= scic_sds_remote_node_context_start_task(&sci_dev
->rnc
, sci_req
);
753 if (status
!= SCI_SUCCESS
)
756 status
= scic_sds_request_start(sci_req
);
759 scic_sds_remote_device_start_request(sci_dev
, sci_req
, status
);
769 * This method takes the request and bulids an appropriate SCU context for the
770 * request and then requests the controller to post the request. none
772 void scic_sds_remote_device_post_request(
773 struct scic_sds_remote_device
*sci_dev
,
778 context
= scic_sds_remote_device_build_command_context(sci_dev
, request
);
780 scic_sds_controller_post_request(
781 scic_sds_remote_device_get_controller(sci_dev
),
786 /* called once the remote node context has transisitioned to a
787 * ready state. This is the indication that the remote device object can also
788 * transition to ready.
790 static void remote_device_resume_done(void *_dev
)
792 struct scic_sds_remote_device
*sci_dev
= _dev
;
794 if (is_remote_device_ready(sci_dev
))
797 /* go 'ready' if we are not already in a ready state */
798 sci_base_state_machine_change_state(&sci_dev
->state_machine
,
799 SCI_BASE_REMOTE_DEVICE_STATE_READY
);
802 static void scic_sds_stp_remote_device_ready_idle_substate_resume_complete_handler(void *_dev
)
804 struct scic_sds_remote_device
*sci_dev
= _dev
;
805 struct isci_remote_device
*idev
= sci_dev_to_idev(sci_dev
);
806 struct scic_sds_controller
*scic
= sci_dev
->owning_port
->owning_controller
;
808 /* For NCQ operation we do not issue a isci_remote_device_not_ready().
809 * As a result, avoid sending the ready notification.
811 if (sci_dev
->state_machine
.previous_state_id
!= SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ
)
812 isci_remote_device_ready(scic_to_ihost(scic
), idev
);
815 static void scic_sds_remote_device_initial_state_enter(void *object
)
817 struct scic_sds_remote_device
*sci_dev
= object
;
819 /* Initial state is a transitional state to the stopped state */
820 sci_base_state_machine_change_state(&sci_dev
->state_machine
,
821 SCI_BASE_REMOTE_DEVICE_STATE_STOPPED
);
825 * scic_remote_device_destruct() - free remote node context and destruct
826 * @remote_device: This parameter specifies the remote device to be destructed.
828 * Remote device objects are a limited resource. As such, they must be
829 * protected. Thus calls to construct and destruct are mutually exclusive and
830 * non-reentrant. The return value shall indicate if the device was
831 * successfully destructed or if some failure occurred. enum sci_status This value
832 * is returned if the device is successfully destructed.
833 * SCI_FAILURE_INVALID_REMOTE_DEVICE This value is returned if the supplied
834 * device isn't valid (e.g. it's already been destoryed, the handle isn't
837 static enum sci_status
scic_remote_device_destruct(struct scic_sds_remote_device
*sci_dev
)
839 struct sci_base_state_machine
*sm
= &sci_dev
->state_machine
;
840 enum scic_sds_remote_device_states state
= sm
->current_state_id
;
841 struct scic_sds_controller
*scic
;
843 if (state
!= SCI_BASE_REMOTE_DEVICE_STATE_STOPPED
) {
844 dev_warn(scirdev_to_dev(sci_dev
), "%s: in wrong state: %d\n",
846 return SCI_FAILURE_INVALID_STATE
;
849 scic
= sci_dev
->owning_port
->owning_controller
;
850 scic_sds_controller_free_remote_node_context(scic
, sci_dev
,
851 sci_dev
->rnc
.remote_node_index
);
852 sci_dev
->rnc
.remote_node_index
= SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX
;
853 sci_base_state_machine_change_state(sm
, SCI_BASE_REMOTE_DEVICE_STATE_FINAL
);
859 * isci_remote_device_deconstruct() - This function frees an isci_remote_device.
860 * @ihost: This parameter specifies the isci host object.
861 * @idev: This parameter specifies the remote device to be freed.
864 static void isci_remote_device_deconstruct(struct isci_host
*ihost
, struct isci_remote_device
*idev
)
866 dev_dbg(&ihost
->pdev
->dev
,
867 "%s: isci_device = %p\n", __func__
, idev
);
869 /* There should not be any outstanding io's. All paths to
870 * here should go through isci_remote_device_nuke_requests.
871 * If we hit this condition, we will need a way to complete
872 * io requests in process */
873 while (!list_empty(&idev
->reqs_in_process
)) {
875 dev_err(&ihost
->pdev
->dev
,
876 "%s: ** request list not empty! **\n", __func__
);
880 scic_remote_device_destruct(&idev
->sci
);
881 idev
->domain_dev
->lldd_dev
= NULL
;
882 idev
->domain_dev
= NULL
;
883 idev
->isci_port
= NULL
;
884 list_del_init(&idev
->node
);
886 clear_bit(IDEV_START_PENDING
, &idev
->flags
);
887 clear_bit(IDEV_STOP_PENDING
, &idev
->flags
);
888 clear_bit(IDEV_EH
, &idev
->flags
);
889 wake_up(&ihost
->eventq
);
893 * isci_remote_device_stop_complete() - This function is called by the scic
894 * when the remote device stop has completed. We mark the isci device as not
895 * ready and remove the isci remote device.
896 * @ihost: This parameter specifies the isci host object.
897 * @idev: This parameter specifies the remote device.
898 * @status: This parameter specifies status of the completion.
901 static void isci_remote_device_stop_complete(struct isci_host
*ihost
,
902 struct isci_remote_device
*idev
)
904 dev_dbg(&ihost
->pdev
->dev
, "%s: complete idev = %p\n", __func__
, idev
);
906 isci_remote_device_change_state(idev
, isci_stopped
);
908 /* after stop, we can tear down resources. */
909 isci_remote_device_deconstruct(ihost
, idev
);
912 static void scic_sds_remote_device_stopped_state_enter(void *object
)
914 struct scic_sds_remote_device
*sci_dev
= object
;
915 struct scic_sds_controller
*scic
= sci_dev
->owning_port
->owning_controller
;
916 struct isci_remote_device
*idev
= sci_dev_to_idev(sci_dev
);
919 /* If we are entering from the stopping state let the SCI User know that
920 * the stop operation has completed.
922 prev_state
= sci_dev
->state_machine
.previous_state_id
;
923 if (prev_state
== SCI_BASE_REMOTE_DEVICE_STATE_STOPPING
)
924 isci_remote_device_stop_complete(scic_to_ihost(scic
), idev
);
926 scic_sds_controller_remote_device_stopped(scic
, sci_dev
);
929 static void scic_sds_remote_device_starting_state_enter(void *object
)
931 struct scic_sds_remote_device
*sci_dev
= object
;
932 struct scic_sds_controller
*scic
= scic_sds_remote_device_get_controller(sci_dev
);
933 struct isci_host
*ihost
= scic_to_ihost(scic
);
934 struct isci_remote_device
*idev
= sci_dev_to_idev(sci_dev
);
936 isci_remote_device_not_ready(ihost
, idev
,
937 SCIC_REMOTE_DEVICE_NOT_READY_START_REQUESTED
);
940 static void scic_sds_remote_device_ready_state_enter(void *object
)
942 struct scic_sds_remote_device
*sci_dev
= object
;
943 struct scic_sds_controller
*scic
= sci_dev
->owning_port
->owning_controller
;
944 struct isci_remote_device
*idev
= sci_dev_to_idev(sci_dev
);
945 struct domain_device
*dev
= idev
->domain_dev
;
947 scic
->remote_device_sequence
[sci_dev
->rnc
.remote_node_index
]++;
949 if (dev
->dev_type
== SATA_DEV
|| (dev
->tproto
& SAS_PROTOCOL_SATA
)) {
950 sci_base_state_machine_change_state(&sci_dev
->state_machine
,
951 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE
);
952 } else if (dev_is_expander(dev
)) {
953 sci_base_state_machine_change_state(&sci_dev
->state_machine
,
954 SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE
);
956 isci_remote_device_ready(scic_to_ihost(scic
), idev
);
959 static void scic_sds_remote_device_ready_state_exit(void *object
)
961 struct scic_sds_remote_device
*sci_dev
= object
;
962 struct domain_device
*dev
= sci_dev_to_domain(sci_dev
);
964 if (dev
->dev_type
== SAS_END_DEV
) {
965 struct scic_sds_controller
*scic
= sci_dev
->owning_port
->owning_controller
;
966 struct isci_remote_device
*idev
= sci_dev_to_idev(sci_dev
);
968 isci_remote_device_not_ready(scic_to_ihost(scic
), idev
,
969 SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED
);
973 static void scic_sds_remote_device_resetting_state_enter(void *object
)
975 struct scic_sds_remote_device
*sci_dev
= object
;
977 scic_sds_remote_node_context_suspend(
978 &sci_dev
->rnc
, SCI_SOFTWARE_SUSPENSION
, NULL
, NULL
);
981 static void scic_sds_remote_device_resetting_state_exit(void *object
)
983 struct scic_sds_remote_device
*sci_dev
= object
;
985 scic_sds_remote_node_context_resume(&sci_dev
->rnc
, NULL
, NULL
);
988 static void scic_sds_stp_remote_device_ready_idle_substate_enter(void *object
)
990 struct scic_sds_remote_device
*sci_dev
= object
;
992 sci_dev
->working_request
= NULL
;
993 if (scic_sds_remote_node_context_is_ready(&sci_dev
->rnc
)) {
995 * Since the RNC is ready, it's alright to finish completion
996 * processing (e.g. signal the remote device is ready). */
997 scic_sds_stp_remote_device_ready_idle_substate_resume_complete_handler(sci_dev
);
999 scic_sds_remote_node_context_resume(&sci_dev
->rnc
,
1000 scic_sds_stp_remote_device_ready_idle_substate_resume_complete_handler
,
1005 static void scic_sds_stp_remote_device_ready_cmd_substate_enter(void *object
)
1007 struct scic_sds_remote_device
*sci_dev
= object
;
1008 struct scic_sds_controller
*scic
= scic_sds_remote_device_get_controller(sci_dev
);
1010 BUG_ON(sci_dev
->working_request
== NULL
);
1012 isci_remote_device_not_ready(scic_to_ihost(scic
), sci_dev_to_idev(sci_dev
),
1013 SCIC_REMOTE_DEVICE_NOT_READY_SATA_REQUEST_STARTED
);
1016 static void scic_sds_stp_remote_device_ready_ncq_error_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
);
1020 struct isci_remote_device
*idev
= sci_dev_to_idev(sci_dev
);
1022 if (sci_dev
->not_ready_reason
== SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED
)
1023 isci_remote_device_not_ready(scic_to_ihost(scic
), idev
,
1024 sci_dev
->not_ready_reason
);
1027 static void scic_sds_smp_remote_device_ready_idle_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
);
1032 isci_remote_device_ready(scic_to_ihost(scic
), sci_dev_to_idev(sci_dev
));
1035 static void scic_sds_smp_remote_device_ready_cmd_substate_enter(void *object
)
1037 struct scic_sds_remote_device
*sci_dev
= object
;
1038 struct scic_sds_controller
*scic
= scic_sds_remote_device_get_controller(sci_dev
);
1040 BUG_ON(sci_dev
->working_request
== NULL
);
1042 isci_remote_device_not_ready(scic_to_ihost(scic
), sci_dev_to_idev(sci_dev
),
1043 SCIC_REMOTE_DEVICE_NOT_READY_SMP_REQUEST_STARTED
);
1046 static void scic_sds_smp_remote_device_ready_cmd_substate_exit(void *object
)
1048 struct scic_sds_remote_device
*sci_dev
= object
;
1050 sci_dev
->working_request
= NULL
;
1053 static const struct sci_base_state scic_sds_remote_device_state_table
[] = {
1054 [SCI_BASE_REMOTE_DEVICE_STATE_INITIAL
] = {
1055 .enter_state
= scic_sds_remote_device_initial_state_enter
,
1057 [SCI_BASE_REMOTE_DEVICE_STATE_STOPPED
] = {
1058 .enter_state
= scic_sds_remote_device_stopped_state_enter
,
1060 [SCI_BASE_REMOTE_DEVICE_STATE_STARTING
] = {
1061 .enter_state
= scic_sds_remote_device_starting_state_enter
,
1063 [SCI_BASE_REMOTE_DEVICE_STATE_READY
] = {
1064 .enter_state
= scic_sds_remote_device_ready_state_enter
,
1065 .exit_state
= scic_sds_remote_device_ready_state_exit
1067 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE
] = {
1068 .enter_state
= scic_sds_stp_remote_device_ready_idle_substate_enter
,
1070 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD
] = {
1071 .enter_state
= scic_sds_stp_remote_device_ready_cmd_substate_enter
,
1073 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ
] = { },
1074 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR
] = {
1075 .enter_state
= scic_sds_stp_remote_device_ready_ncq_error_substate_enter
,
1077 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET
] = { },
1078 [SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE
] = {
1079 .enter_state
= scic_sds_smp_remote_device_ready_idle_substate_enter
,
1081 [SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD
] = {
1082 .enter_state
= scic_sds_smp_remote_device_ready_cmd_substate_enter
,
1083 .exit_state
= scic_sds_smp_remote_device_ready_cmd_substate_exit
,
1085 [SCI_BASE_REMOTE_DEVICE_STATE_STOPPING
] = { },
1086 [SCI_BASE_REMOTE_DEVICE_STATE_FAILED
] = { },
1087 [SCI_BASE_REMOTE_DEVICE_STATE_RESETTING
] = {
1088 .enter_state
= scic_sds_remote_device_resetting_state_enter
,
1089 .exit_state
= scic_sds_remote_device_resetting_state_exit
1091 [SCI_BASE_REMOTE_DEVICE_STATE_FINAL
] = { },
1095 * scic_remote_device_construct() - common construction
1096 * @sci_port: SAS/SATA port through which this device is accessed.
1097 * @sci_dev: remote device to construct
1099 * This routine just performs benign initialization and does not
1100 * allocate the remote_node_context which is left to
1101 * scic_remote_device_[de]a_construct(). scic_remote_device_destruct()
1102 * frees the remote_node_context(s) for the device.
1104 static void scic_remote_device_construct(struct scic_sds_port
*sci_port
,
1105 struct scic_sds_remote_device
*sci_dev
)
1107 sci_dev
->owning_port
= sci_port
;
1108 sci_dev
->started_request_count
= 0;
1110 sci_base_state_machine_construct(
1111 &sci_dev
->state_machine
,
1113 scic_sds_remote_device_state_table
,
1114 SCI_BASE_REMOTE_DEVICE_STATE_INITIAL
1117 sci_base_state_machine_start(
1118 &sci_dev
->state_machine
1121 scic_sds_remote_node_context_construct(&sci_dev
->rnc
,
1122 SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX
);
1126 * scic_remote_device_da_construct() - construct direct attached device.
1128 * The information (e.g. IAF, Signature FIS, etc.) necessary to build
1129 * the device is known to the SCI Core since it is contained in the
1130 * scic_phy object. Remote node context(s) is/are a global resource
1131 * allocated by this routine, freed by scic_remote_device_destruct().
1134 * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
1135 * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
1136 * sata-only controller instance.
1137 * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
1139 static enum sci_status
scic_remote_device_da_construct(struct scic_sds_port
*sci_port
,
1140 struct scic_sds_remote_device
*sci_dev
)
1142 enum sci_status status
;
1143 struct domain_device
*dev
= sci_dev_to_domain(sci_dev
);
1145 scic_remote_device_construct(sci_port
, sci_dev
);
1148 * This information is request to determine how many remote node context
1149 * entries will be needed to store the remote node.
1151 sci_dev
->is_direct_attached
= true;
1152 status
= scic_sds_controller_allocate_remote_node_context(sci_port
->owning_controller
,
1154 &sci_dev
->rnc
.remote_node_index
);
1156 if (status
!= SCI_SUCCESS
)
1159 if (dev
->dev_type
== SAS_END_DEV
|| dev
->dev_type
== SATA_DEV
||
1160 (dev
->tproto
& SAS_PROTOCOL_STP
) || dev_is_expander(dev
))
1163 return SCI_FAILURE_UNSUPPORTED_PROTOCOL
;
1165 sci_dev
->connection_rate
= scic_sds_port_get_max_allowed_speed(sci_port
);
1167 /* / @todo Should I assign the port width by reading all of the phys on the port? */
1168 sci_dev
->device_port_width
= 1;
1174 * scic_remote_device_ea_construct() - construct expander attached device
1176 * Remote node context(s) is/are a global resource allocated by this
1177 * routine, freed by scic_remote_device_destruct().
1180 * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
1181 * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
1182 * sata-only controller instance.
1183 * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
1185 static enum sci_status
scic_remote_device_ea_construct(struct scic_sds_port
*sci_port
,
1186 struct scic_sds_remote_device
*sci_dev
)
1188 struct domain_device
*dev
= sci_dev_to_domain(sci_dev
);
1189 enum sci_status status
;
1191 scic_remote_device_construct(sci_port
, sci_dev
);
1193 status
= scic_sds_controller_allocate_remote_node_context(sci_port
->owning_controller
,
1195 &sci_dev
->rnc
.remote_node_index
);
1196 if (status
!= SCI_SUCCESS
)
1199 if (dev
->dev_type
== SAS_END_DEV
|| dev
->dev_type
== SATA_DEV
||
1200 (dev
->tproto
& SAS_PROTOCOL_STP
) || dev_is_expander(dev
))
1203 return SCI_FAILURE_UNSUPPORTED_PROTOCOL
;
1206 * For SAS-2 the physical link rate is actually a logical link
1207 * rate that incorporates multiplexing. The SCU doesn't
1208 * incorporate multiplexing and for the purposes of the
1209 * connection the logical link rate is that same as the
1210 * physical. Furthermore, the SAS-2 and SAS-1.1 fields overlay
1211 * one another, so this code works for both situations. */
1212 sci_dev
->connection_rate
= min_t(u16
, scic_sds_port_get_max_allowed_speed(sci_port
),
1215 /* / @todo Should I assign the port width by reading all of the phys on the port? */
1216 sci_dev
->device_port_width
= 1;
1222 * scic_remote_device_start() - This method will start the supplied remote
1223 * device. This method enables normal IO requests to flow through to the
1225 * @remote_device: This parameter specifies the device to be started.
1226 * @timeout: This parameter specifies the number of milliseconds in which the
1227 * start operation should complete.
1229 * An indication of whether the device was successfully started. SCI_SUCCESS
1230 * This value is returned if the device was successfully started.
1231 * SCI_FAILURE_INVALID_PHY This value is returned if the user attempts to start
1232 * the device when there have been no phys added to it.
1234 static enum sci_status
scic_remote_device_start(struct scic_sds_remote_device
*sci_dev
,
1237 struct sci_base_state_machine
*sm
= &sci_dev
->state_machine
;
1238 enum scic_sds_remote_device_states state
= sm
->current_state_id
;
1239 enum sci_status status
;
1241 if (state
!= SCI_BASE_REMOTE_DEVICE_STATE_STOPPED
) {
1242 dev_warn(scirdev_to_dev(sci_dev
), "%s: in wrong state: %d\n",
1244 return SCI_FAILURE_INVALID_STATE
;
1247 status
= scic_sds_remote_node_context_resume(&sci_dev
->rnc
,
1248 remote_device_resume_done
,
1250 if (status
!= SCI_SUCCESS
)
1253 sci_base_state_machine_change_state(sm
, SCI_BASE_REMOTE_DEVICE_STATE_STARTING
);
1258 static enum sci_status
isci_remote_device_construct(struct isci_port
*iport
,
1259 struct isci_remote_device
*idev
)
1261 struct scic_sds_port
*sci_port
= &iport
->sci
;
1262 struct isci_host
*ihost
= iport
->isci_host
;
1263 struct domain_device
*dev
= idev
->domain_dev
;
1264 enum sci_status status
;
1266 if (dev
->parent
&& dev_is_expander(dev
->parent
))
1267 status
= scic_remote_device_ea_construct(sci_port
, &idev
->sci
);
1269 status
= scic_remote_device_da_construct(sci_port
, &idev
->sci
);
1271 if (status
!= SCI_SUCCESS
) {
1272 dev_dbg(&ihost
->pdev
->dev
, "%s: construct failed: %d\n",
1278 /* start the device. */
1279 status
= scic_remote_device_start(&idev
->sci
, ISCI_REMOTE_DEVICE_START_TIMEOUT
);
1281 if (status
!= SCI_SUCCESS
)
1282 dev_warn(&ihost
->pdev
->dev
, "remote device start failed: %d\n",
1288 void isci_remote_device_nuke_requests(struct isci_host
*ihost
, struct isci_remote_device
*idev
)
1290 DECLARE_COMPLETION_ONSTACK(aborted_task_completion
);
1292 dev_dbg(&ihost
->pdev
->dev
,
1293 "%s: idev = %p\n", __func__
, idev
);
1295 /* Cleanup all requests pending for this device. */
1296 isci_terminate_pending_requests(ihost
, idev
, terminating
);
1298 dev_dbg(&ihost
->pdev
->dev
,
1299 "%s: idev = %p, done\n", __func__
, idev
);
1303 * This function builds the isci_remote_device when a libsas dev_found message
1305 * @isci_host: This parameter specifies the isci host object.
1306 * @port: This parameter specifies the isci_port conected to this device.
1308 * pointer to new isci_remote_device.
1310 static struct isci_remote_device
*
1311 isci_remote_device_alloc(struct isci_host
*ihost
, struct isci_port
*iport
)
1313 struct isci_remote_device
*idev
;
1316 for (i
= 0; i
< SCI_MAX_REMOTE_DEVICES
; i
++) {
1317 idev
= &ihost
->devices
[i
];
1318 if (!test_and_set_bit(IDEV_ALLOCATED
, &idev
->flags
))
1322 if (i
>= SCI_MAX_REMOTE_DEVICES
) {
1323 dev_warn(&ihost
->pdev
->dev
, "%s: failed\n", __func__
);
1327 if (WARN_ONCE(!list_empty(&idev
->reqs_in_process
), "found requests in process\n"))
1330 if (WARN_ONCE(!list_empty(&idev
->node
), "found non-idle remote device\n"))
1333 isci_remote_device_change_state(idev
, isci_freed
);
1339 * isci_remote_device_stop() - This function is called internally to stop the
1341 * @isci_host: This parameter specifies the isci host object.
1342 * @isci_device: This parameter specifies the remote device.
1344 * The status of the scic request to stop.
1346 enum sci_status
isci_remote_device_stop(struct isci_host
*ihost
, struct isci_remote_device
*idev
)
1348 enum sci_status status
;
1349 unsigned long flags
;
1351 dev_dbg(&ihost
->pdev
->dev
,
1352 "%s: isci_device = %p\n", __func__
, idev
);
1354 isci_remote_device_change_state(idev
, isci_stopping
);
1356 /* Kill all outstanding requests. */
1357 isci_remote_device_nuke_requests(ihost
, idev
);
1359 set_bit(IDEV_STOP_PENDING
, &idev
->flags
);
1361 spin_lock_irqsave(&ihost
->scic_lock
, flags
);
1362 status
= scic_remote_device_stop(&idev
->sci
, 50);
1363 spin_unlock_irqrestore(&ihost
->scic_lock
, flags
);
1365 /* Wait for the stop complete callback. */
1366 if (status
== SCI_SUCCESS
) {
1367 wait_for_device_stop(ihost
, idev
);
1368 clear_bit(IDEV_ALLOCATED
, &idev
->flags
);
1371 dev_dbg(&ihost
->pdev
->dev
,
1372 "%s: idev = %p - after completion wait\n",
1379 * isci_remote_device_gone() - This function is called by libsas when a domain
1380 * device is removed.
1381 * @domain_device: This parameter specifies the libsas domain device.
1384 void isci_remote_device_gone(struct domain_device
*dev
)
1386 struct isci_host
*ihost
= dev_to_ihost(dev
);
1387 struct isci_remote_device
*idev
= dev
->lldd_dev
;
1389 dev_dbg(&ihost
->pdev
->dev
,
1390 "%s: domain_device = %p, isci_device = %p, isci_port = %p\n",
1391 __func__
, dev
, idev
, idev
->isci_port
);
1393 isci_remote_device_stop(ihost
, idev
);
1398 * isci_remote_device_found() - This function is called by libsas when a remote
1399 * device is discovered. A remote device object is created and started. the
1400 * function then sleeps until the sci core device started message is
1402 * @domain_device: This parameter specifies the libsas domain device.
1404 * status, zero indicates success.
1406 int isci_remote_device_found(struct domain_device
*domain_dev
)
1408 struct isci_host
*isci_host
= dev_to_ihost(domain_dev
);
1409 struct isci_port
*isci_port
;
1410 struct isci_phy
*isci_phy
;
1411 struct asd_sas_port
*sas_port
;
1412 struct asd_sas_phy
*sas_phy
;
1413 struct isci_remote_device
*isci_device
;
1414 enum sci_status status
;
1416 dev_dbg(&isci_host
->pdev
->dev
,
1417 "%s: domain_device = %p\n", __func__
, domain_dev
);
1419 wait_for_start(isci_host
);
1421 sas_port
= domain_dev
->port
;
1422 sas_phy
= list_first_entry(&sas_port
->phy_list
, struct asd_sas_phy
,
1424 isci_phy
= to_isci_phy(sas_phy
);
1425 isci_port
= isci_phy
->isci_port
;
1427 /* we are being called for a device on this port,
1428 * so it has to come up eventually
1430 wait_for_completion(&isci_port
->start_complete
);
1432 if ((isci_stopping
== isci_port_get_state(isci_port
)) ||
1433 (isci_stopped
== isci_port_get_state(isci_port
)))
1436 isci_device
= isci_remote_device_alloc(isci_host
, isci_port
);
1440 INIT_LIST_HEAD(&isci_device
->node
);
1441 domain_dev
->lldd_dev
= isci_device
;
1442 isci_device
->domain_dev
= domain_dev
;
1443 isci_device
->isci_port
= isci_port
;
1444 isci_remote_device_change_state(isci_device
, isci_starting
);
1447 spin_lock_irq(&isci_host
->scic_lock
);
1448 list_add_tail(&isci_device
->node
, &isci_port
->remote_dev_list
);
1450 set_bit(IDEV_START_PENDING
, &isci_device
->flags
);
1451 status
= isci_remote_device_construct(isci_port
, isci_device
);
1452 spin_unlock_irq(&isci_host
->scic_lock
);
1454 dev_dbg(&isci_host
->pdev
->dev
,
1455 "%s: isci_device = %p\n",
1456 __func__
, isci_device
);
1458 if (status
!= SCI_SUCCESS
) {
1460 spin_lock_irq(&isci_host
->scic_lock
);
1461 isci_remote_device_deconstruct(
1465 spin_unlock_irq(&isci_host
->scic_lock
);
1469 /* wait for the device ready callback. */
1470 wait_for_device_start(isci_host
, isci_device
);
1475 * isci_device_is_reset_pending() - This function will check if there is any
1476 * pending reset condition on the device.
1477 * @request: This parameter is the isci_device object.
1479 * true if there is a reset pending for the device.
1481 bool isci_device_is_reset_pending(
1482 struct isci_host
*isci_host
,
1483 struct isci_remote_device
*isci_device
)
1485 struct isci_request
*isci_request
;
1486 struct isci_request
*tmp_req
;
1487 bool reset_is_pending
= false;
1488 unsigned long flags
;
1490 dev_dbg(&isci_host
->pdev
->dev
,
1491 "%s: isci_device = %p\n", __func__
, isci_device
);
1493 spin_lock_irqsave(&isci_host
->scic_lock
, flags
);
1495 /* Check for reset on all pending requests. */
1496 list_for_each_entry_safe(isci_request
, tmp_req
,
1497 &isci_device
->reqs_in_process
, dev_node
) {
1498 dev_dbg(&isci_host
->pdev
->dev
,
1499 "%s: isci_device = %p request = %p\n",
1500 __func__
, isci_device
, isci_request
);
1502 if (isci_request
->ttype
== io_task
) {
1503 struct sas_task
*task
= isci_request_access_task(
1506 spin_lock(&task
->task_state_lock
);
1507 if (task
->task_state_flags
& SAS_TASK_NEED_DEV_RESET
)
1508 reset_is_pending
= true;
1509 spin_unlock(&task
->task_state_lock
);
1513 spin_unlock_irqrestore(&isci_host
->scic_lock
, flags
);
1515 dev_dbg(&isci_host
->pdev
->dev
,
1516 "%s: isci_device = %p reset_is_pending = %d\n",
1517 __func__
, isci_device
, reset_is_pending
);
1519 return reset_is_pending
;
1523 * isci_device_clear_reset_pending() - This function will clear if any pending
1524 * reset condition flags on the device.
1525 * @request: This parameter is the isci_device object.
1527 * true if there is a reset pending for the device.
1529 void isci_device_clear_reset_pending(struct isci_host
*ihost
, struct isci_remote_device
*idev
)
1531 struct isci_request
*isci_request
;
1532 struct isci_request
*tmp_req
;
1533 unsigned long flags
= 0;
1535 dev_dbg(&ihost
->pdev
->dev
, "%s: idev=%p, ihost=%p\n",
1536 __func__
, idev
, ihost
);
1538 spin_lock_irqsave(&ihost
->scic_lock
, flags
);
1540 /* Clear reset pending on all pending requests. */
1541 list_for_each_entry_safe(isci_request
, tmp_req
,
1542 &idev
->reqs_in_process
, dev_node
) {
1543 dev_dbg(&ihost
->pdev
->dev
, "%s: idev = %p request = %p\n",
1544 __func__
, idev
, isci_request
);
1546 if (isci_request
->ttype
== io_task
) {
1548 unsigned long flags2
;
1549 struct sas_task
*task
= isci_request_access_task(
1552 spin_lock_irqsave(&task
->task_state_lock
, flags2
);
1553 task
->task_state_flags
&= ~SAS_TASK_NEED_DEV_RESET
;
1554 spin_unlock_irqrestore(&task
->task_state_lock
, flags2
);
1557 spin_unlock_irqrestore(&ihost
->scic_lock
, flags
);