isci: uplevel phy infrastructure
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / scsi / isci / remote_device.c
blob2baa215cb27505bad75fd06a9f30f8c096a74cff
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 "scic_port.h"
61 #include "scic_sds_port.h"
62 #include "remote_node_context.h"
63 #include "scu_event_codes.h"
64 #include "task.h"
66 /**
67 * isci_remote_device_change_state() - This function gets the status of the
68 * remote_device object.
69 * @isci_device: This parameter points to the isci_remote_device object
71 * status of the object as a isci_status enum.
73 void isci_remote_device_change_state(
74 struct isci_remote_device *isci_device,
75 enum isci_status status)
77 unsigned long flags;
79 spin_lock_irqsave(&isci_device->state_lock, flags);
80 isci_device->status = status;
81 spin_unlock_irqrestore(&isci_device->state_lock, flags);
84 /**
85 * isci_remote_device_not_ready() - This function is called by the scic when
86 * the remote device is not ready. We mark the isci device as ready (not
87 * "ready_for_io") and signal the waiting proccess.
88 * @isci_host: This parameter specifies the isci host object.
89 * @isci_device: This parameter specifies the remote device
92 static void isci_remote_device_not_ready(struct isci_host *ihost,
93 struct isci_remote_device *idev, u32 reason)
95 dev_dbg(&ihost->pdev->dev,
96 "%s: isci_device = %p\n", __func__, idev);
98 if (reason == SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED)
99 isci_remote_device_change_state(idev, isci_stopping);
100 else
101 /* device ready is actually a "not ready for io" state. */
102 isci_remote_device_change_state(idev, isci_ready);
106 * isci_remote_device_ready() - This function is called by the scic when the
107 * remote device is ready. We mark the isci device as ready and signal the
108 * waiting proccess.
109 * @ihost: our valid isci_host
110 * @idev: remote device
113 static void isci_remote_device_ready(struct isci_host *ihost, struct isci_remote_device *idev)
115 dev_dbg(&ihost->pdev->dev,
116 "%s: idev = %p\n", __func__, idev);
118 isci_remote_device_change_state(idev, isci_ready_for_io);
119 if (test_and_clear_bit(IDEV_START_PENDING, &idev->flags))
120 wake_up(&ihost->eventq);
123 /* called once the remote node context is ready to be freed.
124 * The remote device can now report that its stop operation is complete. none
126 static void rnc_destruct_done(void *_dev)
128 struct scic_sds_remote_device *sci_dev = _dev;
130 BUG_ON(sci_dev->started_request_count != 0);
131 sci_base_state_machine_change_state(&sci_dev->state_machine,
132 SCI_BASE_REMOTE_DEVICE_STATE_STOPPED);
135 static enum sci_status scic_sds_remote_device_terminate_requests(struct scic_sds_remote_device *sci_dev)
137 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
138 u32 i, request_count = sci_dev->started_request_count;
139 enum sci_status status = SCI_SUCCESS;
141 for (i = 0; i < SCI_MAX_IO_REQUESTS && i < request_count; i++) {
142 struct scic_sds_request *sci_req;
143 enum sci_status s;
145 sci_req = scic->io_request_table[i];
146 if (!sci_req || sci_req->target_device != sci_dev)
147 continue;
148 s = scic_controller_terminate_request(scic, sci_dev, sci_req);
149 if (s != SCI_SUCCESS)
150 status = s;
153 return status;
156 enum sci_status scic_remote_device_stop(struct scic_sds_remote_device *sci_dev,
157 u32 timeout)
159 struct sci_base_state_machine *sm = &sci_dev->state_machine;
160 enum scic_sds_remote_device_states state = sm->current_state_id;
162 switch (state) {
163 case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL:
164 case SCI_BASE_REMOTE_DEVICE_STATE_FAILED:
165 case SCI_BASE_REMOTE_DEVICE_STATE_FINAL:
166 default:
167 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
168 __func__, state);
169 return SCI_FAILURE_INVALID_STATE;
170 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED:
171 return SCI_SUCCESS;
172 case SCI_BASE_REMOTE_DEVICE_STATE_STARTING:
173 /* device not started so there had better be no requests */
174 BUG_ON(sci_dev->started_request_count != 0);
175 scic_sds_remote_node_context_destruct(&sci_dev->rnc,
176 rnc_destruct_done, sci_dev);
177 /* Transition to the stopping state and wait for the
178 * remote node to complete being posted and invalidated.
180 sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_STOPPING);
181 return SCI_SUCCESS;
182 case SCI_BASE_REMOTE_DEVICE_STATE_READY:
183 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
184 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
185 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ:
186 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
187 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
188 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
189 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
190 sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_STOPPING);
191 if (sci_dev->started_request_count == 0) {
192 scic_sds_remote_node_context_destruct(&sci_dev->rnc,
193 rnc_destruct_done, sci_dev);
194 return SCI_SUCCESS;
195 } else
196 return scic_sds_remote_device_terminate_requests(sci_dev);
197 break;
198 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING:
199 /* All requests should have been terminated, but if there is an
200 * attempt to stop a device already in the stopping state, then
201 * try again to terminate.
203 return scic_sds_remote_device_terminate_requests(sci_dev);
204 case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING:
205 sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_STOPPING);
206 return SCI_SUCCESS;
210 enum sci_status scic_remote_device_reset(struct scic_sds_remote_device *sci_dev)
212 struct sci_base_state_machine *sm = &sci_dev->state_machine;
213 enum scic_sds_remote_device_states state = sm->current_state_id;
215 switch (state) {
216 case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL:
217 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED:
218 case SCI_BASE_REMOTE_DEVICE_STATE_STARTING:
219 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
220 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
221 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING:
222 case SCI_BASE_REMOTE_DEVICE_STATE_FAILED:
223 case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING:
224 case SCI_BASE_REMOTE_DEVICE_STATE_FINAL:
225 default:
226 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
227 __func__, state);
228 return SCI_FAILURE_INVALID_STATE;
229 case SCI_BASE_REMOTE_DEVICE_STATE_READY:
230 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
231 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
232 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ:
233 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
234 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
235 sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_RESETTING);
236 return SCI_SUCCESS;
240 enum sci_status scic_remote_device_reset_complete(struct scic_sds_remote_device *sci_dev)
242 struct sci_base_state_machine *sm = &sci_dev->state_machine;
243 enum scic_sds_remote_device_states state = sm->current_state_id;
245 if (state != SCI_BASE_REMOTE_DEVICE_STATE_RESETTING) {
246 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
247 __func__, state);
248 return SCI_FAILURE_INVALID_STATE;
251 sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_READY);
252 return SCI_SUCCESS;
255 enum sci_status scic_sds_remote_device_suspend(struct scic_sds_remote_device *sci_dev,
256 u32 suspend_type)
258 struct sci_base_state_machine *sm = &sci_dev->state_machine;
259 enum scic_sds_remote_device_states state = sm->current_state_id;
261 if (state != SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD) {
262 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
263 __func__, state);
264 return SCI_FAILURE_INVALID_STATE;
267 return scic_sds_remote_node_context_suspend(&sci_dev->rnc,
268 suspend_type, NULL, NULL);
271 enum sci_status scic_sds_remote_device_frame_handler(struct scic_sds_remote_device *sci_dev,
272 u32 frame_index)
274 struct sci_base_state_machine *sm = &sci_dev->state_machine;
275 enum scic_sds_remote_device_states state = sm->current_state_id;
276 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
277 enum sci_status status;
279 switch (state) {
280 case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL:
281 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED:
282 case SCI_BASE_REMOTE_DEVICE_STATE_STARTING:
283 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
284 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
285 case SCI_BASE_REMOTE_DEVICE_STATE_FINAL:
286 default:
287 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
288 __func__, state);
289 /* Return the frame back to the controller */
290 scic_sds_controller_release_frame(scic, frame_index);
291 return SCI_FAILURE_INVALID_STATE;
292 case SCI_BASE_REMOTE_DEVICE_STATE_READY:
293 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
294 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
295 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING:
296 case SCI_BASE_REMOTE_DEVICE_STATE_FAILED:
297 case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING: {
298 struct scic_sds_request *sci_req;
299 struct ssp_frame_hdr hdr;
300 void *frame_header;
301 ssize_t word_cnt;
303 status = scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
304 frame_index,
305 &frame_header);
306 if (status != SCI_SUCCESS)
307 return status;
309 word_cnt = sizeof(hdr) / sizeof(u32);
310 sci_swab32_cpy(&hdr, frame_header, word_cnt);
312 sci_req = scic_request_by_tag(scic, be16_to_cpu(hdr.tag));
313 if (sci_req && sci_req->target_device == sci_dev) {
314 /* The IO request is now in charge of releasing the frame */
315 status = sci_req->state_handlers->frame_handler(sci_req,
316 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 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_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_base_state_machine_change_state(sm, SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_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_base_state_machine_change_state(&sci_dev->state_machine,
348 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR);
349 } else
350 status = SCI_FAILURE;
352 scic_sds_controller_release_frame(scic, frame_index);
353 break;
355 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
356 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
357 /* The device does not process any UF received from the hardware while
358 * in this state. All unsolicited frames are forwarded to the io request
359 * object.
361 status = scic_sds_io_request_frame_handler(sci_dev->working_request, frame_index);
362 break;
365 return status;
368 static bool is_remote_device_ready(struct scic_sds_remote_device *sci_dev)
371 struct sci_base_state_machine *sm = &sci_dev->state_machine;
372 enum scic_sds_remote_device_states state = sm->current_state_id;
374 switch (state) {
375 case SCI_BASE_REMOTE_DEVICE_STATE_READY:
376 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
377 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
378 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ:
379 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
380 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
381 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
382 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
383 return true;
384 default:
385 return false;
389 enum sci_status scic_sds_remote_device_event_handler(struct scic_sds_remote_device *sci_dev,
390 u32 event_code)
392 struct sci_base_state_machine *sm = &sci_dev->state_machine;
393 enum scic_sds_remote_device_states state = sm->current_state_id;
394 enum sci_status status;
396 switch (scu_get_event_type(event_code)) {
397 case SCU_EVENT_TYPE_RNC_OPS_MISC:
398 case SCU_EVENT_TYPE_RNC_SUSPEND_TX:
399 case SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX:
400 status = scic_sds_remote_node_context_event_handler(&sci_dev->rnc, event_code);
401 break;
402 case SCU_EVENT_TYPE_PTX_SCHEDULE_EVENT:
403 if (scu_get_event_code(event_code) == SCU_EVENT_IT_NEXUS_TIMEOUT) {
404 status = SCI_SUCCESS;
406 /* Suspend the associated RNC */
407 scic_sds_remote_node_context_suspend(&sci_dev->rnc,
408 SCI_SOFTWARE_SUSPENSION,
409 NULL, NULL);
411 dev_dbg(scirdev_to_dev(sci_dev),
412 "%s: device: %p event code: %x: %s\n",
413 __func__, sci_dev, event_code,
414 is_remote_device_ready(sci_dev)
415 ? "I_T_Nexus_Timeout event"
416 : "I_T_Nexus_Timeout event in wrong state");
418 break;
420 /* Else, fall through and treat as unhandled... */
421 default:
422 dev_dbg(scirdev_to_dev(sci_dev),
423 "%s: device: %p event code: %x: %s\n",
424 __func__, sci_dev, event_code,
425 is_remote_device_ready(sci_dev)
426 ? "unexpected event"
427 : "unexpected event in wrong state");
428 status = SCI_FAILURE_INVALID_STATE;
429 break;
432 if (status != SCI_SUCCESS)
433 return status;
435 if (state == SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE) {
437 /* We pick up suspension events to handle specifically to this
438 * state. We resume the RNC right away.
440 if (scu_get_event_type(event_code) == SCU_EVENT_TYPE_RNC_SUSPEND_TX ||
441 scu_get_event_type(event_code) == SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX)
442 status = scic_sds_remote_node_context_resume(&sci_dev->rnc, NULL, NULL);
445 return status;
448 static void scic_sds_remote_device_start_request(struct scic_sds_remote_device *sci_dev,
449 struct scic_sds_request *sci_req,
450 enum sci_status status)
452 struct scic_sds_port *sci_port = sci_dev->owning_port;
454 /* cleanup requests that failed after starting on the port */
455 if (status != SCI_SUCCESS)
456 scic_sds_port_complete_io(sci_port, sci_dev, sci_req);
457 else
458 scic_sds_remote_device_increment_request_count(sci_dev);
461 enum sci_status scic_sds_remote_device_start_io(struct scic_sds_controller *scic,
462 struct scic_sds_remote_device *sci_dev,
463 struct scic_sds_request *sci_req)
465 struct sci_base_state_machine *sm = &sci_dev->state_machine;
466 enum scic_sds_remote_device_states state = sm->current_state_id;
467 struct scic_sds_port *sci_port = sci_dev->owning_port;
468 struct isci_request *ireq = sci_req_to_ireq(sci_req);
469 enum sci_status status;
471 switch (state) {
472 case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL:
473 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED:
474 case SCI_BASE_REMOTE_DEVICE_STATE_STARTING:
475 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
476 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING:
477 case SCI_BASE_REMOTE_DEVICE_STATE_FAILED:
478 case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING:
479 case SCI_BASE_REMOTE_DEVICE_STATE_FINAL:
480 default:
481 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
482 __func__, state);
483 return SCI_FAILURE_INVALID_STATE;
484 case SCI_BASE_REMOTE_DEVICE_STATE_READY:
485 /* attempt to start an io request for this device object. The remote
486 * device object will issue the start request for the io and if
487 * successful it will start the request for the port object then
488 * increment its own request count.
490 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
491 if (status != SCI_SUCCESS)
492 return status;
494 status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, sci_req);
495 if (status != SCI_SUCCESS)
496 break;
498 status = scic_sds_request_start(sci_req);
499 break;
500 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE: {
501 /* handle the start io operation for a sata device that is in
502 * the command idle state. - Evalute the type of IO request to
503 * be started - If its an NCQ request change to NCQ substate -
504 * If its any other command change to the CMD substate
506 * If this is a softreset we may want to have a different
507 * substate.
509 enum scic_sds_remote_device_states new_state;
510 struct sas_task *task = isci_request_access_task(ireq);
512 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
513 if (status != SCI_SUCCESS)
514 return status;
516 status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, sci_req);
517 if (status != SCI_SUCCESS)
518 break;
520 status = sci_req->state_handlers->start_handler(sci_req);
521 if (status != SCI_SUCCESS)
522 break;
524 if (task->ata_task.use_ncq)
525 new_state = SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ;
526 else {
527 sci_dev->working_request = sci_req;
528 new_state = SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD;
530 sci_base_state_machine_change_state(sm, new_state);
531 break;
533 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ: {
534 struct sas_task *task = isci_request_access_task(ireq);
536 if (task->ata_task.use_ncq) {
537 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
538 if (status != SCI_SUCCESS)
539 return status;
541 status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, sci_req);
542 if (status != SCI_SUCCESS)
543 break;
545 status = sci_req->state_handlers->start_handler(sci_req);
546 } else
547 return SCI_FAILURE_INVALID_STATE;
548 break;
550 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
551 return SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED;
552 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
553 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
554 if (status != SCI_SUCCESS)
555 return status;
557 status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, sci_req);
558 if (status != SCI_SUCCESS)
559 break;
561 status = scic_sds_request_start(sci_req);
562 if (status != SCI_SUCCESS)
563 break;
565 sci_dev->working_request = sci_req;
566 sci_base_state_machine_change_state(&sci_dev->state_machine,
567 SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD);
568 break;
569 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
570 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_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->state_machine;
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_BASE_REMOTE_DEVICE_STATE_INITIAL:
610 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED:
611 case SCI_BASE_REMOTE_DEVICE_STATE_STARTING:
612 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
613 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
614 case SCI_BASE_REMOTE_DEVICE_STATE_FAILED:
615 case SCI_BASE_REMOTE_DEVICE_STATE_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_BASE_REMOTE_DEVICE_STATE_READY:
621 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
622 case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING:
623 status = common_complete_io(sci_port, sci_dev, sci_req);
624 break;
625 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
626 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ:
627 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_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_base_state_machine_change_state(sm, SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET);
639 } else if (scic_sds_remote_device_get_request_count(sci_dev) == 0)
640 sci_base_state_machine_change_state(sm, SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
641 break;
642 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
643 status = common_complete_io(sci_port, sci_dev, sci_req);
644 if (status != SCI_SUCCESS)
645 break;
646 sci_base_state_machine_change_state(sm, SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
647 break;
648 case SCI_BASE_REMOTE_DEVICE_STATE_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);
666 return status;
669 static void scic_sds_remote_device_continue_request(void *dev)
671 struct scic_sds_remote_device *sci_dev = dev;
673 /* we need to check if this request is still valid to continue. */
674 if (sci_dev->working_request)
675 scic_controller_continue_io(sci_dev->working_request);
678 enum sci_status scic_sds_remote_device_start_task(struct scic_sds_controller *scic,
679 struct scic_sds_remote_device *sci_dev,
680 struct scic_sds_request *sci_req)
682 struct sci_base_state_machine *sm = &sci_dev->state_machine;
683 enum scic_sds_remote_device_states state = sm->current_state_id;
684 struct scic_sds_port *sci_port = sci_dev->owning_port;
685 enum sci_status status;
687 switch (state) {
688 case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL:
689 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED:
690 case SCI_BASE_REMOTE_DEVICE_STATE_STARTING:
691 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
692 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
693 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING:
694 case SCI_BASE_REMOTE_DEVICE_STATE_FAILED:
695 case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING:
696 case SCI_BASE_REMOTE_DEVICE_STATE_FINAL:
697 default:
698 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
699 __func__, state);
700 return SCI_FAILURE_INVALID_STATE;
701 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
702 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
703 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ:
704 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
705 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
706 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
707 if (status != SCI_SUCCESS)
708 return status;
710 status = scic_sds_remote_node_context_start_task(&sci_dev->rnc, sci_req);
711 if (status != SCI_SUCCESS)
712 goto out;
714 status = sci_req->state_handlers->start_handler(sci_req);
715 if (status != SCI_SUCCESS)
716 goto out;
718 /* Note: If the remote device state is not IDLE this will
719 * replace the request that probably resulted in the task
720 * management request.
722 sci_dev->working_request = sci_req;
723 sci_base_state_machine_change_state(sm, SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD);
725 /* The remote node context must cleanup the TCi to NCQ mapping
726 * table. The only way to do this correctly is to either write
727 * to the TLCR register or to invalidate and repost the RNC. In
728 * either case the remote node context state machine will take
729 * the correct action when the remote node context is suspended
730 * and later resumed.
732 scic_sds_remote_node_context_suspend(&sci_dev->rnc,
733 SCI_SOFTWARE_SUSPENSION, NULL, NULL);
734 scic_sds_remote_node_context_resume(&sci_dev->rnc,
735 scic_sds_remote_device_continue_request,
736 sci_dev);
738 out:
739 scic_sds_remote_device_start_request(sci_dev, sci_req, status);
740 /* We need to let the controller start request handler know that
741 * it can't post TC yet. We will provide a callback function to
742 * post TC when RNC gets resumed.
744 return SCI_FAILURE_RESET_DEVICE_PARTIAL_SUCCESS;
745 case SCI_BASE_REMOTE_DEVICE_STATE_READY:
746 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
747 if (status != SCI_SUCCESS)
748 return status;
750 status = scic_sds_remote_node_context_start_task(&sci_dev->rnc, sci_req);
751 if (status != SCI_SUCCESS)
752 break;
754 status = scic_sds_request_start(sci_req);
755 break;
757 scic_sds_remote_device_start_request(sci_dev, sci_req, status);
759 return status;
764 * @sci_dev:
765 * @request:
767 * This method takes the request and bulids an appropriate SCU context for the
768 * request and then requests the controller to post the request. none
770 void scic_sds_remote_device_post_request(
771 struct scic_sds_remote_device *sci_dev,
772 u32 request)
774 u32 context;
776 context = scic_sds_remote_device_build_command_context(sci_dev, request);
778 scic_sds_controller_post_request(
779 scic_sds_remote_device_get_controller(sci_dev),
780 context
784 /* called once the remote node context has transisitioned to a
785 * ready state. This is the indication that the remote device object can also
786 * transition to ready.
788 static void remote_device_resume_done(void *_dev)
790 struct scic_sds_remote_device *sci_dev = _dev;
792 if (is_remote_device_ready(sci_dev))
793 return;
795 /* go 'ready' if we are not already in a ready state */
796 sci_base_state_machine_change_state(&sci_dev->state_machine,
797 SCI_BASE_REMOTE_DEVICE_STATE_READY);
800 static void scic_sds_stp_remote_device_ready_idle_substate_resume_complete_handler(void *_dev)
802 struct scic_sds_remote_device *sci_dev = _dev;
803 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
804 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
806 /* For NCQ operation we do not issue a isci_remote_device_not_ready().
807 * As a result, avoid sending the ready notification.
809 if (sci_dev->state_machine.previous_state_id != SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ)
810 isci_remote_device_ready(scic_to_ihost(scic), idev);
813 static void scic_sds_remote_device_initial_state_enter(void *object)
815 struct scic_sds_remote_device *sci_dev = object;
817 /* Initial state is a transitional state to the stopped state */
818 sci_base_state_machine_change_state(&sci_dev->state_machine,
819 SCI_BASE_REMOTE_DEVICE_STATE_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->state_machine;
838 enum scic_sds_remote_device_states state = sm->current_state_id;
839 struct scic_sds_controller *scic;
841 if (state != SCI_BASE_REMOTE_DEVICE_STATE_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_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_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 while (!list_empty(&idev->reqs_in_process)) {
873 dev_err(&ihost->pdev->dev,
874 "%s: ** request list not empty! **\n", __func__);
875 BUG();
878 scic_remote_device_destruct(&idev->sci);
879 idev->domain_dev->lldd_dev = NULL;
880 idev->domain_dev = NULL;
881 idev->isci_port = NULL;
882 list_del_init(&idev->node);
884 clear_bit(IDEV_START_PENDING, &idev->flags);
885 clear_bit(IDEV_STOP_PENDING, &idev->flags);
886 clear_bit(IDEV_EH, &idev->flags);
887 wake_up(&ihost->eventq);
891 * isci_remote_device_stop_complete() - This function is called by the scic
892 * when the remote device stop has completed. We mark the isci device as not
893 * ready and remove the isci remote device.
894 * @ihost: This parameter specifies the isci host object.
895 * @idev: This parameter specifies the remote device.
896 * @status: This parameter specifies status of the completion.
899 static void isci_remote_device_stop_complete(struct isci_host *ihost,
900 struct isci_remote_device *idev)
902 dev_dbg(&ihost->pdev->dev, "%s: complete idev = %p\n", __func__, idev);
904 isci_remote_device_change_state(idev, isci_stopped);
906 /* after stop, we can tear down resources. */
907 isci_remote_device_deconstruct(ihost, idev);
910 static void scic_sds_remote_device_stopped_state_enter(void *object)
912 struct scic_sds_remote_device *sci_dev = object;
913 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
914 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
915 u32 prev_state;
917 /* If we are entering from the stopping state let the SCI User know that
918 * the stop operation has completed.
920 prev_state = sci_dev->state_machine.previous_state_id;
921 if (prev_state == SCI_BASE_REMOTE_DEVICE_STATE_STOPPING)
922 isci_remote_device_stop_complete(scic_to_ihost(scic), idev);
924 scic_sds_controller_remote_device_stopped(scic, sci_dev);
927 static void scic_sds_remote_device_starting_state_enter(void *object)
929 struct scic_sds_remote_device *sci_dev = object;
930 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
931 struct isci_host *ihost = scic_to_ihost(scic);
932 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
934 isci_remote_device_not_ready(ihost, idev,
935 SCIC_REMOTE_DEVICE_NOT_READY_START_REQUESTED);
938 static void scic_sds_remote_device_ready_state_enter(void *object)
940 struct scic_sds_remote_device *sci_dev = object;
941 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
942 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
943 struct domain_device *dev = idev->domain_dev;
945 scic->remote_device_sequence[sci_dev->rnc.remote_node_index]++;
947 if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_SATA)) {
948 sci_base_state_machine_change_state(&sci_dev->state_machine,
949 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
950 } else if (dev_is_expander(dev)) {
951 sci_base_state_machine_change_state(&sci_dev->state_machine,
952 SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
953 } else
954 isci_remote_device_ready(scic_to_ihost(scic), idev);
957 static void scic_sds_remote_device_ready_state_exit(void *object)
959 struct scic_sds_remote_device *sci_dev = object;
960 struct domain_device *dev = sci_dev_to_domain(sci_dev);
962 if (dev->dev_type == SAS_END_DEV) {
963 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
964 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
966 isci_remote_device_not_ready(scic_to_ihost(scic), idev,
967 SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED);
971 static void scic_sds_remote_device_resetting_state_enter(void *object)
973 struct scic_sds_remote_device *sci_dev = object;
975 scic_sds_remote_node_context_suspend(
976 &sci_dev->rnc, SCI_SOFTWARE_SUSPENSION, NULL, NULL);
979 static void scic_sds_remote_device_resetting_state_exit(void *object)
981 struct scic_sds_remote_device *sci_dev = object;
983 scic_sds_remote_node_context_resume(&sci_dev->rnc, NULL, NULL);
986 static void scic_sds_stp_remote_device_ready_idle_substate_enter(void *object)
988 struct scic_sds_remote_device *sci_dev = object;
990 sci_dev->working_request = NULL;
991 if (scic_sds_remote_node_context_is_ready(&sci_dev->rnc)) {
993 * Since the RNC is ready, it's alright to finish completion
994 * processing (e.g. signal the remote device is ready). */
995 scic_sds_stp_remote_device_ready_idle_substate_resume_complete_handler(sci_dev);
996 } else {
997 scic_sds_remote_node_context_resume(&sci_dev->rnc,
998 scic_sds_stp_remote_device_ready_idle_substate_resume_complete_handler,
999 sci_dev);
1003 static void scic_sds_stp_remote_device_ready_cmd_substate_enter(void *object)
1005 struct scic_sds_remote_device *sci_dev = object;
1006 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1008 BUG_ON(sci_dev->working_request == NULL);
1010 isci_remote_device_not_ready(scic_to_ihost(scic), sci_dev_to_idev(sci_dev),
1011 SCIC_REMOTE_DEVICE_NOT_READY_SATA_REQUEST_STARTED);
1014 static void scic_sds_stp_remote_device_ready_ncq_error_substate_enter(void *object)
1016 struct scic_sds_remote_device *sci_dev = object;
1017 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1018 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
1020 if (sci_dev->not_ready_reason == SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED)
1021 isci_remote_device_not_ready(scic_to_ihost(scic), idev,
1022 sci_dev->not_ready_reason);
1025 static void scic_sds_smp_remote_device_ready_idle_substate_enter(void *object)
1027 struct scic_sds_remote_device *sci_dev = object;
1028 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1030 isci_remote_device_ready(scic_to_ihost(scic), sci_dev_to_idev(sci_dev));
1033 static void scic_sds_smp_remote_device_ready_cmd_substate_enter(void *object)
1035 struct scic_sds_remote_device *sci_dev = object;
1036 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1038 BUG_ON(sci_dev->working_request == NULL);
1040 isci_remote_device_not_ready(scic_to_ihost(scic), sci_dev_to_idev(sci_dev),
1041 SCIC_REMOTE_DEVICE_NOT_READY_SMP_REQUEST_STARTED);
1044 static void scic_sds_smp_remote_device_ready_cmd_substate_exit(void *object)
1046 struct scic_sds_remote_device *sci_dev = object;
1048 sci_dev->working_request = NULL;
1051 static const struct sci_base_state scic_sds_remote_device_state_table[] = {
1052 [SCI_BASE_REMOTE_DEVICE_STATE_INITIAL] = {
1053 .enter_state = scic_sds_remote_device_initial_state_enter,
1055 [SCI_BASE_REMOTE_DEVICE_STATE_STOPPED] = {
1056 .enter_state = scic_sds_remote_device_stopped_state_enter,
1058 [SCI_BASE_REMOTE_DEVICE_STATE_STARTING] = {
1059 .enter_state = scic_sds_remote_device_starting_state_enter,
1061 [SCI_BASE_REMOTE_DEVICE_STATE_READY] = {
1062 .enter_state = scic_sds_remote_device_ready_state_enter,
1063 .exit_state = scic_sds_remote_device_ready_state_exit
1065 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE] = {
1066 .enter_state = scic_sds_stp_remote_device_ready_idle_substate_enter,
1068 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD] = {
1069 .enter_state = scic_sds_stp_remote_device_ready_cmd_substate_enter,
1071 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ] = { },
1072 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR] = {
1073 .enter_state = scic_sds_stp_remote_device_ready_ncq_error_substate_enter,
1075 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET] = { },
1076 [SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE] = {
1077 .enter_state = scic_sds_smp_remote_device_ready_idle_substate_enter,
1079 [SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD] = {
1080 .enter_state = scic_sds_smp_remote_device_ready_cmd_substate_enter,
1081 .exit_state = scic_sds_smp_remote_device_ready_cmd_substate_exit,
1083 [SCI_BASE_REMOTE_DEVICE_STATE_STOPPING] = { },
1084 [SCI_BASE_REMOTE_DEVICE_STATE_FAILED] = { },
1085 [SCI_BASE_REMOTE_DEVICE_STATE_RESETTING] = {
1086 .enter_state = scic_sds_remote_device_resetting_state_enter,
1087 .exit_state = scic_sds_remote_device_resetting_state_exit
1089 [SCI_BASE_REMOTE_DEVICE_STATE_FINAL] = { },
1093 * scic_remote_device_construct() - common construction
1094 * @sci_port: SAS/SATA port through which this device is accessed.
1095 * @sci_dev: remote device to construct
1097 * This routine just performs benign initialization and does not
1098 * allocate the remote_node_context which is left to
1099 * scic_remote_device_[de]a_construct(). scic_remote_device_destruct()
1100 * frees the remote_node_context(s) for the device.
1102 static void scic_remote_device_construct(struct scic_sds_port *sci_port,
1103 struct scic_sds_remote_device *sci_dev)
1105 sci_dev->owning_port = sci_port;
1106 sci_dev->started_request_count = 0;
1108 sci_base_state_machine_construct(
1109 &sci_dev->state_machine,
1110 sci_dev,
1111 scic_sds_remote_device_state_table,
1112 SCI_BASE_REMOTE_DEVICE_STATE_INITIAL
1115 sci_base_state_machine_start(
1116 &sci_dev->state_machine
1119 scic_sds_remote_node_context_construct(&sci_dev->rnc,
1120 SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX);
1124 * scic_remote_device_da_construct() - construct direct attached device.
1126 * The information (e.g. IAF, Signature FIS, etc.) necessary to build
1127 * the device is known to the SCI Core since it is contained in the
1128 * scic_phy object. Remote node context(s) is/are a global resource
1129 * allocated by this routine, freed by scic_remote_device_destruct().
1131 * Returns:
1132 * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
1133 * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
1134 * sata-only controller instance.
1135 * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
1137 static enum sci_status scic_remote_device_da_construct(struct scic_sds_port *sci_port,
1138 struct scic_sds_remote_device *sci_dev)
1140 enum sci_status status;
1141 struct domain_device *dev = sci_dev_to_domain(sci_dev);
1143 scic_remote_device_construct(sci_port, sci_dev);
1146 * This information is request to determine how many remote node context
1147 * entries will be needed to store the remote node.
1149 sci_dev->is_direct_attached = true;
1150 status = scic_sds_controller_allocate_remote_node_context(sci_port->owning_controller,
1151 sci_dev,
1152 &sci_dev->rnc.remote_node_index);
1154 if (status != SCI_SUCCESS)
1155 return status;
1157 if (dev->dev_type == SAS_END_DEV || dev->dev_type == SATA_DEV ||
1158 (dev->tproto & SAS_PROTOCOL_STP) || dev_is_expander(dev))
1159 /* pass */;
1160 else
1161 return SCI_FAILURE_UNSUPPORTED_PROTOCOL;
1163 sci_dev->connection_rate = scic_sds_port_get_max_allowed_speed(sci_port);
1165 /* / @todo Should I assign the port width by reading all of the phys on the port? */
1166 sci_dev->device_port_width = 1;
1168 return SCI_SUCCESS;
1172 * scic_remote_device_ea_construct() - construct expander attached device
1174 * Remote node context(s) is/are a global resource allocated by this
1175 * routine, freed by scic_remote_device_destruct().
1177 * Returns:
1178 * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
1179 * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
1180 * sata-only controller instance.
1181 * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
1183 static enum sci_status scic_remote_device_ea_construct(struct scic_sds_port *sci_port,
1184 struct scic_sds_remote_device *sci_dev)
1186 struct domain_device *dev = sci_dev_to_domain(sci_dev);
1187 enum sci_status status;
1189 scic_remote_device_construct(sci_port, sci_dev);
1191 status = scic_sds_controller_allocate_remote_node_context(sci_port->owning_controller,
1192 sci_dev,
1193 &sci_dev->rnc.remote_node_index);
1194 if (status != SCI_SUCCESS)
1195 return status;
1197 if (dev->dev_type == SAS_END_DEV || dev->dev_type == SATA_DEV ||
1198 (dev->tproto & SAS_PROTOCOL_STP) || dev_is_expander(dev))
1199 /* pass */;
1200 else
1201 return SCI_FAILURE_UNSUPPORTED_PROTOCOL;
1204 * For SAS-2 the physical link rate is actually a logical link
1205 * rate that incorporates multiplexing. The SCU doesn't
1206 * incorporate multiplexing and for the purposes of the
1207 * connection the logical link rate is that same as the
1208 * physical. Furthermore, the SAS-2 and SAS-1.1 fields overlay
1209 * one another, so this code works for both situations. */
1210 sci_dev->connection_rate = min_t(u16, scic_sds_port_get_max_allowed_speed(sci_port),
1211 dev->linkrate);
1213 /* / @todo Should I assign the port width by reading all of the phys on the port? */
1214 sci_dev->device_port_width = 1;
1216 return SCI_SUCCESS;
1220 * scic_remote_device_start() - This method will start the supplied remote
1221 * device. This method enables normal IO requests to flow through to the
1222 * remote device.
1223 * @remote_device: This parameter specifies the device to be started.
1224 * @timeout: This parameter specifies the number of milliseconds in which the
1225 * start operation should complete.
1227 * An indication of whether the device was successfully started. SCI_SUCCESS
1228 * This value is returned if the device was successfully started.
1229 * SCI_FAILURE_INVALID_PHY This value is returned if the user attempts to start
1230 * the device when there have been no phys added to it.
1232 static enum sci_status scic_remote_device_start(struct scic_sds_remote_device *sci_dev,
1233 u32 timeout)
1235 struct sci_base_state_machine *sm = &sci_dev->state_machine;
1236 enum scic_sds_remote_device_states state = sm->current_state_id;
1237 enum sci_status status;
1239 if (state != SCI_BASE_REMOTE_DEVICE_STATE_STOPPED) {
1240 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
1241 __func__, state);
1242 return SCI_FAILURE_INVALID_STATE;
1245 status = scic_sds_remote_node_context_resume(&sci_dev->rnc,
1246 remote_device_resume_done,
1247 sci_dev);
1248 if (status != SCI_SUCCESS)
1249 return status;
1251 sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_STARTING);
1253 return SCI_SUCCESS;
1256 static enum sci_status isci_remote_device_construct(struct isci_port *iport,
1257 struct isci_remote_device *idev)
1259 struct scic_sds_port *sci_port = &iport->sci;
1260 struct isci_host *ihost = iport->isci_host;
1261 struct domain_device *dev = idev->domain_dev;
1262 enum sci_status status;
1264 if (dev->parent && dev_is_expander(dev->parent))
1265 status = scic_remote_device_ea_construct(sci_port, &idev->sci);
1266 else
1267 status = scic_remote_device_da_construct(sci_port, &idev->sci);
1269 if (status != SCI_SUCCESS) {
1270 dev_dbg(&ihost->pdev->dev, "%s: construct failed: %d\n",
1271 __func__, status);
1273 return status;
1276 /* start the device. */
1277 status = scic_remote_device_start(&idev->sci, ISCI_REMOTE_DEVICE_START_TIMEOUT);
1279 if (status != SCI_SUCCESS)
1280 dev_warn(&ihost->pdev->dev, "remote device start failed: %d\n",
1281 status);
1283 return status;
1286 void isci_remote_device_nuke_requests(struct isci_host *ihost, struct isci_remote_device *idev)
1288 DECLARE_COMPLETION_ONSTACK(aborted_task_completion);
1290 dev_dbg(&ihost->pdev->dev,
1291 "%s: idev = %p\n", __func__, idev);
1293 /* Cleanup all requests pending for this device. */
1294 isci_terminate_pending_requests(ihost, idev, terminating);
1296 dev_dbg(&ihost->pdev->dev,
1297 "%s: idev = %p, done\n", __func__, idev);
1301 * This function builds the isci_remote_device when a libsas dev_found message
1302 * is received.
1303 * @isci_host: This parameter specifies the isci host object.
1304 * @port: This parameter specifies the isci_port conected to this device.
1306 * pointer to new isci_remote_device.
1308 static struct isci_remote_device *
1309 isci_remote_device_alloc(struct isci_host *ihost, struct isci_port *iport)
1311 struct isci_remote_device *idev;
1312 int i;
1314 for (i = 0; i < SCI_MAX_REMOTE_DEVICES; i++) {
1315 idev = &ihost->devices[i];
1316 if (!test_and_set_bit(IDEV_ALLOCATED, &idev->flags))
1317 break;
1320 if (i >= SCI_MAX_REMOTE_DEVICES) {
1321 dev_warn(&ihost->pdev->dev, "%s: failed\n", __func__);
1322 return NULL;
1325 if (WARN_ONCE(!list_empty(&idev->reqs_in_process), "found requests in process\n"))
1326 return NULL;
1328 if (WARN_ONCE(!list_empty(&idev->node), "found non-idle remote device\n"))
1329 return NULL;
1331 isci_remote_device_change_state(idev, isci_freed);
1333 return idev;
1337 * isci_remote_device_stop() - This function is called internally to stop the
1338 * remote device.
1339 * @isci_host: This parameter specifies the isci host object.
1340 * @isci_device: This parameter specifies the remote device.
1342 * The status of the scic request to stop.
1344 enum sci_status isci_remote_device_stop(struct isci_host *ihost, struct isci_remote_device *idev)
1346 enum sci_status status;
1347 unsigned long flags;
1349 dev_dbg(&ihost->pdev->dev,
1350 "%s: isci_device = %p\n", __func__, idev);
1352 isci_remote_device_change_state(idev, isci_stopping);
1354 /* Kill all outstanding requests. */
1355 isci_remote_device_nuke_requests(ihost, idev);
1357 set_bit(IDEV_STOP_PENDING, &idev->flags);
1359 spin_lock_irqsave(&ihost->scic_lock, flags);
1360 status = scic_remote_device_stop(&idev->sci, 50);
1361 spin_unlock_irqrestore(&ihost->scic_lock, flags);
1363 /* Wait for the stop complete callback. */
1364 if (status == SCI_SUCCESS) {
1365 wait_for_device_stop(ihost, idev);
1366 clear_bit(IDEV_ALLOCATED, &idev->flags);
1369 dev_dbg(&ihost->pdev->dev,
1370 "%s: idev = %p - after completion wait\n",
1371 __func__, idev);
1373 return status;
1377 * isci_remote_device_gone() - This function is called by libsas when a domain
1378 * device is removed.
1379 * @domain_device: This parameter specifies the libsas domain device.
1382 void isci_remote_device_gone(struct domain_device *dev)
1384 struct isci_host *ihost = dev_to_ihost(dev);
1385 struct isci_remote_device *idev = dev->lldd_dev;
1387 dev_dbg(&ihost->pdev->dev,
1388 "%s: domain_device = %p, isci_device = %p, isci_port = %p\n",
1389 __func__, dev, idev, idev->isci_port);
1391 isci_remote_device_stop(ihost, idev);
1396 * isci_remote_device_found() - This function is called by libsas when a remote
1397 * device is discovered. A remote device object is created and started. the
1398 * function then sleeps until the sci core device started message is
1399 * received.
1400 * @domain_device: This parameter specifies the libsas domain device.
1402 * status, zero indicates success.
1404 int isci_remote_device_found(struct domain_device *domain_dev)
1406 struct isci_host *isci_host = dev_to_ihost(domain_dev);
1407 struct isci_port *isci_port;
1408 struct isci_phy *isci_phy;
1409 struct asd_sas_port *sas_port;
1410 struct asd_sas_phy *sas_phy;
1411 struct isci_remote_device *isci_device;
1412 enum sci_status status;
1414 dev_dbg(&isci_host->pdev->dev,
1415 "%s: domain_device = %p\n", __func__, domain_dev);
1417 wait_for_start(isci_host);
1419 sas_port = domain_dev->port;
1420 sas_phy = list_first_entry(&sas_port->phy_list, struct asd_sas_phy,
1421 port_phy_el);
1422 isci_phy = to_isci_phy(sas_phy);
1423 isci_port = isci_phy->isci_port;
1425 /* we are being called for a device on this port,
1426 * so it has to come up eventually
1428 wait_for_completion(&isci_port->start_complete);
1430 if ((isci_stopping == isci_port_get_state(isci_port)) ||
1431 (isci_stopped == isci_port_get_state(isci_port)))
1432 return -ENODEV;
1434 isci_device = isci_remote_device_alloc(isci_host, isci_port);
1435 if (!isci_device)
1436 return -ENODEV;
1438 INIT_LIST_HEAD(&isci_device->node);
1439 domain_dev->lldd_dev = isci_device;
1440 isci_device->domain_dev = domain_dev;
1441 isci_device->isci_port = isci_port;
1442 isci_remote_device_change_state(isci_device, isci_starting);
1445 spin_lock_irq(&isci_host->scic_lock);
1446 list_add_tail(&isci_device->node, &isci_port->remote_dev_list);
1448 set_bit(IDEV_START_PENDING, &isci_device->flags);
1449 status = isci_remote_device_construct(isci_port, isci_device);
1450 spin_unlock_irq(&isci_host->scic_lock);
1452 dev_dbg(&isci_host->pdev->dev,
1453 "%s: isci_device = %p\n",
1454 __func__, isci_device);
1456 if (status != SCI_SUCCESS) {
1458 spin_lock_irq(&isci_host->scic_lock);
1459 isci_remote_device_deconstruct(
1460 isci_host,
1461 isci_device
1463 spin_unlock_irq(&isci_host->scic_lock);
1464 return -ENODEV;
1467 /* wait for the device ready callback. */
1468 wait_for_device_start(isci_host, isci_device);
1470 return 0;
1473 * isci_device_is_reset_pending() - This function will check if there is any
1474 * pending reset condition on the device.
1475 * @request: This parameter is the isci_device object.
1477 * true if there is a reset pending for the device.
1479 bool isci_device_is_reset_pending(
1480 struct isci_host *isci_host,
1481 struct isci_remote_device *isci_device)
1483 struct isci_request *isci_request;
1484 struct isci_request *tmp_req;
1485 bool reset_is_pending = false;
1486 unsigned long flags;
1488 dev_dbg(&isci_host->pdev->dev,
1489 "%s: isci_device = %p\n", __func__, isci_device);
1491 spin_lock_irqsave(&isci_host->scic_lock, flags);
1493 /* Check for reset on all pending requests. */
1494 list_for_each_entry_safe(isci_request, tmp_req,
1495 &isci_device->reqs_in_process, dev_node) {
1496 dev_dbg(&isci_host->pdev->dev,
1497 "%s: isci_device = %p request = %p\n",
1498 __func__, isci_device, isci_request);
1500 if (isci_request->ttype == io_task) {
1501 struct sas_task *task = isci_request_access_task(
1502 isci_request);
1504 spin_lock(&task->task_state_lock);
1505 if (task->task_state_flags & SAS_TASK_NEED_DEV_RESET)
1506 reset_is_pending = true;
1507 spin_unlock(&task->task_state_lock);
1511 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1513 dev_dbg(&isci_host->pdev->dev,
1514 "%s: isci_device = %p reset_is_pending = %d\n",
1515 __func__, isci_device, reset_is_pending);
1517 return reset_is_pending;
1521 * isci_device_clear_reset_pending() - This function will clear if any pending
1522 * reset condition flags on the device.
1523 * @request: This parameter is the isci_device object.
1525 * true if there is a reset pending for the device.
1527 void isci_device_clear_reset_pending(struct isci_host *ihost, struct isci_remote_device *idev)
1529 struct isci_request *isci_request;
1530 struct isci_request *tmp_req;
1531 unsigned long flags = 0;
1533 dev_dbg(&ihost->pdev->dev, "%s: idev=%p, ihost=%p\n",
1534 __func__, idev, ihost);
1536 spin_lock_irqsave(&ihost->scic_lock, flags);
1538 /* Clear reset pending on all pending requests. */
1539 list_for_each_entry_safe(isci_request, tmp_req,
1540 &idev->reqs_in_process, dev_node) {
1541 dev_dbg(&ihost->pdev->dev, "%s: idev = %p request = %p\n",
1542 __func__, idev, isci_request);
1544 if (isci_request->ttype == io_task) {
1546 unsigned long flags2;
1547 struct sas_task *task = isci_request_access_task(
1548 isci_request);
1550 spin_lock_irqsave(&task->task_state_lock, flags2);
1551 task->task_state_flags &= ~SAS_TASK_NEED_DEV_RESET;
1552 spin_unlock_irqrestore(&task->task_state_lock, flags2);
1555 spin_unlock_irqrestore(&ihost->scic_lock, flags);