]> Pileus Git - ~andy/linux/blob - drivers/scsi/isci/task.c
isci: Manage tag releases differently when aborting tasks.
[~andy/linux] / drivers / scsi / isci / task.c
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>
57 #include <linux/irqflags.h>
58 #include "sas.h"
59 #include <scsi/libsas.h>
60 #include "remote_device.h"
61 #include "remote_node_context.h"
62 #include "isci.h"
63 #include "request.h"
64 #include "task.h"
65 #include "host.h"
66
67 /**
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.
74 *
75 */
76 static void isci_task_refuse(struct isci_host *ihost, struct sas_task *task,
77                              enum service_response response,
78                              enum exec_status status)
79
80 {
81         unsigned long flags;
82
83         /* Normal notification (task_done) */
84         dev_dbg(&ihost->pdev->dev, "%s: task = %p, response=%d, status=%d\n",
85                 __func__, task, response, status);
86
87         spin_lock_irqsave(&task->task_state_lock, flags);
88
89         task->task_status.resp = response;
90         task->task_status.stat = status;
91
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);
100 }
101
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
106
107 static 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 }
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  */
125 int isci_task_execute_task(struct sas_task *task, int num, gfp_t gfp_flags)
126 {
127         struct isci_host *ihost = dev_to_ihost(task->dev);
128         struct isci_remote_device *idev;
129         unsigned long flags;
130         bool io_ready;
131         u16 tag;
132
133         dev_dbg(&ihost->pdev->dev, "%s: num=%d\n", __func__, num);
134
135         for_each_sas_task(num, task) {
136                 enum sci_status status = SCI_FAILURE;
137
138                 spin_lock_irqsave(&ihost->scic_lock, flags);
139                 idev = isci_lookup_device(task->dev);
140                 io_ready = isci_device_io_ready(idev, task);
141                 tag = isci_alloc_tag(ihost);
142                 spin_unlock_irqrestore(&ihost->scic_lock, flags);
143
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);
148
149                 if (!idev) {
150                         isci_task_refuse(ihost, task, SAS_TASK_UNDELIVERED,
151                                          SAS_DEVICE_UNKNOWN);
152                 } else if (!io_ready || tag == SCI_CONTROLLER_INVALID_IO_TAG) {
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);
158                 } else {
159                         /* There is a device and it's ready for I/O. */
160                         spin_lock_irqsave(&task->task_state_lock, flags);
161
162                         if (task->task_state_flags & SAS_TASK_STATE_ABORTED) {
163                                 /* The I/O was aborted. */
164                                 spin_unlock_irqrestore(&task->task_state_lock,
165                                                        flags);
166
167                                 isci_task_refuse(ihost, task,
168                                                  SAS_TASK_UNDELIVERED,
169                                                  SAM_STAT_TASK_ABORTED);
170                         } else {
171                                 task->task_state_flags |= SAS_TASK_AT_INITIATOR;
172                                 spin_unlock_irqrestore(&task->task_state_lock, flags);
173
174                                 /* build and send the request. */
175                                 status = isci_request_execute(ihost, idev, task, tag);
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
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                                         }
205                                 }
206                         }
207                 }
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                 }
216                 isci_put_device(idev);
217         }
218         return 0;
219 }
220
221 static struct isci_request *isci_task_request_build(struct isci_host *ihost,
222                                                     struct isci_remote_device *idev,
223                                                     u16 tag, struct isci_tmf *isci_tmf)
224 {
225         enum sci_status status = SCI_FAILURE;
226         struct isci_request *ireq = NULL;
227         struct domain_device *dev;
228
229         dev_dbg(&ihost->pdev->dev,
230                 "%s: isci_tmf = %p\n", __func__, isci_tmf);
231
232         dev = idev->domain_dev;
233
234         /* do common allocation and init of request object. */
235         ireq = isci_tmf_request_from_tag(ihost, isci_tmf, tag);
236         if (!ireq)
237                 return NULL;
238
239         /* let the core do it's construct. */
240         status = sci_task_request_construct(ihost, idev, tag,
241                                              ireq);
242
243         if (status != SCI_SUCCESS) {
244                 dev_warn(&ihost->pdev->dev,
245                          "%s: sci_task_request_construct failed - "
246                          "status = 0x%x\n",
247                          __func__,
248                          status);
249                 return NULL;
250         }
251
252         /* XXX convert to get this from task->tproto like other drivers */
253         if (dev->dev_type == SAS_END_DEV) {
254                 isci_tmf->proto = SAS_PROTOCOL_SSP;
255                 status = sci_task_request_construct_ssp(ireq);
256                 if (status != SCI_SUCCESS)
257                         return NULL;
258         }
259
260         return ireq;
261 }
262
263 static int isci_task_execute_tmf(struct isci_host *ihost,
264                                  struct isci_remote_device *idev,
265                                  struct isci_tmf *tmf, unsigned long timeout_ms)
266 {
267         DECLARE_COMPLETION_ONSTACK(completion);
268         enum sci_task_status status = SCI_TASK_FAILURE;
269         struct isci_request *ireq;
270         int ret = TMF_RESP_FUNC_FAILED;
271         unsigned long flags;
272         unsigned long timeleft;
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;
281
282         /* sanity check, return TMF_RESP_FUNC_FAILED
283          * if the device is not there and ready.
284          */
285         if (!idev ||
286             (!test_bit(IDEV_IO_READY, &idev->flags) &&
287              !test_bit(IDEV_IO_NCQERROR, &idev->flags))) {
288                 dev_dbg(&ihost->pdev->dev,
289                         "%s: idev = %p not ready (%#lx)\n",
290                         __func__,
291                         idev, idev ? idev->flags : 0);
292                 goto err_tci;
293         } else
294                 dev_dbg(&ihost->pdev->dev,
295                         "%s: idev = %p\n",
296                         __func__, idev);
297
298         /* Assign the pointer to the TMF's completion kernel wait structure. */
299         tmf->complete = &completion;
300         tmf->status = SCI_FAILURE_TIMEOUT;
301
302         ireq = isci_task_request_build(ihost, idev, tag, tmf);
303         if (!ireq)
304                 goto err_tci;
305
306         spin_lock_irqsave(&ihost->scic_lock, flags);
307
308         /* start the TMF io. */
309         status = sci_controller_start_task(ihost, idev, ireq);
310
311         if (status != SCI_TASK_SUCCESS) {
312                 dev_dbg(&ihost->pdev->dev,
313                          "%s: start_io failed - status = 0x%x, request = %p\n",
314                          __func__,
315                          status,
316                          ireq);
317                 spin_unlock_irqrestore(&ihost->scic_lock, flags);
318                 goto err_tci;
319         }
320         spin_unlock_irqrestore(&ihost->scic_lock, flags);
321
322         /* The RNC must be unsuspended before the TMF can get a response. */
323         isci_remote_device_resume_from_abort(ihost, idev);
324
325         /* Wait for the TMF to complete, or a timeout. */
326         timeleft = wait_for_completion_timeout(&completion,
327                                                msecs_to_jiffies(timeout_ms));
328
329         if (timeleft == 0) {
330                 /* The TMF did not complete - this could be because
331                  * of an unplug.  Terminate the TMF request now.
332                  */
333                 isci_remote_device_suspend_terminate(ihost, idev, ireq);
334         }
335
336         isci_print_tmf(ihost, tmf);
337
338         if (tmf->status == SCI_SUCCESS)
339                 ret =  TMF_RESP_FUNC_COMPLETE;
340         else if (tmf->status == SCI_FAILURE_IO_RESPONSE_VALID) {
341                 dev_dbg(&ihost->pdev->dev,
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
349         dev_dbg(&ihost->pdev->dev,
350                 "%s: completed request = %p\n",
351                 __func__,
352                 ireq);
353
354         return ret;
355
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;
362 }
363
364 static void isci_task_build_tmf(struct isci_tmf *tmf,
365                                 enum isci_tmf_function_codes code)
366 {
367         memset(tmf, 0, sizeof(*tmf));
368         tmf->tmf_code = code;
369 }
370
371 static void isci_task_build_abort_task_tmf(struct isci_tmf *tmf,
372                                            enum isci_tmf_function_codes code,
373                                            struct isci_request *old_request)
374 {
375         isci_task_build_tmf(tmf, code);
376         tmf->io_tag = old_request->io_tag;
377 }
378
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  */
386 static 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          */
402         isci_task_build_tmf(&tmf, isci_tmf_ssp_lun_reset);
403
404         #define ISCI_LU_RESET_TIMEOUT_MS 2000 /* 2 second timeout. */
405         ret = isci_task_execute_tmf(isci_host, isci_device, &tmf, ISCI_LU_RESET_TIMEOUT_MS);
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
419 int isci_task_lu_reset(struct domain_device *dev, u8 *lun)
420 {
421         struct isci_host *ihost = dev_to_ihost(dev);
422         struct isci_remote_device *idev;
423         unsigned long flags;
424         int ret;
425
426         spin_lock_irqsave(&ihost->scic_lock, flags);
427         idev = isci_get_device(dev->lldd_dev);
428         spin_unlock_irqrestore(&ihost->scic_lock, flags);
429
430         dev_dbg(&ihost->pdev->dev,
431                 "%s: domain_device=%p, isci_host=%p; isci_device=%p\n",
432                 __func__, dev, ihost, idev);
433
434         if (!idev) {
435                 /* If the device is gone, escalate to I_T_Nexus_Reset. */
436                 dev_dbg(&ihost->pdev->dev, "%s: No dev\n", __func__);
437
438                 ret = TMF_RESP_FUNC_FAILED;
439                 goto out;
440         }
441
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. */
450         if (dev_is_sata(dev)) {
451                 sas_ata_schedule_reset(dev);
452                 ret = TMF_RESP_FUNC_COMPLETE;
453         } else {
454                 /* Send the task management part of the reset. */
455                 ret = isci_task_send_lu_reset_sas(ihost, idev, lun);
456         }
457  out:
458         isci_put_device(idev);
459         return ret;
460 }
461
462
463 /*       int (*lldd_clear_nexus_port)(struct asd_sas_port *); */
464 int isci_task_clear_nexus_port(struct asd_sas_port *port)
465 {
466         return TMF_RESP_FUNC_FAILED;
467 }
468
469
470
471 int isci_task_clear_nexus_ha(struct sas_ha_struct *ha)
472 {
473         return TMF_RESP_FUNC_FAILED;
474 }
475
476 /* Task Management Functions. Must be called from process context.       */
477
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  */
485 int isci_task_abort_task(struct sas_task *task)
486 {
487         struct isci_host *ihost = dev_to_ihost(task->dev);
488         DECLARE_COMPLETION_ONSTACK(aborted_io_completion);
489         struct isci_request       *old_request = NULL;
490         struct isci_remote_device *idev = NULL;
491         struct isci_tmf           tmf;
492         int                       ret = TMF_RESP_FUNC_FAILED;
493         unsigned long             flags;
494
495         /* Get the isci_request reference from the task.  Note that
496          * this check does not depend on the pending request list
497          * in the device, because tasks driving resets may land here
498          * after completion in the core.
499          */
500         spin_lock_irqsave(&ihost->scic_lock, flags);
501         spin_lock(&task->task_state_lock);
502
503         old_request = task->lldd_task;
504
505         /* If task is already done, the request isn't valid */
506         if (!(task->task_state_flags & SAS_TASK_STATE_DONE) &&
507             (task->task_state_flags & SAS_TASK_AT_INITIATOR) &&
508             old_request)
509                 idev = isci_get_device(task->dev->lldd_dev);
510
511         spin_unlock(&task->task_state_lock);
512         spin_unlock_irqrestore(&ihost->scic_lock, flags);
513
514         dev_warn(&ihost->pdev->dev,
515                  "%s: dev = %p, task = %p, old_request == %p\n",
516                  __func__, idev, task, old_request);
517
518         /* Device reset conditions signalled in task_state_flags are the
519          * responsbility of libsas to observe at the start of the error
520          * handler thread.
521          */
522         if (!idev || !old_request) {
523                 /* The request has already completed and there
524                 * is nothing to do here other than to set the task
525                 * done bit, and indicate that the task abort function
526                 * was sucessful.
527                 */
528                 spin_lock_irqsave(&task->task_state_lock, flags);
529                 task->task_state_flags |= SAS_TASK_STATE_DONE;
530                 task->task_state_flags &= ~(SAS_TASK_AT_INITIATOR |
531                                             SAS_TASK_STATE_PENDING);
532                 spin_unlock_irqrestore(&task->task_state_lock, flags);
533
534                 ret = TMF_RESP_FUNC_COMPLETE;
535
536                 dev_warn(&ihost->pdev->dev,
537                          "%s: abort task not needed for %p\n",
538                          __func__, task);
539                 goto out;
540         }
541         /* Suspend the RNC, kill the TC */
542         if (isci_remote_device_suspend_terminate(ihost, idev, old_request)
543             != SCI_SUCCESS) {
544                 dev_warn(&ihost->pdev->dev,
545                          "%s: isci_remote_device_reset_terminate(dev=%p, "
546                                  "req=%p, task=%p) failed\n",
547                          __func__, idev, old_request, task);
548                 ret = TMF_RESP_FUNC_FAILED;
549                 goto out;
550         }
551         spin_lock_irqsave(&ihost->scic_lock, flags);
552
553         if (task->task_proto == SAS_PROTOCOL_SMP ||
554             sas_protocol_ata(task->task_proto) ||
555             test_bit(IREQ_COMPLETE_IN_TARGET, &old_request->flags)) {
556
557                 spin_unlock_irqrestore(&ihost->scic_lock, flags);
558
559                 /* No task to send, so explicitly resume the device here */
560                 isci_remote_device_resume_from_abort(ihost, idev);
561
562                 dev_warn(&ihost->pdev->dev,
563                          "%s: %s request"
564                                  " or complete_in_target (%d), thus no TMF\n",
565                          __func__,
566                          ((task->task_proto == SAS_PROTOCOL_SMP)
567                           ? "SMP"
568                           : (sas_protocol_ata(task->task_proto)
569                                 ? "SATA/STP"
570                                 : "<other>")
571                           ),
572                          test_bit(IREQ_COMPLETE_IN_TARGET,
573                                   &old_request->flags));
574
575                 spin_lock_irqsave(&task->task_state_lock, flags);
576                 task->task_state_flags &= ~(SAS_TASK_AT_INITIATOR |
577                                             SAS_TASK_STATE_PENDING);
578                 task->task_state_flags |= SAS_TASK_STATE_DONE;
579                 spin_unlock_irqrestore(&task->task_state_lock, flags);
580
581                 ret = TMF_RESP_FUNC_COMPLETE;
582         } else {
583                 /* Fill in the tmf stucture */
584                 isci_task_build_abort_task_tmf(&tmf, isci_tmf_ssp_task_abort,
585                                                old_request);
586
587                 spin_unlock_irqrestore(&ihost->scic_lock, flags);
588
589                 /* Send the task management request. */
590                 #define ISCI_ABORT_TASK_TIMEOUT_MS 500 /* 1/2 second timeout */
591                 ret = isci_task_execute_tmf(ihost, idev, &tmf,
592                                             ISCI_ABORT_TASK_TIMEOUT_MS);
593         }
594 out:
595         dev_warn(&ihost->pdev->dev,
596                  "%s: Done; dev = %p, task = %p , old_request == %p\n",
597                  __func__, idev, task, old_request);
598         isci_put_device(idev);
599         return ret;
600 }
601
602 /**
603  * isci_task_abort_task_set() - This function is one of the SAS Domain Template
604  *    functions. This is one of the Task Management functoins called by libsas,
605  *    to abort all task for the given lun.
606  * @d_device: This parameter specifies the domain device associated with this
607  *    request.
608  * @lun: This parameter specifies the lun associated with this request.
609  *
610  * status, zero indicates success.
611  */
612 int isci_task_abort_task_set(
613         struct domain_device *d_device,
614         u8 *lun)
615 {
616         return TMF_RESP_FUNC_FAILED;
617 }
618
619
620 /**
621  * isci_task_clear_aca() - This function is one of the SAS Domain Template
622  *    functions. This is one of the Task Management functoins called by libsas.
623  * @d_device: This parameter specifies the domain device associated with this
624  *    request.
625  * @lun: This parameter specifies the lun        associated with this request.
626  *
627  * status, zero indicates success.
628  */
629 int isci_task_clear_aca(
630         struct domain_device *d_device,
631         u8 *lun)
632 {
633         return TMF_RESP_FUNC_FAILED;
634 }
635
636
637
638 /**
639  * isci_task_clear_task_set() - This function is one of the SAS Domain Template
640  *    functions. This is one of the Task Management functoins called by libsas.
641  * @d_device: This parameter specifies the domain device associated with this
642  *    request.
643  * @lun: This parameter specifies the lun        associated with this request.
644  *
645  * status, zero indicates success.
646  */
647 int isci_task_clear_task_set(
648         struct domain_device *d_device,
649         u8 *lun)
650 {
651         return TMF_RESP_FUNC_FAILED;
652 }
653
654
655 /**
656  * isci_task_query_task() - This function is implemented to cause libsas to
657  *    correctly escalate the failed abort to a LUN or target reset (this is
658  *    because sas_scsi_find_task libsas function does not correctly interpret
659  *    all return codes from the abort task call).  When TMF_RESP_FUNC_SUCC is
660  *    returned, libsas turns this into a LUN reset; when FUNC_FAILED is
661  *    returned, libsas will turn this into a target reset
662  * @task: This parameter specifies the sas task being queried.
663  * @lun: This parameter specifies the lun associated with this request.
664  *
665  * status, zero indicates success.
666  */
667 int isci_task_query_task(
668         struct sas_task *task)
669 {
670         /* See if there is a pending device reset for this device. */
671         if (task->task_state_flags & SAS_TASK_NEED_DEV_RESET)
672                 return TMF_RESP_FUNC_FAILED;
673         else
674                 return TMF_RESP_FUNC_SUCC;
675 }
676
677 /*
678  * isci_task_request_complete() - This function is called by the sci core when
679  *    an task request completes.
680  * @ihost: This parameter specifies the ISCI host object
681  * @ireq: This parameter is the completed isci_request object.
682  * @completion_status: This parameter specifies the completion status from the
683  *    sci core.
684  *
685  * none.
686  */
687 void
688 isci_task_request_complete(struct isci_host *ihost,
689                            struct isci_request *ireq,
690                            enum sci_task_status completion_status)
691 {
692         struct isci_tmf *tmf = isci_request_access_tmf(ireq);
693         struct completion *tmf_complete = NULL;
694
695         dev_dbg(&ihost->pdev->dev,
696                 "%s: request = %p, status=%d\n",
697                 __func__, ireq, completion_status);
698
699         set_bit(IREQ_COMPLETE_IN_TARGET, &ireq->flags);
700
701         if (tmf) {
702                 tmf->status = completion_status;
703
704                 if (tmf->proto == SAS_PROTOCOL_SSP) {
705                         memcpy(&tmf->resp.resp_iu,
706                                &ireq->ssp.rsp,
707                                SSP_RESP_IU_MAX_SIZE);
708                 } else if (tmf->proto == SAS_PROTOCOL_SATA) {
709                         memcpy(&tmf->resp.d2h_fis,
710                                &ireq->stp.rsp,
711                                sizeof(struct dev_to_host_fis));
712                 }
713                 /* PRINT_TMF( ((struct isci_tmf *)request->task)); */
714                 tmf_complete = tmf->complete;
715         }
716         sci_controller_complete_io(ihost, ireq->target_device, ireq);
717         /* set the 'terminated' flag handle to make sure it cannot be terminated
718          *  or completed again.
719          */
720         set_bit(IREQ_TERMINATED, &ireq->flags);
721
722         if (!test_bit(IREQ_NO_AUTO_FREE_TAG, &ireq->flags))
723                 isci_free_tag(ihost, ireq->io_tag);
724
725         /* The task management part completes last. */
726         if (tmf_complete)
727                 complete(tmf_complete);
728 }
729
730 static int isci_reset_device(struct isci_host *ihost,
731                              struct domain_device *dev,
732                              struct isci_remote_device *idev)
733 {
734         int rc = TMF_RESP_FUNC_COMPLETE, reset_stat;
735         struct sas_phy *phy = sas_get_local_phy(dev);
736         struct isci_port *iport = dev->port->lldd_port;
737
738         dev_dbg(&ihost->pdev->dev, "%s: idev %p\n", __func__, idev);
739
740         /* Suspend the RNC, terminate all outstanding TCs. */
741         if (isci_remote_device_suspend_terminate(ihost, idev, NULL)
742             != SCI_SUCCESS) {
743                 rc = TMF_RESP_FUNC_FAILED;
744                 goto out;
745         }
746         /* Note that since the termination for outstanding requests succeeded,
747          * this function will return success.  This is because the resets will
748          * only fail if the device has been removed (ie. hotplug), and the
749          * primary duty of this function is to cleanup tasks, so that is the
750          * relevant status.
751          */
752
753         if (scsi_is_sas_phy_local(phy)) {
754                 struct isci_phy *iphy = &ihost->phys[phy->number];
755
756                 reset_stat = isci_port_perform_hard_reset(ihost, iport, iphy);
757         } else
758                 reset_stat = sas_phy_reset(phy, !dev_is_sata(dev));
759
760         /* Explicitly resume the RNC here, since there was no task sent. */
761         isci_remote_device_resume_from_abort(ihost, idev);
762
763         dev_dbg(&ihost->pdev->dev, "%s: idev %p complete, reset_stat=%d.\n",
764                 __func__, idev, reset_stat);
765  out:
766         sas_put_local_phy(phy);
767         return rc;
768 }
769
770 int isci_task_I_T_nexus_reset(struct domain_device *dev)
771 {
772         struct isci_host *ihost = dev_to_ihost(dev);
773         struct isci_remote_device *idev;
774         unsigned long flags;
775         int ret;
776
777         spin_lock_irqsave(&ihost->scic_lock, flags);
778         idev = isci_get_device(dev->lldd_dev);
779         spin_unlock_irqrestore(&ihost->scic_lock, flags);
780
781         if (!idev) {
782                 /* XXX: need to cleanup any ireqs targeting this
783                  * domain_device
784                  */
785                 ret = TMF_RESP_FUNC_COMPLETE;
786                 goto out;
787         }
788
789         ret = isci_reset_device(ihost, dev, idev);
790  out:
791         isci_put_device(idev);
792         return ret;
793 }