blob: b96e6044eda9f4f154e4448d5bd988950af123d7 [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"
Dan Williams6f231dd2011-07-02 22:56:22 -070064#include "task.h"
Dan Williams312e0c22011-06-28 13:47:09 -070065#include "host.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) {
James Bottomleya5ec7f862011-07-03 14:14:45 -050091 case isci_perform_normal_io_completion:
92 /* Normal notification (task_done) */
93 dev_dbg(&ihost->pdev->dev,
94 "%s: Normal - task = %p, response=%d, "
95 "status=%d\n",
96 __func__, task, response, status);
Dan Williams50e7f9b2011-03-09 21:27:46 -080097
James Bottomleya5ec7f862011-07-03 14:14:45 -050098 task->lldd_task = NULL;
Dan Williams312d3e52011-11-17 17:59:50 -080099 task->task_done(task);
James Bottomleya5ec7f862011-07-03 14:14:45 -0500100 break;
Dan Williams50e7f9b2011-03-09 21:27:46 -0800101
James Bottomleya5ec7f862011-07-03 14:14:45 -0500102 case isci_perform_aborted_io_completion:
103 /*
104 * No notification because this request is already in the
105 * abort path.
106 */
107 dev_dbg(&ihost->pdev->dev,
108 "%s: Aborted - task = %p, response=%d, "
109 "status=%d\n",
110 __func__, task, response, status);
111 break;
Dan Williams50e7f9b2011-03-09 21:27:46 -0800112
James Bottomleya5ec7f862011-07-03 14:14:45 -0500113 case isci_perform_error_io_completion:
114 /* Use sas_task_abort */
115 dev_dbg(&ihost->pdev->dev,
116 "%s: Error - task = %p, response=%d, "
117 "status=%d\n",
118 __func__, task, response, status);
Dan Williams312d3e52011-11-17 17:59:50 -0800119 sas_task_abort(task);
James Bottomleya5ec7f862011-07-03 14:14:45 -0500120 break;
Dan Williams50e7f9b2011-03-09 21:27:46 -0800121
James Bottomleya5ec7f862011-07-03 14:14:45 -0500122 default:
123 dev_dbg(&ihost->pdev->dev,
124 "%s: isci task notification default case!",
125 __func__);
126 sas_task_abort(task);
127 break;
Dan Williams50e7f9b2011-03-09 21:27:46 -0800128 }
129}
Dan Williams6f231dd2011-07-02 22:56:22 -0700130
Dan Williams1077a572011-03-11 10:13:51 -0800131#define for_each_sas_task(num, task) \
132 for (; num > 0; num--,\
133 task = list_entry(task->list.next, struct sas_task, list))
134
Jeff Skirvin9274f452011-06-23 17:09:02 -0700135
136static inline int isci_device_io_ready(struct isci_remote_device *idev,
137 struct sas_task *task)
138{
139 return idev ? test_bit(IDEV_IO_READY, &idev->flags) ||
140 (test_bit(IDEV_IO_NCQERROR, &idev->flags) &&
141 isci_task_is_ncq_recovery(task))
142 : 0;
143}
Dan Williams6f231dd2011-07-02 22:56:22 -0700144/**
145 * isci_task_execute_task() - This function is one of the SAS Domain Template
146 * functions. This function is called by libsas to send a task down to
147 * hardware.
148 * @task: This parameter specifies the SAS task to send.
149 * @num: This parameter specifies the number of tasks to queue.
150 * @gfp_flags: This parameter specifies the context of this call.
151 *
152 * status, zero indicates success.
153 */
154int isci_task_execute_task(struct sas_task *task, int num, gfp_t gfp_flags)
155{
Dan Williams4393aa42011-03-31 13:10:44 -0700156 struct isci_host *ihost = dev_to_ihost(task->dev);
Dan Williams209fae12011-06-13 17:39:44 -0700157 struct isci_remote_device *idev;
Dan Williamsf2088262011-06-16 11:26:12 -0700158 unsigned long flags;
159 bool io_ready;
Dan Williams312e0c22011-06-28 13:47:09 -0700160 u16 tag;
Dan Williams6f231dd2011-07-02 22:56:22 -0700161
Dan Williams1077a572011-03-11 10:13:51 -0800162 dev_dbg(&ihost->pdev->dev, "%s: num=%d\n", __func__, num);
Dan Williams6f231dd2011-07-02 22:56:22 -0700163
Dan Williams1077a572011-03-11 10:13:51 -0800164 for_each_sas_task(num, task) {
Dan Williams312e0c22011-06-28 13:47:09 -0700165 enum sci_status status = SCI_FAILURE;
166
Dan Williams209fae12011-06-13 17:39:44 -0700167 spin_lock_irqsave(&ihost->scic_lock, flags);
168 idev = isci_lookup_device(task->dev);
Jeff Skirvin9274f452011-06-23 17:09:02 -0700169 io_ready = isci_device_io_ready(idev, task);
Dan Williams312e0c22011-06-28 13:47:09 -0700170 tag = isci_alloc_tag(ihost);
Dan Williams209fae12011-06-13 17:39:44 -0700171 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700172
Dan Williamsf2088262011-06-16 11:26:12 -0700173 dev_dbg(&ihost->pdev->dev,
174 "task: %p, num: %d dev: %p idev: %p:%#lx cmd = %p\n",
175 task, num, task->dev, idev, idev ? idev->flags : 0,
176 task->uldd_task);
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700177
Dan Williamsf2088262011-06-16 11:26:12 -0700178 if (!idev) {
179 isci_task_refuse(ihost, task, SAS_TASK_UNDELIVERED,
180 SAS_DEVICE_UNKNOWN);
Dan Williams312e0c22011-06-28 13:47:09 -0700181 } else if (!io_ready || tag == SCI_CONTROLLER_INVALID_IO_TAG) {
Dan Williamsf2088262011-06-16 11:26:12 -0700182 /* Indicate QUEUE_FULL so that the scsi midlayer
183 * retries.
184 */
185 isci_task_refuse(ihost, task, SAS_TASK_COMPLETE,
186 SAS_QUEUE_FULL);
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);
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700199 } else {
Dan Williams6f231dd2011-07-02 22:56:22 -0700200 task->task_state_flags |= SAS_TASK_AT_INITIATOR;
201 spin_unlock_irqrestore(&task->task_state_lock, flags);
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700202
203 /* build and send the request. */
Dan Williamsdb056252011-06-17 14:18:39 -0700204 status = isci_request_execute(ihost, idev, task, tag);
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700205
206 if (status != SCI_SUCCESS) {
207
208 spin_lock_irqsave(&task->task_state_lock, flags);
209 /* Did not really start this command. */
210 task->task_state_flags &= ~SAS_TASK_AT_INITIATOR;
211 spin_unlock_irqrestore(&task->task_state_lock, flags);
212
Jeff Skirvinc2cb8a52011-10-27 15:04:56 -0700213 if (test_bit(IDEV_GONE, &idev->flags)) {
214
215 /* Indicate that the device
216 * is gone.
217 */
218 isci_task_refuse(ihost, task,
219 SAS_TASK_UNDELIVERED,
220 SAS_DEVICE_UNKNOWN);
221 } else {
222 /* Indicate QUEUE_FULL so that
223 * the scsi midlayer retries.
224 * If the request failed for
225 * remote device reasons, it
226 * gets returned as
227 * SAS_TASK_UNDELIVERED next
228 * time through.
229 */
230 isci_task_refuse(ihost, task,
231 SAS_TASK_COMPLETE,
232 SAS_QUEUE_FULL);
233 }
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700234 }
Dan Williams6f231dd2011-07-02 22:56:22 -0700235 }
236 }
Dan Williams312e0c22011-06-28 13:47:09 -0700237 if (status != SCI_SUCCESS && tag != SCI_CONTROLLER_INVALID_IO_TAG) {
238 spin_lock_irqsave(&ihost->scic_lock, flags);
239 /* command never hit the device, so just free
240 * the tci and skip the sequence increment
241 */
242 isci_tci_free(ihost, ISCI_TAG_TCI(tag));
243 spin_unlock_irqrestore(&ihost->scic_lock, flags);
244 }
Dan Williams209fae12011-06-13 17:39:44 -0700245 isci_put_device(idev);
Dan Williams1077a572011-03-11 10:13:51 -0800246 }
Dan Williams6f231dd2011-07-02 22:56:22 -0700247 return 0;
248}
249
Dan Williams16ba7702011-07-01 10:52:55 -0700250static enum sci_status isci_sata_management_task_request_build(struct isci_request *ireq)
251{
252 struct isci_tmf *isci_tmf;
253 enum sci_status status;
254
Jeff Skirvin3b34c162011-10-27 15:05:22 -0700255 if (!test_bit(IREQ_TMF, &ireq->flags))
Dan Williams16ba7702011-07-01 10:52:55 -0700256 return SCI_FAILURE;
257
258 isci_tmf = isci_request_access_tmf(ireq);
259
260 switch (isci_tmf->tmf_code) {
261
262 case isci_tmf_sata_srst_high:
263 case isci_tmf_sata_srst_low: {
264 struct host_to_dev_fis *fis = &ireq->stp.cmd;
265
266 memset(fis, 0, sizeof(*fis));
267
268 fis->fis_type = 0x27;
269 fis->flags &= ~0x80;
270 fis->flags &= 0xF0;
271 if (isci_tmf->tmf_code == isci_tmf_sata_srst_high)
272 fis->control |= ATA_SRST;
273 else
274 fis->control &= ~ATA_SRST;
275 break;
276 }
277 /* other management commnd go here... */
278 default:
279 return SCI_FAILURE;
280 }
281
282 /* core builds the protocol specific request
283 * based on the h2d fis.
284 */
285 status = sci_task_request_construct_sata(ireq);
286
287 return status;
288}
289
Dan Williams0d0cf142011-06-13 00:51:30 -0700290static struct isci_request *isci_task_request_build(struct isci_host *ihost,
Dan Williams209fae12011-06-13 17:39:44 -0700291 struct isci_remote_device *idev,
Dan Williams312e0c22011-06-28 13:47:09 -0700292 u16 tag, struct isci_tmf *isci_tmf)
Dan Williams6f231dd2011-07-02 22:56:22 -0700293{
Dan Williams6f231dd2011-07-02 22:56:22 -0700294 enum sci_status status = SCI_FAILURE;
Dan Williams0d0cf142011-06-13 00:51:30 -0700295 struct isci_request *ireq = NULL;
Dan Williamsa1a113b2011-04-21 18:44:45 -0700296 struct domain_device *dev;
Dan Williams6f231dd2011-07-02 22:56:22 -0700297
Dan Williams0d0cf142011-06-13 00:51:30 -0700298 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -0700299 "%s: isci_tmf = %p\n", __func__, isci_tmf);
300
Dan Williams0d0cf142011-06-13 00:51:30 -0700301 dev = idev->domain_dev;
Dan Williams6f231dd2011-07-02 22:56:22 -0700302
303 /* do common allocation and init of request object. */
Dan Williamsdb056252011-06-17 14:18:39 -0700304 ireq = isci_tmf_request_from_tag(ihost, isci_tmf, tag);
Dan Williams0d0cf142011-06-13 00:51:30 -0700305 if (!ireq)
306 return NULL;
Dan Williams6f231dd2011-07-02 22:56:22 -0700307
308 /* let the core do it's construct. */
Dan Williams89a73012011-06-30 19:14:33 -0700309 status = sci_task_request_construct(ihost, idev, tag,
Dan Williams5076a1a2011-06-27 14:57:03 -0700310 ireq);
Dan Williams6f231dd2011-07-02 22:56:22 -0700311
312 if (status != SCI_SUCCESS) {
Dan Williams0d0cf142011-06-13 00:51:30 -0700313 dev_warn(&ihost->pdev->dev,
Dan Williams89a73012011-06-30 19:14:33 -0700314 "%s: sci_task_request_construct failed - "
Dan Williams6f231dd2011-07-02 22:56:22 -0700315 "status = 0x%x\n",
316 __func__,
317 status);
Dan Williamsdb056252011-06-17 14:18:39 -0700318 return NULL;
Dan Williams6f231dd2011-07-02 22:56:22 -0700319 }
320
Dan Williamsa1a113b2011-04-21 18:44:45 -0700321 /* XXX convert to get this from task->tproto like other drivers */
322 if (dev->dev_type == SAS_END_DEV) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700323 isci_tmf->proto = SAS_PROTOCOL_SSP;
Dan Williams89a73012011-06-30 19:14:33 -0700324 status = sci_task_request_construct_ssp(ireq);
Dan Williams6f231dd2011-07-02 22:56:22 -0700325 if (status != SCI_SUCCESS)
Dan Williamsdb056252011-06-17 14:18:39 -0700326 return NULL;
Dan Williams6f231dd2011-07-02 22:56:22 -0700327 }
328
Dan Williamsa1a113b2011-04-21 18:44:45 -0700329 if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP)) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700330 isci_tmf->proto = SAS_PROTOCOL_SATA;
Dan Williams0d0cf142011-06-13 00:51:30 -0700331 status = isci_sata_management_task_request_build(ireq);
Dan Williams6f231dd2011-07-02 22:56:22 -0700332
333 if (status != SCI_SUCCESS)
Dan Williamsdb056252011-06-17 14:18:39 -0700334 return NULL;
Dan Williams6f231dd2011-07-02 22:56:22 -0700335 }
Dan Williams0d0cf142011-06-13 00:51:30 -0700336 return ireq;
Dan Williams6f231dd2011-07-02 22:56:22 -0700337}
338
Jeff Skirvinb343dff2011-10-27 15:05:06 -0700339/**
340* isci_request_mark_zombie() - This function must be called with scic_lock held.
341*/
342static void isci_request_mark_zombie(struct isci_host *ihost, struct isci_request *ireq)
343{
344 struct completion *tmf_completion = NULL;
345 struct completion *req_completion;
346
347 /* Set the request state to "dead". */
348 ireq->status = dead;
349
350 req_completion = ireq->io_request_completion;
351 ireq->io_request_completion = NULL;
352
Jeff Skirvin3b34c162011-10-27 15:05:22 -0700353 if (test_bit(IREQ_TMF, &ireq->flags)) {
Jeff Skirvinb343dff2011-10-27 15:05:06 -0700354 /* Break links with the TMF request. */
355 struct isci_tmf *tmf = isci_request_access_tmf(ireq);
356
357 /* In the case where a task request is dying,
358 * the thread waiting on the complete will sit and
359 * timeout unless we wake it now. Since the TMF
360 * has a default error status, complete it here
361 * to wake the waiting thread.
362 */
363 if (tmf) {
364 tmf_completion = tmf->complete;
365 tmf->complete = NULL;
366 }
367 ireq->ttype_ptr.tmf_task_ptr = NULL;
368 dev_dbg(&ihost->pdev->dev, "%s: tmf_code %d, managed tag %#x\n",
369 __func__, tmf->tmf_code, tmf->io_tag);
Jeff Skirvin3b34c162011-10-27 15:05:22 -0700370 } else {
371 /* Break links with the sas_task - the callback is done
372 * elsewhere.
373 */
374 struct sas_task *task = isci_request_access_task(ireq);
375
376 if (task)
377 task->lldd_task = NULL;
378
379 ireq->ttype_ptr.io_task_ptr = NULL;
Jeff Skirvinb343dff2011-10-27 15:05:06 -0700380 }
381
382 dev_warn(&ihost->pdev->dev, "task context unrecoverable (tag: %#x)\n",
383 ireq->io_tag);
384
385 /* Don't force waiting threads to timeout. */
386 if (req_completion)
387 complete(req_completion);
388
389 if (tmf_completion != NULL)
390 complete(tmf_completion);
391}
392
Dan Williams16ba7702011-07-01 10:52:55 -0700393static int isci_task_execute_tmf(struct isci_host *ihost,
394 struct isci_remote_device *idev,
395 struct isci_tmf *tmf, unsigned long timeout_ms)
Dan Williams6f231dd2011-07-02 22:56:22 -0700396{
397 DECLARE_COMPLETION_ONSTACK(completion);
Bartosz Barcinski467e8552011-04-12 17:28:41 -0700398 enum sci_task_status status = SCI_TASK_FAILURE;
Dan Williams0d0cf142011-06-13 00:51:30 -0700399 struct isci_request *ireq;
Dan Williams6f231dd2011-07-02 22:56:22 -0700400 int ret = TMF_RESP_FUNC_FAILED;
401 unsigned long flags;
Edmund Nadolskifd183882011-05-19 12:00:15 +0000402 unsigned long timeleft;
Dan Williams312e0c22011-06-28 13:47:09 -0700403 u16 tag;
404
405 spin_lock_irqsave(&ihost->scic_lock, flags);
406 tag = isci_alloc_tag(ihost);
407 spin_unlock_irqrestore(&ihost->scic_lock, flags);
408
409 if (tag == SCI_CONTROLLER_INVALID_IO_TAG)
410 return ret;
Dan Williams6f231dd2011-07-02 22:56:22 -0700411
412 /* sanity check, return TMF_RESP_FUNC_FAILED
413 * if the device is not there and ready.
414 */
Dan Williams78a6f062011-06-30 16:31:37 -0700415 if (!idev ||
416 (!test_bit(IDEV_IO_READY, &idev->flags) &&
417 !test_bit(IDEV_IO_NCQERROR, &idev->flags))) {
Dan Williams0d0cf142011-06-13 00:51:30 -0700418 dev_dbg(&ihost->pdev->dev,
Dan Williams78a6f062011-06-30 16:31:37 -0700419 "%s: idev = %p not ready (%#lx)\n",
Dan Williams6f231dd2011-07-02 22:56:22 -0700420 __func__,
Dan Williams78a6f062011-06-30 16:31:37 -0700421 idev, idev ? idev->flags : 0);
Dan Williams312e0c22011-06-28 13:47:09 -0700422 goto err_tci;
Dan Williams6f231dd2011-07-02 22:56:22 -0700423 } else
Dan Williams0d0cf142011-06-13 00:51:30 -0700424 dev_dbg(&ihost->pdev->dev,
Dan Williams78a6f062011-06-30 16:31:37 -0700425 "%s: idev = %p\n",
426 __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -0700427
428 /* Assign the pointer to the TMF's completion kernel wait structure. */
429 tmf->complete = &completion;
Jeff Skirvinb343dff2011-10-27 15:05:06 -0700430 tmf->status = SCI_FAILURE_TIMEOUT;
Dan Williams6f231dd2011-07-02 22:56:22 -0700431
Dan Williams78a6f062011-06-30 16:31:37 -0700432 ireq = isci_task_request_build(ihost, idev, tag, tmf);
Dan Williams312e0c22011-06-28 13:47:09 -0700433 if (!ireq)
434 goto err_tci;
Dan Williams6f231dd2011-07-02 22:56:22 -0700435
Dan Williams0d0cf142011-06-13 00:51:30 -0700436 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700437
438 /* start the TMF io. */
Dan Williams89a73012011-06-30 19:14:33 -0700439 status = sci_controller_start_task(ihost, idev, ireq);
Dan Williams6f231dd2011-07-02 22:56:22 -0700440
Bartosz Barcinski467e8552011-04-12 17:28:41 -0700441 if (status != SCI_TASK_SUCCESS) {
Dan Williamsa8a0a132011-07-01 12:07:25 -0700442 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -0700443 "%s: start_io failed - status = 0x%x, request = %p\n",
444 __func__,
445 status,
Dan Williams0d0cf142011-06-13 00:51:30 -0700446 ireq);
447 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williamsdb056252011-06-17 14:18:39 -0700448 goto err_tci;
Dan Williams6f231dd2011-07-02 22:56:22 -0700449 }
450
Dan Williams6f231dd2011-07-02 22:56:22 -0700451 if (tmf->cb_state_func != NULL)
452 tmf->cb_state_func(isci_tmf_started, tmf, tmf->cb_data);
453
Dan Williams0d0cf142011-06-13 00:51:30 -0700454 isci_request_change_state(ireq, started);
Dan Williams6f231dd2011-07-02 22:56:22 -0700455
456 /* add the request to the remote device request list. */
Dan Williams78a6f062011-06-30 16:31:37 -0700457 list_add(&ireq->dev_node, &idev->reqs_in_process);
Dan Williams6f231dd2011-07-02 22:56:22 -0700458
Dan Williams0d0cf142011-06-13 00:51:30 -0700459 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700460
461 /* Wait for the TMF to complete, or a timeout. */
Edmund Nadolskifd183882011-05-19 12:00:15 +0000462 timeleft = wait_for_completion_timeout(&completion,
Dan Williams086a0da2011-06-21 16:23:03 -0700463 msecs_to_jiffies(timeout_ms));
Edmund Nadolskifd183882011-05-19 12:00:15 +0000464
465 if (timeleft == 0) {
Jeff Skirvinb343dff2011-10-27 15:05:06 -0700466 /* The TMF did not complete - this could be because
467 * of an unplug. Terminate the TMF request now.
468 */
Dan Williams0d0cf142011-06-13 00:51:30 -0700469 spin_lock_irqsave(&ihost->scic_lock, flags);
Edmund Nadolskifd183882011-05-19 12:00:15 +0000470
471 if (tmf->cb_state_func != NULL)
Jeff Skirvinb343dff2011-10-27 15:05:06 -0700472 tmf->cb_state_func(isci_tmf_timed_out, tmf,
473 tmf->cb_data);
Edmund Nadolskifd183882011-05-19 12:00:15 +0000474
Jeff Skirvinb343dff2011-10-27 15:05:06 -0700475 sci_controller_terminate_request(ihost, idev, ireq);
Edmund Nadolskifd183882011-05-19 12:00:15 +0000476
Dan Williams0d0cf142011-06-13 00:51:30 -0700477 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams086a0da2011-06-21 16:23:03 -0700478
Jeff Skirvinb343dff2011-10-27 15:05:06 -0700479 timeleft = wait_for_completion_timeout(
480 &completion,
481 msecs_to_jiffies(ISCI_TERMINATION_TIMEOUT_MSEC));
482
483 if (!timeleft) {
484 /* Strange condition - the termination of the TMF
485 * request timed-out.
486 */
487 spin_lock_irqsave(&ihost->scic_lock, flags);
488
489 /* If the TMF status has not changed, kill it. */
490 if (tmf->status == SCI_FAILURE_TIMEOUT)
491 isci_request_mark_zombie(ihost, ireq);
492
493 spin_unlock_irqrestore(&ihost->scic_lock, flags);
494 }
Edmund Nadolskifd183882011-05-19 12:00:15 +0000495 }
Dan Williams6f231dd2011-07-02 22:56:22 -0700496
Maciej Trela27234ab2012-01-04 01:33:05 -0800497 isci_print_tmf(ihost, tmf);
Dan Williams6f231dd2011-07-02 22:56:22 -0700498
499 if (tmf->status == SCI_SUCCESS)
500 ret = TMF_RESP_FUNC_COMPLETE;
501 else if (tmf->status == SCI_FAILURE_IO_RESPONSE_VALID) {
Dan Williams0d0cf142011-06-13 00:51:30 -0700502 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -0700503 "%s: tmf.status == "
504 "SCI_FAILURE_IO_RESPONSE_VALID\n",
505 __func__);
506 ret = TMF_RESP_FUNC_COMPLETE;
507 }
508 /* Else - leave the default "failed" status alone. */
509
Dan Williams0d0cf142011-06-13 00:51:30 -0700510 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -0700511 "%s: completed request = %p\n",
512 __func__,
Dan Williams0d0cf142011-06-13 00:51:30 -0700513 ireq);
Dan Williams6f231dd2011-07-02 22:56:22 -0700514
Dan Williams6f231dd2011-07-02 22:56:22 -0700515 return ret;
Dan Williams312e0c22011-06-28 13:47:09 -0700516
Dan Williams312e0c22011-06-28 13:47:09 -0700517 err_tci:
518 spin_lock_irqsave(&ihost->scic_lock, flags);
519 isci_tci_free(ihost, ISCI_TAG_TCI(tag));
520 spin_unlock_irqrestore(&ihost->scic_lock, flags);
521
522 return ret;
Dan Williams6f231dd2011-07-02 22:56:22 -0700523}
524
Dan Williams16ba7702011-07-01 10:52:55 -0700525static void isci_task_build_tmf(struct isci_tmf *tmf,
526 enum isci_tmf_function_codes code,
527 void (*tmf_sent_cb)(enum isci_tmf_cb_state,
528 struct isci_tmf *,
529 void *),
530 void *cb_data)
Dan Williams6f231dd2011-07-02 22:56:22 -0700531{
Dan Williams6f231dd2011-07-02 22:56:22 -0700532 memset(tmf, 0, sizeof(*tmf));
533
Dan Williams6f231dd2011-07-02 22:56:22 -0700534 tmf->tmf_code = code;
Dan Williams6f231dd2011-07-02 22:56:22 -0700535 tmf->cb_state_func = tmf_sent_cb;
Jeff Skirvinc3f42fe2011-03-04 14:06:56 -0800536 tmf->cb_data = cb_data;
537}
Jeff Skirvin1fad9e92011-03-04 14:06:46 -0800538
Dan Williams16ba7702011-07-01 10:52:55 -0700539static void isci_task_build_abort_task_tmf(struct isci_tmf *tmf,
540 enum isci_tmf_function_codes code,
541 void (*tmf_sent_cb)(enum isci_tmf_cb_state,
542 struct isci_tmf *,
543 void *),
544 struct isci_request *old_request)
Jeff Skirvinc3f42fe2011-03-04 14:06:56 -0800545{
Dan Williams16ba7702011-07-01 10:52:55 -0700546 isci_task_build_tmf(tmf, code, tmf_sent_cb, old_request);
Jeff Skirvinc3f42fe2011-03-04 14:06:56 -0800547 tmf->io_tag = old_request->io_tag;
Dan Williams6f231dd2011-07-02 22:56:22 -0700548}
549
Dan Williams6f231dd2011-07-02 22:56:22 -0700550/**
551 * isci_task_validate_request_to_abort() - This function checks the given I/O
552 * against the "started" state. If the request is still "started", it's
553 * state is changed to aborted. NOTE: isci_host->scic_lock MUST BE HELD
554 * BEFORE CALLING THIS FUNCTION.
555 * @isci_request: This parameter specifies the request object to control.
556 * @isci_host: This parameter specifies the ISCI host object
557 * @isci_device: This is the device to which the request is pending.
558 * @aborted_io_completion: This is a completion structure that will be added to
559 * the request in case it is changed to aborting; this completion is
560 * triggered when the request is fully completed.
561 *
562 * Either "started" on successful change of the task status to "aborted", or
563 * "unallocated" if the task cannot be controlled.
564 */
565static enum isci_request_status isci_task_validate_request_to_abort(
566 struct isci_request *isci_request,
567 struct isci_host *isci_host,
568 struct isci_remote_device *isci_device,
569 struct completion *aborted_io_completion)
570{
571 enum isci_request_status old_state = unallocated;
572
573 /* Only abort the task if it's in the
574 * device's request_in_process list
575 */
576 if (isci_request && !list_empty(&isci_request->dev_node)) {
577 old_state = isci_request_change_started_to_aborted(
578 isci_request, aborted_io_completion);
579
Dan Williams6f231dd2011-07-02 22:56:22 -0700580 }
581
582 return old_state;
583}
584
Jeff Skirvind6891682011-10-27 15:05:01 -0700585static int isci_request_is_dealloc_managed(enum isci_request_status stat)
586{
587 switch (stat) {
588 case aborted:
589 case aborting:
590 case terminating:
591 case completed:
592 case dead:
593 return true;
594 default:
595 return false;
596 }
597}
598
Dan Williams6f231dd2011-07-02 22:56:22 -0700599/**
600 * isci_terminate_request_core() - This function will terminate the given
601 * request, and wait for it to complete. This function must only be called
602 * from a thread that can wait. Note that the request is terminated and
603 * completed (back to the host, if started there).
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700604 * @ihost: This SCU.
Dan Williams78a6f062011-06-30 16:31:37 -0700605 * @idev: The target.
Dan Williams6f231dd2011-07-02 22:56:22 -0700606 * @isci_request: The I/O request to be terminated.
607 *
Dan Williams6f231dd2011-07-02 22:56:22 -0700608 */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700609static void isci_terminate_request_core(struct isci_host *ihost,
610 struct isci_remote_device *idev,
611 struct isci_request *isci_request)
Dan Williams6f231dd2011-07-02 22:56:22 -0700612{
Jeff Skirvincbb65c62011-03-04 14:06:50 -0800613 enum sci_status status = SCI_SUCCESS;
Dan Williams6f231dd2011-07-02 22:56:22 -0700614 bool was_terminated = false;
615 bool needs_cleanup_handling = false;
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700616 unsigned long flags;
617 unsigned long termination_completed = 1;
618 struct completion *io_request_completion;
Dan Williams6f231dd2011-07-02 22:56:22 -0700619
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700620 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -0700621 "%s: device = %p; request = %p\n",
Dan Williams78a6f062011-06-30 16:31:37 -0700622 __func__, idev, isci_request);
Dan Williams6f231dd2011-07-02 22:56:22 -0700623
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700624 spin_lock_irqsave(&ihost->scic_lock, flags);
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800625
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700626 io_request_completion = isci_request->io_request_completion;
627
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800628 /* Note that we are not going to control
Dan Williams38d88792011-06-23 14:33:48 -0700629 * the target to abort the request.
630 */
631 set_bit(IREQ_COMPLETE_IN_TARGET, &isci_request->flags);
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800632
Dan Williams6f231dd2011-07-02 22:56:22 -0700633 /* Make sure the request wasn't just sitting around signalling
634 * device condition (if the request handle is NULL, then the
635 * request completed but needed additional handling here).
636 */
Dan Williams38d88792011-06-23 14:33:48 -0700637 if (!test_bit(IREQ_TERMINATED, &isci_request->flags)) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700638 was_terminated = true;
Jeff Skirvincbb65c62011-03-04 14:06:50 -0800639 needs_cleanup_handling = true;
Dan Williams89a73012011-06-30 19:14:33 -0700640 status = sci_controller_terminate_request(ihost,
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700641 idev,
642 isci_request);
Dan Williams6f231dd2011-07-02 22:56:22 -0700643 }
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700644 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700645
646 /*
647 * The only time the request to terminate will
648 * fail is when the io request is completed and
649 * being aborted.
650 */
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800651 if (status != SCI_SUCCESS) {
Dan Williamsa8a0a132011-07-01 12:07:25 -0700652 dev_dbg(&ihost->pdev->dev,
Dan Williams89a73012011-06-30 19:14:33 -0700653 "%s: sci_controller_terminate_request"
Dan Williams6f231dd2011-07-02 22:56:22 -0700654 " returned = 0x%x\n",
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700655 __func__, status);
656
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800657 isci_request->io_request_completion = NULL;
658
659 } else {
Dan Williams6f231dd2011-07-02 22:56:22 -0700660 if (was_terminated) {
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700661 dev_dbg(&ihost->pdev->dev,
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700662 "%s: before completion wait (%p/%p)\n",
663 __func__, isci_request, io_request_completion);
Dan Williams6f231dd2011-07-02 22:56:22 -0700664
665 /* Wait here for the request to complete. */
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700666 termination_completed
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800667 = wait_for_completion_timeout(
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700668 io_request_completion,
Jeff Skirvinb343dff2011-10-27 15:05:06 -0700669 msecs_to_jiffies(ISCI_TERMINATION_TIMEOUT_MSEC));
Dan Williams6f231dd2011-07-02 22:56:22 -0700670
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700671 if (!termination_completed) {
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800672
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700673 /* The request to terminate has timed out. */
Jeff Skirvinb343dff2011-10-27 15:05:06 -0700674 spin_lock_irqsave(&ihost->scic_lock, flags);
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800675
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700676 /* Check for state changes. */
Jeff Skirvinb343dff2011-10-27 15:05:06 -0700677 if (!test_bit(IREQ_TERMINATED,
678 &isci_request->flags)) {
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800679
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700680 /* The best we can do is to have the
681 * request die a silent death if it
682 * ever really completes.
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700683 */
Jeff Skirvinb343dff2011-10-27 15:05:06 -0700684 isci_request_mark_zombie(ihost,
685 isci_request);
686 needs_cleanup_handling = true;
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700687 } else
688 termination_completed = 1;
689
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700690 spin_unlock_irqrestore(&ihost->scic_lock,
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700691 flags);
692
693 if (!termination_completed) {
694
Dan Williamsa8a0a132011-07-01 12:07:25 -0700695 dev_dbg(&ihost->pdev->dev,
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700696 "%s: *** Timeout waiting for "
697 "termination(%p/%p)\n",
698 __func__, io_request_completion,
699 isci_request);
700
701 /* The request can no longer be referenced
702 * safely since it may go away if the
703 * termination every really does complete.
704 */
705 isci_request = NULL;
706 }
707 }
708 if (termination_completed)
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700709 dev_dbg(&ihost->pdev->dev,
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700710 "%s: after completion wait (%p/%p)\n",
711 __func__, isci_request, io_request_completion);
Dan Williams6f231dd2011-07-02 22:56:22 -0700712 }
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800713
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700714 if (termination_completed) {
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800715
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700716 isci_request->io_request_completion = NULL;
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800717
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700718 /* Peek at the status of the request. This will tell
719 * us if there was special handling on the request such that it
720 * needs to be detached and freed here.
721 */
722 spin_lock_irqsave(&isci_request->state_lock, flags);
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700723
Jeff Skirvind6891682011-10-27 15:05:01 -0700724 needs_cleanup_handling
725 = isci_request_is_dealloc_managed(
726 isci_request->status);
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700727
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700728 spin_unlock_irqrestore(&isci_request->state_lock, flags);
729
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800730 }
Jeff Skirvindb49c2d2011-10-27 15:05:11 -0700731 if (needs_cleanup_handling) {
732
733 dev_dbg(&ihost->pdev->dev,
734 "%s: cleanup isci_device=%p, request=%p\n",
735 __func__, idev, isci_request);
736
737 if (isci_request != NULL) {
738 spin_lock_irqsave(&ihost->scic_lock, flags);
739 isci_free_tag(ihost, isci_request->io_tag);
740 isci_request_change_state(isci_request, unallocated);
741 list_del_init(&isci_request->dev_node);
742 spin_unlock_irqrestore(&ihost->scic_lock, flags);
743 }
744 }
Jeff Skirvina5fde222011-03-04 14:06:42 -0800745 }
Dan Williams6f231dd2011-07-02 22:56:22 -0700746}
747
748/**
749 * isci_terminate_pending_requests() - This function will change the all of the
750 * requests on the given device's state to "aborting", will terminate the
751 * requests, and wait for them to complete. This function must only be
752 * called from a thread that can wait. Note that the requests are all
753 * terminated and completed (back to the host, if started there).
754 * @isci_host: This parameter specifies SCU.
Dan Williams78a6f062011-06-30 16:31:37 -0700755 * @idev: This parameter specifies the target.
Dan Williams6f231dd2011-07-02 22:56:22 -0700756 *
Dan Williams6f231dd2011-07-02 22:56:22 -0700757 */
Dan Williams980d3ae2011-06-20 15:11:22 -0700758void isci_terminate_pending_requests(struct isci_host *ihost,
759 struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -0700760{
Dan Williams980d3ae2011-06-20 15:11:22 -0700761 struct completion request_completion;
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700762 enum isci_request_status old_state;
Dan Williams980d3ae2011-06-20 15:11:22 -0700763 unsigned long flags;
764 LIST_HEAD(list);
Dan Williams6f231dd2011-07-02 22:56:22 -0700765
Dan Williams980d3ae2011-06-20 15:11:22 -0700766 spin_lock_irqsave(&ihost->scic_lock, flags);
767 list_splice_init(&idev->reqs_in_process, &list);
Dan Williams6f231dd2011-07-02 22:56:22 -0700768
Dan Williams980d3ae2011-06-20 15:11:22 -0700769 /* assumes that isci_terminate_request_core deletes from the list */
770 while (!list_empty(&list)) {
771 struct isci_request *ireq = list_entry(list.next, typeof(*ireq), dev_node);
Dan Williams6f231dd2011-07-02 22:56:22 -0700772
Dan Williams980d3ae2011-06-20 15:11:22 -0700773 /* Change state to "terminating" if it is currently
774 * "started".
775 */
776 old_state = isci_request_change_started_to_newstate(ireq,
777 &request_completion,
778 terminating);
779 switch (old_state) {
780 case started:
781 case completed:
782 case aborting:
783 break;
784 default:
785 /* termination in progress, or otherwise dispositioned.
786 * We know the request was on 'list' so should be safe
787 * to move it back to reqs_in_process
788 */
789 list_move(&ireq->dev_node, &idev->reqs_in_process);
790 ireq = NULL;
791 break;
792 }
793
794 if (!ireq)
795 continue;
796 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800797
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700798 init_completion(&request_completion);
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800799
Dan Williams980d3ae2011-06-20 15:11:22 -0700800 dev_dbg(&ihost->pdev->dev,
801 "%s: idev=%p request=%p; task=%p old_state=%d\n",
802 __func__, idev, ireq,
Jeff Skirvin3b34c162011-10-27 15:05:22 -0700803 (!test_bit(IREQ_TMF, &ireq->flags)
804 ? isci_request_access_task(ireq)
805 : NULL),
Dan Williams980d3ae2011-06-20 15:11:22 -0700806 old_state);
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700807
Dan Williams980d3ae2011-06-20 15:11:22 -0700808 /* If the old_state is started:
809 * This request was not already being aborted. If it had been,
810 * then the aborting I/O (ie. the TMF request) would not be in
811 * the aborting state, and thus would be terminated here. Note
812 * that since the TMF completion's call to the kernel function
813 * "complete()" does not happen until the pending I/O request
814 * terminate fully completes, we do not have to implement a
815 * special wait here for already aborting requests - the
816 * termination of the TMF request will force the request
817 * to finish it's already started terminate.
818 *
819 * If old_state == completed:
820 * This request completed from the SCU hardware perspective
821 * and now just needs cleaning up in terms of freeing the
822 * request and potentially calling up to libsas.
823 *
824 * If old_state == aborting:
825 * This request has already gone through a TMF timeout, but may
826 * not have been terminated; needs cleaning up at least.
827 */
828 isci_terminate_request_core(ihost, idev, ireq);
829 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700830 }
Dan Williams980d3ae2011-06-20 15:11:22 -0700831 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700832}
833
834/**
835 * isci_task_send_lu_reset_sas() - This function is called by of the SAS Domain
836 * Template functions.
837 * @lun: This parameter specifies the lun to be reset.
838 *
839 * status, zero indicates success.
840 */
841static int isci_task_send_lu_reset_sas(
842 struct isci_host *isci_host,
843 struct isci_remote_device *isci_device,
844 u8 *lun)
845{
846 struct isci_tmf tmf;
847 int ret = TMF_RESP_FUNC_FAILED;
848
849 dev_dbg(&isci_host->pdev->dev,
850 "%s: isci_host = %p, isci_device = %p\n",
851 __func__, isci_host, isci_device);
852 /* Send the LUN reset to the target. By the time the call returns,
853 * the TMF has fully exected in the target (in which case the return
854 * value is "TMF_RESP_FUNC_COMPLETE", or the request timed-out (or
855 * was otherwise unable to be executed ("TMF_RESP_FUNC_FAILED").
856 */
Dan Williams209fae12011-06-13 17:39:44 -0700857 isci_task_build_tmf(&tmf, isci_tmf_ssp_lun_reset, NULL, NULL);
Dan Williams6f231dd2011-07-02 22:56:22 -0700858
859 #define ISCI_LU_RESET_TIMEOUT_MS 2000 /* 2 second timeout. */
Dan Williams209fae12011-06-13 17:39:44 -0700860 ret = isci_task_execute_tmf(isci_host, isci_device, &tmf, ISCI_LU_RESET_TIMEOUT_MS);
Dan Williams6f231dd2011-07-02 22:56:22 -0700861
862 if (ret == TMF_RESP_FUNC_COMPLETE)
863 dev_dbg(&isci_host->pdev->dev,
864 "%s: %p: TMF_LU_RESET passed\n",
865 __func__, isci_device);
866 else
867 dev_dbg(&isci_host->pdev->dev,
868 "%s: %p: TMF_LU_RESET failed (%x)\n",
869 __func__, isci_device, ret);
870
871 return ret;
872}
873
Dan Williams16ba7702011-07-01 10:52:55 -0700874static int isci_task_send_lu_reset_sata(struct isci_host *ihost,
875 struct isci_remote_device *idev, u8 *lun)
876{
877 int ret = TMF_RESP_FUNC_FAILED;
878 struct isci_tmf tmf;
879
880 /* Send the soft reset to the target */
881 #define ISCI_SRST_TIMEOUT_MS 25000 /* 25 second timeout. */
882 isci_task_build_tmf(&tmf, isci_tmf_sata_srst_high, NULL, NULL);
883
884 ret = isci_task_execute_tmf(ihost, idev, &tmf, ISCI_SRST_TIMEOUT_MS);
885
886 if (ret != TMF_RESP_FUNC_COMPLETE) {
Dan Williamsa8a0a132011-07-01 12:07:25 -0700887 dev_dbg(&ihost->pdev->dev,
Dan Williams16ba7702011-07-01 10:52:55 -0700888 "%s: Assert SRST failed (%p) = %x",
889 __func__, idev, ret);
890
891 /* Return the failure so that the LUN reset is escalated
892 * to a target reset.
893 */
894 }
895 return ret;
896}
897
Dan Williams6f231dd2011-07-02 22:56:22 -0700898/**
899 * isci_task_lu_reset() - This function is one of the SAS Domain Template
900 * functions. This is one of the Task Management functoins called by libsas,
901 * to reset the given lun. Note the assumption that while this call is
902 * executing, no I/O will be sent by the host to the device.
903 * @lun: This parameter specifies the lun to be reset.
904 *
905 * status, zero indicates success.
906 */
Dan Williams4393aa42011-03-31 13:10:44 -0700907int isci_task_lu_reset(struct domain_device *domain_device, u8 *lun)
Dan Williams6f231dd2011-07-02 22:56:22 -0700908{
Dan Williams4393aa42011-03-31 13:10:44 -0700909 struct isci_host *isci_host = dev_to_ihost(domain_device);
Dan Williams209fae12011-06-13 17:39:44 -0700910 struct isci_remote_device *isci_device;
911 unsigned long flags;
Dan Williams6f231dd2011-07-02 22:56:22 -0700912 int ret;
Dan Williams6f231dd2011-07-02 22:56:22 -0700913
Dan Williams209fae12011-06-13 17:39:44 -0700914 spin_lock_irqsave(&isci_host->scic_lock, flags);
915 isci_device = isci_lookup_device(domain_device);
916 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700917
Dan Williams4393aa42011-03-31 13:10:44 -0700918 dev_dbg(&isci_host->pdev->dev,
919 "%s: domain_device=%p, isci_host=%p; isci_device=%p\n",
Dan Williams6f231dd2011-07-02 22:56:22 -0700920 __func__, domain_device, isci_host, isci_device);
921
Jeff Skirvin98145cb2011-10-27 15:05:16 -0700922 if (!isci_device) {
923 /* If the device is gone, stop the escalations. */
924 dev_dbg(&isci_host->pdev->dev, "%s: No dev\n", __func__);
Dan Williams6f231dd2011-07-02 22:56:22 -0700925
Jeff Skirvin98145cb2011-10-27 15:05:16 -0700926 ret = TMF_RESP_FUNC_COMPLETE;
Dan Williams209fae12011-06-13 17:39:44 -0700927 goto out;
Dan Williams6f231dd2011-07-02 22:56:22 -0700928 }
Jeff Skirvin98145cb2011-10-27 15:05:16 -0700929 set_bit(IDEV_EH, &isci_device->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700930
Dan Williams6f231dd2011-07-02 22:56:22 -0700931 /* Send the task management part of the reset. */
932 if (sas_protocol_ata(domain_device->tproto)) {
Dan Williams4393aa42011-03-31 13:10:44 -0700933 ret = isci_task_send_lu_reset_sata(isci_host, isci_device, lun);
Dan Williams6f231dd2011-07-02 22:56:22 -0700934 } else
935 ret = isci_task_send_lu_reset_sas(isci_host, isci_device, lun);
936
937 /* If the LUN reset worked, all the I/O can now be terminated. */
938 if (ret == TMF_RESP_FUNC_COMPLETE)
939 /* Terminate all I/O now. */
940 isci_terminate_pending_requests(isci_host,
Dan Williams980d3ae2011-06-20 15:11:22 -0700941 isci_device);
Dan Williams6f231dd2011-07-02 22:56:22 -0700942
Dan Williams209fae12011-06-13 17:39:44 -0700943 out:
944 isci_put_device(isci_device);
Dan Williams6f231dd2011-07-02 22:56:22 -0700945 return ret;
946}
947
948
949/* int (*lldd_clear_nexus_port)(struct asd_sas_port *); */
950int isci_task_clear_nexus_port(struct asd_sas_port *port)
951{
952 return TMF_RESP_FUNC_FAILED;
953}
954
955
956
957int isci_task_clear_nexus_ha(struct sas_ha_struct *ha)
958{
959 return TMF_RESP_FUNC_FAILED;
960}
961
Dan Williams6f231dd2011-07-02 22:56:22 -0700962/* Task Management Functions. Must be called from process context. */
963
964/**
965 * isci_abort_task_process_cb() - This is a helper function for the abort task
966 * TMF command. It manages the request state with respect to the successful
967 * transmission / completion of the abort task request.
968 * @cb_state: This parameter specifies when this function was called - after
969 * the TMF request has been started and after it has timed-out.
970 * @tmf: This parameter specifies the TMF in progress.
971 *
972 *
973 */
974static void isci_abort_task_process_cb(
975 enum isci_tmf_cb_state cb_state,
976 struct isci_tmf *tmf,
977 void *cb_data)
978{
979 struct isci_request *old_request;
980
981 old_request = (struct isci_request *)cb_data;
982
983 dev_dbg(&old_request->isci_host->pdev->dev,
984 "%s: tmf=%p, old_request=%p\n",
985 __func__, tmf, old_request);
986
987 switch (cb_state) {
988
989 case isci_tmf_started:
990 /* The TMF has been started. Nothing to do here, since the
991 * request state was already set to "aborted" by the abort
992 * task function.
993 */
Bartosz Barcinski6cb4d6b2011-04-12 17:28:43 -0700994 if ((old_request->status != aborted)
995 && (old_request->status != completed))
Dan Williamsa8a0a132011-07-01 12:07:25 -0700996 dev_dbg(&old_request->isci_host->pdev->dev,
Bartosz Barcinski6cb4d6b2011-04-12 17:28:43 -0700997 "%s: Bad request status (%d): tmf=%p, old_request=%p\n",
998 __func__, old_request->status, tmf, old_request);
Dan Williams6f231dd2011-07-02 22:56:22 -0700999 break;
1000
1001 case isci_tmf_timed_out:
1002
1003 /* Set the task's state to "aborting", since the abort task
1004 * function thread set it to "aborted" (above) in anticipation
1005 * of the task management request working correctly. Since the
1006 * timeout has now fired, the TMF request failed. We set the
1007 * state such that the request completion will indicate the
1008 * device is no longer present.
1009 */
1010 isci_request_change_state(old_request, aborting);
1011 break;
1012
1013 default:
Dan Williamsa8a0a132011-07-01 12:07:25 -07001014 dev_dbg(&old_request->isci_host->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -07001015 "%s: Bad cb_state (%d): tmf=%p, old_request=%p\n",
1016 __func__, cb_state, tmf, old_request);
1017 break;
1018 }
1019}
1020
1021/**
1022 * isci_task_abort_task() - This function is one of the SAS Domain Template
1023 * functions. This function is called by libsas to abort a specified task.
1024 * @task: This parameter specifies the SAS task to abort.
1025 *
1026 * status, zero indicates success.
1027 */
1028int isci_task_abort_task(struct sas_task *task)
1029{
Dan Williams4393aa42011-03-31 13:10:44 -07001030 struct isci_host *isci_host = dev_to_ihost(task->dev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001031 DECLARE_COMPLETION_ONSTACK(aborted_io_completion);
Jeff Skirvina5fde222011-03-04 14:06:42 -08001032 struct isci_request *old_request = NULL;
1033 enum isci_request_status old_state;
Dan Williams6f231dd2011-07-02 22:56:22 -07001034 struct isci_remote_device *isci_device = NULL;
Jeff Skirvina5fde222011-03-04 14:06:42 -08001035 struct isci_tmf tmf;
1036 int ret = TMF_RESP_FUNC_FAILED;
1037 unsigned long flags;
Jeff Skirvin98145cb2011-10-27 15:05:16 -07001038 int perform_termination = 0;
Dan Williams6f231dd2011-07-02 22:56:22 -07001039
1040 /* Get the isci_request reference from the task. Note that
1041 * this check does not depend on the pending request list
1042 * in the device, because tasks driving resets may land here
1043 * after completion in the core.
1044 */
Dan Williams209fae12011-06-13 17:39:44 -07001045 spin_lock_irqsave(&isci_host->scic_lock, flags);
1046 spin_lock(&task->task_state_lock);
1047
1048 old_request = task->lldd_task;
1049
1050 /* If task is already done, the request isn't valid */
1051 if (!(task->task_state_flags & SAS_TASK_STATE_DONE) &&
1052 (task->task_state_flags & SAS_TASK_AT_INITIATOR) &&
1053 old_request)
1054 isci_device = isci_lookup_device(task->dev);
1055
1056 spin_unlock(&task->task_state_lock);
1057 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001058
1059 dev_dbg(&isci_host->pdev->dev,
Jeff Skirvin98145cb2011-10-27 15:05:16 -07001060 "%s: dev = %p, task = %p, old_request == %p\n",
1061 __func__, isci_device, task, old_request);
Dan Williams6f231dd2011-07-02 22:56:22 -07001062
Jeff Skirvin98145cb2011-10-27 15:05:16 -07001063 if (isci_device)
1064 set_bit(IDEV_EH, &isci_device->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001065
Jeff Skirvin98145cb2011-10-27 15:05:16 -07001066 /* Device reset conditions signalled in task_state_flags are the
1067 * responsbility of libsas to observe at the start of the error
1068 * handler thread.
Dan Williams6f231dd2011-07-02 22:56:22 -07001069 */
Jeff Skirvin98145cb2011-10-27 15:05:16 -07001070 if (!isci_device || !old_request) {
1071 /* The request has already completed and there
1072 * is nothing to do here other than to set the task
1073 * done bit, and indicate that the task abort function
1074 * was sucessful.
1075 */
1076 spin_lock_irqsave(&task->task_state_lock, flags);
1077 task->task_state_flags |= SAS_TASK_STATE_DONE;
1078 task->task_state_flags &= ~(SAS_TASK_AT_INITIATOR |
1079 SAS_TASK_STATE_PENDING);
Jeff Skirvina5fde222011-03-04 14:06:42 -08001080 spin_unlock_irqrestore(&task->task_state_lock, flags);
Jeff Skirvin98145cb2011-10-27 15:05:16 -07001081
1082 ret = TMF_RESP_FUNC_COMPLETE;
1083
1084 dev_dbg(&isci_host->pdev->dev,
1085 "%s: abort task not needed for %p\n",
1086 __func__, task);
1087 goto out;
James Bottomleya5ec7f862011-07-03 14:14:45 -05001088 }
Dan Williams6f231dd2011-07-02 22:56:22 -07001089
1090 spin_lock_irqsave(&isci_host->scic_lock, flags);
1091
Jeff Skirvinf219f012011-03-31 13:10:34 -07001092 /* Check the request status and change to "aborted" if currently
Jeff Skirvina5fde222011-03-04 14:06:42 -08001093 * "starting"; if true then set the I/O kernel completion
Dan Williams6f231dd2011-07-02 22:56:22 -07001094 * struct that will be triggered when the request completes.
1095 */
Jeff Skirvina5fde222011-03-04 14:06:42 -08001096 old_state = isci_task_validate_request_to_abort(
1097 old_request, isci_host, isci_device,
1098 &aborted_io_completion);
Jeff Skirvinf219f012011-03-31 13:10:34 -07001099 if ((old_state != started) &&
1100 (old_state != completed) &&
1101 (old_state != aborting)) {
Dan Williams6f231dd2011-07-02 22:56:22 -07001102
1103 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1104
Jeff Skirvina5fde222011-03-04 14:06:42 -08001105 /* The request was already being handled by someone else (because
1106 * they got to set the state away from started).
1107 */
1108 dev_dbg(&isci_host->pdev->dev,
1109 "%s: device = %p; old_request %p already being aborted\n",
1110 __func__,
1111 isci_device, old_request);
Dan Williams209fae12011-06-13 17:39:44 -07001112 ret = TMF_RESP_FUNC_COMPLETE;
1113 goto out;
Dan Williams6f231dd2011-07-02 22:56:22 -07001114 }
Dan Williams38d88792011-06-23 14:33:48 -07001115 if (task->task_proto == SAS_PROTOCOL_SMP ||
Jeff Skirvin98145cb2011-10-27 15:05:16 -07001116 sas_protocol_ata(task->task_proto) ||
Dan Williams38d88792011-06-23 14:33:48 -07001117 test_bit(IREQ_COMPLETE_IN_TARGET, &old_request->flags)) {
Dan Williams6f231dd2011-07-02 22:56:22 -07001118
1119 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1120
Jeff Skirvina5fde222011-03-04 14:06:42 -08001121 dev_dbg(&isci_host->pdev->dev,
Jeff Skirvin98145cb2011-10-27 15:05:16 -07001122 "%s: %s request"
Jeff Skirvina5fde222011-03-04 14:06:42 -08001123 " or complete_in_target (%d), thus no TMF\n",
Jeff Skirvin98145cb2011-10-27 15:05:16 -07001124 __func__,
1125 ((task->task_proto == SAS_PROTOCOL_SMP)
1126 ? "SMP"
1127 : (sas_protocol_ata(task->task_proto)
1128 ? "SATA/STP"
1129 : "<other>")
1130 ),
Dan Williams38d88792011-06-23 14:33:48 -07001131 test_bit(IREQ_COMPLETE_IN_TARGET, &old_request->flags));
Jeff Skirvina5fde222011-03-04 14:06:42 -08001132
Jeff Skirvin98145cb2011-10-27 15:05:16 -07001133 if (test_bit(IREQ_COMPLETE_IN_TARGET, &old_request->flags)) {
1134 spin_lock_irqsave(&task->task_state_lock, flags);
1135 task->task_state_flags |= SAS_TASK_STATE_DONE;
1136 task->task_state_flags &= ~(SAS_TASK_AT_INITIATOR |
1137 SAS_TASK_STATE_PENDING);
1138 spin_unlock_irqrestore(&task->task_state_lock, flags);
1139 ret = TMF_RESP_FUNC_COMPLETE;
1140 } else {
1141 spin_lock_irqsave(&task->task_state_lock, flags);
1142 task->task_state_flags &= ~(SAS_TASK_AT_INITIATOR |
1143 SAS_TASK_STATE_PENDING);
1144 spin_unlock_irqrestore(&task->task_state_lock, flags);
1145 }
Dan Williams6f231dd2011-07-02 22:56:22 -07001146
Jeff Skirvin98145cb2011-10-27 15:05:16 -07001147 /* STP and SMP devices are not sent a TMF, but the
1148 * outstanding I/O request is terminated below. This is
1149 * because SATA/STP and SMP discovery path timeouts directly
1150 * call the abort task interface for cleanup.
Dan Williams6f231dd2011-07-02 22:56:22 -07001151 */
Jeff Skirvin98145cb2011-10-27 15:05:16 -07001152 perform_termination = 1;
1153
Dan Williams6f231dd2011-07-02 22:56:22 -07001154 } else {
1155 /* Fill in the tmf stucture */
Dan Williams209fae12011-06-13 17:39:44 -07001156 isci_task_build_abort_task_tmf(&tmf, isci_tmf_ssp_task_abort,
Jeff Skirvinc3f42fe2011-03-04 14:06:56 -08001157 isci_abort_task_process_cb,
1158 old_request);
Dan Williams6f231dd2011-07-02 22:56:22 -07001159
Dan Williams6f231dd2011-07-02 22:56:22 -07001160 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1161
Jeff Skirvin98145cb2011-10-27 15:05:16 -07001162 #define ISCI_ABORT_TASK_TIMEOUT_MS 500 /* 1/2 second timeout */
Dan Williams209fae12011-06-13 17:39:44 -07001163 ret = isci_task_execute_tmf(isci_host, isci_device, &tmf,
Dan Williams6f231dd2011-07-02 22:56:22 -07001164 ISCI_ABORT_TASK_TIMEOUT_MS);
1165
Jeff Skirvin98145cb2011-10-27 15:05:16 -07001166 if (ret == TMF_RESP_FUNC_COMPLETE)
1167 perform_termination = 1;
1168 else
Dan Williamsa8a0a132011-07-01 12:07:25 -07001169 dev_dbg(&isci_host->pdev->dev,
Jeff Skirvin98145cb2011-10-27 15:05:16 -07001170 "%s: isci_task_send_tmf failed\n", __func__);
Dan Williams6f231dd2011-07-02 22:56:22 -07001171 }
Jeff Skirvin98145cb2011-10-27 15:05:16 -07001172 if (perform_termination) {
Dan Williams38d88792011-06-23 14:33:48 -07001173 set_bit(IREQ_COMPLETE_IN_TARGET, &old_request->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001174
Jeff Skirvin77c852f2011-06-20 14:09:16 -07001175 /* Clean up the request on our side, and wait for the aborted
1176 * I/O to complete.
1177 */
Jeff Skirvin98145cb2011-10-27 15:05:16 -07001178 isci_terminate_request_core(isci_host, isci_device,
1179 old_request);
Jeff Skirvina5fde222011-03-04 14:06:42 -08001180 }
1181
1182 /* Make sure we do not leave a reference to aborted_io_completion */
1183 old_request->io_request_completion = NULL;
Dan Williams209fae12011-06-13 17:39:44 -07001184 out:
1185 isci_put_device(isci_device);
Dan Williams6f231dd2011-07-02 22:56:22 -07001186 return ret;
1187}
1188
1189/**
1190 * isci_task_abort_task_set() - This function is one of the SAS Domain Template
1191 * functions. This is one of the Task Management functoins called by libsas,
1192 * to abort all task for the given lun.
1193 * @d_device: This parameter specifies the domain device associated with this
1194 * request.
1195 * @lun: This parameter specifies the lun associated with this request.
1196 *
1197 * status, zero indicates success.
1198 */
1199int isci_task_abort_task_set(
1200 struct domain_device *d_device,
1201 u8 *lun)
1202{
1203 return TMF_RESP_FUNC_FAILED;
1204}
1205
1206
1207/**
1208 * isci_task_clear_aca() - This function is one of the SAS Domain Template
1209 * functions. This is one of the Task Management functoins called by libsas.
1210 * @d_device: This parameter specifies the domain device associated with this
1211 * request.
1212 * @lun: This parameter specifies the lun associated with this request.
1213 *
1214 * status, zero indicates success.
1215 */
1216int isci_task_clear_aca(
1217 struct domain_device *d_device,
1218 u8 *lun)
1219{
1220 return TMF_RESP_FUNC_FAILED;
1221}
1222
1223
1224
1225/**
1226 * isci_task_clear_task_set() - This function is one of the SAS Domain Template
1227 * functions. This is one of the Task Management functoins called by libsas.
1228 * @d_device: This parameter specifies the domain device associated with this
1229 * request.
1230 * @lun: This parameter specifies the lun associated with this request.
1231 *
1232 * status, zero indicates success.
1233 */
1234int isci_task_clear_task_set(
1235 struct domain_device *d_device,
1236 u8 *lun)
1237{
1238 return TMF_RESP_FUNC_FAILED;
1239}
1240
1241
1242/**
1243 * isci_task_query_task() - This function is implemented to cause libsas to
1244 * correctly escalate the failed abort to a LUN or target reset (this is
1245 * because sas_scsi_find_task libsas function does not correctly interpret
1246 * all return codes from the abort task call). When TMF_RESP_FUNC_SUCC is
1247 * returned, libsas turns this into a LUN reset; when FUNC_FAILED is
1248 * returned, libsas will turn this into a target reset
1249 * @task: This parameter specifies the sas task being queried.
1250 * @lun: This parameter specifies the lun associated with this request.
1251 *
1252 * status, zero indicates success.
1253 */
1254int isci_task_query_task(
1255 struct sas_task *task)
1256{
1257 /* See if there is a pending device reset for this device. */
1258 if (task->task_state_flags & SAS_TASK_NEED_DEV_RESET)
1259 return TMF_RESP_FUNC_FAILED;
1260 else
1261 return TMF_RESP_FUNC_SUCC;
1262}
1263
Dave Jiangaf5ae892011-05-04 17:53:24 -07001264/*
Dan Williams6f231dd2011-07-02 22:56:22 -07001265 * isci_task_request_complete() - This function is called by the sci core when
1266 * an task request completes.
Dave Jiangaf5ae892011-05-04 17:53:24 -07001267 * @ihost: This parameter specifies the ISCI host object
1268 * @ireq: This parameter is the completed isci_request object.
Dan Williams6f231dd2011-07-02 22:56:22 -07001269 * @completion_status: This parameter specifies the completion status from the
1270 * sci core.
1271 *
1272 * none.
1273 */
Dave Jiangaf5ae892011-05-04 17:53:24 -07001274void
1275isci_task_request_complete(struct isci_host *ihost,
1276 struct isci_request *ireq,
1277 enum sci_task_status completion_status)
Dan Williams6f231dd2011-07-02 22:56:22 -07001278{
Dave Jiangaf5ae892011-05-04 17:53:24 -07001279 struct isci_tmf *tmf = isci_request_access_tmf(ireq);
Jeff Skirvinb343dff2011-10-27 15:05:06 -07001280 struct completion *tmf_complete = NULL;
1281 struct completion *request_complete = ireq->io_request_completion;
Dan Williams6f231dd2011-07-02 22:56:22 -07001282
Dave Jiangaf5ae892011-05-04 17:53:24 -07001283 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -07001284 "%s: request = %p, status=%d\n",
Dave Jiangaf5ae892011-05-04 17:53:24 -07001285 __func__, ireq, completion_status);
Dan Williams6f231dd2011-07-02 22:56:22 -07001286
Dave Jiang8d2c65c2011-06-01 09:03:08 +00001287 isci_request_change_state(ireq, completed);
Dan Williams6f231dd2011-07-02 22:56:22 -07001288
Dan Williams38d88792011-06-23 14:33:48 -07001289 set_bit(IREQ_COMPLETE_IN_TARGET, &ireq->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001290
Jeff Skirvinb343dff2011-10-27 15:05:06 -07001291 if (tmf) {
1292 tmf->status = completion_status;
1293
1294 if (tmf->proto == SAS_PROTOCOL_SSP) {
1295 memcpy(&tmf->resp.resp_iu,
1296 &ireq->ssp.rsp,
1297 SSP_RESP_IU_MAX_SIZE);
1298 } else if (tmf->proto == SAS_PROTOCOL_SATA) {
1299 memcpy(&tmf->resp.d2h_fis,
1300 &ireq->stp.rsp,
1301 sizeof(struct dev_to_host_fis));
1302 }
1303 /* PRINT_TMF( ((struct isci_tmf *)request->task)); */
1304 tmf_complete = tmf->complete;
Dan Williams6f231dd2011-07-02 22:56:22 -07001305 }
Dan Williams89a73012011-06-30 19:14:33 -07001306 sci_controller_complete_io(ihost, ireq->target_device, ireq);
Dan Williams67ea8382011-05-08 11:47:15 -07001307 /* set the 'terminated' flag handle to make sure it cannot be terminated
Dan Williams6f231dd2011-07-02 22:56:22 -07001308 * or completed again.
1309 */
Dan Williams38d88792011-06-23 14:33:48 -07001310 set_bit(IREQ_TERMINATED, &ireq->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001311
Jeff Skirvind6891682011-10-27 15:05:01 -07001312 /* As soon as something is in the terminate path, deallocation is
1313 * managed there. Note that the final non-managed state of a task
1314 * request is "completed".
1315 */
1316 if ((ireq->status == completed) ||
1317 !isci_request_is_dealloc_managed(ireq->status)) {
1318 isci_request_change_state(ireq, unallocated);
1319 isci_free_tag(ihost, ireq->io_tag);
1320 list_del_init(&ireq->dev_node);
1321 }
Dan Williams6f231dd2011-07-02 22:56:22 -07001322
Jeff Skirvinb343dff2011-10-27 15:05:06 -07001323 /* "request_complete" is set if the task was being terminated. */
1324 if (request_complete)
1325 complete(request_complete);
1326
Dan Williams6f231dd2011-07-02 22:56:22 -07001327 /* The task management part completes last. */
Jeff Skirvinb343dff2011-10-27 15:05:06 -07001328 if (tmf_complete)
1329 complete(tmf_complete);
Dan Williams6f231dd2011-07-02 22:56:22 -07001330}
1331
Dan Williams209fae12011-06-13 17:39:44 -07001332static int isci_reset_device(struct isci_host *ihost,
Jeff Skirvinbc6f3872011-07-01 13:03:44 -07001333 struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07001334{
Dan Williamsf41a0c42011-12-21 21:33:17 -08001335 struct sas_phy *phy = sas_get_local_phy(idev->domain_dev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001336 enum sci_status status;
Dan Williamsd06b4872011-05-02 13:59:25 -07001337 unsigned long flags;
1338 int rc;
Dan Williams6f231dd2011-07-02 22:56:22 -07001339
Dan Williamsd06b4872011-05-02 13:59:25 -07001340 dev_dbg(&ihost->pdev->dev, "%s: idev %p\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001341
Dan Williamsd06b4872011-05-02 13:59:25 -07001342 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williams89a73012011-06-30 19:14:33 -07001343 status = sci_remote_device_reset(idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001344 if (status != SCI_SUCCESS) {
Dan Williamsd06b4872011-05-02 13:59:25 -07001345 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001346
Dan Williamsa8a0a132011-07-01 12:07:25 -07001347 dev_dbg(&ihost->pdev->dev,
Dan Williams89a73012011-06-30 19:14:33 -07001348 "%s: sci_remote_device_reset(%p) returned %d!\n",
Dan Williamsd06b4872011-05-02 13:59:25 -07001349 __func__, idev, status);
Dan Williamsf41a0c42011-12-21 21:33:17 -08001350 rc = TMF_RESP_FUNC_FAILED;
1351 goto out;
Dan Williams6f231dd2011-07-02 22:56:22 -07001352 }
Dan Williamsd06b4872011-05-02 13:59:25 -07001353 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001354
Jeff Skirvinbc6f3872011-07-01 13:03:44 -07001355 rc = sas_phy_reset(phy, true);
Dan Williams6f231dd2011-07-02 22:56:22 -07001356
1357 /* Terminate in-progress I/O now. */
Dan Williamsd06b4872011-05-02 13:59:25 -07001358 isci_remote_device_nuke_requests(ihost, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001359
Jeff Skirvinff717ab2011-06-20 14:08:51 -07001360 /* Since all pending TCs have been cleaned, resume the RNC. */
Dan Williamsd06b4872011-05-02 13:59:25 -07001361 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williams89a73012011-06-30 19:14:33 -07001362 status = sci_remote_device_reset_complete(idev);
Dan Williamsd06b4872011-05-02 13:59:25 -07001363 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001364
1365 if (status != SCI_SUCCESS) {
Dan Williamsa8a0a132011-07-01 12:07:25 -07001366 dev_dbg(&ihost->pdev->dev,
Dan Williams89a73012011-06-30 19:14:33 -07001367 "%s: sci_remote_device_reset_complete(%p) "
Dan Williamsd06b4872011-05-02 13:59:25 -07001368 "returned %d!\n", __func__, idev, status);
Dan Williams6f231dd2011-07-02 22:56:22 -07001369 }
Dan Williams6f231dd2011-07-02 22:56:22 -07001370
Dan Williamsd06b4872011-05-02 13:59:25 -07001371 dev_dbg(&ihost->pdev->dev, "%s: idev %p complete.\n", __func__, idev);
Dan Williamsf41a0c42011-12-21 21:33:17 -08001372 out:
1373 sas_put_local_phy(phy);
Dan Williamsd06b4872011-05-02 13:59:25 -07001374 return rc;
1375}
1376
1377int isci_task_I_T_nexus_reset(struct domain_device *dev)
1378{
1379 struct isci_host *ihost = dev_to_ihost(dev);
Dan Williamsd06b4872011-05-02 13:59:25 -07001380 struct isci_remote_device *idev;
1381 unsigned long flags;
Jeff Skirvinbc6f3872011-07-01 13:03:44 -07001382 int ret;
Dan Williamsd06b4872011-05-02 13:59:25 -07001383
Dan Williamsd06b4872011-05-02 13:59:25 -07001384 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williams209fae12011-06-13 17:39:44 -07001385 idev = isci_lookup_device(dev);
Dan Williamsd06b4872011-05-02 13:59:25 -07001386 spin_unlock_irqrestore(&ihost->scic_lock, flags);
1387
Dan Williams209fae12011-06-13 17:39:44 -07001388 if (!idev || !test_bit(IDEV_EH, &idev->flags)) {
1389 ret = TMF_RESP_FUNC_COMPLETE;
1390 goto out;
1391 }
Dan Williamsd06b4872011-05-02 13:59:25 -07001392
Jeff Skirvinbc6f3872011-07-01 13:03:44 -07001393 ret = isci_reset_device(ihost, idev);
Dan Williams209fae12011-06-13 17:39:44 -07001394 out:
1395 isci_put_device(idev);
1396 return ret;
Dan Williamsd06b4872011-05-02 13:59:25 -07001397}
1398
1399int isci_bus_reset_handler(struct scsi_cmnd *cmd)
1400{
1401 struct domain_device *dev = sdev_to_domain_dev(cmd->device);
Dan Williams209fae12011-06-13 17:39:44 -07001402 struct isci_host *ihost = dev_to_ihost(dev);
1403 struct isci_remote_device *idev;
Dan Williams209fae12011-06-13 17:39:44 -07001404 unsigned long flags;
Jeff Skirvinbc6f3872011-07-01 13:03:44 -07001405 int ret;
Dan Williamsd06b4872011-05-02 13:59:25 -07001406
Dan Williams209fae12011-06-13 17:39:44 -07001407 spin_lock_irqsave(&ihost->scic_lock, flags);
1408 idev = isci_lookup_device(dev);
1409 spin_unlock_irqrestore(&ihost->scic_lock, flags);
1410
1411 if (!idev) {
1412 ret = TMF_RESP_FUNC_COMPLETE;
1413 goto out;
1414 }
1415
Jeff Skirvinbc6f3872011-07-01 13:03:44 -07001416 ret = isci_reset_device(ihost, idev);
Dan Williams209fae12011-06-13 17:39:44 -07001417 out:
1418 isci_put_device(idev);
1419 return ret;
Dan Williams6f231dd2011-07-02 22:56:22 -07001420}