blob: ded81cd1a78198f93f9ade762624122f73843177 [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 Williams6f231dd2011-07-02 22:56:22 -0700149 struct isci_remote_device *device;
150 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);
169
Dan Williams4393aa42011-03-31 13:10:44 -0700170 device = task->dev->lldd_dev;
Dan Williams6f231dd2011-07-02 22:56:22 -0700171
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700172 if (device)
173 device_status = device->status;
174 else
175 device_status = isci_freed;
176
177 /* From this point onward, any process that needs to guarantee
178 * that there is no kernel I/O being started will have to wait
179 * for the quiesce spinlock.
180 */
181
182 if (device_status != isci_ready_for_io) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700183
184 /* Forces a retry from scsi mid layer. */
Dave Jiangd2d61432011-04-21 05:36:23 +0000185 dev_dbg(&ihost->pdev->dev,
186 "%s: task %p: isci_host->status = %d, "
187 "device = %p; device_status = 0x%x\n\n",
188 __func__,
189 task,
190 isci_host_get_state(ihost),
191 device,
192 device_status);
Dan Williams6f231dd2011-07-02 22:56:22 -0700193
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700194 if (device_status == isci_ready) {
195 /* Indicate QUEUE_FULL so that the scsi midlayer
196 * retries.
197 */
Dan Williams1077a572011-03-11 10:13:51 -0800198 isci_task_refuse(ihost, task,
199 SAS_TASK_COMPLETE,
200 SAS_QUEUE_FULL);
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700201 } else {
202 /* Else, the device is going down. */
Dan Williams1077a572011-03-11 10:13:51 -0800203 isci_task_refuse(ihost, task,
204 SAS_TASK_UNDELIVERED,
205 SAS_DEVICE_UNKNOWN);
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700206 }
Dan Williams1077a572011-03-11 10:13:51 -0800207 isci_host_can_dequeue(ihost, 1);
Dan Williams6f231dd2011-07-02 22:56:22 -0700208 } else {
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700209 /* There is a device and it's ready for I/O. */
210 spin_lock_irqsave(&task->task_state_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700211
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700212 if (task->task_state_flags & SAS_TASK_STATE_ABORTED) {
213
214 spin_unlock_irqrestore(&task->task_state_lock,
215 flags);
216
Dan Williams1077a572011-03-11 10:13:51 -0800217 isci_task_refuse(ihost, task,
218 SAS_TASK_UNDELIVERED,
219 SAM_STAT_TASK_ABORTED);
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700220
221 /* The I/O was aborted. */
222
223 } else {
Dan Williams6f231dd2011-07-02 22:56:22 -0700224 task->task_state_flags |= SAS_TASK_AT_INITIATOR;
225 spin_unlock_irqrestore(&task->task_state_lock, flags);
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700226
227 /* build and send the request. */
Dan Williams0d0cf142011-06-13 00:51:30 -0700228 status = isci_request_execute(ihost, task, gfp_flags);
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700229
230 if (status != SCI_SUCCESS) {
231
232 spin_lock_irqsave(&task->task_state_lock, flags);
233 /* Did not really start this command. */
234 task->task_state_flags &= ~SAS_TASK_AT_INITIATOR;
235 spin_unlock_irqrestore(&task->task_state_lock, flags);
236
237 /* Indicate QUEUE_FULL so that the scsi
238 * midlayer retries. if the request
239 * failed for remote device reasons,
240 * it gets returned as
241 * SAS_TASK_UNDELIVERED next time
242 * through.
243 */
Dan Williams1077a572011-03-11 10:13:51 -0800244 isci_task_refuse(ihost, task,
245 SAS_TASK_COMPLETE,
246 SAS_QUEUE_FULL);
247 isci_host_can_dequeue(ihost, 1);
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700248 }
Dan Williams6f231dd2011-07-02 22:56:22 -0700249 }
250 }
Dan Williams1077a572011-03-11 10:13:51 -0800251 }
Dan Williams6f231dd2011-07-02 22:56:22 -0700252 return 0;
253}
254
Dan Williams0d0cf142011-06-13 00:51:30 -0700255static struct isci_request *isci_task_request_build(struct isci_host *ihost,
256 struct isci_tmf *isci_tmf)
Dan Williams6f231dd2011-07-02 22:56:22 -0700257{
Dan Williams0d0cf142011-06-13 00:51:30 -0700258 struct scic_sds_remote_device *sci_dev;
Dan Williams6f231dd2011-07-02 22:56:22 -0700259 enum sci_status status = SCI_FAILURE;
Dan Williams0d0cf142011-06-13 00:51:30 -0700260 struct isci_request *ireq = NULL;
261 struct isci_remote_device *idev;
Dan Williamsa1a113b2011-04-21 18:44:45 -0700262 struct domain_device *dev;
Dan Williams6f231dd2011-07-02 22:56:22 -0700263
Dan Williams0d0cf142011-06-13 00:51:30 -0700264 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -0700265 "%s: isci_tmf = %p\n", __func__, isci_tmf);
266
Dan Williams0d0cf142011-06-13 00:51:30 -0700267 idev = isci_tmf->device;
268 sci_dev = &idev->sci;
269 dev = idev->domain_dev;
Dan Williams6f231dd2011-07-02 22:56:22 -0700270
271 /* do common allocation and init of request object. */
Dan Williams0d0cf142011-06-13 00:51:30 -0700272 ireq = isci_request_alloc_tmf(ihost, isci_tmf, idev, GFP_ATOMIC);
273 if (!ireq)
274 return NULL;
Dan Williams6f231dd2011-07-02 22:56:22 -0700275
276 /* let the core do it's construct. */
Dan Williams0d0cf142011-06-13 00:51:30 -0700277 status = scic_task_request_construct(&ihost->sci, sci_dev,
Dan Williams0d843662011-05-08 01:56:57 -0700278 SCI_CONTROLLER_INVALID_IO_TAG,
Dan Williams0d0cf142011-06-13 00:51:30 -0700279 &ireq->sci);
Dan Williams6f231dd2011-07-02 22:56:22 -0700280
281 if (status != SCI_SUCCESS) {
Dan Williams0d0cf142011-06-13 00:51:30 -0700282 dev_warn(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -0700283 "%s: scic_task_request_construct failed - "
284 "status = 0x%x\n",
285 __func__,
286 status);
287 goto errout;
288 }
289
Dan Williamsa1a113b2011-04-21 18:44:45 -0700290 /* XXX convert to get this from task->tproto like other drivers */
291 if (dev->dev_type == SAS_END_DEV) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700292 isci_tmf->proto = SAS_PROTOCOL_SSP;
Dan Williams0d0cf142011-06-13 00:51:30 -0700293 status = scic_task_request_construct_ssp(&ireq->sci);
Dan Williams6f231dd2011-07-02 22:56:22 -0700294 if (status != SCI_SUCCESS)
295 goto errout;
296 }
297
Dan Williamsa1a113b2011-04-21 18:44:45 -0700298 if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP)) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700299 isci_tmf->proto = SAS_PROTOCOL_SATA;
Dan Williams0d0cf142011-06-13 00:51:30 -0700300 status = isci_sata_management_task_request_build(ireq);
Dan Williams6f231dd2011-07-02 22:56:22 -0700301
302 if (status != SCI_SUCCESS)
303 goto errout;
304 }
Dan Williams0d0cf142011-06-13 00:51:30 -0700305 return ireq;
Dan Williams6f231dd2011-07-02 22:56:22 -0700306 errout:
Dan Williams0d0cf142011-06-13 00:51:30 -0700307 isci_request_free(ihost, ireq);
308 ireq = NULL;
309 return ireq;
Dan Williams6f231dd2011-07-02 22:56:22 -0700310}
311
312/**
Dan Williams6f231dd2011-07-02 22:56:22 -0700313 * isci_task_execute_tmf() - This function builds and sends a task request,
314 * then waits for the completion.
315 * @isci_host: This parameter specifies the ISCI host object
316 * @tmf: This parameter is the pointer to the task management structure for
317 * this request.
318 * @timeout_ms: This parameter specifies the timeout period for the task
319 * management request.
320 *
321 * TMF_RESP_FUNC_COMPLETE on successful completion of the TMF (this includes
322 * error conditions reported in the IU status), or TMF_RESP_FUNC_FAILED.
323 */
Dan Williams0d0cf142011-06-13 00:51:30 -0700324int isci_task_execute_tmf(struct isci_host *ihost, struct isci_tmf *tmf,
325 unsigned long timeout_ms)
Dan Williams6f231dd2011-07-02 22:56:22 -0700326{
327 DECLARE_COMPLETION_ONSTACK(completion);
Bartosz Barcinski467e8552011-04-12 17:28:41 -0700328 enum sci_task_status status = SCI_TASK_FAILURE;
Dan Williams6f231dd2011-07-02 22:56:22 -0700329 struct scic_sds_remote_device *sci_device;
330 struct isci_remote_device *isci_device = tmf->device;
Dan Williams0d0cf142011-06-13 00:51:30 -0700331 struct isci_request *ireq;
Dan Williams6f231dd2011-07-02 22:56:22 -0700332 int ret = TMF_RESP_FUNC_FAILED;
333 unsigned long flags;
Edmund Nadolskifd183882011-05-19 12:00:15 +0000334 unsigned long timeleft;
Dan Williams6f231dd2011-07-02 22:56:22 -0700335
336 /* sanity check, return TMF_RESP_FUNC_FAILED
337 * if the device is not there and ready.
338 */
Dan Williams8acaec12011-03-07 14:47:35 -0800339 if (!isci_device || isci_device->status != isci_ready_for_io) {
Dan Williams0d0cf142011-06-13 00:51:30 -0700340 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -0700341 "%s: isci_device = %p not ready (%d)\n",
342 __func__,
Dan Williams8acaec12011-03-07 14:47:35 -0800343 isci_device, isci_device->status);
Dan Williams6f231dd2011-07-02 22:56:22 -0700344 return TMF_RESP_FUNC_FAILED;
345 } else
Dan Williams0d0cf142011-06-13 00:51:30 -0700346 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -0700347 "%s: isci_device = %p\n",
348 __func__, isci_device);
349
Dan Williams57f20f42011-04-21 18:14:45 -0700350 sci_device = &isci_device->sci;
Dan Williams6f231dd2011-07-02 22:56:22 -0700351
352 /* Assign the pointer to the TMF's completion kernel wait structure. */
353 tmf->complete = &completion;
354
Dan Williams0d0cf142011-06-13 00:51:30 -0700355 ireq = isci_task_request_build(ihost, tmf);
356 if (!ireq) {
357 dev_warn(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -0700358 "%s: isci_task_request_build failed\n",
359 __func__);
360 return TMF_RESP_FUNC_FAILED;
361 }
362
Dan Williams0d0cf142011-06-13 00:51:30 -0700363 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700364
365 /* start the TMF io. */
366 status = scic_controller_start_task(
Dan Williams0d0cf142011-06-13 00:51:30 -0700367 &ihost->sci,
Dan Williams6f231dd2011-07-02 22:56:22 -0700368 sci_device,
Dan Williams0d0cf142011-06-13 00:51:30 -0700369 &ireq->sci,
Dan Williams67ea8382011-05-08 11:47:15 -0700370 SCI_CONTROLLER_INVALID_IO_TAG);
Dan Williams6f231dd2011-07-02 22:56:22 -0700371
Bartosz Barcinski467e8552011-04-12 17:28:41 -0700372 if (status != SCI_TASK_SUCCESS) {
Dan Williams0d0cf142011-06-13 00:51:30 -0700373 dev_warn(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -0700374 "%s: start_io failed - status = 0x%x, request = %p\n",
375 __func__,
376 status,
Dan Williams0d0cf142011-06-13 00:51:30 -0700377 ireq);
378 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700379 goto cleanup_request;
380 }
381
Dan Williams6f231dd2011-07-02 22:56:22 -0700382 if (tmf->cb_state_func != NULL)
383 tmf->cb_state_func(isci_tmf_started, tmf, tmf->cb_data);
384
Dan Williams0d0cf142011-06-13 00:51:30 -0700385 isci_request_change_state(ireq, started);
Dan Williams6f231dd2011-07-02 22:56:22 -0700386
387 /* add the request to the remote device request list. */
Dan Williams0d0cf142011-06-13 00:51:30 -0700388 list_add(&ireq->dev_node, &isci_device->reqs_in_process);
Dan Williams6f231dd2011-07-02 22:56:22 -0700389
Dan Williams0d0cf142011-06-13 00:51:30 -0700390 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700391
392 /* Wait for the TMF to complete, or a timeout. */
Edmund Nadolskifd183882011-05-19 12:00:15 +0000393 timeleft = wait_for_completion_timeout(&completion,
394 jiffies + msecs_to_jiffies(timeout_ms));
395
396 if (timeleft == 0) {
Dan Williams0d0cf142011-06-13 00:51:30 -0700397 spin_lock_irqsave(&ihost->scic_lock, flags);
Edmund Nadolskifd183882011-05-19 12:00:15 +0000398
399 if (tmf->cb_state_func != NULL)
400 tmf->cb_state_func(isci_tmf_timed_out, tmf, tmf->cb_data);
401
402 status = scic_controller_terminate_request(
Dan Williams0d0cf142011-06-13 00:51:30 -0700403 &ireq->isci_host->sci,
404 &ireq->isci_device->sci,
405 &ireq->sci);
Edmund Nadolskifd183882011-05-19 12:00:15 +0000406
Dan Williams0d0cf142011-06-13 00:51:30 -0700407 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Edmund Nadolskifd183882011-05-19 12:00:15 +0000408 }
Dan Williams6f231dd2011-07-02 22:56:22 -0700409
410 isci_print_tmf(tmf);
411
412 if (tmf->status == SCI_SUCCESS)
413 ret = TMF_RESP_FUNC_COMPLETE;
414 else if (tmf->status == SCI_FAILURE_IO_RESPONSE_VALID) {
Dan Williams0d0cf142011-06-13 00:51:30 -0700415 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -0700416 "%s: tmf.status == "
417 "SCI_FAILURE_IO_RESPONSE_VALID\n",
418 __func__);
419 ret = TMF_RESP_FUNC_COMPLETE;
420 }
421 /* Else - leave the default "failed" status alone. */
422
Dan Williams0d0cf142011-06-13 00:51:30 -0700423 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -0700424 "%s: completed request = %p\n",
425 __func__,
Dan Williams0d0cf142011-06-13 00:51:30 -0700426 ireq);
Dan Williams6f231dd2011-07-02 22:56:22 -0700427
Dan Williams0d0cf142011-06-13 00:51:30 -0700428 if (ireq->io_request_completion != NULL) {
Edmund Nadolskifd183882011-05-19 12:00:15 +0000429 /* A thread is waiting for this TMF to finish. */
Dan Williams0d0cf142011-06-13 00:51:30 -0700430 complete(ireq->io_request_completion);
Dan Williams6f231dd2011-07-02 22:56:22 -0700431 }
432
Dan Williams6f231dd2011-07-02 22:56:22 -0700433 cleanup_request:
Dan Williams0d0cf142011-06-13 00:51:30 -0700434 isci_request_free(ihost, ireq);
Dan Williams6f231dd2011-07-02 22:56:22 -0700435 return ret;
436}
437
438void isci_task_build_tmf(
439 struct isci_tmf *tmf,
440 struct isci_remote_device *isci_device,
441 enum isci_tmf_function_codes code,
442 void (*tmf_sent_cb)(enum isci_tmf_cb_state,
443 struct isci_tmf *,
444 void *),
Jeff Skirvinc3f42fe2011-03-04 14:06:56 -0800445 void *cb_data)
Dan Williams6f231dd2011-07-02 22:56:22 -0700446{
447 dev_dbg(&isci_device->isci_port->isci_host->pdev->dev,
448 "%s: isci_device = %p\n", __func__, isci_device);
449
450 memset(tmf, 0, sizeof(*tmf));
451
452 tmf->device = isci_device;
453 tmf->tmf_code = code;
Edmund Nadolskifd183882011-05-19 12:00:15 +0000454
Dan Williams6f231dd2011-07-02 22:56:22 -0700455 tmf->cb_state_func = tmf_sent_cb;
Jeff Skirvinc3f42fe2011-03-04 14:06:56 -0800456 tmf->cb_data = cb_data;
457}
Jeff Skirvin1fad9e92011-03-04 14:06:46 -0800458
Dan Williams35173d52011-03-26 16:43:01 -0700459static void isci_task_build_abort_task_tmf(
Jeff Skirvinc3f42fe2011-03-04 14:06:56 -0800460 struct isci_tmf *tmf,
461 struct isci_remote_device *isci_device,
462 enum isci_tmf_function_codes code,
463 void (*tmf_sent_cb)(enum isci_tmf_cb_state,
464 struct isci_tmf *,
465 void *),
466 struct isci_request *old_request)
467{
468 isci_task_build_tmf(tmf, isci_device, code, tmf_sent_cb,
469 (void *)old_request);
470 tmf->io_tag = old_request->io_tag;
Dan Williams6f231dd2011-07-02 22:56:22 -0700471}
472
473static struct isci_request *isci_task_get_request_from_task(
474 struct sas_task *task,
Dan Williams6f231dd2011-07-02 22:56:22 -0700475 struct isci_remote_device **isci_device)
476{
477
478 struct isci_request *request = NULL;
479 unsigned long flags;
480
481 spin_lock_irqsave(&task->task_state_lock, flags);
482
483 request = task->lldd_task;
484
485 /* If task is already done, the request isn't valid */
486 if (!(task->task_state_flags & SAS_TASK_STATE_DONE) &&
487 (task->task_state_flags & SAS_TASK_AT_INITIATOR) &&
488 (request != NULL)) {
489
Dan Williams6f231dd2011-07-02 22:56:22 -0700490 if (isci_device != NULL)
491 *isci_device = request->isci_device;
492 }
493
494 spin_unlock_irqrestore(&task->task_state_lock, flags);
495
496 return request;
497}
498
499/**
500 * isci_task_validate_request_to_abort() - This function checks the given I/O
501 * against the "started" state. If the request is still "started", it's
502 * state is changed to aborted. NOTE: isci_host->scic_lock MUST BE HELD
503 * BEFORE CALLING THIS FUNCTION.
504 * @isci_request: This parameter specifies the request object to control.
505 * @isci_host: This parameter specifies the ISCI host object
506 * @isci_device: This is the device to which the request is pending.
507 * @aborted_io_completion: This is a completion structure that will be added to
508 * the request in case it is changed to aborting; this completion is
509 * triggered when the request is fully completed.
510 *
511 * Either "started" on successful change of the task status to "aborted", or
512 * "unallocated" if the task cannot be controlled.
513 */
514static enum isci_request_status isci_task_validate_request_to_abort(
515 struct isci_request *isci_request,
516 struct isci_host *isci_host,
517 struct isci_remote_device *isci_device,
518 struct completion *aborted_io_completion)
519{
520 enum isci_request_status old_state = unallocated;
521
522 /* Only abort the task if it's in the
523 * device's request_in_process list
524 */
525 if (isci_request && !list_empty(&isci_request->dev_node)) {
526 old_state = isci_request_change_started_to_aborted(
527 isci_request, aborted_io_completion);
528
Dan Williams6f231dd2011-07-02 22:56:22 -0700529 }
530
531 return old_state;
532}
533
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700534/**
535* isci_request_cleanup_completed_loiterer() - This function will take care of
536* the final cleanup on any request which has been explicitly terminated.
537* @isci_host: This parameter specifies the ISCI host object
538* @isci_device: This is the device to which the request is pending.
539* @isci_request: This parameter specifies the terminated request object.
540* @task: This parameter is the libsas I/O request.
541*/
Dan Williams6f231dd2011-07-02 22:56:22 -0700542static void isci_request_cleanup_completed_loiterer(
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700543 struct isci_host *isci_host,
Dan Williams6f231dd2011-07-02 22:56:22 -0700544 struct isci_remote_device *isci_device,
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700545 struct isci_request *isci_request,
546 struct sas_task *task)
Dan Williams6f231dd2011-07-02 22:56:22 -0700547{
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700548 unsigned long flags;
Dan Williams6f231dd2011-07-02 22:56:22 -0700549
550 dev_dbg(&isci_host->pdev->dev,
551 "%s: isci_device=%p, request=%p, task=%p\n",
Jeff Skirvin18d3d722011-03-04 14:06:38 -0800552 __func__, isci_device, isci_request, task);
Dan Williams6f231dd2011-07-02 22:56:22 -0700553
Jeff Skirvina5fde222011-03-04 14:06:42 -0800554 if (task != NULL) {
555
556 spin_lock_irqsave(&task->task_state_lock, flags);
557 task->lldd_task = NULL;
558
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700559 task->task_state_flags &= ~SAS_TASK_NEED_DEV_RESET;
560
Jeff Skirvina5fde222011-03-04 14:06:42 -0800561 isci_set_task_doneflags(task);
562
563 /* If this task is not in the abort path, call task_done. */
564 if (!(task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
565
566 spin_unlock_irqrestore(&task->task_state_lock, flags);
567 task->task_done(task);
568 } else
569 spin_unlock_irqrestore(&task->task_state_lock, flags);
570 }
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800571
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700572 if (isci_request != NULL) {
573 spin_lock_irqsave(&isci_host->scic_lock, flags);
574 list_del_init(&isci_request->dev_node);
575 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800576
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700577 isci_request_free(isci_host, isci_request);
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800578 }
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800579}
580
Dan Williams6f231dd2011-07-02 22:56:22 -0700581/**
582 * isci_terminate_request_core() - This function will terminate the given
583 * request, and wait for it to complete. This function must only be called
584 * from a thread that can wait. Note that the request is terminated and
585 * completed (back to the host, if started there).
586 * @isci_host: This SCU.
587 * @isci_device: The target.
588 * @isci_request: The I/O request to be terminated.
589 *
Dan Williams6f231dd2011-07-02 22:56:22 -0700590 */
591static void isci_terminate_request_core(
592 struct isci_host *isci_host,
593 struct isci_remote_device *isci_device,
Jeff Skirvincbb65c62011-03-04 14:06:50 -0800594 struct isci_request *isci_request)
Dan Williams6f231dd2011-07-02 22:56:22 -0700595{
Jeff Skirvincbb65c62011-03-04 14:06:50 -0800596 enum sci_status status = SCI_SUCCESS;
Dan Williams6f231dd2011-07-02 22:56:22 -0700597 bool was_terminated = false;
598 bool needs_cleanup_handling = false;
599 enum isci_request_status request_status;
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700600 unsigned long flags;
601 unsigned long termination_completed = 1;
602 struct completion *io_request_completion;
603 struct sas_task *task;
Dan Williams6f231dd2011-07-02 22:56:22 -0700604
605 dev_dbg(&isci_host->pdev->dev,
606 "%s: device = %p; request = %p\n",
607 __func__, isci_device, isci_request);
608
Dan Williams6f231dd2011-07-02 22:56:22 -0700609 spin_lock_irqsave(&isci_host->scic_lock, flags);
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800610
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700611 io_request_completion = isci_request->io_request_completion;
612
613 task = (isci_request->ttype == io_task)
614 ? isci_request_access_task(isci_request)
615 : NULL;
616
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800617 /* Note that we are not going to control
618 * the target to abort the request.
619 */
620 isci_request->complete_in_target = true;
621
Dan Williams6f231dd2011-07-02 22:56:22 -0700622 /* Make sure the request wasn't just sitting around signalling
623 * device condition (if the request handle is NULL, then the
624 * request completed but needed additional handling here).
625 */
Dan Williams67ea8382011-05-08 11:47:15 -0700626 if (!isci_request->terminated) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700627 was_terminated = true;
Jeff Skirvincbb65c62011-03-04 14:06:50 -0800628 needs_cleanup_handling = true;
Dan Williams6f231dd2011-07-02 22:56:22 -0700629 status = scic_controller_terminate_request(
Artur Wojcikcc3dbd02011-05-04 07:58:16 +0000630 &isci_host->sci,
Dan Williams57f20f42011-04-21 18:14:45 -0700631 &isci_device->sci,
Dan Williams67ea8382011-05-08 11:47:15 -0700632 &isci_request->sci);
Dan Williams6f231dd2011-07-02 22:56:22 -0700633 }
634 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
635
636 /*
637 * The only time the request to terminate will
638 * fail is when the io request is completed and
639 * being aborted.
640 */
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800641 if (status != SCI_SUCCESS) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700642 dev_err(&isci_host->pdev->dev,
643 "%s: scic_controller_terminate_request"
644 " returned = 0x%x\n",
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700645 __func__, status);
646
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800647 isci_request->io_request_completion = NULL;
648
649 } else {
Dan Williams6f231dd2011-07-02 22:56:22 -0700650 if (was_terminated) {
651 dev_dbg(&isci_host->pdev->dev,
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700652 "%s: before completion wait (%p/%p)\n",
653 __func__, isci_request, io_request_completion);
Dan Williams6f231dd2011-07-02 22:56:22 -0700654
655 /* Wait here for the request to complete. */
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700656 #define TERMINATION_TIMEOUT_MSEC 500
657 termination_completed
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800658 = wait_for_completion_timeout(
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700659 io_request_completion,
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800660 msecs_to_jiffies(TERMINATION_TIMEOUT_MSEC));
Dan Williams6f231dd2011-07-02 22:56:22 -0700661
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700662 if (!termination_completed) {
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800663
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700664 /* The request to terminate has timed out. */
665 spin_lock_irqsave(&isci_host->scic_lock,
666 flags);
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800667
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700668 /* Check for state changes. */
669 if (!isci_request->terminated) {
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800670
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700671 /* The best we can do is to have the
672 * request die a silent death if it
673 * ever really completes.
674 *
675 * Set the request state to "dead",
676 * and clear the task pointer so that
677 * an actual completion event callback
678 * doesn't do anything.
679 */
680 isci_request->status = dead;
681 isci_request->io_request_completion
682 = NULL;
683
684 if (isci_request->ttype == io_task) {
685
686 /* Break links with the
687 * sas_task.
688 */
689 isci_request->ttype_ptr.io_task_ptr
690 = NULL;
691 }
692 } else
693 termination_completed = 1;
694
695 spin_unlock_irqrestore(&isci_host->scic_lock,
696 flags);
697
698 if (!termination_completed) {
699
700 dev_err(&isci_host->pdev->dev,
701 "%s: *** Timeout waiting for "
702 "termination(%p/%p)\n",
703 __func__, io_request_completion,
704 isci_request);
705
706 /* The request can no longer be referenced
707 * safely since it may go away if the
708 * termination every really does complete.
709 */
710 isci_request = NULL;
711 }
712 }
713 if (termination_completed)
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800714 dev_dbg(&isci_host->pdev->dev,
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700715 "%s: after completion wait (%p/%p)\n",
716 __func__, isci_request, io_request_completion);
Dan Williams6f231dd2011-07-02 22:56:22 -0700717 }
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800718
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700719 if (termination_completed) {
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800720
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700721 isci_request->io_request_completion = NULL;
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800722
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700723 /* Peek at the status of the request. This will tell
724 * us if there was special handling on the request such that it
725 * needs to be detached and freed here.
726 */
727 spin_lock_irqsave(&isci_request->state_lock, flags);
728 request_status = isci_request_get_state(isci_request);
729
730 if ((isci_request->ttype == io_task) /* TMFs are in their own thread */
731 && ((request_status == aborted)
732 || (request_status == aborting)
733 || (request_status == terminating)
734 || (request_status == completed)
735 || (request_status == dead)
736 )
737 ) {
738
739 /* The completion routine won't free a request in
740 * the aborted/aborting/etc. states, so we do
741 * it here.
742 */
743 needs_cleanup_handling = true;
744 }
745 spin_unlock_irqrestore(&isci_request->state_lock, flags);
746
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800747 }
Dan Williams6f231dd2011-07-02 22:56:22 -0700748 if (needs_cleanup_handling)
749 isci_request_cleanup_completed_loiterer(
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700750 isci_host, isci_device, isci_request, task);
Jeff Skirvina5fde222011-03-04 14:06:42 -0800751 }
Dan Williams6f231dd2011-07-02 22:56:22 -0700752}
753
754/**
755 * isci_terminate_pending_requests() - This function will change the all of the
756 * requests on the given device's state to "aborting", will terminate the
757 * requests, and wait for them to complete. This function must only be
758 * called from a thread that can wait. Note that the requests are all
759 * terminated and completed (back to the host, if started there).
760 * @isci_host: This parameter specifies SCU.
761 * @isci_device: This parameter specifies the target.
762 *
Dan Williams6f231dd2011-07-02 22:56:22 -0700763 */
Dan Williams980d3ae2011-06-20 15:11:22 -0700764void isci_terminate_pending_requests(struct isci_host *ihost,
765 struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -0700766{
Dan Williams980d3ae2011-06-20 15:11:22 -0700767 struct completion request_completion;
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700768 enum isci_request_status old_state;
Dan Williams980d3ae2011-06-20 15:11:22 -0700769 unsigned long flags;
770 LIST_HEAD(list);
Dan Williams6f231dd2011-07-02 22:56:22 -0700771
Dan Williams980d3ae2011-06-20 15:11:22 -0700772 spin_lock_irqsave(&ihost->scic_lock, flags);
773 list_splice_init(&idev->reqs_in_process, &list);
Dan Williams6f231dd2011-07-02 22:56:22 -0700774
Dan Williams980d3ae2011-06-20 15:11:22 -0700775 /* assumes that isci_terminate_request_core deletes from the list */
776 while (!list_empty(&list)) {
777 struct isci_request *ireq = list_entry(list.next, typeof(*ireq), dev_node);
Dan Williams6f231dd2011-07-02 22:56:22 -0700778
Dan Williams980d3ae2011-06-20 15:11:22 -0700779 /* Change state to "terminating" if it is currently
780 * "started".
781 */
782 old_state = isci_request_change_started_to_newstate(ireq,
783 &request_completion,
784 terminating);
785 switch (old_state) {
786 case started:
787 case completed:
788 case aborting:
789 break;
790 default:
791 /* termination in progress, or otherwise dispositioned.
792 * We know the request was on 'list' so should be safe
793 * to move it back to reqs_in_process
794 */
795 list_move(&ireq->dev_node, &idev->reqs_in_process);
796 ireq = NULL;
797 break;
798 }
799
800 if (!ireq)
801 continue;
802 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800803
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700804 init_completion(&request_completion);
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800805
Dan Williams980d3ae2011-06-20 15:11:22 -0700806 dev_dbg(&ihost->pdev->dev,
807 "%s: idev=%p request=%p; task=%p old_state=%d\n",
808 __func__, idev, ireq,
809 ireq->ttype == io_task ? isci_request_access_task(ireq) : NULL,
810 old_state);
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700811
Dan Williams980d3ae2011-06-20 15:11:22 -0700812 /* If the old_state is started:
813 * This request was not already being aborted. If it had been,
814 * then the aborting I/O (ie. the TMF request) would not be in
815 * the aborting state, and thus would be terminated here. Note
816 * that since the TMF completion's call to the kernel function
817 * "complete()" does not happen until the pending I/O request
818 * terminate fully completes, we do not have to implement a
819 * special wait here for already aborting requests - the
820 * termination of the TMF request will force the request
821 * to finish it's already started terminate.
822 *
823 * If old_state == completed:
824 * This request completed from the SCU hardware perspective
825 * and now just needs cleaning up in terms of freeing the
826 * request and potentially calling up to libsas.
827 *
828 * If old_state == aborting:
829 * This request has already gone through a TMF timeout, but may
830 * not have been terminated; needs cleaning up at least.
831 */
832 isci_terminate_request_core(ihost, idev, ireq);
833 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700834 }
Dan Williams980d3ae2011-06-20 15:11:22 -0700835 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700836}
837
838/**
839 * isci_task_send_lu_reset_sas() - This function is called by of the SAS Domain
840 * Template functions.
841 * @lun: This parameter specifies the lun to be reset.
842 *
843 * status, zero indicates success.
844 */
845static int isci_task_send_lu_reset_sas(
846 struct isci_host *isci_host,
847 struct isci_remote_device *isci_device,
848 u8 *lun)
849{
850 struct isci_tmf tmf;
851 int ret = TMF_RESP_FUNC_FAILED;
852
853 dev_dbg(&isci_host->pdev->dev,
854 "%s: isci_host = %p, isci_device = %p\n",
855 __func__, isci_host, isci_device);
856 /* Send the LUN reset to the target. By the time the call returns,
857 * the TMF has fully exected in the target (in which case the return
858 * value is "TMF_RESP_FUNC_COMPLETE", or the request timed-out (or
859 * was otherwise unable to be executed ("TMF_RESP_FUNC_FAILED").
860 */
861 isci_task_build_tmf(&tmf, isci_device, isci_tmf_ssp_lun_reset, NULL,
862 NULL);
863
864 #define ISCI_LU_RESET_TIMEOUT_MS 2000 /* 2 second timeout. */
865 ret = isci_task_execute_tmf(isci_host, &tmf, ISCI_LU_RESET_TIMEOUT_MS);
866
867 if (ret == TMF_RESP_FUNC_COMPLETE)
868 dev_dbg(&isci_host->pdev->dev,
869 "%s: %p: TMF_LU_RESET passed\n",
870 __func__, isci_device);
871 else
872 dev_dbg(&isci_host->pdev->dev,
873 "%s: %p: TMF_LU_RESET failed (%x)\n",
874 __func__, isci_device, ret);
875
876 return ret;
877}
878
879/**
880 * isci_task_lu_reset() - This function is one of the SAS Domain Template
881 * functions. This is one of the Task Management functoins called by libsas,
882 * to reset the given lun. Note the assumption that while this call is
883 * executing, no I/O will be sent by the host to the device.
884 * @lun: This parameter specifies the lun to be reset.
885 *
886 * status, zero indicates success.
887 */
Dan Williams4393aa42011-03-31 13:10:44 -0700888int isci_task_lu_reset(struct domain_device *domain_device, u8 *lun)
Dan Williams6f231dd2011-07-02 22:56:22 -0700889{
Dan Williams4393aa42011-03-31 13:10:44 -0700890 struct isci_host *isci_host = dev_to_ihost(domain_device);
Dan Williams6f231dd2011-07-02 22:56:22 -0700891 struct isci_remote_device *isci_device = NULL;
892 int ret;
893 bool device_stopping = false;
894
Dan Williams4393aa42011-03-31 13:10:44 -0700895 isci_device = domain_device->lldd_dev;
Dan Williams6f231dd2011-07-02 22:56:22 -0700896
Dan Williams4393aa42011-03-31 13:10:44 -0700897 dev_dbg(&isci_host->pdev->dev,
898 "%s: domain_device=%p, isci_host=%p; isci_device=%p\n",
Dan Williams6f231dd2011-07-02 22:56:22 -0700899 __func__, domain_device, isci_host, isci_device);
900
Dan Williamsd06b4872011-05-02 13:59:25 -0700901 if (isci_device != NULL) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700902 device_stopping = (isci_device->status == isci_stopping)
903 || (isci_device->status == isci_stopped);
Dan Williamsd06b4872011-05-02 13:59:25 -0700904 set_bit(IDEV_EH, &isci_device->flags);
905 }
Dan Williams6f231dd2011-07-02 22:56:22 -0700906
907 /* If there is a device reset pending on any request in the
908 * device's list, fail this LUN reset request in order to
909 * escalate to the device reset.
910 */
Dan Williams4393aa42011-03-31 13:10:44 -0700911 if (!isci_device || device_stopping ||
912 isci_device_is_reset_pending(isci_host, isci_device)) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700913 dev_warn(&isci_host->pdev->dev,
Dan Williams4393aa42011-03-31 13:10:44 -0700914 "%s: No dev (%p), or "
Dan Williams6f231dd2011-07-02 22:56:22 -0700915 "RESET PENDING: domain_device=%p\n",
Dan Williams4393aa42011-03-31 13:10:44 -0700916 __func__, isci_device, domain_device);
Dan Williams6f231dd2011-07-02 22:56:22 -0700917 return TMF_RESP_FUNC_FAILED;
918 }
919
Dan Williams6f231dd2011-07-02 22:56:22 -0700920 /* Send the task management part of the reset. */
921 if (sas_protocol_ata(domain_device->tproto)) {
Dan Williams4393aa42011-03-31 13:10:44 -0700922 ret = isci_task_send_lu_reset_sata(isci_host, isci_device, lun);
Dan Williams6f231dd2011-07-02 22:56:22 -0700923 } else
924 ret = isci_task_send_lu_reset_sas(isci_host, isci_device, lun);
925
926 /* If the LUN reset worked, all the I/O can now be terminated. */
927 if (ret == TMF_RESP_FUNC_COMPLETE)
928 /* Terminate all I/O now. */
929 isci_terminate_pending_requests(isci_host,
Dan Williams980d3ae2011-06-20 15:11:22 -0700930 isci_device);
Dan Williams6f231dd2011-07-02 22:56:22 -0700931
Dan Williams6f231dd2011-07-02 22:56:22 -0700932 return ret;
933}
934
935
936/* int (*lldd_clear_nexus_port)(struct asd_sas_port *); */
937int isci_task_clear_nexus_port(struct asd_sas_port *port)
938{
939 return TMF_RESP_FUNC_FAILED;
940}
941
942
943
944int isci_task_clear_nexus_ha(struct sas_ha_struct *ha)
945{
946 return TMF_RESP_FUNC_FAILED;
947}
948
Dan Williams6f231dd2011-07-02 22:56:22 -0700949/* Task Management Functions. Must be called from process context. */
950
951/**
952 * isci_abort_task_process_cb() - This is a helper function for the abort task
953 * TMF command. It manages the request state with respect to the successful
954 * transmission / completion of the abort task request.
955 * @cb_state: This parameter specifies when this function was called - after
956 * the TMF request has been started and after it has timed-out.
957 * @tmf: This parameter specifies the TMF in progress.
958 *
959 *
960 */
961static void isci_abort_task_process_cb(
962 enum isci_tmf_cb_state cb_state,
963 struct isci_tmf *tmf,
964 void *cb_data)
965{
966 struct isci_request *old_request;
967
968 old_request = (struct isci_request *)cb_data;
969
970 dev_dbg(&old_request->isci_host->pdev->dev,
971 "%s: tmf=%p, old_request=%p\n",
972 __func__, tmf, old_request);
973
974 switch (cb_state) {
975
976 case isci_tmf_started:
977 /* The TMF has been started. Nothing to do here, since the
978 * request state was already set to "aborted" by the abort
979 * task function.
980 */
Bartosz Barcinski6cb4d6b2011-04-12 17:28:43 -0700981 if ((old_request->status != aborted)
982 && (old_request->status != completed))
983 dev_err(&old_request->isci_host->pdev->dev,
984 "%s: Bad request status (%d): tmf=%p, old_request=%p\n",
985 __func__, old_request->status, tmf, old_request);
Dan Williams6f231dd2011-07-02 22:56:22 -0700986 break;
987
988 case isci_tmf_timed_out:
989
990 /* Set the task's state to "aborting", since the abort task
991 * function thread set it to "aborted" (above) in anticipation
992 * of the task management request working correctly. Since the
993 * timeout has now fired, the TMF request failed. We set the
994 * state such that the request completion will indicate the
995 * device is no longer present.
996 */
997 isci_request_change_state(old_request, aborting);
998 break;
999
1000 default:
1001 dev_err(&old_request->isci_host->pdev->dev,
1002 "%s: Bad cb_state (%d): tmf=%p, old_request=%p\n",
1003 __func__, cb_state, tmf, old_request);
1004 break;
1005 }
1006}
1007
1008/**
1009 * isci_task_abort_task() - This function is one of the SAS Domain Template
1010 * functions. This function is called by libsas to abort a specified task.
1011 * @task: This parameter specifies the SAS task to abort.
1012 *
1013 * status, zero indicates success.
1014 */
1015int isci_task_abort_task(struct sas_task *task)
1016{
Dan Williams4393aa42011-03-31 13:10:44 -07001017 struct isci_host *isci_host = dev_to_ihost(task->dev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001018 DECLARE_COMPLETION_ONSTACK(aborted_io_completion);
Jeff Skirvina5fde222011-03-04 14:06:42 -08001019 struct isci_request *old_request = NULL;
1020 enum isci_request_status old_state;
Dan Williams6f231dd2011-07-02 22:56:22 -07001021 struct isci_remote_device *isci_device = NULL;
Jeff Skirvina5fde222011-03-04 14:06:42 -08001022 struct isci_tmf tmf;
1023 int ret = TMF_RESP_FUNC_FAILED;
1024 unsigned long flags;
1025 bool any_dev_reset = false;
1026 bool device_stopping;
Dan Williams6f231dd2011-07-02 22:56:22 -07001027
1028 /* Get the isci_request reference from the task. Note that
1029 * this check does not depend on the pending request list
1030 * in the device, because tasks driving resets may land here
1031 * after completion in the core.
1032 */
Dan Williams4393aa42011-03-31 13:10:44 -07001033 old_request = isci_task_get_request_from_task(task, &isci_device);
Dan Williams6f231dd2011-07-02 22:56:22 -07001034
1035 dev_dbg(&isci_host->pdev->dev,
1036 "%s: task = %p\n", __func__, task);
1037
1038 /* Check if the device has been / is currently being removed.
1039 * If so, no task management will be done, and the I/O will
1040 * be terminated.
1041 */
1042 device_stopping = (isci_device->status == isci_stopping)
1043 || (isci_device->status == isci_stopped);
1044
Dan Williamsd06b4872011-05-02 13:59:25 -07001045 /* XXX need to fix device lookup lifetime (needs to be done
1046 * under scic_lock, among other things...), but for now assume
1047 * the device is available like the above code
1048 */
1049 set_bit(IDEV_EH, &isci_device->flags);
1050
Dan Williams6f231dd2011-07-02 22:56:22 -07001051 /* This version of the driver will fail abort requests for
1052 * SATA/STP. Failing the abort request this way will cause the
1053 * SCSI error handler thread to escalate to LUN reset
1054 */
1055 if (sas_protocol_ata(task->task_proto) && !device_stopping) {
1056 dev_warn(&isci_host->pdev->dev,
1057 " task %p is for a STP/SATA device;"
1058 " returning TMF_RESP_FUNC_FAILED\n"
1059 " to cause a LUN reset...\n", task);
1060 return TMF_RESP_FUNC_FAILED;
1061 }
1062
1063 dev_dbg(&isci_host->pdev->dev,
1064 "%s: old_request == %p\n", __func__, old_request);
1065
Jeff Skirvina5fde222011-03-04 14:06:42 -08001066 if (!device_stopping)
1067 any_dev_reset = isci_device_is_reset_pending(isci_host,isci_device);
1068
Dan Williams6f231dd2011-07-02 22:56:22 -07001069 spin_lock_irqsave(&task->task_state_lock, flags);
1070
1071 /* Don't do resets to stopping devices. */
Jeff Skirvina5fde222011-03-04 14:06:42 -08001072 if (device_stopping) {
1073
Dan Williams6f231dd2011-07-02 22:56:22 -07001074 task->task_state_flags &= ~SAS_TASK_NEED_DEV_RESET;
Jeff Skirvina5fde222011-03-04 14:06:42 -08001075 any_dev_reset = false;
Dan Williams6f231dd2011-07-02 22:56:22 -07001076
Jeff Skirvina5fde222011-03-04 14:06:42 -08001077 } else /* See if there is a pending device reset for this device. */
Dan Williams6f231dd2011-07-02 22:56:22 -07001078 any_dev_reset = any_dev_reset
Jeff Skirvina5fde222011-03-04 14:06:42 -08001079 || (task->task_state_flags & SAS_TASK_NEED_DEV_RESET);
Dan Williams6f231dd2011-07-02 22:56:22 -07001080
1081 /* If the extraction of the request reference from the task
1082 * failed, then the request has been completed (or if there is a
1083 * pending reset then this abort request function must be failed
1084 * in order to escalate to the target reset).
1085 */
Jeff Skirvina5fde222011-03-04 14:06:42 -08001086 if ((old_request == NULL) || any_dev_reset) {
Dan Williams6f231dd2011-07-02 22:56:22 -07001087
1088 /* If the device reset task flag is set, fail the task
1089 * management request. Otherwise, the original request
1090 * has completed.
1091 */
1092 if (any_dev_reset) {
1093
1094 /* Turn off the task's DONE to make sure this
1095 * task is escalated to a target reset.
1096 */
1097 task->task_state_flags &= ~SAS_TASK_STATE_DONE;
1098
Jeff Skirvina5fde222011-03-04 14:06:42 -08001099 /* Make the reset happen as soon as possible. */
1100 task->task_state_flags |= SAS_TASK_NEED_DEV_RESET;
1101
1102 spin_unlock_irqrestore(&task->task_state_lock, flags);
1103
Dan Williams6f231dd2011-07-02 22:56:22 -07001104 /* Fail the task management request in order to
1105 * escalate to the target reset.
1106 */
1107 ret = TMF_RESP_FUNC_FAILED;
1108
1109 dev_dbg(&isci_host->pdev->dev,
1110 "%s: Failing task abort in order to "
1111 "escalate to target reset because\n"
1112 "SAS_TASK_NEED_DEV_RESET is set for "
1113 "task %p on dev %p\n",
1114 __func__, task, isci_device);
1115
Jeff Skirvina5fde222011-03-04 14:06:42 -08001116
Dan Williams6f231dd2011-07-02 22:56:22 -07001117 } else {
Dan Williams6f231dd2011-07-02 22:56:22 -07001118 /* The request has already completed and there
1119 * is nothing to do here other than to set the task
1120 * done bit, and indicate that the task abort function
1121 * was sucessful.
1122 */
1123 isci_set_task_doneflags(task);
1124
Jeff Skirvina5fde222011-03-04 14:06:42 -08001125 spin_unlock_irqrestore(&task->task_state_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001126
Jeff Skirvina5fde222011-03-04 14:06:42 -08001127 ret = TMF_RESP_FUNC_COMPLETE;
Dan Williams6f231dd2011-07-02 22:56:22 -07001128
Jeff Skirvina5fde222011-03-04 14:06:42 -08001129 dev_dbg(&isci_host->pdev->dev,
1130 "%s: abort task not needed for %p\n",
1131 __func__, task);
Dan Williams6f231dd2011-07-02 22:56:22 -07001132 }
Dan Williams6f231dd2011-07-02 22:56:22 -07001133
1134 return ret;
1135 }
Jeff Skirvina5fde222011-03-04 14:06:42 -08001136 else
1137 spin_unlock_irqrestore(&task->task_state_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001138
1139 spin_lock_irqsave(&isci_host->scic_lock, flags);
1140
Jeff Skirvinf219f012011-03-31 13:10:34 -07001141 /* Check the request status and change to "aborted" if currently
Jeff Skirvina5fde222011-03-04 14:06:42 -08001142 * "starting"; if true then set the I/O kernel completion
Dan Williams6f231dd2011-07-02 22:56:22 -07001143 * struct that will be triggered when the request completes.
1144 */
Jeff Skirvina5fde222011-03-04 14:06:42 -08001145 old_state = isci_task_validate_request_to_abort(
1146 old_request, isci_host, isci_device,
1147 &aborted_io_completion);
Jeff Skirvinf219f012011-03-31 13:10:34 -07001148 if ((old_state != started) &&
1149 (old_state != completed) &&
1150 (old_state != aborting)) {
Dan Williams6f231dd2011-07-02 22:56:22 -07001151
1152 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1153
Jeff Skirvina5fde222011-03-04 14:06:42 -08001154 /* The request was already being handled by someone else (because
1155 * they got to set the state away from started).
1156 */
1157 dev_dbg(&isci_host->pdev->dev,
1158 "%s: device = %p; old_request %p already being aborted\n",
1159 __func__,
1160 isci_device, old_request);
Dan Williams6f231dd2011-07-02 22:56:22 -07001161
1162 return TMF_RESP_FUNC_COMPLETE;
1163 }
Jeff Skirvina5fde222011-03-04 14:06:42 -08001164 if ((task->task_proto == SAS_PROTOCOL_SMP)
1165 || device_stopping
1166 || old_request->complete_in_target
1167 ) {
Dan Williams6f231dd2011-07-02 22:56:22 -07001168
1169 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1170
Jeff Skirvina5fde222011-03-04 14:06:42 -08001171 dev_dbg(&isci_host->pdev->dev,
1172 "%s: SMP request (%d)"
1173 " or device is stopping (%d)"
1174 " or complete_in_target (%d), thus no TMF\n",
1175 __func__, (task->task_proto == SAS_PROTOCOL_SMP),
1176 device_stopping, old_request->complete_in_target);
1177
Dan Williams6f231dd2011-07-02 22:56:22 -07001178 /* Set the state on the task. */
1179 isci_task_all_done(task);
1180
1181 ret = TMF_RESP_FUNC_COMPLETE;
1182
1183 /* Stopping and SMP devices are not sent a TMF, and are not
Jeff Skirvina5fde222011-03-04 14:06:42 -08001184 * reset, but the outstanding I/O request is terminated below.
Dan Williams6f231dd2011-07-02 22:56:22 -07001185 */
Dan Williams6f231dd2011-07-02 22:56:22 -07001186 } else {
1187 /* Fill in the tmf stucture */
Jeff Skirvinc3f42fe2011-03-04 14:06:56 -08001188 isci_task_build_abort_task_tmf(&tmf, isci_device,
1189 isci_tmf_ssp_task_abort,
1190 isci_abort_task_process_cb,
1191 old_request);
Dan Williams6f231dd2011-07-02 22:56:22 -07001192
Dan Williams6f231dd2011-07-02 22:56:22 -07001193 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1194
1195 #define ISCI_ABORT_TASK_TIMEOUT_MS 500 /* half second timeout. */
1196 ret = isci_task_execute_tmf(isci_host, &tmf,
1197 ISCI_ABORT_TASK_TIMEOUT_MS);
1198
Jeff Skirvina5fde222011-03-04 14:06:42 -08001199 if (ret != TMF_RESP_FUNC_COMPLETE)
Dan Williams6f231dd2011-07-02 22:56:22 -07001200 dev_err(&isci_host->pdev->dev,
1201 "%s: isci_task_send_tmf failed\n",
1202 __func__);
1203 }
Jeff Skirvina5fde222011-03-04 14:06:42 -08001204 if (ret == TMF_RESP_FUNC_COMPLETE) {
1205 old_request->complete_in_target = true;
Dan Williams6f231dd2011-07-02 22:56:22 -07001206
Jeff Skirvin77c852f2011-06-20 14:09:16 -07001207 /* Clean up the request on our side, and wait for the aborted
1208 * I/O to complete.
1209 */
Jeff Skirvincbb65c62011-03-04 14:06:50 -08001210 isci_terminate_request_core(isci_host, isci_device, old_request);
Jeff Skirvina5fde222011-03-04 14:06:42 -08001211 }
1212
1213 /* Make sure we do not leave a reference to aborted_io_completion */
1214 old_request->io_request_completion = NULL;
Dan Williams6f231dd2011-07-02 22:56:22 -07001215 return ret;
1216}
1217
1218/**
1219 * isci_task_abort_task_set() - This function is one of the SAS Domain Template
1220 * functions. This is one of the Task Management functoins called by libsas,
1221 * to abort all task for the given lun.
1222 * @d_device: This parameter specifies the domain device associated with this
1223 * request.
1224 * @lun: This parameter specifies the lun associated with this request.
1225 *
1226 * status, zero indicates success.
1227 */
1228int isci_task_abort_task_set(
1229 struct domain_device *d_device,
1230 u8 *lun)
1231{
1232 return TMF_RESP_FUNC_FAILED;
1233}
1234
1235
1236/**
1237 * isci_task_clear_aca() - This function is one of the SAS Domain Template
1238 * functions. This is one of the Task Management functoins called by libsas.
1239 * @d_device: This parameter specifies the domain device associated with this
1240 * request.
1241 * @lun: This parameter specifies the lun associated with this request.
1242 *
1243 * status, zero indicates success.
1244 */
1245int isci_task_clear_aca(
1246 struct domain_device *d_device,
1247 u8 *lun)
1248{
1249 return TMF_RESP_FUNC_FAILED;
1250}
1251
1252
1253
1254/**
1255 * isci_task_clear_task_set() - This function is one of the SAS Domain Template
1256 * functions. This is one of the Task Management functoins called by libsas.
1257 * @d_device: This parameter specifies the domain device associated with this
1258 * request.
1259 * @lun: This parameter specifies the lun associated with this request.
1260 *
1261 * status, zero indicates success.
1262 */
1263int isci_task_clear_task_set(
1264 struct domain_device *d_device,
1265 u8 *lun)
1266{
1267 return TMF_RESP_FUNC_FAILED;
1268}
1269
1270
1271/**
1272 * isci_task_query_task() - This function is implemented to cause libsas to
1273 * correctly escalate the failed abort to a LUN or target reset (this is
1274 * because sas_scsi_find_task libsas function does not correctly interpret
1275 * all return codes from the abort task call). When TMF_RESP_FUNC_SUCC is
1276 * returned, libsas turns this into a LUN reset; when FUNC_FAILED is
1277 * returned, libsas will turn this into a target reset
1278 * @task: This parameter specifies the sas task being queried.
1279 * @lun: This parameter specifies the lun associated with this request.
1280 *
1281 * status, zero indicates success.
1282 */
1283int isci_task_query_task(
1284 struct sas_task *task)
1285{
1286 /* See if there is a pending device reset for this device. */
1287 if (task->task_state_flags & SAS_TASK_NEED_DEV_RESET)
1288 return TMF_RESP_FUNC_FAILED;
1289 else
1290 return TMF_RESP_FUNC_SUCC;
1291}
1292
Dave Jiangaf5ae892011-05-04 17:53:24 -07001293/*
Dan Williams6f231dd2011-07-02 22:56:22 -07001294 * isci_task_request_complete() - This function is called by the sci core when
1295 * an task request completes.
Dave Jiangaf5ae892011-05-04 17:53:24 -07001296 * @ihost: This parameter specifies the ISCI host object
1297 * @ireq: This parameter is the completed isci_request object.
Dan Williams6f231dd2011-07-02 22:56:22 -07001298 * @completion_status: This parameter specifies the completion status from the
1299 * sci core.
1300 *
1301 * none.
1302 */
Dave Jiangaf5ae892011-05-04 17:53:24 -07001303void
1304isci_task_request_complete(struct isci_host *ihost,
1305 struct isci_request *ireq,
1306 enum sci_task_status completion_status)
Dan Williams6f231dd2011-07-02 22:56:22 -07001307{
Dave Jiangaf5ae892011-05-04 17:53:24 -07001308 struct isci_remote_device *idev = ireq->isci_device;
Dave Jiangaf5ae892011-05-04 17:53:24 -07001309 struct isci_tmf *tmf = isci_request_access_tmf(ireq);
Dan Williams6f231dd2011-07-02 22:56:22 -07001310 struct completion *tmf_complete;
Dan Williams67ea8382011-05-08 11:47:15 -07001311 struct scic_sds_request *sci_req = &ireq->sci;
Dan Williams6f231dd2011-07-02 22:56:22 -07001312
Dave Jiangaf5ae892011-05-04 17:53:24 -07001313 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -07001314 "%s: request = %p, status=%d\n",
Dave Jiangaf5ae892011-05-04 17:53:24 -07001315 __func__, ireq, completion_status);
Dan Williams6f231dd2011-07-02 22:56:22 -07001316
Dave Jiang8d2c65c2011-06-01 09:03:08 +00001317 isci_request_change_state(ireq, completed);
Dan Williams6f231dd2011-07-02 22:56:22 -07001318
1319 tmf->status = completion_status;
Dave Jiangaf5ae892011-05-04 17:53:24 -07001320 ireq->complete_in_target = true;
Dan Williams6f231dd2011-07-02 22:56:22 -07001321
Dave Jiangaf5ae892011-05-04 17:53:24 -07001322 if (tmf->proto == SAS_PROTOCOL_SSP) {
Dan Williams6f231dd2011-07-02 22:56:22 -07001323 memcpy(&tmf->resp.resp_iu,
Dan Williamsb7645812011-05-08 02:35:32 -07001324 &sci_req->ssp.rsp,
Dave Jiangaf5ae892011-05-04 17:53:24 -07001325 SSP_RESP_IU_MAX_SIZE);
1326 } else if (tmf->proto == SAS_PROTOCOL_SATA) {
Dan Williams6f231dd2011-07-02 22:56:22 -07001327 memcpy(&tmf->resp.d2h_fis,
Dan Williamsb7645812011-05-08 02:35:32 -07001328 &sci_req->stp.rsp,
Dave Jiangf2f30082011-05-04 15:02:02 -07001329 sizeof(struct dev_to_host_fis));
Dan Williams6f231dd2011-07-02 22:56:22 -07001330 }
1331
Dan Williams6f231dd2011-07-02 22:56:22 -07001332 /* PRINT_TMF( ((struct isci_tmf *)request->task)); */
1333 tmf_complete = tmf->complete;
1334
Dan Williams67ea8382011-05-08 11:47:15 -07001335 scic_controller_complete_io(&ihost->sci, &idev->sci, &ireq->sci);
1336 /* set the 'terminated' flag handle to make sure it cannot be terminated
Dan Williams6f231dd2011-07-02 22:56:22 -07001337 * or completed again.
1338 */
Dan Williams67ea8382011-05-08 11:47:15 -07001339 ireq->terminated = true;;
Dan Williams6f231dd2011-07-02 22:56:22 -07001340
Dave Jiangaf5ae892011-05-04 17:53:24 -07001341 isci_request_change_state(ireq, unallocated);
1342 list_del_init(&ireq->dev_node);
Dan Williams6f231dd2011-07-02 22:56:22 -07001343
1344 /* The task management part completes last. */
1345 complete(tmf_complete);
1346}
1347
Jeff Skirvin61aaff42011-06-21 12:16:33 -07001348static void isci_smp_task_timedout(unsigned long _task)
1349{
1350 struct sas_task *task = (void *) _task;
1351 unsigned long flags;
1352
1353 spin_lock_irqsave(&task->task_state_lock, flags);
1354 if (!(task->task_state_flags & SAS_TASK_STATE_DONE))
1355 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
1356 spin_unlock_irqrestore(&task->task_state_lock, flags);
1357
1358 complete(&task->completion);
1359}
1360
1361static void isci_smp_task_done(struct sas_task *task)
1362{
1363 if (!del_timer(&task->timer))
1364 return;
1365 complete(&task->completion);
1366}
1367
1368static struct sas_task *isci_alloc_task(void)
1369{
1370 struct sas_task *task = kzalloc(sizeof(*task), GFP_KERNEL);
1371
1372 if (task) {
1373 INIT_LIST_HEAD(&task->list);
1374 spin_lock_init(&task->task_state_lock);
1375 task->task_state_flags = SAS_TASK_STATE_PENDING;
1376 init_timer(&task->timer);
1377 init_completion(&task->completion);
1378 }
1379
1380 return task;
1381}
1382
1383static void isci_free_task(struct isci_host *ihost, struct sas_task *task)
1384{
1385 if (task) {
1386 BUG_ON(!list_empty(&task->list));
1387 kfree(task);
1388 }
1389}
1390
1391static int isci_smp_execute_task(struct isci_host *ihost,
1392 struct domain_device *dev, void *req,
1393 int req_size, void *resp, int resp_size)
1394{
1395 int res, retry;
1396 struct sas_task *task = NULL;
1397
1398 for (retry = 0; retry < 3; retry++) {
1399 task = isci_alloc_task();
1400 if (!task)
1401 return -ENOMEM;
1402
1403 task->dev = dev;
1404 task->task_proto = dev->tproto;
1405 sg_init_one(&task->smp_task.smp_req, req, req_size);
1406 sg_init_one(&task->smp_task.smp_resp, resp, resp_size);
1407
1408 task->task_done = isci_smp_task_done;
1409
1410 task->timer.data = (unsigned long) task;
1411 task->timer.function = isci_smp_task_timedout;
1412 task->timer.expires = jiffies + 10*HZ;
1413 add_timer(&task->timer);
1414
1415 res = isci_task_execute_task(task, 1, GFP_KERNEL);
1416
1417 if (res) {
1418 del_timer(&task->timer);
1419 dev_err(&ihost->pdev->dev,
1420 "%s: executing SMP task failed:%d\n",
1421 __func__, res);
1422 goto ex_err;
1423 }
1424
1425 wait_for_completion(&task->completion);
1426 res = -ECOMM;
1427 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
1428 dev_err(&ihost->pdev->dev,
1429 "%s: smp task timed out or aborted\n",
1430 __func__);
1431 isci_task_abort_task(task);
1432 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
1433 dev_err(&ihost->pdev->dev,
1434 "%s: SMP task aborted and not done\n",
1435 __func__);
1436 goto ex_err;
1437 }
1438 }
1439 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1440 task->task_status.stat == SAM_STAT_GOOD) {
1441 res = 0;
1442 break;
1443 }
1444 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1445 task->task_status.stat == SAS_DATA_UNDERRUN) {
1446 /* no error, but return the number of bytes of
1447 * underrun */
1448 res = task->task_status.residual;
1449 break;
1450 }
1451 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1452 task->task_status.stat == SAS_DATA_OVERRUN) {
1453 res = -EMSGSIZE;
1454 break;
1455 } else {
1456 dev_err(&ihost->pdev->dev,
1457 "%s: task to dev %016llx response: 0x%x "
1458 "status 0x%x\n", __func__,
1459 SAS_ADDR(dev->sas_addr),
1460 task->task_status.resp,
1461 task->task_status.stat);
1462 isci_free_task(ihost, task);
1463 task = NULL;
1464 }
1465 }
1466ex_err:
1467 BUG_ON(retry == 3 && task != NULL);
1468 isci_free_task(ihost, task);
1469 return res;
1470}
1471
1472#define DISCOVER_REQ_SIZE 16
1473#define DISCOVER_RESP_SIZE 56
1474
1475int isci_smp_get_phy_attached_dev_type(struct isci_host *ihost,
1476 struct domain_device *dev,
1477 int phy_id, int *adt)
1478{
1479 struct smp_resp *disc_resp;
1480 u8 *disc_req;
1481 int res;
1482
1483 disc_resp = kzalloc(DISCOVER_RESP_SIZE, GFP_KERNEL);
1484 if (!disc_resp)
1485 return -ENOMEM;
1486
1487 disc_req = kzalloc(DISCOVER_REQ_SIZE, GFP_KERNEL);
1488 if (disc_req) {
1489 disc_req[0] = SMP_REQUEST;
1490 disc_req[1] = SMP_DISCOVER;
1491 disc_req[9] = phy_id;
1492 } else {
1493 kfree(disc_resp);
1494 return -ENOMEM;
1495 }
1496 res = isci_smp_execute_task(ihost, dev, disc_req, DISCOVER_REQ_SIZE,
1497 disc_resp, DISCOVER_RESP_SIZE);
1498 if (!res) {
1499 if (disc_resp->result != SMP_RESP_FUNC_ACC)
1500 res = disc_resp->result;
1501 else
1502 *adt = disc_resp->disc.attached_dev_type;
1503 }
1504 kfree(disc_req);
1505 kfree(disc_resp);
1506
1507 return res;
1508}
1509
1510static void isci_wait_for_smp_phy_reset(struct isci_remote_device *idev, int phy_num)
1511{
1512 struct domain_device *dev = idev->domain_dev;
1513 struct isci_port *iport = idev->isci_port;
1514 struct isci_host *ihost = iport->isci_host;
1515 int res, iteration = 0, attached_device_type;
1516 #define STP_WAIT_MSECS 25000
1517 unsigned long tmo = msecs_to_jiffies(STP_WAIT_MSECS);
1518 unsigned long deadline = jiffies + tmo;
1519 enum {
1520 SMP_PHYWAIT_PHYDOWN,
1521 SMP_PHYWAIT_PHYUP,
1522 SMP_PHYWAIT_DONE
1523 } phy_state = SMP_PHYWAIT_PHYDOWN;
1524
1525 /* While there is time, wait for the phy to go away and come back */
1526 while (time_is_after_jiffies(deadline) && phy_state != SMP_PHYWAIT_DONE) {
1527 int event = atomic_read(&iport->event);
1528
1529 ++iteration;
1530
1531 tmo = wait_event_timeout(ihost->eventq,
1532 event != atomic_read(&iport->event) ||
1533 !test_bit(IPORT_BCN_BLOCKED, &iport->flags),
1534 tmo);
1535 /* link down, stop polling */
1536 if (!test_bit(IPORT_BCN_BLOCKED, &iport->flags))
1537 break;
1538
1539 dev_dbg(&ihost->pdev->dev,
1540 "%s: iport %p, iteration %d,"
1541 " phase %d: time_remaining %lu, bcns = %d\n",
1542 __func__, iport, iteration, phy_state,
1543 tmo, test_bit(IPORT_BCN_PENDING, &iport->flags));
1544
1545 res = isci_smp_get_phy_attached_dev_type(ihost, dev, phy_num,
1546 &attached_device_type);
1547 tmo = deadline - jiffies;
1548
1549 if (res) {
1550 dev_warn(&ihost->pdev->dev,
1551 "%s: iteration %d, phase %d:"
1552 " SMP error=%d, time_remaining=%lu\n",
1553 __func__, iteration, phy_state, res, tmo);
1554 break;
1555 }
1556 dev_dbg(&ihost->pdev->dev,
1557 "%s: iport %p, iteration %d,"
1558 " phase %d: time_remaining %lu, bcns = %d, "
1559 "attdevtype = %x\n",
1560 __func__, iport, iteration, phy_state,
1561 tmo, test_bit(IPORT_BCN_PENDING, &iport->flags),
1562 attached_device_type);
1563
1564 switch (phy_state) {
1565 case SMP_PHYWAIT_PHYDOWN:
1566 /* Has the device gone away? */
1567 if (!attached_device_type)
1568 phy_state = SMP_PHYWAIT_PHYUP;
1569
1570 break;
1571
1572 case SMP_PHYWAIT_PHYUP:
1573 /* Has the device come back? */
1574 if (attached_device_type)
1575 phy_state = SMP_PHYWAIT_DONE;
1576 break;
1577
1578 case SMP_PHYWAIT_DONE:
1579 break;
1580 }
1581
1582 }
1583 dev_dbg(&ihost->pdev->dev, "%s: done\n", __func__);
1584}
1585
Dan Williamsd06b4872011-05-02 13:59:25 -07001586static int isci_reset_device(struct domain_device *dev, int hard_reset)
Dan Williams6f231dd2011-07-02 22:56:22 -07001587{
Dan Williamsd06b4872011-05-02 13:59:25 -07001588 struct isci_remote_device *idev = dev->lldd_dev;
1589 struct sas_phy *phy = sas_find_local_phy(dev);
1590 struct isci_host *ihost = dev_to_ihost(dev);
Jeff Skirvin61aaff42011-06-21 12:16:33 -07001591 struct isci_port *iport = idev->isci_port;
Dan Williams6f231dd2011-07-02 22:56:22 -07001592 enum sci_status status;
Dan Williamsd06b4872011-05-02 13:59:25 -07001593 unsigned long flags;
1594 int rc;
Dan Williams6f231dd2011-07-02 22:56:22 -07001595
Dan Williamsd06b4872011-05-02 13:59:25 -07001596 dev_dbg(&ihost->pdev->dev, "%s: idev %p\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001597
Dan Williamsd06b4872011-05-02 13:59:25 -07001598 if (!idev) {
1599 dev_warn(&ihost->pdev->dev,
1600 "%s: idev is GONE!\n",
Dan Williams6f231dd2011-07-02 22:56:22 -07001601 __func__);
1602
1603 return TMF_RESP_FUNC_COMPLETE; /* Nothing to reset. */
1604 }
1605
Dan Williamsd06b4872011-05-02 13:59:25 -07001606 spin_lock_irqsave(&ihost->scic_lock, flags);
1607 status = scic_remote_device_reset(&idev->sci);
Dan Williams6f231dd2011-07-02 22:56:22 -07001608 if (status != SCI_SUCCESS) {
Dan Williamsd06b4872011-05-02 13:59:25 -07001609 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001610
Dan Williamsd06b4872011-05-02 13:59:25 -07001611 dev_warn(&ihost->pdev->dev,
1612 "%s: scic_remote_device_reset(%p) returned %d!\n",
1613 __func__, idev, status);
Dan Williams6f231dd2011-07-02 22:56:22 -07001614
1615 return TMF_RESP_FUNC_FAILED;
1616 }
Dan Williamsd06b4872011-05-02 13:59:25 -07001617 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001618
Dan Williams6f231dd2011-07-02 22:56:22 -07001619 /* Make sure all pending requests are able to be fully terminated. */
Dan Williamsd06b4872011-05-02 13:59:25 -07001620 isci_device_clear_reset_pending(ihost, idev);
1621
Jeff Skirvin61aaff42011-06-21 12:16:33 -07001622 /* If this is a device on an expander, disable BCN processing. */
1623 if (!scsi_is_sas_phy_local(phy))
1624 set_bit(IPORT_BCN_BLOCKED, &iport->flags);
1625
Dan Williamsd06b4872011-05-02 13:59:25 -07001626 rc = sas_phy_reset(phy, hard_reset);
Dan Williams6f231dd2011-07-02 22:56:22 -07001627
1628 /* Terminate in-progress I/O now. */
Dan Williamsd06b4872011-05-02 13:59:25 -07001629 isci_remote_device_nuke_requests(ihost, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001630
Jeff Skirvinff717ab2011-06-20 14:08:51 -07001631 /* Since all pending TCs have been cleaned, resume the RNC. */
Dan Williamsd06b4872011-05-02 13:59:25 -07001632 spin_lock_irqsave(&ihost->scic_lock, flags);
1633 status = scic_remote_device_reset_complete(&idev->sci);
1634 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001635
Jeff Skirvin61aaff42011-06-21 12:16:33 -07001636 /* If this is a device on an expander, bring the phy back up. */
1637 if (!scsi_is_sas_phy_local(phy)) {
1638 /* A phy reset will cause the device to go away then reappear.
1639 * Since libsas will take action on incoming BCNs (eg. remove
1640 * a device going through an SMP phy-control driven reset),
1641 * we need to wait until the phy comes back up before letting
1642 * discovery proceed in libsas.
1643 */
1644 isci_wait_for_smp_phy_reset(idev, phy->number);
1645
1646 spin_lock_irqsave(&ihost->scic_lock, flags);
1647 isci_port_bcn_enable(ihost, idev->isci_port);
1648 spin_unlock_irqrestore(&ihost->scic_lock, flags);
1649 }
Jeff Skirvinff717ab2011-06-20 14:08:51 -07001650
Dan Williams6f231dd2011-07-02 22:56:22 -07001651 if (status != SCI_SUCCESS) {
Dan Williamsd06b4872011-05-02 13:59:25 -07001652 dev_warn(&ihost->pdev->dev,
1653 "%s: scic_remote_device_reset_complete(%p) "
1654 "returned %d!\n", __func__, idev, status);
Dan Williams6f231dd2011-07-02 22:56:22 -07001655 }
Dan Williams6f231dd2011-07-02 22:56:22 -07001656
Dan Williamsd06b4872011-05-02 13:59:25 -07001657 dev_dbg(&ihost->pdev->dev, "%s: idev %p complete.\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001658
Dan Williamsd06b4872011-05-02 13:59:25 -07001659 return rc;
1660}
1661
1662int isci_task_I_T_nexus_reset(struct domain_device *dev)
1663{
1664 struct isci_host *ihost = dev_to_ihost(dev);
1665 int ret = TMF_RESP_FUNC_FAILED, hard_reset = 1;
1666 struct isci_remote_device *idev;
1667 unsigned long flags;
1668
1669 /* XXX mvsas is not protecting against ->lldd_dev_gone(), are we
1670 * being too paranoid, or is mvsas busted?!
1671 */
1672 spin_lock_irqsave(&ihost->scic_lock, flags);
1673 idev = dev->lldd_dev;
1674 if (!idev || !test_bit(IDEV_EH, &idev->flags))
1675 ret = TMF_RESP_FUNC_COMPLETE;
1676 spin_unlock_irqrestore(&ihost->scic_lock, flags);
1677
1678 if (ret == TMF_RESP_FUNC_COMPLETE)
1679 return ret;
1680
1681 if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP))
1682 hard_reset = 0;
1683
1684 return isci_reset_device(dev, hard_reset);
1685}
1686
1687int isci_bus_reset_handler(struct scsi_cmnd *cmd)
1688{
1689 struct domain_device *dev = sdev_to_domain_dev(cmd->device);
1690 int hard_reset = 1;
1691
1692 if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP))
1693 hard_reset = 0;
1694
1695 return isci_reset_device(dev, hard_reset);
Dan Williams6f231dd2011-07-02 22:56:22 -07001696}