2 * This file is provided under a dual BSD/GPLv2 license. When using or
3 * redistributing this file, you may do so under either license.
7 * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of version 2 of the GNU General Public License as
11 * published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
21 * The full GNU General Public License is included in this distribution
22 * in the file called LICENSE.GPL.
26 * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
27 * All rights reserved.
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
33 * * Redistributions of source code must retain the above copyright
34 * notice, this list of conditions and the following disclaimer.
35 * * Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer in
37 * the documentation and/or other materials provided with the
39 * * Neither the name of Intel Corporation nor the names of its
40 * contributors may be used to endorse or promote products derived
41 * from this software without specific prior written permission.
43 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
44 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
45 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
46 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
47 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
48 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
49 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
50 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
51 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
52 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
53 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
58 #include "remote_device.h"
60 #include "scic_controller.h"
61 #include "scic_io_request.h"
63 #include "scic_port.h"
64 #include "scic_sds_controller.h"
65 #include "scic_sds_phy.h"
66 #include "scic_sds_port.h"
67 #include "remote_node_context.h"
68 #include "scic_sds_request.h"
69 #include "sci_environment.h"
71 #include "scu_event_codes.h"
75 * isci_remote_device_change_state() - This function gets the status of the
76 * remote_device object.
77 * @isci_device: This parameter points to the isci_remote_device object
79 * status of the object as a isci_status enum.
81 void isci_remote_device_change_state(
82 struct isci_remote_device *isci_device,
83 enum isci_status status)
87 spin_lock_irqsave(&isci_device->state_lock, flags);
88 isci_device->status = status;
89 spin_unlock_irqrestore(&isci_device->state_lock, flags);
93 * isci_remote_device_not_ready() - This function is called by the scic when
94 * the remote device is not ready. We mark the isci device as ready (not
95 * "ready_for_io") and signal the waiting proccess.
96 * @isci_host: This parameter specifies the isci host object.
97 * @isci_device: This parameter specifies the remote device
100 static void isci_remote_device_not_ready(struct isci_host *ihost,
101 struct isci_remote_device *idev, u32 reason)
103 dev_dbg(&ihost->pdev->dev,
104 "%s: isci_device = %p\n", __func__, idev);
106 if (reason == SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED)
107 isci_remote_device_change_state(idev, isci_stopping);
109 /* device ready is actually a "not ready for io" state. */
110 isci_remote_device_change_state(idev, isci_ready);
114 * isci_remote_device_ready() - This function is called by the scic when the
115 * remote device is ready. We mark the isci device as ready and signal the
117 * @ihost: our valid isci_host
118 * @idev: remote device
121 static void isci_remote_device_ready(struct isci_host *ihost, struct isci_remote_device *idev)
123 dev_dbg(&ihost->pdev->dev,
124 "%s: idev = %p\n", __func__, idev);
126 isci_remote_device_change_state(idev, isci_ready_for_io);
127 if (test_and_clear_bit(IDEV_START_PENDING, &idev->flags))
128 wake_up(&ihost->eventq);
131 /* called once the remote node context is ready to be freed.
132 * The remote device can now report that its stop operation is complete. none
134 static void rnc_destruct_done(void *_dev)
136 struct scic_sds_remote_device *sci_dev = _dev;
138 BUG_ON(sci_dev->started_request_count != 0);
139 sci_base_state_machine_change_state(&sci_dev->state_machine,
140 SCI_BASE_REMOTE_DEVICE_STATE_STOPPED);
143 static enum sci_status scic_sds_remote_device_terminate_requests(struct scic_sds_remote_device *sci_dev)
145 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
146 u32 i, request_count = sci_dev->started_request_count;
147 enum sci_status status = SCI_SUCCESS;
149 for (i = 0; i < SCI_MAX_IO_REQUESTS && i < request_count; i++) {
150 struct scic_sds_request *sci_req;
153 sci_req = scic->io_request_table[i];
154 if (!sci_req || sci_req->target_device != sci_dev)
156 s = scic_controller_terminate_request(scic, sci_dev, sci_req);
157 if (s != SCI_SUCCESS)
164 enum sci_status scic_remote_device_stop(struct scic_sds_remote_device *sci_dev,
167 struct sci_base_state_machine *sm = &sci_dev->state_machine;
168 enum scic_sds_remote_device_states state = sm->current_state_id;
171 case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL:
172 case SCI_BASE_REMOTE_DEVICE_STATE_FAILED:
173 case SCI_BASE_REMOTE_DEVICE_STATE_FINAL:
175 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
177 return SCI_FAILURE_INVALID_STATE;
178 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED:
180 case SCI_BASE_REMOTE_DEVICE_STATE_STARTING:
181 /* device not started so there had better be no requests */
182 BUG_ON(sci_dev->started_request_count != 0);
183 scic_sds_remote_node_context_destruct(&sci_dev->rnc,
184 rnc_destruct_done, sci_dev);
185 /* Transition to the stopping state and wait for the
186 * remote node to complete being posted and invalidated.
188 sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_STOPPING);
190 case SCI_BASE_REMOTE_DEVICE_STATE_READY:
191 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
192 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
193 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ:
194 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
195 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
196 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
197 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
198 sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_STOPPING);
199 if (sci_dev->started_request_count == 0) {
200 scic_sds_remote_node_context_destruct(&sci_dev->rnc,
201 rnc_destruct_done, sci_dev);
204 return scic_sds_remote_device_terminate_requests(sci_dev);
206 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING:
207 /* All requests should have been terminated, but if there is an
208 * attempt to stop a device already in the stopping state, then
209 * try again to terminate.
211 return scic_sds_remote_device_terminate_requests(sci_dev);
212 case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING:
213 sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_STOPPING);
218 enum sci_status scic_remote_device_reset(struct scic_sds_remote_device *sci_dev)
220 struct sci_base_state_machine *sm = &sci_dev->state_machine;
221 enum scic_sds_remote_device_states state = sm->current_state_id;
224 case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL:
225 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED:
226 case SCI_BASE_REMOTE_DEVICE_STATE_STARTING:
227 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
228 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
229 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING:
230 case SCI_BASE_REMOTE_DEVICE_STATE_FAILED:
231 case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING:
232 case SCI_BASE_REMOTE_DEVICE_STATE_FINAL:
234 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
236 return SCI_FAILURE_INVALID_STATE;
237 case SCI_BASE_REMOTE_DEVICE_STATE_READY:
238 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
239 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
240 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ:
241 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
242 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
243 sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_RESETTING);
248 enum sci_status scic_remote_device_reset_complete(struct scic_sds_remote_device *sci_dev)
250 struct sci_base_state_machine *sm = &sci_dev->state_machine;
251 enum scic_sds_remote_device_states state = sm->current_state_id;
253 if (state != SCI_BASE_REMOTE_DEVICE_STATE_RESETTING) {
254 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
256 return SCI_FAILURE_INVALID_STATE;
259 sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_READY);
263 enum sci_status scic_sds_remote_device_suspend(struct scic_sds_remote_device *sci_dev,
266 struct sci_base_state_machine *sm = &sci_dev->state_machine;
267 enum scic_sds_remote_device_states state = sm->current_state_id;
269 if (state != SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD) {
270 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
272 return SCI_FAILURE_INVALID_STATE;
275 return scic_sds_remote_node_context_suspend(&sci_dev->rnc,
276 suspend_type, NULL, NULL);
279 enum sci_status scic_sds_remote_device_frame_handler(struct scic_sds_remote_device *sci_dev,
282 struct sci_base_state_machine *sm = &sci_dev->state_machine;
283 enum scic_sds_remote_device_states state = sm->current_state_id;
284 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
285 enum sci_status status;
288 case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL:
289 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED:
290 case SCI_BASE_REMOTE_DEVICE_STATE_STARTING:
291 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
292 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
293 case SCI_BASE_REMOTE_DEVICE_STATE_FINAL:
295 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
297 /* Return the frame back to the controller */
298 scic_sds_controller_release_frame(scic, frame_index);
299 return SCI_FAILURE_INVALID_STATE;
300 case SCI_BASE_REMOTE_DEVICE_STATE_READY:
301 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
302 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
303 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING:
304 case SCI_BASE_REMOTE_DEVICE_STATE_FAILED:
305 case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING: {
306 struct scic_sds_request *sci_req;
307 struct ssp_frame_hdr hdr;
311 status = scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
314 if (status != SCI_SUCCESS)
317 word_cnt = sizeof(hdr) / sizeof(u32);
318 sci_swab32_cpy(&hdr, frame_header, word_cnt);
320 sci_req = scic_request_by_tag(scic, be16_to_cpu(hdr.tag));
321 if (sci_req && sci_req->target_device == sci_dev) {
322 /* The IO request is now in charge of releasing the frame */
323 status = sci_req->state_handlers->frame_handler(sci_req,
326 /* We could not map this tag to a valid IO
327 * request Just toss the frame and continue
329 scic_sds_controller_release_frame(scic, frame_index);
333 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ: {
334 struct dev_to_host_fis *hdr;
336 status = scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
339 if (status != SCI_SUCCESS)
342 if (hdr->fis_type == FIS_SETDEVBITS &&
343 (hdr->status & ATA_ERR)) {
344 sci_dev->not_ready_reason = SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED;
346 /* TODO Check sactive and complete associated IO if any. */
347 sci_base_state_machine_change_state(sm, SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR);
348 } else if (hdr->fis_type == FIS_REGD2H &&
349 (hdr->status & ATA_ERR)) {
351 * Some devices return D2H FIS when an NCQ error is detected.
352 * Treat this like an SDB error FIS ready reason.
354 sci_dev->not_ready_reason = SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED;
355 sci_base_state_machine_change_state(&sci_dev->state_machine,
356 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR);
358 status = SCI_FAILURE;
360 scic_sds_controller_release_frame(scic, frame_index);
363 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
364 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
365 /* The device does not process any UF received from the hardware while
366 * in this state. All unsolicited frames are forwarded to the io request
369 status = scic_sds_io_request_frame_handler(sci_dev->working_request, frame_index);
376 static bool is_remote_device_ready(struct scic_sds_remote_device *sci_dev)
379 struct sci_base_state_machine *sm = &sci_dev->state_machine;
380 enum scic_sds_remote_device_states state = sm->current_state_id;
383 case SCI_BASE_REMOTE_DEVICE_STATE_READY:
384 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
385 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
386 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ:
387 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
388 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
389 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
390 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
397 enum sci_status scic_sds_remote_device_event_handler(struct scic_sds_remote_device *sci_dev,
400 struct sci_base_state_machine *sm = &sci_dev->state_machine;
401 enum scic_sds_remote_device_states state = sm->current_state_id;
402 enum sci_status status;
404 switch (scu_get_event_type(event_code)) {
405 case SCU_EVENT_TYPE_RNC_OPS_MISC:
406 case SCU_EVENT_TYPE_RNC_SUSPEND_TX:
407 case SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX:
408 status = scic_sds_remote_node_context_event_handler(&sci_dev->rnc, event_code);
410 case SCU_EVENT_TYPE_PTX_SCHEDULE_EVENT:
411 if (scu_get_event_code(event_code) == SCU_EVENT_IT_NEXUS_TIMEOUT) {
412 status = SCI_SUCCESS;
414 /* Suspend the associated RNC */
415 scic_sds_remote_node_context_suspend(&sci_dev->rnc,
416 SCI_SOFTWARE_SUSPENSION,
419 dev_dbg(scirdev_to_dev(sci_dev),
420 "%s: device: %p event code: %x: %s\n",
421 __func__, sci_dev, event_code,
422 is_remote_device_ready(sci_dev)
423 ? "I_T_Nexus_Timeout event"
424 : "I_T_Nexus_Timeout event in wrong state");
428 /* Else, fall through and treat as unhandled... */
430 dev_dbg(scirdev_to_dev(sci_dev),
431 "%s: device: %p event code: %x: %s\n",
432 __func__, sci_dev, event_code,
433 is_remote_device_ready(sci_dev)
435 : "unexpected event in wrong state");
436 status = SCI_FAILURE_INVALID_STATE;
440 if (status != SCI_SUCCESS)
443 if (state == SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE) {
445 /* We pick up suspension events to handle specifically to this
446 * state. We resume the RNC right away.
448 if (scu_get_event_type(event_code) == SCU_EVENT_TYPE_RNC_SUSPEND_TX ||
449 scu_get_event_type(event_code) == SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX)
450 status = scic_sds_remote_node_context_resume(&sci_dev->rnc, NULL, NULL);
456 static void scic_sds_remote_device_start_request(struct scic_sds_remote_device *sci_dev,
457 struct scic_sds_request *sci_req,
458 enum sci_status status)
460 struct scic_sds_port *sci_port = sci_dev->owning_port;
462 /* cleanup requests that failed after starting on the port */
463 if (status != SCI_SUCCESS)
464 scic_sds_port_complete_io(sci_port, sci_dev, sci_req);
466 scic_sds_remote_device_increment_request_count(sci_dev);
469 enum sci_status scic_sds_remote_device_start_io(struct scic_sds_controller *scic,
470 struct scic_sds_remote_device *sci_dev,
471 struct scic_sds_request *sci_req)
473 struct sci_base_state_machine *sm = &sci_dev->state_machine;
474 enum scic_sds_remote_device_states state = sm->current_state_id;
475 struct scic_sds_port *sci_port = sci_dev->owning_port;
476 struct isci_request *ireq = sci_req_to_ireq(sci_req);
477 enum sci_status status;
480 case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL:
481 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED:
482 case SCI_BASE_REMOTE_DEVICE_STATE_STARTING:
483 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
484 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING:
485 case SCI_BASE_REMOTE_DEVICE_STATE_FAILED:
486 case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING:
487 case SCI_BASE_REMOTE_DEVICE_STATE_FINAL:
489 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
491 return SCI_FAILURE_INVALID_STATE;
492 case SCI_BASE_REMOTE_DEVICE_STATE_READY:
493 /* attempt to start an io request for this device object. The remote
494 * device object will issue the start request for the io and if
495 * successful it will start the request for the port object then
496 * increment its own request count.
498 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
499 if (status != SCI_SUCCESS)
502 status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, sci_req);
503 if (status != SCI_SUCCESS)
506 status = scic_sds_request_start(sci_req);
508 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE: {
509 /* handle the start io operation for a sata device that is in
510 * the command idle state. - Evalute the type of IO request to
511 * be started - If its an NCQ request change to NCQ substate -
512 * If its any other command change to the CMD substate
514 * If this is a softreset we may want to have a different
517 enum scic_sds_remote_device_states new_state;
518 struct sas_task *task = isci_request_access_task(ireq);
520 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
521 if (status != SCI_SUCCESS)
524 status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, sci_req);
525 if (status != SCI_SUCCESS)
528 status = sci_req->state_handlers->start_handler(sci_req);
529 if (status != SCI_SUCCESS)
532 if (task->ata_task.use_ncq)
533 new_state = SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ;
535 sci_dev->working_request = sci_req;
536 new_state = SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD;
538 sci_base_state_machine_change_state(sm, new_state);
541 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ: {
542 struct sas_task *task = isci_request_access_task(ireq);
544 if (task->ata_task.use_ncq) {
545 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
546 if (status != SCI_SUCCESS)
549 status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, sci_req);
550 if (status != SCI_SUCCESS)
553 status = sci_req->state_handlers->start_handler(sci_req);
555 return SCI_FAILURE_INVALID_STATE;
558 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
559 return SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED;
560 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
561 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
562 if (status != SCI_SUCCESS)
565 status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, sci_req);
566 if (status != SCI_SUCCESS)
569 status = scic_sds_request_start(sci_req);
570 if (status != SCI_SUCCESS)
573 sci_dev->working_request = sci_req;
574 sci_base_state_machine_change_state(&sci_dev->state_machine,
575 SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD);
577 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
578 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
579 /* device is already handling a command it can not accept new commands
580 * until this one is complete.
582 return SCI_FAILURE_INVALID_STATE;
585 scic_sds_remote_device_start_request(sci_dev, sci_req, status);
589 static enum sci_status common_complete_io(struct scic_sds_port *sci_port,
590 struct scic_sds_remote_device *sci_dev,
591 struct scic_sds_request *sci_req)
593 enum sci_status status;
595 status = scic_sds_request_complete(sci_req);
596 if (status != SCI_SUCCESS)
599 status = scic_sds_port_complete_io(sci_port, sci_dev, sci_req);
600 if (status != SCI_SUCCESS)
603 scic_sds_remote_device_decrement_request_count(sci_dev);
607 enum sci_status scic_sds_remote_device_complete_io(struct scic_sds_controller *scic,
608 struct scic_sds_remote_device *sci_dev,
609 struct scic_sds_request *sci_req)
611 struct sci_base_state_machine *sm = &sci_dev->state_machine;
612 enum scic_sds_remote_device_states state = sm->current_state_id;
613 struct scic_sds_port *sci_port = sci_dev->owning_port;
614 enum sci_status status;
617 case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL:
618 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED:
619 case SCI_BASE_REMOTE_DEVICE_STATE_STARTING:
620 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
621 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
622 case SCI_BASE_REMOTE_DEVICE_STATE_FAILED:
623 case SCI_BASE_REMOTE_DEVICE_STATE_FINAL:
625 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
627 return SCI_FAILURE_INVALID_STATE;
628 case SCI_BASE_REMOTE_DEVICE_STATE_READY:
629 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
630 case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING:
631 status = common_complete_io(sci_port, sci_dev, sci_req);
633 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
634 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ:
635 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
636 status = common_complete_io(sci_port, sci_dev, sci_req);
637 if (status != SCI_SUCCESS)
640 if (sci_req->sci_status == SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED) {
641 /* This request causes hardware error, device needs to be Lun Reset.
642 * So here we force the state machine to IDLE state so the rest IOs
643 * can reach RNC state handler, these IOs will be completed by RNC with
644 * status of "DEVICE_RESET_REQUIRED", instead of "INVALID STATE".
646 sci_base_state_machine_change_state(sm, SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET);
647 } else if (scic_sds_remote_device_get_request_count(sci_dev) == 0)
648 sci_base_state_machine_change_state(sm, SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
650 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
651 status = common_complete_io(sci_port, sci_dev, sci_req);
652 if (status != SCI_SUCCESS)
654 sci_base_state_machine_change_state(sm, SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
656 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING:
657 status = common_complete_io(sci_port, sci_dev, sci_req);
658 if (status != SCI_SUCCESS)
661 if (scic_sds_remote_device_get_request_count(sci_dev) == 0)
662 scic_sds_remote_node_context_destruct(&sci_dev->rnc,
668 if (status != SCI_SUCCESS)
669 dev_err(scirdev_to_dev(sci_dev),
670 "%s: Port:0x%p Device:0x%p Request:0x%p Status:0x%x "
671 "could not complete\n", __func__, sci_port,
672 sci_dev, sci_req, status);
677 static void scic_sds_remote_device_continue_request(void *dev)
679 struct scic_sds_remote_device *sci_dev = dev;
681 /* we need to check if this request is still valid to continue. */
682 if (sci_dev->working_request)
683 scic_controller_continue_io(sci_dev->working_request);
686 enum sci_status scic_sds_remote_device_start_task(struct scic_sds_controller *scic,
687 struct scic_sds_remote_device *sci_dev,
688 struct scic_sds_request *sci_req)
690 struct sci_base_state_machine *sm = &sci_dev->state_machine;
691 enum scic_sds_remote_device_states state = sm->current_state_id;
692 struct scic_sds_port *sci_port = sci_dev->owning_port;
693 enum sci_status status;
696 case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL:
697 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED:
698 case SCI_BASE_REMOTE_DEVICE_STATE_STARTING:
699 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
700 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
701 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING:
702 case SCI_BASE_REMOTE_DEVICE_STATE_FAILED:
703 case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING:
704 case SCI_BASE_REMOTE_DEVICE_STATE_FINAL:
706 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
708 return SCI_FAILURE_INVALID_STATE;
709 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
710 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
711 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ:
712 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
713 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
714 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
715 if (status != SCI_SUCCESS)
718 status = scic_sds_remote_node_context_start_task(&sci_dev->rnc, sci_req);
719 if (status != SCI_SUCCESS)
722 status = sci_req->state_handlers->start_handler(sci_req);
723 if (status != SCI_SUCCESS)
726 /* Note: If the remote device state is not IDLE this will
727 * replace the request that probably resulted in the task
728 * management request.
730 sci_dev->working_request = sci_req;
731 sci_base_state_machine_change_state(sm, SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD);
733 /* The remote node context must cleanup the TCi to NCQ mapping
734 * table. The only way to do this correctly is to either write
735 * to the TLCR register or to invalidate and repost the RNC. In
736 * either case the remote node context state machine will take
737 * the correct action when the remote node context is suspended
740 scic_sds_remote_node_context_suspend(&sci_dev->rnc,
741 SCI_SOFTWARE_SUSPENSION, NULL, NULL);
742 scic_sds_remote_node_context_resume(&sci_dev->rnc,
743 scic_sds_remote_device_continue_request,
747 scic_sds_remote_device_start_request(sci_dev, sci_req, status);
748 /* We need to let the controller start request handler know that
749 * it can't post TC yet. We will provide a callback function to
750 * post TC when RNC gets resumed.
752 return SCI_FAILURE_RESET_DEVICE_PARTIAL_SUCCESS;
753 case SCI_BASE_REMOTE_DEVICE_STATE_READY:
754 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
755 if (status != SCI_SUCCESS)
758 status = scic_sds_remote_node_context_start_task(&sci_dev->rnc, sci_req);
759 if (status != SCI_SUCCESS)
762 status = scic_sds_request_start(sci_req);
765 scic_sds_remote_device_start_request(sci_dev, sci_req, status);
775 * This method takes the request and bulids an appropriate SCU context for the
776 * request and then requests the controller to post the request. none
778 void scic_sds_remote_device_post_request(
779 struct scic_sds_remote_device *sci_dev,
784 context = scic_sds_remote_device_build_command_context(sci_dev, request);
786 scic_sds_controller_post_request(
787 scic_sds_remote_device_get_controller(sci_dev),
792 /* called once the remote node context has transisitioned to a
793 * ready state. This is the indication that the remote device object can also
794 * transition to ready.
796 static void remote_device_resume_done(void *_dev)
798 struct scic_sds_remote_device *sci_dev = _dev;
800 if (is_remote_device_ready(sci_dev))
803 /* go 'ready' if we are not already in a ready state */
804 sci_base_state_machine_change_state(&sci_dev->state_machine,
805 SCI_BASE_REMOTE_DEVICE_STATE_READY);
808 static void scic_sds_stp_remote_device_ready_idle_substate_resume_complete_handler(void *_dev)
810 struct scic_sds_remote_device *sci_dev = _dev;
811 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
812 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
814 /* For NCQ operation we do not issue a isci_remote_device_not_ready().
815 * As a result, avoid sending the ready notification.
817 if (sci_dev->state_machine.previous_state_id != SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ)
818 isci_remote_device_ready(scic_to_ihost(scic), idev);
821 static void scic_sds_remote_device_initial_state_enter(void *object)
823 struct scic_sds_remote_device *sci_dev = object;
825 /* Initial state is a transitional state to the stopped state */
826 sci_base_state_machine_change_state(&sci_dev->state_machine,
827 SCI_BASE_REMOTE_DEVICE_STATE_STOPPED);
831 * scic_remote_device_destruct() - free remote node context and destruct
832 * @remote_device: This parameter specifies the remote device to be destructed.
834 * Remote device objects are a limited resource. As such, they must be
835 * protected. Thus calls to construct and destruct are mutually exclusive and
836 * non-reentrant. The return value shall indicate if the device was
837 * successfully destructed or if some failure occurred. enum sci_status This value
838 * is returned if the device is successfully destructed.
839 * SCI_FAILURE_INVALID_REMOTE_DEVICE This value is returned if the supplied
840 * device isn't valid (e.g. it's already been destoryed, the handle isn't
843 static enum sci_status scic_remote_device_destruct(struct scic_sds_remote_device *sci_dev)
845 struct sci_base_state_machine *sm = &sci_dev->state_machine;
846 enum scic_sds_remote_device_states state = sm->current_state_id;
847 struct scic_sds_controller *scic;
849 if (state != SCI_BASE_REMOTE_DEVICE_STATE_STOPPED) {
850 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
852 return SCI_FAILURE_INVALID_STATE;
855 scic = sci_dev->owning_port->owning_controller;
856 scic_sds_controller_free_remote_node_context(scic, sci_dev,
857 sci_dev->rnc.remote_node_index);
858 sci_dev->rnc.remote_node_index = SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX;
859 sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_FINAL);
865 * isci_remote_device_deconstruct() - This function frees an isci_remote_device.
866 * @ihost: This parameter specifies the isci host object.
867 * @idev: This parameter specifies the remote device to be freed.
870 static void isci_remote_device_deconstruct(struct isci_host *ihost, struct isci_remote_device *idev)
872 dev_dbg(&ihost->pdev->dev,
873 "%s: isci_device = %p\n", __func__, idev);
875 /* There should not be any outstanding io's. All paths to
876 * here should go through isci_remote_device_nuke_requests.
877 * If we hit this condition, we will need a way to complete
878 * io requests in process */
879 while (!list_empty(&idev->reqs_in_process)) {
881 dev_err(&ihost->pdev->dev,
882 "%s: ** request list not empty! **\n", __func__);
886 scic_remote_device_destruct(&idev->sci);
887 idev->domain_dev->lldd_dev = NULL;
888 idev->domain_dev = NULL;
889 idev->isci_port = NULL;
890 list_del_init(&idev->node);
892 clear_bit(IDEV_START_PENDING, &idev->flags);
893 clear_bit(IDEV_STOP_PENDING, &idev->flags);
894 clear_bit(IDEV_EH, &idev->flags);
895 wake_up(&ihost->eventq);
899 * isci_remote_device_stop_complete() - This function is called by the scic
900 * when the remote device stop has completed. We mark the isci device as not
901 * ready and remove the isci remote device.
902 * @ihost: This parameter specifies the isci host object.
903 * @idev: This parameter specifies the remote device.
904 * @status: This parameter specifies status of the completion.
907 static void isci_remote_device_stop_complete(struct isci_host *ihost,
908 struct isci_remote_device *idev)
910 dev_dbg(&ihost->pdev->dev, "%s: complete idev = %p\n", __func__, idev);
912 isci_remote_device_change_state(idev, isci_stopped);
914 /* after stop, we can tear down resources. */
915 isci_remote_device_deconstruct(ihost, idev);
918 static void scic_sds_remote_device_stopped_state_enter(void *object)
920 struct scic_sds_remote_device *sci_dev = object;
921 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
922 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
925 /* If we are entering from the stopping state let the SCI User know that
926 * the stop operation has completed.
928 prev_state = sci_dev->state_machine.previous_state_id;
929 if (prev_state == SCI_BASE_REMOTE_DEVICE_STATE_STOPPING)
930 isci_remote_device_stop_complete(scic_to_ihost(scic), idev);
932 scic_sds_controller_remote_device_stopped(scic, sci_dev);
935 static void scic_sds_remote_device_starting_state_enter(void *object)
937 struct scic_sds_remote_device *sci_dev = object;
938 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
939 struct isci_host *ihost = scic_to_ihost(scic);
940 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
942 isci_remote_device_not_ready(ihost, idev,
943 SCIC_REMOTE_DEVICE_NOT_READY_START_REQUESTED);
946 static void scic_sds_remote_device_ready_state_enter(void *object)
948 struct scic_sds_remote_device *sci_dev = object;
949 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
950 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
951 struct domain_device *dev = idev->domain_dev;
953 scic->remote_device_sequence[sci_dev->rnc.remote_node_index]++;
955 if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_SATA)) {
956 sci_base_state_machine_change_state(&sci_dev->state_machine,
957 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
958 } else if (dev_is_expander(dev)) {
959 sci_base_state_machine_change_state(&sci_dev->state_machine,
960 SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
962 isci_remote_device_ready(scic_to_ihost(scic), idev);
965 static void scic_sds_remote_device_ready_state_exit(void *object)
967 struct scic_sds_remote_device *sci_dev = object;
968 struct domain_device *dev = sci_dev_to_domain(sci_dev);
970 if (dev->dev_type == SAS_END_DEV) {
971 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
972 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
974 isci_remote_device_not_ready(scic_to_ihost(scic), idev,
975 SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED);
979 static void scic_sds_remote_device_resetting_state_enter(void *object)
981 struct scic_sds_remote_device *sci_dev = object;
983 scic_sds_remote_node_context_suspend(
984 &sci_dev->rnc, SCI_SOFTWARE_SUSPENSION, NULL, NULL);
987 static void scic_sds_remote_device_resetting_state_exit(void *object)
989 struct scic_sds_remote_device *sci_dev = object;
991 scic_sds_remote_node_context_resume(&sci_dev->rnc, NULL, NULL);
994 static void scic_sds_stp_remote_device_ready_idle_substate_enter(void *object)
996 struct scic_sds_remote_device *sci_dev = object;
998 sci_dev->working_request = NULL;
999 if (scic_sds_remote_node_context_is_ready(&sci_dev->rnc)) {
1001 * Since the RNC is ready, it's alright to finish completion
1002 * processing (e.g. signal the remote device is ready). */
1003 scic_sds_stp_remote_device_ready_idle_substate_resume_complete_handler(sci_dev);
1005 scic_sds_remote_node_context_resume(&sci_dev->rnc,
1006 scic_sds_stp_remote_device_ready_idle_substate_resume_complete_handler,
1011 static void scic_sds_stp_remote_device_ready_cmd_substate_enter(void *object)
1013 struct scic_sds_remote_device *sci_dev = object;
1014 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1016 BUG_ON(sci_dev->working_request == NULL);
1018 isci_remote_device_not_ready(scic_to_ihost(scic), sci_dev_to_idev(sci_dev),
1019 SCIC_REMOTE_DEVICE_NOT_READY_SATA_REQUEST_STARTED);
1022 static void scic_sds_stp_remote_device_ready_ncq_error_substate_enter(void *object)
1024 struct scic_sds_remote_device *sci_dev = object;
1025 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1026 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
1028 if (sci_dev->not_ready_reason == SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED)
1029 isci_remote_device_not_ready(scic_to_ihost(scic), idev,
1030 sci_dev->not_ready_reason);
1033 static void scic_sds_smp_remote_device_ready_idle_substate_enter(void *object)
1035 struct scic_sds_remote_device *sci_dev = object;
1036 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1038 isci_remote_device_ready(scic_to_ihost(scic), sci_dev_to_idev(sci_dev));
1041 static void scic_sds_smp_remote_device_ready_cmd_substate_enter(void *object)
1043 struct scic_sds_remote_device *sci_dev = object;
1044 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1046 BUG_ON(sci_dev->working_request == NULL);
1048 isci_remote_device_not_ready(scic_to_ihost(scic), sci_dev_to_idev(sci_dev),
1049 SCIC_REMOTE_DEVICE_NOT_READY_SMP_REQUEST_STARTED);
1052 static void scic_sds_smp_remote_device_ready_cmd_substate_exit(void *object)
1054 struct scic_sds_remote_device *sci_dev = object;
1056 sci_dev->working_request = NULL;
1059 static const struct sci_base_state scic_sds_remote_device_state_table[] = {
1060 [SCI_BASE_REMOTE_DEVICE_STATE_INITIAL] = {
1061 .enter_state = scic_sds_remote_device_initial_state_enter,
1063 [SCI_BASE_REMOTE_DEVICE_STATE_STOPPED] = {
1064 .enter_state = scic_sds_remote_device_stopped_state_enter,
1066 [SCI_BASE_REMOTE_DEVICE_STATE_STARTING] = {
1067 .enter_state = scic_sds_remote_device_starting_state_enter,
1069 [SCI_BASE_REMOTE_DEVICE_STATE_READY] = {
1070 .enter_state = scic_sds_remote_device_ready_state_enter,
1071 .exit_state = scic_sds_remote_device_ready_state_exit
1073 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE] = {
1074 .enter_state = scic_sds_stp_remote_device_ready_idle_substate_enter,
1076 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD] = {
1077 .enter_state = scic_sds_stp_remote_device_ready_cmd_substate_enter,
1079 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ] = { },
1080 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR] = {
1081 .enter_state = scic_sds_stp_remote_device_ready_ncq_error_substate_enter,
1083 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET] = { },
1084 [SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE] = {
1085 .enter_state = scic_sds_smp_remote_device_ready_idle_substate_enter,
1087 [SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD] = {
1088 .enter_state = scic_sds_smp_remote_device_ready_cmd_substate_enter,
1089 .exit_state = scic_sds_smp_remote_device_ready_cmd_substate_exit,
1091 [SCI_BASE_REMOTE_DEVICE_STATE_STOPPING] = { },
1092 [SCI_BASE_REMOTE_DEVICE_STATE_FAILED] = { },
1093 [SCI_BASE_REMOTE_DEVICE_STATE_RESETTING] = {
1094 .enter_state = scic_sds_remote_device_resetting_state_enter,
1095 .exit_state = scic_sds_remote_device_resetting_state_exit
1097 [SCI_BASE_REMOTE_DEVICE_STATE_FINAL] = { },
1101 * scic_remote_device_construct() - common construction
1102 * @sci_port: SAS/SATA port through which this device is accessed.
1103 * @sci_dev: remote device to construct
1105 * This routine just performs benign initialization and does not
1106 * allocate the remote_node_context which is left to
1107 * scic_remote_device_[de]a_construct(). scic_remote_device_destruct()
1108 * frees the remote_node_context(s) for the device.
1110 static void scic_remote_device_construct(struct scic_sds_port *sci_port,
1111 struct scic_sds_remote_device *sci_dev)
1113 sci_dev->owning_port = sci_port;
1114 sci_dev->started_request_count = 0;
1116 sci_base_state_machine_construct(
1117 &sci_dev->state_machine,
1119 scic_sds_remote_device_state_table,
1120 SCI_BASE_REMOTE_DEVICE_STATE_INITIAL
1123 sci_base_state_machine_start(
1124 &sci_dev->state_machine
1127 scic_sds_remote_node_context_construct(&sci_dev->rnc,
1128 SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX);
1132 * scic_remote_device_da_construct() - construct direct attached device.
1134 * The information (e.g. IAF, Signature FIS, etc.) necessary to build
1135 * the device is known to the SCI Core since it is contained in the
1136 * scic_phy object. Remote node context(s) is/are a global resource
1137 * allocated by this routine, freed by scic_remote_device_destruct().
1140 * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
1141 * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
1142 * sata-only controller instance.
1143 * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
1145 static enum sci_status scic_remote_device_da_construct(struct scic_sds_port *sci_port,
1146 struct scic_sds_remote_device *sci_dev)
1148 enum sci_status status;
1149 struct domain_device *dev = sci_dev_to_domain(sci_dev);
1151 scic_remote_device_construct(sci_port, sci_dev);
1154 * This information is request to determine how many remote node context
1155 * entries will be needed to store the remote node.
1157 sci_dev->is_direct_attached = true;
1158 status = scic_sds_controller_allocate_remote_node_context(sci_port->owning_controller,
1160 &sci_dev->rnc.remote_node_index);
1162 if (status != SCI_SUCCESS)
1165 if (dev->dev_type == SAS_END_DEV || dev->dev_type == SATA_DEV ||
1166 (dev->tproto & SAS_PROTOCOL_STP) || dev_is_expander(dev))
1169 return SCI_FAILURE_UNSUPPORTED_PROTOCOL;
1171 sci_dev->connection_rate = scic_sds_port_get_max_allowed_speed(sci_port);
1173 /* / @todo Should I assign the port width by reading all of the phys on the port? */
1174 sci_dev->device_port_width = 1;
1180 * scic_remote_device_ea_construct() - construct expander attached device
1182 * Remote node context(s) is/are a global resource allocated by this
1183 * routine, freed by scic_remote_device_destruct().
1186 * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
1187 * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
1188 * sata-only controller instance.
1189 * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
1191 static enum sci_status scic_remote_device_ea_construct(struct scic_sds_port *sci_port,
1192 struct scic_sds_remote_device *sci_dev)
1194 struct domain_device *dev = sci_dev_to_domain(sci_dev);
1195 enum sci_status status;
1197 scic_remote_device_construct(sci_port, sci_dev);
1199 status = scic_sds_controller_allocate_remote_node_context(sci_port->owning_controller,
1201 &sci_dev->rnc.remote_node_index);
1202 if (status != SCI_SUCCESS)
1205 if (dev->dev_type == SAS_END_DEV || dev->dev_type == SATA_DEV ||
1206 (dev->tproto & SAS_PROTOCOL_STP) || dev_is_expander(dev))
1209 return SCI_FAILURE_UNSUPPORTED_PROTOCOL;
1212 * For SAS-2 the physical link rate is actually a logical link
1213 * rate that incorporates multiplexing. The SCU doesn't
1214 * incorporate multiplexing and for the purposes of the
1215 * connection the logical link rate is that same as the
1216 * physical. Furthermore, the SAS-2 and SAS-1.1 fields overlay
1217 * one another, so this code works for both situations. */
1218 sci_dev->connection_rate = min_t(u16, scic_sds_port_get_max_allowed_speed(sci_port),
1221 /* / @todo Should I assign the port width by reading all of the phys on the port? */
1222 sci_dev->device_port_width = 1;
1228 * scic_remote_device_start() - This method will start the supplied remote
1229 * device. This method enables normal IO requests to flow through to the
1231 * @remote_device: This parameter specifies the device to be started.
1232 * @timeout: This parameter specifies the number of milliseconds in which the
1233 * start operation should complete.
1235 * An indication of whether the device was successfully started. SCI_SUCCESS
1236 * This value is returned if the device was successfully started.
1237 * SCI_FAILURE_INVALID_PHY This value is returned if the user attempts to start
1238 * the device when there have been no phys added to it.
1240 static enum sci_status scic_remote_device_start(struct scic_sds_remote_device *sci_dev,
1243 struct sci_base_state_machine *sm = &sci_dev->state_machine;
1244 enum scic_sds_remote_device_states state = sm->current_state_id;
1245 enum sci_status status;
1247 if (state != SCI_BASE_REMOTE_DEVICE_STATE_STOPPED) {
1248 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
1250 return SCI_FAILURE_INVALID_STATE;
1253 status = scic_sds_remote_node_context_resume(&sci_dev->rnc,
1254 remote_device_resume_done,
1256 if (status != SCI_SUCCESS)
1259 sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_STARTING);
1264 static enum sci_status isci_remote_device_construct(struct isci_port *iport,
1265 struct isci_remote_device *idev)
1267 struct scic_sds_port *sci_port = &iport->sci;
1268 struct isci_host *ihost = iport->isci_host;
1269 struct domain_device *dev = idev->domain_dev;
1270 enum sci_status status;
1272 if (dev->parent && dev_is_expander(dev->parent))
1273 status = scic_remote_device_ea_construct(sci_port, &idev->sci);
1275 status = scic_remote_device_da_construct(sci_port, &idev->sci);
1277 if (status != SCI_SUCCESS) {
1278 dev_dbg(&ihost->pdev->dev, "%s: construct failed: %d\n",
1284 /* start the device. */
1285 status = scic_remote_device_start(&idev->sci, ISCI_REMOTE_DEVICE_START_TIMEOUT);
1287 if (status != SCI_SUCCESS)
1288 dev_warn(&ihost->pdev->dev, "remote device start failed: %d\n",
1294 void isci_remote_device_nuke_requests(struct isci_host *ihost, struct isci_remote_device *idev)
1296 DECLARE_COMPLETION_ONSTACK(aborted_task_completion);
1298 dev_dbg(&ihost->pdev->dev,
1299 "%s: idev = %p\n", __func__, idev);
1301 /* Cleanup all requests pending for this device. */
1302 isci_terminate_pending_requests(ihost, idev, terminating);
1304 dev_dbg(&ihost->pdev->dev,
1305 "%s: idev = %p, done\n", __func__, idev);
1309 * This function builds the isci_remote_device when a libsas dev_found message
1311 * @isci_host: This parameter specifies the isci host object.
1312 * @port: This parameter specifies the isci_port conected to this device.
1314 * pointer to new isci_remote_device.
1316 static struct isci_remote_device *
1317 isci_remote_device_alloc(struct isci_host *ihost, struct isci_port *iport)
1319 struct isci_remote_device *idev;
1322 for (i = 0; i < SCI_MAX_REMOTE_DEVICES; i++) {
1323 idev = &ihost->devices[i];
1324 if (!test_and_set_bit(IDEV_ALLOCATED, &idev->flags))
1328 if (i >= SCI_MAX_REMOTE_DEVICES) {
1329 dev_warn(&ihost->pdev->dev, "%s: failed\n", __func__);
1333 if (WARN_ONCE(!list_empty(&idev->reqs_in_process), "found requests in process\n"))
1336 if (WARN_ONCE(!list_empty(&idev->node), "found non-idle remote device\n"))
1339 isci_remote_device_change_state(idev, isci_freed);
1345 * isci_remote_device_stop() - This function is called internally to stop the
1347 * @isci_host: This parameter specifies the isci host object.
1348 * @isci_device: This parameter specifies the remote device.
1350 * The status of the scic request to stop.
1352 enum sci_status isci_remote_device_stop(struct isci_host *ihost, struct isci_remote_device *idev)
1354 enum sci_status status;
1355 unsigned long flags;
1357 dev_dbg(&ihost->pdev->dev,
1358 "%s: isci_device = %p\n", __func__, idev);
1360 isci_remote_device_change_state(idev, isci_stopping);
1362 /* Kill all outstanding requests. */
1363 isci_remote_device_nuke_requests(ihost, idev);
1365 set_bit(IDEV_STOP_PENDING, &idev->flags);
1367 spin_lock_irqsave(&ihost->scic_lock, flags);
1368 status = scic_remote_device_stop(&idev->sci, 50);
1369 spin_unlock_irqrestore(&ihost->scic_lock, flags);
1371 /* Wait for the stop complete callback. */
1372 if (status == SCI_SUCCESS) {
1373 wait_for_device_stop(ihost, idev);
1374 clear_bit(IDEV_ALLOCATED, &idev->flags);
1377 dev_dbg(&ihost->pdev->dev,
1378 "%s: idev = %p - after completion wait\n",
1385 * isci_remote_device_gone() - This function is called by libsas when a domain
1386 * device is removed.
1387 * @domain_device: This parameter specifies the libsas domain device.
1390 void isci_remote_device_gone(struct domain_device *dev)
1392 struct isci_host *ihost = dev_to_ihost(dev);
1393 struct isci_remote_device *idev = dev->lldd_dev;
1395 dev_dbg(&ihost->pdev->dev,
1396 "%s: domain_device = %p, isci_device = %p, isci_port = %p\n",
1397 __func__, dev, idev, idev->isci_port);
1399 isci_remote_device_stop(ihost, idev);
1404 * isci_remote_device_found() - This function is called by libsas when a remote
1405 * device is discovered. A remote device object is created and started. the
1406 * function then sleeps until the sci core device started message is
1408 * @domain_device: This parameter specifies the libsas domain device.
1410 * status, zero indicates success.
1412 int isci_remote_device_found(struct domain_device *domain_dev)
1414 struct isci_host *isci_host = dev_to_ihost(domain_dev);
1415 struct isci_port *isci_port;
1416 struct isci_phy *isci_phy;
1417 struct asd_sas_port *sas_port;
1418 struct asd_sas_phy *sas_phy;
1419 struct isci_remote_device *isci_device;
1420 enum sci_status status;
1422 dev_dbg(&isci_host->pdev->dev,
1423 "%s: domain_device = %p\n", __func__, domain_dev);
1425 wait_for_start(isci_host);
1427 sas_port = domain_dev->port;
1428 sas_phy = list_first_entry(&sas_port->phy_list, struct asd_sas_phy,
1430 isci_phy = to_isci_phy(sas_phy);
1431 isci_port = isci_phy->isci_port;
1433 /* we are being called for a device on this port,
1434 * so it has to come up eventually
1436 wait_for_completion(&isci_port->start_complete);
1438 if ((isci_stopping == isci_port_get_state(isci_port)) ||
1439 (isci_stopped == isci_port_get_state(isci_port)))
1442 isci_device = isci_remote_device_alloc(isci_host, isci_port);
1446 INIT_LIST_HEAD(&isci_device->node);
1447 domain_dev->lldd_dev = isci_device;
1448 isci_device->domain_dev = domain_dev;
1449 isci_device->isci_port = isci_port;
1450 isci_remote_device_change_state(isci_device, isci_starting);
1453 spin_lock_irq(&isci_host->scic_lock);
1454 list_add_tail(&isci_device->node, &isci_port->remote_dev_list);
1456 set_bit(IDEV_START_PENDING, &isci_device->flags);
1457 status = isci_remote_device_construct(isci_port, isci_device);
1458 spin_unlock_irq(&isci_host->scic_lock);
1460 dev_dbg(&isci_host->pdev->dev,
1461 "%s: isci_device = %p\n",
1462 __func__, isci_device);
1464 if (status != SCI_SUCCESS) {
1466 spin_lock_irq(&isci_host->scic_lock);
1467 isci_remote_device_deconstruct(
1471 spin_unlock_irq(&isci_host->scic_lock);
1475 /* wait for the device ready callback. */
1476 wait_for_device_start(isci_host, isci_device);
1481 * isci_device_is_reset_pending() - This function will check if there is any
1482 * pending reset condition on the device.
1483 * @request: This parameter is the isci_device object.
1485 * true if there is a reset pending for the device.
1487 bool isci_device_is_reset_pending(
1488 struct isci_host *isci_host,
1489 struct isci_remote_device *isci_device)
1491 struct isci_request *isci_request;
1492 struct isci_request *tmp_req;
1493 bool reset_is_pending = false;
1494 unsigned long flags;
1496 dev_dbg(&isci_host->pdev->dev,
1497 "%s: isci_device = %p\n", __func__, isci_device);
1499 spin_lock_irqsave(&isci_host->scic_lock, flags);
1501 /* Check for reset on all pending requests. */
1502 list_for_each_entry_safe(isci_request, tmp_req,
1503 &isci_device->reqs_in_process, dev_node) {
1504 dev_dbg(&isci_host->pdev->dev,
1505 "%s: isci_device = %p request = %p\n",
1506 __func__, isci_device, isci_request);
1508 if (isci_request->ttype == io_task) {
1509 struct sas_task *task = isci_request_access_task(
1512 spin_lock(&task->task_state_lock);
1513 if (task->task_state_flags & SAS_TASK_NEED_DEV_RESET)
1514 reset_is_pending = true;
1515 spin_unlock(&task->task_state_lock);
1519 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1521 dev_dbg(&isci_host->pdev->dev,
1522 "%s: isci_device = %p reset_is_pending = %d\n",
1523 __func__, isci_device, reset_is_pending);
1525 return reset_is_pending;
1529 * isci_device_clear_reset_pending() - This function will clear if any pending
1530 * reset condition flags on the device.
1531 * @request: This parameter is the isci_device object.
1533 * true if there is a reset pending for the device.
1535 void isci_device_clear_reset_pending(struct isci_host *ihost, struct isci_remote_device *idev)
1537 struct isci_request *isci_request;
1538 struct isci_request *tmp_req;
1539 unsigned long flags = 0;
1541 dev_dbg(&ihost->pdev->dev, "%s: idev=%p, ihost=%p\n",
1542 __func__, idev, ihost);
1544 spin_lock_irqsave(&ihost->scic_lock, flags);
1546 /* Clear reset pending on all pending requests. */
1547 list_for_each_entry_safe(isci_request, tmp_req,
1548 &idev->reqs_in_process, dev_node) {
1549 dev_dbg(&ihost->pdev->dev, "%s: idev = %p request = %p\n",
1550 __func__, idev, isci_request);
1552 if (isci_request->ttype == io_task) {
1554 unsigned long flags2;
1555 struct sas_task *task = isci_request_access_task(
1558 spin_lock_irqsave(&task->task_state_lock, flags2);
1559 task->task_state_flags &= ~SAS_TASK_NEED_DEV_RESET;
1560 spin_unlock_irqrestore(&task->task_state_lock, flags2);
1563 spin_unlock_irqrestore(&ihost->scic_lock, flags);