isci: Removed sci_base_object from scic_sds_controller.
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / scsi / isci / remote_device.c
blobbb696cf11f2c5beccca8f90d2f4d80cc5d54c404
1 /*
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.
5 * GPL LICENSE SUMMARY
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.
24 * BSD LICENSE
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
31 * are met:
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
38 * distribution.
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 "isci.h"
57 #include "port.h"
58 #include "remote_device.h"
59 #include "request.h"
60 #include "scic_controller.h"
61 #include "scic_io_request.h"
62 #include "scic_phy.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"
70 #include "sci_util.h"
71 #include "scu_event_codes.h"
72 #include "task.h"
74 enum sci_status scic_remote_device_stop(
75 struct scic_sds_remote_device *sci_dev,
76 u32 timeout)
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,
119 u32 suspend_type)
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
127 * requested.
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
131 * enum sci_status
133 enum sci_status scic_sds_remote_device_frame_handler(
134 struct scic_sds_remote_device *sci_dev,
135 u32 frame_index)
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
143 * requested.
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,
150 u32 event_code)
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
159 * requested.
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
177 * requested.
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
195 * requested.
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);
211 * @sci_dev:
212 * @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,
219 u32 request)
221 u32 context;
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),
227 context
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;
266 if (
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
280 * started.
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);
295 else{
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
309 * sequence. none
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;
336 u32 index;
337 u32 request_count = sci_dev->started_request_count;
339 for (index = 0;
340 (index < SCI_MAX_IO_REQUESTS) && (request_count > 0);
341 index++) {
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,
347 sci_dev,
348 sci_req
351 if (terminate_status != SCI_SUCCESS)
352 status = terminate_status;
354 request_count--;
358 return status;
361 static enum sci_status
362 default_device_handler(struct scic_sds_remote_device *sci_dev,
363 const char *func)
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
424 * object to process.
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,
432 u32 event_code,
433 bool is_ready_state)
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);
442 break;
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,
451 NULL, NULL);
453 dev_dbg(scirdev_to_dev(sci_dev),
454 "%s: device: %p event code: %x: %s\n",
455 __func__, sci_dev, event_code,
456 (is_ready_state)
457 ? "I_T_Nexus_Timeout event"
458 : "I_T_Nexus_Timeout event in wrong state");
460 break;
462 /* Else, fall through and treat as unhandled... */
464 default:
465 dev_dbg(scirdev_to_dev(sci_dev),
466 "%s: device: %p event code: %x: %s\n",
467 __func__, sci_dev, event_code,
468 (is_ready_state)
469 ? "unexpected event"
470 : "unexpected event in wrong state");
471 status = SCI_FAILURE_INVALID_STATE;
472 break;
475 return status;
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
482 * object to process.
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,
490 u32 event_code)
492 return scic_sds_remote_device_core_event_handler(sci_dev,
493 event_code,
494 false);
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,
510 u32 frame_index)
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",
515 __func__,
516 frame_index,
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,
563 u32 frame_index)
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),
571 frame_index,
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
587 } else {
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);
594 return result;
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,
607 u32 event_code)
609 return scic_sds_remote_device_core_event_handler(sci_dev,
610 event_code,
611 true);
615 * *****************************************************************************
616 * * STOPPED STATE HANDLERS
617 * ***************************************************************************** */
621 * @device:
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);
643 return status;
646 static enum sci_status scic_sds_remote_device_stopped_state_stop_handler(
647 struct scic_sds_remote_device *sci_dev)
649 return SCI_SUCCESS;
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);
676 return SCI_SUCCESS;
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);
705 return SCI_SUCCESS;
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,
720 sci_dev);
721 } else
722 status = scic_sds_remote_device_terminate_requests(sci_dev);
724 return status;
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);
741 return SCI_SUCCESS;
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,
764 request);
765 if (result == SCI_SUCCESS)
766 result = scic_sds_request_start(request);
768 scic_sds_remote_device_start_request(sci_dev, request, result);
771 return 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);
800 return 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)
818 return result;
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),
824 sci_dev, request);
826 if (result == SCI_SUCCESS)
827 scic_sds_remote_device_decrement_request_count(sci_dev);
829 return result;
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)
880 return status;
882 status = scic_sds_port_complete_io(scic_sds_remote_device_get_port(sci_dev),
883 sci_dev, request);
884 if (status != SCI_SUCCESS)
885 return status;
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,
892 sci_dev);
893 return SCI_SUCCESS;
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
913 return SCI_SUCCESS;
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.
922 * enum sci_status
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
932 return SCI_SUCCESS;
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),
953 sci_dev, request);
955 if (status == SCI_SUCCESS) {
956 scic_sds_remote_device_decrement_request_count(sci_dev);
960 return status;
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 = (struct scic_sds_remote_device *)object;
1106 sci_dev = container_of(object, typeof(*sci_dev), parent);
1107 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1108 SCI_BASE_REMOTE_DEVICE_STATE_INITIAL);
1110 /* Initial state is a transitional state to the stopped state */
1111 sci_base_state_machine_change_state(&sci_dev->state_machine,
1112 SCI_BASE_REMOTE_DEVICE_STATE_STOPPED);
1116 * isci_remote_device_change_state() - This function gets the status of the
1117 * remote_device object.
1118 * @isci_device: This parameter points to the isci_remote_device object
1120 * status of the object as a isci_status enum.
1122 void isci_remote_device_change_state(
1123 struct isci_remote_device *isci_device,
1124 enum isci_status status)
1126 unsigned long flags;
1128 spin_lock_irqsave(&isci_device->state_lock, flags);
1129 isci_device->status = status;
1130 spin_unlock_irqrestore(&isci_device->state_lock, flags);
1134 * scic_remote_device_destruct() - free remote node context and destruct
1135 * @remote_device: This parameter specifies the remote device to be destructed.
1137 * Remote device objects are a limited resource. As such, they must be
1138 * protected. Thus calls to construct and destruct are mutually exclusive and
1139 * non-reentrant. The return value shall indicate if the device was
1140 * successfully destructed or if some failure occurred. enum sci_status This value
1141 * is returned if the device is successfully destructed.
1142 * SCI_FAILURE_INVALID_REMOTE_DEVICE This value is returned if the supplied
1143 * device isn't valid (e.g. it's already been destoryed, the handle isn't
1144 * valid, etc.).
1146 static enum sci_status scic_remote_device_destruct(struct scic_sds_remote_device *sci_dev)
1148 return sci_dev->state_handlers->destruct_handler(sci_dev);
1152 * isci_remote_device_deconstruct() - This function frees an isci_remote_device.
1153 * @ihost: This parameter specifies the isci host object.
1154 * @idev: This parameter specifies the remote device to be freed.
1157 static void isci_remote_device_deconstruct(struct isci_host *ihost, struct isci_remote_device *idev)
1159 dev_dbg(&ihost->pdev->dev,
1160 "%s: isci_device = %p\n", __func__, idev);
1162 /* There should not be any outstanding io's. All paths to
1163 * here should go through isci_remote_device_nuke_requests.
1164 * If we hit this condition, we will need a way to complete
1165 * io requests in process */
1166 while (!list_empty(&idev->reqs_in_process)) {
1168 dev_err(&ihost->pdev->dev,
1169 "%s: ** request list not empty! **\n", __func__);
1170 BUG();
1173 scic_remote_device_destruct(&idev->sci);
1174 idev->domain_dev->lldd_dev = NULL;
1175 idev->domain_dev = NULL;
1176 idev->isci_port = NULL;
1177 list_del_init(&idev->node);
1179 clear_bit(IDEV_START_PENDING, &idev->flags);
1180 clear_bit(IDEV_STOP_PENDING, &idev->flags);
1181 wake_up(&ihost->eventq);
1185 * isci_remote_device_stop_complete() - This function is called by the scic
1186 * when the remote device stop has completed. We mark the isci device as not
1187 * ready and remove the isci remote device.
1188 * @ihost: This parameter specifies the isci host object.
1189 * @idev: This parameter specifies the remote device.
1190 * @status: This parameter specifies status of the completion.
1193 static void isci_remote_device_stop_complete(struct isci_host *ihost,
1194 struct isci_remote_device *idev)
1196 dev_dbg(&ihost->pdev->dev, "%s: complete idev = %p\n", __func__, idev);
1198 isci_remote_device_change_state(idev, isci_stopped);
1200 /* after stop, we can tear down resources. */
1201 isci_remote_device_deconstruct(ihost, idev);
1204 static void scic_sds_remote_device_stopped_state_enter(void *object)
1206 struct scic_sds_remote_device *sci_dev;
1207 struct scic_sds_controller *scic;
1208 struct isci_remote_device *idev;
1209 struct isci_host *ihost;
1210 u32 prev_state;
1212 sci_dev = container_of(object, typeof(*sci_dev), parent);
1213 scic = scic_sds_remote_device_get_controller(sci_dev);
1214 ihost = scic->ihost;
1215 idev = sci_object_get_association(sci_dev);
1217 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1218 SCI_BASE_REMOTE_DEVICE_STATE_STOPPED);
1220 /* If we are entering from the stopping state let the SCI User know that
1221 * the stop operation has completed.
1223 prev_state = sci_dev->state_machine.previous_state_id;
1224 if (prev_state == SCI_BASE_REMOTE_DEVICE_STATE_STOPPING)
1225 isci_remote_device_stop_complete(ihost, idev);
1227 scic_sds_controller_remote_device_stopped(scic, sci_dev);
1230 static void scic_sds_remote_device_starting_state_enter(void *object)
1232 struct scic_sds_remote_device *sci_dev = container_of(object, typeof(*sci_dev),
1233 parent);
1234 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1235 struct isci_host *ihost = scic->ihost;
1236 struct isci_remote_device *idev = sci_object_get_association(sci_dev);
1238 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1239 SCI_BASE_REMOTE_DEVICE_STATE_STARTING);
1241 isci_remote_device_not_ready(ihost, idev,
1242 SCIC_REMOTE_DEVICE_NOT_READY_START_REQUESTED);
1245 static void scic_sds_remote_device_ready_state_enter(void *object)
1247 struct scic_sds_remote_device *sci_dev = container_of(object, typeof(*sci_dev),
1248 parent);
1249 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1250 struct isci_host *ihost = scic->ihost;
1251 struct isci_remote_device *idev = sci_object_get_association(sci_dev);
1253 SET_STATE_HANDLER(sci_dev,
1254 scic_sds_remote_device_state_handler_table,
1255 SCI_BASE_REMOTE_DEVICE_STATE_READY);
1257 scic->remote_device_sequence[sci_dev->rnc.remote_node_index]++;
1259 if (sci_dev->has_ready_substate_machine)
1260 sci_base_state_machine_start(&sci_dev->ready_substate_machine);
1261 else
1262 isci_remote_device_ready(ihost, idev);
1265 static void scic_sds_remote_device_ready_state_exit(void *object)
1267 struct scic_sds_remote_device *sci_dev = container_of(object, typeof(*sci_dev),
1268 parent);
1269 if (sci_dev->has_ready_substate_machine)
1270 sci_base_state_machine_stop(&sci_dev->ready_substate_machine);
1271 else {
1272 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1273 struct isci_host *ihost = scic->ihost;
1274 struct isci_remote_device *idev = sci_object_get_association(sci_dev);
1276 isci_remote_device_not_ready(ihost, idev,
1277 SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED);
1281 static void scic_sds_remote_device_stopping_state_enter(void *object)
1283 struct scic_sds_remote_device *sci_dev = (struct scic_sds_remote_device *)object;
1285 SET_STATE_HANDLER(
1286 sci_dev,
1287 scic_sds_remote_device_state_handler_table,
1288 SCI_BASE_REMOTE_DEVICE_STATE_STOPPING
1292 static void scic_sds_remote_device_failed_state_enter(void *object)
1294 struct scic_sds_remote_device *sci_dev = (struct scic_sds_remote_device *)object;
1296 SET_STATE_HANDLER(
1297 sci_dev,
1298 scic_sds_remote_device_state_handler_table,
1299 SCI_BASE_REMOTE_DEVICE_STATE_FAILED
1303 static void scic_sds_remote_device_resetting_state_enter(void *object)
1305 struct scic_sds_remote_device *sci_dev = (struct scic_sds_remote_device *)object;
1307 SET_STATE_HANDLER(
1308 sci_dev,
1309 scic_sds_remote_device_state_handler_table,
1310 SCI_BASE_REMOTE_DEVICE_STATE_RESETTING
1313 scic_sds_remote_node_context_suspend(
1314 &sci_dev->rnc, SCI_SOFTWARE_SUSPENSION, NULL, NULL);
1317 static void scic_sds_remote_device_resetting_state_exit(void *object)
1319 struct scic_sds_remote_device *sci_dev = (struct scic_sds_remote_device *)object;
1321 scic_sds_remote_node_context_resume(&sci_dev->rnc, NULL, NULL);
1324 static void scic_sds_remote_device_final_state_enter(void *object)
1326 struct scic_sds_remote_device *sci_dev = (struct scic_sds_remote_device *)object;
1328 SET_STATE_HANDLER(
1329 sci_dev,
1330 scic_sds_remote_device_state_handler_table,
1331 SCI_BASE_REMOTE_DEVICE_STATE_FINAL
1336 static const struct sci_base_state scic_sds_remote_device_state_table[] = {
1337 [SCI_BASE_REMOTE_DEVICE_STATE_INITIAL] = {
1338 .enter_state = scic_sds_remote_device_initial_state_enter,
1340 [SCI_BASE_REMOTE_DEVICE_STATE_STOPPED] = {
1341 .enter_state = scic_sds_remote_device_stopped_state_enter,
1343 [SCI_BASE_REMOTE_DEVICE_STATE_STARTING] = {
1344 .enter_state = scic_sds_remote_device_starting_state_enter,
1346 [SCI_BASE_REMOTE_DEVICE_STATE_READY] = {
1347 .enter_state = scic_sds_remote_device_ready_state_enter,
1348 .exit_state = scic_sds_remote_device_ready_state_exit
1350 [SCI_BASE_REMOTE_DEVICE_STATE_STOPPING] = {
1351 .enter_state = scic_sds_remote_device_stopping_state_enter,
1353 [SCI_BASE_REMOTE_DEVICE_STATE_FAILED] = {
1354 .enter_state = scic_sds_remote_device_failed_state_enter,
1356 [SCI_BASE_REMOTE_DEVICE_STATE_RESETTING] = {
1357 .enter_state = scic_sds_remote_device_resetting_state_enter,
1358 .exit_state = scic_sds_remote_device_resetting_state_exit
1360 [SCI_BASE_REMOTE_DEVICE_STATE_FINAL] = {
1361 .enter_state = scic_sds_remote_device_final_state_enter,
1366 * scic_remote_device_construct() - common construction
1367 * @sci_port: SAS/SATA port through which this device is accessed.
1368 * @sci_dev: remote device to construct
1370 * This routine just performs benign initialization and does not
1371 * allocate the remote_node_context which is left to
1372 * scic_remote_device_[de]a_construct(). scic_remote_device_destruct()
1373 * frees the remote_node_context(s) for the device.
1375 static void scic_remote_device_construct(struct scic_sds_port *sci_port,
1376 struct scic_sds_remote_device *sci_dev)
1378 sci_dev->owning_port = sci_port;
1379 sci_dev->started_request_count = 0;
1380 sci_dev->parent.private = NULL;
1382 sci_base_state_machine_construct(
1383 &sci_dev->state_machine,
1384 &sci_dev->parent,
1385 scic_sds_remote_device_state_table,
1386 SCI_BASE_REMOTE_DEVICE_STATE_INITIAL
1389 sci_base_state_machine_start(
1390 &sci_dev->state_machine
1393 scic_sds_remote_node_context_construct(&sci_dev->rnc,
1394 SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX);
1396 sci_object_set_association(&sci_dev->rnc, sci_dev);
1400 * scic_remote_device_da_construct() - construct direct attached device.
1402 * The information (e.g. IAF, Signature FIS, etc.) necessary to build
1403 * the device is known to the SCI Core since it is contained in the
1404 * scic_phy object. Remote node context(s) is/are a global resource
1405 * allocated by this routine, freed by scic_remote_device_destruct().
1407 * Returns:
1408 * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
1409 * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
1410 * sata-only controller instance.
1411 * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
1413 static enum sci_status scic_remote_device_da_construct(struct scic_sds_port *sci_port,
1414 struct scic_sds_remote_device *sci_dev)
1416 enum sci_status status;
1417 u16 remote_node_index;
1418 struct domain_device *dev = sci_dev_to_domain(sci_dev);
1420 scic_remote_device_construct(sci_port, sci_dev);
1423 * This information is request to determine how many remote node context
1424 * entries will be needed to store the remote node.
1426 sci_dev->is_direct_attached = true;
1427 status = scic_sds_controller_allocate_remote_node_context(sci_port->owning_controller,
1428 sci_dev,
1429 &remote_node_index);
1431 if (status != SCI_SUCCESS)
1432 return status;
1434 sci_dev->rnc.remote_node_index = remote_node_index;
1436 if (dev->dev_type == SAS_END_DEV)
1437 sci_dev->has_ready_substate_machine = false;
1438 else if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP)) {
1439 sci_dev->has_ready_substate_machine = true;
1441 sci_base_state_machine_construct(
1442 &sci_dev->ready_substate_machine,
1443 &sci_dev->parent,
1444 scic_sds_stp_remote_device_ready_substate_table,
1445 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
1446 } else if (dev_is_expander(dev)) {
1447 sci_dev->has_ready_substate_machine = true;
1449 /* add the SMP ready substate machine construction here */
1450 sci_base_state_machine_construct(
1451 &sci_dev->ready_substate_machine,
1452 &sci_dev->parent,
1453 scic_sds_smp_remote_device_ready_substate_table,
1454 SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
1455 } else
1456 return SCI_FAILURE_UNSUPPORTED_PROTOCOL;
1458 sci_dev->connection_rate = scic_sds_port_get_max_allowed_speed(sci_port);
1460 /* / @todo Should I assign the port width by reading all of the phys on the port? */
1461 sci_dev->device_port_width = 1;
1463 return SCI_SUCCESS;
1467 * scic_remote_device_ea_construct() - construct expander attached device
1469 * Remote node context(s) is/are a global resource allocated by this
1470 * routine, freed by scic_remote_device_destruct().
1472 * Returns:
1473 * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
1474 * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
1475 * sata-only controller instance.
1476 * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
1478 static enum sci_status scic_remote_device_ea_construct(struct scic_sds_port *sci_port,
1479 struct scic_sds_remote_device *sci_dev)
1481 struct scic_sds_controller *scic = sci_port->owning_controller;
1482 struct domain_device *dev = sci_dev_to_domain(sci_dev);
1483 enum sci_status status;
1485 scic_remote_device_construct(sci_port, sci_dev);
1487 status = scic_sds_controller_allocate_remote_node_context(
1488 scic, sci_dev, &sci_dev->rnc.remote_node_index);
1489 if (status != SCI_SUCCESS)
1490 return status;
1492 if (dev->dev_type == SAS_END_DEV)
1493 sci_dev->has_ready_substate_machine = false;
1494 else if (dev_is_expander(dev)) {
1495 sci_dev->has_ready_substate_machine = true;
1497 /* add the SMP ready substate machine construction here */
1498 sci_base_state_machine_construct(
1499 &sci_dev->ready_substate_machine,
1500 &sci_dev->parent,
1501 scic_sds_smp_remote_device_ready_substate_table,
1502 SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
1503 } else if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP)) {
1504 sci_dev->has_ready_substate_machine = true;
1506 sci_base_state_machine_construct(
1507 &sci_dev->ready_substate_machine,
1508 &sci_dev->parent,
1509 scic_sds_stp_remote_device_ready_substate_table,
1510 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
1514 * For SAS-2 the physical link rate is actually a logical link
1515 * rate that incorporates multiplexing. The SCU doesn't
1516 * incorporate multiplexing and for the purposes of the
1517 * connection the logical link rate is that same as the
1518 * physical. Furthermore, the SAS-2 and SAS-1.1 fields overlay
1519 * one another, so this code works for both situations. */
1520 sci_dev->connection_rate = min_t(u16, scic_sds_port_get_max_allowed_speed(sci_port),
1521 dev->linkrate);
1523 /* / @todo Should I assign the port width by reading all of the phys on the port? */
1524 sci_dev->device_port_width = 1;
1526 return status;
1530 * scic_remote_device_start() - This method will start the supplied remote
1531 * device. This method enables normal IO requests to flow through to the
1532 * remote device.
1533 * @remote_device: This parameter specifies the device to be started.
1534 * @timeout: This parameter specifies the number of milliseconds in which the
1535 * start operation should complete.
1537 * An indication of whether the device was successfully started. SCI_SUCCESS
1538 * This value is returned if the device was successfully started.
1539 * SCI_FAILURE_INVALID_PHY This value is returned if the user attempts to start
1540 * the device when there have been no phys added to it.
1542 static enum sci_status scic_remote_device_start(struct scic_sds_remote_device *sci_dev,
1543 u32 timeout)
1545 return sci_dev->state_handlers->start_handler(sci_dev);
1548 static enum sci_status isci_remote_device_construct(struct isci_port *iport,
1549 struct isci_remote_device *idev)
1551 struct scic_sds_port *sci_port = iport->sci_port_handle;
1552 struct isci_host *ihost = iport->isci_host;
1553 struct domain_device *dev = idev->domain_dev;
1554 enum sci_status status;
1556 if (dev->parent && dev_is_expander(dev->parent))
1557 status = scic_remote_device_ea_construct(sci_port, &idev->sci);
1558 else
1559 status = scic_remote_device_da_construct(sci_port, &idev->sci);
1561 if (status != SCI_SUCCESS) {
1562 dev_dbg(&ihost->pdev->dev, "%s: construct failed: %d\n",
1563 __func__, status);
1565 return status;
1568 /* XXX will be killed with sci_base_object removal */
1569 sci_object_set_association(&idev->sci, idev);
1571 /* start the device. */
1572 status = scic_remote_device_start(&idev->sci, ISCI_REMOTE_DEVICE_START_TIMEOUT);
1574 if (status != SCI_SUCCESS)
1575 dev_warn(&ihost->pdev->dev, "remote device start failed: %d\n",
1576 status);
1578 return status;
1581 void isci_remote_device_nuke_requests(struct isci_host *ihost, struct isci_remote_device *idev)
1583 DECLARE_COMPLETION_ONSTACK(aborted_task_completion);
1585 dev_dbg(&ihost->pdev->dev,
1586 "%s: idev = %p\n", __func__, idev);
1588 /* Cleanup all requests pending for this device. */
1589 isci_terminate_pending_requests(ihost, idev, terminating);
1591 dev_dbg(&ihost->pdev->dev,
1592 "%s: idev = %p, done\n", __func__, idev);
1596 * This function builds the isci_remote_device when a libsas dev_found message
1597 * is received.
1598 * @isci_host: This parameter specifies the isci host object.
1599 * @port: This parameter specifies the isci_port conected to this device.
1601 * pointer to new isci_remote_device.
1603 static struct isci_remote_device *
1604 isci_remote_device_alloc(struct isci_host *ihost, struct isci_port *iport)
1606 struct isci_remote_device *idev;
1607 int i;
1609 for (i = 0; i < SCI_MAX_REMOTE_DEVICES; i++) {
1610 idev = &ihost->devices[i];
1611 if (!test_and_set_bit(IDEV_ALLOCATED, &idev->flags))
1612 break;
1615 if (i >= SCI_MAX_REMOTE_DEVICES) {
1616 dev_warn(&ihost->pdev->dev, "%s: failed\n", __func__);
1617 return NULL;
1620 if (WARN_ONCE(!list_empty(&idev->reqs_in_process), "found requests in process\n"))
1621 return NULL;
1623 if (WARN_ONCE(!list_empty(&idev->node), "found non-idle remote device\n"))
1624 return NULL;
1626 isci_remote_device_change_state(idev, isci_freed);
1628 return idev;
1632 * isci_remote_device_ready() - This function is called by the scic when the
1633 * remote device is ready. We mark the isci device as ready and signal the
1634 * waiting proccess.
1635 * @ihost: our valid isci_host
1636 * @idev: remote device
1639 void isci_remote_device_ready(struct isci_host *ihost, struct isci_remote_device *idev)
1641 dev_dbg(&ihost->pdev->dev,
1642 "%s: idev = %p\n", __func__, idev);
1644 isci_remote_device_change_state(idev, isci_ready_for_io);
1645 if (test_and_clear_bit(IDEV_START_PENDING, &idev->flags))
1646 wake_up(&ihost->eventq);
1650 * isci_remote_device_not_ready() - This function is called by the scic when
1651 * the remote device is not ready. We mark the isci device as ready (not
1652 * "ready_for_io") and signal the waiting proccess.
1653 * @isci_host: This parameter specifies the isci host object.
1654 * @isci_device: This parameter specifies the remote device
1657 void isci_remote_device_not_ready(struct isci_host *ihost,
1658 struct isci_remote_device *idev, u32 reason)
1660 dev_dbg(&ihost->pdev->dev,
1661 "%s: isci_device = %p\n", __func__, idev);
1663 if (reason == SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED)
1664 isci_remote_device_change_state(idev, isci_stopping);
1665 else
1666 /* device ready is actually a "not ready for io" state. */
1667 isci_remote_device_change_state(idev, isci_ready);
1671 * isci_remote_device_stop() - This function is called internally to stop the
1672 * remote device.
1673 * @isci_host: This parameter specifies the isci host object.
1674 * @isci_device: This parameter specifies the remote device.
1676 * The status of the scic request to stop.
1678 enum sci_status isci_remote_device_stop(struct isci_host *ihost, struct isci_remote_device *idev)
1680 enum sci_status status;
1681 unsigned long flags;
1683 dev_dbg(&ihost->pdev->dev,
1684 "%s: isci_device = %p\n", __func__, idev);
1686 isci_remote_device_change_state(idev, isci_stopping);
1688 /* Kill all outstanding requests. */
1689 isci_remote_device_nuke_requests(ihost, idev);
1691 set_bit(IDEV_STOP_PENDING, &idev->flags);
1693 spin_lock_irqsave(&ihost->scic_lock, flags);
1694 status = scic_remote_device_stop(&idev->sci, 50);
1695 spin_unlock_irqrestore(&ihost->scic_lock, flags);
1697 /* Wait for the stop complete callback. */
1698 if (status == SCI_SUCCESS) {
1699 wait_for_device_stop(ihost, idev);
1700 clear_bit(IDEV_ALLOCATED, &idev->flags);
1703 dev_dbg(&ihost->pdev->dev,
1704 "%s: idev = %p - after completion wait\n",
1705 __func__, idev);
1707 return status;
1711 * isci_remote_device_gone() - This function is called by libsas when a domain
1712 * device is removed.
1713 * @domain_device: This parameter specifies the libsas domain device.
1716 void isci_remote_device_gone(struct domain_device *dev)
1718 struct isci_host *ihost = dev_to_ihost(dev);
1719 struct isci_remote_device *idev = dev->lldd_dev;
1721 dev_dbg(&ihost->pdev->dev,
1722 "%s: domain_device = %p, isci_device = %p, isci_port = %p\n",
1723 __func__, dev, idev, idev->isci_port);
1725 isci_remote_device_stop(ihost, idev);
1730 * isci_remote_device_found() - This function is called by libsas when a remote
1731 * device is discovered. A remote device object is created and started. the
1732 * function then sleeps until the sci core device started message is
1733 * received.
1734 * @domain_device: This parameter specifies the libsas domain device.
1736 * status, zero indicates success.
1738 int isci_remote_device_found(struct domain_device *domain_dev)
1740 struct isci_host *isci_host = dev_to_ihost(domain_dev);
1741 struct isci_port *isci_port;
1742 struct isci_phy *isci_phy;
1743 struct asd_sas_port *sas_port;
1744 struct asd_sas_phy *sas_phy;
1745 struct isci_remote_device *isci_device;
1746 enum sci_status status;
1748 dev_dbg(&isci_host->pdev->dev,
1749 "%s: domain_device = %p\n", __func__, domain_dev);
1751 wait_for_start(isci_host);
1753 sas_port = domain_dev->port;
1754 sas_phy = list_first_entry(&sas_port->phy_list, struct asd_sas_phy,
1755 port_phy_el);
1756 isci_phy = to_isci_phy(sas_phy);
1757 isci_port = isci_phy->isci_port;
1759 /* we are being called for a device on this port,
1760 * so it has to come up eventually
1762 wait_for_completion(&isci_port->start_complete);
1764 if ((isci_stopping == isci_port_get_state(isci_port)) ||
1765 (isci_stopped == isci_port_get_state(isci_port)))
1766 return -ENODEV;
1768 isci_device = isci_remote_device_alloc(isci_host, isci_port);
1769 if (!isci_device)
1770 return -ENODEV;
1772 INIT_LIST_HEAD(&isci_device->node);
1773 domain_dev->lldd_dev = isci_device;
1774 isci_device->domain_dev = domain_dev;
1775 isci_device->isci_port = isci_port;
1776 isci_remote_device_change_state(isci_device, isci_starting);
1779 spin_lock_irq(&isci_host->scic_lock);
1780 list_add_tail(&isci_device->node, &isci_port->remote_dev_list);
1782 set_bit(IDEV_START_PENDING, &isci_device->flags);
1783 status = isci_remote_device_construct(isci_port, isci_device);
1784 spin_unlock_irq(&isci_host->scic_lock);
1786 dev_dbg(&isci_host->pdev->dev,
1787 "%s: isci_device = %p\n",
1788 __func__, isci_device);
1790 if (status != SCI_SUCCESS) {
1792 spin_lock_irq(&isci_host->scic_lock);
1793 isci_remote_device_deconstruct(
1794 isci_host,
1795 isci_device
1797 spin_unlock_irq(&isci_host->scic_lock);
1798 return -ENODEV;
1801 /* wait for the device ready callback. */
1802 wait_for_device_start(isci_host, isci_device);
1804 return 0;
1807 * isci_device_is_reset_pending() - This function will check if there is any
1808 * pending reset condition on the device.
1809 * @request: This parameter is the isci_device object.
1811 * true if there is a reset pending for the device.
1813 bool isci_device_is_reset_pending(
1814 struct isci_host *isci_host,
1815 struct isci_remote_device *isci_device)
1817 struct isci_request *isci_request;
1818 struct isci_request *tmp_req;
1819 bool reset_is_pending = false;
1820 unsigned long flags;
1822 dev_dbg(&isci_host->pdev->dev,
1823 "%s: isci_device = %p\n", __func__, isci_device);
1825 spin_lock_irqsave(&isci_host->scic_lock, flags);
1827 /* Check for reset on all pending requests. */
1828 list_for_each_entry_safe(isci_request, tmp_req,
1829 &isci_device->reqs_in_process, dev_node) {
1830 dev_dbg(&isci_host->pdev->dev,
1831 "%s: isci_device = %p request = %p\n",
1832 __func__, isci_device, isci_request);
1834 if (isci_request->ttype == io_task) {
1835 struct sas_task *task = isci_request_access_task(
1836 isci_request);
1838 spin_lock(&task->task_state_lock);
1839 if (task->task_state_flags & SAS_TASK_NEED_DEV_RESET)
1840 reset_is_pending = true;
1841 spin_unlock(&task->task_state_lock);
1845 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1847 dev_dbg(&isci_host->pdev->dev,
1848 "%s: isci_device = %p reset_is_pending = %d\n",
1849 __func__, isci_device, reset_is_pending);
1851 return reset_is_pending;
1855 * isci_device_clear_reset_pending() - This function will clear if any pending
1856 * reset condition flags on the device.
1857 * @request: This parameter is the isci_device object.
1859 * true if there is a reset pending for the device.
1861 void isci_device_clear_reset_pending(struct isci_host *ihost, struct isci_remote_device *idev)
1863 struct isci_request *isci_request;
1864 struct isci_request *tmp_req;
1865 unsigned long flags = 0;
1867 dev_dbg(&ihost->pdev->dev, "%s: idev=%p, ihost=%p\n",
1868 __func__, idev, ihost);
1870 spin_lock_irqsave(&ihost->scic_lock, flags);
1872 /* Clear reset pending on all pending requests. */
1873 list_for_each_entry_safe(isci_request, tmp_req,
1874 &idev->reqs_in_process, dev_node) {
1875 dev_dbg(&ihost->pdev->dev, "%s: idev = %p request = %p\n",
1876 __func__, idev, isci_request);
1878 if (isci_request->ttype == io_task) {
1880 unsigned long flags2;
1881 struct sas_task *task = isci_request_access_task(
1882 isci_request);
1884 spin_lock_irqsave(&task->task_state_lock, flags2);
1885 task->task_state_flags &= ~SAS_TASK_NEED_DEV_RESET;
1886 spin_unlock_irqrestore(&task->task_state_lock, flags2);
1889 spin_unlock_irqrestore(&ihost->scic_lock, flags);