blob: dd5e9de1ffb50579c3a695d1b323c0673664b4d7 [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
Dan Williams6f231dd2011-07-02 22:56:22 -0700136/**
137 * isci_task_execute_task() - This function is one of the SAS Domain Template
138 * functions. This function is called by libsas to send a task down to
139 * hardware.
140 * @task: This parameter specifies the SAS task to send.
141 * @num: This parameter specifies the number of tasks to queue.
142 * @gfp_flags: This parameter specifies the context of this call.
143 *
144 * status, zero indicates success.
145 */
146int isci_task_execute_task(struct sas_task *task, int num, gfp_t gfp_flags)
147{
Dan Williams4393aa42011-03-31 13:10:44 -0700148 struct isci_host *ihost = dev_to_ihost(task->dev);
Dan Williams209fae12011-06-13 17:39:44 -0700149 struct isci_remote_device *idev;
Dan Williams6f231dd2011-07-02 22:56:22 -0700150 unsigned long flags;
Dan Williams6f231dd2011-07-02 22:56:22 -0700151 int ret;
152 enum sci_status status;
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700153 enum isci_status device_status;
Dan Williams6f231dd2011-07-02 22:56:22 -0700154
Dan Williams1077a572011-03-11 10:13:51 -0800155 dev_dbg(&ihost->pdev->dev, "%s: num=%d\n", __func__, num);
Dan Williams6f231dd2011-07-02 22:56:22 -0700156
157 /* Check if we have room for more tasks */
Dan Williams1077a572011-03-11 10:13:51 -0800158 ret = isci_host_can_queue(ihost, num);
Dan Williams6f231dd2011-07-02 22:56:22 -0700159
160 if (ret) {
Dan Williams1077a572011-03-11 10:13:51 -0800161 dev_warn(&ihost->pdev->dev, "%s: queue full\n", __func__);
Dan Williams6f231dd2011-07-02 22:56:22 -0700162 return ret;
163 }
164
Dan Williams1077a572011-03-11 10:13:51 -0800165 for_each_sas_task(num, task) {
166 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -0700167 "task = %p, num = %d; dev = %p; cmd = %p\n",
168 task, num, task->dev, task->uldd_task);
Dan Williams209fae12011-06-13 17:39:44 -0700169 spin_lock_irqsave(&ihost->scic_lock, flags);
170 idev = isci_lookup_device(task->dev);
171 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700172
Dan Williams209fae12011-06-13 17:39:44 -0700173 if (idev)
174 device_status = idev->status;
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700175 else
176 device_status = isci_freed;
177
178 /* From this point onward, any process that needs to guarantee
179 * that there is no kernel I/O being started will have to wait
180 * for the quiesce spinlock.
181 */
182
183 if (device_status != isci_ready_for_io) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700184
185 /* Forces a retry from scsi mid layer. */
Dave Jiangd2d61432011-04-21 05:36:23 +0000186 dev_dbg(&ihost->pdev->dev,
187 "%s: task %p: isci_host->status = %d, "
188 "device = %p; device_status = 0x%x\n\n",
189 __func__,
190 task,
191 isci_host_get_state(ihost),
Dan Williams209fae12011-06-13 17:39:44 -0700192 idev,
Dave Jiangd2d61432011-04-21 05:36:23 +0000193 device_status);
Dan Williams6f231dd2011-07-02 22:56:22 -0700194
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700195 if (device_status == isci_ready) {
196 /* Indicate QUEUE_FULL so that the scsi midlayer
197 * retries.
198 */
Dan Williams1077a572011-03-11 10:13:51 -0800199 isci_task_refuse(ihost, task,
200 SAS_TASK_COMPLETE,
201 SAS_QUEUE_FULL);
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700202 } else {
203 /* Else, the device is going down. */
Dan Williams1077a572011-03-11 10:13:51 -0800204 isci_task_refuse(ihost, task,
205 SAS_TASK_UNDELIVERED,
206 SAS_DEVICE_UNKNOWN);
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700207 }
Dan Williams1077a572011-03-11 10:13:51 -0800208 isci_host_can_dequeue(ihost, 1);
Dan Williams6f231dd2011-07-02 22:56:22 -0700209 } else {
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700210 /* There is a device and it's ready for I/O. */
211 spin_lock_irqsave(&task->task_state_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700212
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700213 if (task->task_state_flags & SAS_TASK_STATE_ABORTED) {
214
215 spin_unlock_irqrestore(&task->task_state_lock,
216 flags);
217
Dan Williams1077a572011-03-11 10:13:51 -0800218 isci_task_refuse(ihost, task,
219 SAS_TASK_UNDELIVERED,
220 SAM_STAT_TASK_ABORTED);
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700221
222 /* The I/O was aborted. */
223
224 } else {
Dan Williams6f231dd2011-07-02 22:56:22 -0700225 task->task_state_flags |= SAS_TASK_AT_INITIATOR;
226 spin_unlock_irqrestore(&task->task_state_lock, flags);
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700227
228 /* build and send the request. */
Dan Williams209fae12011-06-13 17:39:44 -0700229 status = isci_request_execute(ihost, idev, task, gfp_flags);
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700230
231 if (status != SCI_SUCCESS) {
232
233 spin_lock_irqsave(&task->task_state_lock, flags);
234 /* Did not really start this command. */
235 task->task_state_flags &= ~SAS_TASK_AT_INITIATOR;
236 spin_unlock_irqrestore(&task->task_state_lock, flags);
237
238 /* Indicate QUEUE_FULL so that the scsi
239 * midlayer retries. if the request
240 * failed for remote device reasons,
241 * it gets returned as
242 * SAS_TASK_UNDELIVERED next time
243 * through.
244 */
Dan Williams1077a572011-03-11 10:13:51 -0800245 isci_task_refuse(ihost, task,
246 SAS_TASK_COMPLETE,
247 SAS_QUEUE_FULL);
248 isci_host_can_dequeue(ihost, 1);
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700249 }
Dan Williams6f231dd2011-07-02 22:56:22 -0700250 }
251 }
Dan Williams209fae12011-06-13 17:39:44 -0700252 isci_put_device(idev);
Dan Williams1077a572011-03-11 10:13:51 -0800253 }
Dan Williams6f231dd2011-07-02 22:56:22 -0700254 return 0;
255}
256
Dan Williams0d0cf142011-06-13 00:51:30 -0700257static struct isci_request *isci_task_request_build(struct isci_host *ihost,
Dan Williams209fae12011-06-13 17:39:44 -0700258 struct isci_remote_device *idev,
Dan Williams0d0cf142011-06-13 00:51:30 -0700259 struct isci_tmf *isci_tmf)
Dan Williams6f231dd2011-07-02 22:56:22 -0700260{
Dan Williams6f231dd2011-07-02 22:56:22 -0700261 enum sci_status status = SCI_FAILURE;
Dan Williams0d0cf142011-06-13 00:51:30 -0700262 struct isci_request *ireq = NULL;
Dan Williamsa1a113b2011-04-21 18:44:45 -0700263 struct domain_device *dev;
Dan Williams6f231dd2011-07-02 22:56:22 -0700264
Dan Williams0d0cf142011-06-13 00:51:30 -0700265 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -0700266 "%s: isci_tmf = %p\n", __func__, isci_tmf);
267
Dan Williams0d0cf142011-06-13 00:51:30 -0700268 dev = idev->domain_dev;
Dan Williams6f231dd2011-07-02 22:56:22 -0700269
270 /* do common allocation and init of request object. */
Dan Williams209fae12011-06-13 17:39:44 -0700271 ireq = isci_request_alloc_tmf(ihost, isci_tmf, GFP_ATOMIC);
Dan Williams0d0cf142011-06-13 00:51:30 -0700272 if (!ireq)
273 return NULL;
Dan Williams6f231dd2011-07-02 22:56:22 -0700274
275 /* let the core do it's construct. */
Dan Williams209fae12011-06-13 17:39:44 -0700276 status = scic_task_request_construct(&ihost->sci, &idev->sci,
Dan Williams0d843662011-05-08 01:56:57 -0700277 SCI_CONTROLLER_INVALID_IO_TAG,
Dan Williams0d0cf142011-06-13 00:51:30 -0700278 &ireq->sci);
Dan Williams6f231dd2011-07-02 22:56:22 -0700279
280 if (status != SCI_SUCCESS) {
Dan Williams0d0cf142011-06-13 00:51:30 -0700281 dev_warn(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -0700282 "%s: scic_task_request_construct failed - "
283 "status = 0x%x\n",
284 __func__,
285 status);
286 goto errout;
287 }
288
Dan Williamsa1a113b2011-04-21 18:44:45 -0700289 /* XXX convert to get this from task->tproto like other drivers */
290 if (dev->dev_type == SAS_END_DEV) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700291 isci_tmf->proto = SAS_PROTOCOL_SSP;
Dan Williams0d0cf142011-06-13 00:51:30 -0700292 status = scic_task_request_construct_ssp(&ireq->sci);
Dan Williams6f231dd2011-07-02 22:56:22 -0700293 if (status != SCI_SUCCESS)
294 goto errout;
295 }
296
Dan Williamsa1a113b2011-04-21 18:44:45 -0700297 if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP)) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700298 isci_tmf->proto = SAS_PROTOCOL_SATA;
Dan Williams0d0cf142011-06-13 00:51:30 -0700299 status = isci_sata_management_task_request_build(ireq);
Dan Williams6f231dd2011-07-02 22:56:22 -0700300
301 if (status != SCI_SUCCESS)
302 goto errout;
303 }
Dan Williams0d0cf142011-06-13 00:51:30 -0700304 return ireq;
Dan Williams6f231dd2011-07-02 22:56:22 -0700305 errout:
Dan Williams0d0cf142011-06-13 00:51:30 -0700306 isci_request_free(ihost, ireq);
307 ireq = NULL;
308 return ireq;
Dan Williams6f231dd2011-07-02 22:56:22 -0700309}
310
Dan Williams209fae12011-06-13 17:39:44 -0700311int isci_task_execute_tmf(struct isci_host *ihost,
312 struct isci_remote_device *isci_device,
313 struct isci_tmf *tmf, unsigned long timeout_ms)
Dan Williams6f231dd2011-07-02 22:56:22 -0700314{
315 DECLARE_COMPLETION_ONSTACK(completion);
Bartosz Barcinski467e8552011-04-12 17:28:41 -0700316 enum sci_task_status status = SCI_TASK_FAILURE;
Dan Williams6f231dd2011-07-02 22:56:22 -0700317 struct scic_sds_remote_device *sci_device;
Dan Williams0d0cf142011-06-13 00:51:30 -0700318 struct isci_request *ireq;
Dan Williams6f231dd2011-07-02 22:56:22 -0700319 int ret = TMF_RESP_FUNC_FAILED;
320 unsigned long flags;
Edmund Nadolskifd183882011-05-19 12:00:15 +0000321 unsigned long timeleft;
Dan Williams6f231dd2011-07-02 22:56:22 -0700322
323 /* sanity check, return TMF_RESP_FUNC_FAILED
324 * if the device is not there and ready.
325 */
Dan Williams8acaec12011-03-07 14:47:35 -0800326 if (!isci_device || isci_device->status != isci_ready_for_io) {
Dan Williams0d0cf142011-06-13 00:51:30 -0700327 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -0700328 "%s: isci_device = %p not ready (%d)\n",
329 __func__,
Dan Williams8acaec12011-03-07 14:47:35 -0800330 isci_device, isci_device->status);
Dan Williams6f231dd2011-07-02 22:56:22 -0700331 return TMF_RESP_FUNC_FAILED;
332 } else
Dan Williams0d0cf142011-06-13 00:51:30 -0700333 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -0700334 "%s: isci_device = %p\n",
335 __func__, isci_device);
336
Dan Williams57f20f42011-04-21 18:14:45 -0700337 sci_device = &isci_device->sci;
Dan Williams6f231dd2011-07-02 22:56:22 -0700338
339 /* Assign the pointer to the TMF's completion kernel wait structure. */
340 tmf->complete = &completion;
341
Dan Williams209fae12011-06-13 17:39:44 -0700342 ireq = isci_task_request_build(ihost, isci_device, tmf);
Dan Williams0d0cf142011-06-13 00:51:30 -0700343 if (!ireq) {
344 dev_warn(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -0700345 "%s: isci_task_request_build failed\n",
346 __func__);
347 return TMF_RESP_FUNC_FAILED;
348 }
349
Dan Williams0d0cf142011-06-13 00:51:30 -0700350 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700351
352 /* start the TMF io. */
353 status = scic_controller_start_task(
Dan Williams0d0cf142011-06-13 00:51:30 -0700354 &ihost->sci,
Dan Williams6f231dd2011-07-02 22:56:22 -0700355 sci_device,
Dan Williams0d0cf142011-06-13 00:51:30 -0700356 &ireq->sci,
Dan Williams67ea8382011-05-08 11:47:15 -0700357 SCI_CONTROLLER_INVALID_IO_TAG);
Dan Williams6f231dd2011-07-02 22:56:22 -0700358
Bartosz Barcinski467e8552011-04-12 17:28:41 -0700359 if (status != SCI_TASK_SUCCESS) {
Dan Williams0d0cf142011-06-13 00:51:30 -0700360 dev_warn(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -0700361 "%s: start_io failed - status = 0x%x, request = %p\n",
362 __func__,
363 status,
Dan Williams0d0cf142011-06-13 00:51:30 -0700364 ireq);
365 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700366 goto cleanup_request;
367 }
368
Dan Williams6f231dd2011-07-02 22:56:22 -0700369 if (tmf->cb_state_func != NULL)
370 tmf->cb_state_func(isci_tmf_started, tmf, tmf->cb_data);
371
Dan Williams0d0cf142011-06-13 00:51:30 -0700372 isci_request_change_state(ireq, started);
Dan Williams6f231dd2011-07-02 22:56:22 -0700373
374 /* add the request to the remote device request list. */
Dan Williams0d0cf142011-06-13 00:51:30 -0700375 list_add(&ireq->dev_node, &isci_device->reqs_in_process);
Dan Williams6f231dd2011-07-02 22:56:22 -0700376
Dan Williams0d0cf142011-06-13 00:51:30 -0700377 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700378
379 /* Wait for the TMF to complete, or a timeout. */
Edmund Nadolskifd183882011-05-19 12:00:15 +0000380 timeleft = wait_for_completion_timeout(&completion,
381 jiffies + msecs_to_jiffies(timeout_ms));
382
383 if (timeleft == 0) {
Dan Williams0d0cf142011-06-13 00:51:30 -0700384 spin_lock_irqsave(&ihost->scic_lock, flags);
Edmund Nadolskifd183882011-05-19 12:00:15 +0000385
386 if (tmf->cb_state_func != NULL)
387 tmf->cb_state_func(isci_tmf_timed_out, tmf, tmf->cb_data);
388
Dan Williams209fae12011-06-13 17:39:44 -0700389 status = scic_controller_terminate_request(&ihost->sci,
390 &isci_device->sci,
391 &ireq->sci);
Edmund Nadolskifd183882011-05-19 12:00:15 +0000392
Dan Williams0d0cf142011-06-13 00:51:30 -0700393 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Edmund Nadolskifd183882011-05-19 12:00:15 +0000394 }
Dan Williams6f231dd2011-07-02 22:56:22 -0700395
396 isci_print_tmf(tmf);
397
398 if (tmf->status == SCI_SUCCESS)
399 ret = TMF_RESP_FUNC_COMPLETE;
400 else if (tmf->status == SCI_FAILURE_IO_RESPONSE_VALID) {
Dan Williams0d0cf142011-06-13 00:51:30 -0700401 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -0700402 "%s: tmf.status == "
403 "SCI_FAILURE_IO_RESPONSE_VALID\n",
404 __func__);
405 ret = TMF_RESP_FUNC_COMPLETE;
406 }
407 /* Else - leave the default "failed" status alone. */
408
Dan Williams0d0cf142011-06-13 00:51:30 -0700409 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -0700410 "%s: completed request = %p\n",
411 __func__,
Dan Williams0d0cf142011-06-13 00:51:30 -0700412 ireq);
Dan Williams6f231dd2011-07-02 22:56:22 -0700413
Dan Williams0d0cf142011-06-13 00:51:30 -0700414 if (ireq->io_request_completion != NULL) {
Edmund Nadolskifd183882011-05-19 12:00:15 +0000415 /* A thread is waiting for this TMF to finish. */
Dan Williams0d0cf142011-06-13 00:51:30 -0700416 complete(ireq->io_request_completion);
Dan Williams6f231dd2011-07-02 22:56:22 -0700417 }
418
Dan Williams6f231dd2011-07-02 22:56:22 -0700419 cleanup_request:
Dan Williams0d0cf142011-06-13 00:51:30 -0700420 isci_request_free(ihost, ireq);
Dan Williams6f231dd2011-07-02 22:56:22 -0700421 return ret;
422}
423
424void isci_task_build_tmf(
425 struct isci_tmf *tmf,
Dan Williams6f231dd2011-07-02 22:56:22 -0700426 enum isci_tmf_function_codes code,
427 void (*tmf_sent_cb)(enum isci_tmf_cb_state,
428 struct isci_tmf *,
429 void *),
Jeff Skirvinc3f42fe2011-03-04 14:06:56 -0800430 void *cb_data)
Dan Williams6f231dd2011-07-02 22:56:22 -0700431{
Dan Williams6f231dd2011-07-02 22:56:22 -0700432 memset(tmf, 0, sizeof(*tmf));
433
Dan Williams6f231dd2011-07-02 22:56:22 -0700434 tmf->tmf_code = code;
Dan Williams6f231dd2011-07-02 22:56:22 -0700435 tmf->cb_state_func = tmf_sent_cb;
Jeff Skirvinc3f42fe2011-03-04 14:06:56 -0800436 tmf->cb_data = cb_data;
437}
Jeff Skirvin1fad9e92011-03-04 14:06:46 -0800438
Dan Williams35173d52011-03-26 16:43:01 -0700439static void isci_task_build_abort_task_tmf(
Jeff Skirvinc3f42fe2011-03-04 14:06:56 -0800440 struct isci_tmf *tmf,
Jeff Skirvinc3f42fe2011-03-04 14:06:56 -0800441 enum isci_tmf_function_codes code,
442 void (*tmf_sent_cb)(enum isci_tmf_cb_state,
443 struct isci_tmf *,
444 void *),
445 struct isci_request *old_request)
446{
Dan Williams209fae12011-06-13 17:39:44 -0700447 isci_task_build_tmf(tmf, code, tmf_sent_cb,
Jeff Skirvinc3f42fe2011-03-04 14:06:56 -0800448 (void *)old_request);
449 tmf->io_tag = old_request->io_tag;
Dan Williams6f231dd2011-07-02 22:56:22 -0700450}
451
Dan Williams6f231dd2011-07-02 22:56:22 -0700452/**
453 * isci_task_validate_request_to_abort() - This function checks the given I/O
454 * against the "started" state. If the request is still "started", it's
455 * state is changed to aborted. NOTE: isci_host->scic_lock MUST BE HELD
456 * BEFORE CALLING THIS FUNCTION.
457 * @isci_request: This parameter specifies the request object to control.
458 * @isci_host: This parameter specifies the ISCI host object
459 * @isci_device: This is the device to which the request is pending.
460 * @aborted_io_completion: This is a completion structure that will be added to
461 * the request in case it is changed to aborting; this completion is
462 * triggered when the request is fully completed.
463 *
464 * Either "started" on successful change of the task status to "aborted", or
465 * "unallocated" if the task cannot be controlled.
466 */
467static enum isci_request_status isci_task_validate_request_to_abort(
468 struct isci_request *isci_request,
469 struct isci_host *isci_host,
470 struct isci_remote_device *isci_device,
471 struct completion *aborted_io_completion)
472{
473 enum isci_request_status old_state = unallocated;
474
475 /* Only abort the task if it's in the
476 * device's request_in_process list
477 */
478 if (isci_request && !list_empty(&isci_request->dev_node)) {
479 old_state = isci_request_change_started_to_aborted(
480 isci_request, aborted_io_completion);
481
Dan Williams6f231dd2011-07-02 22:56:22 -0700482 }
483
484 return old_state;
485}
486
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700487/**
488* isci_request_cleanup_completed_loiterer() - This function will take care of
489* the final cleanup on any request which has been explicitly terminated.
490* @isci_host: This parameter specifies the ISCI host object
491* @isci_device: This is the device to which the request is pending.
492* @isci_request: This parameter specifies the terminated request object.
493* @task: This parameter is the libsas I/O request.
494*/
Dan Williams6f231dd2011-07-02 22:56:22 -0700495static void isci_request_cleanup_completed_loiterer(
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700496 struct isci_host *isci_host,
Dan Williams6f231dd2011-07-02 22:56:22 -0700497 struct isci_remote_device *isci_device,
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700498 struct isci_request *isci_request,
499 struct sas_task *task)
Dan Williams6f231dd2011-07-02 22:56:22 -0700500{
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700501 unsigned long flags;
Dan Williams6f231dd2011-07-02 22:56:22 -0700502
503 dev_dbg(&isci_host->pdev->dev,
504 "%s: isci_device=%p, request=%p, task=%p\n",
Jeff Skirvin18d3d722011-03-04 14:06:38 -0800505 __func__, isci_device, isci_request, task);
Dan Williams6f231dd2011-07-02 22:56:22 -0700506
Jeff Skirvina5fde222011-03-04 14:06:42 -0800507 if (task != NULL) {
508
509 spin_lock_irqsave(&task->task_state_lock, flags);
510 task->lldd_task = NULL;
511
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700512 task->task_state_flags &= ~SAS_TASK_NEED_DEV_RESET;
513
Jeff Skirvina5fde222011-03-04 14:06:42 -0800514 isci_set_task_doneflags(task);
515
516 /* If this task is not in the abort path, call task_done. */
517 if (!(task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
518
519 spin_unlock_irqrestore(&task->task_state_lock, flags);
520 task->task_done(task);
521 } else
522 spin_unlock_irqrestore(&task->task_state_lock, flags);
523 }
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800524
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700525 if (isci_request != NULL) {
526 spin_lock_irqsave(&isci_host->scic_lock, flags);
527 list_del_init(&isci_request->dev_node);
528 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800529
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700530 isci_request_free(isci_host, isci_request);
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800531 }
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800532}
533
Dan Williams6f231dd2011-07-02 22:56:22 -0700534/**
535 * isci_terminate_request_core() - This function will terminate the given
536 * request, and wait for it to complete. This function must only be called
537 * from a thread that can wait. Note that the request is terminated and
538 * completed (back to the host, if started there).
539 * @isci_host: This SCU.
540 * @isci_device: The target.
541 * @isci_request: The I/O request to be terminated.
542 *
Dan Williams6f231dd2011-07-02 22:56:22 -0700543 */
544static void isci_terminate_request_core(
545 struct isci_host *isci_host,
546 struct isci_remote_device *isci_device,
Jeff Skirvincbb65c62011-03-04 14:06:50 -0800547 struct isci_request *isci_request)
Dan Williams6f231dd2011-07-02 22:56:22 -0700548{
Jeff Skirvincbb65c62011-03-04 14:06:50 -0800549 enum sci_status status = SCI_SUCCESS;
Dan Williams6f231dd2011-07-02 22:56:22 -0700550 bool was_terminated = false;
551 bool needs_cleanup_handling = false;
552 enum isci_request_status request_status;
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700553 unsigned long flags;
554 unsigned long termination_completed = 1;
555 struct completion *io_request_completion;
556 struct sas_task *task;
Dan Williams6f231dd2011-07-02 22:56:22 -0700557
558 dev_dbg(&isci_host->pdev->dev,
559 "%s: device = %p; request = %p\n",
560 __func__, isci_device, isci_request);
561
Dan Williams6f231dd2011-07-02 22:56:22 -0700562 spin_lock_irqsave(&isci_host->scic_lock, flags);
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800563
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700564 io_request_completion = isci_request->io_request_completion;
565
566 task = (isci_request->ttype == io_task)
567 ? isci_request_access_task(isci_request)
568 : NULL;
569
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800570 /* Note that we are not going to control
571 * the target to abort the request.
572 */
573 isci_request->complete_in_target = true;
574
Dan Williams6f231dd2011-07-02 22:56:22 -0700575 /* Make sure the request wasn't just sitting around signalling
576 * device condition (if the request handle is NULL, then the
577 * request completed but needed additional handling here).
578 */
Dan Williams67ea8382011-05-08 11:47:15 -0700579 if (!isci_request->terminated) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700580 was_terminated = true;
Jeff Skirvincbb65c62011-03-04 14:06:50 -0800581 needs_cleanup_handling = true;
Dan Williams6f231dd2011-07-02 22:56:22 -0700582 status = scic_controller_terminate_request(
Artur Wojcikcc3dbd02011-05-04 07:58:16 +0000583 &isci_host->sci,
Dan Williams57f20f42011-04-21 18:14:45 -0700584 &isci_device->sci,
Dan Williams67ea8382011-05-08 11:47:15 -0700585 &isci_request->sci);
Dan Williams6f231dd2011-07-02 22:56:22 -0700586 }
587 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
588
589 /*
590 * The only time the request to terminate will
591 * fail is when the io request is completed and
592 * being aborted.
593 */
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800594 if (status != SCI_SUCCESS) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700595 dev_err(&isci_host->pdev->dev,
596 "%s: scic_controller_terminate_request"
597 " returned = 0x%x\n",
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700598 __func__, status);
599
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800600 isci_request->io_request_completion = NULL;
601
602 } else {
Dan Williams6f231dd2011-07-02 22:56:22 -0700603 if (was_terminated) {
604 dev_dbg(&isci_host->pdev->dev,
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700605 "%s: before completion wait (%p/%p)\n",
606 __func__, isci_request, io_request_completion);
Dan Williams6f231dd2011-07-02 22:56:22 -0700607
608 /* Wait here for the request to complete. */
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700609 #define TERMINATION_TIMEOUT_MSEC 500
610 termination_completed
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800611 = wait_for_completion_timeout(
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700612 io_request_completion,
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800613 msecs_to_jiffies(TERMINATION_TIMEOUT_MSEC));
Dan Williams6f231dd2011-07-02 22:56:22 -0700614
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700615 if (!termination_completed) {
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800616
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700617 /* The request to terminate has timed out. */
618 spin_lock_irqsave(&isci_host->scic_lock,
619 flags);
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800620
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700621 /* Check for state changes. */
622 if (!isci_request->terminated) {
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800623
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700624 /* The best we can do is to have the
625 * request die a silent death if it
626 * ever really completes.
627 *
628 * Set the request state to "dead",
629 * and clear the task pointer so that
630 * an actual completion event callback
631 * doesn't do anything.
632 */
633 isci_request->status = dead;
634 isci_request->io_request_completion
635 = NULL;
636
637 if (isci_request->ttype == io_task) {
638
639 /* Break links with the
640 * sas_task.
641 */
642 isci_request->ttype_ptr.io_task_ptr
643 = NULL;
644 }
645 } else
646 termination_completed = 1;
647
648 spin_unlock_irqrestore(&isci_host->scic_lock,
649 flags);
650
651 if (!termination_completed) {
652
653 dev_err(&isci_host->pdev->dev,
654 "%s: *** Timeout waiting for "
655 "termination(%p/%p)\n",
656 __func__, io_request_completion,
657 isci_request);
658
659 /* The request can no longer be referenced
660 * safely since it may go away if the
661 * termination every really does complete.
662 */
663 isci_request = NULL;
664 }
665 }
666 if (termination_completed)
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800667 dev_dbg(&isci_host->pdev->dev,
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700668 "%s: after completion wait (%p/%p)\n",
669 __func__, isci_request, io_request_completion);
Dan Williams6f231dd2011-07-02 22:56:22 -0700670 }
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800671
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700672 if (termination_completed) {
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800673
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700674 isci_request->io_request_completion = NULL;
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800675
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700676 /* Peek at the status of the request. This will tell
677 * us if there was special handling on the request such that it
678 * needs to be detached and freed here.
679 */
680 spin_lock_irqsave(&isci_request->state_lock, flags);
681 request_status = isci_request_get_state(isci_request);
682
683 if ((isci_request->ttype == io_task) /* TMFs are in their own thread */
684 && ((request_status == aborted)
685 || (request_status == aborting)
686 || (request_status == terminating)
687 || (request_status == completed)
688 || (request_status == dead)
689 )
690 ) {
691
692 /* The completion routine won't free a request in
693 * the aborted/aborting/etc. states, so we do
694 * it here.
695 */
696 needs_cleanup_handling = true;
697 }
698 spin_unlock_irqrestore(&isci_request->state_lock, flags);
699
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800700 }
Dan Williams6f231dd2011-07-02 22:56:22 -0700701 if (needs_cleanup_handling)
702 isci_request_cleanup_completed_loiterer(
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700703 isci_host, isci_device, isci_request, task);
Jeff Skirvina5fde222011-03-04 14:06:42 -0800704 }
Dan Williams6f231dd2011-07-02 22:56:22 -0700705}
706
707/**
708 * isci_terminate_pending_requests() - This function will change the all of the
709 * requests on the given device's state to "aborting", will terminate the
710 * requests, and wait for them to complete. This function must only be
711 * called from a thread that can wait. Note that the requests are all
712 * terminated and completed (back to the host, if started there).
713 * @isci_host: This parameter specifies SCU.
714 * @isci_device: This parameter specifies the target.
715 *
Dan Williams6f231dd2011-07-02 22:56:22 -0700716 */
Dan Williams980d3ae2011-06-20 15:11:22 -0700717void isci_terminate_pending_requests(struct isci_host *ihost,
718 struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -0700719{
Dan Williams980d3ae2011-06-20 15:11:22 -0700720 struct completion request_completion;
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700721 enum isci_request_status old_state;
Dan Williams980d3ae2011-06-20 15:11:22 -0700722 unsigned long flags;
723 LIST_HEAD(list);
Dan Williams6f231dd2011-07-02 22:56:22 -0700724
Dan Williams980d3ae2011-06-20 15:11:22 -0700725 spin_lock_irqsave(&ihost->scic_lock, flags);
726 list_splice_init(&idev->reqs_in_process, &list);
Dan Williams6f231dd2011-07-02 22:56:22 -0700727
Dan Williams980d3ae2011-06-20 15:11:22 -0700728 /* assumes that isci_terminate_request_core deletes from the list */
729 while (!list_empty(&list)) {
730 struct isci_request *ireq = list_entry(list.next, typeof(*ireq), dev_node);
Dan Williams6f231dd2011-07-02 22:56:22 -0700731
Dan Williams980d3ae2011-06-20 15:11:22 -0700732 /* Change state to "terminating" if it is currently
733 * "started".
734 */
735 old_state = isci_request_change_started_to_newstate(ireq,
736 &request_completion,
737 terminating);
738 switch (old_state) {
739 case started:
740 case completed:
741 case aborting:
742 break;
743 default:
744 /* termination in progress, or otherwise dispositioned.
745 * We know the request was on 'list' so should be safe
746 * to move it back to reqs_in_process
747 */
748 list_move(&ireq->dev_node, &idev->reqs_in_process);
749 ireq = NULL;
750 break;
751 }
752
753 if (!ireq)
754 continue;
755 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800756
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700757 init_completion(&request_completion);
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800758
Dan Williams980d3ae2011-06-20 15:11:22 -0700759 dev_dbg(&ihost->pdev->dev,
760 "%s: idev=%p request=%p; task=%p old_state=%d\n",
761 __func__, idev, ireq,
762 ireq->ttype == io_task ? isci_request_access_task(ireq) : NULL,
763 old_state);
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700764
Dan Williams980d3ae2011-06-20 15:11:22 -0700765 /* If the old_state is started:
766 * This request was not already being aborted. If it had been,
767 * then the aborting I/O (ie. the TMF request) would not be in
768 * the aborting state, and thus would be terminated here. Note
769 * that since the TMF completion's call to the kernel function
770 * "complete()" does not happen until the pending I/O request
771 * terminate fully completes, we do not have to implement a
772 * special wait here for already aborting requests - the
773 * termination of the TMF request will force the request
774 * to finish it's already started terminate.
775 *
776 * If old_state == completed:
777 * This request completed from the SCU hardware perspective
778 * and now just needs cleaning up in terms of freeing the
779 * request and potentially calling up to libsas.
780 *
781 * If old_state == aborting:
782 * This request has already gone through a TMF timeout, but may
783 * not have been terminated; needs cleaning up at least.
784 */
785 isci_terminate_request_core(ihost, idev, ireq);
786 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700787 }
Dan Williams980d3ae2011-06-20 15:11:22 -0700788 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700789}
790
791/**
792 * isci_task_send_lu_reset_sas() - This function is called by of the SAS Domain
793 * Template functions.
794 * @lun: This parameter specifies the lun to be reset.
795 *
796 * status, zero indicates success.
797 */
798static int isci_task_send_lu_reset_sas(
799 struct isci_host *isci_host,
800 struct isci_remote_device *isci_device,
801 u8 *lun)
802{
803 struct isci_tmf tmf;
804 int ret = TMF_RESP_FUNC_FAILED;
805
806 dev_dbg(&isci_host->pdev->dev,
807 "%s: isci_host = %p, isci_device = %p\n",
808 __func__, isci_host, isci_device);
809 /* Send the LUN reset to the target. By the time the call returns,
810 * the TMF has fully exected in the target (in which case the return
811 * value is "TMF_RESP_FUNC_COMPLETE", or the request timed-out (or
812 * was otherwise unable to be executed ("TMF_RESP_FUNC_FAILED").
813 */
Dan Williams209fae12011-06-13 17:39:44 -0700814 isci_task_build_tmf(&tmf, isci_tmf_ssp_lun_reset, NULL, NULL);
Dan Williams6f231dd2011-07-02 22:56:22 -0700815
816 #define ISCI_LU_RESET_TIMEOUT_MS 2000 /* 2 second timeout. */
Dan Williams209fae12011-06-13 17:39:44 -0700817 ret = isci_task_execute_tmf(isci_host, isci_device, &tmf, ISCI_LU_RESET_TIMEOUT_MS);
Dan Williams6f231dd2011-07-02 22:56:22 -0700818
819 if (ret == TMF_RESP_FUNC_COMPLETE)
820 dev_dbg(&isci_host->pdev->dev,
821 "%s: %p: TMF_LU_RESET passed\n",
822 __func__, isci_device);
823 else
824 dev_dbg(&isci_host->pdev->dev,
825 "%s: %p: TMF_LU_RESET failed (%x)\n",
826 __func__, isci_device, ret);
827
828 return ret;
829}
830
831/**
832 * isci_task_lu_reset() - This function is one of the SAS Domain Template
833 * functions. This is one of the Task Management functoins called by libsas,
834 * to reset the given lun. Note the assumption that while this call is
835 * executing, no I/O will be sent by the host to the device.
836 * @lun: This parameter specifies the lun to be reset.
837 *
838 * status, zero indicates success.
839 */
Dan Williams4393aa42011-03-31 13:10:44 -0700840int isci_task_lu_reset(struct domain_device *domain_device, u8 *lun)
Dan Williams6f231dd2011-07-02 22:56:22 -0700841{
Dan Williams4393aa42011-03-31 13:10:44 -0700842 struct isci_host *isci_host = dev_to_ihost(domain_device);
Dan Williams209fae12011-06-13 17:39:44 -0700843 struct isci_remote_device *isci_device;
844 unsigned long flags;
Dan Williams6f231dd2011-07-02 22:56:22 -0700845 int ret;
Dan Williams6f231dd2011-07-02 22:56:22 -0700846
Dan Williams209fae12011-06-13 17:39:44 -0700847 spin_lock_irqsave(&isci_host->scic_lock, flags);
848 isci_device = isci_lookup_device(domain_device);
849 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700850
Dan Williams4393aa42011-03-31 13:10:44 -0700851 dev_dbg(&isci_host->pdev->dev,
852 "%s: domain_device=%p, isci_host=%p; isci_device=%p\n",
Dan Williams6f231dd2011-07-02 22:56:22 -0700853 __func__, domain_device, isci_host, isci_device);
854
Dan Williams209fae12011-06-13 17:39:44 -0700855 if (isci_device)
Dan Williamsd06b4872011-05-02 13:59:25 -0700856 set_bit(IDEV_EH, &isci_device->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700857
858 /* If there is a device reset pending on any request in the
859 * device's list, fail this LUN reset request in order to
860 * escalate to the device reset.
861 */
Dan Williams209fae12011-06-13 17:39:44 -0700862 if (!isci_device ||
Dan Williams4393aa42011-03-31 13:10:44 -0700863 isci_device_is_reset_pending(isci_host, isci_device)) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700864 dev_warn(&isci_host->pdev->dev,
Dan Williams4393aa42011-03-31 13:10:44 -0700865 "%s: No dev (%p), or "
Dan Williams6f231dd2011-07-02 22:56:22 -0700866 "RESET PENDING: domain_device=%p\n",
Dan Williams4393aa42011-03-31 13:10:44 -0700867 __func__, isci_device, domain_device);
Dan Williams209fae12011-06-13 17:39:44 -0700868 ret = TMF_RESP_FUNC_FAILED;
869 goto out;
Dan Williams6f231dd2011-07-02 22:56:22 -0700870 }
871
Dan Williams6f231dd2011-07-02 22:56:22 -0700872 /* Send the task management part of the reset. */
873 if (sas_protocol_ata(domain_device->tproto)) {
Dan Williams4393aa42011-03-31 13:10:44 -0700874 ret = isci_task_send_lu_reset_sata(isci_host, isci_device, lun);
Dan Williams6f231dd2011-07-02 22:56:22 -0700875 } else
876 ret = isci_task_send_lu_reset_sas(isci_host, isci_device, lun);
877
878 /* If the LUN reset worked, all the I/O can now be terminated. */
879 if (ret == TMF_RESP_FUNC_COMPLETE)
880 /* Terminate all I/O now. */
881 isci_terminate_pending_requests(isci_host,
Dan Williams980d3ae2011-06-20 15:11:22 -0700882 isci_device);
Dan Williams6f231dd2011-07-02 22:56:22 -0700883
Dan Williams209fae12011-06-13 17:39:44 -0700884 out:
885 isci_put_device(isci_device);
Dan Williams6f231dd2011-07-02 22:56:22 -0700886 return ret;
887}
888
889
890/* int (*lldd_clear_nexus_port)(struct asd_sas_port *); */
891int isci_task_clear_nexus_port(struct asd_sas_port *port)
892{
893 return TMF_RESP_FUNC_FAILED;
894}
895
896
897
898int isci_task_clear_nexus_ha(struct sas_ha_struct *ha)
899{
900 return TMF_RESP_FUNC_FAILED;
901}
902
Dan Williams6f231dd2011-07-02 22:56:22 -0700903/* Task Management Functions. Must be called from process context. */
904
905/**
906 * isci_abort_task_process_cb() - This is a helper function for the abort task
907 * TMF command. It manages the request state with respect to the successful
908 * transmission / completion of the abort task request.
909 * @cb_state: This parameter specifies when this function was called - after
910 * the TMF request has been started and after it has timed-out.
911 * @tmf: This parameter specifies the TMF in progress.
912 *
913 *
914 */
915static void isci_abort_task_process_cb(
916 enum isci_tmf_cb_state cb_state,
917 struct isci_tmf *tmf,
918 void *cb_data)
919{
920 struct isci_request *old_request;
921
922 old_request = (struct isci_request *)cb_data;
923
924 dev_dbg(&old_request->isci_host->pdev->dev,
925 "%s: tmf=%p, old_request=%p\n",
926 __func__, tmf, old_request);
927
928 switch (cb_state) {
929
930 case isci_tmf_started:
931 /* The TMF has been started. Nothing to do here, since the
932 * request state was already set to "aborted" by the abort
933 * task function.
934 */
Bartosz Barcinski6cb4d6b2011-04-12 17:28:43 -0700935 if ((old_request->status != aborted)
936 && (old_request->status != completed))
937 dev_err(&old_request->isci_host->pdev->dev,
938 "%s: Bad request status (%d): tmf=%p, old_request=%p\n",
939 __func__, old_request->status, tmf, old_request);
Dan Williams6f231dd2011-07-02 22:56:22 -0700940 break;
941
942 case isci_tmf_timed_out:
943
944 /* Set the task's state to "aborting", since the abort task
945 * function thread set it to "aborted" (above) in anticipation
946 * of the task management request working correctly. Since the
947 * timeout has now fired, the TMF request failed. We set the
948 * state such that the request completion will indicate the
949 * device is no longer present.
950 */
951 isci_request_change_state(old_request, aborting);
952 break;
953
954 default:
955 dev_err(&old_request->isci_host->pdev->dev,
956 "%s: Bad cb_state (%d): tmf=%p, old_request=%p\n",
957 __func__, cb_state, tmf, old_request);
958 break;
959 }
960}
961
962/**
963 * isci_task_abort_task() - This function is one of the SAS Domain Template
964 * functions. This function is called by libsas to abort a specified task.
965 * @task: This parameter specifies the SAS task to abort.
966 *
967 * status, zero indicates success.
968 */
969int isci_task_abort_task(struct sas_task *task)
970{
Dan Williams4393aa42011-03-31 13:10:44 -0700971 struct isci_host *isci_host = dev_to_ihost(task->dev);
Dan Williams6f231dd2011-07-02 22:56:22 -0700972 DECLARE_COMPLETION_ONSTACK(aborted_io_completion);
Jeff Skirvina5fde222011-03-04 14:06:42 -0800973 struct isci_request *old_request = NULL;
974 enum isci_request_status old_state;
Dan Williams6f231dd2011-07-02 22:56:22 -0700975 struct isci_remote_device *isci_device = NULL;
Jeff Skirvina5fde222011-03-04 14:06:42 -0800976 struct isci_tmf tmf;
977 int ret = TMF_RESP_FUNC_FAILED;
978 unsigned long flags;
979 bool any_dev_reset = false;
Dan Williams6f231dd2011-07-02 22:56:22 -0700980
981 /* Get the isci_request reference from the task. Note that
982 * this check does not depend on the pending request list
983 * in the device, because tasks driving resets may land here
984 * after completion in the core.
985 */
Dan Williams209fae12011-06-13 17:39:44 -0700986 spin_lock_irqsave(&isci_host->scic_lock, flags);
987 spin_lock(&task->task_state_lock);
988
989 old_request = task->lldd_task;
990
991 /* If task is already done, the request isn't valid */
992 if (!(task->task_state_flags & SAS_TASK_STATE_DONE) &&
993 (task->task_state_flags & SAS_TASK_AT_INITIATOR) &&
994 old_request)
995 isci_device = isci_lookup_device(task->dev);
996
997 spin_unlock(&task->task_state_lock);
998 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700999
1000 dev_dbg(&isci_host->pdev->dev,
1001 "%s: task = %p\n", __func__, task);
1002
Dan Williams209fae12011-06-13 17:39:44 -07001003 if (!isci_device || !old_request)
1004 goto out;
Dan Williams6f231dd2011-07-02 22:56:22 -07001005
Dan Williamsd06b4872011-05-02 13:59:25 -07001006 set_bit(IDEV_EH, &isci_device->flags);
1007
Dan Williams6f231dd2011-07-02 22:56:22 -07001008 /* This version of the driver will fail abort requests for
1009 * SATA/STP. Failing the abort request this way will cause the
1010 * SCSI error handler thread to escalate to LUN reset
1011 */
Dan Williams209fae12011-06-13 17:39:44 -07001012 if (sas_protocol_ata(task->task_proto)) {
Dan Williams6f231dd2011-07-02 22:56:22 -07001013 dev_warn(&isci_host->pdev->dev,
1014 " task %p is for a STP/SATA device;"
1015 " returning TMF_RESP_FUNC_FAILED\n"
1016 " to cause a LUN reset...\n", task);
Dan Williams209fae12011-06-13 17:39:44 -07001017 goto out;
Dan Williams6f231dd2011-07-02 22:56:22 -07001018 }
1019
1020 dev_dbg(&isci_host->pdev->dev,
1021 "%s: old_request == %p\n", __func__, old_request);
1022
Dan Williams209fae12011-06-13 17:39:44 -07001023 any_dev_reset = isci_device_is_reset_pending(isci_host,isci_device);
Jeff Skirvina5fde222011-03-04 14:06:42 -08001024
Dan Williams6f231dd2011-07-02 22:56:22 -07001025 spin_lock_irqsave(&task->task_state_lock, flags);
1026
Dan Williams209fae12011-06-13 17:39:44 -07001027 any_dev_reset = any_dev_reset || (task->task_state_flags & SAS_TASK_NEED_DEV_RESET);
Dan Williams6f231dd2011-07-02 22:56:22 -07001028
1029 /* If the extraction of the request reference from the task
1030 * failed, then the request has been completed (or if there is a
1031 * pending reset then this abort request function must be failed
1032 * in order to escalate to the target reset).
1033 */
Jeff Skirvina5fde222011-03-04 14:06:42 -08001034 if ((old_request == NULL) || any_dev_reset) {
Dan Williams6f231dd2011-07-02 22:56:22 -07001035
1036 /* If the device reset task flag is set, fail the task
1037 * management request. Otherwise, the original request
1038 * has completed.
1039 */
1040 if (any_dev_reset) {
1041
1042 /* Turn off the task's DONE to make sure this
1043 * task is escalated to a target reset.
1044 */
1045 task->task_state_flags &= ~SAS_TASK_STATE_DONE;
1046
Jeff Skirvina5fde222011-03-04 14:06:42 -08001047 /* Make the reset happen as soon as possible. */
1048 task->task_state_flags |= SAS_TASK_NEED_DEV_RESET;
1049
1050 spin_unlock_irqrestore(&task->task_state_lock, flags);
1051
Dan Williams6f231dd2011-07-02 22:56:22 -07001052 /* Fail the task management request in order to
1053 * escalate to the target reset.
1054 */
1055 ret = TMF_RESP_FUNC_FAILED;
1056
1057 dev_dbg(&isci_host->pdev->dev,
1058 "%s: Failing task abort in order to "
1059 "escalate to target reset because\n"
1060 "SAS_TASK_NEED_DEV_RESET is set for "
1061 "task %p on dev %p\n",
1062 __func__, task, isci_device);
1063
Jeff Skirvina5fde222011-03-04 14:06:42 -08001064
Dan Williams6f231dd2011-07-02 22:56:22 -07001065 } else {
Dan Williams6f231dd2011-07-02 22:56:22 -07001066 /* The request has already completed and there
1067 * is nothing to do here other than to set the task
1068 * done bit, and indicate that the task abort function
1069 * was sucessful.
1070 */
1071 isci_set_task_doneflags(task);
1072
Jeff Skirvina5fde222011-03-04 14:06:42 -08001073 spin_unlock_irqrestore(&task->task_state_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001074
Jeff Skirvina5fde222011-03-04 14:06:42 -08001075 ret = TMF_RESP_FUNC_COMPLETE;
Dan Williams6f231dd2011-07-02 22:56:22 -07001076
Jeff Skirvina5fde222011-03-04 14:06:42 -08001077 dev_dbg(&isci_host->pdev->dev,
1078 "%s: abort task not needed for %p\n",
1079 __func__, task);
Dan Williams6f231dd2011-07-02 22:56:22 -07001080 }
Dan Williams209fae12011-06-13 17:39:44 -07001081 goto out;
Dan Williams6f231dd2011-07-02 22:56:22 -07001082 }
Jeff Skirvina5fde222011-03-04 14:06:42 -08001083 else
1084 spin_unlock_irqrestore(&task->task_state_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001085
1086 spin_lock_irqsave(&isci_host->scic_lock, flags);
1087
Jeff Skirvinf219f012011-03-31 13:10:34 -07001088 /* Check the request status and change to "aborted" if currently
Jeff Skirvina5fde222011-03-04 14:06:42 -08001089 * "starting"; if true then set the I/O kernel completion
Dan Williams6f231dd2011-07-02 22:56:22 -07001090 * struct that will be triggered when the request completes.
1091 */
Jeff Skirvina5fde222011-03-04 14:06:42 -08001092 old_state = isci_task_validate_request_to_abort(
1093 old_request, isci_host, isci_device,
1094 &aborted_io_completion);
Jeff Skirvinf219f012011-03-31 13:10:34 -07001095 if ((old_state != started) &&
1096 (old_state != completed) &&
1097 (old_state != aborting)) {
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 /* The request was already being handled by someone else (because
1102 * they got to set the state away from started).
1103 */
1104 dev_dbg(&isci_host->pdev->dev,
1105 "%s: device = %p; old_request %p already being aborted\n",
1106 __func__,
1107 isci_device, old_request);
Dan Williams209fae12011-06-13 17:39:44 -07001108 ret = TMF_RESP_FUNC_COMPLETE;
1109 goto out;
Dan Williams6f231dd2011-07-02 22:56:22 -07001110 }
Jeff Skirvina5fde222011-03-04 14:06:42 -08001111 if ((task->task_proto == SAS_PROTOCOL_SMP)
Jeff Skirvina5fde222011-03-04 14:06:42 -08001112 || old_request->complete_in_target
1113 ) {
Dan Williams6f231dd2011-07-02 22:56:22 -07001114
1115 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1116
Jeff Skirvina5fde222011-03-04 14:06:42 -08001117 dev_dbg(&isci_host->pdev->dev,
1118 "%s: SMP request (%d)"
Jeff Skirvina5fde222011-03-04 14:06:42 -08001119 " or complete_in_target (%d), thus no TMF\n",
1120 __func__, (task->task_proto == SAS_PROTOCOL_SMP),
Dan Williams209fae12011-06-13 17:39:44 -07001121 old_request->complete_in_target);
Jeff Skirvina5fde222011-03-04 14:06:42 -08001122
Dan Williams6f231dd2011-07-02 22:56:22 -07001123 /* Set the state on the task. */
1124 isci_task_all_done(task);
1125
1126 ret = TMF_RESP_FUNC_COMPLETE;
1127
1128 /* Stopping and SMP devices are not sent a TMF, and are not
Jeff Skirvina5fde222011-03-04 14:06:42 -08001129 * reset, but the outstanding I/O request is terminated below.
Dan Williams6f231dd2011-07-02 22:56:22 -07001130 */
Dan Williams6f231dd2011-07-02 22:56:22 -07001131 } else {
1132 /* Fill in the tmf stucture */
Dan Williams209fae12011-06-13 17:39:44 -07001133 isci_task_build_abort_task_tmf(&tmf, isci_tmf_ssp_task_abort,
Jeff Skirvinc3f42fe2011-03-04 14:06:56 -08001134 isci_abort_task_process_cb,
1135 old_request);
Dan Williams6f231dd2011-07-02 22:56:22 -07001136
Dan Williams6f231dd2011-07-02 22:56:22 -07001137 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1138
1139 #define ISCI_ABORT_TASK_TIMEOUT_MS 500 /* half second timeout. */
Dan Williams209fae12011-06-13 17:39:44 -07001140 ret = isci_task_execute_tmf(isci_host, isci_device, &tmf,
Dan Williams6f231dd2011-07-02 22:56:22 -07001141 ISCI_ABORT_TASK_TIMEOUT_MS);
1142
Jeff Skirvina5fde222011-03-04 14:06:42 -08001143 if (ret != TMF_RESP_FUNC_COMPLETE)
Dan Williams6f231dd2011-07-02 22:56:22 -07001144 dev_err(&isci_host->pdev->dev,
1145 "%s: isci_task_send_tmf failed\n",
1146 __func__);
1147 }
Jeff Skirvina5fde222011-03-04 14:06:42 -08001148 if (ret == TMF_RESP_FUNC_COMPLETE) {
1149 old_request->complete_in_target = true;
Dan Williams6f231dd2011-07-02 22:56:22 -07001150
Jeff Skirvin77c852f2011-06-20 14:09:16 -07001151 /* Clean up the request on our side, and wait for the aborted
1152 * I/O to complete.
1153 */
Jeff Skirvincbb65c62011-03-04 14:06:50 -08001154 isci_terminate_request_core(isci_host, isci_device, old_request);
Jeff Skirvina5fde222011-03-04 14:06:42 -08001155 }
1156
1157 /* Make sure we do not leave a reference to aborted_io_completion */
1158 old_request->io_request_completion = NULL;
Dan Williams209fae12011-06-13 17:39:44 -07001159 out:
1160 isci_put_device(isci_device);
Dan Williams6f231dd2011-07-02 22:56:22 -07001161 return ret;
1162}
1163
1164/**
1165 * isci_task_abort_task_set() - This function is one of the SAS Domain Template
1166 * functions. This is one of the Task Management functoins called by libsas,
1167 * to abort all task for the given lun.
1168 * @d_device: This parameter specifies the domain device associated with this
1169 * request.
1170 * @lun: This parameter specifies the lun associated with this request.
1171 *
1172 * status, zero indicates success.
1173 */
1174int isci_task_abort_task_set(
1175 struct domain_device *d_device,
1176 u8 *lun)
1177{
1178 return TMF_RESP_FUNC_FAILED;
1179}
1180
1181
1182/**
1183 * isci_task_clear_aca() - This function is one of the SAS Domain Template
1184 * functions. This is one of the Task Management functoins called by libsas.
1185 * @d_device: This parameter specifies the domain device associated with this
1186 * request.
1187 * @lun: This parameter specifies the lun associated with this request.
1188 *
1189 * status, zero indicates success.
1190 */
1191int isci_task_clear_aca(
1192 struct domain_device *d_device,
1193 u8 *lun)
1194{
1195 return TMF_RESP_FUNC_FAILED;
1196}
1197
1198
1199
1200/**
1201 * isci_task_clear_task_set() - This function is one of the SAS Domain Template
1202 * functions. This is one of the Task Management functoins called by libsas.
1203 * @d_device: This parameter specifies the domain device associated with this
1204 * request.
1205 * @lun: This parameter specifies the lun associated with this request.
1206 *
1207 * status, zero indicates success.
1208 */
1209int isci_task_clear_task_set(
1210 struct domain_device *d_device,
1211 u8 *lun)
1212{
1213 return TMF_RESP_FUNC_FAILED;
1214}
1215
1216
1217/**
1218 * isci_task_query_task() - This function is implemented to cause libsas to
1219 * correctly escalate the failed abort to a LUN or target reset (this is
1220 * because sas_scsi_find_task libsas function does not correctly interpret
1221 * all return codes from the abort task call). When TMF_RESP_FUNC_SUCC is
1222 * returned, libsas turns this into a LUN reset; when FUNC_FAILED is
1223 * returned, libsas will turn this into a target reset
1224 * @task: This parameter specifies the sas task being queried.
1225 * @lun: This parameter specifies the lun associated with this request.
1226 *
1227 * status, zero indicates success.
1228 */
1229int isci_task_query_task(
1230 struct sas_task *task)
1231{
1232 /* See if there is a pending device reset for this device. */
1233 if (task->task_state_flags & SAS_TASK_NEED_DEV_RESET)
1234 return TMF_RESP_FUNC_FAILED;
1235 else
1236 return TMF_RESP_FUNC_SUCC;
1237}
1238
Dave Jiangaf5ae892011-05-04 17:53:24 -07001239/*
Dan Williams6f231dd2011-07-02 22:56:22 -07001240 * isci_task_request_complete() - This function is called by the sci core when
1241 * an task request completes.
Dave Jiangaf5ae892011-05-04 17:53:24 -07001242 * @ihost: This parameter specifies the ISCI host object
1243 * @ireq: This parameter is the completed isci_request object.
Dan Williams6f231dd2011-07-02 22:56:22 -07001244 * @completion_status: This parameter specifies the completion status from the
1245 * sci core.
1246 *
1247 * none.
1248 */
Dave Jiangaf5ae892011-05-04 17:53:24 -07001249void
1250isci_task_request_complete(struct isci_host *ihost,
1251 struct isci_request *ireq,
1252 enum sci_task_status completion_status)
Dan Williams6f231dd2011-07-02 22:56:22 -07001253{
Dave Jiangaf5ae892011-05-04 17:53:24 -07001254 struct isci_tmf *tmf = isci_request_access_tmf(ireq);
Dan Williams6f231dd2011-07-02 22:56:22 -07001255 struct completion *tmf_complete;
Dan Williams67ea8382011-05-08 11:47:15 -07001256 struct scic_sds_request *sci_req = &ireq->sci;
Dan Williams6f231dd2011-07-02 22:56:22 -07001257
Dave Jiangaf5ae892011-05-04 17:53:24 -07001258 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -07001259 "%s: request = %p, status=%d\n",
Dave Jiangaf5ae892011-05-04 17:53:24 -07001260 __func__, ireq, completion_status);
Dan Williams6f231dd2011-07-02 22:56:22 -07001261
Dave Jiang8d2c65c2011-06-01 09:03:08 +00001262 isci_request_change_state(ireq, completed);
Dan Williams6f231dd2011-07-02 22:56:22 -07001263
1264 tmf->status = completion_status;
Dave Jiangaf5ae892011-05-04 17:53:24 -07001265 ireq->complete_in_target = true;
Dan Williams6f231dd2011-07-02 22:56:22 -07001266
Dave Jiangaf5ae892011-05-04 17:53:24 -07001267 if (tmf->proto == SAS_PROTOCOL_SSP) {
Dan Williams6f231dd2011-07-02 22:56:22 -07001268 memcpy(&tmf->resp.resp_iu,
Dan Williamsb7645812011-05-08 02:35:32 -07001269 &sci_req->ssp.rsp,
Dave Jiangaf5ae892011-05-04 17:53:24 -07001270 SSP_RESP_IU_MAX_SIZE);
1271 } else if (tmf->proto == SAS_PROTOCOL_SATA) {
Dan Williams6f231dd2011-07-02 22:56:22 -07001272 memcpy(&tmf->resp.d2h_fis,
Dan Williamsb7645812011-05-08 02:35:32 -07001273 &sci_req->stp.rsp,
Dave Jiangf2f30082011-05-04 15:02:02 -07001274 sizeof(struct dev_to_host_fis));
Dan Williams6f231dd2011-07-02 22:56:22 -07001275 }
1276
Dan Williams6f231dd2011-07-02 22:56:22 -07001277 /* PRINT_TMF( ((struct isci_tmf *)request->task)); */
1278 tmf_complete = tmf->complete;
1279
Dan Williams209fae12011-06-13 17:39:44 -07001280 scic_controller_complete_io(&ihost->sci, ireq->sci.target_device, &ireq->sci);
Dan Williams67ea8382011-05-08 11:47:15 -07001281 /* set the 'terminated' flag handle to make sure it cannot be terminated
Dan Williams6f231dd2011-07-02 22:56:22 -07001282 * or completed again.
1283 */
Dan Williams67ea8382011-05-08 11:47:15 -07001284 ireq->terminated = true;;
Dan Williams6f231dd2011-07-02 22:56:22 -07001285
Dave Jiangaf5ae892011-05-04 17:53:24 -07001286 isci_request_change_state(ireq, unallocated);
1287 list_del_init(&ireq->dev_node);
Dan Williams6f231dd2011-07-02 22:56:22 -07001288
1289 /* The task management part completes last. */
1290 complete(tmf_complete);
1291}
1292
Jeff Skirvin61aaff42011-06-21 12:16:33 -07001293static void isci_smp_task_timedout(unsigned long _task)
1294{
1295 struct sas_task *task = (void *) _task;
1296 unsigned long flags;
1297
1298 spin_lock_irqsave(&task->task_state_lock, flags);
1299 if (!(task->task_state_flags & SAS_TASK_STATE_DONE))
1300 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
1301 spin_unlock_irqrestore(&task->task_state_lock, flags);
1302
1303 complete(&task->completion);
1304}
1305
1306static void isci_smp_task_done(struct sas_task *task)
1307{
1308 if (!del_timer(&task->timer))
1309 return;
1310 complete(&task->completion);
1311}
1312
1313static struct sas_task *isci_alloc_task(void)
1314{
1315 struct sas_task *task = kzalloc(sizeof(*task), GFP_KERNEL);
1316
1317 if (task) {
1318 INIT_LIST_HEAD(&task->list);
1319 spin_lock_init(&task->task_state_lock);
1320 task->task_state_flags = SAS_TASK_STATE_PENDING;
1321 init_timer(&task->timer);
1322 init_completion(&task->completion);
1323 }
1324
1325 return task;
1326}
1327
1328static void isci_free_task(struct isci_host *ihost, struct sas_task *task)
1329{
1330 if (task) {
1331 BUG_ON(!list_empty(&task->list));
1332 kfree(task);
1333 }
1334}
1335
1336static int isci_smp_execute_task(struct isci_host *ihost,
1337 struct domain_device *dev, void *req,
1338 int req_size, void *resp, int resp_size)
1339{
1340 int res, retry;
1341 struct sas_task *task = NULL;
1342
1343 for (retry = 0; retry < 3; retry++) {
1344 task = isci_alloc_task();
1345 if (!task)
1346 return -ENOMEM;
1347
1348 task->dev = dev;
1349 task->task_proto = dev->tproto;
1350 sg_init_one(&task->smp_task.smp_req, req, req_size);
1351 sg_init_one(&task->smp_task.smp_resp, resp, resp_size);
1352
1353 task->task_done = isci_smp_task_done;
1354
1355 task->timer.data = (unsigned long) task;
1356 task->timer.function = isci_smp_task_timedout;
1357 task->timer.expires = jiffies + 10*HZ;
1358 add_timer(&task->timer);
1359
1360 res = isci_task_execute_task(task, 1, GFP_KERNEL);
1361
1362 if (res) {
1363 del_timer(&task->timer);
1364 dev_err(&ihost->pdev->dev,
1365 "%s: executing SMP task failed:%d\n",
1366 __func__, res);
1367 goto ex_err;
1368 }
1369
1370 wait_for_completion(&task->completion);
1371 res = -ECOMM;
1372 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
1373 dev_err(&ihost->pdev->dev,
1374 "%s: smp task timed out or aborted\n",
1375 __func__);
1376 isci_task_abort_task(task);
1377 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
1378 dev_err(&ihost->pdev->dev,
1379 "%s: SMP task aborted and not done\n",
1380 __func__);
1381 goto ex_err;
1382 }
1383 }
1384 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1385 task->task_status.stat == SAM_STAT_GOOD) {
1386 res = 0;
1387 break;
1388 }
1389 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1390 task->task_status.stat == SAS_DATA_UNDERRUN) {
1391 /* no error, but return the number of bytes of
1392 * underrun */
1393 res = task->task_status.residual;
1394 break;
1395 }
1396 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1397 task->task_status.stat == SAS_DATA_OVERRUN) {
1398 res = -EMSGSIZE;
1399 break;
1400 } else {
1401 dev_err(&ihost->pdev->dev,
1402 "%s: task to dev %016llx response: 0x%x "
1403 "status 0x%x\n", __func__,
1404 SAS_ADDR(dev->sas_addr),
1405 task->task_status.resp,
1406 task->task_status.stat);
1407 isci_free_task(ihost, task);
1408 task = NULL;
1409 }
1410 }
1411ex_err:
1412 BUG_ON(retry == 3 && task != NULL);
1413 isci_free_task(ihost, task);
1414 return res;
1415}
1416
1417#define DISCOVER_REQ_SIZE 16
1418#define DISCOVER_RESP_SIZE 56
1419
1420int isci_smp_get_phy_attached_dev_type(struct isci_host *ihost,
1421 struct domain_device *dev,
1422 int phy_id, int *adt)
1423{
1424 struct smp_resp *disc_resp;
1425 u8 *disc_req;
1426 int res;
1427
1428 disc_resp = kzalloc(DISCOVER_RESP_SIZE, GFP_KERNEL);
1429 if (!disc_resp)
1430 return -ENOMEM;
1431
1432 disc_req = kzalloc(DISCOVER_REQ_SIZE, GFP_KERNEL);
1433 if (disc_req) {
1434 disc_req[0] = SMP_REQUEST;
1435 disc_req[1] = SMP_DISCOVER;
1436 disc_req[9] = phy_id;
1437 } else {
1438 kfree(disc_resp);
1439 return -ENOMEM;
1440 }
1441 res = isci_smp_execute_task(ihost, dev, disc_req, DISCOVER_REQ_SIZE,
1442 disc_resp, DISCOVER_RESP_SIZE);
1443 if (!res) {
1444 if (disc_resp->result != SMP_RESP_FUNC_ACC)
1445 res = disc_resp->result;
1446 else
1447 *adt = disc_resp->disc.attached_dev_type;
1448 }
1449 kfree(disc_req);
1450 kfree(disc_resp);
1451
1452 return res;
1453}
1454
1455static void isci_wait_for_smp_phy_reset(struct isci_remote_device *idev, int phy_num)
1456{
1457 struct domain_device *dev = idev->domain_dev;
1458 struct isci_port *iport = idev->isci_port;
1459 struct isci_host *ihost = iport->isci_host;
1460 int res, iteration = 0, attached_device_type;
1461 #define STP_WAIT_MSECS 25000
1462 unsigned long tmo = msecs_to_jiffies(STP_WAIT_MSECS);
1463 unsigned long deadline = jiffies + tmo;
1464 enum {
1465 SMP_PHYWAIT_PHYDOWN,
1466 SMP_PHYWAIT_PHYUP,
1467 SMP_PHYWAIT_DONE
1468 } phy_state = SMP_PHYWAIT_PHYDOWN;
1469
1470 /* While there is time, wait for the phy to go away and come back */
1471 while (time_is_after_jiffies(deadline) && phy_state != SMP_PHYWAIT_DONE) {
1472 int event = atomic_read(&iport->event);
1473
1474 ++iteration;
1475
1476 tmo = wait_event_timeout(ihost->eventq,
1477 event != atomic_read(&iport->event) ||
1478 !test_bit(IPORT_BCN_BLOCKED, &iport->flags),
1479 tmo);
1480 /* link down, stop polling */
1481 if (!test_bit(IPORT_BCN_BLOCKED, &iport->flags))
1482 break;
1483
1484 dev_dbg(&ihost->pdev->dev,
1485 "%s: iport %p, iteration %d,"
1486 " phase %d: time_remaining %lu, bcns = %d\n",
1487 __func__, iport, iteration, phy_state,
1488 tmo, test_bit(IPORT_BCN_PENDING, &iport->flags));
1489
1490 res = isci_smp_get_phy_attached_dev_type(ihost, dev, phy_num,
1491 &attached_device_type);
1492 tmo = deadline - jiffies;
1493
1494 if (res) {
1495 dev_warn(&ihost->pdev->dev,
1496 "%s: iteration %d, phase %d:"
1497 " SMP error=%d, time_remaining=%lu\n",
1498 __func__, iteration, phy_state, res, tmo);
1499 break;
1500 }
1501 dev_dbg(&ihost->pdev->dev,
1502 "%s: iport %p, iteration %d,"
1503 " phase %d: time_remaining %lu, bcns = %d, "
1504 "attdevtype = %x\n",
1505 __func__, iport, iteration, phy_state,
1506 tmo, test_bit(IPORT_BCN_PENDING, &iport->flags),
1507 attached_device_type);
1508
1509 switch (phy_state) {
1510 case SMP_PHYWAIT_PHYDOWN:
1511 /* Has the device gone away? */
1512 if (!attached_device_type)
1513 phy_state = SMP_PHYWAIT_PHYUP;
1514
1515 break;
1516
1517 case SMP_PHYWAIT_PHYUP:
1518 /* Has the device come back? */
1519 if (attached_device_type)
1520 phy_state = SMP_PHYWAIT_DONE;
1521 break;
1522
1523 case SMP_PHYWAIT_DONE:
1524 break;
1525 }
1526
1527 }
1528 dev_dbg(&ihost->pdev->dev, "%s: done\n", __func__);
1529}
1530
Dan Williams209fae12011-06-13 17:39:44 -07001531static int isci_reset_device(struct isci_host *ihost,
1532 struct isci_remote_device *idev, int hard_reset)
Dan Williams6f231dd2011-07-02 22:56:22 -07001533{
Dan Williams209fae12011-06-13 17:39:44 -07001534 struct sas_phy *phy = sas_find_local_phy(idev->domain_dev);
Jeff Skirvin61aaff42011-06-21 12:16:33 -07001535 struct isci_port *iport = idev->isci_port;
Dan Williams6f231dd2011-07-02 22:56:22 -07001536 enum sci_status status;
Dan Williamsd06b4872011-05-02 13:59:25 -07001537 unsigned long flags;
1538 int rc;
Dan Williams6f231dd2011-07-02 22:56:22 -07001539
Dan Williamsd06b4872011-05-02 13:59:25 -07001540 dev_dbg(&ihost->pdev->dev, "%s: idev %p\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001541
Dan Williamsd06b4872011-05-02 13:59:25 -07001542 spin_lock_irqsave(&ihost->scic_lock, flags);
1543 status = scic_remote_device_reset(&idev->sci);
Dan Williams6f231dd2011-07-02 22:56:22 -07001544 if (status != SCI_SUCCESS) {
Dan Williamsd06b4872011-05-02 13:59:25 -07001545 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001546
Dan Williamsd06b4872011-05-02 13:59:25 -07001547 dev_warn(&ihost->pdev->dev,
1548 "%s: scic_remote_device_reset(%p) returned %d!\n",
1549 __func__, idev, status);
Dan Williams6f231dd2011-07-02 22:56:22 -07001550
1551 return TMF_RESP_FUNC_FAILED;
1552 }
Dan Williamsd06b4872011-05-02 13:59:25 -07001553 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001554
Dan Williams6f231dd2011-07-02 22:56:22 -07001555 /* Make sure all pending requests are able to be fully terminated. */
Dan Williamsd06b4872011-05-02 13:59:25 -07001556 isci_device_clear_reset_pending(ihost, idev);
1557
Jeff Skirvin61aaff42011-06-21 12:16:33 -07001558 /* If this is a device on an expander, disable BCN processing. */
1559 if (!scsi_is_sas_phy_local(phy))
1560 set_bit(IPORT_BCN_BLOCKED, &iport->flags);
1561
Dan Williamsd06b4872011-05-02 13:59:25 -07001562 rc = sas_phy_reset(phy, hard_reset);
Dan Williams6f231dd2011-07-02 22:56:22 -07001563
1564 /* Terminate in-progress I/O now. */
Dan Williamsd06b4872011-05-02 13:59:25 -07001565 isci_remote_device_nuke_requests(ihost, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001566
Jeff Skirvinff717ab2011-06-20 14:08:51 -07001567 /* Since all pending TCs have been cleaned, resume the RNC. */
Dan Williamsd06b4872011-05-02 13:59:25 -07001568 spin_lock_irqsave(&ihost->scic_lock, flags);
1569 status = scic_remote_device_reset_complete(&idev->sci);
1570 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001571
Jeff Skirvin61aaff42011-06-21 12:16:33 -07001572 /* If this is a device on an expander, bring the phy back up. */
1573 if (!scsi_is_sas_phy_local(phy)) {
1574 /* A phy reset will cause the device to go away then reappear.
1575 * Since libsas will take action on incoming BCNs (eg. remove
1576 * a device going through an SMP phy-control driven reset),
1577 * we need to wait until the phy comes back up before letting
1578 * discovery proceed in libsas.
1579 */
1580 isci_wait_for_smp_phy_reset(idev, phy->number);
1581
1582 spin_lock_irqsave(&ihost->scic_lock, flags);
1583 isci_port_bcn_enable(ihost, idev->isci_port);
1584 spin_unlock_irqrestore(&ihost->scic_lock, flags);
1585 }
Jeff Skirvinff717ab2011-06-20 14:08:51 -07001586
Dan Williams6f231dd2011-07-02 22:56:22 -07001587 if (status != SCI_SUCCESS) {
Dan Williamsd06b4872011-05-02 13:59:25 -07001588 dev_warn(&ihost->pdev->dev,
1589 "%s: scic_remote_device_reset_complete(%p) "
1590 "returned %d!\n", __func__, idev, status);
Dan Williams6f231dd2011-07-02 22:56:22 -07001591 }
Dan Williams6f231dd2011-07-02 22:56:22 -07001592
Dan Williamsd06b4872011-05-02 13:59:25 -07001593 dev_dbg(&ihost->pdev->dev, "%s: idev %p complete.\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001594
Dan Williamsd06b4872011-05-02 13:59:25 -07001595 return rc;
1596}
1597
1598int isci_task_I_T_nexus_reset(struct domain_device *dev)
1599{
1600 struct isci_host *ihost = dev_to_ihost(dev);
Dan Williamsd06b4872011-05-02 13:59:25 -07001601 struct isci_remote_device *idev;
Dan Williams209fae12011-06-13 17:39:44 -07001602 int ret, hard_reset = 1;
Dan Williamsd06b4872011-05-02 13:59:25 -07001603 unsigned long flags;
1604
Dan Williamsd06b4872011-05-02 13:59:25 -07001605 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williams209fae12011-06-13 17:39:44 -07001606 idev = isci_lookup_device(dev);
Dan Williamsd06b4872011-05-02 13:59:25 -07001607 spin_unlock_irqrestore(&ihost->scic_lock, flags);
1608
Dan Williams209fae12011-06-13 17:39:44 -07001609 if (!idev || !test_bit(IDEV_EH, &idev->flags)) {
1610 ret = TMF_RESP_FUNC_COMPLETE;
1611 goto out;
1612 }
Dan Williamsd06b4872011-05-02 13:59:25 -07001613
1614 if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP))
1615 hard_reset = 0;
1616
Dan Williams209fae12011-06-13 17:39:44 -07001617 ret = isci_reset_device(ihost, idev, hard_reset);
1618 out:
1619 isci_put_device(idev);
1620 return ret;
Dan Williamsd06b4872011-05-02 13:59:25 -07001621}
1622
1623int isci_bus_reset_handler(struct scsi_cmnd *cmd)
1624{
1625 struct domain_device *dev = sdev_to_domain_dev(cmd->device);
Dan Williams209fae12011-06-13 17:39:44 -07001626 struct isci_host *ihost = dev_to_ihost(dev);
1627 struct isci_remote_device *idev;
1628 int ret, hard_reset = 1;
1629 unsigned long flags;
Dan Williamsd06b4872011-05-02 13:59:25 -07001630
1631 if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP))
1632 hard_reset = 0;
1633
Dan Williams209fae12011-06-13 17:39:44 -07001634 spin_lock_irqsave(&ihost->scic_lock, flags);
1635 idev = isci_lookup_device(dev);
1636 spin_unlock_irqrestore(&ihost->scic_lock, flags);
1637
1638 if (!idev) {
1639 ret = TMF_RESP_FUNC_COMPLETE;
1640 goto out;
1641 }
1642
1643 ret = isci_reset_device(ihost, idev, hard_reset);
1644 out:
1645 isci_put_device(idev);
1646 return ret;
Dan Williams6f231dd2011-07-02 22:56:22 -07001647}