Merge git://git.kernel.org/pub/scm/linux/kernel/git/steve/gfs2-3.0-fixes
[deliverable/linux.git] / drivers / scsi / isci / task.c
CommitLineData
6f231dda
DW
1/*
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>
50e7f9b5 57#include <linux/irqflags.h>
af5ae893 58#include "sas.h"
61aaff49 59#include <scsi/libsas.h>
88f3b62a
DW
60#include "remote_device.h"
61#include "remote_node_context.h"
6f231dda
DW
62#include "isci.h"
63#include "request.h"
6f231dda 64#include "task.h"
312e0c24 65#include "host.h"
af5ae893 66
50e7f9b5 67/**
1077a574
DW
68* 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.
50e7f9b5 74*
50e7f9b5 75*/
1077a574
DW
76static void isci_task_refuse(struct isci_host *ihost, struct sas_task *task,
77 enum service_response response,
78 enum exec_status status)
79
50e7f9b5 80{
14aaa9f0 81 unsigned long flags;
50e7f9b5 82
14aaa9f0
JS
83 /* Normal notification (task_done) */
84 dev_dbg(&ihost->pdev->dev, "%s: task = %p, response=%d, status=%d\n",
85 __func__, task, response, status);
50e7f9b5 86
14aaa9f0 87 spin_lock_irqsave(&task->task_state_lock, flags);
50e7f9b5 88
14aaa9f0
JS
89 task->task_status.resp = response;
90 task->task_status.stat = status;
50e7f9b5 91
14aaa9f0
JS
92 /* Normal notification (task_done) */
93 task->task_state_flags |= SAS_TASK_STATE_DONE;
94 task->task_state_flags &= ~(SAS_TASK_AT_INITIATOR |
95 SAS_TASK_STATE_PENDING);
96 task->lldd_task = NULL;
97 spin_unlock_irqrestore(&task->task_state_lock, flags);
98
99 task->task_done(task);
50e7f9b5 100}
6f231dda 101
1077a574
DW
102#define for_each_sas_task(num, task) \
103 for (; num > 0; num--,\
104 task = list_entry(task->list.next, struct sas_task, list))
105
9274f45e
JS
106
107static inline int isci_device_io_ready(struct isci_remote_device *idev,
108 struct sas_task *task)
109{
110 return idev ? test_bit(IDEV_IO_READY, &idev->flags) ||
111 (test_bit(IDEV_IO_NCQERROR, &idev->flags) &&
112 isci_task_is_ncq_recovery(task))
113 : 0;
114}
6f231dda
DW
115/**
116 * isci_task_execute_task() - This function is one of the SAS Domain Template
117 * functions. This function is called by libsas to send a task down to
118 * hardware.
119 * @task: This parameter specifies the SAS task to send.
120 * @num: This parameter specifies the number of tasks to queue.
121 * @gfp_flags: This parameter specifies the context of this call.
122 *
123 * status, zero indicates success.
124 */
125int isci_task_execute_task(struct sas_task *task, int num, gfp_t gfp_flags)
126{
4393aa4e 127 struct isci_host *ihost = dev_to_ihost(task->dev);
209fae14 128 struct isci_remote_device *idev;
6f231dda 129 unsigned long flags;
f2088267 130 bool io_ready;
312e0c24 131 u16 tag;
6f231dda 132
1077a574 133 dev_dbg(&ihost->pdev->dev, "%s: num=%d\n", __func__, num);
6f231dda 134
1077a574 135 for_each_sas_task(num, task) {
312e0c24
DW
136 enum sci_status status = SCI_FAILURE;
137
209fae14
DW
138 spin_lock_irqsave(&ihost->scic_lock, flags);
139 idev = isci_lookup_device(task->dev);
9274f45e 140 io_ready = isci_device_io_ready(idev, task);
312e0c24 141 tag = isci_alloc_tag(ihost);
209fae14 142 spin_unlock_irqrestore(&ihost->scic_lock, flags);
6f231dda 143
f2088267
DW
144 dev_dbg(&ihost->pdev->dev,
145 "task: %p, num: %d dev: %p idev: %p:%#lx cmd = %p\n",
146 task, num, task->dev, idev, idev ? idev->flags : 0,
147 task->uldd_task);
6f231dda 148
f2088267
DW
149 if (!idev) {
150 isci_task_refuse(ihost, task, SAS_TASK_UNDELIVERED,
151 SAS_DEVICE_UNKNOWN);
312e0c24 152 } else if (!io_ready || tag == SCI_CONTROLLER_INVALID_IO_TAG) {
f2088267
DW
153 /* Indicate QUEUE_FULL so that the scsi midlayer
154 * retries.
155 */
156 isci_task_refuse(ihost, task, SAS_TASK_COMPLETE,
157 SAS_QUEUE_FULL);
f0846c68
JS
158 } else {
159 /* There is a device and it's ready for I/O. */
160 spin_lock_irqsave(&task->task_state_lock, flags);
6f231dda 161
f0846c68 162 if (task->task_state_flags & SAS_TASK_STATE_ABORTED) {
f2088267 163 /* The I/O was aborted. */
f0846c68
JS
164 spin_unlock_irqrestore(&task->task_state_lock,
165 flags);
6f231dda 166
1077a574
DW
167 isci_task_refuse(ihost, task,
168 SAS_TASK_UNDELIVERED,
169 SAM_STAT_TASK_ABORTED);
f0846c68 170 } else {
6f231dda
DW
171 task->task_state_flags |= SAS_TASK_AT_INITIATOR;
172 spin_unlock_irqrestore(&task->task_state_lock, flags);
f0846c68
JS
173
174 /* build and send the request. */
db056250 175 status = isci_request_execute(ihost, idev, task, tag);
f0846c68
JS
176
177 if (status != SCI_SUCCESS) {
178
179 spin_lock_irqsave(&task->task_state_lock, flags);
180 /* Did not really start this command. */
181 task->task_state_flags &= ~SAS_TASK_AT_INITIATOR;
182 spin_unlock_irqrestore(&task->task_state_lock, flags);
183
c2cb8a5f
JS
184 if (test_bit(IDEV_GONE, &idev->flags)) {
185
186 /* Indicate that the device
187 * is gone.
188 */
189 isci_task_refuse(ihost, task,
190 SAS_TASK_UNDELIVERED,
191 SAS_DEVICE_UNKNOWN);
192 } else {
193 /* Indicate QUEUE_FULL so that
194 * the scsi midlayer retries.
195 * If the request failed for
196 * remote device reasons, it
197 * gets returned as
198 * SAS_TASK_UNDELIVERED next
199 * time through.
200 */
201 isci_task_refuse(ihost, task,
202 SAS_TASK_COMPLETE,
203 SAS_QUEUE_FULL);
204 }
f0846c68 205 }
6f231dda
DW
206 }
207 }
312e0c24
DW
208 if (status != SCI_SUCCESS && tag != SCI_CONTROLLER_INVALID_IO_TAG) {
209 spin_lock_irqsave(&ihost->scic_lock, flags);
210 /* command never hit the device, so just free
211 * the tci and skip the sequence increment
212 */
213 isci_tci_free(ihost, ISCI_TAG_TCI(tag));
214 spin_unlock_irqrestore(&ihost->scic_lock, flags);
215 }
209fae14 216 isci_put_device(idev);
1077a574 217 }
6f231dda
DW
218 return 0;
219}
220
0d0cf14c 221static struct isci_request *isci_task_request_build(struct isci_host *ihost,
209fae14 222 struct isci_remote_device *idev,
312e0c24 223 u16 tag, struct isci_tmf *isci_tmf)
6f231dda 224{
6f231dda 225 enum sci_status status = SCI_FAILURE;
0d0cf14c 226 struct isci_request *ireq = NULL;
a1a113b0 227 struct domain_device *dev;
6f231dda 228
0d0cf14c 229 dev_dbg(&ihost->pdev->dev,
6f231dda
DW
230 "%s: isci_tmf = %p\n", __func__, isci_tmf);
231
0d0cf14c 232 dev = idev->domain_dev;
6f231dda
DW
233
234 /* do common allocation and init of request object. */
db056250 235 ireq = isci_tmf_request_from_tag(ihost, isci_tmf, tag);
0d0cf14c
DW
236 if (!ireq)
237 return NULL;
6f231dda
DW
238
239 /* let the core do it's construct. */
89a7301f 240 status = sci_task_request_construct(ihost, idev, tag,
5076a1a9 241 ireq);
6f231dda
DW
242
243 if (status != SCI_SUCCESS) {
0d0cf14c 244 dev_warn(&ihost->pdev->dev,
89a7301f 245 "%s: sci_task_request_construct failed - "
6f231dda
DW
246 "status = 0x%x\n",
247 __func__,
248 status);
db056250 249 return NULL;
6f231dda
DW
250 }
251
a1a113b0 252 /* XXX convert to get this from task->tproto like other drivers */
aa9f8328 253 if (dev->dev_type == SAS_END_DEVICE) {
6f231dda 254 isci_tmf->proto = SAS_PROTOCOL_SSP;
89a7301f 255 status = sci_task_request_construct_ssp(ireq);
6f231dda 256 if (status != SCI_SUCCESS)
db056250 257 return NULL;
6f231dda
DW
258 }
259
0d0cf14c 260 return ireq;
6f231dda
DW
261}
262
16ba7709
DW
263static int isci_task_execute_tmf(struct isci_host *ihost,
264 struct isci_remote_device *idev,
265 struct isci_tmf *tmf, unsigned long timeout_ms)
6f231dda
DW
266{
267 DECLARE_COMPLETION_ONSTACK(completion);
467e855a 268 enum sci_task_status status = SCI_TASK_FAILURE;
0d0cf14c 269 struct isci_request *ireq;
6f231dda
DW
270 int ret = TMF_RESP_FUNC_FAILED;
271 unsigned long flags;
fd18388b 272 unsigned long timeleft;
312e0c24
DW
273 u16 tag;
274
275 spin_lock_irqsave(&ihost->scic_lock, flags);
276 tag = isci_alloc_tag(ihost);
277 spin_unlock_irqrestore(&ihost->scic_lock, flags);
278
279 if (tag == SCI_CONTROLLER_INVALID_IO_TAG)
280 return ret;
6f231dda
DW
281
282 /* sanity check, return TMF_RESP_FUNC_FAILED
283 * if the device is not there and ready.
284 */
78a6f06e
DW
285 if (!idev ||
286 (!test_bit(IDEV_IO_READY, &idev->flags) &&
287 !test_bit(IDEV_IO_NCQERROR, &idev->flags))) {
0d0cf14c 288 dev_dbg(&ihost->pdev->dev,
78a6f06e 289 "%s: idev = %p not ready (%#lx)\n",
6f231dda 290 __func__,
78a6f06e 291 idev, idev ? idev->flags : 0);
312e0c24 292 goto err_tci;
6f231dda 293 } else
0d0cf14c 294 dev_dbg(&ihost->pdev->dev,
78a6f06e
DW
295 "%s: idev = %p\n",
296 __func__, idev);
6f231dda
DW
297
298 /* Assign the pointer to the TMF's completion kernel wait structure. */
299 tmf->complete = &completion;
b343dff1 300 tmf->status = SCI_FAILURE_TIMEOUT;
6f231dda 301
78a6f06e 302 ireq = isci_task_request_build(ihost, idev, tag, tmf);
312e0c24
DW
303 if (!ireq)
304 goto err_tci;
6f231dda 305
0d0cf14c 306 spin_lock_irqsave(&ihost->scic_lock, flags);
6f231dda
DW
307
308 /* start the TMF io. */
89a7301f 309 status = sci_controller_start_task(ihost, idev, ireq);
6f231dda 310
467e855a 311 if (status != SCI_TASK_SUCCESS) {
a8a0a133 312 dev_dbg(&ihost->pdev->dev,
6f231dda
DW
313 "%s: start_io failed - status = 0x%x, request = %p\n",
314 __func__,
315 status,
0d0cf14c
DW
316 ireq);
317 spin_unlock_irqrestore(&ihost->scic_lock, flags);
db056250 318 goto err_tci;
6f231dda 319 }
0d0cf14c 320 spin_unlock_irqrestore(&ihost->scic_lock, flags);
6f231dda 321
31a38ef0
JS
322 /* The RNC must be unsuspended before the TMF can get a response. */
323 isci_remote_device_resume_from_abort(ihost, idev);
324
6f231dda 325 /* Wait for the TMF to complete, or a timeout. */
fd18388b 326 timeleft = wait_for_completion_timeout(&completion,
086a0dab 327 msecs_to_jiffies(timeout_ms));
fd18388b
EN
328
329 if (timeleft == 0) {
b343dff1
JS
330 /* The TMF did not complete - this could be because
331 * of an unplug. Terminate the TMF request now.
332 */
14aaa9f0 333 isci_remote_device_suspend_terminate(ihost, idev, ireq);
fd18388b 334 }
6f231dda 335
27234ab4 336 isci_print_tmf(ihost, tmf);
6f231dda
DW
337
338 if (tmf->status == SCI_SUCCESS)
339 ret = TMF_RESP_FUNC_COMPLETE;
340 else if (tmf->status == SCI_FAILURE_IO_RESPONSE_VALID) {
0d0cf14c 341 dev_dbg(&ihost->pdev->dev,
6f231dda
DW
342 "%s: tmf.status == "
343 "SCI_FAILURE_IO_RESPONSE_VALID\n",
344 __func__);
345 ret = TMF_RESP_FUNC_COMPLETE;
346 }
347 /* Else - leave the default "failed" status alone. */
348
0d0cf14c 349 dev_dbg(&ihost->pdev->dev,
6f231dda
DW
350 "%s: completed request = %p\n",
351 __func__,
0d0cf14c 352 ireq);
6f231dda 353
6f231dda 354 return ret;
312e0c24 355
312e0c24
DW
356 err_tci:
357 spin_lock_irqsave(&ihost->scic_lock, flags);
358 isci_tci_free(ihost, ISCI_TAG_TCI(tag));
359 spin_unlock_irqrestore(&ihost->scic_lock, flags);
360
361 return ret;
6f231dda
DW
362}
363
16ba7709 364static void isci_task_build_tmf(struct isci_tmf *tmf,
14aaa9f0 365 enum isci_tmf_function_codes code)
6f231dda 366{
6f231dda 367 memset(tmf, 0, sizeof(*tmf));
14aaa9f0 368 tmf->tmf_code = code;
c3f42feb 369}
1fad9e93 370
16ba7709
DW
371static void isci_task_build_abort_task_tmf(struct isci_tmf *tmf,
372 enum isci_tmf_function_codes code,
16ba7709 373 struct isci_request *old_request)
c3f42feb 374{
14aaa9f0 375 isci_task_build_tmf(tmf, code);
c3f42feb 376 tmf->io_tag = old_request->io_tag;
6f231dda
DW
377}
378
6f231dda
DW
379/**
380 * isci_task_send_lu_reset_sas() - This function is called by of the SAS Domain
381 * Template functions.
382 * @lun: This parameter specifies the lun to be reset.
383 *
384 * status, zero indicates success.
385 */
386static int isci_task_send_lu_reset_sas(
387 struct isci_host *isci_host,
388 struct isci_remote_device *isci_device,
389 u8 *lun)
390{
391 struct isci_tmf tmf;
392 int ret = TMF_RESP_FUNC_FAILED;
393
394 dev_dbg(&isci_host->pdev->dev,
395 "%s: isci_host = %p, isci_device = %p\n",
396 __func__, isci_host, isci_device);
397 /* Send the LUN reset to the target. By the time the call returns,
398 * the TMF has fully exected in the target (in which case the return
399 * value is "TMF_RESP_FUNC_COMPLETE", or the request timed-out (or
400 * was otherwise unable to be executed ("TMF_RESP_FUNC_FAILED").
401 */
14aaa9f0 402 isci_task_build_tmf(&tmf, isci_tmf_ssp_lun_reset);
6f231dda
DW
403
404 #define ISCI_LU_RESET_TIMEOUT_MS 2000 /* 2 second timeout. */
209fae14 405 ret = isci_task_execute_tmf(isci_host, isci_device, &tmf, ISCI_LU_RESET_TIMEOUT_MS);
6f231dda
DW
406
407 if (ret == TMF_RESP_FUNC_COMPLETE)
408 dev_dbg(&isci_host->pdev->dev,
409 "%s: %p: TMF_LU_RESET passed\n",
410 __func__, isci_device);
411 else
412 dev_dbg(&isci_host->pdev->dev,
413 "%s: %p: TMF_LU_RESET failed (%x)\n",
414 __func__, isci_device, ret);
415
416 return ret;
417}
418
43a5ab15 419int isci_task_lu_reset(struct domain_device *dev, u8 *lun)
16ba7709 420{
5b6bf225 421 struct isci_host *ihost = dev_to_ihost(dev);
14aaa9f0 422 struct isci_remote_device *idev;
209fae14 423 unsigned long flags;
397497dd 424 int ret = TMF_RESP_FUNC_COMPLETE;
6f231dda 425
5b6bf225 426 spin_lock_irqsave(&ihost->scic_lock, flags);
63732502 427 idev = isci_get_device(dev->lldd_dev);
5b6bf225 428 spin_unlock_irqrestore(&ihost->scic_lock, flags);
6f231dda 429
5b6bf225 430 dev_dbg(&ihost->pdev->dev,
4393aa4e 431 "%s: domain_device=%p, isci_host=%p; isci_device=%p\n",
14aaa9f0 432 __func__, dev, ihost, idev);
6f231dda 433
14aaa9f0 434 if (!idev) {
d80ecd57 435 /* If the device is gone, escalate to I_T_Nexus_Reset. */
5b6bf225 436 dev_dbg(&ihost->pdev->dev, "%s: No dev\n", __func__);
6f231dda 437
d80ecd57 438 ret = TMF_RESP_FUNC_FAILED;
209fae14 439 goto out;
6f231dda
DW
440 }
441
08c031e4
JS
442 /* Suspend the RNC, kill all TCs */
443 if (isci_remote_device_suspend_terminate(ihost, idev, NULL)
444 != SCI_SUCCESS) {
445 /* The suspend/terminate only fails if isci_get_device fails */
446 ret = TMF_RESP_FUNC_FAILED;
447 goto out;
448 }
449 /* All pending I/Os have been terminated and cleaned up. */
397497dd
JS
450 if (!test_bit(IDEV_GONE, &idev->flags)) {
451 if (dev_is_sata(dev))
452 sas_ata_schedule_reset(dev);
453 else
454 /* Send the task management part of the reset. */
455 ret = isci_task_send_lu_reset_sas(ihost, idev, lun);
5b6bf225 456 }
209fae14 457 out:
14aaa9f0 458 isci_put_device(idev);
6f231dda
DW
459 return ret;
460}
461
462
463/* int (*lldd_clear_nexus_port)(struct asd_sas_port *); */
464int isci_task_clear_nexus_port(struct asd_sas_port *port)
465{
466 return TMF_RESP_FUNC_FAILED;
467}
468
469
470
471int isci_task_clear_nexus_ha(struct sas_ha_struct *ha)
472{
473 return TMF_RESP_FUNC_FAILED;
474}
475
6f231dda
DW
476/* Task Management Functions. Must be called from process context. */
477
6f231dda
DW
478/**
479 * isci_task_abort_task() - This function is one of the SAS Domain Template
480 * functions. This function is called by libsas to abort a specified task.
481 * @task: This parameter specifies the SAS task to abort.
482 *
483 * status, zero indicates success.
484 */
485int isci_task_abort_task(struct sas_task *task)
486{
14aaa9f0 487 struct isci_host *ihost = dev_to_ihost(task->dev);
6f231dda 488 DECLARE_COMPLETION_ONSTACK(aborted_io_completion);
a5fde225 489 struct isci_request *old_request = NULL;
14aaa9f0 490 struct isci_remote_device *idev = NULL;
a5fde225
JS
491 struct isci_tmf tmf;
492 int ret = TMF_RESP_FUNC_FAILED;
493 unsigned long flags;
96f15f29 494 int target_done_already = 0;
6f231dda
DW
495
496 /* Get the isci_request reference from the task. Note that
497 * this check does not depend on the pending request list
498 * in the device, because tasks driving resets may land here
499 * after completion in the core.
500 */
14aaa9f0 501 spin_lock_irqsave(&ihost->scic_lock, flags);
209fae14
DW
502 spin_lock(&task->task_state_lock);
503
504 old_request = task->lldd_task;
505
506 /* If task is already done, the request isn't valid */
507 if (!(task->task_state_flags & SAS_TASK_STATE_DONE) &&
508 (task->task_state_flags & SAS_TASK_AT_INITIATOR) &&
96f15f29 509 old_request) {
63732502 510 idev = isci_get_device(task->dev->lldd_dev);
96f15f29
JS
511 target_done_already = test_bit(IREQ_COMPLETE_IN_TARGET,
512 &old_request->flags);
513 }
209fae14 514 spin_unlock(&task->task_state_lock);
14aaa9f0 515 spin_unlock_irqrestore(&ihost->scic_lock, flags);
6f231dda 516
14aaa9f0 517 dev_warn(&ihost->pdev->dev,
397497dd
JS
518 "%s: dev = %p (%s%s), task = %p, old_request == %p\n",
519 __func__, idev,
520 (dev_is_sata(task->dev) ? "STP/SATA"
521 : ((dev_is_expander(task->dev))
522 ? "SMP"
523 : "SSP")),
524 ((idev) ? ((test_bit(IDEV_GONE, &idev->flags))
525 ? " IDEV_GONE"
526 : "")
527 : " <NULL>"),
528 task, old_request);
6f231dda 529
98145cb7
JS
530 /* Device reset conditions signalled in task_state_flags are the
531 * responsbility of libsas to observe at the start of the error
532 * handler thread.
6f231dda 533 */
14aaa9f0 534 if (!idev || !old_request) {
98145cb7
JS
535 /* The request has already completed and there
536 * is nothing to do here other than to set the task
537 * done bit, and indicate that the task abort function
4907cb7b 538 * was successful.
98145cb7
JS
539 */
540 spin_lock_irqsave(&task->task_state_lock, flags);
541 task->task_state_flags |= SAS_TASK_STATE_DONE;
542 task->task_state_flags &= ~(SAS_TASK_AT_INITIATOR |
543 SAS_TASK_STATE_PENDING);
544 spin_unlock_irqrestore(&task->task_state_lock, flags);
6f231dda 545
98145cb7 546 ret = TMF_RESP_FUNC_COMPLETE;
6f231dda 547
14aaa9f0
JS
548 dev_warn(&ihost->pdev->dev,
549 "%s: abort task not needed for %p\n",
550 __func__, task);
209fae14 551 goto out;
a5ec7f86 552 }
14aaa9f0
JS
553 /* Suspend the RNC, kill the TC */
554 if (isci_remote_device_suspend_terminate(ihost, idev, old_request)
555 != SCI_SUCCESS) {
556 dev_warn(&ihost->pdev->dev,
557 "%s: isci_remote_device_reset_terminate(dev=%p, "
558 "req=%p, task=%p) failed\n",
559 __func__, idev, old_request, task);
560 ret = TMF_RESP_FUNC_FAILED;
209fae14 561 goto out;
6f231dda 562 }
14aaa9f0
JS
563 spin_lock_irqsave(&ihost->scic_lock, flags);
564
38d8879b 565 if (task->task_proto == SAS_PROTOCOL_SMP ||
98145cb7 566 sas_protocol_ata(task->task_proto) ||
96f15f29 567 target_done_already ||
397497dd 568 test_bit(IDEV_GONE, &idev->flags)) {
6f231dda 569
14aaa9f0 570 spin_unlock_irqrestore(&ihost->scic_lock, flags);
6f231dda 571
31a38ef0
JS
572 /* No task to send, so explicitly resume the device here */
573 isci_remote_device_resume_from_abort(ihost, idev);
574
14aaa9f0
JS
575 dev_warn(&ihost->pdev->dev,
576 "%s: %s request"
397497dd
JS
577 " or complete_in_target (%d), "
578 "or IDEV_GONE (%d), thus no TMF\n",
14aaa9f0
JS
579 __func__,
580 ((task->task_proto == SAS_PROTOCOL_SMP)
581 ? "SMP"
582 : (sas_protocol_ata(task->task_proto)
583 ? "SATA/STP"
584 : "<other>")
585 ),
586 test_bit(IREQ_COMPLETE_IN_TARGET,
397497dd
JS
587 &old_request->flags),
588 test_bit(IDEV_GONE, &idev->flags));
5b6bf225 589
14aaa9f0
JS
590 spin_lock_irqsave(&task->task_state_lock, flags);
591 task->task_state_flags &= ~(SAS_TASK_AT_INITIATOR |
592 SAS_TASK_STATE_PENDING);
593 task->task_state_flags |= SAS_TASK_STATE_DONE;
594 spin_unlock_irqrestore(&task->task_state_lock, flags);
595
596 ret = TMF_RESP_FUNC_COMPLETE;
6f231dda
DW
597 } else {
598 /* Fill in the tmf stucture */
209fae14 599 isci_task_build_abort_task_tmf(&tmf, isci_tmf_ssp_task_abort,
c3f42feb 600 old_request);
6f231dda 601
14aaa9f0 602 spin_unlock_irqrestore(&ihost->scic_lock, flags);
5b6bf225 603
14aaa9f0 604 /* Send the task management request. */
98145cb7 605 #define ISCI_ABORT_TASK_TIMEOUT_MS 500 /* 1/2 second timeout */
14aaa9f0 606 ret = isci_task_execute_tmf(ihost, idev, &tmf,
6f231dda 607 ISCI_ABORT_TASK_TIMEOUT_MS);
a5fde225 608 }
14aaa9f0 609out:
63732502
JS
610 dev_warn(&ihost->pdev->dev,
611 "%s: Done; dev = %p, task = %p , old_request == %p\n",
612 __func__, idev, task, old_request);
14aaa9f0 613 isci_put_device(idev);
6f231dda
DW
614 return ret;
615}
616
617/**
618 * isci_task_abort_task_set() - This function is one of the SAS Domain Template
619 * functions. This is one of the Task Management functoins called by libsas,
620 * to abort all task for the given lun.
621 * @d_device: This parameter specifies the domain device associated with this
622 * request.
623 * @lun: This parameter specifies the lun associated with this request.
624 *
625 * status, zero indicates success.
626 */
627int isci_task_abort_task_set(
628 struct domain_device *d_device,
629 u8 *lun)
630{
631 return TMF_RESP_FUNC_FAILED;
632}
633
634
635/**
636 * isci_task_clear_aca() - This function is one of the SAS Domain Template
637 * functions. This is one of the Task Management functoins called by libsas.
638 * @d_device: This parameter specifies the domain device associated with this
639 * request.
640 * @lun: This parameter specifies the lun associated with this request.
641 *
642 * status, zero indicates success.
643 */
644int isci_task_clear_aca(
645 struct domain_device *d_device,
646 u8 *lun)
647{
648 return TMF_RESP_FUNC_FAILED;
649}
650
651
652
653/**
654 * isci_task_clear_task_set() - This function is one of the SAS Domain Template
655 * functions. This is one of the Task Management functoins called by libsas.
656 * @d_device: This parameter specifies the domain device associated with this
657 * request.
658 * @lun: This parameter specifies the lun associated with this request.
659 *
660 * status, zero indicates success.
661 */
662int isci_task_clear_task_set(
663 struct domain_device *d_device,
664 u8 *lun)
665{
666 return TMF_RESP_FUNC_FAILED;
667}
668
669
670/**
671 * isci_task_query_task() - This function is implemented to cause libsas to
672 * correctly escalate the failed abort to a LUN or target reset (this is
673 * because sas_scsi_find_task libsas function does not correctly interpret
674 * all return codes from the abort task call). When TMF_RESP_FUNC_SUCC is
675 * returned, libsas turns this into a LUN reset; when FUNC_FAILED is
676 * returned, libsas will turn this into a target reset
677 * @task: This parameter specifies the sas task being queried.
678 * @lun: This parameter specifies the lun associated with this request.
679 *
680 * status, zero indicates success.
681 */
682int isci_task_query_task(
683 struct sas_task *task)
684{
685 /* See if there is a pending device reset for this device. */
686 if (task->task_state_flags & SAS_TASK_NEED_DEV_RESET)
687 return TMF_RESP_FUNC_FAILED;
688 else
689 return TMF_RESP_FUNC_SUCC;
690}
691
af5ae893 692/*
6f231dda
DW
693 * isci_task_request_complete() - This function is called by the sci core when
694 * an task request completes.
af5ae893
DJ
695 * @ihost: This parameter specifies the ISCI host object
696 * @ireq: This parameter is the completed isci_request object.
6f231dda
DW
697 * @completion_status: This parameter specifies the completion status from the
698 * sci core.
699 *
700 * none.
701 */
af5ae893
DJ
702void
703isci_task_request_complete(struct isci_host *ihost,
704 struct isci_request *ireq,
705 enum sci_task_status completion_status)
6f231dda 706{
af5ae893 707 struct isci_tmf *tmf = isci_request_access_tmf(ireq);
b343dff1 708 struct completion *tmf_complete = NULL;
6f231dda 709
af5ae893 710 dev_dbg(&ihost->pdev->dev,
6f231dda 711 "%s: request = %p, status=%d\n",
af5ae893 712 __func__, ireq, completion_status);
6f231dda 713
38d8879b 714 set_bit(IREQ_COMPLETE_IN_TARGET, &ireq->flags);
6f231dda 715
b343dff1
JS
716 if (tmf) {
717 tmf->status = completion_status;
718
719 if (tmf->proto == SAS_PROTOCOL_SSP) {
720 memcpy(&tmf->resp.resp_iu,
721 &ireq->ssp.rsp,
722 SSP_RESP_IU_MAX_SIZE);
723 } else if (tmf->proto == SAS_PROTOCOL_SATA) {
724 memcpy(&tmf->resp.d2h_fis,
725 &ireq->stp.rsp,
726 sizeof(struct dev_to_host_fis));
727 }
728 /* PRINT_TMF( ((struct isci_tmf *)request->task)); */
729 tmf_complete = tmf->complete;
6f231dda 730 }
89a7301f 731 sci_controller_complete_io(ihost, ireq->target_device, ireq);
67ea838d 732 /* set the 'terminated' flag handle to make sure it cannot be terminated
6f231dda
DW
733 * or completed again.
734 */
38d8879b 735 set_bit(IREQ_TERMINATED, &ireq->flags);
6f231dda 736
1db79b3e
JS
737 if (test_and_clear_bit(IREQ_ABORT_PATH_ACTIVE, &ireq->flags))
738 wake_up_all(&ihost->eventq);
739
621120ca
JS
740 if (!test_bit(IREQ_NO_AUTO_FREE_TAG, &ireq->flags))
741 isci_free_tag(ihost, ireq->io_tag);
b343dff1 742
6f231dda 743 /* The task management part completes last. */
b343dff1
JS
744 if (tmf_complete)
745 complete(tmf_complete);
6f231dda
DW
746}
747
209fae14 748static int isci_reset_device(struct isci_host *ihost,
92776991 749 struct domain_device *dev,
bc6f387d 750 struct isci_remote_device *idev)
6f231dda 751{
397497dd 752 int rc = TMF_RESP_FUNC_COMPLETE, reset_stat = -1;
92776991
DW
753 struct sas_phy *phy = sas_get_local_phy(dev);
754 struct isci_port *iport = dev->port->lldd_port;
6f231dda 755
d06b487b 756 dev_dbg(&ihost->pdev->dev, "%s: idev %p\n", __func__, idev);
6f231dda 757
14aaa9f0
JS
758 /* Suspend the RNC, terminate all outstanding TCs. */
759 if (isci_remote_device_suspend_terminate(ihost, idev, NULL)
760 != SCI_SUCCESS) {
f41a0c44
DW
761 rc = TMF_RESP_FUNC_FAILED;
762 goto out;
6f231dda 763 }
14aaa9f0
JS
764 /* Note that since the termination for outstanding requests succeeded,
765 * this function will return success. This is because the resets will
766 * only fail if the device has been removed (ie. hotplug), and the
767 * primary duty of this function is to cleanup tasks, so that is the
768 * relevant status.
769 */
397497dd
JS
770 if (!test_bit(IDEV_GONE, &idev->flags)) {
771 if (scsi_is_sas_phy_local(phy)) {
772 struct isci_phy *iphy = &ihost->phys[phy->number];
773
774 reset_stat = isci_port_perform_hard_reset(ihost, iport,
775 iphy);
776 } else
777 reset_stat = sas_phy_reset(phy, !dev_is_sata(dev));
778 }
14aaa9f0 779 /* Explicitly resume the RNC here, since there was no task sent. */
31a38ef0 780 isci_remote_device_resume_from_abort(ihost, idev);
6f231dda 781
14aaa9f0
JS
782 dev_dbg(&ihost->pdev->dev, "%s: idev %p complete, reset_stat=%d.\n",
783 __func__, idev, reset_stat);
f41a0c44
DW
784 out:
785 sas_put_local_phy(phy);
d06b487b
DW
786 return rc;
787}
6f231dda 788
d06b487b
DW
789int isci_task_I_T_nexus_reset(struct domain_device *dev)
790{
791 struct isci_host *ihost = dev_to_ihost(dev);
d06b487b
DW
792 struct isci_remote_device *idev;
793 unsigned long flags;
bc6f387d 794 int ret;
d06b487b 795
d06b487b 796 spin_lock_irqsave(&ihost->scic_lock, flags);
14aaa9f0 797 idev = isci_get_device(dev->lldd_dev);
d06b487b
DW
798 spin_unlock_irqrestore(&ihost->scic_lock, flags);
799
5a998328
DW
800 if (!idev) {
801 /* XXX: need to cleanup any ireqs targeting this
802 * domain_device
803 */
209fae14
DW
804 ret = TMF_RESP_FUNC_COMPLETE;
805 goto out;
806 }
d06b487b 807
92776991 808 ret = isci_reset_device(ihost, dev, idev);
209fae14
DW
809 out:
810 isci_put_device(idev);
811 return ret;
d06b487b 812}
This page took 0.201289 seconds and 5 git commands to generate.