isci: fix isci_terminate_pending() list management
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / scsi / isci / remote_device.c
blob45592ad33c3b5dd459b3769dbd634cbe539d3281
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 <scsi/sas.h>
56 #include "isci.h"
57 #include "port.h"
58 #include "remote_device.h"
59 #include "request.h"
60 #include "remote_node_context.h"
61 #include "scu_event_codes.h"
62 #include "task.h"
64 /**
65 * isci_remote_device_change_state() - This function gets the status of the
66 * remote_device object.
67 * @isci_device: This parameter points to the isci_remote_device object
69 * status of the object as a isci_status enum.
71 void isci_remote_device_change_state(
72 struct isci_remote_device *isci_device,
73 enum isci_status status)
75 unsigned long flags;
77 spin_lock_irqsave(&isci_device->state_lock, flags);
78 isci_device->status = status;
79 spin_unlock_irqrestore(&isci_device->state_lock, flags);
82 /**
83 * isci_remote_device_not_ready() - This function is called by the scic when
84 * the remote device is not ready. We mark the isci device as ready (not
85 * "ready_for_io") and signal the waiting proccess.
86 * @isci_host: This parameter specifies the isci host object.
87 * @isci_device: This parameter specifies the remote device
90 static void isci_remote_device_not_ready(struct isci_host *ihost,
91 struct isci_remote_device *idev, u32 reason)
93 dev_dbg(&ihost->pdev->dev,
94 "%s: isci_device = %p\n", __func__, idev);
96 if (reason == SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED)
97 isci_remote_device_change_state(idev, isci_stopping);
98 else
99 /* device ready is actually a "not ready for io" state. */
100 isci_remote_device_change_state(idev, isci_ready);
104 * isci_remote_device_ready() - This function is called by the scic when the
105 * remote device is ready. We mark the isci device as ready and signal the
106 * waiting proccess.
107 * @ihost: our valid isci_host
108 * @idev: remote device
111 static void isci_remote_device_ready(struct isci_host *ihost, struct isci_remote_device *idev)
113 dev_dbg(&ihost->pdev->dev,
114 "%s: idev = %p\n", __func__, idev);
116 isci_remote_device_change_state(idev, isci_ready_for_io);
117 if (test_and_clear_bit(IDEV_START_PENDING, &idev->flags))
118 wake_up(&ihost->eventq);
121 /* called once the remote node context is ready to be freed.
122 * The remote device can now report that its stop operation is complete. none
124 static void rnc_destruct_done(void *_dev)
126 struct scic_sds_remote_device *sci_dev = _dev;
128 BUG_ON(sci_dev->started_request_count != 0);
129 sci_change_state(&sci_dev->sm, SCI_DEV_STOPPED);
132 static enum sci_status scic_sds_remote_device_terminate_requests(struct scic_sds_remote_device *sci_dev)
134 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
135 u32 i, request_count = sci_dev->started_request_count;
136 enum sci_status status = SCI_SUCCESS;
138 for (i = 0; i < SCI_MAX_IO_REQUESTS && i < request_count; i++) {
139 struct scic_sds_request *sci_req;
140 enum sci_status s;
142 sci_req = scic->io_request_table[i];
143 if (!sci_req || sci_req->target_device != sci_dev)
144 continue;
145 s = scic_controller_terminate_request(scic, sci_dev, sci_req);
146 if (s != SCI_SUCCESS)
147 status = s;
150 return status;
153 enum sci_status scic_remote_device_stop(struct scic_sds_remote_device *sci_dev,
154 u32 timeout)
156 struct sci_base_state_machine *sm = &sci_dev->sm;
157 enum scic_sds_remote_device_states state = sm->current_state_id;
159 switch (state) {
160 case SCI_DEV_INITIAL:
161 case SCI_DEV_FAILED:
162 case SCI_DEV_FINAL:
163 default:
164 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
165 __func__, state);
166 return SCI_FAILURE_INVALID_STATE;
167 case SCI_DEV_STOPPED:
168 return SCI_SUCCESS;
169 case SCI_DEV_STARTING:
170 /* device not started so there had better be no requests */
171 BUG_ON(sci_dev->started_request_count != 0);
172 scic_sds_remote_node_context_destruct(&sci_dev->rnc,
173 rnc_destruct_done, sci_dev);
174 /* Transition to the stopping state and wait for the
175 * remote node to complete being posted and invalidated.
177 sci_change_state(sm, SCI_DEV_STOPPING);
178 return SCI_SUCCESS;
179 case SCI_DEV_READY:
180 case SCI_STP_DEV_IDLE:
181 case SCI_STP_DEV_CMD:
182 case SCI_STP_DEV_NCQ:
183 case SCI_STP_DEV_NCQ_ERROR:
184 case SCI_STP_DEV_AWAIT_RESET:
185 case SCI_SMP_DEV_IDLE:
186 case SCI_SMP_DEV_CMD:
187 sci_change_state(sm, SCI_DEV_STOPPING);
188 if (sci_dev->started_request_count == 0) {
189 scic_sds_remote_node_context_destruct(&sci_dev->rnc,
190 rnc_destruct_done, sci_dev);
191 return SCI_SUCCESS;
192 } else
193 return scic_sds_remote_device_terminate_requests(sci_dev);
194 break;
195 case SCI_DEV_STOPPING:
196 /* All requests should have been terminated, but if there is an
197 * attempt to stop a device already in the stopping state, then
198 * try again to terminate.
200 return scic_sds_remote_device_terminate_requests(sci_dev);
201 case SCI_DEV_RESETTING:
202 sci_change_state(sm, SCI_DEV_STOPPING);
203 return SCI_SUCCESS;
207 enum sci_status scic_remote_device_reset(struct scic_sds_remote_device *sci_dev)
209 struct sci_base_state_machine *sm = &sci_dev->sm;
210 enum scic_sds_remote_device_states state = sm->current_state_id;
212 switch (state) {
213 case SCI_DEV_INITIAL:
214 case SCI_DEV_STOPPED:
215 case SCI_DEV_STARTING:
216 case SCI_SMP_DEV_IDLE:
217 case SCI_SMP_DEV_CMD:
218 case SCI_DEV_STOPPING:
219 case SCI_DEV_FAILED:
220 case SCI_DEV_RESETTING:
221 case SCI_DEV_FINAL:
222 default:
223 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
224 __func__, state);
225 return SCI_FAILURE_INVALID_STATE;
226 case SCI_DEV_READY:
227 case SCI_STP_DEV_IDLE:
228 case SCI_STP_DEV_CMD:
229 case SCI_STP_DEV_NCQ:
230 case SCI_STP_DEV_NCQ_ERROR:
231 case SCI_STP_DEV_AWAIT_RESET:
232 sci_change_state(sm, SCI_DEV_RESETTING);
233 return SCI_SUCCESS;
237 enum sci_status scic_remote_device_reset_complete(struct scic_sds_remote_device *sci_dev)
239 struct sci_base_state_machine *sm = &sci_dev->sm;
240 enum scic_sds_remote_device_states state = sm->current_state_id;
242 if (state != SCI_DEV_RESETTING) {
243 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
244 __func__, state);
245 return SCI_FAILURE_INVALID_STATE;
248 sci_change_state(sm, SCI_DEV_READY);
249 return SCI_SUCCESS;
252 enum sci_status scic_sds_remote_device_suspend(struct scic_sds_remote_device *sci_dev,
253 u32 suspend_type)
255 struct sci_base_state_machine *sm = &sci_dev->sm;
256 enum scic_sds_remote_device_states state = sm->current_state_id;
258 if (state != SCI_STP_DEV_CMD) {
259 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
260 __func__, state);
261 return SCI_FAILURE_INVALID_STATE;
264 return scic_sds_remote_node_context_suspend(&sci_dev->rnc,
265 suspend_type, NULL, NULL);
268 enum sci_status scic_sds_remote_device_frame_handler(struct scic_sds_remote_device *sci_dev,
269 u32 frame_index)
271 struct sci_base_state_machine *sm = &sci_dev->sm;
272 enum scic_sds_remote_device_states state = sm->current_state_id;
273 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
274 enum sci_status status;
276 switch (state) {
277 case SCI_DEV_INITIAL:
278 case SCI_DEV_STOPPED:
279 case SCI_DEV_STARTING:
280 case SCI_STP_DEV_IDLE:
281 case SCI_SMP_DEV_IDLE:
282 case SCI_DEV_FINAL:
283 default:
284 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
285 __func__, state);
286 /* Return the frame back to the controller */
287 scic_sds_controller_release_frame(scic, frame_index);
288 return SCI_FAILURE_INVALID_STATE;
289 case SCI_DEV_READY:
290 case SCI_STP_DEV_NCQ_ERROR:
291 case SCI_STP_DEV_AWAIT_RESET:
292 case SCI_DEV_STOPPING:
293 case SCI_DEV_FAILED:
294 case SCI_DEV_RESETTING: {
295 struct scic_sds_request *sci_req;
296 struct ssp_frame_hdr hdr;
297 void *frame_header;
298 ssize_t word_cnt;
300 status = scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
301 frame_index,
302 &frame_header);
303 if (status != SCI_SUCCESS)
304 return status;
306 word_cnt = sizeof(hdr) / sizeof(u32);
307 sci_swab32_cpy(&hdr, frame_header, word_cnt);
309 sci_req = scic_request_by_tag(scic, be16_to_cpu(hdr.tag));
310 if (sci_req && sci_req->target_device == sci_dev) {
311 /* The IO request is now in charge of releasing the frame */
312 status = scic_sds_io_request_frame_handler(sci_req, frame_index);
313 } else {
314 /* We could not map this tag to a valid IO
315 * request Just toss the frame and continue
317 scic_sds_controller_release_frame(scic, frame_index);
319 break;
321 case SCI_STP_DEV_NCQ: {
322 struct dev_to_host_fis *hdr;
324 status = scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
325 frame_index,
326 (void **)&hdr);
327 if (status != SCI_SUCCESS)
328 return status;
330 if (hdr->fis_type == FIS_SETDEVBITS &&
331 (hdr->status & ATA_ERR)) {
332 sci_dev->not_ready_reason = SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED;
334 /* TODO Check sactive and complete associated IO if any. */
335 sci_change_state(sm, SCI_STP_DEV_NCQ_ERROR);
336 } else if (hdr->fis_type == FIS_REGD2H &&
337 (hdr->status & ATA_ERR)) {
339 * Some devices return D2H FIS when an NCQ error is detected.
340 * Treat this like an SDB error FIS ready reason.
342 sci_dev->not_ready_reason = SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED;
343 sci_change_state(&sci_dev->sm, SCI_STP_DEV_NCQ_ERROR);
344 } else
345 status = SCI_FAILURE;
347 scic_sds_controller_release_frame(scic, frame_index);
348 break;
350 case SCI_STP_DEV_CMD:
351 case SCI_SMP_DEV_CMD:
352 /* The device does not process any UF received from the hardware while
353 * in this state. All unsolicited frames are forwarded to the io request
354 * object.
356 status = scic_sds_io_request_frame_handler(sci_dev->working_request, frame_index);
357 break;
360 return status;
363 static bool is_remote_device_ready(struct scic_sds_remote_device *sci_dev)
366 struct sci_base_state_machine *sm = &sci_dev->sm;
367 enum scic_sds_remote_device_states state = sm->current_state_id;
369 switch (state) {
370 case SCI_DEV_READY:
371 case SCI_STP_DEV_IDLE:
372 case SCI_STP_DEV_CMD:
373 case SCI_STP_DEV_NCQ:
374 case SCI_STP_DEV_NCQ_ERROR:
375 case SCI_STP_DEV_AWAIT_RESET:
376 case SCI_SMP_DEV_IDLE:
377 case SCI_SMP_DEV_CMD:
378 return true;
379 default:
380 return false;
384 enum sci_status scic_sds_remote_device_event_handler(struct scic_sds_remote_device *sci_dev,
385 u32 event_code)
387 struct sci_base_state_machine *sm = &sci_dev->sm;
388 enum scic_sds_remote_device_states state = sm->current_state_id;
389 enum sci_status status;
391 switch (scu_get_event_type(event_code)) {
392 case SCU_EVENT_TYPE_RNC_OPS_MISC:
393 case SCU_EVENT_TYPE_RNC_SUSPEND_TX:
394 case SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX:
395 status = scic_sds_remote_node_context_event_handler(&sci_dev->rnc, event_code);
396 break;
397 case SCU_EVENT_TYPE_PTX_SCHEDULE_EVENT:
398 if (scu_get_event_code(event_code) == SCU_EVENT_IT_NEXUS_TIMEOUT) {
399 status = SCI_SUCCESS;
401 /* Suspend the associated RNC */
402 scic_sds_remote_node_context_suspend(&sci_dev->rnc,
403 SCI_SOFTWARE_SUSPENSION,
404 NULL, NULL);
406 dev_dbg(scirdev_to_dev(sci_dev),
407 "%s: device: %p event code: %x: %s\n",
408 __func__, sci_dev, event_code,
409 is_remote_device_ready(sci_dev)
410 ? "I_T_Nexus_Timeout event"
411 : "I_T_Nexus_Timeout event in wrong state");
413 break;
415 /* Else, fall through and treat as unhandled... */
416 default:
417 dev_dbg(scirdev_to_dev(sci_dev),
418 "%s: device: %p event code: %x: %s\n",
419 __func__, sci_dev, event_code,
420 is_remote_device_ready(sci_dev)
421 ? "unexpected event"
422 : "unexpected event in wrong state");
423 status = SCI_FAILURE_INVALID_STATE;
424 break;
427 if (status != SCI_SUCCESS)
428 return status;
430 if (state == SCI_STP_DEV_IDLE) {
432 /* We pick up suspension events to handle specifically to this
433 * state. We resume the RNC right away.
435 if (scu_get_event_type(event_code) == SCU_EVENT_TYPE_RNC_SUSPEND_TX ||
436 scu_get_event_type(event_code) == SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX)
437 status = scic_sds_remote_node_context_resume(&sci_dev->rnc, NULL, NULL);
440 return status;
443 static void scic_sds_remote_device_start_request(struct scic_sds_remote_device *sci_dev,
444 struct scic_sds_request *sci_req,
445 enum sci_status status)
447 struct scic_sds_port *sci_port = sci_dev->owning_port;
449 /* cleanup requests that failed after starting on the port */
450 if (status != SCI_SUCCESS)
451 scic_sds_port_complete_io(sci_port, sci_dev, sci_req);
452 else
453 scic_sds_remote_device_increment_request_count(sci_dev);
456 enum sci_status scic_sds_remote_device_start_io(struct scic_sds_controller *scic,
457 struct scic_sds_remote_device *sci_dev,
458 struct scic_sds_request *sci_req)
460 struct sci_base_state_machine *sm = &sci_dev->sm;
461 enum scic_sds_remote_device_states state = sm->current_state_id;
462 struct scic_sds_port *sci_port = sci_dev->owning_port;
463 struct isci_request *ireq = sci_req_to_ireq(sci_req);
464 enum sci_status status;
466 switch (state) {
467 case SCI_DEV_INITIAL:
468 case SCI_DEV_STOPPED:
469 case SCI_DEV_STARTING:
470 case SCI_STP_DEV_NCQ_ERROR:
471 case SCI_DEV_STOPPING:
472 case SCI_DEV_FAILED:
473 case SCI_DEV_RESETTING:
474 case SCI_DEV_FINAL:
475 default:
476 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
477 __func__, state);
478 return SCI_FAILURE_INVALID_STATE;
479 case SCI_DEV_READY:
480 /* attempt to start an io request for this device object. The remote
481 * device object will issue the start request for the io and if
482 * successful it will start the request for the port object then
483 * increment its own request count.
485 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
486 if (status != SCI_SUCCESS)
487 return status;
489 status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, sci_req);
490 if (status != SCI_SUCCESS)
491 break;
493 status = scic_sds_request_start(sci_req);
494 break;
495 case SCI_STP_DEV_IDLE: {
496 /* handle the start io operation for a sata device that is in
497 * the command idle state. - Evalute the type of IO request to
498 * be started - If its an NCQ request change to NCQ substate -
499 * If its any other command change to the CMD substate
501 * If this is a softreset we may want to have a different
502 * substate.
504 enum scic_sds_remote_device_states new_state;
505 struct sas_task *task = isci_request_access_task(ireq);
507 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
508 if (status != SCI_SUCCESS)
509 return status;
511 status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, sci_req);
512 if (status != SCI_SUCCESS)
513 break;
515 status = scic_sds_request_start(sci_req);
516 if (status != SCI_SUCCESS)
517 break;
519 if (task->ata_task.use_ncq)
520 new_state = SCI_STP_DEV_NCQ;
521 else {
522 sci_dev->working_request = sci_req;
523 new_state = SCI_STP_DEV_CMD;
525 sci_change_state(sm, new_state);
526 break;
528 case SCI_STP_DEV_NCQ: {
529 struct sas_task *task = isci_request_access_task(ireq);
531 if (task->ata_task.use_ncq) {
532 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
533 if (status != SCI_SUCCESS)
534 return status;
536 status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, sci_req);
537 if (status != SCI_SUCCESS)
538 break;
540 status = scic_sds_request_start(sci_req);
541 } else
542 return SCI_FAILURE_INVALID_STATE;
543 break;
545 case SCI_STP_DEV_AWAIT_RESET:
546 return SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED;
547 case SCI_SMP_DEV_IDLE:
548 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
549 if (status != SCI_SUCCESS)
550 return status;
552 status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, sci_req);
553 if (status != SCI_SUCCESS)
554 break;
556 status = scic_sds_request_start(sci_req);
557 if (status != SCI_SUCCESS)
558 break;
560 sci_dev->working_request = sci_req;
561 sci_change_state(&sci_dev->sm, SCI_SMP_DEV_CMD);
562 break;
563 case SCI_STP_DEV_CMD:
564 case SCI_SMP_DEV_CMD:
565 /* device is already handling a command it can not accept new commands
566 * until this one is complete.
568 return SCI_FAILURE_INVALID_STATE;
571 scic_sds_remote_device_start_request(sci_dev, sci_req, status);
572 return status;
575 static enum sci_status common_complete_io(struct scic_sds_port *sci_port,
576 struct scic_sds_remote_device *sci_dev,
577 struct scic_sds_request *sci_req)
579 enum sci_status status;
581 status = scic_sds_request_complete(sci_req);
582 if (status != SCI_SUCCESS)
583 return status;
585 status = scic_sds_port_complete_io(sci_port, sci_dev, sci_req);
586 if (status != SCI_SUCCESS)
587 return status;
589 scic_sds_remote_device_decrement_request_count(sci_dev);
590 return status;
593 enum sci_status scic_sds_remote_device_complete_io(struct scic_sds_controller *scic,
594 struct scic_sds_remote_device *sci_dev,
595 struct scic_sds_request *sci_req)
597 struct sci_base_state_machine *sm = &sci_dev->sm;
598 enum scic_sds_remote_device_states state = sm->current_state_id;
599 struct scic_sds_port *sci_port = sci_dev->owning_port;
600 enum sci_status status;
602 switch (state) {
603 case SCI_DEV_INITIAL:
604 case SCI_DEV_STOPPED:
605 case SCI_DEV_STARTING:
606 case SCI_STP_DEV_IDLE:
607 case SCI_SMP_DEV_IDLE:
608 case SCI_DEV_FAILED:
609 case SCI_DEV_FINAL:
610 default:
611 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
612 __func__, state);
613 return SCI_FAILURE_INVALID_STATE;
614 case SCI_DEV_READY:
615 case SCI_STP_DEV_AWAIT_RESET:
616 case SCI_DEV_RESETTING:
617 status = common_complete_io(sci_port, sci_dev, sci_req);
618 break;
619 case SCI_STP_DEV_CMD:
620 case SCI_STP_DEV_NCQ:
621 case SCI_STP_DEV_NCQ_ERROR:
622 status = common_complete_io(sci_port, sci_dev, sci_req);
623 if (status != SCI_SUCCESS)
624 break;
626 if (sci_req->sci_status == SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED) {
627 /* This request causes hardware error, device needs to be Lun Reset.
628 * So here we force the state machine to IDLE state so the rest IOs
629 * can reach RNC state handler, these IOs will be completed by RNC with
630 * status of "DEVICE_RESET_REQUIRED", instead of "INVALID STATE".
632 sci_change_state(sm, SCI_STP_DEV_AWAIT_RESET);
633 } else if (scic_sds_remote_device_get_request_count(sci_dev) == 0)
634 sci_change_state(sm, SCI_STP_DEV_IDLE);
635 break;
636 case SCI_SMP_DEV_CMD:
637 status = common_complete_io(sci_port, sci_dev, sci_req);
638 if (status != SCI_SUCCESS)
639 break;
640 sci_change_state(sm, SCI_SMP_DEV_IDLE);
641 break;
642 case SCI_DEV_STOPPING:
643 status = common_complete_io(sci_port, sci_dev, sci_req);
644 if (status != SCI_SUCCESS)
645 break;
647 if (scic_sds_remote_device_get_request_count(sci_dev) == 0)
648 scic_sds_remote_node_context_destruct(&sci_dev->rnc,
649 rnc_destruct_done,
650 sci_dev);
651 break;
654 if (status != SCI_SUCCESS)
655 dev_err(scirdev_to_dev(sci_dev),
656 "%s: Port:0x%p Device:0x%p Request:0x%p Status:0x%x "
657 "could not complete\n", __func__, sci_port,
658 sci_dev, sci_req, status);
660 return status;
663 static void scic_sds_remote_device_continue_request(void *dev)
665 struct scic_sds_remote_device *sci_dev = dev;
667 /* we need to check if this request is still valid to continue. */
668 if (sci_dev->working_request)
669 scic_controller_continue_io(sci_dev->working_request);
672 enum sci_status scic_sds_remote_device_start_task(struct scic_sds_controller *scic,
673 struct scic_sds_remote_device *sci_dev,
674 struct scic_sds_request *sci_req)
676 struct sci_base_state_machine *sm = &sci_dev->sm;
677 enum scic_sds_remote_device_states state = sm->current_state_id;
678 struct scic_sds_port *sci_port = sci_dev->owning_port;
679 enum sci_status status;
681 switch (state) {
682 case SCI_DEV_INITIAL:
683 case SCI_DEV_STOPPED:
684 case SCI_DEV_STARTING:
685 case SCI_SMP_DEV_IDLE:
686 case SCI_SMP_DEV_CMD:
687 case SCI_DEV_STOPPING:
688 case SCI_DEV_FAILED:
689 case SCI_DEV_RESETTING:
690 case SCI_DEV_FINAL:
691 default:
692 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
693 __func__, state);
694 return SCI_FAILURE_INVALID_STATE;
695 case SCI_STP_DEV_IDLE:
696 case SCI_STP_DEV_CMD:
697 case SCI_STP_DEV_NCQ:
698 case SCI_STP_DEV_NCQ_ERROR:
699 case SCI_STP_DEV_AWAIT_RESET:
700 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
701 if (status != SCI_SUCCESS)
702 return status;
704 status = scic_sds_remote_node_context_start_task(&sci_dev->rnc, sci_req);
705 if (status != SCI_SUCCESS)
706 goto out;
708 status = scic_sds_request_start(sci_req);
709 if (status != SCI_SUCCESS)
710 goto out;
712 /* Note: If the remote device state is not IDLE this will
713 * replace the request that probably resulted in the task
714 * management request.
716 sci_dev->working_request = sci_req;
717 sci_change_state(sm, SCI_STP_DEV_CMD);
719 /* The remote node context must cleanup the TCi to NCQ mapping
720 * table. The only way to do this correctly is to either write
721 * to the TLCR register or to invalidate and repost the RNC. In
722 * either case the remote node context state machine will take
723 * the correct action when the remote node context is suspended
724 * and later resumed.
726 scic_sds_remote_node_context_suspend(&sci_dev->rnc,
727 SCI_SOFTWARE_SUSPENSION, NULL, NULL);
728 scic_sds_remote_node_context_resume(&sci_dev->rnc,
729 scic_sds_remote_device_continue_request,
730 sci_dev);
732 out:
733 scic_sds_remote_device_start_request(sci_dev, sci_req, status);
734 /* We need to let the controller start request handler know that
735 * it can't post TC yet. We will provide a callback function to
736 * post TC when RNC gets resumed.
738 return SCI_FAILURE_RESET_DEVICE_PARTIAL_SUCCESS;
739 case SCI_DEV_READY:
740 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
741 if (status != SCI_SUCCESS)
742 return status;
744 status = scic_sds_remote_node_context_start_task(&sci_dev->rnc, sci_req);
745 if (status != SCI_SUCCESS)
746 break;
748 status = scic_sds_request_start(sci_req);
749 break;
751 scic_sds_remote_device_start_request(sci_dev, sci_req, status);
753 return status;
758 * @sci_dev:
759 * @request:
761 * This method takes the request and bulids an appropriate SCU context for the
762 * request and then requests the controller to post the request. none
764 void scic_sds_remote_device_post_request(
765 struct scic_sds_remote_device *sci_dev,
766 u32 request)
768 u32 context;
770 context = scic_sds_remote_device_build_command_context(sci_dev, request);
772 scic_sds_controller_post_request(
773 scic_sds_remote_device_get_controller(sci_dev),
774 context
778 /* called once the remote node context has transisitioned to a
779 * ready state. This is the indication that the remote device object can also
780 * transition to ready.
782 static void remote_device_resume_done(void *_dev)
784 struct scic_sds_remote_device *sci_dev = _dev;
786 if (is_remote_device_ready(sci_dev))
787 return;
789 /* go 'ready' if we are not already in a ready state */
790 sci_change_state(&sci_dev->sm, SCI_DEV_READY);
793 static void scic_sds_stp_remote_device_ready_idle_substate_resume_complete_handler(void *_dev)
795 struct scic_sds_remote_device *sci_dev = _dev;
796 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
797 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
799 /* For NCQ operation we do not issue a isci_remote_device_not_ready().
800 * As a result, avoid sending the ready notification.
802 if (sci_dev->sm.previous_state_id != SCI_STP_DEV_NCQ)
803 isci_remote_device_ready(scic_to_ihost(scic), idev);
806 static void scic_sds_remote_device_initial_state_enter(struct sci_base_state_machine *sm)
808 struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), sm);
810 /* Initial state is a transitional state to the stopped state */
811 sci_change_state(&sci_dev->sm, SCI_DEV_STOPPED);
815 * scic_remote_device_destruct() - free remote node context and destruct
816 * @remote_device: This parameter specifies the remote device to be destructed.
818 * Remote device objects are a limited resource. As such, they must be
819 * protected. Thus calls to construct and destruct are mutually exclusive and
820 * non-reentrant. The return value shall indicate if the device was
821 * successfully destructed or if some failure occurred. enum sci_status This value
822 * is returned if the device is successfully destructed.
823 * SCI_FAILURE_INVALID_REMOTE_DEVICE This value is returned if the supplied
824 * device isn't valid (e.g. it's already been destoryed, the handle isn't
825 * valid, etc.).
827 static enum sci_status scic_remote_device_destruct(struct scic_sds_remote_device *sci_dev)
829 struct sci_base_state_machine *sm = &sci_dev->sm;
830 enum scic_sds_remote_device_states state = sm->current_state_id;
831 struct scic_sds_controller *scic;
833 if (state != SCI_DEV_STOPPED) {
834 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
835 __func__, state);
836 return SCI_FAILURE_INVALID_STATE;
839 scic = sci_dev->owning_port->owning_controller;
840 scic_sds_controller_free_remote_node_context(scic, sci_dev,
841 sci_dev->rnc.remote_node_index);
842 sci_dev->rnc.remote_node_index = SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX;
843 sci_change_state(sm, SCI_DEV_FINAL);
845 return SCI_SUCCESS;
849 * isci_remote_device_deconstruct() - This function frees an isci_remote_device.
850 * @ihost: This parameter specifies the isci host object.
851 * @idev: This parameter specifies the remote device to be freed.
854 static void isci_remote_device_deconstruct(struct isci_host *ihost, struct isci_remote_device *idev)
856 dev_dbg(&ihost->pdev->dev,
857 "%s: isci_device = %p\n", __func__, idev);
859 /* There should not be any outstanding io's. All paths to
860 * here should go through isci_remote_device_nuke_requests.
861 * If we hit this condition, we will need a way to complete
862 * io requests in process */
863 while (!list_empty(&idev->reqs_in_process)) {
865 dev_err(&ihost->pdev->dev,
866 "%s: ** request list not empty! **\n", __func__);
867 BUG();
870 scic_remote_device_destruct(&idev->sci);
871 idev->domain_dev->lldd_dev = NULL;
872 idev->domain_dev = NULL;
873 idev->isci_port = NULL;
874 list_del_init(&idev->node);
876 clear_bit(IDEV_START_PENDING, &idev->flags);
877 clear_bit(IDEV_STOP_PENDING, &idev->flags);
878 clear_bit(IDEV_EH, &idev->flags);
879 wake_up(&ihost->eventq);
883 * isci_remote_device_stop_complete() - This function is called by the scic
884 * when the remote device stop has completed. We mark the isci device as not
885 * ready and remove the isci remote device.
886 * @ihost: This parameter specifies the isci host object.
887 * @idev: This parameter specifies the remote device.
888 * @status: This parameter specifies status of the completion.
891 static void isci_remote_device_stop_complete(struct isci_host *ihost,
892 struct isci_remote_device *idev)
894 dev_dbg(&ihost->pdev->dev, "%s: complete idev = %p\n", __func__, idev);
896 isci_remote_device_change_state(idev, isci_stopped);
898 /* after stop, we can tear down resources. */
899 isci_remote_device_deconstruct(ihost, idev);
902 static void scic_sds_remote_device_stopped_state_enter(struct sci_base_state_machine *sm)
904 struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), sm);
905 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
906 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
907 u32 prev_state;
909 /* If we are entering from the stopping state let the SCI User know that
910 * the stop operation has completed.
912 prev_state = sci_dev->sm.previous_state_id;
913 if (prev_state == SCI_DEV_STOPPING)
914 isci_remote_device_stop_complete(scic_to_ihost(scic), idev);
916 scic_sds_controller_remote_device_stopped(scic, sci_dev);
919 static void scic_sds_remote_device_starting_state_enter(struct sci_base_state_machine *sm)
921 struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), sm);
922 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
923 struct isci_host *ihost = scic_to_ihost(scic);
924 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
926 isci_remote_device_not_ready(ihost, idev,
927 SCIC_REMOTE_DEVICE_NOT_READY_START_REQUESTED);
930 static void scic_sds_remote_device_ready_state_enter(struct sci_base_state_machine *sm)
932 struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), sm);
933 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
934 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
935 struct domain_device *dev = idev->domain_dev;
937 scic->remote_device_sequence[sci_dev->rnc.remote_node_index]++;
939 if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_SATA)) {
940 sci_change_state(&sci_dev->sm, SCI_STP_DEV_IDLE);
941 } else if (dev_is_expander(dev)) {
942 sci_change_state(&sci_dev->sm, SCI_SMP_DEV_IDLE);
943 } else
944 isci_remote_device_ready(scic_to_ihost(scic), idev);
947 static void scic_sds_remote_device_ready_state_exit(struct sci_base_state_machine *sm)
949 struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), sm);
950 struct domain_device *dev = sci_dev_to_domain(sci_dev);
952 if (dev->dev_type == SAS_END_DEV) {
953 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
954 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
956 isci_remote_device_not_ready(scic_to_ihost(scic), idev,
957 SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED);
961 static void scic_sds_remote_device_resetting_state_enter(struct sci_base_state_machine *sm)
963 struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), sm);
965 scic_sds_remote_node_context_suspend(
966 &sci_dev->rnc, SCI_SOFTWARE_SUSPENSION, NULL, NULL);
969 static void scic_sds_remote_device_resetting_state_exit(struct sci_base_state_machine *sm)
971 struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), sm);
973 scic_sds_remote_node_context_resume(&sci_dev->rnc, NULL, NULL);
976 static void scic_sds_stp_remote_device_ready_idle_substate_enter(struct sci_base_state_machine *sm)
978 struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), sm);
980 sci_dev->working_request = NULL;
981 if (scic_sds_remote_node_context_is_ready(&sci_dev->rnc)) {
983 * Since the RNC is ready, it's alright to finish completion
984 * processing (e.g. signal the remote device is ready). */
985 scic_sds_stp_remote_device_ready_idle_substate_resume_complete_handler(sci_dev);
986 } else {
987 scic_sds_remote_node_context_resume(&sci_dev->rnc,
988 scic_sds_stp_remote_device_ready_idle_substate_resume_complete_handler,
989 sci_dev);
993 static void scic_sds_stp_remote_device_ready_cmd_substate_enter(struct sci_base_state_machine *sm)
995 struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), sm);
996 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
998 BUG_ON(sci_dev->working_request == NULL);
1000 isci_remote_device_not_ready(scic_to_ihost(scic), sci_dev_to_idev(sci_dev),
1001 SCIC_REMOTE_DEVICE_NOT_READY_SATA_REQUEST_STARTED);
1004 static void scic_sds_stp_remote_device_ready_ncq_error_substate_enter(struct sci_base_state_machine *sm)
1006 struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), sm);
1007 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1008 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
1010 if (sci_dev->not_ready_reason == SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED)
1011 isci_remote_device_not_ready(scic_to_ihost(scic), idev,
1012 sci_dev->not_ready_reason);
1015 static void scic_sds_smp_remote_device_ready_idle_substate_enter(struct sci_base_state_machine *sm)
1017 struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), sm);
1018 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1020 isci_remote_device_ready(scic_to_ihost(scic), sci_dev_to_idev(sci_dev));
1023 static void scic_sds_smp_remote_device_ready_cmd_substate_enter(struct sci_base_state_machine *sm)
1025 struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), sm);
1026 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1028 BUG_ON(sci_dev->working_request == NULL);
1030 isci_remote_device_not_ready(scic_to_ihost(scic), sci_dev_to_idev(sci_dev),
1031 SCIC_REMOTE_DEVICE_NOT_READY_SMP_REQUEST_STARTED);
1034 static void scic_sds_smp_remote_device_ready_cmd_substate_exit(struct sci_base_state_machine *sm)
1036 struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), sm);
1038 sci_dev->working_request = NULL;
1041 static const struct sci_base_state scic_sds_remote_device_state_table[] = {
1042 [SCI_DEV_INITIAL] = {
1043 .enter_state = scic_sds_remote_device_initial_state_enter,
1045 [SCI_DEV_STOPPED] = {
1046 .enter_state = scic_sds_remote_device_stopped_state_enter,
1048 [SCI_DEV_STARTING] = {
1049 .enter_state = scic_sds_remote_device_starting_state_enter,
1051 [SCI_DEV_READY] = {
1052 .enter_state = scic_sds_remote_device_ready_state_enter,
1053 .exit_state = scic_sds_remote_device_ready_state_exit
1055 [SCI_STP_DEV_IDLE] = {
1056 .enter_state = scic_sds_stp_remote_device_ready_idle_substate_enter,
1058 [SCI_STP_DEV_CMD] = {
1059 .enter_state = scic_sds_stp_remote_device_ready_cmd_substate_enter,
1061 [SCI_STP_DEV_NCQ] = { },
1062 [SCI_STP_DEV_NCQ_ERROR] = {
1063 .enter_state = scic_sds_stp_remote_device_ready_ncq_error_substate_enter,
1065 [SCI_STP_DEV_AWAIT_RESET] = { },
1066 [SCI_SMP_DEV_IDLE] = {
1067 .enter_state = scic_sds_smp_remote_device_ready_idle_substate_enter,
1069 [SCI_SMP_DEV_CMD] = {
1070 .enter_state = scic_sds_smp_remote_device_ready_cmd_substate_enter,
1071 .exit_state = scic_sds_smp_remote_device_ready_cmd_substate_exit,
1073 [SCI_DEV_STOPPING] = { },
1074 [SCI_DEV_FAILED] = { },
1075 [SCI_DEV_RESETTING] = {
1076 .enter_state = scic_sds_remote_device_resetting_state_enter,
1077 .exit_state = scic_sds_remote_device_resetting_state_exit
1079 [SCI_DEV_FINAL] = { },
1083 * scic_remote_device_construct() - common construction
1084 * @sci_port: SAS/SATA port through which this device is accessed.
1085 * @sci_dev: remote device to construct
1087 * This routine just performs benign initialization and does not
1088 * allocate the remote_node_context which is left to
1089 * scic_remote_device_[de]a_construct(). scic_remote_device_destruct()
1090 * frees the remote_node_context(s) for the device.
1092 static void scic_remote_device_construct(struct scic_sds_port *sci_port,
1093 struct scic_sds_remote_device *sci_dev)
1095 sci_dev->owning_port = sci_port;
1096 sci_dev->started_request_count = 0;
1098 sci_init_sm(&sci_dev->sm, scic_sds_remote_device_state_table, SCI_DEV_INITIAL);
1100 scic_sds_remote_node_context_construct(&sci_dev->rnc,
1101 SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX);
1105 * scic_remote_device_da_construct() - construct direct attached device.
1107 * The information (e.g. IAF, Signature FIS, etc.) necessary to build
1108 * the device is known to the SCI Core since it is contained in the
1109 * scic_phy object. Remote node context(s) is/are a global resource
1110 * allocated by this routine, freed by scic_remote_device_destruct().
1112 * Returns:
1113 * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
1114 * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
1115 * sata-only controller instance.
1116 * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
1118 static enum sci_status scic_remote_device_da_construct(struct scic_sds_port *sci_port,
1119 struct scic_sds_remote_device *sci_dev)
1121 enum sci_status status;
1122 struct domain_device *dev = sci_dev_to_domain(sci_dev);
1124 scic_remote_device_construct(sci_port, sci_dev);
1127 * This information is request to determine how many remote node context
1128 * entries will be needed to store the remote node.
1130 sci_dev->is_direct_attached = true;
1131 status = scic_sds_controller_allocate_remote_node_context(sci_port->owning_controller,
1132 sci_dev,
1133 &sci_dev->rnc.remote_node_index);
1135 if (status != SCI_SUCCESS)
1136 return status;
1138 if (dev->dev_type == SAS_END_DEV || dev->dev_type == SATA_DEV ||
1139 (dev->tproto & SAS_PROTOCOL_STP) || dev_is_expander(dev))
1140 /* pass */;
1141 else
1142 return SCI_FAILURE_UNSUPPORTED_PROTOCOL;
1144 sci_dev->connection_rate = scic_sds_port_get_max_allowed_speed(sci_port);
1146 /* / @todo Should I assign the port width by reading all of the phys on the port? */
1147 sci_dev->device_port_width = 1;
1149 return SCI_SUCCESS;
1153 * scic_remote_device_ea_construct() - construct expander attached device
1155 * Remote node context(s) is/are a global resource allocated by this
1156 * routine, freed by scic_remote_device_destruct().
1158 * Returns:
1159 * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
1160 * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
1161 * sata-only controller instance.
1162 * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
1164 static enum sci_status scic_remote_device_ea_construct(struct scic_sds_port *sci_port,
1165 struct scic_sds_remote_device *sci_dev)
1167 struct domain_device *dev = sci_dev_to_domain(sci_dev);
1168 enum sci_status status;
1170 scic_remote_device_construct(sci_port, sci_dev);
1172 status = scic_sds_controller_allocate_remote_node_context(sci_port->owning_controller,
1173 sci_dev,
1174 &sci_dev->rnc.remote_node_index);
1175 if (status != SCI_SUCCESS)
1176 return status;
1178 if (dev->dev_type == SAS_END_DEV || dev->dev_type == SATA_DEV ||
1179 (dev->tproto & SAS_PROTOCOL_STP) || dev_is_expander(dev))
1180 /* pass */;
1181 else
1182 return SCI_FAILURE_UNSUPPORTED_PROTOCOL;
1185 * For SAS-2 the physical link rate is actually a logical link
1186 * rate that incorporates multiplexing. The SCU doesn't
1187 * incorporate multiplexing and for the purposes of the
1188 * connection the logical link rate is that same as the
1189 * physical. Furthermore, the SAS-2 and SAS-1.1 fields overlay
1190 * one another, so this code works for both situations. */
1191 sci_dev->connection_rate = min_t(u16, scic_sds_port_get_max_allowed_speed(sci_port),
1192 dev->linkrate);
1194 /* / @todo Should I assign the port width by reading all of the phys on the port? */
1195 sci_dev->device_port_width = 1;
1197 return SCI_SUCCESS;
1201 * scic_remote_device_start() - This method will start the supplied remote
1202 * device. This method enables normal IO requests to flow through to the
1203 * remote device.
1204 * @remote_device: This parameter specifies the device to be started.
1205 * @timeout: This parameter specifies the number of milliseconds in which the
1206 * start operation should complete.
1208 * An indication of whether the device was successfully started. SCI_SUCCESS
1209 * This value is returned if the device was successfully started.
1210 * SCI_FAILURE_INVALID_PHY This value is returned if the user attempts to start
1211 * the device when there have been no phys added to it.
1213 static enum sci_status scic_remote_device_start(struct scic_sds_remote_device *sci_dev,
1214 u32 timeout)
1216 struct sci_base_state_machine *sm = &sci_dev->sm;
1217 enum scic_sds_remote_device_states state = sm->current_state_id;
1218 enum sci_status status;
1220 if (state != SCI_DEV_STOPPED) {
1221 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
1222 __func__, state);
1223 return SCI_FAILURE_INVALID_STATE;
1226 status = scic_sds_remote_node_context_resume(&sci_dev->rnc,
1227 remote_device_resume_done,
1228 sci_dev);
1229 if (status != SCI_SUCCESS)
1230 return status;
1232 sci_change_state(sm, SCI_DEV_STARTING);
1234 return SCI_SUCCESS;
1237 static enum sci_status isci_remote_device_construct(struct isci_port *iport,
1238 struct isci_remote_device *idev)
1240 struct scic_sds_port *sci_port = &iport->sci;
1241 struct isci_host *ihost = iport->isci_host;
1242 struct domain_device *dev = idev->domain_dev;
1243 enum sci_status status;
1245 if (dev->parent && dev_is_expander(dev->parent))
1246 status = scic_remote_device_ea_construct(sci_port, &idev->sci);
1247 else
1248 status = scic_remote_device_da_construct(sci_port, &idev->sci);
1250 if (status != SCI_SUCCESS) {
1251 dev_dbg(&ihost->pdev->dev, "%s: construct failed: %d\n",
1252 __func__, status);
1254 return status;
1257 /* start the device. */
1258 status = scic_remote_device_start(&idev->sci, ISCI_REMOTE_DEVICE_START_TIMEOUT);
1260 if (status != SCI_SUCCESS)
1261 dev_warn(&ihost->pdev->dev, "remote device start failed: %d\n",
1262 status);
1264 return status;
1267 void isci_remote_device_nuke_requests(struct isci_host *ihost, struct isci_remote_device *idev)
1269 DECLARE_COMPLETION_ONSTACK(aborted_task_completion);
1271 dev_dbg(&ihost->pdev->dev,
1272 "%s: idev = %p\n", __func__, idev);
1274 /* Cleanup all requests pending for this device. */
1275 isci_terminate_pending_requests(ihost, idev);
1277 dev_dbg(&ihost->pdev->dev,
1278 "%s: idev = %p, done\n", __func__, idev);
1282 * This function builds the isci_remote_device when a libsas dev_found message
1283 * is received.
1284 * @isci_host: This parameter specifies the isci host object.
1285 * @port: This parameter specifies the isci_port conected to this device.
1287 * pointer to new isci_remote_device.
1289 static struct isci_remote_device *
1290 isci_remote_device_alloc(struct isci_host *ihost, struct isci_port *iport)
1292 struct isci_remote_device *idev;
1293 int i;
1295 for (i = 0; i < SCI_MAX_REMOTE_DEVICES; i++) {
1296 idev = &ihost->devices[i];
1297 if (!test_and_set_bit(IDEV_ALLOCATED, &idev->flags))
1298 break;
1301 if (i >= SCI_MAX_REMOTE_DEVICES) {
1302 dev_warn(&ihost->pdev->dev, "%s: failed\n", __func__);
1303 return NULL;
1306 if (WARN_ONCE(!list_empty(&idev->reqs_in_process), "found requests in process\n"))
1307 return NULL;
1309 if (WARN_ONCE(!list_empty(&idev->node), "found non-idle remote device\n"))
1310 return NULL;
1312 isci_remote_device_change_state(idev, isci_freed);
1314 return idev;
1318 * isci_remote_device_stop() - This function is called internally to stop the
1319 * remote device.
1320 * @isci_host: This parameter specifies the isci host object.
1321 * @isci_device: This parameter specifies the remote device.
1323 * The status of the scic request to stop.
1325 enum sci_status isci_remote_device_stop(struct isci_host *ihost, struct isci_remote_device *idev)
1327 enum sci_status status;
1328 unsigned long flags;
1330 dev_dbg(&ihost->pdev->dev,
1331 "%s: isci_device = %p\n", __func__, idev);
1333 isci_remote_device_change_state(idev, isci_stopping);
1335 /* Kill all outstanding requests. */
1336 isci_remote_device_nuke_requests(ihost, idev);
1338 set_bit(IDEV_STOP_PENDING, &idev->flags);
1340 spin_lock_irqsave(&ihost->scic_lock, flags);
1341 status = scic_remote_device_stop(&idev->sci, 50);
1342 spin_unlock_irqrestore(&ihost->scic_lock, flags);
1344 /* Wait for the stop complete callback. */
1345 if (status == SCI_SUCCESS) {
1346 wait_for_device_stop(ihost, idev);
1347 clear_bit(IDEV_ALLOCATED, &idev->flags);
1350 dev_dbg(&ihost->pdev->dev,
1351 "%s: idev = %p - after completion wait\n",
1352 __func__, idev);
1354 return status;
1358 * isci_remote_device_gone() - This function is called by libsas when a domain
1359 * device is removed.
1360 * @domain_device: This parameter specifies the libsas domain device.
1363 void isci_remote_device_gone(struct domain_device *dev)
1365 struct isci_host *ihost = dev_to_ihost(dev);
1366 struct isci_remote_device *idev = dev->lldd_dev;
1368 dev_dbg(&ihost->pdev->dev,
1369 "%s: domain_device = %p, isci_device = %p, isci_port = %p\n",
1370 __func__, dev, idev, idev->isci_port);
1372 isci_remote_device_stop(ihost, idev);
1377 * isci_remote_device_found() - This function is called by libsas when a remote
1378 * device is discovered. A remote device object is created and started. the
1379 * function then sleeps until the sci core device started message is
1380 * received.
1381 * @domain_device: This parameter specifies the libsas domain device.
1383 * status, zero indicates success.
1385 int isci_remote_device_found(struct domain_device *domain_dev)
1387 struct isci_host *isci_host = dev_to_ihost(domain_dev);
1388 struct isci_port *isci_port;
1389 struct isci_phy *isci_phy;
1390 struct asd_sas_port *sas_port;
1391 struct asd_sas_phy *sas_phy;
1392 struct isci_remote_device *isci_device;
1393 enum sci_status status;
1395 dev_dbg(&isci_host->pdev->dev,
1396 "%s: domain_device = %p\n", __func__, domain_dev);
1398 wait_for_start(isci_host);
1400 sas_port = domain_dev->port;
1401 sas_phy = list_first_entry(&sas_port->phy_list, struct asd_sas_phy,
1402 port_phy_el);
1403 isci_phy = to_isci_phy(sas_phy);
1404 isci_port = isci_phy->isci_port;
1406 /* we are being called for a device on this port,
1407 * so it has to come up eventually
1409 wait_for_completion(&isci_port->start_complete);
1411 if ((isci_stopping == isci_port_get_state(isci_port)) ||
1412 (isci_stopped == isci_port_get_state(isci_port)))
1413 return -ENODEV;
1415 isci_device = isci_remote_device_alloc(isci_host, isci_port);
1416 if (!isci_device)
1417 return -ENODEV;
1419 INIT_LIST_HEAD(&isci_device->node);
1420 domain_dev->lldd_dev = isci_device;
1421 isci_device->domain_dev = domain_dev;
1422 isci_device->isci_port = isci_port;
1423 isci_remote_device_change_state(isci_device, isci_starting);
1426 spin_lock_irq(&isci_host->scic_lock);
1427 list_add_tail(&isci_device->node, &isci_port->remote_dev_list);
1429 set_bit(IDEV_START_PENDING, &isci_device->flags);
1430 status = isci_remote_device_construct(isci_port, isci_device);
1431 spin_unlock_irq(&isci_host->scic_lock);
1433 dev_dbg(&isci_host->pdev->dev,
1434 "%s: isci_device = %p\n",
1435 __func__, isci_device);
1437 if (status != SCI_SUCCESS) {
1439 spin_lock_irq(&isci_host->scic_lock);
1440 isci_remote_device_deconstruct(
1441 isci_host,
1442 isci_device
1444 spin_unlock_irq(&isci_host->scic_lock);
1445 return -ENODEV;
1448 /* wait for the device ready callback. */
1449 wait_for_device_start(isci_host, isci_device);
1451 return 0;
1454 * isci_device_is_reset_pending() - This function will check if there is any
1455 * pending reset condition on the device.
1456 * @request: This parameter is the isci_device object.
1458 * true if there is a reset pending for the device.
1460 bool isci_device_is_reset_pending(
1461 struct isci_host *isci_host,
1462 struct isci_remote_device *isci_device)
1464 struct isci_request *isci_request;
1465 struct isci_request *tmp_req;
1466 bool reset_is_pending = false;
1467 unsigned long flags;
1469 dev_dbg(&isci_host->pdev->dev,
1470 "%s: isci_device = %p\n", __func__, isci_device);
1472 spin_lock_irqsave(&isci_host->scic_lock, flags);
1474 /* Check for reset on all pending requests. */
1475 list_for_each_entry_safe(isci_request, tmp_req,
1476 &isci_device->reqs_in_process, dev_node) {
1477 dev_dbg(&isci_host->pdev->dev,
1478 "%s: isci_device = %p request = %p\n",
1479 __func__, isci_device, isci_request);
1481 if (isci_request->ttype == io_task) {
1482 struct sas_task *task = isci_request_access_task(
1483 isci_request);
1485 spin_lock(&task->task_state_lock);
1486 if (task->task_state_flags & SAS_TASK_NEED_DEV_RESET)
1487 reset_is_pending = true;
1488 spin_unlock(&task->task_state_lock);
1492 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1494 dev_dbg(&isci_host->pdev->dev,
1495 "%s: isci_device = %p reset_is_pending = %d\n",
1496 __func__, isci_device, reset_is_pending);
1498 return reset_is_pending;
1502 * isci_device_clear_reset_pending() - This function will clear if any pending
1503 * reset condition flags on the device.
1504 * @request: This parameter is the isci_device object.
1506 * true if there is a reset pending for the device.
1508 void isci_device_clear_reset_pending(struct isci_host *ihost, struct isci_remote_device *idev)
1510 struct isci_request *isci_request;
1511 struct isci_request *tmp_req;
1512 unsigned long flags = 0;
1514 dev_dbg(&ihost->pdev->dev, "%s: idev=%p, ihost=%p\n",
1515 __func__, idev, ihost);
1517 spin_lock_irqsave(&ihost->scic_lock, flags);
1519 /* Clear reset pending on all pending requests. */
1520 list_for_each_entry_safe(isci_request, tmp_req,
1521 &idev->reqs_in_process, dev_node) {
1522 dev_dbg(&ihost->pdev->dev, "%s: idev = %p request = %p\n",
1523 __func__, idev, isci_request);
1525 if (isci_request->ttype == io_task) {
1527 unsigned long flags2;
1528 struct sas_task *task = isci_request_access_task(
1529 isci_request);
1531 spin_lock_irqsave(&task->task_state_lock, flags2);
1532 task->task_state_flags &= ~SAS_TASK_NEED_DEV_RESET;
1533 spin_unlock_irqrestore(&task->task_state_lock, flags2);
1536 spin_unlock_irqrestore(&ihost->scic_lock, flags);