isci: unify request frame handlers
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / scsi / isci / remote_device.c
blobb900e2c1b63ea6831ac7a2adc2a12bbe216bb161
1 /*
2 * This file is provided under a dual BSD/GPLv2 license. When using or
3 * redistributing this file, you may do so under either license.
5 * GPL LICENSE SUMMARY
7 * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of version 2 of the GNU General Public License as
11 * published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
21 * The full GNU General Public License is included in this distribution
22 * in the file called LICENSE.GPL.
24 * BSD LICENSE
26 * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
27 * All rights reserved.
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
31 * are met:
33 * * Redistributions of source code must retain the above copyright
34 * notice, this list of conditions and the following disclaimer.
35 * * Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer in
37 * the documentation and/or other materials provided with the
38 * distribution.
39 * * Neither the name of Intel Corporation nor the names of its
40 * contributors may be used to endorse or promote products derived
41 * from this software without specific prior written permission.
43 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
44 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
45 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
46 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
47 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
48 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
49 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
50 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
51 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
52 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
53 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
55 #include <scsi/sas.h>
56 #include "isci.h"
57 #include "port.h"
58 #include "remote_device.h"
59 #include "request.h"
60 #include "remote_node_context.h"
61 #include "scu_event_codes.h"
62 #include "task.h"
64 /**
65 * isci_remote_device_change_state() - This function gets the status of the
66 * remote_device object.
67 * @isci_device: This parameter points to the isci_remote_device object
69 * status of the object as a isci_status enum.
71 void isci_remote_device_change_state(
72 struct isci_remote_device *isci_device,
73 enum isci_status status)
75 unsigned long flags;
77 spin_lock_irqsave(&isci_device->state_lock, flags);
78 isci_device->status = status;
79 spin_unlock_irqrestore(&isci_device->state_lock, flags);
82 /**
83 * isci_remote_device_not_ready() - This function is called by the scic when
84 * the remote device is not ready. We mark the isci device as ready (not
85 * "ready_for_io") and signal the waiting proccess.
86 * @isci_host: This parameter specifies the isci host object.
87 * @isci_device: This parameter specifies the remote device
90 static void isci_remote_device_not_ready(struct isci_host *ihost,
91 struct isci_remote_device *idev, u32 reason)
93 dev_dbg(&ihost->pdev->dev,
94 "%s: isci_device = %p\n", __func__, idev);
96 if (reason == SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED)
97 isci_remote_device_change_state(idev, isci_stopping);
98 else
99 /* device ready is actually a "not ready for io" state. */
100 isci_remote_device_change_state(idev, isci_ready);
104 * isci_remote_device_ready() - This function is called by the scic when the
105 * remote device is ready. We mark the isci device as ready and signal the
106 * waiting proccess.
107 * @ihost: our valid isci_host
108 * @idev: remote device
111 static void isci_remote_device_ready(struct isci_host *ihost, struct isci_remote_device *idev)
113 dev_dbg(&ihost->pdev->dev,
114 "%s: idev = %p\n", __func__, idev);
116 isci_remote_device_change_state(idev, isci_ready_for_io);
117 if (test_and_clear_bit(IDEV_START_PENDING, &idev->flags))
118 wake_up(&ihost->eventq);
121 /* called once the remote node context is ready to be freed.
122 * The remote device can now report that its stop operation is complete. none
124 static void rnc_destruct_done(void *_dev)
126 struct scic_sds_remote_device *sci_dev = _dev;
128 BUG_ON(sci_dev->started_request_count != 0);
129 sci_base_state_machine_change_state(&sci_dev->state_machine,
130 SCI_BASE_REMOTE_DEVICE_STATE_STOPPED);
133 static enum sci_status scic_sds_remote_device_terminate_requests(struct scic_sds_remote_device *sci_dev)
135 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
136 u32 i, request_count = sci_dev->started_request_count;
137 enum sci_status status = SCI_SUCCESS;
139 for (i = 0; i < SCI_MAX_IO_REQUESTS && i < request_count; i++) {
140 struct scic_sds_request *sci_req;
141 enum sci_status s;
143 sci_req = scic->io_request_table[i];
144 if (!sci_req || sci_req->target_device != sci_dev)
145 continue;
146 s = scic_controller_terminate_request(scic, sci_dev, sci_req);
147 if (s != SCI_SUCCESS)
148 status = s;
151 return status;
154 enum sci_status scic_remote_device_stop(struct scic_sds_remote_device *sci_dev,
155 u32 timeout)
157 struct sci_base_state_machine *sm = &sci_dev->state_machine;
158 enum scic_sds_remote_device_states state = sm->current_state_id;
160 switch (state) {
161 case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL:
162 case SCI_BASE_REMOTE_DEVICE_STATE_FAILED:
163 case SCI_BASE_REMOTE_DEVICE_STATE_FINAL:
164 default:
165 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
166 __func__, state);
167 return SCI_FAILURE_INVALID_STATE;
168 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED:
169 return SCI_SUCCESS;
170 case SCI_BASE_REMOTE_DEVICE_STATE_STARTING:
171 /* device not started so there had better be no requests */
172 BUG_ON(sci_dev->started_request_count != 0);
173 scic_sds_remote_node_context_destruct(&sci_dev->rnc,
174 rnc_destruct_done, sci_dev);
175 /* Transition to the stopping state and wait for the
176 * remote node to complete being posted and invalidated.
178 sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_STOPPING);
179 return SCI_SUCCESS;
180 case SCI_BASE_REMOTE_DEVICE_STATE_READY:
181 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
182 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
183 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ:
184 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
185 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
186 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
187 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
188 sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_STOPPING);
189 if (sci_dev->started_request_count == 0) {
190 scic_sds_remote_node_context_destruct(&sci_dev->rnc,
191 rnc_destruct_done, sci_dev);
192 return SCI_SUCCESS;
193 } else
194 return scic_sds_remote_device_terminate_requests(sci_dev);
195 break;
196 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING:
197 /* All requests should have been terminated, but if there is an
198 * attempt to stop a device already in the stopping state, then
199 * try again to terminate.
201 return scic_sds_remote_device_terminate_requests(sci_dev);
202 case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING:
203 sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_STOPPING);
204 return SCI_SUCCESS;
208 enum sci_status scic_remote_device_reset(struct scic_sds_remote_device *sci_dev)
210 struct sci_base_state_machine *sm = &sci_dev->state_machine;
211 enum scic_sds_remote_device_states state = sm->current_state_id;
213 switch (state) {
214 case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL:
215 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED:
216 case SCI_BASE_REMOTE_DEVICE_STATE_STARTING:
217 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
218 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
219 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING:
220 case SCI_BASE_REMOTE_DEVICE_STATE_FAILED:
221 case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING:
222 case SCI_BASE_REMOTE_DEVICE_STATE_FINAL:
223 default:
224 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
225 __func__, state);
226 return SCI_FAILURE_INVALID_STATE;
227 case SCI_BASE_REMOTE_DEVICE_STATE_READY:
228 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
229 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
230 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ:
231 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
232 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
233 sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_RESETTING);
234 return SCI_SUCCESS;
238 enum sci_status scic_remote_device_reset_complete(struct scic_sds_remote_device *sci_dev)
240 struct sci_base_state_machine *sm = &sci_dev->state_machine;
241 enum scic_sds_remote_device_states state = sm->current_state_id;
243 if (state != SCI_BASE_REMOTE_DEVICE_STATE_RESETTING) {
244 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
245 __func__, state);
246 return SCI_FAILURE_INVALID_STATE;
249 sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_READY);
250 return SCI_SUCCESS;
253 enum sci_status scic_sds_remote_device_suspend(struct scic_sds_remote_device *sci_dev,
254 u32 suspend_type)
256 struct sci_base_state_machine *sm = &sci_dev->state_machine;
257 enum scic_sds_remote_device_states state = sm->current_state_id;
259 if (state != SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD) {
260 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
261 __func__, state);
262 return SCI_FAILURE_INVALID_STATE;
265 return scic_sds_remote_node_context_suspend(&sci_dev->rnc,
266 suspend_type, NULL, NULL);
269 enum sci_status scic_sds_remote_device_frame_handler(struct scic_sds_remote_device *sci_dev,
270 u32 frame_index)
272 struct sci_base_state_machine *sm = &sci_dev->state_machine;
273 enum scic_sds_remote_device_states state = sm->current_state_id;
274 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
275 enum sci_status status;
277 switch (state) {
278 case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL:
279 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED:
280 case SCI_BASE_REMOTE_DEVICE_STATE_STARTING:
281 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
282 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
283 case SCI_BASE_REMOTE_DEVICE_STATE_FINAL:
284 default:
285 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
286 __func__, state);
287 /* Return the frame back to the controller */
288 scic_sds_controller_release_frame(scic, frame_index);
289 return SCI_FAILURE_INVALID_STATE;
290 case SCI_BASE_REMOTE_DEVICE_STATE_READY:
291 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
292 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
293 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING:
294 case SCI_BASE_REMOTE_DEVICE_STATE_FAILED:
295 case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING: {
296 struct scic_sds_request *sci_req;
297 struct ssp_frame_hdr hdr;
298 void *frame_header;
299 ssize_t word_cnt;
301 status = scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
302 frame_index,
303 &frame_header);
304 if (status != SCI_SUCCESS)
305 return status;
307 word_cnt = sizeof(hdr) / sizeof(u32);
308 sci_swab32_cpy(&hdr, frame_header, word_cnt);
310 sci_req = scic_request_by_tag(scic, be16_to_cpu(hdr.tag));
311 if (sci_req && sci_req->target_device == sci_dev) {
312 /* The IO request is now in charge of releasing the frame */
313 status = scic_sds_io_request_frame_handler(sci_req, frame_index);
314 } else {
315 /* We could not map this tag to a valid IO
316 * request Just toss the frame and continue
318 scic_sds_controller_release_frame(scic, frame_index);
320 break;
322 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ: {
323 struct dev_to_host_fis *hdr;
325 status = scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
326 frame_index,
327 (void **)&hdr);
328 if (status != SCI_SUCCESS)
329 return status;
331 if (hdr->fis_type == FIS_SETDEVBITS &&
332 (hdr->status & ATA_ERR)) {
333 sci_dev->not_ready_reason = SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED;
335 /* TODO Check sactive and complete associated IO if any. */
336 sci_base_state_machine_change_state(sm, SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR);
337 } else if (hdr->fis_type == FIS_REGD2H &&
338 (hdr->status & ATA_ERR)) {
340 * Some devices return D2H FIS when an NCQ error is detected.
341 * Treat this like an SDB error FIS ready reason.
343 sci_dev->not_ready_reason = SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED;
344 sci_base_state_machine_change_state(&sci_dev->state_machine,
345 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR);
346 } else
347 status = SCI_FAILURE;
349 scic_sds_controller_release_frame(scic, frame_index);
350 break;
352 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
353 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
354 /* The device does not process any UF received from the hardware while
355 * in this state. All unsolicited frames are forwarded to the io request
356 * object.
358 status = scic_sds_io_request_frame_handler(sci_dev->working_request, frame_index);
359 break;
362 return status;
365 static bool is_remote_device_ready(struct scic_sds_remote_device *sci_dev)
368 struct sci_base_state_machine *sm = &sci_dev->state_machine;
369 enum scic_sds_remote_device_states state = sm->current_state_id;
371 switch (state) {
372 case SCI_BASE_REMOTE_DEVICE_STATE_READY:
373 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
374 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
375 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ:
376 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
377 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
378 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
379 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
380 return true;
381 default:
382 return false;
386 enum sci_status scic_sds_remote_device_event_handler(struct scic_sds_remote_device *sci_dev,
387 u32 event_code)
389 struct sci_base_state_machine *sm = &sci_dev->state_machine;
390 enum scic_sds_remote_device_states state = sm->current_state_id;
391 enum sci_status status;
393 switch (scu_get_event_type(event_code)) {
394 case SCU_EVENT_TYPE_RNC_OPS_MISC:
395 case SCU_EVENT_TYPE_RNC_SUSPEND_TX:
396 case SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX:
397 status = scic_sds_remote_node_context_event_handler(&sci_dev->rnc, event_code);
398 break;
399 case SCU_EVENT_TYPE_PTX_SCHEDULE_EVENT:
400 if (scu_get_event_code(event_code) == SCU_EVENT_IT_NEXUS_TIMEOUT) {
401 status = SCI_SUCCESS;
403 /* Suspend the associated RNC */
404 scic_sds_remote_node_context_suspend(&sci_dev->rnc,
405 SCI_SOFTWARE_SUSPENSION,
406 NULL, NULL);
408 dev_dbg(scirdev_to_dev(sci_dev),
409 "%s: device: %p event code: %x: %s\n",
410 __func__, sci_dev, event_code,
411 is_remote_device_ready(sci_dev)
412 ? "I_T_Nexus_Timeout event"
413 : "I_T_Nexus_Timeout event in wrong state");
415 break;
417 /* Else, fall through and treat as unhandled... */
418 default:
419 dev_dbg(scirdev_to_dev(sci_dev),
420 "%s: device: %p event code: %x: %s\n",
421 __func__, sci_dev, event_code,
422 is_remote_device_ready(sci_dev)
423 ? "unexpected event"
424 : "unexpected event in wrong state");
425 status = SCI_FAILURE_INVALID_STATE;
426 break;
429 if (status != SCI_SUCCESS)
430 return status;
432 if (state == SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE) {
434 /* We pick up suspension events to handle specifically to this
435 * state. We resume the RNC right away.
437 if (scu_get_event_type(event_code) == SCU_EVENT_TYPE_RNC_SUSPEND_TX ||
438 scu_get_event_type(event_code) == SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX)
439 status = scic_sds_remote_node_context_resume(&sci_dev->rnc, NULL, NULL);
442 return status;
445 static void scic_sds_remote_device_start_request(struct scic_sds_remote_device *sci_dev,
446 struct scic_sds_request *sci_req,
447 enum sci_status status)
449 struct scic_sds_port *sci_port = sci_dev->owning_port;
451 /* cleanup requests that failed after starting on the port */
452 if (status != SCI_SUCCESS)
453 scic_sds_port_complete_io(sci_port, sci_dev, sci_req);
454 else
455 scic_sds_remote_device_increment_request_count(sci_dev);
458 enum sci_status scic_sds_remote_device_start_io(struct scic_sds_controller *scic,
459 struct scic_sds_remote_device *sci_dev,
460 struct scic_sds_request *sci_req)
462 struct sci_base_state_machine *sm = &sci_dev->state_machine;
463 enum scic_sds_remote_device_states state = sm->current_state_id;
464 struct scic_sds_port *sci_port = sci_dev->owning_port;
465 struct isci_request *ireq = sci_req_to_ireq(sci_req);
466 enum sci_status status;
468 switch (state) {
469 case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL:
470 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED:
471 case SCI_BASE_REMOTE_DEVICE_STATE_STARTING:
472 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
473 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING:
474 case SCI_BASE_REMOTE_DEVICE_STATE_FAILED:
475 case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING:
476 case SCI_BASE_REMOTE_DEVICE_STATE_FINAL:
477 default:
478 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
479 __func__, state);
480 return SCI_FAILURE_INVALID_STATE;
481 case SCI_BASE_REMOTE_DEVICE_STATE_READY:
482 /* attempt to start an io request for this device object. The remote
483 * device object will issue the start request for the io and if
484 * successful it will start the request for the port object then
485 * increment its own request count.
487 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
488 if (status != SCI_SUCCESS)
489 return status;
491 status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, sci_req);
492 if (status != SCI_SUCCESS)
493 break;
495 status = scic_sds_request_start(sci_req);
496 break;
497 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE: {
498 /* handle the start io operation for a sata device that is in
499 * the command idle state. - Evalute the type of IO request to
500 * be started - If its an NCQ request change to NCQ substate -
501 * If its any other command change to the CMD substate
503 * If this is a softreset we may want to have a different
504 * substate.
506 enum scic_sds_remote_device_states new_state;
507 struct sas_task *task = isci_request_access_task(ireq);
509 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
510 if (status != SCI_SUCCESS)
511 return status;
513 status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, sci_req);
514 if (status != SCI_SUCCESS)
515 break;
517 status = scic_sds_request_start(sci_req);
518 if (status != SCI_SUCCESS)
519 break;
521 if (task->ata_task.use_ncq)
522 new_state = SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ;
523 else {
524 sci_dev->working_request = sci_req;
525 new_state = SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD;
527 sci_base_state_machine_change_state(sm, new_state);
528 break;
530 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ: {
531 struct sas_task *task = isci_request_access_task(ireq);
533 if (task->ata_task.use_ncq) {
534 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
535 if (status != SCI_SUCCESS)
536 return status;
538 status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, sci_req);
539 if (status != SCI_SUCCESS)
540 break;
542 status = scic_sds_request_start(sci_req);
543 } else
544 return SCI_FAILURE_INVALID_STATE;
545 break;
547 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
548 return SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED;
549 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
550 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
551 if (status != SCI_SUCCESS)
552 return status;
554 status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, sci_req);
555 if (status != SCI_SUCCESS)
556 break;
558 status = scic_sds_request_start(sci_req);
559 if (status != SCI_SUCCESS)
560 break;
562 sci_dev->working_request = sci_req;
563 sci_base_state_machine_change_state(&sci_dev->state_machine,
564 SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD);
565 break;
566 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
567 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
568 /* device is already handling a command it can not accept new commands
569 * until this one is complete.
571 return SCI_FAILURE_INVALID_STATE;
574 scic_sds_remote_device_start_request(sci_dev, sci_req, status);
575 return status;
578 static enum sci_status common_complete_io(struct scic_sds_port *sci_port,
579 struct scic_sds_remote_device *sci_dev,
580 struct scic_sds_request *sci_req)
582 enum sci_status status;
584 status = scic_sds_request_complete(sci_req);
585 if (status != SCI_SUCCESS)
586 return status;
588 status = scic_sds_port_complete_io(sci_port, sci_dev, sci_req);
589 if (status != SCI_SUCCESS)
590 return status;
592 scic_sds_remote_device_decrement_request_count(sci_dev);
593 return status;
596 enum sci_status scic_sds_remote_device_complete_io(struct scic_sds_controller *scic,
597 struct scic_sds_remote_device *sci_dev,
598 struct scic_sds_request *sci_req)
600 struct sci_base_state_machine *sm = &sci_dev->state_machine;
601 enum scic_sds_remote_device_states state = sm->current_state_id;
602 struct scic_sds_port *sci_port = sci_dev->owning_port;
603 enum sci_status status;
605 switch (state) {
606 case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL:
607 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED:
608 case SCI_BASE_REMOTE_DEVICE_STATE_STARTING:
609 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
610 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
611 case SCI_BASE_REMOTE_DEVICE_STATE_FAILED:
612 case SCI_BASE_REMOTE_DEVICE_STATE_FINAL:
613 default:
614 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
615 __func__, state);
616 return SCI_FAILURE_INVALID_STATE;
617 case SCI_BASE_REMOTE_DEVICE_STATE_READY:
618 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
619 case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING:
620 status = common_complete_io(sci_port, sci_dev, sci_req);
621 break;
622 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
623 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ:
624 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
625 status = common_complete_io(sci_port, sci_dev, sci_req);
626 if (status != SCI_SUCCESS)
627 break;
629 if (sci_req->sci_status == SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED) {
630 /* This request causes hardware error, device needs to be Lun Reset.
631 * So here we force the state machine to IDLE state so the rest IOs
632 * can reach RNC state handler, these IOs will be completed by RNC with
633 * status of "DEVICE_RESET_REQUIRED", instead of "INVALID STATE".
635 sci_base_state_machine_change_state(sm, SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET);
636 } else if (scic_sds_remote_device_get_request_count(sci_dev) == 0)
637 sci_base_state_machine_change_state(sm, SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
638 break;
639 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
640 status = common_complete_io(sci_port, sci_dev, sci_req);
641 if (status != SCI_SUCCESS)
642 break;
643 sci_base_state_machine_change_state(sm, SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
644 break;
645 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING:
646 status = common_complete_io(sci_port, sci_dev, sci_req);
647 if (status != SCI_SUCCESS)
648 break;
650 if (scic_sds_remote_device_get_request_count(sci_dev) == 0)
651 scic_sds_remote_node_context_destruct(&sci_dev->rnc,
652 rnc_destruct_done,
653 sci_dev);
654 break;
657 if (status != SCI_SUCCESS)
658 dev_err(scirdev_to_dev(sci_dev),
659 "%s: Port:0x%p Device:0x%p Request:0x%p Status:0x%x "
660 "could not complete\n", __func__, sci_port,
661 sci_dev, sci_req, status);
663 return status;
666 static void scic_sds_remote_device_continue_request(void *dev)
668 struct scic_sds_remote_device *sci_dev = dev;
670 /* we need to check if this request is still valid to continue. */
671 if (sci_dev->working_request)
672 scic_controller_continue_io(sci_dev->working_request);
675 enum sci_status scic_sds_remote_device_start_task(struct scic_sds_controller *scic,
676 struct scic_sds_remote_device *sci_dev,
677 struct scic_sds_request *sci_req)
679 struct sci_base_state_machine *sm = &sci_dev->state_machine;
680 enum scic_sds_remote_device_states state = sm->current_state_id;
681 struct scic_sds_port *sci_port = sci_dev->owning_port;
682 enum sci_status status;
684 switch (state) {
685 case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL:
686 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED:
687 case SCI_BASE_REMOTE_DEVICE_STATE_STARTING:
688 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
689 case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
690 case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING:
691 case SCI_BASE_REMOTE_DEVICE_STATE_FAILED:
692 case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING:
693 case SCI_BASE_REMOTE_DEVICE_STATE_FINAL:
694 default:
695 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
696 __func__, state);
697 return SCI_FAILURE_INVALID_STATE;
698 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
699 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
700 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ:
701 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
702 case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
703 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
704 if (status != SCI_SUCCESS)
705 return status;
707 status = scic_sds_remote_node_context_start_task(&sci_dev->rnc, sci_req);
708 if (status != SCI_SUCCESS)
709 goto out;
711 status = scic_sds_request_start(sci_req);
712 if (status != SCI_SUCCESS)
713 goto out;
715 /* Note: If the remote device state is not IDLE this will
716 * replace the request that probably resulted in the task
717 * management request.
719 sci_dev->working_request = sci_req;
720 sci_base_state_machine_change_state(sm, SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD);
722 /* The remote node context must cleanup the TCi to NCQ mapping
723 * table. The only way to do this correctly is to either write
724 * to the TLCR register or to invalidate and repost the RNC. In
725 * either case the remote node context state machine will take
726 * the correct action when the remote node context is suspended
727 * and later resumed.
729 scic_sds_remote_node_context_suspend(&sci_dev->rnc,
730 SCI_SOFTWARE_SUSPENSION, NULL, NULL);
731 scic_sds_remote_node_context_resume(&sci_dev->rnc,
732 scic_sds_remote_device_continue_request,
733 sci_dev);
735 out:
736 scic_sds_remote_device_start_request(sci_dev, sci_req, status);
737 /* We need to let the controller start request handler know that
738 * it can't post TC yet. We will provide a callback function to
739 * post TC when RNC gets resumed.
741 return SCI_FAILURE_RESET_DEVICE_PARTIAL_SUCCESS;
742 case SCI_BASE_REMOTE_DEVICE_STATE_READY:
743 status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
744 if (status != SCI_SUCCESS)
745 return status;
747 status = scic_sds_remote_node_context_start_task(&sci_dev->rnc, sci_req);
748 if (status != SCI_SUCCESS)
749 break;
751 status = scic_sds_request_start(sci_req);
752 break;
754 scic_sds_remote_device_start_request(sci_dev, sci_req, status);
756 return status;
761 * @sci_dev:
762 * @request:
764 * This method takes the request and bulids an appropriate SCU context for the
765 * request and then requests the controller to post the request. none
767 void scic_sds_remote_device_post_request(
768 struct scic_sds_remote_device *sci_dev,
769 u32 request)
771 u32 context;
773 context = scic_sds_remote_device_build_command_context(sci_dev, request);
775 scic_sds_controller_post_request(
776 scic_sds_remote_device_get_controller(sci_dev),
777 context
781 /* called once the remote node context has transisitioned to a
782 * ready state. This is the indication that the remote device object can also
783 * transition to ready.
785 static void remote_device_resume_done(void *_dev)
787 struct scic_sds_remote_device *sci_dev = _dev;
789 if (is_remote_device_ready(sci_dev))
790 return;
792 /* go 'ready' if we are not already in a ready state */
793 sci_base_state_machine_change_state(&sci_dev->state_machine,
794 SCI_BASE_REMOTE_DEVICE_STATE_READY);
797 static void scic_sds_stp_remote_device_ready_idle_substate_resume_complete_handler(void *_dev)
799 struct scic_sds_remote_device *sci_dev = _dev;
800 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
801 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
803 /* For NCQ operation we do not issue a isci_remote_device_not_ready().
804 * As a result, avoid sending the ready notification.
806 if (sci_dev->state_machine.previous_state_id != SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ)
807 isci_remote_device_ready(scic_to_ihost(scic), idev);
810 static void scic_sds_remote_device_initial_state_enter(void *object)
812 struct scic_sds_remote_device *sci_dev = object;
814 /* Initial state is a transitional state to the stopped state */
815 sci_base_state_machine_change_state(&sci_dev->state_machine,
816 SCI_BASE_REMOTE_DEVICE_STATE_STOPPED);
820 * scic_remote_device_destruct() - free remote node context and destruct
821 * @remote_device: This parameter specifies the remote device to be destructed.
823 * Remote device objects are a limited resource. As such, they must be
824 * protected. Thus calls to construct and destruct are mutually exclusive and
825 * non-reentrant. The return value shall indicate if the device was
826 * successfully destructed or if some failure occurred. enum sci_status This value
827 * is returned if the device is successfully destructed.
828 * SCI_FAILURE_INVALID_REMOTE_DEVICE This value is returned if the supplied
829 * device isn't valid (e.g. it's already been destoryed, the handle isn't
830 * valid, etc.).
832 static enum sci_status scic_remote_device_destruct(struct scic_sds_remote_device *sci_dev)
834 struct sci_base_state_machine *sm = &sci_dev->state_machine;
835 enum scic_sds_remote_device_states state = sm->current_state_id;
836 struct scic_sds_controller *scic;
838 if (state != SCI_BASE_REMOTE_DEVICE_STATE_STOPPED) {
839 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
840 __func__, state);
841 return SCI_FAILURE_INVALID_STATE;
844 scic = sci_dev->owning_port->owning_controller;
845 scic_sds_controller_free_remote_node_context(scic, sci_dev,
846 sci_dev->rnc.remote_node_index);
847 sci_dev->rnc.remote_node_index = SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX;
848 sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_FINAL);
850 return SCI_SUCCESS;
854 * isci_remote_device_deconstruct() - This function frees an isci_remote_device.
855 * @ihost: This parameter specifies the isci host object.
856 * @idev: This parameter specifies the remote device to be freed.
859 static void isci_remote_device_deconstruct(struct isci_host *ihost, struct isci_remote_device *idev)
861 dev_dbg(&ihost->pdev->dev,
862 "%s: isci_device = %p\n", __func__, idev);
864 /* There should not be any outstanding io's. All paths to
865 * here should go through isci_remote_device_nuke_requests.
866 * If we hit this condition, we will need a way to complete
867 * io requests in process */
868 while (!list_empty(&idev->reqs_in_process)) {
870 dev_err(&ihost->pdev->dev,
871 "%s: ** request list not empty! **\n", __func__);
872 BUG();
875 scic_remote_device_destruct(&idev->sci);
876 idev->domain_dev->lldd_dev = NULL;
877 idev->domain_dev = NULL;
878 idev->isci_port = NULL;
879 list_del_init(&idev->node);
881 clear_bit(IDEV_START_PENDING, &idev->flags);
882 clear_bit(IDEV_STOP_PENDING, &idev->flags);
883 clear_bit(IDEV_EH, &idev->flags);
884 wake_up(&ihost->eventq);
888 * isci_remote_device_stop_complete() - This function is called by the scic
889 * when the remote device stop has completed. We mark the isci device as not
890 * ready and remove the isci remote device.
891 * @ihost: This parameter specifies the isci host object.
892 * @idev: This parameter specifies the remote device.
893 * @status: This parameter specifies status of the completion.
896 static void isci_remote_device_stop_complete(struct isci_host *ihost,
897 struct isci_remote_device *idev)
899 dev_dbg(&ihost->pdev->dev, "%s: complete idev = %p\n", __func__, idev);
901 isci_remote_device_change_state(idev, isci_stopped);
903 /* after stop, we can tear down resources. */
904 isci_remote_device_deconstruct(ihost, idev);
907 static void scic_sds_remote_device_stopped_state_enter(void *object)
909 struct scic_sds_remote_device *sci_dev = object;
910 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
911 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
912 u32 prev_state;
914 /* If we are entering from the stopping state let the SCI User know that
915 * the stop operation has completed.
917 prev_state = sci_dev->state_machine.previous_state_id;
918 if (prev_state == SCI_BASE_REMOTE_DEVICE_STATE_STOPPING)
919 isci_remote_device_stop_complete(scic_to_ihost(scic), idev);
921 scic_sds_controller_remote_device_stopped(scic, sci_dev);
924 static void scic_sds_remote_device_starting_state_enter(void *object)
926 struct scic_sds_remote_device *sci_dev = object;
927 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
928 struct isci_host *ihost = scic_to_ihost(scic);
929 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
931 isci_remote_device_not_ready(ihost, idev,
932 SCIC_REMOTE_DEVICE_NOT_READY_START_REQUESTED);
935 static void scic_sds_remote_device_ready_state_enter(void *object)
937 struct scic_sds_remote_device *sci_dev = object;
938 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
939 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
940 struct domain_device *dev = idev->domain_dev;
942 scic->remote_device_sequence[sci_dev->rnc.remote_node_index]++;
944 if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_SATA)) {
945 sci_base_state_machine_change_state(&sci_dev->state_machine,
946 SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
947 } else if (dev_is_expander(dev)) {
948 sci_base_state_machine_change_state(&sci_dev->state_machine,
949 SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
950 } else
951 isci_remote_device_ready(scic_to_ihost(scic), idev);
954 static void scic_sds_remote_device_ready_state_exit(void *object)
956 struct scic_sds_remote_device *sci_dev = object;
957 struct domain_device *dev = sci_dev_to_domain(sci_dev);
959 if (dev->dev_type == SAS_END_DEV) {
960 struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
961 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
963 isci_remote_device_not_ready(scic_to_ihost(scic), idev,
964 SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED);
968 static void scic_sds_remote_device_resetting_state_enter(void *object)
970 struct scic_sds_remote_device *sci_dev = object;
972 scic_sds_remote_node_context_suspend(
973 &sci_dev->rnc, SCI_SOFTWARE_SUSPENSION, NULL, NULL);
976 static void scic_sds_remote_device_resetting_state_exit(void *object)
978 struct scic_sds_remote_device *sci_dev = object;
980 scic_sds_remote_node_context_resume(&sci_dev->rnc, NULL, NULL);
983 static void scic_sds_stp_remote_device_ready_idle_substate_enter(void *object)
985 struct scic_sds_remote_device *sci_dev = object;
987 sci_dev->working_request = NULL;
988 if (scic_sds_remote_node_context_is_ready(&sci_dev->rnc)) {
990 * Since the RNC is ready, it's alright to finish completion
991 * processing (e.g. signal the remote device is ready). */
992 scic_sds_stp_remote_device_ready_idle_substate_resume_complete_handler(sci_dev);
993 } else {
994 scic_sds_remote_node_context_resume(&sci_dev->rnc,
995 scic_sds_stp_remote_device_ready_idle_substate_resume_complete_handler,
996 sci_dev);
1000 static void scic_sds_stp_remote_device_ready_cmd_substate_enter(void *object)
1002 struct scic_sds_remote_device *sci_dev = object;
1003 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1005 BUG_ON(sci_dev->working_request == NULL);
1007 isci_remote_device_not_ready(scic_to_ihost(scic), sci_dev_to_idev(sci_dev),
1008 SCIC_REMOTE_DEVICE_NOT_READY_SATA_REQUEST_STARTED);
1011 static void scic_sds_stp_remote_device_ready_ncq_error_substate_enter(void *object)
1013 struct scic_sds_remote_device *sci_dev = object;
1014 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1015 struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
1017 if (sci_dev->not_ready_reason == SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED)
1018 isci_remote_device_not_ready(scic_to_ihost(scic), idev,
1019 sci_dev->not_ready_reason);
1022 static void scic_sds_smp_remote_device_ready_idle_substate_enter(void *object)
1024 struct scic_sds_remote_device *sci_dev = object;
1025 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1027 isci_remote_device_ready(scic_to_ihost(scic), sci_dev_to_idev(sci_dev));
1030 static void scic_sds_smp_remote_device_ready_cmd_substate_enter(void *object)
1032 struct scic_sds_remote_device *sci_dev = object;
1033 struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
1035 BUG_ON(sci_dev->working_request == NULL);
1037 isci_remote_device_not_ready(scic_to_ihost(scic), sci_dev_to_idev(sci_dev),
1038 SCIC_REMOTE_DEVICE_NOT_READY_SMP_REQUEST_STARTED);
1041 static void scic_sds_smp_remote_device_ready_cmd_substate_exit(void *object)
1043 struct scic_sds_remote_device *sci_dev = object;
1045 sci_dev->working_request = NULL;
1048 static const struct sci_base_state scic_sds_remote_device_state_table[] = {
1049 [SCI_BASE_REMOTE_DEVICE_STATE_INITIAL] = {
1050 .enter_state = scic_sds_remote_device_initial_state_enter,
1052 [SCI_BASE_REMOTE_DEVICE_STATE_STOPPED] = {
1053 .enter_state = scic_sds_remote_device_stopped_state_enter,
1055 [SCI_BASE_REMOTE_DEVICE_STATE_STARTING] = {
1056 .enter_state = scic_sds_remote_device_starting_state_enter,
1058 [SCI_BASE_REMOTE_DEVICE_STATE_READY] = {
1059 .enter_state = scic_sds_remote_device_ready_state_enter,
1060 .exit_state = scic_sds_remote_device_ready_state_exit
1062 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE] = {
1063 .enter_state = scic_sds_stp_remote_device_ready_idle_substate_enter,
1065 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD] = {
1066 .enter_state = scic_sds_stp_remote_device_ready_cmd_substate_enter,
1068 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ] = { },
1069 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR] = {
1070 .enter_state = scic_sds_stp_remote_device_ready_ncq_error_substate_enter,
1072 [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET] = { },
1073 [SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE] = {
1074 .enter_state = scic_sds_smp_remote_device_ready_idle_substate_enter,
1076 [SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD] = {
1077 .enter_state = scic_sds_smp_remote_device_ready_cmd_substate_enter,
1078 .exit_state = scic_sds_smp_remote_device_ready_cmd_substate_exit,
1080 [SCI_BASE_REMOTE_DEVICE_STATE_STOPPING] = { },
1081 [SCI_BASE_REMOTE_DEVICE_STATE_FAILED] = { },
1082 [SCI_BASE_REMOTE_DEVICE_STATE_RESETTING] = {
1083 .enter_state = scic_sds_remote_device_resetting_state_enter,
1084 .exit_state = scic_sds_remote_device_resetting_state_exit
1086 [SCI_BASE_REMOTE_DEVICE_STATE_FINAL] = { },
1090 * scic_remote_device_construct() - common construction
1091 * @sci_port: SAS/SATA port through which this device is accessed.
1092 * @sci_dev: remote device to construct
1094 * This routine just performs benign initialization and does not
1095 * allocate the remote_node_context which is left to
1096 * scic_remote_device_[de]a_construct(). scic_remote_device_destruct()
1097 * frees the remote_node_context(s) for the device.
1099 static void scic_remote_device_construct(struct scic_sds_port *sci_port,
1100 struct scic_sds_remote_device *sci_dev)
1102 sci_dev->owning_port = sci_port;
1103 sci_dev->started_request_count = 0;
1105 sci_base_state_machine_construct(
1106 &sci_dev->state_machine,
1107 sci_dev,
1108 scic_sds_remote_device_state_table,
1109 SCI_BASE_REMOTE_DEVICE_STATE_INITIAL
1112 sci_base_state_machine_start(
1113 &sci_dev->state_machine
1116 scic_sds_remote_node_context_construct(&sci_dev->rnc,
1117 SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX);
1121 * scic_remote_device_da_construct() - construct direct attached device.
1123 * The information (e.g. IAF, Signature FIS, etc.) necessary to build
1124 * the device is known to the SCI Core since it is contained in the
1125 * scic_phy object. Remote node context(s) is/are a global resource
1126 * allocated by this routine, freed by scic_remote_device_destruct().
1128 * Returns:
1129 * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
1130 * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
1131 * sata-only controller instance.
1132 * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
1134 static enum sci_status scic_remote_device_da_construct(struct scic_sds_port *sci_port,
1135 struct scic_sds_remote_device *sci_dev)
1137 enum sci_status status;
1138 struct domain_device *dev = sci_dev_to_domain(sci_dev);
1140 scic_remote_device_construct(sci_port, sci_dev);
1143 * This information is request to determine how many remote node context
1144 * entries will be needed to store the remote node.
1146 sci_dev->is_direct_attached = true;
1147 status = scic_sds_controller_allocate_remote_node_context(sci_port->owning_controller,
1148 sci_dev,
1149 &sci_dev->rnc.remote_node_index);
1151 if (status != SCI_SUCCESS)
1152 return status;
1154 if (dev->dev_type == SAS_END_DEV || dev->dev_type == SATA_DEV ||
1155 (dev->tproto & SAS_PROTOCOL_STP) || dev_is_expander(dev))
1156 /* pass */;
1157 else
1158 return SCI_FAILURE_UNSUPPORTED_PROTOCOL;
1160 sci_dev->connection_rate = scic_sds_port_get_max_allowed_speed(sci_port);
1162 /* / @todo Should I assign the port width by reading all of the phys on the port? */
1163 sci_dev->device_port_width = 1;
1165 return SCI_SUCCESS;
1169 * scic_remote_device_ea_construct() - construct expander attached device
1171 * Remote node context(s) is/are a global resource allocated by this
1172 * routine, freed by scic_remote_device_destruct().
1174 * Returns:
1175 * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
1176 * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
1177 * sata-only controller instance.
1178 * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
1180 static enum sci_status scic_remote_device_ea_construct(struct scic_sds_port *sci_port,
1181 struct scic_sds_remote_device *sci_dev)
1183 struct domain_device *dev = sci_dev_to_domain(sci_dev);
1184 enum sci_status status;
1186 scic_remote_device_construct(sci_port, sci_dev);
1188 status = scic_sds_controller_allocate_remote_node_context(sci_port->owning_controller,
1189 sci_dev,
1190 &sci_dev->rnc.remote_node_index);
1191 if (status != SCI_SUCCESS)
1192 return status;
1194 if (dev->dev_type == SAS_END_DEV || dev->dev_type == SATA_DEV ||
1195 (dev->tproto & SAS_PROTOCOL_STP) || dev_is_expander(dev))
1196 /* pass */;
1197 else
1198 return SCI_FAILURE_UNSUPPORTED_PROTOCOL;
1201 * For SAS-2 the physical link rate is actually a logical link
1202 * rate that incorporates multiplexing. The SCU doesn't
1203 * incorporate multiplexing and for the purposes of the
1204 * connection the logical link rate is that same as the
1205 * physical. Furthermore, the SAS-2 and SAS-1.1 fields overlay
1206 * one another, so this code works for both situations. */
1207 sci_dev->connection_rate = min_t(u16, scic_sds_port_get_max_allowed_speed(sci_port),
1208 dev->linkrate);
1210 /* / @todo Should I assign the port width by reading all of the phys on the port? */
1211 sci_dev->device_port_width = 1;
1213 return SCI_SUCCESS;
1217 * scic_remote_device_start() - This method will start the supplied remote
1218 * device. This method enables normal IO requests to flow through to the
1219 * remote device.
1220 * @remote_device: This parameter specifies the device to be started.
1221 * @timeout: This parameter specifies the number of milliseconds in which the
1222 * start operation should complete.
1224 * An indication of whether the device was successfully started. SCI_SUCCESS
1225 * This value is returned if the device was successfully started.
1226 * SCI_FAILURE_INVALID_PHY This value is returned if the user attempts to start
1227 * the device when there have been no phys added to it.
1229 static enum sci_status scic_remote_device_start(struct scic_sds_remote_device *sci_dev,
1230 u32 timeout)
1232 struct sci_base_state_machine *sm = &sci_dev->state_machine;
1233 enum scic_sds_remote_device_states state = sm->current_state_id;
1234 enum sci_status status;
1236 if (state != SCI_BASE_REMOTE_DEVICE_STATE_STOPPED) {
1237 dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
1238 __func__, state);
1239 return SCI_FAILURE_INVALID_STATE;
1242 status = scic_sds_remote_node_context_resume(&sci_dev->rnc,
1243 remote_device_resume_done,
1244 sci_dev);
1245 if (status != SCI_SUCCESS)
1246 return status;
1248 sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_STARTING);
1250 return SCI_SUCCESS;
1253 static enum sci_status isci_remote_device_construct(struct isci_port *iport,
1254 struct isci_remote_device *idev)
1256 struct scic_sds_port *sci_port = &iport->sci;
1257 struct isci_host *ihost = iport->isci_host;
1258 struct domain_device *dev = idev->domain_dev;
1259 enum sci_status status;
1261 if (dev->parent && dev_is_expander(dev->parent))
1262 status = scic_remote_device_ea_construct(sci_port, &idev->sci);
1263 else
1264 status = scic_remote_device_da_construct(sci_port, &idev->sci);
1266 if (status != SCI_SUCCESS) {
1267 dev_dbg(&ihost->pdev->dev, "%s: construct failed: %d\n",
1268 __func__, status);
1270 return status;
1273 /* start the device. */
1274 status = scic_remote_device_start(&idev->sci, ISCI_REMOTE_DEVICE_START_TIMEOUT);
1276 if (status != SCI_SUCCESS)
1277 dev_warn(&ihost->pdev->dev, "remote device start failed: %d\n",
1278 status);
1280 return status;
1283 void isci_remote_device_nuke_requests(struct isci_host *ihost, struct isci_remote_device *idev)
1285 DECLARE_COMPLETION_ONSTACK(aborted_task_completion);
1287 dev_dbg(&ihost->pdev->dev,
1288 "%s: idev = %p\n", __func__, idev);
1290 /* Cleanup all requests pending for this device. */
1291 isci_terminate_pending_requests(ihost, idev, terminating);
1293 dev_dbg(&ihost->pdev->dev,
1294 "%s: idev = %p, done\n", __func__, idev);
1298 * This function builds the isci_remote_device when a libsas dev_found message
1299 * is received.
1300 * @isci_host: This parameter specifies the isci host object.
1301 * @port: This parameter specifies the isci_port conected to this device.
1303 * pointer to new isci_remote_device.
1305 static struct isci_remote_device *
1306 isci_remote_device_alloc(struct isci_host *ihost, struct isci_port *iport)
1308 struct isci_remote_device *idev;
1309 int i;
1311 for (i = 0; i < SCI_MAX_REMOTE_DEVICES; i++) {
1312 idev = &ihost->devices[i];
1313 if (!test_and_set_bit(IDEV_ALLOCATED, &idev->flags))
1314 break;
1317 if (i >= SCI_MAX_REMOTE_DEVICES) {
1318 dev_warn(&ihost->pdev->dev, "%s: failed\n", __func__);
1319 return NULL;
1322 if (WARN_ONCE(!list_empty(&idev->reqs_in_process), "found requests in process\n"))
1323 return NULL;
1325 if (WARN_ONCE(!list_empty(&idev->node), "found non-idle remote device\n"))
1326 return NULL;
1328 isci_remote_device_change_state(idev, isci_freed);
1330 return idev;
1334 * isci_remote_device_stop() - This function is called internally to stop the
1335 * remote device.
1336 * @isci_host: This parameter specifies the isci host object.
1337 * @isci_device: This parameter specifies the remote device.
1339 * The status of the scic request to stop.
1341 enum sci_status isci_remote_device_stop(struct isci_host *ihost, struct isci_remote_device *idev)
1343 enum sci_status status;
1344 unsigned long flags;
1346 dev_dbg(&ihost->pdev->dev,
1347 "%s: isci_device = %p\n", __func__, idev);
1349 isci_remote_device_change_state(idev, isci_stopping);
1351 /* Kill all outstanding requests. */
1352 isci_remote_device_nuke_requests(ihost, idev);
1354 set_bit(IDEV_STOP_PENDING, &idev->flags);
1356 spin_lock_irqsave(&ihost->scic_lock, flags);
1357 status = scic_remote_device_stop(&idev->sci, 50);
1358 spin_unlock_irqrestore(&ihost->scic_lock, flags);
1360 /* Wait for the stop complete callback. */
1361 if (status == SCI_SUCCESS) {
1362 wait_for_device_stop(ihost, idev);
1363 clear_bit(IDEV_ALLOCATED, &idev->flags);
1366 dev_dbg(&ihost->pdev->dev,
1367 "%s: idev = %p - after completion wait\n",
1368 __func__, idev);
1370 return status;
1374 * isci_remote_device_gone() - This function is called by libsas when a domain
1375 * device is removed.
1376 * @domain_device: This parameter specifies the libsas domain device.
1379 void isci_remote_device_gone(struct domain_device *dev)
1381 struct isci_host *ihost = dev_to_ihost(dev);
1382 struct isci_remote_device *idev = dev->lldd_dev;
1384 dev_dbg(&ihost->pdev->dev,
1385 "%s: domain_device = %p, isci_device = %p, isci_port = %p\n",
1386 __func__, dev, idev, idev->isci_port);
1388 isci_remote_device_stop(ihost, idev);
1393 * isci_remote_device_found() - This function is called by libsas when a remote
1394 * device is discovered. A remote device object is created and started. the
1395 * function then sleeps until the sci core device started message is
1396 * received.
1397 * @domain_device: This parameter specifies the libsas domain device.
1399 * status, zero indicates success.
1401 int isci_remote_device_found(struct domain_device *domain_dev)
1403 struct isci_host *isci_host = dev_to_ihost(domain_dev);
1404 struct isci_port *isci_port;
1405 struct isci_phy *isci_phy;
1406 struct asd_sas_port *sas_port;
1407 struct asd_sas_phy *sas_phy;
1408 struct isci_remote_device *isci_device;
1409 enum sci_status status;
1411 dev_dbg(&isci_host->pdev->dev,
1412 "%s: domain_device = %p\n", __func__, domain_dev);
1414 wait_for_start(isci_host);
1416 sas_port = domain_dev->port;
1417 sas_phy = list_first_entry(&sas_port->phy_list, struct asd_sas_phy,
1418 port_phy_el);
1419 isci_phy = to_isci_phy(sas_phy);
1420 isci_port = isci_phy->isci_port;
1422 /* we are being called for a device on this port,
1423 * so it has to come up eventually
1425 wait_for_completion(&isci_port->start_complete);
1427 if ((isci_stopping == isci_port_get_state(isci_port)) ||
1428 (isci_stopped == isci_port_get_state(isci_port)))
1429 return -ENODEV;
1431 isci_device = isci_remote_device_alloc(isci_host, isci_port);
1432 if (!isci_device)
1433 return -ENODEV;
1435 INIT_LIST_HEAD(&isci_device->node);
1436 domain_dev->lldd_dev = isci_device;
1437 isci_device->domain_dev = domain_dev;
1438 isci_device->isci_port = isci_port;
1439 isci_remote_device_change_state(isci_device, isci_starting);
1442 spin_lock_irq(&isci_host->scic_lock);
1443 list_add_tail(&isci_device->node, &isci_port->remote_dev_list);
1445 set_bit(IDEV_START_PENDING, &isci_device->flags);
1446 status = isci_remote_device_construct(isci_port, isci_device);
1447 spin_unlock_irq(&isci_host->scic_lock);
1449 dev_dbg(&isci_host->pdev->dev,
1450 "%s: isci_device = %p\n",
1451 __func__, isci_device);
1453 if (status != SCI_SUCCESS) {
1455 spin_lock_irq(&isci_host->scic_lock);
1456 isci_remote_device_deconstruct(
1457 isci_host,
1458 isci_device
1460 spin_unlock_irq(&isci_host->scic_lock);
1461 return -ENODEV;
1464 /* wait for the device ready callback. */
1465 wait_for_device_start(isci_host, isci_device);
1467 return 0;
1470 * isci_device_is_reset_pending() - This function will check if there is any
1471 * pending reset condition on the device.
1472 * @request: This parameter is the isci_device object.
1474 * true if there is a reset pending for the device.
1476 bool isci_device_is_reset_pending(
1477 struct isci_host *isci_host,
1478 struct isci_remote_device *isci_device)
1480 struct isci_request *isci_request;
1481 struct isci_request *tmp_req;
1482 bool reset_is_pending = false;
1483 unsigned long flags;
1485 dev_dbg(&isci_host->pdev->dev,
1486 "%s: isci_device = %p\n", __func__, isci_device);
1488 spin_lock_irqsave(&isci_host->scic_lock, flags);
1490 /* Check for reset on all pending requests. */
1491 list_for_each_entry_safe(isci_request, tmp_req,
1492 &isci_device->reqs_in_process, dev_node) {
1493 dev_dbg(&isci_host->pdev->dev,
1494 "%s: isci_device = %p request = %p\n",
1495 __func__, isci_device, isci_request);
1497 if (isci_request->ttype == io_task) {
1498 struct sas_task *task = isci_request_access_task(
1499 isci_request);
1501 spin_lock(&task->task_state_lock);
1502 if (task->task_state_flags & SAS_TASK_NEED_DEV_RESET)
1503 reset_is_pending = true;
1504 spin_unlock(&task->task_state_lock);
1508 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1510 dev_dbg(&isci_host->pdev->dev,
1511 "%s: isci_device = %p reset_is_pending = %d\n",
1512 __func__, isci_device, reset_is_pending);
1514 return reset_is_pending;
1518 * isci_device_clear_reset_pending() - This function will clear if any pending
1519 * reset condition flags on the device.
1520 * @request: This parameter is the isci_device object.
1522 * true if there is a reset pending for the device.
1524 void isci_device_clear_reset_pending(struct isci_host *ihost, struct isci_remote_device *idev)
1526 struct isci_request *isci_request;
1527 struct isci_request *tmp_req;
1528 unsigned long flags = 0;
1530 dev_dbg(&ihost->pdev->dev, "%s: idev=%p, ihost=%p\n",
1531 __func__, idev, ihost);
1533 spin_lock_irqsave(&ihost->scic_lock, flags);
1535 /* Clear reset pending on all pending requests. */
1536 list_for_each_entry_safe(isci_request, tmp_req,
1537 &idev->reqs_in_process, dev_node) {
1538 dev_dbg(&ihost->pdev->dev, "%s: idev = %p request = %p\n",
1539 __func__, idev, isci_request);
1541 if (isci_request->ttype == io_task) {
1543 unsigned long flags2;
1544 struct sas_task *task = isci_request_access_task(
1545 isci_request);
1547 spin_lock_irqsave(&task->task_state_lock, flags2);
1548 task->task_state_flags &= ~SAS_TASK_NEED_DEV_RESET;
1549 spin_unlock_irqrestore(&task->task_state_lock, flags2);
1552 spin_unlock_irqrestore(&ihost->scic_lock, flags);