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"
58 #include "remote_device.h"
60 #include "scic_controller.h"
61 #include "scic_io_request.h"
63 #include "scic_port.h"
64 #include "scic_sds_controller.h"
65 #include "scic_sds_phy.h"
66 #include "scic_sds_port.h"
67 #include "remote_node_context.h"
68 #include "scic_sds_request.h"
69 #include "sci_environment.h"
71 #include "scu_event_codes.h"
74 enum sci_status scic_remote_device_stop(
75 struct scic_sds_remote_device *sci_dev,
78 return sci_dev->state_handlers->stop_handler(sci_dev);
82 enum sci_status scic_remote_device_reset(
83 struct scic_sds_remote_device *sci_dev)
85 return sci_dev->state_handlers->reset_handler(sci_dev);
89 enum sci_status scic_remote_device_reset_complete(
90 struct scic_sds_remote_device *sci_dev)
92 return sci_dev->state_handlers->reset_complete_handler(sci_dev);
96 enum sas_linkrate scic_remote_device_get_connection_rate(
97 struct scic_sds_remote_device *sci_dev)
99 return sci_dev->connection_rate;
105 * Remote device timer requirements
107 #define SCIC_SDS_REMOTE_DEVICE_MINIMUM_TIMER_COUNT (0)
108 #define SCIC_SDS_REMOTE_DEVICE_MAXIMUM_TIMER_COUNT (SCI_MAX_REMOTE_DEVICES)
113 * @sci_dev: The remote device for which the suspend is being requested.
115 * This method invokes the remote device suspend state handler. enum sci_status
117 enum sci_status scic_sds_remote_device_suspend(
118 struct scic_sds_remote_device *sci_dev,
121 return sci_dev->state_handlers->suspend_handler(sci_dev, suspend_type);
126 * @sci_dev: The remote device for which the event handling is being
128 * @frame_index: This is the frame index that is being processed.
130 * This method invokes the frame handler for the remote device state machine
133 enum sci_status scic_sds_remote_device_frame_handler(
134 struct scic_sds_remote_device *sci_dev,
137 return sci_dev->state_handlers->frame_handler(sci_dev, frame_index);
142 * @sci_dev: The remote device for which the event handling is being
144 * @event_code: This is the event code that is to be processed.
146 * This method invokes the remote device event handler. enum sci_status
148 enum sci_status scic_sds_remote_device_event_handler(
149 struct scic_sds_remote_device *sci_dev,
152 return sci_dev->state_handlers->event_handler(sci_dev, event_code);
157 * @controller: The controller that is starting the io request.
158 * @sci_dev: The remote device for which the start io handling is being
160 * @io_request: The io request that is being started.
162 * This method invokes the remote device start io handler. enum sci_status
164 enum sci_status scic_sds_remote_device_start_io(
165 struct scic_sds_controller *controller,
166 struct scic_sds_remote_device *sci_dev,
167 struct scic_sds_request *io_request)
169 return sci_dev->state_handlers->start_io_handler(
170 sci_dev, io_request);
175 * @controller: The controller that is completing the io request.
176 * @sci_dev: The remote device for which the complete io handling is being
178 * @io_request: The io request that is being completed.
180 * This method invokes the remote device complete io handler. enum sci_status
182 enum sci_status scic_sds_remote_device_complete_io(
183 struct scic_sds_controller *controller,
184 struct scic_sds_remote_device *sci_dev,
185 struct scic_sds_request *io_request)
187 return sci_dev->state_handlers->complete_io_handler(
188 sci_dev, io_request);
193 * @controller: The controller that is starting the task request.
194 * @sci_dev: The remote device for which the start task handling is being
196 * @io_request: The task request that is being started.
198 * This method invokes the remote device start task handler. enum sci_status
200 enum sci_status scic_sds_remote_device_start_task(
201 struct scic_sds_controller *controller,
202 struct scic_sds_remote_device *sci_dev,
203 struct scic_sds_request *io_request)
205 return sci_dev->state_handlers->start_task_handler(
206 sci_dev, io_request);
214 * This method takes the request and bulids an appropriate SCU context for the
215 * request and then requests the controller to post the request. none
217 void scic_sds_remote_device_post_request(
218 struct scic_sds_remote_device *sci_dev,
223 context = scic_sds_remote_device_build_command_context(sci_dev, request);
225 scic_sds_controller_post_request(
226 scic_sds_remote_device_get_controller(sci_dev),
233 * @user_parameter: This is cast to a remote device object.
235 * This method is called once the remote node context is ready to be freed.
236 * The remote device can now report that its stop operation is complete. none
238 static void scic_sds_cb_remote_device_rnc_destruct_complete(
239 void *user_parameter)
241 struct scic_sds_remote_device *sci_dev;
243 sci_dev = (struct scic_sds_remote_device *)user_parameter;
245 BUG_ON(sci_dev->started_request_count != 0);
247 sci_base_state_machine_change_state(&sci_dev->state_machine,
248 SCI_BASE_REMOTE_DEVICE_STATE_STOPPED);
253 * @user_parameter: This is cast to a remote device object.
255 * This method is called once the remote node context has transisitioned to a
256 * ready state. This is the indication that the remote device object can also
257 * transition to ready. none
259 static void scic_sds_remote_device_resume_complete_handler(
260 void *user_parameter)
262 struct scic_sds_remote_device *sci_dev;
264 sci_dev = (struct scic_sds_remote_device *)user_parameter;
267 sci_base_state_machine_get_state(&sci_dev->state_machine)
268 != SCI_BASE_REMOTE_DEVICE_STATE_READY
270 sci_base_state_machine_change_state(
271 &sci_dev->state_machine,
272 SCI_BASE_REMOTE_DEVICE_STATE_READY
279 * @device: This parameter specifies the device for which the request is being
281 * @request: This parameter specifies the request being started.
282 * @status: This parameter specifies the current start operation status.
284 * This method will perform the STP request start processing common to IO
285 * requests and task requests of all types. none
287 void scic_sds_remote_device_start_request(
288 struct scic_sds_remote_device *sci_dev,
289 struct scic_sds_request *sci_req,
290 enum sci_status status)
292 /* We still have a fault in starting the io complete it on the port */
293 if (status == SCI_SUCCESS)
294 scic_sds_remote_device_increment_request_count(sci_dev);
296 sci_dev->owning_port->state_handlers->complete_io_handler(
297 sci_dev->owning_port, sci_dev, sci_req
305 * @request: This parameter specifies the request being continued.
307 * This method will continue to post tc for a STP request. This method usually
308 * serves as a callback when RNC gets resumed during a task management
311 void scic_sds_remote_device_continue_request(void *dev)
313 struct scic_sds_remote_device *sci_dev = dev;
315 /* we need to check if this request is still valid to continue. */
316 if (sci_dev->working_request)
317 scic_controller_continue_io(sci_dev->working_request);
321 * This method will terminate all of the IO requests in the controllers IO
322 * request table that were targeted for this device.
323 * @sci_dev: This parameter specifies the remote device for which to
324 * attempt to terminate all requests.
326 * This method returns an indication as to whether all requests were
327 * successfully terminated. If a single request fails to be terminated, then
328 * this method will return the failure.
330 static enum sci_status scic_sds_remote_device_terminate_requests(
331 struct scic_sds_remote_device *sci_dev)
333 enum sci_status status = SCI_SUCCESS;
334 enum sci_status terminate_status = SCI_SUCCESS;
335 struct scic_sds_request *sci_req;
337 u32 request_count = sci_dev->started_request_count;
340 (index < SCI_MAX_IO_REQUESTS) && (request_count > 0);
342 sci_req = sci_dev->owning_port->owning_controller->io_request_table[index];
344 if ((sci_req != NULL) && (sci_req->target_device == sci_dev)) {
345 terminate_status = scic_controller_terminate_request(
346 sci_dev->owning_port->owning_controller,
351 if (terminate_status != SCI_SUCCESS)
352 status = terminate_status;
361 static enum sci_status
362 default_device_handler(struct scic_sds_remote_device *sci_dev,
365 dev_warn(scirdev_to_dev(sci_dev),
366 "%s: in wrong state: %d\n", func,
367 sci_base_state_machine_get_state(&sci_dev->state_machine));
368 return SCI_FAILURE_INVALID_STATE;
371 enum sci_status scic_sds_remote_device_default_start_handler(
372 struct scic_sds_remote_device *sci_dev)
374 return default_device_handler(sci_dev, __func__);
377 static enum sci_status scic_sds_remote_device_default_stop_handler(
378 struct scic_sds_remote_device *sci_dev)
380 return default_device_handler(sci_dev, __func__);
383 enum sci_status scic_sds_remote_device_default_fail_handler(
384 struct scic_sds_remote_device *sci_dev)
386 return default_device_handler(sci_dev, __func__);
389 enum sci_status scic_sds_remote_device_default_destruct_handler(
390 struct scic_sds_remote_device *sci_dev)
392 return default_device_handler(sci_dev, __func__);
395 enum sci_status scic_sds_remote_device_default_reset_handler(
396 struct scic_sds_remote_device *sci_dev)
398 return default_device_handler(sci_dev, __func__);
401 enum sci_status scic_sds_remote_device_default_reset_complete_handler(
402 struct scic_sds_remote_device *sci_dev)
404 return default_device_handler(sci_dev, __func__);
407 enum sci_status scic_sds_remote_device_default_suspend_handler(
408 struct scic_sds_remote_device *sci_dev, u32 suspend_type)
410 return default_device_handler(sci_dev, __func__);
413 enum sci_status scic_sds_remote_device_default_resume_handler(
414 struct scic_sds_remote_device *sci_dev)
416 return default_device_handler(sci_dev, __func__);
421 * @device: The struct scic_sds_remote_device which is then cast into a
422 * struct scic_sds_remote_device.
423 * @event_code: The event code that the struct scic_sds_controller wants the device
426 * This method is the default event handler. It will call the RNC state
427 * machine handler for any RNC events otherwise it will log a warning and
428 * returns a failure. enum sci_status SCI_FAILURE_INVALID_STATE
430 static enum sci_status scic_sds_remote_device_core_event_handler(
431 struct scic_sds_remote_device *sci_dev,
435 enum sci_status status;
437 switch (scu_get_event_type(event_code)) {
438 case SCU_EVENT_TYPE_RNC_OPS_MISC:
439 case SCU_EVENT_TYPE_RNC_SUSPEND_TX:
440 case SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX:
441 status = scic_sds_remote_node_context_event_handler(&sci_dev->rnc, event_code);
443 case SCU_EVENT_TYPE_PTX_SCHEDULE_EVENT:
445 if (scu_get_event_code(event_code) == SCU_EVENT_IT_NEXUS_TIMEOUT) {
446 status = SCI_SUCCESS;
448 /* Suspend the associated RNC */
449 scic_sds_remote_node_context_suspend(&sci_dev->rnc,
450 SCI_SOFTWARE_SUSPENSION,
453 dev_dbg(scirdev_to_dev(sci_dev),
454 "%s: device: %p event code: %x: %s\n",
455 __func__, sci_dev, event_code,
457 ? "I_T_Nexus_Timeout event"
458 : "I_T_Nexus_Timeout event in wrong state");
462 /* Else, fall through and treat as unhandled... */
465 dev_dbg(scirdev_to_dev(sci_dev),
466 "%s: device: %p event code: %x: %s\n",
467 __func__, sci_dev, event_code,
470 : "unexpected event in wrong state");
471 status = SCI_FAILURE_INVALID_STATE;
479 * @device: The struct scic_sds_remote_device which is then cast into a
480 * struct scic_sds_remote_device.
481 * @event_code: The event code that the struct scic_sds_controller wants the device
484 * This method is the default event handler. It will call the RNC state
485 * machine handler for any RNC events otherwise it will log a warning and
486 * returns a failure. enum sci_status SCI_FAILURE_INVALID_STATE
488 static enum sci_status scic_sds_remote_device_default_event_handler(
489 struct scic_sds_remote_device *sci_dev,
492 return scic_sds_remote_device_core_event_handler(sci_dev,
499 * @device: The struct scic_sds_remote_device which is then cast into a
500 * struct scic_sds_remote_device.
501 * @frame_index: The frame index for which the struct scic_sds_controller wants this
502 * device object to process.
504 * This method is the default unsolicited frame handler. It logs a warning,
505 * releases the frame and returns a failure. enum sci_status
506 * SCI_FAILURE_INVALID_STATE
508 enum sci_status scic_sds_remote_device_default_frame_handler(
509 struct scic_sds_remote_device *sci_dev,
512 dev_warn(scirdev_to_dev(sci_dev),
513 "%s: SCIC Remote Device requested to handle frame %x "
514 "while in wrong state %d\n",
517 sci_base_state_machine_get_state(
518 &sci_dev->state_machine));
520 /* Return the frame back to the controller */
521 scic_sds_controller_release_frame(
522 scic_sds_remote_device_get_controller(sci_dev), frame_index
525 return SCI_FAILURE_INVALID_STATE;
528 enum sci_status scic_sds_remote_device_default_start_request_handler(
529 struct scic_sds_remote_device *sci_dev,
530 struct scic_sds_request *request)
532 return default_device_handler(sci_dev, __func__);
535 enum sci_status scic_sds_remote_device_default_complete_request_handler(
536 struct scic_sds_remote_device *sci_dev,
537 struct scic_sds_request *request)
539 return default_device_handler(sci_dev, __func__);
542 enum sci_status scic_sds_remote_device_default_continue_request_handler(
543 struct scic_sds_remote_device *sci_dev,
544 struct scic_sds_request *request)
546 return default_device_handler(sci_dev, __func__);
551 * @device: The struct scic_sds_remote_device which is then cast into a
552 * struct scic_sds_remote_device.
553 * @frame_index: The frame index for which the struct scic_sds_controller wants this
554 * device object to process.
556 * This method is a general ssp frame handler. In most cases the device object
557 * needs to route the unsolicited frame processing to the io request object.
558 * This method decodes the tag for the io request object and routes the
559 * unsolicited frame to that object. enum sci_status SCI_FAILURE_INVALID_STATE
561 enum sci_status scic_sds_remote_device_general_frame_handler(
562 struct scic_sds_remote_device *sci_dev,
565 enum sci_status result;
566 struct sci_ssp_frame_header *frame_header;
567 struct scic_sds_request *io_request;
569 result = scic_sds_unsolicited_frame_control_get_header(
570 &(scic_sds_remote_device_get_controller(sci_dev)->uf_control),
572 (void **)&frame_header
575 if (SCI_SUCCESS == result) {
576 io_request = scic_sds_controller_get_io_request_from_tag(
577 scic_sds_remote_device_get_controller(sci_dev), frame_header->tag);
579 if ((io_request == NULL)
580 || (io_request->target_device != sci_dev)) {
582 * We could not map this tag to a valid IO request
583 * Just toss the frame and continue */
584 scic_sds_controller_release_frame(
585 scic_sds_remote_device_get_controller(sci_dev), frame_index
588 /* The IO request is now in charge of releasing the frame */
589 result = io_request->state_handlers->frame_handler(
590 io_request, frame_index);
599 * @[in]: sci_dev This is the device object that is receiving the event.
600 * @[in]: event_code The event code to process.
602 * This is a common method for handling events reported to the remote device
603 * from the controller object. enum sci_status
605 enum sci_status scic_sds_remote_device_general_event_handler(
606 struct scic_sds_remote_device *sci_dev,
609 return scic_sds_remote_device_core_event_handler(sci_dev,
615 * *****************************************************************************
616 * * STOPPED STATE HANDLERS
617 * ***************************************************************************** */
623 * This method takes the struct scic_sds_remote_device from a stopped state and
624 * attempts to start it. The RNC buffer for the device is constructed and the
625 * device state machine is transitioned to the
626 * SCIC_BASE_REMOTE_DEVICE_STATE_STARTING. enum sci_status SCI_SUCCESS if there is
627 * an RNC buffer available to construct the remote device.
628 * SCI_FAILURE_INSUFFICIENT_RESOURCES if there is no RNC buffer available in
629 * which to construct the remote device.
631 static enum sci_status scic_sds_remote_device_stopped_state_start_handler(
632 struct scic_sds_remote_device *sci_dev)
634 enum sci_status status;
636 status = scic_sds_remote_node_context_resume(&sci_dev->rnc,
637 scic_sds_remote_device_resume_complete_handler, sci_dev);
639 if (status == SCI_SUCCESS)
640 sci_base_state_machine_change_state(&sci_dev->state_machine,
641 SCI_BASE_REMOTE_DEVICE_STATE_STARTING);
646 static enum sci_status scic_sds_remote_device_stopped_state_stop_handler(
647 struct scic_sds_remote_device *sci_dev)
654 * @sci_dev: The struct scic_sds_remote_device which is cast into a
655 * struct scic_sds_remote_device.
657 * This method will destruct a struct scic_sds_remote_device that is in a stopped
658 * state. This is the only state from which a destruct request will succeed.
659 * The RNi for this struct scic_sds_remote_device is returned to the free pool and the
660 * device object transitions to the SCI_BASE_REMOTE_DEVICE_STATE_FINAL.
661 * enum sci_status SCI_SUCCESS
663 static enum sci_status scic_sds_remote_device_stopped_state_destruct_handler(
664 struct scic_sds_remote_device *sci_dev)
666 struct scic_sds_controller *scic;
668 scic = scic_sds_remote_device_get_controller(sci_dev);
669 scic_sds_controller_free_remote_node_context(scic, sci_dev,
670 sci_dev->rnc.remote_node_index);
671 sci_dev->rnc.remote_node_index = SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX;
673 sci_base_state_machine_change_state(&sci_dev->state_machine,
674 SCI_BASE_REMOTE_DEVICE_STATE_FINAL);
680 * *****************************************************************************
681 * * STARTING STATE HANDLERS
682 * ***************************************************************************** */
684 static enum sci_status scic_sds_remote_device_starting_state_stop_handler(
685 struct scic_sds_remote_device *sci_dev)
688 * This device has not yet started so there had better be no IO requests
690 BUG_ON(sci_dev->started_request_count != 0);
693 * Destroy the remote node context
695 scic_sds_remote_node_context_destruct(&sci_dev->rnc,
696 scic_sds_cb_remote_device_rnc_destruct_complete, sci_dev);
699 * Transition to the stopping state and wait for the remote node to
700 * complete being posted and invalidated.
702 sci_base_state_machine_change_state(&sci_dev->state_machine,
703 SCI_BASE_REMOTE_DEVICE_STATE_STOPPING);
708 enum sci_status scic_sds_remote_device_ready_state_stop_handler(
709 struct scic_sds_remote_device *sci_dev)
711 enum sci_status status = SCI_SUCCESS;
713 /* Request the parent state machine to transition to the stopping state */
714 sci_base_state_machine_change_state(&sci_dev->state_machine,
715 SCI_BASE_REMOTE_DEVICE_STATE_STOPPING);
717 if (sci_dev->started_request_count == 0) {
718 scic_sds_remote_node_context_destruct(&sci_dev->rnc,
719 scic_sds_cb_remote_device_rnc_destruct_complete,
722 status = scic_sds_remote_device_terminate_requests(sci_dev);
729 * @device: The struct scic_sds_remote_device object which is cast to a
730 * struct scic_sds_remote_device object.
732 * This is the ready state device reset handler enum sci_status
734 enum sci_status scic_sds_remote_device_ready_state_reset_handler(
735 struct scic_sds_remote_device *sci_dev)
737 /* Request the parent state machine to transition to the stopping state */
738 sci_base_state_machine_change_state(&sci_dev->state_machine,
739 SCI_BASE_REMOTE_DEVICE_STATE_RESETTING);
745 * This method will attempt to start a task request for this device object. The
746 * remote device object will issue the start request for the task and if
747 * successful it will start the request for the port object then increment its
748 * own requet count. enum sci_status SCI_SUCCESS if the task request is started for
749 * this device object. SCI_FAILURE_INSUFFICIENT_RESOURCES if the io request
750 * object could not get the resources to start.
752 static enum sci_status scic_sds_remote_device_ready_state_start_task_handler(
753 struct scic_sds_remote_device *sci_dev,
754 struct scic_sds_request *request)
756 enum sci_status result;
758 /* See if the port is in a state where we can start the IO request */
759 result = scic_sds_port_start_io(
760 scic_sds_remote_device_get_port(sci_dev), sci_dev, request);
762 if (result == SCI_SUCCESS) {
763 result = scic_sds_remote_node_context_start_task(&sci_dev->rnc,
765 if (result == SCI_SUCCESS)
766 result = scic_sds_request_start(request);
768 scic_sds_remote_device_start_request(sci_dev, request, result);
775 * This method will attempt to start an io request for this device object. The
776 * remote device object will issue the start request for the io and if
777 * successful it will start the request for the port object then increment its
778 * own requet count. enum sci_status SCI_SUCCESS if the io request is started for
779 * this device object. SCI_FAILURE_INSUFFICIENT_RESOURCES if the io request
780 * object could not get the resources to start.
782 static enum sci_status scic_sds_remote_device_ready_state_start_io_handler(
783 struct scic_sds_remote_device *sci_dev,
784 struct scic_sds_request *request)
786 enum sci_status result;
788 /* See if the port is in a state where we can start the IO request */
789 result = scic_sds_port_start_io(
790 scic_sds_remote_device_get_port(sci_dev), sci_dev, request);
792 if (result == SCI_SUCCESS) {
793 result = scic_sds_remote_node_context_start_io(&sci_dev->rnc, request);
794 if (result == SCI_SUCCESS)
795 result = scic_sds_request_start(request);
797 scic_sds_remote_device_start_request(sci_dev, request, result);
804 * This method will complete the request for the remote device object. The
805 * method will call the completion handler for the request object and if
806 * successful it will complete the request on the port object then decrement
807 * its own started_request_count. enum sci_status
809 static enum sci_status scic_sds_remote_device_ready_state_complete_request_handler(
810 struct scic_sds_remote_device *sci_dev,
811 struct scic_sds_request *request)
813 enum sci_status result;
815 result = scic_sds_request_complete(request);
817 if (result != SCI_SUCCESS)
820 /* See if the port is in a state
821 * where we can start the IO request */
822 result = scic_sds_port_complete_io(
823 scic_sds_remote_device_get_port(sci_dev),
826 if (result == SCI_SUCCESS)
827 scic_sds_remote_device_decrement_request_count(sci_dev);
833 * *****************************************************************************
834 * * STOPPING STATE HANDLERS
835 * ***************************************************************************** */
839 * @sci_dev: The struct scic_sds_remote_device which is cast into a
840 * struct scic_sds_remote_device.
842 * This method will stop a struct scic_sds_remote_device that is already in the
843 * SCI_BASE_REMOTE_DEVICE_STATE_STOPPING state. This is not considered an error
844 * since we allow a stop request on a device that is alreay stopping or
845 * stopped. enum sci_status SCI_SUCCESS
847 static enum sci_status scic_sds_remote_device_stopping_state_stop_handler(
848 struct scic_sds_remote_device *device)
851 * All requests should have been terminated, but if there is an
852 * attempt to stop a device already in the stopping state, then
853 * try again to terminate. */
854 return scic_sds_remote_device_terminate_requests(device);
860 * @device: The device object for which the request is completing.
861 * @request: The task request that is being completed.
863 * This method completes requests for this struct scic_sds_remote_device while it is
864 * in the SCI_BASE_REMOTE_DEVICE_STATE_STOPPING state. This method calls the
865 * complete method for the request object and if that is successful the port
866 * object is called to complete the task request. Then the device object itself
867 * completes the task request. If struct scic_sds_remote_device started_request_count
868 * goes to 0 and the invalidate RNC request has completed the device object can
869 * transition to the SCI_BASE_REMOTE_DEVICE_STATE_STOPPED. enum sci_status
871 static enum sci_status scic_sds_remote_device_stopping_state_complete_request_handler(
872 struct scic_sds_remote_device *sci_dev,
873 struct scic_sds_request *request)
875 enum sci_status status = SCI_SUCCESS;
877 status = scic_sds_request_complete(request);
879 if (status != SCI_SUCCESS)
882 status = scic_sds_port_complete_io(scic_sds_remote_device_get_port(sci_dev),
884 if (status != SCI_SUCCESS)
887 scic_sds_remote_device_decrement_request_count(sci_dev);
889 if (scic_sds_remote_device_get_request_count(sci_dev) == 0)
890 scic_sds_remote_node_context_destruct(&sci_dev->rnc,
891 scic_sds_cb_remote_device_rnc_destruct_complete,
898 * @device: The struct scic_sds_remote_device which is to be cast into a
899 * struct scic_sds_remote_device object.
901 * This method will complete the reset operation when the device is in the
902 * resetting state. enum sci_status
904 static enum sci_status scic_sds_remote_device_resetting_state_reset_complete_handler(
905 struct scic_sds_remote_device *sci_dev)
908 sci_base_state_machine_change_state(
909 &sci_dev->state_machine,
910 SCI_BASE_REMOTE_DEVICE_STATE_READY
918 * @device: The struct scic_sds_remote_device which is to be cast into a
919 * struct scic_sds_remote_device object.
921 * This method will stop the remote device while in the resetting state.
924 static enum sci_status scic_sds_remote_device_resetting_state_stop_handler(
925 struct scic_sds_remote_device *sci_dev)
927 sci_base_state_machine_change_state(
928 &sci_dev->state_machine,
929 SCI_BASE_REMOTE_DEVICE_STATE_STOPPING
936 * This method completes requests for this struct scic_sds_remote_device while it is
937 * in the SCI_BASE_REMOTE_DEVICE_STATE_RESETTING state. This method calls the
938 * complete method for the request object and if that is successful the port
939 * object is called to complete the task request. Then the device object itself
940 * completes the task request. enum sci_status
942 static enum sci_status scic_sds_remote_device_resetting_state_complete_request_handler(
943 struct scic_sds_remote_device *sci_dev,
944 struct scic_sds_request *request)
946 enum sci_status status = SCI_SUCCESS;
948 status = scic_sds_request_complete(request);
950 if (status == SCI_SUCCESS) {
951 status = scic_sds_port_complete_io(
952 scic_sds_remote_device_get_port(sci_dev),
955 if (status == SCI_SUCCESS) {
956 scic_sds_remote_device_decrement_request_count(sci_dev);
963 static const struct scic_sds_remote_device_state_handler scic_sds_remote_device_state_handler_table[] = {
964 [SCI_BASE_REMOTE_DEVICE_STATE_INITIAL] = {
965 .start_handler = scic_sds_remote_device_default_start_handler,
966 .stop_handler = scic_sds_remote_device_default_stop_handler,
967 .fail_handler = scic_sds_remote_device_default_fail_handler,
968 .destruct_handler = scic_sds_remote_device_default_destruct_handler,
969 .reset_handler = scic_sds_remote_device_default_reset_handler,
970 .reset_complete_handler = scic_sds_remote_device_default_reset_complete_handler,
971 .start_io_handler = scic_sds_remote_device_default_start_request_handler,
972 .complete_io_handler = scic_sds_remote_device_default_complete_request_handler,
973 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
974 .start_task_handler = scic_sds_remote_device_default_start_request_handler,
975 .complete_task_handler = scic_sds_remote_device_default_complete_request_handler,
976 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
977 .resume_handler = scic_sds_remote_device_default_resume_handler,
978 .event_handler = scic_sds_remote_device_default_event_handler,
979 .frame_handler = scic_sds_remote_device_default_frame_handler
981 [SCI_BASE_REMOTE_DEVICE_STATE_STOPPED] = {
982 .start_handler = scic_sds_remote_device_stopped_state_start_handler,
983 .stop_handler = scic_sds_remote_device_stopped_state_stop_handler,
984 .fail_handler = scic_sds_remote_device_default_fail_handler,
985 .destruct_handler = scic_sds_remote_device_stopped_state_destruct_handler,
986 .reset_handler = scic_sds_remote_device_default_reset_handler,
987 .reset_complete_handler = scic_sds_remote_device_default_reset_complete_handler,
988 .start_io_handler = scic_sds_remote_device_default_start_request_handler,
989 .complete_io_handler = scic_sds_remote_device_default_complete_request_handler,
990 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
991 .start_task_handler = scic_sds_remote_device_default_start_request_handler,
992 .complete_task_handler = scic_sds_remote_device_default_complete_request_handler,
993 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
994 .resume_handler = scic_sds_remote_device_default_resume_handler,
995 .event_handler = scic_sds_remote_device_default_event_handler,
996 .frame_handler = scic_sds_remote_device_default_frame_handler
998 [SCI_BASE_REMOTE_DEVICE_STATE_STARTING] = {
999 .start_handler = scic_sds_remote_device_default_start_handler,
1000 .stop_handler = scic_sds_remote_device_starting_state_stop_handler,
1001 .fail_handler = scic_sds_remote_device_default_fail_handler,
1002 .destruct_handler = scic_sds_remote_device_default_destruct_handler,
1003 .reset_handler = scic_sds_remote_device_default_reset_handler,
1004 .reset_complete_handler = scic_sds_remote_device_default_reset_complete_handler,
1005 .start_io_handler = scic_sds_remote_device_default_start_request_handler,
1006 .complete_io_handler = scic_sds_remote_device_default_complete_request_handler,
1007 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
1008 .start_task_handler = scic_sds_remote_device_default_start_request_handler,
1009 .complete_task_handler = scic_sds_remote_device_default_complete_request_handler,
1010 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1011 .resume_handler = scic_sds_remote_device_default_resume_handler,
1012 .event_handler = scic_sds_remote_device_general_event_handler,
1013 .frame_handler = scic_sds_remote_device_default_frame_handler
1015 [SCI_BASE_REMOTE_DEVICE_STATE_READY] = {
1016 .start_handler = scic_sds_remote_device_default_start_handler,
1017 .stop_handler = scic_sds_remote_device_ready_state_stop_handler,
1018 .fail_handler = scic_sds_remote_device_default_fail_handler,
1019 .destruct_handler = scic_sds_remote_device_default_destruct_handler,
1020 .reset_handler = scic_sds_remote_device_ready_state_reset_handler,
1021 .reset_complete_handler = scic_sds_remote_device_default_reset_complete_handler,
1022 .start_io_handler = scic_sds_remote_device_ready_state_start_io_handler,
1023 .complete_io_handler = scic_sds_remote_device_ready_state_complete_request_handler,
1024 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
1025 .start_task_handler = scic_sds_remote_device_ready_state_start_task_handler,
1026 .complete_task_handler = scic_sds_remote_device_ready_state_complete_request_handler,
1027 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1028 .resume_handler = scic_sds_remote_device_default_resume_handler,
1029 .event_handler = scic_sds_remote_device_general_event_handler,
1030 .frame_handler = scic_sds_remote_device_general_frame_handler,
1032 [SCI_BASE_REMOTE_DEVICE_STATE_STOPPING] = {
1033 .start_handler = scic_sds_remote_device_default_start_handler,
1034 .stop_handler = scic_sds_remote_device_stopping_state_stop_handler,
1035 .fail_handler = scic_sds_remote_device_default_fail_handler,
1036 .destruct_handler = scic_sds_remote_device_default_destruct_handler,
1037 .reset_handler = scic_sds_remote_device_default_reset_handler,
1038 .reset_complete_handler = scic_sds_remote_device_default_reset_complete_handler,
1039 .start_io_handler = scic_sds_remote_device_default_start_request_handler,
1040 .complete_io_handler = scic_sds_remote_device_stopping_state_complete_request_handler,
1041 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
1042 .start_task_handler = scic_sds_remote_device_default_start_request_handler,
1043 .complete_task_handler = scic_sds_remote_device_stopping_state_complete_request_handler,
1044 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1045 .resume_handler = scic_sds_remote_device_default_resume_handler,
1046 .event_handler = scic_sds_remote_device_general_event_handler,
1047 .frame_handler = scic_sds_remote_device_general_frame_handler
1049 [SCI_BASE_REMOTE_DEVICE_STATE_FAILED] = {
1050 .start_handler = scic_sds_remote_device_default_start_handler,
1051 .stop_handler = scic_sds_remote_device_default_stop_handler,
1052 .fail_handler = scic_sds_remote_device_default_fail_handler,
1053 .destruct_handler = scic_sds_remote_device_default_destruct_handler,
1054 .reset_handler = scic_sds_remote_device_default_reset_handler,
1055 .reset_complete_handler = scic_sds_remote_device_default_reset_complete_handler,
1056 .start_io_handler = scic_sds_remote_device_default_start_request_handler,
1057 .complete_io_handler = scic_sds_remote_device_default_complete_request_handler,
1058 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
1059 .start_task_handler = scic_sds_remote_device_default_start_request_handler,
1060 .complete_task_handler = scic_sds_remote_device_default_complete_request_handler,
1061 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1062 .resume_handler = scic_sds_remote_device_default_resume_handler,
1063 .event_handler = scic_sds_remote_device_default_event_handler,
1064 .frame_handler = scic_sds_remote_device_general_frame_handler
1066 [SCI_BASE_REMOTE_DEVICE_STATE_RESETTING] = {
1067 .start_handler = scic_sds_remote_device_default_start_handler,
1068 .stop_handler = scic_sds_remote_device_resetting_state_stop_handler,
1069 .fail_handler = scic_sds_remote_device_default_fail_handler,
1070 .destruct_handler = scic_sds_remote_device_default_destruct_handler,
1071 .reset_handler = scic_sds_remote_device_default_reset_handler,
1072 .reset_complete_handler = scic_sds_remote_device_resetting_state_reset_complete_handler,
1073 .start_io_handler = scic_sds_remote_device_default_start_request_handler,
1074 .complete_io_handler = scic_sds_remote_device_resetting_state_complete_request_handler,
1075 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
1076 .start_task_handler = scic_sds_remote_device_default_start_request_handler,
1077 .complete_task_handler = scic_sds_remote_device_resetting_state_complete_request_handler,
1078 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1079 .resume_handler = scic_sds_remote_device_default_resume_handler,
1080 .event_handler = scic_sds_remote_device_default_event_handler,
1081 .frame_handler = scic_sds_remote_device_general_frame_handler
1083 [SCI_BASE_REMOTE_DEVICE_STATE_FINAL] = {
1084 .start_handler = scic_sds_remote_device_default_start_handler,
1085 .stop_handler = scic_sds_remote_device_default_stop_handler,
1086 .fail_handler = scic_sds_remote_device_default_fail_handler,
1087 .destruct_handler = scic_sds_remote_device_default_destruct_handler,
1088 .reset_handler = scic_sds_remote_device_default_reset_handler,
1089 .reset_complete_handler = scic_sds_remote_device_default_reset_complete_handler,
1090 .start_io_handler = scic_sds_remote_device_default_start_request_handler,
1091 .complete_io_handler = scic_sds_remote_device_default_complete_request_handler,
1092 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
1093 .start_task_handler = scic_sds_remote_device_default_start_request_handler,
1094 .complete_task_handler = scic_sds_remote_device_default_complete_request_handler,
1095 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1096 .resume_handler = scic_sds_remote_device_default_resume_handler,
1097 .event_handler = scic_sds_remote_device_default_event_handler,
1098 .frame_handler = scic_sds_remote_device_default_frame_handler
1102 static void scic_sds_remote_device_initial_state_enter(void *object)
1104 struct scic_sds_remote_device *sci_dev = object;
1106 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1107 SCI_BASE_REMOTE_DEVICE_STATE_INITIAL);
1109 /* Initial state is a transitional state to the stopped state */
1110 sci_base_state_machine_change_state(&sci_dev->state_machine,
1111 SCI_BASE_REMOTE_DEVICE_STATE_STOPPED);
1115 * isci_remote_device_change_state() - This function gets the status of the
1116 * remote_device object.
1117 * @isci_device: This parameter points to the isci_remote_device object
1119 * status of the object as a isci_status enum.
1121 void isci_remote_device_change_state(
1122 struct isci_remote_device *isci_device,
1123 enum isci_status status)
1125 unsigned long flags;
1127 spin_lock_irqsave(&isci_device->state_lock, flags);
1128 isci_device->status = status;
1129 spin_unlock_irqrestore(&isci_device->state_lock, flags);
1133 * scic_remote_device_destruct() - free remote node context and destruct
1134 * @remote_device: This parameter specifies the remote device to be destructed.
1136 * Remote device objects are a limited resource. As such, they must be
1137 * protected. Thus calls to construct and destruct are mutually exclusive and
1138 * non-reentrant. The return value shall indicate if the device was
1139 * successfully destructed or if some failure occurred. enum sci_status This value
1140 * is returned if the device is successfully destructed.
1141 * SCI_FAILURE_INVALID_REMOTE_DEVICE This value is returned if the supplied
1142 * device isn't valid (e.g. it's already been destoryed, the handle isn't
1145 static enum sci_status scic_remote_device_destruct(struct scic_sds_remote_device *sci_dev)
1147 return sci_dev->state_handlers->destruct_handler(sci_dev);
1151 * isci_remote_device_deconstruct() - This function frees an isci_remote_device.
1152 * @ihost: This parameter specifies the isci host object.
1153 * @idev: This parameter specifies the remote device to be freed.
1156 static void isci_remote_device_deconstruct(struct isci_host *ihost, struct isci_remote_device *idev)
1158 dev_dbg(&ihost->pdev->dev,
1159 "%s: isci_device = %p\n", __func__, idev);
1161 /* There should not be any outstanding io's. All paths to
1162 * here should go through isci_remote_device_nuke_requests.
1163 * If we hit this condition, we will need a way to complete
1164 * io requests in process */
1165 while (!list_empty(&idev->reqs_in_process)) {
1167 dev_err(&ihost->pdev->dev,
1168 "%s: ** request list not empty! **\n", __func__);
1172 scic_remote_device_destruct(&idev->sci);
1173 idev->domain_dev->lldd_dev = NULL;
1174 idev->domain_dev = NULL;
1175 idev->isci_port = NULL;
1176 list_del_init(&idev->node);
1178 clear_bit(IDEV_START_PENDING, &idev->flags);
1179 clear_bit(IDEV_STOP_PENDING, &idev->flags);
1180 wake_up(&ihost->eventq);
1184 * isci_remote_device_stop_complete() - This function is called by the scic
1185 * when the remote device stop has completed. We mark the isci device as not
1186 * ready and remove the isci remote device.
1187 * @ihost: This parameter specifies the isci host object.
1188 * @idev: This parameter specifies the remote device.
1189 * @status: This parameter specifies status of the completion.
1192 static void isci_remote_device_stop_complete(struct isci_host *ihost,
1193 struct isci_remote_device *idev)
1195 dev_dbg(&ihost->pdev->dev, "%s: complete idev = %p\n", __func__, idev);
1197 isci_remote_device_change_state(idev, isci_stopped);
1199 /* after stop, we can tear down resources. */
1200 isci_remote_device_deconstruct(ihost, idev);
1203 static void scic_sds_remote_device_stopped_state_enter(void *object)
1205 struct scic_sds_remote_device *sci_dev = object;
1206 struct scic_sds_controller *scic;
1207 struct isci_remote_device *idev;
1208 struct isci_host *ihost;
1211 scic = scic_sds_remote_device_get_controller(sci_dev);
1212 ihost = scic->ihost;
1213 idev = sci_dev_to_idev(sci_dev);
1215 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1216 SCI_BASE_REMOTE_DEVICE_STATE_STOPPED);
1218 /* If we are entering from the stopping state let the SCI User know that
1219 * the stop operation has completed.
1221 prev_state = sci_dev->state_machine.previous_state_id;
1222 if (prev_state == SCI_BASE_REMOTE_DEVICE_STATE_STOPPING)
1223 isci_remote_device_stop_complete(ihost, idev);
1225 scic_sds_controller_remote_device_stopped(scic, sci_dev);
1228 static void scic_sds_remote_device_starting_state_enter(void *object)
1230 struct scic_sds_remote_device *sci_dev = object;
1231 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1232 struct isci_host *ihost = scic->ihost;
1233 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
1235 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1236 SCI_BASE_REMOTE_DEVICE_STATE_STARTING);
1238 isci_remote_device_not_ready(ihost, idev,
1239 SCIC_REMOTE_DEVICE_NOT_READY_START_REQUESTED);
1242 static void scic_sds_remote_device_ready_state_enter(void *object)
1244 struct scic_sds_remote_device *sci_dev = object;
1245 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1246 struct isci_host *ihost = scic->ihost;
1247 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
1249 SET_STATE_HANDLER(sci_dev,
1250 scic_sds_remote_device_state_handler_table,
1251 SCI_BASE_REMOTE_DEVICE_STATE_READY);
1253 scic->remote_device_sequence[sci_dev->rnc.remote_node_index]++;
1255 if (sci_dev->has_ready_substate_machine)
1256 sci_base_state_machine_start(&sci_dev->ready_substate_machine);
1258 isci_remote_device_ready(ihost, idev);
1261 static void scic_sds_remote_device_ready_state_exit(void *object)
1263 struct scic_sds_remote_device *sci_dev = object;
1264 if (sci_dev->has_ready_substate_machine)
1265 sci_base_state_machine_stop(&sci_dev->ready_substate_machine);
1267 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1268 struct isci_host *ihost = scic->ihost;
1269 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
1271 isci_remote_device_not_ready(ihost, idev,
1272 SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED);
1276 static void scic_sds_remote_device_stopping_state_enter(void *object)
1278 struct scic_sds_remote_device *sci_dev = object;
1282 scic_sds_remote_device_state_handler_table,
1283 SCI_BASE_REMOTE_DEVICE_STATE_STOPPING
1287 static void scic_sds_remote_device_failed_state_enter(void *object)
1289 struct scic_sds_remote_device *sci_dev = object;
1293 scic_sds_remote_device_state_handler_table,
1294 SCI_BASE_REMOTE_DEVICE_STATE_FAILED
1298 static void scic_sds_remote_device_resetting_state_enter(void *object)
1300 struct scic_sds_remote_device *sci_dev = object;
1304 scic_sds_remote_device_state_handler_table,
1305 SCI_BASE_REMOTE_DEVICE_STATE_RESETTING
1308 scic_sds_remote_node_context_suspend(
1309 &sci_dev->rnc, SCI_SOFTWARE_SUSPENSION, NULL, NULL);
1312 static void scic_sds_remote_device_resetting_state_exit(void *object)
1314 struct scic_sds_remote_device *sci_dev = object;
1316 scic_sds_remote_node_context_resume(&sci_dev->rnc, NULL, NULL);
1319 static void scic_sds_remote_device_final_state_enter(void *object)
1321 struct scic_sds_remote_device *sci_dev = object;
1325 scic_sds_remote_device_state_handler_table,
1326 SCI_BASE_REMOTE_DEVICE_STATE_FINAL
1331 static const struct sci_base_state scic_sds_remote_device_state_table[] = {
1332 [SCI_BASE_REMOTE_DEVICE_STATE_INITIAL] = {
1333 .enter_state = scic_sds_remote_device_initial_state_enter,
1335 [SCI_BASE_REMOTE_DEVICE_STATE_STOPPED] = {
1336 .enter_state = scic_sds_remote_device_stopped_state_enter,
1338 [SCI_BASE_REMOTE_DEVICE_STATE_STARTING] = {
1339 .enter_state = scic_sds_remote_device_starting_state_enter,
1341 [SCI_BASE_REMOTE_DEVICE_STATE_READY] = {
1342 .enter_state = scic_sds_remote_device_ready_state_enter,
1343 .exit_state = scic_sds_remote_device_ready_state_exit
1345 [SCI_BASE_REMOTE_DEVICE_STATE_STOPPING] = {
1346 .enter_state = scic_sds_remote_device_stopping_state_enter,
1348 [SCI_BASE_REMOTE_DEVICE_STATE_FAILED] = {
1349 .enter_state = scic_sds_remote_device_failed_state_enter,
1351 [SCI_BASE_REMOTE_DEVICE_STATE_RESETTING] = {
1352 .enter_state = scic_sds_remote_device_resetting_state_enter,
1353 .exit_state = scic_sds_remote_device_resetting_state_exit
1355 [SCI_BASE_REMOTE_DEVICE_STATE_FINAL] = {
1356 .enter_state = scic_sds_remote_device_final_state_enter,
1361 * scic_remote_device_construct() - common construction
1362 * @sci_port: SAS/SATA port through which this device is accessed.
1363 * @sci_dev: remote device to construct
1365 * This routine just performs benign initialization and does not
1366 * allocate the remote_node_context which is left to
1367 * scic_remote_device_[de]a_construct(). scic_remote_device_destruct()
1368 * frees the remote_node_context(s) for the device.
1370 static void scic_remote_device_construct(struct scic_sds_port *sci_port,
1371 struct scic_sds_remote_device *sci_dev)
1373 sci_dev->owning_port = sci_port;
1374 sci_dev->started_request_count = 0;
1376 sci_base_state_machine_construct(
1377 &sci_dev->state_machine,
1379 scic_sds_remote_device_state_table,
1380 SCI_BASE_REMOTE_DEVICE_STATE_INITIAL
1383 sci_base_state_machine_start(
1384 &sci_dev->state_machine
1387 scic_sds_remote_node_context_construct(&sci_dev->rnc,
1388 SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX);
1392 * scic_remote_device_da_construct() - construct direct attached device.
1394 * The information (e.g. IAF, Signature FIS, etc.) necessary to build
1395 * the device is known to the SCI Core since it is contained in the
1396 * scic_phy object. Remote node context(s) is/are a global resource
1397 * allocated by this routine, freed by scic_remote_device_destruct().
1400 * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
1401 * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
1402 * sata-only controller instance.
1403 * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
1405 static enum sci_status scic_remote_device_da_construct(struct scic_sds_port *sci_port,
1406 struct scic_sds_remote_device *sci_dev)
1408 enum sci_status status;
1409 u16 remote_node_index;
1410 struct domain_device *dev = sci_dev_to_domain(sci_dev);
1412 scic_remote_device_construct(sci_port, sci_dev);
1415 * This information is request to determine how many remote node context
1416 * entries will be needed to store the remote node.
1418 sci_dev->is_direct_attached = true;
1419 status = scic_sds_controller_allocate_remote_node_context(sci_port->owning_controller,
1421 &remote_node_index);
1423 if (status != SCI_SUCCESS)
1426 sci_dev->rnc.remote_node_index = remote_node_index;
1428 if (dev->dev_type == SAS_END_DEV)
1429 sci_dev->has_ready_substate_machine = false;
1430 else if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP)) {
1431 sci_dev->has_ready_substate_machine = true;
1433 sci_base_state_machine_construct(
1434 &sci_dev->ready_substate_machine,
1436 scic_sds_stp_remote_device_ready_substate_table,
1437 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
1438 } else if (dev_is_expander(dev)) {
1439 sci_dev->has_ready_substate_machine = true;
1441 /* add the SMP ready substate machine construction here */
1442 sci_base_state_machine_construct(
1443 &sci_dev->ready_substate_machine,
1445 scic_sds_smp_remote_device_ready_substate_table,
1446 SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
1448 return SCI_FAILURE_UNSUPPORTED_PROTOCOL;
1450 sci_dev->connection_rate = scic_sds_port_get_max_allowed_speed(sci_port);
1452 /* / @todo Should I assign the port width by reading all of the phys on the port? */
1453 sci_dev->device_port_width = 1;
1459 * scic_remote_device_ea_construct() - construct expander attached device
1461 * Remote node context(s) is/are a global resource allocated by this
1462 * routine, freed by scic_remote_device_destruct().
1465 * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
1466 * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
1467 * sata-only controller instance.
1468 * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
1470 static enum sci_status scic_remote_device_ea_construct(struct scic_sds_port *sci_port,
1471 struct scic_sds_remote_device *sci_dev)
1473 struct scic_sds_controller *scic = sci_port->owning_controller;
1474 struct domain_device *dev = sci_dev_to_domain(sci_dev);
1475 enum sci_status status;
1477 scic_remote_device_construct(sci_port, sci_dev);
1479 status = scic_sds_controller_allocate_remote_node_context(
1480 scic, sci_dev, &sci_dev->rnc.remote_node_index);
1481 if (status != SCI_SUCCESS)
1484 if (dev->dev_type == SAS_END_DEV)
1485 sci_dev->has_ready_substate_machine = false;
1486 else if (dev_is_expander(dev)) {
1487 sci_dev->has_ready_substate_machine = true;
1489 /* add the SMP ready substate machine construction here */
1490 sci_base_state_machine_construct(
1491 &sci_dev->ready_substate_machine,
1493 scic_sds_smp_remote_device_ready_substate_table,
1494 SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
1495 } else if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP)) {
1496 sci_dev->has_ready_substate_machine = true;
1498 sci_base_state_machine_construct(
1499 &sci_dev->ready_substate_machine,
1501 scic_sds_stp_remote_device_ready_substate_table,
1502 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
1506 * For SAS-2 the physical link rate is actually a logical link
1507 * rate that incorporates multiplexing. The SCU doesn't
1508 * incorporate multiplexing and for the purposes of the
1509 * connection the logical link rate is that same as the
1510 * physical. Furthermore, the SAS-2 and SAS-1.1 fields overlay
1511 * one another, so this code works for both situations. */
1512 sci_dev->connection_rate = min_t(u16, scic_sds_port_get_max_allowed_speed(sci_port),
1515 /* / @todo Should I assign the port width by reading all of the phys on the port? */
1516 sci_dev->device_port_width = 1;
1522 * scic_remote_device_start() - This method will start the supplied remote
1523 * device. This method enables normal IO requests to flow through to the
1525 * @remote_device: This parameter specifies the device to be started.
1526 * @timeout: This parameter specifies the number of milliseconds in which the
1527 * start operation should complete.
1529 * An indication of whether the device was successfully started. SCI_SUCCESS
1530 * This value is returned if the device was successfully started.
1531 * SCI_FAILURE_INVALID_PHY This value is returned if the user attempts to start
1532 * the device when there have been no phys added to it.
1534 static enum sci_status scic_remote_device_start(struct scic_sds_remote_device *sci_dev,
1537 return sci_dev->state_handlers->start_handler(sci_dev);
1540 static enum sci_status isci_remote_device_construct(struct isci_port *iport,
1541 struct isci_remote_device *idev)
1543 struct scic_sds_port *sci_port = iport->sci_port_handle;
1544 struct isci_host *ihost = iport->isci_host;
1545 struct domain_device *dev = idev->domain_dev;
1546 enum sci_status status;
1548 if (dev->parent && dev_is_expander(dev->parent))
1549 status = scic_remote_device_ea_construct(sci_port, &idev->sci);
1551 status = scic_remote_device_da_construct(sci_port, &idev->sci);
1553 if (status != SCI_SUCCESS) {
1554 dev_dbg(&ihost->pdev->dev, "%s: construct failed: %d\n",
1560 /* start the device. */
1561 status = scic_remote_device_start(&idev->sci, ISCI_REMOTE_DEVICE_START_TIMEOUT);
1563 if (status != SCI_SUCCESS)
1564 dev_warn(&ihost->pdev->dev, "remote device start failed: %d\n",
1570 void isci_remote_device_nuke_requests(struct isci_host *ihost, struct isci_remote_device *idev)
1572 DECLARE_COMPLETION_ONSTACK(aborted_task_completion);
1574 dev_dbg(&ihost->pdev->dev,
1575 "%s: idev = %p\n", __func__, idev);
1577 /* Cleanup all requests pending for this device. */
1578 isci_terminate_pending_requests(ihost, idev, terminating);
1580 dev_dbg(&ihost->pdev->dev,
1581 "%s: idev = %p, done\n", __func__, idev);
1585 * This function builds the isci_remote_device when a libsas dev_found message
1587 * @isci_host: This parameter specifies the isci host object.
1588 * @port: This parameter specifies the isci_port conected to this device.
1590 * pointer to new isci_remote_device.
1592 static struct isci_remote_device *
1593 isci_remote_device_alloc(struct isci_host *ihost, struct isci_port *iport)
1595 struct isci_remote_device *idev;
1598 for (i = 0; i < SCI_MAX_REMOTE_DEVICES; i++) {
1599 idev = &ihost->devices[i];
1600 if (!test_and_set_bit(IDEV_ALLOCATED, &idev->flags))
1604 if (i >= SCI_MAX_REMOTE_DEVICES) {
1605 dev_warn(&ihost->pdev->dev, "%s: failed\n", __func__);
1609 if (WARN_ONCE(!list_empty(&idev->reqs_in_process), "found requests in process\n"))
1612 if (WARN_ONCE(!list_empty(&idev->node), "found non-idle remote device\n"))
1615 isci_remote_device_change_state(idev, isci_freed);
1621 * isci_remote_device_ready() - This function is called by the scic when the
1622 * remote device is ready. We mark the isci device as ready and signal the
1624 * @ihost: our valid isci_host
1625 * @idev: remote device
1628 void isci_remote_device_ready(struct isci_host *ihost, struct isci_remote_device *idev)
1630 dev_dbg(&ihost->pdev->dev,
1631 "%s: idev = %p\n", __func__, idev);
1633 isci_remote_device_change_state(idev, isci_ready_for_io);
1634 if (test_and_clear_bit(IDEV_START_PENDING, &idev->flags))
1635 wake_up(&ihost->eventq);
1639 * isci_remote_device_not_ready() - This function is called by the scic when
1640 * the remote device is not ready. We mark the isci device as ready (not
1641 * "ready_for_io") and signal the waiting proccess.
1642 * @isci_host: This parameter specifies the isci host object.
1643 * @isci_device: This parameter specifies the remote device
1646 void isci_remote_device_not_ready(struct isci_host *ihost,
1647 struct isci_remote_device *idev, u32 reason)
1649 dev_dbg(&ihost->pdev->dev,
1650 "%s: isci_device = %p\n", __func__, idev);
1652 if (reason == SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED)
1653 isci_remote_device_change_state(idev, isci_stopping);
1655 /* device ready is actually a "not ready for io" state. */
1656 isci_remote_device_change_state(idev, isci_ready);
1660 * isci_remote_device_stop() - This function is called internally to stop the
1662 * @isci_host: This parameter specifies the isci host object.
1663 * @isci_device: This parameter specifies the remote device.
1665 * The status of the scic request to stop.
1667 enum sci_status isci_remote_device_stop(struct isci_host *ihost, struct isci_remote_device *idev)
1669 enum sci_status status;
1670 unsigned long flags;
1672 dev_dbg(&ihost->pdev->dev,
1673 "%s: isci_device = %p\n", __func__, idev);
1675 isci_remote_device_change_state(idev, isci_stopping);
1677 /* Kill all outstanding requests. */
1678 isci_remote_device_nuke_requests(ihost, idev);
1680 set_bit(IDEV_STOP_PENDING, &idev->flags);
1682 spin_lock_irqsave(&ihost->scic_lock, flags);
1683 status = scic_remote_device_stop(&idev->sci, 50);
1684 spin_unlock_irqrestore(&ihost->scic_lock, flags);
1686 /* Wait for the stop complete callback. */
1687 if (status == SCI_SUCCESS) {
1688 wait_for_device_stop(ihost, idev);
1689 clear_bit(IDEV_ALLOCATED, &idev->flags);
1692 dev_dbg(&ihost->pdev->dev,
1693 "%s: idev = %p - after completion wait\n",
1700 * isci_remote_device_gone() - This function is called by libsas when a domain
1701 * device is removed.
1702 * @domain_device: This parameter specifies the libsas domain device.
1705 void isci_remote_device_gone(struct domain_device *dev)
1707 struct isci_host *ihost = dev_to_ihost(dev);
1708 struct isci_remote_device *idev = dev->lldd_dev;
1710 dev_dbg(&ihost->pdev->dev,
1711 "%s: domain_device = %p, isci_device = %p, isci_port = %p\n",
1712 __func__, dev, idev, idev->isci_port);
1714 isci_remote_device_stop(ihost, idev);
1719 * isci_remote_device_found() - This function is called by libsas when a remote
1720 * device is discovered. A remote device object is created and started. the
1721 * function then sleeps until the sci core device started message is
1723 * @domain_device: This parameter specifies the libsas domain device.
1725 * status, zero indicates success.
1727 int isci_remote_device_found(struct domain_device *domain_dev)
1729 struct isci_host *isci_host = dev_to_ihost(domain_dev);
1730 struct isci_port *isci_port;
1731 struct isci_phy *isci_phy;
1732 struct asd_sas_port *sas_port;
1733 struct asd_sas_phy *sas_phy;
1734 struct isci_remote_device *isci_device;
1735 enum sci_status status;
1737 dev_dbg(&isci_host->pdev->dev,
1738 "%s: domain_device = %p\n", __func__, domain_dev);
1740 wait_for_start(isci_host);
1742 sas_port = domain_dev->port;
1743 sas_phy = list_first_entry(&sas_port->phy_list, struct asd_sas_phy,
1745 isci_phy = to_isci_phy(sas_phy);
1746 isci_port = isci_phy->isci_port;
1748 /* we are being called for a device on this port,
1749 * so it has to come up eventually
1751 wait_for_completion(&isci_port->start_complete);
1753 if ((isci_stopping == isci_port_get_state(isci_port)) ||
1754 (isci_stopped == isci_port_get_state(isci_port)))
1757 isci_device = isci_remote_device_alloc(isci_host, isci_port);
1761 INIT_LIST_HEAD(&isci_device->node);
1762 domain_dev->lldd_dev = isci_device;
1763 isci_device->domain_dev = domain_dev;
1764 isci_device->isci_port = isci_port;
1765 isci_remote_device_change_state(isci_device, isci_starting);
1768 spin_lock_irq(&isci_host->scic_lock);
1769 list_add_tail(&isci_device->node, &isci_port->remote_dev_list);
1771 set_bit(IDEV_START_PENDING, &isci_device->flags);
1772 status = isci_remote_device_construct(isci_port, isci_device);
1773 spin_unlock_irq(&isci_host->scic_lock);
1775 dev_dbg(&isci_host->pdev->dev,
1776 "%s: isci_device = %p\n",
1777 __func__, isci_device);
1779 if (status != SCI_SUCCESS) {
1781 spin_lock_irq(&isci_host->scic_lock);
1782 isci_remote_device_deconstruct(
1786 spin_unlock_irq(&isci_host->scic_lock);
1790 /* wait for the device ready callback. */
1791 wait_for_device_start(isci_host, isci_device);
1796 * isci_device_is_reset_pending() - This function will check if there is any
1797 * pending reset condition on the device.
1798 * @request: This parameter is the isci_device object.
1800 * true if there is a reset pending for the device.
1802 bool isci_device_is_reset_pending(
1803 struct isci_host *isci_host,
1804 struct isci_remote_device *isci_device)
1806 struct isci_request *isci_request;
1807 struct isci_request *tmp_req;
1808 bool reset_is_pending = false;
1809 unsigned long flags;
1811 dev_dbg(&isci_host->pdev->dev,
1812 "%s: isci_device = %p\n", __func__, isci_device);
1814 spin_lock_irqsave(&isci_host->scic_lock, flags);
1816 /* Check for reset on all pending requests. */
1817 list_for_each_entry_safe(isci_request, tmp_req,
1818 &isci_device->reqs_in_process, dev_node) {
1819 dev_dbg(&isci_host->pdev->dev,
1820 "%s: isci_device = %p request = %p\n",
1821 __func__, isci_device, isci_request);
1823 if (isci_request->ttype == io_task) {
1824 struct sas_task *task = isci_request_access_task(
1827 spin_lock(&task->task_state_lock);
1828 if (task->task_state_flags & SAS_TASK_NEED_DEV_RESET)
1829 reset_is_pending = true;
1830 spin_unlock(&task->task_state_lock);
1834 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1836 dev_dbg(&isci_host->pdev->dev,
1837 "%s: isci_device = %p reset_is_pending = %d\n",
1838 __func__, isci_device, reset_is_pending);
1840 return reset_is_pending;
1844 * isci_device_clear_reset_pending() - This function will clear if any pending
1845 * reset condition flags on the device.
1846 * @request: This parameter is the isci_device object.
1848 * true if there is a reset pending for the device.
1850 void isci_device_clear_reset_pending(struct isci_host *ihost, struct isci_remote_device *idev)
1852 struct isci_request *isci_request;
1853 struct isci_request *tmp_req;
1854 unsigned long flags = 0;
1856 dev_dbg(&ihost->pdev->dev, "%s: idev=%p, ihost=%p\n",
1857 __func__, idev, ihost);
1859 spin_lock_irqsave(&ihost->scic_lock, flags);
1861 /* Clear reset pending on all pending requests. */
1862 list_for_each_entry_safe(isci_request, tmp_req,
1863 &idev->reqs_in_process, dev_node) {
1864 dev_dbg(&ihost->pdev->dev, "%s: idev = %p request = %p\n",
1865 __func__, idev, isci_request);
1867 if (isci_request->ttype == io_task) {
1869 unsigned long flags2;
1870 struct sas_task *task = isci_request_access_task(
1873 spin_lock_irqsave(&task->task_state_lock, flags2);
1874 task->task_state_flags &= ~SAS_TASK_NEED_DEV_RESET;
1875 spin_unlock_irqrestore(&task->task_state_lock, flags2);
1878 spin_unlock_irqrestore(&ihost->scic_lock, flags);