blob: 157e9978183ac9acfc11102a825645b53a61d4b0 [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"
64#include "sata.h"
65#include "task.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) {
Dan Williams50e7f9b2011-03-09 21:27:46 -080091 case isci_perform_normal_io_completion:
92 /* Normal notification (task_done) */
Dan Williams1077a572011-03-11 10:13:51 -080093 dev_dbg(&ihost->pdev->dev,
Jeff Skirvined8a72d2011-03-31 13:10:40 -070094 "%s: Normal - task = %p, response=%d, "
95 "status=%d\n",
Dan Williams50e7f9b2011-03-09 21:27:46 -080096 __func__, task, response, status);
97
Dan Williams1077a572011-03-11 10:13:51 -080098 task->lldd_task = NULL;
Dan Williams50e7f9b2011-03-09 21:27:46 -080099
Jeff Skirvined8a72d2011-03-31 13:10:40 -0700100 isci_execpath_callback(ihost, task, task->task_done);
Dan Williams50e7f9b2011-03-09 21:27:46 -0800101 break;
102
103 case isci_perform_aborted_io_completion:
104 /* No notification because this request is already in the
105 * abort path.
106 */
Dan Williams1077a572011-03-11 10:13:51 -0800107 dev_warn(&ihost->pdev->dev,
Jeff Skirvined8a72d2011-03-31 13:10:40 -0700108 "%s: Aborted - task = %p, response=%d, "
109 "status=%d\n",
Dan Williams50e7f9b2011-03-09 21:27:46 -0800110 __func__, task, response, status);
111 break;
112
113 case isci_perform_error_io_completion:
114 /* Use sas_task_abort */
Dan Williams1077a572011-03-11 10:13:51 -0800115 dev_warn(&ihost->pdev->dev,
Jeff Skirvined8a72d2011-03-31 13:10:40 -0700116 "%s: Error - task = %p, response=%d, "
117 "status=%d\n",
Dan Williams50e7f9b2011-03-09 21:27:46 -0800118 __func__, task, response, status);
Jeff Skirvined8a72d2011-03-31 13:10:40 -0700119
120 isci_execpath_callback(ihost, task, sas_task_abort);
Dan Williams50e7f9b2011-03-09 21:27:46 -0800121 break;
122
123 default:
Dan Williams1077a572011-03-11 10:13:51 -0800124 dev_warn(&ihost->pdev->dev,
Dan Williams50e7f9b2011-03-09 21:27:46 -0800125 "%s: isci task notification default case!",
126 __func__);
127 sas_task_abort(task);
128 break;
129 }
130}
Dan Williams6f231dd2011-07-02 22:56:22 -0700131
Dan Williams1077a572011-03-11 10:13:51 -0800132#define for_each_sas_task(num, task) \
133 for (; num > 0; num--,\
134 task = list_entry(task->list.next, struct sas_task, list))
135
Jeff Skirvin9274f452011-06-23 17:09:02 -0700136
137static inline int isci_device_io_ready(struct isci_remote_device *idev,
138 struct sas_task *task)
139{
140 return idev ? test_bit(IDEV_IO_READY, &idev->flags) ||
141 (test_bit(IDEV_IO_NCQERROR, &idev->flags) &&
142 isci_task_is_ncq_recovery(task))
143 : 0;
144}
Dan Williams6f231dd2011-07-02 22:56:22 -0700145/**
146 * isci_task_execute_task() - This function is one of the SAS Domain Template
147 * functions. This function is called by libsas to send a task down to
148 * hardware.
149 * @task: This parameter specifies the SAS task to send.
150 * @num: This parameter specifies the number of tasks to queue.
151 * @gfp_flags: This parameter specifies the context of this call.
152 *
153 * status, zero indicates success.
154 */
155int isci_task_execute_task(struct sas_task *task, int num, gfp_t gfp_flags)
156{
Dan Williams4393aa42011-03-31 13:10:44 -0700157 struct isci_host *ihost = dev_to_ihost(task->dev);
Dan Williams209fae12011-06-13 17:39:44 -0700158 struct isci_remote_device *idev;
Dan Williams6f231dd2011-07-02 22:56:22 -0700159 enum sci_status status;
Dan Williamsf2088262011-06-16 11:26:12 -0700160 unsigned long flags;
161 bool io_ready;
162 int ret;
Dan Williams6f231dd2011-07-02 22:56:22 -0700163
Dan Williams1077a572011-03-11 10:13:51 -0800164 dev_dbg(&ihost->pdev->dev, "%s: num=%d\n", __func__, num);
Dan Williams6f231dd2011-07-02 22:56:22 -0700165
166 /* Check if we have room for more tasks */
Dan Williams1077a572011-03-11 10:13:51 -0800167 ret = isci_host_can_queue(ihost, num);
Dan Williams6f231dd2011-07-02 22:56:22 -0700168
169 if (ret) {
Dan Williams1077a572011-03-11 10:13:51 -0800170 dev_warn(&ihost->pdev->dev, "%s: queue full\n", __func__);
Dan Williams6f231dd2011-07-02 22:56:22 -0700171 return ret;
172 }
173
Dan Williams1077a572011-03-11 10:13:51 -0800174 for_each_sas_task(num, task) {
Dan Williams209fae12011-06-13 17:39:44 -0700175 spin_lock_irqsave(&ihost->scic_lock, flags);
176 idev = isci_lookup_device(task->dev);
Jeff Skirvin9274f452011-06-23 17:09:02 -0700177 io_ready = isci_device_io_ready(idev, task);
Dan Williams209fae12011-06-13 17:39:44 -0700178 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700179
Dan Williamsf2088262011-06-16 11:26:12 -0700180 dev_dbg(&ihost->pdev->dev,
181 "task: %p, num: %d dev: %p idev: %p:%#lx cmd = %p\n",
182 task, num, task->dev, idev, idev ? idev->flags : 0,
183 task->uldd_task);
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700184
Dan Williamsf2088262011-06-16 11:26:12 -0700185 if (!idev) {
186 isci_task_refuse(ihost, task, SAS_TASK_UNDELIVERED,
187 SAS_DEVICE_UNKNOWN);
188 isci_host_can_dequeue(ihost, 1);
189 } else if (!io_ready) {
Jeff Skirvin9274f452011-06-23 17:09:02 -0700190
Dan Williamsf2088262011-06-16 11:26:12 -0700191 /* Indicate QUEUE_FULL so that the scsi midlayer
192 * retries.
193 */
194 isci_task_refuse(ihost, task, SAS_TASK_COMPLETE,
195 SAS_QUEUE_FULL);
Dan Williams1077a572011-03-11 10:13:51 -0800196 isci_host_can_dequeue(ihost, 1);
Dan Williams6f231dd2011-07-02 22:56:22 -0700197 } else {
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700198 /* There is a device and it's ready for I/O. */
199 spin_lock_irqsave(&task->task_state_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700200
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700201 if (task->task_state_flags & SAS_TASK_STATE_ABORTED) {
Dan Williamsf2088262011-06-16 11:26:12 -0700202 /* The I/O was aborted. */
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700203 spin_unlock_irqrestore(&task->task_state_lock,
204 flags);
205
Dan Williams1077a572011-03-11 10:13:51 -0800206 isci_task_refuse(ihost, task,
207 SAS_TASK_UNDELIVERED,
208 SAM_STAT_TASK_ABORTED);
Dan Williamsf2088262011-06-16 11:26:12 -0700209 isci_host_can_dequeue(ihost, 1);
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700210 } else {
Dan Williams6f231dd2011-07-02 22:56:22 -0700211 task->task_state_flags |= SAS_TASK_AT_INITIATOR;
212 spin_unlock_irqrestore(&task->task_state_lock, flags);
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700213
214 /* build and send the request. */
Dan Williams209fae12011-06-13 17:39:44 -0700215 status = isci_request_execute(ihost, idev, task, gfp_flags);
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700216
217 if (status != SCI_SUCCESS) {
218
219 spin_lock_irqsave(&task->task_state_lock, flags);
220 /* Did not really start this command. */
221 task->task_state_flags &= ~SAS_TASK_AT_INITIATOR;
222 spin_unlock_irqrestore(&task->task_state_lock, flags);
223
224 /* Indicate QUEUE_FULL so that the scsi
225 * midlayer retries. if the request
226 * failed for remote device reasons,
227 * it gets returned as
228 * SAS_TASK_UNDELIVERED next time
229 * through.
230 */
Dan Williams1077a572011-03-11 10:13:51 -0800231 isci_task_refuse(ihost, task,
232 SAS_TASK_COMPLETE,
233 SAS_QUEUE_FULL);
234 isci_host_can_dequeue(ihost, 1);
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700235 }
Dan Williams6f231dd2011-07-02 22:56:22 -0700236 }
237 }
Dan Williams209fae12011-06-13 17:39:44 -0700238 isci_put_device(idev);
Dan Williams1077a572011-03-11 10:13:51 -0800239 }
Dan Williams6f231dd2011-07-02 22:56:22 -0700240 return 0;
241}
242
Dan Williams0d0cf142011-06-13 00:51:30 -0700243static struct isci_request *isci_task_request_build(struct isci_host *ihost,
Dan Williams209fae12011-06-13 17:39:44 -0700244 struct isci_remote_device *idev,
Dan Williams0d0cf142011-06-13 00:51:30 -0700245 struct isci_tmf *isci_tmf)
Dan Williams6f231dd2011-07-02 22:56:22 -0700246{
Dan Williams6f231dd2011-07-02 22:56:22 -0700247 enum sci_status status = SCI_FAILURE;
Dan Williams0d0cf142011-06-13 00:51:30 -0700248 struct isci_request *ireq = NULL;
Dan Williamsa1a113b2011-04-21 18:44:45 -0700249 struct domain_device *dev;
Dan Williams6f231dd2011-07-02 22:56:22 -0700250
Dan Williams0d0cf142011-06-13 00:51:30 -0700251 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -0700252 "%s: isci_tmf = %p\n", __func__, isci_tmf);
253
Dan Williams0d0cf142011-06-13 00:51:30 -0700254 dev = idev->domain_dev;
Dan Williams6f231dd2011-07-02 22:56:22 -0700255
256 /* do common allocation and init of request object. */
Dan Williams209fae12011-06-13 17:39:44 -0700257 ireq = isci_request_alloc_tmf(ihost, isci_tmf, GFP_ATOMIC);
Dan Williams0d0cf142011-06-13 00:51:30 -0700258 if (!ireq)
259 return NULL;
Dan Williams6f231dd2011-07-02 22:56:22 -0700260
261 /* let the core do it's construct. */
Dan Williams209fae12011-06-13 17:39:44 -0700262 status = scic_task_request_construct(&ihost->sci, &idev->sci,
Dan Williams0d843662011-05-08 01:56:57 -0700263 SCI_CONTROLLER_INVALID_IO_TAG,
Dan Williams0d0cf142011-06-13 00:51:30 -0700264 &ireq->sci);
Dan Williams6f231dd2011-07-02 22:56:22 -0700265
266 if (status != SCI_SUCCESS) {
Dan Williams0d0cf142011-06-13 00:51:30 -0700267 dev_warn(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -0700268 "%s: scic_task_request_construct failed - "
269 "status = 0x%x\n",
270 __func__,
271 status);
272 goto errout;
273 }
274
Dan Williamsa1a113b2011-04-21 18:44:45 -0700275 /* XXX convert to get this from task->tproto like other drivers */
276 if (dev->dev_type == SAS_END_DEV) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700277 isci_tmf->proto = SAS_PROTOCOL_SSP;
Dan Williams0d0cf142011-06-13 00:51:30 -0700278 status = scic_task_request_construct_ssp(&ireq->sci);
Dan Williams6f231dd2011-07-02 22:56:22 -0700279 if (status != SCI_SUCCESS)
280 goto errout;
281 }
282
Dan Williamsa1a113b2011-04-21 18:44:45 -0700283 if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP)) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700284 isci_tmf->proto = SAS_PROTOCOL_SATA;
Dan Williams0d0cf142011-06-13 00:51:30 -0700285 status = isci_sata_management_task_request_build(ireq);
Dan Williams6f231dd2011-07-02 22:56:22 -0700286
287 if (status != SCI_SUCCESS)
288 goto errout;
289 }
Dan Williams0d0cf142011-06-13 00:51:30 -0700290 return ireq;
Dan Williams6f231dd2011-07-02 22:56:22 -0700291 errout:
Dan Williams0d0cf142011-06-13 00:51:30 -0700292 isci_request_free(ihost, ireq);
293 ireq = NULL;
294 return ireq;
Dan Williams6f231dd2011-07-02 22:56:22 -0700295}
296
Dan Williams209fae12011-06-13 17:39:44 -0700297int isci_task_execute_tmf(struct isci_host *ihost,
298 struct isci_remote_device *isci_device,
299 struct isci_tmf *tmf, unsigned long timeout_ms)
Dan Williams6f231dd2011-07-02 22:56:22 -0700300{
301 DECLARE_COMPLETION_ONSTACK(completion);
Bartosz Barcinski467e8552011-04-12 17:28:41 -0700302 enum sci_task_status status = SCI_TASK_FAILURE;
Dan Williams6f231dd2011-07-02 22:56:22 -0700303 struct scic_sds_remote_device *sci_device;
Dan Williams0d0cf142011-06-13 00:51:30 -0700304 struct isci_request *ireq;
Dan Williams6f231dd2011-07-02 22:56:22 -0700305 int ret = TMF_RESP_FUNC_FAILED;
306 unsigned long flags;
Edmund Nadolskifd183882011-05-19 12:00:15 +0000307 unsigned long timeleft;
Dan Williams6f231dd2011-07-02 22:56:22 -0700308
309 /* sanity check, return TMF_RESP_FUNC_FAILED
310 * if the device is not there and ready.
311 */
Jeff Skirvin9274f452011-06-23 17:09:02 -0700312 if (!isci_device ||
313 (!test_bit(IDEV_IO_READY, &isci_device->flags) &&
314 !test_bit(IDEV_IO_NCQERROR, &isci_device->flags))) {
Dan Williams0d0cf142011-06-13 00:51:30 -0700315 dev_dbg(&ihost->pdev->dev,
Dan Williamsf2088262011-06-16 11:26:12 -0700316 "%s: isci_device = %p not ready (%#lx)\n",
Dan Williams6f231dd2011-07-02 22:56:22 -0700317 __func__,
Dan Williamsf2088262011-06-16 11:26:12 -0700318 isci_device, isci_device ? isci_device->flags : 0);
Dan Williams6f231dd2011-07-02 22:56:22 -0700319 return TMF_RESP_FUNC_FAILED;
320 } else
Dan Williams0d0cf142011-06-13 00:51:30 -0700321 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -0700322 "%s: isci_device = %p\n",
323 __func__, isci_device);
324
Dan Williams57f20f42011-04-21 18:14:45 -0700325 sci_device = &isci_device->sci;
Dan Williams6f231dd2011-07-02 22:56:22 -0700326
327 /* Assign the pointer to the TMF's completion kernel wait structure. */
328 tmf->complete = &completion;
329
Dan Williams209fae12011-06-13 17:39:44 -0700330 ireq = isci_task_request_build(ihost, isci_device, tmf);
Dan Williams0d0cf142011-06-13 00:51:30 -0700331 if (!ireq) {
332 dev_warn(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -0700333 "%s: isci_task_request_build failed\n",
334 __func__);
335 return TMF_RESP_FUNC_FAILED;
336 }
337
Dan Williams0d0cf142011-06-13 00:51:30 -0700338 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700339
340 /* start the TMF io. */
341 status = scic_controller_start_task(
Dan Williams0d0cf142011-06-13 00:51:30 -0700342 &ihost->sci,
Dan Williams6f231dd2011-07-02 22:56:22 -0700343 sci_device,
Dan Williams0d0cf142011-06-13 00:51:30 -0700344 &ireq->sci,
Dan Williams67ea8382011-05-08 11:47:15 -0700345 SCI_CONTROLLER_INVALID_IO_TAG);
Dan Williams6f231dd2011-07-02 22:56:22 -0700346
Bartosz Barcinski467e8552011-04-12 17:28:41 -0700347 if (status != SCI_TASK_SUCCESS) {
Dan Williams0d0cf142011-06-13 00:51:30 -0700348 dev_warn(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -0700349 "%s: start_io failed - status = 0x%x, request = %p\n",
350 __func__,
351 status,
Dan Williams0d0cf142011-06-13 00:51:30 -0700352 ireq);
353 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams086a0da2011-06-21 16:23:03 -0700354 isci_request_free(ihost, ireq);
355 return ret;
Dan Williams6f231dd2011-07-02 22:56:22 -0700356 }
357
Dan Williams6f231dd2011-07-02 22:56:22 -0700358 if (tmf->cb_state_func != NULL)
359 tmf->cb_state_func(isci_tmf_started, tmf, tmf->cb_data);
360
Dan Williams0d0cf142011-06-13 00:51:30 -0700361 isci_request_change_state(ireq, started);
Dan Williams6f231dd2011-07-02 22:56:22 -0700362
363 /* add the request to the remote device request list. */
Dan Williams0d0cf142011-06-13 00:51:30 -0700364 list_add(&ireq->dev_node, &isci_device->reqs_in_process);
Dan Williams6f231dd2011-07-02 22:56:22 -0700365
Dan Williams0d0cf142011-06-13 00:51:30 -0700366 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700367
368 /* Wait for the TMF to complete, or a timeout. */
Edmund Nadolskifd183882011-05-19 12:00:15 +0000369 timeleft = wait_for_completion_timeout(&completion,
Dan Williams086a0da2011-06-21 16:23:03 -0700370 msecs_to_jiffies(timeout_ms));
Edmund Nadolskifd183882011-05-19 12:00:15 +0000371
372 if (timeleft == 0) {
Dan Williams0d0cf142011-06-13 00:51:30 -0700373 spin_lock_irqsave(&ihost->scic_lock, flags);
Edmund Nadolskifd183882011-05-19 12:00:15 +0000374
375 if (tmf->cb_state_func != NULL)
376 tmf->cb_state_func(isci_tmf_timed_out, tmf, tmf->cb_data);
377
Dan Williams086a0da2011-06-21 16:23:03 -0700378 scic_controller_terminate_request(&ihost->sci,
379 &isci_device->sci,
380 &ireq->sci);
Edmund Nadolskifd183882011-05-19 12:00:15 +0000381
Dan Williams0d0cf142011-06-13 00:51:30 -0700382 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams086a0da2011-06-21 16:23:03 -0700383
384 wait_for_completion(tmf->complete);
Edmund Nadolskifd183882011-05-19 12:00:15 +0000385 }
Dan Williams6f231dd2011-07-02 22:56:22 -0700386
387 isci_print_tmf(tmf);
388
389 if (tmf->status == SCI_SUCCESS)
390 ret = TMF_RESP_FUNC_COMPLETE;
391 else if (tmf->status == SCI_FAILURE_IO_RESPONSE_VALID) {
Dan Williams0d0cf142011-06-13 00:51:30 -0700392 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -0700393 "%s: tmf.status == "
394 "SCI_FAILURE_IO_RESPONSE_VALID\n",
395 __func__);
396 ret = TMF_RESP_FUNC_COMPLETE;
397 }
398 /* Else - leave the default "failed" status alone. */
399
Dan Williams0d0cf142011-06-13 00:51:30 -0700400 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -0700401 "%s: completed request = %p\n",
402 __func__,
Dan Williams0d0cf142011-06-13 00:51:30 -0700403 ireq);
Dan Williams6f231dd2011-07-02 22:56:22 -0700404
Dan Williams6f231dd2011-07-02 22:56:22 -0700405 return ret;
406}
407
408void isci_task_build_tmf(
409 struct isci_tmf *tmf,
Dan Williams6f231dd2011-07-02 22:56:22 -0700410 enum isci_tmf_function_codes code,
411 void (*tmf_sent_cb)(enum isci_tmf_cb_state,
412 struct isci_tmf *,
413 void *),
Jeff Skirvinc3f42fe2011-03-04 14:06:56 -0800414 void *cb_data)
Dan Williams6f231dd2011-07-02 22:56:22 -0700415{
Dan Williams6f231dd2011-07-02 22:56:22 -0700416 memset(tmf, 0, sizeof(*tmf));
417
Dan Williams6f231dd2011-07-02 22:56:22 -0700418 tmf->tmf_code = code;
Dan Williams6f231dd2011-07-02 22:56:22 -0700419 tmf->cb_state_func = tmf_sent_cb;
Jeff Skirvinc3f42fe2011-03-04 14:06:56 -0800420 tmf->cb_data = cb_data;
421}
Jeff Skirvin1fad9e92011-03-04 14:06:46 -0800422
Dan Williams35173d52011-03-26 16:43:01 -0700423static void isci_task_build_abort_task_tmf(
Jeff Skirvinc3f42fe2011-03-04 14:06:56 -0800424 struct isci_tmf *tmf,
Jeff Skirvinc3f42fe2011-03-04 14:06:56 -0800425 enum isci_tmf_function_codes code,
426 void (*tmf_sent_cb)(enum isci_tmf_cb_state,
427 struct isci_tmf *,
428 void *),
429 struct isci_request *old_request)
430{
Dan Williams209fae12011-06-13 17:39:44 -0700431 isci_task_build_tmf(tmf, code, tmf_sent_cb,
Jeff Skirvinc3f42fe2011-03-04 14:06:56 -0800432 (void *)old_request);
433 tmf->io_tag = old_request->io_tag;
Dan Williams6f231dd2011-07-02 22:56:22 -0700434}
435
Dan Williams6f231dd2011-07-02 22:56:22 -0700436/**
437 * isci_task_validate_request_to_abort() - This function checks the given I/O
438 * against the "started" state. If the request is still "started", it's
439 * state is changed to aborted. NOTE: isci_host->scic_lock MUST BE HELD
440 * BEFORE CALLING THIS FUNCTION.
441 * @isci_request: This parameter specifies the request object to control.
442 * @isci_host: This parameter specifies the ISCI host object
443 * @isci_device: This is the device to which the request is pending.
444 * @aborted_io_completion: This is a completion structure that will be added to
445 * the request in case it is changed to aborting; this completion is
446 * triggered when the request is fully completed.
447 *
448 * Either "started" on successful change of the task status to "aborted", or
449 * "unallocated" if the task cannot be controlled.
450 */
451static enum isci_request_status isci_task_validate_request_to_abort(
452 struct isci_request *isci_request,
453 struct isci_host *isci_host,
454 struct isci_remote_device *isci_device,
455 struct completion *aborted_io_completion)
456{
457 enum isci_request_status old_state = unallocated;
458
459 /* Only abort the task if it's in the
460 * device's request_in_process list
461 */
462 if (isci_request && !list_empty(&isci_request->dev_node)) {
463 old_state = isci_request_change_started_to_aborted(
464 isci_request, aborted_io_completion);
465
Dan Williams6f231dd2011-07-02 22:56:22 -0700466 }
467
468 return old_state;
469}
470
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700471/**
472* isci_request_cleanup_completed_loiterer() - This function will take care of
473* the final cleanup on any request which has been explicitly terminated.
474* @isci_host: This parameter specifies the ISCI host object
475* @isci_device: This is the device to which the request is pending.
476* @isci_request: This parameter specifies the terminated request object.
477* @task: This parameter is the libsas I/O request.
478*/
Dan Williams6f231dd2011-07-02 22:56:22 -0700479static void isci_request_cleanup_completed_loiterer(
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700480 struct isci_host *isci_host,
Dan Williams6f231dd2011-07-02 22:56:22 -0700481 struct isci_remote_device *isci_device,
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700482 struct isci_request *isci_request,
483 struct sas_task *task)
Dan Williams6f231dd2011-07-02 22:56:22 -0700484{
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700485 unsigned long flags;
Dan Williams6f231dd2011-07-02 22:56:22 -0700486
487 dev_dbg(&isci_host->pdev->dev,
488 "%s: isci_device=%p, request=%p, task=%p\n",
Jeff Skirvin18d3d722011-03-04 14:06:38 -0800489 __func__, isci_device, isci_request, task);
Dan Williams6f231dd2011-07-02 22:56:22 -0700490
Jeff Skirvina5fde222011-03-04 14:06:42 -0800491 if (task != NULL) {
492
493 spin_lock_irqsave(&task->task_state_lock, flags);
494 task->lldd_task = NULL;
495
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700496 task->task_state_flags &= ~SAS_TASK_NEED_DEV_RESET;
497
Jeff Skirvina5fde222011-03-04 14:06:42 -0800498 isci_set_task_doneflags(task);
499
500 /* If this task is not in the abort path, call task_done. */
501 if (!(task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
502
503 spin_unlock_irqrestore(&task->task_state_lock, flags);
504 task->task_done(task);
505 } else
506 spin_unlock_irqrestore(&task->task_state_lock, flags);
507 }
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800508
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700509 if (isci_request != NULL) {
510 spin_lock_irqsave(&isci_host->scic_lock, flags);
511 list_del_init(&isci_request->dev_node);
512 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800513
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700514 isci_request_free(isci_host, isci_request);
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800515 }
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800516}
517
Dan Williams6f231dd2011-07-02 22:56:22 -0700518/**
519 * isci_terminate_request_core() - This function will terminate the given
520 * request, and wait for it to complete. This function must only be called
521 * from a thread that can wait. Note that the request is terminated and
522 * completed (back to the host, if started there).
523 * @isci_host: This SCU.
524 * @isci_device: The target.
525 * @isci_request: The I/O request to be terminated.
526 *
Dan Williams6f231dd2011-07-02 22:56:22 -0700527 */
528static void isci_terminate_request_core(
529 struct isci_host *isci_host,
530 struct isci_remote_device *isci_device,
Jeff Skirvincbb65c62011-03-04 14:06:50 -0800531 struct isci_request *isci_request)
Dan Williams6f231dd2011-07-02 22:56:22 -0700532{
Jeff Skirvincbb65c62011-03-04 14:06:50 -0800533 enum sci_status status = SCI_SUCCESS;
Dan Williams6f231dd2011-07-02 22:56:22 -0700534 bool was_terminated = false;
535 bool needs_cleanup_handling = false;
536 enum isci_request_status request_status;
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700537 unsigned long flags;
538 unsigned long termination_completed = 1;
539 struct completion *io_request_completion;
540 struct sas_task *task;
Dan Williams6f231dd2011-07-02 22:56:22 -0700541
542 dev_dbg(&isci_host->pdev->dev,
543 "%s: device = %p; request = %p\n",
544 __func__, isci_device, isci_request);
545
Dan Williams6f231dd2011-07-02 22:56:22 -0700546 spin_lock_irqsave(&isci_host->scic_lock, flags);
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800547
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700548 io_request_completion = isci_request->io_request_completion;
549
550 task = (isci_request->ttype == io_task)
551 ? isci_request_access_task(isci_request)
552 : NULL;
553
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800554 /* Note that we are not going to control
555 * the target to abort the request.
556 */
557 isci_request->complete_in_target = true;
558
Dan Williams6f231dd2011-07-02 22:56:22 -0700559 /* Make sure the request wasn't just sitting around signalling
560 * device condition (if the request handle is NULL, then the
561 * request completed but needed additional handling here).
562 */
Dan Williams67ea8382011-05-08 11:47:15 -0700563 if (!isci_request->terminated) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700564 was_terminated = true;
Jeff Skirvincbb65c62011-03-04 14:06:50 -0800565 needs_cleanup_handling = true;
Dan Williams6f231dd2011-07-02 22:56:22 -0700566 status = scic_controller_terminate_request(
Artur Wojcikcc3dbd02011-05-04 07:58:16 +0000567 &isci_host->sci,
Dan Williams57f20f42011-04-21 18:14:45 -0700568 &isci_device->sci,
Dan Williams67ea8382011-05-08 11:47:15 -0700569 &isci_request->sci);
Dan Williams6f231dd2011-07-02 22:56:22 -0700570 }
571 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
572
573 /*
574 * The only time the request to terminate will
575 * fail is when the io request is completed and
576 * being aborted.
577 */
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800578 if (status != SCI_SUCCESS) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700579 dev_err(&isci_host->pdev->dev,
580 "%s: scic_controller_terminate_request"
581 " returned = 0x%x\n",
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700582 __func__, status);
583
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800584 isci_request->io_request_completion = NULL;
585
586 } else {
Dan Williams6f231dd2011-07-02 22:56:22 -0700587 if (was_terminated) {
588 dev_dbg(&isci_host->pdev->dev,
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700589 "%s: before completion wait (%p/%p)\n",
590 __func__, isci_request, io_request_completion);
Dan Williams6f231dd2011-07-02 22:56:22 -0700591
592 /* Wait here for the request to complete. */
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700593 #define TERMINATION_TIMEOUT_MSEC 500
594 termination_completed
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800595 = wait_for_completion_timeout(
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700596 io_request_completion,
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800597 msecs_to_jiffies(TERMINATION_TIMEOUT_MSEC));
Dan Williams6f231dd2011-07-02 22:56:22 -0700598
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700599 if (!termination_completed) {
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800600
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700601 /* The request to terminate has timed out. */
602 spin_lock_irqsave(&isci_host->scic_lock,
603 flags);
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800604
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700605 /* Check for state changes. */
606 if (!isci_request->terminated) {
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800607
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700608 /* The best we can do is to have the
609 * request die a silent death if it
610 * ever really completes.
611 *
612 * Set the request state to "dead",
613 * and clear the task pointer so that
614 * an actual completion event callback
615 * doesn't do anything.
616 */
617 isci_request->status = dead;
618 isci_request->io_request_completion
619 = NULL;
620
621 if (isci_request->ttype == io_task) {
622
623 /* Break links with the
624 * sas_task.
625 */
626 isci_request->ttype_ptr.io_task_ptr
627 = NULL;
628 }
629 } else
630 termination_completed = 1;
631
632 spin_unlock_irqrestore(&isci_host->scic_lock,
633 flags);
634
635 if (!termination_completed) {
636
637 dev_err(&isci_host->pdev->dev,
638 "%s: *** Timeout waiting for "
639 "termination(%p/%p)\n",
640 __func__, io_request_completion,
641 isci_request);
642
643 /* The request can no longer be referenced
644 * safely since it may go away if the
645 * termination every really does complete.
646 */
647 isci_request = NULL;
648 }
649 }
650 if (termination_completed)
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800651 dev_dbg(&isci_host->pdev->dev,
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700652 "%s: after completion wait (%p/%p)\n",
653 __func__, isci_request, io_request_completion);
Dan Williams6f231dd2011-07-02 22:56:22 -0700654 }
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800655
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700656 if (termination_completed) {
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800657
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700658 isci_request->io_request_completion = NULL;
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800659
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700660 /* Peek at the status of the request. This will tell
661 * us if there was special handling on the request such that it
662 * needs to be detached and freed here.
663 */
664 spin_lock_irqsave(&isci_request->state_lock, flags);
665 request_status = isci_request_get_state(isci_request);
666
667 if ((isci_request->ttype == io_task) /* TMFs are in their own thread */
668 && ((request_status == aborted)
669 || (request_status == aborting)
670 || (request_status == terminating)
671 || (request_status == completed)
672 || (request_status == dead)
673 )
674 ) {
675
676 /* The completion routine won't free a request in
677 * the aborted/aborting/etc. states, so we do
678 * it here.
679 */
680 needs_cleanup_handling = true;
681 }
682 spin_unlock_irqrestore(&isci_request->state_lock, flags);
683
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800684 }
Dan Williams6f231dd2011-07-02 22:56:22 -0700685 if (needs_cleanup_handling)
686 isci_request_cleanup_completed_loiterer(
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700687 isci_host, isci_device, isci_request, task);
Jeff Skirvina5fde222011-03-04 14:06:42 -0800688 }
Dan Williams6f231dd2011-07-02 22:56:22 -0700689}
690
691/**
692 * isci_terminate_pending_requests() - This function will change the all of the
693 * requests on the given device's state to "aborting", will terminate the
694 * requests, and wait for them to complete. This function must only be
695 * called from a thread that can wait. Note that the requests are all
696 * terminated and completed (back to the host, if started there).
697 * @isci_host: This parameter specifies SCU.
698 * @isci_device: This parameter specifies the target.
699 *
Dan Williams6f231dd2011-07-02 22:56:22 -0700700 */
Dan Williams980d3ae2011-06-20 15:11:22 -0700701void isci_terminate_pending_requests(struct isci_host *ihost,
702 struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -0700703{
Dan Williams980d3ae2011-06-20 15:11:22 -0700704 struct completion request_completion;
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700705 enum isci_request_status old_state;
Dan Williams980d3ae2011-06-20 15:11:22 -0700706 unsigned long flags;
707 LIST_HEAD(list);
Dan Williams6f231dd2011-07-02 22:56:22 -0700708
Dan Williams980d3ae2011-06-20 15:11:22 -0700709 spin_lock_irqsave(&ihost->scic_lock, flags);
710 list_splice_init(&idev->reqs_in_process, &list);
Dan Williams6f231dd2011-07-02 22:56:22 -0700711
Dan Williams980d3ae2011-06-20 15:11:22 -0700712 /* assumes that isci_terminate_request_core deletes from the list */
713 while (!list_empty(&list)) {
714 struct isci_request *ireq = list_entry(list.next, typeof(*ireq), dev_node);
Dan Williams6f231dd2011-07-02 22:56:22 -0700715
Dan Williams980d3ae2011-06-20 15:11:22 -0700716 /* Change state to "terminating" if it is currently
717 * "started".
718 */
719 old_state = isci_request_change_started_to_newstate(ireq,
720 &request_completion,
721 terminating);
722 switch (old_state) {
723 case started:
724 case completed:
725 case aborting:
726 break;
727 default:
728 /* termination in progress, or otherwise dispositioned.
729 * We know the request was on 'list' so should be safe
730 * to move it back to reqs_in_process
731 */
732 list_move(&ireq->dev_node, &idev->reqs_in_process);
733 ireq = NULL;
734 break;
735 }
736
737 if (!ireq)
738 continue;
739 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800740
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700741 init_completion(&request_completion);
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800742
Dan Williams980d3ae2011-06-20 15:11:22 -0700743 dev_dbg(&ihost->pdev->dev,
744 "%s: idev=%p request=%p; task=%p old_state=%d\n",
745 __func__, idev, ireq,
746 ireq->ttype == io_task ? isci_request_access_task(ireq) : NULL,
747 old_state);
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700748
Dan Williams980d3ae2011-06-20 15:11:22 -0700749 /* If the old_state is started:
750 * This request was not already being aborted. If it had been,
751 * then the aborting I/O (ie. the TMF request) would not be in
752 * the aborting state, and thus would be terminated here. Note
753 * that since the TMF completion's call to the kernel function
754 * "complete()" does not happen until the pending I/O request
755 * terminate fully completes, we do not have to implement a
756 * special wait here for already aborting requests - the
757 * termination of the TMF request will force the request
758 * to finish it's already started terminate.
759 *
760 * If old_state == completed:
761 * This request completed from the SCU hardware perspective
762 * and now just needs cleaning up in terms of freeing the
763 * request and potentially calling up to libsas.
764 *
765 * If old_state == aborting:
766 * This request has already gone through a TMF timeout, but may
767 * not have been terminated; needs cleaning up at least.
768 */
769 isci_terminate_request_core(ihost, idev, ireq);
770 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700771 }
Dan Williams980d3ae2011-06-20 15:11:22 -0700772 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700773}
774
775/**
776 * isci_task_send_lu_reset_sas() - This function is called by of the SAS Domain
777 * Template functions.
778 * @lun: This parameter specifies the lun to be reset.
779 *
780 * status, zero indicates success.
781 */
782static int isci_task_send_lu_reset_sas(
783 struct isci_host *isci_host,
784 struct isci_remote_device *isci_device,
785 u8 *lun)
786{
787 struct isci_tmf tmf;
788 int ret = TMF_RESP_FUNC_FAILED;
789
790 dev_dbg(&isci_host->pdev->dev,
791 "%s: isci_host = %p, isci_device = %p\n",
792 __func__, isci_host, isci_device);
793 /* Send the LUN reset to the target. By the time the call returns,
794 * the TMF has fully exected in the target (in which case the return
795 * value is "TMF_RESP_FUNC_COMPLETE", or the request timed-out (or
796 * was otherwise unable to be executed ("TMF_RESP_FUNC_FAILED").
797 */
Dan Williams209fae12011-06-13 17:39:44 -0700798 isci_task_build_tmf(&tmf, isci_tmf_ssp_lun_reset, NULL, NULL);
Dan Williams6f231dd2011-07-02 22:56:22 -0700799
800 #define ISCI_LU_RESET_TIMEOUT_MS 2000 /* 2 second timeout. */
Dan Williams209fae12011-06-13 17:39:44 -0700801 ret = isci_task_execute_tmf(isci_host, isci_device, &tmf, ISCI_LU_RESET_TIMEOUT_MS);
Dan Williams6f231dd2011-07-02 22:56:22 -0700802
803 if (ret == TMF_RESP_FUNC_COMPLETE)
804 dev_dbg(&isci_host->pdev->dev,
805 "%s: %p: TMF_LU_RESET passed\n",
806 __func__, isci_device);
807 else
808 dev_dbg(&isci_host->pdev->dev,
809 "%s: %p: TMF_LU_RESET failed (%x)\n",
810 __func__, isci_device, ret);
811
812 return ret;
813}
814
815/**
816 * isci_task_lu_reset() - This function is one of the SAS Domain Template
817 * functions. This is one of the Task Management functoins called by libsas,
818 * to reset the given lun. Note the assumption that while this call is
819 * executing, no I/O will be sent by the host to the device.
820 * @lun: This parameter specifies the lun to be reset.
821 *
822 * status, zero indicates success.
823 */
Dan Williams4393aa42011-03-31 13:10:44 -0700824int isci_task_lu_reset(struct domain_device *domain_device, u8 *lun)
Dan Williams6f231dd2011-07-02 22:56:22 -0700825{
Dan Williams4393aa42011-03-31 13:10:44 -0700826 struct isci_host *isci_host = dev_to_ihost(domain_device);
Dan Williams209fae12011-06-13 17:39:44 -0700827 struct isci_remote_device *isci_device;
828 unsigned long flags;
Dan Williams6f231dd2011-07-02 22:56:22 -0700829 int ret;
Dan Williams6f231dd2011-07-02 22:56:22 -0700830
Dan Williams209fae12011-06-13 17:39:44 -0700831 spin_lock_irqsave(&isci_host->scic_lock, flags);
832 isci_device = isci_lookup_device(domain_device);
833 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700834
Dan Williams4393aa42011-03-31 13:10:44 -0700835 dev_dbg(&isci_host->pdev->dev,
836 "%s: domain_device=%p, isci_host=%p; isci_device=%p\n",
Dan Williams6f231dd2011-07-02 22:56:22 -0700837 __func__, domain_device, isci_host, isci_device);
838
Dan Williams209fae12011-06-13 17:39:44 -0700839 if (isci_device)
Dan Williamsd06b4872011-05-02 13:59:25 -0700840 set_bit(IDEV_EH, &isci_device->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700841
842 /* If there is a device reset pending on any request in the
843 * device's list, fail this LUN reset request in order to
844 * escalate to the device reset.
845 */
Dan Williams209fae12011-06-13 17:39:44 -0700846 if (!isci_device ||
Dan Williams4393aa42011-03-31 13:10:44 -0700847 isci_device_is_reset_pending(isci_host, isci_device)) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700848 dev_warn(&isci_host->pdev->dev,
Dan Williams4393aa42011-03-31 13:10:44 -0700849 "%s: No dev (%p), or "
Dan Williams6f231dd2011-07-02 22:56:22 -0700850 "RESET PENDING: domain_device=%p\n",
Dan Williams4393aa42011-03-31 13:10:44 -0700851 __func__, isci_device, domain_device);
Dan Williams209fae12011-06-13 17:39:44 -0700852 ret = TMF_RESP_FUNC_FAILED;
853 goto out;
Dan Williams6f231dd2011-07-02 22:56:22 -0700854 }
855
Dan Williams6f231dd2011-07-02 22:56:22 -0700856 /* Send the task management part of the reset. */
857 if (sas_protocol_ata(domain_device->tproto)) {
Dan Williams4393aa42011-03-31 13:10:44 -0700858 ret = isci_task_send_lu_reset_sata(isci_host, isci_device, lun);
Dan Williams6f231dd2011-07-02 22:56:22 -0700859 } else
860 ret = isci_task_send_lu_reset_sas(isci_host, isci_device, lun);
861
862 /* If the LUN reset worked, all the I/O can now be terminated. */
863 if (ret == TMF_RESP_FUNC_COMPLETE)
864 /* Terminate all I/O now. */
865 isci_terminate_pending_requests(isci_host,
Dan Williams980d3ae2011-06-20 15:11:22 -0700866 isci_device);
Dan Williams6f231dd2011-07-02 22:56:22 -0700867
Dan Williams209fae12011-06-13 17:39:44 -0700868 out:
869 isci_put_device(isci_device);
Dan Williams6f231dd2011-07-02 22:56:22 -0700870 return ret;
871}
872
873
874/* int (*lldd_clear_nexus_port)(struct asd_sas_port *); */
875int isci_task_clear_nexus_port(struct asd_sas_port *port)
876{
877 return TMF_RESP_FUNC_FAILED;
878}
879
880
881
882int isci_task_clear_nexus_ha(struct sas_ha_struct *ha)
883{
884 return TMF_RESP_FUNC_FAILED;
885}
886
Dan Williams6f231dd2011-07-02 22:56:22 -0700887/* Task Management Functions. Must be called from process context. */
888
889/**
890 * isci_abort_task_process_cb() - This is a helper function for the abort task
891 * TMF command. It manages the request state with respect to the successful
892 * transmission / completion of the abort task request.
893 * @cb_state: This parameter specifies when this function was called - after
894 * the TMF request has been started and after it has timed-out.
895 * @tmf: This parameter specifies the TMF in progress.
896 *
897 *
898 */
899static void isci_abort_task_process_cb(
900 enum isci_tmf_cb_state cb_state,
901 struct isci_tmf *tmf,
902 void *cb_data)
903{
904 struct isci_request *old_request;
905
906 old_request = (struct isci_request *)cb_data;
907
908 dev_dbg(&old_request->isci_host->pdev->dev,
909 "%s: tmf=%p, old_request=%p\n",
910 __func__, tmf, old_request);
911
912 switch (cb_state) {
913
914 case isci_tmf_started:
915 /* The TMF has been started. Nothing to do here, since the
916 * request state was already set to "aborted" by the abort
917 * task function.
918 */
Bartosz Barcinski6cb4d6b2011-04-12 17:28:43 -0700919 if ((old_request->status != aborted)
920 && (old_request->status != completed))
921 dev_err(&old_request->isci_host->pdev->dev,
922 "%s: Bad request status (%d): tmf=%p, old_request=%p\n",
923 __func__, old_request->status, tmf, old_request);
Dan Williams6f231dd2011-07-02 22:56:22 -0700924 break;
925
926 case isci_tmf_timed_out:
927
928 /* Set the task's state to "aborting", since the abort task
929 * function thread set it to "aborted" (above) in anticipation
930 * of the task management request working correctly. Since the
931 * timeout has now fired, the TMF request failed. We set the
932 * state such that the request completion will indicate the
933 * device is no longer present.
934 */
935 isci_request_change_state(old_request, aborting);
936 break;
937
938 default:
939 dev_err(&old_request->isci_host->pdev->dev,
940 "%s: Bad cb_state (%d): tmf=%p, old_request=%p\n",
941 __func__, cb_state, tmf, old_request);
942 break;
943 }
944}
945
946/**
947 * isci_task_abort_task() - This function is one of the SAS Domain Template
948 * functions. This function is called by libsas to abort a specified task.
949 * @task: This parameter specifies the SAS task to abort.
950 *
951 * status, zero indicates success.
952 */
953int isci_task_abort_task(struct sas_task *task)
954{
Dan Williams4393aa42011-03-31 13:10:44 -0700955 struct isci_host *isci_host = dev_to_ihost(task->dev);
Dan Williams6f231dd2011-07-02 22:56:22 -0700956 DECLARE_COMPLETION_ONSTACK(aborted_io_completion);
Jeff Skirvina5fde222011-03-04 14:06:42 -0800957 struct isci_request *old_request = NULL;
958 enum isci_request_status old_state;
Dan Williams6f231dd2011-07-02 22:56:22 -0700959 struct isci_remote_device *isci_device = NULL;
Jeff Skirvina5fde222011-03-04 14:06:42 -0800960 struct isci_tmf tmf;
961 int ret = TMF_RESP_FUNC_FAILED;
962 unsigned long flags;
963 bool any_dev_reset = false;
Dan Williams6f231dd2011-07-02 22:56:22 -0700964
965 /* Get the isci_request reference from the task. Note that
966 * this check does not depend on the pending request list
967 * in the device, because tasks driving resets may land here
968 * after completion in the core.
969 */
Dan Williams209fae12011-06-13 17:39:44 -0700970 spin_lock_irqsave(&isci_host->scic_lock, flags);
971 spin_lock(&task->task_state_lock);
972
973 old_request = task->lldd_task;
974
975 /* If task is already done, the request isn't valid */
976 if (!(task->task_state_flags & SAS_TASK_STATE_DONE) &&
977 (task->task_state_flags & SAS_TASK_AT_INITIATOR) &&
978 old_request)
979 isci_device = isci_lookup_device(task->dev);
980
981 spin_unlock(&task->task_state_lock);
982 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700983
984 dev_dbg(&isci_host->pdev->dev,
985 "%s: task = %p\n", __func__, task);
986
Dan Williams209fae12011-06-13 17:39:44 -0700987 if (!isci_device || !old_request)
988 goto out;
Dan Williams6f231dd2011-07-02 22:56:22 -0700989
Dan Williamsd06b4872011-05-02 13:59:25 -0700990 set_bit(IDEV_EH, &isci_device->flags);
991
Dan Williams6f231dd2011-07-02 22:56:22 -0700992 /* This version of the driver will fail abort requests for
993 * SATA/STP. Failing the abort request this way will cause the
994 * SCSI error handler thread to escalate to LUN reset
995 */
Dan Williams209fae12011-06-13 17:39:44 -0700996 if (sas_protocol_ata(task->task_proto)) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700997 dev_warn(&isci_host->pdev->dev,
998 " task %p is for a STP/SATA device;"
999 " returning TMF_RESP_FUNC_FAILED\n"
1000 " to cause a LUN reset...\n", task);
Dan Williams209fae12011-06-13 17:39:44 -07001001 goto out;
Dan Williams6f231dd2011-07-02 22:56:22 -07001002 }
1003
1004 dev_dbg(&isci_host->pdev->dev,
1005 "%s: old_request == %p\n", __func__, old_request);
1006
Dan Williams209fae12011-06-13 17:39:44 -07001007 any_dev_reset = isci_device_is_reset_pending(isci_host,isci_device);
Jeff Skirvina5fde222011-03-04 14:06:42 -08001008
Dan Williams6f231dd2011-07-02 22:56:22 -07001009 spin_lock_irqsave(&task->task_state_lock, flags);
1010
Dan Williams209fae12011-06-13 17:39:44 -07001011 any_dev_reset = any_dev_reset || (task->task_state_flags & SAS_TASK_NEED_DEV_RESET);
Dan Williams6f231dd2011-07-02 22:56:22 -07001012
1013 /* If the extraction of the request reference from the task
1014 * failed, then the request has been completed (or if there is a
1015 * pending reset then this abort request function must be failed
1016 * in order to escalate to the target reset).
1017 */
Jeff Skirvina5fde222011-03-04 14:06:42 -08001018 if ((old_request == NULL) || any_dev_reset) {
Dan Williams6f231dd2011-07-02 22:56:22 -07001019
1020 /* If the device reset task flag is set, fail the task
1021 * management request. Otherwise, the original request
1022 * has completed.
1023 */
1024 if (any_dev_reset) {
1025
1026 /* Turn off the task's DONE to make sure this
1027 * task is escalated to a target reset.
1028 */
1029 task->task_state_flags &= ~SAS_TASK_STATE_DONE;
1030
Jeff Skirvina5fde222011-03-04 14:06:42 -08001031 /* Make the reset happen as soon as possible. */
1032 task->task_state_flags |= SAS_TASK_NEED_DEV_RESET;
1033
1034 spin_unlock_irqrestore(&task->task_state_lock, flags);
1035
Dan Williams6f231dd2011-07-02 22:56:22 -07001036 /* Fail the task management request in order to
1037 * escalate to the target reset.
1038 */
1039 ret = TMF_RESP_FUNC_FAILED;
1040
1041 dev_dbg(&isci_host->pdev->dev,
1042 "%s: Failing task abort in order to "
1043 "escalate to target reset because\n"
1044 "SAS_TASK_NEED_DEV_RESET is set for "
1045 "task %p on dev %p\n",
1046 __func__, task, isci_device);
1047
Jeff Skirvina5fde222011-03-04 14:06:42 -08001048
Dan Williams6f231dd2011-07-02 22:56:22 -07001049 } else {
Dan Williams6f231dd2011-07-02 22:56:22 -07001050 /* The request has already completed and there
1051 * is nothing to do here other than to set the task
1052 * done bit, and indicate that the task abort function
1053 * was sucessful.
1054 */
1055 isci_set_task_doneflags(task);
1056
Jeff Skirvina5fde222011-03-04 14:06:42 -08001057 spin_unlock_irqrestore(&task->task_state_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001058
Jeff Skirvina5fde222011-03-04 14:06:42 -08001059 ret = TMF_RESP_FUNC_COMPLETE;
Dan Williams6f231dd2011-07-02 22:56:22 -07001060
Jeff Skirvina5fde222011-03-04 14:06:42 -08001061 dev_dbg(&isci_host->pdev->dev,
1062 "%s: abort task not needed for %p\n",
1063 __func__, task);
Dan Williams6f231dd2011-07-02 22:56:22 -07001064 }
Dan Williams209fae12011-06-13 17:39:44 -07001065 goto out;
Dan Williams6f231dd2011-07-02 22:56:22 -07001066 }
Jeff Skirvina5fde222011-03-04 14:06:42 -08001067 else
1068 spin_unlock_irqrestore(&task->task_state_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001069
1070 spin_lock_irqsave(&isci_host->scic_lock, flags);
1071
Jeff Skirvinf219f012011-03-31 13:10:34 -07001072 /* Check the request status and change to "aborted" if currently
Jeff Skirvina5fde222011-03-04 14:06:42 -08001073 * "starting"; if true then set the I/O kernel completion
Dan Williams6f231dd2011-07-02 22:56:22 -07001074 * struct that will be triggered when the request completes.
1075 */
Jeff Skirvina5fde222011-03-04 14:06:42 -08001076 old_state = isci_task_validate_request_to_abort(
1077 old_request, isci_host, isci_device,
1078 &aborted_io_completion);
Jeff Skirvinf219f012011-03-31 13:10:34 -07001079 if ((old_state != started) &&
1080 (old_state != completed) &&
1081 (old_state != aborting)) {
Dan Williams6f231dd2011-07-02 22:56:22 -07001082
1083 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1084
Jeff Skirvina5fde222011-03-04 14:06:42 -08001085 /* The request was already being handled by someone else (because
1086 * they got to set the state away from started).
1087 */
1088 dev_dbg(&isci_host->pdev->dev,
1089 "%s: device = %p; old_request %p already being aborted\n",
1090 __func__,
1091 isci_device, old_request);
Dan Williams209fae12011-06-13 17:39:44 -07001092 ret = TMF_RESP_FUNC_COMPLETE;
1093 goto out;
Dan Williams6f231dd2011-07-02 22:56:22 -07001094 }
Jeff Skirvina5fde222011-03-04 14:06:42 -08001095 if ((task->task_proto == SAS_PROTOCOL_SMP)
Jeff Skirvina5fde222011-03-04 14:06:42 -08001096 || old_request->complete_in_target
1097 ) {
Dan Williams6f231dd2011-07-02 22:56:22 -07001098
1099 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1100
Jeff Skirvina5fde222011-03-04 14:06:42 -08001101 dev_dbg(&isci_host->pdev->dev,
1102 "%s: SMP request (%d)"
Jeff Skirvina5fde222011-03-04 14:06:42 -08001103 " or complete_in_target (%d), thus no TMF\n",
1104 __func__, (task->task_proto == SAS_PROTOCOL_SMP),
Dan Williams209fae12011-06-13 17:39:44 -07001105 old_request->complete_in_target);
Jeff Skirvina5fde222011-03-04 14:06:42 -08001106
Dan Williams6f231dd2011-07-02 22:56:22 -07001107 /* Set the state on the task. */
1108 isci_task_all_done(task);
1109
1110 ret = TMF_RESP_FUNC_COMPLETE;
1111
1112 /* Stopping and SMP devices are not sent a TMF, and are not
Jeff Skirvina5fde222011-03-04 14:06:42 -08001113 * reset, but the outstanding I/O request is terminated below.
Dan Williams6f231dd2011-07-02 22:56:22 -07001114 */
Dan Williams6f231dd2011-07-02 22:56:22 -07001115 } else {
1116 /* Fill in the tmf stucture */
Dan Williams209fae12011-06-13 17:39:44 -07001117 isci_task_build_abort_task_tmf(&tmf, isci_tmf_ssp_task_abort,
Jeff Skirvinc3f42fe2011-03-04 14:06:56 -08001118 isci_abort_task_process_cb,
1119 old_request);
Dan Williams6f231dd2011-07-02 22:56:22 -07001120
Dan Williams6f231dd2011-07-02 22:56:22 -07001121 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1122
1123 #define ISCI_ABORT_TASK_TIMEOUT_MS 500 /* half second timeout. */
Dan Williams209fae12011-06-13 17:39:44 -07001124 ret = isci_task_execute_tmf(isci_host, isci_device, &tmf,
Dan Williams6f231dd2011-07-02 22:56:22 -07001125 ISCI_ABORT_TASK_TIMEOUT_MS);
1126
Jeff Skirvina5fde222011-03-04 14:06:42 -08001127 if (ret != TMF_RESP_FUNC_COMPLETE)
Dan Williams6f231dd2011-07-02 22:56:22 -07001128 dev_err(&isci_host->pdev->dev,
1129 "%s: isci_task_send_tmf failed\n",
1130 __func__);
1131 }
Jeff Skirvina5fde222011-03-04 14:06:42 -08001132 if (ret == TMF_RESP_FUNC_COMPLETE) {
1133 old_request->complete_in_target = true;
Dan Williams6f231dd2011-07-02 22:56:22 -07001134
Jeff Skirvin77c852f2011-06-20 14:09:16 -07001135 /* Clean up the request on our side, and wait for the aborted
1136 * I/O to complete.
1137 */
Jeff Skirvincbb65c62011-03-04 14:06:50 -08001138 isci_terminate_request_core(isci_host, isci_device, old_request);
Jeff Skirvina5fde222011-03-04 14:06:42 -08001139 }
1140
1141 /* Make sure we do not leave a reference to aborted_io_completion */
1142 old_request->io_request_completion = NULL;
Dan Williams209fae12011-06-13 17:39:44 -07001143 out:
1144 isci_put_device(isci_device);
Dan Williams6f231dd2011-07-02 22:56:22 -07001145 return ret;
1146}
1147
1148/**
1149 * isci_task_abort_task_set() - This function is one of the SAS Domain Template
1150 * functions. This is one of the Task Management functoins called by libsas,
1151 * to abort all task for the given lun.
1152 * @d_device: This parameter specifies the domain device associated with this
1153 * request.
1154 * @lun: This parameter specifies the lun associated with this request.
1155 *
1156 * status, zero indicates success.
1157 */
1158int isci_task_abort_task_set(
1159 struct domain_device *d_device,
1160 u8 *lun)
1161{
1162 return TMF_RESP_FUNC_FAILED;
1163}
1164
1165
1166/**
1167 * isci_task_clear_aca() - This function is one of the SAS Domain Template
1168 * functions. This is one of the Task Management functoins called by libsas.
1169 * @d_device: This parameter specifies the domain device associated with this
1170 * request.
1171 * @lun: This parameter specifies the lun associated with this request.
1172 *
1173 * status, zero indicates success.
1174 */
1175int isci_task_clear_aca(
1176 struct domain_device *d_device,
1177 u8 *lun)
1178{
1179 return TMF_RESP_FUNC_FAILED;
1180}
1181
1182
1183
1184/**
1185 * isci_task_clear_task_set() - This function is one of the SAS Domain Template
1186 * functions. This is one of the Task Management functoins called by libsas.
1187 * @d_device: This parameter specifies the domain device associated with this
1188 * request.
1189 * @lun: This parameter specifies the lun associated with this request.
1190 *
1191 * status, zero indicates success.
1192 */
1193int isci_task_clear_task_set(
1194 struct domain_device *d_device,
1195 u8 *lun)
1196{
1197 return TMF_RESP_FUNC_FAILED;
1198}
1199
1200
1201/**
1202 * isci_task_query_task() - This function is implemented to cause libsas to
1203 * correctly escalate the failed abort to a LUN or target reset (this is
1204 * because sas_scsi_find_task libsas function does not correctly interpret
1205 * all return codes from the abort task call). When TMF_RESP_FUNC_SUCC is
1206 * returned, libsas turns this into a LUN reset; when FUNC_FAILED is
1207 * returned, libsas will turn this into a target reset
1208 * @task: This parameter specifies the sas task being queried.
1209 * @lun: This parameter specifies the lun associated with this request.
1210 *
1211 * status, zero indicates success.
1212 */
1213int isci_task_query_task(
1214 struct sas_task *task)
1215{
1216 /* See if there is a pending device reset for this device. */
1217 if (task->task_state_flags & SAS_TASK_NEED_DEV_RESET)
1218 return TMF_RESP_FUNC_FAILED;
1219 else
1220 return TMF_RESP_FUNC_SUCC;
1221}
1222
Dave Jiangaf5ae892011-05-04 17:53:24 -07001223/*
Dan Williams6f231dd2011-07-02 22:56:22 -07001224 * isci_task_request_complete() - This function is called by the sci core when
1225 * an task request completes.
Dave Jiangaf5ae892011-05-04 17:53:24 -07001226 * @ihost: This parameter specifies the ISCI host object
1227 * @ireq: This parameter is the completed isci_request object.
Dan Williams6f231dd2011-07-02 22:56:22 -07001228 * @completion_status: This parameter specifies the completion status from the
1229 * sci core.
1230 *
1231 * none.
1232 */
Dave Jiangaf5ae892011-05-04 17:53:24 -07001233void
1234isci_task_request_complete(struct isci_host *ihost,
1235 struct isci_request *ireq,
1236 enum sci_task_status completion_status)
Dan Williams6f231dd2011-07-02 22:56:22 -07001237{
Dave Jiangaf5ae892011-05-04 17:53:24 -07001238 struct isci_tmf *tmf = isci_request_access_tmf(ireq);
Dan Williams6f231dd2011-07-02 22:56:22 -07001239 struct completion *tmf_complete;
Dan Williams67ea8382011-05-08 11:47:15 -07001240 struct scic_sds_request *sci_req = &ireq->sci;
Dan Williams6f231dd2011-07-02 22:56:22 -07001241
Dave Jiangaf5ae892011-05-04 17:53:24 -07001242 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -07001243 "%s: request = %p, status=%d\n",
Dave Jiangaf5ae892011-05-04 17:53:24 -07001244 __func__, ireq, completion_status);
Dan Williams6f231dd2011-07-02 22:56:22 -07001245
Dave Jiang8d2c65c2011-06-01 09:03:08 +00001246 isci_request_change_state(ireq, completed);
Dan Williams6f231dd2011-07-02 22:56:22 -07001247
1248 tmf->status = completion_status;
Dave Jiangaf5ae892011-05-04 17:53:24 -07001249 ireq->complete_in_target = true;
Dan Williams6f231dd2011-07-02 22:56:22 -07001250
Dave Jiangaf5ae892011-05-04 17:53:24 -07001251 if (tmf->proto == SAS_PROTOCOL_SSP) {
Dan Williams6f231dd2011-07-02 22:56:22 -07001252 memcpy(&tmf->resp.resp_iu,
Dan Williamsb7645812011-05-08 02:35:32 -07001253 &sci_req->ssp.rsp,
Dave Jiangaf5ae892011-05-04 17:53:24 -07001254 SSP_RESP_IU_MAX_SIZE);
1255 } else if (tmf->proto == SAS_PROTOCOL_SATA) {
Dan Williams6f231dd2011-07-02 22:56:22 -07001256 memcpy(&tmf->resp.d2h_fis,
Dan Williamsb7645812011-05-08 02:35:32 -07001257 &sci_req->stp.rsp,
Dave Jiangf2f30082011-05-04 15:02:02 -07001258 sizeof(struct dev_to_host_fis));
Dan Williams6f231dd2011-07-02 22:56:22 -07001259 }
1260
Dan Williams6f231dd2011-07-02 22:56:22 -07001261 /* PRINT_TMF( ((struct isci_tmf *)request->task)); */
1262 tmf_complete = tmf->complete;
1263
Dan Williams209fae12011-06-13 17:39:44 -07001264 scic_controller_complete_io(&ihost->sci, ireq->sci.target_device, &ireq->sci);
Dan Williams67ea8382011-05-08 11:47:15 -07001265 /* set the 'terminated' flag handle to make sure it cannot be terminated
Dan Williams6f231dd2011-07-02 22:56:22 -07001266 * or completed again.
1267 */
Dan Williams67ea8382011-05-08 11:47:15 -07001268 ireq->terminated = true;;
Dan Williams6f231dd2011-07-02 22:56:22 -07001269
Dave Jiangaf5ae892011-05-04 17:53:24 -07001270 isci_request_change_state(ireq, unallocated);
1271 list_del_init(&ireq->dev_node);
Dan Williams6f231dd2011-07-02 22:56:22 -07001272
1273 /* The task management part completes last. */
1274 complete(tmf_complete);
1275}
1276
Jeff Skirvin61aaff42011-06-21 12:16:33 -07001277static void isci_smp_task_timedout(unsigned long _task)
1278{
1279 struct sas_task *task = (void *) _task;
1280 unsigned long flags;
1281
1282 spin_lock_irqsave(&task->task_state_lock, flags);
1283 if (!(task->task_state_flags & SAS_TASK_STATE_DONE))
1284 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
1285 spin_unlock_irqrestore(&task->task_state_lock, flags);
1286
1287 complete(&task->completion);
1288}
1289
1290static void isci_smp_task_done(struct sas_task *task)
1291{
1292 if (!del_timer(&task->timer))
1293 return;
1294 complete(&task->completion);
1295}
1296
1297static struct sas_task *isci_alloc_task(void)
1298{
1299 struct sas_task *task = kzalloc(sizeof(*task), GFP_KERNEL);
1300
1301 if (task) {
1302 INIT_LIST_HEAD(&task->list);
1303 spin_lock_init(&task->task_state_lock);
1304 task->task_state_flags = SAS_TASK_STATE_PENDING;
1305 init_timer(&task->timer);
1306 init_completion(&task->completion);
1307 }
1308
1309 return task;
1310}
1311
1312static void isci_free_task(struct isci_host *ihost, struct sas_task *task)
1313{
1314 if (task) {
1315 BUG_ON(!list_empty(&task->list));
1316 kfree(task);
1317 }
1318}
1319
1320static int isci_smp_execute_task(struct isci_host *ihost,
1321 struct domain_device *dev, void *req,
1322 int req_size, void *resp, int resp_size)
1323{
1324 int res, retry;
1325 struct sas_task *task = NULL;
1326
1327 for (retry = 0; retry < 3; retry++) {
1328 task = isci_alloc_task();
1329 if (!task)
1330 return -ENOMEM;
1331
1332 task->dev = dev;
1333 task->task_proto = dev->tproto;
1334 sg_init_one(&task->smp_task.smp_req, req, req_size);
1335 sg_init_one(&task->smp_task.smp_resp, resp, resp_size);
1336
1337 task->task_done = isci_smp_task_done;
1338
1339 task->timer.data = (unsigned long) task;
1340 task->timer.function = isci_smp_task_timedout;
1341 task->timer.expires = jiffies + 10*HZ;
1342 add_timer(&task->timer);
1343
1344 res = isci_task_execute_task(task, 1, GFP_KERNEL);
1345
1346 if (res) {
1347 del_timer(&task->timer);
1348 dev_err(&ihost->pdev->dev,
1349 "%s: executing SMP task failed:%d\n",
1350 __func__, res);
1351 goto ex_err;
1352 }
1353
1354 wait_for_completion(&task->completion);
1355 res = -ECOMM;
1356 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
1357 dev_err(&ihost->pdev->dev,
1358 "%s: smp task timed out or aborted\n",
1359 __func__);
1360 isci_task_abort_task(task);
1361 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
1362 dev_err(&ihost->pdev->dev,
1363 "%s: SMP task aborted and not done\n",
1364 __func__);
1365 goto ex_err;
1366 }
1367 }
1368 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1369 task->task_status.stat == SAM_STAT_GOOD) {
1370 res = 0;
1371 break;
1372 }
1373 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1374 task->task_status.stat == SAS_DATA_UNDERRUN) {
1375 /* no error, but return the number of bytes of
1376 * underrun */
1377 res = task->task_status.residual;
1378 break;
1379 }
1380 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1381 task->task_status.stat == SAS_DATA_OVERRUN) {
1382 res = -EMSGSIZE;
1383 break;
1384 } else {
1385 dev_err(&ihost->pdev->dev,
1386 "%s: task to dev %016llx response: 0x%x "
1387 "status 0x%x\n", __func__,
1388 SAS_ADDR(dev->sas_addr),
1389 task->task_status.resp,
1390 task->task_status.stat);
1391 isci_free_task(ihost, task);
1392 task = NULL;
1393 }
1394 }
1395ex_err:
1396 BUG_ON(retry == 3 && task != NULL);
1397 isci_free_task(ihost, task);
1398 return res;
1399}
1400
1401#define DISCOVER_REQ_SIZE 16
1402#define DISCOVER_RESP_SIZE 56
1403
1404int isci_smp_get_phy_attached_dev_type(struct isci_host *ihost,
1405 struct domain_device *dev,
1406 int phy_id, int *adt)
1407{
1408 struct smp_resp *disc_resp;
1409 u8 *disc_req;
1410 int res;
1411
1412 disc_resp = kzalloc(DISCOVER_RESP_SIZE, GFP_KERNEL);
1413 if (!disc_resp)
1414 return -ENOMEM;
1415
1416 disc_req = kzalloc(DISCOVER_REQ_SIZE, GFP_KERNEL);
1417 if (disc_req) {
1418 disc_req[0] = SMP_REQUEST;
1419 disc_req[1] = SMP_DISCOVER;
1420 disc_req[9] = phy_id;
1421 } else {
1422 kfree(disc_resp);
1423 return -ENOMEM;
1424 }
1425 res = isci_smp_execute_task(ihost, dev, disc_req, DISCOVER_REQ_SIZE,
1426 disc_resp, DISCOVER_RESP_SIZE);
1427 if (!res) {
1428 if (disc_resp->result != SMP_RESP_FUNC_ACC)
1429 res = disc_resp->result;
1430 else
1431 *adt = disc_resp->disc.attached_dev_type;
1432 }
1433 kfree(disc_req);
1434 kfree(disc_resp);
1435
1436 return res;
1437}
1438
1439static void isci_wait_for_smp_phy_reset(struct isci_remote_device *idev, int phy_num)
1440{
1441 struct domain_device *dev = idev->domain_dev;
1442 struct isci_port *iport = idev->isci_port;
1443 struct isci_host *ihost = iport->isci_host;
1444 int res, iteration = 0, attached_device_type;
1445 #define STP_WAIT_MSECS 25000
1446 unsigned long tmo = msecs_to_jiffies(STP_WAIT_MSECS);
1447 unsigned long deadline = jiffies + tmo;
1448 enum {
1449 SMP_PHYWAIT_PHYDOWN,
1450 SMP_PHYWAIT_PHYUP,
1451 SMP_PHYWAIT_DONE
1452 } phy_state = SMP_PHYWAIT_PHYDOWN;
1453
1454 /* While there is time, wait for the phy to go away and come back */
1455 while (time_is_after_jiffies(deadline) && phy_state != SMP_PHYWAIT_DONE) {
1456 int event = atomic_read(&iport->event);
1457
1458 ++iteration;
1459
1460 tmo = wait_event_timeout(ihost->eventq,
1461 event != atomic_read(&iport->event) ||
1462 !test_bit(IPORT_BCN_BLOCKED, &iport->flags),
1463 tmo);
1464 /* link down, stop polling */
1465 if (!test_bit(IPORT_BCN_BLOCKED, &iport->flags))
1466 break;
1467
1468 dev_dbg(&ihost->pdev->dev,
1469 "%s: iport %p, iteration %d,"
1470 " phase %d: time_remaining %lu, bcns = %d\n",
1471 __func__, iport, iteration, phy_state,
1472 tmo, test_bit(IPORT_BCN_PENDING, &iport->flags));
1473
1474 res = isci_smp_get_phy_attached_dev_type(ihost, dev, phy_num,
1475 &attached_device_type);
1476 tmo = deadline - jiffies;
1477
1478 if (res) {
1479 dev_warn(&ihost->pdev->dev,
1480 "%s: iteration %d, phase %d:"
1481 " SMP error=%d, time_remaining=%lu\n",
1482 __func__, iteration, phy_state, res, tmo);
1483 break;
1484 }
1485 dev_dbg(&ihost->pdev->dev,
1486 "%s: iport %p, iteration %d,"
1487 " phase %d: time_remaining %lu, bcns = %d, "
1488 "attdevtype = %x\n",
1489 __func__, iport, iteration, phy_state,
1490 tmo, test_bit(IPORT_BCN_PENDING, &iport->flags),
1491 attached_device_type);
1492
1493 switch (phy_state) {
1494 case SMP_PHYWAIT_PHYDOWN:
1495 /* Has the device gone away? */
1496 if (!attached_device_type)
1497 phy_state = SMP_PHYWAIT_PHYUP;
1498
1499 break;
1500
1501 case SMP_PHYWAIT_PHYUP:
1502 /* Has the device come back? */
1503 if (attached_device_type)
1504 phy_state = SMP_PHYWAIT_DONE;
1505 break;
1506
1507 case SMP_PHYWAIT_DONE:
1508 break;
1509 }
1510
1511 }
1512 dev_dbg(&ihost->pdev->dev, "%s: done\n", __func__);
1513}
1514
Dan Williams209fae12011-06-13 17:39:44 -07001515static int isci_reset_device(struct isci_host *ihost,
1516 struct isci_remote_device *idev, int hard_reset)
Dan Williams6f231dd2011-07-02 22:56:22 -07001517{
Dan Williams209fae12011-06-13 17:39:44 -07001518 struct sas_phy *phy = sas_find_local_phy(idev->domain_dev);
Jeff Skirvin61aaff42011-06-21 12:16:33 -07001519 struct isci_port *iport = idev->isci_port;
Dan Williams6f231dd2011-07-02 22:56:22 -07001520 enum sci_status status;
Dan Williamsd06b4872011-05-02 13:59:25 -07001521 unsigned long flags;
1522 int rc;
Dan Williams6f231dd2011-07-02 22:56:22 -07001523
Dan Williamsd06b4872011-05-02 13:59:25 -07001524 dev_dbg(&ihost->pdev->dev, "%s: idev %p\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001525
Dan Williamsd06b4872011-05-02 13:59:25 -07001526 spin_lock_irqsave(&ihost->scic_lock, flags);
1527 status = scic_remote_device_reset(&idev->sci);
Dan Williams6f231dd2011-07-02 22:56:22 -07001528 if (status != SCI_SUCCESS) {
Dan Williamsd06b4872011-05-02 13:59:25 -07001529 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001530
Dan Williamsd06b4872011-05-02 13:59:25 -07001531 dev_warn(&ihost->pdev->dev,
1532 "%s: scic_remote_device_reset(%p) returned %d!\n",
1533 __func__, idev, status);
Dan Williams6f231dd2011-07-02 22:56:22 -07001534
1535 return TMF_RESP_FUNC_FAILED;
1536 }
Dan Williamsd06b4872011-05-02 13:59:25 -07001537 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001538
Dan Williams6f231dd2011-07-02 22:56:22 -07001539 /* Make sure all pending requests are able to be fully terminated. */
Dan Williamsd06b4872011-05-02 13:59:25 -07001540 isci_device_clear_reset_pending(ihost, idev);
1541
Jeff Skirvin61aaff42011-06-21 12:16:33 -07001542 /* If this is a device on an expander, disable BCN processing. */
1543 if (!scsi_is_sas_phy_local(phy))
1544 set_bit(IPORT_BCN_BLOCKED, &iport->flags);
1545
Dan Williamsd06b4872011-05-02 13:59:25 -07001546 rc = sas_phy_reset(phy, hard_reset);
Dan Williams6f231dd2011-07-02 22:56:22 -07001547
1548 /* Terminate in-progress I/O now. */
Dan Williamsd06b4872011-05-02 13:59:25 -07001549 isci_remote_device_nuke_requests(ihost, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001550
Jeff Skirvinff717ab2011-06-20 14:08:51 -07001551 /* Since all pending TCs have been cleaned, resume the RNC. */
Dan Williamsd06b4872011-05-02 13:59:25 -07001552 spin_lock_irqsave(&ihost->scic_lock, flags);
1553 status = scic_remote_device_reset_complete(&idev->sci);
1554 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001555
Jeff Skirvin61aaff42011-06-21 12:16:33 -07001556 /* If this is a device on an expander, bring the phy back up. */
1557 if (!scsi_is_sas_phy_local(phy)) {
1558 /* A phy reset will cause the device to go away then reappear.
1559 * Since libsas will take action on incoming BCNs (eg. remove
1560 * a device going through an SMP phy-control driven reset),
1561 * we need to wait until the phy comes back up before letting
1562 * discovery proceed in libsas.
1563 */
1564 isci_wait_for_smp_phy_reset(idev, phy->number);
1565
1566 spin_lock_irqsave(&ihost->scic_lock, flags);
1567 isci_port_bcn_enable(ihost, idev->isci_port);
1568 spin_unlock_irqrestore(&ihost->scic_lock, flags);
1569 }
Jeff Skirvinff717ab2011-06-20 14:08:51 -07001570
Dan Williams6f231dd2011-07-02 22:56:22 -07001571 if (status != SCI_SUCCESS) {
Dan Williamsd06b4872011-05-02 13:59:25 -07001572 dev_warn(&ihost->pdev->dev,
1573 "%s: scic_remote_device_reset_complete(%p) "
1574 "returned %d!\n", __func__, idev, status);
Dan Williams6f231dd2011-07-02 22:56:22 -07001575 }
Dan Williams6f231dd2011-07-02 22:56:22 -07001576
Dan Williamsd06b4872011-05-02 13:59:25 -07001577 dev_dbg(&ihost->pdev->dev, "%s: idev %p complete.\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001578
Dan Williamsd06b4872011-05-02 13:59:25 -07001579 return rc;
1580}
1581
1582int isci_task_I_T_nexus_reset(struct domain_device *dev)
1583{
1584 struct isci_host *ihost = dev_to_ihost(dev);
Dan Williamsd06b4872011-05-02 13:59:25 -07001585 struct isci_remote_device *idev;
Dan Williams209fae12011-06-13 17:39:44 -07001586 int ret, hard_reset = 1;
Dan Williamsd06b4872011-05-02 13:59:25 -07001587 unsigned long flags;
1588
Dan Williamsd06b4872011-05-02 13:59:25 -07001589 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williams209fae12011-06-13 17:39:44 -07001590 idev = isci_lookup_device(dev);
Dan Williamsd06b4872011-05-02 13:59:25 -07001591 spin_unlock_irqrestore(&ihost->scic_lock, flags);
1592
Dan Williams209fae12011-06-13 17:39:44 -07001593 if (!idev || !test_bit(IDEV_EH, &idev->flags)) {
1594 ret = TMF_RESP_FUNC_COMPLETE;
1595 goto out;
1596 }
Dan Williamsd06b4872011-05-02 13:59:25 -07001597
1598 if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP))
1599 hard_reset = 0;
1600
Dan Williams209fae12011-06-13 17:39:44 -07001601 ret = isci_reset_device(ihost, idev, hard_reset);
1602 out:
1603 isci_put_device(idev);
1604 return ret;
Dan Williamsd06b4872011-05-02 13:59:25 -07001605}
1606
1607int isci_bus_reset_handler(struct scsi_cmnd *cmd)
1608{
1609 struct domain_device *dev = sdev_to_domain_dev(cmd->device);
Dan Williams209fae12011-06-13 17:39:44 -07001610 struct isci_host *ihost = dev_to_ihost(dev);
1611 struct isci_remote_device *idev;
1612 int ret, hard_reset = 1;
1613 unsigned long flags;
Dan Williamsd06b4872011-05-02 13:59:25 -07001614
1615 if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP))
1616 hard_reset = 0;
1617
Dan Williams209fae12011-06-13 17:39:44 -07001618 spin_lock_irqsave(&ihost->scic_lock, flags);
1619 idev = isci_lookup_device(dev);
1620 spin_unlock_irqrestore(&ihost->scic_lock, flags);
1621
1622 if (!idev) {
1623 ret = TMF_RESP_FUNC_COMPLETE;
1624 goto out;
1625 }
1626
1627 ret = isci_reset_device(ihost, idev, hard_reset);
1628 out:
1629 isci_put_device(idev);
1630 return ret;
Dan Williams6f231dd2011-07-02 22:56:22 -07001631}