Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * This file is provided under a dual BSD/GPLv2 license.  When using or
0003  * redistributing this file, you may do so under either license.
0004  *
0005  * GPL LICENSE SUMMARY
0006  *
0007  * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
0008  *
0009  * This program is free software; you can redistribute it and/or modify
0010  * it under the terms of version 2 of the GNU General Public License as
0011  * published by the Free Software Foundation.
0012  *
0013  * This program is distributed in the hope that it will be useful, but
0014  * WITHOUT ANY WARRANTY; without even the implied warranty of
0015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0016  * General Public License for more details.
0017  *
0018  * You should have received a copy of the GNU General Public License
0019  * along with this program; if not, write to the Free Software
0020  * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
0021  * The full GNU General Public License is included in this distribution
0022  * in the file called LICENSE.GPL.
0023  *
0024  * BSD LICENSE
0025  *
0026  * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
0027  * All rights reserved.
0028  *
0029  * Redistribution and use in source and binary forms, with or without
0030  * modification, are permitted provided that the following conditions
0031  * are met:
0032  *
0033  *   * Redistributions of source code must retain the above copyright
0034  *     notice, this list of conditions and the following disclaimer.
0035  *   * Redistributions in binary form must reproduce the above copyright
0036  *     notice, this list of conditions and the following disclaimer in
0037  *     the documentation and/or other materials provided with the
0038  *     distribution.
0039  *   * Neither the name of Intel Corporation nor the names of its
0040  *     contributors may be used to endorse or promote products derived
0041  *     from this software without specific prior written permission.
0042  *
0043  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
0044  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
0045  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
0046  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
0047  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
0048  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
0049  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
0050  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
0051  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0052  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
0053  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0054  */
0055 
0056 #include <linux/completion.h>
0057 #include <linux/irqflags.h>
0058 #include "sas.h"
0059 #include <scsi/libsas.h>
0060 #include "remote_device.h"
0061 #include "remote_node_context.h"
0062 #include "isci.h"
0063 #include "request.h"
0064 #include "task.h"
0065 #include "host.h"
0066 
0067 /**
0068 * isci_task_refuse() - complete the request to the upper layer driver in
0069 *     the case where an I/O needs to be completed back in the submit path.
0070 * @ihost: host on which the the request was queued
0071 * @task: request to complete
0072 * @response: response code for the completed task.
0073 * @status: status code for the completed task.
0074 *
0075 */
0076 static void isci_task_refuse(struct isci_host *ihost, struct sas_task *task,
0077                  enum service_response response,
0078                  enum exec_status status)
0079 
0080 {
0081     unsigned long flags;
0082 
0083     /* Normal notification (task_done) */
0084     dev_dbg(&ihost->pdev->dev, "%s: task = %p, response=%d, status=%d\n",
0085         __func__, task, response, status);
0086 
0087     spin_lock_irqsave(&task->task_state_lock, flags);
0088 
0089     task->task_status.resp = response;
0090     task->task_status.stat = status;
0091 
0092     /* Normal notification (task_done) */
0093     task->task_state_flags |= SAS_TASK_STATE_DONE;
0094     task->task_state_flags &= ~SAS_TASK_STATE_PENDING;
0095     task->lldd_task = NULL;
0096     spin_unlock_irqrestore(&task->task_state_lock, flags);
0097 
0098     task->task_done(task);
0099 }
0100 
0101 #define for_each_sas_task(num, task) \
0102     for (; num > 0; num--,\
0103          task = list_entry(task->list.next, struct sas_task, list))
0104 
0105 
0106 static inline int isci_device_io_ready(struct isci_remote_device *idev,
0107                        struct sas_task *task)
0108 {
0109     return idev ? test_bit(IDEV_IO_READY, &idev->flags) ||
0110               (test_bit(IDEV_IO_NCQERROR, &idev->flags) &&
0111                isci_task_is_ncq_recovery(task))
0112             : 0;
0113 }
0114 /**
0115  * isci_task_execute_task() - This function is one of the SAS Domain Template
0116  *    functions. This function is called by libsas to send a task down to
0117  *    hardware.
0118  * @task: This parameter specifies the SAS task to send.
0119  * @gfp_flags: This parameter specifies the context of this call.
0120  *
0121  * status, zero indicates success.
0122  */
0123 int isci_task_execute_task(struct sas_task *task, gfp_t gfp_flags)
0124 {
0125     struct isci_host *ihost = dev_to_ihost(task->dev);
0126     struct isci_remote_device *idev;
0127     unsigned long flags;
0128     enum sci_status status = SCI_FAILURE;
0129     bool io_ready;
0130     u16 tag;
0131 
0132     spin_lock_irqsave(&ihost->scic_lock, flags);
0133     idev = isci_lookup_device(task->dev);
0134     io_ready = isci_device_io_ready(idev, task);
0135     tag = isci_alloc_tag(ihost);
0136     spin_unlock_irqrestore(&ihost->scic_lock, flags);
0137 
0138     dev_dbg(&ihost->pdev->dev,
0139         "task: %p, dev: %p idev: %p:%#lx cmd = %p\n",
0140         task, task->dev, idev, idev ? idev->flags : 0,
0141         task->uldd_task);
0142 
0143     if (!idev) {
0144         isci_task_refuse(ihost, task, SAS_TASK_UNDELIVERED,
0145                  SAS_DEVICE_UNKNOWN);
0146     } else if (!io_ready || tag == SCI_CONTROLLER_INVALID_IO_TAG) {
0147         /* Indicate QUEUE_FULL so that the scsi midlayer
0148          * retries.
0149           */
0150         isci_task_refuse(ihost, task, SAS_TASK_COMPLETE,
0151                  SAS_QUEUE_FULL);
0152     } else {
0153         /* There is a device and it's ready for I/O. */
0154         spin_lock_irqsave(&task->task_state_lock, flags);
0155 
0156         if (task->task_state_flags & SAS_TASK_STATE_ABORTED) {
0157             /* The I/O was aborted. */
0158             spin_unlock_irqrestore(&task->task_state_lock, flags);
0159 
0160             isci_task_refuse(ihost, task,
0161                      SAS_TASK_UNDELIVERED,
0162                      SAS_SAM_STAT_TASK_ABORTED);
0163         } else {
0164             struct isci_request *ireq;
0165 
0166             /* do common allocation and init of request object. */
0167             ireq = isci_io_request_from_tag(ihost, task, tag);
0168             spin_unlock_irqrestore(&task->task_state_lock, flags);
0169 
0170             /* build and send the request. */
0171             /* do common allocation and init of request object. */
0172             status = isci_request_execute(ihost, idev, task, ireq);
0173 
0174             if (status != SCI_SUCCESS) {
0175                 if (test_bit(IDEV_GONE, &idev->flags)) {
0176                     /* Indicate that the device
0177                      * is gone.
0178                      */
0179                     isci_task_refuse(ihost, task,
0180                         SAS_TASK_UNDELIVERED,
0181                         SAS_DEVICE_UNKNOWN);
0182                 } else {
0183                     /* Indicate QUEUE_FULL so that
0184                      * the scsi midlayer retries.
0185                      * If the request failed for
0186                      * remote device reasons, it
0187                      * gets returned as
0188                      * SAS_TASK_UNDELIVERED next
0189                      * time through.
0190                      */
0191                     isci_task_refuse(ihost, task,
0192                         SAS_TASK_COMPLETE,
0193                         SAS_QUEUE_FULL);
0194                 }
0195             }
0196         }
0197     }
0198 
0199     if (status != SCI_SUCCESS && tag != SCI_CONTROLLER_INVALID_IO_TAG) {
0200         spin_lock_irqsave(&ihost->scic_lock, flags);
0201         /* command never hit the device, so just free
0202          * the tci and skip the sequence increment
0203          */
0204         isci_tci_free(ihost, ISCI_TAG_TCI(tag));
0205         spin_unlock_irqrestore(&ihost->scic_lock, flags);
0206     }
0207 
0208     isci_put_device(idev);
0209     return 0;
0210 }
0211 
0212 static struct isci_request *isci_task_request_build(struct isci_host *ihost,
0213                             struct isci_remote_device *idev,
0214                             u16 tag, struct isci_tmf *isci_tmf)
0215 {
0216     enum sci_status status = SCI_FAILURE;
0217     struct isci_request *ireq = NULL;
0218     struct domain_device *dev;
0219 
0220     dev_dbg(&ihost->pdev->dev,
0221         "%s: isci_tmf = %p\n", __func__, isci_tmf);
0222 
0223     dev = idev->domain_dev;
0224 
0225     /* do common allocation and init of request object. */
0226     ireq = isci_tmf_request_from_tag(ihost, isci_tmf, tag);
0227     if (!ireq)
0228         return NULL;
0229 
0230     /* let the core do it's construct. */
0231     status = sci_task_request_construct(ihost, idev, tag,
0232                          ireq);
0233 
0234     if (status != SCI_SUCCESS) {
0235         dev_warn(&ihost->pdev->dev,
0236              "%s: sci_task_request_construct failed - "
0237              "status = 0x%x\n",
0238              __func__,
0239              status);
0240         return NULL;
0241     }
0242 
0243     /* XXX convert to get this from task->tproto like other drivers */
0244     if (dev->dev_type == SAS_END_DEVICE) {
0245         isci_tmf->proto = SAS_PROTOCOL_SSP;
0246         status = sci_task_request_construct_ssp(ireq);
0247         if (status != SCI_SUCCESS)
0248             return NULL;
0249     }
0250 
0251     return ireq;
0252 }
0253 
0254 static int isci_task_execute_tmf(struct isci_host *ihost,
0255                  struct isci_remote_device *idev,
0256                  struct isci_tmf *tmf, unsigned long timeout_ms)
0257 {
0258     DECLARE_COMPLETION_ONSTACK(completion);
0259     enum sci_status status = SCI_FAILURE;
0260     struct isci_request *ireq;
0261     int ret = TMF_RESP_FUNC_FAILED;
0262     unsigned long flags;
0263     unsigned long timeleft;
0264     u16 tag;
0265 
0266     spin_lock_irqsave(&ihost->scic_lock, flags);
0267     tag = isci_alloc_tag(ihost);
0268     spin_unlock_irqrestore(&ihost->scic_lock, flags);
0269 
0270     if (tag == SCI_CONTROLLER_INVALID_IO_TAG)
0271         return ret;
0272 
0273     /* sanity check, return TMF_RESP_FUNC_FAILED
0274      * if the device is not there and ready.
0275      */
0276     if (!idev ||
0277         (!test_bit(IDEV_IO_READY, &idev->flags) &&
0278          !test_bit(IDEV_IO_NCQERROR, &idev->flags))) {
0279         dev_dbg(&ihost->pdev->dev,
0280             "%s: idev = %p not ready (%#lx)\n",
0281             __func__,
0282             idev, idev ? idev->flags : 0);
0283         goto err_tci;
0284     } else
0285         dev_dbg(&ihost->pdev->dev,
0286             "%s: idev = %p\n",
0287             __func__, idev);
0288 
0289     /* Assign the pointer to the TMF's completion kernel wait structure. */
0290     tmf->complete = &completion;
0291     tmf->status = SCI_FAILURE_TIMEOUT;
0292 
0293     ireq = isci_task_request_build(ihost, idev, tag, tmf);
0294     if (!ireq)
0295         goto err_tci;
0296 
0297     spin_lock_irqsave(&ihost->scic_lock, flags);
0298 
0299     /* start the TMF io. */
0300     status = sci_controller_start_task(ihost, idev, ireq);
0301 
0302     if (status != SCI_SUCCESS) {
0303         dev_dbg(&ihost->pdev->dev,
0304              "%s: start_io failed - status = 0x%x, request = %p\n",
0305              __func__,
0306              status,
0307              ireq);
0308         spin_unlock_irqrestore(&ihost->scic_lock, flags);
0309         goto err_tci;
0310     }
0311     spin_unlock_irqrestore(&ihost->scic_lock, flags);
0312 
0313     /* The RNC must be unsuspended before the TMF can get a response. */
0314     isci_remote_device_resume_from_abort(ihost, idev);
0315 
0316     /* Wait for the TMF to complete, or a timeout. */
0317     timeleft = wait_for_completion_timeout(&completion,
0318                            msecs_to_jiffies(timeout_ms));
0319 
0320     if (timeleft == 0) {
0321         /* The TMF did not complete - this could be because
0322          * of an unplug.  Terminate the TMF request now.
0323          */
0324         isci_remote_device_suspend_terminate(ihost, idev, ireq);
0325     }
0326 
0327     isci_print_tmf(ihost, tmf);
0328 
0329     if (tmf->status == SCI_SUCCESS)
0330         ret =  TMF_RESP_FUNC_COMPLETE;
0331     else if (tmf->status == SCI_FAILURE_IO_RESPONSE_VALID) {
0332         dev_dbg(&ihost->pdev->dev,
0333             "%s: tmf.status == "
0334             "SCI_FAILURE_IO_RESPONSE_VALID\n",
0335             __func__);
0336         ret =  TMF_RESP_FUNC_COMPLETE;
0337     }
0338     /* Else - leave the default "failed" status alone. */
0339 
0340     dev_dbg(&ihost->pdev->dev,
0341         "%s: completed request = %p\n",
0342         __func__,
0343         ireq);
0344 
0345     return ret;
0346 
0347  err_tci:
0348     spin_lock_irqsave(&ihost->scic_lock, flags);
0349     isci_tci_free(ihost, ISCI_TAG_TCI(tag));
0350     spin_unlock_irqrestore(&ihost->scic_lock, flags);
0351 
0352     return ret;
0353 }
0354 
0355 static void isci_task_build_tmf(struct isci_tmf *tmf,
0356                 enum isci_tmf_function_codes code)
0357 {
0358     memset(tmf, 0, sizeof(*tmf));
0359     tmf->tmf_code = code;
0360 }
0361 
0362 static void isci_task_build_abort_task_tmf(struct isci_tmf *tmf,
0363                        enum isci_tmf_function_codes code,
0364                        struct isci_request *old_request)
0365 {
0366     isci_task_build_tmf(tmf, code);
0367     tmf->io_tag = old_request->io_tag;
0368 }
0369 
0370 /*
0371  * isci_task_send_lu_reset_sas() - This function is called by of the SAS Domain
0372  *    Template functions.
0373  * @lun: This parameter specifies the lun to be reset.
0374  *
0375  * status, zero indicates success.
0376  */
0377 static int isci_task_send_lu_reset_sas(
0378     struct isci_host *isci_host,
0379     struct isci_remote_device *isci_device,
0380     u8 *lun)
0381 {
0382     struct isci_tmf tmf;
0383     int ret = TMF_RESP_FUNC_FAILED;
0384 
0385     dev_dbg(&isci_host->pdev->dev,
0386         "%s: isci_host = %p, isci_device = %p\n",
0387         __func__, isci_host, isci_device);
0388     /* Send the LUN reset to the target.  By the time the call returns,
0389      * the TMF has fully exected in the target (in which case the return
0390      * value is "TMF_RESP_FUNC_COMPLETE", or the request timed-out (or
0391      * was otherwise unable to be executed ("TMF_RESP_FUNC_FAILED").
0392      */
0393     isci_task_build_tmf(&tmf, isci_tmf_ssp_lun_reset);
0394 
0395     #define ISCI_LU_RESET_TIMEOUT_MS 2000 /* 2 second timeout. */
0396     ret = isci_task_execute_tmf(isci_host, isci_device, &tmf, ISCI_LU_RESET_TIMEOUT_MS);
0397 
0398     if (ret == TMF_RESP_FUNC_COMPLETE)
0399         dev_dbg(&isci_host->pdev->dev,
0400             "%s: %p: TMF_LU_RESET passed\n",
0401             __func__, isci_device);
0402     else
0403         dev_dbg(&isci_host->pdev->dev,
0404             "%s: %p: TMF_LU_RESET failed (%x)\n",
0405             __func__, isci_device, ret);
0406 
0407     return ret;
0408 }
0409 
0410 int isci_task_lu_reset(struct domain_device *dev, u8 *lun)
0411 {
0412     struct isci_host *ihost = dev_to_ihost(dev);
0413     struct isci_remote_device *idev;
0414     unsigned long flags;
0415     int ret = TMF_RESP_FUNC_COMPLETE;
0416 
0417     spin_lock_irqsave(&ihost->scic_lock, flags);
0418     idev = isci_get_device(dev->lldd_dev);
0419     spin_unlock_irqrestore(&ihost->scic_lock, flags);
0420 
0421     dev_dbg(&ihost->pdev->dev,
0422         "%s: domain_device=%p, isci_host=%p; isci_device=%p\n",
0423         __func__, dev, ihost, idev);
0424 
0425     if (!idev) {
0426         /* If the device is gone, escalate to I_T_Nexus_Reset. */
0427         dev_dbg(&ihost->pdev->dev, "%s: No dev\n", __func__);
0428 
0429         ret = TMF_RESP_FUNC_FAILED;
0430         goto out;
0431     }
0432 
0433     /* Suspend the RNC, kill all TCs */
0434     if (isci_remote_device_suspend_terminate(ihost, idev, NULL)
0435         != SCI_SUCCESS) {
0436         /* The suspend/terminate only fails if isci_get_device fails */
0437         ret = TMF_RESP_FUNC_FAILED;
0438         goto out;
0439     }
0440     /* All pending I/Os have been terminated and cleaned up. */
0441     if (!test_bit(IDEV_GONE, &idev->flags)) {
0442         if (dev_is_sata(dev))
0443             sas_ata_schedule_reset(dev);
0444         else
0445             /* Send the task management part of the reset. */
0446             ret = isci_task_send_lu_reset_sas(ihost, idev, lun);
0447     }
0448  out:
0449     isci_put_device(idev);
0450     return ret;
0451 }
0452 
0453 
0454 /*   int (*lldd_clear_nexus_port)(struct asd_sas_port *); */
0455 int isci_task_clear_nexus_port(struct asd_sas_port *port)
0456 {
0457     return TMF_RESP_FUNC_FAILED;
0458 }
0459 
0460 
0461 
0462 int isci_task_clear_nexus_ha(struct sas_ha_struct *ha)
0463 {
0464     return TMF_RESP_FUNC_FAILED;
0465 }
0466 
0467 /* Task Management Functions. Must be called from process context.   */
0468 
0469 /**
0470  * isci_task_abort_task() - This function is one of the SAS Domain Template
0471  *    functions. This function is called by libsas to abort a specified task.
0472  * @task: This parameter specifies the SAS task to abort.
0473  *
0474  * status, zero indicates success.
0475  */
0476 int isci_task_abort_task(struct sas_task *task)
0477 {
0478     struct isci_host *ihost = dev_to_ihost(task->dev);
0479     DECLARE_COMPLETION_ONSTACK(aborted_io_completion);
0480     struct isci_request       *old_request = NULL;
0481     struct isci_remote_device *idev = NULL;
0482     struct isci_tmf           tmf;
0483     int                       ret = TMF_RESP_FUNC_FAILED;
0484     unsigned long             flags;
0485     int                       target_done_already = 0;
0486 
0487     /* Get the isci_request reference from the task.  Note that
0488      * this check does not depend on the pending request list
0489      * in the device, because tasks driving resets may land here
0490      * after completion in the core.
0491      */
0492     spin_lock_irqsave(&ihost->scic_lock, flags);
0493     spin_lock(&task->task_state_lock);
0494 
0495     old_request = task->lldd_task;
0496 
0497     /* If task is already done, the request isn't valid */
0498     if (!(task->task_state_flags & SAS_TASK_STATE_DONE) &&
0499         old_request) {
0500         idev = isci_get_device(task->dev->lldd_dev);
0501         target_done_already = test_bit(IREQ_COMPLETE_IN_TARGET,
0502                            &old_request->flags);
0503     }
0504     spin_unlock(&task->task_state_lock);
0505     spin_unlock_irqrestore(&ihost->scic_lock, flags);
0506 
0507     dev_warn(&ihost->pdev->dev,
0508          "%s: dev = %p (%s%s), task = %p, old_request == %p\n",
0509          __func__, idev,
0510          (dev_is_sata(task->dev) ? "STP/SATA"
0511                      : ((dev_is_expander(task->dev->dev_type))
0512                         ? "SMP"
0513                         : "SSP")),
0514          ((idev) ? ((test_bit(IDEV_GONE, &idev->flags))
0515                ? " IDEV_GONE"
0516                : "")
0517              : " <NULL>"),
0518          task, old_request);
0519 
0520     /* Device reset conditions signalled in task_state_flags are the
0521      * responsbility of libsas to observe at the start of the error
0522      * handler thread.
0523      */
0524     if (!idev || !old_request) {
0525         /* The request has already completed and there
0526         * is nothing to do here other than to set the task
0527         * done bit, and indicate that the task abort function
0528         * was successful.
0529         */
0530         spin_lock_irqsave(&task->task_state_lock, flags);
0531         task->task_state_flags |= SAS_TASK_STATE_DONE;
0532         task->task_state_flags &= ~SAS_TASK_STATE_PENDING;
0533         spin_unlock_irqrestore(&task->task_state_lock, flags);
0534 
0535         ret = TMF_RESP_FUNC_COMPLETE;
0536 
0537         dev_warn(&ihost->pdev->dev,
0538              "%s: abort task not needed for %p\n",
0539              __func__, task);
0540         goto out;
0541     }
0542     /* Suspend the RNC, kill the TC */
0543     if (isci_remote_device_suspend_terminate(ihost, idev, old_request)
0544         != SCI_SUCCESS) {
0545         dev_warn(&ihost->pdev->dev,
0546              "%s: isci_remote_device_reset_terminate(dev=%p, "
0547                  "req=%p, task=%p) failed\n",
0548              __func__, idev, old_request, task);
0549         ret = TMF_RESP_FUNC_FAILED;
0550         goto out;
0551     }
0552     spin_lock_irqsave(&ihost->scic_lock, flags);
0553 
0554     if (task->task_proto == SAS_PROTOCOL_SMP ||
0555         sas_protocol_ata(task->task_proto) ||
0556         target_done_already ||
0557         test_bit(IDEV_GONE, &idev->flags)) {
0558 
0559         spin_unlock_irqrestore(&ihost->scic_lock, flags);
0560 
0561         /* No task to send, so explicitly resume the device here */
0562         isci_remote_device_resume_from_abort(ihost, idev);
0563 
0564         dev_warn(&ihost->pdev->dev,
0565              "%s: %s request"
0566                  " or complete_in_target (%d), "
0567                  "or IDEV_GONE (%d), thus no TMF\n",
0568              __func__,
0569              ((task->task_proto == SAS_PROTOCOL_SMP)
0570               ? "SMP"
0571               : (sas_protocol_ata(task->task_proto)
0572                 ? "SATA/STP"
0573                 : "<other>")
0574               ),
0575              test_bit(IREQ_COMPLETE_IN_TARGET,
0576                   &old_request->flags),
0577              test_bit(IDEV_GONE, &idev->flags));
0578 
0579         spin_lock_irqsave(&task->task_state_lock, flags);
0580         task->task_state_flags &= ~SAS_TASK_STATE_PENDING;
0581         task->task_state_flags |= SAS_TASK_STATE_DONE;
0582         spin_unlock_irqrestore(&task->task_state_lock, flags);
0583 
0584         ret = TMF_RESP_FUNC_COMPLETE;
0585     } else {
0586         /* Fill in the tmf structure */
0587         isci_task_build_abort_task_tmf(&tmf, isci_tmf_ssp_task_abort,
0588                            old_request);
0589 
0590         spin_unlock_irqrestore(&ihost->scic_lock, flags);
0591 
0592         /* Send the task management request. */
0593         #define ISCI_ABORT_TASK_TIMEOUT_MS 500 /* 1/2 second timeout */
0594         ret = isci_task_execute_tmf(ihost, idev, &tmf,
0595                         ISCI_ABORT_TASK_TIMEOUT_MS);
0596     }
0597 out:
0598     dev_warn(&ihost->pdev->dev,
0599          "%s: Done; dev = %p, task = %p , old_request == %p\n",
0600          __func__, idev, task, old_request);
0601     isci_put_device(idev);
0602     return ret;
0603 }
0604 
0605 /**
0606  * isci_task_abort_task_set() - This function is one of the SAS Domain Template
0607  *    functions. This is one of the Task Management functoins called by libsas,
0608  *    to abort all task for the given lun.
0609  * @d_device: This parameter specifies the domain device associated with this
0610  *    request.
0611  * @lun: This parameter specifies the lun associated with this request.
0612  *
0613  * status, zero indicates success.
0614  */
0615 int isci_task_abort_task_set(
0616     struct domain_device *d_device,
0617     u8 *lun)
0618 {
0619     return TMF_RESP_FUNC_FAILED;
0620 }
0621 
0622 
0623 /**
0624  * isci_task_clear_task_set() - This function is one of the SAS Domain Template
0625  *    functions. This is one of the Task Management functoins called by libsas.
0626  * @d_device: This parameter specifies the domain device associated with this
0627  *    request.
0628  * @lun: This parameter specifies the lun    associated with this request.
0629  *
0630  * status, zero indicates success.
0631  */
0632 int isci_task_clear_task_set(
0633     struct domain_device *d_device,
0634     u8 *lun)
0635 {
0636     return TMF_RESP_FUNC_FAILED;
0637 }
0638 
0639 
0640 /**
0641  * isci_task_query_task() - This function is implemented to cause libsas to
0642  *    correctly escalate the failed abort to a LUN or target reset (this is
0643  *    because sas_scsi_find_task libsas function does not correctly interpret
0644  *    all return codes from the abort task call).  When TMF_RESP_FUNC_SUCC is
0645  *    returned, libsas turns this into a LUN reset; when FUNC_FAILED is
0646  *    returned, libsas will turn this into a target reset
0647  * @task: This parameter specifies the sas task being queried.
0648  *
0649  * status, zero indicates success.
0650  */
0651 int isci_task_query_task(
0652     struct sas_task *task)
0653 {
0654     /* See if there is a pending device reset for this device. */
0655     if (task->task_state_flags & SAS_TASK_NEED_DEV_RESET)
0656         return TMF_RESP_FUNC_FAILED;
0657     else
0658         return TMF_RESP_FUNC_SUCC;
0659 }
0660 
0661 /*
0662  * isci_task_request_complete() - This function is called by the sci core when
0663  *    an task request completes.
0664  * @ihost: This parameter specifies the ISCI host object
0665  * @ireq: This parameter is the completed isci_request object.
0666  * @completion_status: This parameter specifies the completion status from the
0667  *    sci core.
0668  *
0669  * none.
0670  */
0671 void
0672 isci_task_request_complete(struct isci_host *ihost,
0673                struct isci_request *ireq,
0674                enum sci_task_status completion_status)
0675 {
0676     struct isci_tmf *tmf = isci_request_access_tmf(ireq);
0677     struct completion *tmf_complete = NULL;
0678 
0679     dev_dbg(&ihost->pdev->dev,
0680         "%s: request = %p, status=%d\n",
0681         __func__, ireq, completion_status);
0682 
0683     set_bit(IREQ_COMPLETE_IN_TARGET, &ireq->flags);
0684 
0685     if (tmf) {
0686         tmf->status = completion_status;
0687 
0688         if (tmf->proto == SAS_PROTOCOL_SSP) {
0689             memcpy(tmf->resp.rsp_buf,
0690                    ireq->ssp.rsp_buf,
0691                    SSP_RESP_IU_MAX_SIZE);
0692         } else if (tmf->proto == SAS_PROTOCOL_SATA) {
0693             memcpy(&tmf->resp.d2h_fis,
0694                    &ireq->stp.rsp,
0695                    sizeof(struct dev_to_host_fis));
0696         }
0697         /* PRINT_TMF( ((struct isci_tmf *)request->task)); */
0698         tmf_complete = tmf->complete;
0699     }
0700     sci_controller_complete_io(ihost, ireq->target_device, ireq);
0701     /* set the 'terminated' flag handle to make sure it cannot be terminated
0702      *  or completed again.
0703      */
0704     set_bit(IREQ_TERMINATED, &ireq->flags);
0705 
0706     if (test_and_clear_bit(IREQ_ABORT_PATH_ACTIVE, &ireq->flags))
0707         wake_up_all(&ihost->eventq);
0708 
0709     if (!test_bit(IREQ_NO_AUTO_FREE_TAG, &ireq->flags))
0710         isci_free_tag(ihost, ireq->io_tag);
0711 
0712     /* The task management part completes last. */
0713     if (tmf_complete)
0714         complete(tmf_complete);
0715 }
0716 
0717 static int isci_reset_device(struct isci_host *ihost,
0718                  struct domain_device *dev,
0719                  struct isci_remote_device *idev)
0720 {
0721     int rc = TMF_RESP_FUNC_COMPLETE, reset_stat = -1;
0722     struct sas_phy *phy = sas_get_local_phy(dev);
0723     struct isci_port *iport = dev->port->lldd_port;
0724 
0725     dev_dbg(&ihost->pdev->dev, "%s: idev %p\n", __func__, idev);
0726 
0727     /* Suspend the RNC, terminate all outstanding TCs. */
0728     if (isci_remote_device_suspend_terminate(ihost, idev, NULL)
0729         != SCI_SUCCESS) {
0730         rc = TMF_RESP_FUNC_FAILED;
0731         goto out;
0732     }
0733     /* Note that since the termination for outstanding requests succeeded,
0734      * this function will return success.  This is because the resets will
0735      * only fail if the device has been removed (ie. hotplug), and the
0736      * primary duty of this function is to cleanup tasks, so that is the
0737      * relevant status.
0738      */
0739     if (!test_bit(IDEV_GONE, &idev->flags)) {
0740         if (scsi_is_sas_phy_local(phy)) {
0741             struct isci_phy *iphy = &ihost->phys[phy->number];
0742 
0743             reset_stat = isci_port_perform_hard_reset(ihost, iport,
0744                                   iphy);
0745         } else
0746             reset_stat = sas_phy_reset(phy, !dev_is_sata(dev));
0747     }
0748     /* Explicitly resume the RNC here, since there was no task sent. */
0749     isci_remote_device_resume_from_abort(ihost, idev);
0750 
0751     dev_dbg(&ihost->pdev->dev, "%s: idev %p complete, reset_stat=%d.\n",
0752         __func__, idev, reset_stat);
0753  out:
0754     sas_put_local_phy(phy);
0755     return rc;
0756 }
0757 
0758 int isci_task_I_T_nexus_reset(struct domain_device *dev)
0759 {
0760     struct isci_host *ihost = dev_to_ihost(dev);
0761     struct isci_remote_device *idev;
0762     unsigned long flags;
0763     int ret;
0764 
0765     spin_lock_irqsave(&ihost->scic_lock, flags);
0766     idev = isci_get_device(dev->lldd_dev);
0767     spin_unlock_irqrestore(&ihost->scic_lock, flags);
0768 
0769     if (!idev) {
0770         /* XXX: need to cleanup any ireqs targeting this
0771          * domain_device
0772          */
0773         ret = -ENODEV;
0774         goto out;
0775     }
0776 
0777     ret = isci_reset_device(ihost, dev, idev);
0778  out:
0779     isci_put_device(idev);
0780     return ret;
0781 }