blob: 3f04e97128a6ae1885733af0732dcd5c1aa1515f [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 */
55
56#include <linux/completion.h>
Dan Williams50e7f9b2011-03-09 21:27:46 -080057#include <linux/irqflags.h>
Dave Jiangaf5ae892011-05-04 17:53:24 -070058#include "sas.h"
Jeff Skirvin61aaff42011-06-21 12:16:33 -070059#include <scsi/libsas.h>
Dan Williams88f3b622011-04-22 19:18:03 -070060#include "remote_device.h"
61#include "remote_node_context.h"
Dan Williams6f231dd2011-07-02 22:56:22 -070062#include "isci.h"
63#include "request.h"
Dan Williams6f231dd2011-07-02 22:56:22 -070064#include "task.h"
Dan Williams312e0c22011-06-28 13:47:09 -070065#include "host.h"
Dave Jiangaf5ae892011-05-04 17:53:24 -070066
Dan Williams50e7f9b2011-03-09 21:27:46 -080067/**
Dan Williams1077a572011-03-11 10:13:51 -080068* isci_task_refuse() - complete the request to the upper layer driver in
69* the case where an I/O needs to be completed back in the submit path.
70* @ihost: host on which the the request was queued
71* @task: request to complete
72* @response: response code for the completed task.
73* @status: status code for the completed task.
Dan Williams50e7f9b2011-03-09 21:27:46 -080074*
Dan Williams50e7f9b2011-03-09 21:27:46 -080075*/
Dan Williams1077a572011-03-11 10:13:51 -080076static void isci_task_refuse(struct isci_host *ihost, struct sas_task *task,
77 enum service_response response,
78 enum exec_status status)
Dan Williams50e7f9b2011-03-09 21:27:46 -080079
Dan Williams1077a572011-03-11 10:13:51 -080080{
81 enum isci_completion_selection disposition;
82
83 disposition = isci_perform_normal_io_completion;
84 disposition = isci_task_set_completion_status(task, response, status,
85 disposition);
Dan Williams50e7f9b2011-03-09 21:27:46 -080086
87 /* Tasks aborted specifically by a call to the lldd_abort_task
Dan Williams1077a572011-03-11 10:13:51 -080088 * function should not be completed to the host in the regular path.
89 */
90 switch (disposition) {
James Bottomleya5ec7f862011-07-03 14:14:45 -050091 case isci_perform_normal_io_completion:
92 /* Normal notification (task_done) */
93 dev_dbg(&ihost->pdev->dev,
94 "%s: Normal - task = %p, response=%d, "
95 "status=%d\n",
96 __func__, task, response, status);
Dan Williams50e7f9b2011-03-09 21:27:46 -080097
James Bottomleya5ec7f862011-07-03 14:14:45 -050098 task->lldd_task = NULL;
Dan Williams312d3e52011-11-17 17:59:50 -080099 task->task_done(task);
James Bottomleya5ec7f862011-07-03 14:14:45 -0500100 break;
Dan Williams50e7f9b2011-03-09 21:27:46 -0800101
James Bottomleya5ec7f862011-07-03 14:14:45 -0500102 case isci_perform_aborted_io_completion:
103 /*
104 * No notification because this request is already in the
105 * abort path.
106 */
107 dev_dbg(&ihost->pdev->dev,
108 "%s: Aborted - task = %p, response=%d, "
109 "status=%d\n",
110 __func__, task, response, status);
111 break;
Dan Williams50e7f9b2011-03-09 21:27:46 -0800112
James Bottomleya5ec7f862011-07-03 14:14:45 -0500113 case isci_perform_error_io_completion:
114 /* Use sas_task_abort */
115 dev_dbg(&ihost->pdev->dev,
116 "%s: Error - task = %p, response=%d, "
117 "status=%d\n",
118 __func__, task, response, status);
Dan Williams312d3e52011-11-17 17:59:50 -0800119 sas_task_abort(task);
James Bottomleya5ec7f862011-07-03 14:14:45 -0500120 break;
Dan Williams50e7f9b2011-03-09 21:27:46 -0800121
James Bottomleya5ec7f862011-07-03 14:14:45 -0500122 default:
123 dev_dbg(&ihost->pdev->dev,
124 "%s: isci task notification default case!",
125 __func__);
126 sas_task_abort(task);
127 break;
Dan Williams50e7f9b2011-03-09 21:27:46 -0800128 }
129}
Dan Williams6f231dd2011-07-02 22:56:22 -0700130
Dan Williams1077a572011-03-11 10:13:51 -0800131#define for_each_sas_task(num, task) \
132 for (; num > 0; num--,\
133 task = list_entry(task->list.next, struct sas_task, list))
134
Jeff Skirvin9274f452011-06-23 17:09:02 -0700135
136static inline int isci_device_io_ready(struct isci_remote_device *idev,
137 struct sas_task *task)
138{
139 return idev ? test_bit(IDEV_IO_READY, &idev->flags) ||
140 (test_bit(IDEV_IO_NCQERROR, &idev->flags) &&
141 isci_task_is_ncq_recovery(task))
142 : 0;
143}
Dan Williams6f231dd2011-07-02 22:56:22 -0700144/**
145 * isci_task_execute_task() - This function is one of the SAS Domain Template
146 * functions. This function is called by libsas to send a task down to
147 * hardware.
148 * @task: This parameter specifies the SAS task to send.
149 * @num: This parameter specifies the number of tasks to queue.
150 * @gfp_flags: This parameter specifies the context of this call.
151 *
152 * status, zero indicates success.
153 */
154int isci_task_execute_task(struct sas_task *task, int num, gfp_t gfp_flags)
155{
Dan Williams4393aa42011-03-31 13:10:44 -0700156 struct isci_host *ihost = dev_to_ihost(task->dev);
Dan Williams209fae12011-06-13 17:39:44 -0700157 struct isci_remote_device *idev;
Dan Williamsf2088262011-06-16 11:26:12 -0700158 unsigned long flags;
159 bool io_ready;
Dan Williams312e0c22011-06-28 13:47:09 -0700160 u16 tag;
Dan Williams6f231dd2011-07-02 22:56:22 -0700161
Dan Williams1077a572011-03-11 10:13:51 -0800162 dev_dbg(&ihost->pdev->dev, "%s: num=%d\n", __func__, num);
Dan Williams6f231dd2011-07-02 22:56:22 -0700163
Dan Williams1077a572011-03-11 10:13:51 -0800164 for_each_sas_task(num, task) {
Dan Williams312e0c22011-06-28 13:47:09 -0700165 enum sci_status status = SCI_FAILURE;
166
Dan Williams209fae12011-06-13 17:39:44 -0700167 spin_lock_irqsave(&ihost->scic_lock, flags);
168 idev = isci_lookup_device(task->dev);
Jeff Skirvin9274f452011-06-23 17:09:02 -0700169 io_ready = isci_device_io_ready(idev, task);
Dan Williams312e0c22011-06-28 13:47:09 -0700170 tag = isci_alloc_tag(ihost);
Dan Williams209fae12011-06-13 17:39:44 -0700171 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700172
Dan Williamsf2088262011-06-16 11:26:12 -0700173 dev_dbg(&ihost->pdev->dev,
174 "task: %p, num: %d dev: %p idev: %p:%#lx cmd = %p\n",
175 task, num, task->dev, idev, idev ? idev->flags : 0,
176 task->uldd_task);
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700177
Dan Williamsf2088262011-06-16 11:26:12 -0700178 if (!idev) {
179 isci_task_refuse(ihost, task, SAS_TASK_UNDELIVERED,
180 SAS_DEVICE_UNKNOWN);
Dan Williams312e0c22011-06-28 13:47:09 -0700181 } else if (!io_ready || tag == SCI_CONTROLLER_INVALID_IO_TAG) {
Dan Williamsf2088262011-06-16 11:26:12 -0700182 /* Indicate QUEUE_FULL so that the scsi midlayer
183 * retries.
184 */
185 isci_task_refuse(ihost, task, SAS_TASK_COMPLETE,
186 SAS_QUEUE_FULL);
Dan Williams6f231dd2011-07-02 22:56:22 -0700187 } else {
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700188 /* There is a device and it's ready for I/O. */
189 spin_lock_irqsave(&task->task_state_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700190
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700191 if (task->task_state_flags & SAS_TASK_STATE_ABORTED) {
Dan Williamsf2088262011-06-16 11:26:12 -0700192 /* The I/O was aborted. */
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700193 spin_unlock_irqrestore(&task->task_state_lock,
194 flags);
195
Dan Williams1077a572011-03-11 10:13:51 -0800196 isci_task_refuse(ihost, task,
197 SAS_TASK_UNDELIVERED,
198 SAM_STAT_TASK_ABORTED);
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700199 } else {
Dan Williams6f231dd2011-07-02 22:56:22 -0700200 task->task_state_flags |= SAS_TASK_AT_INITIATOR;
201 spin_unlock_irqrestore(&task->task_state_lock, flags);
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700202
203 /* build and send the request. */
Dan Williamsdb056252011-06-17 14:18:39 -0700204 status = isci_request_execute(ihost, idev, task, tag);
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700205
206 if (status != SCI_SUCCESS) {
207
208 spin_lock_irqsave(&task->task_state_lock, flags);
209 /* Did not really start this command. */
210 task->task_state_flags &= ~SAS_TASK_AT_INITIATOR;
211 spin_unlock_irqrestore(&task->task_state_lock, flags);
212
Jeff Skirvinc2cb8a52011-10-27 15:04:56 -0700213 if (test_bit(IDEV_GONE, &idev->flags)) {
214
215 /* Indicate that the device
216 * is gone.
217 */
218 isci_task_refuse(ihost, task,
219 SAS_TASK_UNDELIVERED,
220 SAS_DEVICE_UNKNOWN);
221 } else {
222 /* Indicate QUEUE_FULL so that
223 * the scsi midlayer retries.
224 * If the request failed for
225 * remote device reasons, it
226 * gets returned as
227 * SAS_TASK_UNDELIVERED next
228 * time through.
229 */
230 isci_task_refuse(ihost, task,
231 SAS_TASK_COMPLETE,
232 SAS_QUEUE_FULL);
233 }
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700234 }
Dan Williams6f231dd2011-07-02 22:56:22 -0700235 }
236 }
Dan Williams312e0c22011-06-28 13:47:09 -0700237 if (status != SCI_SUCCESS && tag != SCI_CONTROLLER_INVALID_IO_TAG) {
238 spin_lock_irqsave(&ihost->scic_lock, flags);
239 /* command never hit the device, so just free
240 * the tci and skip the sequence increment
241 */
242 isci_tci_free(ihost, ISCI_TAG_TCI(tag));
243 spin_unlock_irqrestore(&ihost->scic_lock, flags);
244 }
Dan Williams209fae12011-06-13 17:39:44 -0700245 isci_put_device(idev);
Dan Williams1077a572011-03-11 10:13:51 -0800246 }
Dan Williams6f231dd2011-07-02 22:56:22 -0700247 return 0;
248}
249
Dan Williams0d0cf142011-06-13 00:51:30 -0700250static struct isci_request *isci_task_request_build(struct isci_host *ihost,
Dan Williams209fae12011-06-13 17:39:44 -0700251 struct isci_remote_device *idev,
Dan Williams312e0c22011-06-28 13:47:09 -0700252 u16 tag, struct isci_tmf *isci_tmf)
Dan Williams6f231dd2011-07-02 22:56:22 -0700253{
Dan Williams6f231dd2011-07-02 22:56:22 -0700254 enum sci_status status = SCI_FAILURE;
Dan Williams0d0cf142011-06-13 00:51:30 -0700255 struct isci_request *ireq = NULL;
Dan Williamsa1a113b2011-04-21 18:44:45 -0700256 struct domain_device *dev;
Dan Williams6f231dd2011-07-02 22:56:22 -0700257
Dan Williams0d0cf142011-06-13 00:51:30 -0700258 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -0700259 "%s: isci_tmf = %p\n", __func__, isci_tmf);
260
Dan Williams0d0cf142011-06-13 00:51:30 -0700261 dev = idev->domain_dev;
Dan Williams6f231dd2011-07-02 22:56:22 -0700262
263 /* do common allocation and init of request object. */
Dan Williamsdb056252011-06-17 14:18:39 -0700264 ireq = isci_tmf_request_from_tag(ihost, isci_tmf, tag);
Dan Williams0d0cf142011-06-13 00:51:30 -0700265 if (!ireq)
266 return NULL;
Dan Williams6f231dd2011-07-02 22:56:22 -0700267
268 /* let the core do it's construct. */
Dan Williams89a73012011-06-30 19:14:33 -0700269 status = sci_task_request_construct(ihost, idev, tag,
Dan Williams5076a1a2011-06-27 14:57:03 -0700270 ireq);
Dan Williams6f231dd2011-07-02 22:56:22 -0700271
272 if (status != SCI_SUCCESS) {
Dan Williams0d0cf142011-06-13 00:51:30 -0700273 dev_warn(&ihost->pdev->dev,
Dan Williams89a73012011-06-30 19:14:33 -0700274 "%s: sci_task_request_construct failed - "
Dan Williams6f231dd2011-07-02 22:56:22 -0700275 "status = 0x%x\n",
276 __func__,
277 status);
Dan Williamsdb056252011-06-17 14:18:39 -0700278 return NULL;
Dan Williams6f231dd2011-07-02 22:56:22 -0700279 }
280
Dan Williamsa1a113b2011-04-21 18:44:45 -0700281 /* XXX convert to get this from task->tproto like other drivers */
282 if (dev->dev_type == SAS_END_DEV) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700283 isci_tmf->proto = SAS_PROTOCOL_SSP;
Dan Williams89a73012011-06-30 19:14:33 -0700284 status = sci_task_request_construct_ssp(ireq);
Dan Williams6f231dd2011-07-02 22:56:22 -0700285 if (status != SCI_SUCCESS)
Dan Williamsdb056252011-06-17 14:18:39 -0700286 return NULL;
Dan Williams6f231dd2011-07-02 22:56:22 -0700287 }
288
Dan Williams0d0cf142011-06-13 00:51:30 -0700289 return ireq;
Dan Williams6f231dd2011-07-02 22:56:22 -0700290}
291
Jeff Skirvinb343dff2011-10-27 15:05:06 -0700292/**
293* isci_request_mark_zombie() - This function must be called with scic_lock held.
294*/
295static void isci_request_mark_zombie(struct isci_host *ihost, struct isci_request *ireq)
296{
297 struct completion *tmf_completion = NULL;
298 struct completion *req_completion;
299
300 /* Set the request state to "dead". */
301 ireq->status = dead;
302
303 req_completion = ireq->io_request_completion;
304 ireq->io_request_completion = NULL;
305
Jeff Skirvin3b34c162011-10-27 15:05:22 -0700306 if (test_bit(IREQ_TMF, &ireq->flags)) {
Jeff Skirvinb343dff2011-10-27 15:05:06 -0700307 /* Break links with the TMF request. */
308 struct isci_tmf *tmf = isci_request_access_tmf(ireq);
309
310 /* In the case where a task request is dying,
311 * the thread waiting on the complete will sit and
312 * timeout unless we wake it now. Since the TMF
313 * has a default error status, complete it here
314 * to wake the waiting thread.
315 */
316 if (tmf) {
317 tmf_completion = tmf->complete;
318 tmf->complete = NULL;
319 }
320 ireq->ttype_ptr.tmf_task_ptr = NULL;
321 dev_dbg(&ihost->pdev->dev, "%s: tmf_code %d, managed tag %#x\n",
322 __func__, tmf->tmf_code, tmf->io_tag);
Jeff Skirvin3b34c162011-10-27 15:05:22 -0700323 } else {
324 /* Break links with the sas_task - the callback is done
325 * elsewhere.
326 */
327 struct sas_task *task = isci_request_access_task(ireq);
328
329 if (task)
330 task->lldd_task = NULL;
331
332 ireq->ttype_ptr.io_task_ptr = NULL;
Jeff Skirvinb343dff2011-10-27 15:05:06 -0700333 }
334
335 dev_warn(&ihost->pdev->dev, "task context unrecoverable (tag: %#x)\n",
336 ireq->io_tag);
337
338 /* Don't force waiting threads to timeout. */
339 if (req_completion)
340 complete(req_completion);
341
342 if (tmf_completion != NULL)
343 complete(tmf_completion);
344}
345
Dan Williams16ba7702011-07-01 10:52:55 -0700346static int isci_task_execute_tmf(struct isci_host *ihost,
347 struct isci_remote_device *idev,
348 struct isci_tmf *tmf, unsigned long timeout_ms)
Dan Williams6f231dd2011-07-02 22:56:22 -0700349{
350 DECLARE_COMPLETION_ONSTACK(completion);
Bartosz Barcinski467e8552011-04-12 17:28:41 -0700351 enum sci_task_status status = SCI_TASK_FAILURE;
Dan Williams0d0cf142011-06-13 00:51:30 -0700352 struct isci_request *ireq;
Dan Williams6f231dd2011-07-02 22:56:22 -0700353 int ret = TMF_RESP_FUNC_FAILED;
354 unsigned long flags;
Edmund Nadolskifd183882011-05-19 12:00:15 +0000355 unsigned long timeleft;
Dan Williams312e0c22011-06-28 13:47:09 -0700356 u16 tag;
357
358 spin_lock_irqsave(&ihost->scic_lock, flags);
359 tag = isci_alloc_tag(ihost);
360 spin_unlock_irqrestore(&ihost->scic_lock, flags);
361
362 if (tag == SCI_CONTROLLER_INVALID_IO_TAG)
363 return ret;
Dan Williams6f231dd2011-07-02 22:56:22 -0700364
365 /* sanity check, return TMF_RESP_FUNC_FAILED
366 * if the device is not there and ready.
367 */
Dan Williams78a6f062011-06-30 16:31:37 -0700368 if (!idev ||
369 (!test_bit(IDEV_IO_READY, &idev->flags) &&
370 !test_bit(IDEV_IO_NCQERROR, &idev->flags))) {
Dan Williams0d0cf142011-06-13 00:51:30 -0700371 dev_dbg(&ihost->pdev->dev,
Dan Williams78a6f062011-06-30 16:31:37 -0700372 "%s: idev = %p not ready (%#lx)\n",
Dan Williams6f231dd2011-07-02 22:56:22 -0700373 __func__,
Dan Williams78a6f062011-06-30 16:31:37 -0700374 idev, idev ? idev->flags : 0);
Dan Williams312e0c22011-06-28 13:47:09 -0700375 goto err_tci;
Dan Williams6f231dd2011-07-02 22:56:22 -0700376 } else
Dan Williams0d0cf142011-06-13 00:51:30 -0700377 dev_dbg(&ihost->pdev->dev,
Dan Williams78a6f062011-06-30 16:31:37 -0700378 "%s: idev = %p\n",
379 __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -0700380
381 /* Assign the pointer to the TMF's completion kernel wait structure. */
382 tmf->complete = &completion;
Jeff Skirvinb343dff2011-10-27 15:05:06 -0700383 tmf->status = SCI_FAILURE_TIMEOUT;
Dan Williams6f231dd2011-07-02 22:56:22 -0700384
Dan Williams78a6f062011-06-30 16:31:37 -0700385 ireq = isci_task_request_build(ihost, idev, tag, tmf);
Dan Williams312e0c22011-06-28 13:47:09 -0700386 if (!ireq)
387 goto err_tci;
Dan Williams6f231dd2011-07-02 22:56:22 -0700388
Dan Williams0d0cf142011-06-13 00:51:30 -0700389 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700390
391 /* start the TMF io. */
Dan Williams89a73012011-06-30 19:14:33 -0700392 status = sci_controller_start_task(ihost, idev, ireq);
Dan Williams6f231dd2011-07-02 22:56:22 -0700393
Bartosz Barcinski467e8552011-04-12 17:28:41 -0700394 if (status != SCI_TASK_SUCCESS) {
Dan Williamsa8a0a132011-07-01 12:07:25 -0700395 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -0700396 "%s: start_io failed - status = 0x%x, request = %p\n",
397 __func__,
398 status,
Dan Williams0d0cf142011-06-13 00:51:30 -0700399 ireq);
400 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williamsdb056252011-06-17 14:18:39 -0700401 goto err_tci;
Dan Williams6f231dd2011-07-02 22:56:22 -0700402 }
403
Dan Williams6f231dd2011-07-02 22:56:22 -0700404 if (tmf->cb_state_func != NULL)
405 tmf->cb_state_func(isci_tmf_started, tmf, tmf->cb_data);
406
Dan Williams0d0cf142011-06-13 00:51:30 -0700407 isci_request_change_state(ireq, started);
Dan Williams6f231dd2011-07-02 22:56:22 -0700408
409 /* add the request to the remote device request list. */
Dan Williams78a6f062011-06-30 16:31:37 -0700410 list_add(&ireq->dev_node, &idev->reqs_in_process);
Dan Williams6f231dd2011-07-02 22:56:22 -0700411
Dan Williams0d0cf142011-06-13 00:51:30 -0700412 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700413
414 /* Wait for the TMF to complete, or a timeout. */
Edmund Nadolskifd183882011-05-19 12:00:15 +0000415 timeleft = wait_for_completion_timeout(&completion,
Dan Williams086a0da2011-06-21 16:23:03 -0700416 msecs_to_jiffies(timeout_ms));
Edmund Nadolskifd183882011-05-19 12:00:15 +0000417
418 if (timeleft == 0) {
Jeff Skirvinb343dff2011-10-27 15:05:06 -0700419 /* The TMF did not complete - this could be because
420 * of an unplug. Terminate the TMF request now.
421 */
Dan Williams0d0cf142011-06-13 00:51:30 -0700422 spin_lock_irqsave(&ihost->scic_lock, flags);
Edmund Nadolskifd183882011-05-19 12:00:15 +0000423
424 if (tmf->cb_state_func != NULL)
Jeff Skirvinb343dff2011-10-27 15:05:06 -0700425 tmf->cb_state_func(isci_tmf_timed_out, tmf,
426 tmf->cb_data);
Edmund Nadolskifd183882011-05-19 12:00:15 +0000427
Jeff Skirvinb343dff2011-10-27 15:05:06 -0700428 sci_controller_terminate_request(ihost, idev, ireq);
Edmund Nadolskifd183882011-05-19 12:00:15 +0000429
Dan Williams0d0cf142011-06-13 00:51:30 -0700430 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams086a0da2011-06-21 16:23:03 -0700431
Jeff Skirvinb343dff2011-10-27 15:05:06 -0700432 timeleft = wait_for_completion_timeout(
433 &completion,
434 msecs_to_jiffies(ISCI_TERMINATION_TIMEOUT_MSEC));
435
436 if (!timeleft) {
437 /* Strange condition - the termination of the TMF
438 * request timed-out.
439 */
440 spin_lock_irqsave(&ihost->scic_lock, flags);
441
442 /* If the TMF status has not changed, kill it. */
443 if (tmf->status == SCI_FAILURE_TIMEOUT)
444 isci_request_mark_zombie(ihost, ireq);
445
446 spin_unlock_irqrestore(&ihost->scic_lock, flags);
447 }
Edmund Nadolskifd183882011-05-19 12:00:15 +0000448 }
Dan Williams6f231dd2011-07-02 22:56:22 -0700449
Maciej Trela27234ab2012-01-04 01:33:05 -0800450 isci_print_tmf(ihost, tmf);
Dan Williams6f231dd2011-07-02 22:56:22 -0700451
452 if (tmf->status == SCI_SUCCESS)
453 ret = TMF_RESP_FUNC_COMPLETE;
454 else if (tmf->status == SCI_FAILURE_IO_RESPONSE_VALID) {
Dan Williams0d0cf142011-06-13 00:51:30 -0700455 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -0700456 "%s: tmf.status == "
457 "SCI_FAILURE_IO_RESPONSE_VALID\n",
458 __func__);
459 ret = TMF_RESP_FUNC_COMPLETE;
460 }
461 /* Else - leave the default "failed" status alone. */
462
Dan Williams0d0cf142011-06-13 00:51:30 -0700463 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -0700464 "%s: completed request = %p\n",
465 __func__,
Dan Williams0d0cf142011-06-13 00:51:30 -0700466 ireq);
Dan Williams6f231dd2011-07-02 22:56:22 -0700467
Dan Williams6f231dd2011-07-02 22:56:22 -0700468 return ret;
Dan Williams312e0c22011-06-28 13:47:09 -0700469
Dan Williams312e0c22011-06-28 13:47:09 -0700470 err_tci:
471 spin_lock_irqsave(&ihost->scic_lock, flags);
472 isci_tci_free(ihost, ISCI_TAG_TCI(tag));
473 spin_unlock_irqrestore(&ihost->scic_lock, flags);
474
475 return ret;
Dan Williams6f231dd2011-07-02 22:56:22 -0700476}
477
Dan Williams16ba7702011-07-01 10:52:55 -0700478static void isci_task_build_tmf(struct isci_tmf *tmf,
479 enum isci_tmf_function_codes code,
480 void (*tmf_sent_cb)(enum isci_tmf_cb_state,
481 struct isci_tmf *,
482 void *),
483 void *cb_data)
Dan Williams6f231dd2011-07-02 22:56:22 -0700484{
Dan Williams6f231dd2011-07-02 22:56:22 -0700485 memset(tmf, 0, sizeof(*tmf));
486
Dan Williams6f231dd2011-07-02 22:56:22 -0700487 tmf->tmf_code = code;
Dan Williams6f231dd2011-07-02 22:56:22 -0700488 tmf->cb_state_func = tmf_sent_cb;
Jeff Skirvinc3f42fe2011-03-04 14:06:56 -0800489 tmf->cb_data = cb_data;
490}
Jeff Skirvin1fad9e92011-03-04 14:06:46 -0800491
Dan Williams16ba7702011-07-01 10:52:55 -0700492static void isci_task_build_abort_task_tmf(struct isci_tmf *tmf,
493 enum isci_tmf_function_codes code,
494 void (*tmf_sent_cb)(enum isci_tmf_cb_state,
495 struct isci_tmf *,
496 void *),
497 struct isci_request *old_request)
Jeff Skirvinc3f42fe2011-03-04 14:06:56 -0800498{
Dan Williams16ba7702011-07-01 10:52:55 -0700499 isci_task_build_tmf(tmf, code, tmf_sent_cb, old_request);
Jeff Skirvinc3f42fe2011-03-04 14:06:56 -0800500 tmf->io_tag = old_request->io_tag;
Dan Williams6f231dd2011-07-02 22:56:22 -0700501}
502
Dan Williams6f231dd2011-07-02 22:56:22 -0700503/**
504 * isci_task_validate_request_to_abort() - This function checks the given I/O
505 * against the "started" state. If the request is still "started", it's
506 * state is changed to aborted. NOTE: isci_host->scic_lock MUST BE HELD
507 * BEFORE CALLING THIS FUNCTION.
508 * @isci_request: This parameter specifies the request object to control.
509 * @isci_host: This parameter specifies the ISCI host object
510 * @isci_device: This is the device to which the request is pending.
511 * @aborted_io_completion: This is a completion structure that will be added to
512 * the request in case it is changed to aborting; this completion is
513 * triggered when the request is fully completed.
514 *
515 * Either "started" on successful change of the task status to "aborted", or
516 * "unallocated" if the task cannot be controlled.
517 */
518static enum isci_request_status isci_task_validate_request_to_abort(
519 struct isci_request *isci_request,
520 struct isci_host *isci_host,
521 struct isci_remote_device *isci_device,
522 struct completion *aborted_io_completion)
523{
524 enum isci_request_status old_state = unallocated;
525
526 /* Only abort the task if it's in the
527 * device's request_in_process list
528 */
529 if (isci_request && !list_empty(&isci_request->dev_node)) {
530 old_state = isci_request_change_started_to_aborted(
531 isci_request, aborted_io_completion);
532
Dan Williams6f231dd2011-07-02 22:56:22 -0700533 }
534
535 return old_state;
536}
537
Jeff Skirvind6891682011-10-27 15:05:01 -0700538static int isci_request_is_dealloc_managed(enum isci_request_status stat)
539{
540 switch (stat) {
541 case aborted:
542 case aborting:
543 case terminating:
544 case completed:
545 case dead:
546 return true;
547 default:
548 return false;
549 }
550}
551
Dan Williams6f231dd2011-07-02 22:56:22 -0700552/**
553 * isci_terminate_request_core() - This function will terminate the given
554 * request, and wait for it to complete. This function must only be called
555 * from a thread that can wait. Note that the request is terminated and
556 * completed (back to the host, if started there).
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700557 * @ihost: This SCU.
Dan Williams78a6f062011-06-30 16:31:37 -0700558 * @idev: The target.
Dan Williams6f231dd2011-07-02 22:56:22 -0700559 * @isci_request: The I/O request to be terminated.
560 *
Dan Williams6f231dd2011-07-02 22:56:22 -0700561 */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700562static void isci_terminate_request_core(struct isci_host *ihost,
563 struct isci_remote_device *idev,
564 struct isci_request *isci_request)
Dan Williams6f231dd2011-07-02 22:56:22 -0700565{
Jeff Skirvincbb65c62011-03-04 14:06:50 -0800566 enum sci_status status = SCI_SUCCESS;
Dan Williams6f231dd2011-07-02 22:56:22 -0700567 bool was_terminated = false;
568 bool needs_cleanup_handling = false;
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700569 unsigned long flags;
570 unsigned long termination_completed = 1;
571 struct completion *io_request_completion;
Dan Williams6f231dd2011-07-02 22:56:22 -0700572
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700573 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -0700574 "%s: device = %p; request = %p\n",
Dan Williams78a6f062011-06-30 16:31:37 -0700575 __func__, idev, isci_request);
Dan Williams6f231dd2011-07-02 22:56:22 -0700576
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700577 spin_lock_irqsave(&ihost->scic_lock, flags);
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800578
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700579 io_request_completion = isci_request->io_request_completion;
580
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800581 /* Note that we are not going to control
Dan Williams38d88792011-06-23 14:33:48 -0700582 * the target to abort the request.
583 */
584 set_bit(IREQ_COMPLETE_IN_TARGET, &isci_request->flags);
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800585
Dan Williams6f231dd2011-07-02 22:56:22 -0700586 /* Make sure the request wasn't just sitting around signalling
587 * device condition (if the request handle is NULL, then the
588 * request completed but needed additional handling here).
589 */
Dan Williams38d88792011-06-23 14:33:48 -0700590 if (!test_bit(IREQ_TERMINATED, &isci_request->flags)) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700591 was_terminated = true;
Jeff Skirvincbb65c62011-03-04 14:06:50 -0800592 needs_cleanup_handling = true;
Dan Williams89a73012011-06-30 19:14:33 -0700593 status = sci_controller_terminate_request(ihost,
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700594 idev,
595 isci_request);
Dan Williams6f231dd2011-07-02 22:56:22 -0700596 }
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700597 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700598
599 /*
600 * The only time the request to terminate will
601 * fail is when the io request is completed and
602 * being aborted.
603 */
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800604 if (status != SCI_SUCCESS) {
Dan Williamsa8a0a132011-07-01 12:07:25 -0700605 dev_dbg(&ihost->pdev->dev,
Dan Williams89a73012011-06-30 19:14:33 -0700606 "%s: sci_controller_terminate_request"
Dan Williams6f231dd2011-07-02 22:56:22 -0700607 " returned = 0x%x\n",
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700608 __func__, status);
609
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800610 isci_request->io_request_completion = NULL;
611
612 } else {
Dan Williams6f231dd2011-07-02 22:56:22 -0700613 if (was_terminated) {
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700614 dev_dbg(&ihost->pdev->dev,
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700615 "%s: before completion wait (%p/%p)\n",
616 __func__, isci_request, io_request_completion);
Dan Williams6f231dd2011-07-02 22:56:22 -0700617
618 /* Wait here for the request to complete. */
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700619 termination_completed
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800620 = wait_for_completion_timeout(
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700621 io_request_completion,
Jeff Skirvinb343dff2011-10-27 15:05:06 -0700622 msecs_to_jiffies(ISCI_TERMINATION_TIMEOUT_MSEC));
Dan Williams6f231dd2011-07-02 22:56:22 -0700623
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700624 if (!termination_completed) {
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800625
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700626 /* The request to terminate has timed out. */
Jeff Skirvinb343dff2011-10-27 15:05:06 -0700627 spin_lock_irqsave(&ihost->scic_lock, flags);
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800628
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700629 /* Check for state changes. */
Jeff Skirvinb343dff2011-10-27 15:05:06 -0700630 if (!test_bit(IREQ_TERMINATED,
631 &isci_request->flags)) {
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800632
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700633 /* The best we can do is to have the
634 * request die a silent death if it
635 * ever really completes.
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700636 */
Jeff Skirvinb343dff2011-10-27 15:05:06 -0700637 isci_request_mark_zombie(ihost,
638 isci_request);
639 needs_cleanup_handling = true;
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700640 } else
641 termination_completed = 1;
642
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700643 spin_unlock_irqrestore(&ihost->scic_lock,
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700644 flags);
645
646 if (!termination_completed) {
647
Dan Williamsa8a0a132011-07-01 12:07:25 -0700648 dev_dbg(&ihost->pdev->dev,
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700649 "%s: *** Timeout waiting for "
650 "termination(%p/%p)\n",
651 __func__, io_request_completion,
652 isci_request);
653
654 /* The request can no longer be referenced
655 * safely since it may go away if the
656 * termination every really does complete.
657 */
658 isci_request = NULL;
659 }
660 }
661 if (termination_completed)
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700662 dev_dbg(&ihost->pdev->dev,
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700663 "%s: after completion wait (%p/%p)\n",
664 __func__, isci_request, io_request_completion);
Dan Williams6f231dd2011-07-02 22:56:22 -0700665 }
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800666
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700667 if (termination_completed) {
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800668
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700669 isci_request->io_request_completion = NULL;
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800670
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700671 /* Peek at the status of the request. This will tell
672 * us if there was special handling on the request such that it
673 * needs to be detached and freed here.
674 */
675 spin_lock_irqsave(&isci_request->state_lock, flags);
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700676
Jeff Skirvind6891682011-10-27 15:05:01 -0700677 needs_cleanup_handling
678 = isci_request_is_dealloc_managed(
679 isci_request->status);
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700680
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700681 spin_unlock_irqrestore(&isci_request->state_lock, flags);
682
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800683 }
Jeff Skirvindb49c2d2011-10-27 15:05:11 -0700684 if (needs_cleanup_handling) {
685
686 dev_dbg(&ihost->pdev->dev,
687 "%s: cleanup isci_device=%p, request=%p\n",
688 __func__, idev, isci_request);
689
690 if (isci_request != NULL) {
691 spin_lock_irqsave(&ihost->scic_lock, flags);
692 isci_free_tag(ihost, isci_request->io_tag);
693 isci_request_change_state(isci_request, unallocated);
694 list_del_init(&isci_request->dev_node);
695 spin_unlock_irqrestore(&ihost->scic_lock, flags);
696 }
697 }
Jeff Skirvina5fde222011-03-04 14:06:42 -0800698 }
Dan Williams6f231dd2011-07-02 22:56:22 -0700699}
700
701/**
702 * isci_terminate_pending_requests() - This function will change the all of the
703 * requests on the given device's state to "aborting", will terminate the
704 * requests, and wait for them to complete. This function must only be
705 * called from a thread that can wait. Note that the requests are all
706 * terminated and completed (back to the host, if started there).
707 * @isci_host: This parameter specifies SCU.
Dan Williams78a6f062011-06-30 16:31:37 -0700708 * @idev: This parameter specifies the target.
Dan Williams6f231dd2011-07-02 22:56:22 -0700709 *
Dan Williams6f231dd2011-07-02 22:56:22 -0700710 */
Dan Williams980d3ae2011-06-20 15:11:22 -0700711void isci_terminate_pending_requests(struct isci_host *ihost,
712 struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -0700713{
Dan Williams980d3ae2011-06-20 15:11:22 -0700714 struct completion request_completion;
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700715 enum isci_request_status old_state;
Dan Williams980d3ae2011-06-20 15:11:22 -0700716 unsigned long flags;
717 LIST_HEAD(list);
Dan Williams6f231dd2011-07-02 22:56:22 -0700718
Dan Williams980d3ae2011-06-20 15:11:22 -0700719 spin_lock_irqsave(&ihost->scic_lock, flags);
720 list_splice_init(&idev->reqs_in_process, &list);
Dan Williams6f231dd2011-07-02 22:56:22 -0700721
Dan Williams980d3ae2011-06-20 15:11:22 -0700722 /* assumes that isci_terminate_request_core deletes from the list */
723 while (!list_empty(&list)) {
724 struct isci_request *ireq = list_entry(list.next, typeof(*ireq), dev_node);
Dan Williams6f231dd2011-07-02 22:56:22 -0700725
Dan Williams980d3ae2011-06-20 15:11:22 -0700726 /* Change state to "terminating" if it is currently
727 * "started".
728 */
729 old_state = isci_request_change_started_to_newstate(ireq,
730 &request_completion,
731 terminating);
732 switch (old_state) {
733 case started:
734 case completed:
735 case aborting:
736 break;
737 default:
738 /* termination in progress, or otherwise dispositioned.
739 * We know the request was on 'list' so should be safe
740 * to move it back to reqs_in_process
741 */
742 list_move(&ireq->dev_node, &idev->reqs_in_process);
743 ireq = NULL;
744 break;
745 }
746
747 if (!ireq)
748 continue;
749 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800750
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700751 init_completion(&request_completion);
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800752
Dan Williams980d3ae2011-06-20 15:11:22 -0700753 dev_dbg(&ihost->pdev->dev,
754 "%s: idev=%p request=%p; task=%p old_state=%d\n",
755 __func__, idev, ireq,
Jeff Skirvin3b34c162011-10-27 15:05:22 -0700756 (!test_bit(IREQ_TMF, &ireq->flags)
757 ? isci_request_access_task(ireq)
758 : NULL),
Dan Williams980d3ae2011-06-20 15:11:22 -0700759 old_state);
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700760
Dan Williams980d3ae2011-06-20 15:11:22 -0700761 /* If the old_state is started:
762 * This request was not already being aborted. If it had been,
763 * then the aborting I/O (ie. the TMF request) would not be in
764 * the aborting state, and thus would be terminated here. Note
765 * that since the TMF completion's call to the kernel function
766 * "complete()" does not happen until the pending I/O request
767 * terminate fully completes, we do not have to implement a
768 * special wait here for already aborting requests - the
769 * termination of the TMF request will force the request
770 * to finish it's already started terminate.
771 *
772 * If old_state == completed:
773 * This request completed from the SCU hardware perspective
774 * and now just needs cleaning up in terms of freeing the
775 * request and potentially calling up to libsas.
776 *
777 * If old_state == aborting:
778 * This request has already gone through a TMF timeout, but may
779 * not have been terminated; needs cleaning up at least.
780 */
781 isci_terminate_request_core(ihost, idev, ireq);
782 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700783 }
Dan Williams980d3ae2011-06-20 15:11:22 -0700784 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700785}
786
787/**
788 * isci_task_send_lu_reset_sas() - This function is called by of the SAS Domain
789 * Template functions.
790 * @lun: This parameter specifies the lun to be reset.
791 *
792 * status, zero indicates success.
793 */
794static int isci_task_send_lu_reset_sas(
795 struct isci_host *isci_host,
796 struct isci_remote_device *isci_device,
797 u8 *lun)
798{
799 struct isci_tmf tmf;
800 int ret = TMF_RESP_FUNC_FAILED;
801
802 dev_dbg(&isci_host->pdev->dev,
803 "%s: isci_host = %p, isci_device = %p\n",
804 __func__, isci_host, isci_device);
805 /* Send the LUN reset to the target. By the time the call returns,
806 * the TMF has fully exected in the target (in which case the return
807 * value is "TMF_RESP_FUNC_COMPLETE", or the request timed-out (or
808 * was otherwise unable to be executed ("TMF_RESP_FUNC_FAILED").
809 */
Dan Williams209fae12011-06-13 17:39:44 -0700810 isci_task_build_tmf(&tmf, isci_tmf_ssp_lun_reset, NULL, NULL);
Dan Williams6f231dd2011-07-02 22:56:22 -0700811
812 #define ISCI_LU_RESET_TIMEOUT_MS 2000 /* 2 second timeout. */
Dan Williams209fae12011-06-13 17:39:44 -0700813 ret = isci_task_execute_tmf(isci_host, isci_device, &tmf, ISCI_LU_RESET_TIMEOUT_MS);
Dan Williams6f231dd2011-07-02 22:56:22 -0700814
815 if (ret == TMF_RESP_FUNC_COMPLETE)
816 dev_dbg(&isci_host->pdev->dev,
817 "%s: %p: TMF_LU_RESET passed\n",
818 __func__, isci_device);
819 else
820 dev_dbg(&isci_host->pdev->dev,
821 "%s: %p: TMF_LU_RESET failed (%x)\n",
822 __func__, isci_device, ret);
823
824 return ret;
825}
826
Dan Williams43a5ab12011-12-08 23:20:44 -0800827int isci_task_lu_reset(struct domain_device *dev, u8 *lun)
Dan Williams16ba7702011-07-01 10:52:55 -0700828{
Dan Williams43a5ab12011-12-08 23:20:44 -0800829 struct isci_host *isci_host = dev_to_ihost(dev);
Dan Williams209fae12011-06-13 17:39:44 -0700830 struct isci_remote_device *isci_device;
831 unsigned long flags;
Dan Williams6f231dd2011-07-02 22:56:22 -0700832 int ret;
Dan Williams6f231dd2011-07-02 22:56:22 -0700833
Dan Williams209fae12011-06-13 17:39:44 -0700834 spin_lock_irqsave(&isci_host->scic_lock, flags);
Dan Williams43a5ab12011-12-08 23:20:44 -0800835 isci_device = isci_lookup_device(dev);
Dan Williams209fae12011-06-13 17:39:44 -0700836 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700837
Dan Williams4393aa42011-03-31 13:10:44 -0700838 dev_dbg(&isci_host->pdev->dev,
839 "%s: domain_device=%p, isci_host=%p; isci_device=%p\n",
Dan Williams43a5ab12011-12-08 23:20:44 -0800840 __func__, dev, isci_host, isci_device);
Dan Williams6f231dd2011-07-02 22:56:22 -0700841
Jeff Skirvin98145cb2011-10-27 15:05:16 -0700842 if (!isci_device) {
843 /* If the device is gone, stop the escalations. */
844 dev_dbg(&isci_host->pdev->dev, "%s: No dev\n", __func__);
Dan Williams6f231dd2011-07-02 22:56:22 -0700845
Jeff Skirvin98145cb2011-10-27 15:05:16 -0700846 ret = TMF_RESP_FUNC_COMPLETE;
Dan Williams209fae12011-06-13 17:39:44 -0700847 goto out;
Dan Williams6f231dd2011-07-02 22:56:22 -0700848 }
Jeff Skirvin98145cb2011-10-27 15:05:16 -0700849 set_bit(IDEV_EH, &isci_device->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700850
Dan Williams6f231dd2011-07-02 22:56:22 -0700851 /* Send the task management part of the reset. */
Dan Williams43a5ab12011-12-08 23:20:44 -0800852 if (dev_is_sata(dev)) {
853 sas_ata_schedule_reset(dev);
854 ret = TMF_RESP_FUNC_COMPLETE;
Dan Williams6f231dd2011-07-02 22:56:22 -0700855 } else
856 ret = isci_task_send_lu_reset_sas(isci_host, isci_device, lun);
857
858 /* If the LUN reset worked, all the I/O can now be terminated. */
859 if (ret == TMF_RESP_FUNC_COMPLETE)
860 /* Terminate all I/O now. */
861 isci_terminate_pending_requests(isci_host,
Dan Williams980d3ae2011-06-20 15:11:22 -0700862 isci_device);
Dan Williams6f231dd2011-07-02 22:56:22 -0700863
Dan Williams209fae12011-06-13 17:39:44 -0700864 out:
865 isci_put_device(isci_device);
Dan Williams6f231dd2011-07-02 22:56:22 -0700866 return ret;
867}
868
869
870/* int (*lldd_clear_nexus_port)(struct asd_sas_port *); */
871int isci_task_clear_nexus_port(struct asd_sas_port *port)
872{
873 return TMF_RESP_FUNC_FAILED;
874}
875
876
877
878int isci_task_clear_nexus_ha(struct sas_ha_struct *ha)
879{
880 return TMF_RESP_FUNC_FAILED;
881}
882
Dan Williams6f231dd2011-07-02 22:56:22 -0700883/* Task Management Functions. Must be called from process context. */
884
885/**
886 * isci_abort_task_process_cb() - This is a helper function for the abort task
887 * TMF command. It manages the request state with respect to the successful
888 * transmission / completion of the abort task request.
889 * @cb_state: This parameter specifies when this function was called - after
890 * the TMF request has been started and after it has timed-out.
891 * @tmf: This parameter specifies the TMF in progress.
892 *
893 *
894 */
895static void isci_abort_task_process_cb(
896 enum isci_tmf_cb_state cb_state,
897 struct isci_tmf *tmf,
898 void *cb_data)
899{
900 struct isci_request *old_request;
901
902 old_request = (struct isci_request *)cb_data;
903
904 dev_dbg(&old_request->isci_host->pdev->dev,
905 "%s: tmf=%p, old_request=%p\n",
906 __func__, tmf, old_request);
907
908 switch (cb_state) {
909
910 case isci_tmf_started:
911 /* The TMF has been started. Nothing to do here, since the
912 * request state was already set to "aborted" by the abort
913 * task function.
914 */
Bartosz Barcinski6cb4d6b2011-04-12 17:28:43 -0700915 if ((old_request->status != aborted)
916 && (old_request->status != completed))
Dan Williamsa8a0a132011-07-01 12:07:25 -0700917 dev_dbg(&old_request->isci_host->pdev->dev,
Bartosz Barcinski6cb4d6b2011-04-12 17:28:43 -0700918 "%s: Bad request status (%d): tmf=%p, old_request=%p\n",
919 __func__, old_request->status, tmf, old_request);
Dan Williams6f231dd2011-07-02 22:56:22 -0700920 break;
921
922 case isci_tmf_timed_out:
923
924 /* Set the task's state to "aborting", since the abort task
925 * function thread set it to "aborted" (above) in anticipation
926 * of the task management request working correctly. Since the
927 * timeout has now fired, the TMF request failed. We set the
928 * state such that the request completion will indicate the
929 * device is no longer present.
930 */
931 isci_request_change_state(old_request, aborting);
932 break;
933
934 default:
Dan Williamsa8a0a132011-07-01 12:07:25 -0700935 dev_dbg(&old_request->isci_host->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -0700936 "%s: Bad cb_state (%d): tmf=%p, old_request=%p\n",
937 __func__, cb_state, tmf, old_request);
938 break;
939 }
940}
941
942/**
943 * isci_task_abort_task() - This function is one of the SAS Domain Template
944 * functions. This function is called by libsas to abort a specified task.
945 * @task: This parameter specifies the SAS task to abort.
946 *
947 * status, zero indicates success.
948 */
949int isci_task_abort_task(struct sas_task *task)
950{
Dan Williams4393aa42011-03-31 13:10:44 -0700951 struct isci_host *isci_host = dev_to_ihost(task->dev);
Dan Williams6f231dd2011-07-02 22:56:22 -0700952 DECLARE_COMPLETION_ONSTACK(aborted_io_completion);
Jeff Skirvina5fde222011-03-04 14:06:42 -0800953 struct isci_request *old_request = NULL;
954 enum isci_request_status old_state;
Dan Williams6f231dd2011-07-02 22:56:22 -0700955 struct isci_remote_device *isci_device = NULL;
Jeff Skirvina5fde222011-03-04 14:06:42 -0800956 struct isci_tmf tmf;
957 int ret = TMF_RESP_FUNC_FAILED;
958 unsigned long flags;
Jeff Skirvin98145cb2011-10-27 15:05:16 -0700959 int perform_termination = 0;
Dan Williams6f231dd2011-07-02 22:56:22 -0700960
961 /* Get the isci_request reference from the task. Note that
962 * this check does not depend on the pending request list
963 * in the device, because tasks driving resets may land here
964 * after completion in the core.
965 */
Dan Williams209fae12011-06-13 17:39:44 -0700966 spin_lock_irqsave(&isci_host->scic_lock, flags);
967 spin_lock(&task->task_state_lock);
968
969 old_request = task->lldd_task;
970
971 /* If task is already done, the request isn't valid */
972 if (!(task->task_state_flags & SAS_TASK_STATE_DONE) &&
973 (task->task_state_flags & SAS_TASK_AT_INITIATOR) &&
974 old_request)
975 isci_device = isci_lookup_device(task->dev);
976
977 spin_unlock(&task->task_state_lock);
978 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700979
980 dev_dbg(&isci_host->pdev->dev,
Jeff Skirvin98145cb2011-10-27 15:05:16 -0700981 "%s: dev = %p, task = %p, old_request == %p\n",
982 __func__, isci_device, task, old_request);
Dan Williams6f231dd2011-07-02 22:56:22 -0700983
Jeff Skirvin98145cb2011-10-27 15:05:16 -0700984 if (isci_device)
985 set_bit(IDEV_EH, &isci_device->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700986
Jeff Skirvin98145cb2011-10-27 15:05:16 -0700987 /* Device reset conditions signalled in task_state_flags are the
988 * responsbility of libsas to observe at the start of the error
989 * handler thread.
Dan Williams6f231dd2011-07-02 22:56:22 -0700990 */
Jeff Skirvin98145cb2011-10-27 15:05:16 -0700991 if (!isci_device || !old_request) {
992 /* The request has already completed and there
993 * is nothing to do here other than to set the task
994 * done bit, and indicate that the task abort function
995 * was sucessful.
996 */
997 spin_lock_irqsave(&task->task_state_lock, flags);
998 task->task_state_flags |= SAS_TASK_STATE_DONE;
999 task->task_state_flags &= ~(SAS_TASK_AT_INITIATOR |
1000 SAS_TASK_STATE_PENDING);
Jeff Skirvina5fde222011-03-04 14:06:42 -08001001 spin_unlock_irqrestore(&task->task_state_lock, flags);
Jeff Skirvin98145cb2011-10-27 15:05:16 -07001002
1003 ret = TMF_RESP_FUNC_COMPLETE;
1004
1005 dev_dbg(&isci_host->pdev->dev,
1006 "%s: abort task not needed for %p\n",
1007 __func__, task);
1008 goto out;
James Bottomleya5ec7f862011-07-03 14:14:45 -05001009 }
Dan Williams6f231dd2011-07-02 22:56:22 -07001010
1011 spin_lock_irqsave(&isci_host->scic_lock, flags);
1012
Jeff Skirvinf219f012011-03-31 13:10:34 -07001013 /* Check the request status and change to "aborted" if currently
Jeff Skirvina5fde222011-03-04 14:06:42 -08001014 * "starting"; if true then set the I/O kernel completion
Dan Williams6f231dd2011-07-02 22:56:22 -07001015 * struct that will be triggered when the request completes.
1016 */
Jeff Skirvina5fde222011-03-04 14:06:42 -08001017 old_state = isci_task_validate_request_to_abort(
1018 old_request, isci_host, isci_device,
1019 &aborted_io_completion);
Jeff Skirvinf219f012011-03-31 13:10:34 -07001020 if ((old_state != started) &&
1021 (old_state != completed) &&
1022 (old_state != aborting)) {
Dan Williams6f231dd2011-07-02 22:56:22 -07001023
1024 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1025
Jeff Skirvina5fde222011-03-04 14:06:42 -08001026 /* The request was already being handled by someone else (because
1027 * they got to set the state away from started).
1028 */
1029 dev_dbg(&isci_host->pdev->dev,
1030 "%s: device = %p; old_request %p already being aborted\n",
1031 __func__,
1032 isci_device, old_request);
Dan Williams209fae12011-06-13 17:39:44 -07001033 ret = TMF_RESP_FUNC_COMPLETE;
1034 goto out;
Dan Williams6f231dd2011-07-02 22:56:22 -07001035 }
Dan Williams38d88792011-06-23 14:33:48 -07001036 if (task->task_proto == SAS_PROTOCOL_SMP ||
Jeff Skirvin98145cb2011-10-27 15:05:16 -07001037 sas_protocol_ata(task->task_proto) ||
Dan Williams38d88792011-06-23 14:33:48 -07001038 test_bit(IREQ_COMPLETE_IN_TARGET, &old_request->flags)) {
Dan Williams6f231dd2011-07-02 22:56:22 -07001039
1040 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1041
Jeff Skirvina5fde222011-03-04 14:06:42 -08001042 dev_dbg(&isci_host->pdev->dev,
Jeff Skirvin98145cb2011-10-27 15:05:16 -07001043 "%s: %s request"
Jeff Skirvina5fde222011-03-04 14:06:42 -08001044 " or complete_in_target (%d), thus no TMF\n",
Jeff Skirvin98145cb2011-10-27 15:05:16 -07001045 __func__,
1046 ((task->task_proto == SAS_PROTOCOL_SMP)
1047 ? "SMP"
1048 : (sas_protocol_ata(task->task_proto)
1049 ? "SATA/STP"
1050 : "<other>")
1051 ),
Dan Williams38d88792011-06-23 14:33:48 -07001052 test_bit(IREQ_COMPLETE_IN_TARGET, &old_request->flags));
Jeff Skirvina5fde222011-03-04 14:06:42 -08001053
Jeff Skirvin98145cb2011-10-27 15:05:16 -07001054 if (test_bit(IREQ_COMPLETE_IN_TARGET, &old_request->flags)) {
1055 spin_lock_irqsave(&task->task_state_lock, flags);
1056 task->task_state_flags |= SAS_TASK_STATE_DONE;
1057 task->task_state_flags &= ~(SAS_TASK_AT_INITIATOR |
1058 SAS_TASK_STATE_PENDING);
1059 spin_unlock_irqrestore(&task->task_state_lock, flags);
1060 ret = TMF_RESP_FUNC_COMPLETE;
1061 } else {
1062 spin_lock_irqsave(&task->task_state_lock, flags);
1063 task->task_state_flags &= ~(SAS_TASK_AT_INITIATOR |
1064 SAS_TASK_STATE_PENDING);
1065 spin_unlock_irqrestore(&task->task_state_lock, flags);
1066 }
Dan Williams6f231dd2011-07-02 22:56:22 -07001067
Jeff Skirvin98145cb2011-10-27 15:05:16 -07001068 /* STP and SMP devices are not sent a TMF, but the
1069 * outstanding I/O request is terminated below. This is
1070 * because SATA/STP and SMP discovery path timeouts directly
1071 * call the abort task interface for cleanup.
Dan Williams6f231dd2011-07-02 22:56:22 -07001072 */
Jeff Skirvin98145cb2011-10-27 15:05:16 -07001073 perform_termination = 1;
1074
Dan Williams6f231dd2011-07-02 22:56:22 -07001075 } else {
1076 /* Fill in the tmf stucture */
Dan Williams209fae12011-06-13 17:39:44 -07001077 isci_task_build_abort_task_tmf(&tmf, isci_tmf_ssp_task_abort,
Jeff Skirvinc3f42fe2011-03-04 14:06:56 -08001078 isci_abort_task_process_cb,
1079 old_request);
Dan Williams6f231dd2011-07-02 22:56:22 -07001080
Dan Williams6f231dd2011-07-02 22:56:22 -07001081 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1082
Jeff Skirvin98145cb2011-10-27 15:05:16 -07001083 #define ISCI_ABORT_TASK_TIMEOUT_MS 500 /* 1/2 second timeout */
Dan Williams209fae12011-06-13 17:39:44 -07001084 ret = isci_task_execute_tmf(isci_host, isci_device, &tmf,
Dan Williams6f231dd2011-07-02 22:56:22 -07001085 ISCI_ABORT_TASK_TIMEOUT_MS);
1086
Jeff Skirvin98145cb2011-10-27 15:05:16 -07001087 if (ret == TMF_RESP_FUNC_COMPLETE)
1088 perform_termination = 1;
1089 else
Dan Williamsa8a0a132011-07-01 12:07:25 -07001090 dev_dbg(&isci_host->pdev->dev,
Jeff Skirvin98145cb2011-10-27 15:05:16 -07001091 "%s: isci_task_send_tmf failed\n", __func__);
Dan Williams6f231dd2011-07-02 22:56:22 -07001092 }
Jeff Skirvin98145cb2011-10-27 15:05:16 -07001093 if (perform_termination) {
Dan Williams38d88792011-06-23 14:33:48 -07001094 set_bit(IREQ_COMPLETE_IN_TARGET, &old_request->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001095
Jeff Skirvin77c852f2011-06-20 14:09:16 -07001096 /* Clean up the request on our side, and wait for the aborted
1097 * I/O to complete.
1098 */
Jeff Skirvin98145cb2011-10-27 15:05:16 -07001099 isci_terminate_request_core(isci_host, isci_device,
1100 old_request);
Jeff Skirvina5fde222011-03-04 14:06:42 -08001101 }
1102
1103 /* Make sure we do not leave a reference to aborted_io_completion */
1104 old_request->io_request_completion = NULL;
Dan Williams209fae12011-06-13 17:39:44 -07001105 out:
1106 isci_put_device(isci_device);
Dan Williams6f231dd2011-07-02 22:56:22 -07001107 return ret;
1108}
1109
1110/**
1111 * isci_task_abort_task_set() - This function is one of the SAS Domain Template
1112 * functions. This is one of the Task Management functoins called by libsas,
1113 * to abort all task for the given lun.
1114 * @d_device: This parameter specifies the domain device associated with this
1115 * request.
1116 * @lun: This parameter specifies the lun associated with this request.
1117 *
1118 * status, zero indicates success.
1119 */
1120int isci_task_abort_task_set(
1121 struct domain_device *d_device,
1122 u8 *lun)
1123{
1124 return TMF_RESP_FUNC_FAILED;
1125}
1126
1127
1128/**
1129 * isci_task_clear_aca() - This function is one of the SAS Domain Template
1130 * functions. This is one of the Task Management functoins called by libsas.
1131 * @d_device: This parameter specifies the domain device associated with this
1132 * request.
1133 * @lun: This parameter specifies the lun associated with this request.
1134 *
1135 * status, zero indicates success.
1136 */
1137int isci_task_clear_aca(
1138 struct domain_device *d_device,
1139 u8 *lun)
1140{
1141 return TMF_RESP_FUNC_FAILED;
1142}
1143
1144
1145
1146/**
1147 * isci_task_clear_task_set() - This function is one of the SAS Domain Template
1148 * functions. This is one of the Task Management functoins called by libsas.
1149 * @d_device: This parameter specifies the domain device associated with this
1150 * request.
1151 * @lun: This parameter specifies the lun associated with this request.
1152 *
1153 * status, zero indicates success.
1154 */
1155int isci_task_clear_task_set(
1156 struct domain_device *d_device,
1157 u8 *lun)
1158{
1159 return TMF_RESP_FUNC_FAILED;
1160}
1161
1162
1163/**
1164 * isci_task_query_task() - This function is implemented to cause libsas to
1165 * correctly escalate the failed abort to a LUN or target reset (this is
1166 * because sas_scsi_find_task libsas function does not correctly interpret
1167 * all return codes from the abort task call). When TMF_RESP_FUNC_SUCC is
1168 * returned, libsas turns this into a LUN reset; when FUNC_FAILED is
1169 * returned, libsas will turn this into a target reset
1170 * @task: This parameter specifies the sas task being queried.
1171 * @lun: This parameter specifies the lun associated with this request.
1172 *
1173 * status, zero indicates success.
1174 */
1175int isci_task_query_task(
1176 struct sas_task *task)
1177{
1178 /* See if there is a pending device reset for this device. */
1179 if (task->task_state_flags & SAS_TASK_NEED_DEV_RESET)
1180 return TMF_RESP_FUNC_FAILED;
1181 else
1182 return TMF_RESP_FUNC_SUCC;
1183}
1184
Dave Jiangaf5ae892011-05-04 17:53:24 -07001185/*
Dan Williams6f231dd2011-07-02 22:56:22 -07001186 * isci_task_request_complete() - This function is called by the sci core when
1187 * an task request completes.
Dave Jiangaf5ae892011-05-04 17:53:24 -07001188 * @ihost: This parameter specifies the ISCI host object
1189 * @ireq: This parameter is the completed isci_request object.
Dan Williams6f231dd2011-07-02 22:56:22 -07001190 * @completion_status: This parameter specifies the completion status from the
1191 * sci core.
1192 *
1193 * none.
1194 */
Dave Jiangaf5ae892011-05-04 17:53:24 -07001195void
1196isci_task_request_complete(struct isci_host *ihost,
1197 struct isci_request *ireq,
1198 enum sci_task_status completion_status)
Dan Williams6f231dd2011-07-02 22:56:22 -07001199{
Dave Jiangaf5ae892011-05-04 17:53:24 -07001200 struct isci_tmf *tmf = isci_request_access_tmf(ireq);
Jeff Skirvinb343dff2011-10-27 15:05:06 -07001201 struct completion *tmf_complete = NULL;
1202 struct completion *request_complete = ireq->io_request_completion;
Dan Williams6f231dd2011-07-02 22:56:22 -07001203
Dave Jiangaf5ae892011-05-04 17:53:24 -07001204 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -07001205 "%s: request = %p, status=%d\n",
Dave Jiangaf5ae892011-05-04 17:53:24 -07001206 __func__, ireq, completion_status);
Dan Williams6f231dd2011-07-02 22:56:22 -07001207
Dave Jiang8d2c65c2011-06-01 09:03:08 +00001208 isci_request_change_state(ireq, completed);
Dan Williams6f231dd2011-07-02 22:56:22 -07001209
Dan Williams38d88792011-06-23 14:33:48 -07001210 set_bit(IREQ_COMPLETE_IN_TARGET, &ireq->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001211
Jeff Skirvinb343dff2011-10-27 15:05:06 -07001212 if (tmf) {
1213 tmf->status = completion_status;
1214
1215 if (tmf->proto == SAS_PROTOCOL_SSP) {
1216 memcpy(&tmf->resp.resp_iu,
1217 &ireq->ssp.rsp,
1218 SSP_RESP_IU_MAX_SIZE);
1219 } else if (tmf->proto == SAS_PROTOCOL_SATA) {
1220 memcpy(&tmf->resp.d2h_fis,
1221 &ireq->stp.rsp,
1222 sizeof(struct dev_to_host_fis));
1223 }
1224 /* PRINT_TMF( ((struct isci_tmf *)request->task)); */
1225 tmf_complete = tmf->complete;
Dan Williams6f231dd2011-07-02 22:56:22 -07001226 }
Dan Williams89a73012011-06-30 19:14:33 -07001227 sci_controller_complete_io(ihost, ireq->target_device, ireq);
Dan Williams67ea8382011-05-08 11:47:15 -07001228 /* set the 'terminated' flag handle to make sure it cannot be terminated
Dan Williams6f231dd2011-07-02 22:56:22 -07001229 * or completed again.
1230 */
Dan Williams38d88792011-06-23 14:33:48 -07001231 set_bit(IREQ_TERMINATED, &ireq->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001232
Jeff Skirvind6891682011-10-27 15:05:01 -07001233 /* As soon as something is in the terminate path, deallocation is
1234 * managed there. Note that the final non-managed state of a task
1235 * request is "completed".
1236 */
1237 if ((ireq->status == completed) ||
1238 !isci_request_is_dealloc_managed(ireq->status)) {
1239 isci_request_change_state(ireq, unallocated);
1240 isci_free_tag(ihost, ireq->io_tag);
1241 list_del_init(&ireq->dev_node);
1242 }
Dan Williams6f231dd2011-07-02 22:56:22 -07001243
Jeff Skirvinb343dff2011-10-27 15:05:06 -07001244 /* "request_complete" is set if the task was being terminated. */
1245 if (request_complete)
1246 complete(request_complete);
1247
Dan Williams6f231dd2011-07-02 22:56:22 -07001248 /* The task management part completes last. */
Jeff Skirvinb343dff2011-10-27 15:05:06 -07001249 if (tmf_complete)
1250 complete(tmf_complete);
Dan Williams6f231dd2011-07-02 22:56:22 -07001251}
1252
Dan Williams209fae12011-06-13 17:39:44 -07001253static int isci_reset_device(struct isci_host *ihost,
Dan Williams92776992011-11-30 11:57:34 -08001254 struct domain_device *dev,
Jeff Skirvinbc6f3872011-07-01 13:03:44 -07001255 struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001256{
Dan Williamsd06b4872011-05-02 13:59:25 -07001257 int rc;
Dan Williams92776992011-11-30 11:57:34 -08001258 unsigned long flags;
1259 enum sci_status status;
1260 struct sas_phy *phy = sas_get_local_phy(dev);
1261 struct isci_port *iport = dev->port->lldd_port;
Dan Williams6f231dd2011-07-02 22:56:22 -07001262
Dan Williamsd06b4872011-05-02 13:59:25 -07001263 dev_dbg(&ihost->pdev->dev, "%s: idev %p\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001264
Dan Williamsd06b4872011-05-02 13:59:25 -07001265 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williams89a73012011-06-30 19:14:33 -07001266 status = sci_remote_device_reset(idev);
Dan Williams92776992011-11-30 11:57:34 -08001267 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001268
Dan Williams92776992011-11-30 11:57:34 -08001269 if (status != SCI_SUCCESS) {
Dan Williamsa8a0a132011-07-01 12:07:25 -07001270 dev_dbg(&ihost->pdev->dev,
Dan Williams89a73012011-06-30 19:14:33 -07001271 "%s: sci_remote_device_reset(%p) returned %d!\n",
Dan Williamsd06b4872011-05-02 13:59:25 -07001272 __func__, idev, status);
Dan Williamsf41a0c42011-12-21 21:33:17 -08001273 rc = TMF_RESP_FUNC_FAILED;
1274 goto out;
Dan Williams6f231dd2011-07-02 22:56:22 -07001275 }
Dan Williams6f231dd2011-07-02 22:56:22 -07001276
Dan Williams92776992011-11-30 11:57:34 -08001277 if (scsi_is_sas_phy_local(phy)) {
1278 struct isci_phy *iphy = &ihost->phys[phy->number];
1279
1280 rc = isci_port_perform_hard_reset(ihost, iport, iphy);
1281 } else
1282 rc = sas_phy_reset(phy, !dev_is_sata(dev));
Dan Williams6f231dd2011-07-02 22:56:22 -07001283
1284 /* Terminate in-progress I/O now. */
Dan Williamsd06b4872011-05-02 13:59:25 -07001285 isci_remote_device_nuke_requests(ihost, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001286
Jeff Skirvinff717ab2011-06-20 14:08:51 -07001287 /* Since all pending TCs have been cleaned, resume the RNC. */
Dan Williamsd06b4872011-05-02 13:59:25 -07001288 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williams89a73012011-06-30 19:14:33 -07001289 status = sci_remote_device_reset_complete(idev);
Dan Williamsd06b4872011-05-02 13:59:25 -07001290 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001291
1292 if (status != SCI_SUCCESS) {
Dan Williamsa8a0a132011-07-01 12:07:25 -07001293 dev_dbg(&ihost->pdev->dev,
Dan Williams89a73012011-06-30 19:14:33 -07001294 "%s: sci_remote_device_reset_complete(%p) "
Dan Williamsd06b4872011-05-02 13:59:25 -07001295 "returned %d!\n", __func__, idev, status);
Dan Williams6f231dd2011-07-02 22:56:22 -07001296 }
Dan Williams6f231dd2011-07-02 22:56:22 -07001297
Dan Williamsd06b4872011-05-02 13:59:25 -07001298 dev_dbg(&ihost->pdev->dev, "%s: idev %p complete.\n", __func__, idev);
Dan Williamsf41a0c42011-12-21 21:33:17 -08001299 out:
1300 sas_put_local_phy(phy);
Dan Williamsd06b4872011-05-02 13:59:25 -07001301 return rc;
1302}
1303
1304int isci_task_I_T_nexus_reset(struct domain_device *dev)
1305{
1306 struct isci_host *ihost = dev_to_ihost(dev);
Dan Williamsd06b4872011-05-02 13:59:25 -07001307 struct isci_remote_device *idev;
1308 unsigned long flags;
Jeff Skirvinbc6f3872011-07-01 13:03:44 -07001309 int ret;
Dan Williamsd06b4872011-05-02 13:59:25 -07001310
Dan Williamsd06b4872011-05-02 13:59:25 -07001311 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williams209fae12011-06-13 17:39:44 -07001312 idev = isci_lookup_device(dev);
Dan Williamsd06b4872011-05-02 13:59:25 -07001313 spin_unlock_irqrestore(&ihost->scic_lock, flags);
1314
Dan Williams209fae12011-06-13 17:39:44 -07001315 if (!idev || !test_bit(IDEV_EH, &idev->flags)) {
1316 ret = TMF_RESP_FUNC_COMPLETE;
1317 goto out;
1318 }
Dan Williamsd06b4872011-05-02 13:59:25 -07001319
Dan Williams92776992011-11-30 11:57:34 -08001320 ret = isci_reset_device(ihost, dev, idev);
Dan Williams209fae12011-06-13 17:39:44 -07001321 out:
1322 isci_put_device(idev);
1323 return ret;
Dan Williamsd06b4872011-05-02 13:59:25 -07001324}
1325
1326int isci_bus_reset_handler(struct scsi_cmnd *cmd)
1327{
1328 struct domain_device *dev = sdev_to_domain_dev(cmd->device);
Dan Williams209fae12011-06-13 17:39:44 -07001329 struct isci_host *ihost = dev_to_ihost(dev);
1330 struct isci_remote_device *idev;
Dan Williams209fae12011-06-13 17:39:44 -07001331 unsigned long flags;
Jeff Skirvinbc6f3872011-07-01 13:03:44 -07001332 int ret;
Dan Williamsd06b4872011-05-02 13:59:25 -07001333
Dan Williams209fae12011-06-13 17:39:44 -07001334 spin_lock_irqsave(&ihost->scic_lock, flags);
1335 idev = isci_lookup_device(dev);
1336 spin_unlock_irqrestore(&ihost->scic_lock, flags);
1337
1338 if (!idev) {
1339 ret = TMF_RESP_FUNC_COMPLETE;
1340 goto out;
1341 }
1342
Dan Williams92776992011-11-30 11:57:34 -08001343 ret = isci_reset_device(ihost, dev, idev);
Dan Williams209fae12011-06-13 17:39:44 -07001344 out:
1345 isci_put_device(idev);
1346 return ret;
Dan Williams6f231dd2011-07-02 22:56:22 -07001347}