isci: remove usage of sci_sas_address in scic_sds_remote_device
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / scsi / isci / remote_device.c
blob1e9e2227820a5bfccb2c4c6681ffdbcfb71e80c2
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;
103 #if !defined(DISABLE_ATAPI)
104 bool scic_remote_device_is_atapi(struct scic_sds_remote_device *sci_dev)
106 return sci_dev->is_atapi;
108 #endif
114 * Remote device timer requirements
116 #define SCIC_SDS_REMOTE_DEVICE_MINIMUM_TIMER_COUNT (0)
117 #define SCIC_SDS_REMOTE_DEVICE_MAXIMUM_TIMER_COUNT (SCI_MAX_REMOTE_DEVICES)
122 * @sci_dev: The remote device for which the suspend is being requested.
124 * This method invokes the remote device suspend state handler. enum sci_status
126 enum sci_status scic_sds_remote_device_suspend(
127 struct scic_sds_remote_device *sci_dev,
128 u32 suspend_type)
130 return sci_dev->state_handlers->suspend_handler(sci_dev, suspend_type);
135 * @sci_dev: The remote device for which the event handling is being
136 * requested.
137 * @frame_index: This is the frame index that is being processed.
139 * This method invokes the frame handler for the remote device state machine
140 * enum sci_status
142 enum sci_status scic_sds_remote_device_frame_handler(
143 struct scic_sds_remote_device *sci_dev,
144 u32 frame_index)
146 return sci_dev->state_handlers->frame_handler(sci_dev, frame_index);
151 * @sci_dev: The remote device for which the event handling is being
152 * requested.
153 * @event_code: This is the event code that is to be processed.
155 * This method invokes the remote device event handler. enum sci_status
157 enum sci_status scic_sds_remote_device_event_handler(
158 struct scic_sds_remote_device *sci_dev,
159 u32 event_code)
161 return sci_dev->state_handlers->event_handler(sci_dev, event_code);
166 * @controller: The controller that is starting the io request.
167 * @sci_dev: The remote device for which the start io handling is being
168 * requested.
169 * @io_request: The io request that is being started.
171 * This method invokes the remote device start io handler. enum sci_status
173 enum sci_status scic_sds_remote_device_start_io(
174 struct scic_sds_controller *controller,
175 struct scic_sds_remote_device *sci_dev,
176 struct scic_sds_request *io_request)
178 return sci_dev->state_handlers->start_io_handler(
179 sci_dev, io_request);
184 * @controller: The controller that is completing the io request.
185 * @sci_dev: The remote device for which the complete io handling is being
186 * requested.
187 * @io_request: The io request that is being completed.
189 * This method invokes the remote device complete io handler. enum sci_status
191 enum sci_status scic_sds_remote_device_complete_io(
192 struct scic_sds_controller *controller,
193 struct scic_sds_remote_device *sci_dev,
194 struct scic_sds_request *io_request)
196 return sci_dev->state_handlers->complete_io_handler(
197 sci_dev, io_request);
202 * @controller: The controller that is starting the task request.
203 * @sci_dev: The remote device for which the start task handling is being
204 * requested.
205 * @io_request: The task request that is being started.
207 * This method invokes the remote device start task handler. enum sci_status
209 enum sci_status scic_sds_remote_device_start_task(
210 struct scic_sds_controller *controller,
211 struct scic_sds_remote_device *sci_dev,
212 struct scic_sds_request *io_request)
214 return sci_dev->state_handlers->start_task_handler(
215 sci_dev, io_request);
220 * @controller: The controller that is completing the task request.
221 * @sci_dev: The remote device for which the complete task handling is
222 * being requested.
223 * @io_request: The task request that is being completed.
225 * This method invokes the remote device complete task handler. enum sci_status
230 * @sci_dev:
231 * @request:
233 * This method takes the request and bulids an appropriate SCU context for the
234 * request and then requests the controller to post the request. none
236 void scic_sds_remote_device_post_request(
237 struct scic_sds_remote_device *sci_dev,
238 u32 request)
240 u32 context;
242 context = scic_sds_remote_device_build_command_context(sci_dev, request);
244 scic_sds_controller_post_request(
245 scic_sds_remote_device_get_controller(sci_dev),
246 context
250 #if !defined(DISABLE_ATAPI)
253 * @sci_dev: The device to be checked.
255 * This method check the signature fis of a stp device to decide whether a
256 * device is atapi or not. true if a device is atapi device. False if a device
257 * is not atapi.
259 bool scic_sds_remote_device_is_atapi(
260 struct scic_sds_remote_device *sci_dev)
262 if (!sci_dev->target_protocols.u.bits.attached_stp_target)
263 return false;
264 else if (sci_dev->is_direct_attached) {
265 struct scic_sds_phy *phy;
266 struct scic_sata_phy_properties properties;
267 struct sata_fis_reg_d2h *signature_fis;
268 phy = scic_sds_port_get_a_connected_phy(sci_dev->owning_port);
269 scic_sata_phy_get_properties(phy, &properties);
271 /* decode the signature fis. */
272 signature_fis = &(properties.signature_fis);
274 if ((signature_fis->sector_count == 0x01)
275 && (signature_fis->lba_low == 0x01)
276 && (signature_fis->lba_mid == 0x14)
277 && (signature_fis->lba_high == 0xEB)
278 && ((signature_fis->device & 0x5F) == 0x00)
280 /* An ATA device supporting the PACKET command set. */
281 return true;
282 } else
283 return false;
284 } else {
285 /* Expander supported ATAPI device is not currently supported. */
286 return false;
289 #endif
293 * @user_parameter: This is cast to a remote device object.
295 * This method is called once the remote node context is ready to be freed.
296 * The remote device can now report that its stop operation is complete. none
298 static void scic_sds_cb_remote_device_rnc_destruct_complete(
299 void *user_parameter)
301 struct scic_sds_remote_device *sci_dev;
303 sci_dev = (struct scic_sds_remote_device *)user_parameter;
305 BUG_ON(sci_dev->started_request_count != 0);
307 sci_base_state_machine_change_state(&sci_dev->state_machine,
308 SCI_BASE_REMOTE_DEVICE_STATE_STOPPED);
313 * @user_parameter: This is cast to a remote device object.
315 * This method is called once the remote node context has transisitioned to a
316 * ready state. This is the indication that the remote device object can also
317 * transition to ready. none
319 static void scic_sds_remote_device_resume_complete_handler(
320 void *user_parameter)
322 struct scic_sds_remote_device *sci_dev;
324 sci_dev = (struct scic_sds_remote_device *)user_parameter;
326 if (
327 sci_base_state_machine_get_state(&sci_dev->state_machine)
328 != SCI_BASE_REMOTE_DEVICE_STATE_READY
330 sci_base_state_machine_change_state(
331 &sci_dev->state_machine,
332 SCI_BASE_REMOTE_DEVICE_STATE_READY
339 * @device: This parameter specifies the device for which the request is being
340 * started.
341 * @request: This parameter specifies the request being started.
342 * @status: This parameter specifies the current start operation status.
344 * This method will perform the STP request start processing common to IO
345 * requests and task requests of all types. none
347 void scic_sds_remote_device_start_request(
348 struct scic_sds_remote_device *sci_dev,
349 struct scic_sds_request *sci_req,
350 enum sci_status status)
352 /* We still have a fault in starting the io complete it on the port */
353 if (status == SCI_SUCCESS)
354 scic_sds_remote_device_increment_request_count(sci_dev);
355 else{
356 sci_dev->owning_port->state_handlers->complete_io_handler(
357 sci_dev->owning_port, sci_dev, sci_req
365 * @request: This parameter specifies the request being continued.
367 * This method will continue to post tc for a STP request. This method usually
368 * serves as a callback when RNC gets resumed during a task management
369 * sequence. none
371 void scic_sds_remote_device_continue_request(void *dev)
373 struct scic_sds_remote_device *sci_dev = dev;
375 /* we need to check if this request is still valid to continue. */
376 if (sci_dev->working_request)
377 scic_controller_continue_io(sci_dev->working_request);
381 * This method will terminate all of the IO requests in the controllers IO
382 * request table that were targeted for this device.
383 * @sci_dev: This parameter specifies the remote device for which to
384 * attempt to terminate all requests.
386 * This method returns an indication as to whether all requests were
387 * successfully terminated. If a single request fails to be terminated, then
388 * this method will return the failure.
390 static enum sci_status scic_sds_remote_device_terminate_requests(
391 struct scic_sds_remote_device *sci_dev)
393 enum sci_status status = SCI_SUCCESS;
394 enum sci_status terminate_status = SCI_SUCCESS;
395 struct scic_sds_request *sci_req;
396 u32 index;
397 u32 request_count = sci_dev->started_request_count;
399 for (index = 0;
400 (index < SCI_MAX_IO_REQUESTS) && (request_count > 0);
401 index++) {
402 sci_req = sci_dev->owning_port->owning_controller->io_request_table[index];
404 if ((sci_req != NULL) && (sci_req->target_device == sci_dev)) {
405 terminate_status = scic_controller_terminate_request(
406 sci_dev->owning_port->owning_controller,
407 sci_dev,
408 sci_req
411 if (terminate_status != SCI_SUCCESS)
412 status = terminate_status;
414 request_count--;
418 return status;
421 static enum sci_status
422 default_device_handler(struct scic_sds_remote_device *sci_dev,
423 const char *func)
425 dev_warn(scirdev_to_dev(sci_dev),
426 "%s: in wrong state: %d\n", func,
427 sci_base_state_machine_get_state(&sci_dev->state_machine));
428 return SCI_FAILURE_INVALID_STATE;
431 enum sci_status scic_sds_remote_device_default_start_handler(
432 struct scic_sds_remote_device *sci_dev)
434 return default_device_handler(sci_dev, __func__);
437 static enum sci_status scic_sds_remote_device_default_stop_handler(
438 struct scic_sds_remote_device *sci_dev)
440 return default_device_handler(sci_dev, __func__);
443 enum sci_status scic_sds_remote_device_default_fail_handler(
444 struct scic_sds_remote_device *sci_dev)
446 return default_device_handler(sci_dev, __func__);
449 enum sci_status scic_sds_remote_device_default_destruct_handler(
450 struct scic_sds_remote_device *sci_dev)
452 return default_device_handler(sci_dev, __func__);
455 enum sci_status scic_sds_remote_device_default_reset_handler(
456 struct scic_sds_remote_device *sci_dev)
458 return default_device_handler(sci_dev, __func__);
461 enum sci_status scic_sds_remote_device_default_reset_complete_handler(
462 struct scic_sds_remote_device *sci_dev)
464 return default_device_handler(sci_dev, __func__);
467 enum sci_status scic_sds_remote_device_default_suspend_handler(
468 struct scic_sds_remote_device *sci_dev, u32 suspend_type)
470 return default_device_handler(sci_dev, __func__);
473 enum sci_status scic_sds_remote_device_default_resume_handler(
474 struct scic_sds_remote_device *sci_dev)
476 return default_device_handler(sci_dev, __func__);
481 * @device: The struct scic_sds_remote_device which is then cast into a
482 * struct scic_sds_remote_device.
483 * @event_code: The event code that the struct scic_sds_controller wants the device
484 * object to process.
486 * This method is the default event handler. It will call the RNC state
487 * machine handler for any RNC events otherwise it will log a warning and
488 * returns a failure. enum sci_status SCI_FAILURE_INVALID_STATE
490 static enum sci_status scic_sds_remote_device_core_event_handler(
491 struct scic_sds_remote_device *sci_dev,
492 u32 event_code,
493 bool is_ready_state)
495 enum sci_status status;
497 switch (scu_get_event_type(event_code)) {
498 case SCU_EVENT_TYPE_RNC_OPS_MISC:
499 case SCU_EVENT_TYPE_RNC_SUSPEND_TX:
500 case SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX:
501 status = scic_sds_remote_node_context_event_handler(&sci_dev->rnc, event_code);
502 break;
503 case SCU_EVENT_TYPE_PTX_SCHEDULE_EVENT:
505 if (scu_get_event_code(event_code) == SCU_EVENT_IT_NEXUS_TIMEOUT) {
506 status = SCI_SUCCESS;
508 /* Suspend the associated RNC */
509 scic_sds_remote_node_context_suspend(&sci_dev->rnc,
510 SCI_SOFTWARE_SUSPENSION,
511 NULL, NULL);
513 dev_dbg(scirdev_to_dev(sci_dev),
514 "%s: device: %p event code: %x: %s\n",
515 __func__, sci_dev, event_code,
516 (is_ready_state)
517 ? "I_T_Nexus_Timeout event"
518 : "I_T_Nexus_Timeout event in wrong state");
520 break;
522 /* Else, fall through and treat as unhandled... */
524 default:
525 dev_dbg(scirdev_to_dev(sci_dev),
526 "%s: device: %p event code: %x: %s\n",
527 __func__, sci_dev, event_code,
528 (is_ready_state)
529 ? "unexpected event"
530 : "unexpected event in wrong state");
531 status = SCI_FAILURE_INVALID_STATE;
532 break;
535 return status;
539 * @device: The struct scic_sds_remote_device which is then cast into a
540 * struct scic_sds_remote_device.
541 * @event_code: The event code that the struct scic_sds_controller wants the device
542 * object to process.
544 * This method is the default event handler. It will call the RNC state
545 * machine handler for any RNC events otherwise it will log a warning and
546 * returns a failure. enum sci_status SCI_FAILURE_INVALID_STATE
548 static enum sci_status scic_sds_remote_device_default_event_handler(
549 struct scic_sds_remote_device *sci_dev,
550 u32 event_code)
552 return scic_sds_remote_device_core_event_handler(sci_dev,
553 event_code,
554 false);
559 * @device: The struct scic_sds_remote_device which is then cast into a
560 * struct scic_sds_remote_device.
561 * @frame_index: The frame index for which the struct scic_sds_controller wants this
562 * device object to process.
564 * This method is the default unsolicited frame handler. It logs a warning,
565 * releases the frame and returns a failure. enum sci_status
566 * SCI_FAILURE_INVALID_STATE
568 enum sci_status scic_sds_remote_device_default_frame_handler(
569 struct scic_sds_remote_device *sci_dev,
570 u32 frame_index)
572 dev_warn(scirdev_to_dev(sci_dev),
573 "%s: SCIC Remote Device requested to handle frame %x "
574 "while in wrong state %d\n",
575 __func__,
576 frame_index,
577 sci_base_state_machine_get_state(
578 &sci_dev->state_machine));
580 /* Return the frame back to the controller */
581 scic_sds_controller_release_frame(
582 scic_sds_remote_device_get_controller(sci_dev), frame_index
585 return SCI_FAILURE_INVALID_STATE;
588 enum sci_status scic_sds_remote_device_default_start_request_handler(
589 struct scic_sds_remote_device *sci_dev,
590 struct scic_sds_request *request)
592 return default_device_handler(sci_dev, __func__);
595 enum sci_status scic_sds_remote_device_default_complete_request_handler(
596 struct scic_sds_remote_device *sci_dev,
597 struct scic_sds_request *request)
599 return default_device_handler(sci_dev, __func__);
602 enum sci_status scic_sds_remote_device_default_continue_request_handler(
603 struct scic_sds_remote_device *sci_dev,
604 struct scic_sds_request *request)
606 return default_device_handler(sci_dev, __func__);
611 * @device: The struct scic_sds_remote_device which is then cast into a
612 * struct scic_sds_remote_device.
613 * @frame_index: The frame index for which the struct scic_sds_controller wants this
614 * device object to process.
616 * This method is a general ssp frame handler. In most cases the device object
617 * needs to route the unsolicited frame processing to the io request object.
618 * This method decodes the tag for the io request object and routes the
619 * unsolicited frame to that object. enum sci_status SCI_FAILURE_INVALID_STATE
621 enum sci_status scic_sds_remote_device_general_frame_handler(
622 struct scic_sds_remote_device *sci_dev,
623 u32 frame_index)
625 enum sci_status result;
626 struct sci_ssp_frame_header *frame_header;
627 struct scic_sds_request *io_request;
629 result = scic_sds_unsolicited_frame_control_get_header(
630 &(scic_sds_remote_device_get_controller(sci_dev)->uf_control),
631 frame_index,
632 (void **)&frame_header
635 if (SCI_SUCCESS == result) {
636 io_request = scic_sds_controller_get_io_request_from_tag(
637 scic_sds_remote_device_get_controller(sci_dev), frame_header->tag);
639 if ((io_request == NULL)
640 || (io_request->target_device != sci_dev)) {
642 * We could not map this tag to a valid IO request
643 * Just toss the frame and continue */
644 scic_sds_controller_release_frame(
645 scic_sds_remote_device_get_controller(sci_dev), frame_index
647 } else {
648 /* The IO request is now in charge of releasing the frame */
649 result = io_request->state_handlers->frame_handler(
650 io_request, frame_index);
654 return result;
659 * @[in]: sci_dev This is the device object that is receiving the event.
660 * @[in]: event_code The event code to process.
662 * This is a common method for handling events reported to the remote device
663 * from the controller object. enum sci_status
665 enum sci_status scic_sds_remote_device_general_event_handler(
666 struct scic_sds_remote_device *sci_dev,
667 u32 event_code)
669 return scic_sds_remote_device_core_event_handler(sci_dev,
670 event_code,
671 true);
675 * *****************************************************************************
676 * * STOPPED STATE HANDLERS
677 * ***************************************************************************** */
681 * @device:
683 * This method takes the struct scic_sds_remote_device from a stopped state and
684 * attempts to start it. The RNC buffer for the device is constructed and the
685 * device state machine is transitioned to the
686 * SCIC_BASE_REMOTE_DEVICE_STATE_STARTING. enum sci_status SCI_SUCCESS if there is
687 * an RNC buffer available to construct the remote device.
688 * SCI_FAILURE_INSUFFICIENT_RESOURCES if there is no RNC buffer available in
689 * which to construct the remote device.
691 static enum sci_status scic_sds_remote_device_stopped_state_start_handler(
692 struct scic_sds_remote_device *sci_dev)
694 enum sci_status status;
696 status = scic_sds_remote_node_context_resume(&sci_dev->rnc,
697 scic_sds_remote_device_resume_complete_handler, sci_dev);
699 if (status == SCI_SUCCESS)
700 sci_base_state_machine_change_state(&sci_dev->state_machine,
701 SCI_BASE_REMOTE_DEVICE_STATE_STARTING);
703 return status;
706 static enum sci_status scic_sds_remote_device_stopped_state_stop_handler(
707 struct scic_sds_remote_device *sci_dev)
709 return SCI_SUCCESS;
714 * @sci_dev: The struct scic_sds_remote_device which is cast into a
715 * struct scic_sds_remote_device.
717 * This method will destruct a struct scic_sds_remote_device that is in a stopped
718 * state. This is the only state from which a destruct request will succeed.
719 * The RNi for this struct scic_sds_remote_device is returned to the free pool and the
720 * device object transitions to the SCI_BASE_REMOTE_DEVICE_STATE_FINAL.
721 * enum sci_status SCI_SUCCESS
723 static enum sci_status scic_sds_remote_device_stopped_state_destruct_handler(
724 struct scic_sds_remote_device *sci_dev)
726 struct scic_sds_controller *scic;
728 scic = scic_sds_remote_device_get_controller(sci_dev);
729 scic_sds_controller_free_remote_node_context(scic, sci_dev,
730 sci_dev->rnc.remote_node_index);
731 sci_dev->rnc.remote_node_index = SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX;
733 sci_base_state_machine_change_state(&sci_dev->state_machine,
734 SCI_BASE_REMOTE_DEVICE_STATE_FINAL);
736 return SCI_SUCCESS;
740 * *****************************************************************************
741 * * STARTING STATE HANDLERS
742 * ***************************************************************************** */
744 static enum sci_status scic_sds_remote_device_starting_state_stop_handler(
745 struct scic_sds_remote_device *sci_dev)
748 * This device has not yet started so there had better be no IO requests
750 BUG_ON(sci_dev->started_request_count != 0);
753 * Destroy the remote node context
755 scic_sds_remote_node_context_destruct(&sci_dev->rnc,
756 scic_sds_cb_remote_device_rnc_destruct_complete, sci_dev);
759 * Transition to the stopping state and wait for the remote node to
760 * complete being posted and invalidated.
762 sci_base_state_machine_change_state(&sci_dev->state_machine,
763 SCI_BASE_REMOTE_DEVICE_STATE_STOPPING);
765 return SCI_SUCCESS;
768 enum sci_status scic_sds_remote_device_ready_state_stop_handler(
769 struct scic_sds_remote_device *sci_dev)
771 enum sci_status status = SCI_SUCCESS;
773 /* Request the parent state machine to transition to the stopping state */
774 sci_base_state_machine_change_state(&sci_dev->state_machine,
775 SCI_BASE_REMOTE_DEVICE_STATE_STOPPING);
777 if (sci_dev->started_request_count == 0) {
778 scic_sds_remote_node_context_destruct(&sci_dev->rnc,
779 scic_sds_cb_remote_device_rnc_destruct_complete,
780 sci_dev);
781 } else
782 status = scic_sds_remote_device_terminate_requests(sci_dev);
784 return status;
789 * @device: The struct scic_sds_remote_device object which is cast to a
790 * struct scic_sds_remote_device object.
792 * This is the ready state device reset handler enum sci_status
794 enum sci_status scic_sds_remote_device_ready_state_reset_handler(
795 struct scic_sds_remote_device *sci_dev)
797 /* Request the parent state machine to transition to the stopping state */
798 sci_base_state_machine_change_state(&sci_dev->state_machine,
799 SCI_BASE_REMOTE_DEVICE_STATE_RESETTING);
801 return SCI_SUCCESS;
805 * This method will attempt to start a task request for this device object. The
806 * remote device object will issue the start request for the task and if
807 * successful it will start the request for the port object then increment its
808 * own requet count. enum sci_status SCI_SUCCESS if the task request is started for
809 * this device object. SCI_FAILURE_INSUFFICIENT_RESOURCES if the io request
810 * object could not get the resources to start.
812 static enum sci_status scic_sds_remote_device_ready_state_start_task_handler(
813 struct scic_sds_remote_device *sci_dev,
814 struct scic_sds_request *request)
816 enum sci_status result;
818 /* See if the port is in a state where we can start the IO request */
819 result = scic_sds_port_start_io(
820 scic_sds_remote_device_get_port(sci_dev), sci_dev, request);
822 if (result == SCI_SUCCESS) {
823 result = scic_sds_remote_node_context_start_task(&sci_dev->rnc,
824 request);
825 if (result == SCI_SUCCESS)
826 result = scic_sds_request_start(request);
828 scic_sds_remote_device_start_request(sci_dev, request, result);
831 return result;
835 * This method will attempt to start an io request for this device object. The
836 * remote device object will issue the start request for the io and if
837 * successful it will start the request for the port object then increment its
838 * own requet count. enum sci_status SCI_SUCCESS if the io request is started for
839 * this device object. SCI_FAILURE_INSUFFICIENT_RESOURCES if the io request
840 * object could not get the resources to start.
842 static enum sci_status scic_sds_remote_device_ready_state_start_io_handler(
843 struct scic_sds_remote_device *sci_dev,
844 struct scic_sds_request *request)
846 enum sci_status result;
848 /* See if the port is in a state where we can start the IO request */
849 result = scic_sds_port_start_io(
850 scic_sds_remote_device_get_port(sci_dev), sci_dev, request);
852 if (result == SCI_SUCCESS) {
853 result = scic_sds_remote_node_context_start_io(&sci_dev->rnc, request);
854 if (result == SCI_SUCCESS)
855 result = scic_sds_request_start(request);
857 scic_sds_remote_device_start_request(sci_dev, request, result);
860 return result;
864 * This method will complete the request for the remote device object. The
865 * method will call the completion handler for the request object and if
866 * successful it will complete the request on the port object then decrement
867 * its own started_request_count. enum sci_status
869 static enum sci_status scic_sds_remote_device_ready_state_complete_request_handler(
870 struct scic_sds_remote_device *sci_dev,
871 struct scic_sds_request *request)
873 enum sci_status result;
875 result = scic_sds_request_complete(request);
877 if (result != SCI_SUCCESS)
878 return result;
880 /* See if the port is in a state
881 * where we can start the IO request */
882 result = scic_sds_port_complete_io(
883 scic_sds_remote_device_get_port(sci_dev),
884 sci_dev, request);
886 if (result == SCI_SUCCESS)
887 scic_sds_remote_device_decrement_request_count(sci_dev);
889 return result;
893 * *****************************************************************************
894 * * STOPPING STATE HANDLERS
895 * ***************************************************************************** */
899 * @sci_dev: The struct scic_sds_remote_device which is cast into a
900 * struct scic_sds_remote_device.
902 * This method will stop a struct scic_sds_remote_device that is already in the
903 * SCI_BASE_REMOTE_DEVICE_STATE_STOPPING state. This is not considered an error
904 * since we allow a stop request on a device that is alreay stopping or
905 * stopped. enum sci_status SCI_SUCCESS
907 static enum sci_status scic_sds_remote_device_stopping_state_stop_handler(
908 struct scic_sds_remote_device *device)
911 * All requests should have been terminated, but if there is an
912 * attempt to stop a device already in the stopping state, then
913 * try again to terminate. */
914 return scic_sds_remote_device_terminate_requests(device);
920 * @device: The device object for which the request is completing.
921 * @request: The task request that is being completed.
923 * This method completes requests for this struct scic_sds_remote_device while it is
924 * in the SCI_BASE_REMOTE_DEVICE_STATE_STOPPING state. This method calls the
925 * complete method for the request object and if that is successful the port
926 * object is called to complete the task request. Then the device object itself
927 * completes the task request. If struct scic_sds_remote_device started_request_count
928 * goes to 0 and the invalidate RNC request has completed the device object can
929 * transition to the SCI_BASE_REMOTE_DEVICE_STATE_STOPPED. enum sci_status
931 static enum sci_status scic_sds_remote_device_stopping_state_complete_request_handler(
932 struct scic_sds_remote_device *sci_dev,
933 struct scic_sds_request *request)
935 enum sci_status status = SCI_SUCCESS;
937 status = scic_sds_request_complete(request);
939 if (status != SCI_SUCCESS)
940 return status;
942 status = scic_sds_port_complete_io(scic_sds_remote_device_get_port(sci_dev),
943 sci_dev, request);
944 if (status != SCI_SUCCESS)
945 return status;
947 scic_sds_remote_device_decrement_request_count(sci_dev);
949 if (scic_sds_remote_device_get_request_count(sci_dev) == 0)
950 scic_sds_remote_node_context_destruct(&sci_dev->rnc,
951 scic_sds_cb_remote_device_rnc_destruct_complete,
952 sci_dev);
953 return SCI_SUCCESS;
958 * @device: The struct scic_sds_remote_device which is to be cast into a
959 * struct scic_sds_remote_device object.
961 * This method will complete the reset operation when the device is in the
962 * resetting state. enum sci_status
964 static enum sci_status scic_sds_remote_device_resetting_state_reset_complete_handler(
965 struct scic_sds_remote_device *sci_dev)
968 sci_base_state_machine_change_state(
969 &sci_dev->state_machine,
970 SCI_BASE_REMOTE_DEVICE_STATE_READY
973 return SCI_SUCCESS;
978 * @device: The struct scic_sds_remote_device which is to be cast into a
979 * struct scic_sds_remote_device object.
981 * This method will stop the remote device while in the resetting state.
982 * enum sci_status
984 static enum sci_status scic_sds_remote_device_resetting_state_stop_handler(
985 struct scic_sds_remote_device *sci_dev)
987 sci_base_state_machine_change_state(
988 &sci_dev->state_machine,
989 SCI_BASE_REMOTE_DEVICE_STATE_STOPPING
992 return SCI_SUCCESS;
996 * This method completes requests for this struct scic_sds_remote_device while it is
997 * in the SCI_BASE_REMOTE_DEVICE_STATE_RESETTING state. This method calls the
998 * complete method for the request object and if that is successful the port
999 * object is called to complete the task request. Then the device object itself
1000 * completes the task request. enum sci_status
1002 static enum sci_status scic_sds_remote_device_resetting_state_complete_request_handler(
1003 struct scic_sds_remote_device *sci_dev,
1004 struct scic_sds_request *request)
1006 enum sci_status status = SCI_SUCCESS;
1008 status = scic_sds_request_complete(request);
1010 if (status == SCI_SUCCESS) {
1011 status = scic_sds_port_complete_io(
1012 scic_sds_remote_device_get_port(sci_dev),
1013 sci_dev, request);
1015 if (status == SCI_SUCCESS) {
1016 scic_sds_remote_device_decrement_request_count(sci_dev);
1020 return status;
1023 static const struct scic_sds_remote_device_state_handler scic_sds_remote_device_state_handler_table[] = {
1024 [SCI_BASE_REMOTE_DEVICE_STATE_INITIAL] = {
1025 .start_handler = scic_sds_remote_device_default_start_handler,
1026 .stop_handler = scic_sds_remote_device_default_stop_handler,
1027 .fail_handler = scic_sds_remote_device_default_fail_handler,
1028 .destruct_handler = scic_sds_remote_device_default_destruct_handler,
1029 .reset_handler = scic_sds_remote_device_default_reset_handler,
1030 .reset_complete_handler = scic_sds_remote_device_default_reset_complete_handler,
1031 .start_io_handler = scic_sds_remote_device_default_start_request_handler,
1032 .complete_io_handler = scic_sds_remote_device_default_complete_request_handler,
1033 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
1034 .start_task_handler = scic_sds_remote_device_default_start_request_handler,
1035 .complete_task_handler = scic_sds_remote_device_default_complete_request_handler,
1036 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1037 .resume_handler = scic_sds_remote_device_default_resume_handler,
1038 .event_handler = scic_sds_remote_device_default_event_handler,
1039 .frame_handler = scic_sds_remote_device_default_frame_handler
1041 [SCI_BASE_REMOTE_DEVICE_STATE_STOPPED] = {
1042 .start_handler = scic_sds_remote_device_stopped_state_start_handler,
1043 .stop_handler = scic_sds_remote_device_stopped_state_stop_handler,
1044 .fail_handler = scic_sds_remote_device_default_fail_handler,
1045 .destruct_handler = scic_sds_remote_device_stopped_state_destruct_handler,
1046 .reset_handler = scic_sds_remote_device_default_reset_handler,
1047 .reset_complete_handler = scic_sds_remote_device_default_reset_complete_handler,
1048 .start_io_handler = scic_sds_remote_device_default_start_request_handler,
1049 .complete_io_handler = scic_sds_remote_device_default_complete_request_handler,
1050 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
1051 .start_task_handler = scic_sds_remote_device_default_start_request_handler,
1052 .complete_task_handler = scic_sds_remote_device_default_complete_request_handler,
1053 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1054 .resume_handler = scic_sds_remote_device_default_resume_handler,
1055 .event_handler = scic_sds_remote_device_default_event_handler,
1056 .frame_handler = scic_sds_remote_device_default_frame_handler
1058 [SCI_BASE_REMOTE_DEVICE_STATE_STARTING] = {
1059 .start_handler = scic_sds_remote_device_default_start_handler,
1060 .stop_handler = scic_sds_remote_device_starting_state_stop_handler,
1061 .fail_handler = scic_sds_remote_device_default_fail_handler,
1062 .destruct_handler = scic_sds_remote_device_default_destruct_handler,
1063 .reset_handler = scic_sds_remote_device_default_reset_handler,
1064 .reset_complete_handler = scic_sds_remote_device_default_reset_complete_handler,
1065 .start_io_handler = scic_sds_remote_device_default_start_request_handler,
1066 .complete_io_handler = scic_sds_remote_device_default_complete_request_handler,
1067 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
1068 .start_task_handler = scic_sds_remote_device_default_start_request_handler,
1069 .complete_task_handler = scic_sds_remote_device_default_complete_request_handler,
1070 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1071 .resume_handler = scic_sds_remote_device_default_resume_handler,
1072 .event_handler = scic_sds_remote_device_general_event_handler,
1073 .frame_handler = scic_sds_remote_device_default_frame_handler
1075 [SCI_BASE_REMOTE_DEVICE_STATE_READY] = {
1076 .start_handler = scic_sds_remote_device_default_start_handler,
1077 .stop_handler = scic_sds_remote_device_ready_state_stop_handler,
1078 .fail_handler = scic_sds_remote_device_default_fail_handler,
1079 .destruct_handler = scic_sds_remote_device_default_destruct_handler,
1080 .reset_handler = scic_sds_remote_device_ready_state_reset_handler,
1081 .reset_complete_handler = scic_sds_remote_device_default_reset_complete_handler,
1082 .start_io_handler = scic_sds_remote_device_ready_state_start_io_handler,
1083 .complete_io_handler = scic_sds_remote_device_ready_state_complete_request_handler,
1084 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
1085 .start_task_handler = scic_sds_remote_device_ready_state_start_task_handler,
1086 .complete_task_handler = scic_sds_remote_device_ready_state_complete_request_handler,
1087 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1088 .resume_handler = scic_sds_remote_device_default_resume_handler,
1089 .event_handler = scic_sds_remote_device_general_event_handler,
1090 .frame_handler = scic_sds_remote_device_general_frame_handler,
1092 [SCI_BASE_REMOTE_DEVICE_STATE_STOPPING] = {
1093 .start_handler = scic_sds_remote_device_default_start_handler,
1094 .stop_handler = scic_sds_remote_device_stopping_state_stop_handler,
1095 .fail_handler = scic_sds_remote_device_default_fail_handler,
1096 .destruct_handler = scic_sds_remote_device_default_destruct_handler,
1097 .reset_handler = scic_sds_remote_device_default_reset_handler,
1098 .reset_complete_handler = scic_sds_remote_device_default_reset_complete_handler,
1099 .start_io_handler = scic_sds_remote_device_default_start_request_handler,
1100 .complete_io_handler = scic_sds_remote_device_stopping_state_complete_request_handler,
1101 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
1102 .start_task_handler = scic_sds_remote_device_default_start_request_handler,
1103 .complete_task_handler = scic_sds_remote_device_stopping_state_complete_request_handler,
1104 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1105 .resume_handler = scic_sds_remote_device_default_resume_handler,
1106 .event_handler = scic_sds_remote_device_general_event_handler,
1107 .frame_handler = scic_sds_remote_device_general_frame_handler
1109 [SCI_BASE_REMOTE_DEVICE_STATE_FAILED] = {
1110 .start_handler = scic_sds_remote_device_default_start_handler,
1111 .stop_handler = scic_sds_remote_device_default_stop_handler,
1112 .fail_handler = scic_sds_remote_device_default_fail_handler,
1113 .destruct_handler = scic_sds_remote_device_default_destruct_handler,
1114 .reset_handler = scic_sds_remote_device_default_reset_handler,
1115 .reset_complete_handler = scic_sds_remote_device_default_reset_complete_handler,
1116 .start_io_handler = scic_sds_remote_device_default_start_request_handler,
1117 .complete_io_handler = scic_sds_remote_device_default_complete_request_handler,
1118 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
1119 .start_task_handler = scic_sds_remote_device_default_start_request_handler,
1120 .complete_task_handler = scic_sds_remote_device_default_complete_request_handler,
1121 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1122 .resume_handler = scic_sds_remote_device_default_resume_handler,
1123 .event_handler = scic_sds_remote_device_default_event_handler,
1124 .frame_handler = scic_sds_remote_device_general_frame_handler
1126 [SCI_BASE_REMOTE_DEVICE_STATE_RESETTING] = {
1127 .start_handler = scic_sds_remote_device_default_start_handler,
1128 .stop_handler = scic_sds_remote_device_resetting_state_stop_handler,
1129 .fail_handler = scic_sds_remote_device_default_fail_handler,
1130 .destruct_handler = scic_sds_remote_device_default_destruct_handler,
1131 .reset_handler = scic_sds_remote_device_default_reset_handler,
1132 .reset_complete_handler = scic_sds_remote_device_resetting_state_reset_complete_handler,
1133 .start_io_handler = scic_sds_remote_device_default_start_request_handler,
1134 .complete_io_handler = scic_sds_remote_device_resetting_state_complete_request_handler,
1135 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
1136 .start_task_handler = scic_sds_remote_device_default_start_request_handler,
1137 .complete_task_handler = scic_sds_remote_device_resetting_state_complete_request_handler,
1138 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1139 .resume_handler = scic_sds_remote_device_default_resume_handler,
1140 .event_handler = scic_sds_remote_device_default_event_handler,
1141 .frame_handler = scic_sds_remote_device_general_frame_handler
1143 [SCI_BASE_REMOTE_DEVICE_STATE_FINAL] = {
1144 .start_handler = scic_sds_remote_device_default_start_handler,
1145 .stop_handler = scic_sds_remote_device_default_stop_handler,
1146 .fail_handler = scic_sds_remote_device_default_fail_handler,
1147 .destruct_handler = scic_sds_remote_device_default_destruct_handler,
1148 .reset_handler = scic_sds_remote_device_default_reset_handler,
1149 .reset_complete_handler = scic_sds_remote_device_default_reset_complete_handler,
1150 .start_io_handler = scic_sds_remote_device_default_start_request_handler,
1151 .complete_io_handler = scic_sds_remote_device_default_complete_request_handler,
1152 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
1153 .start_task_handler = scic_sds_remote_device_default_start_request_handler,
1154 .complete_task_handler = scic_sds_remote_device_default_complete_request_handler,
1155 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1156 .resume_handler = scic_sds_remote_device_default_resume_handler,
1157 .event_handler = scic_sds_remote_device_default_event_handler,
1158 .frame_handler = scic_sds_remote_device_default_frame_handler
1162 static void scic_sds_remote_device_initial_state_enter(
1163 struct sci_base_object *object)
1165 struct scic_sds_remote_device *sci_dev = (struct scic_sds_remote_device *)object;
1167 sci_dev = container_of(object, typeof(*sci_dev), parent);
1168 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1169 SCI_BASE_REMOTE_DEVICE_STATE_INITIAL);
1171 /* Initial state is a transitional state to the stopped state */
1172 sci_base_state_machine_change_state(&sci_dev->state_machine,
1173 SCI_BASE_REMOTE_DEVICE_STATE_STOPPED);
1177 * isci_remote_device_change_state() - This function gets the status of the
1178 * remote_device object.
1179 * @isci_device: This parameter points to the isci_remote_device object
1181 * status of the object as a isci_status enum.
1183 void isci_remote_device_change_state(
1184 struct isci_remote_device *isci_device,
1185 enum isci_status status)
1187 unsigned long flags;
1189 spin_lock_irqsave(&isci_device->state_lock, flags);
1190 isci_device->status = status;
1191 spin_unlock_irqrestore(&isci_device->state_lock, flags);
1195 * scic_remote_device_destruct() - free remote node context and destruct
1196 * @remote_device: This parameter specifies the remote device to be destructed.
1198 * Remote device objects are a limited resource. As such, they must be
1199 * protected. Thus calls to construct and destruct are mutually exclusive and
1200 * non-reentrant. The return value shall indicate if the device was
1201 * successfully destructed or if some failure occurred. enum sci_status This value
1202 * is returned if the device is successfully destructed.
1203 * SCI_FAILURE_INVALID_REMOTE_DEVICE This value is returned if the supplied
1204 * device isn't valid (e.g. it's already been destoryed, the handle isn't
1205 * valid, etc.).
1207 static enum sci_status scic_remote_device_destruct(struct scic_sds_remote_device *sci_dev)
1209 return sci_dev->state_handlers->destruct_handler(sci_dev);
1213 * isci_remote_device_deconstruct() - This function frees an isci_remote_device.
1214 * @ihost: This parameter specifies the isci host object.
1215 * @idev: This parameter specifies the remote device to be freed.
1218 static void isci_remote_device_deconstruct(struct isci_host *ihost, struct isci_remote_device *idev)
1220 dev_dbg(&ihost->pdev->dev,
1221 "%s: isci_device = %p\n", __func__, idev);
1223 /* There should not be any outstanding io's. All paths to
1224 * here should go through isci_remote_device_nuke_requests.
1225 * If we hit this condition, we will need a way to complete
1226 * io requests in process */
1227 while (!list_empty(&idev->reqs_in_process)) {
1229 dev_err(&ihost->pdev->dev,
1230 "%s: ** request list not empty! **\n", __func__);
1231 BUG();
1234 scic_remote_device_destruct(&idev->sci);
1235 idev->domain_dev->lldd_dev = NULL;
1236 idev->domain_dev = NULL;
1237 idev->isci_port = NULL;
1238 list_del_init(&idev->node);
1240 clear_bit(IDEV_START_PENDING, &idev->flags);
1241 clear_bit(IDEV_STOP_PENDING, &idev->flags);
1242 wake_up(&ihost->eventq);
1246 * isci_remote_device_stop_complete() - This function is called by the scic
1247 * when the remote device stop has completed. We mark the isci device as not
1248 * ready and remove the isci remote device.
1249 * @ihost: This parameter specifies the isci host object.
1250 * @idev: This parameter specifies the remote device.
1251 * @status: This parameter specifies status of the completion.
1254 static void isci_remote_device_stop_complete(struct isci_host *ihost,
1255 struct isci_remote_device *idev)
1257 dev_dbg(&ihost->pdev->dev, "%s: complete idev = %p\n", __func__, idev);
1259 isci_remote_device_change_state(idev, isci_stopped);
1261 /* after stop, we can tear down resources. */
1262 isci_remote_device_deconstruct(ihost, idev);
1265 static void scic_sds_remote_device_stopped_state_enter(
1266 struct sci_base_object *object)
1268 struct scic_sds_remote_device *sci_dev;
1269 struct scic_sds_controller *scic;
1270 struct isci_remote_device *idev;
1271 struct isci_host *ihost;
1272 u32 prev_state;
1274 sci_dev = container_of(object, typeof(*sci_dev), parent);
1275 scic = scic_sds_remote_device_get_controller(sci_dev);
1276 ihost = sci_object_get_association(scic);
1277 idev = sci_object_get_association(sci_dev);
1279 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1280 SCI_BASE_REMOTE_DEVICE_STATE_STOPPED);
1282 /* If we are entering from the stopping state let the SCI User know that
1283 * the stop operation has completed.
1285 prev_state = sci_dev->state_machine.previous_state_id;
1286 if (prev_state == SCI_BASE_REMOTE_DEVICE_STATE_STOPPING)
1287 isci_remote_device_stop_complete(ihost, idev);
1289 scic_sds_controller_remote_device_stopped(scic, sci_dev);
1292 static void scic_sds_remote_device_starting_state_enter(struct sci_base_object *object)
1294 struct scic_sds_remote_device *sci_dev = container_of(object, typeof(*sci_dev),
1295 parent);
1296 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1297 struct isci_host *ihost = sci_object_get_association(scic);
1298 struct isci_remote_device *idev = sci_object_get_association(sci_dev);
1300 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1301 SCI_BASE_REMOTE_DEVICE_STATE_STARTING);
1303 isci_remote_device_not_ready(ihost, idev,
1304 SCIC_REMOTE_DEVICE_NOT_READY_START_REQUESTED);
1307 static void scic_sds_remote_device_ready_state_enter(struct sci_base_object *object)
1309 struct scic_sds_remote_device *sci_dev = container_of(object, typeof(*sci_dev),
1310 parent);
1311 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1312 struct isci_host *ihost = sci_object_get_association(scic);
1313 struct isci_remote_device *idev = sci_object_get_association(sci_dev);
1315 SET_STATE_HANDLER(sci_dev,
1316 scic_sds_remote_device_state_handler_table,
1317 SCI_BASE_REMOTE_DEVICE_STATE_READY);
1319 scic->remote_device_sequence[sci_dev->rnc.remote_node_index]++;
1321 if (sci_dev->has_ready_substate_machine)
1322 sci_base_state_machine_start(&sci_dev->ready_substate_machine);
1323 else
1324 isci_remote_device_ready(ihost, idev);
1327 static void scic_sds_remote_device_ready_state_exit(
1328 struct sci_base_object *object)
1330 struct scic_sds_remote_device *sci_dev = container_of(object, typeof(*sci_dev),
1331 parent);
1332 if (sci_dev->has_ready_substate_machine)
1333 sci_base_state_machine_stop(&sci_dev->ready_substate_machine);
1334 else {
1335 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1336 struct isci_host *ihost = sci_object_get_association(scic);
1337 struct isci_remote_device *idev = sci_object_get_association(sci_dev);
1339 isci_remote_device_not_ready(ihost, idev,
1340 SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED);
1344 static void scic_sds_remote_device_stopping_state_enter(
1345 struct sci_base_object *object)
1347 struct scic_sds_remote_device *sci_dev = (struct scic_sds_remote_device *)object;
1349 SET_STATE_HANDLER(
1350 sci_dev,
1351 scic_sds_remote_device_state_handler_table,
1352 SCI_BASE_REMOTE_DEVICE_STATE_STOPPING
1356 static void scic_sds_remote_device_failed_state_enter(
1357 struct sci_base_object *object)
1359 struct scic_sds_remote_device *sci_dev = (struct scic_sds_remote_device *)object;
1361 SET_STATE_HANDLER(
1362 sci_dev,
1363 scic_sds_remote_device_state_handler_table,
1364 SCI_BASE_REMOTE_DEVICE_STATE_FAILED
1368 static void scic_sds_remote_device_resetting_state_enter(
1369 struct sci_base_object *object)
1371 struct scic_sds_remote_device *sci_dev = (struct scic_sds_remote_device *)object;
1373 SET_STATE_HANDLER(
1374 sci_dev,
1375 scic_sds_remote_device_state_handler_table,
1376 SCI_BASE_REMOTE_DEVICE_STATE_RESETTING
1379 scic_sds_remote_node_context_suspend(
1380 &sci_dev->rnc, SCI_SOFTWARE_SUSPENSION, NULL, NULL);
1383 static void scic_sds_remote_device_resetting_state_exit(
1384 struct sci_base_object *object)
1386 struct scic_sds_remote_device *sci_dev = (struct scic_sds_remote_device *)object;
1388 scic_sds_remote_node_context_resume(&sci_dev->rnc, NULL, NULL);
1391 static void scic_sds_remote_device_final_state_enter(
1392 struct sci_base_object *object)
1394 struct scic_sds_remote_device *sci_dev = (struct scic_sds_remote_device *)object;
1396 SET_STATE_HANDLER(
1397 sci_dev,
1398 scic_sds_remote_device_state_handler_table,
1399 SCI_BASE_REMOTE_DEVICE_STATE_FINAL
1404 static const struct sci_base_state scic_sds_remote_device_state_table[] = {
1405 [SCI_BASE_REMOTE_DEVICE_STATE_INITIAL] = {
1406 .enter_state = scic_sds_remote_device_initial_state_enter,
1408 [SCI_BASE_REMOTE_DEVICE_STATE_STOPPED] = {
1409 .enter_state = scic_sds_remote_device_stopped_state_enter,
1411 [SCI_BASE_REMOTE_DEVICE_STATE_STARTING] = {
1412 .enter_state = scic_sds_remote_device_starting_state_enter,
1414 [SCI_BASE_REMOTE_DEVICE_STATE_READY] = {
1415 .enter_state = scic_sds_remote_device_ready_state_enter,
1416 .exit_state = scic_sds_remote_device_ready_state_exit
1418 [SCI_BASE_REMOTE_DEVICE_STATE_STOPPING] = {
1419 .enter_state = scic_sds_remote_device_stopping_state_enter,
1421 [SCI_BASE_REMOTE_DEVICE_STATE_FAILED] = {
1422 .enter_state = scic_sds_remote_device_failed_state_enter,
1424 [SCI_BASE_REMOTE_DEVICE_STATE_RESETTING] = {
1425 .enter_state = scic_sds_remote_device_resetting_state_enter,
1426 .exit_state = scic_sds_remote_device_resetting_state_exit
1428 [SCI_BASE_REMOTE_DEVICE_STATE_FINAL] = {
1429 .enter_state = scic_sds_remote_device_final_state_enter,
1434 * scic_remote_device_construct() - common construction
1435 * @sci_port: SAS/SATA port through which this device is accessed.
1436 * @sci_dev: remote device to construct
1438 * This routine just performs benign initialization and does not
1439 * allocate the remote_node_context which is left to
1440 * scic_remote_device_[de]a_construct(). scic_remote_device_destruct()
1441 * frees the remote_node_context(s) for the device.
1443 static void scic_remote_device_construct(struct scic_sds_port *sci_port,
1444 struct scic_sds_remote_device *sci_dev)
1446 sci_dev->owning_port = sci_port;
1447 sci_dev->started_request_count = 0;
1448 sci_dev->parent.private = NULL;
1450 sci_base_state_machine_construct(
1451 &sci_dev->state_machine,
1452 &sci_dev->parent,
1453 scic_sds_remote_device_state_table,
1454 SCI_BASE_REMOTE_DEVICE_STATE_INITIAL
1457 sci_base_state_machine_start(
1458 &sci_dev->state_machine
1461 scic_sds_remote_node_context_construct(&sci_dev->rnc,
1462 SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX);
1464 sci_object_set_association(&sci_dev->rnc, sci_dev);
1468 * scic_remote_device_da_construct() - construct direct attached device.
1470 * The information (e.g. IAF, Signature FIS, etc.) necessary to build
1471 * the device is known to the SCI Core since it is contained in the
1472 * scic_phy object. Remote node context(s) is/are a global resource
1473 * allocated by this routine, freed by scic_remote_device_destruct().
1475 * Returns:
1476 * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
1477 * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
1478 * sata-only controller instance.
1479 * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
1481 static enum sci_status scic_remote_device_da_construct(struct scic_sds_port *sci_port,
1482 struct scic_sds_remote_device *sci_dev)
1484 enum sci_status status;
1485 u16 remote_node_index;
1486 struct domain_device *dev = sci_dev_to_domain(sci_dev);
1488 scic_remote_device_construct(sci_port, sci_dev);
1491 * This information is request to determine how many remote node context
1492 * entries will be needed to store the remote node.
1494 sci_dev->is_direct_attached = true;
1495 status = scic_sds_controller_allocate_remote_node_context(sci_port->owning_controller,
1496 sci_dev,
1497 &remote_node_index);
1499 if (status != SCI_SUCCESS)
1500 return status;
1502 sci_dev->rnc.remote_node_index = remote_node_index;
1504 if (dev->dev_type == SAS_END_DEV)
1505 sci_dev->has_ready_substate_machine = false;
1506 else if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP)) {
1507 sci_dev->has_ready_substate_machine = true;
1509 sci_base_state_machine_construct(
1510 &sci_dev->ready_substate_machine,
1511 &sci_dev->parent,
1512 scic_sds_stp_remote_device_ready_substate_table,
1513 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
1514 } else if (dev_is_expander(dev)) {
1515 sci_dev->has_ready_substate_machine = true;
1517 /* add the SMP ready substate machine construction here */
1518 sci_base_state_machine_construct(
1519 &sci_dev->ready_substate_machine,
1520 &sci_dev->parent,
1521 scic_sds_smp_remote_device_ready_substate_table,
1522 SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
1523 } else
1524 return SCI_FAILURE_UNSUPPORTED_PROTOCOL;
1526 sci_dev->connection_rate = scic_sds_port_get_max_allowed_speed(sci_port);
1528 /* / @todo Should I assign the port width by reading all of the phys on the port? */
1529 sci_dev->device_port_width = 1;
1531 return SCI_SUCCESS;
1535 * scic_remote_device_ea_construct() - construct expander attached device
1537 * Remote node context(s) is/are a global resource allocated by this
1538 * routine, freed by scic_remote_device_destruct().
1540 * Returns:
1541 * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
1542 * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
1543 * sata-only controller instance.
1544 * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
1546 static enum sci_status scic_remote_device_ea_construct(struct scic_sds_port *sci_port,
1547 struct scic_sds_remote_device *sci_dev)
1549 struct scic_sds_controller *scic = sci_port->owning_controller;
1550 struct domain_device *dev = sci_dev_to_domain(sci_dev);
1551 enum sci_status status;
1553 scic_remote_device_construct(sci_port, sci_dev);
1555 status = scic_sds_controller_allocate_remote_node_context(
1556 scic, sci_dev, &sci_dev->rnc.remote_node_index);
1557 if (status != SCI_SUCCESS)
1558 return status;
1560 if (dev->dev_type == SAS_END_DEV)
1561 sci_dev->has_ready_substate_machine = false;
1562 else if (dev_is_expander(dev)) {
1563 sci_dev->has_ready_substate_machine = true;
1565 /* add the SMP ready substate machine construction here */
1566 sci_base_state_machine_construct(
1567 &sci_dev->ready_substate_machine,
1568 &sci_dev->parent,
1569 scic_sds_smp_remote_device_ready_substate_table,
1570 SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
1571 } else if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP)) {
1572 sci_dev->has_ready_substate_machine = true;
1574 sci_base_state_machine_construct(
1575 &sci_dev->ready_substate_machine,
1576 &sci_dev->parent,
1577 scic_sds_stp_remote_device_ready_substate_table,
1578 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
1582 * For SAS-2 the physical link rate is actually a logical link
1583 * rate that incorporates multiplexing. The SCU doesn't
1584 * incorporate multiplexing and for the purposes of the
1585 * connection the logical link rate is that same as the
1586 * physical. Furthermore, the SAS-2 and SAS-1.1 fields overlay
1587 * one another, so this code works for both situations. */
1588 sci_dev->connection_rate = min_t(u16, scic_sds_port_get_max_allowed_speed(sci_port),
1589 dev->linkrate);
1591 /* / @todo Should I assign the port width by reading all of the phys on the port? */
1592 sci_dev->device_port_width = 1;
1594 return status;
1598 * scic_remote_device_start() - This method will start the supplied remote
1599 * device. This method enables normal IO requests to flow through to the
1600 * remote device.
1601 * @remote_device: This parameter specifies the device to be started.
1602 * @timeout: This parameter specifies the number of milliseconds in which the
1603 * start operation should complete.
1605 * An indication of whether the device was successfully started. SCI_SUCCESS
1606 * This value is returned if the device was successfully started.
1607 * SCI_FAILURE_INVALID_PHY This value is returned if the user attempts to start
1608 * the device when there have been no phys added to it.
1610 static enum sci_status scic_remote_device_start(struct scic_sds_remote_device *sci_dev,
1611 u32 timeout)
1613 return sci_dev->state_handlers->start_handler(sci_dev);
1616 static enum sci_status isci_remote_device_construct(struct isci_port *iport,
1617 struct isci_remote_device *idev)
1619 struct scic_sds_port *sci_port = iport->sci_port_handle;
1620 struct isci_host *ihost = iport->isci_host;
1621 struct domain_device *dev = idev->domain_dev;
1622 enum sci_status status;
1624 if (dev->parent && dev_is_expander(dev->parent))
1625 status = scic_remote_device_ea_construct(sci_port, &idev->sci);
1626 else
1627 status = scic_remote_device_da_construct(sci_port, &idev->sci);
1629 if (status != SCI_SUCCESS) {
1630 dev_dbg(&ihost->pdev->dev, "%s: construct failed: %d\n",
1631 __func__, status);
1633 return status;
1636 /* XXX will be killed with sci_base_object removal */
1637 sci_object_set_association(&idev->sci, idev);
1639 /* start the device. */
1640 status = scic_remote_device_start(&idev->sci, ISCI_REMOTE_DEVICE_START_TIMEOUT);
1642 if (status != SCI_SUCCESS)
1643 dev_warn(&ihost->pdev->dev, "remote device start failed: %d\n",
1644 status);
1646 return status;
1649 void isci_remote_device_nuke_requests(struct isci_host *ihost, struct isci_remote_device *idev)
1651 DECLARE_COMPLETION_ONSTACK(aborted_task_completion);
1653 dev_dbg(&ihost->pdev->dev,
1654 "%s: idev = %p\n", __func__, idev);
1656 /* Cleanup all requests pending for this device. */
1657 isci_terminate_pending_requests(ihost, idev, terminating);
1659 dev_dbg(&ihost->pdev->dev,
1660 "%s: idev = %p, done\n", __func__, idev);
1664 * This function builds the isci_remote_device when a libsas dev_found message
1665 * is received.
1666 * @isci_host: This parameter specifies the isci host object.
1667 * @port: This parameter specifies the isci_port conected to this device.
1669 * pointer to new isci_remote_device.
1671 static struct isci_remote_device *
1672 isci_remote_device_alloc(struct isci_host *ihost, struct isci_port *iport)
1674 struct isci_remote_device *idev;
1675 int i;
1677 for (i = 0; i < SCI_MAX_REMOTE_DEVICES; i++) {
1678 idev = &ihost->devices[i];
1679 if (!test_and_set_bit(IDEV_ALLOCATED, &idev->flags))
1680 break;
1683 if (i >= SCI_MAX_REMOTE_DEVICES) {
1684 dev_warn(&ihost->pdev->dev, "%s: failed\n", __func__);
1685 return NULL;
1688 if (WARN_ONCE(!list_empty(&idev->reqs_in_process), "found requests in process\n"))
1689 return NULL;
1691 if (WARN_ONCE(!list_empty(&idev->node), "found non-idle remote device\n"))
1692 return NULL;
1694 isci_remote_device_change_state(idev, isci_freed);
1696 return idev;
1700 * isci_remote_device_ready() - This function is called by the scic when the
1701 * remote device is ready. We mark the isci device as ready and signal the
1702 * waiting proccess.
1703 * @ihost: our valid isci_host
1704 * @idev: remote device
1707 void isci_remote_device_ready(struct isci_host *ihost, struct isci_remote_device *idev)
1709 dev_dbg(&ihost->pdev->dev,
1710 "%s: idev = %p\n", __func__, idev);
1712 isci_remote_device_change_state(idev, isci_ready_for_io);
1713 if (test_and_clear_bit(IDEV_START_PENDING, &idev->flags))
1714 wake_up(&ihost->eventq);
1718 * isci_remote_device_not_ready() - This function is called by the scic when
1719 * the remote device is not ready. We mark the isci device as ready (not
1720 * "ready_for_io") and signal the waiting proccess.
1721 * @isci_host: This parameter specifies the isci host object.
1722 * @isci_device: This parameter specifies the remote device
1725 void isci_remote_device_not_ready(struct isci_host *ihost,
1726 struct isci_remote_device *idev, u32 reason)
1728 dev_dbg(&ihost->pdev->dev,
1729 "%s: isci_device = %p\n", __func__, idev);
1731 if (reason == SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED)
1732 isci_remote_device_change_state(idev, isci_stopping);
1733 else
1734 /* device ready is actually a "not ready for io" state. */
1735 isci_remote_device_change_state(idev, isci_ready);
1739 * isci_remote_device_stop() - This function is called internally to stop the
1740 * remote device.
1741 * @isci_host: This parameter specifies the isci host object.
1742 * @isci_device: This parameter specifies the remote device.
1744 * The status of the scic request to stop.
1746 enum sci_status isci_remote_device_stop(struct isci_host *ihost, struct isci_remote_device *idev)
1748 enum sci_status status;
1749 unsigned long flags;
1751 dev_dbg(&ihost->pdev->dev,
1752 "%s: isci_device = %p\n", __func__, idev);
1754 isci_remote_device_change_state(idev, isci_stopping);
1756 /* Kill all outstanding requests. */
1757 isci_remote_device_nuke_requests(ihost, idev);
1759 set_bit(IDEV_STOP_PENDING, &idev->flags);
1761 spin_lock_irqsave(&ihost->scic_lock, flags);
1762 status = scic_remote_device_stop(&idev->sci, 50);
1763 spin_unlock_irqrestore(&ihost->scic_lock, flags);
1765 /* Wait for the stop complete callback. */
1766 if (status == SCI_SUCCESS) {
1767 wait_for_device_stop(ihost, idev);
1768 clear_bit(IDEV_ALLOCATED, &idev->flags);
1771 dev_dbg(&ihost->pdev->dev,
1772 "%s: idev = %p - after completion wait\n",
1773 __func__, idev);
1775 return status;
1779 * isci_remote_device_gone() - This function is called by libsas when a domain
1780 * device is removed.
1781 * @domain_device: This parameter specifies the libsas domain device.
1784 void isci_remote_device_gone(struct domain_device *dev)
1786 struct isci_host *ihost = dev_to_ihost(dev);
1787 struct isci_remote_device *idev = dev->lldd_dev;
1789 dev_dbg(&ihost->pdev->dev,
1790 "%s: domain_device = %p, isci_device = %p, isci_port = %p\n",
1791 __func__, dev, idev, idev->isci_port);
1793 isci_remote_device_stop(ihost, idev);
1798 * isci_remote_device_found() - This function is called by libsas when a remote
1799 * device is discovered. A remote device object is created and started. the
1800 * function then sleeps until the sci core device started message is
1801 * received.
1802 * @domain_device: This parameter specifies the libsas domain device.
1804 * status, zero indicates success.
1806 int isci_remote_device_found(struct domain_device *domain_dev)
1808 struct isci_host *isci_host = dev_to_ihost(domain_dev);
1809 struct isci_port *isci_port;
1810 struct isci_phy *isci_phy;
1811 struct asd_sas_port *sas_port;
1812 struct asd_sas_phy *sas_phy;
1813 struct isci_remote_device *isci_device;
1814 enum sci_status status;
1816 dev_dbg(&isci_host->pdev->dev,
1817 "%s: domain_device = %p\n", __func__, domain_dev);
1819 wait_for_start(isci_host);
1821 sas_port = domain_dev->port;
1822 sas_phy = list_first_entry(&sas_port->phy_list, struct asd_sas_phy,
1823 port_phy_el);
1824 isci_phy = to_isci_phy(sas_phy);
1825 isci_port = isci_phy->isci_port;
1827 /* we are being called for a device on this port,
1828 * so it has to come up eventually
1830 wait_for_completion(&isci_port->start_complete);
1832 if ((isci_stopping == isci_port_get_state(isci_port)) ||
1833 (isci_stopped == isci_port_get_state(isci_port)))
1834 return -ENODEV;
1836 isci_device = isci_remote_device_alloc(isci_host, isci_port);
1837 if (!isci_device)
1838 return -ENODEV;
1840 INIT_LIST_HEAD(&isci_device->node);
1841 domain_dev->lldd_dev = isci_device;
1842 isci_device->domain_dev = domain_dev;
1843 isci_device->isci_port = isci_port;
1844 isci_remote_device_change_state(isci_device, isci_starting);
1847 spin_lock_irq(&isci_host->scic_lock);
1848 list_add_tail(&isci_device->node, &isci_port->remote_dev_list);
1850 set_bit(IDEV_START_PENDING, &isci_device->flags);
1851 status = isci_remote_device_construct(isci_port, isci_device);
1852 spin_unlock_irq(&isci_host->scic_lock);
1854 dev_dbg(&isci_host->pdev->dev,
1855 "%s: isci_device = %p\n",
1856 __func__, isci_device);
1858 if (status != SCI_SUCCESS) {
1860 spin_lock_irq(&isci_host->scic_lock);
1861 isci_remote_device_deconstruct(
1862 isci_host,
1863 isci_device
1865 spin_unlock_irq(&isci_host->scic_lock);
1866 return -ENODEV;
1869 /* wait for the device ready callback. */
1870 wait_for_device_start(isci_host, isci_device);
1872 return 0;
1875 * isci_device_is_reset_pending() - This function will check if there is any
1876 * pending reset condition on the device.
1877 * @request: This parameter is the isci_device object.
1879 * true if there is a reset pending for the device.
1881 bool isci_device_is_reset_pending(
1882 struct isci_host *isci_host,
1883 struct isci_remote_device *isci_device)
1885 struct isci_request *isci_request;
1886 struct isci_request *tmp_req;
1887 bool reset_is_pending = false;
1888 unsigned long flags;
1890 dev_dbg(&isci_host->pdev->dev,
1891 "%s: isci_device = %p\n", __func__, isci_device);
1893 spin_lock_irqsave(&isci_host->scic_lock, flags);
1895 /* Check for reset on all pending requests. */
1896 list_for_each_entry_safe(isci_request, tmp_req,
1897 &isci_device->reqs_in_process, dev_node) {
1898 dev_dbg(&isci_host->pdev->dev,
1899 "%s: isci_device = %p request = %p\n",
1900 __func__, isci_device, isci_request);
1902 if (isci_request->ttype == io_task) {
1903 struct sas_task *task = isci_request_access_task(
1904 isci_request);
1906 spin_lock(&task->task_state_lock);
1907 if (task->task_state_flags & SAS_TASK_NEED_DEV_RESET)
1908 reset_is_pending = true;
1909 spin_unlock(&task->task_state_lock);
1913 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1915 dev_dbg(&isci_host->pdev->dev,
1916 "%s: isci_device = %p reset_is_pending = %d\n",
1917 __func__, isci_device, reset_is_pending);
1919 return reset_is_pending;
1923 * isci_device_clear_reset_pending() - This function will clear if any pending
1924 * reset condition flags on the device.
1925 * @request: This parameter is the isci_device object.
1927 * true if there is a reset pending for the device.
1929 void isci_device_clear_reset_pending(struct isci_host *ihost, struct isci_remote_device *idev)
1931 struct isci_request *isci_request;
1932 struct isci_request *tmp_req;
1933 unsigned long flags = 0;
1935 dev_dbg(&ihost->pdev->dev, "%s: idev=%p, ihost=%p\n",
1936 __func__, idev, ihost);
1938 spin_lock_irqsave(&ihost->scic_lock, flags);
1940 /* Clear reset pending on all pending requests. */
1941 list_for_each_entry_safe(isci_request, tmp_req,
1942 &idev->reqs_in_process, dev_node) {
1943 dev_dbg(&ihost->pdev->dev, "%s: idev = %p request = %p\n",
1944 __func__, idev, isci_request);
1946 if (isci_request->ttype == io_task) {
1948 unsigned long flags2;
1949 struct sas_task *task = isci_request_access_task(
1950 isci_request);
1952 spin_lock_irqsave(&task->task_state_lock, flags2);
1953 task->task_state_flags &= ~SAS_TASK_NEED_DEV_RESET;
1954 spin_unlock_irqrestore(&task->task_state_lock, flags2);
1957 spin_unlock_irqrestore(&ihost->scic_lock, flags);