isci: Terminate dev requests on FIS err bit rx in NCQ
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / scsi / isci / remote_device.c
blobc5ce0f0f3645d30eef41d9f1dc43aae46e520842
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_not_ready() - This function is called by the scic when
66 * the remote device is not ready. We mark the isci device as ready (not
67 * "ready_for_io") and signal the waiting proccess.
68 * @isci_host: This parameter specifies the isci host object.
69 * @isci_device: This parameter specifies the remote device
71 * scic_lock is held on entrance to this function.
73 static void isci_remote_device_not_ready(struct isci_host *ihost,
74 struct isci_remote_device *idev, u32 reason)
76 struct isci_request * ireq;
78 dev_dbg(&ihost->pdev->dev,
79 "%s: isci_device = %p\n", __func__, idev);
81 switch (reason) {
82 case SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED:
83 set_bit(IDEV_GONE, &idev->flags);
84 break;
85 case SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED:
86 set_bit(IDEV_IO_NCQERROR, &idev->flags);
88 /* Kill all outstanding requests for the device. */
89 list_for_each_entry(ireq, &idev->reqs_in_process, dev_node) {
91 dev_dbg(&ihost->pdev->dev,
92 "%s: isci_device = %p request = %p\n",
93 __func__, idev, ireq);
95 scic_controller_terminate_request(&ihost->sci,
96 &idev->sci,
97 &ireq->sci);
99 /* Fall through into the default case... */
100 default:
101 clear_bit(IDEV_IO_READY, &idev->flags);
102 break;
107 * isci_remote_device_ready() - This function is called by the scic when the
108 * remote device is ready. We mark the isci device as ready and signal the
109 * waiting proccess.
110 * @ihost: our valid isci_host
111 * @idev: remote device
114 static void isci_remote_device_ready(struct isci_host *ihost, struct isci_remote_device *idev)
116 dev_dbg(&ihost->pdev->dev,
117 "%s: idev = %p\n", __func__, idev);
119 clear_bit(IDEV_IO_NCQERROR, &idev->flags);
120 set_bit(IDEV_IO_READY, &idev->flags);
121 if (test_and_clear_bit(IDEV_START_PENDING, &idev->flags))
122 wake_up(&ihost->eventq);
125 /* called once the remote node context is ready to be freed.
126 * The remote device can now report that its stop operation is complete. none
128 static void rnc_destruct_done(void *_dev)
130 struct scic_sds_remote_device *sci_dev = _dev;
132 BUG_ON(sci_dev->started_request_count != 0);
133 sci_change_state(&sci_dev->sm, SCI_DEV_STOPPED);
136 static enum sci_status scic_sds_remote_device_terminate_requests(struct scic_sds_remote_device *sci_dev)
138 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
139 u32 i, request_count = sci_dev->started_request_count;
140 enum sci_status status = SCI_SUCCESS;
142 for (i = 0; i < SCI_MAX_IO_REQUESTS && i < request_count; i++) {
143 struct scic_sds_request *sci_req;
144 enum sci_status s;
146 sci_req = scic->io_request_table[i];
147 if (!sci_req || sci_req->target_device != sci_dev)
148 continue;
149 s = scic_controller_terminate_request(scic, sci_dev, sci_req);
150 if (s != SCI_SUCCESS)
151 status = s;
154 return status;
157 enum sci_status scic_remote_device_stop(struct scic_sds_remote_device *sci_dev,
158 u32 timeout)
160 struct sci_base_state_machine *sm = &sci_dev->sm;
161 enum scic_sds_remote_device_states state = sm->current_state_id;
163 switch (state) {
164 case SCI_DEV_INITIAL:
165 case SCI_DEV_FAILED:
166 case SCI_DEV_FINAL:
167 default:
168 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
169 __func__, state);
170 return SCI_FAILURE_INVALID_STATE;
171 case SCI_DEV_STOPPED:
172 return SCI_SUCCESS;
173 case SCI_DEV_STARTING:
174 /* device not started so there had better be no requests */
175 BUG_ON(sci_dev->started_request_count != 0);
176 scic_sds_remote_node_context_destruct(&sci_dev->rnc,
177 rnc_destruct_done, sci_dev);
178 /* Transition to the stopping state and wait for the
179 * remote node to complete being posted and invalidated.
181 sci_change_state(sm, SCI_DEV_STOPPING);
182 return SCI_SUCCESS;
183 case SCI_DEV_READY:
184 case SCI_STP_DEV_IDLE:
185 case SCI_STP_DEV_CMD:
186 case SCI_STP_DEV_NCQ:
187 case SCI_STP_DEV_NCQ_ERROR:
188 case SCI_STP_DEV_AWAIT_RESET:
189 case SCI_SMP_DEV_IDLE:
190 case SCI_SMP_DEV_CMD:
191 sci_change_state(sm, SCI_DEV_STOPPING);
192 if (sci_dev->started_request_count == 0) {
193 scic_sds_remote_node_context_destruct(&sci_dev->rnc,
194 rnc_destruct_done, sci_dev);
195 return SCI_SUCCESS;
196 } else
197 return scic_sds_remote_device_terminate_requests(sci_dev);
198 break;
199 case SCI_DEV_STOPPING:
200 /* All requests should have been terminated, but if there is an
201 * attempt to stop a device already in the stopping state, then
202 * try again to terminate.
204 return scic_sds_remote_device_terminate_requests(sci_dev);
205 case SCI_DEV_RESETTING:
206 sci_change_state(sm, SCI_DEV_STOPPING);
207 return SCI_SUCCESS;
211 enum sci_status scic_remote_device_reset(struct scic_sds_remote_device *sci_dev)
213 struct sci_base_state_machine *sm = &sci_dev->sm;
214 enum scic_sds_remote_device_states state = sm->current_state_id;
216 switch (state) {
217 case SCI_DEV_INITIAL:
218 case SCI_DEV_STOPPED:
219 case SCI_DEV_STARTING:
220 case SCI_SMP_DEV_IDLE:
221 case SCI_SMP_DEV_CMD:
222 case SCI_DEV_STOPPING:
223 case SCI_DEV_FAILED:
224 case SCI_DEV_RESETTING:
225 case SCI_DEV_FINAL:
226 default:
227 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
228 __func__, state);
229 return SCI_FAILURE_INVALID_STATE;
230 case SCI_DEV_READY:
231 case SCI_STP_DEV_IDLE:
232 case SCI_STP_DEV_CMD:
233 case SCI_STP_DEV_NCQ:
234 case SCI_STP_DEV_NCQ_ERROR:
235 case SCI_STP_DEV_AWAIT_RESET:
236 sci_change_state(sm, SCI_DEV_RESETTING);
237 return SCI_SUCCESS;
241 enum sci_status scic_remote_device_reset_complete(struct scic_sds_remote_device *sci_dev)
243 struct sci_base_state_machine *sm = &sci_dev->sm;
244 enum scic_sds_remote_device_states state = sm->current_state_id;
246 if (state != SCI_DEV_RESETTING) {
247 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
248 __func__, state);
249 return SCI_FAILURE_INVALID_STATE;
252 sci_change_state(sm, SCI_DEV_READY);
253 return SCI_SUCCESS;
256 enum sci_status scic_sds_remote_device_suspend(struct scic_sds_remote_device *sci_dev,
257 u32 suspend_type)
259 struct sci_base_state_machine *sm = &sci_dev->sm;
260 enum scic_sds_remote_device_states state = sm->current_state_id;
262 if (state != SCI_STP_DEV_CMD) {
263 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
264 __func__, state);
265 return SCI_FAILURE_INVALID_STATE;
268 return scic_sds_remote_node_context_suspend(&sci_dev->rnc,
269 suspend_type, NULL, NULL);
272 enum sci_status scic_sds_remote_device_frame_handler(struct scic_sds_remote_device *sci_dev,
273 u32 frame_index)
275 struct sci_base_state_machine *sm = &sci_dev->sm;
276 enum scic_sds_remote_device_states state = sm->current_state_id;
277 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
278 enum sci_status status;
280 switch (state) {
281 case SCI_DEV_INITIAL:
282 case SCI_DEV_STOPPED:
283 case SCI_DEV_STARTING:
284 case SCI_STP_DEV_IDLE:
285 case SCI_SMP_DEV_IDLE:
286 case SCI_DEV_FINAL:
287 default:
288 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
289 __func__, state);
290 /* Return the frame back to the controller */
291 scic_sds_controller_release_frame(scic, frame_index);
292 return SCI_FAILURE_INVALID_STATE;
293 case SCI_DEV_READY:
294 case SCI_STP_DEV_NCQ_ERROR:
295 case SCI_STP_DEV_AWAIT_RESET:
296 case SCI_DEV_STOPPING:
297 case SCI_DEV_FAILED:
298 case SCI_DEV_RESETTING: {
299 struct scic_sds_request *sci_req;
300 struct ssp_frame_hdr hdr;
301 void *frame_header;
302 ssize_t word_cnt;
304 status = scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
305 frame_index,
306 &frame_header);
307 if (status != SCI_SUCCESS)
308 return status;
310 word_cnt = sizeof(hdr) / sizeof(u32);
311 sci_swab32_cpy(&hdr, frame_header, word_cnt);
313 sci_req = scic_request_by_tag(scic, be16_to_cpu(hdr.tag));
314 if (sci_req && sci_req->target_device == sci_dev) {
315 /* The IO request is now in charge of releasing the frame */
316 status = scic_sds_io_request_frame_handler(sci_req, frame_index);
317 } else {
318 /* We could not map this tag to a valid IO
319 * request Just toss the frame and continue
321 scic_sds_controller_release_frame(scic, frame_index);
323 break;
325 case SCI_STP_DEV_NCQ: {
326 struct dev_to_host_fis *hdr;
328 status = scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
329 frame_index,
330 (void **)&hdr);
331 if (status != SCI_SUCCESS)
332 return status;
334 if (hdr->fis_type == FIS_SETDEVBITS &&
335 (hdr->status & ATA_ERR)) {
336 sci_dev->not_ready_reason = SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED;
338 /* TODO Check sactive and complete associated IO if any. */
339 sci_change_state(sm, SCI_STP_DEV_NCQ_ERROR);
340 } else if (hdr->fis_type == FIS_REGD2H &&
341 (hdr->status & ATA_ERR)) {
343 * Some devices return D2H FIS when an NCQ error is detected.
344 * Treat this like an SDB error FIS ready reason.
346 sci_dev->not_ready_reason = SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED;
347 sci_change_state(&sci_dev->sm, SCI_STP_DEV_NCQ_ERROR);
348 } else
349 status = SCI_FAILURE;
351 scic_sds_controller_release_frame(scic, frame_index);
352 break;
354 case SCI_STP_DEV_CMD:
355 case SCI_SMP_DEV_CMD:
356 /* The device does not process any UF received from the hardware while
357 * in this state. All unsolicited frames are forwarded to the io request
358 * object.
360 status = scic_sds_io_request_frame_handler(sci_dev->working_request, frame_index);
361 break;
364 return status;
367 static bool is_remote_device_ready(struct scic_sds_remote_device *sci_dev)
370 struct sci_base_state_machine *sm = &sci_dev->sm;
371 enum scic_sds_remote_device_states state = sm->current_state_id;
373 switch (state) {
374 case SCI_DEV_READY:
375 case SCI_STP_DEV_IDLE:
376 case SCI_STP_DEV_CMD:
377 case SCI_STP_DEV_NCQ:
378 case SCI_STP_DEV_NCQ_ERROR:
379 case SCI_STP_DEV_AWAIT_RESET:
380 case SCI_SMP_DEV_IDLE:
381 case SCI_SMP_DEV_CMD:
382 return true;
383 default:
384 return false;
388 enum sci_status scic_sds_remote_device_event_handler(struct scic_sds_remote_device *sci_dev,
389 u32 event_code)
391 struct sci_base_state_machine *sm = &sci_dev->sm;
392 enum scic_sds_remote_device_states state = sm->current_state_id;
393 enum sci_status status;
395 switch (scu_get_event_type(event_code)) {
396 case SCU_EVENT_TYPE_RNC_OPS_MISC:
397 case SCU_EVENT_TYPE_RNC_SUSPEND_TX:
398 case SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX:
399 status = scic_sds_remote_node_context_event_handler(&sci_dev->rnc, event_code);
400 break;
401 case SCU_EVENT_TYPE_PTX_SCHEDULE_EVENT:
402 if (scu_get_event_code(event_code) == SCU_EVENT_IT_NEXUS_TIMEOUT) {
403 status = SCI_SUCCESS;
405 /* Suspend the associated RNC */
406 scic_sds_remote_node_context_suspend(&sci_dev->rnc,
407 SCI_SOFTWARE_SUSPENSION,
408 NULL, NULL);
410 dev_dbg(scirdev_to_dev(sci_dev),
411 "%s: device: %p event code: %x: %s\n",
412 __func__, sci_dev, event_code,
413 is_remote_device_ready(sci_dev)
414 ? "I_T_Nexus_Timeout event"
415 : "I_T_Nexus_Timeout event in wrong state");
417 break;
419 /* Else, fall through and treat as unhandled... */
420 default:
421 dev_dbg(scirdev_to_dev(sci_dev),
422 "%s: device: %p event code: %x: %s\n",
423 __func__, sci_dev, event_code,
424 is_remote_device_ready(sci_dev)
425 ? "unexpected event"
426 : "unexpected event in wrong state");
427 status = SCI_FAILURE_INVALID_STATE;
428 break;
431 if (status != SCI_SUCCESS)
432 return status;
434 if (state == SCI_STP_DEV_IDLE) {
436 /* We pick up suspension events to handle specifically to this
437 * state. We resume the RNC right away.
439 if (scu_get_event_type(event_code) == SCU_EVENT_TYPE_RNC_SUSPEND_TX ||
440 scu_get_event_type(event_code) == SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX)
441 status = scic_sds_remote_node_context_resume(&sci_dev->rnc, NULL, NULL);
444 return status;
447 static void scic_sds_remote_device_start_request(struct scic_sds_remote_device *sci_dev,
448 struct scic_sds_request *sci_req,
449 enum sci_status status)
451 struct scic_sds_port *sci_port = sci_dev->owning_port;
453 /* cleanup requests that failed after starting on the port */
454 if (status != SCI_SUCCESS)
455 scic_sds_port_complete_io(sci_port, sci_dev, sci_req);
456 else {
457 kref_get(&sci_dev_to_idev(sci_dev)->kref);
458 scic_sds_remote_device_increment_request_count(sci_dev);
462 enum sci_status scic_sds_remote_device_start_io(struct scic_sds_controller *scic,
463 struct scic_sds_remote_device *sci_dev,
464 struct scic_sds_request *sci_req)
466 struct sci_base_state_machine *sm = &sci_dev->sm;
467 enum scic_sds_remote_device_states state = sm->current_state_id;
468 struct scic_sds_port *sci_port = sci_dev->owning_port;
469 struct isci_request *ireq = sci_req_to_ireq(sci_req);
470 enum sci_status status;
472 switch (state) {
473 case SCI_DEV_INITIAL:
474 case SCI_DEV_STOPPED:
475 case SCI_DEV_STARTING:
476 case SCI_STP_DEV_NCQ_ERROR:
477 case SCI_DEV_STOPPING:
478 case SCI_DEV_FAILED:
479 case SCI_DEV_RESETTING:
480 case SCI_DEV_FINAL:
481 default:
482 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
483 __func__, state);
484 return SCI_FAILURE_INVALID_STATE;
485 case SCI_DEV_READY:
486 /* attempt to start an io request for this device object. The remote
487 * device object will issue the start request for the io and if
488 * successful it will start the request for the port object then
489 * increment its own request count.
491 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
492 if (status != SCI_SUCCESS)
493 return status;
495 status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, sci_req);
496 if (status != SCI_SUCCESS)
497 break;
499 status = scic_sds_request_start(sci_req);
500 break;
501 case SCI_STP_DEV_IDLE: {
502 /* handle the start io operation for a sata device that is in
503 * the command idle state. - Evalute the type of IO request to
504 * be started - If its an NCQ request change to NCQ substate -
505 * If its any other command change to the CMD substate
507 * If this is a softreset we may want to have a different
508 * substate.
510 enum scic_sds_remote_device_states new_state;
511 struct sas_task *task = isci_request_access_task(ireq);
513 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
514 if (status != SCI_SUCCESS)
515 return status;
517 status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, sci_req);
518 if (status != SCI_SUCCESS)
519 break;
521 status = scic_sds_request_start(sci_req);
522 if (status != SCI_SUCCESS)
523 break;
525 if (task->ata_task.use_ncq)
526 new_state = SCI_STP_DEV_NCQ;
527 else {
528 sci_dev->working_request = sci_req;
529 new_state = SCI_STP_DEV_CMD;
531 sci_change_state(sm, new_state);
532 break;
534 case SCI_STP_DEV_NCQ: {
535 struct sas_task *task = isci_request_access_task(ireq);
537 if (task->ata_task.use_ncq) {
538 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
539 if (status != SCI_SUCCESS)
540 return status;
542 status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, sci_req);
543 if (status != SCI_SUCCESS)
544 break;
546 status = scic_sds_request_start(sci_req);
547 } else
548 return SCI_FAILURE_INVALID_STATE;
549 break;
551 case SCI_STP_DEV_AWAIT_RESET:
552 return SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED;
553 case SCI_SMP_DEV_IDLE:
554 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
555 if (status != SCI_SUCCESS)
556 return status;
558 status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, sci_req);
559 if (status != SCI_SUCCESS)
560 break;
562 status = scic_sds_request_start(sci_req);
563 if (status != SCI_SUCCESS)
564 break;
566 sci_dev->working_request = sci_req;
567 sci_change_state(&sci_dev->sm, SCI_SMP_DEV_CMD);
568 break;
569 case SCI_STP_DEV_CMD:
570 case SCI_SMP_DEV_CMD:
571 /* device is already handling a command it can not accept new commands
572 * until this one is complete.
574 return SCI_FAILURE_INVALID_STATE;
577 scic_sds_remote_device_start_request(sci_dev, sci_req, status);
578 return status;
581 static enum sci_status common_complete_io(struct scic_sds_port *sci_port,
582 struct scic_sds_remote_device *sci_dev,
583 struct scic_sds_request *sci_req)
585 enum sci_status status;
587 status = scic_sds_request_complete(sci_req);
588 if (status != SCI_SUCCESS)
589 return status;
591 status = scic_sds_port_complete_io(sci_port, sci_dev, sci_req);
592 if (status != SCI_SUCCESS)
593 return status;
595 scic_sds_remote_device_decrement_request_count(sci_dev);
596 return status;
599 enum sci_status scic_sds_remote_device_complete_io(struct scic_sds_controller *scic,
600 struct scic_sds_remote_device *sci_dev,
601 struct scic_sds_request *sci_req)
603 struct sci_base_state_machine *sm = &sci_dev->sm;
604 enum scic_sds_remote_device_states state = sm->current_state_id;
605 struct scic_sds_port *sci_port = sci_dev->owning_port;
606 enum sci_status status;
608 switch (state) {
609 case SCI_DEV_INITIAL:
610 case SCI_DEV_STOPPED:
611 case SCI_DEV_STARTING:
612 case SCI_STP_DEV_IDLE:
613 case SCI_SMP_DEV_IDLE:
614 case SCI_DEV_FAILED:
615 case SCI_DEV_FINAL:
616 default:
617 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
618 __func__, state);
619 return SCI_FAILURE_INVALID_STATE;
620 case SCI_DEV_READY:
621 case SCI_STP_DEV_AWAIT_RESET:
622 case SCI_DEV_RESETTING:
623 status = common_complete_io(sci_port, sci_dev, sci_req);
624 break;
625 case SCI_STP_DEV_CMD:
626 case SCI_STP_DEV_NCQ:
627 case SCI_STP_DEV_NCQ_ERROR:
628 status = common_complete_io(sci_port, sci_dev, sci_req);
629 if (status != SCI_SUCCESS)
630 break;
632 if (sci_req->sci_status == SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED) {
633 /* This request causes hardware error, device needs to be Lun Reset.
634 * So here we force the state machine to IDLE state so the rest IOs
635 * can reach RNC state handler, these IOs will be completed by RNC with
636 * status of "DEVICE_RESET_REQUIRED", instead of "INVALID STATE".
638 sci_change_state(sm, SCI_STP_DEV_AWAIT_RESET);
639 } else if (scic_sds_remote_device_get_request_count(sci_dev) == 0)
640 sci_change_state(sm, SCI_STP_DEV_IDLE);
641 break;
642 case SCI_SMP_DEV_CMD:
643 status = common_complete_io(sci_port, sci_dev, sci_req);
644 if (status != SCI_SUCCESS)
645 break;
646 sci_change_state(sm, SCI_SMP_DEV_IDLE);
647 break;
648 case SCI_DEV_STOPPING:
649 status = common_complete_io(sci_port, sci_dev, sci_req);
650 if (status != SCI_SUCCESS)
651 break;
653 if (scic_sds_remote_device_get_request_count(sci_dev) == 0)
654 scic_sds_remote_node_context_destruct(&sci_dev->rnc,
655 rnc_destruct_done,
656 sci_dev);
657 break;
660 if (status != SCI_SUCCESS)
661 dev_err(scirdev_to_dev(sci_dev),
662 "%s: Port:0x%p Device:0x%p Request:0x%p Status:0x%x "
663 "could not complete\n", __func__, sci_port,
664 sci_dev, sci_req, status);
665 else
666 isci_put_device(sci_dev_to_idev(sci_dev));
668 return status;
671 static void scic_sds_remote_device_continue_request(void *dev)
673 struct scic_sds_remote_device *sci_dev = dev;
675 /* we need to check if this request is still valid to continue. */
676 if (sci_dev->working_request)
677 scic_controller_continue_io(sci_dev->working_request);
680 enum sci_status scic_sds_remote_device_start_task(struct scic_sds_controller *scic,
681 struct scic_sds_remote_device *sci_dev,
682 struct scic_sds_request *sci_req)
684 struct sci_base_state_machine *sm = &sci_dev->sm;
685 enum scic_sds_remote_device_states state = sm->current_state_id;
686 struct scic_sds_port *sci_port = sci_dev->owning_port;
687 enum sci_status status;
689 switch (state) {
690 case SCI_DEV_INITIAL:
691 case SCI_DEV_STOPPED:
692 case SCI_DEV_STARTING:
693 case SCI_SMP_DEV_IDLE:
694 case SCI_SMP_DEV_CMD:
695 case SCI_DEV_STOPPING:
696 case SCI_DEV_FAILED:
697 case SCI_DEV_RESETTING:
698 case SCI_DEV_FINAL:
699 default:
700 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
701 __func__, state);
702 return SCI_FAILURE_INVALID_STATE;
703 case SCI_STP_DEV_IDLE:
704 case SCI_STP_DEV_CMD:
705 case SCI_STP_DEV_NCQ:
706 case SCI_STP_DEV_NCQ_ERROR:
707 case SCI_STP_DEV_AWAIT_RESET:
708 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
709 if (status != SCI_SUCCESS)
710 return status;
712 status = scic_sds_remote_node_context_start_task(&sci_dev->rnc, sci_req);
713 if (status != SCI_SUCCESS)
714 goto out;
716 status = scic_sds_request_start(sci_req);
717 if (status != SCI_SUCCESS)
718 goto out;
720 /* Note: If the remote device state is not IDLE this will
721 * replace the request that probably resulted in the task
722 * management request.
724 sci_dev->working_request = sci_req;
725 sci_change_state(sm, SCI_STP_DEV_CMD);
727 /* The remote node context must cleanup the TCi to NCQ mapping
728 * table. The only way to do this correctly is to either write
729 * to the TLCR register or to invalidate and repost the RNC. In
730 * either case the remote node context state machine will take
731 * the correct action when the remote node context is suspended
732 * and later resumed.
734 scic_sds_remote_node_context_suspend(&sci_dev->rnc,
735 SCI_SOFTWARE_SUSPENSION, NULL, NULL);
736 scic_sds_remote_node_context_resume(&sci_dev->rnc,
737 scic_sds_remote_device_continue_request,
738 sci_dev);
740 out:
741 scic_sds_remote_device_start_request(sci_dev, sci_req, status);
742 /* We need to let the controller start request handler know that
743 * it can't post TC yet. We will provide a callback function to
744 * post TC when RNC gets resumed.
746 return SCI_FAILURE_RESET_DEVICE_PARTIAL_SUCCESS;
747 case SCI_DEV_READY:
748 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
749 if (status != SCI_SUCCESS)
750 return status;
752 status = scic_sds_remote_node_context_start_task(&sci_dev->rnc, sci_req);
753 if (status != SCI_SUCCESS)
754 break;
756 status = scic_sds_request_start(sci_req);
757 break;
759 scic_sds_remote_device_start_request(sci_dev, sci_req, status);
761 return status;
766 * @sci_dev:
767 * @request:
769 * This method takes the request and bulids an appropriate SCU context for the
770 * request and then requests the controller to post the request. none
772 void scic_sds_remote_device_post_request(
773 struct scic_sds_remote_device *sci_dev,
774 u32 request)
776 u32 context;
778 context = scic_sds_remote_device_build_command_context(sci_dev, request);
780 scic_sds_controller_post_request(
781 scic_sds_remote_device_get_controller(sci_dev),
782 context
786 /* called once the remote node context has transisitioned to a
787 * ready state. This is the indication that the remote device object can also
788 * transition to ready.
790 static void remote_device_resume_done(void *_dev)
792 struct scic_sds_remote_device *sci_dev = _dev;
794 if (is_remote_device_ready(sci_dev))
795 return;
797 /* go 'ready' if we are not already in a ready state */
798 sci_change_state(&sci_dev->sm, SCI_DEV_READY);
801 static void scic_sds_stp_remote_device_ready_idle_substate_resume_complete_handler(void *_dev)
803 struct scic_sds_remote_device *sci_dev = _dev;
804 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
805 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
807 /* For NCQ operation we do not issue a isci_remote_device_not_ready().
808 * As a result, avoid sending the ready notification.
810 if (sci_dev->sm.previous_state_id != SCI_STP_DEV_NCQ)
811 isci_remote_device_ready(scic_to_ihost(scic), idev);
814 static void scic_sds_remote_device_initial_state_enter(struct sci_base_state_machine *sm)
816 struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), sm);
818 /* Initial state is a transitional state to the stopped state */
819 sci_change_state(&sci_dev->sm, SCI_DEV_STOPPED);
823 * scic_remote_device_destruct() - free remote node context and destruct
824 * @remote_device: This parameter specifies the remote device to be destructed.
826 * Remote device objects are a limited resource. As such, they must be
827 * protected. Thus calls to construct and destruct are mutually exclusive and
828 * non-reentrant. The return value shall indicate if the device was
829 * successfully destructed or if some failure occurred. enum sci_status This value
830 * is returned if the device is successfully destructed.
831 * SCI_FAILURE_INVALID_REMOTE_DEVICE This value is returned if the supplied
832 * device isn't valid (e.g. it's already been destoryed, the handle isn't
833 * valid, etc.).
835 static enum sci_status scic_remote_device_destruct(struct scic_sds_remote_device *sci_dev)
837 struct sci_base_state_machine *sm = &sci_dev->sm;
838 enum scic_sds_remote_device_states state = sm->current_state_id;
839 struct scic_sds_controller *scic;
841 if (state != SCI_DEV_STOPPED) {
842 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
843 __func__, state);
844 return SCI_FAILURE_INVALID_STATE;
847 scic = sci_dev->owning_port->owning_controller;
848 scic_sds_controller_free_remote_node_context(scic, sci_dev,
849 sci_dev->rnc.remote_node_index);
850 sci_dev->rnc.remote_node_index = SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX;
851 sci_change_state(sm, SCI_DEV_FINAL);
853 return SCI_SUCCESS;
857 * isci_remote_device_deconstruct() - This function frees an isci_remote_device.
858 * @ihost: This parameter specifies the isci host object.
859 * @idev: This parameter specifies the remote device to be freed.
862 static void isci_remote_device_deconstruct(struct isci_host *ihost, struct isci_remote_device *idev)
864 dev_dbg(&ihost->pdev->dev,
865 "%s: isci_device = %p\n", __func__, idev);
867 /* There should not be any outstanding io's. All paths to
868 * here should go through isci_remote_device_nuke_requests.
869 * If we hit this condition, we will need a way to complete
870 * io requests in process */
871 BUG_ON(!list_empty(&idev->reqs_in_process));
873 scic_remote_device_destruct(&idev->sci);
874 list_del_init(&idev->node);
875 isci_put_device(idev);
878 static void scic_sds_remote_device_stopped_state_enter(struct sci_base_state_machine *sm)
880 struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), sm);
881 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
882 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
883 u32 prev_state;
885 /* If we are entering from the stopping state let the SCI User know that
886 * the stop operation has completed.
888 prev_state = sci_dev->sm.previous_state_id;
889 if (prev_state == SCI_DEV_STOPPING)
890 isci_remote_device_deconstruct(scic_to_ihost(scic), idev);
892 scic_sds_controller_remote_device_stopped(scic, sci_dev);
895 static void scic_sds_remote_device_starting_state_enter(struct sci_base_state_machine *sm)
897 struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), sm);
898 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
899 struct isci_host *ihost = scic_to_ihost(scic);
900 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
902 isci_remote_device_not_ready(ihost, idev,
903 SCIC_REMOTE_DEVICE_NOT_READY_START_REQUESTED);
906 static void scic_sds_remote_device_ready_state_enter(struct sci_base_state_machine *sm)
908 struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), sm);
909 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
910 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
911 struct domain_device *dev = idev->domain_dev;
913 if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_SATA)) {
914 sci_change_state(&sci_dev->sm, SCI_STP_DEV_IDLE);
915 } else if (dev_is_expander(dev)) {
916 sci_change_state(&sci_dev->sm, SCI_SMP_DEV_IDLE);
917 } else
918 isci_remote_device_ready(scic_to_ihost(scic), idev);
921 static void scic_sds_remote_device_ready_state_exit(struct sci_base_state_machine *sm)
923 struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), sm);
924 struct domain_device *dev = sci_dev_to_domain(sci_dev);
926 if (dev->dev_type == SAS_END_DEV) {
927 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
928 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
930 isci_remote_device_not_ready(scic_to_ihost(scic), idev,
931 SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED);
935 static void scic_sds_remote_device_resetting_state_enter(struct sci_base_state_machine *sm)
937 struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), sm);
939 scic_sds_remote_node_context_suspend(
940 &sci_dev->rnc, SCI_SOFTWARE_SUSPENSION, NULL, NULL);
943 static void scic_sds_remote_device_resetting_state_exit(struct sci_base_state_machine *sm)
945 struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), sm);
947 scic_sds_remote_node_context_resume(&sci_dev->rnc, NULL, NULL);
950 static void scic_sds_stp_remote_device_ready_idle_substate_enter(struct sci_base_state_machine *sm)
952 struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), sm);
954 sci_dev->working_request = NULL;
955 if (scic_sds_remote_node_context_is_ready(&sci_dev->rnc)) {
957 * Since the RNC is ready, it's alright to finish completion
958 * processing (e.g. signal the remote device is ready). */
959 scic_sds_stp_remote_device_ready_idle_substate_resume_complete_handler(sci_dev);
960 } else {
961 scic_sds_remote_node_context_resume(&sci_dev->rnc,
962 scic_sds_stp_remote_device_ready_idle_substate_resume_complete_handler,
963 sci_dev);
967 static void scic_sds_stp_remote_device_ready_cmd_substate_enter(struct sci_base_state_machine *sm)
969 struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), sm);
970 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
972 BUG_ON(sci_dev->working_request == NULL);
974 isci_remote_device_not_ready(scic_to_ihost(scic), sci_dev_to_idev(sci_dev),
975 SCIC_REMOTE_DEVICE_NOT_READY_SATA_REQUEST_STARTED);
978 static void scic_sds_stp_remote_device_ready_ncq_error_substate_enter(struct sci_base_state_machine *sm)
980 struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), sm);
981 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
982 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
984 if (sci_dev->not_ready_reason == SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED)
985 isci_remote_device_not_ready(scic_to_ihost(scic), idev,
986 sci_dev->not_ready_reason);
989 static void scic_sds_smp_remote_device_ready_idle_substate_enter(struct sci_base_state_machine *sm)
991 struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), sm);
992 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
994 isci_remote_device_ready(scic_to_ihost(scic), sci_dev_to_idev(sci_dev));
997 static void scic_sds_smp_remote_device_ready_cmd_substate_enter(struct sci_base_state_machine *sm)
999 struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), sm);
1000 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1002 BUG_ON(sci_dev->working_request == NULL);
1004 isci_remote_device_not_ready(scic_to_ihost(scic), sci_dev_to_idev(sci_dev),
1005 SCIC_REMOTE_DEVICE_NOT_READY_SMP_REQUEST_STARTED);
1008 static void scic_sds_smp_remote_device_ready_cmd_substate_exit(struct sci_base_state_machine *sm)
1010 struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), sm);
1012 sci_dev->working_request = NULL;
1015 static const struct sci_base_state scic_sds_remote_device_state_table[] = {
1016 [SCI_DEV_INITIAL] = {
1017 .enter_state = scic_sds_remote_device_initial_state_enter,
1019 [SCI_DEV_STOPPED] = {
1020 .enter_state = scic_sds_remote_device_stopped_state_enter,
1022 [SCI_DEV_STARTING] = {
1023 .enter_state = scic_sds_remote_device_starting_state_enter,
1025 [SCI_DEV_READY] = {
1026 .enter_state = scic_sds_remote_device_ready_state_enter,
1027 .exit_state = scic_sds_remote_device_ready_state_exit
1029 [SCI_STP_DEV_IDLE] = {
1030 .enter_state = scic_sds_stp_remote_device_ready_idle_substate_enter,
1032 [SCI_STP_DEV_CMD] = {
1033 .enter_state = scic_sds_stp_remote_device_ready_cmd_substate_enter,
1035 [SCI_STP_DEV_NCQ] = { },
1036 [SCI_STP_DEV_NCQ_ERROR] = {
1037 .enter_state = scic_sds_stp_remote_device_ready_ncq_error_substate_enter,
1039 [SCI_STP_DEV_AWAIT_RESET] = { },
1040 [SCI_SMP_DEV_IDLE] = {
1041 .enter_state = scic_sds_smp_remote_device_ready_idle_substate_enter,
1043 [SCI_SMP_DEV_CMD] = {
1044 .enter_state = scic_sds_smp_remote_device_ready_cmd_substate_enter,
1045 .exit_state = scic_sds_smp_remote_device_ready_cmd_substate_exit,
1047 [SCI_DEV_STOPPING] = { },
1048 [SCI_DEV_FAILED] = { },
1049 [SCI_DEV_RESETTING] = {
1050 .enter_state = scic_sds_remote_device_resetting_state_enter,
1051 .exit_state = scic_sds_remote_device_resetting_state_exit
1053 [SCI_DEV_FINAL] = { },
1057 * scic_remote_device_construct() - common construction
1058 * @sci_port: SAS/SATA port through which this device is accessed.
1059 * @sci_dev: remote device to construct
1061 * This routine just performs benign initialization and does not
1062 * allocate the remote_node_context which is left to
1063 * scic_remote_device_[de]a_construct(). scic_remote_device_destruct()
1064 * frees the remote_node_context(s) for the device.
1066 static void scic_remote_device_construct(struct scic_sds_port *sci_port,
1067 struct scic_sds_remote_device *sci_dev)
1069 sci_dev->owning_port = sci_port;
1070 sci_dev->started_request_count = 0;
1072 sci_init_sm(&sci_dev->sm, scic_sds_remote_device_state_table, SCI_DEV_INITIAL);
1074 scic_sds_remote_node_context_construct(&sci_dev->rnc,
1075 SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX);
1079 * scic_remote_device_da_construct() - construct direct attached device.
1081 * The information (e.g. IAF, Signature FIS, etc.) necessary to build
1082 * the device is known to the SCI Core since it is contained in the
1083 * scic_phy object. Remote node context(s) is/are a global resource
1084 * allocated by this routine, freed by scic_remote_device_destruct().
1086 * Returns:
1087 * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
1088 * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
1089 * sata-only controller instance.
1090 * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
1092 static enum sci_status scic_remote_device_da_construct(struct scic_sds_port *sci_port,
1093 struct scic_sds_remote_device *sci_dev)
1095 enum sci_status status;
1096 struct domain_device *dev = sci_dev_to_domain(sci_dev);
1098 scic_remote_device_construct(sci_port, sci_dev);
1101 * This information is request to determine how many remote node context
1102 * entries will be needed to store the remote node.
1104 sci_dev->is_direct_attached = true;
1105 status = scic_sds_controller_allocate_remote_node_context(sci_port->owning_controller,
1106 sci_dev,
1107 &sci_dev->rnc.remote_node_index);
1109 if (status != SCI_SUCCESS)
1110 return status;
1112 if (dev->dev_type == SAS_END_DEV || dev->dev_type == SATA_DEV ||
1113 (dev->tproto & SAS_PROTOCOL_STP) || dev_is_expander(dev))
1114 /* pass */;
1115 else
1116 return SCI_FAILURE_UNSUPPORTED_PROTOCOL;
1118 sci_dev->connection_rate = scic_sds_port_get_max_allowed_speed(sci_port);
1120 /* / @todo Should I assign the port width by reading all of the phys on the port? */
1121 sci_dev->device_port_width = 1;
1123 return SCI_SUCCESS;
1127 * scic_remote_device_ea_construct() - construct expander attached device
1129 * Remote node context(s) is/are a global resource allocated by this
1130 * routine, freed by scic_remote_device_destruct().
1132 * Returns:
1133 * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
1134 * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
1135 * sata-only controller instance.
1136 * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
1138 static enum sci_status scic_remote_device_ea_construct(struct scic_sds_port *sci_port,
1139 struct scic_sds_remote_device *sci_dev)
1141 struct domain_device *dev = sci_dev_to_domain(sci_dev);
1142 enum sci_status status;
1144 scic_remote_device_construct(sci_port, sci_dev);
1146 status = scic_sds_controller_allocate_remote_node_context(sci_port->owning_controller,
1147 sci_dev,
1148 &sci_dev->rnc.remote_node_index);
1149 if (status != SCI_SUCCESS)
1150 return status;
1152 if (dev->dev_type == SAS_END_DEV || dev->dev_type == SATA_DEV ||
1153 (dev->tproto & SAS_PROTOCOL_STP) || dev_is_expander(dev))
1154 /* pass */;
1155 else
1156 return SCI_FAILURE_UNSUPPORTED_PROTOCOL;
1159 * For SAS-2 the physical link rate is actually a logical link
1160 * rate that incorporates multiplexing. The SCU doesn't
1161 * incorporate multiplexing and for the purposes of the
1162 * connection the logical link rate is that same as the
1163 * physical. Furthermore, the SAS-2 and SAS-1.1 fields overlay
1164 * one another, so this code works for both situations. */
1165 sci_dev->connection_rate = min_t(u16, scic_sds_port_get_max_allowed_speed(sci_port),
1166 dev->linkrate);
1168 /* / @todo Should I assign the port width by reading all of the phys on the port? */
1169 sci_dev->device_port_width = 1;
1171 return SCI_SUCCESS;
1175 * scic_remote_device_start() - This method will start the supplied remote
1176 * device. This method enables normal IO requests to flow through to the
1177 * remote device.
1178 * @remote_device: This parameter specifies the device to be started.
1179 * @timeout: This parameter specifies the number of milliseconds in which the
1180 * start operation should complete.
1182 * An indication of whether the device was successfully started. SCI_SUCCESS
1183 * This value is returned if the device was successfully started.
1184 * SCI_FAILURE_INVALID_PHY This value is returned if the user attempts to start
1185 * the device when there have been no phys added to it.
1187 static enum sci_status scic_remote_device_start(struct scic_sds_remote_device *sci_dev,
1188 u32 timeout)
1190 struct sci_base_state_machine *sm = &sci_dev->sm;
1191 enum scic_sds_remote_device_states state = sm->current_state_id;
1192 enum sci_status status;
1194 if (state != SCI_DEV_STOPPED) {
1195 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
1196 __func__, state);
1197 return SCI_FAILURE_INVALID_STATE;
1200 status = scic_sds_remote_node_context_resume(&sci_dev->rnc,
1201 remote_device_resume_done,
1202 sci_dev);
1203 if (status != SCI_SUCCESS)
1204 return status;
1206 sci_change_state(sm, SCI_DEV_STARTING);
1208 return SCI_SUCCESS;
1211 static enum sci_status isci_remote_device_construct(struct isci_port *iport,
1212 struct isci_remote_device *idev)
1214 struct scic_sds_port *sci_port = &iport->sci;
1215 struct isci_host *ihost = iport->isci_host;
1216 struct domain_device *dev = idev->domain_dev;
1217 enum sci_status status;
1219 if (dev->parent && dev_is_expander(dev->parent))
1220 status = scic_remote_device_ea_construct(sci_port, &idev->sci);
1221 else
1222 status = scic_remote_device_da_construct(sci_port, &idev->sci);
1224 if (status != SCI_SUCCESS) {
1225 dev_dbg(&ihost->pdev->dev, "%s: construct failed: %d\n",
1226 __func__, status);
1228 return status;
1231 /* start the device. */
1232 status = scic_remote_device_start(&idev->sci, ISCI_REMOTE_DEVICE_START_TIMEOUT);
1234 if (status != SCI_SUCCESS)
1235 dev_warn(&ihost->pdev->dev, "remote device start failed: %d\n",
1236 status);
1238 return status;
1241 void isci_remote_device_nuke_requests(struct isci_host *ihost, struct isci_remote_device *idev)
1243 DECLARE_COMPLETION_ONSTACK(aborted_task_completion);
1245 dev_dbg(&ihost->pdev->dev,
1246 "%s: idev = %p\n", __func__, idev);
1248 /* Cleanup all requests pending for this device. */
1249 isci_terminate_pending_requests(ihost, idev);
1251 dev_dbg(&ihost->pdev->dev,
1252 "%s: idev = %p, done\n", __func__, idev);
1256 * This function builds the isci_remote_device when a libsas dev_found message
1257 * is received.
1258 * @isci_host: This parameter specifies the isci host object.
1259 * @port: This parameter specifies the isci_port conected to this device.
1261 * pointer to new isci_remote_device.
1263 static struct isci_remote_device *
1264 isci_remote_device_alloc(struct isci_host *ihost, struct isci_port *iport)
1266 struct isci_remote_device *idev;
1267 int i;
1269 for (i = 0; i < SCI_MAX_REMOTE_DEVICES; i++) {
1270 idev = &ihost->devices[i];
1271 if (!test_and_set_bit(IDEV_ALLOCATED, &idev->flags))
1272 break;
1275 if (i >= SCI_MAX_REMOTE_DEVICES) {
1276 dev_warn(&ihost->pdev->dev, "%s: failed\n", __func__);
1277 return NULL;
1280 if (WARN_ONCE(!list_empty(&idev->reqs_in_process), "found requests in process\n"))
1281 return NULL;
1283 if (WARN_ONCE(!list_empty(&idev->node), "found non-idle remote device\n"))
1284 return NULL;
1286 return idev;
1289 void isci_remote_device_release(struct kref *kref)
1291 struct isci_remote_device *idev = container_of(kref, typeof(*idev), kref);
1292 struct isci_host *ihost = idev->isci_port->isci_host;
1294 idev->domain_dev = NULL;
1295 idev->isci_port = NULL;
1296 clear_bit(IDEV_START_PENDING, &idev->flags);
1297 clear_bit(IDEV_STOP_PENDING, &idev->flags);
1298 clear_bit(IDEV_IO_READY, &idev->flags);
1299 clear_bit(IDEV_GONE, &idev->flags);
1300 clear_bit(IDEV_EH, &idev->flags);
1301 smp_mb__before_clear_bit();
1302 clear_bit(IDEV_ALLOCATED, &idev->flags);
1303 wake_up(&ihost->eventq);
1307 * isci_remote_device_stop() - This function is called internally to stop the
1308 * remote device.
1309 * @isci_host: This parameter specifies the isci host object.
1310 * @isci_device: This parameter specifies the remote device.
1312 * The status of the scic request to stop.
1314 enum sci_status isci_remote_device_stop(struct isci_host *ihost, struct isci_remote_device *idev)
1316 enum sci_status status;
1317 unsigned long flags;
1319 dev_dbg(&ihost->pdev->dev,
1320 "%s: isci_device = %p\n", __func__, idev);
1322 spin_lock_irqsave(&ihost->scic_lock, flags);
1323 idev->domain_dev->lldd_dev = NULL; /* disable new lookups */
1324 set_bit(IDEV_GONE, &idev->flags);
1325 spin_unlock_irqrestore(&ihost->scic_lock, flags);
1327 /* Kill all outstanding requests. */
1328 isci_remote_device_nuke_requests(ihost, idev);
1330 set_bit(IDEV_STOP_PENDING, &idev->flags);
1332 spin_lock_irqsave(&ihost->scic_lock, flags);
1333 status = scic_remote_device_stop(&idev->sci, 50);
1334 spin_unlock_irqrestore(&ihost->scic_lock, flags);
1336 /* Wait for the stop complete callback. */
1337 if (WARN_ONCE(status != SCI_SUCCESS, "failed to stop device\n"))
1338 /* nothing to wait for */;
1339 else
1340 wait_for_device_stop(ihost, idev);
1342 return status;
1346 * isci_remote_device_gone() - This function is called by libsas when a domain
1347 * device is removed.
1348 * @domain_device: This parameter specifies the libsas domain device.
1351 void isci_remote_device_gone(struct domain_device *dev)
1353 struct isci_host *ihost = dev_to_ihost(dev);
1354 struct isci_remote_device *idev = dev->lldd_dev;
1356 dev_dbg(&ihost->pdev->dev,
1357 "%s: domain_device = %p, isci_device = %p, isci_port = %p\n",
1358 __func__, dev, idev, idev->isci_port);
1360 isci_remote_device_stop(ihost, idev);
1365 * isci_remote_device_found() - This function is called by libsas when a remote
1366 * device is discovered. A remote device object is created and started. the
1367 * function then sleeps until the sci core device started message is
1368 * received.
1369 * @domain_device: This parameter specifies the libsas domain device.
1371 * status, zero indicates success.
1373 int isci_remote_device_found(struct domain_device *domain_dev)
1375 struct isci_host *isci_host = dev_to_ihost(domain_dev);
1376 struct isci_port *isci_port;
1377 struct isci_phy *isci_phy;
1378 struct asd_sas_port *sas_port;
1379 struct asd_sas_phy *sas_phy;
1380 struct isci_remote_device *isci_device;
1381 enum sci_status status;
1383 dev_dbg(&isci_host->pdev->dev,
1384 "%s: domain_device = %p\n", __func__, domain_dev);
1386 wait_for_start(isci_host);
1388 sas_port = domain_dev->port;
1389 sas_phy = list_first_entry(&sas_port->phy_list, struct asd_sas_phy,
1390 port_phy_el);
1391 isci_phy = to_isci_phy(sas_phy);
1392 isci_port = isci_phy->isci_port;
1394 /* we are being called for a device on this port,
1395 * so it has to come up eventually
1397 wait_for_completion(&isci_port->start_complete);
1399 if ((isci_stopping == isci_port_get_state(isci_port)) ||
1400 (isci_stopped == isci_port_get_state(isci_port)))
1401 return -ENODEV;
1403 isci_device = isci_remote_device_alloc(isci_host, isci_port);
1404 if (!isci_device)
1405 return -ENODEV;
1407 kref_init(&isci_device->kref);
1408 INIT_LIST_HEAD(&isci_device->node);
1410 spin_lock_irq(&isci_host->scic_lock);
1411 isci_device->domain_dev = domain_dev;
1412 isci_device->isci_port = isci_port;
1413 list_add_tail(&isci_device->node, &isci_port->remote_dev_list);
1415 set_bit(IDEV_START_PENDING, &isci_device->flags);
1416 status = isci_remote_device_construct(isci_port, isci_device);
1418 dev_dbg(&isci_host->pdev->dev,
1419 "%s: isci_device = %p\n",
1420 __func__, isci_device);
1422 if (status == SCI_SUCCESS) {
1423 /* device came up, advertise it to the world */
1424 domain_dev->lldd_dev = isci_device;
1425 } else
1426 isci_put_device(isci_device);
1427 spin_unlock_irq(&isci_host->scic_lock);
1429 /* wait for the device ready callback. */
1430 wait_for_device_start(isci_host, isci_device);
1432 return status == SCI_SUCCESS ? 0 : -ENODEV;
1435 * isci_device_is_reset_pending() - This function will check if there is any
1436 * pending reset condition on the device.
1437 * @request: This parameter is the isci_device object.
1439 * true if there is a reset pending for the device.
1441 bool isci_device_is_reset_pending(
1442 struct isci_host *isci_host,
1443 struct isci_remote_device *isci_device)
1445 struct isci_request *isci_request;
1446 struct isci_request *tmp_req;
1447 bool reset_is_pending = false;
1448 unsigned long flags;
1450 dev_dbg(&isci_host->pdev->dev,
1451 "%s: isci_device = %p\n", __func__, isci_device);
1453 spin_lock_irqsave(&isci_host->scic_lock, flags);
1455 /* Check for reset on all pending requests. */
1456 list_for_each_entry_safe(isci_request, tmp_req,
1457 &isci_device->reqs_in_process, dev_node) {
1458 dev_dbg(&isci_host->pdev->dev,
1459 "%s: isci_device = %p request = %p\n",
1460 __func__, isci_device, isci_request);
1462 if (isci_request->ttype == io_task) {
1463 struct sas_task *task = isci_request_access_task(
1464 isci_request);
1466 spin_lock(&task->task_state_lock);
1467 if (task->task_state_flags & SAS_TASK_NEED_DEV_RESET)
1468 reset_is_pending = true;
1469 spin_unlock(&task->task_state_lock);
1473 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1475 dev_dbg(&isci_host->pdev->dev,
1476 "%s: isci_device = %p reset_is_pending = %d\n",
1477 __func__, isci_device, reset_is_pending);
1479 return reset_is_pending;
1483 * isci_device_clear_reset_pending() - This function will clear if any pending
1484 * reset condition flags on the device.
1485 * @request: This parameter is the isci_device object.
1487 * true if there is a reset pending for the device.
1489 void isci_device_clear_reset_pending(struct isci_host *ihost, struct isci_remote_device *idev)
1491 struct isci_request *isci_request;
1492 struct isci_request *tmp_req;
1493 unsigned long flags = 0;
1495 dev_dbg(&ihost->pdev->dev, "%s: idev=%p, ihost=%p\n",
1496 __func__, idev, ihost);
1498 spin_lock_irqsave(&ihost->scic_lock, flags);
1500 /* Clear reset pending on all pending requests. */
1501 list_for_each_entry_safe(isci_request, tmp_req,
1502 &idev->reqs_in_process, dev_node) {
1503 dev_dbg(&ihost->pdev->dev, "%s: idev = %p request = %p\n",
1504 __func__, idev, isci_request);
1506 if (isci_request->ttype == io_task) {
1508 unsigned long flags2;
1509 struct sas_task *task = isci_request_access_task(
1510 isci_request);
1512 spin_lock_irqsave(&task->task_state_lock, flags2);
1513 task->task_state_flags &= ~SAS_TASK_NEED_DEV_RESET;
1514 spin_unlock_irqrestore(&task->task_state_lock, flags2);
1517 spin_unlock_irqrestore(&ihost->scic_lock, flags);