12908d778SJames Bottomley /* 22908d778SJames Bottomley * Serial Attached SCSI (SAS) class SCSI Host glue. 32908d778SJames Bottomley * 42908d778SJames Bottomley * Copyright (C) 2005 Adaptec, Inc. All rights reserved. 52908d778SJames Bottomley * Copyright (C) 2005 Luben Tuikov <luben_tuikov@adaptec.com> 62908d778SJames Bottomley * 72908d778SJames Bottomley * This file is licensed under GPLv2. 82908d778SJames Bottomley * 92908d778SJames Bottomley * This program is free software; you can redistribute it and/or 102908d778SJames Bottomley * modify it under the terms of the GNU General Public License as 112908d778SJames Bottomley * published by the Free Software Foundation; either version 2 of the 122908d778SJames Bottomley * License, or (at your option) any later version. 132908d778SJames Bottomley * 142908d778SJames Bottomley * This program is distributed in the hope that it will be useful, but 152908d778SJames Bottomley * WITHOUT ANY WARRANTY; without even the implied warranty of 162908d778SJames Bottomley * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 172908d778SJames Bottomley * General Public License for more details. 182908d778SJames Bottomley * 192908d778SJames Bottomley * You should have received a copy of the GNU General Public License 202908d778SJames Bottomley * along with this program; if not, write to the Free Software 212908d778SJames Bottomley * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 222908d778SJames Bottomley * USA 232908d778SJames Bottomley * 242908d778SJames Bottomley */ 252908d778SJames Bottomley 26d7a54e30SChristoph Hellwig #include <linux/kthread.h> 2745e6cdf4SDarrick J. Wong #include <linux/firmware.h> 2809703660SPaul Gortmaker #include <linux/export.h> 2945e6cdf4SDarrick J. Wong #include <linux/ctype.h> 309ea4e076SAndy Shevchenko #include <linux/kernel.h> 31d7a54e30SChristoph Hellwig 322908d778SJames Bottomley #include "sas_internal.h" 332908d778SJames Bottomley 342908d778SJames Bottomley #include <scsi/scsi_host.h> 352908d778SJames Bottomley #include <scsi/scsi_device.h> 362908d778SJames Bottomley #include <scsi/scsi_tcq.h> 372908d778SJames Bottomley #include <scsi/scsi.h> 38f456393eSDarrick J. Wong #include <scsi/scsi_eh.h> 392908d778SJames Bottomley #include <scsi/scsi_transport.h> 402908d778SJames Bottomley #include <scsi/scsi_transport_sas.h> 41338ec570SDarrick J. Wong #include <scsi/sas_ata.h> 422908d778SJames Bottomley #include "../scsi_sas_internal.h" 4379a5eb60SDarrick J. Wong #include "../scsi_transport_api.h" 44ad689233SDarrick J. Wong #include "../scsi_priv.h" 452908d778SJames Bottomley 462908d778SJames Bottomley #include <linux/err.h> 472908d778SJames Bottomley #include <linux/blkdev.h> 4883144186SRafael J. Wysocki #include <linux/freezer.h> 495a0e3ad6STejun Heo #include <linux/gfp.h> 502908d778SJames Bottomley #include <linux/scatterlist.h> 51fa1c1e8fSDarrick J. Wong #include <linux/libata.h> 522908d778SJames Bottomley 53a3a14252SDan Williams /* record final status and free the task */ 54a3a14252SDan Williams static void sas_end_task(struct scsi_cmnd *sc, struct sas_task *task) 552908d778SJames Bottomley { 562908d778SJames Bottomley struct task_status_struct *ts = &task->task_status; 572908d778SJames Bottomley int hs = 0, stat = 0; 582908d778SJames Bottomley 592908d778SJames Bottomley if (ts->resp == SAS_TASK_UNDELIVERED) { 602908d778SJames Bottomley /* transport error */ 612908d778SJames Bottomley hs = DID_NO_CONNECT; 622908d778SJames Bottomley } else { /* ts->resp == SAS_TASK_COMPLETE */ 632908d778SJames Bottomley /* task delivered, what happened afterwards? */ 642908d778SJames Bottomley switch (ts->stat) { 652908d778SJames Bottomley case SAS_DEV_NO_RESPONSE: 662908d778SJames Bottomley case SAS_INTERRUPTED: 672908d778SJames Bottomley case SAS_PHY_DOWN: 682908d778SJames Bottomley case SAS_NAK_R_ERR: 692908d778SJames Bottomley case SAS_OPEN_TO: 702908d778SJames Bottomley hs = DID_NO_CONNECT; 712908d778SJames Bottomley break; 722908d778SJames Bottomley case SAS_DATA_UNDERRUN: 73c13e5566SFUJITA Tomonori scsi_set_resid(sc, ts->residual); 74c13e5566SFUJITA Tomonori if (scsi_bufflen(sc) - scsi_get_resid(sc) < sc->underflow) 752908d778SJames Bottomley hs = DID_ERROR; 762908d778SJames Bottomley break; 772908d778SJames Bottomley case SAS_DATA_OVERRUN: 782908d778SJames Bottomley hs = DID_ERROR; 792908d778SJames Bottomley break; 802908d778SJames Bottomley case SAS_QUEUE_FULL: 812908d778SJames Bottomley hs = DID_SOFT_ERROR; /* retry */ 822908d778SJames Bottomley break; 832908d778SJames Bottomley case SAS_DEVICE_UNKNOWN: 842908d778SJames Bottomley hs = DID_BAD_TARGET; 852908d778SJames Bottomley break; 862908d778SJames Bottomley case SAS_SG_ERR: 872908d778SJames Bottomley hs = DID_PARITY; 882908d778SJames Bottomley break; 892908d778SJames Bottomley case SAS_OPEN_REJECT: 902908d778SJames Bottomley if (ts->open_rej_reason == SAS_OREJ_RSVD_RETRY) 912908d778SJames Bottomley hs = DID_SOFT_ERROR; /* retry */ 922908d778SJames Bottomley else 932908d778SJames Bottomley hs = DID_ERROR; 942908d778SJames Bottomley break; 952908d778SJames Bottomley case SAS_PROTO_RESPONSE: 962908d778SJames Bottomley SAS_DPRINTK("LLDD:%s sent SAS_PROTO_RESP for an SSP " 972908d778SJames Bottomley "task; please report this\n", 982908d778SJames Bottomley task->dev->port->ha->sas_ha_name); 992908d778SJames Bottomley break; 1002908d778SJames Bottomley case SAS_ABORTED_TASK: 1012908d778SJames Bottomley hs = DID_ABORT; 1022908d778SJames Bottomley break; 103df64d3caSJames Bottomley case SAM_STAT_CHECK_CONDITION: 1042908d778SJames Bottomley memcpy(sc->sense_buffer, ts->buf, 105cc75e8abSFUJITA Tomonori min(SCSI_SENSE_BUFFERSIZE, ts->buf_valid_size)); 106df64d3caSJames Bottomley stat = SAM_STAT_CHECK_CONDITION; 1072908d778SJames Bottomley break; 1082908d778SJames Bottomley default: 1092908d778SJames Bottomley stat = ts->stat; 1102908d778SJames Bottomley break; 1112908d778SJames Bottomley } 1122908d778SJames Bottomley } 113a3a14252SDan Williams 1142908d778SJames Bottomley sc->result = (hs << 16) | stat; 115a3a14252SDan Williams ASSIGN_SAS_TASK(sc, NULL); 1162908d778SJames Bottomley sas_free_task(task); 117a3a14252SDan Williams } 118a3a14252SDan Williams 119a3a14252SDan Williams static void sas_scsi_task_done(struct sas_task *task) 120a3a14252SDan Williams { 121a3a14252SDan Williams struct scsi_cmnd *sc = task->uldd_task; 1229095a64aSDan Williams struct domain_device *dev = task->dev; 1239095a64aSDan Williams struct sas_ha_struct *ha = dev->port->ha; 1249095a64aSDan Williams unsigned long flags; 125a3a14252SDan Williams 1269095a64aSDan Williams spin_lock_irqsave(&dev->done_lock, flags); 1279095a64aSDan Williams if (test_bit(SAS_HA_FROZEN, &ha->state)) 1289095a64aSDan Williams task = NULL; 1299095a64aSDan Williams else 1309095a64aSDan Williams ASSIGN_SAS_TASK(sc, NULL); 1319095a64aSDan Williams spin_unlock_irqrestore(&dev->done_lock, flags); 1329095a64aSDan Williams 1339095a64aSDan Williams if (unlikely(!task)) { 1349095a64aSDan Williams /* task will be completed by the error handler */ 135a3a14252SDan Williams SAS_DPRINTK("task done but aborted\n"); 136a3a14252SDan Williams return; 137a3a14252SDan Williams } 138a3a14252SDan Williams 139a3a14252SDan Williams if (unlikely(!sc)) { 140a3a14252SDan Williams SAS_DPRINTK("task_done called with non existing SCSI cmnd!\n"); 141a3a14252SDan Williams sas_free_task(task); 142a3a14252SDan Williams return; 143a3a14252SDan Williams } 144a3a14252SDan Williams 145a3a14252SDan Williams sas_end_task(sc, task); 1462908d778SJames Bottomley sc->scsi_done(sc); 1472908d778SJames Bottomley } 1482908d778SJames Bottomley 1492908d778SJames Bottomley static struct sas_task *sas_create_task(struct scsi_cmnd *cmd, 1502908d778SJames Bottomley struct domain_device *dev, 1513cc27547SAl Viro gfp_t gfp_flags) 1522908d778SJames Bottomley { 1532908d778SJames Bottomley struct sas_task *task = sas_alloc_task(gfp_flags); 1542908d778SJames Bottomley struct scsi_lun lun; 1552908d778SJames Bottomley 1562908d778SJames Bottomley if (!task) 1572908d778SJames Bottomley return NULL; 1582908d778SJames Bottomley 1592908d778SJames Bottomley task->uldd_task = cmd; 1602908d778SJames Bottomley ASSIGN_SAS_TASK(cmd, task); 1612908d778SJames Bottomley 1622908d778SJames Bottomley task->dev = dev; 1632908d778SJames Bottomley task->task_proto = task->dev->tproto; /* BUG_ON(!SSP) */ 1642908d778SJames Bottomley 1652908d778SJames Bottomley task->ssp_task.retry_count = 1; 1662908d778SJames Bottomley int_to_scsilun(cmd->device->lun, &lun); 1672908d778SJames Bottomley memcpy(task->ssp_task.LUN, &lun.scsi_lun, 8); 1689cbbdca4STejun Heo task->ssp_task.task_attr = TASK_ATTR_SIMPLE; 169e73823f7SJames Bottomley task->ssp_task.cmd = cmd; 1702908d778SJames Bottomley 171c13e5566SFUJITA Tomonori task->scatter = scsi_sglist(cmd); 172c13e5566SFUJITA Tomonori task->num_scatter = scsi_sg_count(cmd); 173c13e5566SFUJITA Tomonori task->total_xfer_len = scsi_bufflen(cmd); 1742908d778SJames Bottomley task->data_dir = cmd->sc_data_direction; 1752908d778SJames Bottomley 1762908d778SJames Bottomley task->task_done = sas_scsi_task_done; 1772908d778SJames Bottomley 1782908d778SJames Bottomley return task; 1792908d778SJames Bottomley } 1802908d778SJames Bottomley 18192bd401bSChristoph Hellwig int sas_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd) 1822908d778SJames Bottomley { 1832908d778SJames Bottomley struct sas_internal *i = to_sas_internal(host->transportt); 184a923f756SChristoph Hellwig struct domain_device *dev = cmd_to_domain_dev(cmd); 1852908d778SJames Bottomley struct sas_task *task; 186a923f756SChristoph Hellwig int res = 0; 1872908d778SJames Bottomley 1883673f4bfSDan Williams /* If the device fell off, no sense in issuing commands */ 189e139942dSDan Williams if (test_bit(SAS_DEV_GONE, &dev->state)) { 1903673f4bfSDan Williams cmd->result = DID_BAD_TARGET << 16; 191a923f756SChristoph Hellwig goto out_done; 1923673f4bfSDan Williams } 1933673f4bfSDan Williams 194fa1c1e8fSDarrick J. Wong if (dev_is_sata(dev)) { 195312d3e56SDan Williams spin_lock_irq(dev->sata_dev.ap->lock); 196b27dcfb0SJeff Garzik res = ata_sas_queuecmd(cmd, dev->sata_dev.ap); 197312d3e56SDan Williams spin_unlock_irq(dev->sata_dev.ap->lock); 198a923f756SChristoph Hellwig return res; 199fa1c1e8fSDarrick J. Wong } 200fa1c1e8fSDarrick J. Wong 2012908d778SJames Bottomley task = sas_create_task(cmd, dev, GFP_ATOMIC); 2022908d778SJames Bottomley if (!task) 203ac81c6a8SChristoph Hellwig return SCSI_MLQUEUE_HOST_BUSY; 2042908d778SJames Bottomley 20579855d17SChristoph Hellwig res = i->dft->lldd_execute_task(task, GFP_ATOMIC); 206a923f756SChristoph Hellwig if (res) 207a923f756SChristoph Hellwig goto out_free_task; 208a923f756SChristoph Hellwig return 0; 209a923f756SChristoph Hellwig 210a923f756SChristoph Hellwig out_free_task: 2112908d778SJames Bottomley SAS_DPRINTK("lldd_execute_task returned: %d\n", res); 2122908d778SJames Bottomley ASSIGN_SAS_TASK(cmd, NULL); 2132908d778SJames Bottomley sas_free_task(task); 214ac81c6a8SChristoph Hellwig if (res == -SAS_QUEUE_FULL) 215a923f756SChristoph Hellwig cmd->result = DID_SOFT_ERROR << 16; /* retry */ 216ac81c6a8SChristoph Hellwig else 217ac81c6a8SChristoph Hellwig cmd->result = DID_ERROR << 16; 218a923f756SChristoph Hellwig out_done: 219a923f756SChristoph Hellwig cmd->scsi_done(cmd); 220a923f756SChristoph Hellwig return 0; 2212908d778SJames Bottomley } 2222908d778SJames Bottomley 223a8e14fecSJames Bottomley static void sas_eh_finish_cmd(struct scsi_cmnd *cmd) 224a8e14fecSJames Bottomley { 225a8e14fecSJames Bottomley struct sas_ha_struct *sas_ha = SHOST_TO_SAS_HA(cmd->device->host); 226318aaf34SJason Yan struct domain_device *dev = cmd_to_domain_dev(cmd); 22745c73b65SDan Williams struct sas_task *task = TO_SAS_TASK(cmd); 228a8e14fecSJames Bottomley 229a3a14252SDan Williams /* At this point, we only get called following an actual abort 230a3a14252SDan Williams * of the task, so we should be guaranteed not to be racing with 231a3a14252SDan Williams * any completions from the LLD. Task is freed after this. 232a3a14252SDan Williams */ 233a3a14252SDan Williams sas_end_task(cmd, task); 234a3a14252SDan Williams 235318aaf34SJason Yan if (dev_is_sata(dev)) { 236318aaf34SJason Yan /* defer commands to libata so that libata EH can 237318aaf34SJason Yan * handle ata qcs correctly 238318aaf34SJason Yan */ 239318aaf34SJason Yan list_move_tail(&cmd->eh_entry, &sas_ha->eh_ata_q); 240318aaf34SJason Yan return; 241318aaf34SJason Yan } 242318aaf34SJason Yan 243a8e14fecSJames Bottomley /* now finish the command and move it on to the error 244a8e14fecSJames Bottomley * handler done list, this also takes it off the 245a3a14252SDan Williams * error handler pending list. 246a3a14252SDan Williams */ 247a8e14fecSJames Bottomley scsi_eh_finish_cmd(cmd, &sas_ha->eh_done_q); 248a8e14fecSJames Bottomley } 249a8e14fecSJames Bottomley 2502908d778SJames Bottomley static void sas_scsi_clear_queue_lu(struct list_head *error_q, struct scsi_cmnd *my_cmd) 2512908d778SJames Bottomley { 2522908d778SJames Bottomley struct scsi_cmnd *cmd, *n; 2532908d778SJames Bottomley 2542908d778SJames Bottomley list_for_each_entry_safe(cmd, n, error_q, eh_entry) { 25563e4563bSJames Bottomley if (cmd->device->sdev_target == my_cmd->device->sdev_target && 25663e4563bSJames Bottomley cmd->device->lun == my_cmd->device->lun) 257318aaf34SJason Yan sas_eh_finish_cmd(cmd); 2582908d778SJames Bottomley } 2592908d778SJames Bottomley } 2602908d778SJames Bottomley 2612908d778SJames Bottomley static void sas_scsi_clear_queue_I_T(struct list_head *error_q, 2622908d778SJames Bottomley struct domain_device *dev) 2632908d778SJames Bottomley { 2642908d778SJames Bottomley struct scsi_cmnd *cmd, *n; 2652908d778SJames Bottomley 2662908d778SJames Bottomley list_for_each_entry_safe(cmd, n, error_q, eh_entry) { 2672908d778SJames Bottomley struct domain_device *x = cmd_to_domain_dev(cmd); 2682908d778SJames Bottomley 2692908d778SJames Bottomley if (x == dev) 270a8e14fecSJames Bottomley sas_eh_finish_cmd(cmd); 2712908d778SJames Bottomley } 2722908d778SJames Bottomley } 2732908d778SJames Bottomley 2742908d778SJames Bottomley static void sas_scsi_clear_queue_port(struct list_head *error_q, 2752908d778SJames Bottomley struct asd_sas_port *port) 2762908d778SJames Bottomley { 2772908d778SJames Bottomley struct scsi_cmnd *cmd, *n; 2782908d778SJames Bottomley 2792908d778SJames Bottomley list_for_each_entry_safe(cmd, n, error_q, eh_entry) { 2802908d778SJames Bottomley struct domain_device *dev = cmd_to_domain_dev(cmd); 2812908d778SJames Bottomley struct asd_sas_port *x = dev->port; 2822908d778SJames Bottomley 2832908d778SJames Bottomley if (x == port) 284a8e14fecSJames Bottomley sas_eh_finish_cmd(cmd); 2852908d778SJames Bottomley } 2862908d778SJames Bottomley } 2872908d778SJames Bottomley 2882908d778SJames Bottomley enum task_disposition { 2892908d778SJames Bottomley TASK_IS_DONE, 2902908d778SJames Bottomley TASK_IS_ABORTED, 2912908d778SJames Bottomley TASK_IS_AT_LU, 2922908d778SJames Bottomley TASK_IS_NOT_AT_LU, 29302cd743bSDarrick J. Wong TASK_ABORT_FAILED, 2942908d778SJames Bottomley }; 2952908d778SJames Bottomley 2962908d778SJames Bottomley static enum task_disposition sas_scsi_find_task(struct sas_task *task) 2972908d778SJames Bottomley { 2982908d778SJames Bottomley unsigned long flags; 2992908d778SJames Bottomley int i, res; 3002908d778SJames Bottomley struct sas_internal *si = 3012908d778SJames Bottomley to_sas_internal(task->dev->port->ha->core.shost->transportt); 3022908d778SJames Bottomley 3032908d778SJames Bottomley for (i = 0; i < 5; i++) { 304cadbd4a5SHarvey Harrison SAS_DPRINTK("%s: aborting task 0x%p\n", __func__, task); 3052908d778SJames Bottomley res = si->dft->lldd_abort_task(task); 3062908d778SJames Bottomley 3072908d778SJames Bottomley spin_lock_irqsave(&task->task_state_lock, flags); 3082908d778SJames Bottomley if (task->task_state_flags & SAS_TASK_STATE_DONE) { 3092908d778SJames Bottomley spin_unlock_irqrestore(&task->task_state_lock, flags); 310cadbd4a5SHarvey Harrison SAS_DPRINTK("%s: task 0x%p is done\n", __func__, 3112908d778SJames Bottomley task); 3122908d778SJames Bottomley return TASK_IS_DONE; 3132908d778SJames Bottomley } 3142908d778SJames Bottomley spin_unlock_irqrestore(&task->task_state_lock, flags); 3152908d778SJames Bottomley 3162908d778SJames Bottomley if (res == TMF_RESP_FUNC_COMPLETE) { 3172908d778SJames Bottomley SAS_DPRINTK("%s: task 0x%p is aborted\n", 318cadbd4a5SHarvey Harrison __func__, task); 3192908d778SJames Bottomley return TASK_IS_ABORTED; 3202908d778SJames Bottomley } else if (si->dft->lldd_query_task) { 3212908d778SJames Bottomley SAS_DPRINTK("%s: querying task 0x%p\n", 322cadbd4a5SHarvey Harrison __func__, task); 3232908d778SJames Bottomley res = si->dft->lldd_query_task(task); 32402cd743bSDarrick J. Wong switch (res) { 32502cd743bSDarrick J. Wong case TMF_RESP_FUNC_SUCC: 3262908d778SJames Bottomley SAS_DPRINTK("%s: task 0x%p at LU\n", 327cadbd4a5SHarvey Harrison __func__, task); 3282908d778SJames Bottomley return TASK_IS_AT_LU; 32902cd743bSDarrick J. Wong case TMF_RESP_FUNC_COMPLETE: 3302908d778SJames Bottomley SAS_DPRINTK("%s: task 0x%p not at LU\n", 331cadbd4a5SHarvey Harrison __func__, task); 3322908d778SJames Bottomley return TASK_IS_NOT_AT_LU; 33302cd743bSDarrick J. Wong case TMF_RESP_FUNC_FAILED: 33402cd743bSDarrick J. Wong SAS_DPRINTK("%s: task 0x%p failed to abort\n", 335cadbd4a5SHarvey Harrison __func__, task); 33602cd743bSDarrick J. Wong return TASK_ABORT_FAILED; 3372908d778SJames Bottomley } 33802cd743bSDarrick J. Wong 3392908d778SJames Bottomley } 3402908d778SJames Bottomley } 3412908d778SJames Bottomley return res; 3422908d778SJames Bottomley } 3432908d778SJames Bottomley 3442908d778SJames Bottomley static int sas_recover_lu(struct domain_device *dev, struct scsi_cmnd *cmd) 3452908d778SJames Bottomley { 3462908d778SJames Bottomley int res = TMF_RESP_FUNC_FAILED; 3472908d778SJames Bottomley struct scsi_lun lun; 3482908d778SJames Bottomley struct sas_internal *i = 3492908d778SJames Bottomley to_sas_internal(dev->port->ha->core.shost->transportt); 3502908d778SJames Bottomley 3512908d778SJames Bottomley int_to_scsilun(cmd->device->lun, &lun); 3522908d778SJames Bottomley 3539cb78c16SHannes Reinecke SAS_DPRINTK("eh: device %llx LUN %llx has the task\n", 3542908d778SJames Bottomley SAS_ADDR(dev->sas_addr), 3552908d778SJames Bottomley cmd->device->lun); 3562908d778SJames Bottomley 3572908d778SJames Bottomley if (i->dft->lldd_abort_task_set) 3582908d778SJames Bottomley res = i->dft->lldd_abort_task_set(dev, lun.scsi_lun); 3592908d778SJames Bottomley 3602908d778SJames Bottomley if (res == TMF_RESP_FUNC_FAILED) { 3612908d778SJames Bottomley if (i->dft->lldd_clear_task_set) 3622908d778SJames Bottomley res = i->dft->lldd_clear_task_set(dev, lun.scsi_lun); 3632908d778SJames Bottomley } 3642908d778SJames Bottomley 3652908d778SJames Bottomley if (res == TMF_RESP_FUNC_FAILED) { 3662908d778SJames Bottomley if (i->dft->lldd_lu_reset) 3672908d778SJames Bottomley res = i->dft->lldd_lu_reset(dev, lun.scsi_lun); 3682908d778SJames Bottomley } 3692908d778SJames Bottomley 3702908d778SJames Bottomley return res; 3712908d778SJames Bottomley } 3722908d778SJames Bottomley 3732908d778SJames Bottomley static int sas_recover_I_T(struct domain_device *dev) 3742908d778SJames Bottomley { 3752908d778SJames Bottomley int res = TMF_RESP_FUNC_FAILED; 3762908d778SJames Bottomley struct sas_internal *i = 3772908d778SJames Bottomley to_sas_internal(dev->port->ha->core.shost->transportt); 3782908d778SJames Bottomley 3792908d778SJames Bottomley SAS_DPRINTK("I_T nexus reset for dev %016llx\n", 3802908d778SJames Bottomley SAS_ADDR(dev->sas_addr)); 3812908d778SJames Bottomley 3822908d778SJames Bottomley if (i->dft->lldd_I_T_nexus_reset) 3832908d778SJames Bottomley res = i->dft->lldd_I_T_nexus_reset(dev); 3842908d778SJames Bottomley 3852908d778SJames Bottomley return res; 3862908d778SJames Bottomley } 3872908d778SJames Bottomley 388f41a0c44SDan Williams /* take a reference on the last known good phy for this device */ 389f41a0c44SDan Williams struct sas_phy *sas_get_local_phy(struct domain_device *dev) 3903ebf6922SDarrick J. Wong { 391f41a0c44SDan Williams struct sas_ha_struct *ha = dev->port->ha; 392f41a0c44SDan Williams struct sas_phy *phy; 393f41a0c44SDan Williams unsigned long flags; 3943ebf6922SDarrick J. Wong 395f41a0c44SDan Williams /* a published domain device always has a valid phy, it may be 396f41a0c44SDan Williams * stale, but it is never NULL 397f41a0c44SDan Williams */ 398f41a0c44SDan Williams BUG_ON(!dev->phy); 3993ebf6922SDarrick J. Wong 400f41a0c44SDan Williams spin_lock_irqsave(&ha->phy_port_lock, flags); 401f41a0c44SDan Williams phy = dev->phy; 402f41a0c44SDan Williams get_device(&phy->dev); 403f41a0c44SDan Williams spin_unlock_irqrestore(&ha->phy_port_lock, flags); 404f41a0c44SDan Williams 405f41a0c44SDan Williams return phy; 4063ebf6922SDarrick J. Wong } 407f41a0c44SDan Williams EXPORT_SYMBOL_GPL(sas_get_local_phy); 408ad689233SDarrick J. Wong 4095db45bdcSDan Williams static void sas_wait_eh(struct domain_device *dev) 4105db45bdcSDan Williams { 4115db45bdcSDan Williams struct sas_ha_struct *ha = dev->port->ha; 4125db45bdcSDan Williams DEFINE_WAIT(wait); 4135db45bdcSDan Williams 4145db45bdcSDan Williams if (dev_is_sata(dev)) { 4155db45bdcSDan Williams ata_port_wait_eh(dev->sata_dev.ap); 4165db45bdcSDan Williams return; 4175db45bdcSDan Williams } 4185db45bdcSDan Williams retry: 4195db45bdcSDan Williams spin_lock_irq(&ha->lock); 4205db45bdcSDan Williams 4215db45bdcSDan Williams while (test_bit(SAS_DEV_EH_PENDING, &dev->state)) { 4225db45bdcSDan Williams prepare_to_wait(&ha->eh_wait_q, &wait, TASK_UNINTERRUPTIBLE); 4235db45bdcSDan Williams spin_unlock_irq(&ha->lock); 4245db45bdcSDan Williams schedule(); 4255db45bdcSDan Williams spin_lock_irq(&ha->lock); 4265db45bdcSDan Williams } 4275db45bdcSDan Williams finish_wait(&ha->eh_wait_q, &wait); 4285db45bdcSDan Williams 4295db45bdcSDan Williams spin_unlock_irq(&ha->lock); 4305db45bdcSDan Williams 4315db45bdcSDan Williams /* make sure SCSI EH is complete */ 4325db45bdcSDan Williams if (scsi_host_in_recovery(ha->core.shost)) { 4335db45bdcSDan Williams msleep(10); 4345db45bdcSDan Williams goto retry; 4355db45bdcSDan Williams } 4365db45bdcSDan Williams } 4375db45bdcSDan Williams EXPORT_SYMBOL(sas_wait_eh); 4385db45bdcSDan Williams 4399cb78c16SHannes Reinecke static int sas_queue_reset(struct domain_device *dev, int reset_type, 4409cb78c16SHannes Reinecke u64 lun, int wait) 4415db45bdcSDan Williams { 4425db45bdcSDan Williams struct sas_ha_struct *ha = dev->port->ha; 4435db45bdcSDan Williams int scheduled = 0, tries = 100; 4445db45bdcSDan Williams 4455db45bdcSDan Williams /* ata: promote lun reset to bus reset */ 4465db45bdcSDan Williams if (dev_is_sata(dev)) { 4475db45bdcSDan Williams sas_ata_schedule_reset(dev); 4485db45bdcSDan Williams if (wait) 4495db45bdcSDan Williams sas_ata_wait_eh(dev); 4505db45bdcSDan Williams return SUCCESS; 4515db45bdcSDan Williams } 4525db45bdcSDan Williams 4535db45bdcSDan Williams while (!scheduled && tries--) { 4545db45bdcSDan Williams spin_lock_irq(&ha->lock); 4555db45bdcSDan Williams if (!test_bit(SAS_DEV_EH_PENDING, &dev->state) && 4565db45bdcSDan Williams !test_bit(reset_type, &dev->state)) { 4575db45bdcSDan Williams scheduled = 1; 4585db45bdcSDan Williams ha->eh_active++; 4595db45bdcSDan Williams list_add_tail(&dev->ssp_dev.eh_list_node, &ha->eh_dev_q); 4605db45bdcSDan Williams set_bit(SAS_DEV_EH_PENDING, &dev->state); 4615db45bdcSDan Williams set_bit(reset_type, &dev->state); 4625db45bdcSDan Williams int_to_scsilun(lun, &dev->ssp_dev.reset_lun); 4635db45bdcSDan Williams scsi_schedule_eh(ha->core.shost); 4645db45bdcSDan Williams } 4655db45bdcSDan Williams spin_unlock_irq(&ha->lock); 4665db45bdcSDan Williams 4675db45bdcSDan Williams if (wait) 4685db45bdcSDan Williams sas_wait_eh(dev); 4695db45bdcSDan Williams 4705db45bdcSDan Williams if (scheduled) 4715db45bdcSDan Williams return SUCCESS; 4725db45bdcSDan Williams } 4735db45bdcSDan Williams 4745db45bdcSDan Williams SAS_DPRINTK("%s reset of %s failed\n", 4755db45bdcSDan Williams reset_type == SAS_DEV_LU_RESET ? "LUN" : "Bus", 4765db45bdcSDan Williams dev_name(&dev->rphy->dev)); 4775db45bdcSDan Williams 4785db45bdcSDan Williams return FAILED; 4795db45bdcSDan Williams } 4805db45bdcSDan Williams 4819524c682SDan Williams int sas_eh_abort_handler(struct scsi_cmnd *cmd) 4829524c682SDan Williams { 483c9f92600SHannes Reinecke int res = TMF_RESP_FUNC_FAILED; 4849524c682SDan Williams struct sas_task *task = TO_SAS_TASK(cmd); 4859524c682SDan Williams struct Scsi_Host *host = cmd->device->host; 486c9f92600SHannes Reinecke struct domain_device *dev = cmd_to_domain_dev(cmd); 4879524c682SDan Williams struct sas_internal *i = to_sas_internal(host->transportt); 488c9f92600SHannes Reinecke unsigned long flags; 4899524c682SDan Williams 4909524c682SDan Williams if (!i->dft->lldd_abort_task) 4919524c682SDan Williams return FAILED; 4929524c682SDan Williams 493c9f92600SHannes Reinecke spin_lock_irqsave(host->host_lock, flags); 494c9f92600SHannes Reinecke /* We cannot do async aborts for SATA devices */ 495c9f92600SHannes Reinecke if (dev_is_sata(dev) && !host->host_eh_scheduled) { 496c9f92600SHannes Reinecke spin_unlock_irqrestore(host->host_lock, flags); 497c9f92600SHannes Reinecke return FAILED; 498c9f92600SHannes Reinecke } 499c9f92600SHannes Reinecke spin_unlock_irqrestore(host->host_lock, flags); 500c9f92600SHannes Reinecke 501c9f92600SHannes Reinecke if (task) 5029524c682SDan Williams res = i->dft->lldd_abort_task(task); 503c9f92600SHannes Reinecke else 504c9f92600SHannes Reinecke SAS_DPRINTK("no task to abort\n"); 5059524c682SDan Williams if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE) 5069524c682SDan Williams return SUCCESS; 5079524c682SDan Williams 5089524c682SDan Williams return FAILED; 5099524c682SDan Williams } 5109524c682SDan Williams EXPORT_SYMBOL_GPL(sas_eh_abort_handler); 5119524c682SDan Williams 512a9344e68SDarrick J. Wong /* Attempt to send a LUN reset message to a device */ 513ad689233SDarrick J. Wong int sas_eh_device_reset_handler(struct scsi_cmnd *cmd) 5142908d778SJames Bottomley { 515a9344e68SDarrick J. Wong int res; 5165db45bdcSDan Williams struct scsi_lun lun; 5175db45bdcSDan Williams struct Scsi_Host *host = cmd->device->host; 5185db45bdcSDan Williams struct domain_device *dev = cmd_to_domain_dev(cmd); 5195db45bdcSDan Williams struct sas_internal *i = to_sas_internal(host->transportt); 5205db45bdcSDan Williams 5215db45bdcSDan Williams if (current != host->ehandler) 5225db45bdcSDan Williams return sas_queue_reset(dev, SAS_DEV_LU_RESET, cmd->device->lun, 0); 523a9344e68SDarrick J. Wong 524a9344e68SDarrick J. Wong int_to_scsilun(cmd->device->lun, &lun); 525a9344e68SDarrick J. Wong 526a9344e68SDarrick J. Wong if (!i->dft->lldd_lu_reset) 527a9344e68SDarrick J. Wong return FAILED; 528a9344e68SDarrick J. Wong 529a9344e68SDarrick J. Wong res = i->dft->lldd_lu_reset(dev, lun.scsi_lun); 530a9344e68SDarrick J. Wong if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE) 531a9344e68SDarrick J. Wong return SUCCESS; 532a9344e68SDarrick J. Wong 533a9344e68SDarrick J. Wong return FAILED; 534a9344e68SDarrick J. Wong } 535a9344e68SDarrick J. Wong 536cc199e78SHannes Reinecke int sas_eh_target_reset_handler(struct scsi_cmnd *cmd) 537a9344e68SDarrick J. Wong { 538ad689233SDarrick J. Wong int res; 539e7db8229SDan Williams struct Scsi_Host *host = cmd->device->host; 540e7db8229SDan Williams struct domain_device *dev = cmd_to_domain_dev(cmd); 541e7db8229SDan Williams struct sas_internal *i = to_sas_internal(host->transportt); 542ad689233SDarrick J. Wong 5435db45bdcSDan Williams if (current != host->ehandler) 5445db45bdcSDan Williams return sas_queue_reset(dev, SAS_DEV_RESET, 0, 0); 5455db45bdcSDan Williams 546e7db8229SDan Williams if (!i->dft->lldd_I_T_nexus_reset) 547e7db8229SDan Williams return FAILED; 548f41a0c44SDan Williams 549e7db8229SDan Williams res = i->dft->lldd_I_T_nexus_reset(dev); 550e7db8229SDan Williams if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE || 551e7db8229SDan Williams res == -ENODEV) 552ad689233SDarrick J. Wong return SUCCESS; 553ad689233SDarrick J. Wong 554ad689233SDarrick J. Wong return FAILED; 555ad689233SDarrick J. Wong } 556ad689233SDarrick J. Wong 557ad689233SDarrick J. Wong /* Try to reset a device */ 5588de3ef25SJames Bottomley static int try_to_reset_cmd_device(struct scsi_cmnd *cmd) 559ad689233SDarrick J. Wong { 560a9344e68SDarrick J. Wong int res; 5618de3ef25SJames Bottomley struct Scsi_Host *shost = cmd->device->host; 562ad689233SDarrick J. Wong 563a9344e68SDarrick J. Wong if (!shost->hostt->eh_device_reset_handler) 564cc199e78SHannes Reinecke goto try_target_reset; 565a9344e68SDarrick J. Wong 566a9344e68SDarrick J. Wong res = shost->hostt->eh_device_reset_handler(cmd); 567a9344e68SDarrick J. Wong if (res == SUCCESS) 568a9344e68SDarrick J. Wong return res; 569a9344e68SDarrick J. Wong 570cc199e78SHannes Reinecke try_target_reset: 571cc199e78SHannes Reinecke if (shost->hostt->eh_target_reset_handler) 572cc199e78SHannes Reinecke return shost->hostt->eh_target_reset_handler(cmd); 573a9344e68SDarrick J. Wong 574a9344e68SDarrick J. Wong return FAILED; 575ad689233SDarrick J. Wong } 576ad689233SDarrick J. Wong 57784023474SDan Williams static void sas_eh_handle_sas_errors(struct Scsi_Host *shost, struct list_head *work_q) 578ad689233SDarrick J. Wong { 5792908d778SJames Bottomley struct scsi_cmnd *cmd, *n; 5802908d778SJames Bottomley enum task_disposition res = TASK_IS_DONE; 5813ebf6922SDarrick J. Wong int tmf_resp, need_reset; 5822908d778SJames Bottomley struct sas_internal *i = to_sas_internal(shost->transportt); 583ad689233SDarrick J. Wong unsigned long flags; 584ad689233SDarrick J. Wong struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost); 58545c73b65SDan Williams LIST_HEAD(done); 5862908d778SJames Bottomley 58745c73b65SDan Williams /* clean out any commands that won the completion vs eh race */ 588ad689233SDarrick J. Wong list_for_each_entry_safe(cmd, n, work_q, eh_entry) { 5899095a64aSDan Williams struct domain_device *dev = cmd_to_domain_dev(cmd); 5909095a64aSDan Williams struct sas_task *task; 5919095a64aSDan Williams 5929095a64aSDan Williams spin_lock_irqsave(&dev->done_lock, flags); 5939095a64aSDan Williams /* by this point the lldd has either observed 5949095a64aSDan Williams * SAS_HA_FROZEN and is leaving the task alone, or has 5959095a64aSDan Williams * won the race with eh and decided to complete it 5969095a64aSDan Williams */ 5979095a64aSDan Williams task = TO_SAS_TASK(cmd); 5989095a64aSDan Williams spin_unlock_irqrestore(&dev->done_lock, flags); 599f456393eSDarrick J. Wong 600ad689233SDarrick J. Wong if (!task) 60145c73b65SDan Williams list_move_tail(&cmd->eh_entry, &done); 60245c73b65SDan Williams } 60345c73b65SDan Williams 60445c73b65SDan Williams Again: 60545c73b65SDan Williams list_for_each_entry_safe(cmd, n, work_q, eh_entry) { 60645c73b65SDan Williams struct sas_task *task = TO_SAS_TASK(cmd); 607ad689233SDarrick J. Wong 608ad689233SDarrick J. Wong list_del_init(&cmd->eh_entry); 6093ebf6922SDarrick J. Wong 6103ebf6922SDarrick J. Wong spin_lock_irqsave(&task->task_state_lock, flags); 6113ebf6922SDarrick J. Wong need_reset = task->task_state_flags & SAS_TASK_NEED_DEV_RESET; 6123ebf6922SDarrick J. Wong spin_unlock_irqrestore(&task->task_state_lock, flags); 6133ebf6922SDarrick J. Wong 6148de3ef25SJames Bottomley if (need_reset) { 6158de3ef25SJames Bottomley SAS_DPRINTK("%s: task 0x%p requests reset\n", 616cadbd4a5SHarvey Harrison __func__, task); 6178de3ef25SJames Bottomley goto reset; 6188de3ef25SJames Bottomley } 6198de3ef25SJames Bottomley 620f456393eSDarrick J. Wong SAS_DPRINTK("trying to find task 0x%p\n", task); 6212908d778SJames Bottomley res = sas_scsi_find_task(task); 6222908d778SJames Bottomley 6232908d778SJames Bottomley switch (res) { 6242908d778SJames Bottomley case TASK_IS_DONE: 625cadbd4a5SHarvey Harrison SAS_DPRINTK("%s: task 0x%p is done\n", __func__, 6262908d778SJames Bottomley task); 627318aaf34SJason Yan sas_eh_finish_cmd(cmd); 6282908d778SJames Bottomley continue; 6292908d778SJames Bottomley case TASK_IS_ABORTED: 6302908d778SJames Bottomley SAS_DPRINTK("%s: task 0x%p is aborted\n", 631cadbd4a5SHarvey Harrison __func__, task); 632318aaf34SJason Yan sas_eh_finish_cmd(cmd); 6332908d778SJames Bottomley continue; 6342908d778SJames Bottomley case TASK_IS_AT_LU: 6352908d778SJames Bottomley SAS_DPRINTK("task 0x%p is at LU: lu recover\n", task); 6368de3ef25SJames Bottomley reset: 6372908d778SJames Bottomley tmf_resp = sas_recover_lu(task->dev, cmd); 6382908d778SJames Bottomley if (tmf_resp == TMF_RESP_FUNC_COMPLETE) { 6399cb78c16SHannes Reinecke SAS_DPRINTK("dev %016llx LU %llx is " 6402908d778SJames Bottomley "recovered\n", 6412908d778SJames Bottomley SAS_ADDR(task->dev), 6422908d778SJames Bottomley cmd->device->lun); 643318aaf34SJason Yan sas_eh_finish_cmd(cmd); 644ad689233SDarrick J. Wong sas_scsi_clear_queue_lu(work_q, cmd); 6452908d778SJames Bottomley goto Again; 6462908d778SJames Bottomley } 6472908d778SJames Bottomley /* fallthrough */ 6482908d778SJames Bottomley case TASK_IS_NOT_AT_LU: 64902cd743bSDarrick J. Wong case TASK_ABORT_FAILED: 6502908d778SJames Bottomley SAS_DPRINTK("task 0x%p is not at LU: I_T recover\n", 6512908d778SJames Bottomley task); 6522908d778SJames Bottomley tmf_resp = sas_recover_I_T(task->dev); 65326a2e68fSDan Williams if (tmf_resp == TMF_RESP_FUNC_COMPLETE || 65426a2e68fSDan Williams tmf_resp == -ENODEV) { 6558de3ef25SJames Bottomley struct domain_device *dev = task->dev; 6562908d778SJames Bottomley SAS_DPRINTK("I_T %016llx recovered\n", 6572908d778SJames Bottomley SAS_ADDR(task->dev->sas_addr)); 658a8e14fecSJames Bottomley sas_eh_finish_cmd(cmd); 6598de3ef25SJames Bottomley sas_scsi_clear_queue_I_T(work_q, dev); 6602908d778SJames Bottomley goto Again; 6612908d778SJames Bottomley } 6622908d778SJames Bottomley /* Hammer time :-) */ 6638de3ef25SJames Bottomley try_to_reset_cmd_device(cmd); 6642908d778SJames Bottomley if (i->dft->lldd_clear_nexus_port) { 6652908d778SJames Bottomley struct asd_sas_port *port = task->dev->port; 6662908d778SJames Bottomley SAS_DPRINTK("clearing nexus for port:%d\n", 6672908d778SJames Bottomley port->id); 6682908d778SJames Bottomley res = i->dft->lldd_clear_nexus_port(port); 6692908d778SJames Bottomley if (res == TMF_RESP_FUNC_COMPLETE) { 6702908d778SJames Bottomley SAS_DPRINTK("clear nexus port:%d " 6712908d778SJames Bottomley "succeeded\n", port->id); 672a8e14fecSJames Bottomley sas_eh_finish_cmd(cmd); 673ad689233SDarrick J. Wong sas_scsi_clear_queue_port(work_q, 6742908d778SJames Bottomley port); 6752908d778SJames Bottomley goto Again; 6762908d778SJames Bottomley } 6772908d778SJames Bottomley } 6782908d778SJames Bottomley if (i->dft->lldd_clear_nexus_ha) { 6792908d778SJames Bottomley SAS_DPRINTK("clear nexus ha\n"); 6802908d778SJames Bottomley res = i->dft->lldd_clear_nexus_ha(ha); 6812908d778SJames Bottomley if (res == TMF_RESP_FUNC_COMPLETE) { 6822908d778SJames Bottomley SAS_DPRINTK("clear nexus ha " 6832908d778SJames Bottomley "succeeded\n"); 684a8e14fecSJames Bottomley sas_eh_finish_cmd(cmd); 685a8e14fecSJames Bottomley goto clear_q; 6862908d778SJames Bottomley } 6872908d778SJames Bottomley } 6882908d778SJames Bottomley /* If we are here -- this means that no amount 6892908d778SJames Bottomley * of effort could recover from errors. Quite 6902908d778SJames Bottomley * possibly the HA just disappeared. 6912908d778SJames Bottomley */ 6929cb78c16SHannes Reinecke SAS_DPRINTK("error from device %llx, LUN %llx " 6932908d778SJames Bottomley "couldn't be recovered in any way\n", 6942908d778SJames Bottomley SAS_ADDR(task->dev->sas_addr), 6952908d778SJames Bottomley cmd->device->lun); 6962908d778SJames Bottomley 697a8e14fecSJames Bottomley sas_eh_finish_cmd(cmd); 6982908d778SJames Bottomley goto clear_q; 6992908d778SJames Bottomley } 7002908d778SJames Bottomley } 70145c73b65SDan Williams out: 70245c73b65SDan Williams list_splice_tail(&done, work_q); 7033944f509SDan Williams list_splice_tail_init(&ha->eh_ata_q, work_q); 70484023474SDan Williams return; 70545c73b65SDan Williams 7062908d778SJames Bottomley clear_q: 707cadbd4a5SHarvey Harrison SAS_DPRINTK("--- Exit %s -- clear_q\n", __func__); 708a8e14fecSJames Bottomley list_for_each_entry_safe(cmd, n, work_q, eh_entry) 709a8e14fecSJames Bottomley sas_eh_finish_cmd(cmd); 71045c73b65SDan Williams goto out; 711ad689233SDarrick J. Wong } 712ad689233SDarrick J. Wong 7135db45bdcSDan Williams static void sas_eh_handle_resets(struct Scsi_Host *shost) 7145db45bdcSDan Williams { 7155db45bdcSDan Williams struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost); 7165db45bdcSDan Williams struct sas_internal *i = to_sas_internal(shost->transportt); 7175db45bdcSDan Williams 7185db45bdcSDan Williams /* handle directed resets to sas devices */ 7195db45bdcSDan Williams spin_lock_irq(&ha->lock); 7205db45bdcSDan Williams while (!list_empty(&ha->eh_dev_q)) { 7215db45bdcSDan Williams struct domain_device *dev; 7225db45bdcSDan Williams struct ssp_device *ssp; 7235db45bdcSDan Williams 7245db45bdcSDan Williams ssp = list_entry(ha->eh_dev_q.next, typeof(*ssp), eh_list_node); 7255db45bdcSDan Williams list_del_init(&ssp->eh_list_node); 7265db45bdcSDan Williams dev = container_of(ssp, typeof(*dev), ssp_dev); 7275db45bdcSDan Williams kref_get(&dev->kref); 7285db45bdcSDan Williams WARN_ONCE(dev_is_sata(dev), "ssp reset to ata device?\n"); 7295db45bdcSDan Williams 7305db45bdcSDan Williams spin_unlock_irq(&ha->lock); 7315db45bdcSDan Williams 7325db45bdcSDan Williams if (test_and_clear_bit(SAS_DEV_LU_RESET, &dev->state)) 7335db45bdcSDan Williams i->dft->lldd_lu_reset(dev, ssp->reset_lun.scsi_lun); 7345db45bdcSDan Williams 7355db45bdcSDan Williams if (test_and_clear_bit(SAS_DEV_RESET, &dev->state)) 7365db45bdcSDan Williams i->dft->lldd_I_T_nexus_reset(dev); 7375db45bdcSDan Williams 7385db45bdcSDan Williams sas_put_device(dev); 7395db45bdcSDan Williams spin_lock_irq(&ha->lock); 7405db45bdcSDan Williams clear_bit(SAS_DEV_EH_PENDING, &dev->state); 7415db45bdcSDan Williams ha->eh_active--; 7425db45bdcSDan Williams } 7435db45bdcSDan Williams spin_unlock_irq(&ha->lock); 7445db45bdcSDan Williams } 7455db45bdcSDan Williams 746e4a9c373SDan Williams 747ad689233SDarrick J. Wong void sas_scsi_recover_host(struct Scsi_Host *shost) 748ad689233SDarrick J. Wong { 749ad689233SDarrick J. Wong struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost); 750ad689233SDarrick J. Wong LIST_HEAD(eh_work_q); 751e4a9c373SDan Williams int tries = 0; 752e4a9c373SDan Williams bool retry; 753ad689233SDarrick J. Wong 754e4a9c373SDan Williams retry: 755e4a9c373SDan Williams tries++; 756e4a9c373SDan Williams retry = true; 757e4a9c373SDan Williams spin_lock_irq(shost->host_lock); 758ad689233SDarrick J. Wong list_splice_init(&shost->eh_cmd_q, &eh_work_q); 759e4a9c373SDan Williams spin_unlock_irq(shost->host_lock); 760ad689233SDarrick J. Wong 761d230ce69SDan Williams SAS_DPRINTK("Enter %s busy: %d failed: %d\n", 762*c84b023aSMing Lei __func__, scsi_host_busy(shost), shost->host_failed); 763ad689233SDarrick J. Wong /* 764ad689233SDarrick J. Wong * Deal with commands that still have SAS tasks (i.e. they didn't 76584023474SDan Williams * complete via the normal sas_task completion mechanism), 76684023474SDan Williams * SAS_HA_FROZEN gives eh dominion over all sas_task completion. 767ad689233SDarrick J. Wong */ 7689095a64aSDan Williams set_bit(SAS_HA_FROZEN, &ha->state); 76984023474SDan Williams sas_eh_handle_sas_errors(shost, &eh_work_q); 77084023474SDan Williams clear_bit(SAS_HA_FROZEN, &ha->state); 77184023474SDan Williams if (list_empty(&eh_work_q)) 772ad689233SDarrick J. Wong goto out; 773ad689233SDarrick J. Wong 774ad689233SDarrick J. Wong /* 775ad689233SDarrick J. Wong * Now deal with SCSI commands that completed ok but have a an error 776ad689233SDarrick J. Wong * code (and hopefully sense data) attached. This is roughly what 777ad689233SDarrick J. Wong * scsi_unjam_host does, but we skip scsi_eh_abort_cmds because any 778ad689233SDarrick J. Wong * command we see here has no sas_task and is thus unknown to the HA. 779ad689233SDarrick J. Wong */ 780d230ce69SDan Williams sas_ata_eh(shost, &eh_work_q, &ha->eh_done_q); 781ad689233SDarrick J. Wong if (!scsi_eh_get_sense(&eh_work_q, &ha->eh_done_q)) 782ad689233SDarrick J. Wong scsi_eh_ready_devs(shost, &eh_work_q, &ha->eh_done_q); 783ad689233SDarrick J. Wong 784ad689233SDarrick J. Wong out: 7855db45bdcSDan Williams sas_eh_handle_resets(shost); 7865db45bdcSDan Williams 78700dd4998SJames Bottomley /* now link into libata eh --- if we have any ata devices */ 78800dd4998SJames Bottomley sas_ata_strategy_handler(shost); 78900dd4998SJames Bottomley 790ad689233SDarrick J. Wong scsi_eh_flush_done_q(&ha->eh_done_q); 79100dd4998SJames Bottomley 792e4a9c373SDan Williams /* check if any new eh work was scheduled during the last run */ 793e4a9c373SDan Williams spin_lock_irq(&ha->lock); 794e4a9c373SDan Williams if (ha->eh_active == 0) { 795e4a9c373SDan Williams shost->host_eh_scheduled = 0; 796e4a9c373SDan Williams retry = false; 797e4a9c373SDan Williams } 798e4a9c373SDan Williams spin_unlock_irq(&ha->lock); 799e4a9c373SDan Williams 800e4a9c373SDan Williams if (retry) 801e4a9c373SDan Williams goto retry; 802e4a9c373SDan Williams 803e4a9c373SDan Williams SAS_DPRINTK("--- Exit %s: busy: %d failed: %d tries: %d\n", 804*c84b023aSMing Lei __func__, scsi_host_busy(shost), 80574665016SChristoph Hellwig shost->host_failed, tries); 8062908d778SJames Bottomley } 8072908d778SJames Bottomley 808fa1c1e8fSDarrick J. Wong int sas_ioctl(struct scsi_device *sdev, int cmd, void __user *arg) 809fa1c1e8fSDarrick J. Wong { 810fa1c1e8fSDarrick J. Wong struct domain_device *dev = sdev_to_domain_dev(sdev); 811fa1c1e8fSDarrick J. Wong 812fa1c1e8fSDarrick J. Wong if (dev_is_sata(dev)) 81394be9a58SJeff Garzik return ata_sas_scsi_ioctl(dev->sata_dev.ap, sdev, cmd, arg); 814fa1c1e8fSDarrick J. Wong 815fa1c1e8fSDarrick J. Wong return -EINVAL; 816fa1c1e8fSDarrick J. Wong } 817fa1c1e8fSDarrick J. Wong 8182908d778SJames Bottomley struct domain_device *sas_find_dev_by_rphy(struct sas_rphy *rphy) 8192908d778SJames Bottomley { 8202908d778SJames Bottomley struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent); 8212908d778SJames Bottomley struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost); 8222908d778SJames Bottomley struct domain_device *found_dev = NULL; 8232908d778SJames Bottomley int i; 824980fa2f9SDarrick J. Wong unsigned long flags; 8252908d778SJames Bottomley 826980fa2f9SDarrick J. Wong spin_lock_irqsave(&ha->phy_port_lock, flags); 8272908d778SJames Bottomley for (i = 0; i < ha->num_phys; i++) { 8282908d778SJames Bottomley struct asd_sas_port *port = ha->sas_port[i]; 8292908d778SJames Bottomley struct domain_device *dev; 8302908d778SJames Bottomley 8312908d778SJames Bottomley spin_lock(&port->dev_list_lock); 8322908d778SJames Bottomley list_for_each_entry(dev, &port->dev_list, dev_list_node) { 8332908d778SJames Bottomley if (rphy == dev->rphy) { 8342908d778SJames Bottomley found_dev = dev; 8352908d778SJames Bottomley spin_unlock(&port->dev_list_lock); 8362908d778SJames Bottomley goto found; 8372908d778SJames Bottomley } 8382908d778SJames Bottomley } 8392908d778SJames Bottomley spin_unlock(&port->dev_list_lock); 8402908d778SJames Bottomley } 8412908d778SJames Bottomley found: 842980fa2f9SDarrick J. Wong spin_unlock_irqrestore(&ha->phy_port_lock, flags); 8432908d778SJames Bottomley 8442908d778SJames Bottomley return found_dev; 8452908d778SJames Bottomley } 8462908d778SJames Bottomley 8472908d778SJames Bottomley int sas_target_alloc(struct scsi_target *starget) 8482908d778SJames Bottomley { 849735f7d2fSDan Williams struct sas_rphy *rphy = dev_to_rphy(starget->dev.parent); 850735f7d2fSDan Williams struct domain_device *found_dev = sas_find_dev_by_rphy(rphy); 8512908d778SJames Bottomley 8522908d778SJames Bottomley if (!found_dev) 8532908d778SJames Bottomley return -ENODEV; 8542908d778SJames Bottomley 855735f7d2fSDan Williams kref_get(&found_dev->kref); 8562908d778SJames Bottomley starget->hostdata = found_dev; 8572908d778SJames Bottomley return 0; 8582908d778SJames Bottomley } 8592908d778SJames Bottomley 86097a1420dSDan Williams #define SAS_DEF_QD 256 8612908d778SJames Bottomley 8622908d778SJames Bottomley int sas_slave_configure(struct scsi_device *scsi_dev) 8632908d778SJames Bottomley { 8642908d778SJames Bottomley struct domain_device *dev = sdev_to_domain_dev(scsi_dev); 8652908d778SJames Bottomley 8662908d778SJames Bottomley BUG_ON(dev->rphy->identify.device_type != SAS_END_DEVICE); 8672908d778SJames Bottomley 868fa1c1e8fSDarrick J. Wong if (dev_is_sata(dev)) { 869fa1c1e8fSDarrick J. Wong ata_sas_slave_configure(scsi_dev, dev->sata_dev.ap); 870fa1c1e8fSDarrick J. Wong return 0; 871fa1c1e8fSDarrick J. Wong } 872fa1c1e8fSDarrick J. Wong 8732908d778SJames Bottomley sas_read_port_mode_page(scsi_dev); 8742908d778SJames Bottomley 8752908d778SJames Bottomley if (scsi_dev->tagged_supported) { 876db5ed4dfSChristoph Hellwig scsi_change_queue_depth(scsi_dev, SAS_DEF_QD); 8772908d778SJames Bottomley } else { 8789cb78c16SHannes Reinecke SAS_DPRINTK("device %llx, LUN %llx doesn't support " 8792908d778SJames Bottomley "TCQ\n", SAS_ADDR(dev->sas_addr), 8802908d778SJames Bottomley scsi_dev->lun); 881db5ed4dfSChristoph Hellwig scsi_change_queue_depth(scsi_dev, 1); 8822908d778SJames Bottomley } 8832908d778SJames Bottomley 884f27708fcSDarrick J. Wong scsi_dev->allow_restart = 1; 885f27708fcSDarrick J. Wong 8862908d778SJames Bottomley return 0; 8872908d778SJames Bottomley } 8882908d778SJames Bottomley 889db5ed4dfSChristoph Hellwig int sas_change_queue_depth(struct scsi_device *sdev, int depth) 8902908d778SJames Bottomley { 89197a1420dSDan Williams struct domain_device *dev = sdev_to_domain_dev(sdev); 8922908d778SJames Bottomley 893f6e67035SDan Williams if (dev_is_sata(dev)) 894db5ed4dfSChristoph Hellwig return __ata_change_queue_depth(dev->sata_dev.ap, sdev, depth); 895f6e67035SDan Williams 89697a1420dSDan Williams if (!sdev->tagged_supported) 89797a1420dSDan Williams depth = 1; 898db5ed4dfSChristoph Hellwig return scsi_change_queue_depth(sdev, depth); 8992908d778SJames Bottomley } 9002908d778SJames Bottomley 9012908d778SJames Bottomley int sas_bios_param(struct scsi_device *scsi_dev, 9022908d778SJames Bottomley struct block_device *bdev, 9032908d778SJames Bottomley sector_t capacity, int *hsc) 9042908d778SJames Bottomley { 9052908d778SJames Bottomley hsc[0] = 255; 9062908d778SJames Bottomley hsc[1] = 63; 9072908d778SJames Bottomley sector_div(capacity, 255*63); 9082908d778SJames Bottomley hsc[2] = capacity; 9092908d778SJames Bottomley 9102908d778SJames Bottomley return 0; 9112908d778SJames Bottomley } 9122908d778SJames Bottomley 913396819fbSDarrick J. Wong /* 914396819fbSDarrick J. Wong * Tell an upper layer that it needs to initiate an abort for a given task. 915396819fbSDarrick J. Wong * This should only ever be called by an LLDD. 916396819fbSDarrick J. Wong */ 917396819fbSDarrick J. Wong void sas_task_abort(struct sas_task *task) 91879a5eb60SDarrick J. Wong { 919396819fbSDarrick J. Wong struct scsi_cmnd *sc = task->uldd_task; 92079a5eb60SDarrick J. Wong 921396819fbSDarrick J. Wong /* Escape for libsas internal commands */ 922396819fbSDarrick J. Wong if (!sc) { 923f0bf750cSDan Williams struct sas_task_slow *slow = task->slow_task; 924f0bf750cSDan Williams 925f0bf750cSDan Williams if (!slow) 92679a5eb60SDarrick J. Wong return; 927f0bf750cSDan Williams if (!del_timer(&slow->timer)) 928f0bf750cSDan Williams return; 929841b86f3SKees Cook slow->timer.function(&slow->timer); 930396819fbSDarrick J. Wong return; 931396819fbSDarrick J. Wong } 93279a5eb60SDarrick J. Wong 9333a2755afSDarrick J. Wong if (dev_is_sata(task->dev)) { 9343a2755afSDarrick J. Wong sas_ata_task_abort(task); 9351b4d0d8eSJames Bottomley } else { 9361b4d0d8eSJames Bottomley struct request_queue *q = sc->device->request_queue; 9371b4d0d8eSJames Bottomley unsigned long flags; 9383a2755afSDarrick J. Wong 93970b25f89STejun Heo spin_lock_irqsave(q->queue_lock, flags); 940242f9dcbSJens Axboe blk_abort_request(sc->request); 94170b25f89STejun Heo spin_unlock_irqrestore(q->queue_lock, flags); 94279a5eb60SDarrick J. Wong } 9431b4d0d8eSJames Bottomley } 94479a5eb60SDarrick J. Wong 945fa1c1e8fSDarrick J. Wong void sas_target_destroy(struct scsi_target *starget) 946fa1c1e8fSDarrick J. Wong { 947735f7d2fSDan Williams struct domain_device *found_dev = starget->hostdata; 948fa1c1e8fSDarrick J. Wong 949fa1c1e8fSDarrick J. Wong if (!found_dev) 950fa1c1e8fSDarrick J. Wong return; 951fa1c1e8fSDarrick J. Wong 952735f7d2fSDan Williams starget->hostdata = NULL; 953735f7d2fSDan Williams sas_put_device(found_dev); 954fa1c1e8fSDarrick J. Wong } 955fa1c1e8fSDarrick J. Wong 95645e6cdf4SDarrick J. Wong #define SAS_STRING_ADDR_SIZE 16 95745e6cdf4SDarrick J. Wong 95845e6cdf4SDarrick J. Wong int sas_request_addr(struct Scsi_Host *shost, u8 *addr) 95945e6cdf4SDarrick J. Wong { 96045e6cdf4SDarrick J. Wong int res; 96145e6cdf4SDarrick J. Wong const struct firmware *fw; 96245e6cdf4SDarrick J. Wong 96345e6cdf4SDarrick J. Wong res = request_firmware(&fw, "sas_addr", &shost->shost_gendev); 96445e6cdf4SDarrick J. Wong if (res) 96545e6cdf4SDarrick J. Wong return res; 96645e6cdf4SDarrick J. Wong 96745e6cdf4SDarrick J. Wong if (fw->size < SAS_STRING_ADDR_SIZE) { 96845e6cdf4SDarrick J. Wong res = -ENODEV; 96945e6cdf4SDarrick J. Wong goto out; 97045e6cdf4SDarrick J. Wong } 97145e6cdf4SDarrick J. Wong 9729ea4e076SAndy Shevchenko res = hex2bin(addr, fw->data, strnlen(fw->data, SAS_ADDR_SIZE * 2) / 2); 9739ea4e076SAndy Shevchenko if (res) 9749ea4e076SAndy Shevchenko goto out; 97545e6cdf4SDarrick J. Wong 97645e6cdf4SDarrick J. Wong out: 97745e6cdf4SDarrick J. Wong release_firmware(fw); 97845e6cdf4SDarrick J. Wong return res; 97945e6cdf4SDarrick J. Wong } 98045e6cdf4SDarrick J. Wong EXPORT_SYMBOL_GPL(sas_request_addr); 98145e6cdf4SDarrick J. Wong 9822908d778SJames Bottomley EXPORT_SYMBOL_GPL(sas_queuecommand); 9832908d778SJames Bottomley EXPORT_SYMBOL_GPL(sas_target_alloc); 9842908d778SJames Bottomley EXPORT_SYMBOL_GPL(sas_slave_configure); 9852908d778SJames Bottomley EXPORT_SYMBOL_GPL(sas_change_queue_depth); 9862908d778SJames Bottomley EXPORT_SYMBOL_GPL(sas_bios_param); 98779a5eb60SDarrick J. Wong EXPORT_SYMBOL_GPL(sas_task_abort); 988dea22214SDarrick J. Wong EXPORT_SYMBOL_GPL(sas_phy_reset); 989ad689233SDarrick J. Wong EXPORT_SYMBOL_GPL(sas_eh_device_reset_handler); 990cc199e78SHannes Reinecke EXPORT_SYMBOL_GPL(sas_eh_target_reset_handler); 991fa1c1e8fSDarrick J. Wong EXPORT_SYMBOL_GPL(sas_target_destroy); 992fa1c1e8fSDarrick J. Wong EXPORT_SYMBOL_GPL(sas_ioctl); 993