1ba6d10abSLinus Torvalds // SPDX-License-Identifier: GPL-2.0-only
22908d778SJames Bottomley /*
32908d778SJames Bottomley * Serial Attached SCSI (SAS) class SCSI Host glue.
42908d778SJames Bottomley *
52908d778SJames Bottomley * Copyright (C) 2005 Adaptec, Inc. All rights reserved.
62908d778SJames Bottomley * Copyright (C) 2005 Luben Tuikov <luben_tuikov@adaptec.com>
72908d778SJames Bottomley */
82908d778SJames Bottomley
9d7a54e30SChristoph Hellwig #include <linux/kthread.h>
1045e6cdf4SDarrick J. Wong #include <linux/firmware.h>
1109703660SPaul Gortmaker #include <linux/export.h>
1245e6cdf4SDarrick J. Wong #include <linux/ctype.h>
139ea4e076SAndy Shevchenko #include <linux/kernel.h>
14d7a54e30SChristoph Hellwig
152908d778SJames Bottomley #include "sas_internal.h"
162908d778SJames Bottomley
172908d778SJames Bottomley #include <scsi/scsi_host.h>
182908d778SJames Bottomley #include <scsi/scsi_device.h>
192908d778SJames Bottomley #include <scsi/scsi_tcq.h>
202908d778SJames Bottomley #include <scsi/scsi.h>
21f456393eSDarrick J. Wong #include <scsi/scsi_eh.h>
222908d778SJames Bottomley #include <scsi/scsi_transport.h>
232908d778SJames Bottomley #include <scsi/scsi_transport_sas.h>
24338ec570SDarrick J. Wong #include <scsi/sas_ata.h>
25e15f669cSJason Yan #include "scsi_sas_internal.h"
26e15f669cSJason Yan #include "scsi_transport_api.h"
27e15f669cSJason Yan #include "scsi_priv.h"
282908d778SJames Bottomley
292908d778SJames Bottomley #include <linux/err.h>
302908d778SJames Bottomley #include <linux/blkdev.h>
3183144186SRafael J. Wysocki #include <linux/freezer.h>
325a0e3ad6STejun Heo #include <linux/gfp.h>
332908d778SJames Bottomley #include <linux/scatterlist.h>
34fa1c1e8fSDarrick J. Wong #include <linux/libata.h>
352908d778SJames Bottomley
36a3a14252SDan Williams /* record final status and free the task */
sas_end_task(struct scsi_cmnd * sc,struct sas_task * task)37a3a14252SDan Williams static void sas_end_task(struct scsi_cmnd *sc, struct sas_task *task)
382908d778SJames Bottomley {
392908d778SJames Bottomley struct task_status_struct *ts = &task->task_status;
404be6181fSJohn Garry enum scsi_host_status hs = DID_OK;
414be6181fSJohn Garry enum exec_status stat = SAS_SAM_STAT_GOOD;
422908d778SJames Bottomley
432908d778SJames Bottomley if (ts->resp == SAS_TASK_UNDELIVERED) {
442908d778SJames Bottomley /* transport error */
452908d778SJames Bottomley hs = DID_NO_CONNECT;
462908d778SJames Bottomley } else { /* ts->resp == SAS_TASK_COMPLETE */
472908d778SJames Bottomley /* task delivered, what happened afterwards? */
482908d778SJames Bottomley switch (ts->stat) {
492908d778SJames Bottomley case SAS_DEV_NO_RESPONSE:
502908d778SJames Bottomley case SAS_INTERRUPTED:
512908d778SJames Bottomley case SAS_PHY_DOWN:
522908d778SJames Bottomley case SAS_NAK_R_ERR:
532908d778SJames Bottomley case SAS_OPEN_TO:
542908d778SJames Bottomley hs = DID_NO_CONNECT;
552908d778SJames Bottomley break;
562908d778SJames Bottomley case SAS_DATA_UNDERRUN:
57c13e5566SFUJITA Tomonori scsi_set_resid(sc, ts->residual);
58c13e5566SFUJITA Tomonori if (scsi_bufflen(sc) - scsi_get_resid(sc) < sc->underflow)
592908d778SJames Bottomley hs = DID_ERROR;
602908d778SJames Bottomley break;
612908d778SJames Bottomley case SAS_DATA_OVERRUN:
622908d778SJames Bottomley hs = DID_ERROR;
632908d778SJames Bottomley break;
642908d778SJames Bottomley case SAS_QUEUE_FULL:
652908d778SJames Bottomley hs = DID_SOFT_ERROR; /* retry */
662908d778SJames Bottomley break;
672908d778SJames Bottomley case SAS_DEVICE_UNKNOWN:
682908d778SJames Bottomley hs = DID_BAD_TARGET;
692908d778SJames Bottomley break;
702908d778SJames Bottomley case SAS_OPEN_REJECT:
712908d778SJames Bottomley if (ts->open_rej_reason == SAS_OREJ_RSVD_RETRY)
722908d778SJames Bottomley hs = DID_SOFT_ERROR; /* retry */
732908d778SJames Bottomley else
742908d778SJames Bottomley hs = DID_ERROR;
752908d778SJames Bottomley break;
762908d778SJames Bottomley case SAS_PROTO_RESPONSE:
7715ba7806SJohn Garry pr_notice("LLDD:%s sent SAS_PROTO_RESP for an SSP task; please report this\n",
782908d778SJames Bottomley task->dev->port->ha->sas_ha_name);
792908d778SJames Bottomley break;
802908d778SJames Bottomley case SAS_ABORTED_TASK:
812908d778SJames Bottomley hs = DID_ABORT;
822908d778SJames Bottomley break;
834be6181fSJohn Garry case SAS_SAM_STAT_CHECK_CONDITION:
842908d778SJames Bottomley memcpy(sc->sense_buffer, ts->buf,
85cc75e8abSFUJITA Tomonori min(SCSI_SENSE_BUFFERSIZE, ts->buf_valid_size));
864be6181fSJohn Garry stat = SAS_SAM_STAT_CHECK_CONDITION;
872908d778SJames Bottomley break;
882908d778SJames Bottomley default:
892908d778SJames Bottomley stat = ts->stat;
902908d778SJames Bottomley break;
912908d778SJames Bottomley }
922908d778SJames Bottomley }
93a3a14252SDan Williams
942908d778SJames Bottomley sc->result = (hs << 16) | stat;
95a3a14252SDan Williams ASSIGN_SAS_TASK(sc, NULL);
962908d778SJames Bottomley sas_free_task(task);
97a3a14252SDan Williams }
98a3a14252SDan Williams
sas_scsi_task_done(struct sas_task * task)99a3a14252SDan Williams static void sas_scsi_task_done(struct sas_task *task)
100a3a14252SDan Williams {
101a3a14252SDan Williams struct scsi_cmnd *sc = task->uldd_task;
1029095a64aSDan Williams struct domain_device *dev = task->dev;
1039095a64aSDan Williams struct sas_ha_struct *ha = dev->port->ha;
1049095a64aSDan Williams unsigned long flags;
105a3a14252SDan Williams
1069095a64aSDan Williams spin_lock_irqsave(&dev->done_lock, flags);
1079095a64aSDan Williams if (test_bit(SAS_HA_FROZEN, &ha->state))
1089095a64aSDan Williams task = NULL;
1099095a64aSDan Williams else
1109095a64aSDan Williams ASSIGN_SAS_TASK(sc, NULL);
1119095a64aSDan Williams spin_unlock_irqrestore(&dev->done_lock, flags);
1129095a64aSDan Williams
1139095a64aSDan Williams if (unlikely(!task)) {
1149095a64aSDan Williams /* task will be completed by the error handler */
11515ba7806SJohn Garry pr_debug("task done but aborted\n");
116a3a14252SDan Williams return;
117a3a14252SDan Williams }
118a3a14252SDan Williams
119a3a14252SDan Williams if (unlikely(!sc)) {
12015ba7806SJohn Garry pr_debug("task_done called with non existing SCSI cmnd!\n");
121a3a14252SDan Williams sas_free_task(task);
122a3a14252SDan Williams return;
123a3a14252SDan Williams }
124a3a14252SDan Williams
125a3a14252SDan Williams sas_end_task(sc, task);
126e803bc52SBart Van Assche scsi_done(sc);
1272908d778SJames Bottomley }
1282908d778SJames Bottomley
sas_create_task(struct scsi_cmnd * cmd,struct domain_device * dev,gfp_t gfp_flags)1292908d778SJames Bottomley static struct sas_task *sas_create_task(struct scsi_cmnd *cmd,
1302908d778SJames Bottomley struct domain_device *dev,
1313cc27547SAl Viro gfp_t gfp_flags)
1322908d778SJames Bottomley {
1332908d778SJames Bottomley struct sas_task *task = sas_alloc_task(gfp_flags);
1342908d778SJames Bottomley struct scsi_lun lun;
1352908d778SJames Bottomley
1362908d778SJames Bottomley if (!task)
1372908d778SJames Bottomley return NULL;
1382908d778SJames Bottomley
1392908d778SJames Bottomley task->uldd_task = cmd;
1402908d778SJames Bottomley ASSIGN_SAS_TASK(cmd, task);
1412908d778SJames Bottomley
1422908d778SJames Bottomley task->dev = dev;
1432908d778SJames Bottomley task->task_proto = task->dev->tproto; /* BUG_ON(!SSP) */
1442908d778SJames Bottomley
1452908d778SJames Bottomley int_to_scsilun(cmd->device->lun, &lun);
1462908d778SJames Bottomley memcpy(task->ssp_task.LUN, &lun.scsi_lun, 8);
1479cbbdca4STejun Heo task->ssp_task.task_attr = TASK_ATTR_SIMPLE;
148e73823f7SJames Bottomley task->ssp_task.cmd = cmd;
1492908d778SJames Bottomley
150c13e5566SFUJITA Tomonori task->scatter = scsi_sglist(cmd);
151c13e5566SFUJITA Tomonori task->num_scatter = scsi_sg_count(cmd);
152c13e5566SFUJITA Tomonori task->total_xfer_len = scsi_bufflen(cmd);
1532908d778SJames Bottomley task->data_dir = cmd->sc_data_direction;
1542908d778SJames Bottomley
1552908d778SJames Bottomley task->task_done = sas_scsi_task_done;
1562908d778SJames Bottomley
1572908d778SJames Bottomley return task;
1582908d778SJames Bottomley }
1592908d778SJames Bottomley
sas_queuecommand(struct Scsi_Host * host,struct scsi_cmnd * cmd)16092bd401bSChristoph Hellwig int sas_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
1612908d778SJames Bottomley {
1622908d778SJames Bottomley struct sas_internal *i = to_sas_internal(host->transportt);
163a923f756SChristoph Hellwig struct domain_device *dev = cmd_to_domain_dev(cmd);
1642908d778SJames Bottomley struct sas_task *task;
165a923f756SChristoph Hellwig int res = 0;
1662908d778SJames Bottomley
1673673f4bfSDan Williams /* If the device fell off, no sense in issuing commands */
168e139942dSDan Williams if (test_bit(SAS_DEV_GONE, &dev->state)) {
1693673f4bfSDan Williams cmd->result = DID_BAD_TARGET << 16;
170a923f756SChristoph Hellwig goto out_done;
1713673f4bfSDan Williams }
1723673f4bfSDan Williams
173fa1c1e8fSDarrick J. Wong if (dev_is_sata(dev)) {
174312d3e56SDan Williams spin_lock_irq(dev->sata_dev.ap->lock);
175b27dcfb0SJeff Garzik res = ata_sas_queuecmd(cmd, dev->sata_dev.ap);
176312d3e56SDan Williams spin_unlock_irq(dev->sata_dev.ap->lock);
177a923f756SChristoph Hellwig return res;
178fa1c1e8fSDarrick J. Wong }
179fa1c1e8fSDarrick J. Wong
1802908d778SJames Bottomley task = sas_create_task(cmd, dev, GFP_ATOMIC);
1812908d778SJames Bottomley if (!task)
182ac81c6a8SChristoph Hellwig return SCSI_MLQUEUE_HOST_BUSY;
1832908d778SJames Bottomley
18479855d17SChristoph Hellwig res = i->dft->lldd_execute_task(task, GFP_ATOMIC);
185a923f756SChristoph Hellwig if (res)
186a923f756SChristoph Hellwig goto out_free_task;
187a923f756SChristoph Hellwig return 0;
188a923f756SChristoph Hellwig
189a923f756SChristoph Hellwig out_free_task:
19015ba7806SJohn Garry pr_debug("lldd_execute_task returned: %d\n", res);
1912908d778SJames Bottomley ASSIGN_SAS_TASK(cmd, NULL);
1922908d778SJames Bottomley sas_free_task(task);
193ac81c6a8SChristoph Hellwig if (res == -SAS_QUEUE_FULL)
194a923f756SChristoph Hellwig cmd->result = DID_SOFT_ERROR << 16; /* retry */
195ac81c6a8SChristoph Hellwig else
196ac81c6a8SChristoph Hellwig cmd->result = DID_ERROR << 16;
197a923f756SChristoph Hellwig out_done:
198e803bc52SBart Van Assche scsi_done(cmd);
199a923f756SChristoph Hellwig return 0;
2002908d778SJames Bottomley }
201ce4fc333SJohn Garry EXPORT_SYMBOL_GPL(sas_queuecommand);
2022908d778SJames Bottomley
sas_eh_finish_cmd(struct scsi_cmnd * cmd)203a8e14fecSJames Bottomley static void sas_eh_finish_cmd(struct scsi_cmnd *cmd)
204a8e14fecSJames Bottomley {
205a8e14fecSJames Bottomley struct sas_ha_struct *sas_ha = SHOST_TO_SAS_HA(cmd->device->host);
206318aaf34SJason Yan struct domain_device *dev = cmd_to_domain_dev(cmd);
20745c73b65SDan Williams struct sas_task *task = TO_SAS_TASK(cmd);
208a8e14fecSJames Bottomley
209a3a14252SDan Williams /* At this point, we only get called following an actual abort
210a3a14252SDan Williams * of the task, so we should be guaranteed not to be racing with
211a3a14252SDan Williams * any completions from the LLD. Task is freed after this.
212a3a14252SDan Williams */
213a3a14252SDan Williams sas_end_task(cmd, task);
214a3a14252SDan Williams
215318aaf34SJason Yan if (dev_is_sata(dev)) {
216318aaf34SJason Yan /* defer commands to libata so that libata EH can
217318aaf34SJason Yan * handle ata qcs correctly
218318aaf34SJason Yan */
219318aaf34SJason Yan list_move_tail(&cmd->eh_entry, &sas_ha->eh_ata_q);
220318aaf34SJason Yan return;
221318aaf34SJason Yan }
222318aaf34SJason Yan
223a8e14fecSJames Bottomley /* now finish the command and move it on to the error
224a8e14fecSJames Bottomley * handler done list, this also takes it off the
225a3a14252SDan Williams * error handler pending list.
226a3a14252SDan Williams */
227a8e14fecSJames Bottomley scsi_eh_finish_cmd(cmd, &sas_ha->eh_done_q);
228a8e14fecSJames Bottomley }
229a8e14fecSJames Bottomley
sas_scsi_clear_queue_lu(struct list_head * error_q,struct scsi_cmnd * my_cmd)2302908d778SJames Bottomley static void sas_scsi_clear_queue_lu(struct list_head *error_q, struct scsi_cmnd *my_cmd)
2312908d778SJames Bottomley {
2322908d778SJames Bottomley struct scsi_cmnd *cmd, *n;
2332908d778SJames Bottomley
2342908d778SJames Bottomley list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
23563e4563bSJames Bottomley if (cmd->device->sdev_target == my_cmd->device->sdev_target &&
23663e4563bSJames Bottomley cmd->device->lun == my_cmd->device->lun)
237318aaf34SJason Yan sas_eh_finish_cmd(cmd);
2382908d778SJames Bottomley }
2392908d778SJames Bottomley }
2402908d778SJames Bottomley
sas_scsi_clear_queue_I_T(struct list_head * error_q,struct domain_device * dev)2412908d778SJames Bottomley static void sas_scsi_clear_queue_I_T(struct list_head *error_q,
2422908d778SJames Bottomley struct domain_device *dev)
2432908d778SJames Bottomley {
2442908d778SJames Bottomley struct scsi_cmnd *cmd, *n;
2452908d778SJames Bottomley
2462908d778SJames Bottomley list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
2472908d778SJames Bottomley struct domain_device *x = cmd_to_domain_dev(cmd);
2482908d778SJames Bottomley
2492908d778SJames Bottomley if (x == dev)
250a8e14fecSJames Bottomley sas_eh_finish_cmd(cmd);
2512908d778SJames Bottomley }
2522908d778SJames Bottomley }
2532908d778SJames Bottomley
sas_scsi_clear_queue_port(struct list_head * error_q,struct asd_sas_port * port)2542908d778SJames Bottomley static void sas_scsi_clear_queue_port(struct list_head *error_q,
2552908d778SJames Bottomley struct asd_sas_port *port)
2562908d778SJames Bottomley {
2572908d778SJames Bottomley struct scsi_cmnd *cmd, *n;
2582908d778SJames Bottomley
2592908d778SJames Bottomley list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
2602908d778SJames Bottomley struct domain_device *dev = cmd_to_domain_dev(cmd);
2612908d778SJames Bottomley struct asd_sas_port *x = dev->port;
2622908d778SJames Bottomley
2632908d778SJames Bottomley if (x == port)
264a8e14fecSJames Bottomley sas_eh_finish_cmd(cmd);
2652908d778SJames Bottomley }
2662908d778SJames Bottomley }
2672908d778SJames Bottomley
2682908d778SJames Bottomley enum task_disposition {
2692908d778SJames Bottomley TASK_IS_DONE,
2702908d778SJames Bottomley TASK_IS_ABORTED,
2712908d778SJames Bottomley TASK_IS_AT_LU,
2722908d778SJames Bottomley TASK_IS_NOT_AT_LU,
27302cd743bSDarrick J. Wong TASK_ABORT_FAILED,
2742908d778SJames Bottomley };
2752908d778SJames Bottomley
sas_scsi_find_task(struct sas_task * task)2762908d778SJames Bottomley static enum task_disposition sas_scsi_find_task(struct sas_task *task)
2772908d778SJames Bottomley {
2782908d778SJames Bottomley unsigned long flags;
2792908d778SJames Bottomley int i, res;
2802908d778SJames Bottomley struct sas_internal *si =
2811136a022SJohn Garry to_sas_internal(task->dev->port->ha->shost->transportt);
2822908d778SJames Bottomley
2832908d778SJames Bottomley for (i = 0; i < 5; i++) {
28415ba7806SJohn Garry pr_notice("%s: aborting task 0x%p\n", __func__, task);
2852908d778SJames Bottomley res = si->dft->lldd_abort_task(task);
2862908d778SJames Bottomley
2872908d778SJames Bottomley spin_lock_irqsave(&task->task_state_lock, flags);
2882908d778SJames Bottomley if (task->task_state_flags & SAS_TASK_STATE_DONE) {
2892908d778SJames Bottomley spin_unlock_irqrestore(&task->task_state_lock, flags);
29015ba7806SJohn Garry pr_debug("%s: task 0x%p is done\n", __func__, task);
2912908d778SJames Bottomley return TASK_IS_DONE;
2922908d778SJames Bottomley }
2932908d778SJames Bottomley spin_unlock_irqrestore(&task->task_state_lock, flags);
2942908d778SJames Bottomley
2952908d778SJames Bottomley if (res == TMF_RESP_FUNC_COMPLETE) {
29615ba7806SJohn Garry pr_notice("%s: task 0x%p is aborted\n",
297cadbd4a5SHarvey Harrison __func__, task);
2982908d778SJames Bottomley return TASK_IS_ABORTED;
2992908d778SJames Bottomley } else if (si->dft->lldd_query_task) {
30015ba7806SJohn Garry pr_notice("%s: querying task 0x%p\n", __func__, task);
3012908d778SJames Bottomley res = si->dft->lldd_query_task(task);
30202cd743bSDarrick J. Wong switch (res) {
30302cd743bSDarrick J. Wong case TMF_RESP_FUNC_SUCC:
30415ba7806SJohn Garry pr_notice("%s: task 0x%p at LU\n", __func__,
30515ba7806SJohn Garry task);
3062908d778SJames Bottomley return TASK_IS_AT_LU;
30702cd743bSDarrick J. Wong case TMF_RESP_FUNC_COMPLETE:
30815ba7806SJohn Garry pr_notice("%s: task 0x%p not at LU\n",
309cadbd4a5SHarvey Harrison __func__, task);
3102908d778SJames Bottomley return TASK_IS_NOT_AT_LU;
31102cd743bSDarrick J. Wong case TMF_RESP_FUNC_FAILED:
31215ba7806SJohn Garry pr_notice("%s: task 0x%p failed to abort\n",
313cadbd4a5SHarvey Harrison __func__, task);
31402cd743bSDarrick J. Wong return TASK_ABORT_FAILED;
3159aacf6feSJohn Garry default:
3169aacf6feSJohn Garry pr_notice("%s: task 0x%p result code %d not handled\n",
3179aacf6feSJohn Garry __func__, task, res);
3182908d778SJames Bottomley }
3192908d778SJames Bottomley }
3209aacf6feSJohn Garry }
3219aacf6feSJohn Garry return TASK_ABORT_FAILED;
3222908d778SJames Bottomley }
3232908d778SJames Bottomley
sas_recover_lu(struct domain_device * dev,struct scsi_cmnd * cmd)3242908d778SJames Bottomley static int sas_recover_lu(struct domain_device *dev, struct scsi_cmnd *cmd)
3252908d778SJames Bottomley {
3262908d778SJames Bottomley int res = TMF_RESP_FUNC_FAILED;
3272908d778SJames Bottomley struct scsi_lun lun;
3282908d778SJames Bottomley struct sas_internal *i =
3291136a022SJohn Garry to_sas_internal(dev->port->ha->shost->transportt);
3302908d778SJames Bottomley
3312908d778SJames Bottomley int_to_scsilun(cmd->device->lun, &lun);
3322908d778SJames Bottomley
333b3e3d4c6SJohn Garry pr_notice("eh: device %016llx LUN 0x%llx has the task\n",
3342908d778SJames Bottomley SAS_ADDR(dev->sas_addr),
3352908d778SJames Bottomley cmd->device->lun);
3362908d778SJames Bottomley
3372908d778SJames Bottomley if (i->dft->lldd_abort_task_set)
3382908d778SJames Bottomley res = i->dft->lldd_abort_task_set(dev, lun.scsi_lun);
3392908d778SJames Bottomley
3402908d778SJames Bottomley if (res == TMF_RESP_FUNC_FAILED) {
3412908d778SJames Bottomley if (i->dft->lldd_clear_task_set)
3422908d778SJames Bottomley res = i->dft->lldd_clear_task_set(dev, lun.scsi_lun);
3432908d778SJames Bottomley }
3442908d778SJames Bottomley
3452908d778SJames Bottomley if (res == TMF_RESP_FUNC_FAILED) {
3462908d778SJames Bottomley if (i->dft->lldd_lu_reset)
3472908d778SJames Bottomley res = i->dft->lldd_lu_reset(dev, lun.scsi_lun);
3482908d778SJames Bottomley }
3492908d778SJames Bottomley
3502908d778SJames Bottomley return res;
3512908d778SJames Bottomley }
3522908d778SJames Bottomley
sas_recover_I_T(struct domain_device * dev)3532908d778SJames Bottomley static int sas_recover_I_T(struct domain_device *dev)
3542908d778SJames Bottomley {
3552908d778SJames Bottomley int res = TMF_RESP_FUNC_FAILED;
3562908d778SJames Bottomley struct sas_internal *i =
3571136a022SJohn Garry to_sas_internal(dev->port->ha->shost->transportt);
3582908d778SJames Bottomley
35915ba7806SJohn Garry pr_notice("I_T nexus reset for dev %016llx\n",
3602908d778SJames Bottomley SAS_ADDR(dev->sas_addr));
3612908d778SJames Bottomley
3622908d778SJames Bottomley if (i->dft->lldd_I_T_nexus_reset)
3632908d778SJames Bottomley res = i->dft->lldd_I_T_nexus_reset(dev);
3642908d778SJames Bottomley
3652908d778SJames Bottomley return res;
3662908d778SJames Bottomley }
3672908d778SJames Bottomley
368f41a0c44SDan Williams /* take a reference on the last known good phy for this device */
sas_get_local_phy(struct domain_device * dev)369f41a0c44SDan Williams struct sas_phy *sas_get_local_phy(struct domain_device *dev)
3703ebf6922SDarrick J. Wong {
371f41a0c44SDan Williams struct sas_ha_struct *ha = dev->port->ha;
372f41a0c44SDan Williams struct sas_phy *phy;
373f41a0c44SDan Williams unsigned long flags;
3743ebf6922SDarrick J. Wong
375f41a0c44SDan Williams /* a published domain device always has a valid phy, it may be
376f41a0c44SDan Williams * stale, but it is never NULL
377f41a0c44SDan Williams */
378f41a0c44SDan Williams BUG_ON(!dev->phy);
3793ebf6922SDarrick J. Wong
380f41a0c44SDan Williams spin_lock_irqsave(&ha->phy_port_lock, flags);
381f41a0c44SDan Williams phy = dev->phy;
382f41a0c44SDan Williams get_device(&phy->dev);
383f41a0c44SDan Williams spin_unlock_irqrestore(&ha->phy_port_lock, flags);
384f41a0c44SDan Williams
385f41a0c44SDan Williams return phy;
3863ebf6922SDarrick J. Wong }
387f41a0c44SDan Williams EXPORT_SYMBOL_GPL(sas_get_local_phy);
388ad689233SDarrick J. Wong
sas_queue_reset(struct domain_device * dev,int reset_type,u64 lun)389*be946e31SWenchao Hao static int sas_queue_reset(struct domain_device *dev, int reset_type, u64 lun)
3905db45bdcSDan Williams {
3915db45bdcSDan Williams struct sas_ha_struct *ha = dev->port->ha;
3925db45bdcSDan Williams int scheduled = 0, tries = 100;
3935db45bdcSDan Williams
3945db45bdcSDan Williams /* ata: promote lun reset to bus reset */
3955db45bdcSDan Williams if (dev_is_sata(dev)) {
3965db45bdcSDan Williams sas_ata_schedule_reset(dev);
3975db45bdcSDan Williams return SUCCESS;
3985db45bdcSDan Williams }
3995db45bdcSDan Williams
4005db45bdcSDan Williams while (!scheduled && tries--) {
4015db45bdcSDan Williams spin_lock_irq(&ha->lock);
4025db45bdcSDan Williams if (!test_bit(SAS_DEV_EH_PENDING, &dev->state) &&
4035db45bdcSDan Williams !test_bit(reset_type, &dev->state)) {
4045db45bdcSDan Williams scheduled = 1;
4055db45bdcSDan Williams ha->eh_active++;
4065db45bdcSDan Williams list_add_tail(&dev->ssp_dev.eh_list_node, &ha->eh_dev_q);
4075db45bdcSDan Williams set_bit(SAS_DEV_EH_PENDING, &dev->state);
4085db45bdcSDan Williams set_bit(reset_type, &dev->state);
4095db45bdcSDan Williams int_to_scsilun(lun, &dev->ssp_dev.reset_lun);
4101136a022SJohn Garry scsi_schedule_eh(ha->shost);
4115db45bdcSDan Williams }
4125db45bdcSDan Williams spin_unlock_irq(&ha->lock);
4135db45bdcSDan Williams
4145db45bdcSDan Williams if (scheduled)
4155db45bdcSDan Williams return SUCCESS;
4165db45bdcSDan Williams }
4175db45bdcSDan Williams
41815ba7806SJohn Garry pr_warn("%s reset of %s failed\n",
4195db45bdcSDan Williams reset_type == SAS_DEV_LU_RESET ? "LUN" : "Bus",
4205db45bdcSDan Williams dev_name(&dev->rphy->dev));
4215db45bdcSDan Williams
4225db45bdcSDan Williams return FAILED;
4235db45bdcSDan Williams }
4245db45bdcSDan Williams
sas_eh_abort_handler(struct scsi_cmnd * cmd)4259524c682SDan Williams int sas_eh_abort_handler(struct scsi_cmnd *cmd)
4269524c682SDan Williams {
427c9f92600SHannes Reinecke int res = TMF_RESP_FUNC_FAILED;
4289524c682SDan Williams struct sas_task *task = TO_SAS_TASK(cmd);
4299524c682SDan Williams struct Scsi_Host *host = cmd->device->host;
430c9f92600SHannes Reinecke struct domain_device *dev = cmd_to_domain_dev(cmd);
4319524c682SDan Williams struct sas_internal *i = to_sas_internal(host->transportt);
432c9f92600SHannes Reinecke unsigned long flags;
4339524c682SDan Williams
4349524c682SDan Williams if (!i->dft->lldd_abort_task)
4359524c682SDan Williams return FAILED;
4369524c682SDan Williams
437c9f92600SHannes Reinecke spin_lock_irqsave(host->host_lock, flags);
438c9f92600SHannes Reinecke /* We cannot do async aborts for SATA devices */
439c9f92600SHannes Reinecke if (dev_is_sata(dev) && !host->host_eh_scheduled) {
440c9f92600SHannes Reinecke spin_unlock_irqrestore(host->host_lock, flags);
441c9f92600SHannes Reinecke return FAILED;
442c9f92600SHannes Reinecke }
443c9f92600SHannes Reinecke spin_unlock_irqrestore(host->host_lock, flags);
444c9f92600SHannes Reinecke
445c9f92600SHannes Reinecke if (task)
4469524c682SDan Williams res = i->dft->lldd_abort_task(task);
447c9f92600SHannes Reinecke else
44815ba7806SJohn Garry pr_notice("no task to abort\n");
4499524c682SDan Williams if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE)
4509524c682SDan Williams return SUCCESS;
4519524c682SDan Williams
4529524c682SDan Williams return FAILED;
4539524c682SDan Williams }
4549524c682SDan Williams EXPORT_SYMBOL_GPL(sas_eh_abort_handler);
4559524c682SDan Williams
456a9344e68SDarrick J. Wong /* Attempt to send a LUN reset message to a device */
sas_eh_device_reset_handler(struct scsi_cmnd * cmd)457ad689233SDarrick J. Wong int sas_eh_device_reset_handler(struct scsi_cmnd *cmd)
4582908d778SJames Bottomley {
459a9344e68SDarrick J. Wong int res;
4605db45bdcSDan Williams struct scsi_lun lun;
4615db45bdcSDan Williams struct Scsi_Host *host = cmd->device->host;
4625db45bdcSDan Williams struct domain_device *dev = cmd_to_domain_dev(cmd);
4635db45bdcSDan Williams struct sas_internal *i = to_sas_internal(host->transportt);
4645db45bdcSDan Williams
4655db45bdcSDan Williams if (current != host->ehandler)
466*be946e31SWenchao Hao return sas_queue_reset(dev, SAS_DEV_LU_RESET, cmd->device->lun);
467a9344e68SDarrick J. Wong
468a9344e68SDarrick J. Wong int_to_scsilun(cmd->device->lun, &lun);
469a9344e68SDarrick J. Wong
470a9344e68SDarrick J. Wong if (!i->dft->lldd_lu_reset)
471a9344e68SDarrick J. Wong return FAILED;
472a9344e68SDarrick J. Wong
473a9344e68SDarrick J. Wong res = i->dft->lldd_lu_reset(dev, lun.scsi_lun);
474a9344e68SDarrick J. Wong if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE)
475a9344e68SDarrick J. Wong return SUCCESS;
476a9344e68SDarrick J. Wong
477a9344e68SDarrick J. Wong return FAILED;
478a9344e68SDarrick J. Wong }
479ce4fc333SJohn Garry EXPORT_SYMBOL_GPL(sas_eh_device_reset_handler);
480a9344e68SDarrick J. Wong
sas_eh_target_reset_handler(struct scsi_cmnd * cmd)481cc199e78SHannes Reinecke int sas_eh_target_reset_handler(struct scsi_cmnd *cmd)
482a9344e68SDarrick J. Wong {
483ad689233SDarrick J. Wong int res;
484e7db8229SDan Williams struct Scsi_Host *host = cmd->device->host;
485e7db8229SDan Williams struct domain_device *dev = cmd_to_domain_dev(cmd);
486e7db8229SDan Williams struct sas_internal *i = to_sas_internal(host->transportt);
487ad689233SDarrick J. Wong
4885db45bdcSDan Williams if (current != host->ehandler)
489*be946e31SWenchao Hao return sas_queue_reset(dev, SAS_DEV_RESET, 0);
4905db45bdcSDan Williams
491e7db8229SDan Williams if (!i->dft->lldd_I_T_nexus_reset)
492e7db8229SDan Williams return FAILED;
493f41a0c44SDan Williams
494e7db8229SDan Williams res = i->dft->lldd_I_T_nexus_reset(dev);
495e7db8229SDan Williams if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE ||
496e7db8229SDan Williams res == -ENODEV)
497ad689233SDarrick J. Wong return SUCCESS;
498ad689233SDarrick J. Wong
499ad689233SDarrick J. Wong return FAILED;
500ad689233SDarrick J. Wong }
501ce4fc333SJohn Garry EXPORT_SYMBOL_GPL(sas_eh_target_reset_handler);
502ad689233SDarrick J. Wong
503ad689233SDarrick J. Wong /* Try to reset a device */
try_to_reset_cmd_device(struct scsi_cmnd * cmd)5048de3ef25SJames Bottomley static int try_to_reset_cmd_device(struct scsi_cmnd *cmd)
505ad689233SDarrick J. Wong {
506a9344e68SDarrick J. Wong int res;
5078de3ef25SJames Bottomley struct Scsi_Host *shost = cmd->device->host;
508ad689233SDarrick J. Wong
509a9344e68SDarrick J. Wong if (!shost->hostt->eh_device_reset_handler)
510cc199e78SHannes Reinecke goto try_target_reset;
511a9344e68SDarrick J. Wong
512a9344e68SDarrick J. Wong res = shost->hostt->eh_device_reset_handler(cmd);
513a9344e68SDarrick J. Wong if (res == SUCCESS)
514a9344e68SDarrick J. Wong return res;
515a9344e68SDarrick J. Wong
516cc199e78SHannes Reinecke try_target_reset:
517cc199e78SHannes Reinecke if (shost->hostt->eh_target_reset_handler)
518cc199e78SHannes Reinecke return shost->hostt->eh_target_reset_handler(cmd);
519a9344e68SDarrick J. Wong
520a9344e68SDarrick J. Wong return FAILED;
521ad689233SDarrick J. Wong }
522ad689233SDarrick J. Wong
sas_eh_handle_sas_errors(struct Scsi_Host * shost,struct list_head * work_q)52384023474SDan Williams static void sas_eh_handle_sas_errors(struct Scsi_Host *shost, struct list_head *work_q)
524ad689233SDarrick J. Wong {
5252908d778SJames Bottomley struct scsi_cmnd *cmd, *n;
5262908d778SJames Bottomley enum task_disposition res = TASK_IS_DONE;
5273ebf6922SDarrick J. Wong int tmf_resp, need_reset;
5282908d778SJames Bottomley struct sas_internal *i = to_sas_internal(shost->transportt);
529ad689233SDarrick J. Wong unsigned long flags;
530ad689233SDarrick J. Wong struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
53145c73b65SDan Williams LIST_HEAD(done);
5322908d778SJames Bottomley
53345c73b65SDan Williams /* clean out any commands that won the completion vs eh race */
534ad689233SDarrick J. Wong list_for_each_entry_safe(cmd, n, work_q, eh_entry) {
5359095a64aSDan Williams struct domain_device *dev = cmd_to_domain_dev(cmd);
5369095a64aSDan Williams struct sas_task *task;
5379095a64aSDan Williams
5389095a64aSDan Williams spin_lock_irqsave(&dev->done_lock, flags);
5399095a64aSDan Williams /* by this point the lldd has either observed
5409095a64aSDan Williams * SAS_HA_FROZEN and is leaving the task alone, or has
5419095a64aSDan Williams * won the race with eh and decided to complete it
5429095a64aSDan Williams */
5439095a64aSDan Williams task = TO_SAS_TASK(cmd);
5449095a64aSDan Williams spin_unlock_irqrestore(&dev->done_lock, flags);
545f456393eSDarrick J. Wong
546ad689233SDarrick J. Wong if (!task)
54745c73b65SDan Williams list_move_tail(&cmd->eh_entry, &done);
54845c73b65SDan Williams }
54945c73b65SDan Williams
55045c73b65SDan Williams Again:
55145c73b65SDan Williams list_for_each_entry_safe(cmd, n, work_q, eh_entry) {
55245c73b65SDan Williams struct sas_task *task = TO_SAS_TASK(cmd);
553ad689233SDarrick J. Wong
554ad689233SDarrick J. Wong list_del_init(&cmd->eh_entry);
5553ebf6922SDarrick J. Wong
5563ebf6922SDarrick J. Wong spin_lock_irqsave(&task->task_state_lock, flags);
5573ebf6922SDarrick J. Wong need_reset = task->task_state_flags & SAS_TASK_NEED_DEV_RESET;
5583ebf6922SDarrick J. Wong spin_unlock_irqrestore(&task->task_state_lock, flags);
5593ebf6922SDarrick J. Wong
5608de3ef25SJames Bottomley if (need_reset) {
56115ba7806SJohn Garry pr_notice("%s: task 0x%p requests reset\n",
562cadbd4a5SHarvey Harrison __func__, task);
5638de3ef25SJames Bottomley goto reset;
5648de3ef25SJames Bottomley }
5658de3ef25SJames Bottomley
56615ba7806SJohn Garry pr_debug("trying to find task 0x%p\n", task);
5672908d778SJames Bottomley res = sas_scsi_find_task(task);
5682908d778SJames Bottomley
5692908d778SJames Bottomley switch (res) {
5702908d778SJames Bottomley case TASK_IS_DONE:
57115ba7806SJohn Garry pr_notice("%s: task 0x%p is done\n", __func__,
5722908d778SJames Bottomley task);
573318aaf34SJason Yan sas_eh_finish_cmd(cmd);
5742908d778SJames Bottomley continue;
5752908d778SJames Bottomley case TASK_IS_ABORTED:
57615ba7806SJohn Garry pr_notice("%s: task 0x%p is aborted\n",
577cadbd4a5SHarvey Harrison __func__, task);
578318aaf34SJason Yan sas_eh_finish_cmd(cmd);
5792908d778SJames Bottomley continue;
5802908d778SJames Bottomley case TASK_IS_AT_LU:
58115ba7806SJohn Garry pr_info("task 0x%p is at LU: lu recover\n", task);
5828de3ef25SJames Bottomley reset:
5832908d778SJames Bottomley tmf_resp = sas_recover_lu(task->dev, cmd);
5842908d778SJames Bottomley if (tmf_resp == TMF_RESP_FUNC_COMPLETE) {
585b3e3d4c6SJohn Garry pr_notice("dev %016llx LU 0x%llx is recovered\n",
5862908d778SJames Bottomley SAS_ADDR(task->dev),
5872908d778SJames Bottomley cmd->device->lun);
588318aaf34SJason Yan sas_eh_finish_cmd(cmd);
589ad689233SDarrick J. Wong sas_scsi_clear_queue_lu(work_q, cmd);
5902908d778SJames Bottomley goto Again;
5912908d778SJames Bottomley }
592df561f66SGustavo A. R. Silva fallthrough;
5932908d778SJames Bottomley case TASK_IS_NOT_AT_LU:
59402cd743bSDarrick J. Wong case TASK_ABORT_FAILED:
59515ba7806SJohn Garry pr_notice("task 0x%p is not at LU: I_T recover\n",
5962908d778SJames Bottomley task);
5972908d778SJames Bottomley tmf_resp = sas_recover_I_T(task->dev);
59826a2e68fSDan Williams if (tmf_resp == TMF_RESP_FUNC_COMPLETE ||
59926a2e68fSDan Williams tmf_resp == -ENODEV) {
6008de3ef25SJames Bottomley struct domain_device *dev = task->dev;
60115ba7806SJohn Garry pr_notice("I_T %016llx recovered\n",
6022908d778SJames Bottomley SAS_ADDR(task->dev->sas_addr));
603a8e14fecSJames Bottomley sas_eh_finish_cmd(cmd);
6048de3ef25SJames Bottomley sas_scsi_clear_queue_I_T(work_q, dev);
6052908d778SJames Bottomley goto Again;
6062908d778SJames Bottomley }
6072908d778SJames Bottomley /* Hammer time :-) */
6088de3ef25SJames Bottomley try_to_reset_cmd_device(cmd);
6092908d778SJames Bottomley if (i->dft->lldd_clear_nexus_port) {
6102908d778SJames Bottomley struct asd_sas_port *port = task->dev->port;
61115ba7806SJohn Garry pr_debug("clearing nexus for port:%d\n",
6122908d778SJames Bottomley port->id);
6132908d778SJames Bottomley res = i->dft->lldd_clear_nexus_port(port);
6142908d778SJames Bottomley if (res == TMF_RESP_FUNC_COMPLETE) {
61515ba7806SJohn Garry pr_notice("clear nexus port:%d succeeded\n",
61615ba7806SJohn Garry port->id);
617a8e14fecSJames Bottomley sas_eh_finish_cmd(cmd);
618ad689233SDarrick J. Wong sas_scsi_clear_queue_port(work_q,
6192908d778SJames Bottomley port);
6202908d778SJames Bottomley goto Again;
6212908d778SJames Bottomley }
6222908d778SJames Bottomley }
6232908d778SJames Bottomley if (i->dft->lldd_clear_nexus_ha) {
62415ba7806SJohn Garry pr_debug("clear nexus ha\n");
6252908d778SJames Bottomley res = i->dft->lldd_clear_nexus_ha(ha);
6262908d778SJames Bottomley if (res == TMF_RESP_FUNC_COMPLETE) {
62715ba7806SJohn Garry pr_notice("clear nexus ha succeeded\n");
628a8e14fecSJames Bottomley sas_eh_finish_cmd(cmd);
629a8e14fecSJames Bottomley goto clear_q;
6302908d778SJames Bottomley }
6312908d778SJames Bottomley }
6322908d778SJames Bottomley /* If we are here -- this means that no amount
6332908d778SJames Bottomley * of effort could recover from errors. Quite
6342908d778SJames Bottomley * possibly the HA just disappeared.
6352908d778SJames Bottomley */
636b3e3d4c6SJohn Garry pr_err("error from device %016llx, LUN 0x%llx couldn't be recovered in any way\n",
6372908d778SJames Bottomley SAS_ADDR(task->dev->sas_addr),
6382908d778SJames Bottomley cmd->device->lun);
6392908d778SJames Bottomley
640a8e14fecSJames Bottomley sas_eh_finish_cmd(cmd);
6412908d778SJames Bottomley goto clear_q;
6422908d778SJames Bottomley }
6432908d778SJames Bottomley }
64445c73b65SDan Williams out:
64545c73b65SDan Williams list_splice_tail(&done, work_q);
6463944f509SDan Williams list_splice_tail_init(&ha->eh_ata_q, work_q);
64784023474SDan Williams return;
64845c73b65SDan Williams
6492908d778SJames Bottomley clear_q:
65015ba7806SJohn Garry pr_debug("--- Exit %s -- clear_q\n", __func__);
651a8e14fecSJames Bottomley list_for_each_entry_safe(cmd, n, work_q, eh_entry)
652a8e14fecSJames Bottomley sas_eh_finish_cmd(cmd);
65345c73b65SDan Williams goto out;
654ad689233SDarrick J. Wong }
655ad689233SDarrick J. Wong
sas_eh_handle_resets(struct Scsi_Host * shost)6565db45bdcSDan Williams static void sas_eh_handle_resets(struct Scsi_Host *shost)
6575db45bdcSDan Williams {
6585db45bdcSDan Williams struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
6595db45bdcSDan Williams struct sas_internal *i = to_sas_internal(shost->transportt);
6605db45bdcSDan Williams
6615db45bdcSDan Williams /* handle directed resets to sas devices */
6625db45bdcSDan Williams spin_lock_irq(&ha->lock);
6635db45bdcSDan Williams while (!list_empty(&ha->eh_dev_q)) {
6645db45bdcSDan Williams struct domain_device *dev;
6655db45bdcSDan Williams struct ssp_device *ssp;
6665db45bdcSDan Williams
6675db45bdcSDan Williams ssp = list_entry(ha->eh_dev_q.next, typeof(*ssp), eh_list_node);
6685db45bdcSDan Williams list_del_init(&ssp->eh_list_node);
6695db45bdcSDan Williams dev = container_of(ssp, typeof(*dev), ssp_dev);
6705db45bdcSDan Williams kref_get(&dev->kref);
6715db45bdcSDan Williams WARN_ONCE(dev_is_sata(dev), "ssp reset to ata device?\n");
6725db45bdcSDan Williams
6735db45bdcSDan Williams spin_unlock_irq(&ha->lock);
6745db45bdcSDan Williams
6755db45bdcSDan Williams if (test_and_clear_bit(SAS_DEV_LU_RESET, &dev->state))
6765db45bdcSDan Williams i->dft->lldd_lu_reset(dev, ssp->reset_lun.scsi_lun);
6775db45bdcSDan Williams
6785db45bdcSDan Williams if (test_and_clear_bit(SAS_DEV_RESET, &dev->state))
6795db45bdcSDan Williams i->dft->lldd_I_T_nexus_reset(dev);
6805db45bdcSDan Williams
6815db45bdcSDan Williams sas_put_device(dev);
6825db45bdcSDan Williams spin_lock_irq(&ha->lock);
6835db45bdcSDan Williams clear_bit(SAS_DEV_EH_PENDING, &dev->state);
6845db45bdcSDan Williams ha->eh_active--;
6855db45bdcSDan Williams }
6865db45bdcSDan Williams spin_unlock_irq(&ha->lock);
6875db45bdcSDan Williams }
6885db45bdcSDan Williams
689e4a9c373SDan Williams
sas_scsi_recover_host(struct Scsi_Host * shost)690ad689233SDarrick J. Wong void sas_scsi_recover_host(struct Scsi_Host *shost)
691ad689233SDarrick J. Wong {
692ad689233SDarrick J. Wong struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
693ad689233SDarrick J. Wong LIST_HEAD(eh_work_q);
694e4a9c373SDan Williams int tries = 0;
695e4a9c373SDan Williams bool retry;
696ad689233SDarrick J. Wong
697e4a9c373SDan Williams retry:
698e4a9c373SDan Williams tries++;
699e4a9c373SDan Williams retry = true;
700e4a9c373SDan Williams spin_lock_irq(shost->host_lock);
701ad689233SDarrick J. Wong list_splice_init(&shost->eh_cmd_q, &eh_work_q);
702e4a9c373SDan Williams spin_unlock_irq(shost->host_lock);
703ad689233SDarrick J. Wong
70415ba7806SJohn Garry pr_notice("Enter %s busy: %d failed: %d\n",
705c84b023aSMing Lei __func__, scsi_host_busy(shost), shost->host_failed);
706ad689233SDarrick J. Wong /*
707ad689233SDarrick J. Wong * Deal with commands that still have SAS tasks (i.e. they didn't
70884023474SDan Williams * complete via the normal sas_task completion mechanism),
70984023474SDan Williams * SAS_HA_FROZEN gives eh dominion over all sas_task completion.
710ad689233SDarrick J. Wong */
7119095a64aSDan Williams set_bit(SAS_HA_FROZEN, &ha->state);
71284023474SDan Williams sas_eh_handle_sas_errors(shost, &eh_work_q);
71384023474SDan Williams clear_bit(SAS_HA_FROZEN, &ha->state);
71484023474SDan Williams if (list_empty(&eh_work_q))
715ad689233SDarrick J. Wong goto out;
716ad689233SDarrick J. Wong
717ad689233SDarrick J. Wong /*
718ad689233SDarrick J. Wong * Now deal with SCSI commands that completed ok but have a an error
719ad689233SDarrick J. Wong * code (and hopefully sense data) attached. This is roughly what
720ad689233SDarrick J. Wong * scsi_unjam_host does, but we skip scsi_eh_abort_cmds because any
721ad689233SDarrick J. Wong * command we see here has no sas_task and is thus unknown to the HA.
722ad689233SDarrick J. Wong */
7233a20e642SXiang Chen sas_ata_eh(shost, &eh_work_q);
724ad689233SDarrick J. Wong if (!scsi_eh_get_sense(&eh_work_q, &ha->eh_done_q))
725ad689233SDarrick J. Wong scsi_eh_ready_devs(shost, &eh_work_q, &ha->eh_done_q);
726ad689233SDarrick J. Wong
727ad689233SDarrick J. Wong out:
7285db45bdcSDan Williams sas_eh_handle_resets(shost);
7295db45bdcSDan Williams
73000dd4998SJames Bottomley /* now link into libata eh --- if we have any ata devices */
73100dd4998SJames Bottomley sas_ata_strategy_handler(shost);
73200dd4998SJames Bottomley
733ad689233SDarrick J. Wong scsi_eh_flush_done_q(&ha->eh_done_q);
73400dd4998SJames Bottomley
735e4a9c373SDan Williams /* check if any new eh work was scheduled during the last run */
736e4a9c373SDan Williams spin_lock_irq(&ha->lock);
737e4a9c373SDan Williams if (ha->eh_active == 0) {
738e4a9c373SDan Williams shost->host_eh_scheduled = 0;
739e4a9c373SDan Williams retry = false;
740e4a9c373SDan Williams }
741e4a9c373SDan Williams spin_unlock_irq(&ha->lock);
742e4a9c373SDan Williams
743e4a9c373SDan Williams if (retry)
744e4a9c373SDan Williams goto retry;
745e4a9c373SDan Williams
74615ba7806SJohn Garry pr_notice("--- Exit %s: busy: %d failed: %d tries: %d\n",
747c84b023aSMing Lei __func__, scsi_host_busy(shost),
74874665016SChristoph Hellwig shost->host_failed, tries);
7492908d778SJames Bottomley }
7502908d778SJames Bottomley
sas_ioctl(struct scsi_device * sdev,unsigned int cmd,void __user * arg)7516f4e626fSNathan Chancellor int sas_ioctl(struct scsi_device *sdev, unsigned int cmd, void __user *arg)
752fa1c1e8fSDarrick J. Wong {
753fa1c1e8fSDarrick J. Wong struct domain_device *dev = sdev_to_domain_dev(sdev);
754fa1c1e8fSDarrick J. Wong
755fa1c1e8fSDarrick J. Wong if (dev_is_sata(dev))
75694be9a58SJeff Garzik return ata_sas_scsi_ioctl(dev->sata_dev.ap, sdev, cmd, arg);
757fa1c1e8fSDarrick J. Wong
758fa1c1e8fSDarrick J. Wong return -EINVAL;
759fa1c1e8fSDarrick J. Wong }
760ce4fc333SJohn Garry EXPORT_SYMBOL_GPL(sas_ioctl);
761fa1c1e8fSDarrick J. Wong
sas_find_dev_by_rphy(struct sas_rphy * rphy)7622908d778SJames Bottomley struct domain_device *sas_find_dev_by_rphy(struct sas_rphy *rphy)
7632908d778SJames Bottomley {
7642908d778SJames Bottomley struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent);
7652908d778SJames Bottomley struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
7662908d778SJames Bottomley struct domain_device *found_dev = NULL;
7672908d778SJames Bottomley int i;
768980fa2f9SDarrick J. Wong unsigned long flags;
7692908d778SJames Bottomley
770980fa2f9SDarrick J. Wong spin_lock_irqsave(&ha->phy_port_lock, flags);
7712908d778SJames Bottomley for (i = 0; i < ha->num_phys; i++) {
7722908d778SJames Bottomley struct asd_sas_port *port = ha->sas_port[i];
7732908d778SJames Bottomley struct domain_device *dev;
7742908d778SJames Bottomley
7752908d778SJames Bottomley spin_lock(&port->dev_list_lock);
7762908d778SJames Bottomley list_for_each_entry(dev, &port->dev_list, dev_list_node) {
7772908d778SJames Bottomley if (rphy == dev->rphy) {
7782908d778SJames Bottomley found_dev = dev;
7792908d778SJames Bottomley spin_unlock(&port->dev_list_lock);
7802908d778SJames Bottomley goto found;
7812908d778SJames Bottomley }
7822908d778SJames Bottomley }
7832908d778SJames Bottomley spin_unlock(&port->dev_list_lock);
7842908d778SJames Bottomley }
7852908d778SJames Bottomley found:
786980fa2f9SDarrick J. Wong spin_unlock_irqrestore(&ha->phy_port_lock, flags);
7872908d778SJames Bottomley
7882908d778SJames Bottomley return found_dev;
7892908d778SJames Bottomley }
7902908d778SJames Bottomley
sas_target_alloc(struct scsi_target * starget)7912908d778SJames Bottomley int sas_target_alloc(struct scsi_target *starget)
7922908d778SJames Bottomley {
793735f7d2fSDan Williams struct sas_rphy *rphy = dev_to_rphy(starget->dev.parent);
794735f7d2fSDan Williams struct domain_device *found_dev = sas_find_dev_by_rphy(rphy);
7952908d778SJames Bottomley
7962908d778SJames Bottomley if (!found_dev)
7972908d778SJames Bottomley return -ENODEV;
7982908d778SJames Bottomley
799735f7d2fSDan Williams kref_get(&found_dev->kref);
8002908d778SJames Bottomley starget->hostdata = found_dev;
8012908d778SJames Bottomley return 0;
8022908d778SJames Bottomley }
803ce4fc333SJohn Garry EXPORT_SYMBOL_GPL(sas_target_alloc);
8042908d778SJames Bottomley
80597a1420dSDan Williams #define SAS_DEF_QD 256
8062908d778SJames Bottomley
sas_slave_configure(struct scsi_device * scsi_dev)8072908d778SJames Bottomley int sas_slave_configure(struct scsi_device *scsi_dev)
8082908d778SJames Bottomley {
8092908d778SJames Bottomley struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
8102908d778SJames Bottomley
8112908d778SJames Bottomley BUG_ON(dev->rphy->identify.device_type != SAS_END_DEVICE);
8122908d778SJames Bottomley
813fa1c1e8fSDarrick J. Wong if (dev_is_sata(dev)) {
814fa1c1e8fSDarrick J. Wong ata_sas_slave_configure(scsi_dev, dev->sata_dev.ap);
815fa1c1e8fSDarrick J. Wong return 0;
816fa1c1e8fSDarrick J. Wong }
817fa1c1e8fSDarrick J. Wong
8182908d778SJames Bottomley sas_read_port_mode_page(scsi_dev);
8192908d778SJames Bottomley
8202908d778SJames Bottomley if (scsi_dev->tagged_supported) {
821db5ed4dfSChristoph Hellwig scsi_change_queue_depth(scsi_dev, SAS_DEF_QD);
8222908d778SJames Bottomley } else {
823b3e3d4c6SJohn Garry pr_notice("device %016llx, LUN 0x%llx doesn't support TCQ\n",
82415ba7806SJohn Garry SAS_ADDR(dev->sas_addr), scsi_dev->lun);
825db5ed4dfSChristoph Hellwig scsi_change_queue_depth(scsi_dev, 1);
8262908d778SJames Bottomley }
8272908d778SJames Bottomley
828f27708fcSDarrick J. Wong scsi_dev->allow_restart = 1;
829f27708fcSDarrick J. Wong
8302908d778SJames Bottomley return 0;
8312908d778SJames Bottomley }
832ce4fc333SJohn Garry EXPORT_SYMBOL_GPL(sas_slave_configure);
8332908d778SJames Bottomley
sas_change_queue_depth(struct scsi_device * sdev,int depth)834db5ed4dfSChristoph Hellwig int sas_change_queue_depth(struct scsi_device *sdev, int depth)
8352908d778SJames Bottomley {
83697a1420dSDan Williams struct domain_device *dev = sdev_to_domain_dev(sdev);
8372908d778SJames Bottomley
838f6e67035SDan Williams if (dev_is_sata(dev))
839371b74c8SDamien Le Moal return ata_change_queue_depth(dev->sata_dev.ap, sdev, depth);
840f6e67035SDan Williams
84197a1420dSDan Williams if (!sdev->tagged_supported)
84297a1420dSDan Williams depth = 1;
843db5ed4dfSChristoph Hellwig return scsi_change_queue_depth(sdev, depth);
8442908d778SJames Bottomley }
845ce4fc333SJohn Garry EXPORT_SYMBOL_GPL(sas_change_queue_depth);
8462908d778SJames Bottomley
sas_bios_param(struct scsi_device * scsi_dev,struct block_device * bdev,sector_t capacity,int * hsc)8472908d778SJames Bottomley int sas_bios_param(struct scsi_device *scsi_dev,
8482908d778SJames Bottomley struct block_device *bdev,
8492908d778SJames Bottomley sector_t capacity, int *hsc)
8502908d778SJames Bottomley {
8512908d778SJames Bottomley hsc[0] = 255;
8522908d778SJames Bottomley hsc[1] = 63;
8532908d778SJames Bottomley sector_div(capacity, 255*63);
8542908d778SJames Bottomley hsc[2] = capacity;
8552908d778SJames Bottomley
8562908d778SJames Bottomley return 0;
8572908d778SJames Bottomley }
858ce4fc333SJohn Garry EXPORT_SYMBOL_GPL(sas_bios_param);
8592908d778SJames Bottomley
sas_task_internal_done(struct sas_task * task)8604aef43b2SJohn Garry void sas_task_internal_done(struct sas_task *task)
8614aef43b2SJohn Garry {
8624aef43b2SJohn Garry del_timer(&task->slow_task->timer);
8634aef43b2SJohn Garry complete(&task->slow_task->completion);
8644aef43b2SJohn Garry }
8654aef43b2SJohn Garry
sas_task_internal_timedout(struct timer_list * t)8664aef43b2SJohn Garry void sas_task_internal_timedout(struct timer_list *t)
8674aef43b2SJohn Garry {
8684aef43b2SJohn Garry struct sas_task_slow *slow = from_timer(slow, t, timer);
8694aef43b2SJohn Garry struct sas_task *task = slow->task;
8704aef43b2SJohn Garry bool is_completed = true;
8714aef43b2SJohn Garry unsigned long flags;
8724aef43b2SJohn Garry
8734aef43b2SJohn Garry spin_lock_irqsave(&task->task_state_lock, flags);
8744aef43b2SJohn Garry if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
8754aef43b2SJohn Garry task->task_state_flags |= SAS_TASK_STATE_ABORTED;
8764aef43b2SJohn Garry is_completed = false;
8774aef43b2SJohn Garry }
8784aef43b2SJohn Garry spin_unlock_irqrestore(&task->task_state_lock, flags);
8794aef43b2SJohn Garry
8804aef43b2SJohn Garry if (!is_completed)
8814aef43b2SJohn Garry complete(&task->slow_task->completion);
8824aef43b2SJohn Garry }
8834aef43b2SJohn Garry
884001ec7f8SJohn Garry #define TASK_TIMEOUT (20 * HZ)
885001ec7f8SJohn Garry #define TASK_RETRY 3
886001ec7f8SJohn Garry
sas_execute_internal_abort(struct domain_device * device,enum sas_internal_abort type,u16 tag,unsigned int qid,void * data)8875c9bf363SJohn Garry static int sas_execute_internal_abort(struct domain_device *device,
8885c9bf363SJohn Garry enum sas_internal_abort type, u16 tag,
8895c9bf363SJohn Garry unsigned int qid, void *data)
8905c9bf363SJohn Garry {
8915c9bf363SJohn Garry struct sas_ha_struct *ha = device->port->ha;
8921136a022SJohn Garry struct sas_internal *i = to_sas_internal(ha->shost->transportt);
8935c9bf363SJohn Garry struct sas_task *task = NULL;
8945c9bf363SJohn Garry int res, retry;
8955c9bf363SJohn Garry
8965c9bf363SJohn Garry for (retry = 0; retry < TASK_RETRY; retry++) {
8975c9bf363SJohn Garry task = sas_alloc_slow_task(GFP_KERNEL);
8985c9bf363SJohn Garry if (!task)
8995c9bf363SJohn Garry return -ENOMEM;
9005c9bf363SJohn Garry
9015c9bf363SJohn Garry task->dev = device;
9025c9bf363SJohn Garry task->task_proto = SAS_PROTOCOL_INTERNAL_ABORT;
9035c9bf363SJohn Garry task->task_done = sas_task_internal_done;
9045c9bf363SJohn Garry task->slow_task->timer.function = sas_task_internal_timedout;
9055c9bf363SJohn Garry task->slow_task->timer.expires = jiffies + TASK_TIMEOUT;
9065c9bf363SJohn Garry add_timer(&task->slow_task->timer);
9075c9bf363SJohn Garry
9085c9bf363SJohn Garry task->abort_task.tag = tag;
9095c9bf363SJohn Garry task->abort_task.type = type;
910095478a6SJohn Garry task->abort_task.qid = qid;
9115c9bf363SJohn Garry
9125c9bf363SJohn Garry res = i->dft->lldd_execute_task(task, GFP_KERNEL);
9135c9bf363SJohn Garry if (res) {
9145c9bf363SJohn Garry del_timer_sync(&task->slow_task->timer);
9155c9bf363SJohn Garry pr_err("Executing internal abort failed %016llx (%d)\n",
9165c9bf363SJohn Garry SAS_ADDR(device->sas_addr), res);
9175c9bf363SJohn Garry break;
9185c9bf363SJohn Garry }
9195c9bf363SJohn Garry
9205c9bf363SJohn Garry wait_for_completion(&task->slow_task->completion);
9215c9bf363SJohn Garry res = TMF_RESP_FUNC_FAILED;
9225c9bf363SJohn Garry
9235c9bf363SJohn Garry /* Even if the internal abort timed out, return direct. */
9245c9bf363SJohn Garry if (task->task_state_flags & SAS_TASK_STATE_ABORTED) {
925095478a6SJohn Garry bool quit = true;
926095478a6SJohn Garry
927095478a6SJohn Garry if (i->dft->lldd_abort_timeout)
928095478a6SJohn Garry quit = i->dft->lldd_abort_timeout(task, data);
929095478a6SJohn Garry else
9305c9bf363SJohn Garry pr_err("Internal abort: timeout %016llx\n",
9315c9bf363SJohn Garry SAS_ADDR(device->sas_addr));
9325c9bf363SJohn Garry res = -EIO;
933095478a6SJohn Garry if (quit)
9345c9bf363SJohn Garry break;
9355c9bf363SJohn Garry }
9365c9bf363SJohn Garry
9375c9bf363SJohn Garry if (task->task_status.resp == SAS_TASK_COMPLETE &&
9385c9bf363SJohn Garry task->task_status.stat == SAS_SAM_STAT_GOOD) {
9395c9bf363SJohn Garry res = TMF_RESP_FUNC_COMPLETE;
9405c9bf363SJohn Garry break;
9415c9bf363SJohn Garry }
9425c9bf363SJohn Garry
9435c9bf363SJohn Garry if (task->task_status.resp == SAS_TASK_COMPLETE &&
9445c9bf363SJohn Garry task->task_status.stat == TMF_RESP_FUNC_SUCC) {
9455c9bf363SJohn Garry res = TMF_RESP_FUNC_SUCC;
9465c9bf363SJohn Garry break;
9475c9bf363SJohn Garry }
9485c9bf363SJohn Garry
9495c9bf363SJohn Garry pr_err("Internal abort: task to dev %016llx response: 0x%x status 0x%x\n",
9505c9bf363SJohn Garry SAS_ADDR(device->sas_addr), task->task_status.resp,
9515c9bf363SJohn Garry task->task_status.stat);
9525c9bf363SJohn Garry sas_free_task(task);
9535c9bf363SJohn Garry task = NULL;
9545c9bf363SJohn Garry }
9555c9bf363SJohn Garry BUG_ON(retry == TASK_RETRY && task != NULL);
9565c9bf363SJohn Garry sas_free_task(task);
9575c9bf363SJohn Garry return res;
9585c9bf363SJohn Garry }
9595c9bf363SJohn Garry
sas_execute_internal_abort_single(struct domain_device * device,u16 tag,unsigned int qid,void * data)9605c9bf363SJohn Garry int sas_execute_internal_abort_single(struct domain_device *device, u16 tag,
9615c9bf363SJohn Garry unsigned int qid, void *data)
9625c9bf363SJohn Garry {
9635c9bf363SJohn Garry return sas_execute_internal_abort(device, SAS_INTERNAL_ABORT_SINGLE,
9645c9bf363SJohn Garry tag, qid, data);
9655c9bf363SJohn Garry }
9665c9bf363SJohn Garry EXPORT_SYMBOL_GPL(sas_execute_internal_abort_single);
9675c9bf363SJohn Garry
sas_execute_internal_abort_dev(struct domain_device * device,unsigned int qid,void * data)9686a91c3e3SJohn Garry int sas_execute_internal_abort_dev(struct domain_device *device,
9696a91c3e3SJohn Garry unsigned int qid, void *data)
9706a91c3e3SJohn Garry {
9716a91c3e3SJohn Garry return sas_execute_internal_abort(device, SAS_INTERNAL_ABORT_DEV,
9726a91c3e3SJohn Garry SCSI_NO_TAG, qid, data);
9736a91c3e3SJohn Garry }
9746a91c3e3SJohn Garry EXPORT_SYMBOL_GPL(sas_execute_internal_abort_dev);
9756a91c3e3SJohn Garry
sas_execute_tmf(struct domain_device * device,void * parameter,int para_len,int force_phy_id,struct sas_tmf_task * tmf)976001ec7f8SJohn Garry int sas_execute_tmf(struct domain_device *device, void *parameter,
977001ec7f8SJohn Garry int para_len, int force_phy_id,
978001ec7f8SJohn Garry struct sas_tmf_task *tmf)
979001ec7f8SJohn Garry {
980001ec7f8SJohn Garry struct sas_task *task;
981001ec7f8SJohn Garry struct sas_internal *i =
9821136a022SJohn Garry to_sas_internal(device->port->ha->shost->transportt);
983001ec7f8SJohn Garry int res, retry;
984001ec7f8SJohn Garry
985001ec7f8SJohn Garry for (retry = 0; retry < TASK_RETRY; retry++) {
986001ec7f8SJohn Garry task = sas_alloc_slow_task(GFP_KERNEL);
987001ec7f8SJohn Garry if (!task)
988001ec7f8SJohn Garry return -ENOMEM;
989001ec7f8SJohn Garry
990001ec7f8SJohn Garry task->dev = device;
991001ec7f8SJohn Garry task->task_proto = device->tproto;
992001ec7f8SJohn Garry
9933f2e252eSJohn Garry if (dev_is_sata(device)) {
9943f2e252eSJohn Garry task->ata_task.device_control_reg_update = 1;
9953f2e252eSJohn Garry if (force_phy_id >= 0) {
9963f2e252eSJohn Garry task->ata_task.force_phy = true;
9973f2e252eSJohn Garry task->ata_task.force_phy_id = force_phy_id;
9983f2e252eSJohn Garry }
9993f2e252eSJohn Garry memcpy(&task->ata_task.fis, parameter, para_len);
10003f2e252eSJohn Garry } else {
1001350d85baSJohn Garry memcpy(&task->ssp_task, parameter, para_len);
10023f2e252eSJohn Garry }
1003350d85baSJohn Garry
1004001ec7f8SJohn Garry task->task_done = sas_task_internal_done;
1005001ec7f8SJohn Garry task->tmf = tmf;
1006001ec7f8SJohn Garry
1007001ec7f8SJohn Garry task->slow_task->timer.function = sas_task_internal_timedout;
1008001ec7f8SJohn Garry task->slow_task->timer.expires = jiffies + TASK_TIMEOUT;
1009001ec7f8SJohn Garry add_timer(&task->slow_task->timer);
1010001ec7f8SJohn Garry
1011001ec7f8SJohn Garry res = i->dft->lldd_execute_task(task, GFP_KERNEL);
1012001ec7f8SJohn Garry if (res) {
1013001ec7f8SJohn Garry del_timer_sync(&task->slow_task->timer);
1014001ec7f8SJohn Garry pr_err("executing TMF task failed %016llx (%d)\n",
1015001ec7f8SJohn Garry SAS_ADDR(device->sas_addr), res);
1016001ec7f8SJohn Garry break;
1017001ec7f8SJohn Garry }
1018001ec7f8SJohn Garry
1019001ec7f8SJohn Garry wait_for_completion(&task->slow_task->completion);
1020001ec7f8SJohn Garry
10212037a340SJohn Garry if (i->dft->lldd_tmf_exec_complete)
10222037a340SJohn Garry i->dft->lldd_tmf_exec_complete(device);
10232037a340SJohn Garry
1024001ec7f8SJohn Garry res = TMF_RESP_FUNC_FAILED;
1025001ec7f8SJohn Garry
1026001ec7f8SJohn Garry if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
1027001ec7f8SJohn Garry if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
1028001ec7f8SJohn Garry pr_err("TMF task timeout for %016llx and not done\n",
1029001ec7f8SJohn Garry SAS_ADDR(device->sas_addr));
1030693e66a0SJohn Garry if (i->dft->lldd_tmf_aborted)
1031693e66a0SJohn Garry i->dft->lldd_tmf_aborted(task);
1032001ec7f8SJohn Garry break;
1033001ec7f8SJohn Garry }
1034001ec7f8SJohn Garry pr_warn("TMF task timeout for %016llx and done\n",
1035001ec7f8SJohn Garry SAS_ADDR(device->sas_addr));
1036001ec7f8SJohn Garry }
1037001ec7f8SJohn Garry
1038001ec7f8SJohn Garry if (task->task_status.resp == SAS_TASK_COMPLETE &&
1039001ec7f8SJohn Garry task->task_status.stat == TMF_RESP_FUNC_COMPLETE) {
1040001ec7f8SJohn Garry res = TMF_RESP_FUNC_COMPLETE;
1041001ec7f8SJohn Garry break;
1042001ec7f8SJohn Garry }
1043001ec7f8SJohn Garry
1044001ec7f8SJohn Garry if (task->task_status.resp == SAS_TASK_COMPLETE &&
1045001ec7f8SJohn Garry task->task_status.stat == TMF_RESP_FUNC_SUCC) {
1046001ec7f8SJohn Garry res = TMF_RESP_FUNC_SUCC;
1047001ec7f8SJohn Garry break;
1048001ec7f8SJohn Garry }
1049001ec7f8SJohn Garry
1050001ec7f8SJohn Garry if (task->task_status.resp == SAS_TASK_COMPLETE &&
1051001ec7f8SJohn Garry task->task_status.stat == SAS_DATA_UNDERRUN) {
1052001ec7f8SJohn Garry /* no error, but return the number of bytes of
1053001ec7f8SJohn Garry * underrun
1054001ec7f8SJohn Garry */
1055001ec7f8SJohn Garry pr_warn("TMF task to dev %016llx resp: 0x%x sts 0x%x underrun\n",
1056001ec7f8SJohn Garry SAS_ADDR(device->sas_addr),
1057001ec7f8SJohn Garry task->task_status.resp,
1058001ec7f8SJohn Garry task->task_status.stat);
1059001ec7f8SJohn Garry res = task->task_status.residual;
1060001ec7f8SJohn Garry break;
1061001ec7f8SJohn Garry }
1062001ec7f8SJohn Garry
1063001ec7f8SJohn Garry if (task->task_status.resp == SAS_TASK_COMPLETE &&
1064001ec7f8SJohn Garry task->task_status.stat == SAS_DATA_OVERRUN) {
1065001ec7f8SJohn Garry pr_warn("TMF task blocked task error %016llx\n",
1066001ec7f8SJohn Garry SAS_ADDR(device->sas_addr));
1067001ec7f8SJohn Garry res = -EMSGSIZE;
1068001ec7f8SJohn Garry break;
1069001ec7f8SJohn Garry }
1070001ec7f8SJohn Garry
1071001ec7f8SJohn Garry if (task->task_status.resp == SAS_TASK_COMPLETE &&
1072001ec7f8SJohn Garry task->task_status.stat == SAS_OPEN_REJECT) {
1073001ec7f8SJohn Garry pr_warn("TMF task open reject failed %016llx\n",
1074001ec7f8SJohn Garry SAS_ADDR(device->sas_addr));
1075001ec7f8SJohn Garry res = -EIO;
1076001ec7f8SJohn Garry } else {
1077001ec7f8SJohn Garry pr_warn("TMF task to dev %016llx resp: 0x%x status 0x%x\n",
1078001ec7f8SJohn Garry SAS_ADDR(device->sas_addr),
1079001ec7f8SJohn Garry task->task_status.resp,
1080001ec7f8SJohn Garry task->task_status.stat);
1081001ec7f8SJohn Garry }
1082001ec7f8SJohn Garry sas_free_task(task);
1083001ec7f8SJohn Garry task = NULL;
1084001ec7f8SJohn Garry }
1085001ec7f8SJohn Garry
1086001ec7f8SJohn Garry if (retry == TASK_RETRY)
1087001ec7f8SJohn Garry pr_warn("executing TMF for %016llx failed after %d attempts!\n",
1088001ec7f8SJohn Garry SAS_ADDR(device->sas_addr), TASK_RETRY);
1089001ec7f8SJohn Garry sas_free_task(task);
1090001ec7f8SJohn Garry
1091001ec7f8SJohn Garry return res;
1092001ec7f8SJohn Garry }
1093001ec7f8SJohn Garry
sas_execute_ssp_tmf(struct domain_device * device,u8 * lun,struct sas_tmf_task * tmf)109469b80a0eSJohn Garry static int sas_execute_ssp_tmf(struct domain_device *device, u8 *lun,
1095350d85baSJohn Garry struct sas_tmf_task *tmf)
1096350d85baSJohn Garry {
1097350d85baSJohn Garry struct sas_ssp_task ssp_task;
1098350d85baSJohn Garry
1099350d85baSJohn Garry if (!(device->tproto & SAS_PROTOCOL_SSP))
1100350d85baSJohn Garry return TMF_RESP_FUNC_ESUPP;
1101350d85baSJohn Garry
1102350d85baSJohn Garry memcpy(ssp_task.LUN, lun, 8);
1103350d85baSJohn Garry
1104350d85baSJohn Garry return sas_execute_tmf(device, &ssp_task, sizeof(ssp_task), -1, tmf);
1105350d85baSJohn Garry }
1106350d85baSJohn Garry
sas_abort_task_set(struct domain_device * dev,u8 * lun)110769b80a0eSJohn Garry int sas_abort_task_set(struct domain_device *dev, u8 *lun)
110869b80a0eSJohn Garry {
110969b80a0eSJohn Garry struct sas_tmf_task tmf_task = {
111069b80a0eSJohn Garry .tmf = TMF_ABORT_TASK_SET,
111169b80a0eSJohn Garry };
111269b80a0eSJohn Garry
111369b80a0eSJohn Garry return sas_execute_ssp_tmf(dev, lun, &tmf_task);
111469b80a0eSJohn Garry }
111569b80a0eSJohn Garry EXPORT_SYMBOL_GPL(sas_abort_task_set);
111669b80a0eSJohn Garry
sas_clear_task_set(struct domain_device * dev,u8 * lun)1117e8585452SJohn Garry int sas_clear_task_set(struct domain_device *dev, u8 *lun)
1118e8585452SJohn Garry {
1119e8585452SJohn Garry struct sas_tmf_task tmf_task = {
1120e8585452SJohn Garry .tmf = TMF_CLEAR_TASK_SET,
1121e8585452SJohn Garry };
1122e8585452SJohn Garry
1123e8585452SJohn Garry return sas_execute_ssp_tmf(dev, lun, &tmf_task);
1124e8585452SJohn Garry }
1125e8585452SJohn Garry EXPORT_SYMBOL_GPL(sas_clear_task_set);
1126e8585452SJohn Garry
sas_lu_reset(struct domain_device * dev,u8 * lun)112729d77690SJohn Garry int sas_lu_reset(struct domain_device *dev, u8 *lun)
112829d77690SJohn Garry {
112929d77690SJohn Garry struct sas_tmf_task tmf_task = {
113029d77690SJohn Garry .tmf = TMF_LU_RESET,
113129d77690SJohn Garry };
113229d77690SJohn Garry
113329d77690SJohn Garry return sas_execute_ssp_tmf(dev, lun, &tmf_task);
113429d77690SJohn Garry }
113529d77690SJohn Garry EXPORT_SYMBOL_GPL(sas_lu_reset);
113629d77690SJohn Garry
sas_query_task(struct sas_task * task,u16 tag)113772f8810eSJohn Garry int sas_query_task(struct sas_task *task, u16 tag)
113872f8810eSJohn Garry {
113972f8810eSJohn Garry struct sas_tmf_task tmf_task = {
114072f8810eSJohn Garry .tmf = TMF_QUERY_TASK,
114172f8810eSJohn Garry .tag_of_task_to_be_managed = tag,
114272f8810eSJohn Garry };
114372f8810eSJohn Garry struct scsi_cmnd *cmnd = task->uldd_task;
114472f8810eSJohn Garry struct domain_device *dev = task->dev;
114572f8810eSJohn Garry struct scsi_lun lun;
114672f8810eSJohn Garry
114772f8810eSJohn Garry int_to_scsilun(cmnd->device->lun, &lun);
114872f8810eSJohn Garry
114972f8810eSJohn Garry return sas_execute_ssp_tmf(dev, lun.scsi_lun, &tmf_task);
115072f8810eSJohn Garry }
115172f8810eSJohn Garry EXPORT_SYMBOL_GPL(sas_query_task);
115272f8810eSJohn Garry
sas_abort_task(struct sas_task * task,u16 tag)11534fea759eSJohn Garry int sas_abort_task(struct sas_task *task, u16 tag)
11544fea759eSJohn Garry {
11554fea759eSJohn Garry struct sas_tmf_task tmf_task = {
11564fea759eSJohn Garry .tmf = TMF_ABORT_TASK,
11574fea759eSJohn Garry .tag_of_task_to_be_managed = tag,
11584fea759eSJohn Garry };
11594fea759eSJohn Garry struct scsi_cmnd *cmnd = task->uldd_task;
11604fea759eSJohn Garry struct domain_device *dev = task->dev;
11614fea759eSJohn Garry struct scsi_lun lun;
11624fea759eSJohn Garry
11634fea759eSJohn Garry int_to_scsilun(cmnd->device->lun, &lun);
11644fea759eSJohn Garry
11654fea759eSJohn Garry return sas_execute_ssp_tmf(dev, lun.scsi_lun, &tmf_task);
11664fea759eSJohn Garry }
11674fea759eSJohn Garry EXPORT_SYMBOL_GPL(sas_abort_task);
11684fea759eSJohn Garry
1169396819fbSDarrick J. Wong /*
1170396819fbSDarrick J. Wong * Tell an upper layer that it needs to initiate an abort for a given task.
1171396819fbSDarrick J. Wong * This should only ever be called by an LLDD.
1172396819fbSDarrick J. Wong */
sas_task_abort(struct sas_task * task)1173396819fbSDarrick J. Wong void sas_task_abort(struct sas_task *task)
117479a5eb60SDarrick J. Wong {
1175396819fbSDarrick J. Wong struct scsi_cmnd *sc = task->uldd_task;
117679a5eb60SDarrick J. Wong
1177396819fbSDarrick J. Wong /* Escape for libsas internal commands */
1178396819fbSDarrick J. Wong if (!sc) {
1179f0bf750cSDan Williams struct sas_task_slow *slow = task->slow_task;
1180f0bf750cSDan Williams
1181f0bf750cSDan Williams if (!slow)
118279a5eb60SDarrick J. Wong return;
1183f0bf750cSDan Williams if (!del_timer(&slow->timer))
1184f0bf750cSDan Williams return;
1185841b86f3SKees Cook slow->timer.function(&slow->timer);
1186396819fbSDarrick J. Wong return;
1187396819fbSDarrick J. Wong }
118879a5eb60SDarrick J. Wong
118939795d65SChristoph Hellwig if (dev_is_sata(task->dev))
11903a2755afSDarrick J. Wong sas_ata_task_abort(task);
119139795d65SChristoph Hellwig else
1192cad1a780SBart Van Assche blk_abort_request(scsi_cmd_to_rq(sc));
11931b4d0d8eSJames Bottomley }
1194ce4fc333SJohn Garry EXPORT_SYMBOL_GPL(sas_task_abort);
119579a5eb60SDarrick J. Wong
sas_slave_alloc(struct scsi_device * sdev)119649da96d7SYufen Yu int sas_slave_alloc(struct scsi_device *sdev)
119749da96d7SYufen Yu {
119849da96d7SYufen Yu if (dev_is_sata(sdev_to_domain_dev(sdev)) && sdev->lun)
119949da96d7SYufen Yu return -ENXIO;
120049da96d7SYufen Yu
120149da96d7SYufen Yu return 0;
120249da96d7SYufen Yu }
1203ce4fc333SJohn Garry EXPORT_SYMBOL_GPL(sas_slave_alloc);
120449da96d7SYufen Yu
sas_target_destroy(struct scsi_target * starget)1205fa1c1e8fSDarrick J. Wong void sas_target_destroy(struct scsi_target *starget)
1206fa1c1e8fSDarrick J. Wong {
1207735f7d2fSDan Williams struct domain_device *found_dev = starget->hostdata;
1208fa1c1e8fSDarrick J. Wong
1209fa1c1e8fSDarrick J. Wong if (!found_dev)
1210fa1c1e8fSDarrick J. Wong return;
1211fa1c1e8fSDarrick J. Wong
1212735f7d2fSDan Williams starget->hostdata = NULL;
1213735f7d2fSDan Williams sas_put_device(found_dev);
1214fa1c1e8fSDarrick J. Wong }
1215ce4fc333SJohn Garry EXPORT_SYMBOL_GPL(sas_target_destroy);
1216fa1c1e8fSDarrick J. Wong
121745e6cdf4SDarrick J. Wong #define SAS_STRING_ADDR_SIZE 16
121845e6cdf4SDarrick J. Wong
sas_request_addr(struct Scsi_Host * shost,u8 * addr)121945e6cdf4SDarrick J. Wong int sas_request_addr(struct Scsi_Host *shost, u8 *addr)
122045e6cdf4SDarrick J. Wong {
122145e6cdf4SDarrick J. Wong int res;
122245e6cdf4SDarrick J. Wong const struct firmware *fw;
122345e6cdf4SDarrick J. Wong
122445e6cdf4SDarrick J. Wong res = request_firmware(&fw, "sas_addr", &shost->shost_gendev);
122545e6cdf4SDarrick J. Wong if (res)
122645e6cdf4SDarrick J. Wong return res;
122745e6cdf4SDarrick J. Wong
122845e6cdf4SDarrick J. Wong if (fw->size < SAS_STRING_ADDR_SIZE) {
122945e6cdf4SDarrick J. Wong res = -ENODEV;
123045e6cdf4SDarrick J. Wong goto out;
123145e6cdf4SDarrick J. Wong }
123245e6cdf4SDarrick J. Wong
12339ea4e076SAndy Shevchenko res = hex2bin(addr, fw->data, strnlen(fw->data, SAS_ADDR_SIZE * 2) / 2);
12349ea4e076SAndy Shevchenko if (res)
12359ea4e076SAndy Shevchenko goto out;
123645e6cdf4SDarrick J. Wong
123745e6cdf4SDarrick J. Wong out:
123845e6cdf4SDarrick J. Wong release_firmware(fw);
123945e6cdf4SDarrick J. Wong return res;
124045e6cdf4SDarrick J. Wong }
124145e6cdf4SDarrick J. Wong EXPORT_SYMBOL_GPL(sas_request_addr);
124245e6cdf4SDarrick J. Wong
1243