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 */ 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_SG_ERR: 712908d778SJames Bottomley hs = DID_PARITY; 722908d778SJames Bottomley break; 732908d778SJames Bottomley case SAS_OPEN_REJECT: 742908d778SJames Bottomley if (ts->open_rej_reason == SAS_OREJ_RSVD_RETRY) 752908d778SJames Bottomley hs = DID_SOFT_ERROR; /* retry */ 762908d778SJames Bottomley else 772908d778SJames Bottomley hs = DID_ERROR; 782908d778SJames Bottomley break; 792908d778SJames Bottomley case SAS_PROTO_RESPONSE: 8015ba7806SJohn Garry pr_notice("LLDD:%s sent SAS_PROTO_RESP for an SSP task; please report this\n", 812908d778SJames Bottomley task->dev->port->ha->sas_ha_name); 822908d778SJames Bottomley break; 832908d778SJames Bottomley case SAS_ABORTED_TASK: 842908d778SJames Bottomley hs = DID_ABORT; 852908d778SJames Bottomley break; 864be6181fSJohn Garry case SAS_SAM_STAT_CHECK_CONDITION: 872908d778SJames Bottomley memcpy(sc->sense_buffer, ts->buf, 88cc75e8abSFUJITA Tomonori min(SCSI_SENSE_BUFFERSIZE, ts->buf_valid_size)); 894be6181fSJohn Garry stat = SAS_SAM_STAT_CHECK_CONDITION; 902908d778SJames Bottomley break; 912908d778SJames Bottomley default: 922908d778SJames Bottomley stat = ts->stat; 932908d778SJames Bottomley break; 942908d778SJames Bottomley } 952908d778SJames Bottomley } 96a3a14252SDan Williams 972908d778SJames Bottomley sc->result = (hs << 16) | stat; 98a3a14252SDan Williams ASSIGN_SAS_TASK(sc, NULL); 992908d778SJames Bottomley sas_free_task(task); 100a3a14252SDan Williams } 101a3a14252SDan Williams 102a3a14252SDan Williams static void sas_scsi_task_done(struct sas_task *task) 103a3a14252SDan Williams { 104a3a14252SDan Williams struct scsi_cmnd *sc = task->uldd_task; 1059095a64aSDan Williams struct domain_device *dev = task->dev; 1069095a64aSDan Williams struct sas_ha_struct *ha = dev->port->ha; 1079095a64aSDan Williams unsigned long flags; 108a3a14252SDan Williams 1099095a64aSDan Williams spin_lock_irqsave(&dev->done_lock, flags); 1109095a64aSDan Williams if (test_bit(SAS_HA_FROZEN, &ha->state)) 1119095a64aSDan Williams task = NULL; 1129095a64aSDan Williams else 1139095a64aSDan Williams ASSIGN_SAS_TASK(sc, NULL); 1149095a64aSDan Williams spin_unlock_irqrestore(&dev->done_lock, flags); 1159095a64aSDan Williams 1169095a64aSDan Williams if (unlikely(!task)) { 1179095a64aSDan Williams /* task will be completed by the error handler */ 11815ba7806SJohn Garry pr_debug("task done but aborted\n"); 119a3a14252SDan Williams return; 120a3a14252SDan Williams } 121a3a14252SDan Williams 122a3a14252SDan Williams if (unlikely(!sc)) { 12315ba7806SJohn Garry pr_debug("task_done called with non existing SCSI cmnd!\n"); 124a3a14252SDan Williams sas_free_task(task); 125a3a14252SDan Williams return; 126a3a14252SDan Williams } 127a3a14252SDan Williams 128a3a14252SDan Williams sas_end_task(sc, task); 129e803bc52SBart Van Assche scsi_done(sc); 1302908d778SJames Bottomley } 1312908d778SJames Bottomley 1322908d778SJames Bottomley static struct sas_task *sas_create_task(struct scsi_cmnd *cmd, 1332908d778SJames Bottomley struct domain_device *dev, 1343cc27547SAl Viro gfp_t gfp_flags) 1352908d778SJames Bottomley { 1362908d778SJames Bottomley struct sas_task *task = sas_alloc_task(gfp_flags); 1372908d778SJames Bottomley struct scsi_lun lun; 1382908d778SJames Bottomley 1392908d778SJames Bottomley if (!task) 1402908d778SJames Bottomley return NULL; 1412908d778SJames Bottomley 1422908d778SJames Bottomley task->uldd_task = cmd; 1432908d778SJames Bottomley ASSIGN_SAS_TASK(cmd, task); 1442908d778SJames Bottomley 1452908d778SJames Bottomley task->dev = dev; 1462908d778SJames Bottomley task->task_proto = task->dev->tproto; /* BUG_ON(!SSP) */ 1472908d778SJames Bottomley 1482908d778SJames Bottomley task->ssp_task.retry_count = 1; 1492908d778SJames Bottomley int_to_scsilun(cmd->device->lun, &lun); 1502908d778SJames Bottomley memcpy(task->ssp_task.LUN, &lun.scsi_lun, 8); 1519cbbdca4STejun Heo task->ssp_task.task_attr = TASK_ATTR_SIMPLE; 152e73823f7SJames Bottomley task->ssp_task.cmd = cmd; 1532908d778SJames Bottomley 154c13e5566SFUJITA Tomonori task->scatter = scsi_sglist(cmd); 155c13e5566SFUJITA Tomonori task->num_scatter = scsi_sg_count(cmd); 156c13e5566SFUJITA Tomonori task->total_xfer_len = scsi_bufflen(cmd); 1572908d778SJames Bottomley task->data_dir = cmd->sc_data_direction; 1582908d778SJames Bottomley 1592908d778SJames Bottomley task->task_done = sas_scsi_task_done; 1602908d778SJames Bottomley 1612908d778SJames Bottomley return task; 1622908d778SJames Bottomley } 1632908d778SJames Bottomley 16492bd401bSChristoph Hellwig int sas_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd) 1652908d778SJames Bottomley { 1662908d778SJames Bottomley struct sas_internal *i = to_sas_internal(host->transportt); 167a923f756SChristoph Hellwig struct domain_device *dev = cmd_to_domain_dev(cmd); 1682908d778SJames Bottomley struct sas_task *task; 169a923f756SChristoph Hellwig int res = 0; 1702908d778SJames Bottomley 1713673f4bfSDan Williams /* If the device fell off, no sense in issuing commands */ 172e139942dSDan Williams if (test_bit(SAS_DEV_GONE, &dev->state)) { 1733673f4bfSDan Williams cmd->result = DID_BAD_TARGET << 16; 174a923f756SChristoph Hellwig goto out_done; 1753673f4bfSDan Williams } 1763673f4bfSDan Williams 177fa1c1e8fSDarrick J. Wong if (dev_is_sata(dev)) { 178312d3e56SDan Williams spin_lock_irq(dev->sata_dev.ap->lock); 179b27dcfb0SJeff Garzik res = ata_sas_queuecmd(cmd, dev->sata_dev.ap); 180312d3e56SDan Williams spin_unlock_irq(dev->sata_dev.ap->lock); 181a923f756SChristoph Hellwig return res; 182fa1c1e8fSDarrick J. Wong } 183fa1c1e8fSDarrick J. Wong 1842908d778SJames Bottomley task = sas_create_task(cmd, dev, GFP_ATOMIC); 1852908d778SJames Bottomley if (!task) 186ac81c6a8SChristoph Hellwig return SCSI_MLQUEUE_HOST_BUSY; 1872908d778SJames Bottomley 18879855d17SChristoph Hellwig res = i->dft->lldd_execute_task(task, GFP_ATOMIC); 189a923f756SChristoph Hellwig if (res) 190a923f756SChristoph Hellwig goto out_free_task; 191a923f756SChristoph Hellwig return 0; 192a923f756SChristoph Hellwig 193a923f756SChristoph Hellwig out_free_task: 19415ba7806SJohn Garry pr_debug("lldd_execute_task returned: %d\n", res); 1952908d778SJames Bottomley ASSIGN_SAS_TASK(cmd, NULL); 1962908d778SJames Bottomley sas_free_task(task); 197ac81c6a8SChristoph Hellwig if (res == -SAS_QUEUE_FULL) 198a923f756SChristoph Hellwig cmd->result = DID_SOFT_ERROR << 16; /* retry */ 199ac81c6a8SChristoph Hellwig else 200ac81c6a8SChristoph Hellwig cmd->result = DID_ERROR << 16; 201a923f756SChristoph Hellwig out_done: 202e803bc52SBart Van Assche scsi_done(cmd); 203a923f756SChristoph Hellwig return 0; 2042908d778SJames Bottomley } 205ce4fc333SJohn Garry EXPORT_SYMBOL_GPL(sas_queuecommand); 2062908d778SJames Bottomley 207a8e14fecSJames Bottomley static void sas_eh_finish_cmd(struct scsi_cmnd *cmd) 208a8e14fecSJames Bottomley { 209a8e14fecSJames Bottomley struct sas_ha_struct *sas_ha = SHOST_TO_SAS_HA(cmd->device->host); 210318aaf34SJason Yan struct domain_device *dev = cmd_to_domain_dev(cmd); 21145c73b65SDan Williams struct sas_task *task = TO_SAS_TASK(cmd); 212a8e14fecSJames Bottomley 213a3a14252SDan Williams /* At this point, we only get called following an actual abort 214a3a14252SDan Williams * of the task, so we should be guaranteed not to be racing with 215a3a14252SDan Williams * any completions from the LLD. Task is freed after this. 216a3a14252SDan Williams */ 217a3a14252SDan Williams sas_end_task(cmd, task); 218a3a14252SDan Williams 219318aaf34SJason Yan if (dev_is_sata(dev)) { 220318aaf34SJason Yan /* defer commands to libata so that libata EH can 221318aaf34SJason Yan * handle ata qcs correctly 222318aaf34SJason Yan */ 223318aaf34SJason Yan list_move_tail(&cmd->eh_entry, &sas_ha->eh_ata_q); 224318aaf34SJason Yan return; 225318aaf34SJason Yan } 226318aaf34SJason Yan 227a8e14fecSJames Bottomley /* now finish the command and move it on to the error 228a8e14fecSJames Bottomley * handler done list, this also takes it off the 229a3a14252SDan Williams * error handler pending list. 230a3a14252SDan Williams */ 231a8e14fecSJames Bottomley scsi_eh_finish_cmd(cmd, &sas_ha->eh_done_q); 232a8e14fecSJames Bottomley } 233a8e14fecSJames Bottomley 2342908d778SJames Bottomley static void sas_scsi_clear_queue_lu(struct list_head *error_q, struct scsi_cmnd *my_cmd) 2352908d778SJames Bottomley { 2362908d778SJames Bottomley struct scsi_cmnd *cmd, *n; 2372908d778SJames Bottomley 2382908d778SJames Bottomley list_for_each_entry_safe(cmd, n, error_q, eh_entry) { 23963e4563bSJames Bottomley if (cmd->device->sdev_target == my_cmd->device->sdev_target && 24063e4563bSJames Bottomley cmd->device->lun == my_cmd->device->lun) 241318aaf34SJason Yan sas_eh_finish_cmd(cmd); 2422908d778SJames Bottomley } 2432908d778SJames Bottomley } 2442908d778SJames Bottomley 2452908d778SJames Bottomley static void sas_scsi_clear_queue_I_T(struct list_head *error_q, 2462908d778SJames Bottomley struct domain_device *dev) 2472908d778SJames Bottomley { 2482908d778SJames Bottomley struct scsi_cmnd *cmd, *n; 2492908d778SJames Bottomley 2502908d778SJames Bottomley list_for_each_entry_safe(cmd, n, error_q, eh_entry) { 2512908d778SJames Bottomley struct domain_device *x = cmd_to_domain_dev(cmd); 2522908d778SJames Bottomley 2532908d778SJames Bottomley if (x == dev) 254a8e14fecSJames Bottomley sas_eh_finish_cmd(cmd); 2552908d778SJames Bottomley } 2562908d778SJames Bottomley } 2572908d778SJames Bottomley 2582908d778SJames Bottomley static void sas_scsi_clear_queue_port(struct list_head *error_q, 2592908d778SJames Bottomley struct asd_sas_port *port) 2602908d778SJames Bottomley { 2612908d778SJames Bottomley struct scsi_cmnd *cmd, *n; 2622908d778SJames Bottomley 2632908d778SJames Bottomley list_for_each_entry_safe(cmd, n, error_q, eh_entry) { 2642908d778SJames Bottomley struct domain_device *dev = cmd_to_domain_dev(cmd); 2652908d778SJames Bottomley struct asd_sas_port *x = dev->port; 2662908d778SJames Bottomley 2672908d778SJames Bottomley if (x == port) 268a8e14fecSJames Bottomley sas_eh_finish_cmd(cmd); 2692908d778SJames Bottomley } 2702908d778SJames Bottomley } 2712908d778SJames Bottomley 2722908d778SJames Bottomley enum task_disposition { 2732908d778SJames Bottomley TASK_IS_DONE, 2742908d778SJames Bottomley TASK_IS_ABORTED, 2752908d778SJames Bottomley TASK_IS_AT_LU, 2762908d778SJames Bottomley TASK_IS_NOT_AT_LU, 27702cd743bSDarrick J. Wong TASK_ABORT_FAILED, 2782908d778SJames Bottomley }; 2792908d778SJames Bottomley 2802908d778SJames Bottomley static enum task_disposition sas_scsi_find_task(struct sas_task *task) 2812908d778SJames Bottomley { 2822908d778SJames Bottomley unsigned long flags; 2832908d778SJames Bottomley int i, res; 2842908d778SJames Bottomley struct sas_internal *si = 2852908d778SJames Bottomley to_sas_internal(task->dev->port->ha->core.shost->transportt); 2862908d778SJames Bottomley 2872908d778SJames Bottomley for (i = 0; i < 5; i++) { 28815ba7806SJohn Garry pr_notice("%s: aborting task 0x%p\n", __func__, task); 2892908d778SJames Bottomley res = si->dft->lldd_abort_task(task); 2902908d778SJames Bottomley 2912908d778SJames Bottomley spin_lock_irqsave(&task->task_state_lock, flags); 2922908d778SJames Bottomley if (task->task_state_flags & SAS_TASK_STATE_DONE) { 2932908d778SJames Bottomley spin_unlock_irqrestore(&task->task_state_lock, flags); 29415ba7806SJohn Garry pr_debug("%s: task 0x%p is done\n", __func__, task); 2952908d778SJames Bottomley return TASK_IS_DONE; 2962908d778SJames Bottomley } 2972908d778SJames Bottomley spin_unlock_irqrestore(&task->task_state_lock, flags); 2982908d778SJames Bottomley 2992908d778SJames Bottomley if (res == TMF_RESP_FUNC_COMPLETE) { 30015ba7806SJohn Garry pr_notice("%s: task 0x%p is aborted\n", 301cadbd4a5SHarvey Harrison __func__, task); 3022908d778SJames Bottomley return TASK_IS_ABORTED; 3032908d778SJames Bottomley } else if (si->dft->lldd_query_task) { 30415ba7806SJohn Garry pr_notice("%s: querying task 0x%p\n", __func__, task); 3052908d778SJames Bottomley res = si->dft->lldd_query_task(task); 30602cd743bSDarrick J. Wong switch (res) { 30702cd743bSDarrick J. Wong case TMF_RESP_FUNC_SUCC: 30815ba7806SJohn Garry pr_notice("%s: task 0x%p at LU\n", __func__, 30915ba7806SJohn Garry task); 3102908d778SJames Bottomley return TASK_IS_AT_LU; 31102cd743bSDarrick J. Wong case TMF_RESP_FUNC_COMPLETE: 31215ba7806SJohn Garry pr_notice("%s: task 0x%p not at LU\n", 313cadbd4a5SHarvey Harrison __func__, task); 3142908d778SJames Bottomley return TASK_IS_NOT_AT_LU; 31502cd743bSDarrick J. Wong case TMF_RESP_FUNC_FAILED: 31615ba7806SJohn Garry pr_notice("%s: task 0x%p failed to abort\n", 317cadbd4a5SHarvey Harrison __func__, task); 31802cd743bSDarrick J. Wong return TASK_ABORT_FAILED; 319*9aacf6feSJohn Garry default: 320*9aacf6feSJohn Garry pr_notice("%s: task 0x%p result code %d not handled\n", 321*9aacf6feSJohn Garry __func__, task, res); 3222908d778SJames Bottomley } 3232908d778SJames Bottomley } 324*9aacf6feSJohn Garry } 325*9aacf6feSJohn Garry return TASK_ABORT_FAILED; 3262908d778SJames Bottomley } 3272908d778SJames Bottomley 3282908d778SJames Bottomley static int sas_recover_lu(struct domain_device *dev, struct scsi_cmnd *cmd) 3292908d778SJames Bottomley { 3302908d778SJames Bottomley int res = TMF_RESP_FUNC_FAILED; 3312908d778SJames Bottomley struct scsi_lun lun; 3322908d778SJames Bottomley struct sas_internal *i = 3332908d778SJames Bottomley to_sas_internal(dev->port->ha->core.shost->transportt); 3342908d778SJames Bottomley 3352908d778SJames Bottomley int_to_scsilun(cmd->device->lun, &lun); 3362908d778SJames Bottomley 337b3e3d4c6SJohn Garry pr_notice("eh: device %016llx LUN 0x%llx has the task\n", 3382908d778SJames Bottomley SAS_ADDR(dev->sas_addr), 3392908d778SJames Bottomley cmd->device->lun); 3402908d778SJames Bottomley 3412908d778SJames Bottomley if (i->dft->lldd_abort_task_set) 3422908d778SJames Bottomley res = i->dft->lldd_abort_task_set(dev, lun.scsi_lun); 3432908d778SJames Bottomley 3442908d778SJames Bottomley if (res == TMF_RESP_FUNC_FAILED) { 3452908d778SJames Bottomley if (i->dft->lldd_clear_task_set) 3462908d778SJames Bottomley res = i->dft->lldd_clear_task_set(dev, lun.scsi_lun); 3472908d778SJames Bottomley } 3482908d778SJames Bottomley 3492908d778SJames Bottomley if (res == TMF_RESP_FUNC_FAILED) { 3502908d778SJames Bottomley if (i->dft->lldd_lu_reset) 3512908d778SJames Bottomley res = i->dft->lldd_lu_reset(dev, lun.scsi_lun); 3522908d778SJames Bottomley } 3532908d778SJames Bottomley 3542908d778SJames Bottomley return res; 3552908d778SJames Bottomley } 3562908d778SJames Bottomley 3572908d778SJames Bottomley static int sas_recover_I_T(struct domain_device *dev) 3582908d778SJames Bottomley { 3592908d778SJames Bottomley int res = TMF_RESP_FUNC_FAILED; 3602908d778SJames Bottomley struct sas_internal *i = 3612908d778SJames Bottomley to_sas_internal(dev->port->ha->core.shost->transportt); 3622908d778SJames Bottomley 36315ba7806SJohn Garry pr_notice("I_T nexus reset for dev %016llx\n", 3642908d778SJames Bottomley SAS_ADDR(dev->sas_addr)); 3652908d778SJames Bottomley 3662908d778SJames Bottomley if (i->dft->lldd_I_T_nexus_reset) 3672908d778SJames Bottomley res = i->dft->lldd_I_T_nexus_reset(dev); 3682908d778SJames Bottomley 3692908d778SJames Bottomley return res; 3702908d778SJames Bottomley } 3712908d778SJames Bottomley 372f41a0c44SDan Williams /* take a reference on the last known good phy for this device */ 373f41a0c44SDan Williams struct sas_phy *sas_get_local_phy(struct domain_device *dev) 3743ebf6922SDarrick J. Wong { 375f41a0c44SDan Williams struct sas_ha_struct *ha = dev->port->ha; 376f41a0c44SDan Williams struct sas_phy *phy; 377f41a0c44SDan Williams unsigned long flags; 3783ebf6922SDarrick J. Wong 379f41a0c44SDan Williams /* a published domain device always has a valid phy, it may be 380f41a0c44SDan Williams * stale, but it is never NULL 381f41a0c44SDan Williams */ 382f41a0c44SDan Williams BUG_ON(!dev->phy); 3833ebf6922SDarrick J. Wong 384f41a0c44SDan Williams spin_lock_irqsave(&ha->phy_port_lock, flags); 385f41a0c44SDan Williams phy = dev->phy; 386f41a0c44SDan Williams get_device(&phy->dev); 387f41a0c44SDan Williams spin_unlock_irqrestore(&ha->phy_port_lock, flags); 388f41a0c44SDan Williams 389f41a0c44SDan Williams return phy; 3903ebf6922SDarrick J. Wong } 391f41a0c44SDan Williams EXPORT_SYMBOL_GPL(sas_get_local_phy); 392ad689233SDarrick J. Wong 3935db45bdcSDan Williams static void sas_wait_eh(struct domain_device *dev) 3945db45bdcSDan Williams { 3955db45bdcSDan Williams struct sas_ha_struct *ha = dev->port->ha; 3965db45bdcSDan Williams DEFINE_WAIT(wait); 3975db45bdcSDan Williams 3985db45bdcSDan Williams if (dev_is_sata(dev)) { 3995db45bdcSDan Williams ata_port_wait_eh(dev->sata_dev.ap); 4005db45bdcSDan Williams return; 4015db45bdcSDan Williams } 4025db45bdcSDan Williams retry: 4035db45bdcSDan Williams spin_lock_irq(&ha->lock); 4045db45bdcSDan Williams 4055db45bdcSDan Williams while (test_bit(SAS_DEV_EH_PENDING, &dev->state)) { 4065db45bdcSDan Williams prepare_to_wait(&ha->eh_wait_q, &wait, TASK_UNINTERRUPTIBLE); 4075db45bdcSDan Williams spin_unlock_irq(&ha->lock); 4085db45bdcSDan Williams schedule(); 4095db45bdcSDan Williams spin_lock_irq(&ha->lock); 4105db45bdcSDan Williams } 4115db45bdcSDan Williams finish_wait(&ha->eh_wait_q, &wait); 4125db45bdcSDan Williams 4135db45bdcSDan Williams spin_unlock_irq(&ha->lock); 4145db45bdcSDan Williams 4155db45bdcSDan Williams /* make sure SCSI EH is complete */ 4165db45bdcSDan Williams if (scsi_host_in_recovery(ha->core.shost)) { 4175db45bdcSDan Williams msleep(10); 4185db45bdcSDan Williams goto retry; 4195db45bdcSDan Williams } 4205db45bdcSDan Williams } 4215db45bdcSDan Williams 4229cb78c16SHannes Reinecke static int sas_queue_reset(struct domain_device *dev, int reset_type, 4239cb78c16SHannes Reinecke u64 lun, int wait) 4245db45bdcSDan Williams { 4255db45bdcSDan Williams struct sas_ha_struct *ha = dev->port->ha; 4265db45bdcSDan Williams int scheduled = 0, tries = 100; 4275db45bdcSDan Williams 4285db45bdcSDan Williams /* ata: promote lun reset to bus reset */ 4295db45bdcSDan Williams if (dev_is_sata(dev)) { 4305db45bdcSDan Williams sas_ata_schedule_reset(dev); 4315db45bdcSDan Williams if (wait) 4325db45bdcSDan Williams sas_ata_wait_eh(dev); 4335db45bdcSDan Williams return SUCCESS; 4345db45bdcSDan Williams } 4355db45bdcSDan Williams 4365db45bdcSDan Williams while (!scheduled && tries--) { 4375db45bdcSDan Williams spin_lock_irq(&ha->lock); 4385db45bdcSDan Williams if (!test_bit(SAS_DEV_EH_PENDING, &dev->state) && 4395db45bdcSDan Williams !test_bit(reset_type, &dev->state)) { 4405db45bdcSDan Williams scheduled = 1; 4415db45bdcSDan Williams ha->eh_active++; 4425db45bdcSDan Williams list_add_tail(&dev->ssp_dev.eh_list_node, &ha->eh_dev_q); 4435db45bdcSDan Williams set_bit(SAS_DEV_EH_PENDING, &dev->state); 4445db45bdcSDan Williams set_bit(reset_type, &dev->state); 4455db45bdcSDan Williams int_to_scsilun(lun, &dev->ssp_dev.reset_lun); 4465db45bdcSDan Williams scsi_schedule_eh(ha->core.shost); 4475db45bdcSDan Williams } 4485db45bdcSDan Williams spin_unlock_irq(&ha->lock); 4495db45bdcSDan Williams 4505db45bdcSDan Williams if (wait) 4515db45bdcSDan Williams sas_wait_eh(dev); 4525db45bdcSDan Williams 4535db45bdcSDan Williams if (scheduled) 4545db45bdcSDan Williams return SUCCESS; 4555db45bdcSDan Williams } 4565db45bdcSDan Williams 45715ba7806SJohn Garry pr_warn("%s reset of %s failed\n", 4585db45bdcSDan Williams reset_type == SAS_DEV_LU_RESET ? "LUN" : "Bus", 4595db45bdcSDan Williams dev_name(&dev->rphy->dev)); 4605db45bdcSDan Williams 4615db45bdcSDan Williams return FAILED; 4625db45bdcSDan Williams } 4635db45bdcSDan Williams 4649524c682SDan Williams int sas_eh_abort_handler(struct scsi_cmnd *cmd) 4659524c682SDan Williams { 466c9f92600SHannes Reinecke int res = TMF_RESP_FUNC_FAILED; 4679524c682SDan Williams struct sas_task *task = TO_SAS_TASK(cmd); 4689524c682SDan Williams struct Scsi_Host *host = cmd->device->host; 469c9f92600SHannes Reinecke struct domain_device *dev = cmd_to_domain_dev(cmd); 4709524c682SDan Williams struct sas_internal *i = to_sas_internal(host->transportt); 471c9f92600SHannes Reinecke unsigned long flags; 4729524c682SDan Williams 4739524c682SDan Williams if (!i->dft->lldd_abort_task) 4749524c682SDan Williams return FAILED; 4759524c682SDan Williams 476c9f92600SHannes Reinecke spin_lock_irqsave(host->host_lock, flags); 477c9f92600SHannes Reinecke /* We cannot do async aborts for SATA devices */ 478c9f92600SHannes Reinecke if (dev_is_sata(dev) && !host->host_eh_scheduled) { 479c9f92600SHannes Reinecke spin_unlock_irqrestore(host->host_lock, flags); 480c9f92600SHannes Reinecke return FAILED; 481c9f92600SHannes Reinecke } 482c9f92600SHannes Reinecke spin_unlock_irqrestore(host->host_lock, flags); 483c9f92600SHannes Reinecke 484c9f92600SHannes Reinecke if (task) 4859524c682SDan Williams res = i->dft->lldd_abort_task(task); 486c9f92600SHannes Reinecke else 48715ba7806SJohn Garry pr_notice("no task to abort\n"); 4889524c682SDan Williams if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE) 4899524c682SDan Williams return SUCCESS; 4909524c682SDan Williams 4919524c682SDan Williams return FAILED; 4929524c682SDan Williams } 4939524c682SDan Williams EXPORT_SYMBOL_GPL(sas_eh_abort_handler); 4949524c682SDan Williams 495a9344e68SDarrick J. Wong /* Attempt to send a LUN reset message to a device */ 496ad689233SDarrick J. Wong int sas_eh_device_reset_handler(struct scsi_cmnd *cmd) 4972908d778SJames Bottomley { 498a9344e68SDarrick J. Wong int res; 4995db45bdcSDan Williams struct scsi_lun lun; 5005db45bdcSDan Williams struct Scsi_Host *host = cmd->device->host; 5015db45bdcSDan Williams struct domain_device *dev = cmd_to_domain_dev(cmd); 5025db45bdcSDan Williams struct sas_internal *i = to_sas_internal(host->transportt); 5035db45bdcSDan Williams 5045db45bdcSDan Williams if (current != host->ehandler) 5055db45bdcSDan Williams return sas_queue_reset(dev, SAS_DEV_LU_RESET, cmd->device->lun, 0); 506a9344e68SDarrick J. Wong 507a9344e68SDarrick J. Wong int_to_scsilun(cmd->device->lun, &lun); 508a9344e68SDarrick J. Wong 509a9344e68SDarrick J. Wong if (!i->dft->lldd_lu_reset) 510a9344e68SDarrick J. Wong return FAILED; 511a9344e68SDarrick J. Wong 512a9344e68SDarrick J. Wong res = i->dft->lldd_lu_reset(dev, lun.scsi_lun); 513a9344e68SDarrick J. Wong if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE) 514a9344e68SDarrick J. Wong return SUCCESS; 515a9344e68SDarrick J. Wong 516a9344e68SDarrick J. Wong return FAILED; 517a9344e68SDarrick J. Wong } 518ce4fc333SJohn Garry EXPORT_SYMBOL_GPL(sas_eh_device_reset_handler); 519a9344e68SDarrick J. Wong 520cc199e78SHannes Reinecke int sas_eh_target_reset_handler(struct scsi_cmnd *cmd) 521a9344e68SDarrick J. Wong { 522ad689233SDarrick J. Wong int res; 523e7db8229SDan Williams struct Scsi_Host *host = cmd->device->host; 524e7db8229SDan Williams struct domain_device *dev = cmd_to_domain_dev(cmd); 525e7db8229SDan Williams struct sas_internal *i = to_sas_internal(host->transportt); 526ad689233SDarrick J. Wong 5275db45bdcSDan Williams if (current != host->ehandler) 5285db45bdcSDan Williams return sas_queue_reset(dev, SAS_DEV_RESET, 0, 0); 5295db45bdcSDan Williams 530e7db8229SDan Williams if (!i->dft->lldd_I_T_nexus_reset) 531e7db8229SDan Williams return FAILED; 532f41a0c44SDan Williams 533e7db8229SDan Williams res = i->dft->lldd_I_T_nexus_reset(dev); 534e7db8229SDan Williams if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE || 535e7db8229SDan Williams res == -ENODEV) 536ad689233SDarrick J. Wong return SUCCESS; 537ad689233SDarrick J. Wong 538ad689233SDarrick J. Wong return FAILED; 539ad689233SDarrick J. Wong } 540ce4fc333SJohn Garry EXPORT_SYMBOL_GPL(sas_eh_target_reset_handler); 541ad689233SDarrick J. Wong 542ad689233SDarrick J. Wong /* Try to reset a device */ 5438de3ef25SJames Bottomley static int try_to_reset_cmd_device(struct scsi_cmnd *cmd) 544ad689233SDarrick J. Wong { 545a9344e68SDarrick J. Wong int res; 5468de3ef25SJames Bottomley struct Scsi_Host *shost = cmd->device->host; 547ad689233SDarrick J. Wong 548a9344e68SDarrick J. Wong if (!shost->hostt->eh_device_reset_handler) 549cc199e78SHannes Reinecke goto try_target_reset; 550a9344e68SDarrick J. Wong 551a9344e68SDarrick J. Wong res = shost->hostt->eh_device_reset_handler(cmd); 552a9344e68SDarrick J. Wong if (res == SUCCESS) 553a9344e68SDarrick J. Wong return res; 554a9344e68SDarrick J. Wong 555cc199e78SHannes Reinecke try_target_reset: 556cc199e78SHannes Reinecke if (shost->hostt->eh_target_reset_handler) 557cc199e78SHannes Reinecke return shost->hostt->eh_target_reset_handler(cmd); 558a9344e68SDarrick J. Wong 559a9344e68SDarrick J. Wong return FAILED; 560ad689233SDarrick J. Wong } 561ad689233SDarrick J. Wong 56284023474SDan Williams static void sas_eh_handle_sas_errors(struct Scsi_Host *shost, struct list_head *work_q) 563ad689233SDarrick J. Wong { 5642908d778SJames Bottomley struct scsi_cmnd *cmd, *n; 5652908d778SJames Bottomley enum task_disposition res = TASK_IS_DONE; 5663ebf6922SDarrick J. Wong int tmf_resp, need_reset; 5672908d778SJames Bottomley struct sas_internal *i = to_sas_internal(shost->transportt); 568ad689233SDarrick J. Wong unsigned long flags; 569ad689233SDarrick J. Wong struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost); 57045c73b65SDan Williams LIST_HEAD(done); 5712908d778SJames Bottomley 57245c73b65SDan Williams /* clean out any commands that won the completion vs eh race */ 573ad689233SDarrick J. Wong list_for_each_entry_safe(cmd, n, work_q, eh_entry) { 5749095a64aSDan Williams struct domain_device *dev = cmd_to_domain_dev(cmd); 5759095a64aSDan Williams struct sas_task *task; 5769095a64aSDan Williams 5779095a64aSDan Williams spin_lock_irqsave(&dev->done_lock, flags); 5789095a64aSDan Williams /* by this point the lldd has either observed 5799095a64aSDan Williams * SAS_HA_FROZEN and is leaving the task alone, or has 5809095a64aSDan Williams * won the race with eh and decided to complete it 5819095a64aSDan Williams */ 5829095a64aSDan Williams task = TO_SAS_TASK(cmd); 5839095a64aSDan Williams spin_unlock_irqrestore(&dev->done_lock, flags); 584f456393eSDarrick J. Wong 585ad689233SDarrick J. Wong if (!task) 58645c73b65SDan Williams list_move_tail(&cmd->eh_entry, &done); 58745c73b65SDan Williams } 58845c73b65SDan Williams 58945c73b65SDan Williams Again: 59045c73b65SDan Williams list_for_each_entry_safe(cmd, n, work_q, eh_entry) { 59145c73b65SDan Williams struct sas_task *task = TO_SAS_TASK(cmd); 592ad689233SDarrick J. Wong 593ad689233SDarrick J. Wong list_del_init(&cmd->eh_entry); 5943ebf6922SDarrick J. Wong 5953ebf6922SDarrick J. Wong spin_lock_irqsave(&task->task_state_lock, flags); 5963ebf6922SDarrick J. Wong need_reset = task->task_state_flags & SAS_TASK_NEED_DEV_RESET; 5973ebf6922SDarrick J. Wong spin_unlock_irqrestore(&task->task_state_lock, flags); 5983ebf6922SDarrick J. Wong 5998de3ef25SJames Bottomley if (need_reset) { 60015ba7806SJohn Garry pr_notice("%s: task 0x%p requests reset\n", 601cadbd4a5SHarvey Harrison __func__, task); 6028de3ef25SJames Bottomley goto reset; 6038de3ef25SJames Bottomley } 6048de3ef25SJames Bottomley 60515ba7806SJohn Garry pr_debug("trying to find task 0x%p\n", task); 6062908d778SJames Bottomley res = sas_scsi_find_task(task); 6072908d778SJames Bottomley 6082908d778SJames Bottomley switch (res) { 6092908d778SJames Bottomley case TASK_IS_DONE: 61015ba7806SJohn Garry pr_notice("%s: task 0x%p is done\n", __func__, 6112908d778SJames Bottomley task); 612318aaf34SJason Yan sas_eh_finish_cmd(cmd); 6132908d778SJames Bottomley continue; 6142908d778SJames Bottomley case TASK_IS_ABORTED: 61515ba7806SJohn Garry pr_notice("%s: task 0x%p is aborted\n", 616cadbd4a5SHarvey Harrison __func__, task); 617318aaf34SJason Yan sas_eh_finish_cmd(cmd); 6182908d778SJames Bottomley continue; 6192908d778SJames Bottomley case TASK_IS_AT_LU: 62015ba7806SJohn Garry pr_info("task 0x%p is at LU: lu recover\n", task); 6218de3ef25SJames Bottomley reset: 6222908d778SJames Bottomley tmf_resp = sas_recover_lu(task->dev, cmd); 6232908d778SJames Bottomley if (tmf_resp == TMF_RESP_FUNC_COMPLETE) { 624b3e3d4c6SJohn Garry pr_notice("dev %016llx LU 0x%llx is recovered\n", 6252908d778SJames Bottomley SAS_ADDR(task->dev), 6262908d778SJames Bottomley cmd->device->lun); 627318aaf34SJason Yan sas_eh_finish_cmd(cmd); 628ad689233SDarrick J. Wong sas_scsi_clear_queue_lu(work_q, cmd); 6292908d778SJames Bottomley goto Again; 6302908d778SJames Bottomley } 631df561f66SGustavo A. R. Silva fallthrough; 6322908d778SJames Bottomley case TASK_IS_NOT_AT_LU: 63302cd743bSDarrick J. Wong case TASK_ABORT_FAILED: 63415ba7806SJohn Garry pr_notice("task 0x%p is not at LU: I_T recover\n", 6352908d778SJames Bottomley task); 6362908d778SJames Bottomley tmf_resp = sas_recover_I_T(task->dev); 63726a2e68fSDan Williams if (tmf_resp == TMF_RESP_FUNC_COMPLETE || 63826a2e68fSDan Williams tmf_resp == -ENODEV) { 6398de3ef25SJames Bottomley struct domain_device *dev = task->dev; 64015ba7806SJohn Garry pr_notice("I_T %016llx recovered\n", 6412908d778SJames Bottomley SAS_ADDR(task->dev->sas_addr)); 642a8e14fecSJames Bottomley sas_eh_finish_cmd(cmd); 6438de3ef25SJames Bottomley sas_scsi_clear_queue_I_T(work_q, dev); 6442908d778SJames Bottomley goto Again; 6452908d778SJames Bottomley } 6462908d778SJames Bottomley /* Hammer time :-) */ 6478de3ef25SJames Bottomley try_to_reset_cmd_device(cmd); 6482908d778SJames Bottomley if (i->dft->lldd_clear_nexus_port) { 6492908d778SJames Bottomley struct asd_sas_port *port = task->dev->port; 65015ba7806SJohn Garry pr_debug("clearing nexus for port:%d\n", 6512908d778SJames Bottomley port->id); 6522908d778SJames Bottomley res = i->dft->lldd_clear_nexus_port(port); 6532908d778SJames Bottomley if (res == TMF_RESP_FUNC_COMPLETE) { 65415ba7806SJohn Garry pr_notice("clear nexus port:%d succeeded\n", 65515ba7806SJohn Garry port->id); 656a8e14fecSJames Bottomley sas_eh_finish_cmd(cmd); 657ad689233SDarrick J. Wong sas_scsi_clear_queue_port(work_q, 6582908d778SJames Bottomley port); 6592908d778SJames Bottomley goto Again; 6602908d778SJames Bottomley } 6612908d778SJames Bottomley } 6622908d778SJames Bottomley if (i->dft->lldd_clear_nexus_ha) { 66315ba7806SJohn Garry pr_debug("clear nexus ha\n"); 6642908d778SJames Bottomley res = i->dft->lldd_clear_nexus_ha(ha); 6652908d778SJames Bottomley if (res == TMF_RESP_FUNC_COMPLETE) { 66615ba7806SJohn Garry pr_notice("clear nexus ha succeeded\n"); 667a8e14fecSJames Bottomley sas_eh_finish_cmd(cmd); 668a8e14fecSJames Bottomley goto clear_q; 6692908d778SJames Bottomley } 6702908d778SJames Bottomley } 6712908d778SJames Bottomley /* If we are here -- this means that no amount 6722908d778SJames Bottomley * of effort could recover from errors. Quite 6732908d778SJames Bottomley * possibly the HA just disappeared. 6742908d778SJames Bottomley */ 675b3e3d4c6SJohn Garry pr_err("error from device %016llx, LUN 0x%llx couldn't be recovered in any way\n", 6762908d778SJames Bottomley SAS_ADDR(task->dev->sas_addr), 6772908d778SJames Bottomley cmd->device->lun); 6782908d778SJames Bottomley 679a8e14fecSJames Bottomley sas_eh_finish_cmd(cmd); 6802908d778SJames Bottomley goto clear_q; 6812908d778SJames Bottomley } 6822908d778SJames Bottomley } 68345c73b65SDan Williams out: 68445c73b65SDan Williams list_splice_tail(&done, work_q); 6853944f509SDan Williams list_splice_tail_init(&ha->eh_ata_q, work_q); 68684023474SDan Williams return; 68745c73b65SDan Williams 6882908d778SJames Bottomley clear_q: 68915ba7806SJohn Garry pr_debug("--- Exit %s -- clear_q\n", __func__); 690a8e14fecSJames Bottomley list_for_each_entry_safe(cmd, n, work_q, eh_entry) 691a8e14fecSJames Bottomley sas_eh_finish_cmd(cmd); 69245c73b65SDan Williams goto out; 693ad689233SDarrick J. Wong } 694ad689233SDarrick J. Wong 6955db45bdcSDan Williams static void sas_eh_handle_resets(struct Scsi_Host *shost) 6965db45bdcSDan Williams { 6975db45bdcSDan Williams struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost); 6985db45bdcSDan Williams struct sas_internal *i = to_sas_internal(shost->transportt); 6995db45bdcSDan Williams 7005db45bdcSDan Williams /* handle directed resets to sas devices */ 7015db45bdcSDan Williams spin_lock_irq(&ha->lock); 7025db45bdcSDan Williams while (!list_empty(&ha->eh_dev_q)) { 7035db45bdcSDan Williams struct domain_device *dev; 7045db45bdcSDan Williams struct ssp_device *ssp; 7055db45bdcSDan Williams 7065db45bdcSDan Williams ssp = list_entry(ha->eh_dev_q.next, typeof(*ssp), eh_list_node); 7075db45bdcSDan Williams list_del_init(&ssp->eh_list_node); 7085db45bdcSDan Williams dev = container_of(ssp, typeof(*dev), ssp_dev); 7095db45bdcSDan Williams kref_get(&dev->kref); 7105db45bdcSDan Williams WARN_ONCE(dev_is_sata(dev), "ssp reset to ata device?\n"); 7115db45bdcSDan Williams 7125db45bdcSDan Williams spin_unlock_irq(&ha->lock); 7135db45bdcSDan Williams 7145db45bdcSDan Williams if (test_and_clear_bit(SAS_DEV_LU_RESET, &dev->state)) 7155db45bdcSDan Williams i->dft->lldd_lu_reset(dev, ssp->reset_lun.scsi_lun); 7165db45bdcSDan Williams 7175db45bdcSDan Williams if (test_and_clear_bit(SAS_DEV_RESET, &dev->state)) 7185db45bdcSDan Williams i->dft->lldd_I_T_nexus_reset(dev); 7195db45bdcSDan Williams 7205db45bdcSDan Williams sas_put_device(dev); 7215db45bdcSDan Williams spin_lock_irq(&ha->lock); 7225db45bdcSDan Williams clear_bit(SAS_DEV_EH_PENDING, &dev->state); 7235db45bdcSDan Williams ha->eh_active--; 7245db45bdcSDan Williams } 7255db45bdcSDan Williams spin_unlock_irq(&ha->lock); 7265db45bdcSDan Williams } 7275db45bdcSDan Williams 728e4a9c373SDan Williams 729ad689233SDarrick J. Wong void sas_scsi_recover_host(struct Scsi_Host *shost) 730ad689233SDarrick J. Wong { 731ad689233SDarrick J. Wong struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost); 732ad689233SDarrick J. Wong LIST_HEAD(eh_work_q); 733e4a9c373SDan Williams int tries = 0; 734e4a9c373SDan Williams bool retry; 735ad689233SDarrick J. Wong 736e4a9c373SDan Williams retry: 737e4a9c373SDan Williams tries++; 738e4a9c373SDan Williams retry = true; 739e4a9c373SDan Williams spin_lock_irq(shost->host_lock); 740ad689233SDarrick J. Wong list_splice_init(&shost->eh_cmd_q, &eh_work_q); 741e4a9c373SDan Williams spin_unlock_irq(shost->host_lock); 742ad689233SDarrick J. Wong 74315ba7806SJohn Garry pr_notice("Enter %s busy: %d failed: %d\n", 744c84b023aSMing Lei __func__, scsi_host_busy(shost), shost->host_failed); 745ad689233SDarrick J. Wong /* 746ad689233SDarrick J. Wong * Deal with commands that still have SAS tasks (i.e. they didn't 74784023474SDan Williams * complete via the normal sas_task completion mechanism), 74884023474SDan Williams * SAS_HA_FROZEN gives eh dominion over all sas_task completion. 749ad689233SDarrick J. Wong */ 7509095a64aSDan Williams set_bit(SAS_HA_FROZEN, &ha->state); 75184023474SDan Williams sas_eh_handle_sas_errors(shost, &eh_work_q); 75284023474SDan Williams clear_bit(SAS_HA_FROZEN, &ha->state); 75384023474SDan Williams if (list_empty(&eh_work_q)) 754ad689233SDarrick J. Wong goto out; 755ad689233SDarrick J. Wong 756ad689233SDarrick J. Wong /* 757ad689233SDarrick J. Wong * Now deal with SCSI commands that completed ok but have a an error 758ad689233SDarrick J. Wong * code (and hopefully sense data) attached. This is roughly what 759ad689233SDarrick J. Wong * scsi_unjam_host does, but we skip scsi_eh_abort_cmds because any 760ad689233SDarrick J. Wong * command we see here has no sas_task and is thus unknown to the HA. 761ad689233SDarrick J. Wong */ 7623a20e642SXiang Chen sas_ata_eh(shost, &eh_work_q); 763ad689233SDarrick J. Wong if (!scsi_eh_get_sense(&eh_work_q, &ha->eh_done_q)) 764ad689233SDarrick J. Wong scsi_eh_ready_devs(shost, &eh_work_q, &ha->eh_done_q); 765ad689233SDarrick J. Wong 766ad689233SDarrick J. Wong out: 7675db45bdcSDan Williams sas_eh_handle_resets(shost); 7685db45bdcSDan Williams 76900dd4998SJames Bottomley /* now link into libata eh --- if we have any ata devices */ 77000dd4998SJames Bottomley sas_ata_strategy_handler(shost); 77100dd4998SJames Bottomley 772ad689233SDarrick J. Wong scsi_eh_flush_done_q(&ha->eh_done_q); 77300dd4998SJames Bottomley 774e4a9c373SDan Williams /* check if any new eh work was scheduled during the last run */ 775e4a9c373SDan Williams spin_lock_irq(&ha->lock); 776e4a9c373SDan Williams if (ha->eh_active == 0) { 777e4a9c373SDan Williams shost->host_eh_scheduled = 0; 778e4a9c373SDan Williams retry = false; 779e4a9c373SDan Williams } 780e4a9c373SDan Williams spin_unlock_irq(&ha->lock); 781e4a9c373SDan Williams 782e4a9c373SDan Williams if (retry) 783e4a9c373SDan Williams goto retry; 784e4a9c373SDan Williams 78515ba7806SJohn Garry pr_notice("--- Exit %s: busy: %d failed: %d tries: %d\n", 786c84b023aSMing Lei __func__, scsi_host_busy(shost), 78774665016SChristoph Hellwig shost->host_failed, tries); 7882908d778SJames Bottomley } 7892908d778SJames Bottomley 7906f4e626fSNathan Chancellor int sas_ioctl(struct scsi_device *sdev, unsigned int cmd, void __user *arg) 791fa1c1e8fSDarrick J. Wong { 792fa1c1e8fSDarrick J. Wong struct domain_device *dev = sdev_to_domain_dev(sdev); 793fa1c1e8fSDarrick J. Wong 794fa1c1e8fSDarrick J. Wong if (dev_is_sata(dev)) 79594be9a58SJeff Garzik return ata_sas_scsi_ioctl(dev->sata_dev.ap, sdev, cmd, arg); 796fa1c1e8fSDarrick J. Wong 797fa1c1e8fSDarrick J. Wong return -EINVAL; 798fa1c1e8fSDarrick J. Wong } 799ce4fc333SJohn Garry EXPORT_SYMBOL_GPL(sas_ioctl); 800fa1c1e8fSDarrick J. Wong 8012908d778SJames Bottomley struct domain_device *sas_find_dev_by_rphy(struct sas_rphy *rphy) 8022908d778SJames Bottomley { 8032908d778SJames Bottomley struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent); 8042908d778SJames Bottomley struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost); 8052908d778SJames Bottomley struct domain_device *found_dev = NULL; 8062908d778SJames Bottomley int i; 807980fa2f9SDarrick J. Wong unsigned long flags; 8082908d778SJames Bottomley 809980fa2f9SDarrick J. Wong spin_lock_irqsave(&ha->phy_port_lock, flags); 8102908d778SJames Bottomley for (i = 0; i < ha->num_phys; i++) { 8112908d778SJames Bottomley struct asd_sas_port *port = ha->sas_port[i]; 8122908d778SJames Bottomley struct domain_device *dev; 8132908d778SJames Bottomley 8142908d778SJames Bottomley spin_lock(&port->dev_list_lock); 8152908d778SJames Bottomley list_for_each_entry(dev, &port->dev_list, dev_list_node) { 8162908d778SJames Bottomley if (rphy == dev->rphy) { 8172908d778SJames Bottomley found_dev = dev; 8182908d778SJames Bottomley spin_unlock(&port->dev_list_lock); 8192908d778SJames Bottomley goto found; 8202908d778SJames Bottomley } 8212908d778SJames Bottomley } 8222908d778SJames Bottomley spin_unlock(&port->dev_list_lock); 8232908d778SJames Bottomley } 8242908d778SJames Bottomley found: 825980fa2f9SDarrick J. Wong spin_unlock_irqrestore(&ha->phy_port_lock, flags); 8262908d778SJames Bottomley 8272908d778SJames Bottomley return found_dev; 8282908d778SJames Bottomley } 8292908d778SJames Bottomley 8302908d778SJames Bottomley int sas_target_alloc(struct scsi_target *starget) 8312908d778SJames Bottomley { 832735f7d2fSDan Williams struct sas_rphy *rphy = dev_to_rphy(starget->dev.parent); 833735f7d2fSDan Williams struct domain_device *found_dev = sas_find_dev_by_rphy(rphy); 8342908d778SJames Bottomley 8352908d778SJames Bottomley if (!found_dev) 8362908d778SJames Bottomley return -ENODEV; 8372908d778SJames Bottomley 838735f7d2fSDan Williams kref_get(&found_dev->kref); 8392908d778SJames Bottomley starget->hostdata = found_dev; 8402908d778SJames Bottomley return 0; 8412908d778SJames Bottomley } 842ce4fc333SJohn Garry EXPORT_SYMBOL_GPL(sas_target_alloc); 8432908d778SJames Bottomley 84497a1420dSDan Williams #define SAS_DEF_QD 256 8452908d778SJames Bottomley 8462908d778SJames Bottomley int sas_slave_configure(struct scsi_device *scsi_dev) 8472908d778SJames Bottomley { 8482908d778SJames Bottomley struct domain_device *dev = sdev_to_domain_dev(scsi_dev); 8492908d778SJames Bottomley 8502908d778SJames Bottomley BUG_ON(dev->rphy->identify.device_type != SAS_END_DEVICE); 8512908d778SJames Bottomley 852fa1c1e8fSDarrick J. Wong if (dev_is_sata(dev)) { 853fa1c1e8fSDarrick J. Wong ata_sas_slave_configure(scsi_dev, dev->sata_dev.ap); 854fa1c1e8fSDarrick J. Wong return 0; 855fa1c1e8fSDarrick J. Wong } 856fa1c1e8fSDarrick J. Wong 8572908d778SJames Bottomley sas_read_port_mode_page(scsi_dev); 8582908d778SJames Bottomley 8592908d778SJames Bottomley if (scsi_dev->tagged_supported) { 860db5ed4dfSChristoph Hellwig scsi_change_queue_depth(scsi_dev, SAS_DEF_QD); 8612908d778SJames Bottomley } else { 862b3e3d4c6SJohn Garry pr_notice("device %016llx, LUN 0x%llx doesn't support TCQ\n", 86315ba7806SJohn Garry SAS_ADDR(dev->sas_addr), scsi_dev->lun); 864db5ed4dfSChristoph Hellwig scsi_change_queue_depth(scsi_dev, 1); 8652908d778SJames Bottomley } 8662908d778SJames Bottomley 867f27708fcSDarrick J. Wong scsi_dev->allow_restart = 1; 868f27708fcSDarrick J. Wong 8692908d778SJames Bottomley return 0; 8702908d778SJames Bottomley } 871ce4fc333SJohn Garry EXPORT_SYMBOL_GPL(sas_slave_configure); 8722908d778SJames Bottomley 873db5ed4dfSChristoph Hellwig int sas_change_queue_depth(struct scsi_device *sdev, int depth) 8742908d778SJames Bottomley { 87597a1420dSDan Williams struct domain_device *dev = sdev_to_domain_dev(sdev); 8762908d778SJames Bottomley 877f6e67035SDan Williams if (dev_is_sata(dev)) 878db5ed4dfSChristoph Hellwig return __ata_change_queue_depth(dev->sata_dev.ap, sdev, depth); 879f6e67035SDan Williams 88097a1420dSDan Williams if (!sdev->tagged_supported) 88197a1420dSDan Williams depth = 1; 882db5ed4dfSChristoph Hellwig return scsi_change_queue_depth(sdev, depth); 8832908d778SJames Bottomley } 884ce4fc333SJohn Garry EXPORT_SYMBOL_GPL(sas_change_queue_depth); 8852908d778SJames Bottomley 8862908d778SJames Bottomley int sas_bios_param(struct scsi_device *scsi_dev, 8872908d778SJames Bottomley struct block_device *bdev, 8882908d778SJames Bottomley sector_t capacity, int *hsc) 8892908d778SJames Bottomley { 8902908d778SJames Bottomley hsc[0] = 255; 8912908d778SJames Bottomley hsc[1] = 63; 8922908d778SJames Bottomley sector_div(capacity, 255*63); 8932908d778SJames Bottomley hsc[2] = capacity; 8942908d778SJames Bottomley 8952908d778SJames Bottomley return 0; 8962908d778SJames Bottomley } 897ce4fc333SJohn Garry EXPORT_SYMBOL_GPL(sas_bios_param); 8982908d778SJames Bottomley 899396819fbSDarrick J. Wong /* 900396819fbSDarrick J. Wong * Tell an upper layer that it needs to initiate an abort for a given task. 901396819fbSDarrick J. Wong * This should only ever be called by an LLDD. 902396819fbSDarrick J. Wong */ 903396819fbSDarrick J. Wong void sas_task_abort(struct sas_task *task) 90479a5eb60SDarrick J. Wong { 905396819fbSDarrick J. Wong struct scsi_cmnd *sc = task->uldd_task; 90679a5eb60SDarrick J. Wong 907396819fbSDarrick J. Wong /* Escape for libsas internal commands */ 908396819fbSDarrick J. Wong if (!sc) { 909f0bf750cSDan Williams struct sas_task_slow *slow = task->slow_task; 910f0bf750cSDan Williams 911f0bf750cSDan Williams if (!slow) 91279a5eb60SDarrick J. Wong return; 913f0bf750cSDan Williams if (!del_timer(&slow->timer)) 914f0bf750cSDan Williams return; 915841b86f3SKees Cook slow->timer.function(&slow->timer); 916396819fbSDarrick J. Wong return; 917396819fbSDarrick J. Wong } 91879a5eb60SDarrick J. Wong 91939795d65SChristoph Hellwig if (dev_is_sata(task->dev)) 9203a2755afSDarrick J. Wong sas_ata_task_abort(task); 92139795d65SChristoph Hellwig else 922cad1a780SBart Van Assche blk_abort_request(scsi_cmd_to_rq(sc)); 9231b4d0d8eSJames Bottomley } 924ce4fc333SJohn Garry EXPORT_SYMBOL_GPL(sas_task_abort); 92579a5eb60SDarrick J. Wong 92649da96d7SYufen Yu int sas_slave_alloc(struct scsi_device *sdev) 92749da96d7SYufen Yu { 92849da96d7SYufen Yu if (dev_is_sata(sdev_to_domain_dev(sdev)) && sdev->lun) 92949da96d7SYufen Yu return -ENXIO; 93049da96d7SYufen Yu 93149da96d7SYufen Yu return 0; 93249da96d7SYufen Yu } 933ce4fc333SJohn Garry EXPORT_SYMBOL_GPL(sas_slave_alloc); 93449da96d7SYufen Yu 935fa1c1e8fSDarrick J. Wong void sas_target_destroy(struct scsi_target *starget) 936fa1c1e8fSDarrick J. Wong { 937735f7d2fSDan Williams struct domain_device *found_dev = starget->hostdata; 938fa1c1e8fSDarrick J. Wong 939fa1c1e8fSDarrick J. Wong if (!found_dev) 940fa1c1e8fSDarrick J. Wong return; 941fa1c1e8fSDarrick J. Wong 942735f7d2fSDan Williams starget->hostdata = NULL; 943735f7d2fSDan Williams sas_put_device(found_dev); 944fa1c1e8fSDarrick J. Wong } 945ce4fc333SJohn Garry EXPORT_SYMBOL_GPL(sas_target_destroy); 946fa1c1e8fSDarrick J. Wong 94745e6cdf4SDarrick J. Wong #define SAS_STRING_ADDR_SIZE 16 94845e6cdf4SDarrick J. Wong 94945e6cdf4SDarrick J. Wong int sas_request_addr(struct Scsi_Host *shost, u8 *addr) 95045e6cdf4SDarrick J. Wong { 95145e6cdf4SDarrick J. Wong int res; 95245e6cdf4SDarrick J. Wong const struct firmware *fw; 95345e6cdf4SDarrick J. Wong 95445e6cdf4SDarrick J. Wong res = request_firmware(&fw, "sas_addr", &shost->shost_gendev); 95545e6cdf4SDarrick J. Wong if (res) 95645e6cdf4SDarrick J. Wong return res; 95745e6cdf4SDarrick J. Wong 95845e6cdf4SDarrick J. Wong if (fw->size < SAS_STRING_ADDR_SIZE) { 95945e6cdf4SDarrick J. Wong res = -ENODEV; 96045e6cdf4SDarrick J. Wong goto out; 96145e6cdf4SDarrick J. Wong } 96245e6cdf4SDarrick J. Wong 9639ea4e076SAndy Shevchenko res = hex2bin(addr, fw->data, strnlen(fw->data, SAS_ADDR_SIZE * 2) / 2); 9649ea4e076SAndy Shevchenko if (res) 9659ea4e076SAndy Shevchenko goto out; 96645e6cdf4SDarrick J. Wong 96745e6cdf4SDarrick J. Wong out: 96845e6cdf4SDarrick J. Wong release_firmware(fw); 96945e6cdf4SDarrick J. Wong return res; 97045e6cdf4SDarrick J. Wong } 97145e6cdf4SDarrick J. Wong EXPORT_SYMBOL_GPL(sas_request_addr); 97245e6cdf4SDarrick J. Wong 973