blob: 68ab4fb9032e7aa1ea68a69802ee3c4ce98aa03a [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 Williamsd7a0ccd2012-02-10 01:18:44 -080065#undef C
66#define C(a) (#a)
67const char *dev_state_name(enum sci_remote_device_states state)
68{
69 static const char * const strings[] = REMOTE_DEV_STATES;
70
71 return strings[state];
72}
73#undef C
74
Jeff Skirvinc94fc1a2012-03-08 22:41:58 -080075static enum sci_status sci_remote_device_suspend(struct isci_remote_device *idev,
76 enum sci_remote_node_suspension_reasons reason)
Jeff Skirvin23ec2aa2012-03-08 22:41:52 -080077{
Jeff Skirvin447bfbc2012-03-08 22:41:59 -080078 return sci_remote_node_context_suspend(&idev->rnc, reason,
79 SCI_SOFTWARE_SUSPEND_EXPECTED_EVENT);
Jeff Skirvin23ec2aa2012-03-08 22:41:52 -080080}
81
Dan Williamsab2e8f72011-04-27 16:32:45 -070082/**
Dan Williamsd9dcb4b2011-06-30 17:38:32 -070083 * isci_remote_device_ready() - This function is called by the ihost when the
Dan Williamsab2e8f72011-04-27 16:32:45 -070084 * remote device is ready. We mark the isci device as ready and signal the
85 * waiting proccess.
86 * @ihost: our valid isci_host
87 * @idev: remote device
88 *
89 */
90static void isci_remote_device_ready(struct isci_host *ihost, struct isci_remote_device *idev)
91{
92 dev_dbg(&ihost->pdev->dev,
93 "%s: idev = %p\n", __func__, idev);
94
Jeff Skirvin9274f452011-06-23 17:09:02 -070095 clear_bit(IDEV_IO_NCQERROR, &idev->flags);
Dan Williamsf2088262011-06-16 11:26:12 -070096 set_bit(IDEV_IO_READY, &idev->flags);
Dan Williamsab2e8f72011-04-27 16:32:45 -070097 if (test_and_clear_bit(IDEV_START_PENDING, &idev->flags))
98 wake_up(&ihost->eventq);
99}
100
Jeff Skirvin14aaa9f2012-03-08 22:41:54 -0800101static enum sci_status sci_remote_device_terminate_req(
102 struct isci_host *ihost,
103 struct isci_remote_device *idev,
104 int check_abort,
105 struct isci_request *ireq)
Jeff Skirvin5b6bf222012-03-08 22:41:51 -0800106{
Jeff Skirvin14aaa9f2012-03-08 22:41:54 -0800107 if (!test_bit(IREQ_ACTIVE, &ireq->flags) ||
108 (ireq->target_device != idev) ||
109 (check_abort && !test_bit(IREQ_PENDING_ABORT, &ireq->flags)))
110 return SCI_SUCCESS;
111
Jeff Skirvin621120c2012-03-08 22:42:03 -0800112 dev_dbg(&ihost->pdev->dev,
113 "%s: idev=%p; flags=%lx; req=%p; req target=%p\n",
114 __func__, idev, idev->flags, ireq, ireq->target_device);
115
Jeff Skirvin14aaa9f2012-03-08 22:41:54 -0800116 set_bit(IREQ_ABORT_PATH_ACTIVE, &ireq->flags);
117
118 return sci_controller_terminate_request(ihost, idev, ireq);
Jeff Skirvin5b6bf222012-03-08 22:41:51 -0800119}
120
Jeff Skirvin14aaa9f2012-03-08 22:41:54 -0800121static enum sci_status sci_remote_device_terminate_reqs_checkabort(
122 struct isci_remote_device *idev,
123 int chk)
Jeff Skirvin726980d2012-03-08 22:41:50 -0800124{
Jeff Skirvin14aaa9f2012-03-08 22:41:54 -0800125 struct isci_host *ihost = idev->owning_port->owning_controller;
126 enum sci_status status = SCI_SUCCESS;
127 u32 i;
128
129 for (i = 0; i < SCI_MAX_IO_REQUESTS; i++) {
130 struct isci_request *ireq = ihost->reqs[i];
131 enum sci_status s;
132
133 s = sci_remote_device_terminate_req(ihost, idev, chk, ireq);
134 if (s != SCI_SUCCESS)
135 status = s;
136 }
137 return status;
138}
139
Jeff Skirvin31a38ef2012-03-08 22:42:01 -0800140static bool isci_compare_suspendcount(
141 struct isci_remote_device *idev,
142 u32 localcount)
143{
144 smp_rmb();
Jeff Skirvin1f053882012-03-08 22:42:08 -0800145
146 /* Check for a change in the suspend count, or the RNC
147 * being destroyed.
148 */
149 return (localcount != idev->rnc.suspend_count)
150 || sci_remote_node_context_is_being_destroyed(&idev->rnc);
Jeff Skirvin31a38ef2012-03-08 22:42:01 -0800151}
152
Jeff Skirvin033d19d2012-03-08 22:42:03 -0800153static bool isci_check_reqterm(
154 struct isci_host *ihost,
155 struct isci_remote_device *idev,
156 struct isci_request *ireq,
157 u32 localcount)
158{
159 unsigned long flags;
160 bool res;
161
162 spin_lock_irqsave(&ihost->scic_lock, flags);
163 res = isci_compare_suspendcount(idev, localcount)
164 && !test_bit(IREQ_ABORT_PATH_ACTIVE, &ireq->flags);
165 spin_unlock_irqrestore(&ihost->scic_lock, flags);
166
167 return res;
168}
169
170static bool isci_check_devempty(
171 struct isci_host *ihost,
172 struct isci_remote_device *idev,
173 u32 localcount)
174{
175 unsigned long flags;
176 bool res;
177
178 spin_lock_irqsave(&ihost->scic_lock, flags);
179 res = isci_compare_suspendcount(idev, localcount)
180 && idev->started_request_count == 0;
181 spin_unlock_irqrestore(&ihost->scic_lock, flags);
182
183 return res;
184}
185
Jeff Skirvin14aaa9f2012-03-08 22:41:54 -0800186enum sci_status isci_remote_device_terminate_requests(
187 struct isci_host *ihost,
188 struct isci_remote_device *idev,
189 struct isci_request *ireq)
190{
191 enum sci_status status = SCI_SUCCESS;
Jeff Skirvin726980d2012-03-08 22:41:50 -0800192 unsigned long flags;
Jeff Skirvin31a38ef2012-03-08 22:42:01 -0800193 u32 rnc_suspend_count;
Jeff Skirvin726980d2012-03-08 22:41:50 -0800194
195 spin_lock_irqsave(&ihost->scic_lock, flags);
Jeff Skirvin31a38ef2012-03-08 22:42:01 -0800196
Jeff Skirvin14aaa9f2012-03-08 22:41:54 -0800197 if (isci_get_device(idev) == NULL) {
198 dev_dbg(&ihost->pdev->dev, "%s: failed isci_get_device(idev=%p)\n",
199 __func__, idev);
Jeff Skirvin726980d2012-03-08 22:41:50 -0800200 spin_unlock_irqrestore(&ihost->scic_lock, flags);
201 status = SCI_FAILURE;
202 } else {
Jeff Skirvin31a38ef2012-03-08 22:42:01 -0800203 /* If already suspended, don't wait for another suspension. */
204 smp_rmb();
205 rnc_suspend_count
206 = sci_remote_node_context_is_suspended(&idev->rnc)
207 ? 0 : idev->rnc.suspend_count;
208
Jeff Skirvin14aaa9f2012-03-08 22:41:54 -0800209 dev_dbg(&ihost->pdev->dev,
210 "%s: idev=%p, ireq=%p; started_request_count=%d, "
Jeff Skirvin31a38ef2012-03-08 22:42:01 -0800211 "rnc_suspend_count=%d, rnc.suspend_count=%d"
Jeff Skirvin14aaa9f2012-03-08 22:41:54 -0800212 "about to wait\n",
Jeff Skirvin31a38ef2012-03-08 22:42:01 -0800213 __func__, idev, ireq, idev->started_request_count,
214 rnc_suspend_count, idev->rnc.suspend_count);
Jeff Skirvin8c731882012-03-08 22:42:06 -0800215
216 #define MAX_SUSPEND_MSECS 10000
Jeff Skirvin14aaa9f2012-03-08 22:41:54 -0800217 if (ireq) {
218 /* Terminate a specific TC. */
Jeff Skirvin621120c2012-03-08 22:42:03 -0800219 set_bit(IREQ_NO_AUTO_FREE_TAG, &ireq->flags);
Jeff Skirvin14aaa9f2012-03-08 22:41:54 -0800220 sci_remote_device_terminate_req(ihost, idev, 0, ireq);
221 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Jeff Skirvin8c731882012-03-08 22:42:06 -0800222 if (!wait_event_timeout(ihost->eventq,
223 isci_check_reqterm(ihost, idev, ireq,
224 rnc_suspend_count),
225 msecs_to_jiffies(MAX_SUSPEND_MSECS))) {
226
227 dev_warn(&ihost->pdev->dev, "%s host%d timeout single\n",
228 __func__, ihost->id);
229 dev_dbg(&ihost->pdev->dev,
230 "%s: ******* Timeout waiting for "
231 "suspend; idev=%p, current state %s; "
232 "started_request_count=%d, flags=%lx\n\t"
233 "rnc_suspend_count=%d, rnc.suspend_count=%d "
234 "RNC: current state %s, current "
235 "suspend_type %x dest state %d;\n"
236 "ireq=%p, ireq->flags = %lx\n",
237 __func__, idev,
238 dev_state_name(idev->sm.current_state_id),
239 idev->started_request_count, idev->flags,
240 rnc_suspend_count, idev->rnc.suspend_count,
241 rnc_state_name(idev->rnc.sm.current_state_id),
242 idev->rnc.suspend_type,
243 idev->rnc.destination_state,
244 ireq, ireq->flags);
245 }
Jeff Skirvin621120c2012-03-08 22:42:03 -0800246 clear_bit(IREQ_NO_AUTO_FREE_TAG, &ireq->flags);
247 isci_free_tag(ihost, ireq->io_tag);
Jeff Skirvin14aaa9f2012-03-08 22:41:54 -0800248 } else {
249 /* Terminate all TCs. */
250 sci_remote_device_terminate_requests(idev);
251 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Jeff Skirvin8c731882012-03-08 22:42:06 -0800252 if (!wait_event_timeout(ihost->eventq,
253 isci_check_devempty(ihost, idev,
254 rnc_suspend_count),
255 msecs_to_jiffies(MAX_SUSPEND_MSECS))) {
256
257 dev_warn(&ihost->pdev->dev, "%s host%d timeout all\n",
258 __func__, ihost->id);
259 dev_dbg(&ihost->pdev->dev,
260 "%s: ******* Timeout waiting for "
261 "suspend; idev=%p, current state %s; "
262 "started_request_count=%d, flags=%lx\n\t"
263 "rnc_suspend_count=%d, "
264 "RNC: current state %s, "
265 "rnc.suspend_count=%d, current "
266 "suspend_type %x dest state %d\n",
267 __func__, idev,
268 dev_state_name(idev->sm.current_state_id),
269 idev->started_request_count, idev->flags,
270 rnc_suspend_count,
271 rnc_state_name(idev->rnc.sm.current_state_id),
272 idev->rnc.suspend_count,
273 idev->rnc.suspend_type,
274 idev->rnc.destination_state);
275 }
Jeff Skirvin14aaa9f2012-03-08 22:41:54 -0800276 }
277 dev_dbg(&ihost->pdev->dev, "%s: idev=%p, wait done\n",
278 __func__, idev);
Jeff Skirvin726980d2012-03-08 22:41:50 -0800279 isci_put_device(idev);
280 }
281 return status;
282}
283
Jeff Skirvin14aaa9f2012-03-08 22:41:54 -0800284/**
285* isci_remote_device_not_ready() - This function is called by the ihost when
286* the remote device is not ready. We mark the isci device as ready (not
287* "ready_for_io") and signal the waiting proccess.
288* @isci_host: This parameter specifies the isci host object.
289* @isci_device: This parameter specifies the remote device
290*
291* sci_lock is held on entrance to this function.
292*/
293static void isci_remote_device_not_ready(struct isci_host *ihost,
294 struct isci_remote_device *idev,
295 u32 reason)
296{
297 dev_dbg(&ihost->pdev->dev,
Jeff Skirvin59e35392012-03-08 22:41:54 -0800298 "%s: isci_device = %p; reason = %d\n", __func__, idev, reason);
Jeff Skirvin14aaa9f2012-03-08 22:41:54 -0800299
300 switch (reason) {
Jeff Skirvin14aaa9f2012-03-08 22:41:54 -0800301 case SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED:
302 set_bit(IDEV_IO_NCQERROR, &idev->flags);
303
304 /* Suspend the remote device so the I/O can be terminated. */
Jeff Skirvinc94fc1a2012-03-08 22:41:58 -0800305 sci_remote_device_suspend(idev, SCI_SW_SUSPEND_NORMAL);
Jeff Skirvin14aaa9f2012-03-08 22:41:54 -0800306
307 /* Kill all outstanding requests for the device. */
308 sci_remote_device_terminate_requests(idev);
309
310 /* Fall through into the default case... */
311 default:
312 clear_bit(IDEV_IO_READY, &idev->flags);
313 break;
314 }
315}
316
Dan Williamsec575662011-05-01 14:19:25 -0700317/* called once the remote node context is ready to be freed.
318 * The remote device can now report that its stop operation is complete. none
319 */
320static void rnc_destruct_done(void *_dev)
Dan Williams88f3b622011-04-22 19:18:03 -0700321{
Dan Williams78a6f062011-06-30 16:31:37 -0700322 struct isci_remote_device *idev = _dev;
Dan Williamsec575662011-05-01 14:19:25 -0700323
Dan Williams78a6f062011-06-30 16:31:37 -0700324 BUG_ON(idev->started_request_count != 0);
325 sci_change_state(&idev->sm, SCI_DEV_STOPPED);
Dan Williamsec575662011-05-01 14:19:25 -0700326}
327
Jeff Skirvin726980d2012-03-08 22:41:50 -0800328enum sci_status sci_remote_device_terminate_requests(
329 struct isci_remote_device *idev)
330{
Jeff Skirvin14aaa9f2012-03-08 22:41:54 -0800331 return sci_remote_device_terminate_reqs_checkabort(idev, 0);
Jeff Skirvin726980d2012-03-08 22:41:50 -0800332}
333
Dan Williams89a73012011-06-30 19:14:33 -0700334enum sci_status sci_remote_device_stop(struct isci_remote_device *idev,
Dan Williamsec575662011-05-01 14:19:25 -0700335 u32 timeout)
336{
Dan Williams78a6f062011-06-30 16:31:37 -0700337 struct sci_base_state_machine *sm = &idev->sm;
Dan Williams89a73012011-06-30 19:14:33 -0700338 enum sci_remote_device_states state = sm->current_state_id;
Dan Williamsec575662011-05-01 14:19:25 -0700339
340 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000341 case SCI_DEV_INITIAL:
342 case SCI_DEV_FAILED:
343 case SCI_DEV_FINAL:
Dan Williamsec575662011-05-01 14:19:25 -0700344 default:
Dan Williamsd7a0ccd2012-02-10 01:18:44 -0800345 dev_warn(scirdev_to_dev(idev), "%s: in wrong state: %s\n",
346 __func__, dev_state_name(state));
Dan Williamsec575662011-05-01 14:19:25 -0700347 return SCI_FAILURE_INVALID_STATE;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000348 case SCI_DEV_STOPPED:
Dan Williamsec575662011-05-01 14:19:25 -0700349 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000350 case SCI_DEV_STARTING:
Dan Williamsec575662011-05-01 14:19:25 -0700351 /* device not started so there had better be no requests */
Dan Williams78a6f062011-06-30 16:31:37 -0700352 BUG_ON(idev->started_request_count != 0);
Dan Williams89a73012011-06-30 19:14:33 -0700353 sci_remote_node_context_destruct(&idev->rnc,
Dan Williams78a6f062011-06-30 16:31:37 -0700354 rnc_destruct_done, idev);
Dan Williamsec575662011-05-01 14:19:25 -0700355 /* Transition to the stopping state and wait for the
356 * remote node to complete being posted and invalidated.
357 */
Edmund Nadolskie3013702011-06-02 00:10:43 +0000358 sci_change_state(sm, SCI_DEV_STOPPING);
Dan Williamsec575662011-05-01 14:19:25 -0700359 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000360 case SCI_DEV_READY:
361 case SCI_STP_DEV_IDLE:
362 case SCI_STP_DEV_CMD:
363 case SCI_STP_DEV_NCQ:
364 case SCI_STP_DEV_NCQ_ERROR:
365 case SCI_STP_DEV_AWAIT_RESET:
366 case SCI_SMP_DEV_IDLE:
367 case SCI_SMP_DEV_CMD:
368 sci_change_state(sm, SCI_DEV_STOPPING);
Jeff Skirvin83884012012-03-08 22:41:52 -0800369 if (idev->started_request_count == 0)
Dan Williams89a73012011-06-30 19:14:33 -0700370 sci_remote_node_context_destruct(&idev->rnc,
Jeff Skirvin83884012012-03-08 22:41:52 -0800371 rnc_destruct_done,
372 idev);
373 else {
Jeff Skirvinc94fc1a2012-03-08 22:41:58 -0800374 sci_remote_device_suspend(
375 idev, SCI_SW_SUSPEND_LINKHANG_DETECT);
Jeff Skirvin83884012012-03-08 22:41:52 -0800376 sci_remote_device_terminate_requests(idev);
377 }
378 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000379 case SCI_DEV_STOPPING:
Dan Williamsec575662011-05-01 14:19:25 -0700380 /* All requests should have been terminated, but if there is an
381 * attempt to stop a device already in the stopping state, then
382 * try again to terminate.
383 */
Dan Williams89a73012011-06-30 19:14:33 -0700384 return sci_remote_device_terminate_requests(idev);
Edmund Nadolskie3013702011-06-02 00:10:43 +0000385 case SCI_DEV_RESETTING:
386 sci_change_state(sm, SCI_DEV_STOPPING);
Dan Williamsec575662011-05-01 14:19:25 -0700387 return SCI_SUCCESS;
388 }
Dan Williams88f3b622011-04-22 19:18:03 -0700389}
Dan Williams6f231dd2011-07-02 22:56:22 -0700390
Dan Williams89a73012011-06-30 19:14:33 -0700391enum sci_status sci_remote_device_reset(struct isci_remote_device *idev)
Dan Williams88f3b622011-04-22 19:18:03 -0700392{
Dan Williams78a6f062011-06-30 16:31:37 -0700393 struct sci_base_state_machine *sm = &idev->sm;
Dan Williams89a73012011-06-30 19:14:33 -0700394 enum sci_remote_device_states state = sm->current_state_id;
Dan Williams4fd0d2e2011-05-01 14:48:54 -0700395
396 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000397 case SCI_DEV_INITIAL:
398 case SCI_DEV_STOPPED:
399 case SCI_DEV_STARTING:
400 case SCI_SMP_DEV_IDLE:
401 case SCI_SMP_DEV_CMD:
402 case SCI_DEV_STOPPING:
403 case SCI_DEV_FAILED:
404 case SCI_DEV_RESETTING:
405 case SCI_DEV_FINAL:
Dan Williams4fd0d2e2011-05-01 14:48:54 -0700406 default:
Dan Williamsd7a0ccd2012-02-10 01:18:44 -0800407 dev_warn(scirdev_to_dev(idev), "%s: in wrong state: %s\n",
408 __func__, dev_state_name(state));
Dan Williams4fd0d2e2011-05-01 14:48:54 -0700409 return SCI_FAILURE_INVALID_STATE;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000410 case SCI_DEV_READY:
411 case SCI_STP_DEV_IDLE:
412 case SCI_STP_DEV_CMD:
413 case SCI_STP_DEV_NCQ:
414 case SCI_STP_DEV_NCQ_ERROR:
415 case SCI_STP_DEV_AWAIT_RESET:
416 sci_change_state(sm, SCI_DEV_RESETTING);
Dan Williams4fd0d2e2011-05-01 14:48:54 -0700417 return SCI_SUCCESS;
418 }
Dan Williams88f3b622011-04-22 19:18:03 -0700419}
420
Dan Williams89a73012011-06-30 19:14:33 -0700421enum sci_status sci_remote_device_reset_complete(struct isci_remote_device *idev)
Dan Williams88f3b622011-04-22 19:18:03 -0700422{
Dan Williams78a6f062011-06-30 16:31:37 -0700423 struct sci_base_state_machine *sm = &idev->sm;
Dan Williams89a73012011-06-30 19:14:33 -0700424 enum sci_remote_device_states state = sm->current_state_id;
Dan Williams88f3b622011-04-22 19:18:03 -0700425
Edmund Nadolskie3013702011-06-02 00:10:43 +0000426 if (state != SCI_DEV_RESETTING) {
Dan Williamsd7a0ccd2012-02-10 01:18:44 -0800427 dev_warn(scirdev_to_dev(idev), "%s: in wrong state: %s\n",
428 __func__, dev_state_name(state));
Dan Williams81515182011-05-01 14:53:00 -0700429 return SCI_FAILURE_INVALID_STATE;
430 }
431
Edmund Nadolskie3013702011-06-02 00:10:43 +0000432 sci_change_state(sm, SCI_DEV_READY);
Dan Williams81515182011-05-01 14:53:00 -0700433 return SCI_SUCCESS;
434}
Dan Williams88f3b622011-04-22 19:18:03 -0700435
Dan Williams89a73012011-06-30 19:14:33 -0700436enum sci_status sci_remote_device_frame_handler(struct isci_remote_device *idev,
Dan Williams01bec772011-05-01 16:51:11 -0700437 u32 frame_index)
Dan Williams88f3b622011-04-22 19:18:03 -0700438{
Dan Williams78a6f062011-06-30 16:31:37 -0700439 struct sci_base_state_machine *sm = &idev->sm;
Dan Williams89a73012011-06-30 19:14:33 -0700440 enum sci_remote_device_states state = sm->current_state_id;
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700441 struct isci_host *ihost = idev->owning_port->owning_controller;
Dan Williams01bec772011-05-01 16:51:11 -0700442 enum sci_status status;
443
444 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000445 case SCI_DEV_INITIAL:
446 case SCI_DEV_STOPPED:
447 case SCI_DEV_STARTING:
448 case SCI_STP_DEV_IDLE:
449 case SCI_SMP_DEV_IDLE:
450 case SCI_DEV_FINAL:
Dan Williams01bec772011-05-01 16:51:11 -0700451 default:
Dan Williamsd7a0ccd2012-02-10 01:18:44 -0800452 dev_warn(scirdev_to_dev(idev), "%s: in wrong state: %s\n",
453 __func__, dev_state_name(state));
Dan Williams01bec772011-05-01 16:51:11 -0700454 /* Return the frame back to the controller */
Dan Williams89a73012011-06-30 19:14:33 -0700455 sci_controller_release_frame(ihost, frame_index);
Dan Williams01bec772011-05-01 16:51:11 -0700456 return SCI_FAILURE_INVALID_STATE;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000457 case SCI_DEV_READY:
458 case SCI_STP_DEV_NCQ_ERROR:
459 case SCI_STP_DEV_AWAIT_RESET:
460 case SCI_DEV_STOPPING:
461 case SCI_DEV_FAILED:
462 case SCI_DEV_RESETTING: {
Dan Williams5076a1a2011-06-27 14:57:03 -0700463 struct isci_request *ireq;
Dave Jiang2d9c2242011-05-04 18:45:05 -0700464 struct ssp_frame_hdr hdr;
465 void *frame_header;
466 ssize_t word_cnt;
Dan Williams01bec772011-05-01 16:51:11 -0700467
Dan Williams89a73012011-06-30 19:14:33 -0700468 status = sci_unsolicited_frame_control_get_header(&ihost->uf_control,
Dan Williams01bec772011-05-01 16:51:11 -0700469 frame_index,
Dave Jiang2d9c2242011-05-04 18:45:05 -0700470 &frame_header);
Dan Williams01bec772011-05-01 16:51:11 -0700471 if (status != SCI_SUCCESS)
472 return status;
473
Dave Jiang2d9c2242011-05-04 18:45:05 -0700474 word_cnt = sizeof(hdr) / sizeof(u32);
475 sci_swab32_cpy(&hdr, frame_header, word_cnt);
476
Dan Williams89a73012011-06-30 19:14:33 -0700477 ireq = sci_request_by_tag(ihost, be16_to_cpu(hdr.tag));
Dan Williams78a6f062011-06-30 16:31:37 -0700478 if (ireq && ireq->target_device == idev) {
Dan Williams01bec772011-05-01 16:51:11 -0700479 /* The IO request is now in charge of releasing the frame */
Dan Williams89a73012011-06-30 19:14:33 -0700480 status = sci_io_request_frame_handler(ireq, frame_index);
Dan Williams01bec772011-05-01 16:51:11 -0700481 } else {
482 /* We could not map this tag to a valid IO
483 * request Just toss the frame and continue
484 */
Dan Williams89a73012011-06-30 19:14:33 -0700485 sci_controller_release_frame(ihost, frame_index);
Dan Williams01bec772011-05-01 16:51:11 -0700486 }
487 break;
488 }
Edmund Nadolskie3013702011-06-02 00:10:43 +0000489 case SCI_STP_DEV_NCQ: {
Dave Jiange76d6182011-05-04 15:02:03 -0700490 struct dev_to_host_fis *hdr;
Dan Williams01bec772011-05-01 16:51:11 -0700491
Dan Williams89a73012011-06-30 19:14:33 -0700492 status = sci_unsolicited_frame_control_get_header(&ihost->uf_control,
Dan Williams01bec772011-05-01 16:51:11 -0700493 frame_index,
494 (void **)&hdr);
495 if (status != SCI_SUCCESS)
496 return status;
497
Dave Jiange76d6182011-05-04 15:02:03 -0700498 if (hdr->fis_type == FIS_SETDEVBITS &&
499 (hdr->status & ATA_ERR)) {
Dan Williams78a6f062011-06-30 16:31:37 -0700500 idev->not_ready_reason = SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED;
Dan Williams01bec772011-05-01 16:51:11 -0700501
502 /* TODO Check sactive and complete associated IO if any. */
Edmund Nadolskie3013702011-06-02 00:10:43 +0000503 sci_change_state(sm, SCI_STP_DEV_NCQ_ERROR);
Dave Jiange76d6182011-05-04 15:02:03 -0700504 } else if (hdr->fis_type == FIS_REGD2H &&
505 (hdr->status & ATA_ERR)) {
Dan Williams01bec772011-05-01 16:51:11 -0700506 /*
507 * Some devices return D2H FIS when an NCQ error is detected.
508 * Treat this like an SDB error FIS ready reason.
509 */
Dan Williams78a6f062011-06-30 16:31:37 -0700510 idev->not_ready_reason = SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED;
511 sci_change_state(&idev->sm, SCI_STP_DEV_NCQ_ERROR);
Dan Williams01bec772011-05-01 16:51:11 -0700512 } else
513 status = SCI_FAILURE;
514
Dan Williams89a73012011-06-30 19:14:33 -0700515 sci_controller_release_frame(ihost, frame_index);
Dan Williams01bec772011-05-01 16:51:11 -0700516 break;
517 }
Edmund Nadolskie3013702011-06-02 00:10:43 +0000518 case SCI_STP_DEV_CMD:
519 case SCI_SMP_DEV_CMD:
Dan Williams01bec772011-05-01 16:51:11 -0700520 /* The device does not process any UF received from the hardware while
521 * in this state. All unsolicited frames are forwarded to the io request
522 * object.
523 */
Dan Williams89a73012011-06-30 19:14:33 -0700524 status = sci_io_request_frame_handler(idev->working_request, frame_index);
Dan Williams01bec772011-05-01 16:51:11 -0700525 break;
526 }
527
528 return status;
Dan Williams88f3b622011-04-22 19:18:03 -0700529}
530
Dan Williams78a6f062011-06-30 16:31:37 -0700531static bool is_remote_device_ready(struct isci_remote_device *idev)
Dan Williams88f3b622011-04-22 19:18:03 -0700532{
Dan Williamse6225712011-05-01 16:26:09 -0700533
Dan Williams78a6f062011-06-30 16:31:37 -0700534 struct sci_base_state_machine *sm = &idev->sm;
Dan Williams89a73012011-06-30 19:14:33 -0700535 enum sci_remote_device_states state = sm->current_state_id;
Dan Williamse6225712011-05-01 16:26:09 -0700536
537 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000538 case SCI_DEV_READY:
539 case SCI_STP_DEV_IDLE:
540 case SCI_STP_DEV_CMD:
541 case SCI_STP_DEV_NCQ:
542 case SCI_STP_DEV_NCQ_ERROR:
543 case SCI_STP_DEV_AWAIT_RESET:
544 case SCI_SMP_DEV_IDLE:
545 case SCI_SMP_DEV_CMD:
Dan Williamse6225712011-05-01 16:26:09 -0700546 return true;
547 default:
548 return false;
549 }
550}
551
Dan Williamsb50102d2011-09-30 18:52:19 -0700552/*
553 * called once the remote node context has transisitioned to a ready
554 * state (after suspending RX and/or TX due to early D2H fis)
555 */
556static void atapi_remote_device_resume_done(void *_dev)
557{
558 struct isci_remote_device *idev = _dev;
559 struct isci_request *ireq = idev->working_request;
560
561 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
562}
563
Dan Williams89a73012011-06-30 19:14:33 -0700564enum sci_status sci_remote_device_event_handler(struct isci_remote_device *idev,
Dan Williamse6225712011-05-01 16:26:09 -0700565 u32 event_code)
566{
Dan Williamse6225712011-05-01 16:26:09 -0700567 enum sci_status status;
568
569 switch (scu_get_event_type(event_code)) {
570 case SCU_EVENT_TYPE_RNC_OPS_MISC:
571 case SCU_EVENT_TYPE_RNC_SUSPEND_TX:
572 case SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX:
Dan Williams89a73012011-06-30 19:14:33 -0700573 status = sci_remote_node_context_event_handler(&idev->rnc, event_code);
Dan Williamse6225712011-05-01 16:26:09 -0700574 break;
575 case SCU_EVENT_TYPE_PTX_SCHEDULE_EVENT:
576 if (scu_get_event_code(event_code) == SCU_EVENT_IT_NEXUS_TIMEOUT) {
577 status = SCI_SUCCESS;
578
579 /* Suspend the associated RNC */
Jeff Skirvinc94fc1a2012-03-08 22:41:58 -0800580 sci_remote_device_suspend(idev, SCI_SW_SUSPEND_NORMAL);
Dan Williamse6225712011-05-01 16:26:09 -0700581
Dan Williams78a6f062011-06-30 16:31:37 -0700582 dev_dbg(scirdev_to_dev(idev),
Dan Williamse6225712011-05-01 16:26:09 -0700583 "%s: device: %p event code: %x: %s\n",
Dan Williams78a6f062011-06-30 16:31:37 -0700584 __func__, idev, event_code,
585 is_remote_device_ready(idev)
Dan Williamse6225712011-05-01 16:26:09 -0700586 ? "I_T_Nexus_Timeout event"
587 : "I_T_Nexus_Timeout event in wrong state");
588
589 break;
590 }
591 /* Else, fall through and treat as unhandled... */
592 default:
Dan Williams78a6f062011-06-30 16:31:37 -0700593 dev_dbg(scirdev_to_dev(idev),
Dan Williamse6225712011-05-01 16:26:09 -0700594 "%s: device: %p event code: %x: %s\n",
Dan Williams78a6f062011-06-30 16:31:37 -0700595 __func__, idev, event_code,
596 is_remote_device_ready(idev)
Dan Williamse6225712011-05-01 16:26:09 -0700597 ? "unexpected event"
598 : "unexpected event in wrong state");
599 status = SCI_FAILURE_INVALID_STATE;
600 break;
601 }
602
603 if (status != SCI_SUCCESS)
604 return status;
605
Dan Williamse6225712011-05-01 16:26:09 -0700606 return status;
Dan Williams88f3b622011-04-22 19:18:03 -0700607}
608
Dan Williams89a73012011-06-30 19:14:33 -0700609static void sci_remote_device_start_request(struct isci_remote_device *idev,
Dan Williams5076a1a2011-06-27 14:57:03 -0700610 struct isci_request *ireq,
Dan Williams18606552011-05-01 14:57:11 -0700611 enum sci_status status)
Dan Williams88f3b622011-04-22 19:18:03 -0700612{
Dan Williams78a6f062011-06-30 16:31:37 -0700613 struct isci_port *iport = idev->owning_port;
Dan Williams18606552011-05-01 14:57:11 -0700614
615 /* cleanup requests that failed after starting on the port */
616 if (status != SCI_SUCCESS)
Dan Williams89a73012011-06-30 19:14:33 -0700617 sci_port_complete_io(iport, idev, ireq);
Dan Williams209fae12011-06-13 17:39:44 -0700618 else {
Dan Williams78a6f062011-06-30 16:31:37 -0700619 kref_get(&idev->kref);
Dan Williams34a99152011-07-01 02:25:15 -0700620 idev->started_request_count++;
Dan Williams209fae12011-06-13 17:39:44 -0700621 }
Dan Williams18606552011-05-01 14:57:11 -0700622}
623
Dan Williams89a73012011-06-30 19:14:33 -0700624enum sci_status sci_remote_device_start_io(struct isci_host *ihost,
Dan Williams78a6f062011-06-30 16:31:37 -0700625 struct isci_remote_device *idev,
Dan Williams5076a1a2011-06-27 14:57:03 -0700626 struct isci_request *ireq)
Dan Williams18606552011-05-01 14:57:11 -0700627{
Dan Williams78a6f062011-06-30 16:31:37 -0700628 struct sci_base_state_machine *sm = &idev->sm;
Dan Williams89a73012011-06-30 19:14:33 -0700629 enum sci_remote_device_states state = sm->current_state_id;
Dan Williams78a6f062011-06-30 16:31:37 -0700630 struct isci_port *iport = idev->owning_port;
Dan Williams18606552011-05-01 14:57:11 -0700631 enum sci_status status;
632
633 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000634 case SCI_DEV_INITIAL:
635 case SCI_DEV_STOPPED:
636 case SCI_DEV_STARTING:
637 case SCI_STP_DEV_NCQ_ERROR:
638 case SCI_DEV_STOPPING:
639 case SCI_DEV_FAILED:
640 case SCI_DEV_RESETTING:
641 case SCI_DEV_FINAL:
Dan Williams18606552011-05-01 14:57:11 -0700642 default:
Dan Williamsd7a0ccd2012-02-10 01:18:44 -0800643 dev_warn(scirdev_to_dev(idev), "%s: in wrong state: %s\n",
644 __func__, dev_state_name(state));
Dan Williams18606552011-05-01 14:57:11 -0700645 return SCI_FAILURE_INVALID_STATE;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000646 case SCI_DEV_READY:
Dan Williams18606552011-05-01 14:57:11 -0700647 /* attempt to start an io request for this device object. The remote
648 * device object will issue the start request for the io and if
649 * successful it will start the request for the port object then
650 * increment its own request count.
651 */
Dan Williams89a73012011-06-30 19:14:33 -0700652 status = sci_port_start_io(iport, idev, ireq);
Dan Williams18606552011-05-01 14:57:11 -0700653 if (status != SCI_SUCCESS)
654 return status;
655
Dan Williams89a73012011-06-30 19:14:33 -0700656 status = sci_remote_node_context_start_io(&idev->rnc, ireq);
Dan Williams18606552011-05-01 14:57:11 -0700657 if (status != SCI_SUCCESS)
658 break;
659
Dan Williams89a73012011-06-30 19:14:33 -0700660 status = sci_request_start(ireq);
Dan Williams18606552011-05-01 14:57:11 -0700661 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000662 case SCI_STP_DEV_IDLE: {
Dan Williams18606552011-05-01 14:57:11 -0700663 /* handle the start io operation for a sata device that is in
664 * the command idle state. - Evalute the type of IO request to
665 * be started - If its an NCQ request change to NCQ substate -
666 * If its any other command change to the CMD substate
667 *
668 * If this is a softreset we may want to have a different
669 * substate.
670 */
Dan Williams89a73012011-06-30 19:14:33 -0700671 enum sci_remote_device_states new_state;
Dave Jiange76d6182011-05-04 15:02:03 -0700672 struct sas_task *task = isci_request_access_task(ireq);
Dan Williams18606552011-05-01 14:57:11 -0700673
Dan Williams89a73012011-06-30 19:14:33 -0700674 status = sci_port_start_io(iport, idev, ireq);
Dan Williams18606552011-05-01 14:57:11 -0700675 if (status != SCI_SUCCESS)
676 return status;
677
Dan Williams89a73012011-06-30 19:14:33 -0700678 status = sci_remote_node_context_start_io(&idev->rnc, ireq);
Dan Williams18606552011-05-01 14:57:11 -0700679 if (status != SCI_SUCCESS)
680 break;
681
Dan Williams89a73012011-06-30 19:14:33 -0700682 status = sci_request_start(ireq);
Dan Williams18606552011-05-01 14:57:11 -0700683 if (status != SCI_SUCCESS)
684 break;
685
Dave Jiange76d6182011-05-04 15:02:03 -0700686 if (task->ata_task.use_ncq)
Edmund Nadolskie3013702011-06-02 00:10:43 +0000687 new_state = SCI_STP_DEV_NCQ;
Dan Williams18606552011-05-01 14:57:11 -0700688 else {
Dan Williams78a6f062011-06-30 16:31:37 -0700689 idev->working_request = ireq;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000690 new_state = SCI_STP_DEV_CMD;
Dan Williams18606552011-05-01 14:57:11 -0700691 }
Edmund Nadolskie3013702011-06-02 00:10:43 +0000692 sci_change_state(sm, new_state);
Dan Williams18606552011-05-01 14:57:11 -0700693 break;
694 }
Edmund Nadolskie3013702011-06-02 00:10:43 +0000695 case SCI_STP_DEV_NCQ: {
Dave Jiange76d6182011-05-04 15:02:03 -0700696 struct sas_task *task = isci_request_access_task(ireq);
697
698 if (task->ata_task.use_ncq) {
Dan Williams89a73012011-06-30 19:14:33 -0700699 status = sci_port_start_io(iport, idev, ireq);
Dan Williams18606552011-05-01 14:57:11 -0700700 if (status != SCI_SUCCESS)
701 return status;
702
Dan Williams89a73012011-06-30 19:14:33 -0700703 status = sci_remote_node_context_start_io(&idev->rnc, ireq);
Dan Williams18606552011-05-01 14:57:11 -0700704 if (status != SCI_SUCCESS)
705 break;
706
Dan Williams89a73012011-06-30 19:14:33 -0700707 status = sci_request_start(ireq);
Dan Williams18606552011-05-01 14:57:11 -0700708 } else
709 return SCI_FAILURE_INVALID_STATE;
710 break;
Dave Jiange76d6182011-05-04 15:02:03 -0700711 }
Edmund Nadolskie3013702011-06-02 00:10:43 +0000712 case SCI_STP_DEV_AWAIT_RESET:
Dan Williams18606552011-05-01 14:57:11 -0700713 return SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000714 case SCI_SMP_DEV_IDLE:
Dan Williams89a73012011-06-30 19:14:33 -0700715 status = sci_port_start_io(iport, idev, ireq);
Dan Williams18606552011-05-01 14:57:11 -0700716 if (status != SCI_SUCCESS)
717 return status;
718
Dan Williams89a73012011-06-30 19:14:33 -0700719 status = sci_remote_node_context_start_io(&idev->rnc, ireq);
Dan Williams18606552011-05-01 14:57:11 -0700720 if (status != SCI_SUCCESS)
721 break;
722
Dan Williams89a73012011-06-30 19:14:33 -0700723 status = sci_request_start(ireq);
Dan Williams18606552011-05-01 14:57:11 -0700724 if (status != SCI_SUCCESS)
725 break;
726
Dan Williams78a6f062011-06-30 16:31:37 -0700727 idev->working_request = ireq;
728 sci_change_state(&idev->sm, SCI_SMP_DEV_CMD);
Dan Williams18606552011-05-01 14:57:11 -0700729 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000730 case SCI_STP_DEV_CMD:
731 case SCI_SMP_DEV_CMD:
Dan Williams18606552011-05-01 14:57:11 -0700732 /* device is already handling a command it can not accept new commands
733 * until this one is complete.
734 */
735 return SCI_FAILURE_INVALID_STATE;
736 }
737
Dan Williams89a73012011-06-30 19:14:33 -0700738 sci_remote_device_start_request(idev, ireq, status);
Dan Williams18606552011-05-01 14:57:11 -0700739 return status;
Dan Williams88f3b622011-04-22 19:18:03 -0700740}
741
Dan Williamsffe191c2011-06-29 13:09:25 -0700742static enum sci_status common_complete_io(struct isci_port *iport,
Dan Williams78a6f062011-06-30 16:31:37 -0700743 struct isci_remote_device *idev,
Dan Williams5076a1a2011-06-27 14:57:03 -0700744 struct isci_request *ireq)
Dan Williams88f3b622011-04-22 19:18:03 -0700745{
Dan Williams10a09e62011-05-01 15:33:43 -0700746 enum sci_status status;
747
Dan Williams89a73012011-06-30 19:14:33 -0700748 status = sci_request_complete(ireq);
Dan Williams10a09e62011-05-01 15:33:43 -0700749 if (status != SCI_SUCCESS)
750 return status;
751
Dan Williams89a73012011-06-30 19:14:33 -0700752 status = sci_port_complete_io(iport, idev, ireq);
Dan Williams10a09e62011-05-01 15:33:43 -0700753 if (status != SCI_SUCCESS)
754 return status;
755
Dan Williams89a73012011-06-30 19:14:33 -0700756 sci_remote_device_decrement_request_count(idev);
Dan Williams10a09e62011-05-01 15:33:43 -0700757 return status;
758}
759
Dan Williams89a73012011-06-30 19:14:33 -0700760enum sci_status sci_remote_device_complete_io(struct isci_host *ihost,
Dan Williams78a6f062011-06-30 16:31:37 -0700761 struct isci_remote_device *idev,
Dan Williams5076a1a2011-06-27 14:57:03 -0700762 struct isci_request *ireq)
Dan Williams10a09e62011-05-01 15:33:43 -0700763{
Dan Williams78a6f062011-06-30 16:31:37 -0700764 struct sci_base_state_machine *sm = &idev->sm;
Dan Williams89a73012011-06-30 19:14:33 -0700765 enum sci_remote_device_states state = sm->current_state_id;
Dan Williams78a6f062011-06-30 16:31:37 -0700766 struct isci_port *iport = idev->owning_port;
Dan Williams10a09e62011-05-01 15:33:43 -0700767 enum sci_status status;
768
769 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000770 case SCI_DEV_INITIAL:
771 case SCI_DEV_STOPPED:
772 case SCI_DEV_STARTING:
773 case SCI_STP_DEV_IDLE:
774 case SCI_SMP_DEV_IDLE:
775 case SCI_DEV_FAILED:
776 case SCI_DEV_FINAL:
Dan Williams10a09e62011-05-01 15:33:43 -0700777 default:
Dan Williamsd7a0ccd2012-02-10 01:18:44 -0800778 dev_warn(scirdev_to_dev(idev), "%s: in wrong state: %s\n",
779 __func__, dev_state_name(state));
Dan Williams10a09e62011-05-01 15:33:43 -0700780 return SCI_FAILURE_INVALID_STATE;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000781 case SCI_DEV_READY:
782 case SCI_STP_DEV_AWAIT_RESET:
783 case SCI_DEV_RESETTING:
Dan Williams78a6f062011-06-30 16:31:37 -0700784 status = common_complete_io(iport, idev, ireq);
Dan Williams10a09e62011-05-01 15:33:43 -0700785 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000786 case SCI_STP_DEV_CMD:
787 case SCI_STP_DEV_NCQ:
788 case SCI_STP_DEV_NCQ_ERROR:
Dan Williamsb50102d2011-09-30 18:52:19 -0700789 case SCI_STP_DEV_ATAPI_ERROR:
Dan Williams78a6f062011-06-30 16:31:37 -0700790 status = common_complete_io(iport, idev, ireq);
Dan Williams10a09e62011-05-01 15:33:43 -0700791 if (status != SCI_SUCCESS)
792 break;
793
Dan Williams5076a1a2011-06-27 14:57:03 -0700794 if (ireq->sci_status == SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED) {
Dan Williams10a09e62011-05-01 15:33:43 -0700795 /* This request causes hardware error, device needs to be Lun Reset.
796 * So here we force the state machine to IDLE state so the rest IOs
797 * can reach RNC state handler, these IOs will be completed by RNC with
798 * status of "DEVICE_RESET_REQUIRED", instead of "INVALID STATE".
799 */
Edmund Nadolskie3013702011-06-02 00:10:43 +0000800 sci_change_state(sm, SCI_STP_DEV_AWAIT_RESET);
Dan Williams34a99152011-07-01 02:25:15 -0700801 } else if (idev->started_request_count == 0)
Edmund Nadolskie3013702011-06-02 00:10:43 +0000802 sci_change_state(sm, SCI_STP_DEV_IDLE);
Dan Williams10a09e62011-05-01 15:33:43 -0700803 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000804 case SCI_SMP_DEV_CMD:
Dan Williams78a6f062011-06-30 16:31:37 -0700805 status = common_complete_io(iport, idev, ireq);
Dan Williams10a09e62011-05-01 15:33:43 -0700806 if (status != SCI_SUCCESS)
807 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000808 sci_change_state(sm, SCI_SMP_DEV_IDLE);
Dan Williams10a09e62011-05-01 15:33:43 -0700809 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000810 case SCI_DEV_STOPPING:
Dan Williams78a6f062011-06-30 16:31:37 -0700811 status = common_complete_io(iport, idev, ireq);
Dan Williams10a09e62011-05-01 15:33:43 -0700812 if (status != SCI_SUCCESS)
813 break;
814
Dan Williams34a99152011-07-01 02:25:15 -0700815 if (idev->started_request_count == 0)
Dan Williams89a73012011-06-30 19:14:33 -0700816 sci_remote_node_context_destruct(&idev->rnc,
Dan Williams34a99152011-07-01 02:25:15 -0700817 rnc_destruct_done,
818 idev);
Dan Williams10a09e62011-05-01 15:33:43 -0700819 break;
820 }
821
822 if (status != SCI_SUCCESS)
Dan Williams78a6f062011-06-30 16:31:37 -0700823 dev_err(scirdev_to_dev(idev),
Dan Williams10a09e62011-05-01 15:33:43 -0700824 "%s: Port:0x%p Device:0x%p Request:0x%p Status:0x%x "
Dan Williamsffe191c2011-06-29 13:09:25 -0700825 "could not complete\n", __func__, iport,
Dan Williams78a6f062011-06-30 16:31:37 -0700826 idev, ireq, status);
Dan Williams209fae12011-06-13 17:39:44 -0700827 else
Dan Williams78a6f062011-06-30 16:31:37 -0700828 isci_put_device(idev);
Dan Williams10a09e62011-05-01 15:33:43 -0700829
830 return status;
Dan Williams88f3b622011-04-22 19:18:03 -0700831}
832
Dan Williams89a73012011-06-30 19:14:33 -0700833static void sci_remote_device_continue_request(void *dev)
Dan Williams88f3b622011-04-22 19:18:03 -0700834{
Dan Williams78a6f062011-06-30 16:31:37 -0700835 struct isci_remote_device *idev = dev;
Dan Williams84b9b022011-05-01 15:53:25 -0700836
837 /* we need to check if this request is still valid to continue. */
Dan Williams78a6f062011-06-30 16:31:37 -0700838 if (idev->working_request)
Dan Williams89a73012011-06-30 19:14:33 -0700839 sci_controller_continue_io(idev->working_request);
Dan Williams84b9b022011-05-01 15:53:25 -0700840}
841
Dan Williams89a73012011-06-30 19:14:33 -0700842enum sci_status sci_remote_device_start_task(struct isci_host *ihost,
Dan Williams78a6f062011-06-30 16:31:37 -0700843 struct isci_remote_device *idev,
Dan Williams5076a1a2011-06-27 14:57:03 -0700844 struct isci_request *ireq)
Dan Williams84b9b022011-05-01 15:53:25 -0700845{
Dan Williams78a6f062011-06-30 16:31:37 -0700846 struct sci_base_state_machine *sm = &idev->sm;
Dan Williams89a73012011-06-30 19:14:33 -0700847 enum sci_remote_device_states state = sm->current_state_id;
Dan Williams78a6f062011-06-30 16:31:37 -0700848 struct isci_port *iport = idev->owning_port;
Dan Williams84b9b022011-05-01 15:53:25 -0700849 enum sci_status status;
850
851 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000852 case SCI_DEV_INITIAL:
853 case SCI_DEV_STOPPED:
854 case SCI_DEV_STARTING:
855 case SCI_SMP_DEV_IDLE:
856 case SCI_SMP_DEV_CMD:
857 case SCI_DEV_STOPPING:
858 case SCI_DEV_FAILED:
859 case SCI_DEV_RESETTING:
860 case SCI_DEV_FINAL:
Dan Williams84b9b022011-05-01 15:53:25 -0700861 default:
Dan Williamsd7a0ccd2012-02-10 01:18:44 -0800862 dev_warn(scirdev_to_dev(idev), "%s: in wrong state: %s\n",
863 __func__, dev_state_name(state));
Dan Williams84b9b022011-05-01 15:53:25 -0700864 return SCI_FAILURE_INVALID_STATE;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000865 case SCI_STP_DEV_IDLE:
866 case SCI_STP_DEV_CMD:
867 case SCI_STP_DEV_NCQ:
868 case SCI_STP_DEV_NCQ_ERROR:
869 case SCI_STP_DEV_AWAIT_RESET:
Dan Williams89a73012011-06-30 19:14:33 -0700870 status = sci_port_start_io(iport, idev, ireq);
Dan Williams84b9b022011-05-01 15:53:25 -0700871 if (status != SCI_SUCCESS)
872 return status;
873
Dan Williams89a73012011-06-30 19:14:33 -0700874 status = sci_request_start(ireq);
Dan Williams84b9b022011-05-01 15:53:25 -0700875 if (status != SCI_SUCCESS)
876 goto out;
877
878 /* Note: If the remote device state is not IDLE this will
879 * replace the request that probably resulted in the task
880 * management request.
881 */
Dan Williams78a6f062011-06-30 16:31:37 -0700882 idev->working_request = ireq;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000883 sci_change_state(sm, SCI_STP_DEV_CMD);
Dan Williams84b9b022011-05-01 15:53:25 -0700884
885 /* The remote node context must cleanup the TCi to NCQ mapping
886 * table. The only way to do this correctly is to either write
887 * to the TLCR register or to invalidate and repost the RNC. In
888 * either case the remote node context state machine will take
889 * the correct action when the remote node context is suspended
890 * and later resumed.
891 */
Jeff Skirvinc94fc1a2012-03-08 22:41:58 -0800892 sci_remote_device_suspend(idev,
893 SCI_SW_SUSPEND_LINKHANG_DETECT);
Jeff Skirvin14aaa9f2012-03-08 22:41:54 -0800894
895 status = sci_remote_node_context_start_task(&idev->rnc, ireq,
896 sci_remote_device_continue_request, idev);
Dan Williams84b9b022011-05-01 15:53:25 -0700897
898 out:
Dan Williams89a73012011-06-30 19:14:33 -0700899 sci_remote_device_start_request(idev, ireq, status);
Dan Williams84b9b022011-05-01 15:53:25 -0700900 /* We need to let the controller start request handler know that
901 * it can't post TC yet. We will provide a callback function to
902 * post TC when RNC gets resumed.
903 */
904 return SCI_FAILURE_RESET_DEVICE_PARTIAL_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000905 case SCI_DEV_READY:
Dan Williams89a73012011-06-30 19:14:33 -0700906 status = sci_port_start_io(iport, idev, ireq);
Dan Williams84b9b022011-05-01 15:53:25 -0700907 if (status != SCI_SUCCESS)
908 return status;
909
Jeff Skirvin14aaa9f2012-03-08 22:41:54 -0800910 /* Resume the RNC as needed: */
911 status = sci_remote_node_context_start_task(&idev->rnc, ireq,
912 NULL, NULL);
Dan Williams84b9b022011-05-01 15:53:25 -0700913 if (status != SCI_SUCCESS)
914 break;
915
Dan Williams89a73012011-06-30 19:14:33 -0700916 status = sci_request_start(ireq);
Dan Williams84b9b022011-05-01 15:53:25 -0700917 break;
918 }
Dan Williams89a73012011-06-30 19:14:33 -0700919 sci_remote_device_start_request(idev, ireq, status);
Dan Williams84b9b022011-05-01 15:53:25 -0700920
921 return status;
Dan Williams88f3b622011-04-22 19:18:03 -0700922}
923
Dan Williams34a99152011-07-01 02:25:15 -0700924void sci_remote_device_post_request(struct isci_remote_device *idev, u32 request)
Dan Williams88f3b622011-04-22 19:18:03 -0700925{
Dan Williams34a99152011-07-01 02:25:15 -0700926 struct isci_port *iport = idev->owning_port;
Dan Williams88f3b622011-04-22 19:18:03 -0700927 u32 context;
928
Dan Williams34a99152011-07-01 02:25:15 -0700929 context = request |
930 (ISCI_PEG << SCU_CONTEXT_COMMAND_PROTOCOL_ENGINE_GROUP_SHIFT) |
931 (iport->physical_port_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT) |
932 idev->rnc.remote_node_index;
Dan Williams88f3b622011-04-22 19:18:03 -0700933
Dan Williams34a99152011-07-01 02:25:15 -0700934 sci_controller_post_request(iport->owning_controller, context);
Dan Williams88f3b622011-04-22 19:18:03 -0700935}
936
Dan Williamsab2e8f72011-04-27 16:32:45 -0700937/* called once the remote node context has transisitioned to a
Dan Williams88f3b622011-04-22 19:18:03 -0700938 * ready state. This is the indication that the remote device object can also
Dan Williamsab2e8f72011-04-27 16:32:45 -0700939 * transition to ready.
Dan Williams88f3b622011-04-22 19:18:03 -0700940 */
Dan Williamseb229672011-05-01 14:05:57 -0700941static void remote_device_resume_done(void *_dev)
Dan Williams88f3b622011-04-22 19:18:03 -0700942{
Dan Williams78a6f062011-06-30 16:31:37 -0700943 struct isci_remote_device *idev = _dev;
Dan Williams88f3b622011-04-22 19:18:03 -0700944
Dan Williams78a6f062011-06-30 16:31:37 -0700945 if (is_remote_device_ready(idev))
Dan Williamse6225712011-05-01 16:26:09 -0700946 return;
Dan Williams88f3b622011-04-22 19:18:03 -0700947
Dan Williamse6225712011-05-01 16:26:09 -0700948 /* go 'ready' if we are not already in a ready state */
Dan Williams78a6f062011-06-30 16:31:37 -0700949 sci_change_state(&idev->sm, SCI_DEV_READY);
Dan Williams88f3b622011-04-22 19:18:03 -0700950}
951
Dan Williams89a73012011-06-30 19:14:33 -0700952static void sci_stp_remote_device_ready_idle_substate_resume_complete_handler(void *_dev)
Dan Williamsab2e8f72011-04-27 16:32:45 -0700953{
Dan Williams78a6f062011-06-30 16:31:37 -0700954 struct isci_remote_device *idev = _dev;
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700955 struct isci_host *ihost = idev->owning_port->owning_controller;
Dan Williamsab2e8f72011-04-27 16:32:45 -0700956
957 /* For NCQ operation we do not issue a isci_remote_device_not_ready().
958 * As a result, avoid sending the ready notification.
959 */
Dan Williams78a6f062011-06-30 16:31:37 -0700960 if (idev->sm.previous_state_id != SCI_STP_DEV_NCQ)
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700961 isci_remote_device_ready(ihost, idev);
Dan Williamsab2e8f72011-04-27 16:32:45 -0700962}
963
Dan Williams89a73012011-06-30 19:14:33 -0700964static void sci_remote_device_initial_state_enter(struct sci_base_state_machine *sm)
Dan Williams88f3b622011-04-22 19:18:03 -0700965{
Dan Williams78a6f062011-06-30 16:31:37 -0700966 struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
Dan Williams88f3b622011-04-22 19:18:03 -0700967
Dan Williams88f3b622011-04-22 19:18:03 -0700968 /* Initial state is a transitional state to the stopped state */
Dan Williams78a6f062011-06-30 16:31:37 -0700969 sci_change_state(&idev->sm, SCI_DEV_STOPPED);
Dan Williams88f3b622011-04-22 19:18:03 -0700970}
971
972/**
Dan Williams89a73012011-06-30 19:14:33 -0700973 * sci_remote_device_destruct() - free remote node context and destruct
Dan Williams88f3b622011-04-22 19:18:03 -0700974 * @remote_device: This parameter specifies the remote device to be destructed.
975 *
976 * Remote device objects are a limited resource. As such, they must be
977 * protected. Thus calls to construct and destruct are mutually exclusive and
978 * non-reentrant. The return value shall indicate if the device was
979 * successfully destructed or if some failure occurred. enum sci_status This value
980 * is returned if the device is successfully destructed.
981 * SCI_FAILURE_INVALID_REMOTE_DEVICE This value is returned if the supplied
982 * device isn't valid (e.g. it's already been destoryed, the handle isn't
983 * valid, etc.).
984 */
Dan Williams89a73012011-06-30 19:14:33 -0700985static enum sci_status sci_remote_device_destruct(struct isci_remote_device *idev)
Dan Williams88f3b622011-04-22 19:18:03 -0700986{
Dan Williams78a6f062011-06-30 16:31:37 -0700987 struct sci_base_state_machine *sm = &idev->sm;
Dan Williams89a73012011-06-30 19:14:33 -0700988 enum sci_remote_device_states state = sm->current_state_id;
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700989 struct isci_host *ihost;
Dan Williamsb8d82f62011-05-01 14:38:26 -0700990
Edmund Nadolskie3013702011-06-02 00:10:43 +0000991 if (state != SCI_DEV_STOPPED) {
Dan Williamsd7a0ccd2012-02-10 01:18:44 -0800992 dev_warn(scirdev_to_dev(idev), "%s: in wrong state: %s\n",
993 __func__, dev_state_name(state));
Dan Williamsb8d82f62011-05-01 14:38:26 -0700994 return SCI_FAILURE_INVALID_STATE;
995 }
996
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700997 ihost = idev->owning_port->owning_controller;
Dan Williams89a73012011-06-30 19:14:33 -0700998 sci_controller_free_remote_node_context(ihost, idev,
Dan Williams78a6f062011-06-30 16:31:37 -0700999 idev->rnc.remote_node_index);
1000 idev->rnc.remote_node_index = SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001001 sci_change_state(sm, SCI_DEV_FINAL);
Dan Williamsb8d82f62011-05-01 14:38:26 -07001002
1003 return SCI_SUCCESS;
Dan Williams88f3b622011-04-22 19:18:03 -07001004}
1005
Dan Williams6f231dd2011-07-02 22:56:22 -07001006/**
1007 * isci_remote_device_deconstruct() - This function frees an isci_remote_device.
Dan Williamsd9c37392011-03-03 17:59:32 -08001008 * @ihost: This parameter specifies the isci host object.
1009 * @idev: This parameter specifies the remote device to be freed.
Dan Williams6f231dd2011-07-02 22:56:22 -07001010 *
1011 */
Dan Williamsd9c37392011-03-03 17:59:32 -08001012static void isci_remote_device_deconstruct(struct isci_host *ihost, struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001013{
Dan Williamsd9c37392011-03-03 17:59:32 -08001014 dev_dbg(&ihost->pdev->dev,
1015 "%s: isci_device = %p\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001016
1017 /* There should not be any outstanding io's. All paths to
1018 * here should go through isci_remote_device_nuke_requests.
1019 * If we hit this condition, we will need a way to complete
1020 * io requests in process */
Jeff Skirvind6b2a0e2012-03-08 22:41:57 -08001021 BUG_ON(idev->started_request_count > 0);
Dan Williams6f231dd2011-07-02 22:56:22 -07001022
Dan Williams89a73012011-06-30 19:14:33 -07001023 sci_remote_device_destruct(idev);
Dan Williamsd9c37392011-03-03 17:59:32 -08001024 list_del_init(&idev->node);
Dan Williams209fae12011-06-13 17:39:44 -07001025 isci_put_device(idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001026}
1027
Dan Williams89a73012011-06-30 19:14:33 -07001028static void sci_remote_device_stopped_state_enter(struct sci_base_state_machine *sm)
Dan Williams88f3b622011-04-22 19:18:03 -07001029{
Dan Williams78a6f062011-06-30 16:31:37 -07001030 struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001031 struct isci_host *ihost = idev->owning_port->owning_controller;
Dan Williams88f3b622011-04-22 19:18:03 -07001032 u32 prev_state;
1033
Dan Williams88f3b622011-04-22 19:18:03 -07001034 /* If we are entering from the stopping state let the SCI User know that
1035 * the stop operation has completed.
1036 */
Dan Williams78a6f062011-06-30 16:31:37 -07001037 prev_state = idev->sm.previous_state_id;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001038 if (prev_state == SCI_DEV_STOPPING)
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001039 isci_remote_device_deconstruct(ihost, idev);
Dan Williams88f3b622011-04-22 19:18:03 -07001040
Dan Williams89a73012011-06-30 19:14:33 -07001041 sci_controller_remote_device_stopped(ihost, idev);
Dan Williams88f3b622011-04-22 19:18:03 -07001042}
1043
Dan Williams89a73012011-06-30 19:14:33 -07001044static void sci_remote_device_starting_state_enter(struct sci_base_state_machine *sm)
Dan Williams88f3b622011-04-22 19:18:03 -07001045{
Dan Williams78a6f062011-06-30 16:31:37 -07001046 struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
Dan Williams34a99152011-07-01 02:25:15 -07001047 struct isci_host *ihost = idev->owning_port->owning_controller;
Dan Williams88f3b622011-04-22 19:18:03 -07001048
Dan Williams88f3b622011-04-22 19:18:03 -07001049 isci_remote_device_not_ready(ihost, idev,
1050 SCIC_REMOTE_DEVICE_NOT_READY_START_REQUESTED);
1051}
1052
Dan Williams89a73012011-06-30 19:14:33 -07001053static void sci_remote_device_ready_state_enter(struct sci_base_state_machine *sm)
Dan Williams88f3b622011-04-22 19:18:03 -07001054{
Dan Williams78a6f062011-06-30 16:31:37 -07001055 struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001056 struct isci_host *ihost = idev->owning_port->owning_controller;
Artur Wojcikcc3dbd02011-05-04 07:58:16 +00001057 struct domain_device *dev = idev->domain_dev;
Dan Williams88f3b622011-04-22 19:18:03 -07001058
Dan Williamsab2e8f72011-04-27 16:32:45 -07001059 if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_SATA)) {
Dan Williams78a6f062011-06-30 16:31:37 -07001060 sci_change_state(&idev->sm, SCI_STP_DEV_IDLE);
Dan Williamsab2e8f72011-04-27 16:32:45 -07001061 } else if (dev_is_expander(dev)) {
Dan Williams78a6f062011-06-30 16:31:37 -07001062 sci_change_state(&idev->sm, SCI_SMP_DEV_IDLE);
Dan Williamsab2e8f72011-04-27 16:32:45 -07001063 } else
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001064 isci_remote_device_ready(ihost, idev);
Dan Williams88f3b622011-04-22 19:18:03 -07001065}
1066
Dan Williams89a73012011-06-30 19:14:33 -07001067static void sci_remote_device_ready_state_exit(struct sci_base_state_machine *sm)
Dan Williams88f3b622011-04-22 19:18:03 -07001068{
Dan Williams78a6f062011-06-30 16:31:37 -07001069 struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
1070 struct domain_device *dev = idev->domain_dev;
Dan Williamsab2e8f72011-04-27 16:32:45 -07001071
1072 if (dev->dev_type == SAS_END_DEV) {
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001073 struct isci_host *ihost = idev->owning_port->owning_controller;
Dan Williams88f3b622011-04-22 19:18:03 -07001074
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001075 isci_remote_device_not_ready(ihost, idev,
Dan Williams88f3b622011-04-22 19:18:03 -07001076 SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED);
1077 }
1078}
1079
Dan Williams89a73012011-06-30 19:14:33 -07001080static void sci_remote_device_resetting_state_enter(struct sci_base_state_machine *sm)
Dan Williams88f3b622011-04-22 19:18:03 -07001081{
Dan Williams78a6f062011-06-30 16:31:37 -07001082 struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
Jeff Skirvinac78ed02012-03-08 22:41:50 -08001083 struct isci_host *ihost = idev->owning_port->owning_controller;
1084
1085 dev_dbg(&ihost->pdev->dev,
1086 "%s: isci_device = %p\n", __func__, idev);
Dan Williams88f3b622011-04-22 19:18:03 -07001087
Jeff Skirvinc94fc1a2012-03-08 22:41:58 -08001088 sci_remote_device_suspend(idev, SCI_SW_SUSPEND_LINKHANG_DETECT);
Dan Williams88f3b622011-04-22 19:18:03 -07001089}
1090
Dan Williams89a73012011-06-30 19:14:33 -07001091static void sci_remote_device_resetting_state_exit(struct sci_base_state_machine *sm)
Dan Williams88f3b622011-04-22 19:18:03 -07001092{
Dan Williams78a6f062011-06-30 16:31:37 -07001093 struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
Jeff Skirvinac78ed02012-03-08 22:41:50 -08001094 struct isci_host *ihost = idev->owning_port->owning_controller;
1095
1096 dev_dbg(&ihost->pdev->dev,
1097 "%s: isci_device = %p\n", __func__, idev);
Dan Williams88f3b622011-04-22 19:18:03 -07001098
Dan Williams89a73012011-06-30 19:14:33 -07001099 sci_remote_node_context_resume(&idev->rnc, NULL, NULL);
Dan Williams88f3b622011-04-22 19:18:03 -07001100}
1101
Dan Williams89a73012011-06-30 19:14:33 -07001102static void sci_stp_remote_device_ready_idle_substate_enter(struct sci_base_state_machine *sm)
Dan Williamsab2e8f72011-04-27 16:32:45 -07001103{
Dan Williams78a6f062011-06-30 16:31:37 -07001104 struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
Dan Williamsab2e8f72011-04-27 16:32:45 -07001105
Dan Williams78a6f062011-06-30 16:31:37 -07001106 idev->working_request = NULL;
Dan Williams89a73012011-06-30 19:14:33 -07001107 if (sci_remote_node_context_is_ready(&idev->rnc)) {
Dan Williamsab2e8f72011-04-27 16:32:45 -07001108 /*
1109 * Since the RNC is ready, it's alright to finish completion
1110 * processing (e.g. signal the remote device is ready). */
Dan Williams89a73012011-06-30 19:14:33 -07001111 sci_stp_remote_device_ready_idle_substate_resume_complete_handler(idev);
Dan Williamsab2e8f72011-04-27 16:32:45 -07001112 } else {
Dan Williams89a73012011-06-30 19:14:33 -07001113 sci_remote_node_context_resume(&idev->rnc,
1114 sci_stp_remote_device_ready_idle_substate_resume_complete_handler,
Dan Williams78a6f062011-06-30 16:31:37 -07001115 idev);
Dan Williamsab2e8f72011-04-27 16:32:45 -07001116 }
1117}
1118
Dan Williams89a73012011-06-30 19:14:33 -07001119static void sci_stp_remote_device_ready_cmd_substate_enter(struct sci_base_state_machine *sm)
Dan Williamsab2e8f72011-04-27 16:32:45 -07001120{
Dan Williams78a6f062011-06-30 16:31:37 -07001121 struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
Dan Williams34a99152011-07-01 02:25:15 -07001122 struct isci_host *ihost = idev->owning_port->owning_controller;
Dan Williamsab2e8f72011-04-27 16:32:45 -07001123
Dan Williams78a6f062011-06-30 16:31:37 -07001124 BUG_ON(idev->working_request == NULL);
Dan Williamsab2e8f72011-04-27 16:32:45 -07001125
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001126 isci_remote_device_not_ready(ihost, idev,
Dan Williamsab2e8f72011-04-27 16:32:45 -07001127 SCIC_REMOTE_DEVICE_NOT_READY_SATA_REQUEST_STARTED);
1128}
1129
Dan Williams89a73012011-06-30 19:14:33 -07001130static void sci_stp_remote_device_ready_ncq_error_substate_enter(struct sci_base_state_machine *sm)
Dan Williamsab2e8f72011-04-27 16:32:45 -07001131{
Dan Williams78a6f062011-06-30 16:31:37 -07001132 struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
Dan Williams34a99152011-07-01 02:25:15 -07001133 struct isci_host *ihost = idev->owning_port->owning_controller;
Dan Williamsab2e8f72011-04-27 16:32:45 -07001134
Dan Williams78a6f062011-06-30 16:31:37 -07001135 if (idev->not_ready_reason == SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED)
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001136 isci_remote_device_not_ready(ihost, idev,
Dan Williams78a6f062011-06-30 16:31:37 -07001137 idev->not_ready_reason);
Dan Williamsab2e8f72011-04-27 16:32:45 -07001138}
1139
Jeff Skirvinac78ed02012-03-08 22:41:50 -08001140static void sci_stp_remote_device_atapi_error_substate_enter(
1141 struct sci_base_state_machine *sm)
1142{
1143 struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
1144
1145 /* This state is entered when an I/O is decoded with an error
1146 * condition. By this point the RNC expected suspension state is set.
1147 * The error conditions suspend the device, so unsuspend here if
1148 * possible.
1149 */
1150 sci_remote_node_context_resume(&idev->rnc,
1151 atapi_remote_device_resume_done,
1152 idev);
1153}
1154
Dan Williams89a73012011-06-30 19:14:33 -07001155static void sci_smp_remote_device_ready_idle_substate_enter(struct sci_base_state_machine *sm)
Dan Williamsab2e8f72011-04-27 16:32:45 -07001156{
Dan Williams78a6f062011-06-30 16:31:37 -07001157 struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
Dan Williams34a99152011-07-01 02:25:15 -07001158 struct isci_host *ihost = idev->owning_port->owning_controller;
Dan Williamsab2e8f72011-04-27 16:32:45 -07001159
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001160 isci_remote_device_ready(ihost, idev);
Dan Williamsab2e8f72011-04-27 16:32:45 -07001161}
1162
Dan Williams89a73012011-06-30 19:14:33 -07001163static void sci_smp_remote_device_ready_cmd_substate_enter(struct sci_base_state_machine *sm)
Dan Williamsab2e8f72011-04-27 16:32:45 -07001164{
Dan Williams78a6f062011-06-30 16:31:37 -07001165 struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
Dan Williams34a99152011-07-01 02:25:15 -07001166 struct isci_host *ihost = idev->owning_port->owning_controller;
Dan Williamsab2e8f72011-04-27 16:32:45 -07001167
Dan Williams78a6f062011-06-30 16:31:37 -07001168 BUG_ON(idev->working_request == NULL);
Dan Williamsab2e8f72011-04-27 16:32:45 -07001169
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001170 isci_remote_device_not_ready(ihost, idev,
Dan Williamsab2e8f72011-04-27 16:32:45 -07001171 SCIC_REMOTE_DEVICE_NOT_READY_SMP_REQUEST_STARTED);
1172}
1173
Dan Williams89a73012011-06-30 19:14:33 -07001174static void sci_smp_remote_device_ready_cmd_substate_exit(struct sci_base_state_machine *sm)
Dan Williamsab2e8f72011-04-27 16:32:45 -07001175{
Dan Williams78a6f062011-06-30 16:31:37 -07001176 struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
Dan Williamsab2e8f72011-04-27 16:32:45 -07001177
Dan Williams78a6f062011-06-30 16:31:37 -07001178 idev->working_request = NULL;
Dan Williamsab2e8f72011-04-27 16:32:45 -07001179}
Dan Williams88f3b622011-04-22 19:18:03 -07001180
Dan Williams89a73012011-06-30 19:14:33 -07001181static const struct sci_base_state sci_remote_device_state_table[] = {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001182 [SCI_DEV_INITIAL] = {
Dan Williams89a73012011-06-30 19:14:33 -07001183 .enter_state = sci_remote_device_initial_state_enter,
Dan Williams88f3b622011-04-22 19:18:03 -07001184 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001185 [SCI_DEV_STOPPED] = {
Dan Williams89a73012011-06-30 19:14:33 -07001186 .enter_state = sci_remote_device_stopped_state_enter,
Dan Williams88f3b622011-04-22 19:18:03 -07001187 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001188 [SCI_DEV_STARTING] = {
Dan Williams89a73012011-06-30 19:14:33 -07001189 .enter_state = sci_remote_device_starting_state_enter,
Dan Williams88f3b622011-04-22 19:18:03 -07001190 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001191 [SCI_DEV_READY] = {
Dan Williams89a73012011-06-30 19:14:33 -07001192 .enter_state = sci_remote_device_ready_state_enter,
1193 .exit_state = sci_remote_device_ready_state_exit
Dan Williams88f3b622011-04-22 19:18:03 -07001194 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001195 [SCI_STP_DEV_IDLE] = {
Dan Williams89a73012011-06-30 19:14:33 -07001196 .enter_state = sci_stp_remote_device_ready_idle_substate_enter,
Dan Williamsab2e8f72011-04-27 16:32:45 -07001197 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001198 [SCI_STP_DEV_CMD] = {
Dan Williams89a73012011-06-30 19:14:33 -07001199 .enter_state = sci_stp_remote_device_ready_cmd_substate_enter,
Dan Williamsab2e8f72011-04-27 16:32:45 -07001200 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001201 [SCI_STP_DEV_NCQ] = { },
1202 [SCI_STP_DEV_NCQ_ERROR] = {
Dan Williams89a73012011-06-30 19:14:33 -07001203 .enter_state = sci_stp_remote_device_ready_ncq_error_substate_enter,
Dan Williamsab2e8f72011-04-27 16:32:45 -07001204 },
Jeff Skirvinac78ed02012-03-08 22:41:50 -08001205 [SCI_STP_DEV_ATAPI_ERROR] = {
1206 .enter_state = sci_stp_remote_device_atapi_error_substate_enter,
1207 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001208 [SCI_STP_DEV_AWAIT_RESET] = { },
1209 [SCI_SMP_DEV_IDLE] = {
Dan Williams89a73012011-06-30 19:14:33 -07001210 .enter_state = sci_smp_remote_device_ready_idle_substate_enter,
Dan Williamsab2e8f72011-04-27 16:32:45 -07001211 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001212 [SCI_SMP_DEV_CMD] = {
Dan Williams89a73012011-06-30 19:14:33 -07001213 .enter_state = sci_smp_remote_device_ready_cmd_substate_enter,
1214 .exit_state = sci_smp_remote_device_ready_cmd_substate_exit,
Dan Williamsab2e8f72011-04-27 16:32:45 -07001215 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001216 [SCI_DEV_STOPPING] = { },
1217 [SCI_DEV_FAILED] = { },
1218 [SCI_DEV_RESETTING] = {
Dan Williams89a73012011-06-30 19:14:33 -07001219 .enter_state = sci_remote_device_resetting_state_enter,
1220 .exit_state = sci_remote_device_resetting_state_exit
Dan Williams88f3b622011-04-22 19:18:03 -07001221 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001222 [SCI_DEV_FINAL] = { },
Dan Williams88f3b622011-04-22 19:18:03 -07001223};
1224
1225/**
Dan Williams89a73012011-06-30 19:14:33 -07001226 * sci_remote_device_construct() - common construction
Dan Williams88f3b622011-04-22 19:18:03 -07001227 * @sci_port: SAS/SATA port through which this device is accessed.
1228 * @sci_dev: remote device to construct
1229 *
Dan Williamsb87ee302011-04-25 11:48:29 -07001230 * This routine just performs benign initialization and does not
1231 * allocate the remote_node_context which is left to
Dan Williams89a73012011-06-30 19:14:33 -07001232 * sci_remote_device_[de]a_construct(). sci_remote_device_destruct()
Dan Williamsb87ee302011-04-25 11:48:29 -07001233 * frees the remote_node_context(s) for the device.
Dan Williams88f3b622011-04-22 19:18:03 -07001234 */
Dan Williams89a73012011-06-30 19:14:33 -07001235static void sci_remote_device_construct(struct isci_port *iport,
Dan Williams78a6f062011-06-30 16:31:37 -07001236 struct isci_remote_device *idev)
Dan Williams88f3b622011-04-22 19:18:03 -07001237{
Dan Williams78a6f062011-06-30 16:31:37 -07001238 idev->owning_port = iport;
1239 idev->started_request_count = 0;
Dan Williams88f3b622011-04-22 19:18:03 -07001240
Dan Williams89a73012011-06-30 19:14:33 -07001241 sci_init_sm(&idev->sm, sci_remote_device_state_table, SCI_DEV_INITIAL);
Dan Williams88f3b622011-04-22 19:18:03 -07001242
Dan Williams89a73012011-06-30 19:14:33 -07001243 sci_remote_node_context_construct(&idev->rnc,
Dan Williams88f3b622011-04-22 19:18:03 -07001244 SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX);
Dan Williams88f3b622011-04-22 19:18:03 -07001245}
1246
1247/**
Dan Williams89a73012011-06-30 19:14:33 -07001248 * sci_remote_device_da_construct() - construct direct attached device.
Dan Williams88f3b622011-04-22 19:18:03 -07001249 *
Dan Williamsb87ee302011-04-25 11:48:29 -07001250 * The information (e.g. IAF, Signature FIS, etc.) necessary to build
1251 * the device is known to the SCI Core since it is contained in the
Dan Williams89a73012011-06-30 19:14:33 -07001252 * sci_phy object. Remote node context(s) is/are a global resource
1253 * allocated by this routine, freed by sci_remote_device_destruct().
Dan Williamsb87ee302011-04-25 11:48:29 -07001254 *
1255 * Returns:
1256 * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
1257 * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
1258 * sata-only controller instance.
1259 * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
Dan Williams88f3b622011-04-22 19:18:03 -07001260 */
Dan Williams89a73012011-06-30 19:14:33 -07001261static enum sci_status sci_remote_device_da_construct(struct isci_port *iport,
Dan Williams78a6f062011-06-30 16:31:37 -07001262 struct isci_remote_device *idev)
Dan Williams88f3b622011-04-22 19:18:03 -07001263{
1264 enum sci_status status;
Bartek Nowakowski7e629842012-01-04 01:33:20 -08001265 struct sci_port_properties properties;
Dan Williams88f3b622011-04-22 19:18:03 -07001266
Dan Williams89a73012011-06-30 19:14:33 -07001267 sci_remote_device_construct(iport, idev);
Dan Williamsb87ee302011-04-25 11:48:29 -07001268
Bartek Nowakowski7e629842012-01-04 01:33:20 -08001269 sci_port_get_properties(iport, &properties);
1270 /* Get accurate port width from port's phy mask for a DA device. */
1271 idev->device_port_width = hweight32(properties.phy_mask);
1272
Dan Williams89a73012011-06-30 19:14:33 -07001273 status = sci_controller_allocate_remote_node_context(iport->owning_controller,
Dan Williams11cc5182012-02-01 00:23:10 -08001274 idev,
1275 &idev->rnc.remote_node_index);
Dan Williams88f3b622011-04-22 19:18:03 -07001276
Dan Williamsa1a113b2011-04-21 18:44:45 -07001277 if (status != SCI_SUCCESS)
1278 return status;
Dan Williams88f3b622011-04-22 19:18:03 -07001279
Dan Williams89a73012011-06-30 19:14:33 -07001280 idev->connection_rate = sci_port_get_max_allowed_speed(iport);
Dan Williams88f3b622011-04-22 19:18:03 -07001281
Dan Williamsa1a113b2011-04-21 18:44:45 -07001282 return SCI_SUCCESS;
Dan Williams88f3b622011-04-22 19:18:03 -07001283}
1284
Dan Williams88f3b622011-04-22 19:18:03 -07001285/**
Dan Williams89a73012011-06-30 19:14:33 -07001286 * sci_remote_device_ea_construct() - construct expander attached device
Dan Williams88f3b622011-04-22 19:18:03 -07001287 *
Dan Williamsb87ee302011-04-25 11:48:29 -07001288 * Remote node context(s) is/are a global resource allocated by this
Dan Williams89a73012011-06-30 19:14:33 -07001289 * routine, freed by sci_remote_device_destruct().
Dan Williamsb87ee302011-04-25 11:48:29 -07001290 *
1291 * Returns:
1292 * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
1293 * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
1294 * sata-only controller instance.
1295 * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
Dan Williams88f3b622011-04-22 19:18:03 -07001296 */
Dan Williams89a73012011-06-30 19:14:33 -07001297static enum sci_status sci_remote_device_ea_construct(struct isci_port *iport,
Dan Williams78a6f062011-06-30 16:31:37 -07001298 struct isci_remote_device *idev)
Dan Williams88f3b622011-04-22 19:18:03 -07001299{
Dan Williams78a6f062011-06-30 16:31:37 -07001300 struct domain_device *dev = idev->domain_dev;
Dan Williams88f3b622011-04-22 19:18:03 -07001301 enum sci_status status;
Dan Williams88f3b622011-04-22 19:18:03 -07001302
Dan Williams89a73012011-06-30 19:14:33 -07001303 sci_remote_device_construct(iport, idev);
Dan Williams88f3b622011-04-22 19:18:03 -07001304
Dan Williams89a73012011-06-30 19:14:33 -07001305 status = sci_controller_allocate_remote_node_context(iport->owning_controller,
Dan Williams78a6f062011-06-30 16:31:37 -07001306 idev,
1307 &idev->rnc.remote_node_index);
Dan Williamsa1a113b2011-04-21 18:44:45 -07001308 if (status != SCI_SUCCESS)
1309 return status;
Dan Williams88f3b622011-04-22 19:18:03 -07001310
Dan Williams11cc5182012-02-01 00:23:10 -08001311 /* For SAS-2 the physical link rate is actually a logical link
Dan Williamsa1a113b2011-04-21 18:44:45 -07001312 * rate that incorporates multiplexing. The SCU doesn't
1313 * incorporate multiplexing and for the purposes of the
1314 * connection the logical link rate is that same as the
1315 * physical. Furthermore, the SAS-2 and SAS-1.1 fields overlay
Dan Williams11cc5182012-02-01 00:23:10 -08001316 * one another, so this code works for both situations.
1317 */
Dan Williams89a73012011-06-30 19:14:33 -07001318 idev->connection_rate = min_t(u16, sci_port_get_max_allowed_speed(iport),
Dan Williams00d680e2011-04-25 14:29:29 -07001319 dev->linkrate);
Dan Williams88f3b622011-04-22 19:18:03 -07001320
Dan Williamsa1a113b2011-04-21 18:44:45 -07001321 /* / @todo Should I assign the port width by reading all of the phys on the port? */
Dan Williams78a6f062011-06-30 16:31:37 -07001322 idev->device_port_width = 1;
Dan Williams88f3b622011-04-22 19:18:03 -07001323
Dan Williamsab2e8f72011-04-27 16:32:45 -07001324 return SCI_SUCCESS;
Dan Williams88f3b622011-04-22 19:18:03 -07001325}
1326
Jeff Skirvin5b6bf222012-03-08 22:41:51 -08001327enum sci_status sci_remote_device_resume(
1328 struct isci_remote_device *idev,
1329 scics_sds_remote_node_context_callback cb_fn,
1330 void *cb_p)
1331{
1332 enum sci_status status;
1333
1334 status = sci_remote_node_context_resume(&idev->rnc, cb_fn, cb_p);
1335 if (status != SCI_SUCCESS)
1336 dev_dbg(scirdev_to_dev(idev), "%s: failed to resume: %d\n",
1337 __func__, status);
1338 return status;
1339}
1340
Jeff Skirvin0cce1652012-03-08 22:42:04 -08001341static void isci_remote_device_resume_from_abort_complete(void *cbparam)
1342{
1343 struct isci_remote_device *idev = cbparam;
1344 struct isci_host *ihost = idev->owning_port->owning_controller;
1345 scics_sds_remote_node_context_callback abort_resume_cb =
1346 idev->abort_resume_cb;
1347
1348 dev_dbg(scirdev_to_dev(idev), "%s: passing-along resume: %p\n",
1349 __func__, abort_resume_cb);
1350
1351 if (abort_resume_cb != NULL) {
1352 idev->abort_resume_cb = NULL;
1353 abort_resume_cb(idev->abort_resume_cbparam);
1354 }
1355 clear_bit(IDEV_ABORT_PATH_RESUME_PENDING, &idev->flags);
1356 wake_up(&ihost->eventq);
1357}
1358
1359
1360void isci_remote_device_wait_for_resume_from_abort(
1361 struct isci_host *ihost,
1362 struct isci_remote_device *idev)
1363{
1364 dev_dbg(scirdev_to_dev(idev), "%s: starting resume wait: %p\n",
1365 __func__, idev);
1366
Jeff Skirvin8c731882012-03-08 22:42:06 -08001367 #define MAX_RESUME_MSECS 10000
Jeff Skirvin0cce1652012-03-08 22:42:04 -08001368 if (!wait_event_timeout(ihost->eventq,
1369 (!test_bit(IDEV_ABORT_PATH_RESUME_PENDING,
1370 &idev->flags)
1371 || test_bit(IDEV_STOP_PENDING, &idev->flags)),
1372 msecs_to_jiffies(MAX_RESUME_MSECS))) {
1373
1374 dev_warn(scirdev_to_dev(idev), "%s: #### Timeout waiting for "
1375 "resume: %p\n", __func__, idev);
1376 }
1377 clear_bit(IDEV_ABORT_PATH_RESUME_PENDING, &idev->flags);
1378
1379 dev_dbg(scirdev_to_dev(idev), "%s: resume wait done: %p\n",
1380 __func__, idev);
1381}
1382
Jeff Skirvin31a38ef2012-03-08 22:42:01 -08001383enum sci_status isci_remote_device_resume_from_abort(
Jeff Skirvin5b6bf222012-03-08 22:41:51 -08001384 struct isci_host *ihost,
Jeff Skirvin31a38ef2012-03-08 22:42:01 -08001385 struct isci_remote_device *idev)
Jeff Skirvin5b6bf222012-03-08 22:41:51 -08001386{
1387 unsigned long flags;
Jeff Skirvin1f053882012-03-08 22:42:08 -08001388 enum sci_status status = SCI_SUCCESS;
1389 int destroyed;
Jeff Skirvin5b6bf222012-03-08 22:41:51 -08001390
1391 spin_lock_irqsave(&ihost->scic_lock, flags);
Jeff Skirvin31a38ef2012-03-08 22:42:01 -08001392 /* Preserve any current resume callbacks, for instance from other
1393 * resumptions.
1394 */
Jeff Skirvin0cce1652012-03-08 22:42:04 -08001395 idev->abort_resume_cb = idev->rnc.user_callback;
1396 idev->abort_resume_cbparam = idev->rnc.user_cookie;
1397 set_bit(IDEV_ABORT_PATH_RESUME_PENDING, &idev->flags);
Jeff Skirvin0c3ce382012-03-08 22:42:02 -08001398 clear_bit(IDEV_ABORT_PATH_ACTIVE, &idev->flags);
Jeff Skirvin1f053882012-03-08 22:42:08 -08001399 destroyed = sci_remote_node_context_is_being_destroyed(&idev->rnc);
1400 if (!destroyed)
1401 status = sci_remote_device_resume(
Jeff Skirvin0cce1652012-03-08 22:42:04 -08001402 idev, isci_remote_device_resume_from_abort_complete,
1403 idev);
Jeff Skirvin5b6bf222012-03-08 22:41:51 -08001404 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Jeff Skirvin1f053882012-03-08 22:42:08 -08001405 if (!destroyed)
1406 isci_remote_device_wait_for_resume_from_abort(ihost, idev);
1407 else
1408 clear_bit(IDEV_ABORT_PATH_RESUME_PENDING, &idev->flags);
1409
Jeff Skirvin5b6bf222012-03-08 22:41:51 -08001410 return status;
1411}
Jeff Skirvin0cce1652012-03-08 22:42:04 -08001412
Dan Williams88f3b622011-04-22 19:18:03 -07001413/**
Dan Williams89a73012011-06-30 19:14:33 -07001414 * sci_remote_device_start() - This method will start the supplied remote
Dan Williams88f3b622011-04-22 19:18:03 -07001415 * device. This method enables normal IO requests to flow through to the
1416 * remote device.
1417 * @remote_device: This parameter specifies the device to be started.
1418 * @timeout: This parameter specifies the number of milliseconds in which the
1419 * start operation should complete.
1420 *
1421 * An indication of whether the device was successfully started. SCI_SUCCESS
1422 * This value is returned if the device was successfully started.
1423 * SCI_FAILURE_INVALID_PHY This value is returned if the user attempts to start
1424 * the device when there have been no phys added to it.
1425 */
Dan Williams89a73012011-06-30 19:14:33 -07001426static enum sci_status sci_remote_device_start(struct isci_remote_device *idev,
Jeff Skirvin726980d2012-03-08 22:41:50 -08001427 u32 timeout)
Dan Williams88f3b622011-04-22 19:18:03 -07001428{
Dan Williams78a6f062011-06-30 16:31:37 -07001429 struct sci_base_state_machine *sm = &idev->sm;
Dan Williams89a73012011-06-30 19:14:33 -07001430 enum sci_remote_device_states state = sm->current_state_id;
Dan Williamseb229672011-05-01 14:05:57 -07001431 enum sci_status status;
1432
Edmund Nadolskie3013702011-06-02 00:10:43 +00001433 if (state != SCI_DEV_STOPPED) {
Dan Williamsd7a0ccd2012-02-10 01:18:44 -08001434 dev_warn(scirdev_to_dev(idev), "%s: in wrong state: %s\n",
1435 __func__, dev_state_name(state));
Dan Williamseb229672011-05-01 14:05:57 -07001436 return SCI_FAILURE_INVALID_STATE;
1437 }
1438
Jeff Skirvin5b6bf222012-03-08 22:41:51 -08001439 status = sci_remote_device_resume(idev, remote_device_resume_done,
1440 idev);
Dan Williamseb229672011-05-01 14:05:57 -07001441 if (status != SCI_SUCCESS)
1442 return status;
1443
Edmund Nadolskie3013702011-06-02 00:10:43 +00001444 sci_change_state(sm, SCI_DEV_STARTING);
Dan Williamseb229672011-05-01 14:05:57 -07001445
1446 return SCI_SUCCESS;
Dan Williams88f3b622011-04-22 19:18:03 -07001447}
Dan Williams6f231dd2011-07-02 22:56:22 -07001448
Dan Williams00d680e2011-04-25 14:29:29 -07001449static enum sci_status isci_remote_device_construct(struct isci_port *iport,
1450 struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001451{
Dan Williams00d680e2011-04-25 14:29:29 -07001452 struct isci_host *ihost = iport->isci_host;
1453 struct domain_device *dev = idev->domain_dev;
1454 enum sci_status status;
Dan Williams6f231dd2011-07-02 22:56:22 -07001455
Dan Williams00d680e2011-04-25 14:29:29 -07001456 if (dev->parent && dev_is_expander(dev->parent))
Dan Williams89a73012011-06-30 19:14:33 -07001457 status = sci_remote_device_ea_construct(iport, idev);
Dan Williams00d680e2011-04-25 14:29:29 -07001458 else
Dan Williams89a73012011-06-30 19:14:33 -07001459 status = sci_remote_device_da_construct(iport, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001460
1461 if (status != SCI_SUCCESS) {
Dan Williams00d680e2011-04-25 14:29:29 -07001462 dev_dbg(&ihost->pdev->dev, "%s: construct failed: %d\n",
1463 __func__, status);
Dan Williams6f231dd2011-07-02 22:56:22 -07001464
1465 return status;
1466 }
1467
Dan Williams6f231dd2011-07-02 22:56:22 -07001468 /* start the device. */
Dan Williams89a73012011-06-30 19:14:33 -07001469 status = sci_remote_device_start(idev, ISCI_REMOTE_DEVICE_START_TIMEOUT);
Dan Williams6f231dd2011-07-02 22:56:22 -07001470
Dan Williams00d680e2011-04-25 14:29:29 -07001471 if (status != SCI_SUCCESS)
1472 dev_warn(&ihost->pdev->dev, "remote device start failed: %d\n",
1473 status);
Dan Williams6f231dd2011-07-02 22:56:22 -07001474
1475 return status;
1476}
1477
Dan Williams6f231dd2011-07-02 22:56:22 -07001478/**
1479 * This function builds the isci_remote_device when a libsas dev_found message
1480 * is received.
1481 * @isci_host: This parameter specifies the isci host object.
1482 * @port: This parameter specifies the isci_port conected to this device.
1483 *
1484 * pointer to new isci_remote_device.
1485 */
1486static struct isci_remote_device *
Dan Williamsd9c37392011-03-03 17:59:32 -08001487isci_remote_device_alloc(struct isci_host *ihost, struct isci_port *iport)
Dan Williams6f231dd2011-07-02 22:56:22 -07001488{
Dan Williamsd9c37392011-03-03 17:59:32 -08001489 struct isci_remote_device *idev;
1490 int i;
Dan Williams6f231dd2011-07-02 22:56:22 -07001491
Dan Williamsd9c37392011-03-03 17:59:32 -08001492 for (i = 0; i < SCI_MAX_REMOTE_DEVICES; i++) {
Dan Williams57f20f42011-04-21 18:14:45 -07001493 idev = &ihost->devices[i];
Dan Williamsd9c37392011-03-03 17:59:32 -08001494 if (!test_and_set_bit(IDEV_ALLOCATED, &idev->flags))
1495 break;
1496 }
Dan Williams6f231dd2011-07-02 22:56:22 -07001497
Dan Williamsd9c37392011-03-03 17:59:32 -08001498 if (i >= SCI_MAX_REMOTE_DEVICES) {
1499 dev_warn(&ihost->pdev->dev, "%s: failed\n", __func__);
Dan Williams6f231dd2011-07-02 22:56:22 -07001500 return NULL;
1501 }
Bartosz Barcinski6cb4d6b2011-04-12 17:28:43 -07001502 if (WARN_ONCE(!list_empty(&idev->node), "found non-idle remote device\n"))
1503 return NULL;
1504
Dan Williamsd9c37392011-03-03 17:59:32 -08001505 return idev;
Dan Williams6f231dd2011-07-02 22:56:22 -07001506}
Dan Williams6f231dd2011-07-02 22:56:22 -07001507
Dan Williams209fae12011-06-13 17:39:44 -07001508void isci_remote_device_release(struct kref *kref)
1509{
1510 struct isci_remote_device *idev = container_of(kref, typeof(*idev), kref);
1511 struct isci_host *ihost = idev->isci_port->isci_host;
1512
1513 idev->domain_dev = NULL;
1514 idev->isci_port = NULL;
1515 clear_bit(IDEV_START_PENDING, &idev->flags);
1516 clear_bit(IDEV_STOP_PENDING, &idev->flags);
Dan Williamsf2088262011-06-16 11:26:12 -07001517 clear_bit(IDEV_IO_READY, &idev->flags);
Dan Williams209fae12011-06-13 17:39:44 -07001518 clear_bit(IDEV_GONE, &idev->flags);
Dan Williams209fae12011-06-13 17:39:44 -07001519 smp_mb__before_clear_bit();
1520 clear_bit(IDEV_ALLOCATED, &idev->flags);
1521 wake_up(&ihost->eventq);
1522}
1523
Dan Williams6f231dd2011-07-02 22:56:22 -07001524/**
Dan Williams6f231dd2011-07-02 22:56:22 -07001525 * isci_remote_device_stop() - This function is called internally to stop the
1526 * remote device.
1527 * @isci_host: This parameter specifies the isci host object.
1528 * @isci_device: This parameter specifies the remote device.
1529 *
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001530 * The status of the ihost request to stop.
Dan Williams6f231dd2011-07-02 22:56:22 -07001531 */
Dan Williams6ad31fe2011-03-04 12:10:29 -08001532enum sci_status isci_remote_device_stop(struct isci_host *ihost, struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001533{
1534 enum sci_status status;
1535 unsigned long flags;
Dan Williams6f231dd2011-07-02 22:56:22 -07001536
Dan Williams6ad31fe2011-03-04 12:10:29 -08001537 dev_dbg(&ihost->pdev->dev,
1538 "%s: isci_device = %p\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001539
Dan Williams209fae12011-06-13 17:39:44 -07001540 spin_lock_irqsave(&ihost->scic_lock, flags);
1541 idev->domain_dev->lldd_dev = NULL; /* disable new lookups */
1542 set_bit(IDEV_GONE, &idev->flags);
Jeff Skirvin6e2802a2011-03-08 20:32:16 -07001543
Dan Williams6ad31fe2011-03-04 12:10:29 -08001544 set_bit(IDEV_STOP_PENDING, &idev->flags);
Dan Williams89a73012011-06-30 19:14:33 -07001545 status = sci_remote_device_stop(idev, 50);
Dan Williams6ad31fe2011-03-04 12:10:29 -08001546 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001547
1548 /* Wait for the stop complete callback. */
Dan Williams209fae12011-06-13 17:39:44 -07001549 if (WARN_ONCE(status != SCI_SUCCESS, "failed to stop device\n"))
1550 /* nothing to wait for */;
1551 else
Dan Williams6ad31fe2011-03-04 12:10:29 -08001552 wait_for_device_stop(ihost, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001553
Jeff Skirvin83884012012-03-08 22:41:52 -08001554 dev_dbg(&ihost->pdev->dev,
1555 "%s: isci_device = %p, waiting done.\n", __func__, idev);
1556
Dan Williams6f231dd2011-07-02 22:56:22 -07001557 return status;
1558}
1559
1560/**
1561 * isci_remote_device_gone() - This function is called by libsas when a domain
1562 * device is removed.
1563 * @domain_device: This parameter specifies the libsas domain device.
1564 *
1565 */
Dan Williams6ad31fe2011-03-04 12:10:29 -08001566void isci_remote_device_gone(struct domain_device *dev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001567{
Dan Williams4393aa42011-03-31 13:10:44 -07001568 struct isci_host *ihost = dev_to_ihost(dev);
Dan Williams6ad31fe2011-03-04 12:10:29 -08001569 struct isci_remote_device *idev = dev->lldd_dev;
Dan Williams6f231dd2011-07-02 22:56:22 -07001570
Dan Williams6ad31fe2011-03-04 12:10:29 -08001571 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -07001572 "%s: domain_device = %p, isci_device = %p, isci_port = %p\n",
Dan Williams6ad31fe2011-03-04 12:10:29 -08001573 __func__, dev, idev, idev->isci_port);
Dan Williams6f231dd2011-07-02 22:56:22 -07001574
Dan Williams6ad31fe2011-03-04 12:10:29 -08001575 isci_remote_device_stop(ihost, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001576}
1577
1578
1579/**
1580 * isci_remote_device_found() - This function is called by libsas when a remote
1581 * device is discovered. A remote device object is created and started. the
1582 * function then sleeps until the sci core device started message is
1583 * received.
1584 * @domain_device: This parameter specifies the libsas domain device.
1585 *
1586 * status, zero indicates success.
1587 */
Dan Williamsc132f692012-01-03 23:26:08 -08001588int isci_remote_device_found(struct domain_device *dev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001589{
Dan Williamsc132f692012-01-03 23:26:08 -08001590 struct isci_host *isci_host = dev_to_ihost(dev);
1591 struct isci_port *isci_port = dev->port->lldd_port;
Dan Williams6f231dd2011-07-02 22:56:22 -07001592 struct isci_remote_device *isci_device;
1593 enum sci_status status;
Dan Williams6f231dd2011-07-02 22:56:22 -07001594
Dan Williams6f231dd2011-07-02 22:56:22 -07001595 dev_dbg(&isci_host->pdev->dev,
Dan Williamsc132f692012-01-03 23:26:08 -08001596 "%s: domain_device = %p\n", __func__, dev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001597
Dan Williamsc132f692012-01-03 23:26:08 -08001598 if (!isci_port)
1599 return -ENODEV;
Dan Williams6f231dd2011-07-02 22:56:22 -07001600
Dan Williams6f231dd2011-07-02 22:56:22 -07001601 isci_device = isci_remote_device_alloc(isci_host, isci_port);
Dan Williamsd9c37392011-03-03 17:59:32 -08001602 if (!isci_device)
1603 return -ENODEV;
Dan Williams6f231dd2011-07-02 22:56:22 -07001604
Dan Williams209fae12011-06-13 17:39:44 -07001605 kref_init(&isci_device->kref);
Dan Williams6f231dd2011-07-02 22:56:22 -07001606 INIT_LIST_HEAD(&isci_device->node);
Dan Williams209fae12011-06-13 17:39:44 -07001607
1608 spin_lock_irq(&isci_host->scic_lock);
Dan Williamsc132f692012-01-03 23:26:08 -08001609 isci_device->domain_dev = dev;
Dan Williams6f231dd2011-07-02 22:56:22 -07001610 isci_device->isci_port = isci_port;
Dan Williams6f231dd2011-07-02 22:56:22 -07001611 list_add_tail(&isci_device->node, &isci_port->remote_dev_list);
1612
Dan Williams6ad31fe2011-03-04 12:10:29 -08001613 set_bit(IDEV_START_PENDING, &isci_device->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001614 status = isci_remote_device_construct(isci_port, isci_device);
Dan Williams6f231dd2011-07-02 22:56:22 -07001615
Dan Williams6f231dd2011-07-02 22:56:22 -07001616 dev_dbg(&isci_host->pdev->dev,
1617 "%s: isci_device = %p\n",
1618 __func__, isci_device);
1619
Dan Williams209fae12011-06-13 17:39:44 -07001620 if (status == SCI_SUCCESS) {
1621 /* device came up, advertise it to the world */
Dan Williamsc132f692012-01-03 23:26:08 -08001622 dev->lldd_dev = isci_device;
Dan Williams209fae12011-06-13 17:39:44 -07001623 } else
1624 isci_put_device(isci_device);
1625 spin_unlock_irq(&isci_host->scic_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07001626
Dan Williams6ad31fe2011-03-04 12:10:29 -08001627 /* wait for the device ready callback. */
1628 wait_for_device_start(isci_host, isci_device);
1629
Dan Williams209fae12011-06-13 17:39:44 -07001630 return status == SCI_SUCCESS ? 0 : -ENODEV;
Dan Williams6f231dd2011-07-02 22:56:22 -07001631}
Jeff Skirvin726980d2012-03-08 22:41:50 -08001632
Jeff Skirvin14aaa9f2012-03-08 22:41:54 -08001633enum sci_status isci_remote_device_suspend_terminate(
Jeff Skirvin5b6bf222012-03-08 22:41:51 -08001634 struct isci_host *ihost,
Jeff Skirvin14aaa9f2012-03-08 22:41:54 -08001635 struct isci_remote_device *idev,
1636 struct isci_request *ireq)
Jeff Skirvin726980d2012-03-08 22:41:50 -08001637{
Jeff Skirvin726980d2012-03-08 22:41:50 -08001638 unsigned long flags;
1639 enum sci_status status;
1640
Jeff Skirvin14aaa9f2012-03-08 22:41:54 -08001641 /* Put the device into suspension. */
Jeff Skirvin726980d2012-03-08 22:41:50 -08001642 spin_lock_irqsave(&ihost->scic_lock, flags);
Jeff Skirvin0c3ce382012-03-08 22:42:02 -08001643 set_bit(IDEV_ABORT_PATH_ACTIVE, &idev->flags);
Jeff Skirvinc94fc1a2012-03-08 22:41:58 -08001644 sci_remote_device_suspend(idev, SCI_SW_SUSPEND_LINKHANG_DETECT);
Jeff Skirvin726980d2012-03-08 22:41:50 -08001645 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Jeff Skirvin14aaa9f2012-03-08 22:41:54 -08001646
1647 /* Terminate and wait for the completions. */
1648 status = isci_remote_device_terminate_requests(ihost, idev, ireq);
1649 if (status != SCI_SUCCESS)
Jeff Skirvin726980d2012-03-08 22:41:50 -08001650 dev_dbg(&ihost->pdev->dev,
Jeff Skirvin14aaa9f2012-03-08 22:41:54 -08001651 "%s: isci_remote_device_terminate_requests(%p) "
1652 "returned %d!\n",
Jeff Skirvin726980d2012-03-08 22:41:50 -08001653 __func__, idev, status);
Jeff Skirvin14aaa9f2012-03-08 22:41:54 -08001654
1655 /* NOTE: RNC resumption is left to the caller! */
Jeff Skirvin726980d2012-03-08 22:41:50 -08001656 return status;
1657}
1658
1659int isci_remote_device_is_safe_to_abort(
1660 struct isci_remote_device *idev)
1661{
1662 return sci_remote_node_context_is_safe_to_abort(&idev->rnc);
1663}
1664
1665enum sci_status sci_remote_device_abort_requests_pending_abort(
1666 struct isci_remote_device *idev)
1667{
Jeff Skirvin14aaa9f2012-03-08 22:41:54 -08001668 return sci_remote_device_terminate_reqs_checkabort(idev, 1);
Jeff Skirvin726980d2012-03-08 22:41:50 -08001669}
Jeff Skirvin5b6bf222012-03-08 22:41:51 -08001670
1671enum sci_status isci_remote_device_reset_complete(
1672 struct isci_host *ihost,
1673 struct isci_remote_device *idev)
1674{
1675 unsigned long flags;
1676 enum sci_status status;
1677
1678 spin_lock_irqsave(&ihost->scic_lock, flags);
1679 status = sci_remote_device_reset_complete(idev);
Jeff Skirvin5b6bf222012-03-08 22:41:51 -08001680 spin_unlock_irqrestore(&ihost->scic_lock, flags);
1681
1682 return status;
1683}
1684
Jeff Skirvin9608b642012-03-08 22:42:00 -08001685void isci_dev_set_hang_detection_timeout(
1686 struct isci_remote_device *idev,
1687 u32 timeout)
1688{
1689 if (dev_is_sata(idev->domain_dev)) {
1690 if (timeout) {
1691 if (test_and_set_bit(IDEV_RNC_LLHANG_ENABLED,
1692 &idev->flags))
1693 return; /* Already enabled. */
1694 } else if (!test_and_clear_bit(IDEV_RNC_LLHANG_ENABLED,
1695 &idev->flags))
1696 return; /* Not enabled. */
1697
1698 sci_port_set_hang_detection_timeout(idev->owning_port,
1699 timeout);
1700 }
1701}