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.
55 #include "intel_sas.h"
56 #include "intel_ata.h"
59 #include "remote_device.h"
61 #include "scic_controller.h"
62 #include "scic_io_request.h"
64 #include "scic_port.h"
65 #include "scic_sds_controller.h"
66 #include "scic_sds_phy.h"
67 #include "scic_sds_port.h"
68 #include "remote_node_context.h"
69 #include "scic_sds_request.h"
70 #include "sci_environment.h"
72 #include "scu_event_codes.h"
76 * isci_remote_device_change_state() - This function gets the status of the
77 * remote_device object.
78 * @isci_device: This parameter points to the isci_remote_device object
80 * status of the object as a isci_status enum.
82 void isci_remote_device_change_state(
83 struct isci_remote_device *isci_device,
84 enum isci_status status)
88 spin_lock_irqsave(&isci_device->state_lock, flags);
89 isci_device->status = status;
90 spin_unlock_irqrestore(&isci_device->state_lock, flags);
94 * isci_remote_device_not_ready() - This function is called by the scic when
95 * the remote device is not ready. We mark the isci device as ready (not
96 * "ready_for_io") and signal the waiting proccess.
97 * @isci_host: This parameter specifies the isci host object.
98 * @isci_device: This parameter specifies the remote device
101 static void isci_remote_device_not_ready(struct isci_host *ihost,
102 struct isci_remote_device *idev, u32 reason)
104 dev_dbg(&ihost->pdev->dev,
105 "%s: isci_device = %p\n", __func__, idev);
107 if (reason == SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED)
108 isci_remote_device_change_state(idev, isci_stopping);
110 /* device ready is actually a "not ready for io" state. */
111 isci_remote_device_change_state(idev, isci_ready);
115 * isci_remote_device_ready() - This function is called by the scic when the
116 * remote device is ready. We mark the isci device as ready and signal the
118 * @ihost: our valid isci_host
119 * @idev: remote device
122 static void isci_remote_device_ready(struct isci_host *ihost, struct isci_remote_device *idev)
124 dev_dbg(&ihost->pdev->dev,
125 "%s: idev = %p\n", __func__, idev);
127 isci_remote_device_change_state(idev, isci_ready_for_io);
128 if (test_and_clear_bit(IDEV_START_PENDING, &idev->flags))
129 wake_up(&ihost->eventq);
134 enum sci_status scic_remote_device_stop(
135 struct scic_sds_remote_device *sci_dev,
138 return sci_dev->state_handlers->stop_handler(sci_dev);
142 enum sci_status scic_remote_device_reset(
143 struct scic_sds_remote_device *sci_dev)
145 return sci_dev->state_handlers->reset_handler(sci_dev);
149 enum sci_status scic_remote_device_reset_complete(
150 struct scic_sds_remote_device *sci_dev)
152 return sci_dev->state_handlers->reset_complete_handler(sci_dev);
155 #define SCIC_SDS_REMOTE_DEVICE_MINIMUM_TIMER_COUNT (0)
156 #define SCIC_SDS_REMOTE_DEVICE_MAXIMUM_TIMER_COUNT (SCI_MAX_REMOTE_DEVICES)
160 * @sci_dev: The remote device for which the suspend is being requested.
162 * This method invokes the remote device suspend state handler. enum sci_status
164 enum sci_status scic_sds_remote_device_suspend(
165 struct scic_sds_remote_device *sci_dev,
168 return sci_dev->state_handlers->suspend_handler(sci_dev, suspend_type);
173 * @sci_dev: The remote device for which the event handling is being
175 * @frame_index: This is the frame index that is being processed.
177 * This method invokes the frame handler for the remote device state machine
180 enum sci_status scic_sds_remote_device_frame_handler(
181 struct scic_sds_remote_device *sci_dev,
184 return sci_dev->state_handlers->frame_handler(sci_dev, frame_index);
189 * @sci_dev: The remote device for which the event handling is being
191 * @event_code: This is the event code that is to be processed.
193 * This method invokes the remote device event handler. enum sci_status
195 enum sci_status scic_sds_remote_device_event_handler(
196 struct scic_sds_remote_device *sci_dev,
199 return sci_dev->state_handlers->event_handler(sci_dev, event_code);
204 * @controller: The controller that is starting the io request.
205 * @sci_dev: The remote device for which the start io handling is being
207 * @io_request: The io request that is being started.
209 * This method invokes the remote device start io handler. enum sci_status
211 enum sci_status scic_sds_remote_device_start_io(
212 struct scic_sds_controller *controller,
213 struct scic_sds_remote_device *sci_dev,
214 struct scic_sds_request *io_request)
216 return sci_dev->state_handlers->start_io_handler(
217 sci_dev, io_request);
222 * @controller: The controller that is completing the io request.
223 * @sci_dev: The remote device for which the complete io handling is being
225 * @io_request: The io request that is being completed.
227 * This method invokes the remote device complete io handler. enum sci_status
229 enum sci_status scic_sds_remote_device_complete_io(
230 struct scic_sds_controller *controller,
231 struct scic_sds_remote_device *sci_dev,
232 struct scic_sds_request *io_request)
234 return sci_dev->state_handlers->complete_io_handler(
235 sci_dev, io_request);
240 * @controller: The controller that is starting the task request.
241 * @sci_dev: The remote device for which the start task handling is being
243 * @io_request: The task request that is being started.
245 * This method invokes the remote device start task handler. enum sci_status
247 enum sci_status scic_sds_remote_device_start_task(
248 struct scic_sds_controller *controller,
249 struct scic_sds_remote_device *sci_dev,
250 struct scic_sds_request *io_request)
252 return sci_dev->state_handlers->start_task_handler(
253 sci_dev, io_request);
261 * This method takes the request and bulids an appropriate SCU context for the
262 * request and then requests the controller to post the request. none
264 void scic_sds_remote_device_post_request(
265 struct scic_sds_remote_device *sci_dev,
270 context = scic_sds_remote_device_build_command_context(sci_dev, request);
272 scic_sds_controller_post_request(
273 scic_sds_remote_device_get_controller(sci_dev),
278 /* called once the remote node context is ready to be freed.
279 * The remote device can now report that its stop operation is complete. none
281 static void scic_sds_cb_remote_device_rnc_destruct_complete(void *_dev)
283 struct scic_sds_remote_device *sci_dev = _dev;
285 BUG_ON(sci_dev->started_request_count != 0);
286 sci_base_state_machine_change_state(&sci_dev->state_machine,
287 SCI_BASE_REMOTE_DEVICE_STATE_STOPPED);
290 /* called once the remote node context has transisitioned to a
291 * ready state. This is the indication that the remote device object can also
292 * transition to ready.
294 static void scic_sds_remote_device_resume_complete_handler(void *_dev)
296 struct scic_sds_remote_device *sci_dev = _dev;
297 enum scic_sds_remote_device_states state;
299 state = sci_dev->state_machine.current_state_id;
301 case SCI_BASE_REMOTE_DEVICE_STATE_READY:
302 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
303 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
304 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ:
305 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
306 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
307 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
308 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
311 /* go 'ready' if we are not already in a ready state */
312 sci_base_state_machine_change_state(&sci_dev->state_machine,
313 SCI_BASE_REMOTE_DEVICE_STATE_READY);
320 * @device: This parameter specifies the device for which the request is being
322 * @request: This parameter specifies the request being started.
323 * @status: This parameter specifies the current start operation status.
325 * This method will perform the STP request start processing common to IO
326 * requests and task requests of all types. none
328 static void scic_sds_remote_device_start_request(
329 struct scic_sds_remote_device *sci_dev,
330 struct scic_sds_request *sci_req,
331 enum sci_status status)
333 /* We still have a fault in starting the io complete it on the port */
334 if (status == SCI_SUCCESS)
335 scic_sds_remote_device_increment_request_count(sci_dev);
337 sci_dev->owning_port->state_handlers->complete_io_handler(
338 sci_dev->owning_port, sci_dev, sci_req
346 * @request: This parameter specifies the request being continued.
348 * This method will continue to post tc for a STP request. This method usually
349 * serves as a callback when RNC gets resumed during a task management
352 static void scic_sds_remote_device_continue_request(void *dev)
354 struct scic_sds_remote_device *sci_dev = dev;
356 /* we need to check if this request is still valid to continue. */
357 if (sci_dev->working_request)
358 scic_controller_continue_io(sci_dev->working_request);
362 * This method will terminate all of the IO requests in the controllers IO
363 * request table that were targeted for this device.
364 * @sci_dev: This parameter specifies the remote device for which to
365 * attempt to terminate all requests.
367 * This method returns an indication as to whether all requests were
368 * successfully terminated. If a single request fails to be terminated, then
369 * this method will return the failure.
371 static enum sci_status scic_sds_remote_device_terminate_requests(
372 struct scic_sds_remote_device *sci_dev)
374 enum sci_status status = SCI_SUCCESS;
375 enum sci_status terminate_status = SCI_SUCCESS;
376 struct scic_sds_request *sci_req;
378 u32 request_count = sci_dev->started_request_count;
381 (index < SCI_MAX_IO_REQUESTS) && (request_count > 0);
383 sci_req = sci_dev->owning_port->owning_controller->io_request_table[index];
385 if ((sci_req != NULL) && (sci_req->target_device == sci_dev)) {
386 terminate_status = scic_controller_terminate_request(
387 sci_dev->owning_port->owning_controller,
392 if (terminate_status != SCI_SUCCESS)
393 status = terminate_status;
402 static enum sci_status
403 default_device_handler(struct scic_sds_remote_device *sci_dev,
406 dev_warn(scirdev_to_dev(sci_dev),
407 "%s: in wrong state: %d\n", func,
408 sci_base_state_machine_get_state(&sci_dev->state_machine));
409 return SCI_FAILURE_INVALID_STATE;
412 static enum sci_status scic_sds_remote_device_default_start_handler(
413 struct scic_sds_remote_device *sci_dev)
415 return default_device_handler(sci_dev, __func__);
418 static enum sci_status scic_sds_remote_device_default_stop_handler(
419 struct scic_sds_remote_device *sci_dev)
421 return default_device_handler(sci_dev, __func__);
424 static enum sci_status scic_sds_remote_device_default_fail_handler(
425 struct scic_sds_remote_device *sci_dev)
427 return default_device_handler(sci_dev, __func__);
430 static enum sci_status scic_sds_remote_device_default_destruct_handler(
431 struct scic_sds_remote_device *sci_dev)
433 return default_device_handler(sci_dev, __func__);
436 static enum sci_status scic_sds_remote_device_default_reset_handler(
437 struct scic_sds_remote_device *sci_dev)
439 return default_device_handler(sci_dev, __func__);
442 static enum sci_status scic_sds_remote_device_default_reset_complete_handler(
443 struct scic_sds_remote_device *sci_dev)
445 return default_device_handler(sci_dev, __func__);
448 static enum sci_status scic_sds_remote_device_default_suspend_handler(
449 struct scic_sds_remote_device *sci_dev, u32 suspend_type)
451 return default_device_handler(sci_dev, __func__);
454 static enum sci_status scic_sds_remote_device_default_resume_handler(
455 struct scic_sds_remote_device *sci_dev)
457 return default_device_handler(sci_dev, __func__);
462 * @device: The struct scic_sds_remote_device which is then cast into a
463 * struct scic_sds_remote_device.
464 * @event_code: The event code that the struct scic_sds_controller wants the device
467 * This method is the default event handler. It will call the RNC state
468 * machine handler for any RNC events otherwise it will log a warning and
469 * returns a failure. enum sci_status SCI_FAILURE_INVALID_STATE
471 static enum sci_status scic_sds_remote_device_core_event_handler(
472 struct scic_sds_remote_device *sci_dev,
476 enum sci_status status;
478 switch (scu_get_event_type(event_code)) {
479 case SCU_EVENT_TYPE_RNC_OPS_MISC:
480 case SCU_EVENT_TYPE_RNC_SUSPEND_TX:
481 case SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX:
482 status = scic_sds_remote_node_context_event_handler(&sci_dev->rnc, event_code);
484 case SCU_EVENT_TYPE_PTX_SCHEDULE_EVENT:
486 if (scu_get_event_code(event_code) == SCU_EVENT_IT_NEXUS_TIMEOUT) {
487 status = SCI_SUCCESS;
489 /* Suspend the associated RNC */
490 scic_sds_remote_node_context_suspend(&sci_dev->rnc,
491 SCI_SOFTWARE_SUSPENSION,
494 dev_dbg(scirdev_to_dev(sci_dev),
495 "%s: device: %p event code: %x: %s\n",
496 __func__, sci_dev, event_code,
498 ? "I_T_Nexus_Timeout event"
499 : "I_T_Nexus_Timeout event in wrong state");
503 /* Else, fall through and treat as unhandled... */
506 dev_dbg(scirdev_to_dev(sci_dev),
507 "%s: device: %p event code: %x: %s\n",
508 __func__, sci_dev, event_code,
511 : "unexpected event in wrong state");
512 status = SCI_FAILURE_INVALID_STATE;
520 * @device: The struct scic_sds_remote_device which is then cast into a
521 * struct scic_sds_remote_device.
522 * @event_code: The event code that the struct scic_sds_controller wants the device
525 * This method is the default event handler. It will call the RNC state
526 * machine handler for any RNC events otherwise it will log a warning and
527 * returns a failure. enum sci_status SCI_FAILURE_INVALID_STATE
529 static enum sci_status scic_sds_remote_device_default_event_handler(
530 struct scic_sds_remote_device *sci_dev,
533 return scic_sds_remote_device_core_event_handler(sci_dev,
540 * @device: The struct scic_sds_remote_device which is then cast into a
541 * struct scic_sds_remote_device.
542 * @frame_index: The frame index for which the struct scic_sds_controller wants this
543 * device object to process.
545 * This method is the default unsolicited frame handler. It logs a warning,
546 * releases the frame and returns a failure. enum sci_status
547 * SCI_FAILURE_INVALID_STATE
549 static enum sci_status scic_sds_remote_device_default_frame_handler(
550 struct scic_sds_remote_device *sci_dev,
553 dev_warn(scirdev_to_dev(sci_dev),
554 "%s: SCIC Remote Device requested to handle frame %x "
555 "while in wrong state %d\n",
558 sci_base_state_machine_get_state(
559 &sci_dev->state_machine));
561 /* Return the frame back to the controller */
562 scic_sds_controller_release_frame(
563 scic_sds_remote_device_get_controller(sci_dev), frame_index
566 return SCI_FAILURE_INVALID_STATE;
569 static enum sci_status scic_sds_remote_device_default_start_request_handler(
570 struct scic_sds_remote_device *sci_dev,
571 struct scic_sds_request *request)
573 return default_device_handler(sci_dev, __func__);
576 static enum sci_status scic_sds_remote_device_default_complete_request_handler(
577 struct scic_sds_remote_device *sci_dev,
578 struct scic_sds_request *request)
580 return default_device_handler(sci_dev, __func__);
583 static enum sci_status scic_sds_remote_device_default_continue_request_handler(
584 struct scic_sds_remote_device *sci_dev,
585 struct scic_sds_request *request)
587 return default_device_handler(sci_dev, __func__);
592 * @device: The struct scic_sds_remote_device which is then cast into a
593 * struct scic_sds_remote_device.
594 * @frame_index: The frame index for which the struct scic_sds_controller wants this
595 * device object to process.
597 * This method is a general ssp frame handler. In most cases the device object
598 * needs to route the unsolicited frame processing to the io request object.
599 * This method decodes the tag for the io request object and routes the
600 * unsolicited frame to that object. enum sci_status SCI_FAILURE_INVALID_STATE
602 static enum sci_status scic_sds_remote_device_general_frame_handler(
603 struct scic_sds_remote_device *sci_dev,
606 enum sci_status result;
607 struct sci_ssp_frame_header *frame_header;
608 struct scic_sds_request *io_request;
610 result = scic_sds_unsolicited_frame_control_get_header(
611 &(scic_sds_remote_device_get_controller(sci_dev)->uf_control),
613 (void **)&frame_header
616 if (SCI_SUCCESS == result) {
617 io_request = scic_sds_controller_get_io_request_from_tag(
618 scic_sds_remote_device_get_controller(sci_dev), frame_header->tag);
620 if ((io_request == NULL)
621 || (io_request->target_device != sci_dev)) {
623 * We could not map this tag to a valid IO request
624 * Just toss the frame and continue */
625 scic_sds_controller_release_frame(
626 scic_sds_remote_device_get_controller(sci_dev), frame_index
629 /* The IO request is now in charge of releasing the frame */
630 result = io_request->state_handlers->frame_handler(
631 io_request, frame_index);
640 * @[in]: sci_dev This is the device object that is receiving the event.
641 * @[in]: event_code The event code to process.
643 * This is a common method for handling events reported to the remote device
644 * from the controller object. enum sci_status
646 static enum sci_status scic_sds_remote_device_general_event_handler(
647 struct scic_sds_remote_device *sci_dev,
650 return scic_sds_remote_device_core_event_handler(sci_dev,
656 * *****************************************************************************
657 * * STOPPED STATE HANDLERS
658 * ***************************************************************************** */
664 * This method takes the struct scic_sds_remote_device from a stopped state and
665 * attempts to start it. The RNC buffer for the device is constructed and the
666 * device state machine is transitioned to the
667 * SCIC_BASE_REMOTE_DEVICE_STATE_STARTING. enum sci_status SCI_SUCCESS if there is
668 * an RNC buffer available to construct the remote device.
669 * SCI_FAILURE_INSUFFICIENT_RESOURCES if there is no RNC buffer available in
670 * which to construct the remote device.
672 static enum sci_status scic_sds_remote_device_stopped_state_start_handler(
673 struct scic_sds_remote_device *sci_dev)
675 enum sci_status status;
677 status = scic_sds_remote_node_context_resume(&sci_dev->rnc,
678 scic_sds_remote_device_resume_complete_handler, sci_dev);
680 if (status == SCI_SUCCESS)
681 sci_base_state_machine_change_state(&sci_dev->state_machine,
682 SCI_BASE_REMOTE_DEVICE_STATE_STARTING);
687 static enum sci_status scic_sds_remote_device_stopped_state_stop_handler(
688 struct scic_sds_remote_device *sci_dev)
695 * @sci_dev: The struct scic_sds_remote_device which is cast into a
696 * struct scic_sds_remote_device.
698 * This method will destruct a struct scic_sds_remote_device that is in a stopped
699 * state. This is the only state from which a destruct request will succeed.
700 * The RNi for this struct scic_sds_remote_device is returned to the free pool and the
701 * device object transitions to the SCI_BASE_REMOTE_DEVICE_STATE_FINAL.
702 * enum sci_status SCI_SUCCESS
704 static enum sci_status scic_sds_remote_device_stopped_state_destruct_handler(
705 struct scic_sds_remote_device *sci_dev)
707 struct scic_sds_controller *scic;
709 scic = scic_sds_remote_device_get_controller(sci_dev);
710 scic_sds_controller_free_remote_node_context(scic, sci_dev,
711 sci_dev->rnc.remote_node_index);
712 sci_dev->rnc.remote_node_index = SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX;
714 sci_base_state_machine_change_state(&sci_dev->state_machine,
715 SCI_BASE_REMOTE_DEVICE_STATE_FINAL);
721 * *****************************************************************************
722 * * STARTING STATE HANDLERS
723 * ***************************************************************************** */
725 static enum sci_status scic_sds_remote_device_starting_state_stop_handler(
726 struct scic_sds_remote_device *sci_dev)
729 * This device has not yet started so there had better be no IO requests
731 BUG_ON(sci_dev->started_request_count != 0);
734 * Destroy the remote node context
736 scic_sds_remote_node_context_destruct(&sci_dev->rnc,
737 scic_sds_cb_remote_device_rnc_destruct_complete, sci_dev);
740 * Transition to the stopping state and wait for the remote node to
741 * complete being posted and invalidated.
743 sci_base_state_machine_change_state(&sci_dev->state_machine,
744 SCI_BASE_REMOTE_DEVICE_STATE_STOPPING);
749 static enum sci_status scic_sds_remote_device_ready_state_stop_handler(
750 struct scic_sds_remote_device *sci_dev)
752 enum sci_status status = SCI_SUCCESS;
754 /* Request the parent state machine to transition to the stopping state */
755 sci_base_state_machine_change_state(&sci_dev->state_machine,
756 SCI_BASE_REMOTE_DEVICE_STATE_STOPPING);
758 if (sci_dev->started_request_count == 0) {
759 scic_sds_remote_node_context_destruct(&sci_dev->rnc,
760 scic_sds_cb_remote_device_rnc_destruct_complete,
763 status = scic_sds_remote_device_terminate_requests(sci_dev);
770 * @device: The struct scic_sds_remote_device object which is cast to a
771 * struct scic_sds_remote_device object.
773 * This is the ready state device reset handler enum sci_status
775 static enum sci_status scic_sds_remote_device_ready_state_reset_handler(
776 struct scic_sds_remote_device *sci_dev)
778 /* Request the parent state machine to transition to the stopping state */
779 sci_base_state_machine_change_state(&sci_dev->state_machine,
780 SCI_BASE_REMOTE_DEVICE_STATE_RESETTING);
786 * This method will attempt to start a task request for this device object. The
787 * remote device object will issue the start request for the task and if
788 * successful it will start the request for the port object then increment its
789 * own requet count. enum sci_status SCI_SUCCESS if the task request is started for
790 * this device object. SCI_FAILURE_INSUFFICIENT_RESOURCES if the io request
791 * object could not get the resources to start.
793 static enum sci_status scic_sds_remote_device_ready_state_start_task_handler(
794 struct scic_sds_remote_device *sci_dev,
795 struct scic_sds_request *request)
797 enum sci_status result;
799 /* See if the port is in a state where we can start the IO request */
800 result = scic_sds_port_start_io(
801 scic_sds_remote_device_get_port(sci_dev), sci_dev, request);
803 if (result == SCI_SUCCESS) {
804 result = scic_sds_remote_node_context_start_task(&sci_dev->rnc,
806 if (result == SCI_SUCCESS)
807 result = scic_sds_request_start(request);
809 scic_sds_remote_device_start_request(sci_dev, request, result);
816 * This method will attempt to start an io request for this device object. The
817 * remote device object will issue the start request for the io and if
818 * successful it will start the request for the port object then increment its
819 * own requet count. enum sci_status SCI_SUCCESS if the io request is started for
820 * this device object. SCI_FAILURE_INSUFFICIENT_RESOURCES if the io request
821 * object could not get the resources to start.
823 static enum sci_status scic_sds_remote_device_ready_state_start_io_handler(
824 struct scic_sds_remote_device *sci_dev,
825 struct scic_sds_request *request)
827 enum sci_status result;
829 /* See if the port is in a state where we can start the IO request */
830 result = scic_sds_port_start_io(
831 scic_sds_remote_device_get_port(sci_dev), sci_dev, request);
833 if (result == SCI_SUCCESS) {
834 result = scic_sds_remote_node_context_start_io(&sci_dev->rnc, request);
835 if (result == SCI_SUCCESS)
836 result = scic_sds_request_start(request);
838 scic_sds_remote_device_start_request(sci_dev, request, result);
845 * This method will complete the request for the remote device object. The
846 * method will call the completion handler for the request object and if
847 * successful it will complete the request on the port object then decrement
848 * its own started_request_count. enum sci_status
850 static enum sci_status scic_sds_remote_device_ready_state_complete_request_handler(
851 struct scic_sds_remote_device *sci_dev,
852 struct scic_sds_request *request)
854 enum sci_status result;
856 result = scic_sds_request_complete(request);
858 if (result != SCI_SUCCESS)
861 /* See if the port is in a state
862 * where we can start the IO request */
863 result = scic_sds_port_complete_io(
864 scic_sds_remote_device_get_port(sci_dev),
867 if (result == SCI_SUCCESS)
868 scic_sds_remote_device_decrement_request_count(sci_dev);
874 * *****************************************************************************
875 * * STOPPING STATE HANDLERS
876 * ***************************************************************************** */
880 * @sci_dev: The struct scic_sds_remote_device which is cast into a
881 * struct scic_sds_remote_device.
883 * This method will stop a struct scic_sds_remote_device that is already in the
884 * SCI_BASE_REMOTE_DEVICE_STATE_STOPPING state. This is not considered an error
885 * since we allow a stop request on a device that is alreay stopping or
886 * stopped. enum sci_status SCI_SUCCESS
888 static enum sci_status scic_sds_remote_device_stopping_state_stop_handler(
889 struct scic_sds_remote_device *device)
892 * All requests should have been terminated, but if there is an
893 * attempt to stop a device already in the stopping state, then
894 * try again to terminate. */
895 return scic_sds_remote_device_terminate_requests(device);
901 * @device: The device object for which the request is completing.
902 * @request: The task request that is being completed.
904 * This method completes requests for this struct scic_sds_remote_device while it is
905 * in the SCI_BASE_REMOTE_DEVICE_STATE_STOPPING state. This method calls the
906 * complete method for the request object and if that is successful the port
907 * object is called to complete the task request. Then the device object itself
908 * completes the task request. If struct scic_sds_remote_device started_request_count
909 * goes to 0 and the invalidate RNC request has completed the device object can
910 * transition to the SCI_BASE_REMOTE_DEVICE_STATE_STOPPED. enum sci_status
912 static enum sci_status scic_sds_remote_device_stopping_state_complete_request_handler(
913 struct scic_sds_remote_device *sci_dev,
914 struct scic_sds_request *request)
916 enum sci_status status = SCI_SUCCESS;
918 status = scic_sds_request_complete(request);
920 if (status != SCI_SUCCESS)
923 status = scic_sds_port_complete_io(scic_sds_remote_device_get_port(sci_dev),
925 if (status != SCI_SUCCESS)
928 scic_sds_remote_device_decrement_request_count(sci_dev);
930 if (scic_sds_remote_device_get_request_count(sci_dev) == 0)
931 scic_sds_remote_node_context_destruct(&sci_dev->rnc,
932 scic_sds_cb_remote_device_rnc_destruct_complete,
937 static enum sci_status scic_sds_remote_device_resetting_state_reset_complete_handler(
938 struct scic_sds_remote_device *sci_dev)
940 sci_base_state_machine_change_state(&sci_dev->state_machine,
941 SCI_BASE_REMOTE_DEVICE_STATE_READY);
946 static enum sci_status scic_sds_remote_device_resetting_state_stop_handler(
947 struct scic_sds_remote_device *sci_dev)
949 sci_base_state_machine_change_state(
950 &sci_dev->state_machine,
951 SCI_BASE_REMOTE_DEVICE_STATE_STOPPING
957 /* complete requests for this device while it is in the
958 * SCI_BASE_REMOTE_DEVICE_STATE_RESETTING state. This method calls the complete
959 * method for the request object and if that is successful the port object is
960 * called to complete the task request. Then the device object itself completes
961 * the task request. enum sci_status
963 static enum sci_status scic_sds_remote_device_resetting_state_complete_request_handler(
964 struct scic_sds_remote_device *sci_dev,
965 struct scic_sds_request *request)
967 enum sci_status status = SCI_SUCCESS;
969 status = scic_sds_request_complete(request);
971 if (status == SCI_SUCCESS) {
972 status = scic_sds_port_complete_io(
973 scic_sds_remote_device_get_port(sci_dev),
976 if (status == SCI_SUCCESS) {
977 scic_sds_remote_device_decrement_request_count(sci_dev);
984 static enum sci_status scic_sds_stp_remote_device_complete_request(struct scic_sds_remote_device *sci_dev,
985 struct scic_sds_request *sci_req)
987 enum sci_status status;
989 status = scic_sds_io_request_complete(sci_req);
990 if (status != SCI_SUCCESS)
993 status = scic_sds_port_complete_io(sci_dev->owning_port, sci_dev, sci_req);
994 if (status != SCI_SUCCESS)
997 scic_sds_remote_device_decrement_request_count(sci_dev);
998 if (sci_req->sci_status == SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED) {
999 /* This request causes hardware error, device needs to be Lun Reset.
1000 * So here we force the state machine to IDLE state so the rest IOs
1001 * can reach RNC state handler, these IOs will be completed by RNC with
1002 * status of "DEVICE_RESET_REQUIRED", instead of "INVALID STATE".
1004 sci_base_state_machine_change_state(&sci_dev->state_machine,
1005 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET);
1006 } else if (scic_sds_remote_device_get_request_count(sci_dev) == 0)
1007 sci_base_state_machine_change_state(&sci_dev->state_machine,
1008 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
1012 if (status != SCI_SUCCESS)
1013 dev_err(scirdev_to_dev(sci_dev),
1014 "%s: Port:0x%p Device:0x%p Request:0x%p Status:0x%x "
1015 "could not complete\n", __func__, sci_dev->owning_port,
1016 sci_dev, sci_req, status);
1021 /* scic_sds_stp_remote_device_ready_substate_start_request_handler - start stp
1022 * @device: The target device a task management request towards to.
1023 * @request: The task request.
1025 * This is the READY NCQ substate handler to start task management request. In
1026 * this routine, we suspend and resume the RNC. enum sci_status Always return
1027 * SCI_FAILURE_RESET_DEVICE_PARTIAL_SUCCESS status to let
1028 * controller_start_task_handler know that the controller can't post TC for
1029 * task request yet, instead, when RNC gets resumed, a controller_continue_task
1030 * callback will be called.
1032 static enum sci_status scic_sds_stp_remote_device_ready_substate_start_request_handler(
1033 struct scic_sds_remote_device *device,
1034 struct scic_sds_request *request)
1036 enum sci_status status;
1038 /* Will the port allow the io request to start? */
1039 status = device->owning_port->state_handlers->start_io_handler(
1040 device->owning_port, device, request);
1041 if (status != SCI_SUCCESS)
1044 status = scic_sds_remote_node_context_start_task(&device->rnc, request);
1045 if (status != SCI_SUCCESS)
1048 status = request->state_handlers->start_handler(request);
1049 if (status != SCI_SUCCESS)
1053 * Note: If the remote device state is not IDLE this will replace
1054 * the request that probably resulted in the task management request.
1056 device->working_request = request;
1057 sci_base_state_machine_change_state(&device->state_machine,
1058 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD);
1061 * The remote node context must cleanup the TCi to NCQ mapping table.
1062 * The only way to do this correctly is to either write to the TLCR
1063 * register or to invalidate and repost the RNC. In either case the
1064 * remote node context state machine will take the correct action when
1065 * the remote node context is suspended and later resumed.
1067 scic_sds_remote_node_context_suspend(&device->rnc,
1068 SCI_SOFTWARE_SUSPENSION, NULL, NULL);
1069 scic_sds_remote_node_context_resume(&device->rnc,
1070 scic_sds_remote_device_continue_request,
1074 scic_sds_remote_device_start_request(device, request, status);
1076 * We need to let the controller start request handler know that it can't
1077 * post TC yet. We will provide a callback function to post TC when RNC gets
1080 return SCI_FAILURE_RESET_DEVICE_PARTIAL_SUCCESS;
1083 /* handle the start io operation for a sata device that is in the command idle
1084 * state. - Evalute the type of IO request to be started - If its an NCQ
1085 * request change to NCQ substate - If its any other command change to the CMD
1088 * If this is a softreset we may want to have a different substate.
1090 static enum sci_status scic_sds_stp_remote_device_ready_idle_substate_start_io_handler(
1091 struct scic_sds_remote_device *sci_dev,
1092 struct scic_sds_request *request)
1094 enum sci_status status;
1095 struct isci_request *isci_request = request->ireq;
1096 enum scic_sds_remote_device_states new_state;
1098 /* Will the port allow the io request to start? */
1099 status = sci_dev->owning_port->state_handlers->start_io_handler(
1100 sci_dev->owning_port, sci_dev, request);
1101 if (status != SCI_SUCCESS)
1104 status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, request);
1105 if (status != SCI_SUCCESS)
1108 status = request->state_handlers->start_handler(request);
1109 if (status != SCI_SUCCESS)
1112 if (isci_sata_get_sat_protocol(isci_request) == SAT_PROTOCOL_FPDMA)
1113 new_state = SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ;
1115 sci_dev->working_request = request;
1116 new_state = SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD;
1118 sci_base_state_machine_change_state(&sci_dev->state_machine, new_state);
1120 scic_sds_remote_device_start_request(sci_dev, request, status);
1125 static enum sci_status scic_sds_stp_remote_device_ready_idle_substate_event_handler(
1126 struct scic_sds_remote_device *sci_dev,
1129 enum sci_status status;
1131 status = scic_sds_remote_device_general_event_handler(sci_dev, event_code);
1132 if (status != SCI_SUCCESS)
1135 /* We pick up suspension events to handle specifically to this state. We
1136 * resume the RNC right away. enum sci_status
1138 if (scu_get_event_type(event_code) == SCU_EVENT_TYPE_RNC_SUSPEND_TX ||
1139 scu_get_event_type(event_code) == SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX)
1140 status = scic_sds_remote_node_context_resume(&sci_dev->rnc, NULL, NULL);
1145 static enum sci_status scic_sds_stp_remote_device_ready_ncq_substate_start_io_handler(
1146 struct scic_sds_remote_device *sci_dev,
1147 struct scic_sds_request *request)
1149 enum sci_status status;
1150 struct isci_request *isci_request = request->ireq;
1151 scic_sds_port_io_request_handler_t start_io;
1153 if (isci_sata_get_sat_protocol(isci_request) == SAT_PROTOCOL_FPDMA) {
1154 start_io = sci_dev->owning_port->state_handlers->start_io_handler;
1155 status = start_io(sci_dev->owning_port, sci_dev, request);
1156 if (status != SCI_SUCCESS)
1159 status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, request);
1160 if (status == SCI_SUCCESS)
1161 status = request->state_handlers->start_handler(request);
1163 scic_sds_remote_device_start_request(sci_dev, request, status);
1165 status = SCI_FAILURE_INVALID_STATE;
1170 static enum sci_status scic_sds_stp_remote_device_ready_ncq_substate_frame_handler(struct scic_sds_remote_device *sci_dev,
1173 enum sci_status status;
1174 struct sata_fis_header *frame_header;
1175 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
1177 status = scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
1179 (void **)&frame_header);
1180 if (status != SCI_SUCCESS)
1183 if (frame_header->fis_type == SATA_FIS_TYPE_SETDEVBITS &&
1184 (frame_header->status & ATA_STATUS_REG_ERROR_BIT)) {
1185 sci_dev->not_ready_reason = SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED;
1187 /* TODO Check sactive and complete associated IO if any. */
1189 sci_base_state_machine_change_state(&sci_dev->state_machine,
1190 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR);
1191 } else if (frame_header->fis_type == SATA_FIS_TYPE_REGD2H &&
1192 (frame_header->status & ATA_STATUS_REG_ERROR_BIT)) {
1194 * Some devices return D2H FIS when an NCQ error is detected.
1195 * Treat this like an SDB error FIS ready reason.
1197 sci_dev->not_ready_reason = SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED;
1199 sci_base_state_machine_change_state(&sci_dev->state_machine,
1200 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR);
1202 status = SCI_FAILURE;
1204 scic_sds_controller_release_frame(scic, frame_index);
1209 static enum sci_status scic_sds_stp_remote_device_ready_cmd_substate_start_io_handler(
1210 struct scic_sds_remote_device *device,
1211 struct scic_sds_request *request)
1213 /* device is already handling a command it can not accept new commands
1214 * until this one is complete.
1216 return SCI_FAILURE_INVALID_STATE;
1219 static enum sci_status scic_sds_stp_remote_device_ready_cmd_substate_suspend_handler(
1220 struct scic_sds_remote_device *sci_dev,
1223 enum sci_status status;
1225 status = scic_sds_remote_node_context_suspend(&sci_dev->rnc,
1226 suspend_type, NULL, NULL);
1231 static enum sci_status scic_sds_stp_remote_device_ready_cmd_substate_frame_handler(
1232 struct scic_sds_remote_device *sci_dev,
1235 /* The device doe not process any UF received from the hardware while
1236 * in this state. All unsolicited frames are forwarded to the io
1239 return scic_sds_io_request_frame_handler(sci_dev->working_request,
1243 static enum sci_status scic_sds_stp_remote_device_ready_await_reset_substate_start_io_handler(
1244 struct scic_sds_remote_device *device,
1245 struct scic_sds_request *request)
1247 return SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED;
1250 static enum sci_status scic_sds_stp_remote_device_ready_await_reset_substate_complete_request_handler(
1251 struct scic_sds_remote_device *device,
1252 struct scic_sds_request *request)
1254 struct scic_sds_request *sci_req = request;
1255 enum sci_status status;
1257 status = scic_sds_io_request_complete(sci_req);
1258 if (status != SCI_SUCCESS)
1261 status = scic_sds_port_complete_io(device->owning_port, device, sci_req);
1262 if (status != SCI_SUCCESS)
1265 scic_sds_remote_device_decrement_request_count(device);
1267 if (status != SCI_SUCCESS)
1268 dev_err(scirdev_to_dev(device),
1269 "%s: Port:0x%p Device:0x%p Request:0x%p Status:0x%x "
1270 "could not complete\n",
1271 __func__, device->owning_port, device, sci_req, status);
1276 static void scic_sds_stp_remote_device_ready_idle_substate_resume_complete_handler(void *_dev)
1278 struct scic_sds_remote_device *sci_dev = _dev;
1279 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
1280 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
1282 /* For NCQ operation we do not issue a isci_remote_device_not_ready().
1283 * As a result, avoid sending the ready notification.
1285 if (sci_dev->state_machine.previous_state_id != SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ)
1286 isci_remote_device_ready(scic->ihost, idev);
1289 static enum sci_status scic_sds_smp_remote_device_ready_idle_substate_start_io_handler(
1290 struct scic_sds_remote_device *sci_dev,
1291 struct scic_sds_request *sci_req)
1293 enum sci_status status;
1295 /* Will the port allow the io request to start? */
1296 status = sci_dev->owning_port->state_handlers->start_io_handler(
1297 sci_dev->owning_port, sci_dev, sci_req);
1298 if (status != SCI_SUCCESS)
1301 status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, sci_req);
1302 if (status != SCI_SUCCESS)
1305 status = scic_sds_request_start(sci_req);
1306 if (status != SCI_SUCCESS)
1309 sci_dev->working_request = sci_req;
1310 sci_base_state_machine_change_state(&sci_dev->state_machine,
1311 SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD);
1314 scic_sds_remote_device_start_request(sci_dev, sci_req, status);
1319 static enum sci_status scic_sds_smp_remote_device_ready_cmd_substate_start_io_handler(
1320 struct scic_sds_remote_device *device,
1321 struct scic_sds_request *request)
1323 /* device is already handling a command it can not accept new commands
1324 * until this one is complete.
1326 return SCI_FAILURE_INVALID_STATE;
1329 static enum sci_status
1330 scic_sds_smp_remote_device_ready_cmd_substate_complete_io_handler(struct scic_sds_remote_device *sci_dev,
1331 struct scic_sds_request *sci_req)
1333 enum sci_status status;
1335 status = scic_sds_io_request_complete(sci_req);
1336 if (status != SCI_SUCCESS)
1339 status = scic_sds_port_complete_io(sci_dev->owning_port, sci_dev, sci_req);
1340 if (status != SCI_SUCCESS) {
1341 dev_err(scirdev_to_dev(sci_dev),
1342 "%s: SCIC SDS Remote Device 0x%p io request "
1343 "0x%p could not be completd on the port 0x%p "
1344 "failed with status %d.\n", __func__, sci_dev, sci_req,
1345 sci_dev->owning_port, status);
1349 scic_sds_remote_device_decrement_request_count(sci_dev);
1350 sci_base_state_machine_change_state(&sci_dev->state_machine,
1351 SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
1356 static enum sci_status scic_sds_smp_remote_device_ready_cmd_substate_frame_handler(
1357 struct scic_sds_remote_device *sci_dev,
1360 enum sci_status status;
1362 /* The device does not process any UF received from the hardware while
1363 * in this state. All unsolicited frames are forwarded to the io request
1366 status = scic_sds_io_request_frame_handler(
1367 sci_dev->working_request,
1374 static const struct scic_sds_remote_device_state_handler scic_sds_remote_device_state_handler_table[] = {
1375 [SCI_BASE_REMOTE_DEVICE_STATE_INITIAL] = {
1376 .start_handler = scic_sds_remote_device_default_start_handler,
1377 .stop_handler = scic_sds_remote_device_default_stop_handler,
1378 .fail_handler = scic_sds_remote_device_default_fail_handler,
1379 .destruct_handler = scic_sds_remote_device_default_destruct_handler,
1380 .reset_handler = scic_sds_remote_device_default_reset_handler,
1381 .reset_complete_handler = scic_sds_remote_device_default_reset_complete_handler,
1382 .start_io_handler = scic_sds_remote_device_default_start_request_handler,
1383 .complete_io_handler = scic_sds_remote_device_default_complete_request_handler,
1384 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
1385 .start_task_handler = scic_sds_remote_device_default_start_request_handler,
1386 .complete_task_handler = scic_sds_remote_device_default_complete_request_handler,
1387 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1388 .resume_handler = scic_sds_remote_device_default_resume_handler,
1389 .event_handler = scic_sds_remote_device_default_event_handler,
1390 .frame_handler = scic_sds_remote_device_default_frame_handler
1392 [SCI_BASE_REMOTE_DEVICE_STATE_STOPPED] = {
1393 .start_handler = scic_sds_remote_device_stopped_state_start_handler,
1394 .stop_handler = scic_sds_remote_device_stopped_state_stop_handler,
1395 .fail_handler = scic_sds_remote_device_default_fail_handler,
1396 .destruct_handler = scic_sds_remote_device_stopped_state_destruct_handler,
1397 .reset_handler = scic_sds_remote_device_default_reset_handler,
1398 .reset_complete_handler = scic_sds_remote_device_default_reset_complete_handler,
1399 .start_io_handler = scic_sds_remote_device_default_start_request_handler,
1400 .complete_io_handler = scic_sds_remote_device_default_complete_request_handler,
1401 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
1402 .start_task_handler = scic_sds_remote_device_default_start_request_handler,
1403 .complete_task_handler = scic_sds_remote_device_default_complete_request_handler,
1404 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1405 .resume_handler = scic_sds_remote_device_default_resume_handler,
1406 .event_handler = scic_sds_remote_device_default_event_handler,
1407 .frame_handler = scic_sds_remote_device_default_frame_handler
1409 [SCI_BASE_REMOTE_DEVICE_STATE_STARTING] = {
1410 .start_handler = scic_sds_remote_device_default_start_handler,
1411 .stop_handler = scic_sds_remote_device_starting_state_stop_handler,
1412 .fail_handler = scic_sds_remote_device_default_fail_handler,
1413 .destruct_handler = scic_sds_remote_device_default_destruct_handler,
1414 .reset_handler = scic_sds_remote_device_default_reset_handler,
1415 .reset_complete_handler = scic_sds_remote_device_default_reset_complete_handler,
1416 .start_io_handler = scic_sds_remote_device_default_start_request_handler,
1417 .complete_io_handler = scic_sds_remote_device_default_complete_request_handler,
1418 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
1419 .start_task_handler = scic_sds_remote_device_default_start_request_handler,
1420 .complete_task_handler = scic_sds_remote_device_default_complete_request_handler,
1421 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1422 .resume_handler = scic_sds_remote_device_default_resume_handler,
1423 .event_handler = scic_sds_remote_device_general_event_handler,
1424 .frame_handler = scic_sds_remote_device_default_frame_handler
1426 [SCI_BASE_REMOTE_DEVICE_STATE_READY] = {
1427 .start_handler = scic_sds_remote_device_default_start_handler,
1428 .stop_handler = scic_sds_remote_device_ready_state_stop_handler,
1429 .fail_handler = scic_sds_remote_device_default_fail_handler,
1430 .destruct_handler = scic_sds_remote_device_default_destruct_handler,
1431 .reset_handler = scic_sds_remote_device_ready_state_reset_handler,
1432 .reset_complete_handler = scic_sds_remote_device_default_reset_complete_handler,
1433 .start_io_handler = scic_sds_remote_device_ready_state_start_io_handler,
1434 .complete_io_handler = scic_sds_remote_device_ready_state_complete_request_handler,
1435 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
1436 .start_task_handler = scic_sds_remote_device_ready_state_start_task_handler,
1437 .complete_task_handler = scic_sds_remote_device_ready_state_complete_request_handler,
1438 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1439 .resume_handler = scic_sds_remote_device_default_resume_handler,
1440 .event_handler = scic_sds_remote_device_general_event_handler,
1441 .frame_handler = scic_sds_remote_device_general_frame_handler,
1443 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE] = {
1444 .start_handler = scic_sds_remote_device_default_start_handler,
1445 .stop_handler = scic_sds_remote_device_ready_state_stop_handler,
1446 .fail_handler = scic_sds_remote_device_default_fail_handler,
1447 .destruct_handler = scic_sds_remote_device_default_destruct_handler,
1448 .reset_handler = scic_sds_remote_device_ready_state_reset_handler,
1449 .reset_complete_handler = scic_sds_remote_device_default_reset_complete_handler,
1450 .start_io_handler = scic_sds_stp_remote_device_ready_idle_substate_start_io_handler,
1451 .complete_io_handler = scic_sds_remote_device_default_complete_request_handler,
1452 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
1453 .start_task_handler = scic_sds_stp_remote_device_ready_substate_start_request_handler,
1454 .complete_task_handler = scic_sds_remote_device_default_complete_request_handler,
1455 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1456 .resume_handler = scic_sds_remote_device_default_resume_handler,
1457 .event_handler = scic_sds_stp_remote_device_ready_idle_substate_event_handler,
1458 .frame_handler = scic_sds_remote_device_default_frame_handler
1460 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD] = {
1461 .start_handler = scic_sds_remote_device_default_start_handler,
1462 .stop_handler = scic_sds_remote_device_ready_state_stop_handler,
1463 .fail_handler = scic_sds_remote_device_default_fail_handler,
1464 .destruct_handler = scic_sds_remote_device_default_destruct_handler,
1465 .reset_handler = scic_sds_remote_device_ready_state_reset_handler,
1466 .reset_complete_handler = scic_sds_remote_device_default_reset_complete_handler,
1467 .start_io_handler = scic_sds_stp_remote_device_ready_cmd_substate_start_io_handler,
1468 .complete_io_handler = scic_sds_stp_remote_device_complete_request,
1469 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
1470 .start_task_handler = scic_sds_stp_remote_device_ready_substate_start_request_handler,
1471 .complete_task_handler = scic_sds_stp_remote_device_complete_request,
1472 .suspend_handler = scic_sds_stp_remote_device_ready_cmd_substate_suspend_handler,
1473 .resume_handler = scic_sds_remote_device_default_resume_handler,
1474 .event_handler = scic_sds_remote_device_general_event_handler,
1475 .frame_handler = scic_sds_stp_remote_device_ready_cmd_substate_frame_handler
1477 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ] = {
1478 .start_handler = scic_sds_remote_device_default_start_handler,
1479 .stop_handler = scic_sds_remote_device_ready_state_stop_handler,
1480 .fail_handler = scic_sds_remote_device_default_fail_handler,
1481 .destruct_handler = scic_sds_remote_device_default_destruct_handler,
1482 .reset_handler = scic_sds_remote_device_ready_state_reset_handler,
1483 .reset_complete_handler = scic_sds_remote_device_default_reset_complete_handler,
1484 .start_io_handler = scic_sds_stp_remote_device_ready_ncq_substate_start_io_handler,
1485 .complete_io_handler = scic_sds_stp_remote_device_complete_request,
1486 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
1487 .start_task_handler = scic_sds_stp_remote_device_ready_substate_start_request_handler,
1488 .complete_task_handler = scic_sds_stp_remote_device_complete_request,
1489 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1490 .resume_handler = scic_sds_remote_device_default_resume_handler,
1491 .event_handler = scic_sds_remote_device_general_event_handler,
1492 .frame_handler = scic_sds_stp_remote_device_ready_ncq_substate_frame_handler
1494 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR] = {
1495 .start_handler = scic_sds_remote_device_default_start_handler,
1496 .stop_handler = scic_sds_remote_device_ready_state_stop_handler,
1497 .fail_handler = scic_sds_remote_device_default_fail_handler,
1498 .destruct_handler = scic_sds_remote_device_default_destruct_handler,
1499 .reset_handler = scic_sds_remote_device_ready_state_reset_handler,
1500 .reset_complete_handler = scic_sds_remote_device_default_reset_complete_handler,
1501 .start_io_handler = scic_sds_remote_device_default_start_request_handler,
1502 .complete_io_handler = scic_sds_stp_remote_device_complete_request,
1503 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
1504 .start_task_handler = scic_sds_stp_remote_device_ready_substate_start_request_handler,
1505 .complete_task_handler = scic_sds_stp_remote_device_complete_request,
1506 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1507 .resume_handler = scic_sds_remote_device_default_resume_handler,
1508 .event_handler = scic_sds_remote_device_general_event_handler,
1509 .frame_handler = scic_sds_remote_device_general_frame_handler
1511 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET] = {
1512 .start_handler = scic_sds_remote_device_default_start_handler,
1513 .stop_handler = scic_sds_remote_device_ready_state_stop_handler,
1514 .fail_handler = scic_sds_remote_device_default_fail_handler,
1515 .destruct_handler = scic_sds_remote_device_default_destruct_handler,
1516 .reset_handler = scic_sds_remote_device_ready_state_reset_handler,
1517 .reset_complete_handler = scic_sds_remote_device_default_reset_complete_handler,
1518 .start_io_handler = scic_sds_stp_remote_device_ready_await_reset_substate_start_io_handler,
1519 .complete_io_handler = scic_sds_stp_remote_device_ready_await_reset_substate_complete_request_handler,
1520 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
1521 .start_task_handler = scic_sds_stp_remote_device_ready_substate_start_request_handler,
1522 .complete_task_handler = scic_sds_stp_remote_device_complete_request,
1523 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1524 .resume_handler = scic_sds_remote_device_default_resume_handler,
1525 .event_handler = scic_sds_remote_device_general_event_handler,
1526 .frame_handler = scic_sds_remote_device_general_frame_handler
1528 [SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE] = {
1529 .start_handler = scic_sds_remote_device_default_start_handler,
1530 .stop_handler = scic_sds_remote_device_ready_state_stop_handler,
1531 .fail_handler = scic_sds_remote_device_default_fail_handler,
1532 .destruct_handler = scic_sds_remote_device_default_destruct_handler,
1533 .reset_handler = scic_sds_remote_device_default_reset_handler,
1534 .reset_complete_handler = scic_sds_remote_device_default_reset_complete_handler,
1535 .start_io_handler = scic_sds_smp_remote_device_ready_idle_substate_start_io_handler,
1536 .complete_io_handler = scic_sds_remote_device_default_complete_request_handler,
1537 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
1538 .start_task_handler = scic_sds_remote_device_default_start_request_handler,
1539 .complete_task_handler = scic_sds_remote_device_default_complete_request_handler,
1540 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1541 .resume_handler = scic_sds_remote_device_default_resume_handler,
1542 .event_handler = scic_sds_remote_device_general_event_handler,
1543 .frame_handler = scic_sds_remote_device_default_frame_handler
1545 [SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD] = {
1546 .start_handler = scic_sds_remote_device_default_start_handler,
1547 .stop_handler = scic_sds_remote_device_ready_state_stop_handler,
1548 .fail_handler = scic_sds_remote_device_default_fail_handler,
1549 .destruct_handler = scic_sds_remote_device_default_destruct_handler,
1550 .reset_handler = scic_sds_remote_device_default_reset_handler,
1551 .reset_complete_handler = scic_sds_remote_device_default_reset_complete_handler,
1552 .start_io_handler = scic_sds_smp_remote_device_ready_cmd_substate_start_io_handler,
1553 .complete_io_handler = scic_sds_smp_remote_device_ready_cmd_substate_complete_io_handler,
1554 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
1555 .start_task_handler = scic_sds_remote_device_default_start_request_handler,
1556 .complete_task_handler = scic_sds_remote_device_default_complete_request_handler,
1557 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1558 .resume_handler = scic_sds_remote_device_default_resume_handler,
1559 .event_handler = scic_sds_remote_device_general_event_handler,
1560 .frame_handler = scic_sds_smp_remote_device_ready_cmd_substate_frame_handler
1562 [SCI_BASE_REMOTE_DEVICE_STATE_STOPPING] = {
1563 .start_handler = scic_sds_remote_device_default_start_handler,
1564 .stop_handler = scic_sds_remote_device_stopping_state_stop_handler,
1565 .fail_handler = scic_sds_remote_device_default_fail_handler,
1566 .destruct_handler = scic_sds_remote_device_default_destruct_handler,
1567 .reset_handler = scic_sds_remote_device_default_reset_handler,
1568 .reset_complete_handler = scic_sds_remote_device_default_reset_complete_handler,
1569 .start_io_handler = scic_sds_remote_device_default_start_request_handler,
1570 .complete_io_handler = scic_sds_remote_device_stopping_state_complete_request_handler,
1571 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
1572 .start_task_handler = scic_sds_remote_device_default_start_request_handler,
1573 .complete_task_handler = scic_sds_remote_device_stopping_state_complete_request_handler,
1574 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1575 .resume_handler = scic_sds_remote_device_default_resume_handler,
1576 .event_handler = scic_sds_remote_device_general_event_handler,
1577 .frame_handler = scic_sds_remote_device_general_frame_handler
1579 [SCI_BASE_REMOTE_DEVICE_STATE_FAILED] = {
1580 .start_handler = scic_sds_remote_device_default_start_handler,
1581 .stop_handler = scic_sds_remote_device_default_stop_handler,
1582 .fail_handler = scic_sds_remote_device_default_fail_handler,
1583 .destruct_handler = scic_sds_remote_device_default_destruct_handler,
1584 .reset_handler = scic_sds_remote_device_default_reset_handler,
1585 .reset_complete_handler = scic_sds_remote_device_default_reset_complete_handler,
1586 .start_io_handler = scic_sds_remote_device_default_start_request_handler,
1587 .complete_io_handler = scic_sds_remote_device_default_complete_request_handler,
1588 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
1589 .start_task_handler = scic_sds_remote_device_default_start_request_handler,
1590 .complete_task_handler = scic_sds_remote_device_default_complete_request_handler,
1591 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1592 .resume_handler = scic_sds_remote_device_default_resume_handler,
1593 .event_handler = scic_sds_remote_device_default_event_handler,
1594 .frame_handler = scic_sds_remote_device_general_frame_handler
1596 [SCI_BASE_REMOTE_DEVICE_STATE_RESETTING] = {
1597 .start_handler = scic_sds_remote_device_default_start_handler,
1598 .stop_handler = scic_sds_remote_device_resetting_state_stop_handler,
1599 .fail_handler = scic_sds_remote_device_default_fail_handler,
1600 .destruct_handler = scic_sds_remote_device_default_destruct_handler,
1601 .reset_handler = scic_sds_remote_device_default_reset_handler,
1602 .reset_complete_handler = scic_sds_remote_device_resetting_state_reset_complete_handler,
1603 .start_io_handler = scic_sds_remote_device_default_start_request_handler,
1604 .complete_io_handler = scic_sds_remote_device_resetting_state_complete_request_handler,
1605 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
1606 .start_task_handler = scic_sds_remote_device_default_start_request_handler,
1607 .complete_task_handler = scic_sds_remote_device_resetting_state_complete_request_handler,
1608 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1609 .resume_handler = scic_sds_remote_device_default_resume_handler,
1610 .event_handler = scic_sds_remote_device_default_event_handler,
1611 .frame_handler = scic_sds_remote_device_general_frame_handler
1613 [SCI_BASE_REMOTE_DEVICE_STATE_FINAL] = {
1614 .start_handler = scic_sds_remote_device_default_start_handler,
1615 .stop_handler = scic_sds_remote_device_default_stop_handler,
1616 .fail_handler = scic_sds_remote_device_default_fail_handler,
1617 .destruct_handler = scic_sds_remote_device_default_destruct_handler,
1618 .reset_handler = scic_sds_remote_device_default_reset_handler,
1619 .reset_complete_handler = scic_sds_remote_device_default_reset_complete_handler,
1620 .start_io_handler = scic_sds_remote_device_default_start_request_handler,
1621 .complete_io_handler = scic_sds_remote_device_default_complete_request_handler,
1622 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
1623 .start_task_handler = scic_sds_remote_device_default_start_request_handler,
1624 .complete_task_handler = scic_sds_remote_device_default_complete_request_handler,
1625 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1626 .resume_handler = scic_sds_remote_device_default_resume_handler,
1627 .event_handler = scic_sds_remote_device_default_event_handler,
1628 .frame_handler = scic_sds_remote_device_default_frame_handler
1632 static void scic_sds_remote_device_initial_state_enter(void *object)
1634 struct scic_sds_remote_device *sci_dev = object;
1636 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1637 SCI_BASE_REMOTE_DEVICE_STATE_INITIAL);
1639 /* Initial state is a transitional state to the stopped state */
1640 sci_base_state_machine_change_state(&sci_dev->state_machine,
1641 SCI_BASE_REMOTE_DEVICE_STATE_STOPPED);
1645 * scic_remote_device_destruct() - free remote node context and destruct
1646 * @remote_device: This parameter specifies the remote device to be destructed.
1648 * Remote device objects are a limited resource. As such, they must be
1649 * protected. Thus calls to construct and destruct are mutually exclusive and
1650 * non-reentrant. The return value shall indicate if the device was
1651 * successfully destructed or if some failure occurred. enum sci_status This value
1652 * is returned if the device is successfully destructed.
1653 * SCI_FAILURE_INVALID_REMOTE_DEVICE This value is returned if the supplied
1654 * device isn't valid (e.g. it's already been destoryed, the handle isn't
1657 static enum sci_status scic_remote_device_destruct(struct scic_sds_remote_device *sci_dev)
1659 return sci_dev->state_handlers->destruct_handler(sci_dev);
1663 * isci_remote_device_deconstruct() - This function frees an isci_remote_device.
1664 * @ihost: This parameter specifies the isci host object.
1665 * @idev: This parameter specifies the remote device to be freed.
1668 static void isci_remote_device_deconstruct(struct isci_host *ihost, struct isci_remote_device *idev)
1670 dev_dbg(&ihost->pdev->dev,
1671 "%s: isci_device = %p\n", __func__, idev);
1673 /* There should not be any outstanding io's. All paths to
1674 * here should go through isci_remote_device_nuke_requests.
1675 * If we hit this condition, we will need a way to complete
1676 * io requests in process */
1677 while (!list_empty(&idev->reqs_in_process)) {
1679 dev_err(&ihost->pdev->dev,
1680 "%s: ** request list not empty! **\n", __func__);
1684 scic_remote_device_destruct(&idev->sci);
1685 idev->domain_dev->lldd_dev = NULL;
1686 idev->domain_dev = NULL;
1687 idev->isci_port = NULL;
1688 list_del_init(&idev->node);
1690 clear_bit(IDEV_START_PENDING, &idev->flags);
1691 clear_bit(IDEV_STOP_PENDING, &idev->flags);
1692 wake_up(&ihost->eventq);
1696 * isci_remote_device_stop_complete() - This function is called by the scic
1697 * when the remote device stop has completed. We mark the isci device as not
1698 * ready and remove the isci remote device.
1699 * @ihost: This parameter specifies the isci host object.
1700 * @idev: This parameter specifies the remote device.
1701 * @status: This parameter specifies status of the completion.
1704 static void isci_remote_device_stop_complete(struct isci_host *ihost,
1705 struct isci_remote_device *idev)
1707 dev_dbg(&ihost->pdev->dev, "%s: complete idev = %p\n", __func__, idev);
1709 isci_remote_device_change_state(idev, isci_stopped);
1711 /* after stop, we can tear down resources. */
1712 isci_remote_device_deconstruct(ihost, idev);
1715 static void scic_sds_remote_device_stopped_state_enter(void *object)
1717 struct scic_sds_remote_device *sci_dev = object;
1718 struct scic_sds_controller *scic;
1719 struct isci_remote_device *idev;
1720 struct isci_host *ihost;
1723 scic = scic_sds_remote_device_get_controller(sci_dev);
1724 ihost = scic->ihost;
1725 idev = sci_dev_to_idev(sci_dev);
1727 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1728 SCI_BASE_REMOTE_DEVICE_STATE_STOPPED);
1730 /* If we are entering from the stopping state let the SCI User know that
1731 * the stop operation has completed.
1733 prev_state = sci_dev->state_machine.previous_state_id;
1734 if (prev_state == SCI_BASE_REMOTE_DEVICE_STATE_STOPPING)
1735 isci_remote_device_stop_complete(ihost, idev);
1737 scic_sds_controller_remote_device_stopped(scic, sci_dev);
1740 static void scic_sds_remote_device_starting_state_enter(void *object)
1742 struct scic_sds_remote_device *sci_dev = object;
1743 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1744 struct isci_host *ihost = scic->ihost;
1745 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
1747 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1748 SCI_BASE_REMOTE_DEVICE_STATE_STARTING);
1750 isci_remote_device_not_ready(ihost, idev,
1751 SCIC_REMOTE_DEVICE_NOT_READY_START_REQUESTED);
1754 static void scic_sds_remote_device_ready_state_enter(void *object)
1756 struct scic_sds_remote_device *sci_dev = object;
1757 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
1758 struct domain_device *dev = sci_dev_to_domain(sci_dev);
1760 SET_STATE_HANDLER(sci_dev,
1761 scic_sds_remote_device_state_handler_table,
1762 SCI_BASE_REMOTE_DEVICE_STATE_READY);
1764 scic->remote_device_sequence[sci_dev->rnc.remote_node_index]++;
1766 if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_SATA)) {
1767 sci_base_state_machine_change_state(&sci_dev->state_machine,
1768 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
1769 } else if (dev_is_expander(dev)) {
1770 sci_base_state_machine_change_state(&sci_dev->state_machine,
1771 SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
1773 isci_remote_device_ready(scic->ihost, sci_dev_to_idev(sci_dev));
1776 static void scic_sds_remote_device_ready_state_exit(void *object)
1778 struct scic_sds_remote_device *sci_dev = object;
1779 struct domain_device *dev = sci_dev_to_domain(sci_dev);
1781 if (dev->dev_type == SAS_END_DEV) {
1782 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
1783 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
1785 isci_remote_device_not_ready(scic->ihost, idev,
1786 SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED);
1790 static void scic_sds_remote_device_stopping_state_enter(void *object)
1792 struct scic_sds_remote_device *sci_dev = object;
1796 scic_sds_remote_device_state_handler_table,
1797 SCI_BASE_REMOTE_DEVICE_STATE_STOPPING
1801 static void scic_sds_remote_device_failed_state_enter(void *object)
1803 struct scic_sds_remote_device *sci_dev = object;
1807 scic_sds_remote_device_state_handler_table,
1808 SCI_BASE_REMOTE_DEVICE_STATE_FAILED
1812 static void scic_sds_remote_device_resetting_state_enter(void *object)
1814 struct scic_sds_remote_device *sci_dev = object;
1818 scic_sds_remote_device_state_handler_table,
1819 SCI_BASE_REMOTE_DEVICE_STATE_RESETTING
1822 scic_sds_remote_node_context_suspend(
1823 &sci_dev->rnc, SCI_SOFTWARE_SUSPENSION, NULL, NULL);
1826 static void scic_sds_remote_device_resetting_state_exit(void *object)
1828 struct scic_sds_remote_device *sci_dev = object;
1830 scic_sds_remote_node_context_resume(&sci_dev->rnc, NULL, NULL);
1833 static void scic_sds_remote_device_final_state_enter(void *object)
1835 struct scic_sds_remote_device *sci_dev = object;
1839 scic_sds_remote_device_state_handler_table,
1840 SCI_BASE_REMOTE_DEVICE_STATE_FINAL
1844 static void scic_sds_stp_remote_device_ready_idle_substate_enter(void *object)
1846 struct scic_sds_remote_device *sci_dev = object;
1848 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1849 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
1851 sci_dev->working_request = NULL;
1852 if (scic_sds_remote_node_context_is_ready(&sci_dev->rnc)) {
1854 * Since the RNC is ready, it's alright to finish completion
1855 * processing (e.g. signal the remote device is ready). */
1856 scic_sds_stp_remote_device_ready_idle_substate_resume_complete_handler(sci_dev);
1858 scic_sds_remote_node_context_resume(&sci_dev->rnc,
1859 scic_sds_stp_remote_device_ready_idle_substate_resume_complete_handler,
1864 static void scic_sds_stp_remote_device_ready_cmd_substate_enter(void *object)
1866 struct scic_sds_remote_device *sci_dev = object;
1867 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1869 BUG_ON(sci_dev->working_request == NULL);
1871 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1872 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD);
1874 isci_remote_device_not_ready(scic->ihost, sci_dev_to_idev(sci_dev),
1875 SCIC_REMOTE_DEVICE_NOT_READY_SATA_REQUEST_STARTED);
1878 static void scic_sds_stp_remote_device_ready_ncq_substate_enter(void *object)
1880 struct scic_sds_remote_device *sci_dev = object;
1882 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1883 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ);
1886 static void scic_sds_stp_remote_device_ready_ncq_error_substate_enter(void *object)
1888 struct scic_sds_remote_device *sci_dev = object;
1889 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1890 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
1892 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1893 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR);
1895 if (sci_dev->not_ready_reason == SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED)
1896 isci_remote_device_not_ready(scic->ihost, idev,
1897 sci_dev->not_ready_reason);
1900 static void scic_sds_stp_remote_device_ready_await_reset_substate_enter(void *object)
1902 struct scic_sds_remote_device *sci_dev = object;
1904 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1905 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET);
1908 static void scic_sds_smp_remote_device_ready_idle_substate_enter(void *object)
1910 struct scic_sds_remote_device *sci_dev = object;
1911 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1913 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1914 SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
1916 isci_remote_device_ready(scic->ihost, sci_dev_to_idev(sci_dev));
1919 static void scic_sds_smp_remote_device_ready_cmd_substate_enter(void *object)
1921 struct scic_sds_remote_device *sci_dev = object;
1922 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1924 BUG_ON(sci_dev->working_request == NULL);
1926 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1927 SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD);
1929 isci_remote_device_not_ready(scic->ihost, sci_dev_to_idev(sci_dev),
1930 SCIC_REMOTE_DEVICE_NOT_READY_SMP_REQUEST_STARTED);
1933 static void scic_sds_smp_remote_device_ready_cmd_substate_exit(void *object)
1935 struct scic_sds_remote_device *sci_dev = object;
1937 sci_dev->working_request = NULL;
1940 static const struct sci_base_state scic_sds_remote_device_state_table[] = {
1941 [SCI_BASE_REMOTE_DEVICE_STATE_INITIAL] = {
1942 .enter_state = scic_sds_remote_device_initial_state_enter,
1944 [SCI_BASE_REMOTE_DEVICE_STATE_STOPPED] = {
1945 .enter_state = scic_sds_remote_device_stopped_state_enter,
1947 [SCI_BASE_REMOTE_DEVICE_STATE_STARTING] = {
1948 .enter_state = scic_sds_remote_device_starting_state_enter,
1950 [SCI_BASE_REMOTE_DEVICE_STATE_READY] = {
1951 .enter_state = scic_sds_remote_device_ready_state_enter,
1952 .exit_state = scic_sds_remote_device_ready_state_exit
1954 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE] = {
1955 .enter_state = scic_sds_stp_remote_device_ready_idle_substate_enter,
1957 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD] = {
1958 .enter_state = scic_sds_stp_remote_device_ready_cmd_substate_enter,
1960 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ] = {
1961 .enter_state = scic_sds_stp_remote_device_ready_ncq_substate_enter,
1963 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR] = {
1964 .enter_state = scic_sds_stp_remote_device_ready_ncq_error_substate_enter,
1966 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET] = {
1967 .enter_state = scic_sds_stp_remote_device_ready_await_reset_substate_enter,
1969 [SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE] = {
1970 .enter_state = scic_sds_smp_remote_device_ready_idle_substate_enter,
1972 [SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD] = {
1973 .enter_state = scic_sds_smp_remote_device_ready_cmd_substate_enter,
1974 .exit_state = scic_sds_smp_remote_device_ready_cmd_substate_exit,
1976 [SCI_BASE_REMOTE_DEVICE_STATE_STOPPING] = {
1977 .enter_state = scic_sds_remote_device_stopping_state_enter,
1979 [SCI_BASE_REMOTE_DEVICE_STATE_FAILED] = {
1980 .enter_state = scic_sds_remote_device_failed_state_enter,
1982 [SCI_BASE_REMOTE_DEVICE_STATE_RESETTING] = {
1983 .enter_state = scic_sds_remote_device_resetting_state_enter,
1984 .exit_state = scic_sds_remote_device_resetting_state_exit
1986 [SCI_BASE_REMOTE_DEVICE_STATE_FINAL] = {
1987 .enter_state = scic_sds_remote_device_final_state_enter,
1992 * scic_remote_device_construct() - common construction
1993 * @sci_port: SAS/SATA port through which this device is accessed.
1994 * @sci_dev: remote device to construct
1996 * This routine just performs benign initialization and does not
1997 * allocate the remote_node_context which is left to
1998 * scic_remote_device_[de]a_construct(). scic_remote_device_destruct()
1999 * frees the remote_node_context(s) for the device.
2001 static void scic_remote_device_construct(struct scic_sds_port *sci_port,
2002 struct scic_sds_remote_device *sci_dev)
2004 sci_dev->owning_port = sci_port;
2005 sci_dev->started_request_count = 0;
2007 sci_base_state_machine_construct(
2008 &sci_dev->state_machine,
2010 scic_sds_remote_device_state_table,
2011 SCI_BASE_REMOTE_DEVICE_STATE_INITIAL
2014 sci_base_state_machine_start(
2015 &sci_dev->state_machine
2018 scic_sds_remote_node_context_construct(&sci_dev->rnc,
2019 SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX);
2023 * scic_remote_device_da_construct() - construct direct attached device.
2025 * The information (e.g. IAF, Signature FIS, etc.) necessary to build
2026 * the device is known to the SCI Core since it is contained in the
2027 * scic_phy object. Remote node context(s) is/are a global resource
2028 * allocated by this routine, freed by scic_remote_device_destruct().
2031 * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
2032 * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
2033 * sata-only controller instance.
2034 * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
2036 static enum sci_status scic_remote_device_da_construct(struct scic_sds_port *sci_port,
2037 struct scic_sds_remote_device *sci_dev)
2039 enum sci_status status;
2040 struct domain_device *dev = sci_dev_to_domain(sci_dev);
2042 scic_remote_device_construct(sci_port, sci_dev);
2045 * This information is request to determine how many remote node context
2046 * entries will be needed to store the remote node.
2048 sci_dev->is_direct_attached = true;
2049 status = scic_sds_controller_allocate_remote_node_context(sci_port->owning_controller,
2051 &sci_dev->rnc.remote_node_index);
2053 if (status != SCI_SUCCESS)
2056 if (dev->dev_type == SAS_END_DEV || dev->dev_type == SATA_DEV ||
2057 (dev->tproto & SAS_PROTOCOL_STP) || dev_is_expander(dev))
2060 return SCI_FAILURE_UNSUPPORTED_PROTOCOL;
2062 sci_dev->connection_rate = scic_sds_port_get_max_allowed_speed(sci_port);
2064 /* / @todo Should I assign the port width by reading all of the phys on the port? */
2065 sci_dev->device_port_width = 1;
2071 * scic_remote_device_ea_construct() - construct expander attached device
2073 * Remote node context(s) is/are a global resource allocated by this
2074 * routine, freed by scic_remote_device_destruct().
2077 * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
2078 * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
2079 * sata-only controller instance.
2080 * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
2082 static enum sci_status scic_remote_device_ea_construct(struct scic_sds_port *sci_port,
2083 struct scic_sds_remote_device *sci_dev)
2085 struct domain_device *dev = sci_dev_to_domain(sci_dev);
2086 enum sci_status status;
2088 scic_remote_device_construct(sci_port, sci_dev);
2090 status = scic_sds_controller_allocate_remote_node_context(sci_port->owning_controller,
2092 &sci_dev->rnc.remote_node_index);
2093 if (status != SCI_SUCCESS)
2096 if (dev->dev_type == SAS_END_DEV || dev->dev_type == SATA_DEV ||
2097 (dev->tproto & SAS_PROTOCOL_STP) || dev_is_expander(dev))
2100 return SCI_FAILURE_UNSUPPORTED_PROTOCOL;
2103 * For SAS-2 the physical link rate is actually a logical link
2104 * rate that incorporates multiplexing. The SCU doesn't
2105 * incorporate multiplexing and for the purposes of the
2106 * connection the logical link rate is that same as the
2107 * physical. Furthermore, the SAS-2 and SAS-1.1 fields overlay
2108 * one another, so this code works for both situations. */
2109 sci_dev->connection_rate = min_t(u16, scic_sds_port_get_max_allowed_speed(sci_port),
2112 /* / @todo Should I assign the port width by reading all of the phys on the port? */
2113 sci_dev->device_port_width = 1;
2119 * scic_remote_device_start() - This method will start the supplied remote
2120 * device. This method enables normal IO requests to flow through to the
2122 * @remote_device: This parameter specifies the device to be started.
2123 * @timeout: This parameter specifies the number of milliseconds in which the
2124 * start operation should complete.
2126 * An indication of whether the device was successfully started. SCI_SUCCESS
2127 * This value is returned if the device was successfully started.
2128 * SCI_FAILURE_INVALID_PHY This value is returned if the user attempts to start
2129 * the device when there have been no phys added to it.
2131 static enum sci_status scic_remote_device_start(struct scic_sds_remote_device *sci_dev,
2134 return sci_dev->state_handlers->start_handler(sci_dev);
2137 static enum sci_status isci_remote_device_construct(struct isci_port *iport,
2138 struct isci_remote_device *idev)
2140 struct scic_sds_port *sci_port = iport->sci_port_handle;
2141 struct isci_host *ihost = iport->isci_host;
2142 struct domain_device *dev = idev->domain_dev;
2143 enum sci_status status;
2145 if (dev->parent && dev_is_expander(dev->parent))
2146 status = scic_remote_device_ea_construct(sci_port, &idev->sci);
2148 status = scic_remote_device_da_construct(sci_port, &idev->sci);
2150 if (status != SCI_SUCCESS) {
2151 dev_dbg(&ihost->pdev->dev, "%s: construct failed: %d\n",
2157 /* start the device. */
2158 status = scic_remote_device_start(&idev->sci, ISCI_REMOTE_DEVICE_START_TIMEOUT);
2160 if (status != SCI_SUCCESS)
2161 dev_warn(&ihost->pdev->dev, "remote device start failed: %d\n",
2167 void isci_remote_device_nuke_requests(struct isci_host *ihost, struct isci_remote_device *idev)
2169 DECLARE_COMPLETION_ONSTACK(aborted_task_completion);
2171 dev_dbg(&ihost->pdev->dev,
2172 "%s: idev = %p\n", __func__, idev);
2174 /* Cleanup all requests pending for this device. */
2175 isci_terminate_pending_requests(ihost, idev, terminating);
2177 dev_dbg(&ihost->pdev->dev,
2178 "%s: idev = %p, done\n", __func__, idev);
2182 * This function builds the isci_remote_device when a libsas dev_found message
2184 * @isci_host: This parameter specifies the isci host object.
2185 * @port: This parameter specifies the isci_port conected to this device.
2187 * pointer to new isci_remote_device.
2189 static struct isci_remote_device *
2190 isci_remote_device_alloc(struct isci_host *ihost, struct isci_port *iport)
2192 struct isci_remote_device *idev;
2195 for (i = 0; i < SCI_MAX_REMOTE_DEVICES; i++) {
2196 idev = &ihost->devices[i];
2197 if (!test_and_set_bit(IDEV_ALLOCATED, &idev->flags))
2201 if (i >= SCI_MAX_REMOTE_DEVICES) {
2202 dev_warn(&ihost->pdev->dev, "%s: failed\n", __func__);
2206 if (WARN_ONCE(!list_empty(&idev->reqs_in_process), "found requests in process\n"))
2209 if (WARN_ONCE(!list_empty(&idev->node), "found non-idle remote device\n"))
2212 isci_remote_device_change_state(idev, isci_freed);
2218 * isci_remote_device_stop() - This function is called internally to stop the
2220 * @isci_host: This parameter specifies the isci host object.
2221 * @isci_device: This parameter specifies the remote device.
2223 * The status of the scic request to stop.
2225 enum sci_status isci_remote_device_stop(struct isci_host *ihost, struct isci_remote_device *idev)
2227 enum sci_status status;
2228 unsigned long flags;
2230 dev_dbg(&ihost->pdev->dev,
2231 "%s: isci_device = %p\n", __func__, idev);
2233 isci_remote_device_change_state(idev, isci_stopping);
2235 /* Kill all outstanding requests. */
2236 isci_remote_device_nuke_requests(ihost, idev);
2238 set_bit(IDEV_STOP_PENDING, &idev->flags);
2240 spin_lock_irqsave(&ihost->scic_lock, flags);
2241 status = scic_remote_device_stop(&idev->sci, 50);
2242 spin_unlock_irqrestore(&ihost->scic_lock, flags);
2244 /* Wait for the stop complete callback. */
2245 if (status == SCI_SUCCESS) {
2246 wait_for_device_stop(ihost, idev);
2247 clear_bit(IDEV_ALLOCATED, &idev->flags);
2250 dev_dbg(&ihost->pdev->dev,
2251 "%s: idev = %p - after completion wait\n",
2258 * isci_remote_device_gone() - This function is called by libsas when a domain
2259 * device is removed.
2260 * @domain_device: This parameter specifies the libsas domain device.
2263 void isci_remote_device_gone(struct domain_device *dev)
2265 struct isci_host *ihost = dev_to_ihost(dev);
2266 struct isci_remote_device *idev = dev->lldd_dev;
2268 dev_dbg(&ihost->pdev->dev,
2269 "%s: domain_device = %p, isci_device = %p, isci_port = %p\n",
2270 __func__, dev, idev, idev->isci_port);
2272 isci_remote_device_stop(ihost, idev);
2277 * isci_remote_device_found() - This function is called by libsas when a remote
2278 * device is discovered. A remote device object is created and started. the
2279 * function then sleeps until the sci core device started message is
2281 * @domain_device: This parameter specifies the libsas domain device.
2283 * status, zero indicates success.
2285 int isci_remote_device_found(struct domain_device *domain_dev)
2287 struct isci_host *isci_host = dev_to_ihost(domain_dev);
2288 struct isci_port *isci_port;
2289 struct isci_phy *isci_phy;
2290 struct asd_sas_port *sas_port;
2291 struct asd_sas_phy *sas_phy;
2292 struct isci_remote_device *isci_device;
2293 enum sci_status status;
2295 dev_dbg(&isci_host->pdev->dev,
2296 "%s: domain_device = %p\n", __func__, domain_dev);
2298 wait_for_start(isci_host);
2300 sas_port = domain_dev->port;
2301 sas_phy = list_first_entry(&sas_port->phy_list, struct asd_sas_phy,
2303 isci_phy = to_isci_phy(sas_phy);
2304 isci_port = isci_phy->isci_port;
2306 /* we are being called for a device on this port,
2307 * so it has to come up eventually
2309 wait_for_completion(&isci_port->start_complete);
2311 if ((isci_stopping == isci_port_get_state(isci_port)) ||
2312 (isci_stopped == isci_port_get_state(isci_port)))
2315 isci_device = isci_remote_device_alloc(isci_host, isci_port);
2319 INIT_LIST_HEAD(&isci_device->node);
2320 domain_dev->lldd_dev = isci_device;
2321 isci_device->domain_dev = domain_dev;
2322 isci_device->isci_port = isci_port;
2323 isci_remote_device_change_state(isci_device, isci_starting);
2326 spin_lock_irq(&isci_host->scic_lock);
2327 list_add_tail(&isci_device->node, &isci_port->remote_dev_list);
2329 set_bit(IDEV_START_PENDING, &isci_device->flags);
2330 status = isci_remote_device_construct(isci_port, isci_device);
2331 spin_unlock_irq(&isci_host->scic_lock);
2333 dev_dbg(&isci_host->pdev->dev,
2334 "%s: isci_device = %p\n",
2335 __func__, isci_device);
2337 if (status != SCI_SUCCESS) {
2339 spin_lock_irq(&isci_host->scic_lock);
2340 isci_remote_device_deconstruct(
2344 spin_unlock_irq(&isci_host->scic_lock);
2348 /* wait for the device ready callback. */
2349 wait_for_device_start(isci_host, isci_device);
2354 * isci_device_is_reset_pending() - This function will check if there is any
2355 * pending reset condition on the device.
2356 * @request: This parameter is the isci_device object.
2358 * true if there is a reset pending for the device.
2360 bool isci_device_is_reset_pending(
2361 struct isci_host *isci_host,
2362 struct isci_remote_device *isci_device)
2364 struct isci_request *isci_request;
2365 struct isci_request *tmp_req;
2366 bool reset_is_pending = false;
2367 unsigned long flags;
2369 dev_dbg(&isci_host->pdev->dev,
2370 "%s: isci_device = %p\n", __func__, isci_device);
2372 spin_lock_irqsave(&isci_host->scic_lock, flags);
2374 /* Check for reset on all pending requests. */
2375 list_for_each_entry_safe(isci_request, tmp_req,
2376 &isci_device->reqs_in_process, dev_node) {
2377 dev_dbg(&isci_host->pdev->dev,
2378 "%s: isci_device = %p request = %p\n",
2379 __func__, isci_device, isci_request);
2381 if (isci_request->ttype == io_task) {
2382 struct sas_task *task = isci_request_access_task(
2385 spin_lock(&task->task_state_lock);
2386 if (task->task_state_flags & SAS_TASK_NEED_DEV_RESET)
2387 reset_is_pending = true;
2388 spin_unlock(&task->task_state_lock);
2392 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
2394 dev_dbg(&isci_host->pdev->dev,
2395 "%s: isci_device = %p reset_is_pending = %d\n",
2396 __func__, isci_device, reset_is_pending);
2398 return reset_is_pending;
2402 * isci_device_clear_reset_pending() - This function will clear if any pending
2403 * reset condition flags on the device.
2404 * @request: This parameter is the isci_device object.
2406 * true if there is a reset pending for the device.
2408 void isci_device_clear_reset_pending(struct isci_host *ihost, struct isci_remote_device *idev)
2410 struct isci_request *isci_request;
2411 struct isci_request *tmp_req;
2412 unsigned long flags = 0;
2414 dev_dbg(&ihost->pdev->dev, "%s: idev=%p, ihost=%p\n",
2415 __func__, idev, ihost);
2417 spin_lock_irqsave(&ihost->scic_lock, flags);
2419 /* Clear reset pending on all pending requests. */
2420 list_for_each_entry_safe(isci_request, tmp_req,
2421 &idev->reqs_in_process, dev_node) {
2422 dev_dbg(&ihost->pdev->dev, "%s: idev = %p request = %p\n",
2423 __func__, idev, isci_request);
2425 if (isci_request->ttype == io_task) {
2427 unsigned long flags2;
2428 struct sas_task *task = isci_request_access_task(
2431 spin_lock_irqsave(&task->task_state_lock, flags2);
2432 task->task_state_flags &= ~SAS_TASK_NEED_DEV_RESET;
2433 spin_unlock_irqrestore(&task->task_state_lock, flags2);
2436 spin_unlock_irqrestore(&ihost->scic_lock, flags);