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 52*a3a14252SDan Williams /* record final status and free the task */ 53*a3a14252SDan 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 } 112*a3a14252SDan Williams 1132908d778SJames Bottomley sc->result = (hs << 16) | stat; 114*a3a14252SDan Williams ASSIGN_SAS_TASK(sc, NULL); 1152908d778SJames Bottomley list_del_init(&task->list); 1162908d778SJames Bottomley sas_free_task(task); 117*a3a14252SDan Williams } 118*a3a14252SDan Williams 119*a3a14252SDan Williams static void sas_scsi_task_done(struct sas_task *task) 120*a3a14252SDan Williams { 121*a3a14252SDan Williams struct scsi_cmnd *sc = task->uldd_task; 122*a3a14252SDan Williams 123*a3a14252SDan Williams if (unlikely(task->task_state_flags & SAS_TASK_STATE_ABORTED)) { 124*a3a14252SDan Williams /* Aborted tasks will be completed by the error handler */ 125*a3a14252SDan Williams SAS_DPRINTK("task done but aborted\n"); 126*a3a14252SDan Williams return; 127*a3a14252SDan Williams } 128*a3a14252SDan Williams 129*a3a14252SDan Williams if (unlikely(!sc)) { 130*a3a14252SDan Williams SAS_DPRINTK("task_done called with non existing SCSI cmnd!\n"); 131*a3a14252SDan Williams list_del_init(&task->list); 132*a3a14252SDan Williams sas_free_task(task); 133*a3a14252SDan Williams return; 134*a3a14252SDan Williams } 135*a3a14252SDan Williams 136*a3a14252SDan Williams ASSIGN_SAS_TASK(sc, NULL); 137*a3a14252SDan Williams sas_end_task(sc, task); 1382908d778SJames Bottomley sc->scsi_done(sc); 1392908d778SJames Bottomley } 1402908d778SJames Bottomley 1412908d778SJames Bottomley static struct sas_task *sas_create_task(struct scsi_cmnd *cmd, 1422908d778SJames Bottomley struct domain_device *dev, 1433cc27547SAl Viro gfp_t gfp_flags) 1442908d778SJames Bottomley { 1452908d778SJames Bottomley struct sas_task *task = sas_alloc_task(gfp_flags); 1462908d778SJames Bottomley struct scsi_lun lun; 1472908d778SJames Bottomley 1482908d778SJames Bottomley if (!task) 1492908d778SJames Bottomley return NULL; 1502908d778SJames Bottomley 1512908d778SJames Bottomley task->uldd_task = cmd; 1522908d778SJames Bottomley ASSIGN_SAS_TASK(cmd, task); 1532908d778SJames Bottomley 1542908d778SJames Bottomley task->dev = dev; 1552908d778SJames Bottomley task->task_proto = task->dev->tproto; /* BUG_ON(!SSP) */ 1562908d778SJames Bottomley 1572908d778SJames Bottomley task->ssp_task.retry_count = 1; 1582908d778SJames Bottomley int_to_scsilun(cmd->device->lun, &lun); 1592908d778SJames Bottomley memcpy(task->ssp_task.LUN, &lun.scsi_lun, 8); 1609cbbdca4STejun Heo task->ssp_task.task_attr = TASK_ATTR_SIMPLE; 1612908d778SJames Bottomley memcpy(task->ssp_task.cdb, cmd->cmnd, 16); 1622908d778SJames Bottomley 163c13e5566SFUJITA Tomonori task->scatter = scsi_sglist(cmd); 164c13e5566SFUJITA Tomonori task->num_scatter = scsi_sg_count(cmd); 165c13e5566SFUJITA Tomonori task->total_xfer_len = scsi_bufflen(cmd); 1662908d778SJames Bottomley task->data_dir = cmd->sc_data_direction; 1672908d778SJames Bottomley 1682908d778SJames Bottomley task->task_done = sas_scsi_task_done; 1692908d778SJames Bottomley 1702908d778SJames Bottomley return task; 1712908d778SJames Bottomley } 1722908d778SJames Bottomley 173338ec570SDarrick J. Wong int sas_queue_up(struct sas_task *task) 1742908d778SJames Bottomley { 1752908d778SJames Bottomley struct sas_ha_struct *sas_ha = task->dev->port->ha; 1762908d778SJames Bottomley struct scsi_core *core = &sas_ha->core; 1772908d778SJames Bottomley unsigned long flags; 1782908d778SJames Bottomley LIST_HEAD(list); 1792908d778SJames Bottomley 1802908d778SJames Bottomley spin_lock_irqsave(&core->task_queue_lock, flags); 1812908d778SJames Bottomley if (sas_ha->lldd_queue_size < core->task_queue_size + 1) { 1822908d778SJames Bottomley spin_unlock_irqrestore(&core->task_queue_lock, flags); 1832908d778SJames Bottomley return -SAS_QUEUE_FULL; 1842908d778SJames Bottomley } 1852908d778SJames Bottomley list_add_tail(&task->list, &core->task_queue); 1862908d778SJames Bottomley core->task_queue_size += 1; 1872908d778SJames Bottomley spin_unlock_irqrestore(&core->task_queue_lock, flags); 188d7a54e30SChristoph Hellwig wake_up_process(core->queue_thread); 1892908d778SJames Bottomley 1902908d778SJames Bottomley return 0; 1912908d778SJames Bottomley } 1922908d778SJames Bottomley 19392bd401bSChristoph Hellwig int sas_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd) 1942908d778SJames Bottomley { 1952908d778SJames Bottomley struct sas_internal *i = to_sas_internal(host->transportt); 196a923f756SChristoph Hellwig struct domain_device *dev = cmd_to_domain_dev(cmd); 1972908d778SJames Bottomley struct sas_ha_struct *sas_ha = dev->port->ha; 1982908d778SJames Bottomley struct sas_task *task; 199a923f756SChristoph Hellwig int res = 0; 2002908d778SJames Bottomley 2013673f4bfSDan Williams /* If the device fell off, no sense in issuing commands */ 202e139942dSDan Williams if (test_bit(SAS_DEV_GONE, &dev->state)) { 2033673f4bfSDan Williams cmd->result = DID_BAD_TARGET << 16; 204a923f756SChristoph Hellwig goto out_done; 2053673f4bfSDan Williams } 2063673f4bfSDan Williams 207fa1c1e8fSDarrick J. Wong if (dev_is_sata(dev)) { 208312d3e56SDan Williams spin_lock_irq(dev->sata_dev.ap->lock); 209b27dcfb0SJeff Garzik res = ata_sas_queuecmd(cmd, dev->sata_dev.ap); 210312d3e56SDan Williams spin_unlock_irq(dev->sata_dev.ap->lock); 211a923f756SChristoph Hellwig return res; 212fa1c1e8fSDarrick J. Wong } 213fa1c1e8fSDarrick J. Wong 2142908d778SJames Bottomley task = sas_create_task(cmd, dev, GFP_ATOMIC); 2152908d778SJames Bottomley if (!task) 216ac81c6a8SChristoph Hellwig return SCSI_MLQUEUE_HOST_BUSY; 2172908d778SJames Bottomley 2182908d778SJames Bottomley /* Queue up, Direct Mode or Task Collector Mode. */ 2192908d778SJames Bottomley if (sas_ha->lldd_max_execute_num < 2) 2202908d778SJames Bottomley res = i->dft->lldd_execute_task(task, 1, GFP_ATOMIC); 2212908d778SJames Bottomley else 2222908d778SJames Bottomley res = sas_queue_up(task); 2232908d778SJames Bottomley 224a923f756SChristoph Hellwig if (res) 225a923f756SChristoph Hellwig goto out_free_task; 226a923f756SChristoph Hellwig return 0; 227a923f756SChristoph Hellwig 228a923f756SChristoph Hellwig out_free_task: 2292908d778SJames Bottomley SAS_DPRINTK("lldd_execute_task returned: %d\n", res); 2302908d778SJames Bottomley ASSIGN_SAS_TASK(cmd, NULL); 2312908d778SJames Bottomley sas_free_task(task); 232ac81c6a8SChristoph Hellwig if (res == -SAS_QUEUE_FULL) 233a923f756SChristoph Hellwig cmd->result = DID_SOFT_ERROR << 16; /* retry */ 234ac81c6a8SChristoph Hellwig else 235ac81c6a8SChristoph Hellwig cmd->result = DID_ERROR << 16; 236a923f756SChristoph Hellwig out_done: 237a923f756SChristoph Hellwig cmd->scsi_done(cmd); 238a923f756SChristoph Hellwig return 0; 2392908d778SJames Bottomley } 2402908d778SJames Bottomley 241a8e14fecSJames Bottomley static void sas_eh_finish_cmd(struct scsi_cmnd *cmd) 242a8e14fecSJames Bottomley { 243a8e14fecSJames Bottomley struct sas_task *task = TO_SAS_TASK(cmd); 244a8e14fecSJames Bottomley struct sas_ha_struct *sas_ha = SHOST_TO_SAS_HA(cmd->device->host); 245a8e14fecSJames Bottomley 246*a3a14252SDan Williams /* At this point, we only get called following an actual abort 247*a3a14252SDan Williams * of the task, so we should be guaranteed not to be racing with 248*a3a14252SDan Williams * any completions from the LLD. Task is freed after this. 249*a3a14252SDan Williams */ 250*a3a14252SDan Williams sas_end_task(cmd, task); 251*a3a14252SDan Williams 252a8e14fecSJames Bottomley /* now finish the command and move it on to the error 253a8e14fecSJames Bottomley * handler done list, this also takes it off the 254*a3a14252SDan Williams * error handler pending list. 255*a3a14252SDan Williams */ 256a8e14fecSJames Bottomley scsi_eh_finish_cmd(cmd, &sas_ha->eh_done_q); 257a8e14fecSJames Bottomley } 258a8e14fecSJames Bottomley 2592908d778SJames Bottomley static void sas_scsi_clear_queue_lu(struct list_head *error_q, struct scsi_cmnd *my_cmd) 2602908d778SJames Bottomley { 2612908d778SJames Bottomley struct scsi_cmnd *cmd, *n; 2622908d778SJames Bottomley 2632908d778SJames Bottomley list_for_each_entry_safe(cmd, n, error_q, eh_entry) { 26463e4563bSJames Bottomley if (cmd->device->sdev_target == my_cmd->device->sdev_target && 26563e4563bSJames Bottomley cmd->device->lun == my_cmd->device->lun) 266a8e14fecSJames Bottomley sas_eh_finish_cmd(cmd); 2672908d778SJames Bottomley } 2682908d778SJames Bottomley } 2692908d778SJames Bottomley 2702908d778SJames Bottomley static void sas_scsi_clear_queue_I_T(struct list_head *error_q, 2712908d778SJames Bottomley struct domain_device *dev) 2722908d778SJames Bottomley { 2732908d778SJames Bottomley struct scsi_cmnd *cmd, *n; 2742908d778SJames Bottomley 2752908d778SJames Bottomley list_for_each_entry_safe(cmd, n, error_q, eh_entry) { 2762908d778SJames Bottomley struct domain_device *x = cmd_to_domain_dev(cmd); 2772908d778SJames Bottomley 2782908d778SJames Bottomley if (x == dev) 279a8e14fecSJames Bottomley sas_eh_finish_cmd(cmd); 2802908d778SJames Bottomley } 2812908d778SJames Bottomley } 2822908d778SJames Bottomley 2832908d778SJames Bottomley static void sas_scsi_clear_queue_port(struct list_head *error_q, 2842908d778SJames Bottomley struct asd_sas_port *port) 2852908d778SJames Bottomley { 2862908d778SJames Bottomley struct scsi_cmnd *cmd, *n; 2872908d778SJames Bottomley 2882908d778SJames Bottomley list_for_each_entry_safe(cmd, n, error_q, eh_entry) { 2892908d778SJames Bottomley struct domain_device *dev = cmd_to_domain_dev(cmd); 2902908d778SJames Bottomley struct asd_sas_port *x = dev->port; 2912908d778SJames Bottomley 2922908d778SJames Bottomley if (x == port) 293a8e14fecSJames Bottomley sas_eh_finish_cmd(cmd); 2942908d778SJames Bottomley } 2952908d778SJames Bottomley } 2962908d778SJames Bottomley 2972908d778SJames Bottomley enum task_disposition { 2982908d778SJames Bottomley TASK_IS_DONE, 2992908d778SJames Bottomley TASK_IS_ABORTED, 3002908d778SJames Bottomley TASK_IS_AT_LU, 3012908d778SJames Bottomley TASK_IS_NOT_AT_LU, 30202cd743bSDarrick J. Wong TASK_ABORT_FAILED, 3032908d778SJames Bottomley }; 3042908d778SJames Bottomley 3052908d778SJames Bottomley static enum task_disposition sas_scsi_find_task(struct sas_task *task) 3062908d778SJames Bottomley { 3072908d778SJames Bottomley struct sas_ha_struct *ha = task->dev->port->ha; 3082908d778SJames Bottomley unsigned long flags; 3092908d778SJames Bottomley int i, res; 3102908d778SJames Bottomley struct sas_internal *si = 3112908d778SJames Bottomley to_sas_internal(task->dev->port->ha->core.shost->transportt); 3122908d778SJames Bottomley 3132908d778SJames Bottomley if (ha->lldd_max_execute_num > 1) { 3142908d778SJames Bottomley struct scsi_core *core = &ha->core; 3152908d778SJames Bottomley struct sas_task *t, *n; 3162908d778SJames Bottomley 3172908d778SJames Bottomley spin_lock_irqsave(&core->task_queue_lock, flags); 3182908d778SJames Bottomley list_for_each_entry_safe(t, n, &core->task_queue, list) { 3192908d778SJames Bottomley if (task == t) { 3202908d778SJames Bottomley list_del_init(&t->list); 3212908d778SJames Bottomley spin_unlock_irqrestore(&core->task_queue_lock, 3222908d778SJames Bottomley flags); 3232908d778SJames Bottomley SAS_DPRINTK("%s: task 0x%p aborted from " 3242908d778SJames Bottomley "task_queue\n", 325cadbd4a5SHarvey Harrison __func__, task); 3262908d778SJames Bottomley return TASK_IS_ABORTED; 3272908d778SJames Bottomley } 3282908d778SJames Bottomley } 3292908d778SJames Bottomley spin_unlock_irqrestore(&core->task_queue_lock, flags); 3302908d778SJames Bottomley } 3312908d778SJames Bottomley 3322908d778SJames Bottomley for (i = 0; i < 5; i++) { 333cadbd4a5SHarvey Harrison SAS_DPRINTK("%s: aborting task 0x%p\n", __func__, task); 3342908d778SJames Bottomley res = si->dft->lldd_abort_task(task); 3352908d778SJames Bottomley 3362908d778SJames Bottomley spin_lock_irqsave(&task->task_state_lock, flags); 3372908d778SJames Bottomley if (task->task_state_flags & SAS_TASK_STATE_DONE) { 3382908d778SJames Bottomley spin_unlock_irqrestore(&task->task_state_lock, flags); 339cadbd4a5SHarvey Harrison SAS_DPRINTK("%s: task 0x%p is done\n", __func__, 3402908d778SJames Bottomley task); 3412908d778SJames Bottomley return TASK_IS_DONE; 3422908d778SJames Bottomley } 3432908d778SJames Bottomley spin_unlock_irqrestore(&task->task_state_lock, flags); 3442908d778SJames Bottomley 3452908d778SJames Bottomley if (res == TMF_RESP_FUNC_COMPLETE) { 3462908d778SJames Bottomley SAS_DPRINTK("%s: task 0x%p is aborted\n", 347cadbd4a5SHarvey Harrison __func__, task); 3482908d778SJames Bottomley return TASK_IS_ABORTED; 3492908d778SJames Bottomley } else if (si->dft->lldd_query_task) { 3502908d778SJames Bottomley SAS_DPRINTK("%s: querying task 0x%p\n", 351cadbd4a5SHarvey Harrison __func__, task); 3522908d778SJames Bottomley res = si->dft->lldd_query_task(task); 35302cd743bSDarrick J. Wong switch (res) { 35402cd743bSDarrick J. Wong case TMF_RESP_FUNC_SUCC: 3552908d778SJames Bottomley SAS_DPRINTK("%s: task 0x%p at LU\n", 356cadbd4a5SHarvey Harrison __func__, task); 3572908d778SJames Bottomley return TASK_IS_AT_LU; 35802cd743bSDarrick J. Wong case TMF_RESP_FUNC_COMPLETE: 3592908d778SJames Bottomley SAS_DPRINTK("%s: task 0x%p not at LU\n", 360cadbd4a5SHarvey Harrison __func__, task); 3612908d778SJames Bottomley return TASK_IS_NOT_AT_LU; 36202cd743bSDarrick J. Wong case TMF_RESP_FUNC_FAILED: 36302cd743bSDarrick J. Wong SAS_DPRINTK("%s: task 0x%p failed to abort\n", 364cadbd4a5SHarvey Harrison __func__, task); 36502cd743bSDarrick J. Wong return TASK_ABORT_FAILED; 3662908d778SJames Bottomley } 36702cd743bSDarrick J. Wong 3682908d778SJames Bottomley } 3692908d778SJames Bottomley } 3702908d778SJames Bottomley return res; 3712908d778SJames Bottomley } 3722908d778SJames Bottomley 3732908d778SJames Bottomley static int sas_recover_lu(struct domain_device *dev, struct scsi_cmnd *cmd) 3742908d778SJames Bottomley { 3752908d778SJames Bottomley int res = TMF_RESP_FUNC_FAILED; 3762908d778SJames Bottomley struct scsi_lun lun; 3772908d778SJames Bottomley struct sas_internal *i = 3782908d778SJames Bottomley to_sas_internal(dev->port->ha->core.shost->transportt); 3792908d778SJames Bottomley 3802908d778SJames Bottomley int_to_scsilun(cmd->device->lun, &lun); 3812908d778SJames Bottomley 3822908d778SJames Bottomley SAS_DPRINTK("eh: device %llx LUN %x has the task\n", 3832908d778SJames Bottomley SAS_ADDR(dev->sas_addr), 3842908d778SJames Bottomley cmd->device->lun); 3852908d778SJames Bottomley 3862908d778SJames Bottomley if (i->dft->lldd_abort_task_set) 3872908d778SJames Bottomley res = i->dft->lldd_abort_task_set(dev, lun.scsi_lun); 3882908d778SJames Bottomley 3892908d778SJames Bottomley if (res == TMF_RESP_FUNC_FAILED) { 3902908d778SJames Bottomley if (i->dft->lldd_clear_task_set) 3912908d778SJames Bottomley res = i->dft->lldd_clear_task_set(dev, lun.scsi_lun); 3922908d778SJames Bottomley } 3932908d778SJames Bottomley 3942908d778SJames Bottomley if (res == TMF_RESP_FUNC_FAILED) { 3952908d778SJames Bottomley if (i->dft->lldd_lu_reset) 3962908d778SJames Bottomley res = i->dft->lldd_lu_reset(dev, lun.scsi_lun); 3972908d778SJames Bottomley } 3982908d778SJames Bottomley 3992908d778SJames Bottomley return res; 4002908d778SJames Bottomley } 4012908d778SJames Bottomley 4022908d778SJames Bottomley static int sas_recover_I_T(struct domain_device *dev) 4032908d778SJames Bottomley { 4042908d778SJames Bottomley int res = TMF_RESP_FUNC_FAILED; 4052908d778SJames Bottomley struct sas_internal *i = 4062908d778SJames Bottomley to_sas_internal(dev->port->ha->core.shost->transportt); 4072908d778SJames Bottomley 4082908d778SJames Bottomley SAS_DPRINTK("I_T nexus reset for dev %016llx\n", 4092908d778SJames Bottomley SAS_ADDR(dev->sas_addr)); 4102908d778SJames Bottomley 4112908d778SJames Bottomley if (i->dft->lldd_I_T_nexus_reset) 4122908d778SJames Bottomley res = i->dft->lldd_I_T_nexus_reset(dev); 4132908d778SJames Bottomley 4142908d778SJames Bottomley return res; 4152908d778SJames Bottomley } 4162908d778SJames Bottomley 417ad689233SDarrick J. Wong /* Find the sas_phy that's attached to this device */ 4185319578cSJames Bottomley struct sas_phy *sas_find_local_phy(struct domain_device *dev) 4193ebf6922SDarrick J. Wong { 420ad689233SDarrick J. Wong struct domain_device *pdev = dev->parent; 421ad689233SDarrick J. Wong struct ex_phy *exphy = NULL; 422ad689233SDarrick J. Wong int i; 4233ebf6922SDarrick J. Wong 424ad689233SDarrick J. Wong /* Directly attached device */ 425ad689233SDarrick J. Wong if (!pdev) 426ad689233SDarrick J. Wong return dev->port->phy; 4273ebf6922SDarrick J. Wong 428ad689233SDarrick J. Wong /* Otherwise look in the expander */ 429ad689233SDarrick J. Wong for (i = 0; i < pdev->ex_dev.num_phys; i++) 430ad689233SDarrick J. Wong if (!memcmp(dev->sas_addr, 431ad689233SDarrick J. Wong pdev->ex_dev.ex_phy[i].attached_sas_addr, 432ad689233SDarrick J. Wong SAS_ADDR_SIZE)) { 433ad689233SDarrick J. Wong exphy = &pdev->ex_dev.ex_phy[i]; 434ad689233SDarrick J. Wong break; 4353ebf6922SDarrick J. Wong } 4363ebf6922SDarrick J. Wong 437ad689233SDarrick J. Wong BUG_ON(!exphy); 438ad689233SDarrick J. Wong return exphy->phy; 439ad689233SDarrick J. Wong } 4405319578cSJames Bottomley EXPORT_SYMBOL_GPL(sas_find_local_phy); 441ad689233SDarrick J. Wong 442a9344e68SDarrick J. Wong /* Attempt to send a LUN reset message to a device */ 443ad689233SDarrick J. Wong int sas_eh_device_reset_handler(struct scsi_cmnd *cmd) 4442908d778SJames Bottomley { 445ad689233SDarrick J. Wong struct domain_device *dev = cmd_to_domain_dev(cmd); 446a9344e68SDarrick J. Wong struct sas_internal *i = 447a9344e68SDarrick J. Wong to_sas_internal(dev->port->ha->core.shost->transportt); 448a9344e68SDarrick J. Wong struct scsi_lun lun; 449a9344e68SDarrick J. Wong int res; 450a9344e68SDarrick J. Wong 451a9344e68SDarrick J. Wong int_to_scsilun(cmd->device->lun, &lun); 452a9344e68SDarrick J. Wong 453a9344e68SDarrick J. Wong if (!i->dft->lldd_lu_reset) 454a9344e68SDarrick J. Wong return FAILED; 455a9344e68SDarrick J. Wong 456a9344e68SDarrick J. Wong res = i->dft->lldd_lu_reset(dev, lun.scsi_lun); 457a9344e68SDarrick J. Wong if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE) 458a9344e68SDarrick J. Wong return SUCCESS; 459a9344e68SDarrick J. Wong 460a9344e68SDarrick J. Wong return FAILED; 461a9344e68SDarrick J. Wong } 462a9344e68SDarrick J. Wong 463a9344e68SDarrick J. Wong /* Attempt to send a phy (bus) reset */ 464a9344e68SDarrick J. Wong int sas_eh_bus_reset_handler(struct scsi_cmnd *cmd) 465a9344e68SDarrick J. Wong { 466a9344e68SDarrick J. Wong struct domain_device *dev = cmd_to_domain_dev(cmd); 4675319578cSJames Bottomley struct sas_phy *phy = sas_find_local_phy(dev); 468ad689233SDarrick J. Wong int res; 469ad689233SDarrick J. Wong 470ad689233SDarrick J. Wong res = sas_phy_reset(phy, 1); 471ad689233SDarrick J. Wong if (res) 472a9344e68SDarrick J. Wong SAS_DPRINTK("Bus reset of %s failed 0x%x\n", 473af5ca3f4SKay Sievers kobject_name(&phy->dev.kobj), 474ad689233SDarrick J. Wong res); 475ad689233SDarrick J. Wong if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE) 476ad689233SDarrick J. Wong return SUCCESS; 477ad689233SDarrick J. Wong 478ad689233SDarrick J. Wong return FAILED; 479ad689233SDarrick J. Wong } 480ad689233SDarrick J. Wong 481ad689233SDarrick J. Wong /* Try to reset a device */ 4828de3ef25SJames Bottomley static int try_to_reset_cmd_device(struct scsi_cmnd *cmd) 483ad689233SDarrick J. Wong { 484a9344e68SDarrick J. Wong int res; 4858de3ef25SJames Bottomley struct Scsi_Host *shost = cmd->device->host; 486ad689233SDarrick J. Wong 487a9344e68SDarrick J. Wong if (!shost->hostt->eh_device_reset_handler) 488a9344e68SDarrick J. Wong goto try_bus_reset; 489a9344e68SDarrick J. Wong 490a9344e68SDarrick J. Wong res = shost->hostt->eh_device_reset_handler(cmd); 491a9344e68SDarrick J. Wong if (res == SUCCESS) 492a9344e68SDarrick J. Wong return res; 493a9344e68SDarrick J. Wong 494a9344e68SDarrick J. Wong try_bus_reset: 495a9344e68SDarrick J. Wong if (shost->hostt->eh_bus_reset_handler) 496a9344e68SDarrick J. Wong return shost->hostt->eh_bus_reset_handler(cmd); 497a9344e68SDarrick J. Wong 498a9344e68SDarrick J. Wong return FAILED; 499ad689233SDarrick J. Wong } 500ad689233SDarrick J. Wong 501ad689233SDarrick J. Wong static int sas_eh_handle_sas_errors(struct Scsi_Host *shost, 502ad689233SDarrick J. Wong struct list_head *work_q, 503ad689233SDarrick J. Wong struct list_head *done_q) 504ad689233SDarrick J. Wong { 5052908d778SJames Bottomley struct scsi_cmnd *cmd, *n; 5062908d778SJames Bottomley enum task_disposition res = TASK_IS_DONE; 5073ebf6922SDarrick J. Wong int tmf_resp, need_reset; 5082908d778SJames Bottomley struct sas_internal *i = to_sas_internal(shost->transportt); 509ad689233SDarrick J. Wong unsigned long flags; 510ad689233SDarrick J. Wong struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost); 5112908d778SJames Bottomley 5122908d778SJames Bottomley Again: 513ad689233SDarrick J. Wong list_for_each_entry_safe(cmd, n, work_q, eh_entry) { 5142908d778SJames Bottomley struct sas_task *task = TO_SAS_TASK(cmd); 515f456393eSDarrick J. Wong 516ad689233SDarrick J. Wong if (!task) 517f456393eSDarrick J. Wong continue; 518ad689233SDarrick J. Wong 519ad689233SDarrick J. Wong list_del_init(&cmd->eh_entry); 5203ebf6922SDarrick J. Wong 5213ebf6922SDarrick J. Wong spin_lock_irqsave(&task->task_state_lock, flags); 5223ebf6922SDarrick J. Wong need_reset = task->task_state_flags & SAS_TASK_NEED_DEV_RESET; 5233ebf6922SDarrick J. Wong spin_unlock_irqrestore(&task->task_state_lock, flags); 5243ebf6922SDarrick J. Wong 5258de3ef25SJames Bottomley if (need_reset) { 5268de3ef25SJames Bottomley SAS_DPRINTK("%s: task 0x%p requests reset\n", 527cadbd4a5SHarvey Harrison __func__, task); 5288de3ef25SJames Bottomley goto reset; 5298de3ef25SJames Bottomley } 5308de3ef25SJames Bottomley 531f456393eSDarrick J. Wong SAS_DPRINTK("trying to find task 0x%p\n", task); 5322908d778SJames Bottomley res = sas_scsi_find_task(task); 5332908d778SJames Bottomley 5342908d778SJames Bottomley cmd->eh_eflags = 0; 5352908d778SJames Bottomley 5362908d778SJames Bottomley switch (res) { 5372908d778SJames Bottomley case TASK_IS_DONE: 538cadbd4a5SHarvey Harrison SAS_DPRINTK("%s: task 0x%p is done\n", __func__, 5392908d778SJames Bottomley task); 540a8e14fecSJames Bottomley sas_eh_finish_cmd(cmd); 5412908d778SJames Bottomley continue; 5422908d778SJames Bottomley case TASK_IS_ABORTED: 5432908d778SJames Bottomley SAS_DPRINTK("%s: task 0x%p is aborted\n", 544cadbd4a5SHarvey Harrison __func__, task); 545a8e14fecSJames Bottomley sas_eh_finish_cmd(cmd); 5462908d778SJames Bottomley continue; 5472908d778SJames Bottomley case TASK_IS_AT_LU: 5482908d778SJames Bottomley SAS_DPRINTK("task 0x%p is at LU: lu recover\n", task); 5498de3ef25SJames Bottomley reset: 5502908d778SJames Bottomley tmf_resp = sas_recover_lu(task->dev, cmd); 5512908d778SJames Bottomley if (tmf_resp == TMF_RESP_FUNC_COMPLETE) { 5522908d778SJames Bottomley SAS_DPRINTK("dev %016llx LU %x is " 5532908d778SJames Bottomley "recovered\n", 5542908d778SJames Bottomley SAS_ADDR(task->dev), 5552908d778SJames Bottomley cmd->device->lun); 556a8e14fecSJames Bottomley sas_eh_finish_cmd(cmd); 557ad689233SDarrick J. Wong sas_scsi_clear_queue_lu(work_q, cmd); 5582908d778SJames Bottomley goto Again; 5592908d778SJames Bottomley } 5602908d778SJames Bottomley /* fallthrough */ 5612908d778SJames Bottomley case TASK_IS_NOT_AT_LU: 56202cd743bSDarrick J. Wong case TASK_ABORT_FAILED: 5632908d778SJames Bottomley SAS_DPRINTK("task 0x%p is not at LU: I_T recover\n", 5642908d778SJames Bottomley task); 5652908d778SJames Bottomley tmf_resp = sas_recover_I_T(task->dev); 5662908d778SJames Bottomley if (tmf_resp == TMF_RESP_FUNC_COMPLETE) { 5678de3ef25SJames Bottomley struct domain_device *dev = task->dev; 5682908d778SJames Bottomley SAS_DPRINTK("I_T %016llx recovered\n", 5692908d778SJames Bottomley SAS_ADDR(task->dev->sas_addr)); 570a8e14fecSJames Bottomley sas_eh_finish_cmd(cmd); 5718de3ef25SJames Bottomley sas_scsi_clear_queue_I_T(work_q, dev); 5722908d778SJames Bottomley goto Again; 5732908d778SJames Bottomley } 5742908d778SJames Bottomley /* Hammer time :-) */ 5758de3ef25SJames Bottomley try_to_reset_cmd_device(cmd); 5762908d778SJames Bottomley if (i->dft->lldd_clear_nexus_port) { 5772908d778SJames Bottomley struct asd_sas_port *port = task->dev->port; 5782908d778SJames Bottomley SAS_DPRINTK("clearing nexus for port:%d\n", 5792908d778SJames Bottomley port->id); 5802908d778SJames Bottomley res = i->dft->lldd_clear_nexus_port(port); 5812908d778SJames Bottomley if (res == TMF_RESP_FUNC_COMPLETE) { 5822908d778SJames Bottomley SAS_DPRINTK("clear nexus port:%d " 5832908d778SJames Bottomley "succeeded\n", port->id); 584a8e14fecSJames Bottomley sas_eh_finish_cmd(cmd); 585ad689233SDarrick J. Wong sas_scsi_clear_queue_port(work_q, 5862908d778SJames Bottomley port); 5872908d778SJames Bottomley goto Again; 5882908d778SJames Bottomley } 5892908d778SJames Bottomley } 5902908d778SJames Bottomley if (i->dft->lldd_clear_nexus_ha) { 5912908d778SJames Bottomley SAS_DPRINTK("clear nexus ha\n"); 5922908d778SJames Bottomley res = i->dft->lldd_clear_nexus_ha(ha); 5932908d778SJames Bottomley if (res == TMF_RESP_FUNC_COMPLETE) { 5942908d778SJames Bottomley SAS_DPRINTK("clear nexus ha " 5952908d778SJames Bottomley "succeeded\n"); 596a8e14fecSJames Bottomley sas_eh_finish_cmd(cmd); 597a8e14fecSJames Bottomley goto clear_q; 5982908d778SJames Bottomley } 5992908d778SJames Bottomley } 6002908d778SJames Bottomley /* If we are here -- this means that no amount 6012908d778SJames Bottomley * of effort could recover from errors. Quite 6022908d778SJames Bottomley * possibly the HA just disappeared. 6032908d778SJames Bottomley */ 6042908d778SJames Bottomley SAS_DPRINTK("error from device %llx, LUN %x " 6052908d778SJames Bottomley "couldn't be recovered in any way\n", 6062908d778SJames Bottomley SAS_ADDR(task->dev->sas_addr), 6072908d778SJames Bottomley cmd->device->lun); 6082908d778SJames Bottomley 609a8e14fecSJames Bottomley sas_eh_finish_cmd(cmd); 6102908d778SJames Bottomley goto clear_q; 6112908d778SJames Bottomley } 6122908d778SJames Bottomley } 613ad689233SDarrick J. Wong return list_empty(work_q); 6142908d778SJames Bottomley clear_q: 615cadbd4a5SHarvey Harrison SAS_DPRINTK("--- Exit %s -- clear_q\n", __func__); 616a8e14fecSJames Bottomley list_for_each_entry_safe(cmd, n, work_q, eh_entry) 617a8e14fecSJames Bottomley sas_eh_finish_cmd(cmd); 618a8e14fecSJames Bottomley 619ad689233SDarrick J. Wong return list_empty(work_q); 620ad689233SDarrick J. Wong } 621ad689233SDarrick J. Wong 622ad689233SDarrick J. Wong void sas_scsi_recover_host(struct Scsi_Host *shost) 623ad689233SDarrick J. Wong { 624ad689233SDarrick J. Wong struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost); 625ad689233SDarrick J. Wong unsigned long flags; 626ad689233SDarrick J. Wong LIST_HEAD(eh_work_q); 627ad689233SDarrick J. Wong 628ad689233SDarrick J. Wong spin_lock_irqsave(shost->host_lock, flags); 629ad689233SDarrick J. Wong list_splice_init(&shost->eh_cmd_q, &eh_work_q); 6309ee91f7fSJames Bottomley shost->host_eh_scheduled = 0; 631ad689233SDarrick J. Wong spin_unlock_irqrestore(shost->host_lock, flags); 632ad689233SDarrick J. Wong 633cadbd4a5SHarvey Harrison SAS_DPRINTK("Enter %s\n", __func__); 634ad689233SDarrick J. Wong /* 635ad689233SDarrick J. Wong * Deal with commands that still have SAS tasks (i.e. they didn't 636ad689233SDarrick J. Wong * complete via the normal sas_task completion mechanism) 637ad689233SDarrick J. Wong */ 638ad689233SDarrick J. Wong if (sas_eh_handle_sas_errors(shost, &eh_work_q, &ha->eh_done_q)) 639ad689233SDarrick J. Wong goto out; 640ad689233SDarrick J. Wong 641ad689233SDarrick J. Wong /* 642ad689233SDarrick J. Wong * Now deal with SCSI commands that completed ok but have a an error 643ad689233SDarrick J. Wong * code (and hopefully sense data) attached. This is roughly what 644ad689233SDarrick J. Wong * scsi_unjam_host does, but we skip scsi_eh_abort_cmds because any 645ad689233SDarrick J. Wong * command we see here has no sas_task and is thus unknown to the HA. 646ad689233SDarrick J. Wong */ 64700dd4998SJames Bottomley if (!sas_ata_eh(shost, &eh_work_q, &ha->eh_done_q)) 648ad689233SDarrick J. Wong if (!scsi_eh_get_sense(&eh_work_q, &ha->eh_done_q)) 649ad689233SDarrick J. Wong scsi_eh_ready_devs(shost, &eh_work_q, &ha->eh_done_q); 650ad689233SDarrick J. Wong 651ad689233SDarrick J. Wong out: 65200dd4998SJames Bottomley /* now link into libata eh --- if we have any ata devices */ 65300dd4998SJames Bottomley sas_ata_strategy_handler(shost); 65400dd4998SJames Bottomley 655ad689233SDarrick J. Wong scsi_eh_flush_done_q(&ha->eh_done_q); 65600dd4998SJames Bottomley 657cadbd4a5SHarvey Harrison SAS_DPRINTK("--- Exit %s\n", __func__); 658ad689233SDarrick J. Wong return; 6592908d778SJames Bottomley } 6602908d778SJames Bottomley 661242f9dcbSJens Axboe enum blk_eh_timer_return sas_scsi_timed_out(struct scsi_cmnd *cmd) 6622908d778SJames Bottomley { 6632908d778SJames Bottomley struct sas_task *task = TO_SAS_TASK(cmd); 6642908d778SJames Bottomley unsigned long flags; 66500dd4998SJames Bottomley enum blk_eh_timer_return rtn; 66600dd4998SJames Bottomley 66700dd4998SJames Bottomley if (sas_ata_timed_out(cmd, task, &rtn)) 66800dd4998SJames Bottomley return rtn; 6692908d778SJames Bottomley 6702908d778SJames Bottomley if (!task) { 671242f9dcbSJens Axboe cmd->request->timeout /= 2; 6726d4dcd4dSDarrick J. Wong SAS_DPRINTK("command 0x%p, task 0x%p, gone: %s\n", 673242f9dcbSJens Axboe cmd, task, (cmd->request->timeout ? 674242f9dcbSJens Axboe "BLK_EH_RESET_TIMER" : "BLK_EH_NOT_HANDLED")); 675242f9dcbSJens Axboe if (!cmd->request->timeout) 676242f9dcbSJens Axboe return BLK_EH_NOT_HANDLED; 677242f9dcbSJens Axboe return BLK_EH_RESET_TIMER; 6782908d778SJames Bottomley } 6792908d778SJames Bottomley 6802908d778SJames Bottomley spin_lock_irqsave(&task->task_state_lock, flags); 681396819fbSDarrick J. Wong BUG_ON(task->task_state_flags & SAS_TASK_STATE_ABORTED); 6822908d778SJames Bottomley if (task->task_state_flags & SAS_TASK_STATE_DONE) { 6832908d778SJames Bottomley spin_unlock_irqrestore(&task->task_state_lock, flags); 684242f9dcbSJens Axboe SAS_DPRINTK("command 0x%p, task 0x%p, timed out: " 685242f9dcbSJens Axboe "BLK_EH_HANDLED\n", cmd, task); 686242f9dcbSJens Axboe return BLK_EH_HANDLED; 6872908d778SJames Bottomley } 688b218a0d8SDarrick J. Wong if (!(task->task_state_flags & SAS_TASK_AT_INITIATOR)) { 689b218a0d8SDarrick J. Wong spin_unlock_irqrestore(&task->task_state_lock, flags); 690b218a0d8SDarrick J. Wong SAS_DPRINTK("command 0x%p, task 0x%p, not at initiator: " 691242f9dcbSJens Axboe "BLK_EH_RESET_TIMER\n", 692b218a0d8SDarrick J. Wong cmd, task); 693242f9dcbSJens Axboe return BLK_EH_RESET_TIMER; 694b218a0d8SDarrick J. Wong } 6952908d778SJames Bottomley task->task_state_flags |= SAS_TASK_STATE_ABORTED; 6962908d778SJames Bottomley spin_unlock_irqrestore(&task->task_state_lock, flags); 6972908d778SJames Bottomley 698242f9dcbSJens Axboe SAS_DPRINTK("command 0x%p, task 0x%p, timed out: BLK_EH_NOT_HANDLED\n", 6992908d778SJames Bottomley cmd, task); 7002908d778SJames Bottomley 701242f9dcbSJens Axboe return BLK_EH_NOT_HANDLED; 7022908d778SJames Bottomley } 7032908d778SJames Bottomley 704fa1c1e8fSDarrick J. Wong int sas_ioctl(struct scsi_device *sdev, int cmd, void __user *arg) 705fa1c1e8fSDarrick J. Wong { 706fa1c1e8fSDarrick J. Wong struct domain_device *dev = sdev_to_domain_dev(sdev); 707fa1c1e8fSDarrick J. Wong 708fa1c1e8fSDarrick J. Wong if (dev_is_sata(dev)) 70994be9a58SJeff Garzik return ata_sas_scsi_ioctl(dev->sata_dev.ap, sdev, cmd, arg); 710fa1c1e8fSDarrick J. Wong 711fa1c1e8fSDarrick J. Wong return -EINVAL; 712fa1c1e8fSDarrick J. Wong } 713fa1c1e8fSDarrick J. Wong 7142908d778SJames Bottomley struct domain_device *sas_find_dev_by_rphy(struct sas_rphy *rphy) 7152908d778SJames Bottomley { 7162908d778SJames Bottomley struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent); 7172908d778SJames Bottomley struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost); 7182908d778SJames Bottomley struct domain_device *found_dev = NULL; 7192908d778SJames Bottomley int i; 720980fa2f9SDarrick J. Wong unsigned long flags; 7212908d778SJames Bottomley 722980fa2f9SDarrick J. Wong spin_lock_irqsave(&ha->phy_port_lock, flags); 7232908d778SJames Bottomley for (i = 0; i < ha->num_phys; i++) { 7242908d778SJames Bottomley struct asd_sas_port *port = ha->sas_port[i]; 7252908d778SJames Bottomley struct domain_device *dev; 7262908d778SJames Bottomley 7272908d778SJames Bottomley spin_lock(&port->dev_list_lock); 7282908d778SJames Bottomley list_for_each_entry(dev, &port->dev_list, dev_list_node) { 7292908d778SJames Bottomley if (rphy == dev->rphy) { 7302908d778SJames Bottomley found_dev = dev; 7312908d778SJames Bottomley spin_unlock(&port->dev_list_lock); 7322908d778SJames Bottomley goto found; 7332908d778SJames Bottomley } 7342908d778SJames Bottomley } 7352908d778SJames Bottomley spin_unlock(&port->dev_list_lock); 7362908d778SJames Bottomley } 7372908d778SJames Bottomley found: 738980fa2f9SDarrick J. Wong spin_unlock_irqrestore(&ha->phy_port_lock, flags); 7392908d778SJames Bottomley 7402908d778SJames Bottomley return found_dev; 7412908d778SJames Bottomley } 7422908d778SJames Bottomley 7432908d778SJames Bottomley int sas_target_alloc(struct scsi_target *starget) 7442908d778SJames Bottomley { 745735f7d2fSDan Williams struct sas_rphy *rphy = dev_to_rphy(starget->dev.parent); 746735f7d2fSDan Williams struct domain_device *found_dev = sas_find_dev_by_rphy(rphy); 747338ec570SDarrick J. Wong int res; 7482908d778SJames Bottomley 7492908d778SJames Bottomley if (!found_dev) 7502908d778SJames Bottomley return -ENODEV; 7512908d778SJames Bottomley 752fa1c1e8fSDarrick J. Wong if (dev_is_sata(found_dev)) { 753338ec570SDarrick J. Wong res = sas_ata_init_host_and_port(found_dev, starget); 754338ec570SDarrick J. Wong if (res) 755338ec570SDarrick J. Wong return res; 756fa1c1e8fSDarrick J. Wong } 757fa1c1e8fSDarrick J. Wong 758735f7d2fSDan Williams kref_get(&found_dev->kref); 7592908d778SJames Bottomley starget->hostdata = found_dev; 7602908d778SJames Bottomley return 0; 7612908d778SJames Bottomley } 7622908d778SJames Bottomley 76397a1420dSDan Williams #define SAS_DEF_QD 256 7642908d778SJames Bottomley 7652908d778SJames Bottomley int sas_slave_configure(struct scsi_device *scsi_dev) 7662908d778SJames Bottomley { 7672908d778SJames Bottomley struct domain_device *dev = sdev_to_domain_dev(scsi_dev); 7682908d778SJames Bottomley struct sas_ha_struct *sas_ha; 7692908d778SJames Bottomley 7702908d778SJames Bottomley BUG_ON(dev->rphy->identify.device_type != SAS_END_DEVICE); 7712908d778SJames Bottomley 772fa1c1e8fSDarrick J. Wong if (dev_is_sata(dev)) { 773fa1c1e8fSDarrick J. Wong ata_sas_slave_configure(scsi_dev, dev->sata_dev.ap); 774fa1c1e8fSDarrick J. Wong return 0; 775fa1c1e8fSDarrick J. Wong } 776fa1c1e8fSDarrick J. Wong 7772908d778SJames Bottomley sas_ha = dev->port->ha; 7782908d778SJames Bottomley 7792908d778SJames Bottomley sas_read_port_mode_page(scsi_dev); 7802908d778SJames Bottomley 7812908d778SJames Bottomley if (scsi_dev->tagged_supported) { 7822908d778SJames Bottomley scsi_set_tag_type(scsi_dev, MSG_SIMPLE_TAG); 7832908d778SJames Bottomley scsi_activate_tcq(scsi_dev, SAS_DEF_QD); 7842908d778SJames Bottomley } else { 7852908d778SJames Bottomley SAS_DPRINTK("device %llx, LUN %x doesn't support " 7862908d778SJames Bottomley "TCQ\n", SAS_ADDR(dev->sas_addr), 7872908d778SJames Bottomley scsi_dev->lun); 7882908d778SJames Bottomley scsi_dev->tagged_supported = 0; 7892908d778SJames Bottomley scsi_set_tag_type(scsi_dev, 0); 7902908d778SJames Bottomley scsi_deactivate_tcq(scsi_dev, 1); 7912908d778SJames Bottomley } 7922908d778SJames Bottomley 793f27708fcSDarrick J. Wong scsi_dev->allow_restart = 1; 794f27708fcSDarrick J. Wong 7952908d778SJames Bottomley return 0; 7962908d778SJames Bottomley } 7972908d778SJames Bottomley 79897a1420dSDan Williams int sas_change_queue_depth(struct scsi_device *sdev, int depth, int reason) 7992908d778SJames Bottomley { 80097a1420dSDan Williams struct domain_device *dev = sdev_to_domain_dev(sdev); 8012908d778SJames Bottomley 802f6e67035SDan Williams if (dev_is_sata(dev)) 80397a1420dSDan Williams return __ata_change_queue_depth(dev->sata_dev.ap, sdev, depth, 80497a1420dSDan Williams reason); 805f6e67035SDan Williams 80697a1420dSDan Williams switch (reason) { 80797a1420dSDan Williams case SCSI_QDEPTH_DEFAULT: 80897a1420dSDan Williams case SCSI_QDEPTH_RAMP_UP: 80997a1420dSDan Williams if (!sdev->tagged_supported) 81097a1420dSDan Williams depth = 1; 81197a1420dSDan Williams scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth); 81297a1420dSDan Williams break; 81397a1420dSDan Williams case SCSI_QDEPTH_QFULL: 81497a1420dSDan Williams scsi_track_queue_full(sdev, depth); 81597a1420dSDan Williams break; 81697a1420dSDan Williams default: 817e881a172SMike Christie return -EOPNOTSUPP; 8182908d778SJames Bottomley } 8192908d778SJames Bottomley 82097a1420dSDan Williams return depth; 8212908d778SJames Bottomley } 8222908d778SJames Bottomley 8232908d778SJames Bottomley int sas_change_queue_type(struct scsi_device *scsi_dev, int qt) 8242908d778SJames Bottomley { 825f6e67035SDan Williams struct domain_device *dev = sdev_to_domain_dev(scsi_dev); 826f6e67035SDan Williams 827f6e67035SDan Williams if (dev_is_sata(dev)) 828f6e67035SDan Williams return -EINVAL; 829f6e67035SDan Williams 8302908d778SJames Bottomley if (!scsi_dev->tagged_supported) 8312908d778SJames Bottomley return 0; 8322908d778SJames Bottomley 8332908d778SJames Bottomley scsi_deactivate_tcq(scsi_dev, 1); 8342908d778SJames Bottomley 8352908d778SJames Bottomley scsi_set_tag_type(scsi_dev, qt); 8362908d778SJames Bottomley scsi_activate_tcq(scsi_dev, scsi_dev->queue_depth); 8372908d778SJames Bottomley 8382908d778SJames Bottomley return qt; 8392908d778SJames Bottomley } 8402908d778SJames Bottomley 8412908d778SJames Bottomley int sas_bios_param(struct scsi_device *scsi_dev, 8422908d778SJames Bottomley struct block_device *bdev, 8432908d778SJames Bottomley sector_t capacity, int *hsc) 8442908d778SJames Bottomley { 8452908d778SJames Bottomley hsc[0] = 255; 8462908d778SJames Bottomley hsc[1] = 63; 8472908d778SJames Bottomley sector_div(capacity, 255*63); 8482908d778SJames Bottomley hsc[2] = capacity; 8492908d778SJames Bottomley 8502908d778SJames Bottomley return 0; 8512908d778SJames Bottomley } 8522908d778SJames Bottomley 8532908d778SJames Bottomley /* ---------- Task Collector Thread implementation ---------- */ 8542908d778SJames Bottomley 8552908d778SJames Bottomley static void sas_queue(struct sas_ha_struct *sas_ha) 8562908d778SJames Bottomley { 8572908d778SJames Bottomley struct scsi_core *core = &sas_ha->core; 8582908d778SJames Bottomley unsigned long flags; 8592908d778SJames Bottomley LIST_HEAD(q); 8602908d778SJames Bottomley int can_queue; 8612908d778SJames Bottomley int res; 8622908d778SJames Bottomley struct sas_internal *i = to_sas_internal(core->shost->transportt); 8632908d778SJames Bottomley 8642908d778SJames Bottomley spin_lock_irqsave(&core->task_queue_lock, flags); 865d7a54e30SChristoph Hellwig while (!kthread_should_stop() && 8662908d778SJames Bottomley !list_empty(&core->task_queue)) { 8672908d778SJames Bottomley 8682908d778SJames Bottomley can_queue = sas_ha->lldd_queue_size - core->task_queue_size; 8692908d778SJames Bottomley if (can_queue >= 0) { 8702908d778SJames Bottomley can_queue = core->task_queue_size; 8712908d778SJames Bottomley list_splice_init(&core->task_queue, &q); 8722908d778SJames Bottomley } else { 8732908d778SJames Bottomley struct list_head *a, *n; 8742908d778SJames Bottomley 8752908d778SJames Bottomley can_queue = sas_ha->lldd_queue_size; 8762908d778SJames Bottomley list_for_each_safe(a, n, &core->task_queue) { 8772908d778SJames Bottomley list_move_tail(a, &q); 8782908d778SJames Bottomley if (--can_queue == 0) 8792908d778SJames Bottomley break; 8802908d778SJames Bottomley } 8812908d778SJames Bottomley can_queue = sas_ha->lldd_queue_size; 8822908d778SJames Bottomley } 8832908d778SJames Bottomley core->task_queue_size -= can_queue; 8842908d778SJames Bottomley spin_unlock_irqrestore(&core->task_queue_lock, flags); 8852908d778SJames Bottomley { 8862908d778SJames Bottomley struct sas_task *task = list_entry(q.next, 8872908d778SJames Bottomley struct sas_task, 8882908d778SJames Bottomley list); 8892908d778SJames Bottomley list_del_init(&q); 8902908d778SJames Bottomley res = i->dft->lldd_execute_task(task, can_queue, 8912908d778SJames Bottomley GFP_KERNEL); 8922908d778SJames Bottomley if (unlikely(res)) 8932908d778SJames Bottomley __list_add(&q, task->list.prev, &task->list); 8942908d778SJames Bottomley } 8952908d778SJames Bottomley spin_lock_irqsave(&core->task_queue_lock, flags); 8962908d778SJames Bottomley if (res) { 8972908d778SJames Bottomley list_splice_init(&q, &core->task_queue); /*at head*/ 8982908d778SJames Bottomley core->task_queue_size += can_queue; 8992908d778SJames Bottomley } 9002908d778SJames Bottomley } 9012908d778SJames Bottomley spin_unlock_irqrestore(&core->task_queue_lock, flags); 9022908d778SJames Bottomley } 9032908d778SJames Bottomley 9042908d778SJames Bottomley /** 9052908d778SJames Bottomley * sas_queue_thread -- The Task Collector thread 9062908d778SJames Bottomley * @_sas_ha: pointer to struct sas_ha 9072908d778SJames Bottomley */ 9082908d778SJames Bottomley static int sas_queue_thread(void *_sas_ha) 9092908d778SJames Bottomley { 9102908d778SJames Bottomley struct sas_ha_struct *sas_ha = _sas_ha; 9112908d778SJames Bottomley 9122908d778SJames Bottomley while (1) { 913d7a54e30SChristoph Hellwig set_current_state(TASK_INTERRUPTIBLE); 914d7a54e30SChristoph Hellwig schedule(); 9152908d778SJames Bottomley sas_queue(sas_ha); 916d7a54e30SChristoph Hellwig if (kthread_should_stop()) 9172908d778SJames Bottomley break; 9182908d778SJames Bottomley } 9192908d778SJames Bottomley 9202908d778SJames Bottomley return 0; 9212908d778SJames Bottomley } 9222908d778SJames Bottomley 9232908d778SJames Bottomley int sas_init_queue(struct sas_ha_struct *sas_ha) 9242908d778SJames Bottomley { 9252908d778SJames Bottomley struct scsi_core *core = &sas_ha->core; 9262908d778SJames Bottomley 9272908d778SJames Bottomley spin_lock_init(&core->task_queue_lock); 9282908d778SJames Bottomley core->task_queue_size = 0; 9292908d778SJames Bottomley INIT_LIST_HEAD(&core->task_queue); 9302908d778SJames Bottomley 931d7a54e30SChristoph Hellwig core->queue_thread = kthread_run(sas_queue_thread, sas_ha, 932d7a54e30SChristoph Hellwig "sas_queue_%d", core->shost->host_no); 933d7a54e30SChristoph Hellwig if (IS_ERR(core->queue_thread)) 934d7a54e30SChristoph Hellwig return PTR_ERR(core->queue_thread); 935d7a54e30SChristoph Hellwig return 0; 9362908d778SJames Bottomley } 9372908d778SJames Bottomley 9382908d778SJames Bottomley void sas_shutdown_queue(struct sas_ha_struct *sas_ha) 9392908d778SJames Bottomley { 9402908d778SJames Bottomley unsigned long flags; 9412908d778SJames Bottomley struct scsi_core *core = &sas_ha->core; 9422908d778SJames Bottomley struct sas_task *task, *n; 9432908d778SJames Bottomley 944d7a54e30SChristoph Hellwig kthread_stop(core->queue_thread); 9452908d778SJames Bottomley 9462908d778SJames Bottomley if (!list_empty(&core->task_queue)) 9472908d778SJames Bottomley SAS_DPRINTK("HA: %llx: scsi core task queue is NOT empty!?\n", 9482908d778SJames Bottomley SAS_ADDR(sas_ha->sas_addr)); 9492908d778SJames Bottomley 9502908d778SJames Bottomley spin_lock_irqsave(&core->task_queue_lock, flags); 9512908d778SJames Bottomley list_for_each_entry_safe(task, n, &core->task_queue, list) { 9522908d778SJames Bottomley struct scsi_cmnd *cmd = task->uldd_task; 9532908d778SJames Bottomley 9542908d778SJames Bottomley list_del_init(&task->list); 9552908d778SJames Bottomley 9562908d778SJames Bottomley ASSIGN_SAS_TASK(cmd, NULL); 9572908d778SJames Bottomley sas_free_task(task); 9582908d778SJames Bottomley cmd->result = DID_ABORT << 16; 9592908d778SJames Bottomley cmd->scsi_done(cmd); 9602908d778SJames Bottomley } 9612908d778SJames Bottomley spin_unlock_irqrestore(&core->task_queue_lock, flags); 9622908d778SJames Bottomley } 9632908d778SJames Bottomley 964396819fbSDarrick J. Wong /* 965396819fbSDarrick J. Wong * Tell an upper layer that it needs to initiate an abort for a given task. 966396819fbSDarrick J. Wong * This should only ever be called by an LLDD. 967396819fbSDarrick J. Wong */ 968396819fbSDarrick J. Wong void sas_task_abort(struct sas_task *task) 96979a5eb60SDarrick J. Wong { 970396819fbSDarrick J. Wong struct scsi_cmnd *sc = task->uldd_task; 97179a5eb60SDarrick J. Wong 972396819fbSDarrick J. Wong /* Escape for libsas internal commands */ 973396819fbSDarrick J. Wong if (!sc) { 974396819fbSDarrick J. Wong if (!del_timer(&task->timer)) 97579a5eb60SDarrick J. Wong return; 976396819fbSDarrick J. Wong task->timer.function(task->timer.data); 977396819fbSDarrick J. Wong return; 978396819fbSDarrick J. Wong } 97979a5eb60SDarrick J. Wong 9803a2755afSDarrick J. Wong if (dev_is_sata(task->dev)) { 9813a2755afSDarrick J. Wong sas_ata_task_abort(task); 9821b4d0d8eSJames Bottomley } else { 9831b4d0d8eSJames Bottomley struct request_queue *q = sc->device->request_queue; 9841b4d0d8eSJames Bottomley unsigned long flags; 9853a2755afSDarrick J. Wong 98670b25f89STejun Heo spin_lock_irqsave(q->queue_lock, flags); 987242f9dcbSJens Axboe blk_abort_request(sc->request); 98870b25f89STejun Heo spin_unlock_irqrestore(q->queue_lock, flags); 989396819fbSDarrick J. Wong scsi_schedule_eh(sc->device->host); 99079a5eb60SDarrick J. Wong } 9911b4d0d8eSJames Bottomley } 99279a5eb60SDarrick J. Wong 993fa1c1e8fSDarrick J. Wong int sas_slave_alloc(struct scsi_device *scsi_dev) 994fa1c1e8fSDarrick J. Wong { 995fa1c1e8fSDarrick J. Wong struct domain_device *dev = sdev_to_domain_dev(scsi_dev); 996fa1c1e8fSDarrick J. Wong 997fa1c1e8fSDarrick J. Wong if (dev_is_sata(dev)) 998fa1c1e8fSDarrick J. Wong return ata_sas_port_init(dev->sata_dev.ap); 999fa1c1e8fSDarrick J. Wong 1000fa1c1e8fSDarrick J. Wong return 0; 1001fa1c1e8fSDarrick J. Wong } 1002fa1c1e8fSDarrick J. Wong 1003fa1c1e8fSDarrick J. Wong void sas_target_destroy(struct scsi_target *starget) 1004fa1c1e8fSDarrick J. Wong { 1005735f7d2fSDan Williams struct domain_device *found_dev = starget->hostdata; 1006fa1c1e8fSDarrick J. Wong 1007fa1c1e8fSDarrick J. Wong if (!found_dev) 1008fa1c1e8fSDarrick J. Wong return; 1009fa1c1e8fSDarrick J. Wong 1010fa1c1e8fSDarrick J. Wong if (dev_is_sata(found_dev)) 1011fa1c1e8fSDarrick J. Wong ata_sas_port_destroy(found_dev->sata_dev.ap); 1012fa1c1e8fSDarrick J. Wong 1013735f7d2fSDan Williams starget->hostdata = NULL; 1014735f7d2fSDan Williams sas_put_device(found_dev); 1015fa1c1e8fSDarrick J. Wong } 1016fa1c1e8fSDarrick J. Wong 101745e6cdf4SDarrick J. Wong static void sas_parse_addr(u8 *sas_addr, const char *p) 101845e6cdf4SDarrick J. Wong { 101945e6cdf4SDarrick J. Wong int i; 102045e6cdf4SDarrick J. Wong for (i = 0; i < SAS_ADDR_SIZE; i++) { 102145e6cdf4SDarrick J. Wong u8 h, l; 102245e6cdf4SDarrick J. Wong if (!*p) 102345e6cdf4SDarrick J. Wong break; 102445e6cdf4SDarrick J. Wong h = isdigit(*p) ? *p-'0' : toupper(*p)-'A'+10; 102545e6cdf4SDarrick J. Wong p++; 102645e6cdf4SDarrick J. Wong l = isdigit(*p) ? *p-'0' : toupper(*p)-'A'+10; 102745e6cdf4SDarrick J. Wong p++; 102845e6cdf4SDarrick J. Wong sas_addr[i] = (h<<4) | l; 102945e6cdf4SDarrick J. Wong } 103045e6cdf4SDarrick J. Wong } 103145e6cdf4SDarrick J. Wong 103245e6cdf4SDarrick J. Wong #define SAS_STRING_ADDR_SIZE 16 103345e6cdf4SDarrick J. Wong 103445e6cdf4SDarrick J. Wong int sas_request_addr(struct Scsi_Host *shost, u8 *addr) 103545e6cdf4SDarrick J. Wong { 103645e6cdf4SDarrick J. Wong int res; 103745e6cdf4SDarrick J. Wong const struct firmware *fw; 103845e6cdf4SDarrick J. Wong 103945e6cdf4SDarrick J. Wong res = request_firmware(&fw, "sas_addr", &shost->shost_gendev); 104045e6cdf4SDarrick J. Wong if (res) 104145e6cdf4SDarrick J. Wong return res; 104245e6cdf4SDarrick J. Wong 104345e6cdf4SDarrick J. Wong if (fw->size < SAS_STRING_ADDR_SIZE) { 104445e6cdf4SDarrick J. Wong res = -ENODEV; 104545e6cdf4SDarrick J. Wong goto out; 104645e6cdf4SDarrick J. Wong } 104745e6cdf4SDarrick J. Wong 104845e6cdf4SDarrick J. Wong sas_parse_addr(addr, fw->data); 104945e6cdf4SDarrick J. Wong 105045e6cdf4SDarrick J. Wong out: 105145e6cdf4SDarrick J. Wong release_firmware(fw); 105245e6cdf4SDarrick J. Wong return res; 105345e6cdf4SDarrick J. Wong } 105445e6cdf4SDarrick J. Wong EXPORT_SYMBOL_GPL(sas_request_addr); 105545e6cdf4SDarrick J. Wong 10562908d778SJames Bottomley EXPORT_SYMBOL_GPL(sas_queuecommand); 10572908d778SJames Bottomley EXPORT_SYMBOL_GPL(sas_target_alloc); 10582908d778SJames Bottomley EXPORT_SYMBOL_GPL(sas_slave_configure); 10592908d778SJames Bottomley EXPORT_SYMBOL_GPL(sas_change_queue_depth); 10602908d778SJames Bottomley EXPORT_SYMBOL_GPL(sas_change_queue_type); 10612908d778SJames Bottomley EXPORT_SYMBOL_GPL(sas_bios_param); 106279a5eb60SDarrick J. Wong EXPORT_SYMBOL_GPL(sas_task_abort); 1063dea22214SDarrick J. Wong EXPORT_SYMBOL_GPL(sas_phy_reset); 1064acbf167dSDarrick J. Wong EXPORT_SYMBOL_GPL(sas_phy_enable); 1065ad689233SDarrick J. Wong EXPORT_SYMBOL_GPL(sas_eh_device_reset_handler); 1066a9344e68SDarrick J. Wong EXPORT_SYMBOL_GPL(sas_eh_bus_reset_handler); 1067fa1c1e8fSDarrick J. Wong EXPORT_SYMBOL_GPL(sas_slave_alloc); 1068fa1c1e8fSDarrick J. Wong EXPORT_SYMBOL_GPL(sas_target_destroy); 1069fa1c1e8fSDarrick J. Wong EXPORT_SYMBOL_GPL(sas_ioctl); 1070