blob: 89b01eef44b1d32a38d17c65e2e41fe25d4e467b [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"
Dan Williams312e0c22011-06-28 13:47:09 -070066#include "host.h"
Dave Jiangaf5ae892011-05-04 17:53:24 -070067
Dan Williams50e7f9b2011-03-09 21:27:46 -080068/**
Dan Williams1077a572011-03-11 10:13:51 -080069* isci_task_refuse() - complete the request to the upper layer driver in
70* the case where an I/O needs to be completed back in the submit path.
71* @ihost: host on which the the request was queued
72* @task: request to complete
73* @response: response code for the completed task.
74* @status: status code for the completed task.
Dan Williams50e7f9b2011-03-09 21:27:46 -080075*
Dan Williams50e7f9b2011-03-09 21:27:46 -080076*/
Dan Williams1077a572011-03-11 10:13:51 -080077static void isci_task_refuse(struct isci_host *ihost, struct sas_task *task,
78 enum service_response response,
79 enum exec_status status)
Dan Williams50e7f9b2011-03-09 21:27:46 -080080
Dan Williams1077a572011-03-11 10:13:51 -080081{
82 enum isci_completion_selection disposition;
83
84 disposition = isci_perform_normal_io_completion;
85 disposition = isci_task_set_completion_status(task, response, status,
86 disposition);
Dan Williams50e7f9b2011-03-09 21:27:46 -080087
88 /* Tasks aborted specifically by a call to the lldd_abort_task
Dan Williams1077a572011-03-11 10:13:51 -080089 * function should not be completed to the host in the regular path.
90 */
91 switch (disposition) {
Dan Williams50e7f9b2011-03-09 21:27:46 -080092 case isci_perform_normal_io_completion:
93 /* Normal notification (task_done) */
Dan Williams1077a572011-03-11 10:13:51 -080094 dev_dbg(&ihost->pdev->dev,
Jeff Skirvined8a72d2011-03-31 13:10:40 -070095 "%s: Normal - task = %p, response=%d, "
96 "status=%d\n",
Dan Williams50e7f9b2011-03-09 21:27:46 -080097 __func__, task, response, status);
98
Dan Williams1077a572011-03-11 10:13:51 -080099 task->lldd_task = NULL;
Dan Williams50e7f9b2011-03-09 21:27:46 -0800100
Jeff Skirvined8a72d2011-03-31 13:10:40 -0700101 isci_execpath_callback(ihost, task, task->task_done);
Dan Williams50e7f9b2011-03-09 21:27:46 -0800102 break;
103
104 case isci_perform_aborted_io_completion:
105 /* No notification because this request is already in the
106 * abort path.
107 */
Dan Williams1077a572011-03-11 10:13:51 -0800108 dev_warn(&ihost->pdev->dev,
Jeff Skirvined8a72d2011-03-31 13:10:40 -0700109 "%s: Aborted - task = %p, response=%d, "
110 "status=%d\n",
Dan Williams50e7f9b2011-03-09 21:27:46 -0800111 __func__, task, response, status);
112 break;
113
114 case isci_perform_error_io_completion:
115 /* Use sas_task_abort */
Dan Williams1077a572011-03-11 10:13:51 -0800116 dev_warn(&ihost->pdev->dev,
Jeff Skirvined8a72d2011-03-31 13:10:40 -0700117 "%s: Error - task = %p, response=%d, "
118 "status=%d\n",
Dan Williams50e7f9b2011-03-09 21:27:46 -0800119 __func__, task, response, status);
Jeff Skirvined8a72d2011-03-31 13:10:40 -0700120
121 isci_execpath_callback(ihost, task, sas_task_abort);
Dan Williams50e7f9b2011-03-09 21:27:46 -0800122 break;
123
124 default:
Dan Williams1077a572011-03-11 10:13:51 -0800125 dev_warn(&ihost->pdev->dev,
Dan Williams50e7f9b2011-03-09 21:27:46 -0800126 "%s: isci task notification default case!",
127 __func__);
128 sas_task_abort(task);
129 break;
130 }
131}
Dan Williams6f231dd2011-07-02 22:56:22 -0700132
Dan Williams1077a572011-03-11 10:13:51 -0800133#define for_each_sas_task(num, task) \
134 for (; num > 0; num--,\
135 task = list_entry(task->list.next, struct sas_task, list))
136
Jeff Skirvin9274f452011-06-23 17:09:02 -0700137
138static inline int isci_device_io_ready(struct isci_remote_device *idev,
139 struct sas_task *task)
140{
141 return idev ? test_bit(IDEV_IO_READY, &idev->flags) ||
142 (test_bit(IDEV_IO_NCQERROR, &idev->flags) &&
143 isci_task_is_ncq_recovery(task))
144 : 0;
145}
Dan Williams6f231dd2011-07-02 22:56:22 -0700146/**
147 * isci_task_execute_task() - This function is one of the SAS Domain Template
148 * functions. This function is called by libsas to send a task down to
149 * hardware.
150 * @task: This parameter specifies the SAS task to send.
151 * @num: This parameter specifies the number of tasks to queue.
152 * @gfp_flags: This parameter specifies the context of this call.
153 *
154 * status, zero indicates success.
155 */
156int isci_task_execute_task(struct sas_task *task, int num, gfp_t gfp_flags)
157{
Dan Williams4393aa42011-03-31 13:10:44 -0700158 struct isci_host *ihost = dev_to_ihost(task->dev);
Dan Williams209fae12011-06-13 17:39:44 -0700159 struct isci_remote_device *idev;
Dan Williamsf2088262011-06-16 11:26:12 -0700160 unsigned long flags;
161 bool io_ready;
Dan Williams312e0c22011-06-28 13:47:09 -0700162 u16 tag;
Dan Williams6f231dd2011-07-02 22:56:22 -0700163
Dan Williams1077a572011-03-11 10:13:51 -0800164 dev_dbg(&ihost->pdev->dev, "%s: num=%d\n", __func__, num);
Dan Williams6f231dd2011-07-02 22:56:22 -0700165
Dan Williams1077a572011-03-11 10:13:51 -0800166 for_each_sas_task(num, task) {
Dan Williams312e0c22011-06-28 13:47:09 -0700167 enum sci_status status = SCI_FAILURE;
168
Dan Williams209fae12011-06-13 17:39:44 -0700169 spin_lock_irqsave(&ihost->scic_lock, flags);
170 idev = isci_lookup_device(task->dev);
Jeff Skirvin9274f452011-06-23 17:09:02 -0700171 io_ready = isci_device_io_ready(idev, task);
Dan Williams312e0c22011-06-28 13:47:09 -0700172 tag = isci_alloc_tag(ihost);
Dan Williams209fae12011-06-13 17:39:44 -0700173 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700174
Dan Williamsf2088262011-06-16 11:26:12 -0700175 dev_dbg(&ihost->pdev->dev,
176 "task: %p, num: %d dev: %p idev: %p:%#lx cmd = %p\n",
177 task, num, task->dev, idev, idev ? idev->flags : 0,
178 task->uldd_task);
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700179
Dan Williamsf2088262011-06-16 11:26:12 -0700180 if (!idev) {
181 isci_task_refuse(ihost, task, SAS_TASK_UNDELIVERED,
182 SAS_DEVICE_UNKNOWN);
Dan Williams312e0c22011-06-28 13:47:09 -0700183 } else if (!io_ready || tag == SCI_CONTROLLER_INVALID_IO_TAG) {
Dan Williamsf2088262011-06-16 11:26:12 -0700184 /* Indicate QUEUE_FULL so that the scsi midlayer
185 * retries.
186 */
187 isci_task_refuse(ihost, task, SAS_TASK_COMPLETE,
188 SAS_QUEUE_FULL);
Dan Williams6f231dd2011-07-02 22:56:22 -0700189 } else {
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700190 /* There is a device and it's ready for I/O. */
191 spin_lock_irqsave(&task->task_state_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700192
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700193 if (task->task_state_flags & SAS_TASK_STATE_ABORTED) {
Dan Williamsf2088262011-06-16 11:26:12 -0700194 /* The I/O was aborted. */
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700195 spin_unlock_irqrestore(&task->task_state_lock,
196 flags);
197
Dan Williams1077a572011-03-11 10:13:51 -0800198 isci_task_refuse(ihost, task,
199 SAS_TASK_UNDELIVERED,
200 SAM_STAT_TASK_ABORTED);
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700201 } else {
Dan Williams6f231dd2011-07-02 22:56:22 -0700202 task->task_state_flags |= SAS_TASK_AT_INITIATOR;
203 spin_unlock_irqrestore(&task->task_state_lock, flags);
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700204
205 /* build and send the request. */
Dan Williamsdb056252011-06-17 14:18:39 -0700206 status = isci_request_execute(ihost, idev, task, tag);
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700207
208 if (status != SCI_SUCCESS) {
209
210 spin_lock_irqsave(&task->task_state_lock, flags);
211 /* Did not really start this command. */
212 task->task_state_flags &= ~SAS_TASK_AT_INITIATOR;
213 spin_unlock_irqrestore(&task->task_state_lock, flags);
214
215 /* Indicate QUEUE_FULL so that the scsi
216 * midlayer retries. if the request
217 * failed for remote device reasons,
218 * it gets returned as
219 * SAS_TASK_UNDELIVERED next time
220 * through.
221 */
Dan Williams1077a572011-03-11 10:13:51 -0800222 isci_task_refuse(ihost, task,
223 SAS_TASK_COMPLETE,
224 SAS_QUEUE_FULL);
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700225 }
Dan Williams6f231dd2011-07-02 22:56:22 -0700226 }
227 }
Dan Williams312e0c22011-06-28 13:47:09 -0700228 if (status != SCI_SUCCESS && tag != SCI_CONTROLLER_INVALID_IO_TAG) {
229 spin_lock_irqsave(&ihost->scic_lock, flags);
230 /* command never hit the device, so just free
231 * the tci and skip the sequence increment
232 */
233 isci_tci_free(ihost, ISCI_TAG_TCI(tag));
234 spin_unlock_irqrestore(&ihost->scic_lock, flags);
235 }
Dan Williams209fae12011-06-13 17:39:44 -0700236 isci_put_device(idev);
Dan Williams1077a572011-03-11 10:13:51 -0800237 }
Dan Williams6f231dd2011-07-02 22:56:22 -0700238 return 0;
239}
240
Dan Williams0d0cf142011-06-13 00:51:30 -0700241static struct isci_request *isci_task_request_build(struct isci_host *ihost,
Dan Williams209fae12011-06-13 17:39:44 -0700242 struct isci_remote_device *idev,
Dan Williams312e0c22011-06-28 13:47:09 -0700243 u16 tag, struct isci_tmf *isci_tmf)
Dan Williams6f231dd2011-07-02 22:56:22 -0700244{
Dan Williams6f231dd2011-07-02 22:56:22 -0700245 enum sci_status status = SCI_FAILURE;
Dan Williams0d0cf142011-06-13 00:51:30 -0700246 struct isci_request *ireq = NULL;
Dan Williamsa1a113b2011-04-21 18:44:45 -0700247 struct domain_device *dev;
Dan Williams6f231dd2011-07-02 22:56:22 -0700248
Dan Williams0d0cf142011-06-13 00:51:30 -0700249 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -0700250 "%s: isci_tmf = %p\n", __func__, isci_tmf);
251
Dan Williams0d0cf142011-06-13 00:51:30 -0700252 dev = idev->domain_dev;
Dan Williams6f231dd2011-07-02 22:56:22 -0700253
254 /* do common allocation and init of request object. */
Dan Williamsdb056252011-06-17 14:18:39 -0700255 ireq = isci_tmf_request_from_tag(ihost, isci_tmf, tag);
Dan Williams0d0cf142011-06-13 00:51:30 -0700256 if (!ireq)
257 return NULL;
Dan Williams6f231dd2011-07-02 22:56:22 -0700258
259 /* let the core do it's construct. */
Dan Williams78a6f062011-06-30 16:31:37 -0700260 status = scic_task_request_construct(&ihost->sci, idev, tag,
Dan Williams5076a1a2011-06-27 14:57:03 -0700261 ireq);
Dan Williams6f231dd2011-07-02 22:56:22 -0700262
263 if (status != SCI_SUCCESS) {
Dan Williams0d0cf142011-06-13 00:51:30 -0700264 dev_warn(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -0700265 "%s: scic_task_request_construct failed - "
266 "status = 0x%x\n",
267 __func__,
268 status);
Dan Williamsdb056252011-06-17 14:18:39 -0700269 return NULL;
Dan Williams6f231dd2011-07-02 22:56:22 -0700270 }
271
Dan Williamsa1a113b2011-04-21 18:44:45 -0700272 /* XXX convert to get this from task->tproto like other drivers */
273 if (dev->dev_type == SAS_END_DEV) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700274 isci_tmf->proto = SAS_PROTOCOL_SSP;
Dan Williams5076a1a2011-06-27 14:57:03 -0700275 status = scic_task_request_construct_ssp(ireq);
Dan Williams6f231dd2011-07-02 22:56:22 -0700276 if (status != SCI_SUCCESS)
Dan Williamsdb056252011-06-17 14:18:39 -0700277 return NULL;
Dan Williams6f231dd2011-07-02 22:56:22 -0700278 }
279
Dan Williamsa1a113b2011-04-21 18:44:45 -0700280 if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP)) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700281 isci_tmf->proto = SAS_PROTOCOL_SATA;
Dan Williams0d0cf142011-06-13 00:51:30 -0700282 status = isci_sata_management_task_request_build(ireq);
Dan Williams6f231dd2011-07-02 22:56:22 -0700283
284 if (status != SCI_SUCCESS)
Dan Williamsdb056252011-06-17 14:18:39 -0700285 return NULL;
Dan Williams6f231dd2011-07-02 22:56:22 -0700286 }
Dan Williams0d0cf142011-06-13 00:51:30 -0700287 return ireq;
Dan Williams6f231dd2011-07-02 22:56:22 -0700288}
289
Dan Williams209fae12011-06-13 17:39:44 -0700290int isci_task_execute_tmf(struct isci_host *ihost,
Dan Williams78a6f062011-06-30 16:31:37 -0700291 struct isci_remote_device *idev,
Dan Williams209fae12011-06-13 17:39:44 -0700292 struct isci_tmf *tmf, unsigned long timeout_ms)
Dan Williams6f231dd2011-07-02 22:56:22 -0700293{
294 DECLARE_COMPLETION_ONSTACK(completion);
Bartosz Barcinski467e8552011-04-12 17:28:41 -0700295 enum sci_task_status status = SCI_TASK_FAILURE;
Dan Williams0d0cf142011-06-13 00:51:30 -0700296 struct isci_request *ireq;
Dan Williams6f231dd2011-07-02 22:56:22 -0700297 int ret = TMF_RESP_FUNC_FAILED;
298 unsigned long flags;
Edmund Nadolskifd183882011-05-19 12:00:15 +0000299 unsigned long timeleft;
Dan Williams312e0c22011-06-28 13:47:09 -0700300 u16 tag;
301
302 spin_lock_irqsave(&ihost->scic_lock, flags);
303 tag = isci_alloc_tag(ihost);
304 spin_unlock_irqrestore(&ihost->scic_lock, flags);
305
306 if (tag == SCI_CONTROLLER_INVALID_IO_TAG)
307 return ret;
Dan Williams6f231dd2011-07-02 22:56:22 -0700308
309 /* sanity check, return TMF_RESP_FUNC_FAILED
310 * if the device is not there and ready.
311 */
Dan Williams78a6f062011-06-30 16:31:37 -0700312 if (!idev ||
313 (!test_bit(IDEV_IO_READY, &idev->flags) &&
314 !test_bit(IDEV_IO_NCQERROR, &idev->flags))) {
Dan Williams0d0cf142011-06-13 00:51:30 -0700315 dev_dbg(&ihost->pdev->dev,
Dan Williams78a6f062011-06-30 16:31:37 -0700316 "%s: idev = %p not ready (%#lx)\n",
Dan Williams6f231dd2011-07-02 22:56:22 -0700317 __func__,
Dan Williams78a6f062011-06-30 16:31:37 -0700318 idev, idev ? idev->flags : 0);
Dan Williams312e0c22011-06-28 13:47:09 -0700319 goto err_tci;
Dan Williams6f231dd2011-07-02 22:56:22 -0700320 } else
Dan Williams0d0cf142011-06-13 00:51:30 -0700321 dev_dbg(&ihost->pdev->dev,
Dan Williams78a6f062011-06-30 16:31:37 -0700322 "%s: idev = %p\n",
323 __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -0700324
325 /* Assign the pointer to the TMF's completion kernel wait structure. */
326 tmf->complete = &completion;
327
Dan Williams78a6f062011-06-30 16:31:37 -0700328 ireq = isci_task_request_build(ihost, idev, tag, tmf);
Dan Williams312e0c22011-06-28 13:47:09 -0700329 if (!ireq)
330 goto err_tci;
Dan Williams6f231dd2011-07-02 22:56:22 -0700331
Dan Williams0d0cf142011-06-13 00:51:30 -0700332 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700333
334 /* start the TMF io. */
Dan Williams78a6f062011-06-30 16:31:37 -0700335 status = scic_controller_start_task(&ihost->sci, idev, ireq);
Dan Williams6f231dd2011-07-02 22:56:22 -0700336
Bartosz Barcinski467e8552011-04-12 17:28:41 -0700337 if (status != SCI_TASK_SUCCESS) {
Dan Williams0d0cf142011-06-13 00:51:30 -0700338 dev_warn(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -0700339 "%s: start_io failed - status = 0x%x, request = %p\n",
340 __func__,
341 status,
Dan Williams0d0cf142011-06-13 00:51:30 -0700342 ireq);
343 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williamsdb056252011-06-17 14:18:39 -0700344 goto err_tci;
Dan Williams6f231dd2011-07-02 22:56:22 -0700345 }
346
Dan Williams6f231dd2011-07-02 22:56:22 -0700347 if (tmf->cb_state_func != NULL)
348 tmf->cb_state_func(isci_tmf_started, tmf, tmf->cb_data);
349
Dan Williams0d0cf142011-06-13 00:51:30 -0700350 isci_request_change_state(ireq, started);
Dan Williams6f231dd2011-07-02 22:56:22 -0700351
352 /* add the request to the remote device request list. */
Dan Williams78a6f062011-06-30 16:31:37 -0700353 list_add(&ireq->dev_node, &idev->reqs_in_process);
Dan Williams6f231dd2011-07-02 22:56:22 -0700354
Dan Williams0d0cf142011-06-13 00:51:30 -0700355 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700356
357 /* Wait for the TMF to complete, or a timeout. */
Edmund Nadolskifd183882011-05-19 12:00:15 +0000358 timeleft = wait_for_completion_timeout(&completion,
Dan Williams086a0da2011-06-21 16:23:03 -0700359 msecs_to_jiffies(timeout_ms));
Edmund Nadolskifd183882011-05-19 12:00:15 +0000360
361 if (timeleft == 0) {
Dan Williams0d0cf142011-06-13 00:51:30 -0700362 spin_lock_irqsave(&ihost->scic_lock, flags);
Edmund Nadolskifd183882011-05-19 12:00:15 +0000363
364 if (tmf->cb_state_func != NULL)
365 tmf->cb_state_func(isci_tmf_timed_out, tmf, tmf->cb_data);
366
Dan Williams086a0da2011-06-21 16:23:03 -0700367 scic_controller_terminate_request(&ihost->sci,
Dan Williams78a6f062011-06-30 16:31:37 -0700368 idev,
Dan Williams5076a1a2011-06-27 14:57:03 -0700369 ireq);
Edmund Nadolskifd183882011-05-19 12:00:15 +0000370
Dan Williams0d0cf142011-06-13 00:51:30 -0700371 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams086a0da2011-06-21 16:23:03 -0700372
373 wait_for_completion(tmf->complete);
Edmund Nadolskifd183882011-05-19 12:00:15 +0000374 }
Dan Williams6f231dd2011-07-02 22:56:22 -0700375
376 isci_print_tmf(tmf);
377
378 if (tmf->status == SCI_SUCCESS)
379 ret = TMF_RESP_FUNC_COMPLETE;
380 else if (tmf->status == SCI_FAILURE_IO_RESPONSE_VALID) {
Dan Williams0d0cf142011-06-13 00:51:30 -0700381 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -0700382 "%s: tmf.status == "
383 "SCI_FAILURE_IO_RESPONSE_VALID\n",
384 __func__);
385 ret = TMF_RESP_FUNC_COMPLETE;
386 }
387 /* Else - leave the default "failed" status alone. */
388
Dan Williams0d0cf142011-06-13 00:51:30 -0700389 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -0700390 "%s: completed request = %p\n",
391 __func__,
Dan Williams0d0cf142011-06-13 00:51:30 -0700392 ireq);
Dan Williams6f231dd2011-07-02 22:56:22 -0700393
Dan Williams6f231dd2011-07-02 22:56:22 -0700394 return ret;
Dan Williams312e0c22011-06-28 13:47:09 -0700395
Dan Williams312e0c22011-06-28 13:47:09 -0700396 err_tci:
397 spin_lock_irqsave(&ihost->scic_lock, flags);
398 isci_tci_free(ihost, ISCI_TAG_TCI(tag));
399 spin_unlock_irqrestore(&ihost->scic_lock, flags);
400
401 return ret;
Dan Williams6f231dd2011-07-02 22:56:22 -0700402}
403
404void isci_task_build_tmf(
405 struct isci_tmf *tmf,
Dan Williams6f231dd2011-07-02 22:56:22 -0700406 enum isci_tmf_function_codes code,
407 void (*tmf_sent_cb)(enum isci_tmf_cb_state,
408 struct isci_tmf *,
409 void *),
Jeff Skirvinc3f42fe2011-03-04 14:06:56 -0800410 void *cb_data)
Dan Williams6f231dd2011-07-02 22:56:22 -0700411{
Dan Williams6f231dd2011-07-02 22:56:22 -0700412 memset(tmf, 0, sizeof(*tmf));
413
Dan Williams6f231dd2011-07-02 22:56:22 -0700414 tmf->tmf_code = code;
Dan Williams6f231dd2011-07-02 22:56:22 -0700415 tmf->cb_state_func = tmf_sent_cb;
Jeff Skirvinc3f42fe2011-03-04 14:06:56 -0800416 tmf->cb_data = cb_data;
417}
Jeff Skirvin1fad9e92011-03-04 14:06:46 -0800418
Dan Williams35173d52011-03-26 16:43:01 -0700419static void isci_task_build_abort_task_tmf(
Jeff Skirvinc3f42fe2011-03-04 14:06:56 -0800420 struct isci_tmf *tmf,
Jeff Skirvinc3f42fe2011-03-04 14:06:56 -0800421 enum isci_tmf_function_codes code,
422 void (*tmf_sent_cb)(enum isci_tmf_cb_state,
423 struct isci_tmf *,
424 void *),
425 struct isci_request *old_request)
426{
Dan Williams209fae12011-06-13 17:39:44 -0700427 isci_task_build_tmf(tmf, code, tmf_sent_cb,
Jeff Skirvinc3f42fe2011-03-04 14:06:56 -0800428 (void *)old_request);
429 tmf->io_tag = old_request->io_tag;
Dan Williams6f231dd2011-07-02 22:56:22 -0700430}
431
Dan Williams6f231dd2011-07-02 22:56:22 -0700432/**
433 * isci_task_validate_request_to_abort() - This function checks the given I/O
434 * against the "started" state. If the request is still "started", it's
435 * state is changed to aborted. NOTE: isci_host->scic_lock MUST BE HELD
436 * BEFORE CALLING THIS FUNCTION.
437 * @isci_request: This parameter specifies the request object to control.
438 * @isci_host: This parameter specifies the ISCI host object
439 * @isci_device: This is the device to which the request is pending.
440 * @aborted_io_completion: This is a completion structure that will be added to
441 * the request in case it is changed to aborting; this completion is
442 * triggered when the request is fully completed.
443 *
444 * Either "started" on successful change of the task status to "aborted", or
445 * "unallocated" if the task cannot be controlled.
446 */
447static enum isci_request_status isci_task_validate_request_to_abort(
448 struct isci_request *isci_request,
449 struct isci_host *isci_host,
450 struct isci_remote_device *isci_device,
451 struct completion *aborted_io_completion)
452{
453 enum isci_request_status old_state = unallocated;
454
455 /* Only abort the task if it's in the
456 * device's request_in_process list
457 */
458 if (isci_request && !list_empty(&isci_request->dev_node)) {
459 old_state = isci_request_change_started_to_aborted(
460 isci_request, aborted_io_completion);
461
Dan Williams6f231dd2011-07-02 22:56:22 -0700462 }
463
464 return old_state;
465}
466
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700467/**
468* isci_request_cleanup_completed_loiterer() - This function will take care of
469* the final cleanup on any request which has been explicitly terminated.
470* @isci_host: This parameter specifies the ISCI host object
471* @isci_device: This is the device to which the request is pending.
472* @isci_request: This parameter specifies the terminated request object.
473* @task: This parameter is the libsas I/O request.
474*/
Dan Williams6f231dd2011-07-02 22:56:22 -0700475static void isci_request_cleanup_completed_loiterer(
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700476 struct isci_host *isci_host,
Dan Williams6f231dd2011-07-02 22:56:22 -0700477 struct isci_remote_device *isci_device,
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700478 struct isci_request *isci_request,
479 struct sas_task *task)
Dan Williams6f231dd2011-07-02 22:56:22 -0700480{
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700481 unsigned long flags;
Dan Williams6f231dd2011-07-02 22:56:22 -0700482
483 dev_dbg(&isci_host->pdev->dev,
484 "%s: isci_device=%p, request=%p, task=%p\n",
Jeff Skirvin18d3d722011-03-04 14:06:38 -0800485 __func__, isci_device, isci_request, task);
Dan Williams6f231dd2011-07-02 22:56:22 -0700486
Jeff Skirvina5fde222011-03-04 14:06:42 -0800487 if (task != NULL) {
488
489 spin_lock_irqsave(&task->task_state_lock, flags);
490 task->lldd_task = NULL;
491
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700492 task->task_state_flags &= ~SAS_TASK_NEED_DEV_RESET;
493
Jeff Skirvina5fde222011-03-04 14:06:42 -0800494 isci_set_task_doneflags(task);
495
496 /* If this task is not in the abort path, call task_done. */
497 if (!(task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
498
499 spin_unlock_irqrestore(&task->task_state_lock, flags);
500 task->task_done(task);
501 } else
502 spin_unlock_irqrestore(&task->task_state_lock, flags);
503 }
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800504
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700505 if (isci_request != NULL) {
506 spin_lock_irqsave(&isci_host->scic_lock, flags);
507 list_del_init(&isci_request->dev_node);
508 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800509 }
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800510}
511
Dan Williams6f231dd2011-07-02 22:56:22 -0700512/**
513 * isci_terminate_request_core() - This function will terminate the given
514 * request, and wait for it to complete. This function must only be called
515 * from a thread that can wait. Note that the request is terminated and
516 * completed (back to the host, if started there).
517 * @isci_host: This SCU.
Dan Williams78a6f062011-06-30 16:31:37 -0700518 * @idev: The target.
Dan Williams6f231dd2011-07-02 22:56:22 -0700519 * @isci_request: The I/O request to be terminated.
520 *
Dan Williams6f231dd2011-07-02 22:56:22 -0700521 */
522static void isci_terminate_request_core(
523 struct isci_host *isci_host,
Dan Williams78a6f062011-06-30 16:31:37 -0700524 struct isci_remote_device *idev,
Jeff Skirvincbb65c62011-03-04 14:06:50 -0800525 struct isci_request *isci_request)
Dan Williams6f231dd2011-07-02 22:56:22 -0700526{
Jeff Skirvincbb65c62011-03-04 14:06:50 -0800527 enum sci_status status = SCI_SUCCESS;
Dan Williams6f231dd2011-07-02 22:56:22 -0700528 bool was_terminated = false;
529 bool needs_cleanup_handling = false;
530 enum isci_request_status request_status;
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700531 unsigned long flags;
532 unsigned long termination_completed = 1;
533 struct completion *io_request_completion;
534 struct sas_task *task;
Dan Williams6f231dd2011-07-02 22:56:22 -0700535
536 dev_dbg(&isci_host->pdev->dev,
537 "%s: device = %p; request = %p\n",
Dan Williams78a6f062011-06-30 16:31:37 -0700538 __func__, idev, isci_request);
Dan Williams6f231dd2011-07-02 22:56:22 -0700539
Dan Williams6f231dd2011-07-02 22:56:22 -0700540 spin_lock_irqsave(&isci_host->scic_lock, flags);
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800541
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700542 io_request_completion = isci_request->io_request_completion;
543
544 task = (isci_request->ttype == io_task)
545 ? isci_request_access_task(isci_request)
546 : NULL;
547
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800548 /* Note that we are not going to control
Dan Williams38d88792011-06-23 14:33:48 -0700549 * the target to abort the request.
550 */
551 set_bit(IREQ_COMPLETE_IN_TARGET, &isci_request->flags);
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800552
Dan Williams6f231dd2011-07-02 22:56:22 -0700553 /* Make sure the request wasn't just sitting around signalling
554 * device condition (if the request handle is NULL, then the
555 * request completed but needed additional handling here).
556 */
Dan Williams38d88792011-06-23 14:33:48 -0700557 if (!test_bit(IREQ_TERMINATED, &isci_request->flags)) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700558 was_terminated = true;
Jeff Skirvincbb65c62011-03-04 14:06:50 -0800559 needs_cleanup_handling = true;
Dan Williams6f231dd2011-07-02 22:56:22 -0700560 status = scic_controller_terminate_request(
Artur Wojcikcc3dbd02011-05-04 07:58:16 +0000561 &isci_host->sci,
Dan Williams78a6f062011-06-30 16:31:37 -0700562 idev,
Dan Williams5076a1a2011-06-27 14:57:03 -0700563 isci_request);
Dan Williams6f231dd2011-07-02 22:56:22 -0700564 }
565 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
566
567 /*
568 * The only time the request to terminate will
569 * fail is when the io request is completed and
570 * being aborted.
571 */
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800572 if (status != SCI_SUCCESS) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700573 dev_err(&isci_host->pdev->dev,
574 "%s: scic_controller_terminate_request"
575 " returned = 0x%x\n",
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700576 __func__, status);
577
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800578 isci_request->io_request_completion = NULL;
579
580 } else {
Dan Williams6f231dd2011-07-02 22:56:22 -0700581 if (was_terminated) {
582 dev_dbg(&isci_host->pdev->dev,
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700583 "%s: before completion wait (%p/%p)\n",
584 __func__, isci_request, io_request_completion);
Dan Williams6f231dd2011-07-02 22:56:22 -0700585
586 /* Wait here for the request to complete. */
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700587 #define TERMINATION_TIMEOUT_MSEC 500
588 termination_completed
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800589 = wait_for_completion_timeout(
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700590 io_request_completion,
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800591 msecs_to_jiffies(TERMINATION_TIMEOUT_MSEC));
Dan Williams6f231dd2011-07-02 22:56:22 -0700592
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700593 if (!termination_completed) {
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800594
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700595 /* The request to terminate has timed out. */
596 spin_lock_irqsave(&isci_host->scic_lock,
597 flags);
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800598
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700599 /* Check for state changes. */
Dan Williams38d88792011-06-23 14:33:48 -0700600 if (!test_bit(IREQ_TERMINATED, &isci_request->flags)) {
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800601
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700602 /* The best we can do is to have the
603 * request die a silent death if it
604 * ever really completes.
605 *
606 * Set the request state to "dead",
607 * and clear the task pointer so that
608 * an actual completion event callback
609 * doesn't do anything.
610 */
611 isci_request->status = dead;
612 isci_request->io_request_completion
613 = NULL;
614
615 if (isci_request->ttype == io_task) {
616
617 /* Break links with the
618 * sas_task.
619 */
620 isci_request->ttype_ptr.io_task_ptr
621 = NULL;
622 }
623 } else
624 termination_completed = 1;
625
626 spin_unlock_irqrestore(&isci_host->scic_lock,
627 flags);
628
629 if (!termination_completed) {
630
631 dev_err(&isci_host->pdev->dev,
632 "%s: *** Timeout waiting for "
633 "termination(%p/%p)\n",
634 __func__, io_request_completion,
635 isci_request);
636
637 /* The request can no longer be referenced
638 * safely since it may go away if the
639 * termination every really does complete.
640 */
641 isci_request = NULL;
642 }
643 }
644 if (termination_completed)
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800645 dev_dbg(&isci_host->pdev->dev,
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700646 "%s: after completion wait (%p/%p)\n",
647 __func__, isci_request, io_request_completion);
Dan Williams6f231dd2011-07-02 22:56:22 -0700648 }
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800649
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700650 if (termination_completed) {
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800651
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700652 isci_request->io_request_completion = NULL;
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800653
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700654 /* Peek at the status of the request. This will tell
655 * us if there was special handling on the request such that it
656 * needs to be detached and freed here.
657 */
658 spin_lock_irqsave(&isci_request->state_lock, flags);
659 request_status = isci_request_get_state(isci_request);
660
661 if ((isci_request->ttype == io_task) /* TMFs are in their own thread */
662 && ((request_status == aborted)
663 || (request_status == aborting)
664 || (request_status == terminating)
665 || (request_status == completed)
666 || (request_status == dead)
667 )
668 ) {
669
670 /* The completion routine won't free a request in
671 * the aborted/aborting/etc. states, so we do
672 * it here.
673 */
674 needs_cleanup_handling = true;
675 }
676 spin_unlock_irqrestore(&isci_request->state_lock, flags);
677
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800678 }
Dan Williams6f231dd2011-07-02 22:56:22 -0700679 if (needs_cleanup_handling)
680 isci_request_cleanup_completed_loiterer(
Dan Williams78a6f062011-06-30 16:31:37 -0700681 isci_host, idev, isci_request, task);
Jeff Skirvina5fde222011-03-04 14:06:42 -0800682 }
Dan Williams6f231dd2011-07-02 22:56:22 -0700683}
684
685/**
686 * isci_terminate_pending_requests() - This function will change the all of the
687 * requests on the given device's state to "aborting", will terminate the
688 * requests, and wait for them to complete. This function must only be
689 * called from a thread that can wait. Note that the requests are all
690 * terminated and completed (back to the host, if started there).
691 * @isci_host: This parameter specifies SCU.
Dan Williams78a6f062011-06-30 16:31:37 -0700692 * @idev: This parameter specifies the target.
Dan Williams6f231dd2011-07-02 22:56:22 -0700693 *
Dan Williams6f231dd2011-07-02 22:56:22 -0700694 */
Dan Williams980d3ae2011-06-20 15:11:22 -0700695void isci_terminate_pending_requests(struct isci_host *ihost,
696 struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -0700697{
Dan Williams980d3ae2011-06-20 15:11:22 -0700698 struct completion request_completion;
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700699 enum isci_request_status old_state;
Dan Williams980d3ae2011-06-20 15:11:22 -0700700 unsigned long flags;
701 LIST_HEAD(list);
Dan Williams6f231dd2011-07-02 22:56:22 -0700702
Dan Williams980d3ae2011-06-20 15:11:22 -0700703 spin_lock_irqsave(&ihost->scic_lock, flags);
704 list_splice_init(&idev->reqs_in_process, &list);
Dan Williams6f231dd2011-07-02 22:56:22 -0700705
Dan Williams980d3ae2011-06-20 15:11:22 -0700706 /* assumes that isci_terminate_request_core deletes from the list */
707 while (!list_empty(&list)) {
708 struct isci_request *ireq = list_entry(list.next, typeof(*ireq), dev_node);
Dan Williams6f231dd2011-07-02 22:56:22 -0700709
Dan Williams980d3ae2011-06-20 15:11:22 -0700710 /* Change state to "terminating" if it is currently
711 * "started".
712 */
713 old_state = isci_request_change_started_to_newstate(ireq,
714 &request_completion,
715 terminating);
716 switch (old_state) {
717 case started:
718 case completed:
719 case aborting:
720 break;
721 default:
722 /* termination in progress, or otherwise dispositioned.
723 * We know the request was on 'list' so should be safe
724 * to move it back to reqs_in_process
725 */
726 list_move(&ireq->dev_node, &idev->reqs_in_process);
727 ireq = NULL;
728 break;
729 }
730
731 if (!ireq)
732 continue;
733 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800734
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700735 init_completion(&request_completion);
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800736
Dan Williams980d3ae2011-06-20 15:11:22 -0700737 dev_dbg(&ihost->pdev->dev,
738 "%s: idev=%p request=%p; task=%p old_state=%d\n",
739 __func__, idev, ireq,
740 ireq->ttype == io_task ? isci_request_access_task(ireq) : NULL,
741 old_state);
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700742
Dan Williams980d3ae2011-06-20 15:11:22 -0700743 /* If the old_state is started:
744 * This request was not already being aborted. If it had been,
745 * then the aborting I/O (ie. the TMF request) would not be in
746 * the aborting state, and thus would be terminated here. Note
747 * that since the TMF completion's call to the kernel function
748 * "complete()" does not happen until the pending I/O request
749 * terminate fully completes, we do not have to implement a
750 * special wait here for already aborting requests - the
751 * termination of the TMF request will force the request
752 * to finish it's already started terminate.
753 *
754 * If old_state == completed:
755 * This request completed from the SCU hardware perspective
756 * and now just needs cleaning up in terms of freeing the
757 * request and potentially calling up to libsas.
758 *
759 * If old_state == aborting:
760 * This request has already gone through a TMF timeout, but may
761 * not have been terminated; needs cleaning up at least.
762 */
763 isci_terminate_request_core(ihost, idev, ireq);
764 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700765 }
Dan Williams980d3ae2011-06-20 15:11:22 -0700766 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700767}
768
769/**
770 * isci_task_send_lu_reset_sas() - This function is called by of the SAS Domain
771 * Template functions.
772 * @lun: This parameter specifies the lun to be reset.
773 *
774 * status, zero indicates success.
775 */
776static int isci_task_send_lu_reset_sas(
777 struct isci_host *isci_host,
778 struct isci_remote_device *isci_device,
779 u8 *lun)
780{
781 struct isci_tmf tmf;
782 int ret = TMF_RESP_FUNC_FAILED;
783
784 dev_dbg(&isci_host->pdev->dev,
785 "%s: isci_host = %p, isci_device = %p\n",
786 __func__, isci_host, isci_device);
787 /* Send the LUN reset to the target. By the time the call returns,
788 * the TMF has fully exected in the target (in which case the return
789 * value is "TMF_RESP_FUNC_COMPLETE", or the request timed-out (or
790 * was otherwise unable to be executed ("TMF_RESP_FUNC_FAILED").
791 */
Dan Williams209fae12011-06-13 17:39:44 -0700792 isci_task_build_tmf(&tmf, isci_tmf_ssp_lun_reset, NULL, NULL);
Dan Williams6f231dd2011-07-02 22:56:22 -0700793
794 #define ISCI_LU_RESET_TIMEOUT_MS 2000 /* 2 second timeout. */
Dan Williams209fae12011-06-13 17:39:44 -0700795 ret = isci_task_execute_tmf(isci_host, isci_device, &tmf, ISCI_LU_RESET_TIMEOUT_MS);
Dan Williams6f231dd2011-07-02 22:56:22 -0700796
797 if (ret == TMF_RESP_FUNC_COMPLETE)
798 dev_dbg(&isci_host->pdev->dev,
799 "%s: %p: TMF_LU_RESET passed\n",
800 __func__, isci_device);
801 else
802 dev_dbg(&isci_host->pdev->dev,
803 "%s: %p: TMF_LU_RESET failed (%x)\n",
804 __func__, isci_device, ret);
805
806 return ret;
807}
808
809/**
810 * isci_task_lu_reset() - This function is one of the SAS Domain Template
811 * functions. This is one of the Task Management functoins called by libsas,
812 * to reset the given lun. Note the assumption that while this call is
813 * executing, no I/O will be sent by the host to the device.
814 * @lun: This parameter specifies the lun to be reset.
815 *
816 * status, zero indicates success.
817 */
Dan Williams4393aa42011-03-31 13:10:44 -0700818int isci_task_lu_reset(struct domain_device *domain_device, u8 *lun)
Dan Williams6f231dd2011-07-02 22:56:22 -0700819{
Dan Williams4393aa42011-03-31 13:10:44 -0700820 struct isci_host *isci_host = dev_to_ihost(domain_device);
Dan Williams209fae12011-06-13 17:39:44 -0700821 struct isci_remote_device *isci_device;
822 unsigned long flags;
Dan Williams6f231dd2011-07-02 22:56:22 -0700823 int ret;
Dan Williams6f231dd2011-07-02 22:56:22 -0700824
Dan Williams209fae12011-06-13 17:39:44 -0700825 spin_lock_irqsave(&isci_host->scic_lock, flags);
826 isci_device = isci_lookup_device(domain_device);
827 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700828
Dan Williams4393aa42011-03-31 13:10:44 -0700829 dev_dbg(&isci_host->pdev->dev,
830 "%s: domain_device=%p, isci_host=%p; isci_device=%p\n",
Dan Williams6f231dd2011-07-02 22:56:22 -0700831 __func__, domain_device, isci_host, isci_device);
832
Dan Williams209fae12011-06-13 17:39:44 -0700833 if (isci_device)
Dan Williamsd06b4872011-05-02 13:59:25 -0700834 set_bit(IDEV_EH, &isci_device->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700835
836 /* If there is a device reset pending on any request in the
837 * device's list, fail this LUN reset request in order to
838 * escalate to the device reset.
839 */
Dan Williams209fae12011-06-13 17:39:44 -0700840 if (!isci_device ||
Dan Williams4393aa42011-03-31 13:10:44 -0700841 isci_device_is_reset_pending(isci_host, isci_device)) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700842 dev_warn(&isci_host->pdev->dev,
Dan Williams4393aa42011-03-31 13:10:44 -0700843 "%s: No dev (%p), or "
Dan Williams6f231dd2011-07-02 22:56:22 -0700844 "RESET PENDING: domain_device=%p\n",
Dan Williams4393aa42011-03-31 13:10:44 -0700845 __func__, isci_device, domain_device);
Dan Williams209fae12011-06-13 17:39:44 -0700846 ret = TMF_RESP_FUNC_FAILED;
847 goto out;
Dan Williams6f231dd2011-07-02 22:56:22 -0700848 }
849
Dan Williams6f231dd2011-07-02 22:56:22 -0700850 /* Send the task management part of the reset. */
851 if (sas_protocol_ata(domain_device->tproto)) {
Dan Williams4393aa42011-03-31 13:10:44 -0700852 ret = isci_task_send_lu_reset_sata(isci_host, isci_device, lun);
Dan Williams6f231dd2011-07-02 22:56:22 -0700853 } else
854 ret = isci_task_send_lu_reset_sas(isci_host, isci_device, lun);
855
856 /* If the LUN reset worked, all the I/O can now be terminated. */
857 if (ret == TMF_RESP_FUNC_COMPLETE)
858 /* Terminate all I/O now. */
859 isci_terminate_pending_requests(isci_host,
Dan Williams980d3ae2011-06-20 15:11:22 -0700860 isci_device);
Dan Williams6f231dd2011-07-02 22:56:22 -0700861
Dan Williams209fae12011-06-13 17:39:44 -0700862 out:
863 isci_put_device(isci_device);
Dan Williams6f231dd2011-07-02 22:56:22 -0700864 return ret;
865}
866
867
868/* int (*lldd_clear_nexus_port)(struct asd_sas_port *); */
869int isci_task_clear_nexus_port(struct asd_sas_port *port)
870{
871 return TMF_RESP_FUNC_FAILED;
872}
873
874
875
876int isci_task_clear_nexus_ha(struct sas_ha_struct *ha)
877{
878 return TMF_RESP_FUNC_FAILED;
879}
880
Dan Williams6f231dd2011-07-02 22:56:22 -0700881/* Task Management Functions. Must be called from process context. */
882
883/**
884 * isci_abort_task_process_cb() - This is a helper function for the abort task
885 * TMF command. It manages the request state with respect to the successful
886 * transmission / completion of the abort task request.
887 * @cb_state: This parameter specifies when this function was called - after
888 * the TMF request has been started and after it has timed-out.
889 * @tmf: This parameter specifies the TMF in progress.
890 *
891 *
892 */
893static void isci_abort_task_process_cb(
894 enum isci_tmf_cb_state cb_state,
895 struct isci_tmf *tmf,
896 void *cb_data)
897{
898 struct isci_request *old_request;
899
900 old_request = (struct isci_request *)cb_data;
901
902 dev_dbg(&old_request->isci_host->pdev->dev,
903 "%s: tmf=%p, old_request=%p\n",
904 __func__, tmf, old_request);
905
906 switch (cb_state) {
907
908 case isci_tmf_started:
909 /* The TMF has been started. Nothing to do here, since the
910 * request state was already set to "aborted" by the abort
911 * task function.
912 */
Bartosz Barcinski6cb4d6b2011-04-12 17:28:43 -0700913 if ((old_request->status != aborted)
914 && (old_request->status != completed))
915 dev_err(&old_request->isci_host->pdev->dev,
916 "%s: Bad request status (%d): tmf=%p, old_request=%p\n",
917 __func__, old_request->status, tmf, old_request);
Dan Williams6f231dd2011-07-02 22:56:22 -0700918 break;
919
920 case isci_tmf_timed_out:
921
922 /* Set the task's state to "aborting", since the abort task
923 * function thread set it to "aborted" (above) in anticipation
924 * of the task management request working correctly. Since the
925 * timeout has now fired, the TMF request failed. We set the
926 * state such that the request completion will indicate the
927 * device is no longer present.
928 */
929 isci_request_change_state(old_request, aborting);
930 break;
931
932 default:
933 dev_err(&old_request->isci_host->pdev->dev,
934 "%s: Bad cb_state (%d): tmf=%p, old_request=%p\n",
935 __func__, cb_state, tmf, old_request);
936 break;
937 }
938}
939
940/**
941 * isci_task_abort_task() - This function is one of the SAS Domain Template
942 * functions. This function is called by libsas to abort a specified task.
943 * @task: This parameter specifies the SAS task to abort.
944 *
945 * status, zero indicates success.
946 */
947int isci_task_abort_task(struct sas_task *task)
948{
Dan Williams4393aa42011-03-31 13:10:44 -0700949 struct isci_host *isci_host = dev_to_ihost(task->dev);
Dan Williams6f231dd2011-07-02 22:56:22 -0700950 DECLARE_COMPLETION_ONSTACK(aborted_io_completion);
Jeff Skirvina5fde222011-03-04 14:06:42 -0800951 struct isci_request *old_request = NULL;
952 enum isci_request_status old_state;
Dan Williams6f231dd2011-07-02 22:56:22 -0700953 struct isci_remote_device *isci_device = NULL;
Jeff Skirvina5fde222011-03-04 14:06:42 -0800954 struct isci_tmf tmf;
955 int ret = TMF_RESP_FUNC_FAILED;
956 unsigned long flags;
957 bool any_dev_reset = false;
Dan Williams6f231dd2011-07-02 22:56:22 -0700958
959 /* Get the isci_request reference from the task. Note that
960 * this check does not depend on the pending request list
961 * in the device, because tasks driving resets may land here
962 * after completion in the core.
963 */
Dan Williams209fae12011-06-13 17:39:44 -0700964 spin_lock_irqsave(&isci_host->scic_lock, flags);
965 spin_lock(&task->task_state_lock);
966
967 old_request = task->lldd_task;
968
969 /* If task is already done, the request isn't valid */
970 if (!(task->task_state_flags & SAS_TASK_STATE_DONE) &&
971 (task->task_state_flags & SAS_TASK_AT_INITIATOR) &&
972 old_request)
973 isci_device = isci_lookup_device(task->dev);
974
975 spin_unlock(&task->task_state_lock);
976 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700977
978 dev_dbg(&isci_host->pdev->dev,
979 "%s: task = %p\n", __func__, task);
980
Dan Williams209fae12011-06-13 17:39:44 -0700981 if (!isci_device || !old_request)
982 goto out;
Dan Williams6f231dd2011-07-02 22:56:22 -0700983
Dan Williamsd06b4872011-05-02 13:59:25 -0700984 set_bit(IDEV_EH, &isci_device->flags);
985
Dan Williams6f231dd2011-07-02 22:56:22 -0700986 /* This version of the driver will fail abort requests for
987 * SATA/STP. Failing the abort request this way will cause the
988 * SCSI error handler thread to escalate to LUN reset
989 */
Dan Williams209fae12011-06-13 17:39:44 -0700990 if (sas_protocol_ata(task->task_proto)) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700991 dev_warn(&isci_host->pdev->dev,
992 " task %p is for a STP/SATA device;"
993 " returning TMF_RESP_FUNC_FAILED\n"
994 " to cause a LUN reset...\n", task);
Dan Williams209fae12011-06-13 17:39:44 -0700995 goto out;
Dan Williams6f231dd2011-07-02 22:56:22 -0700996 }
997
998 dev_dbg(&isci_host->pdev->dev,
999 "%s: old_request == %p\n", __func__, old_request);
1000
Dan Williams209fae12011-06-13 17:39:44 -07001001 any_dev_reset = isci_device_is_reset_pending(isci_host,isci_device);
Jeff Skirvina5fde222011-03-04 14:06:42 -08001002
Dan Williams6f231dd2011-07-02 22:56:22 -07001003 spin_lock_irqsave(&task->task_state_lock, flags);
1004
Dan Williams209fae12011-06-13 17:39:44 -07001005 any_dev_reset = any_dev_reset || (task->task_state_flags & SAS_TASK_NEED_DEV_RESET);
Dan Williams6f231dd2011-07-02 22:56:22 -07001006
1007 /* If the extraction of the request reference from the task
1008 * failed, then the request has been completed (or if there is a
1009 * pending reset then this abort request function must be failed
1010 * in order to escalate to the target reset).
1011 */
Jeff Skirvina5fde222011-03-04 14:06:42 -08001012 if ((old_request == NULL) || any_dev_reset) {
Dan Williams6f231dd2011-07-02 22:56:22 -07001013
1014 /* If the device reset task flag is set, fail the task
1015 * management request. Otherwise, the original request
1016 * has completed.
1017 */
1018 if (any_dev_reset) {
1019
1020 /* Turn off the task's DONE to make sure this
1021 * task is escalated to a target reset.
1022 */
1023 task->task_state_flags &= ~SAS_TASK_STATE_DONE;
1024
Jeff Skirvina5fde222011-03-04 14:06:42 -08001025 /* Make the reset happen as soon as possible. */
1026 task->task_state_flags |= SAS_TASK_NEED_DEV_RESET;
1027
1028 spin_unlock_irqrestore(&task->task_state_lock, flags);
1029
Dan Williams6f231dd2011-07-02 22:56:22 -07001030 /* Fail the task management request in order to
1031 * escalate to the target reset.
1032 */
1033 ret = TMF_RESP_FUNC_FAILED;
1034
1035 dev_dbg(&isci_host->pdev->dev,
1036 "%s: Failing task abort in order to "
1037 "escalate to target reset because\n"
1038 "SAS_TASK_NEED_DEV_RESET is set for "
1039 "task %p on dev %p\n",
1040 __func__, task, isci_device);
1041
Jeff Skirvina5fde222011-03-04 14:06:42 -08001042
Dan Williams6f231dd2011-07-02 22:56:22 -07001043 } else {
Dan Williams6f231dd2011-07-02 22:56:22 -07001044 /* The request has already completed and there
1045 * is nothing to do here other than to set the task
1046 * done bit, and indicate that the task abort function
1047 * was sucessful.
1048 */
1049 isci_set_task_doneflags(task);
1050
Jeff Skirvina5fde222011-03-04 14:06:42 -08001051 spin_unlock_irqrestore(&task->task_state_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001052
Jeff Skirvina5fde222011-03-04 14:06:42 -08001053 ret = TMF_RESP_FUNC_COMPLETE;
Dan Williams6f231dd2011-07-02 22:56:22 -07001054
Jeff Skirvina5fde222011-03-04 14:06:42 -08001055 dev_dbg(&isci_host->pdev->dev,
1056 "%s: abort task not needed for %p\n",
1057 __func__, task);
Dan Williams6f231dd2011-07-02 22:56:22 -07001058 }
Dan Williams209fae12011-06-13 17:39:44 -07001059 goto out;
Dan Williams6f231dd2011-07-02 22:56:22 -07001060 }
Jeff Skirvina5fde222011-03-04 14:06:42 -08001061 else
1062 spin_unlock_irqrestore(&task->task_state_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001063
1064 spin_lock_irqsave(&isci_host->scic_lock, flags);
1065
Jeff Skirvinf219f012011-03-31 13:10:34 -07001066 /* Check the request status and change to "aborted" if currently
Jeff Skirvina5fde222011-03-04 14:06:42 -08001067 * "starting"; if true then set the I/O kernel completion
Dan Williams6f231dd2011-07-02 22:56:22 -07001068 * struct that will be triggered when the request completes.
1069 */
Jeff Skirvina5fde222011-03-04 14:06:42 -08001070 old_state = isci_task_validate_request_to_abort(
1071 old_request, isci_host, isci_device,
1072 &aborted_io_completion);
Jeff Skirvinf219f012011-03-31 13:10:34 -07001073 if ((old_state != started) &&
1074 (old_state != completed) &&
1075 (old_state != aborting)) {
Dan Williams6f231dd2011-07-02 22:56:22 -07001076
1077 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1078
Jeff Skirvina5fde222011-03-04 14:06:42 -08001079 /* The request was already being handled by someone else (because
1080 * they got to set the state away from started).
1081 */
1082 dev_dbg(&isci_host->pdev->dev,
1083 "%s: device = %p; old_request %p already being aborted\n",
1084 __func__,
1085 isci_device, old_request);
Dan Williams209fae12011-06-13 17:39:44 -07001086 ret = TMF_RESP_FUNC_COMPLETE;
1087 goto out;
Dan Williams6f231dd2011-07-02 22:56:22 -07001088 }
Dan Williams38d88792011-06-23 14:33:48 -07001089 if (task->task_proto == SAS_PROTOCOL_SMP ||
1090 test_bit(IREQ_COMPLETE_IN_TARGET, &old_request->flags)) {
Dan Williams6f231dd2011-07-02 22:56:22 -07001091
1092 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1093
Jeff Skirvina5fde222011-03-04 14:06:42 -08001094 dev_dbg(&isci_host->pdev->dev,
1095 "%s: SMP request (%d)"
Jeff Skirvina5fde222011-03-04 14:06:42 -08001096 " or complete_in_target (%d), thus no TMF\n",
1097 __func__, (task->task_proto == SAS_PROTOCOL_SMP),
Dan Williams38d88792011-06-23 14:33:48 -07001098 test_bit(IREQ_COMPLETE_IN_TARGET, &old_request->flags));
Jeff Skirvina5fde222011-03-04 14:06:42 -08001099
Dan Williams6f231dd2011-07-02 22:56:22 -07001100 /* Set the state on the task. */
1101 isci_task_all_done(task);
1102
1103 ret = TMF_RESP_FUNC_COMPLETE;
1104
1105 /* Stopping and SMP devices are not sent a TMF, and are not
Jeff Skirvina5fde222011-03-04 14:06:42 -08001106 * reset, but the outstanding I/O request is terminated below.
Dan Williams6f231dd2011-07-02 22:56:22 -07001107 */
Dan Williams6f231dd2011-07-02 22:56:22 -07001108 } else {
1109 /* Fill in the tmf stucture */
Dan Williams209fae12011-06-13 17:39:44 -07001110 isci_task_build_abort_task_tmf(&tmf, isci_tmf_ssp_task_abort,
Jeff Skirvinc3f42fe2011-03-04 14:06:56 -08001111 isci_abort_task_process_cb,
1112 old_request);
Dan Williams6f231dd2011-07-02 22:56:22 -07001113
Dan Williams6f231dd2011-07-02 22:56:22 -07001114 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1115
1116 #define ISCI_ABORT_TASK_TIMEOUT_MS 500 /* half second timeout. */
Dan Williams209fae12011-06-13 17:39:44 -07001117 ret = isci_task_execute_tmf(isci_host, isci_device, &tmf,
Dan Williams6f231dd2011-07-02 22:56:22 -07001118 ISCI_ABORT_TASK_TIMEOUT_MS);
1119
Jeff Skirvina5fde222011-03-04 14:06:42 -08001120 if (ret != TMF_RESP_FUNC_COMPLETE)
Dan Williams6f231dd2011-07-02 22:56:22 -07001121 dev_err(&isci_host->pdev->dev,
1122 "%s: isci_task_send_tmf failed\n",
1123 __func__);
1124 }
Jeff Skirvina5fde222011-03-04 14:06:42 -08001125 if (ret == TMF_RESP_FUNC_COMPLETE) {
Dan Williams38d88792011-06-23 14:33:48 -07001126 set_bit(IREQ_COMPLETE_IN_TARGET, &old_request->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001127
Jeff Skirvin77c852f2011-06-20 14:09:16 -07001128 /* Clean up the request on our side, and wait for the aborted
1129 * I/O to complete.
1130 */
Jeff Skirvincbb65c62011-03-04 14:06:50 -08001131 isci_terminate_request_core(isci_host, isci_device, old_request);
Jeff Skirvina5fde222011-03-04 14:06:42 -08001132 }
1133
1134 /* Make sure we do not leave a reference to aborted_io_completion */
1135 old_request->io_request_completion = NULL;
Dan Williams209fae12011-06-13 17:39:44 -07001136 out:
1137 isci_put_device(isci_device);
Dan Williams6f231dd2011-07-02 22:56:22 -07001138 return ret;
1139}
1140
1141/**
1142 * isci_task_abort_task_set() - This function is one of the SAS Domain Template
1143 * functions. This is one of the Task Management functoins called by libsas,
1144 * to abort all task for the given lun.
1145 * @d_device: This parameter specifies the domain device associated with this
1146 * request.
1147 * @lun: This parameter specifies the lun associated with this request.
1148 *
1149 * status, zero indicates success.
1150 */
1151int isci_task_abort_task_set(
1152 struct domain_device *d_device,
1153 u8 *lun)
1154{
1155 return TMF_RESP_FUNC_FAILED;
1156}
1157
1158
1159/**
1160 * isci_task_clear_aca() - This function is one of the SAS Domain Template
1161 * functions. This is one of the Task Management functoins called by libsas.
1162 * @d_device: This parameter specifies the domain device associated with this
1163 * request.
1164 * @lun: This parameter specifies the lun associated with this request.
1165 *
1166 * status, zero indicates success.
1167 */
1168int isci_task_clear_aca(
1169 struct domain_device *d_device,
1170 u8 *lun)
1171{
1172 return TMF_RESP_FUNC_FAILED;
1173}
1174
1175
1176
1177/**
1178 * isci_task_clear_task_set() - This function is one of the SAS Domain Template
1179 * functions. This is one of the Task Management functoins called by libsas.
1180 * @d_device: This parameter specifies the domain device associated with this
1181 * request.
1182 * @lun: This parameter specifies the lun associated with this request.
1183 *
1184 * status, zero indicates success.
1185 */
1186int isci_task_clear_task_set(
1187 struct domain_device *d_device,
1188 u8 *lun)
1189{
1190 return TMF_RESP_FUNC_FAILED;
1191}
1192
1193
1194/**
1195 * isci_task_query_task() - This function is implemented to cause libsas to
1196 * correctly escalate the failed abort to a LUN or target reset (this is
1197 * because sas_scsi_find_task libsas function does not correctly interpret
1198 * all return codes from the abort task call). When TMF_RESP_FUNC_SUCC is
1199 * returned, libsas turns this into a LUN reset; when FUNC_FAILED is
1200 * returned, libsas will turn this into a target reset
1201 * @task: This parameter specifies the sas task being queried.
1202 * @lun: This parameter specifies the lun associated with this request.
1203 *
1204 * status, zero indicates success.
1205 */
1206int isci_task_query_task(
1207 struct sas_task *task)
1208{
1209 /* See if there is a pending device reset for this device. */
1210 if (task->task_state_flags & SAS_TASK_NEED_DEV_RESET)
1211 return TMF_RESP_FUNC_FAILED;
1212 else
1213 return TMF_RESP_FUNC_SUCC;
1214}
1215
Dave Jiangaf5ae892011-05-04 17:53:24 -07001216/*
Dan Williams6f231dd2011-07-02 22:56:22 -07001217 * isci_task_request_complete() - This function is called by the sci core when
1218 * an task request completes.
Dave Jiangaf5ae892011-05-04 17:53:24 -07001219 * @ihost: This parameter specifies the ISCI host object
1220 * @ireq: This parameter is the completed isci_request object.
Dan Williams6f231dd2011-07-02 22:56:22 -07001221 * @completion_status: This parameter specifies the completion status from the
1222 * sci core.
1223 *
1224 * none.
1225 */
Dave Jiangaf5ae892011-05-04 17:53:24 -07001226void
1227isci_task_request_complete(struct isci_host *ihost,
1228 struct isci_request *ireq,
1229 enum sci_task_status completion_status)
Dan Williams6f231dd2011-07-02 22:56:22 -07001230{
Dave Jiangaf5ae892011-05-04 17:53:24 -07001231 struct isci_tmf *tmf = isci_request_access_tmf(ireq);
Dan Williams6f231dd2011-07-02 22:56:22 -07001232 struct completion *tmf_complete;
1233
Dave Jiangaf5ae892011-05-04 17:53:24 -07001234 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -07001235 "%s: request = %p, status=%d\n",
Dave Jiangaf5ae892011-05-04 17:53:24 -07001236 __func__, ireq, completion_status);
Dan Williams6f231dd2011-07-02 22:56:22 -07001237
Dave Jiang8d2c65c2011-06-01 09:03:08 +00001238 isci_request_change_state(ireq, completed);
Dan Williams6f231dd2011-07-02 22:56:22 -07001239
1240 tmf->status = completion_status;
Dan Williams38d88792011-06-23 14:33:48 -07001241 set_bit(IREQ_COMPLETE_IN_TARGET, &ireq->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001242
Dave Jiangaf5ae892011-05-04 17:53:24 -07001243 if (tmf->proto == SAS_PROTOCOL_SSP) {
Dan Williams6f231dd2011-07-02 22:56:22 -07001244 memcpy(&tmf->resp.resp_iu,
Dan Williams5076a1a2011-06-27 14:57:03 -07001245 &ireq->ssp.rsp,
Dave Jiangaf5ae892011-05-04 17:53:24 -07001246 SSP_RESP_IU_MAX_SIZE);
1247 } else if (tmf->proto == SAS_PROTOCOL_SATA) {
Dan Williams6f231dd2011-07-02 22:56:22 -07001248 memcpy(&tmf->resp.d2h_fis,
Dan Williams5076a1a2011-06-27 14:57:03 -07001249 &ireq->stp.rsp,
Dave Jiangf2f30082011-05-04 15:02:02 -07001250 sizeof(struct dev_to_host_fis));
Dan Williams6f231dd2011-07-02 22:56:22 -07001251 }
1252
Dan Williams6f231dd2011-07-02 22:56:22 -07001253 /* PRINT_TMF( ((struct isci_tmf *)request->task)); */
1254 tmf_complete = tmf->complete;
1255
Dan Williams5076a1a2011-06-27 14:57:03 -07001256 scic_controller_complete_io(&ihost->sci, ireq->target_device, ireq);
Dan Williams67ea8382011-05-08 11:47:15 -07001257 /* set the 'terminated' flag handle to make sure it cannot be terminated
Dan Williams6f231dd2011-07-02 22:56:22 -07001258 * or completed again.
1259 */
Dan Williams38d88792011-06-23 14:33:48 -07001260 set_bit(IREQ_TERMINATED, &ireq->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001261
Dave Jiangaf5ae892011-05-04 17:53:24 -07001262 isci_request_change_state(ireq, unallocated);
1263 list_del_init(&ireq->dev_node);
Dan Williams6f231dd2011-07-02 22:56:22 -07001264
1265 /* The task management part completes last. */
1266 complete(tmf_complete);
1267}
1268
Jeff Skirvin61aaff42011-06-21 12:16:33 -07001269static void isci_smp_task_timedout(unsigned long _task)
1270{
1271 struct sas_task *task = (void *) _task;
1272 unsigned long flags;
1273
1274 spin_lock_irqsave(&task->task_state_lock, flags);
1275 if (!(task->task_state_flags & SAS_TASK_STATE_DONE))
1276 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
1277 spin_unlock_irqrestore(&task->task_state_lock, flags);
1278
1279 complete(&task->completion);
1280}
1281
1282static void isci_smp_task_done(struct sas_task *task)
1283{
1284 if (!del_timer(&task->timer))
1285 return;
1286 complete(&task->completion);
1287}
1288
1289static struct sas_task *isci_alloc_task(void)
1290{
1291 struct sas_task *task = kzalloc(sizeof(*task), GFP_KERNEL);
1292
1293 if (task) {
1294 INIT_LIST_HEAD(&task->list);
1295 spin_lock_init(&task->task_state_lock);
1296 task->task_state_flags = SAS_TASK_STATE_PENDING;
1297 init_timer(&task->timer);
1298 init_completion(&task->completion);
1299 }
1300
1301 return task;
1302}
1303
1304static void isci_free_task(struct isci_host *ihost, struct sas_task *task)
1305{
1306 if (task) {
1307 BUG_ON(!list_empty(&task->list));
1308 kfree(task);
1309 }
1310}
1311
1312static int isci_smp_execute_task(struct isci_host *ihost,
1313 struct domain_device *dev, void *req,
1314 int req_size, void *resp, int resp_size)
1315{
1316 int res, retry;
1317 struct sas_task *task = NULL;
1318
1319 for (retry = 0; retry < 3; retry++) {
1320 task = isci_alloc_task();
1321 if (!task)
1322 return -ENOMEM;
1323
1324 task->dev = dev;
1325 task->task_proto = dev->tproto;
1326 sg_init_one(&task->smp_task.smp_req, req, req_size);
1327 sg_init_one(&task->smp_task.smp_resp, resp, resp_size);
1328
1329 task->task_done = isci_smp_task_done;
1330
1331 task->timer.data = (unsigned long) task;
1332 task->timer.function = isci_smp_task_timedout;
1333 task->timer.expires = jiffies + 10*HZ;
1334 add_timer(&task->timer);
1335
1336 res = isci_task_execute_task(task, 1, GFP_KERNEL);
1337
1338 if (res) {
1339 del_timer(&task->timer);
1340 dev_err(&ihost->pdev->dev,
1341 "%s: executing SMP task failed:%d\n",
1342 __func__, res);
1343 goto ex_err;
1344 }
1345
1346 wait_for_completion(&task->completion);
1347 res = -ECOMM;
1348 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
1349 dev_err(&ihost->pdev->dev,
1350 "%s: smp task timed out or aborted\n",
1351 __func__);
1352 isci_task_abort_task(task);
1353 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
1354 dev_err(&ihost->pdev->dev,
1355 "%s: SMP task aborted and not done\n",
1356 __func__);
1357 goto ex_err;
1358 }
1359 }
1360 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1361 task->task_status.stat == SAM_STAT_GOOD) {
1362 res = 0;
1363 break;
1364 }
1365 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1366 task->task_status.stat == SAS_DATA_UNDERRUN) {
1367 /* no error, but return the number of bytes of
1368 * underrun */
1369 res = task->task_status.residual;
1370 break;
1371 }
1372 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1373 task->task_status.stat == SAS_DATA_OVERRUN) {
1374 res = -EMSGSIZE;
1375 break;
1376 } else {
1377 dev_err(&ihost->pdev->dev,
1378 "%s: task to dev %016llx response: 0x%x "
1379 "status 0x%x\n", __func__,
1380 SAS_ADDR(dev->sas_addr),
1381 task->task_status.resp,
1382 task->task_status.stat);
1383 isci_free_task(ihost, task);
1384 task = NULL;
1385 }
1386 }
1387ex_err:
1388 BUG_ON(retry == 3 && task != NULL);
1389 isci_free_task(ihost, task);
1390 return res;
1391}
1392
1393#define DISCOVER_REQ_SIZE 16
1394#define DISCOVER_RESP_SIZE 56
1395
1396int isci_smp_get_phy_attached_dev_type(struct isci_host *ihost,
1397 struct domain_device *dev,
1398 int phy_id, int *adt)
1399{
1400 struct smp_resp *disc_resp;
1401 u8 *disc_req;
1402 int res;
1403
1404 disc_resp = kzalloc(DISCOVER_RESP_SIZE, GFP_KERNEL);
1405 if (!disc_resp)
1406 return -ENOMEM;
1407
1408 disc_req = kzalloc(DISCOVER_REQ_SIZE, GFP_KERNEL);
1409 if (disc_req) {
1410 disc_req[0] = SMP_REQUEST;
1411 disc_req[1] = SMP_DISCOVER;
1412 disc_req[9] = phy_id;
1413 } else {
1414 kfree(disc_resp);
1415 return -ENOMEM;
1416 }
1417 res = isci_smp_execute_task(ihost, dev, disc_req, DISCOVER_REQ_SIZE,
1418 disc_resp, DISCOVER_RESP_SIZE);
1419 if (!res) {
1420 if (disc_resp->result != SMP_RESP_FUNC_ACC)
1421 res = disc_resp->result;
1422 else
1423 *adt = disc_resp->disc.attached_dev_type;
1424 }
1425 kfree(disc_req);
1426 kfree(disc_resp);
1427
1428 return res;
1429}
1430
1431static void isci_wait_for_smp_phy_reset(struct isci_remote_device *idev, int phy_num)
1432{
1433 struct domain_device *dev = idev->domain_dev;
1434 struct isci_port *iport = idev->isci_port;
1435 struct isci_host *ihost = iport->isci_host;
1436 int res, iteration = 0, attached_device_type;
1437 #define STP_WAIT_MSECS 25000
1438 unsigned long tmo = msecs_to_jiffies(STP_WAIT_MSECS);
1439 unsigned long deadline = jiffies + tmo;
1440 enum {
1441 SMP_PHYWAIT_PHYDOWN,
1442 SMP_PHYWAIT_PHYUP,
1443 SMP_PHYWAIT_DONE
1444 } phy_state = SMP_PHYWAIT_PHYDOWN;
1445
1446 /* While there is time, wait for the phy to go away and come back */
1447 while (time_is_after_jiffies(deadline) && phy_state != SMP_PHYWAIT_DONE) {
1448 int event = atomic_read(&iport->event);
1449
1450 ++iteration;
1451
1452 tmo = wait_event_timeout(ihost->eventq,
1453 event != atomic_read(&iport->event) ||
1454 !test_bit(IPORT_BCN_BLOCKED, &iport->flags),
1455 tmo);
1456 /* link down, stop polling */
1457 if (!test_bit(IPORT_BCN_BLOCKED, &iport->flags))
1458 break;
1459
1460 dev_dbg(&ihost->pdev->dev,
1461 "%s: iport %p, iteration %d,"
1462 " phase %d: time_remaining %lu, bcns = %d\n",
1463 __func__, iport, iteration, phy_state,
1464 tmo, test_bit(IPORT_BCN_PENDING, &iport->flags));
1465
1466 res = isci_smp_get_phy_attached_dev_type(ihost, dev, phy_num,
1467 &attached_device_type);
1468 tmo = deadline - jiffies;
1469
1470 if (res) {
1471 dev_warn(&ihost->pdev->dev,
1472 "%s: iteration %d, phase %d:"
1473 " SMP error=%d, time_remaining=%lu\n",
1474 __func__, iteration, phy_state, res, tmo);
1475 break;
1476 }
1477 dev_dbg(&ihost->pdev->dev,
1478 "%s: iport %p, iteration %d,"
1479 " phase %d: time_remaining %lu, bcns = %d, "
1480 "attdevtype = %x\n",
1481 __func__, iport, iteration, phy_state,
1482 tmo, test_bit(IPORT_BCN_PENDING, &iport->flags),
1483 attached_device_type);
1484
1485 switch (phy_state) {
1486 case SMP_PHYWAIT_PHYDOWN:
1487 /* Has the device gone away? */
1488 if (!attached_device_type)
1489 phy_state = SMP_PHYWAIT_PHYUP;
1490
1491 break;
1492
1493 case SMP_PHYWAIT_PHYUP:
1494 /* Has the device come back? */
1495 if (attached_device_type)
1496 phy_state = SMP_PHYWAIT_DONE;
1497 break;
1498
1499 case SMP_PHYWAIT_DONE:
1500 break;
1501 }
1502
1503 }
1504 dev_dbg(&ihost->pdev->dev, "%s: done\n", __func__);
1505}
1506
Dan Williams209fae12011-06-13 17:39:44 -07001507static int isci_reset_device(struct isci_host *ihost,
1508 struct isci_remote_device *idev, int hard_reset)
Dan Williams6f231dd2011-07-02 22:56:22 -07001509{
Dan Williams209fae12011-06-13 17:39:44 -07001510 struct sas_phy *phy = sas_find_local_phy(idev->domain_dev);
Jeff Skirvin61aaff42011-06-21 12:16:33 -07001511 struct isci_port *iport = idev->isci_port;
Dan Williams6f231dd2011-07-02 22:56:22 -07001512 enum sci_status status;
Dan Williamsd06b4872011-05-02 13:59:25 -07001513 unsigned long flags;
1514 int rc;
Dan Williams6f231dd2011-07-02 22:56:22 -07001515
Dan Williamsd06b4872011-05-02 13:59:25 -07001516 dev_dbg(&ihost->pdev->dev, "%s: idev %p\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001517
Dan Williamsd06b4872011-05-02 13:59:25 -07001518 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williams78a6f062011-06-30 16:31:37 -07001519 status = scic_remote_device_reset(idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001520 if (status != SCI_SUCCESS) {
Dan Williamsd06b4872011-05-02 13:59:25 -07001521 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001522
Dan Williamsd06b4872011-05-02 13:59:25 -07001523 dev_warn(&ihost->pdev->dev,
1524 "%s: scic_remote_device_reset(%p) returned %d!\n",
1525 __func__, idev, status);
Dan Williams6f231dd2011-07-02 22:56:22 -07001526
1527 return TMF_RESP_FUNC_FAILED;
1528 }
Dan Williamsd06b4872011-05-02 13:59:25 -07001529 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001530
Dan Williams6f231dd2011-07-02 22:56:22 -07001531 /* Make sure all pending requests are able to be fully terminated. */
Dan Williamsd06b4872011-05-02 13:59:25 -07001532 isci_device_clear_reset_pending(ihost, idev);
1533
Jeff Skirvin61aaff42011-06-21 12:16:33 -07001534 /* If this is a device on an expander, disable BCN processing. */
1535 if (!scsi_is_sas_phy_local(phy))
1536 set_bit(IPORT_BCN_BLOCKED, &iport->flags);
1537
Dan Williamsd06b4872011-05-02 13:59:25 -07001538 rc = sas_phy_reset(phy, hard_reset);
Dan Williams6f231dd2011-07-02 22:56:22 -07001539
1540 /* Terminate in-progress I/O now. */
Dan Williamsd06b4872011-05-02 13:59:25 -07001541 isci_remote_device_nuke_requests(ihost, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001542
Jeff Skirvinff717ab2011-06-20 14:08:51 -07001543 /* Since all pending TCs have been cleaned, resume the RNC. */
Dan Williamsd06b4872011-05-02 13:59:25 -07001544 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williams78a6f062011-06-30 16:31:37 -07001545 status = scic_remote_device_reset_complete(idev);
Dan Williamsd06b4872011-05-02 13:59:25 -07001546 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001547
Jeff Skirvin61aaff42011-06-21 12:16:33 -07001548 /* If this is a device on an expander, bring the phy back up. */
1549 if (!scsi_is_sas_phy_local(phy)) {
1550 /* A phy reset will cause the device to go away then reappear.
1551 * Since libsas will take action on incoming BCNs (eg. remove
1552 * a device going through an SMP phy-control driven reset),
1553 * we need to wait until the phy comes back up before letting
1554 * discovery proceed in libsas.
1555 */
1556 isci_wait_for_smp_phy_reset(idev, phy->number);
1557
1558 spin_lock_irqsave(&ihost->scic_lock, flags);
1559 isci_port_bcn_enable(ihost, idev->isci_port);
1560 spin_unlock_irqrestore(&ihost->scic_lock, flags);
1561 }
Jeff Skirvinff717ab2011-06-20 14:08:51 -07001562
Dan Williams6f231dd2011-07-02 22:56:22 -07001563 if (status != SCI_SUCCESS) {
Dan Williamsd06b4872011-05-02 13:59:25 -07001564 dev_warn(&ihost->pdev->dev,
1565 "%s: scic_remote_device_reset_complete(%p) "
1566 "returned %d!\n", __func__, idev, status);
Dan Williams6f231dd2011-07-02 22:56:22 -07001567 }
Dan Williams6f231dd2011-07-02 22:56:22 -07001568
Dan Williamsd06b4872011-05-02 13:59:25 -07001569 dev_dbg(&ihost->pdev->dev, "%s: idev %p complete.\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001570
Dan Williamsd06b4872011-05-02 13:59:25 -07001571 return rc;
1572}
1573
1574int isci_task_I_T_nexus_reset(struct domain_device *dev)
1575{
1576 struct isci_host *ihost = dev_to_ihost(dev);
Dan Williamsd06b4872011-05-02 13:59:25 -07001577 struct isci_remote_device *idev;
Dan Williams209fae12011-06-13 17:39:44 -07001578 int ret, hard_reset = 1;
Dan Williamsd06b4872011-05-02 13:59:25 -07001579 unsigned long flags;
1580
Dan Williamsd06b4872011-05-02 13:59:25 -07001581 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williams209fae12011-06-13 17:39:44 -07001582 idev = isci_lookup_device(dev);
Dan Williamsd06b4872011-05-02 13:59:25 -07001583 spin_unlock_irqrestore(&ihost->scic_lock, flags);
1584
Dan Williams209fae12011-06-13 17:39:44 -07001585 if (!idev || !test_bit(IDEV_EH, &idev->flags)) {
1586 ret = TMF_RESP_FUNC_COMPLETE;
1587 goto out;
1588 }
Dan Williamsd06b4872011-05-02 13:59:25 -07001589
1590 if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP))
1591 hard_reset = 0;
1592
Dan Williams209fae12011-06-13 17:39:44 -07001593 ret = isci_reset_device(ihost, idev, hard_reset);
1594 out:
1595 isci_put_device(idev);
1596 return ret;
Dan Williamsd06b4872011-05-02 13:59:25 -07001597}
1598
1599int isci_bus_reset_handler(struct scsi_cmnd *cmd)
1600{
1601 struct domain_device *dev = sdev_to_domain_dev(cmd->device);
Dan Williams209fae12011-06-13 17:39:44 -07001602 struct isci_host *ihost = dev_to_ihost(dev);
1603 struct isci_remote_device *idev;
1604 int ret, hard_reset = 1;
1605 unsigned long flags;
Dan Williamsd06b4872011-05-02 13:59:25 -07001606
1607 if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP))
1608 hard_reset = 0;
1609
Dan Williams209fae12011-06-13 17:39:44 -07001610 spin_lock_irqsave(&ihost->scic_lock, flags);
1611 idev = isci_lookup_device(dev);
1612 spin_unlock_irqrestore(&ihost->scic_lock, flags);
1613
1614 if (!idev) {
1615 ret = TMF_RESP_FUNC_COMPLETE;
1616 goto out;
1617 }
1618
1619 ret = isci_reset_device(ihost, idev, hard_reset);
1620 out:
1621 isci_put_device(idev);
1622 return ret;
Dan Williams6f231dd2011-07-02 22:56:22 -07001623}