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 <linux/bitops.h>
59 #include "remote_device.h"
61 #include "remote_node_context.h"
62 #include "scu_event_codes.h"
67 const char *dev_state_name(enum sci_remote_device_states state)
69 static const char * const strings[] = REMOTE_DEV_STATES;
71 return strings[state];
75 static enum sci_status sci_remote_device_suspend(struct isci_remote_device *idev,
76 enum sci_remote_node_suspension_reasons reason)
78 return sci_remote_node_context_suspend(&idev->rnc,
80 SCI_SOFTWARE_SUSPEND_EXPECTED_EVENT,
85 * isci_remote_device_ready() - This function is called by the ihost when the
86 * remote device is ready. We mark the isci device as ready and signal the
88 * @ihost: our valid isci_host
89 * @idev: remote device
92 static void isci_remote_device_ready(struct isci_host *ihost, struct isci_remote_device *idev)
94 dev_dbg(&ihost->pdev->dev,
95 "%s: idev = %p\n", __func__, idev);
97 clear_bit(IDEV_IO_NCQERROR, &idev->flags);
98 set_bit(IDEV_IO_READY, &idev->flags);
99 if (test_and_clear_bit(IDEV_START_PENDING, &idev->flags))
100 wake_up(&ihost->eventq);
103 static enum sci_status sci_remote_device_terminate_req(
104 struct isci_host *ihost,
105 struct isci_remote_device *idev,
107 struct isci_request *ireq)
109 dev_dbg(&ihost->pdev->dev,
110 "%s: idev=%p; flags=%lx; req=%p; req target=%p\n",
111 __func__, idev, idev->flags, ireq, ireq->target_device);
113 if (!test_bit(IREQ_ACTIVE, &ireq->flags) ||
114 (ireq->target_device != idev) ||
115 (check_abort && !test_bit(IREQ_PENDING_ABORT, &ireq->flags)))
118 set_bit(IREQ_ABORT_PATH_ACTIVE, &ireq->flags);
120 return sci_controller_terminate_request(ihost, idev, ireq);
123 static enum sci_status sci_remote_device_terminate_reqs_checkabort(
124 struct isci_remote_device *idev,
127 struct isci_host *ihost = idev->owning_port->owning_controller;
128 enum sci_status status = SCI_SUCCESS;
131 for (i = 0; i < SCI_MAX_IO_REQUESTS; i++) {
132 struct isci_request *ireq = ihost->reqs[i];
135 s = sci_remote_device_terminate_req(ihost, idev, chk, ireq);
136 if (s != SCI_SUCCESS)
142 enum sci_status isci_remote_device_terminate_requests(
143 struct isci_host *ihost,
144 struct isci_remote_device *idev,
145 struct isci_request *ireq)
147 enum sci_status status = SCI_SUCCESS;
150 spin_lock_irqsave(&ihost->scic_lock, flags);
151 if (isci_get_device(idev) == NULL) {
152 dev_dbg(&ihost->pdev->dev, "%s: failed isci_get_device(idev=%p)\n",
154 spin_unlock_irqrestore(&ihost->scic_lock, flags);
155 status = SCI_FAILURE;
157 dev_dbg(&ihost->pdev->dev,
158 "%s: idev=%p, ireq=%p; started_request_count=%d, "
160 __func__, idev, ireq, idev->started_request_count);
162 /* Terminate a specific TC. */
163 sci_remote_device_terminate_req(ihost, idev, 0, ireq);
164 spin_unlock_irqrestore(&ihost->scic_lock, flags);
165 wait_event(ihost->eventq, !test_bit(IREQ_ACTIVE,
169 /* Terminate all TCs. */
170 sci_remote_device_terminate_requests(idev);
171 spin_unlock_irqrestore(&ihost->scic_lock, flags);
172 wait_event(ihost->eventq,
173 idev->started_request_count == 0);
175 dev_dbg(&ihost->pdev->dev, "%s: idev=%p, wait done\n",
177 isci_put_device(idev);
183 * isci_remote_device_not_ready() - This function is called by the ihost when
184 * the remote device is not ready. We mark the isci device as ready (not
185 * "ready_for_io") and signal the waiting proccess.
186 * @isci_host: This parameter specifies the isci host object.
187 * @isci_device: This parameter specifies the remote device
189 * sci_lock is held on entrance to this function.
191 static void isci_remote_device_not_ready(struct isci_host *ihost,
192 struct isci_remote_device *idev,
195 dev_dbg(&ihost->pdev->dev,
196 "%s: isci_device = %p; reason = %d\n", __func__, idev, reason);
199 case SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED:
200 set_bit(IDEV_IO_NCQERROR, &idev->flags);
202 /* Suspend the remote device so the I/O can be terminated. */
203 sci_remote_device_suspend(idev, SCI_SW_SUSPEND_NORMAL);
205 /* Kill all outstanding requests for the device. */
206 sci_remote_device_terminate_requests(idev);
208 /* Fall through into the default case... */
210 clear_bit(IDEV_IO_READY, &idev->flags);
215 /* called once the remote node context is ready to be freed.
216 * The remote device can now report that its stop operation is complete. none
218 static void rnc_destruct_done(void *_dev)
220 struct isci_remote_device *idev = _dev;
222 BUG_ON(idev->started_request_count != 0);
223 sci_change_state(&idev->sm, SCI_DEV_STOPPED);
226 enum sci_status sci_remote_device_terminate_requests(
227 struct isci_remote_device *idev)
229 return sci_remote_device_terminate_reqs_checkabort(idev, 0);
232 enum sci_status sci_remote_device_stop(struct isci_remote_device *idev,
235 struct sci_base_state_machine *sm = &idev->sm;
236 enum sci_remote_device_states state = sm->current_state_id;
239 case SCI_DEV_INITIAL:
243 dev_warn(scirdev_to_dev(idev), "%s: in wrong state: %s\n",
244 __func__, dev_state_name(state));
245 return SCI_FAILURE_INVALID_STATE;
246 case SCI_DEV_STOPPED:
248 case SCI_DEV_STARTING:
249 /* device not started so there had better be no requests */
250 BUG_ON(idev->started_request_count != 0);
251 sci_remote_node_context_destruct(&idev->rnc,
252 rnc_destruct_done, idev);
253 /* Transition to the stopping state and wait for the
254 * remote node to complete being posted and invalidated.
256 sci_change_state(sm, SCI_DEV_STOPPING);
259 case SCI_STP_DEV_IDLE:
260 case SCI_STP_DEV_CMD:
261 case SCI_STP_DEV_NCQ:
262 case SCI_STP_DEV_NCQ_ERROR:
263 case SCI_STP_DEV_AWAIT_RESET:
264 case SCI_SMP_DEV_IDLE:
265 case SCI_SMP_DEV_CMD:
266 sci_change_state(sm, SCI_DEV_STOPPING);
267 if (idev->started_request_count == 0)
268 sci_remote_node_context_destruct(&idev->rnc,
272 sci_remote_device_suspend(
273 idev, SCI_SW_SUSPEND_LINKHANG_DETECT);
274 sci_remote_device_terminate_requests(idev);
277 case SCI_DEV_STOPPING:
278 /* All requests should have been terminated, but if there is an
279 * attempt to stop a device already in the stopping state, then
280 * try again to terminate.
282 return sci_remote_device_terminate_requests(idev);
283 case SCI_DEV_RESETTING:
284 sci_change_state(sm, SCI_DEV_STOPPING);
289 enum sci_status sci_remote_device_reset(struct isci_remote_device *idev)
291 struct sci_base_state_machine *sm = &idev->sm;
292 enum sci_remote_device_states state = sm->current_state_id;
295 case SCI_DEV_INITIAL:
296 case SCI_DEV_STOPPED:
297 case SCI_DEV_STARTING:
298 case SCI_SMP_DEV_IDLE:
299 case SCI_SMP_DEV_CMD:
300 case SCI_DEV_STOPPING:
302 case SCI_DEV_RESETTING:
305 dev_warn(scirdev_to_dev(idev), "%s: in wrong state: %s\n",
306 __func__, dev_state_name(state));
307 return SCI_FAILURE_INVALID_STATE;
309 case SCI_STP_DEV_IDLE:
310 case SCI_STP_DEV_CMD:
311 case SCI_STP_DEV_NCQ:
312 case SCI_STP_DEV_NCQ_ERROR:
313 case SCI_STP_DEV_AWAIT_RESET:
314 sci_change_state(sm, SCI_DEV_RESETTING);
319 enum sci_status sci_remote_device_reset_complete(struct isci_remote_device *idev)
321 struct sci_base_state_machine *sm = &idev->sm;
322 enum sci_remote_device_states state = sm->current_state_id;
324 if (state != SCI_DEV_RESETTING) {
325 dev_warn(scirdev_to_dev(idev), "%s: in wrong state: %s\n",
326 __func__, dev_state_name(state));
327 return SCI_FAILURE_INVALID_STATE;
330 sci_change_state(sm, SCI_DEV_READY);
334 enum sci_status sci_remote_device_frame_handler(struct isci_remote_device *idev,
337 struct sci_base_state_machine *sm = &idev->sm;
338 enum sci_remote_device_states state = sm->current_state_id;
339 struct isci_host *ihost = idev->owning_port->owning_controller;
340 enum sci_status status;
343 case SCI_DEV_INITIAL:
344 case SCI_DEV_STOPPED:
345 case SCI_DEV_STARTING:
346 case SCI_STP_DEV_IDLE:
347 case SCI_SMP_DEV_IDLE:
350 dev_warn(scirdev_to_dev(idev), "%s: in wrong state: %s\n",
351 __func__, dev_state_name(state));
352 /* Return the frame back to the controller */
353 sci_controller_release_frame(ihost, frame_index);
354 return SCI_FAILURE_INVALID_STATE;
356 case SCI_STP_DEV_NCQ_ERROR:
357 case SCI_STP_DEV_AWAIT_RESET:
358 case SCI_DEV_STOPPING:
360 case SCI_DEV_RESETTING: {
361 struct isci_request *ireq;
362 struct ssp_frame_hdr hdr;
366 status = sci_unsolicited_frame_control_get_header(&ihost->uf_control,
369 if (status != SCI_SUCCESS)
372 word_cnt = sizeof(hdr) / sizeof(u32);
373 sci_swab32_cpy(&hdr, frame_header, word_cnt);
375 ireq = sci_request_by_tag(ihost, be16_to_cpu(hdr.tag));
376 if (ireq && ireq->target_device == idev) {
377 /* The IO request is now in charge of releasing the frame */
378 status = sci_io_request_frame_handler(ireq, frame_index);
380 /* We could not map this tag to a valid IO
381 * request Just toss the frame and continue
383 sci_controller_release_frame(ihost, frame_index);
387 case SCI_STP_DEV_NCQ: {
388 struct dev_to_host_fis *hdr;
390 status = sci_unsolicited_frame_control_get_header(&ihost->uf_control,
393 if (status != SCI_SUCCESS)
396 if (hdr->fis_type == FIS_SETDEVBITS &&
397 (hdr->status & ATA_ERR)) {
398 idev->not_ready_reason = SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED;
400 /* TODO Check sactive and complete associated IO if any. */
401 sci_change_state(sm, SCI_STP_DEV_NCQ_ERROR);
402 } else if (hdr->fis_type == FIS_REGD2H &&
403 (hdr->status & ATA_ERR)) {
405 * Some devices return D2H FIS when an NCQ error is detected.
406 * Treat this like an SDB error FIS ready reason.
408 idev->not_ready_reason = SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED;
409 sci_change_state(&idev->sm, SCI_STP_DEV_NCQ_ERROR);
411 status = SCI_FAILURE;
413 sci_controller_release_frame(ihost, frame_index);
416 case SCI_STP_DEV_CMD:
417 case SCI_SMP_DEV_CMD:
418 /* The device does not process any UF received from the hardware while
419 * in this state. All unsolicited frames are forwarded to the io request
422 status = sci_io_request_frame_handler(idev->working_request, frame_index);
429 static bool is_remote_device_ready(struct isci_remote_device *idev)
432 struct sci_base_state_machine *sm = &idev->sm;
433 enum sci_remote_device_states state = sm->current_state_id;
437 case SCI_STP_DEV_IDLE:
438 case SCI_STP_DEV_CMD:
439 case SCI_STP_DEV_NCQ:
440 case SCI_STP_DEV_NCQ_ERROR:
441 case SCI_STP_DEV_AWAIT_RESET:
442 case SCI_SMP_DEV_IDLE:
443 case SCI_SMP_DEV_CMD:
451 * called once the remote node context has transisitioned to a ready
452 * state (after suspending RX and/or TX due to early D2H fis)
454 static void atapi_remote_device_resume_done(void *_dev)
456 struct isci_remote_device *idev = _dev;
457 struct isci_request *ireq = idev->working_request;
459 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
462 enum sci_status sci_remote_device_event_handler(struct isci_remote_device *idev,
465 enum sci_status status;
467 switch (scu_get_event_type(event_code)) {
468 case SCU_EVENT_TYPE_RNC_OPS_MISC:
469 case SCU_EVENT_TYPE_RNC_SUSPEND_TX:
470 case SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX:
471 status = sci_remote_node_context_event_handler(&idev->rnc, event_code);
473 case SCU_EVENT_TYPE_PTX_SCHEDULE_EVENT:
474 if (scu_get_event_code(event_code) == SCU_EVENT_IT_NEXUS_TIMEOUT) {
475 status = SCI_SUCCESS;
477 /* Suspend the associated RNC */
478 sci_remote_device_suspend(idev, SCI_SW_SUSPEND_NORMAL);
480 dev_dbg(scirdev_to_dev(idev),
481 "%s: device: %p event code: %x: %s\n",
482 __func__, idev, event_code,
483 is_remote_device_ready(idev)
484 ? "I_T_Nexus_Timeout event"
485 : "I_T_Nexus_Timeout event in wrong state");
489 /* Else, fall through and treat as unhandled... */
491 dev_dbg(scirdev_to_dev(idev),
492 "%s: device: %p event code: %x: %s\n",
493 __func__, idev, event_code,
494 is_remote_device_ready(idev)
496 : "unexpected event in wrong state");
497 status = SCI_FAILURE_INVALID_STATE;
501 if (status != SCI_SUCCESS)
507 static void sci_remote_device_start_request(struct isci_remote_device *idev,
508 struct isci_request *ireq,
509 enum sci_status status)
511 struct isci_port *iport = idev->owning_port;
513 /* cleanup requests that failed after starting on the port */
514 if (status != SCI_SUCCESS)
515 sci_port_complete_io(iport, idev, ireq);
517 kref_get(&idev->kref);
518 idev->started_request_count++;
522 enum sci_status sci_remote_device_start_io(struct isci_host *ihost,
523 struct isci_remote_device *idev,
524 struct isci_request *ireq)
526 struct sci_base_state_machine *sm = &idev->sm;
527 enum sci_remote_device_states state = sm->current_state_id;
528 struct isci_port *iport = idev->owning_port;
529 enum sci_status status;
532 case SCI_DEV_INITIAL:
533 case SCI_DEV_STOPPED:
534 case SCI_DEV_STARTING:
535 case SCI_STP_DEV_NCQ_ERROR:
536 case SCI_DEV_STOPPING:
538 case SCI_DEV_RESETTING:
541 dev_warn(scirdev_to_dev(idev), "%s: in wrong state: %s\n",
542 __func__, dev_state_name(state));
543 return SCI_FAILURE_INVALID_STATE;
545 /* attempt to start an io request for this device object. The remote
546 * device object will issue the start request for the io and if
547 * successful it will start the request for the port object then
548 * increment its own request count.
550 status = sci_port_start_io(iport, idev, ireq);
551 if (status != SCI_SUCCESS)
554 status = sci_remote_node_context_start_io(&idev->rnc, ireq);
555 if (status != SCI_SUCCESS)
558 status = sci_request_start(ireq);
560 case SCI_STP_DEV_IDLE: {
561 /* handle the start io operation for a sata device that is in
562 * the command idle state. - Evalute the type of IO request to
563 * be started - If its an NCQ request change to NCQ substate -
564 * If its any other command change to the CMD substate
566 * If this is a softreset we may want to have a different
569 enum sci_remote_device_states new_state;
570 struct sas_task *task = isci_request_access_task(ireq);
572 status = sci_port_start_io(iport, idev, ireq);
573 if (status != SCI_SUCCESS)
576 status = sci_remote_node_context_start_io(&idev->rnc, ireq);
577 if (status != SCI_SUCCESS)
580 status = sci_request_start(ireq);
581 if (status != SCI_SUCCESS)
584 if (task->ata_task.use_ncq)
585 new_state = SCI_STP_DEV_NCQ;
587 idev->working_request = ireq;
588 new_state = SCI_STP_DEV_CMD;
590 sci_change_state(sm, new_state);
593 case SCI_STP_DEV_NCQ: {
594 struct sas_task *task = isci_request_access_task(ireq);
596 if (task->ata_task.use_ncq) {
597 status = sci_port_start_io(iport, idev, ireq);
598 if (status != SCI_SUCCESS)
601 status = sci_remote_node_context_start_io(&idev->rnc, ireq);
602 if (status != SCI_SUCCESS)
605 status = sci_request_start(ireq);
607 return SCI_FAILURE_INVALID_STATE;
610 case SCI_STP_DEV_AWAIT_RESET:
611 return SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED;
612 case SCI_SMP_DEV_IDLE:
613 status = sci_port_start_io(iport, idev, ireq);
614 if (status != SCI_SUCCESS)
617 status = sci_remote_node_context_start_io(&idev->rnc, ireq);
618 if (status != SCI_SUCCESS)
621 status = sci_request_start(ireq);
622 if (status != SCI_SUCCESS)
625 idev->working_request = ireq;
626 sci_change_state(&idev->sm, SCI_SMP_DEV_CMD);
628 case SCI_STP_DEV_CMD:
629 case SCI_SMP_DEV_CMD:
630 /* device is already handling a command it can not accept new commands
631 * until this one is complete.
633 return SCI_FAILURE_INVALID_STATE;
636 sci_remote_device_start_request(idev, ireq, status);
640 static enum sci_status common_complete_io(struct isci_port *iport,
641 struct isci_remote_device *idev,
642 struct isci_request *ireq)
644 enum sci_status status;
646 status = sci_request_complete(ireq);
647 if (status != SCI_SUCCESS)
650 status = sci_port_complete_io(iport, idev, ireq);
651 if (status != SCI_SUCCESS)
654 sci_remote_device_decrement_request_count(idev);
658 enum sci_status sci_remote_device_complete_io(struct isci_host *ihost,
659 struct isci_remote_device *idev,
660 struct isci_request *ireq)
662 struct sci_base_state_machine *sm = &idev->sm;
663 enum sci_remote_device_states state = sm->current_state_id;
664 struct isci_port *iport = idev->owning_port;
665 enum sci_status status;
668 case SCI_DEV_INITIAL:
669 case SCI_DEV_STOPPED:
670 case SCI_DEV_STARTING:
671 case SCI_STP_DEV_IDLE:
672 case SCI_SMP_DEV_IDLE:
676 dev_warn(scirdev_to_dev(idev), "%s: in wrong state: %s\n",
677 __func__, dev_state_name(state));
678 return SCI_FAILURE_INVALID_STATE;
680 case SCI_STP_DEV_AWAIT_RESET:
681 case SCI_DEV_RESETTING:
682 status = common_complete_io(iport, idev, ireq);
684 case SCI_STP_DEV_CMD:
685 case SCI_STP_DEV_NCQ:
686 case SCI_STP_DEV_NCQ_ERROR:
687 case SCI_STP_DEV_ATAPI_ERROR:
688 status = common_complete_io(iport, idev, ireq);
689 if (status != SCI_SUCCESS)
692 if (ireq->sci_status == SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED) {
693 /* This request causes hardware error, device needs to be Lun Reset.
694 * So here we force the state machine to IDLE state so the rest IOs
695 * can reach RNC state handler, these IOs will be completed by RNC with
696 * status of "DEVICE_RESET_REQUIRED", instead of "INVALID STATE".
698 sci_change_state(sm, SCI_STP_DEV_AWAIT_RESET);
699 } else if (idev->started_request_count == 0)
700 sci_change_state(sm, SCI_STP_DEV_IDLE);
702 case SCI_SMP_DEV_CMD:
703 status = common_complete_io(iport, idev, ireq);
704 if (status != SCI_SUCCESS)
706 sci_change_state(sm, SCI_SMP_DEV_IDLE);
708 case SCI_DEV_STOPPING:
709 status = common_complete_io(iport, idev, ireq);
710 if (status != SCI_SUCCESS)
713 if (idev->started_request_count == 0)
714 sci_remote_node_context_destruct(&idev->rnc,
720 if (status != SCI_SUCCESS)
721 dev_err(scirdev_to_dev(idev),
722 "%s: Port:0x%p Device:0x%p Request:0x%p Status:0x%x "
723 "could not complete\n", __func__, iport,
726 isci_put_device(idev);
731 static void sci_remote_device_continue_request(void *dev)
733 struct isci_remote_device *idev = dev;
735 /* we need to check if this request is still valid to continue. */
736 if (idev->working_request)
737 sci_controller_continue_io(idev->working_request);
740 enum sci_status sci_remote_device_start_task(struct isci_host *ihost,
741 struct isci_remote_device *idev,
742 struct isci_request *ireq)
744 struct sci_base_state_machine *sm = &idev->sm;
745 enum sci_remote_device_states state = sm->current_state_id;
746 struct isci_port *iport = idev->owning_port;
747 enum sci_status status;
750 case SCI_DEV_INITIAL:
751 case SCI_DEV_STOPPED:
752 case SCI_DEV_STARTING:
753 case SCI_SMP_DEV_IDLE:
754 case SCI_SMP_DEV_CMD:
755 case SCI_DEV_STOPPING:
757 case SCI_DEV_RESETTING:
760 dev_warn(scirdev_to_dev(idev), "%s: in wrong state: %s\n",
761 __func__, dev_state_name(state));
762 return SCI_FAILURE_INVALID_STATE;
763 case SCI_STP_DEV_IDLE:
764 case SCI_STP_DEV_CMD:
765 case SCI_STP_DEV_NCQ:
766 case SCI_STP_DEV_NCQ_ERROR:
767 case SCI_STP_DEV_AWAIT_RESET:
768 status = sci_port_start_io(iport, idev, ireq);
769 if (status != SCI_SUCCESS)
772 status = sci_request_start(ireq);
773 if (status != SCI_SUCCESS)
776 /* Note: If the remote device state is not IDLE this will
777 * replace the request that probably resulted in the task
778 * management request.
780 idev->working_request = ireq;
781 sci_change_state(sm, SCI_STP_DEV_CMD);
783 /* The remote node context must cleanup the TCi to NCQ mapping
784 * table. The only way to do this correctly is to either write
785 * to the TLCR register or to invalidate and repost the RNC. In
786 * either case the remote node context state machine will take
787 * the correct action when the remote node context is suspended
790 sci_remote_device_suspend(idev,
791 SCI_SW_SUSPEND_LINKHANG_DETECT);
793 status = sci_remote_node_context_start_task(&idev->rnc, ireq,
794 sci_remote_device_continue_request, idev);
797 sci_remote_device_start_request(idev, ireq, status);
798 /* We need to let the controller start request handler know that
799 * it can't post TC yet. We will provide a callback function to
800 * post TC when RNC gets resumed.
802 return SCI_FAILURE_RESET_DEVICE_PARTIAL_SUCCESS;
804 status = sci_port_start_io(iport, idev, ireq);
805 if (status != SCI_SUCCESS)
808 /* Resume the RNC as needed: */
809 status = sci_remote_node_context_start_task(&idev->rnc, ireq,
811 if (status != SCI_SUCCESS)
814 status = sci_request_start(ireq);
817 sci_remote_device_start_request(idev, ireq, status);
822 void sci_remote_device_post_request(struct isci_remote_device *idev, u32 request)
824 struct isci_port *iport = idev->owning_port;
828 (ISCI_PEG << SCU_CONTEXT_COMMAND_PROTOCOL_ENGINE_GROUP_SHIFT) |
829 (iport->physical_port_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT) |
830 idev->rnc.remote_node_index;
832 sci_controller_post_request(iport->owning_controller, context);
835 /* called once the remote node context has transisitioned to a
836 * ready state. This is the indication that the remote device object can also
837 * transition to ready.
839 static void remote_device_resume_done(void *_dev)
841 struct isci_remote_device *idev = _dev;
843 if (is_remote_device_ready(idev))
846 /* go 'ready' if we are not already in a ready state */
847 sci_change_state(&idev->sm, SCI_DEV_READY);
850 static void sci_stp_remote_device_ready_idle_substate_resume_complete_handler(void *_dev)
852 struct isci_remote_device *idev = _dev;
853 struct isci_host *ihost = idev->owning_port->owning_controller;
855 /* For NCQ operation we do not issue a isci_remote_device_not_ready().
856 * As a result, avoid sending the ready notification.
858 if (idev->sm.previous_state_id != SCI_STP_DEV_NCQ)
859 isci_remote_device_ready(ihost, idev);
862 static void sci_remote_device_initial_state_enter(struct sci_base_state_machine *sm)
864 struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
866 /* Initial state is a transitional state to the stopped state */
867 sci_change_state(&idev->sm, SCI_DEV_STOPPED);
871 * sci_remote_device_destruct() - free remote node context and destruct
872 * @remote_device: This parameter specifies the remote device to be destructed.
874 * Remote device objects are a limited resource. As such, they must be
875 * protected. Thus calls to construct and destruct are mutually exclusive and
876 * non-reentrant. The return value shall indicate if the device was
877 * successfully destructed or if some failure occurred. enum sci_status This value
878 * is returned if the device is successfully destructed.
879 * SCI_FAILURE_INVALID_REMOTE_DEVICE This value is returned if the supplied
880 * device isn't valid (e.g. it's already been destoryed, the handle isn't
883 static enum sci_status sci_remote_device_destruct(struct isci_remote_device *idev)
885 struct sci_base_state_machine *sm = &idev->sm;
886 enum sci_remote_device_states state = sm->current_state_id;
887 struct isci_host *ihost;
889 if (state != SCI_DEV_STOPPED) {
890 dev_warn(scirdev_to_dev(idev), "%s: in wrong state: %s\n",
891 __func__, dev_state_name(state));
892 return SCI_FAILURE_INVALID_STATE;
895 ihost = idev->owning_port->owning_controller;
896 sci_controller_free_remote_node_context(ihost, idev,
897 idev->rnc.remote_node_index);
898 idev->rnc.remote_node_index = SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX;
899 sci_change_state(sm, SCI_DEV_FINAL);
905 * isci_remote_device_deconstruct() - This function frees an isci_remote_device.
906 * @ihost: This parameter specifies the isci host object.
907 * @idev: This parameter specifies the remote device to be freed.
910 static void isci_remote_device_deconstruct(struct isci_host *ihost, struct isci_remote_device *idev)
912 dev_dbg(&ihost->pdev->dev,
913 "%s: isci_device = %p\n", __func__, idev);
915 /* There should not be any outstanding io's. All paths to
916 * here should go through isci_remote_device_nuke_requests.
917 * If we hit this condition, we will need a way to complete
918 * io requests in process */
919 BUG_ON(idev->started_request_count > 0);
921 sci_remote_device_destruct(idev);
922 list_del_init(&idev->node);
923 isci_put_device(idev);
926 static void sci_remote_device_stopped_state_enter(struct sci_base_state_machine *sm)
928 struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
929 struct isci_host *ihost = idev->owning_port->owning_controller;
932 /* If we are entering from the stopping state let the SCI User know that
933 * the stop operation has completed.
935 prev_state = idev->sm.previous_state_id;
936 if (prev_state == SCI_DEV_STOPPING)
937 isci_remote_device_deconstruct(ihost, idev);
939 sci_controller_remote_device_stopped(ihost, idev);
942 static void sci_remote_device_starting_state_enter(struct sci_base_state_machine *sm)
944 struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
945 struct isci_host *ihost = idev->owning_port->owning_controller;
947 isci_remote_device_not_ready(ihost, idev,
948 SCIC_REMOTE_DEVICE_NOT_READY_START_REQUESTED);
951 static void sci_remote_device_ready_state_enter(struct sci_base_state_machine *sm)
953 struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
954 struct isci_host *ihost = idev->owning_port->owning_controller;
955 struct domain_device *dev = idev->domain_dev;
957 if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_SATA)) {
958 sci_change_state(&idev->sm, SCI_STP_DEV_IDLE);
959 } else if (dev_is_expander(dev)) {
960 sci_change_state(&idev->sm, SCI_SMP_DEV_IDLE);
962 isci_remote_device_ready(ihost, idev);
965 static void sci_remote_device_ready_state_exit(struct sci_base_state_machine *sm)
967 struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
968 struct domain_device *dev = idev->domain_dev;
970 if (dev->dev_type == SAS_END_DEV) {
971 struct isci_host *ihost = idev->owning_port->owning_controller;
973 isci_remote_device_not_ready(ihost, idev,
974 SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED);
978 static void sci_remote_device_resetting_state_enter(struct sci_base_state_machine *sm)
980 struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
981 struct isci_host *ihost = idev->owning_port->owning_controller;
983 dev_dbg(&ihost->pdev->dev,
984 "%s: isci_device = %p\n", __func__, idev);
986 sci_remote_device_suspend(idev, SCI_SW_SUSPEND_LINKHANG_DETECT);
989 static void sci_remote_device_resetting_state_exit(struct sci_base_state_machine *sm)
991 struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
992 struct isci_host *ihost = idev->owning_port->owning_controller;
994 dev_dbg(&ihost->pdev->dev,
995 "%s: isci_device = %p\n", __func__, idev);
997 sci_remote_node_context_resume(&idev->rnc, NULL, NULL);
1000 static void sci_stp_remote_device_ready_idle_substate_enter(struct sci_base_state_machine *sm)
1002 struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
1004 idev->working_request = NULL;
1005 if (sci_remote_node_context_is_ready(&idev->rnc)) {
1007 * Since the RNC is ready, it's alright to finish completion
1008 * processing (e.g. signal the remote device is ready). */
1009 sci_stp_remote_device_ready_idle_substate_resume_complete_handler(idev);
1011 sci_remote_node_context_resume(&idev->rnc,
1012 sci_stp_remote_device_ready_idle_substate_resume_complete_handler,
1017 static void sci_stp_remote_device_ready_cmd_substate_enter(struct sci_base_state_machine *sm)
1019 struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
1020 struct isci_host *ihost = idev->owning_port->owning_controller;
1022 BUG_ON(idev->working_request == NULL);
1024 isci_remote_device_not_ready(ihost, idev,
1025 SCIC_REMOTE_DEVICE_NOT_READY_SATA_REQUEST_STARTED);
1028 static void sci_stp_remote_device_ready_ncq_error_substate_enter(struct sci_base_state_machine *sm)
1030 struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
1031 struct isci_host *ihost = idev->owning_port->owning_controller;
1033 if (idev->not_ready_reason == SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED)
1034 isci_remote_device_not_ready(ihost, idev,
1035 idev->not_ready_reason);
1038 static void sci_stp_remote_device_atapi_error_substate_enter(
1039 struct sci_base_state_machine *sm)
1041 struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
1043 /* This state is entered when an I/O is decoded with an error
1044 * condition. By this point the RNC expected suspension state is set.
1045 * The error conditions suspend the device, so unsuspend here if
1048 sci_remote_node_context_resume(&idev->rnc,
1049 atapi_remote_device_resume_done,
1053 static void sci_smp_remote_device_ready_idle_substate_enter(struct sci_base_state_machine *sm)
1055 struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
1056 struct isci_host *ihost = idev->owning_port->owning_controller;
1058 isci_remote_device_ready(ihost, idev);
1061 static void sci_smp_remote_device_ready_cmd_substate_enter(struct sci_base_state_machine *sm)
1063 struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
1064 struct isci_host *ihost = idev->owning_port->owning_controller;
1066 BUG_ON(idev->working_request == NULL);
1068 isci_remote_device_not_ready(ihost, idev,
1069 SCIC_REMOTE_DEVICE_NOT_READY_SMP_REQUEST_STARTED);
1072 static void sci_smp_remote_device_ready_cmd_substate_exit(struct sci_base_state_machine *sm)
1074 struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
1076 idev->working_request = NULL;
1079 static const struct sci_base_state sci_remote_device_state_table[] = {
1080 [SCI_DEV_INITIAL] = {
1081 .enter_state = sci_remote_device_initial_state_enter,
1083 [SCI_DEV_STOPPED] = {
1084 .enter_state = sci_remote_device_stopped_state_enter,
1086 [SCI_DEV_STARTING] = {
1087 .enter_state = sci_remote_device_starting_state_enter,
1090 .enter_state = sci_remote_device_ready_state_enter,
1091 .exit_state = sci_remote_device_ready_state_exit
1093 [SCI_STP_DEV_IDLE] = {
1094 .enter_state = sci_stp_remote_device_ready_idle_substate_enter,
1096 [SCI_STP_DEV_CMD] = {
1097 .enter_state = sci_stp_remote_device_ready_cmd_substate_enter,
1099 [SCI_STP_DEV_NCQ] = { },
1100 [SCI_STP_DEV_NCQ_ERROR] = {
1101 .enter_state = sci_stp_remote_device_ready_ncq_error_substate_enter,
1103 [SCI_STP_DEV_ATAPI_ERROR] = {
1104 .enter_state = sci_stp_remote_device_atapi_error_substate_enter,
1106 [SCI_STP_DEV_AWAIT_RESET] = { },
1107 [SCI_SMP_DEV_IDLE] = {
1108 .enter_state = sci_smp_remote_device_ready_idle_substate_enter,
1110 [SCI_SMP_DEV_CMD] = {
1111 .enter_state = sci_smp_remote_device_ready_cmd_substate_enter,
1112 .exit_state = sci_smp_remote_device_ready_cmd_substate_exit,
1114 [SCI_DEV_STOPPING] = { },
1115 [SCI_DEV_FAILED] = { },
1116 [SCI_DEV_RESETTING] = {
1117 .enter_state = sci_remote_device_resetting_state_enter,
1118 .exit_state = sci_remote_device_resetting_state_exit
1120 [SCI_DEV_FINAL] = { },
1124 * sci_remote_device_construct() - common construction
1125 * @sci_port: SAS/SATA port through which this device is accessed.
1126 * @sci_dev: remote device to construct
1128 * This routine just performs benign initialization and does not
1129 * allocate the remote_node_context which is left to
1130 * sci_remote_device_[de]a_construct(). sci_remote_device_destruct()
1131 * frees the remote_node_context(s) for the device.
1133 static void sci_remote_device_construct(struct isci_port *iport,
1134 struct isci_remote_device *idev)
1136 idev->owning_port = iport;
1137 idev->started_request_count = 0;
1139 sci_init_sm(&idev->sm, sci_remote_device_state_table, SCI_DEV_INITIAL);
1141 sci_remote_node_context_construct(&idev->rnc,
1142 SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX);
1146 * sci_remote_device_da_construct() - construct direct attached device.
1148 * The information (e.g. IAF, Signature FIS, etc.) necessary to build
1149 * the device is known to the SCI Core since it is contained in the
1150 * sci_phy object. Remote node context(s) is/are a global resource
1151 * allocated by this routine, freed by sci_remote_device_destruct().
1154 * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
1155 * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
1156 * sata-only controller instance.
1157 * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
1159 static enum sci_status sci_remote_device_da_construct(struct isci_port *iport,
1160 struct isci_remote_device *idev)
1162 enum sci_status status;
1163 struct sci_port_properties properties;
1165 sci_remote_device_construct(iport, idev);
1167 sci_port_get_properties(iport, &properties);
1168 /* Get accurate port width from port's phy mask for a DA device. */
1169 idev->device_port_width = hweight32(properties.phy_mask);
1171 status = sci_controller_allocate_remote_node_context(iport->owning_controller,
1173 &idev->rnc.remote_node_index);
1175 if (status != SCI_SUCCESS)
1178 idev->connection_rate = sci_port_get_max_allowed_speed(iport);
1184 * sci_remote_device_ea_construct() - construct expander attached device
1186 * Remote node context(s) is/are a global resource allocated by this
1187 * routine, freed by sci_remote_device_destruct().
1190 * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
1191 * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
1192 * sata-only controller instance.
1193 * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
1195 static enum sci_status sci_remote_device_ea_construct(struct isci_port *iport,
1196 struct isci_remote_device *idev)
1198 struct domain_device *dev = idev->domain_dev;
1199 enum sci_status status;
1201 sci_remote_device_construct(iport, idev);
1203 status = sci_controller_allocate_remote_node_context(iport->owning_controller,
1205 &idev->rnc.remote_node_index);
1206 if (status != SCI_SUCCESS)
1209 /* For SAS-2 the physical link rate is actually a logical link
1210 * rate that incorporates multiplexing. The SCU doesn't
1211 * incorporate multiplexing and for the purposes of the
1212 * connection the logical link rate is that same as the
1213 * physical. Furthermore, the SAS-2 and SAS-1.1 fields overlay
1214 * one another, so this code works for both situations.
1216 idev->connection_rate = min_t(u16, sci_port_get_max_allowed_speed(iport),
1219 /* / @todo Should I assign the port width by reading all of the phys on the port? */
1220 idev->device_port_width = 1;
1225 enum sci_status sci_remote_device_resume(
1226 struct isci_remote_device *idev,
1227 scics_sds_remote_node_context_callback cb_fn,
1230 enum sci_status status;
1232 status = sci_remote_node_context_resume(&idev->rnc, cb_fn, cb_p);
1233 if (status != SCI_SUCCESS)
1234 dev_dbg(scirdev_to_dev(idev), "%s: failed to resume: %d\n",
1239 enum sci_status isci_remote_device_resume(
1240 struct isci_host *ihost,
1241 struct isci_remote_device *idev,
1242 scics_sds_remote_node_context_callback cb_fn,
1245 unsigned long flags;
1246 enum sci_status status;
1248 spin_lock_irqsave(&ihost->scic_lock, flags);
1249 status = sci_remote_device_resume(idev, cb_fn, cb_p);
1250 spin_unlock_irqrestore(&ihost->scic_lock, flags);
1255 * sci_remote_device_start() - This method will start the supplied remote
1256 * device. This method enables normal IO requests to flow through to the
1258 * @remote_device: This parameter specifies the device to be started.
1259 * @timeout: This parameter specifies the number of milliseconds in which the
1260 * start operation should complete.
1262 * An indication of whether the device was successfully started. SCI_SUCCESS
1263 * This value is returned if the device was successfully started.
1264 * SCI_FAILURE_INVALID_PHY This value is returned if the user attempts to start
1265 * the device when there have been no phys added to it.
1267 static enum sci_status sci_remote_device_start(struct isci_remote_device *idev,
1270 struct sci_base_state_machine *sm = &idev->sm;
1271 enum sci_remote_device_states state = sm->current_state_id;
1272 enum sci_status status;
1274 if (state != SCI_DEV_STOPPED) {
1275 dev_warn(scirdev_to_dev(idev), "%s: in wrong state: %s\n",
1276 __func__, dev_state_name(state));
1277 return SCI_FAILURE_INVALID_STATE;
1280 status = sci_remote_device_resume(idev, remote_device_resume_done,
1282 if (status != SCI_SUCCESS)
1285 sci_change_state(sm, SCI_DEV_STARTING);
1290 static enum sci_status isci_remote_device_construct(struct isci_port *iport,
1291 struct isci_remote_device *idev)
1293 struct isci_host *ihost = iport->isci_host;
1294 struct domain_device *dev = idev->domain_dev;
1295 enum sci_status status;
1297 if (dev->parent && dev_is_expander(dev->parent))
1298 status = sci_remote_device_ea_construct(iport, idev);
1300 status = sci_remote_device_da_construct(iport, idev);
1302 if (status != SCI_SUCCESS) {
1303 dev_dbg(&ihost->pdev->dev, "%s: construct failed: %d\n",
1309 /* start the device. */
1310 status = sci_remote_device_start(idev, ISCI_REMOTE_DEVICE_START_TIMEOUT);
1312 if (status != SCI_SUCCESS)
1313 dev_warn(&ihost->pdev->dev, "remote device start failed: %d\n",
1320 * This function builds the isci_remote_device when a libsas dev_found message
1322 * @isci_host: This parameter specifies the isci host object.
1323 * @port: This parameter specifies the isci_port conected to this device.
1325 * pointer to new isci_remote_device.
1327 static struct isci_remote_device *
1328 isci_remote_device_alloc(struct isci_host *ihost, struct isci_port *iport)
1330 struct isci_remote_device *idev;
1333 for (i = 0; i < SCI_MAX_REMOTE_DEVICES; i++) {
1334 idev = &ihost->devices[i];
1335 if (!test_and_set_bit(IDEV_ALLOCATED, &idev->flags))
1339 if (i >= SCI_MAX_REMOTE_DEVICES) {
1340 dev_warn(&ihost->pdev->dev, "%s: failed\n", __func__);
1343 if (WARN_ONCE(!list_empty(&idev->node), "found non-idle remote device\n"))
1349 void isci_remote_device_release(struct kref *kref)
1351 struct isci_remote_device *idev = container_of(kref, typeof(*idev), kref);
1352 struct isci_host *ihost = idev->isci_port->isci_host;
1354 idev->domain_dev = NULL;
1355 idev->isci_port = NULL;
1356 clear_bit(IDEV_START_PENDING, &idev->flags);
1357 clear_bit(IDEV_STOP_PENDING, &idev->flags);
1358 clear_bit(IDEV_IO_READY, &idev->flags);
1359 clear_bit(IDEV_GONE, &idev->flags);
1360 smp_mb__before_clear_bit();
1361 clear_bit(IDEV_ALLOCATED, &idev->flags);
1362 wake_up(&ihost->eventq);
1366 * isci_remote_device_stop() - This function is called internally to stop the
1368 * @isci_host: This parameter specifies the isci host object.
1369 * @isci_device: This parameter specifies the remote device.
1371 * The status of the ihost request to stop.
1373 enum sci_status isci_remote_device_stop(struct isci_host *ihost, struct isci_remote_device *idev)
1375 enum sci_status status;
1376 unsigned long flags;
1378 dev_dbg(&ihost->pdev->dev,
1379 "%s: isci_device = %p\n", __func__, idev);
1381 spin_lock_irqsave(&ihost->scic_lock, flags);
1382 idev->domain_dev->lldd_dev = NULL; /* disable new lookups */
1383 set_bit(IDEV_GONE, &idev->flags);
1385 set_bit(IDEV_STOP_PENDING, &idev->flags);
1386 status = sci_remote_device_stop(idev, 50);
1387 spin_unlock_irqrestore(&ihost->scic_lock, flags);
1389 /* Wait for the stop complete callback. */
1390 if (WARN_ONCE(status != SCI_SUCCESS, "failed to stop device\n"))
1391 /* nothing to wait for */;
1393 wait_for_device_stop(ihost, idev);
1395 dev_dbg(&ihost->pdev->dev,
1396 "%s: isci_device = %p, waiting done.\n", __func__, idev);
1402 * isci_remote_device_gone() - This function is called by libsas when a domain
1403 * device is removed.
1404 * @domain_device: This parameter specifies the libsas domain device.
1407 void isci_remote_device_gone(struct domain_device *dev)
1409 struct isci_host *ihost = dev_to_ihost(dev);
1410 struct isci_remote_device *idev = dev->lldd_dev;
1412 dev_dbg(&ihost->pdev->dev,
1413 "%s: domain_device = %p, isci_device = %p, isci_port = %p\n",
1414 __func__, dev, idev, idev->isci_port);
1416 isci_remote_device_stop(ihost, idev);
1421 * isci_remote_device_found() - This function is called by libsas when a remote
1422 * device is discovered. A remote device object is created and started. the
1423 * function then sleeps until the sci core device started message is
1425 * @domain_device: This parameter specifies the libsas domain device.
1427 * status, zero indicates success.
1429 int isci_remote_device_found(struct domain_device *dev)
1431 struct isci_host *isci_host = dev_to_ihost(dev);
1432 struct isci_port *isci_port = dev->port->lldd_port;
1433 struct isci_remote_device *isci_device;
1434 enum sci_status status;
1436 dev_dbg(&isci_host->pdev->dev,
1437 "%s: domain_device = %p\n", __func__, dev);
1442 isci_device = isci_remote_device_alloc(isci_host, isci_port);
1446 kref_init(&isci_device->kref);
1447 INIT_LIST_HEAD(&isci_device->node);
1449 spin_lock_irq(&isci_host->scic_lock);
1450 isci_device->domain_dev = dev;
1451 isci_device->isci_port = isci_port;
1452 list_add_tail(&isci_device->node, &isci_port->remote_dev_list);
1454 set_bit(IDEV_START_PENDING, &isci_device->flags);
1455 status = isci_remote_device_construct(isci_port, isci_device);
1457 dev_dbg(&isci_host->pdev->dev,
1458 "%s: isci_device = %p\n",
1459 __func__, isci_device);
1461 if (status == SCI_SUCCESS) {
1462 /* device came up, advertise it to the world */
1463 dev->lldd_dev = isci_device;
1465 isci_put_device(isci_device);
1466 spin_unlock_irq(&isci_host->scic_lock);
1468 /* wait for the device ready callback. */
1469 wait_for_device_start(isci_host, isci_device);
1471 return status == SCI_SUCCESS ? 0 : -ENODEV;
1474 enum sci_status isci_remote_device_suspend_terminate(
1475 struct isci_host *ihost,
1476 struct isci_remote_device *idev,
1477 struct isci_request *ireq)
1479 unsigned long flags;
1480 enum sci_status status;
1482 /* Put the device into suspension. */
1483 spin_lock_irqsave(&ihost->scic_lock, flags);
1484 sci_remote_device_suspend(idev, SCI_SW_SUSPEND_LINKHANG_DETECT);
1485 spin_unlock_irqrestore(&ihost->scic_lock, flags);
1487 /* Terminate and wait for the completions. */
1488 status = isci_remote_device_terminate_requests(ihost, idev, ireq);
1489 if (status != SCI_SUCCESS)
1490 dev_dbg(&ihost->pdev->dev,
1491 "%s: isci_remote_device_terminate_requests(%p) "
1493 __func__, idev, status);
1495 /* NOTE: RNC resumption is left to the caller! */
1499 int isci_remote_device_is_safe_to_abort(
1500 struct isci_remote_device *idev)
1502 return sci_remote_node_context_is_safe_to_abort(&idev->rnc);
1505 enum sci_status sci_remote_device_abort_requests_pending_abort(
1506 struct isci_remote_device *idev)
1508 return sci_remote_device_terminate_reqs_checkabort(idev, 1);
1511 enum sci_status isci_remote_device_reset_complete(
1512 struct isci_host *ihost,
1513 struct isci_remote_device *idev)
1515 unsigned long flags;
1516 enum sci_status status;
1518 spin_lock_irqsave(&ihost->scic_lock, flags);
1519 status = sci_remote_device_reset_complete(idev);
1520 spin_unlock_irqrestore(&ihost->scic_lock, flags);