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> 30d7a54e30SChristoph Hellwig 312908d778SJames Bottomley #include "sas_internal.h" 322908d778SJames Bottomley 332908d778SJames Bottomley #include <scsi/scsi_host.h> 342908d778SJames Bottomley #include <scsi/scsi_device.h> 352908d778SJames Bottomley #include <scsi/scsi_tcq.h> 362908d778SJames Bottomley #include <scsi/scsi.h> 37f456393eSDarrick J. Wong #include <scsi/scsi_eh.h> 382908d778SJames Bottomley #include <scsi/scsi_transport.h> 392908d778SJames Bottomley #include <scsi/scsi_transport_sas.h> 40338ec570SDarrick J. Wong #include <scsi/sas_ata.h> 412908d778SJames Bottomley #include "../scsi_sas_internal.h" 4279a5eb60SDarrick J. Wong #include "../scsi_transport_api.h" 43ad689233SDarrick J. Wong #include "../scsi_priv.h" 442908d778SJames Bottomley 452908d778SJames Bottomley #include <linux/err.h> 462908d778SJames Bottomley #include <linux/blkdev.h> 4783144186SRafael J. Wysocki #include <linux/freezer.h> 485a0e3ad6STejun Heo #include <linux/gfp.h> 492908d778SJames Bottomley #include <linux/scatterlist.h> 50fa1c1e8fSDarrick J. Wong #include <linux/libata.h> 512908d778SJames Bottomley 52a3a14252SDan Williams /* record final status and free the task */ 53a3a14252SDan Williams static void sas_end_task(struct scsi_cmnd *sc, struct sas_task *task) 542908d778SJames Bottomley { 552908d778SJames Bottomley struct task_status_struct *ts = &task->task_status; 562908d778SJames Bottomley int hs = 0, stat = 0; 572908d778SJames Bottomley 582908d778SJames Bottomley if (ts->resp == SAS_TASK_UNDELIVERED) { 592908d778SJames Bottomley /* transport error */ 602908d778SJames Bottomley hs = DID_NO_CONNECT; 612908d778SJames Bottomley } else { /* ts->resp == SAS_TASK_COMPLETE */ 622908d778SJames Bottomley /* task delivered, what happened afterwards? */ 632908d778SJames Bottomley switch (ts->stat) { 642908d778SJames Bottomley case SAS_DEV_NO_RESPONSE: 652908d778SJames Bottomley case SAS_INTERRUPTED: 662908d778SJames Bottomley case SAS_PHY_DOWN: 672908d778SJames Bottomley case SAS_NAK_R_ERR: 682908d778SJames Bottomley case SAS_OPEN_TO: 692908d778SJames Bottomley hs = DID_NO_CONNECT; 702908d778SJames Bottomley break; 712908d778SJames Bottomley case SAS_DATA_UNDERRUN: 72c13e5566SFUJITA Tomonori scsi_set_resid(sc, ts->residual); 73c13e5566SFUJITA Tomonori if (scsi_bufflen(sc) - scsi_get_resid(sc) < sc->underflow) 742908d778SJames Bottomley hs = DID_ERROR; 752908d778SJames Bottomley break; 762908d778SJames Bottomley case SAS_DATA_OVERRUN: 772908d778SJames Bottomley hs = DID_ERROR; 782908d778SJames Bottomley break; 792908d778SJames Bottomley case SAS_QUEUE_FULL: 802908d778SJames Bottomley hs = DID_SOFT_ERROR; /* retry */ 812908d778SJames Bottomley break; 822908d778SJames Bottomley case SAS_DEVICE_UNKNOWN: 832908d778SJames Bottomley hs = DID_BAD_TARGET; 842908d778SJames Bottomley break; 852908d778SJames Bottomley case SAS_SG_ERR: 862908d778SJames Bottomley hs = DID_PARITY; 872908d778SJames Bottomley break; 882908d778SJames Bottomley case SAS_OPEN_REJECT: 892908d778SJames Bottomley if (ts->open_rej_reason == SAS_OREJ_RSVD_RETRY) 902908d778SJames Bottomley hs = DID_SOFT_ERROR; /* retry */ 912908d778SJames Bottomley else 922908d778SJames Bottomley hs = DID_ERROR; 932908d778SJames Bottomley break; 942908d778SJames Bottomley case SAS_PROTO_RESPONSE: 952908d778SJames Bottomley SAS_DPRINTK("LLDD:%s sent SAS_PROTO_RESP for an SSP " 962908d778SJames Bottomley "task; please report this\n", 972908d778SJames Bottomley task->dev->port->ha->sas_ha_name); 982908d778SJames Bottomley break; 992908d778SJames Bottomley case SAS_ABORTED_TASK: 1002908d778SJames Bottomley hs = DID_ABORT; 1012908d778SJames Bottomley break; 102df64d3caSJames Bottomley case SAM_STAT_CHECK_CONDITION: 1032908d778SJames Bottomley memcpy(sc->sense_buffer, ts->buf, 104cc75e8abSFUJITA Tomonori min(SCSI_SENSE_BUFFERSIZE, ts->buf_valid_size)); 105df64d3caSJames Bottomley stat = SAM_STAT_CHECK_CONDITION; 1062908d778SJames Bottomley break; 1072908d778SJames Bottomley default: 1082908d778SJames Bottomley stat = ts->stat; 1092908d778SJames Bottomley break; 1102908d778SJames Bottomley } 1112908d778SJames Bottomley } 112a3a14252SDan Williams 1132908d778SJames Bottomley sc->result = (hs << 16) | stat; 114a3a14252SDan Williams ASSIGN_SAS_TASK(sc, NULL); 1152908d778SJames Bottomley list_del_init(&task->list); 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 list_del_init(&task->list); 142a3a14252SDan Williams sas_free_task(task); 143a3a14252SDan Williams return; 144a3a14252SDan Williams } 145a3a14252SDan Williams 146a3a14252SDan Williams sas_end_task(sc, task); 1472908d778SJames Bottomley sc->scsi_done(sc); 1482908d778SJames Bottomley } 1492908d778SJames Bottomley 1502908d778SJames Bottomley static struct sas_task *sas_create_task(struct scsi_cmnd *cmd, 1512908d778SJames Bottomley struct domain_device *dev, 1523cc27547SAl Viro gfp_t gfp_flags) 1532908d778SJames Bottomley { 1542908d778SJames Bottomley struct sas_task *task = sas_alloc_task(gfp_flags); 1552908d778SJames Bottomley struct scsi_lun lun; 1562908d778SJames Bottomley 1572908d778SJames Bottomley if (!task) 1582908d778SJames Bottomley return NULL; 1592908d778SJames Bottomley 1602908d778SJames Bottomley task->uldd_task = cmd; 1612908d778SJames Bottomley ASSIGN_SAS_TASK(cmd, task); 1622908d778SJames Bottomley 1632908d778SJames Bottomley task->dev = dev; 1642908d778SJames Bottomley task->task_proto = task->dev->tproto; /* BUG_ON(!SSP) */ 1652908d778SJames Bottomley 1662908d778SJames Bottomley task->ssp_task.retry_count = 1; 1672908d778SJames Bottomley int_to_scsilun(cmd->device->lun, &lun); 1682908d778SJames Bottomley memcpy(task->ssp_task.LUN, &lun.scsi_lun, 8); 1699cbbdca4STejun Heo task->ssp_task.task_attr = TASK_ATTR_SIMPLE; 1702908d778SJames Bottomley memcpy(task->ssp_task.cdb, cmd->cmnd, 16); 1712908d778SJames Bottomley 172c13e5566SFUJITA Tomonori task->scatter = scsi_sglist(cmd); 173c13e5566SFUJITA Tomonori task->num_scatter = scsi_sg_count(cmd); 174c13e5566SFUJITA Tomonori task->total_xfer_len = scsi_bufflen(cmd); 1752908d778SJames Bottomley task->data_dir = cmd->sc_data_direction; 1762908d778SJames Bottomley 1772908d778SJames Bottomley task->task_done = sas_scsi_task_done; 1782908d778SJames Bottomley 1792908d778SJames Bottomley return task; 1802908d778SJames Bottomley } 1812908d778SJames Bottomley 182338ec570SDarrick J. Wong int sas_queue_up(struct sas_task *task) 1832908d778SJames Bottomley { 1842908d778SJames Bottomley struct sas_ha_struct *sas_ha = task->dev->port->ha; 1852908d778SJames Bottomley struct scsi_core *core = &sas_ha->core; 1862908d778SJames Bottomley unsigned long flags; 1872908d778SJames Bottomley LIST_HEAD(list); 1882908d778SJames Bottomley 1892908d778SJames Bottomley spin_lock_irqsave(&core->task_queue_lock, flags); 1902908d778SJames Bottomley if (sas_ha->lldd_queue_size < core->task_queue_size + 1) { 1912908d778SJames Bottomley spin_unlock_irqrestore(&core->task_queue_lock, flags); 1922908d778SJames Bottomley return -SAS_QUEUE_FULL; 1932908d778SJames Bottomley } 1942908d778SJames Bottomley list_add_tail(&task->list, &core->task_queue); 1952908d778SJames Bottomley core->task_queue_size += 1; 1962908d778SJames Bottomley spin_unlock_irqrestore(&core->task_queue_lock, flags); 197d7a54e30SChristoph Hellwig wake_up_process(core->queue_thread); 1982908d778SJames Bottomley 1992908d778SJames Bottomley return 0; 2002908d778SJames Bottomley } 2012908d778SJames Bottomley 20292bd401bSChristoph Hellwig int sas_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd) 2032908d778SJames Bottomley { 2042908d778SJames Bottomley struct sas_internal *i = to_sas_internal(host->transportt); 205a923f756SChristoph Hellwig struct domain_device *dev = cmd_to_domain_dev(cmd); 2062908d778SJames Bottomley struct sas_ha_struct *sas_ha = dev->port->ha; 2072908d778SJames Bottomley struct sas_task *task; 208a923f756SChristoph Hellwig int res = 0; 2092908d778SJames Bottomley 2103673f4bfSDan Williams /* If the device fell off, no sense in issuing commands */ 211e139942dSDan Williams if (test_bit(SAS_DEV_GONE, &dev->state)) { 2123673f4bfSDan Williams cmd->result = DID_BAD_TARGET << 16; 213a923f756SChristoph Hellwig goto out_done; 2143673f4bfSDan Williams } 2153673f4bfSDan Williams 216fa1c1e8fSDarrick J. Wong if (dev_is_sata(dev)) { 217312d3e56SDan Williams spin_lock_irq(dev->sata_dev.ap->lock); 218b27dcfb0SJeff Garzik res = ata_sas_queuecmd(cmd, dev->sata_dev.ap); 219312d3e56SDan Williams spin_unlock_irq(dev->sata_dev.ap->lock); 220a923f756SChristoph Hellwig return res; 221fa1c1e8fSDarrick J. Wong } 222fa1c1e8fSDarrick J. Wong 2232908d778SJames Bottomley task = sas_create_task(cmd, dev, GFP_ATOMIC); 2242908d778SJames Bottomley if (!task) 225ac81c6a8SChristoph Hellwig return SCSI_MLQUEUE_HOST_BUSY; 2262908d778SJames Bottomley 2272908d778SJames Bottomley /* Queue up, Direct Mode or Task Collector Mode. */ 2282908d778SJames Bottomley if (sas_ha->lldd_max_execute_num < 2) 2292908d778SJames Bottomley res = i->dft->lldd_execute_task(task, 1, GFP_ATOMIC); 2302908d778SJames Bottomley else 2312908d778SJames Bottomley res = sas_queue_up(task); 2322908d778SJames Bottomley 233a923f756SChristoph Hellwig if (res) 234a923f756SChristoph Hellwig goto out_free_task; 235a923f756SChristoph Hellwig return 0; 236a923f756SChristoph Hellwig 237a923f756SChristoph Hellwig out_free_task: 2382908d778SJames Bottomley SAS_DPRINTK("lldd_execute_task returned: %d\n", res); 2392908d778SJames Bottomley ASSIGN_SAS_TASK(cmd, NULL); 2402908d778SJames Bottomley sas_free_task(task); 241ac81c6a8SChristoph Hellwig if (res == -SAS_QUEUE_FULL) 242a923f756SChristoph Hellwig cmd->result = DID_SOFT_ERROR << 16; /* retry */ 243ac81c6a8SChristoph Hellwig else 244ac81c6a8SChristoph Hellwig cmd->result = DID_ERROR << 16; 245a923f756SChristoph Hellwig out_done: 246a923f756SChristoph Hellwig cmd->scsi_done(cmd); 247a923f756SChristoph Hellwig return 0; 2482908d778SJames Bottomley } 2492908d778SJames Bottomley 250a8e14fecSJames Bottomley static void sas_eh_finish_cmd(struct scsi_cmnd *cmd) 251a8e14fecSJames Bottomley { 252a8e14fecSJames Bottomley struct sas_task *task = TO_SAS_TASK(cmd); 253a8e14fecSJames Bottomley struct sas_ha_struct *sas_ha = SHOST_TO_SAS_HA(cmd->device->host); 254a8e14fecSJames Bottomley 255a3a14252SDan Williams /* At this point, we only get called following an actual abort 256a3a14252SDan Williams * of the task, so we should be guaranteed not to be racing with 257a3a14252SDan Williams * any completions from the LLD. Task is freed after this. 258a3a14252SDan Williams */ 259a3a14252SDan Williams sas_end_task(cmd, task); 260a3a14252SDan Williams 261a8e14fecSJames Bottomley /* now finish the command and move it on to the error 262a8e14fecSJames Bottomley * handler done list, this also takes it off the 263a3a14252SDan Williams * error handler pending list. 264a3a14252SDan Williams */ 265a8e14fecSJames Bottomley scsi_eh_finish_cmd(cmd, &sas_ha->eh_done_q); 266a8e14fecSJames Bottomley } 267a8e14fecSJames Bottomley 268*3944f509SDan Williams static void sas_eh_defer_cmd(struct scsi_cmnd *cmd) 269*3944f509SDan Williams { 270*3944f509SDan Williams struct sas_task *task = TO_SAS_TASK(cmd); 271*3944f509SDan Williams struct domain_device *dev = task->dev; 272*3944f509SDan Williams struct sas_ha_struct *ha = dev->port->ha; 273*3944f509SDan Williams 274*3944f509SDan Williams if (!dev_is_sata(dev)) { 275*3944f509SDan Williams sas_eh_finish_cmd(cmd); 276*3944f509SDan Williams return; 277*3944f509SDan Williams } 278*3944f509SDan Williams 279*3944f509SDan Williams /* report the timeout to libata */ 280*3944f509SDan Williams sas_end_task(cmd, task); 281*3944f509SDan Williams list_move_tail(&cmd->eh_entry, &ha->eh_ata_q); 282*3944f509SDan Williams } 283*3944f509SDan Williams 2842908d778SJames Bottomley static void sas_scsi_clear_queue_lu(struct list_head *error_q, struct scsi_cmnd *my_cmd) 2852908d778SJames Bottomley { 2862908d778SJames Bottomley struct scsi_cmnd *cmd, *n; 2872908d778SJames Bottomley 2882908d778SJames Bottomley list_for_each_entry_safe(cmd, n, error_q, eh_entry) { 28963e4563bSJames Bottomley if (cmd->device->sdev_target == my_cmd->device->sdev_target && 29063e4563bSJames Bottomley cmd->device->lun == my_cmd->device->lun) 291a8e14fecSJames Bottomley sas_eh_finish_cmd(cmd); 2922908d778SJames Bottomley } 2932908d778SJames Bottomley } 2942908d778SJames Bottomley 2952908d778SJames Bottomley static void sas_scsi_clear_queue_I_T(struct list_head *error_q, 2962908d778SJames Bottomley struct domain_device *dev) 2972908d778SJames Bottomley { 2982908d778SJames Bottomley struct scsi_cmnd *cmd, *n; 2992908d778SJames Bottomley 3002908d778SJames Bottomley list_for_each_entry_safe(cmd, n, error_q, eh_entry) { 3012908d778SJames Bottomley struct domain_device *x = cmd_to_domain_dev(cmd); 3022908d778SJames Bottomley 3032908d778SJames Bottomley if (x == dev) 304a8e14fecSJames Bottomley sas_eh_finish_cmd(cmd); 3052908d778SJames Bottomley } 3062908d778SJames Bottomley } 3072908d778SJames Bottomley 3082908d778SJames Bottomley static void sas_scsi_clear_queue_port(struct list_head *error_q, 3092908d778SJames Bottomley struct asd_sas_port *port) 3102908d778SJames Bottomley { 3112908d778SJames Bottomley struct scsi_cmnd *cmd, *n; 3122908d778SJames Bottomley 3132908d778SJames Bottomley list_for_each_entry_safe(cmd, n, error_q, eh_entry) { 3142908d778SJames Bottomley struct domain_device *dev = cmd_to_domain_dev(cmd); 3152908d778SJames Bottomley struct asd_sas_port *x = dev->port; 3162908d778SJames Bottomley 3172908d778SJames Bottomley if (x == port) 318a8e14fecSJames Bottomley sas_eh_finish_cmd(cmd); 3192908d778SJames Bottomley } 3202908d778SJames Bottomley } 3212908d778SJames Bottomley 3222908d778SJames Bottomley enum task_disposition { 3232908d778SJames Bottomley TASK_IS_DONE, 3242908d778SJames Bottomley TASK_IS_ABORTED, 3252908d778SJames Bottomley TASK_IS_AT_LU, 3269095a64aSDan Williams TASK_IS_NOT_AT_HA, 3272908d778SJames Bottomley TASK_IS_NOT_AT_LU, 32802cd743bSDarrick J. Wong TASK_ABORT_FAILED, 3292908d778SJames Bottomley }; 3302908d778SJames Bottomley 3312908d778SJames Bottomley static enum task_disposition sas_scsi_find_task(struct sas_task *task) 3322908d778SJames Bottomley { 3332908d778SJames Bottomley struct sas_ha_struct *ha = task->dev->port->ha; 3342908d778SJames Bottomley unsigned long flags; 3352908d778SJames Bottomley int i, res; 3362908d778SJames Bottomley struct sas_internal *si = 3372908d778SJames Bottomley to_sas_internal(task->dev->port->ha->core.shost->transportt); 3382908d778SJames Bottomley 3392908d778SJames Bottomley if (ha->lldd_max_execute_num > 1) { 3402908d778SJames Bottomley struct scsi_core *core = &ha->core; 3412908d778SJames Bottomley struct sas_task *t, *n; 3422908d778SJames Bottomley 3439095a64aSDan Williams mutex_lock(&core->task_queue_flush); 3442908d778SJames Bottomley spin_lock_irqsave(&core->task_queue_lock, flags); 3459095a64aSDan Williams list_for_each_entry_safe(t, n, &core->task_queue, list) 3462908d778SJames Bottomley if (task == t) { 3472908d778SJames Bottomley list_del_init(&t->list); 3489095a64aSDan Williams break; 3492908d778SJames Bottomley } 3502908d778SJames Bottomley spin_unlock_irqrestore(&core->task_queue_lock, flags); 3519095a64aSDan Williams mutex_unlock(&core->task_queue_flush); 3529095a64aSDan Williams 3539095a64aSDan Williams if (task == t) 3549095a64aSDan Williams return TASK_IS_NOT_AT_HA; 3552908d778SJames Bottomley } 3562908d778SJames Bottomley 3572908d778SJames Bottomley for (i = 0; i < 5; i++) { 358cadbd4a5SHarvey Harrison SAS_DPRINTK("%s: aborting task 0x%p\n", __func__, task); 3592908d778SJames Bottomley res = si->dft->lldd_abort_task(task); 3602908d778SJames Bottomley 3612908d778SJames Bottomley spin_lock_irqsave(&task->task_state_lock, flags); 3622908d778SJames Bottomley if (task->task_state_flags & SAS_TASK_STATE_DONE) { 3632908d778SJames Bottomley spin_unlock_irqrestore(&task->task_state_lock, flags); 364cadbd4a5SHarvey Harrison SAS_DPRINTK("%s: task 0x%p is done\n", __func__, 3652908d778SJames Bottomley task); 3662908d778SJames Bottomley return TASK_IS_DONE; 3672908d778SJames Bottomley } 3682908d778SJames Bottomley spin_unlock_irqrestore(&task->task_state_lock, flags); 3692908d778SJames Bottomley 3702908d778SJames Bottomley if (res == TMF_RESP_FUNC_COMPLETE) { 3712908d778SJames Bottomley SAS_DPRINTK("%s: task 0x%p is aborted\n", 372cadbd4a5SHarvey Harrison __func__, task); 3732908d778SJames Bottomley return TASK_IS_ABORTED; 3742908d778SJames Bottomley } else if (si->dft->lldd_query_task) { 3752908d778SJames Bottomley SAS_DPRINTK("%s: querying task 0x%p\n", 376cadbd4a5SHarvey Harrison __func__, task); 3772908d778SJames Bottomley res = si->dft->lldd_query_task(task); 37802cd743bSDarrick J. Wong switch (res) { 37902cd743bSDarrick J. Wong case TMF_RESP_FUNC_SUCC: 3802908d778SJames Bottomley SAS_DPRINTK("%s: task 0x%p at LU\n", 381cadbd4a5SHarvey Harrison __func__, task); 3822908d778SJames Bottomley return TASK_IS_AT_LU; 38302cd743bSDarrick J. Wong case TMF_RESP_FUNC_COMPLETE: 3842908d778SJames Bottomley SAS_DPRINTK("%s: task 0x%p not at LU\n", 385cadbd4a5SHarvey Harrison __func__, task); 3862908d778SJames Bottomley return TASK_IS_NOT_AT_LU; 38702cd743bSDarrick J. Wong case TMF_RESP_FUNC_FAILED: 38802cd743bSDarrick J. Wong SAS_DPRINTK("%s: task 0x%p failed to abort\n", 389cadbd4a5SHarvey Harrison __func__, task); 39002cd743bSDarrick J. Wong return TASK_ABORT_FAILED; 3912908d778SJames Bottomley } 39202cd743bSDarrick J. Wong 3932908d778SJames Bottomley } 3942908d778SJames Bottomley } 3952908d778SJames Bottomley return res; 3962908d778SJames Bottomley } 3972908d778SJames Bottomley 3982908d778SJames Bottomley static int sas_recover_lu(struct domain_device *dev, struct scsi_cmnd *cmd) 3992908d778SJames Bottomley { 4002908d778SJames Bottomley int res = TMF_RESP_FUNC_FAILED; 4012908d778SJames Bottomley struct scsi_lun lun; 4022908d778SJames Bottomley struct sas_internal *i = 4032908d778SJames Bottomley to_sas_internal(dev->port->ha->core.shost->transportt); 4042908d778SJames Bottomley 4052908d778SJames Bottomley int_to_scsilun(cmd->device->lun, &lun); 4062908d778SJames Bottomley 4072908d778SJames Bottomley SAS_DPRINTK("eh: device %llx LUN %x has the task\n", 4082908d778SJames Bottomley SAS_ADDR(dev->sas_addr), 4092908d778SJames Bottomley cmd->device->lun); 4102908d778SJames Bottomley 4112908d778SJames Bottomley if (i->dft->lldd_abort_task_set) 4122908d778SJames Bottomley res = i->dft->lldd_abort_task_set(dev, lun.scsi_lun); 4132908d778SJames Bottomley 4142908d778SJames Bottomley if (res == TMF_RESP_FUNC_FAILED) { 4152908d778SJames Bottomley if (i->dft->lldd_clear_task_set) 4162908d778SJames Bottomley res = i->dft->lldd_clear_task_set(dev, lun.scsi_lun); 4172908d778SJames Bottomley } 4182908d778SJames Bottomley 4192908d778SJames Bottomley if (res == TMF_RESP_FUNC_FAILED) { 4202908d778SJames Bottomley if (i->dft->lldd_lu_reset) 4212908d778SJames Bottomley res = i->dft->lldd_lu_reset(dev, lun.scsi_lun); 4222908d778SJames Bottomley } 4232908d778SJames Bottomley 4242908d778SJames Bottomley return res; 4252908d778SJames Bottomley } 4262908d778SJames Bottomley 4272908d778SJames Bottomley static int sas_recover_I_T(struct domain_device *dev) 4282908d778SJames Bottomley { 4292908d778SJames Bottomley int res = TMF_RESP_FUNC_FAILED; 4302908d778SJames Bottomley struct sas_internal *i = 4312908d778SJames Bottomley to_sas_internal(dev->port->ha->core.shost->transportt); 4322908d778SJames Bottomley 4332908d778SJames Bottomley SAS_DPRINTK("I_T nexus reset for dev %016llx\n", 4342908d778SJames Bottomley SAS_ADDR(dev->sas_addr)); 4352908d778SJames Bottomley 4362908d778SJames Bottomley if (i->dft->lldd_I_T_nexus_reset) 4372908d778SJames Bottomley res = i->dft->lldd_I_T_nexus_reset(dev); 4382908d778SJames Bottomley 4392908d778SJames Bottomley return res; 4402908d778SJames Bottomley } 4412908d778SJames Bottomley 442ad689233SDarrick J. Wong /* Find the sas_phy that's attached to this device */ 4435319578cSJames Bottomley struct sas_phy *sas_find_local_phy(struct domain_device *dev) 4443ebf6922SDarrick J. Wong { 445ad689233SDarrick J. Wong struct domain_device *pdev = dev->parent; 446ad689233SDarrick J. Wong struct ex_phy *exphy = NULL; 447ad689233SDarrick J. Wong int i; 4483ebf6922SDarrick J. Wong 449ad689233SDarrick J. Wong /* Directly attached device */ 450ad689233SDarrick J. Wong if (!pdev) 451ad689233SDarrick J. Wong return dev->port->phy; 4523ebf6922SDarrick J. Wong 453ad689233SDarrick J. Wong /* Otherwise look in the expander */ 454ad689233SDarrick J. Wong for (i = 0; i < pdev->ex_dev.num_phys; i++) 455ad689233SDarrick J. Wong if (!memcmp(dev->sas_addr, 456ad689233SDarrick J. Wong pdev->ex_dev.ex_phy[i].attached_sas_addr, 457ad689233SDarrick J. Wong SAS_ADDR_SIZE)) { 458ad689233SDarrick J. Wong exphy = &pdev->ex_dev.ex_phy[i]; 459ad689233SDarrick J. Wong break; 4603ebf6922SDarrick J. Wong } 4613ebf6922SDarrick J. Wong 462ad689233SDarrick J. Wong BUG_ON(!exphy); 463ad689233SDarrick J. Wong return exphy->phy; 464ad689233SDarrick J. Wong } 4655319578cSJames Bottomley EXPORT_SYMBOL_GPL(sas_find_local_phy); 466ad689233SDarrick J. Wong 467a9344e68SDarrick J. Wong /* Attempt to send a LUN reset message to a device */ 468ad689233SDarrick J. Wong int sas_eh_device_reset_handler(struct scsi_cmnd *cmd) 4692908d778SJames Bottomley { 470ad689233SDarrick J. Wong struct domain_device *dev = cmd_to_domain_dev(cmd); 471a9344e68SDarrick J. Wong struct sas_internal *i = 472a9344e68SDarrick J. Wong to_sas_internal(dev->port->ha->core.shost->transportt); 473a9344e68SDarrick J. Wong struct scsi_lun lun; 474a9344e68SDarrick J. Wong int res; 475a9344e68SDarrick J. Wong 476a9344e68SDarrick J. Wong int_to_scsilun(cmd->device->lun, &lun); 477a9344e68SDarrick J. Wong 478a9344e68SDarrick J. Wong if (!i->dft->lldd_lu_reset) 479a9344e68SDarrick J. Wong return FAILED; 480a9344e68SDarrick J. Wong 481a9344e68SDarrick J. Wong res = i->dft->lldd_lu_reset(dev, lun.scsi_lun); 482a9344e68SDarrick J. Wong if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE) 483a9344e68SDarrick J. Wong return SUCCESS; 484a9344e68SDarrick J. Wong 485a9344e68SDarrick J. Wong return FAILED; 486a9344e68SDarrick J. Wong } 487a9344e68SDarrick J. Wong 488a9344e68SDarrick J. Wong /* Attempt to send a phy (bus) reset */ 489a9344e68SDarrick J. Wong int sas_eh_bus_reset_handler(struct scsi_cmnd *cmd) 490a9344e68SDarrick J. Wong { 491a9344e68SDarrick J. Wong struct domain_device *dev = cmd_to_domain_dev(cmd); 4925319578cSJames Bottomley struct sas_phy *phy = sas_find_local_phy(dev); 493ad689233SDarrick J. Wong int res; 494ad689233SDarrick J. Wong 495ad689233SDarrick J. Wong res = sas_phy_reset(phy, 1); 496ad689233SDarrick J. Wong if (res) 497a9344e68SDarrick J. Wong SAS_DPRINTK("Bus reset of %s failed 0x%x\n", 498af5ca3f4SKay Sievers kobject_name(&phy->dev.kobj), 499ad689233SDarrick J. Wong res); 500ad689233SDarrick J. Wong if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE) 501ad689233SDarrick J. Wong return SUCCESS; 502ad689233SDarrick J. Wong 503ad689233SDarrick J. Wong return FAILED; 504ad689233SDarrick J. Wong } 505ad689233SDarrick J. Wong 506ad689233SDarrick J. Wong /* Try to reset a device */ 5078de3ef25SJames Bottomley static int try_to_reset_cmd_device(struct scsi_cmnd *cmd) 508ad689233SDarrick J. Wong { 509a9344e68SDarrick J. Wong int res; 5108de3ef25SJames Bottomley struct Scsi_Host *shost = cmd->device->host; 511ad689233SDarrick J. Wong 512a9344e68SDarrick J. Wong if (!shost->hostt->eh_device_reset_handler) 513a9344e68SDarrick J. Wong goto try_bus_reset; 514a9344e68SDarrick J. Wong 515a9344e68SDarrick J. Wong res = shost->hostt->eh_device_reset_handler(cmd); 516a9344e68SDarrick J. Wong if (res == SUCCESS) 517a9344e68SDarrick J. Wong return res; 518a9344e68SDarrick J. Wong 519a9344e68SDarrick J. Wong try_bus_reset: 520a9344e68SDarrick J. Wong if (shost->hostt->eh_bus_reset_handler) 521a9344e68SDarrick J. Wong return shost->hostt->eh_bus_reset_handler(cmd); 522a9344e68SDarrick J. Wong 523a9344e68SDarrick J. Wong return FAILED; 524ad689233SDarrick J. Wong } 525ad689233SDarrick J. Wong 526ad689233SDarrick J. Wong static int sas_eh_handle_sas_errors(struct Scsi_Host *shost, 5279095a64aSDan Williams struct list_head *work_q) 528ad689233SDarrick J. Wong { 5292908d778SJames Bottomley struct scsi_cmnd *cmd, *n; 5302908d778SJames Bottomley enum task_disposition res = TASK_IS_DONE; 5313ebf6922SDarrick J. Wong int tmf_resp, need_reset; 5322908d778SJames Bottomley struct sas_internal *i = to_sas_internal(shost->transportt); 533ad689233SDarrick J. Wong unsigned long flags; 534ad689233SDarrick J. Wong struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost); 5352908d778SJames Bottomley 5362908d778SJames Bottomley Again: 537ad689233SDarrick J. Wong list_for_each_entry_safe(cmd, n, work_q, eh_entry) { 5389095a64aSDan Williams struct domain_device *dev = cmd_to_domain_dev(cmd); 5399095a64aSDan Williams struct sas_task *task; 5409095a64aSDan Williams 5419095a64aSDan Williams spin_lock_irqsave(&dev->done_lock, flags); 5429095a64aSDan Williams /* by this point the lldd has either observed 5439095a64aSDan Williams * SAS_HA_FROZEN and is leaving the task alone, or has 5449095a64aSDan Williams * won the race with eh and decided to complete it 5459095a64aSDan Williams */ 5469095a64aSDan Williams task = TO_SAS_TASK(cmd); 5479095a64aSDan Williams spin_unlock_irqrestore(&dev->done_lock, flags); 548f456393eSDarrick J. Wong 549ad689233SDarrick J. Wong if (!task) 550f456393eSDarrick J. Wong continue; 551ad689233SDarrick J. Wong 552ad689233SDarrick J. Wong list_del_init(&cmd->eh_entry); 5533ebf6922SDarrick J. Wong 5543ebf6922SDarrick J. Wong spin_lock_irqsave(&task->task_state_lock, flags); 5553ebf6922SDarrick J. Wong need_reset = task->task_state_flags & SAS_TASK_NEED_DEV_RESET; 5563ebf6922SDarrick J. Wong spin_unlock_irqrestore(&task->task_state_lock, flags); 5573ebf6922SDarrick J. Wong 5588de3ef25SJames Bottomley if (need_reset) { 5598de3ef25SJames Bottomley SAS_DPRINTK("%s: task 0x%p requests reset\n", 560cadbd4a5SHarvey Harrison __func__, task); 5618de3ef25SJames Bottomley goto reset; 5628de3ef25SJames Bottomley } 5638de3ef25SJames Bottomley 564f456393eSDarrick J. Wong SAS_DPRINTK("trying to find task 0x%p\n", task); 5652908d778SJames Bottomley res = sas_scsi_find_task(task); 5662908d778SJames Bottomley 5672908d778SJames Bottomley cmd->eh_eflags = 0; 5682908d778SJames Bottomley 5692908d778SJames Bottomley switch (res) { 5709095a64aSDan Williams case TASK_IS_NOT_AT_HA: 5719095a64aSDan Williams SAS_DPRINTK("%s: task 0x%p is not at ha: %s\n", 5729095a64aSDan Williams __func__, task, 5739095a64aSDan Williams cmd->retries ? "retry" : "aborted"); 5749095a64aSDan Williams if (cmd->retries) 5759095a64aSDan Williams cmd->retries--; 5769095a64aSDan Williams sas_eh_finish_cmd(cmd); 5779095a64aSDan Williams continue; 5782908d778SJames Bottomley case TASK_IS_DONE: 579cadbd4a5SHarvey Harrison SAS_DPRINTK("%s: task 0x%p is done\n", __func__, 5802908d778SJames Bottomley task); 581*3944f509SDan Williams sas_eh_defer_cmd(cmd); 5822908d778SJames Bottomley continue; 5832908d778SJames Bottomley case TASK_IS_ABORTED: 5842908d778SJames Bottomley SAS_DPRINTK("%s: task 0x%p is aborted\n", 585cadbd4a5SHarvey Harrison __func__, task); 586*3944f509SDan Williams sas_eh_defer_cmd(cmd); 5872908d778SJames Bottomley continue; 5882908d778SJames Bottomley case TASK_IS_AT_LU: 5892908d778SJames Bottomley SAS_DPRINTK("task 0x%p is at LU: lu recover\n", task); 5908de3ef25SJames Bottomley reset: 5912908d778SJames Bottomley tmf_resp = sas_recover_lu(task->dev, cmd); 5922908d778SJames Bottomley if (tmf_resp == TMF_RESP_FUNC_COMPLETE) { 5932908d778SJames Bottomley SAS_DPRINTK("dev %016llx LU %x is " 5942908d778SJames Bottomley "recovered\n", 5952908d778SJames Bottomley SAS_ADDR(task->dev), 5962908d778SJames Bottomley cmd->device->lun); 597a8e14fecSJames Bottomley sas_eh_finish_cmd(cmd); 598ad689233SDarrick J. Wong sas_scsi_clear_queue_lu(work_q, cmd); 5992908d778SJames Bottomley goto Again; 6002908d778SJames Bottomley } 6012908d778SJames Bottomley /* fallthrough */ 6022908d778SJames Bottomley case TASK_IS_NOT_AT_LU: 60302cd743bSDarrick J. Wong case TASK_ABORT_FAILED: 6042908d778SJames Bottomley SAS_DPRINTK("task 0x%p is not at LU: I_T recover\n", 6052908d778SJames Bottomley task); 6062908d778SJames Bottomley tmf_resp = sas_recover_I_T(task->dev); 6072908d778SJames Bottomley if (tmf_resp == TMF_RESP_FUNC_COMPLETE) { 6088de3ef25SJames Bottomley struct domain_device *dev = task->dev; 6092908d778SJames Bottomley SAS_DPRINTK("I_T %016llx recovered\n", 6102908d778SJames Bottomley SAS_ADDR(task->dev->sas_addr)); 611a8e14fecSJames Bottomley sas_eh_finish_cmd(cmd); 6128de3ef25SJames Bottomley sas_scsi_clear_queue_I_T(work_q, dev); 6132908d778SJames Bottomley goto Again; 6142908d778SJames Bottomley } 6152908d778SJames Bottomley /* Hammer time :-) */ 6168de3ef25SJames Bottomley try_to_reset_cmd_device(cmd); 6172908d778SJames Bottomley if (i->dft->lldd_clear_nexus_port) { 6182908d778SJames Bottomley struct asd_sas_port *port = task->dev->port; 6192908d778SJames Bottomley SAS_DPRINTK("clearing nexus for port:%d\n", 6202908d778SJames Bottomley port->id); 6212908d778SJames Bottomley res = i->dft->lldd_clear_nexus_port(port); 6222908d778SJames Bottomley if (res == TMF_RESP_FUNC_COMPLETE) { 6232908d778SJames Bottomley SAS_DPRINTK("clear nexus port:%d " 6242908d778SJames Bottomley "succeeded\n", port->id); 625a8e14fecSJames Bottomley sas_eh_finish_cmd(cmd); 626ad689233SDarrick J. Wong sas_scsi_clear_queue_port(work_q, 6272908d778SJames Bottomley port); 6282908d778SJames Bottomley goto Again; 6292908d778SJames Bottomley } 6302908d778SJames Bottomley } 6312908d778SJames Bottomley if (i->dft->lldd_clear_nexus_ha) { 6322908d778SJames Bottomley SAS_DPRINTK("clear nexus ha\n"); 6332908d778SJames Bottomley res = i->dft->lldd_clear_nexus_ha(ha); 6342908d778SJames Bottomley if (res == TMF_RESP_FUNC_COMPLETE) { 6352908d778SJames Bottomley SAS_DPRINTK("clear nexus ha " 6362908d778SJames Bottomley "succeeded\n"); 637a8e14fecSJames Bottomley sas_eh_finish_cmd(cmd); 638a8e14fecSJames Bottomley goto clear_q; 6392908d778SJames Bottomley } 6402908d778SJames Bottomley } 6412908d778SJames Bottomley /* If we are here -- this means that no amount 6422908d778SJames Bottomley * of effort could recover from errors. Quite 6432908d778SJames Bottomley * possibly the HA just disappeared. 6442908d778SJames Bottomley */ 6452908d778SJames Bottomley SAS_DPRINTK("error from device %llx, LUN %x " 6462908d778SJames Bottomley "couldn't be recovered in any way\n", 6472908d778SJames Bottomley SAS_ADDR(task->dev->sas_addr), 6482908d778SJames Bottomley cmd->device->lun); 6492908d778SJames Bottomley 650a8e14fecSJames Bottomley sas_eh_finish_cmd(cmd); 6512908d778SJames Bottomley goto clear_q; 6522908d778SJames Bottomley } 6532908d778SJames Bottomley } 654*3944f509SDan Williams list_splice_tail_init(&ha->eh_ata_q, work_q); 655ad689233SDarrick J. Wong return list_empty(work_q); 6562908d778SJames Bottomley clear_q: 657cadbd4a5SHarvey Harrison SAS_DPRINTK("--- Exit %s -- clear_q\n", __func__); 658a8e14fecSJames Bottomley list_for_each_entry_safe(cmd, n, work_q, eh_entry) 659a8e14fecSJames Bottomley sas_eh_finish_cmd(cmd); 660a8e14fecSJames Bottomley 661*3944f509SDan Williams list_splice_tail_init(&ha->eh_ata_q, work_q); 662ad689233SDarrick J. Wong return list_empty(work_q); 663ad689233SDarrick J. Wong } 664ad689233SDarrick J. Wong 665ad689233SDarrick J. Wong void sas_scsi_recover_host(struct Scsi_Host *shost) 666ad689233SDarrick J. Wong { 667ad689233SDarrick J. Wong struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost); 668ad689233SDarrick J. Wong unsigned long flags; 669ad689233SDarrick J. Wong LIST_HEAD(eh_work_q); 670ad689233SDarrick J. Wong 671ad689233SDarrick J. Wong spin_lock_irqsave(shost->host_lock, flags); 672ad689233SDarrick J. Wong list_splice_init(&shost->eh_cmd_q, &eh_work_q); 6739ee91f7fSJames Bottomley shost->host_eh_scheduled = 0; 674ad689233SDarrick J. Wong spin_unlock_irqrestore(shost->host_lock, flags); 675ad689233SDarrick J. Wong 676cadbd4a5SHarvey Harrison SAS_DPRINTK("Enter %s\n", __func__); 677ad689233SDarrick J. Wong /* 678ad689233SDarrick J. Wong * Deal with commands that still have SAS tasks (i.e. they didn't 679ad689233SDarrick J. Wong * complete via the normal sas_task completion mechanism) 680ad689233SDarrick J. Wong */ 6819095a64aSDan Williams set_bit(SAS_HA_FROZEN, &ha->state); 6829095a64aSDan Williams if (sas_eh_handle_sas_errors(shost, &eh_work_q)) 683ad689233SDarrick J. Wong goto out; 684ad689233SDarrick J. Wong 685ad689233SDarrick J. Wong /* 686ad689233SDarrick J. Wong * Now deal with SCSI commands that completed ok but have a an error 687ad689233SDarrick J. Wong * code (and hopefully sense data) attached. This is roughly what 688ad689233SDarrick J. Wong * scsi_unjam_host does, but we skip scsi_eh_abort_cmds because any 689ad689233SDarrick J. Wong * command we see here has no sas_task and is thus unknown to the HA. 690ad689233SDarrick J. Wong */ 69100dd4998SJames Bottomley if (!sas_ata_eh(shost, &eh_work_q, &ha->eh_done_q)) 692ad689233SDarrick J. Wong if (!scsi_eh_get_sense(&eh_work_q, &ha->eh_done_q)) 693ad689233SDarrick J. Wong scsi_eh_ready_devs(shost, &eh_work_q, &ha->eh_done_q); 694ad689233SDarrick J. Wong 695ad689233SDarrick J. Wong out: 6969095a64aSDan Williams clear_bit(SAS_HA_FROZEN, &ha->state); 6979095a64aSDan Williams if (ha->lldd_max_execute_num > 1) 6989095a64aSDan Williams wake_up_process(ha->core.queue_thread); 6999095a64aSDan Williams 70000dd4998SJames Bottomley /* now link into libata eh --- if we have any ata devices */ 70100dd4998SJames Bottomley sas_ata_strategy_handler(shost); 70200dd4998SJames Bottomley 703ad689233SDarrick J. Wong scsi_eh_flush_done_q(&ha->eh_done_q); 70400dd4998SJames Bottomley 705cadbd4a5SHarvey Harrison SAS_DPRINTK("--- Exit %s\n", __func__); 706ad689233SDarrick J. Wong return; 7072908d778SJames Bottomley } 7082908d778SJames Bottomley 709242f9dcbSJens Axboe enum blk_eh_timer_return sas_scsi_timed_out(struct scsi_cmnd *cmd) 7102908d778SJames Bottomley { 7119095a64aSDan Williams scmd_printk(KERN_DEBUG, cmd, "command %p timed out\n", cmd); 7122908d778SJames Bottomley 713242f9dcbSJens Axboe return BLK_EH_NOT_HANDLED; 7142908d778SJames Bottomley } 7152908d778SJames Bottomley 716fa1c1e8fSDarrick J. Wong int sas_ioctl(struct scsi_device *sdev, int cmd, void __user *arg) 717fa1c1e8fSDarrick J. Wong { 718fa1c1e8fSDarrick J. Wong struct domain_device *dev = sdev_to_domain_dev(sdev); 719fa1c1e8fSDarrick J. Wong 720fa1c1e8fSDarrick J. Wong if (dev_is_sata(dev)) 72194be9a58SJeff Garzik return ata_sas_scsi_ioctl(dev->sata_dev.ap, sdev, cmd, arg); 722fa1c1e8fSDarrick J. Wong 723fa1c1e8fSDarrick J. Wong return -EINVAL; 724fa1c1e8fSDarrick J. Wong } 725fa1c1e8fSDarrick J. Wong 7262908d778SJames Bottomley struct domain_device *sas_find_dev_by_rphy(struct sas_rphy *rphy) 7272908d778SJames Bottomley { 7282908d778SJames Bottomley struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent); 7292908d778SJames Bottomley struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost); 7302908d778SJames Bottomley struct domain_device *found_dev = NULL; 7312908d778SJames Bottomley int i; 732980fa2f9SDarrick J. Wong unsigned long flags; 7332908d778SJames Bottomley 734980fa2f9SDarrick J. Wong spin_lock_irqsave(&ha->phy_port_lock, flags); 7352908d778SJames Bottomley for (i = 0; i < ha->num_phys; i++) { 7362908d778SJames Bottomley struct asd_sas_port *port = ha->sas_port[i]; 7372908d778SJames Bottomley struct domain_device *dev; 7382908d778SJames Bottomley 7392908d778SJames Bottomley spin_lock(&port->dev_list_lock); 7402908d778SJames Bottomley list_for_each_entry(dev, &port->dev_list, dev_list_node) { 7412908d778SJames Bottomley if (rphy == dev->rphy) { 7422908d778SJames Bottomley found_dev = dev; 7432908d778SJames Bottomley spin_unlock(&port->dev_list_lock); 7442908d778SJames Bottomley goto found; 7452908d778SJames Bottomley } 7462908d778SJames Bottomley } 7472908d778SJames Bottomley spin_unlock(&port->dev_list_lock); 7482908d778SJames Bottomley } 7492908d778SJames Bottomley found: 750980fa2f9SDarrick J. Wong spin_unlock_irqrestore(&ha->phy_port_lock, flags); 7512908d778SJames Bottomley 7522908d778SJames Bottomley return found_dev; 7532908d778SJames Bottomley } 7542908d778SJames Bottomley 7552908d778SJames Bottomley int sas_target_alloc(struct scsi_target *starget) 7562908d778SJames Bottomley { 757735f7d2fSDan Williams struct sas_rphy *rphy = dev_to_rphy(starget->dev.parent); 758735f7d2fSDan Williams struct domain_device *found_dev = sas_find_dev_by_rphy(rphy); 759338ec570SDarrick J. Wong int res; 7602908d778SJames Bottomley 7612908d778SJames Bottomley if (!found_dev) 7622908d778SJames Bottomley return -ENODEV; 7632908d778SJames Bottomley 764fa1c1e8fSDarrick J. Wong if (dev_is_sata(found_dev)) { 765338ec570SDarrick J. Wong res = sas_ata_init_host_and_port(found_dev, starget); 766338ec570SDarrick J. Wong if (res) 767338ec570SDarrick J. Wong return res; 768fa1c1e8fSDarrick J. Wong } 769fa1c1e8fSDarrick J. Wong 770735f7d2fSDan Williams kref_get(&found_dev->kref); 7712908d778SJames Bottomley starget->hostdata = found_dev; 7722908d778SJames Bottomley return 0; 7732908d778SJames Bottomley } 7742908d778SJames Bottomley 77597a1420dSDan Williams #define SAS_DEF_QD 256 7762908d778SJames Bottomley 7772908d778SJames Bottomley int sas_slave_configure(struct scsi_device *scsi_dev) 7782908d778SJames Bottomley { 7792908d778SJames Bottomley struct domain_device *dev = sdev_to_domain_dev(scsi_dev); 7802908d778SJames Bottomley struct sas_ha_struct *sas_ha; 7812908d778SJames Bottomley 7822908d778SJames Bottomley BUG_ON(dev->rphy->identify.device_type != SAS_END_DEVICE); 7832908d778SJames Bottomley 784fa1c1e8fSDarrick J. Wong if (dev_is_sata(dev)) { 785fa1c1e8fSDarrick J. Wong ata_sas_slave_configure(scsi_dev, dev->sata_dev.ap); 786fa1c1e8fSDarrick J. Wong return 0; 787fa1c1e8fSDarrick J. Wong } 788fa1c1e8fSDarrick J. Wong 7892908d778SJames Bottomley sas_ha = dev->port->ha; 7902908d778SJames Bottomley 7912908d778SJames Bottomley sas_read_port_mode_page(scsi_dev); 7922908d778SJames Bottomley 7932908d778SJames Bottomley if (scsi_dev->tagged_supported) { 7942908d778SJames Bottomley scsi_set_tag_type(scsi_dev, MSG_SIMPLE_TAG); 7952908d778SJames Bottomley scsi_activate_tcq(scsi_dev, SAS_DEF_QD); 7962908d778SJames Bottomley } else { 7972908d778SJames Bottomley SAS_DPRINTK("device %llx, LUN %x doesn't support " 7982908d778SJames Bottomley "TCQ\n", SAS_ADDR(dev->sas_addr), 7992908d778SJames Bottomley scsi_dev->lun); 8002908d778SJames Bottomley scsi_dev->tagged_supported = 0; 8012908d778SJames Bottomley scsi_set_tag_type(scsi_dev, 0); 8022908d778SJames Bottomley scsi_deactivate_tcq(scsi_dev, 1); 8032908d778SJames Bottomley } 8042908d778SJames Bottomley 805f27708fcSDarrick J. Wong scsi_dev->allow_restart = 1; 806f27708fcSDarrick J. Wong 8072908d778SJames Bottomley return 0; 8082908d778SJames Bottomley } 8092908d778SJames Bottomley 81097a1420dSDan Williams int sas_change_queue_depth(struct scsi_device *sdev, int depth, int reason) 8112908d778SJames Bottomley { 81297a1420dSDan Williams struct domain_device *dev = sdev_to_domain_dev(sdev); 8132908d778SJames Bottomley 814f6e67035SDan Williams if (dev_is_sata(dev)) 81597a1420dSDan Williams return __ata_change_queue_depth(dev->sata_dev.ap, sdev, depth, 81697a1420dSDan Williams reason); 817f6e67035SDan Williams 81897a1420dSDan Williams switch (reason) { 81997a1420dSDan Williams case SCSI_QDEPTH_DEFAULT: 82097a1420dSDan Williams case SCSI_QDEPTH_RAMP_UP: 82197a1420dSDan Williams if (!sdev->tagged_supported) 82297a1420dSDan Williams depth = 1; 82397a1420dSDan Williams scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth); 82497a1420dSDan Williams break; 82597a1420dSDan Williams case SCSI_QDEPTH_QFULL: 82697a1420dSDan Williams scsi_track_queue_full(sdev, depth); 82797a1420dSDan Williams break; 82897a1420dSDan Williams default: 829e881a172SMike Christie return -EOPNOTSUPP; 8302908d778SJames Bottomley } 8312908d778SJames Bottomley 83297a1420dSDan Williams return depth; 8332908d778SJames Bottomley } 8342908d778SJames Bottomley 8352908d778SJames Bottomley int sas_change_queue_type(struct scsi_device *scsi_dev, int qt) 8362908d778SJames Bottomley { 837f6e67035SDan Williams struct domain_device *dev = sdev_to_domain_dev(scsi_dev); 838f6e67035SDan Williams 839f6e67035SDan Williams if (dev_is_sata(dev)) 840f6e67035SDan Williams return -EINVAL; 841f6e67035SDan Williams 8422908d778SJames Bottomley if (!scsi_dev->tagged_supported) 8432908d778SJames Bottomley return 0; 8442908d778SJames Bottomley 8452908d778SJames Bottomley scsi_deactivate_tcq(scsi_dev, 1); 8462908d778SJames Bottomley 8472908d778SJames Bottomley scsi_set_tag_type(scsi_dev, qt); 8482908d778SJames Bottomley scsi_activate_tcq(scsi_dev, scsi_dev->queue_depth); 8492908d778SJames Bottomley 8502908d778SJames Bottomley return qt; 8512908d778SJames Bottomley } 8522908d778SJames Bottomley 8532908d778SJames Bottomley int sas_bios_param(struct scsi_device *scsi_dev, 8542908d778SJames Bottomley struct block_device *bdev, 8552908d778SJames Bottomley sector_t capacity, int *hsc) 8562908d778SJames Bottomley { 8572908d778SJames Bottomley hsc[0] = 255; 8582908d778SJames Bottomley hsc[1] = 63; 8592908d778SJames Bottomley sector_div(capacity, 255*63); 8602908d778SJames Bottomley hsc[2] = capacity; 8612908d778SJames Bottomley 8622908d778SJames Bottomley return 0; 8632908d778SJames Bottomley } 8642908d778SJames Bottomley 8652908d778SJames Bottomley /* ---------- Task Collector Thread implementation ---------- */ 8662908d778SJames Bottomley 8672908d778SJames Bottomley static void sas_queue(struct sas_ha_struct *sas_ha) 8682908d778SJames Bottomley { 8692908d778SJames Bottomley struct scsi_core *core = &sas_ha->core; 8702908d778SJames Bottomley unsigned long flags; 8712908d778SJames Bottomley LIST_HEAD(q); 8722908d778SJames Bottomley int can_queue; 8732908d778SJames Bottomley int res; 8742908d778SJames Bottomley struct sas_internal *i = to_sas_internal(core->shost->transportt); 8752908d778SJames Bottomley 8769095a64aSDan Williams mutex_lock(&core->task_queue_flush); 8772908d778SJames Bottomley spin_lock_irqsave(&core->task_queue_lock, flags); 878d7a54e30SChristoph Hellwig while (!kthread_should_stop() && 8799095a64aSDan Williams !list_empty(&core->task_queue) && 8809095a64aSDan Williams !test_bit(SAS_HA_FROZEN, &sas_ha->state)) { 8812908d778SJames Bottomley 8822908d778SJames Bottomley can_queue = sas_ha->lldd_queue_size - core->task_queue_size; 8832908d778SJames Bottomley if (can_queue >= 0) { 8842908d778SJames Bottomley can_queue = core->task_queue_size; 8852908d778SJames Bottomley list_splice_init(&core->task_queue, &q); 8862908d778SJames Bottomley } else { 8872908d778SJames Bottomley struct list_head *a, *n; 8882908d778SJames Bottomley 8892908d778SJames Bottomley can_queue = sas_ha->lldd_queue_size; 8902908d778SJames Bottomley list_for_each_safe(a, n, &core->task_queue) { 8912908d778SJames Bottomley list_move_tail(a, &q); 8922908d778SJames Bottomley if (--can_queue == 0) 8932908d778SJames Bottomley break; 8942908d778SJames Bottomley } 8952908d778SJames Bottomley can_queue = sas_ha->lldd_queue_size; 8962908d778SJames Bottomley } 8972908d778SJames Bottomley core->task_queue_size -= can_queue; 8982908d778SJames Bottomley spin_unlock_irqrestore(&core->task_queue_lock, flags); 8992908d778SJames Bottomley { 9002908d778SJames Bottomley struct sas_task *task = list_entry(q.next, 9012908d778SJames Bottomley struct sas_task, 9022908d778SJames Bottomley list); 9032908d778SJames Bottomley list_del_init(&q); 9042908d778SJames Bottomley res = i->dft->lldd_execute_task(task, can_queue, 9052908d778SJames Bottomley GFP_KERNEL); 9062908d778SJames Bottomley if (unlikely(res)) 9072908d778SJames Bottomley __list_add(&q, task->list.prev, &task->list); 9082908d778SJames Bottomley } 9092908d778SJames Bottomley spin_lock_irqsave(&core->task_queue_lock, flags); 9102908d778SJames Bottomley if (res) { 9112908d778SJames Bottomley list_splice_init(&q, &core->task_queue); /*at head*/ 9122908d778SJames Bottomley core->task_queue_size += can_queue; 9132908d778SJames Bottomley } 9142908d778SJames Bottomley } 9152908d778SJames Bottomley spin_unlock_irqrestore(&core->task_queue_lock, flags); 9169095a64aSDan Williams mutex_unlock(&core->task_queue_flush); 9172908d778SJames Bottomley } 9182908d778SJames Bottomley 9192908d778SJames Bottomley /** 9202908d778SJames Bottomley * sas_queue_thread -- The Task Collector thread 9212908d778SJames Bottomley * @_sas_ha: pointer to struct sas_ha 9222908d778SJames Bottomley */ 9232908d778SJames Bottomley static int sas_queue_thread(void *_sas_ha) 9242908d778SJames Bottomley { 9252908d778SJames Bottomley struct sas_ha_struct *sas_ha = _sas_ha; 9262908d778SJames Bottomley 9272908d778SJames Bottomley while (1) { 928d7a54e30SChristoph Hellwig set_current_state(TASK_INTERRUPTIBLE); 929d7a54e30SChristoph Hellwig schedule(); 9302908d778SJames Bottomley sas_queue(sas_ha); 931d7a54e30SChristoph Hellwig if (kthread_should_stop()) 9322908d778SJames Bottomley break; 9332908d778SJames Bottomley } 9342908d778SJames Bottomley 9352908d778SJames Bottomley return 0; 9362908d778SJames Bottomley } 9372908d778SJames Bottomley 9382908d778SJames Bottomley int sas_init_queue(struct sas_ha_struct *sas_ha) 9392908d778SJames Bottomley { 9402908d778SJames Bottomley struct scsi_core *core = &sas_ha->core; 9412908d778SJames Bottomley 9422908d778SJames Bottomley spin_lock_init(&core->task_queue_lock); 9439095a64aSDan Williams mutex_init(&core->task_queue_flush); 9442908d778SJames Bottomley core->task_queue_size = 0; 9452908d778SJames Bottomley INIT_LIST_HEAD(&core->task_queue); 9462908d778SJames Bottomley 947d7a54e30SChristoph Hellwig core->queue_thread = kthread_run(sas_queue_thread, sas_ha, 948d7a54e30SChristoph Hellwig "sas_queue_%d", core->shost->host_no); 949d7a54e30SChristoph Hellwig if (IS_ERR(core->queue_thread)) 950d7a54e30SChristoph Hellwig return PTR_ERR(core->queue_thread); 951d7a54e30SChristoph Hellwig return 0; 9522908d778SJames Bottomley } 9532908d778SJames Bottomley 9542908d778SJames Bottomley void sas_shutdown_queue(struct sas_ha_struct *sas_ha) 9552908d778SJames Bottomley { 9562908d778SJames Bottomley unsigned long flags; 9572908d778SJames Bottomley struct scsi_core *core = &sas_ha->core; 9582908d778SJames Bottomley struct sas_task *task, *n; 9592908d778SJames Bottomley 960d7a54e30SChristoph Hellwig kthread_stop(core->queue_thread); 9612908d778SJames Bottomley 9622908d778SJames Bottomley if (!list_empty(&core->task_queue)) 9632908d778SJames Bottomley SAS_DPRINTK("HA: %llx: scsi core task queue is NOT empty!?\n", 9642908d778SJames Bottomley SAS_ADDR(sas_ha->sas_addr)); 9652908d778SJames Bottomley 9662908d778SJames Bottomley spin_lock_irqsave(&core->task_queue_lock, flags); 9672908d778SJames Bottomley list_for_each_entry_safe(task, n, &core->task_queue, list) { 9682908d778SJames Bottomley struct scsi_cmnd *cmd = task->uldd_task; 9692908d778SJames Bottomley 9702908d778SJames Bottomley list_del_init(&task->list); 9712908d778SJames Bottomley 9722908d778SJames Bottomley ASSIGN_SAS_TASK(cmd, NULL); 9732908d778SJames Bottomley sas_free_task(task); 9742908d778SJames Bottomley cmd->result = DID_ABORT << 16; 9752908d778SJames Bottomley cmd->scsi_done(cmd); 9762908d778SJames Bottomley } 9772908d778SJames Bottomley spin_unlock_irqrestore(&core->task_queue_lock, flags); 9782908d778SJames Bottomley } 9792908d778SJames Bottomley 980396819fbSDarrick J. Wong /* 981396819fbSDarrick J. Wong * Tell an upper layer that it needs to initiate an abort for a given task. 982396819fbSDarrick J. Wong * This should only ever be called by an LLDD. 983396819fbSDarrick J. Wong */ 984396819fbSDarrick J. Wong void sas_task_abort(struct sas_task *task) 98579a5eb60SDarrick J. Wong { 986396819fbSDarrick J. Wong struct scsi_cmnd *sc = task->uldd_task; 98779a5eb60SDarrick J. Wong 988396819fbSDarrick J. Wong /* Escape for libsas internal commands */ 989396819fbSDarrick J. Wong if (!sc) { 990396819fbSDarrick J. Wong if (!del_timer(&task->timer)) 99179a5eb60SDarrick J. Wong return; 992396819fbSDarrick J. Wong task->timer.function(task->timer.data); 993396819fbSDarrick J. Wong return; 994396819fbSDarrick J. Wong } 99579a5eb60SDarrick J. Wong 9963a2755afSDarrick J. Wong if (dev_is_sata(task->dev)) { 9973a2755afSDarrick J. Wong sas_ata_task_abort(task); 9981b4d0d8eSJames Bottomley } else { 9991b4d0d8eSJames Bottomley struct request_queue *q = sc->device->request_queue; 10001b4d0d8eSJames Bottomley unsigned long flags; 10013a2755afSDarrick J. Wong 100270b25f89STejun Heo spin_lock_irqsave(q->queue_lock, flags); 1003242f9dcbSJens Axboe blk_abort_request(sc->request); 100470b25f89STejun Heo spin_unlock_irqrestore(q->queue_lock, flags); 1005396819fbSDarrick J. Wong scsi_schedule_eh(sc->device->host); 100679a5eb60SDarrick J. Wong } 10071b4d0d8eSJames Bottomley } 100879a5eb60SDarrick J. Wong 1009fa1c1e8fSDarrick J. Wong int sas_slave_alloc(struct scsi_device *scsi_dev) 1010fa1c1e8fSDarrick J. Wong { 1011fa1c1e8fSDarrick J. Wong struct domain_device *dev = sdev_to_domain_dev(scsi_dev); 1012fa1c1e8fSDarrick J. Wong 1013fa1c1e8fSDarrick J. Wong if (dev_is_sata(dev)) 1014fa1c1e8fSDarrick J. Wong return ata_sas_port_init(dev->sata_dev.ap); 1015fa1c1e8fSDarrick J. Wong 1016fa1c1e8fSDarrick J. Wong return 0; 1017fa1c1e8fSDarrick J. Wong } 1018fa1c1e8fSDarrick J. Wong 1019fa1c1e8fSDarrick J. Wong void sas_target_destroy(struct scsi_target *starget) 1020fa1c1e8fSDarrick J. Wong { 1021735f7d2fSDan Williams struct domain_device *found_dev = starget->hostdata; 1022fa1c1e8fSDarrick J. Wong 1023fa1c1e8fSDarrick J. Wong if (!found_dev) 1024fa1c1e8fSDarrick J. Wong return; 1025fa1c1e8fSDarrick J. Wong 1026fa1c1e8fSDarrick J. Wong if (dev_is_sata(found_dev)) 1027fa1c1e8fSDarrick J. Wong ata_sas_port_destroy(found_dev->sata_dev.ap); 1028fa1c1e8fSDarrick J. Wong 1029735f7d2fSDan Williams starget->hostdata = NULL; 1030735f7d2fSDan Williams sas_put_device(found_dev); 1031fa1c1e8fSDarrick J. Wong } 1032fa1c1e8fSDarrick J. Wong 103345e6cdf4SDarrick J. Wong static void sas_parse_addr(u8 *sas_addr, const char *p) 103445e6cdf4SDarrick J. Wong { 103545e6cdf4SDarrick J. Wong int i; 103645e6cdf4SDarrick J. Wong for (i = 0; i < SAS_ADDR_SIZE; i++) { 103745e6cdf4SDarrick J. Wong u8 h, l; 103845e6cdf4SDarrick J. Wong if (!*p) 103945e6cdf4SDarrick J. Wong break; 104045e6cdf4SDarrick J. Wong h = isdigit(*p) ? *p-'0' : toupper(*p)-'A'+10; 104145e6cdf4SDarrick J. Wong p++; 104245e6cdf4SDarrick J. Wong l = isdigit(*p) ? *p-'0' : toupper(*p)-'A'+10; 104345e6cdf4SDarrick J. Wong p++; 104445e6cdf4SDarrick J. Wong sas_addr[i] = (h<<4) | l; 104545e6cdf4SDarrick J. Wong } 104645e6cdf4SDarrick J. Wong } 104745e6cdf4SDarrick J. Wong 104845e6cdf4SDarrick J. Wong #define SAS_STRING_ADDR_SIZE 16 104945e6cdf4SDarrick J. Wong 105045e6cdf4SDarrick J. Wong int sas_request_addr(struct Scsi_Host *shost, u8 *addr) 105145e6cdf4SDarrick J. Wong { 105245e6cdf4SDarrick J. Wong int res; 105345e6cdf4SDarrick J. Wong const struct firmware *fw; 105445e6cdf4SDarrick J. Wong 105545e6cdf4SDarrick J. Wong res = request_firmware(&fw, "sas_addr", &shost->shost_gendev); 105645e6cdf4SDarrick J. Wong if (res) 105745e6cdf4SDarrick J. Wong return res; 105845e6cdf4SDarrick J. Wong 105945e6cdf4SDarrick J. Wong if (fw->size < SAS_STRING_ADDR_SIZE) { 106045e6cdf4SDarrick J. Wong res = -ENODEV; 106145e6cdf4SDarrick J. Wong goto out; 106245e6cdf4SDarrick J. Wong } 106345e6cdf4SDarrick J. Wong 106445e6cdf4SDarrick J. Wong sas_parse_addr(addr, fw->data); 106545e6cdf4SDarrick J. Wong 106645e6cdf4SDarrick J. Wong out: 106745e6cdf4SDarrick J. Wong release_firmware(fw); 106845e6cdf4SDarrick J. Wong return res; 106945e6cdf4SDarrick J. Wong } 107045e6cdf4SDarrick J. Wong EXPORT_SYMBOL_GPL(sas_request_addr); 107145e6cdf4SDarrick J. Wong 10722908d778SJames Bottomley EXPORT_SYMBOL_GPL(sas_queuecommand); 10732908d778SJames Bottomley EXPORT_SYMBOL_GPL(sas_target_alloc); 10742908d778SJames Bottomley EXPORT_SYMBOL_GPL(sas_slave_configure); 10752908d778SJames Bottomley EXPORT_SYMBOL_GPL(sas_change_queue_depth); 10762908d778SJames Bottomley EXPORT_SYMBOL_GPL(sas_change_queue_type); 10772908d778SJames Bottomley EXPORT_SYMBOL_GPL(sas_bios_param); 107879a5eb60SDarrick J. Wong EXPORT_SYMBOL_GPL(sas_task_abort); 1079dea22214SDarrick J. Wong EXPORT_SYMBOL_GPL(sas_phy_reset); 1080acbf167dSDarrick J. Wong EXPORT_SYMBOL_GPL(sas_phy_enable); 1081ad689233SDarrick J. Wong EXPORT_SYMBOL_GPL(sas_eh_device_reset_handler); 1082a9344e68SDarrick J. Wong EXPORT_SYMBOL_GPL(sas_eh_bus_reset_handler); 1083fa1c1e8fSDarrick J. Wong EXPORT_SYMBOL_GPL(sas_slave_alloc); 1084fa1c1e8fSDarrick J. Wong EXPORT_SYMBOL_GPL(sas_target_destroy); 1085fa1c1e8fSDarrick J. Wong EXPORT_SYMBOL_GPL(sas_ioctl); 1086