isci: unify remote_device start_io_handlers
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / scsi / isci / remote_device.c
blobf4ba5be00c03e2387640ff280ae13eae90fcca02
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 "intel_ata.h"
57 #include "isci.h"
58 #include "port.h"
59 #include "remote_device.h"
60 #include "request.h"
61 #include "scic_controller.h"
62 #include "scic_io_request.h"
63 #include "scic_phy.h"
64 #include "scic_port.h"
65 #include "scic_sds_controller.h"
66 #include "scic_sds_phy.h"
67 #include "scic_sds_port.h"
68 #include "remote_node_context.h"
69 #include "scic_sds_request.h"
70 #include "sci_environment.h"
71 #include "sci_util.h"
72 #include "scu_event_codes.h"
73 #include "task.h"
75 /**
76 * isci_remote_device_change_state() - This function gets the status of the
77 * remote_device object.
78 * @isci_device: This parameter points to the isci_remote_device object
80 * status of the object as a isci_status enum.
82 void isci_remote_device_change_state(
83 struct isci_remote_device *isci_device,
84 enum isci_status status)
86 unsigned long flags;
88 spin_lock_irqsave(&isci_device->state_lock, flags);
89 isci_device->status = status;
90 spin_unlock_irqrestore(&isci_device->state_lock, flags);
93 /**
94 * isci_remote_device_not_ready() - This function is called by the scic when
95 * the remote device is not ready. We mark the isci device as ready (not
96 * "ready_for_io") and signal the waiting proccess.
97 * @isci_host: This parameter specifies the isci host object.
98 * @isci_device: This parameter specifies the remote device
101 static void isci_remote_device_not_ready(struct isci_host *ihost,
102 struct isci_remote_device *idev, u32 reason)
104 dev_dbg(&ihost->pdev->dev,
105 "%s: isci_device = %p\n", __func__, idev);
107 if (reason == SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED)
108 isci_remote_device_change_state(idev, isci_stopping);
109 else
110 /* device ready is actually a "not ready for io" state. */
111 isci_remote_device_change_state(idev, isci_ready);
115 * isci_remote_device_ready() - This function is called by the scic when the
116 * remote device is ready. We mark the isci device as ready and signal the
117 * waiting proccess.
118 * @ihost: our valid isci_host
119 * @idev: remote device
122 static void isci_remote_device_ready(struct isci_host *ihost, struct isci_remote_device *idev)
124 dev_dbg(&ihost->pdev->dev,
125 "%s: idev = %p\n", __func__, idev);
127 isci_remote_device_change_state(idev, isci_ready_for_io);
128 if (test_and_clear_bit(IDEV_START_PENDING, &idev->flags))
129 wake_up(&ihost->eventq);
132 /* called once the remote node context is ready to be freed.
133 * The remote device can now report that its stop operation is complete. none
135 static void rnc_destruct_done(void *_dev)
137 struct scic_sds_remote_device *sci_dev = _dev;
139 BUG_ON(sci_dev->started_request_count != 0);
140 sci_base_state_machine_change_state(&sci_dev->state_machine,
141 SCI_BASE_REMOTE_DEVICE_STATE_STOPPED);
144 static enum sci_status scic_sds_remote_device_terminate_requests(struct scic_sds_remote_device *sci_dev)
146 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
147 u32 i, request_count = sci_dev->started_request_count;
148 enum sci_status status = SCI_SUCCESS;
150 for (i = 0; i < SCI_MAX_IO_REQUESTS && i < request_count; i++) {
151 struct scic_sds_request *sci_req;
152 enum sci_status s;
154 sci_req = scic->io_request_table[i];
155 if (!sci_req || sci_req->target_device != sci_dev)
156 continue;
157 s = scic_controller_terminate_request(scic, sci_dev, sci_req);
158 if (s != SCI_SUCCESS)
159 status = s;
162 return status;
165 enum sci_status scic_remote_device_stop(struct scic_sds_remote_device *sci_dev,
166 u32 timeout)
168 struct sci_base_state_machine *sm = &sci_dev->state_machine;
169 enum scic_sds_remote_device_states state = sm->current_state_id;
171 switch (state) {
172 case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL:
173 case SCI_BASE_REMOTE_DEVICE_STATE_FAILED:
174 case SCI_BASE_REMOTE_DEVICE_STATE_FINAL:
175 default:
176 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
177 __func__, state);
178 return SCI_FAILURE_INVALID_STATE;
179 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED:
180 return SCI_SUCCESS;
181 case SCI_BASE_REMOTE_DEVICE_STATE_STARTING:
182 /* device not started so there had better be no requests */
183 BUG_ON(sci_dev->started_request_count != 0);
184 scic_sds_remote_node_context_destruct(&sci_dev->rnc,
185 rnc_destruct_done, sci_dev);
186 /* Transition to the stopping state and wait for the
187 * remote node to complete being posted and invalidated.
189 sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_STOPPING);
190 return SCI_SUCCESS;
191 case SCI_BASE_REMOTE_DEVICE_STATE_READY:
192 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
193 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
194 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ:
195 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
196 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
197 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
198 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
199 sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_STOPPING);
200 if (sci_dev->started_request_count == 0) {
201 scic_sds_remote_node_context_destruct(&sci_dev->rnc,
202 rnc_destruct_done, sci_dev);
203 return SCI_SUCCESS;
204 } else
205 return scic_sds_remote_device_terminate_requests(sci_dev);
206 break;
207 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING:
208 /* All requests should have been terminated, but if there is an
209 * attempt to stop a device already in the stopping state, then
210 * try again to terminate.
212 return scic_sds_remote_device_terminate_requests(sci_dev);
213 case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING:
214 sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_STOPPING);
215 return SCI_SUCCESS;
219 enum sci_status scic_remote_device_reset(struct scic_sds_remote_device *sci_dev)
221 struct sci_base_state_machine *sm = &sci_dev->state_machine;
222 enum scic_sds_remote_device_states state = sm->current_state_id;
224 switch (state) {
225 case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL:
226 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED:
227 case SCI_BASE_REMOTE_DEVICE_STATE_STARTING:
228 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
229 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
230 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING:
231 case SCI_BASE_REMOTE_DEVICE_STATE_FAILED:
232 case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING:
233 case SCI_BASE_REMOTE_DEVICE_STATE_FINAL:
234 default:
235 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
236 __func__, state);
237 return SCI_FAILURE_INVALID_STATE;
238 case SCI_BASE_REMOTE_DEVICE_STATE_READY:
239 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
240 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
241 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ:
242 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
243 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
244 sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_RESETTING);
245 return SCI_SUCCESS;
249 enum sci_status scic_remote_device_reset_complete(struct scic_sds_remote_device *sci_dev)
251 struct sci_base_state_machine *sm = &sci_dev->state_machine;
252 enum scic_sds_remote_device_states state = sm->current_state_id;
254 if (state != SCI_BASE_REMOTE_DEVICE_STATE_RESETTING) {
255 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
256 __func__, state);
257 return SCI_FAILURE_INVALID_STATE;
260 sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_READY);
261 return SCI_SUCCESS;
266 * @sci_dev: The remote device for which the suspend is being requested.
268 * This method invokes the remote device suspend state handler. enum sci_status
270 enum sci_status scic_sds_remote_device_suspend(
271 struct scic_sds_remote_device *sci_dev,
272 u32 suspend_type)
274 return sci_dev->state_handlers->suspend_handler(sci_dev, suspend_type);
279 * @sci_dev: The remote device for which the event handling is being
280 * requested.
281 * @frame_index: This is the frame index that is being processed.
283 * This method invokes the frame handler for the remote device state machine
284 * enum sci_status
286 enum sci_status scic_sds_remote_device_frame_handler(
287 struct scic_sds_remote_device *sci_dev,
288 u32 frame_index)
290 return sci_dev->state_handlers->frame_handler(sci_dev, frame_index);
295 * @sci_dev: The remote device for which the event handling is being
296 * requested.
297 * @event_code: This is the event code that is to be processed.
299 * This method invokes the remote device event handler. enum sci_status
301 enum sci_status scic_sds_remote_device_event_handler(
302 struct scic_sds_remote_device *sci_dev,
303 u32 event_code)
305 return sci_dev->state_handlers->event_handler(sci_dev, event_code);
308 static void scic_sds_remote_device_start_request(struct scic_sds_remote_device *sci_dev,
309 struct scic_sds_request *sci_req,
310 enum sci_status status)
312 struct scic_sds_port *sci_port = sci_dev->owning_port;
314 /* cleanup requests that failed after starting on the port */
315 if (status != SCI_SUCCESS)
316 scic_sds_port_complete_io(sci_port, sci_dev, sci_req);
317 else
318 scic_sds_remote_device_increment_request_count(sci_dev);
321 enum sci_status scic_sds_remote_device_start_io(struct scic_sds_controller *scic,
322 struct scic_sds_remote_device *sci_dev,
323 struct scic_sds_request *sci_req)
325 struct sci_base_state_machine *sm = &sci_dev->state_machine;
326 enum scic_sds_remote_device_states state = sm->current_state_id;
327 struct scic_sds_port *sci_port = sci_dev->owning_port;
328 enum sci_status status;
330 switch (state) {
331 case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL:
332 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED:
333 case SCI_BASE_REMOTE_DEVICE_STATE_STARTING:
334 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
335 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING:
336 case SCI_BASE_REMOTE_DEVICE_STATE_FAILED:
337 case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING:
338 case SCI_BASE_REMOTE_DEVICE_STATE_FINAL:
339 default:
340 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
341 __func__, state);
342 return SCI_FAILURE_INVALID_STATE;
343 case SCI_BASE_REMOTE_DEVICE_STATE_READY:
344 /* attempt to start an io request for this device object. The remote
345 * device object will issue the start request for the io and if
346 * successful it will start the request for the port object then
347 * increment its own request count.
349 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
350 if (status != SCI_SUCCESS)
351 return status;
353 status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, sci_req);
354 if (status != SCI_SUCCESS)
355 break;
357 status = scic_sds_request_start(sci_req);
358 break;
359 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE: {
360 /* handle the start io operation for a sata device that is in
361 * the command idle state. - Evalute the type of IO request to
362 * be started - If its an NCQ request change to NCQ substate -
363 * If its any other command change to the CMD substate
365 * If this is a softreset we may want to have a different
366 * substate.
368 enum scic_sds_remote_device_states new_state;
370 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
371 if (status != SCI_SUCCESS)
372 return status;
374 status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, sci_req);
375 if (status != SCI_SUCCESS)
376 break;
378 status = sci_req->state_handlers->start_handler(sci_req);
379 if (status != SCI_SUCCESS)
380 break;
382 if (isci_sata_get_sat_protocol(sci_req->ireq) == SAT_PROTOCOL_FPDMA)
383 new_state = SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ;
384 else {
385 sci_dev->working_request = sci_req;
386 new_state = SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD;
388 sci_base_state_machine_change_state(sm, new_state);
389 break;
391 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ:
392 if (isci_sata_get_sat_protocol(sci_req->ireq) == SAT_PROTOCOL_FPDMA) {
393 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
394 if (status != SCI_SUCCESS)
395 return status;
397 status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, sci_req);
398 if (status != SCI_SUCCESS)
399 break;
401 status = sci_req->state_handlers->start_handler(sci_req);
402 } else
403 return SCI_FAILURE_INVALID_STATE;
404 break;
405 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
406 return SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED;
407 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
408 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
409 if (status != SCI_SUCCESS)
410 return status;
412 status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, sci_req);
413 if (status != SCI_SUCCESS)
414 break;
416 status = scic_sds_request_start(sci_req);
417 if (status != SCI_SUCCESS)
418 break;
420 sci_dev->working_request = sci_req;
421 sci_base_state_machine_change_state(&sci_dev->state_machine,
422 SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD);
423 break;
424 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
425 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
426 /* device is already handling a command it can not accept new commands
427 * until this one is complete.
429 return SCI_FAILURE_INVALID_STATE;
432 scic_sds_remote_device_start_request(sci_dev, sci_req, status);
433 return status;
438 * @controller: The controller that is completing the io request.
439 * @sci_dev: The remote device for which the complete io handling is being
440 * requested.
441 * @io_request: The io request that is being completed.
443 * This method invokes the remote device complete io handler. enum sci_status
445 enum sci_status scic_sds_remote_device_complete_io(
446 struct scic_sds_controller *controller,
447 struct scic_sds_remote_device *sci_dev,
448 struct scic_sds_request *io_request)
450 return sci_dev->state_handlers->complete_io_handler(
451 sci_dev, io_request);
456 * @controller: The controller that is starting the task request.
457 * @sci_dev: The remote device for which the start task handling is being
458 * requested.
459 * @io_request: The task request that is being started.
461 * This method invokes the remote device start task handler. enum sci_status
463 enum sci_status scic_sds_remote_device_start_task(
464 struct scic_sds_controller *controller,
465 struct scic_sds_remote_device *sci_dev,
466 struct scic_sds_request *io_request)
468 return sci_dev->state_handlers->start_task_handler(
469 sci_dev, io_request);
474 * @sci_dev:
475 * @request:
477 * This method takes the request and bulids an appropriate SCU context for the
478 * request and then requests the controller to post the request. none
480 void scic_sds_remote_device_post_request(
481 struct scic_sds_remote_device *sci_dev,
482 u32 request)
484 u32 context;
486 context = scic_sds_remote_device_build_command_context(sci_dev, request);
488 scic_sds_controller_post_request(
489 scic_sds_remote_device_get_controller(sci_dev),
490 context
494 /* called once the remote node context has transisitioned to a
495 * ready state. This is the indication that the remote device object can also
496 * transition to ready.
498 static void remote_device_resume_done(void *_dev)
500 struct scic_sds_remote_device *sci_dev = _dev;
501 enum scic_sds_remote_device_states state;
503 state = sci_dev->state_machine.current_state_id;
504 switch (state) {
505 case SCI_BASE_REMOTE_DEVICE_STATE_READY:
506 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
507 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
508 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ:
509 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
510 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
511 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
512 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
513 break;
514 default:
515 /* go 'ready' if we are not already in a ready state */
516 sci_base_state_machine_change_state(&sci_dev->state_machine,
517 SCI_BASE_REMOTE_DEVICE_STATE_READY);
518 break;
524 * @request: This parameter specifies the request being continued.
526 * This method will continue to post tc for a STP request. This method usually
527 * serves as a callback when RNC gets resumed during a task management
528 * sequence. none
530 static void scic_sds_remote_device_continue_request(void *dev)
532 struct scic_sds_remote_device *sci_dev = dev;
534 /* we need to check if this request is still valid to continue. */
535 if (sci_dev->working_request)
536 scic_controller_continue_io(sci_dev->working_request);
539 static enum sci_status
540 default_device_handler(struct scic_sds_remote_device *sci_dev,
541 const char *func)
543 dev_warn(scirdev_to_dev(sci_dev),
544 "%s: in wrong state: %d\n", func,
545 sci_base_state_machine_get_state(&sci_dev->state_machine));
546 return SCI_FAILURE_INVALID_STATE;
549 static enum sci_status scic_sds_remote_device_default_suspend_handler(
550 struct scic_sds_remote_device *sci_dev, u32 suspend_type)
552 return default_device_handler(sci_dev, __func__);
555 static enum sci_status scic_sds_remote_device_default_resume_handler(
556 struct scic_sds_remote_device *sci_dev)
558 return default_device_handler(sci_dev, __func__);
563 * @device: The struct scic_sds_remote_device which is then cast into a
564 * struct scic_sds_remote_device.
565 * @event_code: The event code that the struct scic_sds_controller wants the device
566 * object to process.
568 * This method is the default event handler. It will call the RNC state
569 * machine handler for any RNC events otherwise it will log a warning and
570 * returns a failure. enum sci_status SCI_FAILURE_INVALID_STATE
572 static enum sci_status scic_sds_remote_device_core_event_handler(
573 struct scic_sds_remote_device *sci_dev,
574 u32 event_code,
575 bool is_ready_state)
577 enum sci_status status;
579 switch (scu_get_event_type(event_code)) {
580 case SCU_EVENT_TYPE_RNC_OPS_MISC:
581 case SCU_EVENT_TYPE_RNC_SUSPEND_TX:
582 case SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX:
583 status = scic_sds_remote_node_context_event_handler(&sci_dev->rnc, event_code);
584 break;
585 case SCU_EVENT_TYPE_PTX_SCHEDULE_EVENT:
587 if (scu_get_event_code(event_code) == SCU_EVENT_IT_NEXUS_TIMEOUT) {
588 status = SCI_SUCCESS;
590 /* Suspend the associated RNC */
591 scic_sds_remote_node_context_suspend(&sci_dev->rnc,
592 SCI_SOFTWARE_SUSPENSION,
593 NULL, NULL);
595 dev_dbg(scirdev_to_dev(sci_dev),
596 "%s: device: %p event code: %x: %s\n",
597 __func__, sci_dev, event_code,
598 (is_ready_state)
599 ? "I_T_Nexus_Timeout event"
600 : "I_T_Nexus_Timeout event in wrong state");
602 break;
604 /* Else, fall through and treat as unhandled... */
606 default:
607 dev_dbg(scirdev_to_dev(sci_dev),
608 "%s: device: %p event code: %x: %s\n",
609 __func__, sci_dev, event_code,
610 (is_ready_state)
611 ? "unexpected event"
612 : "unexpected event in wrong state");
613 status = SCI_FAILURE_INVALID_STATE;
614 break;
617 return status;
621 * @device: The struct scic_sds_remote_device which is then cast into a
622 * struct scic_sds_remote_device.
623 * @event_code: The event code that the struct scic_sds_controller wants the device
624 * object to process.
626 * This method is the default event handler. It will call the RNC state
627 * machine handler for any RNC events otherwise it will log a warning and
628 * returns a failure. enum sci_status SCI_FAILURE_INVALID_STATE
630 static enum sci_status scic_sds_remote_device_default_event_handler(
631 struct scic_sds_remote_device *sci_dev,
632 u32 event_code)
634 return scic_sds_remote_device_core_event_handler(sci_dev,
635 event_code,
636 false);
641 * @device: The struct scic_sds_remote_device which is then cast into a
642 * struct scic_sds_remote_device.
643 * @frame_index: The frame index for which the struct scic_sds_controller wants this
644 * device object to process.
646 * This method is the default unsolicited frame handler. It logs a warning,
647 * releases the frame and returns a failure. enum sci_status
648 * SCI_FAILURE_INVALID_STATE
650 static enum sci_status scic_sds_remote_device_default_frame_handler(
651 struct scic_sds_remote_device *sci_dev,
652 u32 frame_index)
654 dev_warn(scirdev_to_dev(sci_dev),
655 "%s: SCIC Remote Device requested to handle frame %x "
656 "while in wrong state %d\n",
657 __func__,
658 frame_index,
659 sci_base_state_machine_get_state(
660 &sci_dev->state_machine));
662 /* Return the frame back to the controller */
663 scic_sds_controller_release_frame(
664 scic_sds_remote_device_get_controller(sci_dev), frame_index
667 return SCI_FAILURE_INVALID_STATE;
670 static enum sci_status scic_sds_remote_device_default_start_request_handler(
671 struct scic_sds_remote_device *sci_dev,
672 struct scic_sds_request *request)
674 return default_device_handler(sci_dev, __func__);
677 static enum sci_status scic_sds_remote_device_default_complete_request_handler(
678 struct scic_sds_remote_device *sci_dev,
679 struct scic_sds_request *request)
681 return default_device_handler(sci_dev, __func__);
684 static enum sci_status scic_sds_remote_device_default_continue_request_handler(
685 struct scic_sds_remote_device *sci_dev,
686 struct scic_sds_request *request)
688 return default_device_handler(sci_dev, __func__);
693 * @device: The struct scic_sds_remote_device which is then cast into a
694 * struct scic_sds_remote_device.
695 * @frame_index: The frame index for which the struct scic_sds_controller wants this
696 * device object to process.
698 * This method is a general ssp frame handler. In most cases the device object
699 * needs to route the unsolicited frame processing to the io request object.
700 * This method decodes the tag for the io request object and routes the
701 * unsolicited frame to that object. enum sci_status SCI_FAILURE_INVALID_STATE
703 static enum sci_status scic_sds_remote_device_general_frame_handler(
704 struct scic_sds_remote_device *sci_dev,
705 u32 frame_index)
707 enum sci_status result;
708 struct sci_ssp_frame_header *frame_header;
709 struct scic_sds_request *io_request;
711 result = scic_sds_unsolicited_frame_control_get_header(
712 &(scic_sds_remote_device_get_controller(sci_dev)->uf_control),
713 frame_index,
714 (void **)&frame_header
717 if (SCI_SUCCESS == result) {
718 io_request = scic_sds_controller_get_io_request_from_tag(
719 scic_sds_remote_device_get_controller(sci_dev), frame_header->tag);
721 if ((io_request == NULL)
722 || (io_request->target_device != sci_dev)) {
724 * We could not map this tag to a valid IO request
725 * Just toss the frame and continue */
726 scic_sds_controller_release_frame(
727 scic_sds_remote_device_get_controller(sci_dev), frame_index
729 } else {
730 /* The IO request is now in charge of releasing the frame */
731 result = io_request->state_handlers->frame_handler(
732 io_request, frame_index);
736 return result;
741 * @[in]: sci_dev This is the device object that is receiving the event.
742 * @[in]: event_code The event code to process.
744 * This is a common method for handling events reported to the remote device
745 * from the controller object. enum sci_status
747 static enum sci_status scic_sds_remote_device_general_event_handler(
748 struct scic_sds_remote_device *sci_dev,
749 u32 event_code)
751 return scic_sds_remote_device_core_event_handler(sci_dev,
752 event_code,
753 true);
757 * This method will attempt to start a task request for this device object. The
758 * remote device object will issue the start request for the task and if
759 * successful it will start the request for the port object then increment its
760 * own requet count. enum sci_status SCI_SUCCESS if the task request is started for
761 * this device object. SCI_FAILURE_INSUFFICIENT_RESOURCES if the io request
762 * object could not get the resources to start.
764 static enum sci_status scic_sds_remote_device_ready_state_start_task_handler(
765 struct scic_sds_remote_device *sci_dev,
766 struct scic_sds_request *request)
768 enum sci_status result;
770 /* See if the port is in a state where we can start the IO request */
771 result = scic_sds_port_start_io(
772 scic_sds_remote_device_get_port(sci_dev), sci_dev, request);
774 if (result == SCI_SUCCESS) {
775 result = scic_sds_remote_node_context_start_task(&sci_dev->rnc,
776 request);
777 if (result == SCI_SUCCESS)
778 result = scic_sds_request_start(request);
780 scic_sds_remote_device_start_request(sci_dev, request, result);
783 return result;
787 * This method will complete the request for the remote device object. The
788 * method will call the completion handler for the request object and if
789 * successful it will complete the request on the port object then decrement
790 * its own started_request_count. enum sci_status
792 static enum sci_status scic_sds_remote_device_ready_state_complete_request_handler(
793 struct scic_sds_remote_device *sci_dev,
794 struct scic_sds_request *request)
796 enum sci_status result;
798 result = scic_sds_request_complete(request);
800 if (result != SCI_SUCCESS)
801 return result;
803 /* See if the port is in a state
804 * where we can start the IO request */
805 result = scic_sds_port_complete_io(
806 scic_sds_remote_device_get_port(sci_dev),
807 sci_dev, request);
809 if (result == SCI_SUCCESS)
810 scic_sds_remote_device_decrement_request_count(sci_dev);
812 return result;
817 * @device: The device object for which the request is completing.
818 * @request: The task request that is being completed.
820 * This method completes requests for this struct scic_sds_remote_device while it is
821 * in the SCI_BASE_REMOTE_DEVICE_STATE_STOPPING state. This method calls the
822 * complete method for the request object and if that is successful the port
823 * object is called to complete the task request. Then the device object itself
824 * completes the task request. If struct scic_sds_remote_device started_request_count
825 * goes to 0 and the invalidate RNC request has completed the device object can
826 * transition to the SCI_BASE_REMOTE_DEVICE_STATE_STOPPED. enum sci_status
828 static enum sci_status scic_sds_remote_device_stopping_state_complete_request_handler(
829 struct scic_sds_remote_device *sci_dev,
830 struct scic_sds_request *request)
832 enum sci_status status = SCI_SUCCESS;
834 status = scic_sds_request_complete(request);
836 if (status != SCI_SUCCESS)
837 return status;
839 status = scic_sds_port_complete_io(scic_sds_remote_device_get_port(sci_dev),
840 sci_dev, request);
841 if (status != SCI_SUCCESS)
842 return status;
844 scic_sds_remote_device_decrement_request_count(sci_dev);
846 if (scic_sds_remote_device_get_request_count(sci_dev) == 0)
847 scic_sds_remote_node_context_destruct(&sci_dev->rnc,
848 rnc_destruct_done, sci_dev);
849 return SCI_SUCCESS;
852 /* complete requests for this device while it is in the
853 * SCI_BASE_REMOTE_DEVICE_STATE_RESETTING state. This method calls the complete
854 * method for the request object and if that is successful the port object is
855 * called to complete the task request. Then the device object itself completes
856 * the task request. enum sci_status
858 static enum sci_status scic_sds_remote_device_resetting_state_complete_request_handler(
859 struct scic_sds_remote_device *sci_dev,
860 struct scic_sds_request *request)
862 enum sci_status status = SCI_SUCCESS;
864 status = scic_sds_request_complete(request);
866 if (status == SCI_SUCCESS) {
867 status = scic_sds_port_complete_io(
868 scic_sds_remote_device_get_port(sci_dev),
869 sci_dev, request);
871 if (status == SCI_SUCCESS) {
872 scic_sds_remote_device_decrement_request_count(sci_dev);
876 return status;
879 static enum sci_status scic_sds_stp_remote_device_complete_request(struct scic_sds_remote_device *sci_dev,
880 struct scic_sds_request *sci_req)
882 enum sci_status status;
884 status = scic_sds_io_request_complete(sci_req);
885 if (status != SCI_SUCCESS)
886 goto out;
888 status = scic_sds_port_complete_io(sci_dev->owning_port, sci_dev, sci_req);
889 if (status != SCI_SUCCESS)
890 goto out;
892 scic_sds_remote_device_decrement_request_count(sci_dev);
893 if (sci_req->sci_status == SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED) {
894 /* This request causes hardware error, device needs to be Lun Reset.
895 * So here we force the state machine to IDLE state so the rest IOs
896 * can reach RNC state handler, these IOs will be completed by RNC with
897 * status of "DEVICE_RESET_REQUIRED", instead of "INVALID STATE".
899 sci_base_state_machine_change_state(&sci_dev->state_machine,
900 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET);
901 } else if (scic_sds_remote_device_get_request_count(sci_dev) == 0)
902 sci_base_state_machine_change_state(&sci_dev->state_machine,
903 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
906 out:
907 if (status != SCI_SUCCESS)
908 dev_err(scirdev_to_dev(sci_dev),
909 "%s: Port:0x%p Device:0x%p Request:0x%p Status:0x%x "
910 "could not complete\n", __func__, sci_dev->owning_port,
911 sci_dev, sci_req, status);
913 return status;
916 /* scic_sds_stp_remote_device_ready_substate_start_request_handler - start stp
917 * @device: The target device a task management request towards to.
918 * @request: The task request.
920 * This is the READY NCQ substate handler to start task management request. In
921 * this routine, we suspend and resume the RNC. enum sci_status Always return
922 * SCI_FAILURE_RESET_DEVICE_PARTIAL_SUCCESS status to let
923 * controller_start_task_handler know that the controller can't post TC for
924 * task request yet, instead, when RNC gets resumed, a controller_continue_task
925 * callback will be called.
927 static enum sci_status scic_sds_stp_remote_device_ready_substate_start_request_handler(
928 struct scic_sds_remote_device *device,
929 struct scic_sds_request *request)
931 enum sci_status status;
933 /* Will the port allow the io request to start? */
934 status = device->owning_port->state_handlers->start_io_handler(
935 device->owning_port, device, request);
936 if (status != SCI_SUCCESS)
937 return status;
939 status = scic_sds_remote_node_context_start_task(&device->rnc, request);
940 if (status != SCI_SUCCESS)
941 goto out;
943 status = request->state_handlers->start_handler(request);
944 if (status != SCI_SUCCESS)
945 goto out;
948 * Note: If the remote device state is not IDLE this will replace
949 * the request that probably resulted in the task management request.
951 device->working_request = request;
952 sci_base_state_machine_change_state(&device->state_machine,
953 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD);
956 * The remote node context must cleanup the TCi to NCQ mapping table.
957 * The only way to do this correctly is to either write to the TLCR
958 * register or to invalidate and repost the RNC. In either case the
959 * remote node context state machine will take the correct action when
960 * the remote node context is suspended and later resumed.
962 scic_sds_remote_node_context_suspend(&device->rnc,
963 SCI_SOFTWARE_SUSPENSION, NULL, NULL);
964 scic_sds_remote_node_context_resume(&device->rnc,
965 scic_sds_remote_device_continue_request,
966 device);
968 out:
969 scic_sds_remote_device_start_request(device, request, status);
971 * We need to let the controller start request handler know that it can't
972 * post TC yet. We will provide a callback function to post TC when RNC gets
973 * resumed.
975 return SCI_FAILURE_RESET_DEVICE_PARTIAL_SUCCESS;
978 static enum sci_status scic_sds_stp_remote_device_ready_idle_substate_event_handler(
979 struct scic_sds_remote_device *sci_dev,
980 u32 event_code)
982 enum sci_status status;
984 status = scic_sds_remote_device_general_event_handler(sci_dev, event_code);
985 if (status != SCI_SUCCESS)
986 return status;
988 /* We pick up suspension events to handle specifically to this state. We
989 * resume the RNC right away. enum sci_status
991 if (scu_get_event_type(event_code) == SCU_EVENT_TYPE_RNC_SUSPEND_TX ||
992 scu_get_event_type(event_code) == SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX)
993 status = scic_sds_remote_node_context_resume(&sci_dev->rnc, NULL, NULL);
995 return status;
998 static enum sci_status scic_sds_stp_remote_device_ready_ncq_substate_frame_handler(struct scic_sds_remote_device *sci_dev,
999 u32 frame_index)
1001 enum sci_status status;
1002 struct sata_fis_header *frame_header;
1003 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
1005 status = scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
1006 frame_index,
1007 (void **)&frame_header);
1008 if (status != SCI_SUCCESS)
1009 return status;
1011 if (frame_header->fis_type == SATA_FIS_TYPE_SETDEVBITS &&
1012 (frame_header->status & ATA_STATUS_REG_ERROR_BIT)) {
1013 sci_dev->not_ready_reason = SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED;
1015 /* TODO Check sactive and complete associated IO if any. */
1017 sci_base_state_machine_change_state(&sci_dev->state_machine,
1018 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR);
1019 } else if (frame_header->fis_type == SATA_FIS_TYPE_REGD2H &&
1020 (frame_header->status & ATA_STATUS_REG_ERROR_BIT)) {
1022 * Some devices return D2H FIS when an NCQ error is detected.
1023 * Treat this like an SDB error FIS ready reason.
1025 sci_dev->not_ready_reason = SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED;
1027 sci_base_state_machine_change_state(&sci_dev->state_machine,
1028 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR);
1029 } else
1030 status = SCI_FAILURE;
1032 scic_sds_controller_release_frame(scic, frame_index);
1034 return status;
1037 static enum sci_status scic_sds_stp_remote_device_ready_cmd_substate_suspend_handler(
1038 struct scic_sds_remote_device *sci_dev,
1039 u32 suspend_type)
1041 enum sci_status status;
1043 status = scic_sds_remote_node_context_suspend(&sci_dev->rnc,
1044 suspend_type, NULL, NULL);
1046 return status;
1049 static enum sci_status scic_sds_stp_remote_device_ready_cmd_substate_frame_handler(
1050 struct scic_sds_remote_device *sci_dev,
1051 u32 frame_index)
1053 /* The device doe not process any UF received from the hardware while
1054 * in this state. All unsolicited frames are forwarded to the io
1055 * request object.
1057 return scic_sds_io_request_frame_handler(sci_dev->working_request,
1058 frame_index);
1061 static enum sci_status scic_sds_stp_remote_device_ready_await_reset_substate_complete_request_handler(
1062 struct scic_sds_remote_device *device,
1063 struct scic_sds_request *request)
1065 struct scic_sds_request *sci_req = request;
1066 enum sci_status status;
1068 status = scic_sds_io_request_complete(sci_req);
1069 if (status != SCI_SUCCESS)
1070 goto out;
1072 status = scic_sds_port_complete_io(device->owning_port, device, sci_req);
1073 if (status != SCI_SUCCESS)
1074 goto out;
1076 scic_sds_remote_device_decrement_request_count(device);
1077 out:
1078 if (status != SCI_SUCCESS)
1079 dev_err(scirdev_to_dev(device),
1080 "%s: Port:0x%p Device:0x%p Request:0x%p Status:0x%x "
1081 "could not complete\n",
1082 __func__, device->owning_port, device, sci_req, status);
1084 return status;
1087 static void scic_sds_stp_remote_device_ready_idle_substate_resume_complete_handler(void *_dev)
1089 struct scic_sds_remote_device *sci_dev = _dev;
1090 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
1091 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
1093 /* For NCQ operation we do not issue a isci_remote_device_not_ready().
1094 * As a result, avoid sending the ready notification.
1096 if (sci_dev->state_machine.previous_state_id != SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ)
1097 isci_remote_device_ready(scic->ihost, idev);
1100 static enum sci_status
1101 scic_sds_smp_remote_device_ready_cmd_substate_complete_io_handler(struct scic_sds_remote_device *sci_dev,
1102 struct scic_sds_request *sci_req)
1104 enum sci_status status;
1106 status = scic_sds_io_request_complete(sci_req);
1107 if (status != SCI_SUCCESS)
1108 return status;
1110 status = scic_sds_port_complete_io(sci_dev->owning_port, sci_dev, sci_req);
1111 if (status != SCI_SUCCESS) {
1112 dev_err(scirdev_to_dev(sci_dev),
1113 "%s: SCIC SDS Remote Device 0x%p io request "
1114 "0x%p could not be completd on the port 0x%p "
1115 "failed with status %d.\n", __func__, sci_dev, sci_req,
1116 sci_dev->owning_port, status);
1117 return status;
1120 scic_sds_remote_device_decrement_request_count(sci_dev);
1121 sci_base_state_machine_change_state(&sci_dev->state_machine,
1122 SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
1124 return status;
1127 static enum sci_status scic_sds_smp_remote_device_ready_cmd_substate_frame_handler(
1128 struct scic_sds_remote_device *sci_dev,
1129 u32 frame_index)
1131 enum sci_status status;
1133 /* The device does not process any UF received from the hardware while
1134 * in this state. All unsolicited frames are forwarded to the io request
1135 * object.
1137 status = scic_sds_io_request_frame_handler(
1138 sci_dev->working_request,
1139 frame_index
1142 return status;
1145 static const struct scic_sds_remote_device_state_handler scic_sds_remote_device_state_handler_table[] = {
1146 [SCI_BASE_REMOTE_DEVICE_STATE_INITIAL] = {
1147 .complete_io_handler = scic_sds_remote_device_default_complete_request_handler,
1148 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
1149 .start_task_handler = scic_sds_remote_device_default_start_request_handler,
1150 .complete_task_handler = scic_sds_remote_device_default_complete_request_handler,
1151 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1152 .resume_handler = scic_sds_remote_device_default_resume_handler,
1153 .event_handler = scic_sds_remote_device_default_event_handler,
1154 .frame_handler = scic_sds_remote_device_default_frame_handler
1156 [SCI_BASE_REMOTE_DEVICE_STATE_STOPPED] = {
1157 .complete_io_handler = scic_sds_remote_device_default_complete_request_handler,
1158 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
1159 .start_task_handler = scic_sds_remote_device_default_start_request_handler,
1160 .complete_task_handler = scic_sds_remote_device_default_complete_request_handler,
1161 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1162 .resume_handler = scic_sds_remote_device_default_resume_handler,
1163 .event_handler = scic_sds_remote_device_default_event_handler,
1164 .frame_handler = scic_sds_remote_device_default_frame_handler
1166 [SCI_BASE_REMOTE_DEVICE_STATE_STARTING] = {
1167 .complete_io_handler = scic_sds_remote_device_default_complete_request_handler,
1168 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
1169 .start_task_handler = scic_sds_remote_device_default_start_request_handler,
1170 .complete_task_handler = scic_sds_remote_device_default_complete_request_handler,
1171 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1172 .resume_handler = scic_sds_remote_device_default_resume_handler,
1173 .event_handler = scic_sds_remote_device_general_event_handler,
1174 .frame_handler = scic_sds_remote_device_default_frame_handler
1176 [SCI_BASE_REMOTE_DEVICE_STATE_READY] = {
1177 .complete_io_handler = scic_sds_remote_device_ready_state_complete_request_handler,
1178 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
1179 .start_task_handler = scic_sds_remote_device_ready_state_start_task_handler,
1180 .complete_task_handler = scic_sds_remote_device_ready_state_complete_request_handler,
1181 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1182 .resume_handler = scic_sds_remote_device_default_resume_handler,
1183 .event_handler = scic_sds_remote_device_general_event_handler,
1184 .frame_handler = scic_sds_remote_device_general_frame_handler,
1186 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE] = {
1187 .complete_io_handler = scic_sds_remote_device_default_complete_request_handler,
1188 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
1189 .start_task_handler = scic_sds_stp_remote_device_ready_substate_start_request_handler,
1190 .complete_task_handler = scic_sds_remote_device_default_complete_request_handler,
1191 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1192 .resume_handler = scic_sds_remote_device_default_resume_handler,
1193 .event_handler = scic_sds_stp_remote_device_ready_idle_substate_event_handler,
1194 .frame_handler = scic_sds_remote_device_default_frame_handler
1196 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD] = {
1197 .complete_io_handler = scic_sds_stp_remote_device_complete_request,
1198 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
1199 .start_task_handler = scic_sds_stp_remote_device_ready_substate_start_request_handler,
1200 .complete_task_handler = scic_sds_stp_remote_device_complete_request,
1201 .suspend_handler = scic_sds_stp_remote_device_ready_cmd_substate_suspend_handler,
1202 .resume_handler = scic_sds_remote_device_default_resume_handler,
1203 .event_handler = scic_sds_remote_device_general_event_handler,
1204 .frame_handler = scic_sds_stp_remote_device_ready_cmd_substate_frame_handler
1206 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ] = {
1207 .complete_io_handler = scic_sds_stp_remote_device_complete_request,
1208 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
1209 .start_task_handler = scic_sds_stp_remote_device_ready_substate_start_request_handler,
1210 .complete_task_handler = scic_sds_stp_remote_device_complete_request,
1211 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1212 .resume_handler = scic_sds_remote_device_default_resume_handler,
1213 .event_handler = scic_sds_remote_device_general_event_handler,
1214 .frame_handler = scic_sds_stp_remote_device_ready_ncq_substate_frame_handler
1216 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR] = {
1217 .complete_io_handler = scic_sds_stp_remote_device_complete_request,
1218 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
1219 .start_task_handler = scic_sds_stp_remote_device_ready_substate_start_request_handler,
1220 .complete_task_handler = scic_sds_stp_remote_device_complete_request,
1221 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1222 .resume_handler = scic_sds_remote_device_default_resume_handler,
1223 .event_handler = scic_sds_remote_device_general_event_handler,
1224 .frame_handler = scic_sds_remote_device_general_frame_handler
1226 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET] = {
1227 .complete_io_handler = scic_sds_stp_remote_device_ready_await_reset_substate_complete_request_handler,
1228 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
1229 .start_task_handler = scic_sds_stp_remote_device_ready_substate_start_request_handler,
1230 .complete_task_handler = scic_sds_stp_remote_device_complete_request,
1231 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1232 .resume_handler = scic_sds_remote_device_default_resume_handler,
1233 .event_handler = scic_sds_remote_device_general_event_handler,
1234 .frame_handler = scic_sds_remote_device_general_frame_handler
1236 [SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE] = {
1237 .complete_io_handler = scic_sds_remote_device_default_complete_request_handler,
1238 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
1239 .start_task_handler = scic_sds_remote_device_default_start_request_handler,
1240 .complete_task_handler = scic_sds_remote_device_default_complete_request_handler,
1241 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1242 .resume_handler = scic_sds_remote_device_default_resume_handler,
1243 .event_handler = scic_sds_remote_device_general_event_handler,
1244 .frame_handler = scic_sds_remote_device_default_frame_handler
1246 [SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD] = {
1247 .complete_io_handler = scic_sds_smp_remote_device_ready_cmd_substate_complete_io_handler,
1248 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
1249 .start_task_handler = scic_sds_remote_device_default_start_request_handler,
1250 .complete_task_handler = scic_sds_remote_device_default_complete_request_handler,
1251 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1252 .resume_handler = scic_sds_remote_device_default_resume_handler,
1253 .event_handler = scic_sds_remote_device_general_event_handler,
1254 .frame_handler = scic_sds_smp_remote_device_ready_cmd_substate_frame_handler
1256 [SCI_BASE_REMOTE_DEVICE_STATE_STOPPING] = {
1257 .complete_io_handler = scic_sds_remote_device_stopping_state_complete_request_handler,
1258 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
1259 .start_task_handler = scic_sds_remote_device_default_start_request_handler,
1260 .complete_task_handler = scic_sds_remote_device_stopping_state_complete_request_handler,
1261 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1262 .resume_handler = scic_sds_remote_device_default_resume_handler,
1263 .event_handler = scic_sds_remote_device_general_event_handler,
1264 .frame_handler = scic_sds_remote_device_general_frame_handler
1266 [SCI_BASE_REMOTE_DEVICE_STATE_FAILED] = {
1267 .complete_io_handler = scic_sds_remote_device_default_complete_request_handler,
1268 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
1269 .start_task_handler = scic_sds_remote_device_default_start_request_handler,
1270 .complete_task_handler = scic_sds_remote_device_default_complete_request_handler,
1271 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1272 .resume_handler = scic_sds_remote_device_default_resume_handler,
1273 .event_handler = scic_sds_remote_device_default_event_handler,
1274 .frame_handler = scic_sds_remote_device_general_frame_handler
1276 [SCI_BASE_REMOTE_DEVICE_STATE_RESETTING] = {
1277 .complete_io_handler = scic_sds_remote_device_resetting_state_complete_request_handler,
1278 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
1279 .start_task_handler = scic_sds_remote_device_default_start_request_handler,
1280 .complete_task_handler = scic_sds_remote_device_resetting_state_complete_request_handler,
1281 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1282 .resume_handler = scic_sds_remote_device_default_resume_handler,
1283 .event_handler = scic_sds_remote_device_default_event_handler,
1284 .frame_handler = scic_sds_remote_device_general_frame_handler
1286 [SCI_BASE_REMOTE_DEVICE_STATE_FINAL] = {
1287 .complete_io_handler = scic_sds_remote_device_default_complete_request_handler,
1288 .continue_io_handler = scic_sds_remote_device_default_continue_request_handler,
1289 .start_task_handler = scic_sds_remote_device_default_start_request_handler,
1290 .complete_task_handler = scic_sds_remote_device_default_complete_request_handler,
1291 .suspend_handler = scic_sds_remote_device_default_suspend_handler,
1292 .resume_handler = scic_sds_remote_device_default_resume_handler,
1293 .event_handler = scic_sds_remote_device_default_event_handler,
1294 .frame_handler = scic_sds_remote_device_default_frame_handler
1298 static void scic_sds_remote_device_initial_state_enter(void *object)
1300 struct scic_sds_remote_device *sci_dev = object;
1302 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1303 SCI_BASE_REMOTE_DEVICE_STATE_INITIAL);
1305 /* Initial state is a transitional state to the stopped state */
1306 sci_base_state_machine_change_state(&sci_dev->state_machine,
1307 SCI_BASE_REMOTE_DEVICE_STATE_STOPPED);
1311 * scic_remote_device_destruct() - free remote node context and destruct
1312 * @remote_device: This parameter specifies the remote device to be destructed.
1314 * Remote device objects are a limited resource. As such, they must be
1315 * protected. Thus calls to construct and destruct are mutually exclusive and
1316 * non-reentrant. The return value shall indicate if the device was
1317 * successfully destructed or if some failure occurred. enum sci_status This value
1318 * is returned if the device is successfully destructed.
1319 * SCI_FAILURE_INVALID_REMOTE_DEVICE This value is returned if the supplied
1320 * device isn't valid (e.g. it's already been destoryed, the handle isn't
1321 * valid, etc.).
1323 static enum sci_status scic_remote_device_destruct(struct scic_sds_remote_device *sci_dev)
1325 struct sci_base_state_machine *sm = &sci_dev->state_machine;
1326 enum scic_sds_remote_device_states state = sm->current_state_id;
1327 struct scic_sds_controller *scic;
1329 if (state != SCI_BASE_REMOTE_DEVICE_STATE_STOPPED) {
1330 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
1331 __func__, state);
1332 return SCI_FAILURE_INVALID_STATE;
1335 scic = sci_dev->owning_port->owning_controller;
1336 scic_sds_controller_free_remote_node_context(scic, sci_dev,
1337 sci_dev->rnc.remote_node_index);
1338 sci_dev->rnc.remote_node_index = SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX;
1339 sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_FINAL);
1341 return SCI_SUCCESS;
1345 * isci_remote_device_deconstruct() - This function frees an isci_remote_device.
1346 * @ihost: This parameter specifies the isci host object.
1347 * @idev: This parameter specifies the remote device to be freed.
1350 static void isci_remote_device_deconstruct(struct isci_host *ihost, struct isci_remote_device *idev)
1352 dev_dbg(&ihost->pdev->dev,
1353 "%s: isci_device = %p\n", __func__, idev);
1355 /* There should not be any outstanding io's. All paths to
1356 * here should go through isci_remote_device_nuke_requests.
1357 * If we hit this condition, we will need a way to complete
1358 * io requests in process */
1359 while (!list_empty(&idev->reqs_in_process)) {
1361 dev_err(&ihost->pdev->dev,
1362 "%s: ** request list not empty! **\n", __func__);
1363 BUG();
1366 scic_remote_device_destruct(&idev->sci);
1367 idev->domain_dev->lldd_dev = NULL;
1368 idev->domain_dev = NULL;
1369 idev->isci_port = NULL;
1370 list_del_init(&idev->node);
1372 clear_bit(IDEV_START_PENDING, &idev->flags);
1373 clear_bit(IDEV_STOP_PENDING, &idev->flags);
1374 wake_up(&ihost->eventq);
1378 * isci_remote_device_stop_complete() - This function is called by the scic
1379 * when the remote device stop has completed. We mark the isci device as not
1380 * ready and remove the isci remote device.
1381 * @ihost: This parameter specifies the isci host object.
1382 * @idev: This parameter specifies the remote device.
1383 * @status: This parameter specifies status of the completion.
1386 static void isci_remote_device_stop_complete(struct isci_host *ihost,
1387 struct isci_remote_device *idev)
1389 dev_dbg(&ihost->pdev->dev, "%s: complete idev = %p\n", __func__, idev);
1391 isci_remote_device_change_state(idev, isci_stopped);
1393 /* after stop, we can tear down resources. */
1394 isci_remote_device_deconstruct(ihost, idev);
1397 static void scic_sds_remote_device_stopped_state_enter(void *object)
1399 struct scic_sds_remote_device *sci_dev = object;
1400 struct scic_sds_controller *scic;
1401 struct isci_remote_device *idev;
1402 struct isci_host *ihost;
1403 u32 prev_state;
1405 scic = scic_sds_remote_device_get_controller(sci_dev);
1406 ihost = scic->ihost;
1407 idev = sci_dev_to_idev(sci_dev);
1409 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1410 SCI_BASE_REMOTE_DEVICE_STATE_STOPPED);
1412 /* If we are entering from the stopping state let the SCI User know that
1413 * the stop operation has completed.
1415 prev_state = sci_dev->state_machine.previous_state_id;
1416 if (prev_state == SCI_BASE_REMOTE_DEVICE_STATE_STOPPING)
1417 isci_remote_device_stop_complete(ihost, idev);
1419 scic_sds_controller_remote_device_stopped(scic, sci_dev);
1422 static void scic_sds_remote_device_starting_state_enter(void *object)
1424 struct scic_sds_remote_device *sci_dev = object;
1425 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1426 struct isci_host *ihost = scic->ihost;
1427 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
1429 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1430 SCI_BASE_REMOTE_DEVICE_STATE_STARTING);
1432 isci_remote_device_not_ready(ihost, idev,
1433 SCIC_REMOTE_DEVICE_NOT_READY_START_REQUESTED);
1436 static void scic_sds_remote_device_ready_state_enter(void *object)
1438 struct scic_sds_remote_device *sci_dev = object;
1439 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
1440 struct domain_device *dev = sci_dev_to_domain(sci_dev);
1442 SET_STATE_HANDLER(sci_dev,
1443 scic_sds_remote_device_state_handler_table,
1444 SCI_BASE_REMOTE_DEVICE_STATE_READY);
1446 scic->remote_device_sequence[sci_dev->rnc.remote_node_index]++;
1448 if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_SATA)) {
1449 sci_base_state_machine_change_state(&sci_dev->state_machine,
1450 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
1451 } else if (dev_is_expander(dev)) {
1452 sci_base_state_machine_change_state(&sci_dev->state_machine,
1453 SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
1454 } else
1455 isci_remote_device_ready(scic->ihost, sci_dev_to_idev(sci_dev));
1458 static void scic_sds_remote_device_ready_state_exit(void *object)
1460 struct scic_sds_remote_device *sci_dev = object;
1461 struct domain_device *dev = sci_dev_to_domain(sci_dev);
1463 if (dev->dev_type == SAS_END_DEV) {
1464 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
1465 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
1467 isci_remote_device_not_ready(scic->ihost, idev,
1468 SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED);
1472 static void scic_sds_remote_device_stopping_state_enter(void *object)
1474 struct scic_sds_remote_device *sci_dev = object;
1476 SET_STATE_HANDLER(
1477 sci_dev,
1478 scic_sds_remote_device_state_handler_table,
1479 SCI_BASE_REMOTE_DEVICE_STATE_STOPPING
1483 static void scic_sds_remote_device_failed_state_enter(void *object)
1485 struct scic_sds_remote_device *sci_dev = object;
1487 SET_STATE_HANDLER(
1488 sci_dev,
1489 scic_sds_remote_device_state_handler_table,
1490 SCI_BASE_REMOTE_DEVICE_STATE_FAILED
1494 static void scic_sds_remote_device_resetting_state_enter(void *object)
1496 struct scic_sds_remote_device *sci_dev = object;
1498 SET_STATE_HANDLER(
1499 sci_dev,
1500 scic_sds_remote_device_state_handler_table,
1501 SCI_BASE_REMOTE_DEVICE_STATE_RESETTING
1504 scic_sds_remote_node_context_suspend(
1505 &sci_dev->rnc, SCI_SOFTWARE_SUSPENSION, NULL, NULL);
1508 static void scic_sds_remote_device_resetting_state_exit(void *object)
1510 struct scic_sds_remote_device *sci_dev = object;
1512 scic_sds_remote_node_context_resume(&sci_dev->rnc, NULL, NULL);
1515 static void scic_sds_remote_device_final_state_enter(void *object)
1517 struct scic_sds_remote_device *sci_dev = object;
1519 SET_STATE_HANDLER(
1520 sci_dev,
1521 scic_sds_remote_device_state_handler_table,
1522 SCI_BASE_REMOTE_DEVICE_STATE_FINAL
1526 static void scic_sds_stp_remote_device_ready_idle_substate_enter(void *object)
1528 struct scic_sds_remote_device *sci_dev = object;
1530 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1531 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
1533 sci_dev->working_request = NULL;
1534 if (scic_sds_remote_node_context_is_ready(&sci_dev->rnc)) {
1536 * Since the RNC is ready, it's alright to finish completion
1537 * processing (e.g. signal the remote device is ready). */
1538 scic_sds_stp_remote_device_ready_idle_substate_resume_complete_handler(sci_dev);
1539 } else {
1540 scic_sds_remote_node_context_resume(&sci_dev->rnc,
1541 scic_sds_stp_remote_device_ready_idle_substate_resume_complete_handler,
1542 sci_dev);
1546 static void scic_sds_stp_remote_device_ready_cmd_substate_enter(void *object)
1548 struct scic_sds_remote_device *sci_dev = object;
1549 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1551 BUG_ON(sci_dev->working_request == NULL);
1553 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1554 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD);
1556 isci_remote_device_not_ready(scic->ihost, sci_dev_to_idev(sci_dev),
1557 SCIC_REMOTE_DEVICE_NOT_READY_SATA_REQUEST_STARTED);
1560 static void scic_sds_stp_remote_device_ready_ncq_substate_enter(void *object)
1562 struct scic_sds_remote_device *sci_dev = object;
1564 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1565 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ);
1568 static void scic_sds_stp_remote_device_ready_ncq_error_substate_enter(void *object)
1570 struct scic_sds_remote_device *sci_dev = object;
1571 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1572 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
1574 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1575 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR);
1577 if (sci_dev->not_ready_reason == SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED)
1578 isci_remote_device_not_ready(scic->ihost, idev,
1579 sci_dev->not_ready_reason);
1582 static void scic_sds_stp_remote_device_ready_await_reset_substate_enter(void *object)
1584 struct scic_sds_remote_device *sci_dev = object;
1586 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1587 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET);
1590 static void scic_sds_smp_remote_device_ready_idle_substate_enter(void *object)
1592 struct scic_sds_remote_device *sci_dev = object;
1593 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1595 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1596 SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
1598 isci_remote_device_ready(scic->ihost, sci_dev_to_idev(sci_dev));
1601 static void scic_sds_smp_remote_device_ready_cmd_substate_enter(void *object)
1603 struct scic_sds_remote_device *sci_dev = object;
1604 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1606 BUG_ON(sci_dev->working_request == NULL);
1608 SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
1609 SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD);
1611 isci_remote_device_not_ready(scic->ihost, sci_dev_to_idev(sci_dev),
1612 SCIC_REMOTE_DEVICE_NOT_READY_SMP_REQUEST_STARTED);
1615 static void scic_sds_smp_remote_device_ready_cmd_substate_exit(void *object)
1617 struct scic_sds_remote_device *sci_dev = object;
1619 sci_dev->working_request = NULL;
1622 static const struct sci_base_state scic_sds_remote_device_state_table[] = {
1623 [SCI_BASE_REMOTE_DEVICE_STATE_INITIAL] = {
1624 .enter_state = scic_sds_remote_device_initial_state_enter,
1626 [SCI_BASE_REMOTE_DEVICE_STATE_STOPPED] = {
1627 .enter_state = scic_sds_remote_device_stopped_state_enter,
1629 [SCI_BASE_REMOTE_DEVICE_STATE_STARTING] = {
1630 .enter_state = scic_sds_remote_device_starting_state_enter,
1632 [SCI_BASE_REMOTE_DEVICE_STATE_READY] = {
1633 .enter_state = scic_sds_remote_device_ready_state_enter,
1634 .exit_state = scic_sds_remote_device_ready_state_exit
1636 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE] = {
1637 .enter_state = scic_sds_stp_remote_device_ready_idle_substate_enter,
1639 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD] = {
1640 .enter_state = scic_sds_stp_remote_device_ready_cmd_substate_enter,
1642 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ] = {
1643 .enter_state = scic_sds_stp_remote_device_ready_ncq_substate_enter,
1645 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR] = {
1646 .enter_state = scic_sds_stp_remote_device_ready_ncq_error_substate_enter,
1648 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET] = {
1649 .enter_state = scic_sds_stp_remote_device_ready_await_reset_substate_enter,
1651 [SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE] = {
1652 .enter_state = scic_sds_smp_remote_device_ready_idle_substate_enter,
1654 [SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD] = {
1655 .enter_state = scic_sds_smp_remote_device_ready_cmd_substate_enter,
1656 .exit_state = scic_sds_smp_remote_device_ready_cmd_substate_exit,
1658 [SCI_BASE_REMOTE_DEVICE_STATE_STOPPING] = {
1659 .enter_state = scic_sds_remote_device_stopping_state_enter,
1661 [SCI_BASE_REMOTE_DEVICE_STATE_FAILED] = {
1662 .enter_state = scic_sds_remote_device_failed_state_enter,
1664 [SCI_BASE_REMOTE_DEVICE_STATE_RESETTING] = {
1665 .enter_state = scic_sds_remote_device_resetting_state_enter,
1666 .exit_state = scic_sds_remote_device_resetting_state_exit
1668 [SCI_BASE_REMOTE_DEVICE_STATE_FINAL] = {
1669 .enter_state = scic_sds_remote_device_final_state_enter,
1674 * scic_remote_device_construct() - common construction
1675 * @sci_port: SAS/SATA port through which this device is accessed.
1676 * @sci_dev: remote device to construct
1678 * This routine just performs benign initialization and does not
1679 * allocate the remote_node_context which is left to
1680 * scic_remote_device_[de]a_construct(). scic_remote_device_destruct()
1681 * frees the remote_node_context(s) for the device.
1683 static void scic_remote_device_construct(struct scic_sds_port *sci_port,
1684 struct scic_sds_remote_device *sci_dev)
1686 sci_dev->owning_port = sci_port;
1687 sci_dev->started_request_count = 0;
1689 sci_base_state_machine_construct(
1690 &sci_dev->state_machine,
1691 sci_dev,
1692 scic_sds_remote_device_state_table,
1693 SCI_BASE_REMOTE_DEVICE_STATE_INITIAL
1696 sci_base_state_machine_start(
1697 &sci_dev->state_machine
1700 scic_sds_remote_node_context_construct(&sci_dev->rnc,
1701 SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX);
1705 * scic_remote_device_da_construct() - construct direct attached device.
1707 * The information (e.g. IAF, Signature FIS, etc.) necessary to build
1708 * the device is known to the SCI Core since it is contained in the
1709 * scic_phy object. Remote node context(s) is/are a global resource
1710 * allocated by this routine, freed by scic_remote_device_destruct().
1712 * Returns:
1713 * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
1714 * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
1715 * sata-only controller instance.
1716 * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
1718 static enum sci_status scic_remote_device_da_construct(struct scic_sds_port *sci_port,
1719 struct scic_sds_remote_device *sci_dev)
1721 enum sci_status status;
1722 struct domain_device *dev = sci_dev_to_domain(sci_dev);
1724 scic_remote_device_construct(sci_port, sci_dev);
1727 * This information is request to determine how many remote node context
1728 * entries will be needed to store the remote node.
1730 sci_dev->is_direct_attached = true;
1731 status = scic_sds_controller_allocate_remote_node_context(sci_port->owning_controller,
1732 sci_dev,
1733 &sci_dev->rnc.remote_node_index);
1735 if (status != SCI_SUCCESS)
1736 return status;
1738 if (dev->dev_type == SAS_END_DEV || dev->dev_type == SATA_DEV ||
1739 (dev->tproto & SAS_PROTOCOL_STP) || dev_is_expander(dev))
1740 /* pass */;
1741 else
1742 return SCI_FAILURE_UNSUPPORTED_PROTOCOL;
1744 sci_dev->connection_rate = scic_sds_port_get_max_allowed_speed(sci_port);
1746 /* / @todo Should I assign the port width by reading all of the phys on the port? */
1747 sci_dev->device_port_width = 1;
1749 return SCI_SUCCESS;
1753 * scic_remote_device_ea_construct() - construct expander attached device
1755 * Remote node context(s) is/are a global resource allocated by this
1756 * routine, freed by scic_remote_device_destruct().
1758 * Returns:
1759 * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
1760 * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
1761 * sata-only controller instance.
1762 * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
1764 static enum sci_status scic_remote_device_ea_construct(struct scic_sds_port *sci_port,
1765 struct scic_sds_remote_device *sci_dev)
1767 struct domain_device *dev = sci_dev_to_domain(sci_dev);
1768 enum sci_status status;
1770 scic_remote_device_construct(sci_port, sci_dev);
1772 status = scic_sds_controller_allocate_remote_node_context(sci_port->owning_controller,
1773 sci_dev,
1774 &sci_dev->rnc.remote_node_index);
1775 if (status != SCI_SUCCESS)
1776 return status;
1778 if (dev->dev_type == SAS_END_DEV || dev->dev_type == SATA_DEV ||
1779 (dev->tproto & SAS_PROTOCOL_STP) || dev_is_expander(dev))
1780 /* pass */;
1781 else
1782 return SCI_FAILURE_UNSUPPORTED_PROTOCOL;
1785 * For SAS-2 the physical link rate is actually a logical link
1786 * rate that incorporates multiplexing. The SCU doesn't
1787 * incorporate multiplexing and for the purposes of the
1788 * connection the logical link rate is that same as the
1789 * physical. Furthermore, the SAS-2 and SAS-1.1 fields overlay
1790 * one another, so this code works for both situations. */
1791 sci_dev->connection_rate = min_t(u16, scic_sds_port_get_max_allowed_speed(sci_port),
1792 dev->linkrate);
1794 /* / @todo Should I assign the port width by reading all of the phys on the port? */
1795 sci_dev->device_port_width = 1;
1797 return SCI_SUCCESS;
1801 * scic_remote_device_start() - This method will start the supplied remote
1802 * device. This method enables normal IO requests to flow through to the
1803 * remote device.
1804 * @remote_device: This parameter specifies the device to be started.
1805 * @timeout: This parameter specifies the number of milliseconds in which the
1806 * start operation should complete.
1808 * An indication of whether the device was successfully started. SCI_SUCCESS
1809 * This value is returned if the device was successfully started.
1810 * SCI_FAILURE_INVALID_PHY This value is returned if the user attempts to start
1811 * the device when there have been no phys added to it.
1813 static enum sci_status scic_remote_device_start(struct scic_sds_remote_device *sci_dev,
1814 u32 timeout)
1816 struct sci_base_state_machine *sm = &sci_dev->state_machine;
1817 enum scic_sds_remote_device_states state = sm->current_state_id;
1818 enum sci_status status;
1820 if (state != SCI_BASE_REMOTE_DEVICE_STATE_STOPPED) {
1821 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
1822 __func__, state);
1823 return SCI_FAILURE_INVALID_STATE;
1826 status = scic_sds_remote_node_context_resume(&sci_dev->rnc,
1827 remote_device_resume_done,
1828 sci_dev);
1829 if (status != SCI_SUCCESS)
1830 return status;
1832 sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_STARTING);
1834 return SCI_SUCCESS;
1837 static enum sci_status isci_remote_device_construct(struct isci_port *iport,
1838 struct isci_remote_device *idev)
1840 struct scic_sds_port *sci_port = iport->sci_port_handle;
1841 struct isci_host *ihost = iport->isci_host;
1842 struct domain_device *dev = idev->domain_dev;
1843 enum sci_status status;
1845 if (dev->parent && dev_is_expander(dev->parent))
1846 status = scic_remote_device_ea_construct(sci_port, &idev->sci);
1847 else
1848 status = scic_remote_device_da_construct(sci_port, &idev->sci);
1850 if (status != SCI_SUCCESS) {
1851 dev_dbg(&ihost->pdev->dev, "%s: construct failed: %d\n",
1852 __func__, status);
1854 return status;
1857 /* start the device. */
1858 status = scic_remote_device_start(&idev->sci, ISCI_REMOTE_DEVICE_START_TIMEOUT);
1860 if (status != SCI_SUCCESS)
1861 dev_warn(&ihost->pdev->dev, "remote device start failed: %d\n",
1862 status);
1864 return status;
1867 void isci_remote_device_nuke_requests(struct isci_host *ihost, struct isci_remote_device *idev)
1869 DECLARE_COMPLETION_ONSTACK(aborted_task_completion);
1871 dev_dbg(&ihost->pdev->dev,
1872 "%s: idev = %p\n", __func__, idev);
1874 /* Cleanup all requests pending for this device. */
1875 isci_terminate_pending_requests(ihost, idev, terminating);
1877 dev_dbg(&ihost->pdev->dev,
1878 "%s: idev = %p, done\n", __func__, idev);
1882 * This function builds the isci_remote_device when a libsas dev_found message
1883 * is received.
1884 * @isci_host: This parameter specifies the isci host object.
1885 * @port: This parameter specifies the isci_port conected to this device.
1887 * pointer to new isci_remote_device.
1889 static struct isci_remote_device *
1890 isci_remote_device_alloc(struct isci_host *ihost, struct isci_port *iport)
1892 struct isci_remote_device *idev;
1893 int i;
1895 for (i = 0; i < SCI_MAX_REMOTE_DEVICES; i++) {
1896 idev = &ihost->devices[i];
1897 if (!test_and_set_bit(IDEV_ALLOCATED, &idev->flags))
1898 break;
1901 if (i >= SCI_MAX_REMOTE_DEVICES) {
1902 dev_warn(&ihost->pdev->dev, "%s: failed\n", __func__);
1903 return NULL;
1906 if (WARN_ONCE(!list_empty(&idev->reqs_in_process), "found requests in process\n"))
1907 return NULL;
1909 if (WARN_ONCE(!list_empty(&idev->node), "found non-idle remote device\n"))
1910 return NULL;
1912 isci_remote_device_change_state(idev, isci_freed);
1914 return idev;
1918 * isci_remote_device_stop() - This function is called internally to stop the
1919 * remote device.
1920 * @isci_host: This parameter specifies the isci host object.
1921 * @isci_device: This parameter specifies the remote device.
1923 * The status of the scic request to stop.
1925 enum sci_status isci_remote_device_stop(struct isci_host *ihost, struct isci_remote_device *idev)
1927 enum sci_status status;
1928 unsigned long flags;
1930 dev_dbg(&ihost->pdev->dev,
1931 "%s: isci_device = %p\n", __func__, idev);
1933 isci_remote_device_change_state(idev, isci_stopping);
1935 /* Kill all outstanding requests. */
1936 isci_remote_device_nuke_requests(ihost, idev);
1938 set_bit(IDEV_STOP_PENDING, &idev->flags);
1940 spin_lock_irqsave(&ihost->scic_lock, flags);
1941 status = scic_remote_device_stop(&idev->sci, 50);
1942 spin_unlock_irqrestore(&ihost->scic_lock, flags);
1944 /* Wait for the stop complete callback. */
1945 if (status == SCI_SUCCESS) {
1946 wait_for_device_stop(ihost, idev);
1947 clear_bit(IDEV_ALLOCATED, &idev->flags);
1950 dev_dbg(&ihost->pdev->dev,
1951 "%s: idev = %p - after completion wait\n",
1952 __func__, idev);
1954 return status;
1958 * isci_remote_device_gone() - This function is called by libsas when a domain
1959 * device is removed.
1960 * @domain_device: This parameter specifies the libsas domain device.
1963 void isci_remote_device_gone(struct domain_device *dev)
1965 struct isci_host *ihost = dev_to_ihost(dev);
1966 struct isci_remote_device *idev = dev->lldd_dev;
1968 dev_dbg(&ihost->pdev->dev,
1969 "%s: domain_device = %p, isci_device = %p, isci_port = %p\n",
1970 __func__, dev, idev, idev->isci_port);
1972 isci_remote_device_stop(ihost, idev);
1977 * isci_remote_device_found() - This function is called by libsas when a remote
1978 * device is discovered. A remote device object is created and started. the
1979 * function then sleeps until the sci core device started message is
1980 * received.
1981 * @domain_device: This parameter specifies the libsas domain device.
1983 * status, zero indicates success.
1985 int isci_remote_device_found(struct domain_device *domain_dev)
1987 struct isci_host *isci_host = dev_to_ihost(domain_dev);
1988 struct isci_port *isci_port;
1989 struct isci_phy *isci_phy;
1990 struct asd_sas_port *sas_port;
1991 struct asd_sas_phy *sas_phy;
1992 struct isci_remote_device *isci_device;
1993 enum sci_status status;
1995 dev_dbg(&isci_host->pdev->dev,
1996 "%s: domain_device = %p\n", __func__, domain_dev);
1998 wait_for_start(isci_host);
2000 sas_port = domain_dev->port;
2001 sas_phy = list_first_entry(&sas_port->phy_list, struct asd_sas_phy,
2002 port_phy_el);
2003 isci_phy = to_isci_phy(sas_phy);
2004 isci_port = isci_phy->isci_port;
2006 /* we are being called for a device on this port,
2007 * so it has to come up eventually
2009 wait_for_completion(&isci_port->start_complete);
2011 if ((isci_stopping == isci_port_get_state(isci_port)) ||
2012 (isci_stopped == isci_port_get_state(isci_port)))
2013 return -ENODEV;
2015 isci_device = isci_remote_device_alloc(isci_host, isci_port);
2016 if (!isci_device)
2017 return -ENODEV;
2019 INIT_LIST_HEAD(&isci_device->node);
2020 domain_dev->lldd_dev = isci_device;
2021 isci_device->domain_dev = domain_dev;
2022 isci_device->isci_port = isci_port;
2023 isci_remote_device_change_state(isci_device, isci_starting);
2026 spin_lock_irq(&isci_host->scic_lock);
2027 list_add_tail(&isci_device->node, &isci_port->remote_dev_list);
2029 set_bit(IDEV_START_PENDING, &isci_device->flags);
2030 status = isci_remote_device_construct(isci_port, isci_device);
2031 spin_unlock_irq(&isci_host->scic_lock);
2033 dev_dbg(&isci_host->pdev->dev,
2034 "%s: isci_device = %p\n",
2035 __func__, isci_device);
2037 if (status != SCI_SUCCESS) {
2039 spin_lock_irq(&isci_host->scic_lock);
2040 isci_remote_device_deconstruct(
2041 isci_host,
2042 isci_device
2044 spin_unlock_irq(&isci_host->scic_lock);
2045 return -ENODEV;
2048 /* wait for the device ready callback. */
2049 wait_for_device_start(isci_host, isci_device);
2051 return 0;
2054 * isci_device_is_reset_pending() - This function will check if there is any
2055 * pending reset condition on the device.
2056 * @request: This parameter is the isci_device object.
2058 * true if there is a reset pending for the device.
2060 bool isci_device_is_reset_pending(
2061 struct isci_host *isci_host,
2062 struct isci_remote_device *isci_device)
2064 struct isci_request *isci_request;
2065 struct isci_request *tmp_req;
2066 bool reset_is_pending = false;
2067 unsigned long flags;
2069 dev_dbg(&isci_host->pdev->dev,
2070 "%s: isci_device = %p\n", __func__, isci_device);
2072 spin_lock_irqsave(&isci_host->scic_lock, flags);
2074 /* Check for reset on all pending requests. */
2075 list_for_each_entry_safe(isci_request, tmp_req,
2076 &isci_device->reqs_in_process, dev_node) {
2077 dev_dbg(&isci_host->pdev->dev,
2078 "%s: isci_device = %p request = %p\n",
2079 __func__, isci_device, isci_request);
2081 if (isci_request->ttype == io_task) {
2082 struct sas_task *task = isci_request_access_task(
2083 isci_request);
2085 spin_lock(&task->task_state_lock);
2086 if (task->task_state_flags & SAS_TASK_NEED_DEV_RESET)
2087 reset_is_pending = true;
2088 spin_unlock(&task->task_state_lock);
2092 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
2094 dev_dbg(&isci_host->pdev->dev,
2095 "%s: isci_device = %p reset_is_pending = %d\n",
2096 __func__, isci_device, reset_is_pending);
2098 return reset_is_pending;
2102 * isci_device_clear_reset_pending() - This function will clear if any pending
2103 * reset condition flags on the device.
2104 * @request: This parameter is the isci_device object.
2106 * true if there is a reset pending for the device.
2108 void isci_device_clear_reset_pending(struct isci_host *ihost, struct isci_remote_device *idev)
2110 struct isci_request *isci_request;
2111 struct isci_request *tmp_req;
2112 unsigned long flags = 0;
2114 dev_dbg(&ihost->pdev->dev, "%s: idev=%p, ihost=%p\n",
2115 __func__, idev, ihost);
2117 spin_lock_irqsave(&ihost->scic_lock, flags);
2119 /* Clear reset pending on all pending requests. */
2120 list_for_each_entry_safe(isci_request, tmp_req,
2121 &idev->reqs_in_process, dev_node) {
2122 dev_dbg(&ihost->pdev->dev, "%s: idev = %p request = %p\n",
2123 __func__, idev, isci_request);
2125 if (isci_request->ttype == io_task) {
2127 unsigned long flags2;
2128 struct sas_task *task = isci_request_access_task(
2129 isci_request);
2131 spin_lock_irqsave(&task->task_state_lock, flags2);
2132 task->task_state_flags &= ~SAS_TASK_NEED_DEV_RESET;
2133 spin_unlock_irqrestore(&task->task_state_lock, flags2);
2136 spin_unlock_irqrestore(&ihost->scic_lock, flags);