blob: 0835a2c2dc713461ebfe588728e4ae90f27a047c [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 enum sci_status status;
Dan Williamsf2088262011-06-16 11:26:12 -0700151 unsigned long flags;
152 bool io_ready;
153 int ret;
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) {
Dan Williams209fae12011-06-13 17:39:44 -0700166 spin_lock_irqsave(&ihost->scic_lock, flags);
167 idev = isci_lookup_device(task->dev);
Dan Williamsf2088262011-06-16 11:26:12 -0700168 io_ready = idev ? test_bit(IDEV_IO_READY, &idev->flags) : 0;
Dan Williams209fae12011-06-13 17:39:44 -0700169 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700170
Dan Williamsf2088262011-06-16 11:26:12 -0700171 dev_dbg(&ihost->pdev->dev,
172 "task: %p, num: %d dev: %p idev: %p:%#lx cmd = %p\n",
173 task, num, task->dev, idev, idev ? idev->flags : 0,
174 task->uldd_task);
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700175
Dan Williamsf2088262011-06-16 11:26:12 -0700176 if (!idev) {
177 isci_task_refuse(ihost, task, SAS_TASK_UNDELIVERED,
178 SAS_DEVICE_UNKNOWN);
179 isci_host_can_dequeue(ihost, 1);
180 } else if (!io_ready) {
181 /* Indicate QUEUE_FULL so that the scsi midlayer
182 * retries.
183 */
184 isci_task_refuse(ihost, task, SAS_TASK_COMPLETE,
185 SAS_QUEUE_FULL);
Dan Williams1077a572011-03-11 10:13:51 -0800186 isci_host_can_dequeue(ihost, 1);
Dan Williams6f231dd2011-07-02 22:56:22 -0700187 } else {
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700188 /* There is a device and it's ready for I/O. */
189 spin_lock_irqsave(&task->task_state_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700190
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700191 if (task->task_state_flags & SAS_TASK_STATE_ABORTED) {
Dan Williamsf2088262011-06-16 11:26:12 -0700192 /* The I/O was aborted. */
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700193 spin_unlock_irqrestore(&task->task_state_lock,
194 flags);
195
Dan Williams1077a572011-03-11 10:13:51 -0800196 isci_task_refuse(ihost, task,
197 SAS_TASK_UNDELIVERED,
198 SAM_STAT_TASK_ABORTED);
Dan Williamsf2088262011-06-16 11:26:12 -0700199 isci_host_can_dequeue(ihost, 1);
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700200 } else {
Dan Williams6f231dd2011-07-02 22:56:22 -0700201 task->task_state_flags |= SAS_TASK_AT_INITIATOR;
202 spin_unlock_irqrestore(&task->task_state_lock, flags);
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700203
204 /* build and send the request. */
Dan Williams209fae12011-06-13 17:39:44 -0700205 status = isci_request_execute(ihost, idev, task, gfp_flags);
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700206
207 if (status != SCI_SUCCESS) {
208
209 spin_lock_irqsave(&task->task_state_lock, flags);
210 /* Did not really start this command. */
211 task->task_state_flags &= ~SAS_TASK_AT_INITIATOR;
212 spin_unlock_irqrestore(&task->task_state_lock, flags);
213
214 /* Indicate QUEUE_FULL so that the scsi
215 * midlayer retries. if the request
216 * failed for remote device reasons,
217 * it gets returned as
218 * SAS_TASK_UNDELIVERED next time
219 * through.
220 */
Dan Williams1077a572011-03-11 10:13:51 -0800221 isci_task_refuse(ihost, task,
222 SAS_TASK_COMPLETE,
223 SAS_QUEUE_FULL);
224 isci_host_can_dequeue(ihost, 1);
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700225 }
Dan Williams6f231dd2011-07-02 22:56:22 -0700226 }
227 }
Dan Williams209fae12011-06-13 17:39:44 -0700228 isci_put_device(idev);
Dan Williams1077a572011-03-11 10:13:51 -0800229 }
Dan Williams6f231dd2011-07-02 22:56:22 -0700230 return 0;
231}
232
Dan Williams0d0cf142011-06-13 00:51:30 -0700233static struct isci_request *isci_task_request_build(struct isci_host *ihost,
Dan Williams209fae12011-06-13 17:39:44 -0700234 struct isci_remote_device *idev,
Dan Williams0d0cf142011-06-13 00:51:30 -0700235 struct isci_tmf *isci_tmf)
Dan Williams6f231dd2011-07-02 22:56:22 -0700236{
Dan Williams6f231dd2011-07-02 22:56:22 -0700237 enum sci_status status = SCI_FAILURE;
Dan Williams0d0cf142011-06-13 00:51:30 -0700238 struct isci_request *ireq = NULL;
Dan Williamsa1a113b2011-04-21 18:44:45 -0700239 struct domain_device *dev;
Dan Williams6f231dd2011-07-02 22:56:22 -0700240
Dan Williams0d0cf142011-06-13 00:51:30 -0700241 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -0700242 "%s: isci_tmf = %p\n", __func__, isci_tmf);
243
Dan Williams0d0cf142011-06-13 00:51:30 -0700244 dev = idev->domain_dev;
Dan Williams6f231dd2011-07-02 22:56:22 -0700245
246 /* do common allocation and init of request object. */
Dan Williams209fae12011-06-13 17:39:44 -0700247 ireq = isci_request_alloc_tmf(ihost, isci_tmf, GFP_ATOMIC);
Dan Williams0d0cf142011-06-13 00:51:30 -0700248 if (!ireq)
249 return NULL;
Dan Williams6f231dd2011-07-02 22:56:22 -0700250
251 /* let the core do it's construct. */
Dan Williams209fae12011-06-13 17:39:44 -0700252 status = scic_task_request_construct(&ihost->sci, &idev->sci,
Dan Williams0d843662011-05-08 01:56:57 -0700253 SCI_CONTROLLER_INVALID_IO_TAG,
Dan Williams0d0cf142011-06-13 00:51:30 -0700254 &ireq->sci);
Dan Williams6f231dd2011-07-02 22:56:22 -0700255
256 if (status != SCI_SUCCESS) {
Dan Williams0d0cf142011-06-13 00:51:30 -0700257 dev_warn(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -0700258 "%s: scic_task_request_construct failed - "
259 "status = 0x%x\n",
260 __func__,
261 status);
262 goto errout;
263 }
264
Dan Williamsa1a113b2011-04-21 18:44:45 -0700265 /* XXX convert to get this from task->tproto like other drivers */
266 if (dev->dev_type == SAS_END_DEV) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700267 isci_tmf->proto = SAS_PROTOCOL_SSP;
Dan Williams0d0cf142011-06-13 00:51:30 -0700268 status = scic_task_request_construct_ssp(&ireq->sci);
Dan Williams6f231dd2011-07-02 22:56:22 -0700269 if (status != SCI_SUCCESS)
270 goto errout;
271 }
272
Dan Williamsa1a113b2011-04-21 18:44:45 -0700273 if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP)) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700274 isci_tmf->proto = SAS_PROTOCOL_SATA;
Dan Williams0d0cf142011-06-13 00:51:30 -0700275 status = isci_sata_management_task_request_build(ireq);
Dan Williams6f231dd2011-07-02 22:56:22 -0700276
277 if (status != SCI_SUCCESS)
278 goto errout;
279 }
Dan Williams0d0cf142011-06-13 00:51:30 -0700280 return ireq;
Dan Williams6f231dd2011-07-02 22:56:22 -0700281 errout:
Dan Williams0d0cf142011-06-13 00:51:30 -0700282 isci_request_free(ihost, ireq);
283 ireq = NULL;
284 return ireq;
Dan Williams6f231dd2011-07-02 22:56:22 -0700285}
286
Dan Williams209fae12011-06-13 17:39:44 -0700287int isci_task_execute_tmf(struct isci_host *ihost,
288 struct isci_remote_device *isci_device,
289 struct isci_tmf *tmf, unsigned long timeout_ms)
Dan Williams6f231dd2011-07-02 22:56:22 -0700290{
291 DECLARE_COMPLETION_ONSTACK(completion);
Bartosz Barcinski467e8552011-04-12 17:28:41 -0700292 enum sci_task_status status = SCI_TASK_FAILURE;
Dan Williams6f231dd2011-07-02 22:56:22 -0700293 struct scic_sds_remote_device *sci_device;
Dan Williams0d0cf142011-06-13 00:51:30 -0700294 struct isci_request *ireq;
Dan Williams6f231dd2011-07-02 22:56:22 -0700295 int ret = TMF_RESP_FUNC_FAILED;
296 unsigned long flags;
Edmund Nadolskifd183882011-05-19 12:00:15 +0000297 unsigned long timeleft;
Dan Williams6f231dd2011-07-02 22:56:22 -0700298
299 /* sanity check, return TMF_RESP_FUNC_FAILED
300 * if the device is not there and ready.
301 */
Dan Williamsf2088262011-06-16 11:26:12 -0700302 if (!isci_device || !test_bit(IDEV_IO_READY, &isci_device->flags)) {
Dan Williams0d0cf142011-06-13 00:51:30 -0700303 dev_dbg(&ihost->pdev->dev,
Dan Williamsf2088262011-06-16 11:26:12 -0700304 "%s: isci_device = %p not ready (%#lx)\n",
Dan Williams6f231dd2011-07-02 22:56:22 -0700305 __func__,
Dan Williamsf2088262011-06-16 11:26:12 -0700306 isci_device, isci_device ? isci_device->flags : 0);
Dan Williams6f231dd2011-07-02 22:56:22 -0700307 return TMF_RESP_FUNC_FAILED;
308 } else
Dan Williams0d0cf142011-06-13 00:51:30 -0700309 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -0700310 "%s: isci_device = %p\n",
311 __func__, isci_device);
312
Dan Williams57f20f42011-04-21 18:14:45 -0700313 sci_device = &isci_device->sci;
Dan Williams6f231dd2011-07-02 22:56:22 -0700314
315 /* Assign the pointer to the TMF's completion kernel wait structure. */
316 tmf->complete = &completion;
317
Dan Williams209fae12011-06-13 17:39:44 -0700318 ireq = isci_task_request_build(ihost, isci_device, tmf);
Dan Williams0d0cf142011-06-13 00:51:30 -0700319 if (!ireq) {
320 dev_warn(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -0700321 "%s: isci_task_request_build failed\n",
322 __func__);
323 return TMF_RESP_FUNC_FAILED;
324 }
325
Dan Williams0d0cf142011-06-13 00:51:30 -0700326 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700327
328 /* start the TMF io. */
329 status = scic_controller_start_task(
Dan Williams0d0cf142011-06-13 00:51:30 -0700330 &ihost->sci,
Dan Williams6f231dd2011-07-02 22:56:22 -0700331 sci_device,
Dan Williams0d0cf142011-06-13 00:51:30 -0700332 &ireq->sci,
Dan Williams67ea8382011-05-08 11:47:15 -0700333 SCI_CONTROLLER_INVALID_IO_TAG);
Dan Williams6f231dd2011-07-02 22:56:22 -0700334
Bartosz Barcinski467e8552011-04-12 17:28:41 -0700335 if (status != SCI_TASK_SUCCESS) {
Dan Williams0d0cf142011-06-13 00:51:30 -0700336 dev_warn(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -0700337 "%s: start_io failed - status = 0x%x, request = %p\n",
338 __func__,
339 status,
Dan Williams0d0cf142011-06-13 00:51:30 -0700340 ireq);
341 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams086a0da2011-06-21 16:23:03 -0700342 isci_request_free(ihost, ireq);
343 return ret;
Dan Williams6f231dd2011-07-02 22:56:22 -0700344 }
345
Dan Williams6f231dd2011-07-02 22:56:22 -0700346 if (tmf->cb_state_func != NULL)
347 tmf->cb_state_func(isci_tmf_started, tmf, tmf->cb_data);
348
Dan Williams0d0cf142011-06-13 00:51:30 -0700349 isci_request_change_state(ireq, started);
Dan Williams6f231dd2011-07-02 22:56:22 -0700350
351 /* add the request to the remote device request list. */
Dan Williams0d0cf142011-06-13 00:51:30 -0700352 list_add(&ireq->dev_node, &isci_device->reqs_in_process);
Dan Williams6f231dd2011-07-02 22:56:22 -0700353
Dan Williams0d0cf142011-06-13 00:51:30 -0700354 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700355
356 /* Wait for the TMF to complete, or a timeout. */
Edmund Nadolskifd183882011-05-19 12:00:15 +0000357 timeleft = wait_for_completion_timeout(&completion,
Dan Williams086a0da2011-06-21 16:23:03 -0700358 msecs_to_jiffies(timeout_ms));
Edmund Nadolskifd183882011-05-19 12:00:15 +0000359
360 if (timeleft == 0) {
Dan Williams0d0cf142011-06-13 00:51:30 -0700361 spin_lock_irqsave(&ihost->scic_lock, flags);
Edmund Nadolskifd183882011-05-19 12:00:15 +0000362
363 if (tmf->cb_state_func != NULL)
364 tmf->cb_state_func(isci_tmf_timed_out, tmf, tmf->cb_data);
365
Dan Williams086a0da2011-06-21 16:23:03 -0700366 scic_controller_terminate_request(&ihost->sci,
367 &isci_device->sci,
368 &ireq->sci);
Edmund Nadolskifd183882011-05-19 12:00:15 +0000369
Dan Williams0d0cf142011-06-13 00:51:30 -0700370 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams086a0da2011-06-21 16:23:03 -0700371
372 wait_for_completion(tmf->complete);
Edmund Nadolskifd183882011-05-19 12:00:15 +0000373 }
Dan Williams6f231dd2011-07-02 22:56:22 -0700374
375 isci_print_tmf(tmf);
376
377 if (tmf->status == SCI_SUCCESS)
378 ret = TMF_RESP_FUNC_COMPLETE;
379 else if (tmf->status == SCI_FAILURE_IO_RESPONSE_VALID) {
Dan Williams0d0cf142011-06-13 00:51:30 -0700380 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -0700381 "%s: tmf.status == "
382 "SCI_FAILURE_IO_RESPONSE_VALID\n",
383 __func__);
384 ret = TMF_RESP_FUNC_COMPLETE;
385 }
386 /* Else - leave the default "failed" status alone. */
387
Dan Williams0d0cf142011-06-13 00:51:30 -0700388 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -0700389 "%s: completed request = %p\n",
390 __func__,
Dan Williams0d0cf142011-06-13 00:51:30 -0700391 ireq);
Dan Williams6f231dd2011-07-02 22:56:22 -0700392
Dan Williams6f231dd2011-07-02 22:56:22 -0700393 return ret;
394}
395
396void isci_task_build_tmf(
397 struct isci_tmf *tmf,
Dan Williams6f231dd2011-07-02 22:56:22 -0700398 enum isci_tmf_function_codes code,
399 void (*tmf_sent_cb)(enum isci_tmf_cb_state,
400 struct isci_tmf *,
401 void *),
Jeff Skirvinc3f42fe2011-03-04 14:06:56 -0800402 void *cb_data)
Dan Williams6f231dd2011-07-02 22:56:22 -0700403{
Dan Williams6f231dd2011-07-02 22:56:22 -0700404 memset(tmf, 0, sizeof(*tmf));
405
Dan Williams6f231dd2011-07-02 22:56:22 -0700406 tmf->tmf_code = code;
Dan Williams6f231dd2011-07-02 22:56:22 -0700407 tmf->cb_state_func = tmf_sent_cb;
Jeff Skirvinc3f42fe2011-03-04 14:06:56 -0800408 tmf->cb_data = cb_data;
409}
Jeff Skirvin1fad9e92011-03-04 14:06:46 -0800410
Dan Williams35173d52011-03-26 16:43:01 -0700411static void isci_task_build_abort_task_tmf(
Jeff Skirvinc3f42fe2011-03-04 14:06:56 -0800412 struct isci_tmf *tmf,
Jeff Skirvinc3f42fe2011-03-04 14:06:56 -0800413 enum isci_tmf_function_codes code,
414 void (*tmf_sent_cb)(enum isci_tmf_cb_state,
415 struct isci_tmf *,
416 void *),
417 struct isci_request *old_request)
418{
Dan Williams209fae12011-06-13 17:39:44 -0700419 isci_task_build_tmf(tmf, code, tmf_sent_cb,
Jeff Skirvinc3f42fe2011-03-04 14:06:56 -0800420 (void *)old_request);
421 tmf->io_tag = old_request->io_tag;
Dan Williams6f231dd2011-07-02 22:56:22 -0700422}
423
Dan Williams6f231dd2011-07-02 22:56:22 -0700424/**
425 * isci_task_validate_request_to_abort() - This function checks the given I/O
426 * against the "started" state. If the request is still "started", it's
427 * state is changed to aborted. NOTE: isci_host->scic_lock MUST BE HELD
428 * BEFORE CALLING THIS FUNCTION.
429 * @isci_request: This parameter specifies the request object to control.
430 * @isci_host: This parameter specifies the ISCI host object
431 * @isci_device: This is the device to which the request is pending.
432 * @aborted_io_completion: This is a completion structure that will be added to
433 * the request in case it is changed to aborting; this completion is
434 * triggered when the request is fully completed.
435 *
436 * Either "started" on successful change of the task status to "aborted", or
437 * "unallocated" if the task cannot be controlled.
438 */
439static enum isci_request_status isci_task_validate_request_to_abort(
440 struct isci_request *isci_request,
441 struct isci_host *isci_host,
442 struct isci_remote_device *isci_device,
443 struct completion *aborted_io_completion)
444{
445 enum isci_request_status old_state = unallocated;
446
447 /* Only abort the task if it's in the
448 * device's request_in_process list
449 */
450 if (isci_request && !list_empty(&isci_request->dev_node)) {
451 old_state = isci_request_change_started_to_aborted(
452 isci_request, aborted_io_completion);
453
Dan Williams6f231dd2011-07-02 22:56:22 -0700454 }
455
456 return old_state;
457}
458
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700459/**
460* isci_request_cleanup_completed_loiterer() - This function will take care of
461* the final cleanup on any request which has been explicitly terminated.
462* @isci_host: This parameter specifies the ISCI host object
463* @isci_device: This is the device to which the request is pending.
464* @isci_request: This parameter specifies the terminated request object.
465* @task: This parameter is the libsas I/O request.
466*/
Dan Williams6f231dd2011-07-02 22:56:22 -0700467static void isci_request_cleanup_completed_loiterer(
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700468 struct isci_host *isci_host,
Dan Williams6f231dd2011-07-02 22:56:22 -0700469 struct isci_remote_device *isci_device,
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700470 struct isci_request *isci_request,
471 struct sas_task *task)
Dan Williams6f231dd2011-07-02 22:56:22 -0700472{
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700473 unsigned long flags;
Dan Williams6f231dd2011-07-02 22:56:22 -0700474
475 dev_dbg(&isci_host->pdev->dev,
476 "%s: isci_device=%p, request=%p, task=%p\n",
Jeff Skirvin18d3d722011-03-04 14:06:38 -0800477 __func__, isci_device, isci_request, task);
Dan Williams6f231dd2011-07-02 22:56:22 -0700478
Jeff Skirvina5fde222011-03-04 14:06:42 -0800479 if (task != NULL) {
480
481 spin_lock_irqsave(&task->task_state_lock, flags);
482 task->lldd_task = NULL;
483
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700484 task->task_state_flags &= ~SAS_TASK_NEED_DEV_RESET;
485
Jeff Skirvina5fde222011-03-04 14:06:42 -0800486 isci_set_task_doneflags(task);
487
488 /* If this task is not in the abort path, call task_done. */
489 if (!(task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
490
491 spin_unlock_irqrestore(&task->task_state_lock, flags);
492 task->task_done(task);
493 } else
494 spin_unlock_irqrestore(&task->task_state_lock, flags);
495 }
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800496
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700497 if (isci_request != NULL) {
498 spin_lock_irqsave(&isci_host->scic_lock, flags);
499 list_del_init(&isci_request->dev_node);
500 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800501
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700502 isci_request_free(isci_host, isci_request);
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800503 }
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800504}
505
Dan Williams6f231dd2011-07-02 22:56:22 -0700506/**
507 * isci_terminate_request_core() - This function will terminate the given
508 * request, and wait for it to complete. This function must only be called
509 * from a thread that can wait. Note that the request is terminated and
510 * completed (back to the host, if started there).
511 * @isci_host: This SCU.
512 * @isci_device: The target.
513 * @isci_request: The I/O request to be terminated.
514 *
Dan Williams6f231dd2011-07-02 22:56:22 -0700515 */
516static void isci_terminate_request_core(
517 struct isci_host *isci_host,
518 struct isci_remote_device *isci_device,
Jeff Skirvincbb65c62011-03-04 14:06:50 -0800519 struct isci_request *isci_request)
Dan Williams6f231dd2011-07-02 22:56:22 -0700520{
Jeff Skirvincbb65c62011-03-04 14:06:50 -0800521 enum sci_status status = SCI_SUCCESS;
Dan Williams6f231dd2011-07-02 22:56:22 -0700522 bool was_terminated = false;
523 bool needs_cleanup_handling = false;
524 enum isci_request_status request_status;
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700525 unsigned long flags;
526 unsigned long termination_completed = 1;
527 struct completion *io_request_completion;
528 struct sas_task *task;
Dan Williams6f231dd2011-07-02 22:56:22 -0700529
530 dev_dbg(&isci_host->pdev->dev,
531 "%s: device = %p; request = %p\n",
532 __func__, isci_device, isci_request);
533
Dan Williams6f231dd2011-07-02 22:56:22 -0700534 spin_lock_irqsave(&isci_host->scic_lock, flags);
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800535
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700536 io_request_completion = isci_request->io_request_completion;
537
538 task = (isci_request->ttype == io_task)
539 ? isci_request_access_task(isci_request)
540 : NULL;
541
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800542 /* Note that we are not going to control
543 * the target to abort the request.
544 */
545 isci_request->complete_in_target = true;
546
Dan Williams6f231dd2011-07-02 22:56:22 -0700547 /* Make sure the request wasn't just sitting around signalling
548 * device condition (if the request handle is NULL, then the
549 * request completed but needed additional handling here).
550 */
Dan Williams67ea8382011-05-08 11:47:15 -0700551 if (!isci_request->terminated) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700552 was_terminated = true;
Jeff Skirvincbb65c62011-03-04 14:06:50 -0800553 needs_cleanup_handling = true;
Dan Williams6f231dd2011-07-02 22:56:22 -0700554 status = scic_controller_terminate_request(
Artur Wojcikcc3dbd02011-05-04 07:58:16 +0000555 &isci_host->sci,
Dan Williams57f20f42011-04-21 18:14:45 -0700556 &isci_device->sci,
Dan Williams67ea8382011-05-08 11:47:15 -0700557 &isci_request->sci);
Dan Williams6f231dd2011-07-02 22:56:22 -0700558 }
559 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
560
561 /*
562 * The only time the request to terminate will
563 * fail is when the io request is completed and
564 * being aborted.
565 */
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800566 if (status != SCI_SUCCESS) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700567 dev_err(&isci_host->pdev->dev,
568 "%s: scic_controller_terminate_request"
569 " returned = 0x%x\n",
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700570 __func__, status);
571
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800572 isci_request->io_request_completion = NULL;
573
574 } else {
Dan Williams6f231dd2011-07-02 22:56:22 -0700575 if (was_terminated) {
576 dev_dbg(&isci_host->pdev->dev,
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700577 "%s: before completion wait (%p/%p)\n",
578 __func__, isci_request, io_request_completion);
Dan Williams6f231dd2011-07-02 22:56:22 -0700579
580 /* Wait here for the request to complete. */
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700581 #define TERMINATION_TIMEOUT_MSEC 500
582 termination_completed
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800583 = wait_for_completion_timeout(
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700584 io_request_completion,
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800585 msecs_to_jiffies(TERMINATION_TIMEOUT_MSEC));
Dan Williams6f231dd2011-07-02 22:56:22 -0700586
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700587 if (!termination_completed) {
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800588
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700589 /* The request to terminate has timed out. */
590 spin_lock_irqsave(&isci_host->scic_lock,
591 flags);
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800592
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700593 /* Check for state changes. */
594 if (!isci_request->terminated) {
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800595
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700596 /* The best we can do is to have the
597 * request die a silent death if it
598 * ever really completes.
599 *
600 * Set the request state to "dead",
601 * and clear the task pointer so that
602 * an actual completion event callback
603 * doesn't do anything.
604 */
605 isci_request->status = dead;
606 isci_request->io_request_completion
607 = NULL;
608
609 if (isci_request->ttype == io_task) {
610
611 /* Break links with the
612 * sas_task.
613 */
614 isci_request->ttype_ptr.io_task_ptr
615 = NULL;
616 }
617 } else
618 termination_completed = 1;
619
620 spin_unlock_irqrestore(&isci_host->scic_lock,
621 flags);
622
623 if (!termination_completed) {
624
625 dev_err(&isci_host->pdev->dev,
626 "%s: *** Timeout waiting for "
627 "termination(%p/%p)\n",
628 __func__, io_request_completion,
629 isci_request);
630
631 /* The request can no longer be referenced
632 * safely since it may go away if the
633 * termination every really does complete.
634 */
635 isci_request = NULL;
636 }
637 }
638 if (termination_completed)
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800639 dev_dbg(&isci_host->pdev->dev,
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700640 "%s: after completion wait (%p/%p)\n",
641 __func__, isci_request, io_request_completion);
Dan Williams6f231dd2011-07-02 22:56:22 -0700642 }
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800643
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700644 if (termination_completed) {
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800645
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700646 isci_request->io_request_completion = NULL;
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800647
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700648 /* Peek at the status of the request. This will tell
649 * us if there was special handling on the request such that it
650 * needs to be detached and freed here.
651 */
652 spin_lock_irqsave(&isci_request->state_lock, flags);
653 request_status = isci_request_get_state(isci_request);
654
655 if ((isci_request->ttype == io_task) /* TMFs are in their own thread */
656 && ((request_status == aborted)
657 || (request_status == aborting)
658 || (request_status == terminating)
659 || (request_status == completed)
660 || (request_status == dead)
661 )
662 ) {
663
664 /* The completion routine won't free a request in
665 * the aborted/aborting/etc. states, so we do
666 * it here.
667 */
668 needs_cleanup_handling = true;
669 }
670 spin_unlock_irqrestore(&isci_request->state_lock, flags);
671
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800672 }
Dan Williams6f231dd2011-07-02 22:56:22 -0700673 if (needs_cleanup_handling)
674 isci_request_cleanup_completed_loiterer(
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700675 isci_host, isci_device, isci_request, task);
Jeff Skirvina5fde222011-03-04 14:06:42 -0800676 }
Dan Williams6f231dd2011-07-02 22:56:22 -0700677}
678
679/**
680 * isci_terminate_pending_requests() - This function will change the all of the
681 * requests on the given device's state to "aborting", will terminate the
682 * requests, and wait for them to complete. This function must only be
683 * called from a thread that can wait. Note that the requests are all
684 * terminated and completed (back to the host, if started there).
685 * @isci_host: This parameter specifies SCU.
686 * @isci_device: This parameter specifies the target.
687 *
Dan Williams6f231dd2011-07-02 22:56:22 -0700688 */
Dan Williams980d3ae2011-06-20 15:11:22 -0700689void isci_terminate_pending_requests(struct isci_host *ihost,
690 struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -0700691{
Dan Williams980d3ae2011-06-20 15:11:22 -0700692 struct completion request_completion;
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700693 enum isci_request_status old_state;
Dan Williams980d3ae2011-06-20 15:11:22 -0700694 unsigned long flags;
695 LIST_HEAD(list);
Dan Williams6f231dd2011-07-02 22:56:22 -0700696
Dan Williams980d3ae2011-06-20 15:11:22 -0700697 spin_lock_irqsave(&ihost->scic_lock, flags);
698 list_splice_init(&idev->reqs_in_process, &list);
Dan Williams6f231dd2011-07-02 22:56:22 -0700699
Dan Williams980d3ae2011-06-20 15:11:22 -0700700 /* assumes that isci_terminate_request_core deletes from the list */
701 while (!list_empty(&list)) {
702 struct isci_request *ireq = list_entry(list.next, typeof(*ireq), dev_node);
Dan Williams6f231dd2011-07-02 22:56:22 -0700703
Dan Williams980d3ae2011-06-20 15:11:22 -0700704 /* Change state to "terminating" if it is currently
705 * "started".
706 */
707 old_state = isci_request_change_started_to_newstate(ireq,
708 &request_completion,
709 terminating);
710 switch (old_state) {
711 case started:
712 case completed:
713 case aborting:
714 break;
715 default:
716 /* termination in progress, or otherwise dispositioned.
717 * We know the request was on 'list' so should be safe
718 * to move it back to reqs_in_process
719 */
720 list_move(&ireq->dev_node, &idev->reqs_in_process);
721 ireq = NULL;
722 break;
723 }
724
725 if (!ireq)
726 continue;
727 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800728
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700729 init_completion(&request_completion);
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800730
Dan Williams980d3ae2011-06-20 15:11:22 -0700731 dev_dbg(&ihost->pdev->dev,
732 "%s: idev=%p request=%p; task=%p old_state=%d\n",
733 __func__, idev, ireq,
734 ireq->ttype == io_task ? isci_request_access_task(ireq) : NULL,
735 old_state);
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700736
Dan Williams980d3ae2011-06-20 15:11:22 -0700737 /* If the old_state is started:
738 * This request was not already being aborted. If it had been,
739 * then the aborting I/O (ie. the TMF request) would not be in
740 * the aborting state, and thus would be terminated here. Note
741 * that since the TMF completion's call to the kernel function
742 * "complete()" does not happen until the pending I/O request
743 * terminate fully completes, we do not have to implement a
744 * special wait here for already aborting requests - the
745 * termination of the TMF request will force the request
746 * to finish it's already started terminate.
747 *
748 * If old_state == completed:
749 * This request completed from the SCU hardware perspective
750 * and now just needs cleaning up in terms of freeing the
751 * request and potentially calling up to libsas.
752 *
753 * If old_state == aborting:
754 * This request has already gone through a TMF timeout, but may
755 * not have been terminated; needs cleaning up at least.
756 */
757 isci_terminate_request_core(ihost, idev, ireq);
758 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700759 }
Dan Williams980d3ae2011-06-20 15:11:22 -0700760 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700761}
762
763/**
764 * isci_task_send_lu_reset_sas() - This function is called by of the SAS Domain
765 * Template functions.
766 * @lun: This parameter specifies the lun to be reset.
767 *
768 * status, zero indicates success.
769 */
770static int isci_task_send_lu_reset_sas(
771 struct isci_host *isci_host,
772 struct isci_remote_device *isci_device,
773 u8 *lun)
774{
775 struct isci_tmf tmf;
776 int ret = TMF_RESP_FUNC_FAILED;
777
778 dev_dbg(&isci_host->pdev->dev,
779 "%s: isci_host = %p, isci_device = %p\n",
780 __func__, isci_host, isci_device);
781 /* Send the LUN reset to the target. By the time the call returns,
782 * the TMF has fully exected in the target (in which case the return
783 * value is "TMF_RESP_FUNC_COMPLETE", or the request timed-out (or
784 * was otherwise unable to be executed ("TMF_RESP_FUNC_FAILED").
785 */
Dan Williams209fae12011-06-13 17:39:44 -0700786 isci_task_build_tmf(&tmf, isci_tmf_ssp_lun_reset, NULL, NULL);
Dan Williams6f231dd2011-07-02 22:56:22 -0700787
788 #define ISCI_LU_RESET_TIMEOUT_MS 2000 /* 2 second timeout. */
Dan Williams209fae12011-06-13 17:39:44 -0700789 ret = isci_task_execute_tmf(isci_host, isci_device, &tmf, ISCI_LU_RESET_TIMEOUT_MS);
Dan Williams6f231dd2011-07-02 22:56:22 -0700790
791 if (ret == TMF_RESP_FUNC_COMPLETE)
792 dev_dbg(&isci_host->pdev->dev,
793 "%s: %p: TMF_LU_RESET passed\n",
794 __func__, isci_device);
795 else
796 dev_dbg(&isci_host->pdev->dev,
797 "%s: %p: TMF_LU_RESET failed (%x)\n",
798 __func__, isci_device, ret);
799
800 return ret;
801}
802
803/**
804 * isci_task_lu_reset() - This function is one of the SAS Domain Template
805 * functions. This is one of the Task Management functoins called by libsas,
806 * to reset the given lun. Note the assumption that while this call is
807 * executing, no I/O will be sent by the host to the device.
808 * @lun: This parameter specifies the lun to be reset.
809 *
810 * status, zero indicates success.
811 */
Dan Williams4393aa42011-03-31 13:10:44 -0700812int isci_task_lu_reset(struct domain_device *domain_device, u8 *lun)
Dan Williams6f231dd2011-07-02 22:56:22 -0700813{
Dan Williams4393aa42011-03-31 13:10:44 -0700814 struct isci_host *isci_host = dev_to_ihost(domain_device);
Dan Williams209fae12011-06-13 17:39:44 -0700815 struct isci_remote_device *isci_device;
816 unsigned long flags;
Dan Williams6f231dd2011-07-02 22:56:22 -0700817 int ret;
Dan Williams6f231dd2011-07-02 22:56:22 -0700818
Dan Williams209fae12011-06-13 17:39:44 -0700819 spin_lock_irqsave(&isci_host->scic_lock, flags);
820 isci_device = isci_lookup_device(domain_device);
821 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700822
Dan Williams4393aa42011-03-31 13:10:44 -0700823 dev_dbg(&isci_host->pdev->dev,
824 "%s: domain_device=%p, isci_host=%p; isci_device=%p\n",
Dan Williams6f231dd2011-07-02 22:56:22 -0700825 __func__, domain_device, isci_host, isci_device);
826
Dan Williams209fae12011-06-13 17:39:44 -0700827 if (isci_device)
Dan Williamsd06b4872011-05-02 13:59:25 -0700828 set_bit(IDEV_EH, &isci_device->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700829
830 /* If there is a device reset pending on any request in the
831 * device's list, fail this LUN reset request in order to
832 * escalate to the device reset.
833 */
Dan Williams209fae12011-06-13 17:39:44 -0700834 if (!isci_device ||
Dan Williams4393aa42011-03-31 13:10:44 -0700835 isci_device_is_reset_pending(isci_host, isci_device)) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700836 dev_warn(&isci_host->pdev->dev,
Dan Williams4393aa42011-03-31 13:10:44 -0700837 "%s: No dev (%p), or "
Dan Williams6f231dd2011-07-02 22:56:22 -0700838 "RESET PENDING: domain_device=%p\n",
Dan Williams4393aa42011-03-31 13:10:44 -0700839 __func__, isci_device, domain_device);
Dan Williams209fae12011-06-13 17:39:44 -0700840 ret = TMF_RESP_FUNC_FAILED;
841 goto out;
Dan Williams6f231dd2011-07-02 22:56:22 -0700842 }
843
Dan Williams6f231dd2011-07-02 22:56:22 -0700844 /* Send the task management part of the reset. */
845 if (sas_protocol_ata(domain_device->tproto)) {
Dan Williams4393aa42011-03-31 13:10:44 -0700846 ret = isci_task_send_lu_reset_sata(isci_host, isci_device, lun);
Dan Williams6f231dd2011-07-02 22:56:22 -0700847 } else
848 ret = isci_task_send_lu_reset_sas(isci_host, isci_device, lun);
849
850 /* If the LUN reset worked, all the I/O can now be terminated. */
851 if (ret == TMF_RESP_FUNC_COMPLETE)
852 /* Terminate all I/O now. */
853 isci_terminate_pending_requests(isci_host,
Dan Williams980d3ae2011-06-20 15:11:22 -0700854 isci_device);
Dan Williams6f231dd2011-07-02 22:56:22 -0700855
Dan Williams209fae12011-06-13 17:39:44 -0700856 out:
857 isci_put_device(isci_device);
Dan Williams6f231dd2011-07-02 22:56:22 -0700858 return ret;
859}
860
861
862/* int (*lldd_clear_nexus_port)(struct asd_sas_port *); */
863int isci_task_clear_nexus_port(struct asd_sas_port *port)
864{
865 return TMF_RESP_FUNC_FAILED;
866}
867
868
869
870int isci_task_clear_nexus_ha(struct sas_ha_struct *ha)
871{
872 return TMF_RESP_FUNC_FAILED;
873}
874
Dan Williams6f231dd2011-07-02 22:56:22 -0700875/* Task Management Functions. Must be called from process context. */
876
877/**
878 * isci_abort_task_process_cb() - This is a helper function for the abort task
879 * TMF command. It manages the request state with respect to the successful
880 * transmission / completion of the abort task request.
881 * @cb_state: This parameter specifies when this function was called - after
882 * the TMF request has been started and after it has timed-out.
883 * @tmf: This parameter specifies the TMF in progress.
884 *
885 *
886 */
887static void isci_abort_task_process_cb(
888 enum isci_tmf_cb_state cb_state,
889 struct isci_tmf *tmf,
890 void *cb_data)
891{
892 struct isci_request *old_request;
893
894 old_request = (struct isci_request *)cb_data;
895
896 dev_dbg(&old_request->isci_host->pdev->dev,
897 "%s: tmf=%p, old_request=%p\n",
898 __func__, tmf, old_request);
899
900 switch (cb_state) {
901
902 case isci_tmf_started:
903 /* The TMF has been started. Nothing to do here, since the
904 * request state was already set to "aborted" by the abort
905 * task function.
906 */
Bartosz Barcinski6cb4d6b2011-04-12 17:28:43 -0700907 if ((old_request->status != aborted)
908 && (old_request->status != completed))
909 dev_err(&old_request->isci_host->pdev->dev,
910 "%s: Bad request status (%d): tmf=%p, old_request=%p\n",
911 __func__, old_request->status, tmf, old_request);
Dan Williams6f231dd2011-07-02 22:56:22 -0700912 break;
913
914 case isci_tmf_timed_out:
915
916 /* Set the task's state to "aborting", since the abort task
917 * function thread set it to "aborted" (above) in anticipation
918 * of the task management request working correctly. Since the
919 * timeout has now fired, the TMF request failed. We set the
920 * state such that the request completion will indicate the
921 * device is no longer present.
922 */
923 isci_request_change_state(old_request, aborting);
924 break;
925
926 default:
927 dev_err(&old_request->isci_host->pdev->dev,
928 "%s: Bad cb_state (%d): tmf=%p, old_request=%p\n",
929 __func__, cb_state, tmf, old_request);
930 break;
931 }
932}
933
934/**
935 * isci_task_abort_task() - This function is one of the SAS Domain Template
936 * functions. This function is called by libsas to abort a specified task.
937 * @task: This parameter specifies the SAS task to abort.
938 *
939 * status, zero indicates success.
940 */
941int isci_task_abort_task(struct sas_task *task)
942{
Dan Williams4393aa42011-03-31 13:10:44 -0700943 struct isci_host *isci_host = dev_to_ihost(task->dev);
Dan Williams6f231dd2011-07-02 22:56:22 -0700944 DECLARE_COMPLETION_ONSTACK(aborted_io_completion);
Jeff Skirvina5fde222011-03-04 14:06:42 -0800945 struct isci_request *old_request = NULL;
946 enum isci_request_status old_state;
Dan Williams6f231dd2011-07-02 22:56:22 -0700947 struct isci_remote_device *isci_device = NULL;
Jeff Skirvina5fde222011-03-04 14:06:42 -0800948 struct isci_tmf tmf;
949 int ret = TMF_RESP_FUNC_FAILED;
950 unsigned long flags;
951 bool any_dev_reset = false;
Dan Williams6f231dd2011-07-02 22:56:22 -0700952
953 /* Get the isci_request reference from the task. Note that
954 * this check does not depend on the pending request list
955 * in the device, because tasks driving resets may land here
956 * after completion in the core.
957 */
Dan Williams209fae12011-06-13 17:39:44 -0700958 spin_lock_irqsave(&isci_host->scic_lock, flags);
959 spin_lock(&task->task_state_lock);
960
961 old_request = task->lldd_task;
962
963 /* If task is already done, the request isn't valid */
964 if (!(task->task_state_flags & SAS_TASK_STATE_DONE) &&
965 (task->task_state_flags & SAS_TASK_AT_INITIATOR) &&
966 old_request)
967 isci_device = isci_lookup_device(task->dev);
968
969 spin_unlock(&task->task_state_lock);
970 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700971
972 dev_dbg(&isci_host->pdev->dev,
973 "%s: task = %p\n", __func__, task);
974
Dan Williams209fae12011-06-13 17:39:44 -0700975 if (!isci_device || !old_request)
976 goto out;
Dan Williams6f231dd2011-07-02 22:56:22 -0700977
Dan Williamsd06b4872011-05-02 13:59:25 -0700978 set_bit(IDEV_EH, &isci_device->flags);
979
Dan Williams6f231dd2011-07-02 22:56:22 -0700980 /* This version of the driver will fail abort requests for
981 * SATA/STP. Failing the abort request this way will cause the
982 * SCSI error handler thread to escalate to LUN reset
983 */
Dan Williams209fae12011-06-13 17:39:44 -0700984 if (sas_protocol_ata(task->task_proto)) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700985 dev_warn(&isci_host->pdev->dev,
986 " task %p is for a STP/SATA device;"
987 " returning TMF_RESP_FUNC_FAILED\n"
988 " to cause a LUN reset...\n", task);
Dan Williams209fae12011-06-13 17:39:44 -0700989 goto out;
Dan Williams6f231dd2011-07-02 22:56:22 -0700990 }
991
992 dev_dbg(&isci_host->pdev->dev,
993 "%s: old_request == %p\n", __func__, old_request);
994
Dan Williams209fae12011-06-13 17:39:44 -0700995 any_dev_reset = isci_device_is_reset_pending(isci_host,isci_device);
Jeff Skirvina5fde222011-03-04 14:06:42 -0800996
Dan Williams6f231dd2011-07-02 22:56:22 -0700997 spin_lock_irqsave(&task->task_state_lock, flags);
998
Dan Williams209fae12011-06-13 17:39:44 -0700999 any_dev_reset = any_dev_reset || (task->task_state_flags & SAS_TASK_NEED_DEV_RESET);
Dan Williams6f231dd2011-07-02 22:56:22 -07001000
1001 /* If the extraction of the request reference from the task
1002 * failed, then the request has been completed (or if there is a
1003 * pending reset then this abort request function must be failed
1004 * in order to escalate to the target reset).
1005 */
Jeff Skirvina5fde222011-03-04 14:06:42 -08001006 if ((old_request == NULL) || any_dev_reset) {
Dan Williams6f231dd2011-07-02 22:56:22 -07001007
1008 /* If the device reset task flag is set, fail the task
1009 * management request. Otherwise, the original request
1010 * has completed.
1011 */
1012 if (any_dev_reset) {
1013
1014 /* Turn off the task's DONE to make sure this
1015 * task is escalated to a target reset.
1016 */
1017 task->task_state_flags &= ~SAS_TASK_STATE_DONE;
1018
Jeff Skirvina5fde222011-03-04 14:06:42 -08001019 /* Make the reset happen as soon as possible. */
1020 task->task_state_flags |= SAS_TASK_NEED_DEV_RESET;
1021
1022 spin_unlock_irqrestore(&task->task_state_lock, flags);
1023
Dan Williams6f231dd2011-07-02 22:56:22 -07001024 /* Fail the task management request in order to
1025 * escalate to the target reset.
1026 */
1027 ret = TMF_RESP_FUNC_FAILED;
1028
1029 dev_dbg(&isci_host->pdev->dev,
1030 "%s: Failing task abort in order to "
1031 "escalate to target reset because\n"
1032 "SAS_TASK_NEED_DEV_RESET is set for "
1033 "task %p on dev %p\n",
1034 __func__, task, isci_device);
1035
Jeff Skirvina5fde222011-03-04 14:06:42 -08001036
Dan Williams6f231dd2011-07-02 22:56:22 -07001037 } else {
Dan Williams6f231dd2011-07-02 22:56:22 -07001038 /* The request has already completed and there
1039 * is nothing to do here other than to set the task
1040 * done bit, and indicate that the task abort function
1041 * was sucessful.
1042 */
1043 isci_set_task_doneflags(task);
1044
Jeff Skirvina5fde222011-03-04 14:06:42 -08001045 spin_unlock_irqrestore(&task->task_state_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001046
Jeff Skirvina5fde222011-03-04 14:06:42 -08001047 ret = TMF_RESP_FUNC_COMPLETE;
Dan Williams6f231dd2011-07-02 22:56:22 -07001048
Jeff Skirvina5fde222011-03-04 14:06:42 -08001049 dev_dbg(&isci_host->pdev->dev,
1050 "%s: abort task not needed for %p\n",
1051 __func__, task);
Dan Williams6f231dd2011-07-02 22:56:22 -07001052 }
Dan Williams209fae12011-06-13 17:39:44 -07001053 goto out;
Dan Williams6f231dd2011-07-02 22:56:22 -07001054 }
Jeff Skirvina5fde222011-03-04 14:06:42 -08001055 else
1056 spin_unlock_irqrestore(&task->task_state_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001057
1058 spin_lock_irqsave(&isci_host->scic_lock, flags);
1059
Jeff Skirvinf219f012011-03-31 13:10:34 -07001060 /* Check the request status and change to "aborted" if currently
Jeff Skirvina5fde222011-03-04 14:06:42 -08001061 * "starting"; if true then set the I/O kernel completion
Dan Williams6f231dd2011-07-02 22:56:22 -07001062 * struct that will be triggered when the request completes.
1063 */
Jeff Skirvina5fde222011-03-04 14:06:42 -08001064 old_state = isci_task_validate_request_to_abort(
1065 old_request, isci_host, isci_device,
1066 &aborted_io_completion);
Jeff Skirvinf219f012011-03-31 13:10:34 -07001067 if ((old_state != started) &&
1068 (old_state != completed) &&
1069 (old_state != aborting)) {
Dan Williams6f231dd2011-07-02 22:56:22 -07001070
1071 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1072
Jeff Skirvina5fde222011-03-04 14:06:42 -08001073 /* The request was already being handled by someone else (because
1074 * they got to set the state away from started).
1075 */
1076 dev_dbg(&isci_host->pdev->dev,
1077 "%s: device = %p; old_request %p already being aborted\n",
1078 __func__,
1079 isci_device, old_request);
Dan Williams209fae12011-06-13 17:39:44 -07001080 ret = TMF_RESP_FUNC_COMPLETE;
1081 goto out;
Dan Williams6f231dd2011-07-02 22:56:22 -07001082 }
Jeff Skirvina5fde222011-03-04 14:06:42 -08001083 if ((task->task_proto == SAS_PROTOCOL_SMP)
Jeff Skirvina5fde222011-03-04 14:06:42 -08001084 || old_request->complete_in_target
1085 ) {
Dan Williams6f231dd2011-07-02 22:56:22 -07001086
1087 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1088
Jeff Skirvina5fde222011-03-04 14:06:42 -08001089 dev_dbg(&isci_host->pdev->dev,
1090 "%s: SMP request (%d)"
Jeff Skirvina5fde222011-03-04 14:06:42 -08001091 " or complete_in_target (%d), thus no TMF\n",
1092 __func__, (task->task_proto == SAS_PROTOCOL_SMP),
Dan Williams209fae12011-06-13 17:39:44 -07001093 old_request->complete_in_target);
Jeff Skirvina5fde222011-03-04 14:06:42 -08001094
Dan Williams6f231dd2011-07-02 22:56:22 -07001095 /* Set the state on the task. */
1096 isci_task_all_done(task);
1097
1098 ret = TMF_RESP_FUNC_COMPLETE;
1099
1100 /* Stopping and SMP devices are not sent a TMF, and are not
Jeff Skirvina5fde222011-03-04 14:06:42 -08001101 * reset, but the outstanding I/O request is terminated below.
Dan Williams6f231dd2011-07-02 22:56:22 -07001102 */
Dan Williams6f231dd2011-07-02 22:56:22 -07001103 } else {
1104 /* Fill in the tmf stucture */
Dan Williams209fae12011-06-13 17:39:44 -07001105 isci_task_build_abort_task_tmf(&tmf, isci_tmf_ssp_task_abort,
Jeff Skirvinc3f42fe2011-03-04 14:06:56 -08001106 isci_abort_task_process_cb,
1107 old_request);
Dan Williams6f231dd2011-07-02 22:56:22 -07001108
Dan Williams6f231dd2011-07-02 22:56:22 -07001109 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1110
1111 #define ISCI_ABORT_TASK_TIMEOUT_MS 500 /* half second timeout. */
Dan Williams209fae12011-06-13 17:39:44 -07001112 ret = isci_task_execute_tmf(isci_host, isci_device, &tmf,
Dan Williams6f231dd2011-07-02 22:56:22 -07001113 ISCI_ABORT_TASK_TIMEOUT_MS);
1114
Jeff Skirvina5fde222011-03-04 14:06:42 -08001115 if (ret != TMF_RESP_FUNC_COMPLETE)
Dan Williams6f231dd2011-07-02 22:56:22 -07001116 dev_err(&isci_host->pdev->dev,
1117 "%s: isci_task_send_tmf failed\n",
1118 __func__);
1119 }
Jeff Skirvina5fde222011-03-04 14:06:42 -08001120 if (ret == TMF_RESP_FUNC_COMPLETE) {
1121 old_request->complete_in_target = true;
Dan Williams6f231dd2011-07-02 22:56:22 -07001122
Jeff Skirvin77c852f2011-06-20 14:09:16 -07001123 /* Clean up the request on our side, and wait for the aborted
1124 * I/O to complete.
1125 */
Jeff Skirvincbb65c62011-03-04 14:06:50 -08001126 isci_terminate_request_core(isci_host, isci_device, old_request);
Jeff Skirvina5fde222011-03-04 14:06:42 -08001127 }
1128
1129 /* Make sure we do not leave a reference to aborted_io_completion */
1130 old_request->io_request_completion = NULL;
Dan Williams209fae12011-06-13 17:39:44 -07001131 out:
1132 isci_put_device(isci_device);
Dan Williams6f231dd2011-07-02 22:56:22 -07001133 return ret;
1134}
1135
1136/**
1137 * isci_task_abort_task_set() - This function is one of the SAS Domain Template
1138 * functions. This is one of the Task Management functoins called by libsas,
1139 * to abort all task for the given lun.
1140 * @d_device: This parameter specifies the domain device associated with this
1141 * request.
1142 * @lun: This parameter specifies the lun associated with this request.
1143 *
1144 * status, zero indicates success.
1145 */
1146int isci_task_abort_task_set(
1147 struct domain_device *d_device,
1148 u8 *lun)
1149{
1150 return TMF_RESP_FUNC_FAILED;
1151}
1152
1153
1154/**
1155 * isci_task_clear_aca() - This function is one of the SAS Domain Template
1156 * functions. This is one of the Task Management functoins called by libsas.
1157 * @d_device: This parameter specifies the domain device associated with this
1158 * request.
1159 * @lun: This parameter specifies the lun associated with this request.
1160 *
1161 * status, zero indicates success.
1162 */
1163int isci_task_clear_aca(
1164 struct domain_device *d_device,
1165 u8 *lun)
1166{
1167 return TMF_RESP_FUNC_FAILED;
1168}
1169
1170
1171
1172/**
1173 * isci_task_clear_task_set() - This function is one of the SAS Domain Template
1174 * functions. This is one of the Task Management functoins called by libsas.
1175 * @d_device: This parameter specifies the domain device associated with this
1176 * request.
1177 * @lun: This parameter specifies the lun associated with this request.
1178 *
1179 * status, zero indicates success.
1180 */
1181int isci_task_clear_task_set(
1182 struct domain_device *d_device,
1183 u8 *lun)
1184{
1185 return TMF_RESP_FUNC_FAILED;
1186}
1187
1188
1189/**
1190 * isci_task_query_task() - This function is implemented to cause libsas to
1191 * correctly escalate the failed abort to a LUN or target reset (this is
1192 * because sas_scsi_find_task libsas function does not correctly interpret
1193 * all return codes from the abort task call). When TMF_RESP_FUNC_SUCC is
1194 * returned, libsas turns this into a LUN reset; when FUNC_FAILED is
1195 * returned, libsas will turn this into a target reset
1196 * @task: This parameter specifies the sas task being queried.
1197 * @lun: This parameter specifies the lun associated with this request.
1198 *
1199 * status, zero indicates success.
1200 */
1201int isci_task_query_task(
1202 struct sas_task *task)
1203{
1204 /* See if there is a pending device reset for this device. */
1205 if (task->task_state_flags & SAS_TASK_NEED_DEV_RESET)
1206 return TMF_RESP_FUNC_FAILED;
1207 else
1208 return TMF_RESP_FUNC_SUCC;
1209}
1210
Dave Jiangaf5ae892011-05-04 17:53:24 -07001211/*
Dan Williams6f231dd2011-07-02 22:56:22 -07001212 * isci_task_request_complete() - This function is called by the sci core when
1213 * an task request completes.
Dave Jiangaf5ae892011-05-04 17:53:24 -07001214 * @ihost: This parameter specifies the ISCI host object
1215 * @ireq: This parameter is the completed isci_request object.
Dan Williams6f231dd2011-07-02 22:56:22 -07001216 * @completion_status: This parameter specifies the completion status from the
1217 * sci core.
1218 *
1219 * none.
1220 */
Dave Jiangaf5ae892011-05-04 17:53:24 -07001221void
1222isci_task_request_complete(struct isci_host *ihost,
1223 struct isci_request *ireq,
1224 enum sci_task_status completion_status)
Dan Williams6f231dd2011-07-02 22:56:22 -07001225{
Dave Jiangaf5ae892011-05-04 17:53:24 -07001226 struct isci_tmf *tmf = isci_request_access_tmf(ireq);
Dan Williams6f231dd2011-07-02 22:56:22 -07001227 struct completion *tmf_complete;
Dan Williams67ea8382011-05-08 11:47:15 -07001228 struct scic_sds_request *sci_req = &ireq->sci;
Dan Williams6f231dd2011-07-02 22:56:22 -07001229
Dave Jiangaf5ae892011-05-04 17:53:24 -07001230 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -07001231 "%s: request = %p, status=%d\n",
Dave Jiangaf5ae892011-05-04 17:53:24 -07001232 __func__, ireq, completion_status);
Dan Williams6f231dd2011-07-02 22:56:22 -07001233
Dave Jiang8d2c65c2011-06-01 09:03:08 +00001234 isci_request_change_state(ireq, completed);
Dan Williams6f231dd2011-07-02 22:56:22 -07001235
1236 tmf->status = completion_status;
Dave Jiangaf5ae892011-05-04 17:53:24 -07001237 ireq->complete_in_target = true;
Dan Williams6f231dd2011-07-02 22:56:22 -07001238
Dave Jiangaf5ae892011-05-04 17:53:24 -07001239 if (tmf->proto == SAS_PROTOCOL_SSP) {
Dan Williams6f231dd2011-07-02 22:56:22 -07001240 memcpy(&tmf->resp.resp_iu,
Dan Williamsb7645812011-05-08 02:35:32 -07001241 &sci_req->ssp.rsp,
Dave Jiangaf5ae892011-05-04 17:53:24 -07001242 SSP_RESP_IU_MAX_SIZE);
1243 } else if (tmf->proto == SAS_PROTOCOL_SATA) {
Dan Williams6f231dd2011-07-02 22:56:22 -07001244 memcpy(&tmf->resp.d2h_fis,
Dan Williamsb7645812011-05-08 02:35:32 -07001245 &sci_req->stp.rsp,
Dave Jiangf2f30082011-05-04 15:02:02 -07001246 sizeof(struct dev_to_host_fis));
Dan Williams6f231dd2011-07-02 22:56:22 -07001247 }
1248
Dan Williams6f231dd2011-07-02 22:56:22 -07001249 /* PRINT_TMF( ((struct isci_tmf *)request->task)); */
1250 tmf_complete = tmf->complete;
1251
Dan Williams209fae12011-06-13 17:39:44 -07001252 scic_controller_complete_io(&ihost->sci, ireq->sci.target_device, &ireq->sci);
Dan Williams67ea8382011-05-08 11:47:15 -07001253 /* set the 'terminated' flag handle to make sure it cannot be terminated
Dan Williams6f231dd2011-07-02 22:56:22 -07001254 * or completed again.
1255 */
Dan Williams67ea8382011-05-08 11:47:15 -07001256 ireq->terminated = true;;
Dan Williams6f231dd2011-07-02 22:56:22 -07001257
Dave Jiangaf5ae892011-05-04 17:53:24 -07001258 isci_request_change_state(ireq, unallocated);
1259 list_del_init(&ireq->dev_node);
Dan Williams6f231dd2011-07-02 22:56:22 -07001260
1261 /* The task management part completes last. */
1262 complete(tmf_complete);
1263}
1264
Jeff Skirvin61aaff42011-06-21 12:16:33 -07001265static void isci_smp_task_timedout(unsigned long _task)
1266{
1267 struct sas_task *task = (void *) _task;
1268 unsigned long flags;
1269
1270 spin_lock_irqsave(&task->task_state_lock, flags);
1271 if (!(task->task_state_flags & SAS_TASK_STATE_DONE))
1272 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
1273 spin_unlock_irqrestore(&task->task_state_lock, flags);
1274
1275 complete(&task->completion);
1276}
1277
1278static void isci_smp_task_done(struct sas_task *task)
1279{
1280 if (!del_timer(&task->timer))
1281 return;
1282 complete(&task->completion);
1283}
1284
1285static struct sas_task *isci_alloc_task(void)
1286{
1287 struct sas_task *task = kzalloc(sizeof(*task), GFP_KERNEL);
1288
1289 if (task) {
1290 INIT_LIST_HEAD(&task->list);
1291 spin_lock_init(&task->task_state_lock);
1292 task->task_state_flags = SAS_TASK_STATE_PENDING;
1293 init_timer(&task->timer);
1294 init_completion(&task->completion);
1295 }
1296
1297 return task;
1298}
1299
1300static void isci_free_task(struct isci_host *ihost, struct sas_task *task)
1301{
1302 if (task) {
1303 BUG_ON(!list_empty(&task->list));
1304 kfree(task);
1305 }
1306}
1307
1308static int isci_smp_execute_task(struct isci_host *ihost,
1309 struct domain_device *dev, void *req,
1310 int req_size, void *resp, int resp_size)
1311{
1312 int res, retry;
1313 struct sas_task *task = NULL;
1314
1315 for (retry = 0; retry < 3; retry++) {
1316 task = isci_alloc_task();
1317 if (!task)
1318 return -ENOMEM;
1319
1320 task->dev = dev;
1321 task->task_proto = dev->tproto;
1322 sg_init_one(&task->smp_task.smp_req, req, req_size);
1323 sg_init_one(&task->smp_task.smp_resp, resp, resp_size);
1324
1325 task->task_done = isci_smp_task_done;
1326
1327 task->timer.data = (unsigned long) task;
1328 task->timer.function = isci_smp_task_timedout;
1329 task->timer.expires = jiffies + 10*HZ;
1330 add_timer(&task->timer);
1331
1332 res = isci_task_execute_task(task, 1, GFP_KERNEL);
1333
1334 if (res) {
1335 del_timer(&task->timer);
1336 dev_err(&ihost->pdev->dev,
1337 "%s: executing SMP task failed:%d\n",
1338 __func__, res);
1339 goto ex_err;
1340 }
1341
1342 wait_for_completion(&task->completion);
1343 res = -ECOMM;
1344 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
1345 dev_err(&ihost->pdev->dev,
1346 "%s: smp task timed out or aborted\n",
1347 __func__);
1348 isci_task_abort_task(task);
1349 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
1350 dev_err(&ihost->pdev->dev,
1351 "%s: SMP task aborted and not done\n",
1352 __func__);
1353 goto ex_err;
1354 }
1355 }
1356 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1357 task->task_status.stat == SAM_STAT_GOOD) {
1358 res = 0;
1359 break;
1360 }
1361 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1362 task->task_status.stat == SAS_DATA_UNDERRUN) {
1363 /* no error, but return the number of bytes of
1364 * underrun */
1365 res = task->task_status.residual;
1366 break;
1367 }
1368 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1369 task->task_status.stat == SAS_DATA_OVERRUN) {
1370 res = -EMSGSIZE;
1371 break;
1372 } else {
1373 dev_err(&ihost->pdev->dev,
1374 "%s: task to dev %016llx response: 0x%x "
1375 "status 0x%x\n", __func__,
1376 SAS_ADDR(dev->sas_addr),
1377 task->task_status.resp,
1378 task->task_status.stat);
1379 isci_free_task(ihost, task);
1380 task = NULL;
1381 }
1382 }
1383ex_err:
1384 BUG_ON(retry == 3 && task != NULL);
1385 isci_free_task(ihost, task);
1386 return res;
1387}
1388
1389#define DISCOVER_REQ_SIZE 16
1390#define DISCOVER_RESP_SIZE 56
1391
1392int isci_smp_get_phy_attached_dev_type(struct isci_host *ihost,
1393 struct domain_device *dev,
1394 int phy_id, int *adt)
1395{
1396 struct smp_resp *disc_resp;
1397 u8 *disc_req;
1398 int res;
1399
1400 disc_resp = kzalloc(DISCOVER_RESP_SIZE, GFP_KERNEL);
1401 if (!disc_resp)
1402 return -ENOMEM;
1403
1404 disc_req = kzalloc(DISCOVER_REQ_SIZE, GFP_KERNEL);
1405 if (disc_req) {
1406 disc_req[0] = SMP_REQUEST;
1407 disc_req[1] = SMP_DISCOVER;
1408 disc_req[9] = phy_id;
1409 } else {
1410 kfree(disc_resp);
1411 return -ENOMEM;
1412 }
1413 res = isci_smp_execute_task(ihost, dev, disc_req, DISCOVER_REQ_SIZE,
1414 disc_resp, DISCOVER_RESP_SIZE);
1415 if (!res) {
1416 if (disc_resp->result != SMP_RESP_FUNC_ACC)
1417 res = disc_resp->result;
1418 else
1419 *adt = disc_resp->disc.attached_dev_type;
1420 }
1421 kfree(disc_req);
1422 kfree(disc_resp);
1423
1424 return res;
1425}
1426
1427static void isci_wait_for_smp_phy_reset(struct isci_remote_device *idev, int phy_num)
1428{
1429 struct domain_device *dev = idev->domain_dev;
1430 struct isci_port *iport = idev->isci_port;
1431 struct isci_host *ihost = iport->isci_host;
1432 int res, iteration = 0, attached_device_type;
1433 #define STP_WAIT_MSECS 25000
1434 unsigned long tmo = msecs_to_jiffies(STP_WAIT_MSECS);
1435 unsigned long deadline = jiffies + tmo;
1436 enum {
1437 SMP_PHYWAIT_PHYDOWN,
1438 SMP_PHYWAIT_PHYUP,
1439 SMP_PHYWAIT_DONE
1440 } phy_state = SMP_PHYWAIT_PHYDOWN;
1441
1442 /* While there is time, wait for the phy to go away and come back */
1443 while (time_is_after_jiffies(deadline) && phy_state != SMP_PHYWAIT_DONE) {
1444 int event = atomic_read(&iport->event);
1445
1446 ++iteration;
1447
1448 tmo = wait_event_timeout(ihost->eventq,
1449 event != atomic_read(&iport->event) ||
1450 !test_bit(IPORT_BCN_BLOCKED, &iport->flags),
1451 tmo);
1452 /* link down, stop polling */
1453 if (!test_bit(IPORT_BCN_BLOCKED, &iport->flags))
1454 break;
1455
1456 dev_dbg(&ihost->pdev->dev,
1457 "%s: iport %p, iteration %d,"
1458 " phase %d: time_remaining %lu, bcns = %d\n",
1459 __func__, iport, iteration, phy_state,
1460 tmo, test_bit(IPORT_BCN_PENDING, &iport->flags));
1461
1462 res = isci_smp_get_phy_attached_dev_type(ihost, dev, phy_num,
1463 &attached_device_type);
1464 tmo = deadline - jiffies;
1465
1466 if (res) {
1467 dev_warn(&ihost->pdev->dev,
1468 "%s: iteration %d, phase %d:"
1469 " SMP error=%d, time_remaining=%lu\n",
1470 __func__, iteration, phy_state, res, tmo);
1471 break;
1472 }
1473 dev_dbg(&ihost->pdev->dev,
1474 "%s: iport %p, iteration %d,"
1475 " phase %d: time_remaining %lu, bcns = %d, "
1476 "attdevtype = %x\n",
1477 __func__, iport, iteration, phy_state,
1478 tmo, test_bit(IPORT_BCN_PENDING, &iport->flags),
1479 attached_device_type);
1480
1481 switch (phy_state) {
1482 case SMP_PHYWAIT_PHYDOWN:
1483 /* Has the device gone away? */
1484 if (!attached_device_type)
1485 phy_state = SMP_PHYWAIT_PHYUP;
1486
1487 break;
1488
1489 case SMP_PHYWAIT_PHYUP:
1490 /* Has the device come back? */
1491 if (attached_device_type)
1492 phy_state = SMP_PHYWAIT_DONE;
1493 break;
1494
1495 case SMP_PHYWAIT_DONE:
1496 break;
1497 }
1498
1499 }
1500 dev_dbg(&ihost->pdev->dev, "%s: done\n", __func__);
1501}
1502
Dan Williams209fae12011-06-13 17:39:44 -07001503static int isci_reset_device(struct isci_host *ihost,
1504 struct isci_remote_device *idev, int hard_reset)
Dan Williams6f231dd2011-07-02 22:56:22 -07001505{
Dan Williams209fae12011-06-13 17:39:44 -07001506 struct sas_phy *phy = sas_find_local_phy(idev->domain_dev);
Jeff Skirvin61aaff42011-06-21 12:16:33 -07001507 struct isci_port *iport = idev->isci_port;
Dan Williams6f231dd2011-07-02 22:56:22 -07001508 enum sci_status status;
Dan Williamsd06b4872011-05-02 13:59:25 -07001509 unsigned long flags;
1510 int rc;
Dan Williams6f231dd2011-07-02 22:56:22 -07001511
Dan Williamsd06b4872011-05-02 13:59:25 -07001512 dev_dbg(&ihost->pdev->dev, "%s: idev %p\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001513
Dan Williamsd06b4872011-05-02 13:59:25 -07001514 spin_lock_irqsave(&ihost->scic_lock, flags);
1515 status = scic_remote_device_reset(&idev->sci);
Dan Williams6f231dd2011-07-02 22:56:22 -07001516 if (status != SCI_SUCCESS) {
Dan Williamsd06b4872011-05-02 13:59:25 -07001517 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001518
Dan Williamsd06b4872011-05-02 13:59:25 -07001519 dev_warn(&ihost->pdev->dev,
1520 "%s: scic_remote_device_reset(%p) returned %d!\n",
1521 __func__, idev, status);
Dan Williams6f231dd2011-07-02 22:56:22 -07001522
1523 return TMF_RESP_FUNC_FAILED;
1524 }
Dan Williamsd06b4872011-05-02 13:59:25 -07001525 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001526
Dan Williams6f231dd2011-07-02 22:56:22 -07001527 /* Make sure all pending requests are able to be fully terminated. */
Dan Williamsd06b4872011-05-02 13:59:25 -07001528 isci_device_clear_reset_pending(ihost, idev);
1529
Jeff Skirvin61aaff42011-06-21 12:16:33 -07001530 /* If this is a device on an expander, disable BCN processing. */
1531 if (!scsi_is_sas_phy_local(phy))
1532 set_bit(IPORT_BCN_BLOCKED, &iport->flags);
1533
Dan Williamsd06b4872011-05-02 13:59:25 -07001534 rc = sas_phy_reset(phy, hard_reset);
Dan Williams6f231dd2011-07-02 22:56:22 -07001535
1536 /* Terminate in-progress I/O now. */
Dan Williamsd06b4872011-05-02 13:59:25 -07001537 isci_remote_device_nuke_requests(ihost, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001538
Jeff Skirvinff717ab2011-06-20 14:08:51 -07001539 /* Since all pending TCs have been cleaned, resume the RNC. */
Dan Williamsd06b4872011-05-02 13:59:25 -07001540 spin_lock_irqsave(&ihost->scic_lock, flags);
1541 status = scic_remote_device_reset_complete(&idev->sci);
1542 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001543
Jeff Skirvin61aaff42011-06-21 12:16:33 -07001544 /* If this is a device on an expander, bring the phy back up. */
1545 if (!scsi_is_sas_phy_local(phy)) {
1546 /* A phy reset will cause the device to go away then reappear.
1547 * Since libsas will take action on incoming BCNs (eg. remove
1548 * a device going through an SMP phy-control driven reset),
1549 * we need to wait until the phy comes back up before letting
1550 * discovery proceed in libsas.
1551 */
1552 isci_wait_for_smp_phy_reset(idev, phy->number);
1553
1554 spin_lock_irqsave(&ihost->scic_lock, flags);
1555 isci_port_bcn_enable(ihost, idev->isci_port);
1556 spin_unlock_irqrestore(&ihost->scic_lock, flags);
1557 }
Jeff Skirvinff717ab2011-06-20 14:08:51 -07001558
Dan Williams6f231dd2011-07-02 22:56:22 -07001559 if (status != SCI_SUCCESS) {
Dan Williamsd06b4872011-05-02 13:59:25 -07001560 dev_warn(&ihost->pdev->dev,
1561 "%s: scic_remote_device_reset_complete(%p) "
1562 "returned %d!\n", __func__, idev, status);
Dan Williams6f231dd2011-07-02 22:56:22 -07001563 }
Dan Williams6f231dd2011-07-02 22:56:22 -07001564
Dan Williamsd06b4872011-05-02 13:59:25 -07001565 dev_dbg(&ihost->pdev->dev, "%s: idev %p complete.\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001566
Dan Williamsd06b4872011-05-02 13:59:25 -07001567 return rc;
1568}
1569
1570int isci_task_I_T_nexus_reset(struct domain_device *dev)
1571{
1572 struct isci_host *ihost = dev_to_ihost(dev);
Dan Williamsd06b4872011-05-02 13:59:25 -07001573 struct isci_remote_device *idev;
Dan Williams209fae12011-06-13 17:39:44 -07001574 int ret, hard_reset = 1;
Dan Williamsd06b4872011-05-02 13:59:25 -07001575 unsigned long flags;
1576
Dan Williamsd06b4872011-05-02 13:59:25 -07001577 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williams209fae12011-06-13 17:39:44 -07001578 idev = isci_lookup_device(dev);
Dan Williamsd06b4872011-05-02 13:59:25 -07001579 spin_unlock_irqrestore(&ihost->scic_lock, flags);
1580
Dan Williams209fae12011-06-13 17:39:44 -07001581 if (!idev || !test_bit(IDEV_EH, &idev->flags)) {
1582 ret = TMF_RESP_FUNC_COMPLETE;
1583 goto out;
1584 }
Dan Williamsd06b4872011-05-02 13:59:25 -07001585
1586 if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP))
1587 hard_reset = 0;
1588
Dan Williams209fae12011-06-13 17:39:44 -07001589 ret = isci_reset_device(ihost, idev, hard_reset);
1590 out:
1591 isci_put_device(idev);
1592 return ret;
Dan Williamsd06b4872011-05-02 13:59:25 -07001593}
1594
1595int isci_bus_reset_handler(struct scsi_cmnd *cmd)
1596{
1597 struct domain_device *dev = sdev_to_domain_dev(cmd->device);
Dan Williams209fae12011-06-13 17:39:44 -07001598 struct isci_host *ihost = dev_to_ihost(dev);
1599 struct isci_remote_device *idev;
1600 int ret, hard_reset = 1;
1601 unsigned long flags;
Dan Williamsd06b4872011-05-02 13:59:25 -07001602
1603 if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP))
1604 hard_reset = 0;
1605
Dan Williams209fae12011-06-13 17:39:44 -07001606 spin_lock_irqsave(&ihost->scic_lock, flags);
1607 idev = isci_lookup_device(dev);
1608 spin_unlock_irqrestore(&ihost->scic_lock, flags);
1609
1610 if (!idev) {
1611 ret = TMF_RESP_FUNC_COMPLETE;
1612 goto out;
1613 }
1614
1615 ret = isci_reset_device(ihost, idev, hard_reset);
1616 out:
1617 isci_put_device(idev);
1618 return ret;
Dan Williams6f231dd2011-07-02 22:56:22 -07001619}