blob: cff684fc1efcec47bb26d3851749b5d1aac17bde [file] [log] [blame]
Dan Williams6f231dd2011-07-02 22:56:22 -07001/*
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.
4 *
5 * GPL LICENSE SUMMARY
6 *
7 * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
8 *
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.
12 *
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.
17 *
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.
23 *
24 * BSD LICENSE
25 *
26 * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
27 * All rights reserved.
28 *
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
31 * are met:
32 *
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.
42 *
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.
54 */
Dave Jiang2d9c2242011-05-04 18:45:05 -070055#include <scsi/sas.h>
Bartek Nowakowski7e629842012-01-04 01:33:20 -080056#include <linux/bitops.h>
Dan Williams6f231dd2011-07-02 22:56:22 -070057#include "isci.h"
Dan Williams6f231dd2011-07-02 22:56:22 -070058#include "port.h"
59#include "remote_device.h"
60#include "request.h"
Dan Williams88f3b622011-04-22 19:18:03 -070061#include "remote_node_context.h"
Dan Williams88f3b622011-04-22 19:18:03 -070062#include "scu_event_codes.h"
Dan Williams6f231dd2011-07-02 22:56:22 -070063#include "task.h"
64
Dan Williamsab2e8f72011-04-27 16:32:45 -070065/**
Dan Williamsd9dcb4b2011-06-30 17:38:32 -070066 * isci_remote_device_not_ready() - This function is called by the ihost when
Dan Williamsab2e8f72011-04-27 16:32:45 -070067 * the remote device is not ready. We mark the isci device as ready (not
68 * "ready_for_io") and signal the waiting proccess.
69 * @isci_host: This parameter specifies the isci host object.
70 * @isci_device: This parameter specifies the remote device
71 *
Dan Williams89a73012011-06-30 19:14:33 -070072 * sci_lock is held on entrance to this function.
Dan Williamsab2e8f72011-04-27 16:32:45 -070073 */
74static void isci_remote_device_not_ready(struct isci_host *ihost,
75 struct isci_remote_device *idev, u32 reason)
76{
James Bottomleya5ec7f862011-07-03 14:14:45 -050077 struct isci_request *ireq;
Jeff Skirvin9274f452011-06-23 17:09:02 -070078
Dan Williamsab2e8f72011-04-27 16:32:45 -070079 dev_dbg(&ihost->pdev->dev,
80 "%s: isci_device = %p\n", __func__, idev);
81
Jeff Skirvin9274f452011-06-23 17:09:02 -070082 switch (reason) {
83 case SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED:
Dan Williams209fae12011-06-13 17:39:44 -070084 set_bit(IDEV_GONE, &idev->flags);
Jeff Skirvin9274f452011-06-23 17:09:02 -070085 break;
86 case SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED:
87 set_bit(IDEV_IO_NCQERROR, &idev->flags);
88
89 /* Kill all outstanding requests for the device. */
90 list_for_each_entry(ireq, &idev->reqs_in_process, dev_node) {
91
92 dev_dbg(&ihost->pdev->dev,
93 "%s: isci_device = %p request = %p\n",
94 __func__, idev, ireq);
95
Dan Williams89a73012011-06-30 19:14:33 -070096 sci_controller_terminate_request(ihost,
Dan Williams78a6f062011-06-30 16:31:37 -070097 idev,
Dan Williams5076a1a2011-06-27 14:57:03 -070098 ireq);
Jeff Skirvin9274f452011-06-23 17:09:02 -070099 }
100 /* Fall through into the default case... */
101 default:
Dan Williamsf2088262011-06-16 11:26:12 -0700102 clear_bit(IDEV_IO_READY, &idev->flags);
Jeff Skirvin9274f452011-06-23 17:09:02 -0700103 break;
104 }
Dan Williamsab2e8f72011-04-27 16:32:45 -0700105}
106
107/**
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700108 * isci_remote_device_ready() - This function is called by the ihost when the
Dan Williamsab2e8f72011-04-27 16:32:45 -0700109 * remote device is ready. We mark the isci device as ready and signal the
110 * waiting proccess.
111 * @ihost: our valid isci_host
112 * @idev: remote device
113 *
114 */
115static void isci_remote_device_ready(struct isci_host *ihost, struct isci_remote_device *idev)
116{
117 dev_dbg(&ihost->pdev->dev,
118 "%s: idev = %p\n", __func__, idev);
119
Jeff Skirvin9274f452011-06-23 17:09:02 -0700120 clear_bit(IDEV_IO_NCQERROR, &idev->flags);
Dan Williamsf2088262011-06-16 11:26:12 -0700121 set_bit(IDEV_IO_READY, &idev->flags);
Dan Williamsab2e8f72011-04-27 16:32:45 -0700122 if (test_and_clear_bit(IDEV_START_PENDING, &idev->flags))
123 wake_up(&ihost->eventq);
124}
125
Dan Williamsec575662011-05-01 14:19:25 -0700126/* called once the remote node context is ready to be freed.
127 * The remote device can now report that its stop operation is complete. none
128 */
129static void rnc_destruct_done(void *_dev)
Dan Williams88f3b622011-04-22 19:18:03 -0700130{
Dan Williams78a6f062011-06-30 16:31:37 -0700131 struct isci_remote_device *idev = _dev;
Dan Williamsec575662011-05-01 14:19:25 -0700132
Dan Williams78a6f062011-06-30 16:31:37 -0700133 BUG_ON(idev->started_request_count != 0);
134 sci_change_state(&idev->sm, SCI_DEV_STOPPED);
Dan Williamsec575662011-05-01 14:19:25 -0700135}
136
Dan Williams89a73012011-06-30 19:14:33 -0700137static enum sci_status sci_remote_device_terminate_requests(struct isci_remote_device *idev)
Dan Williamsec575662011-05-01 14:19:25 -0700138{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700139 struct isci_host *ihost = idev->owning_port->owning_controller;
Dan Williamsec575662011-05-01 14:19:25 -0700140 enum sci_status status = SCI_SUCCESS;
Dan Williams76802ce2011-06-29 09:45:48 -0700141 u32 i;
Dan Williamsec575662011-05-01 14:19:25 -0700142
Dan Williams76802ce2011-06-29 09:45:48 -0700143 for (i = 0; i < SCI_MAX_IO_REQUESTS; i++) {
Dan Williamsdb056252011-06-17 14:18:39 -0700144 struct isci_request *ireq = ihost->reqs[i];
Dan Williamsec575662011-05-01 14:19:25 -0700145 enum sci_status s;
146
Dan Williamsdb056252011-06-17 14:18:39 -0700147 if (!test_bit(IREQ_ACTIVE, &ireq->flags) ||
Dan Williams78a6f062011-06-30 16:31:37 -0700148 ireq->target_device != idev)
Dan Williamsec575662011-05-01 14:19:25 -0700149 continue;
Dan Williamsdb056252011-06-17 14:18:39 -0700150
Dan Williams89a73012011-06-30 19:14:33 -0700151 s = sci_controller_terminate_request(ihost, idev, ireq);
Dan Williamsec575662011-05-01 14:19:25 -0700152 if (s != SCI_SUCCESS)
153 status = s;
154 }
155
156 return status;
157}
158
Dan Williams89a73012011-06-30 19:14:33 -0700159enum sci_status sci_remote_device_stop(struct isci_remote_device *idev,
Dan Williamsec575662011-05-01 14:19:25 -0700160 u32 timeout)
161{
Dan Williams78a6f062011-06-30 16:31:37 -0700162 struct sci_base_state_machine *sm = &idev->sm;
Dan Williams89a73012011-06-30 19:14:33 -0700163 enum sci_remote_device_states state = sm->current_state_id;
Dan Williamsec575662011-05-01 14:19:25 -0700164
165 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000166 case SCI_DEV_INITIAL:
167 case SCI_DEV_FAILED:
168 case SCI_DEV_FINAL:
Dan Williamsec575662011-05-01 14:19:25 -0700169 default:
Dan Williams78a6f062011-06-30 16:31:37 -0700170 dev_warn(scirdev_to_dev(idev), "%s: in wrong state: %d\n",
Dan Williamsec575662011-05-01 14:19:25 -0700171 __func__, state);
172 return SCI_FAILURE_INVALID_STATE;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000173 case SCI_DEV_STOPPED:
Dan Williamsec575662011-05-01 14:19:25 -0700174 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000175 case SCI_DEV_STARTING:
Dan Williamsec575662011-05-01 14:19:25 -0700176 /* device not started so there had better be no requests */
Dan Williams78a6f062011-06-30 16:31:37 -0700177 BUG_ON(idev->started_request_count != 0);
Dan Williams89a73012011-06-30 19:14:33 -0700178 sci_remote_node_context_destruct(&idev->rnc,
Dan Williams78a6f062011-06-30 16:31:37 -0700179 rnc_destruct_done, idev);
Dan Williamsec575662011-05-01 14:19:25 -0700180 /* Transition to the stopping state and wait for the
181 * remote node to complete being posted and invalidated.
182 */
Edmund Nadolskie3013702011-06-02 00:10:43 +0000183 sci_change_state(sm, SCI_DEV_STOPPING);
Dan Williamsec575662011-05-01 14:19:25 -0700184 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000185 case SCI_DEV_READY:
186 case SCI_STP_DEV_IDLE:
187 case SCI_STP_DEV_CMD:
188 case SCI_STP_DEV_NCQ:
189 case SCI_STP_DEV_NCQ_ERROR:
190 case SCI_STP_DEV_AWAIT_RESET:
191 case SCI_SMP_DEV_IDLE:
192 case SCI_SMP_DEV_CMD:
193 sci_change_state(sm, SCI_DEV_STOPPING);
Dan Williams78a6f062011-06-30 16:31:37 -0700194 if (idev->started_request_count == 0) {
Dan Williams89a73012011-06-30 19:14:33 -0700195 sci_remote_node_context_destruct(&idev->rnc,
Dan Williams78a6f062011-06-30 16:31:37 -0700196 rnc_destruct_done, idev);
Dan Williamsec575662011-05-01 14:19:25 -0700197 return SCI_SUCCESS;
198 } else
Dan Williams89a73012011-06-30 19:14:33 -0700199 return sci_remote_device_terminate_requests(idev);
Dan Williamsec575662011-05-01 14:19:25 -0700200 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000201 case SCI_DEV_STOPPING:
Dan Williamsec575662011-05-01 14:19:25 -0700202 /* All requests should have been terminated, but if there is an
203 * attempt to stop a device already in the stopping state, then
204 * try again to terminate.
205 */
Dan Williams89a73012011-06-30 19:14:33 -0700206 return sci_remote_device_terminate_requests(idev);
Edmund Nadolskie3013702011-06-02 00:10:43 +0000207 case SCI_DEV_RESETTING:
208 sci_change_state(sm, SCI_DEV_STOPPING);
Dan Williamsec575662011-05-01 14:19:25 -0700209 return SCI_SUCCESS;
210 }
Dan Williams88f3b622011-04-22 19:18:03 -0700211}
Dan Williams6f231dd2011-07-02 22:56:22 -0700212
Dan Williams89a73012011-06-30 19:14:33 -0700213enum sci_status sci_remote_device_reset(struct isci_remote_device *idev)
Dan Williams88f3b622011-04-22 19:18:03 -0700214{
Dan Williams78a6f062011-06-30 16:31:37 -0700215 struct sci_base_state_machine *sm = &idev->sm;
Dan Williams89a73012011-06-30 19:14:33 -0700216 enum sci_remote_device_states state = sm->current_state_id;
Dan Williams4fd0d2e2011-05-01 14:48:54 -0700217
218 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000219 case SCI_DEV_INITIAL:
220 case SCI_DEV_STOPPED:
221 case SCI_DEV_STARTING:
222 case SCI_SMP_DEV_IDLE:
223 case SCI_SMP_DEV_CMD:
224 case SCI_DEV_STOPPING:
225 case SCI_DEV_FAILED:
226 case SCI_DEV_RESETTING:
227 case SCI_DEV_FINAL:
Dan Williams4fd0d2e2011-05-01 14:48:54 -0700228 default:
Dan Williams78a6f062011-06-30 16:31:37 -0700229 dev_warn(scirdev_to_dev(idev), "%s: in wrong state: %d\n",
Dan Williams4fd0d2e2011-05-01 14:48:54 -0700230 __func__, state);
231 return SCI_FAILURE_INVALID_STATE;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000232 case SCI_DEV_READY:
233 case SCI_STP_DEV_IDLE:
234 case SCI_STP_DEV_CMD:
235 case SCI_STP_DEV_NCQ:
236 case SCI_STP_DEV_NCQ_ERROR:
237 case SCI_STP_DEV_AWAIT_RESET:
238 sci_change_state(sm, SCI_DEV_RESETTING);
Dan Williams4fd0d2e2011-05-01 14:48:54 -0700239 return SCI_SUCCESS;
240 }
Dan Williams88f3b622011-04-22 19:18:03 -0700241}
242
Dan Williams89a73012011-06-30 19:14:33 -0700243enum sci_status sci_remote_device_reset_complete(struct isci_remote_device *idev)
Dan Williams88f3b622011-04-22 19:18:03 -0700244{
Dan Williams78a6f062011-06-30 16:31:37 -0700245 struct sci_base_state_machine *sm = &idev->sm;
Dan Williams89a73012011-06-30 19:14:33 -0700246 enum sci_remote_device_states state = sm->current_state_id;
Dan Williams88f3b622011-04-22 19:18:03 -0700247
Edmund Nadolskie3013702011-06-02 00:10:43 +0000248 if (state != SCI_DEV_RESETTING) {
Dan Williams78a6f062011-06-30 16:31:37 -0700249 dev_warn(scirdev_to_dev(idev), "%s: in wrong state: %d\n",
Dan Williams81515182011-05-01 14:53:00 -0700250 __func__, state);
251 return SCI_FAILURE_INVALID_STATE;
252 }
253
Edmund Nadolskie3013702011-06-02 00:10:43 +0000254 sci_change_state(sm, SCI_DEV_READY);
Dan Williams81515182011-05-01 14:53:00 -0700255 return SCI_SUCCESS;
256}
Dan Williams88f3b622011-04-22 19:18:03 -0700257
Dan Williams89a73012011-06-30 19:14:33 -0700258enum sci_status sci_remote_device_suspend(struct isci_remote_device *idev,
Dan Williams323f0ec2011-05-01 16:15:47 -0700259 u32 suspend_type)
Dan Williams88f3b622011-04-22 19:18:03 -0700260{
Dan Williams78a6f062011-06-30 16:31:37 -0700261 struct sci_base_state_machine *sm = &idev->sm;
Dan Williams89a73012011-06-30 19:14:33 -0700262 enum sci_remote_device_states state = sm->current_state_id;
Dan Williams323f0ec2011-05-01 16:15:47 -0700263
Edmund Nadolskie3013702011-06-02 00:10:43 +0000264 if (state != SCI_STP_DEV_CMD) {
Dan Williams78a6f062011-06-30 16:31:37 -0700265 dev_warn(scirdev_to_dev(idev), "%s: in wrong state: %d\n",
Dan Williams323f0ec2011-05-01 16:15:47 -0700266 __func__, state);
267 return SCI_FAILURE_INVALID_STATE;
268 }
269
Dan Williams89a73012011-06-30 19:14:33 -0700270 return sci_remote_node_context_suspend(&idev->rnc,
Dan Williams323f0ec2011-05-01 16:15:47 -0700271 suspend_type, NULL, NULL);
Dan Williams88f3b622011-04-22 19:18:03 -0700272}
273
Dan Williams89a73012011-06-30 19:14:33 -0700274enum sci_status sci_remote_device_frame_handler(struct isci_remote_device *idev,
Dan Williams01bec772011-05-01 16:51:11 -0700275 u32 frame_index)
Dan Williams88f3b622011-04-22 19:18:03 -0700276{
Dan Williams78a6f062011-06-30 16:31:37 -0700277 struct sci_base_state_machine *sm = &idev->sm;
Dan Williams89a73012011-06-30 19:14:33 -0700278 enum sci_remote_device_states state = sm->current_state_id;
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700279 struct isci_host *ihost = idev->owning_port->owning_controller;
Dan Williams01bec772011-05-01 16:51:11 -0700280 enum sci_status status;
281
282 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000283 case SCI_DEV_INITIAL:
284 case SCI_DEV_STOPPED:
285 case SCI_DEV_STARTING:
286 case SCI_STP_DEV_IDLE:
287 case SCI_SMP_DEV_IDLE:
288 case SCI_DEV_FINAL:
Dan Williams01bec772011-05-01 16:51:11 -0700289 default:
Dan Williams78a6f062011-06-30 16:31:37 -0700290 dev_warn(scirdev_to_dev(idev), "%s: in wrong state: %d\n",
Dan Williams01bec772011-05-01 16:51:11 -0700291 __func__, state);
292 /* Return the frame back to the controller */
Dan Williams89a73012011-06-30 19:14:33 -0700293 sci_controller_release_frame(ihost, frame_index);
Dan Williams01bec772011-05-01 16:51:11 -0700294 return SCI_FAILURE_INVALID_STATE;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000295 case SCI_DEV_READY:
296 case SCI_STP_DEV_NCQ_ERROR:
297 case SCI_STP_DEV_AWAIT_RESET:
298 case SCI_DEV_STOPPING:
299 case SCI_DEV_FAILED:
300 case SCI_DEV_RESETTING: {
Dan Williams5076a1a2011-06-27 14:57:03 -0700301 struct isci_request *ireq;
Dave Jiang2d9c2242011-05-04 18:45:05 -0700302 struct ssp_frame_hdr hdr;
303 void *frame_header;
304 ssize_t word_cnt;
Dan Williams01bec772011-05-01 16:51:11 -0700305
Dan Williams89a73012011-06-30 19:14:33 -0700306 status = sci_unsolicited_frame_control_get_header(&ihost->uf_control,
Dan Williams01bec772011-05-01 16:51:11 -0700307 frame_index,
Dave Jiang2d9c2242011-05-04 18:45:05 -0700308 &frame_header);
Dan Williams01bec772011-05-01 16:51:11 -0700309 if (status != SCI_SUCCESS)
310 return status;
311
Dave Jiang2d9c2242011-05-04 18:45:05 -0700312 word_cnt = sizeof(hdr) / sizeof(u32);
313 sci_swab32_cpy(&hdr, frame_header, word_cnt);
314
Dan Williams89a73012011-06-30 19:14:33 -0700315 ireq = sci_request_by_tag(ihost, be16_to_cpu(hdr.tag));
Dan Williams78a6f062011-06-30 16:31:37 -0700316 if (ireq && ireq->target_device == idev) {
Dan Williams01bec772011-05-01 16:51:11 -0700317 /* The IO request is now in charge of releasing the frame */
Dan Williams89a73012011-06-30 19:14:33 -0700318 status = sci_io_request_frame_handler(ireq, frame_index);
Dan Williams01bec772011-05-01 16:51:11 -0700319 } else {
320 /* We could not map this tag to a valid IO
321 * request Just toss the frame and continue
322 */
Dan Williams89a73012011-06-30 19:14:33 -0700323 sci_controller_release_frame(ihost, frame_index);
Dan Williams01bec772011-05-01 16:51:11 -0700324 }
325 break;
326 }
Edmund Nadolskie3013702011-06-02 00:10:43 +0000327 case SCI_STP_DEV_NCQ: {
Dave Jiange76d6182011-05-04 15:02:03 -0700328 struct dev_to_host_fis *hdr;
Dan Williams01bec772011-05-01 16:51:11 -0700329
Dan Williams89a73012011-06-30 19:14:33 -0700330 status = sci_unsolicited_frame_control_get_header(&ihost->uf_control,
Dan Williams01bec772011-05-01 16:51:11 -0700331 frame_index,
332 (void **)&hdr);
333 if (status != SCI_SUCCESS)
334 return status;
335
Dave Jiange76d6182011-05-04 15:02:03 -0700336 if (hdr->fis_type == FIS_SETDEVBITS &&
337 (hdr->status & ATA_ERR)) {
Dan Williams78a6f062011-06-30 16:31:37 -0700338 idev->not_ready_reason = SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED;
Dan Williams01bec772011-05-01 16:51:11 -0700339
340 /* TODO Check sactive and complete associated IO if any. */
Edmund Nadolskie3013702011-06-02 00:10:43 +0000341 sci_change_state(sm, SCI_STP_DEV_NCQ_ERROR);
Dave Jiange76d6182011-05-04 15:02:03 -0700342 } else if (hdr->fis_type == FIS_REGD2H &&
343 (hdr->status & ATA_ERR)) {
Dan Williams01bec772011-05-01 16:51:11 -0700344 /*
345 * Some devices return D2H FIS when an NCQ error is detected.
346 * Treat this like an SDB error FIS ready reason.
347 */
Dan Williams78a6f062011-06-30 16:31:37 -0700348 idev->not_ready_reason = SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED;
349 sci_change_state(&idev->sm, SCI_STP_DEV_NCQ_ERROR);
Dan Williams01bec772011-05-01 16:51:11 -0700350 } else
351 status = SCI_FAILURE;
352
Dan Williams89a73012011-06-30 19:14:33 -0700353 sci_controller_release_frame(ihost, frame_index);
Dan Williams01bec772011-05-01 16:51:11 -0700354 break;
355 }
Edmund Nadolskie3013702011-06-02 00:10:43 +0000356 case SCI_STP_DEV_CMD:
357 case SCI_SMP_DEV_CMD:
Dan Williams01bec772011-05-01 16:51:11 -0700358 /* The device does not process any UF received from the hardware while
359 * in this state. All unsolicited frames are forwarded to the io request
360 * object.
361 */
Dan Williams89a73012011-06-30 19:14:33 -0700362 status = sci_io_request_frame_handler(idev->working_request, frame_index);
Dan Williams01bec772011-05-01 16:51:11 -0700363 break;
364 }
365
366 return status;
Dan Williams88f3b622011-04-22 19:18:03 -0700367}
368
Dan Williams78a6f062011-06-30 16:31:37 -0700369static bool is_remote_device_ready(struct isci_remote_device *idev)
Dan Williams88f3b622011-04-22 19:18:03 -0700370{
Dan Williamse6225712011-05-01 16:26:09 -0700371
Dan Williams78a6f062011-06-30 16:31:37 -0700372 struct sci_base_state_machine *sm = &idev->sm;
Dan Williams89a73012011-06-30 19:14:33 -0700373 enum sci_remote_device_states state = sm->current_state_id;
Dan Williamse6225712011-05-01 16:26:09 -0700374
375 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000376 case SCI_DEV_READY:
377 case SCI_STP_DEV_IDLE:
378 case SCI_STP_DEV_CMD:
379 case SCI_STP_DEV_NCQ:
380 case SCI_STP_DEV_NCQ_ERROR:
381 case SCI_STP_DEV_AWAIT_RESET:
382 case SCI_SMP_DEV_IDLE:
383 case SCI_SMP_DEV_CMD:
Dan Williamse6225712011-05-01 16:26:09 -0700384 return true;
385 default:
386 return false;
387 }
388}
389
Dan Williamsb50102d2011-09-30 18:52:19 -0700390/*
391 * called once the remote node context has transisitioned to a ready
392 * state (after suspending RX and/or TX due to early D2H fis)
393 */
394static void atapi_remote_device_resume_done(void *_dev)
395{
396 struct isci_remote_device *idev = _dev;
397 struct isci_request *ireq = idev->working_request;
398
399 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
400}
401
Dan Williams89a73012011-06-30 19:14:33 -0700402enum sci_status sci_remote_device_event_handler(struct isci_remote_device *idev,
Dan Williamse6225712011-05-01 16:26:09 -0700403 u32 event_code)
404{
Dan Williams78a6f062011-06-30 16:31:37 -0700405 struct sci_base_state_machine *sm = &idev->sm;
Dan Williams89a73012011-06-30 19:14:33 -0700406 enum sci_remote_device_states state = sm->current_state_id;
Dan Williamse6225712011-05-01 16:26:09 -0700407 enum sci_status status;
408
409 switch (scu_get_event_type(event_code)) {
410 case SCU_EVENT_TYPE_RNC_OPS_MISC:
411 case SCU_EVENT_TYPE_RNC_SUSPEND_TX:
412 case SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX:
Dan Williams89a73012011-06-30 19:14:33 -0700413 status = sci_remote_node_context_event_handler(&idev->rnc, event_code);
Dan Williamse6225712011-05-01 16:26:09 -0700414 break;
415 case SCU_EVENT_TYPE_PTX_SCHEDULE_EVENT:
416 if (scu_get_event_code(event_code) == SCU_EVENT_IT_NEXUS_TIMEOUT) {
417 status = SCI_SUCCESS;
418
419 /* Suspend the associated RNC */
Dan Williams89a73012011-06-30 19:14:33 -0700420 sci_remote_node_context_suspend(&idev->rnc,
Dan Williamse6225712011-05-01 16:26:09 -0700421 SCI_SOFTWARE_SUSPENSION,
422 NULL, NULL);
423
Dan Williams78a6f062011-06-30 16:31:37 -0700424 dev_dbg(scirdev_to_dev(idev),
Dan Williamse6225712011-05-01 16:26:09 -0700425 "%s: device: %p event code: %x: %s\n",
Dan Williams78a6f062011-06-30 16:31:37 -0700426 __func__, idev, event_code,
427 is_remote_device_ready(idev)
Dan Williamse6225712011-05-01 16:26:09 -0700428 ? "I_T_Nexus_Timeout event"
429 : "I_T_Nexus_Timeout event in wrong state");
430
431 break;
432 }
433 /* Else, fall through and treat as unhandled... */
434 default:
Dan Williams78a6f062011-06-30 16:31:37 -0700435 dev_dbg(scirdev_to_dev(idev),
Dan Williamse6225712011-05-01 16:26:09 -0700436 "%s: device: %p event code: %x: %s\n",
Dan Williams78a6f062011-06-30 16:31:37 -0700437 __func__, idev, event_code,
438 is_remote_device_ready(idev)
Dan Williamse6225712011-05-01 16:26:09 -0700439 ? "unexpected event"
440 : "unexpected event in wrong state");
441 status = SCI_FAILURE_INVALID_STATE;
442 break;
443 }
444
445 if (status != SCI_SUCCESS)
446 return status;
447
Dan Williamsb50102d2011-09-30 18:52:19 -0700448 if (state == SCI_STP_DEV_ATAPI_ERROR) {
449 /* For ATAPI error state resume the RNC right away. */
450 if (scu_get_event_type(event_code) == SCU_EVENT_TYPE_RNC_SUSPEND_TX ||
451 scu_get_event_type(event_code) == SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX) {
452 return sci_remote_node_context_resume(&idev->rnc,
453 atapi_remote_device_resume_done,
454 idev);
455 }
456 }
457
Edmund Nadolskie3013702011-06-02 00:10:43 +0000458 if (state == SCI_STP_DEV_IDLE) {
Dan Williamse6225712011-05-01 16:26:09 -0700459
460 /* We pick up suspension events to handle specifically to this
461 * state. We resume the RNC right away.
462 */
463 if (scu_get_event_type(event_code) == SCU_EVENT_TYPE_RNC_SUSPEND_TX ||
464 scu_get_event_type(event_code) == SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX)
Dan Williams89a73012011-06-30 19:14:33 -0700465 status = sci_remote_node_context_resume(&idev->rnc, NULL, NULL);
Dan Williamse6225712011-05-01 16:26:09 -0700466 }
467
468 return status;
Dan Williams88f3b622011-04-22 19:18:03 -0700469}
470
Dan Williams89a73012011-06-30 19:14:33 -0700471static void sci_remote_device_start_request(struct isci_remote_device *idev,
Dan Williams5076a1a2011-06-27 14:57:03 -0700472 struct isci_request *ireq,
Dan Williams18606552011-05-01 14:57:11 -0700473 enum sci_status status)
Dan Williams88f3b622011-04-22 19:18:03 -0700474{
Dan Williams78a6f062011-06-30 16:31:37 -0700475 struct isci_port *iport = idev->owning_port;
Dan Williams18606552011-05-01 14:57:11 -0700476
477 /* cleanup requests that failed after starting on the port */
478 if (status != SCI_SUCCESS)
Dan Williams89a73012011-06-30 19:14:33 -0700479 sci_port_complete_io(iport, idev, ireq);
Dan Williams209fae12011-06-13 17:39:44 -0700480 else {
Dan Williams78a6f062011-06-30 16:31:37 -0700481 kref_get(&idev->kref);
Dan Williams34a99152011-07-01 02:25:15 -0700482 idev->started_request_count++;
Dan Williams209fae12011-06-13 17:39:44 -0700483 }
Dan Williams18606552011-05-01 14:57:11 -0700484}
485
Dan Williams89a73012011-06-30 19:14:33 -0700486enum sci_status sci_remote_device_start_io(struct isci_host *ihost,
Dan Williams78a6f062011-06-30 16:31:37 -0700487 struct isci_remote_device *idev,
Dan Williams5076a1a2011-06-27 14:57:03 -0700488 struct isci_request *ireq)
Dan Williams18606552011-05-01 14:57:11 -0700489{
Dan Williams78a6f062011-06-30 16:31:37 -0700490 struct sci_base_state_machine *sm = &idev->sm;
Dan Williams89a73012011-06-30 19:14:33 -0700491 enum sci_remote_device_states state = sm->current_state_id;
Dan Williams78a6f062011-06-30 16:31:37 -0700492 struct isci_port *iport = idev->owning_port;
Dan Williams18606552011-05-01 14:57:11 -0700493 enum sci_status status;
494
495 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000496 case SCI_DEV_INITIAL:
497 case SCI_DEV_STOPPED:
498 case SCI_DEV_STARTING:
499 case SCI_STP_DEV_NCQ_ERROR:
500 case SCI_DEV_STOPPING:
501 case SCI_DEV_FAILED:
502 case SCI_DEV_RESETTING:
503 case SCI_DEV_FINAL:
Dan Williams18606552011-05-01 14:57:11 -0700504 default:
Dan Williams78a6f062011-06-30 16:31:37 -0700505 dev_warn(scirdev_to_dev(idev), "%s: in wrong state: %d\n",
Dan Williams18606552011-05-01 14:57:11 -0700506 __func__, state);
507 return SCI_FAILURE_INVALID_STATE;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000508 case SCI_DEV_READY:
Dan Williams18606552011-05-01 14:57:11 -0700509 /* attempt to start an io request for this device object. The remote
510 * device object will issue the start request for the io and if
511 * successful it will start the request for the port object then
512 * increment its own request count.
513 */
Dan Williams89a73012011-06-30 19:14:33 -0700514 status = sci_port_start_io(iport, idev, ireq);
Dan Williams18606552011-05-01 14:57:11 -0700515 if (status != SCI_SUCCESS)
516 return status;
517
Dan Williams89a73012011-06-30 19:14:33 -0700518 status = sci_remote_node_context_start_io(&idev->rnc, ireq);
Dan Williams18606552011-05-01 14:57:11 -0700519 if (status != SCI_SUCCESS)
520 break;
521
Dan Williams89a73012011-06-30 19:14:33 -0700522 status = sci_request_start(ireq);
Dan Williams18606552011-05-01 14:57:11 -0700523 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000524 case SCI_STP_DEV_IDLE: {
Dan Williams18606552011-05-01 14:57:11 -0700525 /* handle the start io operation for a sata device that is in
526 * the command idle state. - Evalute the type of IO request to
527 * be started - If its an NCQ request change to NCQ substate -
528 * If its any other command change to the CMD substate
529 *
530 * If this is a softreset we may want to have a different
531 * substate.
532 */
Dan Williams89a73012011-06-30 19:14:33 -0700533 enum sci_remote_device_states new_state;
Dave Jiange76d6182011-05-04 15:02:03 -0700534 struct sas_task *task = isci_request_access_task(ireq);
Dan Williams18606552011-05-01 14:57:11 -0700535
Dan Williams89a73012011-06-30 19:14:33 -0700536 status = sci_port_start_io(iport, idev, ireq);
Dan Williams18606552011-05-01 14:57:11 -0700537 if (status != SCI_SUCCESS)
538 return status;
539
Dan Williams89a73012011-06-30 19:14:33 -0700540 status = sci_remote_node_context_start_io(&idev->rnc, ireq);
Dan Williams18606552011-05-01 14:57:11 -0700541 if (status != SCI_SUCCESS)
542 break;
543
Dan Williams89a73012011-06-30 19:14:33 -0700544 status = sci_request_start(ireq);
Dan Williams18606552011-05-01 14:57:11 -0700545 if (status != SCI_SUCCESS)
546 break;
547
Dave Jiange76d6182011-05-04 15:02:03 -0700548 if (task->ata_task.use_ncq)
Edmund Nadolskie3013702011-06-02 00:10:43 +0000549 new_state = SCI_STP_DEV_NCQ;
Dan Williams18606552011-05-01 14:57:11 -0700550 else {
Dan Williams78a6f062011-06-30 16:31:37 -0700551 idev->working_request = ireq;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000552 new_state = SCI_STP_DEV_CMD;
Dan Williams18606552011-05-01 14:57:11 -0700553 }
Edmund Nadolskie3013702011-06-02 00:10:43 +0000554 sci_change_state(sm, new_state);
Dan Williams18606552011-05-01 14:57:11 -0700555 break;
556 }
Edmund Nadolskie3013702011-06-02 00:10:43 +0000557 case SCI_STP_DEV_NCQ: {
Dave Jiange76d6182011-05-04 15:02:03 -0700558 struct sas_task *task = isci_request_access_task(ireq);
559
560 if (task->ata_task.use_ncq) {
Dan Williams89a73012011-06-30 19:14:33 -0700561 status = sci_port_start_io(iport, idev, ireq);
Dan Williams18606552011-05-01 14:57:11 -0700562 if (status != SCI_SUCCESS)
563 return status;
564
Dan Williams89a73012011-06-30 19:14:33 -0700565 status = sci_remote_node_context_start_io(&idev->rnc, ireq);
Dan Williams18606552011-05-01 14:57:11 -0700566 if (status != SCI_SUCCESS)
567 break;
568
Dan Williams89a73012011-06-30 19:14:33 -0700569 status = sci_request_start(ireq);
Dan Williams18606552011-05-01 14:57:11 -0700570 } else
571 return SCI_FAILURE_INVALID_STATE;
572 break;
Dave Jiange76d6182011-05-04 15:02:03 -0700573 }
Edmund Nadolskie3013702011-06-02 00:10:43 +0000574 case SCI_STP_DEV_AWAIT_RESET:
Dan Williams18606552011-05-01 14:57:11 -0700575 return SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000576 case SCI_SMP_DEV_IDLE:
Dan Williams89a73012011-06-30 19:14:33 -0700577 status = sci_port_start_io(iport, idev, ireq);
Dan Williams18606552011-05-01 14:57:11 -0700578 if (status != SCI_SUCCESS)
579 return status;
580
Dan Williams89a73012011-06-30 19:14:33 -0700581 status = sci_remote_node_context_start_io(&idev->rnc, ireq);
Dan Williams18606552011-05-01 14:57:11 -0700582 if (status != SCI_SUCCESS)
583 break;
584
Dan Williams89a73012011-06-30 19:14:33 -0700585 status = sci_request_start(ireq);
Dan Williams18606552011-05-01 14:57:11 -0700586 if (status != SCI_SUCCESS)
587 break;
588
Dan Williams78a6f062011-06-30 16:31:37 -0700589 idev->working_request = ireq;
590 sci_change_state(&idev->sm, SCI_SMP_DEV_CMD);
Dan Williams18606552011-05-01 14:57:11 -0700591 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000592 case SCI_STP_DEV_CMD:
593 case SCI_SMP_DEV_CMD:
Dan Williams18606552011-05-01 14:57:11 -0700594 /* device is already handling a command it can not accept new commands
595 * until this one is complete.
596 */
597 return SCI_FAILURE_INVALID_STATE;
598 }
599
Dan Williams89a73012011-06-30 19:14:33 -0700600 sci_remote_device_start_request(idev, ireq, status);
Dan Williams18606552011-05-01 14:57:11 -0700601 return status;
Dan Williams88f3b622011-04-22 19:18:03 -0700602}
603
Dan Williamsffe191c2011-06-29 13:09:25 -0700604static enum sci_status common_complete_io(struct isci_port *iport,
Dan Williams78a6f062011-06-30 16:31:37 -0700605 struct isci_remote_device *idev,
Dan Williams5076a1a2011-06-27 14:57:03 -0700606 struct isci_request *ireq)
Dan Williams88f3b622011-04-22 19:18:03 -0700607{
Dan Williams10a09e62011-05-01 15:33:43 -0700608 enum sci_status status;
609
Dan Williams89a73012011-06-30 19:14:33 -0700610 status = sci_request_complete(ireq);
Dan Williams10a09e62011-05-01 15:33:43 -0700611 if (status != SCI_SUCCESS)
612 return status;
613
Dan Williams89a73012011-06-30 19:14:33 -0700614 status = sci_port_complete_io(iport, idev, ireq);
Dan Williams10a09e62011-05-01 15:33:43 -0700615 if (status != SCI_SUCCESS)
616 return status;
617
Dan Williams89a73012011-06-30 19:14:33 -0700618 sci_remote_device_decrement_request_count(idev);
Dan Williams10a09e62011-05-01 15:33:43 -0700619 return status;
620}
621
Dan Williams89a73012011-06-30 19:14:33 -0700622enum sci_status sci_remote_device_complete_io(struct isci_host *ihost,
Dan Williams78a6f062011-06-30 16:31:37 -0700623 struct isci_remote_device *idev,
Dan Williams5076a1a2011-06-27 14:57:03 -0700624 struct isci_request *ireq)
Dan Williams10a09e62011-05-01 15:33:43 -0700625{
Dan Williams78a6f062011-06-30 16:31:37 -0700626 struct sci_base_state_machine *sm = &idev->sm;
Dan Williams89a73012011-06-30 19:14:33 -0700627 enum sci_remote_device_states state = sm->current_state_id;
Dan Williams78a6f062011-06-30 16:31:37 -0700628 struct isci_port *iport = idev->owning_port;
Dan Williams10a09e62011-05-01 15:33:43 -0700629 enum sci_status status;
630
631 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000632 case SCI_DEV_INITIAL:
633 case SCI_DEV_STOPPED:
634 case SCI_DEV_STARTING:
635 case SCI_STP_DEV_IDLE:
636 case SCI_SMP_DEV_IDLE:
637 case SCI_DEV_FAILED:
638 case SCI_DEV_FINAL:
Dan Williams10a09e62011-05-01 15:33:43 -0700639 default:
Dan Williams78a6f062011-06-30 16:31:37 -0700640 dev_warn(scirdev_to_dev(idev), "%s: in wrong state: %d\n",
Dan Williams10a09e62011-05-01 15:33:43 -0700641 __func__, state);
642 return SCI_FAILURE_INVALID_STATE;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000643 case SCI_DEV_READY:
644 case SCI_STP_DEV_AWAIT_RESET:
645 case SCI_DEV_RESETTING:
Dan Williams78a6f062011-06-30 16:31:37 -0700646 status = common_complete_io(iport, idev, ireq);
Dan Williams10a09e62011-05-01 15:33:43 -0700647 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000648 case SCI_STP_DEV_CMD:
649 case SCI_STP_DEV_NCQ:
650 case SCI_STP_DEV_NCQ_ERROR:
Dan Williamsb50102d2011-09-30 18:52:19 -0700651 case SCI_STP_DEV_ATAPI_ERROR:
Dan Williams78a6f062011-06-30 16:31:37 -0700652 status = common_complete_io(iport, idev, ireq);
Dan Williams10a09e62011-05-01 15:33:43 -0700653 if (status != SCI_SUCCESS)
654 break;
655
Dan Williams5076a1a2011-06-27 14:57:03 -0700656 if (ireq->sci_status == SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED) {
Dan Williams10a09e62011-05-01 15:33:43 -0700657 /* This request causes hardware error, device needs to be Lun Reset.
658 * So here we force the state machine to IDLE state so the rest IOs
659 * can reach RNC state handler, these IOs will be completed by RNC with
660 * status of "DEVICE_RESET_REQUIRED", instead of "INVALID STATE".
661 */
Edmund Nadolskie3013702011-06-02 00:10:43 +0000662 sci_change_state(sm, SCI_STP_DEV_AWAIT_RESET);
Dan Williams34a99152011-07-01 02:25:15 -0700663 } else if (idev->started_request_count == 0)
Edmund Nadolskie3013702011-06-02 00:10:43 +0000664 sci_change_state(sm, SCI_STP_DEV_IDLE);
Dan Williams10a09e62011-05-01 15:33:43 -0700665 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000666 case SCI_SMP_DEV_CMD:
Dan Williams78a6f062011-06-30 16:31:37 -0700667 status = common_complete_io(iport, idev, ireq);
Dan Williams10a09e62011-05-01 15:33:43 -0700668 if (status != SCI_SUCCESS)
669 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000670 sci_change_state(sm, SCI_SMP_DEV_IDLE);
Dan Williams10a09e62011-05-01 15:33:43 -0700671 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000672 case SCI_DEV_STOPPING:
Dan Williams78a6f062011-06-30 16:31:37 -0700673 status = common_complete_io(iport, idev, ireq);
Dan Williams10a09e62011-05-01 15:33:43 -0700674 if (status != SCI_SUCCESS)
675 break;
676
Dan Williams34a99152011-07-01 02:25:15 -0700677 if (idev->started_request_count == 0)
Dan Williams89a73012011-06-30 19:14:33 -0700678 sci_remote_node_context_destruct(&idev->rnc,
Dan Williams34a99152011-07-01 02:25:15 -0700679 rnc_destruct_done,
680 idev);
Dan Williams10a09e62011-05-01 15:33:43 -0700681 break;
682 }
683
684 if (status != SCI_SUCCESS)
Dan Williams78a6f062011-06-30 16:31:37 -0700685 dev_err(scirdev_to_dev(idev),
Dan Williams10a09e62011-05-01 15:33:43 -0700686 "%s: Port:0x%p Device:0x%p Request:0x%p Status:0x%x "
Dan Williamsffe191c2011-06-29 13:09:25 -0700687 "could not complete\n", __func__, iport,
Dan Williams78a6f062011-06-30 16:31:37 -0700688 idev, ireq, status);
Dan Williams209fae12011-06-13 17:39:44 -0700689 else
Dan Williams78a6f062011-06-30 16:31:37 -0700690 isci_put_device(idev);
Dan Williams10a09e62011-05-01 15:33:43 -0700691
692 return status;
Dan Williams88f3b622011-04-22 19:18:03 -0700693}
694
Dan Williams89a73012011-06-30 19:14:33 -0700695static void sci_remote_device_continue_request(void *dev)
Dan Williams88f3b622011-04-22 19:18:03 -0700696{
Dan Williams78a6f062011-06-30 16:31:37 -0700697 struct isci_remote_device *idev = dev;
Dan Williams84b9b022011-05-01 15:53:25 -0700698
699 /* we need to check if this request is still valid to continue. */
Dan Williams78a6f062011-06-30 16:31:37 -0700700 if (idev->working_request)
Dan Williams89a73012011-06-30 19:14:33 -0700701 sci_controller_continue_io(idev->working_request);
Dan Williams84b9b022011-05-01 15:53:25 -0700702}
703
Dan Williams89a73012011-06-30 19:14:33 -0700704enum sci_status sci_remote_device_start_task(struct isci_host *ihost,
Dan Williams78a6f062011-06-30 16:31:37 -0700705 struct isci_remote_device *idev,
Dan Williams5076a1a2011-06-27 14:57:03 -0700706 struct isci_request *ireq)
Dan Williams84b9b022011-05-01 15:53:25 -0700707{
Dan Williams78a6f062011-06-30 16:31:37 -0700708 struct sci_base_state_machine *sm = &idev->sm;
Dan Williams89a73012011-06-30 19:14:33 -0700709 enum sci_remote_device_states state = sm->current_state_id;
Dan Williams78a6f062011-06-30 16:31:37 -0700710 struct isci_port *iport = idev->owning_port;
Dan Williams84b9b022011-05-01 15:53:25 -0700711 enum sci_status status;
712
713 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000714 case SCI_DEV_INITIAL:
715 case SCI_DEV_STOPPED:
716 case SCI_DEV_STARTING:
717 case SCI_SMP_DEV_IDLE:
718 case SCI_SMP_DEV_CMD:
719 case SCI_DEV_STOPPING:
720 case SCI_DEV_FAILED:
721 case SCI_DEV_RESETTING:
722 case SCI_DEV_FINAL:
Dan Williams84b9b022011-05-01 15:53:25 -0700723 default:
Dan Williams78a6f062011-06-30 16:31:37 -0700724 dev_warn(scirdev_to_dev(idev), "%s: in wrong state: %d\n",
Dan Williams84b9b022011-05-01 15:53:25 -0700725 __func__, state);
726 return SCI_FAILURE_INVALID_STATE;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000727 case SCI_STP_DEV_IDLE:
728 case SCI_STP_DEV_CMD:
729 case SCI_STP_DEV_NCQ:
730 case SCI_STP_DEV_NCQ_ERROR:
731 case SCI_STP_DEV_AWAIT_RESET:
Dan Williams89a73012011-06-30 19:14:33 -0700732 status = sci_port_start_io(iport, idev, ireq);
Dan Williams84b9b022011-05-01 15:53:25 -0700733 if (status != SCI_SUCCESS)
734 return status;
735
Dan Williams89a73012011-06-30 19:14:33 -0700736 status = sci_remote_node_context_start_task(&idev->rnc, ireq);
Dan Williams84b9b022011-05-01 15:53:25 -0700737 if (status != SCI_SUCCESS)
738 goto out;
739
Dan Williams89a73012011-06-30 19:14:33 -0700740 status = sci_request_start(ireq);
Dan Williams84b9b022011-05-01 15:53:25 -0700741 if (status != SCI_SUCCESS)
742 goto out;
743
744 /* Note: If the remote device state is not IDLE this will
745 * replace the request that probably resulted in the task
746 * management request.
747 */
Dan Williams78a6f062011-06-30 16:31:37 -0700748 idev->working_request = ireq;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000749 sci_change_state(sm, SCI_STP_DEV_CMD);
Dan Williams84b9b022011-05-01 15:53:25 -0700750
751 /* The remote node context must cleanup the TCi to NCQ mapping
752 * table. The only way to do this correctly is to either write
753 * to the TLCR register or to invalidate and repost the RNC. In
754 * either case the remote node context state machine will take
755 * the correct action when the remote node context is suspended
756 * and later resumed.
757 */
Dan Williams89a73012011-06-30 19:14:33 -0700758 sci_remote_node_context_suspend(&idev->rnc,
Dan Williams84b9b022011-05-01 15:53:25 -0700759 SCI_SOFTWARE_SUSPENSION, NULL, NULL);
Dan Williams89a73012011-06-30 19:14:33 -0700760 sci_remote_node_context_resume(&idev->rnc,
761 sci_remote_device_continue_request,
Dan Williams78a6f062011-06-30 16:31:37 -0700762 idev);
Dan Williams84b9b022011-05-01 15:53:25 -0700763
764 out:
Dan Williams89a73012011-06-30 19:14:33 -0700765 sci_remote_device_start_request(idev, ireq, status);
Dan Williams84b9b022011-05-01 15:53:25 -0700766 /* We need to let the controller start request handler know that
767 * it can't post TC yet. We will provide a callback function to
768 * post TC when RNC gets resumed.
769 */
770 return SCI_FAILURE_RESET_DEVICE_PARTIAL_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000771 case SCI_DEV_READY:
Dan Williams89a73012011-06-30 19:14:33 -0700772 status = sci_port_start_io(iport, idev, ireq);
Dan Williams84b9b022011-05-01 15:53:25 -0700773 if (status != SCI_SUCCESS)
774 return status;
775
Dan Williams89a73012011-06-30 19:14:33 -0700776 status = sci_remote_node_context_start_task(&idev->rnc, ireq);
Dan Williams84b9b022011-05-01 15:53:25 -0700777 if (status != SCI_SUCCESS)
778 break;
779
Dan Williams89a73012011-06-30 19:14:33 -0700780 status = sci_request_start(ireq);
Dan Williams84b9b022011-05-01 15:53:25 -0700781 break;
782 }
Dan Williams89a73012011-06-30 19:14:33 -0700783 sci_remote_device_start_request(idev, ireq, status);
Dan Williams84b9b022011-05-01 15:53:25 -0700784
785 return status;
Dan Williams88f3b622011-04-22 19:18:03 -0700786}
787
Dan Williams34a99152011-07-01 02:25:15 -0700788void sci_remote_device_post_request(struct isci_remote_device *idev, u32 request)
Dan Williams88f3b622011-04-22 19:18:03 -0700789{
Dan Williams34a99152011-07-01 02:25:15 -0700790 struct isci_port *iport = idev->owning_port;
Dan Williams88f3b622011-04-22 19:18:03 -0700791 u32 context;
792
Dan Williams34a99152011-07-01 02:25:15 -0700793 context = request |
794 (ISCI_PEG << SCU_CONTEXT_COMMAND_PROTOCOL_ENGINE_GROUP_SHIFT) |
795 (iport->physical_port_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT) |
796 idev->rnc.remote_node_index;
Dan Williams88f3b622011-04-22 19:18:03 -0700797
Dan Williams34a99152011-07-01 02:25:15 -0700798 sci_controller_post_request(iport->owning_controller, context);
Dan Williams88f3b622011-04-22 19:18:03 -0700799}
800
Dan Williamsab2e8f72011-04-27 16:32:45 -0700801/* called once the remote node context has transisitioned to a
Dan Williams88f3b622011-04-22 19:18:03 -0700802 * ready state. This is the indication that the remote device object can also
Dan Williamsab2e8f72011-04-27 16:32:45 -0700803 * transition to ready.
Dan Williams88f3b622011-04-22 19:18:03 -0700804 */
Dan Williamseb229672011-05-01 14:05:57 -0700805static void remote_device_resume_done(void *_dev)
Dan Williams88f3b622011-04-22 19:18:03 -0700806{
Dan Williams78a6f062011-06-30 16:31:37 -0700807 struct isci_remote_device *idev = _dev;
Dan Williams88f3b622011-04-22 19:18:03 -0700808
Dan Williams78a6f062011-06-30 16:31:37 -0700809 if (is_remote_device_ready(idev))
Dan Williamse6225712011-05-01 16:26:09 -0700810 return;
Dan Williams88f3b622011-04-22 19:18:03 -0700811
Dan Williamse6225712011-05-01 16:26:09 -0700812 /* go 'ready' if we are not already in a ready state */
Dan Williams78a6f062011-06-30 16:31:37 -0700813 sci_change_state(&idev->sm, SCI_DEV_READY);
Dan Williams88f3b622011-04-22 19:18:03 -0700814}
815
Dan Williams89a73012011-06-30 19:14:33 -0700816static void sci_stp_remote_device_ready_idle_substate_resume_complete_handler(void *_dev)
Dan Williamsab2e8f72011-04-27 16:32:45 -0700817{
Dan Williams78a6f062011-06-30 16:31:37 -0700818 struct isci_remote_device *idev = _dev;
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700819 struct isci_host *ihost = idev->owning_port->owning_controller;
Dan Williamsab2e8f72011-04-27 16:32:45 -0700820
821 /* For NCQ operation we do not issue a isci_remote_device_not_ready().
822 * As a result, avoid sending the ready notification.
823 */
Dan Williams78a6f062011-06-30 16:31:37 -0700824 if (idev->sm.previous_state_id != SCI_STP_DEV_NCQ)
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700825 isci_remote_device_ready(ihost, idev);
Dan Williamsab2e8f72011-04-27 16:32:45 -0700826}
827
Dan Williams89a73012011-06-30 19:14:33 -0700828static void sci_remote_device_initial_state_enter(struct sci_base_state_machine *sm)
Dan Williams88f3b622011-04-22 19:18:03 -0700829{
Dan Williams78a6f062011-06-30 16:31:37 -0700830 struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
Dan Williams88f3b622011-04-22 19:18:03 -0700831
Dan Williams88f3b622011-04-22 19:18:03 -0700832 /* Initial state is a transitional state to the stopped state */
Dan Williams78a6f062011-06-30 16:31:37 -0700833 sci_change_state(&idev->sm, SCI_DEV_STOPPED);
Dan Williams88f3b622011-04-22 19:18:03 -0700834}
835
836/**
Dan Williams89a73012011-06-30 19:14:33 -0700837 * sci_remote_device_destruct() - free remote node context and destruct
Dan Williams88f3b622011-04-22 19:18:03 -0700838 * @remote_device: This parameter specifies the remote device to be destructed.
839 *
840 * Remote device objects are a limited resource. As such, they must be
841 * protected. Thus calls to construct and destruct are mutually exclusive and
842 * non-reentrant. The return value shall indicate if the device was
843 * successfully destructed or if some failure occurred. enum sci_status This value
844 * is returned if the device is successfully destructed.
845 * SCI_FAILURE_INVALID_REMOTE_DEVICE This value is returned if the supplied
846 * device isn't valid (e.g. it's already been destoryed, the handle isn't
847 * valid, etc.).
848 */
Dan Williams89a73012011-06-30 19:14:33 -0700849static enum sci_status sci_remote_device_destruct(struct isci_remote_device *idev)
Dan Williams88f3b622011-04-22 19:18:03 -0700850{
Dan Williams78a6f062011-06-30 16:31:37 -0700851 struct sci_base_state_machine *sm = &idev->sm;
Dan Williams89a73012011-06-30 19:14:33 -0700852 enum sci_remote_device_states state = sm->current_state_id;
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700853 struct isci_host *ihost;
Dan Williamsb8d82f62011-05-01 14:38:26 -0700854
Edmund Nadolskie3013702011-06-02 00:10:43 +0000855 if (state != SCI_DEV_STOPPED) {
Dan Williams78a6f062011-06-30 16:31:37 -0700856 dev_warn(scirdev_to_dev(idev), "%s: in wrong state: %d\n",
Dan Williamsb8d82f62011-05-01 14:38:26 -0700857 __func__, state);
858 return SCI_FAILURE_INVALID_STATE;
859 }
860
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700861 ihost = idev->owning_port->owning_controller;
Dan Williams89a73012011-06-30 19:14:33 -0700862 sci_controller_free_remote_node_context(ihost, idev,
Dan Williams78a6f062011-06-30 16:31:37 -0700863 idev->rnc.remote_node_index);
864 idev->rnc.remote_node_index = SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000865 sci_change_state(sm, SCI_DEV_FINAL);
Dan Williamsb8d82f62011-05-01 14:38:26 -0700866
867 return SCI_SUCCESS;
Dan Williams88f3b622011-04-22 19:18:03 -0700868}
869
Dan Williams6f231dd2011-07-02 22:56:22 -0700870/**
871 * isci_remote_device_deconstruct() - This function frees an isci_remote_device.
Dan Williamsd9c37392011-03-03 17:59:32 -0800872 * @ihost: This parameter specifies the isci host object.
873 * @idev: This parameter specifies the remote device to be freed.
Dan Williams6f231dd2011-07-02 22:56:22 -0700874 *
875 */
Dan Williamsd9c37392011-03-03 17:59:32 -0800876static void isci_remote_device_deconstruct(struct isci_host *ihost, struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -0700877{
Dan Williamsd9c37392011-03-03 17:59:32 -0800878 dev_dbg(&ihost->pdev->dev,
879 "%s: isci_device = %p\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -0700880
881 /* There should not be any outstanding io's. All paths to
882 * here should go through isci_remote_device_nuke_requests.
883 * If we hit this condition, we will need a way to complete
884 * io requests in process */
Dan Williams209fae12011-06-13 17:39:44 -0700885 BUG_ON(!list_empty(&idev->reqs_in_process));
Dan Williams6f231dd2011-07-02 22:56:22 -0700886
Dan Williams89a73012011-06-30 19:14:33 -0700887 sci_remote_device_destruct(idev);
Dan Williamsd9c37392011-03-03 17:59:32 -0800888 list_del_init(&idev->node);
Dan Williams209fae12011-06-13 17:39:44 -0700889 isci_put_device(idev);
Dan Williams6f231dd2011-07-02 22:56:22 -0700890}
891
Dan Williams89a73012011-06-30 19:14:33 -0700892static void sci_remote_device_stopped_state_enter(struct sci_base_state_machine *sm)
Dan Williams88f3b622011-04-22 19:18:03 -0700893{
Dan Williams78a6f062011-06-30 16:31:37 -0700894 struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700895 struct isci_host *ihost = idev->owning_port->owning_controller;
Dan Williams88f3b622011-04-22 19:18:03 -0700896 u32 prev_state;
897
Dan Williams88f3b622011-04-22 19:18:03 -0700898 /* If we are entering from the stopping state let the SCI User know that
899 * the stop operation has completed.
900 */
Dan Williams78a6f062011-06-30 16:31:37 -0700901 prev_state = idev->sm.previous_state_id;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000902 if (prev_state == SCI_DEV_STOPPING)
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700903 isci_remote_device_deconstruct(ihost, idev);
Dan Williams88f3b622011-04-22 19:18:03 -0700904
Dan Williams89a73012011-06-30 19:14:33 -0700905 sci_controller_remote_device_stopped(ihost, idev);
Dan Williams88f3b622011-04-22 19:18:03 -0700906}
907
Dan Williams89a73012011-06-30 19:14:33 -0700908static void sci_remote_device_starting_state_enter(struct sci_base_state_machine *sm)
Dan Williams88f3b622011-04-22 19:18:03 -0700909{
Dan Williams78a6f062011-06-30 16:31:37 -0700910 struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
Dan Williams34a99152011-07-01 02:25:15 -0700911 struct isci_host *ihost = idev->owning_port->owning_controller;
Dan Williams88f3b622011-04-22 19:18:03 -0700912
Dan Williams88f3b622011-04-22 19:18:03 -0700913 isci_remote_device_not_ready(ihost, idev,
914 SCIC_REMOTE_DEVICE_NOT_READY_START_REQUESTED);
915}
916
Dan Williams89a73012011-06-30 19:14:33 -0700917static void sci_remote_device_ready_state_enter(struct sci_base_state_machine *sm)
Dan Williams88f3b622011-04-22 19:18:03 -0700918{
Dan Williams78a6f062011-06-30 16:31:37 -0700919 struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700920 struct isci_host *ihost = idev->owning_port->owning_controller;
Artur Wojcikcc3dbd02011-05-04 07:58:16 +0000921 struct domain_device *dev = idev->domain_dev;
Dan Williams88f3b622011-04-22 19:18:03 -0700922
Dan Williamsab2e8f72011-04-27 16:32:45 -0700923 if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_SATA)) {
Dan Williams78a6f062011-06-30 16:31:37 -0700924 sci_change_state(&idev->sm, SCI_STP_DEV_IDLE);
Dan Williamsab2e8f72011-04-27 16:32:45 -0700925 } else if (dev_is_expander(dev)) {
Dan Williams78a6f062011-06-30 16:31:37 -0700926 sci_change_state(&idev->sm, SCI_SMP_DEV_IDLE);
Dan Williamsab2e8f72011-04-27 16:32:45 -0700927 } else
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700928 isci_remote_device_ready(ihost, idev);
Dan Williams88f3b622011-04-22 19:18:03 -0700929}
930
Dan Williams89a73012011-06-30 19:14:33 -0700931static void sci_remote_device_ready_state_exit(struct sci_base_state_machine *sm)
Dan Williams88f3b622011-04-22 19:18:03 -0700932{
Dan Williams78a6f062011-06-30 16:31:37 -0700933 struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
934 struct domain_device *dev = idev->domain_dev;
Dan Williamsab2e8f72011-04-27 16:32:45 -0700935
936 if (dev->dev_type == SAS_END_DEV) {
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700937 struct isci_host *ihost = idev->owning_port->owning_controller;
Dan Williams88f3b622011-04-22 19:18:03 -0700938
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700939 isci_remote_device_not_ready(ihost, idev,
Dan Williams88f3b622011-04-22 19:18:03 -0700940 SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED);
941 }
942}
943
Dan Williams89a73012011-06-30 19:14:33 -0700944static void sci_remote_device_resetting_state_enter(struct sci_base_state_machine *sm)
Dan Williams88f3b622011-04-22 19:18:03 -0700945{
Dan Williams78a6f062011-06-30 16:31:37 -0700946 struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
Dan Williams88f3b622011-04-22 19:18:03 -0700947
Dan Williams89a73012011-06-30 19:14:33 -0700948 sci_remote_node_context_suspend(
Dan Williams78a6f062011-06-30 16:31:37 -0700949 &idev->rnc, SCI_SOFTWARE_SUSPENSION, NULL, NULL);
Dan Williams88f3b622011-04-22 19:18:03 -0700950}
951
Dan Williams89a73012011-06-30 19:14:33 -0700952static void sci_remote_device_resetting_state_exit(struct sci_base_state_machine *sm)
Dan Williams88f3b622011-04-22 19:18:03 -0700953{
Dan Williams78a6f062011-06-30 16:31:37 -0700954 struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
Dan Williams88f3b622011-04-22 19:18:03 -0700955
Dan Williams89a73012011-06-30 19:14:33 -0700956 sci_remote_node_context_resume(&idev->rnc, NULL, NULL);
Dan Williams88f3b622011-04-22 19:18:03 -0700957}
958
Dan Williams89a73012011-06-30 19:14:33 -0700959static void sci_stp_remote_device_ready_idle_substate_enter(struct sci_base_state_machine *sm)
Dan Williamsab2e8f72011-04-27 16:32:45 -0700960{
Dan Williams78a6f062011-06-30 16:31:37 -0700961 struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
Dan Williamsab2e8f72011-04-27 16:32:45 -0700962
Dan Williams78a6f062011-06-30 16:31:37 -0700963 idev->working_request = NULL;
Dan Williams89a73012011-06-30 19:14:33 -0700964 if (sci_remote_node_context_is_ready(&idev->rnc)) {
Dan Williamsab2e8f72011-04-27 16:32:45 -0700965 /*
966 * Since the RNC is ready, it's alright to finish completion
967 * processing (e.g. signal the remote device is ready). */
Dan Williams89a73012011-06-30 19:14:33 -0700968 sci_stp_remote_device_ready_idle_substate_resume_complete_handler(idev);
Dan Williamsab2e8f72011-04-27 16:32:45 -0700969 } else {
Dan Williams89a73012011-06-30 19:14:33 -0700970 sci_remote_node_context_resume(&idev->rnc,
971 sci_stp_remote_device_ready_idle_substate_resume_complete_handler,
Dan Williams78a6f062011-06-30 16:31:37 -0700972 idev);
Dan Williamsab2e8f72011-04-27 16:32:45 -0700973 }
974}
975
Dan Williams89a73012011-06-30 19:14:33 -0700976static void sci_stp_remote_device_ready_cmd_substate_enter(struct sci_base_state_machine *sm)
Dan Williamsab2e8f72011-04-27 16:32:45 -0700977{
Dan Williams78a6f062011-06-30 16:31:37 -0700978 struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
Dan Williams34a99152011-07-01 02:25:15 -0700979 struct isci_host *ihost = idev->owning_port->owning_controller;
Dan Williamsab2e8f72011-04-27 16:32:45 -0700980
Dan Williams78a6f062011-06-30 16:31:37 -0700981 BUG_ON(idev->working_request == NULL);
Dan Williamsab2e8f72011-04-27 16:32:45 -0700982
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700983 isci_remote_device_not_ready(ihost, idev,
Dan Williamsab2e8f72011-04-27 16:32:45 -0700984 SCIC_REMOTE_DEVICE_NOT_READY_SATA_REQUEST_STARTED);
985}
986
Dan Williams89a73012011-06-30 19:14:33 -0700987static void sci_stp_remote_device_ready_ncq_error_substate_enter(struct sci_base_state_machine *sm)
Dan Williamsab2e8f72011-04-27 16:32:45 -0700988{
Dan Williams78a6f062011-06-30 16:31:37 -0700989 struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
Dan Williams34a99152011-07-01 02:25:15 -0700990 struct isci_host *ihost = idev->owning_port->owning_controller;
Dan Williamsab2e8f72011-04-27 16:32:45 -0700991
Dan Williams78a6f062011-06-30 16:31:37 -0700992 if (idev->not_ready_reason == SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED)
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700993 isci_remote_device_not_ready(ihost, idev,
Dan Williams78a6f062011-06-30 16:31:37 -0700994 idev->not_ready_reason);
Dan Williamsab2e8f72011-04-27 16:32:45 -0700995}
996
Dan Williams89a73012011-06-30 19:14:33 -0700997static void sci_smp_remote_device_ready_idle_substate_enter(struct sci_base_state_machine *sm)
Dan Williamsab2e8f72011-04-27 16:32:45 -0700998{
Dan Williams78a6f062011-06-30 16:31:37 -0700999 struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
Dan Williams34a99152011-07-01 02:25:15 -07001000 struct isci_host *ihost = idev->owning_port->owning_controller;
Dan Williamsab2e8f72011-04-27 16:32:45 -07001001
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001002 isci_remote_device_ready(ihost, idev);
Dan Williamsab2e8f72011-04-27 16:32:45 -07001003}
1004
Dan Williams89a73012011-06-30 19:14:33 -07001005static void sci_smp_remote_device_ready_cmd_substate_enter(struct sci_base_state_machine *sm)
Dan Williamsab2e8f72011-04-27 16:32:45 -07001006{
Dan Williams78a6f062011-06-30 16:31:37 -07001007 struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
Dan Williams34a99152011-07-01 02:25:15 -07001008 struct isci_host *ihost = idev->owning_port->owning_controller;
Dan Williamsab2e8f72011-04-27 16:32:45 -07001009
Dan Williams78a6f062011-06-30 16:31:37 -07001010 BUG_ON(idev->working_request == NULL);
Dan Williamsab2e8f72011-04-27 16:32:45 -07001011
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001012 isci_remote_device_not_ready(ihost, idev,
Dan Williamsab2e8f72011-04-27 16:32:45 -07001013 SCIC_REMOTE_DEVICE_NOT_READY_SMP_REQUEST_STARTED);
1014}
1015
Dan Williams89a73012011-06-30 19:14:33 -07001016static void sci_smp_remote_device_ready_cmd_substate_exit(struct sci_base_state_machine *sm)
Dan Williamsab2e8f72011-04-27 16:32:45 -07001017{
Dan Williams78a6f062011-06-30 16:31:37 -07001018 struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
Dan Williamsab2e8f72011-04-27 16:32:45 -07001019
Dan Williams78a6f062011-06-30 16:31:37 -07001020 idev->working_request = NULL;
Dan Williamsab2e8f72011-04-27 16:32:45 -07001021}
Dan Williams88f3b622011-04-22 19:18:03 -07001022
Dan Williams89a73012011-06-30 19:14:33 -07001023static const struct sci_base_state sci_remote_device_state_table[] = {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001024 [SCI_DEV_INITIAL] = {
Dan Williams89a73012011-06-30 19:14:33 -07001025 .enter_state = sci_remote_device_initial_state_enter,
Dan Williams88f3b622011-04-22 19:18:03 -07001026 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001027 [SCI_DEV_STOPPED] = {
Dan Williams89a73012011-06-30 19:14:33 -07001028 .enter_state = sci_remote_device_stopped_state_enter,
Dan Williams88f3b622011-04-22 19:18:03 -07001029 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001030 [SCI_DEV_STARTING] = {
Dan Williams89a73012011-06-30 19:14:33 -07001031 .enter_state = sci_remote_device_starting_state_enter,
Dan Williams88f3b622011-04-22 19:18:03 -07001032 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001033 [SCI_DEV_READY] = {
Dan Williams89a73012011-06-30 19:14:33 -07001034 .enter_state = sci_remote_device_ready_state_enter,
1035 .exit_state = sci_remote_device_ready_state_exit
Dan Williams88f3b622011-04-22 19:18:03 -07001036 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001037 [SCI_STP_DEV_IDLE] = {
Dan Williams89a73012011-06-30 19:14:33 -07001038 .enter_state = sci_stp_remote_device_ready_idle_substate_enter,
Dan Williamsab2e8f72011-04-27 16:32:45 -07001039 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001040 [SCI_STP_DEV_CMD] = {
Dan Williams89a73012011-06-30 19:14:33 -07001041 .enter_state = sci_stp_remote_device_ready_cmd_substate_enter,
Dan Williamsab2e8f72011-04-27 16:32:45 -07001042 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001043 [SCI_STP_DEV_NCQ] = { },
1044 [SCI_STP_DEV_NCQ_ERROR] = {
Dan Williams89a73012011-06-30 19:14:33 -07001045 .enter_state = sci_stp_remote_device_ready_ncq_error_substate_enter,
Dan Williamsab2e8f72011-04-27 16:32:45 -07001046 },
Dan Williamsb50102d2011-09-30 18:52:19 -07001047 [SCI_STP_DEV_ATAPI_ERROR] = { },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001048 [SCI_STP_DEV_AWAIT_RESET] = { },
1049 [SCI_SMP_DEV_IDLE] = {
Dan Williams89a73012011-06-30 19:14:33 -07001050 .enter_state = sci_smp_remote_device_ready_idle_substate_enter,
Dan Williamsab2e8f72011-04-27 16:32:45 -07001051 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001052 [SCI_SMP_DEV_CMD] = {
Dan Williams89a73012011-06-30 19:14:33 -07001053 .enter_state = sci_smp_remote_device_ready_cmd_substate_enter,
1054 .exit_state = sci_smp_remote_device_ready_cmd_substate_exit,
Dan Williamsab2e8f72011-04-27 16:32:45 -07001055 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001056 [SCI_DEV_STOPPING] = { },
1057 [SCI_DEV_FAILED] = { },
1058 [SCI_DEV_RESETTING] = {
Dan Williams89a73012011-06-30 19:14:33 -07001059 .enter_state = sci_remote_device_resetting_state_enter,
1060 .exit_state = sci_remote_device_resetting_state_exit
Dan Williams88f3b622011-04-22 19:18:03 -07001061 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001062 [SCI_DEV_FINAL] = { },
Dan Williams88f3b622011-04-22 19:18:03 -07001063};
1064
1065/**
Dan Williams89a73012011-06-30 19:14:33 -07001066 * sci_remote_device_construct() - common construction
Dan Williams88f3b622011-04-22 19:18:03 -07001067 * @sci_port: SAS/SATA port through which this device is accessed.
1068 * @sci_dev: remote device to construct
1069 *
Dan Williamsb87ee302011-04-25 11:48:29 -07001070 * This routine just performs benign initialization and does not
1071 * allocate the remote_node_context which is left to
Dan Williams89a73012011-06-30 19:14:33 -07001072 * sci_remote_device_[de]a_construct(). sci_remote_device_destruct()
Dan Williamsb87ee302011-04-25 11:48:29 -07001073 * frees the remote_node_context(s) for the device.
Dan Williams88f3b622011-04-22 19:18:03 -07001074 */
Dan Williams89a73012011-06-30 19:14:33 -07001075static void sci_remote_device_construct(struct isci_port *iport,
Dan Williams78a6f062011-06-30 16:31:37 -07001076 struct isci_remote_device *idev)
Dan Williams88f3b622011-04-22 19:18:03 -07001077{
Dan Williams78a6f062011-06-30 16:31:37 -07001078 idev->owning_port = iport;
1079 idev->started_request_count = 0;
Dan Williams88f3b622011-04-22 19:18:03 -07001080
Dan Williams89a73012011-06-30 19:14:33 -07001081 sci_init_sm(&idev->sm, sci_remote_device_state_table, SCI_DEV_INITIAL);
Dan Williams88f3b622011-04-22 19:18:03 -07001082
Dan Williams89a73012011-06-30 19:14:33 -07001083 sci_remote_node_context_construct(&idev->rnc,
Dan Williams88f3b622011-04-22 19:18:03 -07001084 SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX);
Dan Williams88f3b622011-04-22 19:18:03 -07001085}
1086
1087/**
Dan Williams89a73012011-06-30 19:14:33 -07001088 * sci_remote_device_da_construct() - construct direct attached device.
Dan Williams88f3b622011-04-22 19:18:03 -07001089 *
Dan Williamsb87ee302011-04-25 11:48:29 -07001090 * The information (e.g. IAF, Signature FIS, etc.) necessary to build
1091 * the device is known to the SCI Core since it is contained in the
Dan Williams89a73012011-06-30 19:14:33 -07001092 * sci_phy object. Remote node context(s) is/are a global resource
1093 * allocated by this routine, freed by sci_remote_device_destruct().
Dan Williamsb87ee302011-04-25 11:48:29 -07001094 *
1095 * Returns:
1096 * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
1097 * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
1098 * sata-only controller instance.
1099 * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
Dan Williams88f3b622011-04-22 19:18:03 -07001100 */
Dan Williams89a73012011-06-30 19:14:33 -07001101static enum sci_status sci_remote_device_da_construct(struct isci_port *iport,
Dan Williams78a6f062011-06-30 16:31:37 -07001102 struct isci_remote_device *idev)
Dan Williams88f3b622011-04-22 19:18:03 -07001103{
1104 enum sci_status status;
Bartek Nowakowski7e629842012-01-04 01:33:20 -08001105 struct sci_port_properties properties;
Dan Williams78a6f062011-06-30 16:31:37 -07001106 struct domain_device *dev = idev->domain_dev;
Dan Williams88f3b622011-04-22 19:18:03 -07001107
Dan Williams89a73012011-06-30 19:14:33 -07001108 sci_remote_device_construct(iport, idev);
Dan Williamsb87ee302011-04-25 11:48:29 -07001109
Dan Williams88f3b622011-04-22 19:18:03 -07001110 /*
1111 * This information is request to determine how many remote node context
1112 * entries will be needed to store the remote node.
1113 */
Dan Williams78a6f062011-06-30 16:31:37 -07001114 idev->is_direct_attached = true;
Bartek Nowakowski7e629842012-01-04 01:33:20 -08001115
1116 sci_port_get_properties(iport, &properties);
1117 /* Get accurate port width from port's phy mask for a DA device. */
1118 idev->device_port_width = hweight32(properties.phy_mask);
1119
Dan Williams89a73012011-06-30 19:14:33 -07001120 status = sci_controller_allocate_remote_node_context(iport->owning_controller,
Dan Williams78a6f062011-06-30 16:31:37 -07001121 idev,
1122 &idev->rnc.remote_node_index);
Dan Williams88f3b622011-04-22 19:18:03 -07001123
Dan Williamsa1a113b2011-04-21 18:44:45 -07001124 if (status != SCI_SUCCESS)
1125 return status;
Dan Williams88f3b622011-04-22 19:18:03 -07001126
Dan Williamsab2e8f72011-04-27 16:32:45 -07001127 if (dev->dev_type == SAS_END_DEV || dev->dev_type == SATA_DEV ||
1128 (dev->tproto & SAS_PROTOCOL_STP) || dev_is_expander(dev))
1129 /* pass */;
1130 else
Dan Williamsa1a113b2011-04-21 18:44:45 -07001131 return SCI_FAILURE_UNSUPPORTED_PROTOCOL;
Dan Williams88f3b622011-04-22 19:18:03 -07001132
Dan Williams89a73012011-06-30 19:14:33 -07001133 idev->connection_rate = sci_port_get_max_allowed_speed(iport);
Dan Williams88f3b622011-04-22 19:18:03 -07001134
Dan Williamsa1a113b2011-04-21 18:44:45 -07001135 return SCI_SUCCESS;
Dan Williams88f3b622011-04-22 19:18:03 -07001136}
1137
Dan Williams88f3b622011-04-22 19:18:03 -07001138/**
Dan Williams89a73012011-06-30 19:14:33 -07001139 * sci_remote_device_ea_construct() - construct expander attached device
Dan Williams88f3b622011-04-22 19:18:03 -07001140 *
Dan Williamsb87ee302011-04-25 11:48:29 -07001141 * Remote node context(s) is/are a global resource allocated by this
Dan Williams89a73012011-06-30 19:14:33 -07001142 * routine, freed by sci_remote_device_destruct().
Dan Williamsb87ee302011-04-25 11:48:29 -07001143 *
1144 * Returns:
1145 * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
1146 * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
1147 * sata-only controller instance.
1148 * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
Dan Williams88f3b622011-04-22 19:18:03 -07001149 */
Dan Williams89a73012011-06-30 19:14:33 -07001150static enum sci_status sci_remote_device_ea_construct(struct isci_port *iport,
Dan Williams78a6f062011-06-30 16:31:37 -07001151 struct isci_remote_device *idev)
Dan Williams88f3b622011-04-22 19:18:03 -07001152{
Dan Williams78a6f062011-06-30 16:31:37 -07001153 struct domain_device *dev = idev->domain_dev;
Dan Williams88f3b622011-04-22 19:18:03 -07001154 enum sci_status status;
Dan Williams88f3b622011-04-22 19:18:03 -07001155
Dan Williams89a73012011-06-30 19:14:33 -07001156 sci_remote_device_construct(iport, idev);
Dan Williams88f3b622011-04-22 19:18:03 -07001157
Dan Williams89a73012011-06-30 19:14:33 -07001158 status = sci_controller_allocate_remote_node_context(iport->owning_controller,
Dan Williams78a6f062011-06-30 16:31:37 -07001159 idev,
1160 &idev->rnc.remote_node_index);
Dan Williamsa1a113b2011-04-21 18:44:45 -07001161 if (status != SCI_SUCCESS)
1162 return status;
Dan Williams88f3b622011-04-22 19:18:03 -07001163
Dan Williamsab2e8f72011-04-27 16:32:45 -07001164 if (dev->dev_type == SAS_END_DEV || dev->dev_type == SATA_DEV ||
1165 (dev->tproto & SAS_PROTOCOL_STP) || dev_is_expander(dev))
1166 /* pass */;
1167 else
1168 return SCI_FAILURE_UNSUPPORTED_PROTOCOL;
Dan Williams88f3b622011-04-22 19:18:03 -07001169
Dan Williamsa1a113b2011-04-21 18:44:45 -07001170 /*
1171 * For SAS-2 the physical link rate is actually a logical link
1172 * rate that incorporates multiplexing. The SCU doesn't
1173 * incorporate multiplexing and for the purposes of the
1174 * connection the logical link rate is that same as the
1175 * physical. Furthermore, the SAS-2 and SAS-1.1 fields overlay
1176 * one another, so this code works for both situations. */
Dan Williams89a73012011-06-30 19:14:33 -07001177 idev->connection_rate = min_t(u16, sci_port_get_max_allowed_speed(iport),
Dan Williams00d680e2011-04-25 14:29:29 -07001178 dev->linkrate);
Dan Williams88f3b622011-04-22 19:18:03 -07001179
Dan Williamsa1a113b2011-04-21 18:44:45 -07001180 /* / @todo Should I assign the port width by reading all of the phys on the port? */
Dan Williams78a6f062011-06-30 16:31:37 -07001181 idev->device_port_width = 1;
Dan Williams88f3b622011-04-22 19:18:03 -07001182
Dan Williamsab2e8f72011-04-27 16:32:45 -07001183 return SCI_SUCCESS;
Dan Williams88f3b622011-04-22 19:18:03 -07001184}
1185
1186/**
Dan Williams89a73012011-06-30 19:14:33 -07001187 * sci_remote_device_start() - This method will start the supplied remote
Dan Williams88f3b622011-04-22 19:18:03 -07001188 * device. This method enables normal IO requests to flow through to the
1189 * remote device.
1190 * @remote_device: This parameter specifies the device to be started.
1191 * @timeout: This parameter specifies the number of milliseconds in which the
1192 * start operation should complete.
1193 *
1194 * An indication of whether the device was successfully started. SCI_SUCCESS
1195 * This value is returned if the device was successfully started.
1196 * SCI_FAILURE_INVALID_PHY This value is returned if the user attempts to start
1197 * the device when there have been no phys added to it.
1198 */
Dan Williams89a73012011-06-30 19:14:33 -07001199static enum sci_status sci_remote_device_start(struct isci_remote_device *idev,
Dan Williamseb229672011-05-01 14:05:57 -07001200 u32 timeout)
Dan Williams88f3b622011-04-22 19:18:03 -07001201{
Dan Williams78a6f062011-06-30 16:31:37 -07001202 struct sci_base_state_machine *sm = &idev->sm;
Dan Williams89a73012011-06-30 19:14:33 -07001203 enum sci_remote_device_states state = sm->current_state_id;
Dan Williamseb229672011-05-01 14:05:57 -07001204 enum sci_status status;
1205
Edmund Nadolskie3013702011-06-02 00:10:43 +00001206 if (state != SCI_DEV_STOPPED) {
Dan Williams78a6f062011-06-30 16:31:37 -07001207 dev_warn(scirdev_to_dev(idev), "%s: in wrong state: %d\n",
Dan Williamseb229672011-05-01 14:05:57 -07001208 __func__, state);
1209 return SCI_FAILURE_INVALID_STATE;
1210 }
1211
Dan Williams89a73012011-06-30 19:14:33 -07001212 status = sci_remote_node_context_resume(&idev->rnc,
Dan Williamseb229672011-05-01 14:05:57 -07001213 remote_device_resume_done,
Dan Williams78a6f062011-06-30 16:31:37 -07001214 idev);
Dan Williamseb229672011-05-01 14:05:57 -07001215 if (status != SCI_SUCCESS)
1216 return status;
1217
Edmund Nadolskie3013702011-06-02 00:10:43 +00001218 sci_change_state(sm, SCI_DEV_STARTING);
Dan Williamseb229672011-05-01 14:05:57 -07001219
1220 return SCI_SUCCESS;
Dan Williams88f3b622011-04-22 19:18:03 -07001221}
Dan Williams6f231dd2011-07-02 22:56:22 -07001222
Dan Williams00d680e2011-04-25 14:29:29 -07001223static enum sci_status isci_remote_device_construct(struct isci_port *iport,
1224 struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001225{
Dan Williams00d680e2011-04-25 14:29:29 -07001226 struct isci_host *ihost = iport->isci_host;
1227 struct domain_device *dev = idev->domain_dev;
1228 enum sci_status status;
Dan Williams6f231dd2011-07-02 22:56:22 -07001229
Dan Williams00d680e2011-04-25 14:29:29 -07001230 if (dev->parent && dev_is_expander(dev->parent))
Dan Williams89a73012011-06-30 19:14:33 -07001231 status = sci_remote_device_ea_construct(iport, idev);
Dan Williams00d680e2011-04-25 14:29:29 -07001232 else
Dan Williams89a73012011-06-30 19:14:33 -07001233 status = sci_remote_device_da_construct(iport, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001234
1235 if (status != SCI_SUCCESS) {
Dan Williams00d680e2011-04-25 14:29:29 -07001236 dev_dbg(&ihost->pdev->dev, "%s: construct failed: %d\n",
1237 __func__, status);
Dan Williams6f231dd2011-07-02 22:56:22 -07001238
1239 return status;
1240 }
1241
Dan Williams6f231dd2011-07-02 22:56:22 -07001242 /* start the device. */
Dan Williams89a73012011-06-30 19:14:33 -07001243 status = sci_remote_device_start(idev, ISCI_REMOTE_DEVICE_START_TIMEOUT);
Dan Williams6f231dd2011-07-02 22:56:22 -07001244
Dan Williams00d680e2011-04-25 14:29:29 -07001245 if (status != SCI_SUCCESS)
1246 dev_warn(&ihost->pdev->dev, "remote device start failed: %d\n",
1247 status);
Dan Williams6f231dd2011-07-02 22:56:22 -07001248
1249 return status;
1250}
1251
Dan Williams4393aa42011-03-31 13:10:44 -07001252void isci_remote_device_nuke_requests(struct isci_host *ihost, struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001253{
1254 DECLARE_COMPLETION_ONSTACK(aborted_task_completion);
Dan Williams6f231dd2011-07-02 22:56:22 -07001255
Dan Williams4393aa42011-03-31 13:10:44 -07001256 dev_dbg(&ihost->pdev->dev,
1257 "%s: idev = %p\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001258
1259 /* Cleanup all requests pending for this device. */
Dan Williams980d3ae2011-06-20 15:11:22 -07001260 isci_terminate_pending_requests(ihost, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001261
Dan Williams4393aa42011-03-31 13:10:44 -07001262 dev_dbg(&ihost->pdev->dev,
1263 "%s: idev = %p, done\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001264}
1265
Dan Williams6f231dd2011-07-02 22:56:22 -07001266/**
1267 * This function builds the isci_remote_device when a libsas dev_found message
1268 * is received.
1269 * @isci_host: This parameter specifies the isci host object.
1270 * @port: This parameter specifies the isci_port conected to this device.
1271 *
1272 * pointer to new isci_remote_device.
1273 */
1274static struct isci_remote_device *
Dan Williamsd9c37392011-03-03 17:59:32 -08001275isci_remote_device_alloc(struct isci_host *ihost, struct isci_port *iport)
Dan Williams6f231dd2011-07-02 22:56:22 -07001276{
Dan Williamsd9c37392011-03-03 17:59:32 -08001277 struct isci_remote_device *idev;
1278 int i;
Dan Williams6f231dd2011-07-02 22:56:22 -07001279
Dan Williamsd9c37392011-03-03 17:59:32 -08001280 for (i = 0; i < SCI_MAX_REMOTE_DEVICES; i++) {
Dan Williams57f20f42011-04-21 18:14:45 -07001281 idev = &ihost->devices[i];
Dan Williamsd9c37392011-03-03 17:59:32 -08001282 if (!test_and_set_bit(IDEV_ALLOCATED, &idev->flags))
1283 break;
1284 }
Dan Williams6f231dd2011-07-02 22:56:22 -07001285
Dan Williamsd9c37392011-03-03 17:59:32 -08001286 if (i >= SCI_MAX_REMOTE_DEVICES) {
1287 dev_warn(&ihost->pdev->dev, "%s: failed\n", __func__);
Dan Williams6f231dd2011-07-02 22:56:22 -07001288 return NULL;
1289 }
1290
Bartosz Barcinski6cb4d6b2011-04-12 17:28:43 -07001291 if (WARN_ONCE(!list_empty(&idev->reqs_in_process), "found requests in process\n"))
1292 return NULL;
1293
1294 if (WARN_ONCE(!list_empty(&idev->node), "found non-idle remote device\n"))
1295 return NULL;
1296
Dan Williamsd9c37392011-03-03 17:59:32 -08001297 return idev;
Dan Williams6f231dd2011-07-02 22:56:22 -07001298}
Dan Williams6f231dd2011-07-02 22:56:22 -07001299
Dan Williams209fae12011-06-13 17:39:44 -07001300void isci_remote_device_release(struct kref *kref)
1301{
1302 struct isci_remote_device *idev = container_of(kref, typeof(*idev), kref);
1303 struct isci_host *ihost = idev->isci_port->isci_host;
1304
1305 idev->domain_dev = NULL;
1306 idev->isci_port = NULL;
1307 clear_bit(IDEV_START_PENDING, &idev->flags);
1308 clear_bit(IDEV_STOP_PENDING, &idev->flags);
Dan Williamsf2088262011-06-16 11:26:12 -07001309 clear_bit(IDEV_IO_READY, &idev->flags);
Dan Williams209fae12011-06-13 17:39:44 -07001310 clear_bit(IDEV_GONE, &idev->flags);
1311 clear_bit(IDEV_EH, &idev->flags);
1312 smp_mb__before_clear_bit();
1313 clear_bit(IDEV_ALLOCATED, &idev->flags);
1314 wake_up(&ihost->eventq);
1315}
1316
Dan Williams6f231dd2011-07-02 22:56:22 -07001317/**
Dan Williams6f231dd2011-07-02 22:56:22 -07001318 * isci_remote_device_stop() - This function is called internally to stop the
1319 * remote device.
1320 * @isci_host: This parameter specifies the isci host object.
1321 * @isci_device: This parameter specifies the remote device.
1322 *
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001323 * The status of the ihost request to stop.
Dan Williams6f231dd2011-07-02 22:56:22 -07001324 */
Dan Williams6ad31fe2011-03-04 12:10:29 -08001325enum sci_status isci_remote_device_stop(struct isci_host *ihost, struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001326{
1327 enum sci_status status;
1328 unsigned long flags;
Dan Williams6f231dd2011-07-02 22:56:22 -07001329
Dan Williams6ad31fe2011-03-04 12:10:29 -08001330 dev_dbg(&ihost->pdev->dev,
1331 "%s: isci_device = %p\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001332
Dan Williams209fae12011-06-13 17:39:44 -07001333 spin_lock_irqsave(&ihost->scic_lock, flags);
1334 idev->domain_dev->lldd_dev = NULL; /* disable new lookups */
1335 set_bit(IDEV_GONE, &idev->flags);
Dan Williams209fae12011-06-13 17:39:44 -07001336 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Jeff Skirvin6e2802a2011-03-08 20:32:16 -07001337
1338 /* Kill all outstanding requests. */
Dan Williams4393aa42011-03-31 13:10:44 -07001339 isci_remote_device_nuke_requests(ihost, idev);
Jeff Skirvin6e2802a2011-03-08 20:32:16 -07001340
Dan Williams6ad31fe2011-03-04 12:10:29 -08001341 set_bit(IDEV_STOP_PENDING, &idev->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001342
Dan Williams6ad31fe2011-03-04 12:10:29 -08001343 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williams89a73012011-06-30 19:14:33 -07001344 status = sci_remote_device_stop(idev, 50);
Dan Williams6ad31fe2011-03-04 12:10:29 -08001345 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001346
1347 /* Wait for the stop complete callback. */
Dan Williams209fae12011-06-13 17:39:44 -07001348 if (WARN_ONCE(status != SCI_SUCCESS, "failed to stop device\n"))
1349 /* nothing to wait for */;
1350 else
Dan Williams6ad31fe2011-03-04 12:10:29 -08001351 wait_for_device_stop(ihost, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001352
Dan Williams6f231dd2011-07-02 22:56:22 -07001353 return status;
1354}
1355
1356/**
1357 * isci_remote_device_gone() - This function is called by libsas when a domain
1358 * device is removed.
1359 * @domain_device: This parameter specifies the libsas domain device.
1360 *
1361 */
Dan Williams6ad31fe2011-03-04 12:10:29 -08001362void isci_remote_device_gone(struct domain_device *dev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001363{
Dan Williams4393aa42011-03-31 13:10:44 -07001364 struct isci_host *ihost = dev_to_ihost(dev);
Dan Williams6ad31fe2011-03-04 12:10:29 -08001365 struct isci_remote_device *idev = dev->lldd_dev;
Dan Williams6f231dd2011-07-02 22:56:22 -07001366
Dan Williams6ad31fe2011-03-04 12:10:29 -08001367 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -07001368 "%s: domain_device = %p, isci_device = %p, isci_port = %p\n",
Dan Williams6ad31fe2011-03-04 12:10:29 -08001369 __func__, dev, idev, idev->isci_port);
Dan Williams6f231dd2011-07-02 22:56:22 -07001370
Dan Williams6ad31fe2011-03-04 12:10:29 -08001371 isci_remote_device_stop(ihost, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001372}
1373
1374
1375/**
1376 * isci_remote_device_found() - This function is called by libsas when a remote
1377 * device is discovered. A remote device object is created and started. the
1378 * function then sleeps until the sci core device started message is
1379 * received.
1380 * @domain_device: This parameter specifies the libsas domain device.
1381 *
1382 * status, zero indicates success.
1383 */
Dan Williamsc132f692012-01-03 23:26:08 -08001384int isci_remote_device_found(struct domain_device *dev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001385{
Dan Williamsc132f692012-01-03 23:26:08 -08001386 struct isci_host *isci_host = dev_to_ihost(dev);
1387 struct isci_port *isci_port = dev->port->lldd_port;
Dan Williams6f231dd2011-07-02 22:56:22 -07001388 struct isci_remote_device *isci_device;
1389 enum sci_status status;
Dan Williams6f231dd2011-07-02 22:56:22 -07001390
Dan Williams6f231dd2011-07-02 22:56:22 -07001391 dev_dbg(&isci_host->pdev->dev,
Dan Williamsc132f692012-01-03 23:26:08 -08001392 "%s: domain_device = %p\n", __func__, dev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001393
Dan Williamsc132f692012-01-03 23:26:08 -08001394 if (!isci_port)
1395 return -ENODEV;
Dan Williams6f231dd2011-07-02 22:56:22 -07001396
1397 if ((isci_stopping == isci_port_get_state(isci_port)) ||
1398 (isci_stopped == isci_port_get_state(isci_port)))
1399 return -ENODEV;
1400
1401 isci_device = isci_remote_device_alloc(isci_host, isci_port);
Dan Williamsd9c37392011-03-03 17:59:32 -08001402 if (!isci_device)
1403 return -ENODEV;
Dan Williams6f231dd2011-07-02 22:56:22 -07001404
Dan Williams209fae12011-06-13 17:39:44 -07001405 kref_init(&isci_device->kref);
Dan Williams6f231dd2011-07-02 22:56:22 -07001406 INIT_LIST_HEAD(&isci_device->node);
Dan Williams209fae12011-06-13 17:39:44 -07001407
1408 spin_lock_irq(&isci_host->scic_lock);
Dan Williamsc132f692012-01-03 23:26:08 -08001409 isci_device->domain_dev = dev;
Dan Williams6f231dd2011-07-02 22:56:22 -07001410 isci_device->isci_port = isci_port;
Dan Williams6f231dd2011-07-02 22:56:22 -07001411 list_add_tail(&isci_device->node, &isci_port->remote_dev_list);
1412
Dan Williams6ad31fe2011-03-04 12:10:29 -08001413 set_bit(IDEV_START_PENDING, &isci_device->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001414 status = isci_remote_device_construct(isci_port, isci_device);
Dan Williams6f231dd2011-07-02 22:56:22 -07001415
Dan Williams6f231dd2011-07-02 22:56:22 -07001416 dev_dbg(&isci_host->pdev->dev,
1417 "%s: isci_device = %p\n",
1418 __func__, isci_device);
1419
Dan Williams209fae12011-06-13 17:39:44 -07001420 if (status == SCI_SUCCESS) {
1421 /* device came up, advertise it to the world */
Dan Williamsc132f692012-01-03 23:26:08 -08001422 dev->lldd_dev = isci_device;
Dan Williams209fae12011-06-13 17:39:44 -07001423 } else
1424 isci_put_device(isci_device);
1425 spin_unlock_irq(&isci_host->scic_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07001426
Dan Williams6ad31fe2011-03-04 12:10:29 -08001427 /* wait for the device ready callback. */
1428 wait_for_device_start(isci_host, isci_device);
1429
Dan Williams209fae12011-06-13 17:39:44 -07001430 return status == SCI_SUCCESS ? 0 : -ENODEV;
Dan Williams6f231dd2011-07-02 22:56:22 -07001431}