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 26*d7a54e30SChristoph Hellwig #include <linux/kthread.h> 27*d7a54e30SChristoph Hellwig 282908d778SJames Bottomley #include "sas_internal.h" 292908d778SJames Bottomley 302908d778SJames Bottomley #include <scsi/scsi_host.h> 312908d778SJames Bottomley #include <scsi/scsi_device.h> 322908d778SJames Bottomley #include <scsi/scsi_tcq.h> 332908d778SJames Bottomley #include <scsi/scsi.h> 34f456393eSDarrick J. Wong #include <scsi/scsi_eh.h> 352908d778SJames Bottomley #include <scsi/scsi_transport.h> 362908d778SJames Bottomley #include <scsi/scsi_transport_sas.h> 372908d778SJames Bottomley #include "../scsi_sas_internal.h" 3879a5eb60SDarrick J. Wong #include "../scsi_transport_api.h" 39ad689233SDarrick J. Wong #include "../scsi_priv.h" 402908d778SJames Bottomley 412908d778SJames Bottomley #include <linux/err.h> 422908d778SJames Bottomley #include <linux/blkdev.h> 432908d778SJames Bottomley #include <linux/scatterlist.h> 442908d778SJames Bottomley 452908d778SJames Bottomley /* ---------- SCSI Host glue ---------- */ 462908d778SJames Bottomley 472908d778SJames Bottomley #define TO_SAS_TASK(_scsi_cmd) ((void *)(_scsi_cmd)->host_scribble) 482908d778SJames Bottomley #define ASSIGN_SAS_TASK(_sc, _t) do { (_sc)->host_scribble = (void *) _t; } while (0) 492908d778SJames Bottomley 502908d778SJames Bottomley static void sas_scsi_task_done(struct sas_task *task) 512908d778SJames Bottomley { 522908d778SJames Bottomley struct task_status_struct *ts = &task->task_status; 532908d778SJames Bottomley struct scsi_cmnd *sc = task->uldd_task; 54f456393eSDarrick J. Wong struct sas_ha_struct *sas_ha = SHOST_TO_SAS_HA(sc->device->host); 552908d778SJames Bottomley unsigned ts_flags = task->task_state_flags; 562908d778SJames Bottomley int hs = 0, stat = 0; 572908d778SJames Bottomley 582908d778SJames Bottomley if (unlikely(!sc)) { 592908d778SJames Bottomley SAS_DPRINTK("task_done called with non existing SCSI cmnd!\n"); 602908d778SJames Bottomley list_del_init(&task->list); 612908d778SJames Bottomley sas_free_task(task); 622908d778SJames Bottomley return; 632908d778SJames Bottomley } 642908d778SJames Bottomley 652908d778SJames Bottomley if (ts->resp == SAS_TASK_UNDELIVERED) { 662908d778SJames Bottomley /* transport error */ 672908d778SJames Bottomley hs = DID_NO_CONNECT; 682908d778SJames Bottomley } else { /* ts->resp == SAS_TASK_COMPLETE */ 692908d778SJames Bottomley /* task delivered, what happened afterwards? */ 702908d778SJames Bottomley switch (ts->stat) { 712908d778SJames Bottomley case SAS_DEV_NO_RESPONSE: 722908d778SJames Bottomley case SAS_INTERRUPTED: 732908d778SJames Bottomley case SAS_PHY_DOWN: 742908d778SJames Bottomley case SAS_NAK_R_ERR: 752908d778SJames Bottomley case SAS_OPEN_TO: 762908d778SJames Bottomley hs = DID_NO_CONNECT; 772908d778SJames Bottomley break; 782908d778SJames Bottomley case SAS_DATA_UNDERRUN: 792908d778SJames Bottomley sc->resid = ts->residual; 802908d778SJames Bottomley if (sc->request_bufflen - sc->resid < sc->underflow) 812908d778SJames Bottomley hs = DID_ERROR; 822908d778SJames Bottomley break; 832908d778SJames Bottomley case SAS_DATA_OVERRUN: 842908d778SJames Bottomley hs = DID_ERROR; 852908d778SJames Bottomley break; 862908d778SJames Bottomley case SAS_QUEUE_FULL: 872908d778SJames Bottomley hs = DID_SOFT_ERROR; /* retry */ 882908d778SJames Bottomley break; 892908d778SJames Bottomley case SAS_DEVICE_UNKNOWN: 902908d778SJames Bottomley hs = DID_BAD_TARGET; 912908d778SJames Bottomley break; 922908d778SJames Bottomley case SAS_SG_ERR: 932908d778SJames Bottomley hs = DID_PARITY; 942908d778SJames Bottomley break; 952908d778SJames Bottomley case SAS_OPEN_REJECT: 962908d778SJames Bottomley if (ts->open_rej_reason == SAS_OREJ_RSVD_RETRY) 972908d778SJames Bottomley hs = DID_SOFT_ERROR; /* retry */ 982908d778SJames Bottomley else 992908d778SJames Bottomley hs = DID_ERROR; 1002908d778SJames Bottomley break; 1012908d778SJames Bottomley case SAS_PROTO_RESPONSE: 1022908d778SJames Bottomley SAS_DPRINTK("LLDD:%s sent SAS_PROTO_RESP for an SSP " 1032908d778SJames Bottomley "task; please report this\n", 1042908d778SJames Bottomley task->dev->port->ha->sas_ha_name); 1052908d778SJames Bottomley break; 1062908d778SJames Bottomley case SAS_ABORTED_TASK: 1072908d778SJames Bottomley hs = DID_ABORT; 1082908d778SJames Bottomley break; 1092908d778SJames Bottomley case SAM_CHECK_COND: 1102908d778SJames Bottomley memcpy(sc->sense_buffer, ts->buf, 1112908d778SJames Bottomley max(SCSI_SENSE_BUFFERSIZE, ts->buf_valid_size)); 1122908d778SJames Bottomley stat = SAM_CHECK_COND; 1132908d778SJames Bottomley break; 1142908d778SJames Bottomley default: 1152908d778SJames Bottomley stat = ts->stat; 1162908d778SJames Bottomley break; 1172908d778SJames Bottomley } 1182908d778SJames Bottomley } 1192908d778SJames Bottomley ASSIGN_SAS_TASK(sc, NULL); 1202908d778SJames Bottomley sc->result = (hs << 16) | stat; 1212908d778SJames Bottomley list_del_init(&task->list); 1222908d778SJames Bottomley sas_free_task(task); 1232908d778SJames Bottomley /* This is very ugly but this is how SCSI Core works. */ 1242908d778SJames Bottomley if (ts_flags & SAS_TASK_STATE_ABORTED) 125f456393eSDarrick J. Wong scsi_eh_finish_cmd(sc, &sas_ha->eh_done_q); 1262908d778SJames Bottomley else 1272908d778SJames Bottomley sc->scsi_done(sc); 1282908d778SJames Bottomley } 1292908d778SJames Bottomley 1302908d778SJames Bottomley static enum task_attribute sas_scsi_get_task_attr(struct scsi_cmnd *cmd) 1312908d778SJames Bottomley { 1322908d778SJames Bottomley enum task_attribute ta = TASK_ATTR_SIMPLE; 1332908d778SJames Bottomley if (cmd->request && blk_rq_tagged(cmd->request)) { 1342908d778SJames Bottomley if (cmd->device->ordered_tags && 1351bdfd554SJeff Garzik (cmd->request->cmd_flags & REQ_HARDBARRIER)) 13663bb1bf0SFUJITA Tomonori ta = TASK_ATTR_ORDERED; 1372908d778SJames Bottomley } 1382908d778SJames Bottomley return ta; 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 *(u32 *)cmd->sense_buffer = 0; 1522908d778SJames Bottomley task->uldd_task = cmd; 1532908d778SJames Bottomley ASSIGN_SAS_TASK(cmd, task); 1542908d778SJames Bottomley 1552908d778SJames Bottomley task->dev = dev; 1562908d778SJames Bottomley task->task_proto = task->dev->tproto; /* BUG_ON(!SSP) */ 1572908d778SJames Bottomley 1582908d778SJames Bottomley task->ssp_task.retry_count = 1; 1592908d778SJames Bottomley int_to_scsilun(cmd->device->lun, &lun); 1602908d778SJames Bottomley memcpy(task->ssp_task.LUN, &lun.scsi_lun, 8); 1612908d778SJames Bottomley task->ssp_task.task_attr = sas_scsi_get_task_attr(cmd); 1622908d778SJames Bottomley memcpy(task->ssp_task.cdb, cmd->cmnd, 16); 1632908d778SJames Bottomley 1642908d778SJames Bottomley task->scatter = cmd->request_buffer; 1652908d778SJames Bottomley task->num_scatter = cmd->use_sg; 1662908d778SJames Bottomley task->total_xfer_len = cmd->request_bufflen; 1672908d778SJames Bottomley task->data_dir = cmd->sc_data_direction; 1682908d778SJames Bottomley 1692908d778SJames Bottomley task->task_done = sas_scsi_task_done; 1702908d778SJames Bottomley 1712908d778SJames Bottomley return task; 1722908d778SJames Bottomley } 1732908d778SJames Bottomley 1742908d778SJames Bottomley static int sas_queue_up(struct sas_task *task) 1752908d778SJames Bottomley { 1762908d778SJames Bottomley struct sas_ha_struct *sas_ha = task->dev->port->ha; 1772908d778SJames Bottomley struct scsi_core *core = &sas_ha->core; 1782908d778SJames Bottomley unsigned long flags; 1792908d778SJames Bottomley LIST_HEAD(list); 1802908d778SJames Bottomley 1812908d778SJames Bottomley spin_lock_irqsave(&core->task_queue_lock, flags); 1822908d778SJames Bottomley if (sas_ha->lldd_queue_size < core->task_queue_size + 1) { 1832908d778SJames Bottomley spin_unlock_irqrestore(&core->task_queue_lock, flags); 1842908d778SJames Bottomley return -SAS_QUEUE_FULL; 1852908d778SJames Bottomley } 1862908d778SJames Bottomley list_add_tail(&task->list, &core->task_queue); 1872908d778SJames Bottomley core->task_queue_size += 1; 1882908d778SJames Bottomley spin_unlock_irqrestore(&core->task_queue_lock, flags); 189*d7a54e30SChristoph Hellwig wake_up_process(core->queue_thread); 1902908d778SJames Bottomley 1912908d778SJames Bottomley return 0; 1922908d778SJames Bottomley } 1932908d778SJames Bottomley 1942908d778SJames Bottomley /** 1952908d778SJames Bottomley * sas_queuecommand -- Enqueue a command for processing 1962908d778SJames Bottomley * @parameters: See SCSI Core documentation 1972908d778SJames Bottomley * 1982908d778SJames Bottomley * Note: XXX: Remove the host unlock/lock pair when SCSI Core can 1992908d778SJames Bottomley * call us without holding an IRQ spinlock... 2002908d778SJames Bottomley */ 2012908d778SJames Bottomley int sas_queuecommand(struct scsi_cmnd *cmd, 2022908d778SJames Bottomley void (*scsi_done)(struct scsi_cmnd *)) 2032908d778SJames Bottomley { 2042908d778SJames Bottomley int res = 0; 2052908d778SJames Bottomley struct domain_device *dev = cmd_to_domain_dev(cmd); 2062908d778SJames Bottomley struct Scsi_Host *host = cmd->device->host; 2072908d778SJames Bottomley struct sas_internal *i = to_sas_internal(host->transportt); 2082908d778SJames Bottomley 2092908d778SJames Bottomley spin_unlock_irq(host->host_lock); 2102908d778SJames Bottomley 2112908d778SJames Bottomley { 2122908d778SJames Bottomley struct sas_ha_struct *sas_ha = dev->port->ha; 2132908d778SJames Bottomley struct sas_task *task; 2142908d778SJames Bottomley 2152908d778SJames Bottomley res = -ENOMEM; 2162908d778SJames Bottomley task = sas_create_task(cmd, dev, GFP_ATOMIC); 2172908d778SJames Bottomley if (!task) 2182908d778SJames Bottomley goto out; 2192908d778SJames Bottomley 2202908d778SJames Bottomley cmd->scsi_done = scsi_done; 2212908d778SJames Bottomley /* Queue up, Direct Mode or Task Collector Mode. */ 2222908d778SJames Bottomley if (sas_ha->lldd_max_execute_num < 2) 2232908d778SJames Bottomley res = i->dft->lldd_execute_task(task, 1, GFP_ATOMIC); 2242908d778SJames Bottomley else 2252908d778SJames Bottomley res = sas_queue_up(task); 2262908d778SJames Bottomley 2272908d778SJames Bottomley /* Examine */ 2282908d778SJames Bottomley if (res) { 2292908d778SJames Bottomley SAS_DPRINTK("lldd_execute_task returned: %d\n", res); 2302908d778SJames Bottomley ASSIGN_SAS_TASK(cmd, NULL); 2312908d778SJames Bottomley sas_free_task(task); 2322908d778SJames Bottomley if (res == -SAS_QUEUE_FULL) { 2332908d778SJames Bottomley cmd->result = DID_SOFT_ERROR << 16; /* retry */ 2342908d778SJames Bottomley res = 0; 2352908d778SJames Bottomley scsi_done(cmd); 2362908d778SJames Bottomley } 2372908d778SJames Bottomley goto out; 2382908d778SJames Bottomley } 2392908d778SJames Bottomley } 2402908d778SJames Bottomley out: 2412908d778SJames Bottomley spin_lock_irq(host->host_lock); 2422908d778SJames Bottomley return res; 2432908d778SJames Bottomley } 2442908d778SJames Bottomley 2452908d778SJames Bottomley static void sas_scsi_clear_queue_lu(struct list_head *error_q, struct scsi_cmnd *my_cmd) 2462908d778SJames Bottomley { 2472908d778SJames Bottomley struct scsi_cmnd *cmd, *n; 2482908d778SJames Bottomley 2492908d778SJames Bottomley list_for_each_entry_safe(cmd, n, error_q, eh_entry) { 2502908d778SJames Bottomley if (cmd == my_cmd) 2512908d778SJames Bottomley list_del_init(&cmd->eh_entry); 2522908d778SJames Bottomley } 2532908d778SJames Bottomley } 2542908d778SJames Bottomley 2552908d778SJames Bottomley static void sas_scsi_clear_queue_I_T(struct list_head *error_q, 2562908d778SJames Bottomley struct domain_device *dev) 2572908d778SJames Bottomley { 2582908d778SJames Bottomley struct scsi_cmnd *cmd, *n; 2592908d778SJames Bottomley 2602908d778SJames Bottomley list_for_each_entry_safe(cmd, n, error_q, eh_entry) { 2612908d778SJames Bottomley struct domain_device *x = cmd_to_domain_dev(cmd); 2622908d778SJames Bottomley 2632908d778SJames Bottomley if (x == dev) 2642908d778SJames Bottomley list_del_init(&cmd->eh_entry); 2652908d778SJames Bottomley } 2662908d778SJames Bottomley } 2672908d778SJames Bottomley 2682908d778SJames Bottomley static void sas_scsi_clear_queue_port(struct list_head *error_q, 2692908d778SJames Bottomley struct asd_sas_port *port) 2702908d778SJames Bottomley { 2712908d778SJames Bottomley struct scsi_cmnd *cmd, *n; 2722908d778SJames Bottomley 2732908d778SJames Bottomley list_for_each_entry_safe(cmd, n, error_q, eh_entry) { 2742908d778SJames Bottomley struct domain_device *dev = cmd_to_domain_dev(cmd); 2752908d778SJames Bottomley struct asd_sas_port *x = dev->port; 2762908d778SJames Bottomley 2772908d778SJames Bottomley if (x == port) 2782908d778SJames Bottomley list_del_init(&cmd->eh_entry); 2792908d778SJames Bottomley } 2802908d778SJames Bottomley } 2812908d778SJames Bottomley 2822908d778SJames Bottomley enum task_disposition { 2832908d778SJames Bottomley TASK_IS_DONE, 2842908d778SJames Bottomley TASK_IS_ABORTED, 2852908d778SJames Bottomley TASK_IS_AT_LU, 2862908d778SJames Bottomley TASK_IS_NOT_AT_LU, 28702cd743bSDarrick J. Wong TASK_ABORT_FAILED, 2882908d778SJames Bottomley }; 2892908d778SJames Bottomley 2902908d778SJames Bottomley static enum task_disposition sas_scsi_find_task(struct sas_task *task) 2912908d778SJames Bottomley { 2922908d778SJames Bottomley struct sas_ha_struct *ha = task->dev->port->ha; 2932908d778SJames Bottomley unsigned long flags; 2942908d778SJames Bottomley int i, res; 2952908d778SJames Bottomley struct sas_internal *si = 2962908d778SJames Bottomley to_sas_internal(task->dev->port->ha->core.shost->transportt); 2972908d778SJames Bottomley 2982908d778SJames Bottomley if (ha->lldd_max_execute_num > 1) { 2992908d778SJames Bottomley struct scsi_core *core = &ha->core; 3002908d778SJames Bottomley struct sas_task *t, *n; 3012908d778SJames Bottomley 3022908d778SJames Bottomley spin_lock_irqsave(&core->task_queue_lock, flags); 3032908d778SJames Bottomley list_for_each_entry_safe(t, n, &core->task_queue, list) { 3042908d778SJames Bottomley if (task == t) { 3052908d778SJames Bottomley list_del_init(&t->list); 3062908d778SJames Bottomley spin_unlock_irqrestore(&core->task_queue_lock, 3072908d778SJames Bottomley flags); 3082908d778SJames Bottomley SAS_DPRINTK("%s: task 0x%p aborted from " 3092908d778SJames Bottomley "task_queue\n", 3102908d778SJames Bottomley __FUNCTION__, task); 3112908d778SJames Bottomley return TASK_IS_ABORTED; 3122908d778SJames Bottomley } 3132908d778SJames Bottomley } 3142908d778SJames Bottomley spin_unlock_irqrestore(&core->task_queue_lock, flags); 3152908d778SJames Bottomley } 3162908d778SJames Bottomley 3172908d778SJames Bottomley for (i = 0; i < 5; i++) { 3182908d778SJames Bottomley SAS_DPRINTK("%s: aborting task 0x%p\n", __FUNCTION__, task); 3192908d778SJames Bottomley res = si->dft->lldd_abort_task(task); 3202908d778SJames Bottomley 3212908d778SJames Bottomley spin_lock_irqsave(&task->task_state_lock, flags); 3222908d778SJames Bottomley if (task->task_state_flags & SAS_TASK_STATE_DONE) { 3232908d778SJames Bottomley spin_unlock_irqrestore(&task->task_state_lock, flags); 3242908d778SJames Bottomley SAS_DPRINTK("%s: task 0x%p is done\n", __FUNCTION__, 3252908d778SJames Bottomley task); 3262908d778SJames Bottomley return TASK_IS_DONE; 3272908d778SJames Bottomley } 3282908d778SJames Bottomley spin_unlock_irqrestore(&task->task_state_lock, flags); 3292908d778SJames Bottomley 3302908d778SJames Bottomley if (res == TMF_RESP_FUNC_COMPLETE) { 3312908d778SJames Bottomley SAS_DPRINTK("%s: task 0x%p is aborted\n", 3322908d778SJames Bottomley __FUNCTION__, task); 3332908d778SJames Bottomley return TASK_IS_ABORTED; 3342908d778SJames Bottomley } else if (si->dft->lldd_query_task) { 3352908d778SJames Bottomley SAS_DPRINTK("%s: querying task 0x%p\n", 3362908d778SJames Bottomley __FUNCTION__, task); 3372908d778SJames Bottomley res = si->dft->lldd_query_task(task); 33802cd743bSDarrick J. Wong switch (res) { 33902cd743bSDarrick J. Wong case TMF_RESP_FUNC_SUCC: 3402908d778SJames Bottomley SAS_DPRINTK("%s: task 0x%p at LU\n", 3412908d778SJames Bottomley __FUNCTION__, task); 3422908d778SJames Bottomley return TASK_IS_AT_LU; 34302cd743bSDarrick J. Wong case TMF_RESP_FUNC_COMPLETE: 3442908d778SJames Bottomley SAS_DPRINTK("%s: task 0x%p not at LU\n", 3452908d778SJames Bottomley __FUNCTION__, task); 3462908d778SJames Bottomley return TASK_IS_NOT_AT_LU; 34702cd743bSDarrick J. Wong case TMF_RESP_FUNC_FAILED: 34802cd743bSDarrick J. Wong SAS_DPRINTK("%s: task 0x%p failed to abort\n", 34902cd743bSDarrick J. Wong __FUNCTION__, task); 35002cd743bSDarrick J. Wong return TASK_ABORT_FAILED; 3512908d778SJames Bottomley } 35202cd743bSDarrick J. Wong 3532908d778SJames Bottomley } 3542908d778SJames Bottomley } 3552908d778SJames Bottomley return res; 3562908d778SJames Bottomley } 3572908d778SJames Bottomley 3582908d778SJames Bottomley static int sas_recover_lu(struct domain_device *dev, struct scsi_cmnd *cmd) 3592908d778SJames Bottomley { 3602908d778SJames Bottomley int res = TMF_RESP_FUNC_FAILED; 3612908d778SJames Bottomley struct scsi_lun lun; 3622908d778SJames Bottomley struct sas_internal *i = 3632908d778SJames Bottomley to_sas_internal(dev->port->ha->core.shost->transportt); 3642908d778SJames Bottomley 3652908d778SJames Bottomley int_to_scsilun(cmd->device->lun, &lun); 3662908d778SJames Bottomley 3672908d778SJames Bottomley SAS_DPRINTK("eh: device %llx LUN %x has the task\n", 3682908d778SJames Bottomley SAS_ADDR(dev->sas_addr), 3692908d778SJames Bottomley cmd->device->lun); 3702908d778SJames Bottomley 3712908d778SJames Bottomley if (i->dft->lldd_abort_task_set) 3722908d778SJames Bottomley res = i->dft->lldd_abort_task_set(dev, lun.scsi_lun); 3732908d778SJames Bottomley 3742908d778SJames Bottomley if (res == TMF_RESP_FUNC_FAILED) { 3752908d778SJames Bottomley if (i->dft->lldd_clear_task_set) 3762908d778SJames Bottomley res = i->dft->lldd_clear_task_set(dev, lun.scsi_lun); 3772908d778SJames Bottomley } 3782908d778SJames Bottomley 3792908d778SJames Bottomley if (res == TMF_RESP_FUNC_FAILED) { 3802908d778SJames Bottomley if (i->dft->lldd_lu_reset) 3812908d778SJames Bottomley res = i->dft->lldd_lu_reset(dev, lun.scsi_lun); 3822908d778SJames Bottomley } 3832908d778SJames Bottomley 3842908d778SJames Bottomley return res; 3852908d778SJames Bottomley } 3862908d778SJames Bottomley 3872908d778SJames Bottomley static int sas_recover_I_T(struct domain_device *dev) 3882908d778SJames Bottomley { 3892908d778SJames Bottomley int res = TMF_RESP_FUNC_FAILED; 3902908d778SJames Bottomley struct sas_internal *i = 3912908d778SJames Bottomley to_sas_internal(dev->port->ha->core.shost->transportt); 3922908d778SJames Bottomley 3932908d778SJames Bottomley SAS_DPRINTK("I_T nexus reset for dev %016llx\n", 3942908d778SJames Bottomley SAS_ADDR(dev->sas_addr)); 3952908d778SJames Bottomley 3962908d778SJames Bottomley if (i->dft->lldd_I_T_nexus_reset) 3972908d778SJames Bottomley res = i->dft->lldd_I_T_nexus_reset(dev); 3982908d778SJames Bottomley 3992908d778SJames Bottomley return res; 4002908d778SJames Bottomley } 4012908d778SJames Bottomley 402ad689233SDarrick J. Wong /* Find the sas_phy that's attached to this device */ 403ad689233SDarrick J. Wong struct sas_phy *find_local_sas_phy(struct domain_device *dev) 4043ebf6922SDarrick J. Wong { 405ad689233SDarrick J. Wong struct domain_device *pdev = dev->parent; 406ad689233SDarrick J. Wong struct ex_phy *exphy = NULL; 407ad689233SDarrick J. Wong int i; 4083ebf6922SDarrick J. Wong 409ad689233SDarrick J. Wong /* Directly attached device */ 410ad689233SDarrick J. Wong if (!pdev) 411ad689233SDarrick J. Wong return dev->port->phy; 4123ebf6922SDarrick J. Wong 413ad689233SDarrick J. Wong /* Otherwise look in the expander */ 414ad689233SDarrick J. Wong for (i = 0; i < pdev->ex_dev.num_phys; i++) 415ad689233SDarrick J. Wong if (!memcmp(dev->sas_addr, 416ad689233SDarrick J. Wong pdev->ex_dev.ex_phy[i].attached_sas_addr, 417ad689233SDarrick J. Wong SAS_ADDR_SIZE)) { 418ad689233SDarrick J. Wong exphy = &pdev->ex_dev.ex_phy[i]; 419ad689233SDarrick J. Wong break; 4203ebf6922SDarrick J. Wong } 4213ebf6922SDarrick J. Wong 422ad689233SDarrick J. Wong BUG_ON(!exphy); 423ad689233SDarrick J. Wong return exphy->phy; 424ad689233SDarrick J. Wong } 425ad689233SDarrick J. Wong 426a9344e68SDarrick J. Wong /* Attempt to send a LUN reset message to a device */ 427ad689233SDarrick J. Wong int sas_eh_device_reset_handler(struct scsi_cmnd *cmd) 4282908d778SJames Bottomley { 429ad689233SDarrick J. Wong struct domain_device *dev = cmd_to_domain_dev(cmd); 430a9344e68SDarrick J. Wong struct sas_internal *i = 431a9344e68SDarrick J. Wong to_sas_internal(dev->port->ha->core.shost->transportt); 432a9344e68SDarrick J. Wong struct scsi_lun lun; 433a9344e68SDarrick J. Wong int res; 434a9344e68SDarrick J. Wong 435a9344e68SDarrick J. Wong int_to_scsilun(cmd->device->lun, &lun); 436a9344e68SDarrick J. Wong 437a9344e68SDarrick J. Wong if (!i->dft->lldd_lu_reset) 438a9344e68SDarrick J. Wong return FAILED; 439a9344e68SDarrick J. Wong 440a9344e68SDarrick J. Wong res = i->dft->lldd_lu_reset(dev, lun.scsi_lun); 441a9344e68SDarrick J. Wong if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE) 442a9344e68SDarrick J. Wong return SUCCESS; 443a9344e68SDarrick J. Wong 444a9344e68SDarrick J. Wong return FAILED; 445a9344e68SDarrick J. Wong } 446a9344e68SDarrick J. Wong 447a9344e68SDarrick J. Wong /* Attempt to send a phy (bus) reset */ 448a9344e68SDarrick J. Wong int sas_eh_bus_reset_handler(struct scsi_cmnd *cmd) 449a9344e68SDarrick J. Wong { 450a9344e68SDarrick J. Wong struct domain_device *dev = cmd_to_domain_dev(cmd); 451ad689233SDarrick J. Wong struct sas_phy *phy = find_local_sas_phy(dev); 452ad689233SDarrick J. Wong int res; 453ad689233SDarrick J. Wong 454ad689233SDarrick J. Wong res = sas_phy_reset(phy, 1); 455ad689233SDarrick J. Wong if (res) 456a9344e68SDarrick J. Wong SAS_DPRINTK("Bus reset of %s failed 0x%x\n", 457ad689233SDarrick J. Wong phy->dev.kobj.k_name, 458ad689233SDarrick J. Wong res); 459ad689233SDarrick J. Wong if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE) 460ad689233SDarrick J. Wong return SUCCESS; 461ad689233SDarrick J. Wong 462ad689233SDarrick J. Wong return FAILED; 463ad689233SDarrick J. Wong } 464ad689233SDarrick J. Wong 465ad689233SDarrick J. Wong /* Try to reset a device */ 466ad689233SDarrick J. Wong static int try_to_reset_cmd_device(struct Scsi_Host *shost, 467ad689233SDarrick J. Wong struct scsi_cmnd *cmd) 468ad689233SDarrick J. Wong { 469a9344e68SDarrick J. Wong int res; 470ad689233SDarrick J. Wong 471a9344e68SDarrick J. Wong if (!shost->hostt->eh_device_reset_handler) 472a9344e68SDarrick J. Wong goto try_bus_reset; 473a9344e68SDarrick J. Wong 474a9344e68SDarrick J. Wong res = shost->hostt->eh_device_reset_handler(cmd); 475a9344e68SDarrick J. Wong if (res == SUCCESS) 476a9344e68SDarrick J. Wong return res; 477a9344e68SDarrick J. Wong 478a9344e68SDarrick J. Wong try_bus_reset: 479a9344e68SDarrick J. Wong if (shost->hostt->eh_bus_reset_handler) 480a9344e68SDarrick J. Wong return shost->hostt->eh_bus_reset_handler(cmd); 481a9344e68SDarrick J. Wong 482a9344e68SDarrick J. Wong return FAILED; 483ad689233SDarrick J. Wong } 484ad689233SDarrick J. Wong 485ad689233SDarrick J. Wong static int sas_eh_handle_sas_errors(struct Scsi_Host *shost, 486ad689233SDarrick J. Wong struct list_head *work_q, 487ad689233SDarrick J. Wong struct list_head *done_q) 488ad689233SDarrick J. Wong { 4892908d778SJames Bottomley struct scsi_cmnd *cmd, *n; 4902908d778SJames Bottomley enum task_disposition res = TASK_IS_DONE; 4913ebf6922SDarrick J. Wong int tmf_resp, need_reset; 4922908d778SJames Bottomley struct sas_internal *i = to_sas_internal(shost->transportt); 493ad689233SDarrick J. Wong unsigned long flags; 494ad689233SDarrick J. Wong struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost); 4952908d778SJames Bottomley 4962908d778SJames Bottomley Again: 497ad689233SDarrick J. Wong list_for_each_entry_safe(cmd, n, work_q, eh_entry) { 4982908d778SJames Bottomley struct sas_task *task = TO_SAS_TASK(cmd); 499f456393eSDarrick J. Wong 500ad689233SDarrick J. Wong if (!task) 501f456393eSDarrick J. Wong continue; 502ad689233SDarrick J. Wong 503ad689233SDarrick J. Wong list_del_init(&cmd->eh_entry); 5043ebf6922SDarrick J. Wong 5053ebf6922SDarrick J. Wong spin_lock_irqsave(&task->task_state_lock, flags); 5063ebf6922SDarrick J. Wong need_reset = task->task_state_flags & SAS_TASK_NEED_DEV_RESET; 5073ebf6922SDarrick J. Wong spin_unlock_irqrestore(&task->task_state_lock, flags); 5083ebf6922SDarrick J. Wong 509f456393eSDarrick J. Wong SAS_DPRINTK("trying to find task 0x%p\n", task); 5102908d778SJames Bottomley res = sas_scsi_find_task(task); 5112908d778SJames Bottomley 5122908d778SJames Bottomley cmd->eh_eflags = 0; 5132908d778SJames Bottomley 5142908d778SJames Bottomley switch (res) { 5152908d778SJames Bottomley case TASK_IS_DONE: 5162908d778SJames Bottomley SAS_DPRINTK("%s: task 0x%p is done\n", __FUNCTION__, 5172908d778SJames Bottomley task); 5182908d778SJames Bottomley task->task_done(task); 5193ebf6922SDarrick J. Wong if (need_reset) 520ad689233SDarrick J. Wong try_to_reset_cmd_device(shost, cmd); 5212908d778SJames Bottomley continue; 5222908d778SJames Bottomley case TASK_IS_ABORTED: 5232908d778SJames Bottomley SAS_DPRINTK("%s: task 0x%p is aborted\n", 5242908d778SJames Bottomley __FUNCTION__, task); 5252908d778SJames Bottomley task->task_done(task); 5263ebf6922SDarrick J. Wong if (need_reset) 527ad689233SDarrick J. Wong try_to_reset_cmd_device(shost, cmd); 5282908d778SJames Bottomley continue; 5292908d778SJames Bottomley case TASK_IS_AT_LU: 5302908d778SJames Bottomley SAS_DPRINTK("task 0x%p is at LU: lu recover\n", task); 5312908d778SJames Bottomley tmf_resp = sas_recover_lu(task->dev, cmd); 5322908d778SJames Bottomley if (tmf_resp == TMF_RESP_FUNC_COMPLETE) { 5332908d778SJames Bottomley SAS_DPRINTK("dev %016llx LU %x is " 5342908d778SJames Bottomley "recovered\n", 5352908d778SJames Bottomley SAS_ADDR(task->dev), 5362908d778SJames Bottomley cmd->device->lun); 5372908d778SJames Bottomley task->task_done(task); 5383ebf6922SDarrick J. Wong if (need_reset) 539ad689233SDarrick J. Wong try_to_reset_cmd_device(shost, cmd); 540ad689233SDarrick J. Wong sas_scsi_clear_queue_lu(work_q, cmd); 5412908d778SJames Bottomley goto Again; 5422908d778SJames Bottomley } 5432908d778SJames Bottomley /* fallthrough */ 5442908d778SJames Bottomley case TASK_IS_NOT_AT_LU: 54502cd743bSDarrick J. Wong case TASK_ABORT_FAILED: 5462908d778SJames Bottomley SAS_DPRINTK("task 0x%p is not at LU: I_T recover\n", 5472908d778SJames Bottomley task); 5482908d778SJames Bottomley tmf_resp = sas_recover_I_T(task->dev); 5492908d778SJames Bottomley if (tmf_resp == TMF_RESP_FUNC_COMPLETE) { 5502908d778SJames Bottomley SAS_DPRINTK("I_T %016llx recovered\n", 5512908d778SJames Bottomley SAS_ADDR(task->dev->sas_addr)); 5522908d778SJames Bottomley task->task_done(task); 5533ebf6922SDarrick J. Wong if (need_reset) 554ad689233SDarrick J. Wong try_to_reset_cmd_device(shost, cmd); 555ad689233SDarrick J. Wong sas_scsi_clear_queue_I_T(work_q, task->dev); 5562908d778SJames Bottomley goto Again; 5572908d778SJames Bottomley } 5582908d778SJames Bottomley /* Hammer time :-) */ 5592908d778SJames Bottomley if (i->dft->lldd_clear_nexus_port) { 5602908d778SJames Bottomley struct asd_sas_port *port = task->dev->port; 5612908d778SJames Bottomley SAS_DPRINTK("clearing nexus for port:%d\n", 5622908d778SJames Bottomley port->id); 5632908d778SJames Bottomley res = i->dft->lldd_clear_nexus_port(port); 5642908d778SJames Bottomley if (res == TMF_RESP_FUNC_COMPLETE) { 5652908d778SJames Bottomley SAS_DPRINTK("clear nexus port:%d " 5662908d778SJames Bottomley "succeeded\n", port->id); 5672908d778SJames Bottomley task->task_done(task); 5683ebf6922SDarrick J. Wong if (need_reset) 569ad689233SDarrick J. Wong try_to_reset_cmd_device(shost, cmd); 570ad689233SDarrick J. Wong sas_scsi_clear_queue_port(work_q, 5712908d778SJames Bottomley port); 5722908d778SJames Bottomley goto Again; 5732908d778SJames Bottomley } 5742908d778SJames Bottomley } 5752908d778SJames Bottomley if (i->dft->lldd_clear_nexus_ha) { 5762908d778SJames Bottomley SAS_DPRINTK("clear nexus ha\n"); 5772908d778SJames Bottomley res = i->dft->lldd_clear_nexus_ha(ha); 5782908d778SJames Bottomley if (res == TMF_RESP_FUNC_COMPLETE) { 5792908d778SJames Bottomley SAS_DPRINTK("clear nexus ha " 5802908d778SJames Bottomley "succeeded\n"); 5812908d778SJames Bottomley task->task_done(task); 5823ebf6922SDarrick J. Wong if (need_reset) 583ad689233SDarrick J. Wong try_to_reset_cmd_device(shost, cmd); 5842908d778SJames Bottomley goto out; 5852908d778SJames Bottomley } 5862908d778SJames Bottomley } 5872908d778SJames Bottomley /* If we are here -- this means that no amount 5882908d778SJames Bottomley * of effort could recover from errors. Quite 5892908d778SJames Bottomley * possibly the HA just disappeared. 5902908d778SJames Bottomley */ 5912908d778SJames Bottomley SAS_DPRINTK("error from device %llx, LUN %x " 5922908d778SJames Bottomley "couldn't be recovered in any way\n", 5932908d778SJames Bottomley SAS_ADDR(task->dev->sas_addr), 5942908d778SJames Bottomley cmd->device->lun); 5952908d778SJames Bottomley 5962908d778SJames Bottomley task->task_done(task); 5973ebf6922SDarrick J. Wong if (need_reset) 598ad689233SDarrick J. Wong try_to_reset_cmd_device(shost, cmd); 5992908d778SJames Bottomley goto clear_q; 6002908d778SJames Bottomley } 6012908d778SJames Bottomley } 6022908d778SJames Bottomley out: 603ad689233SDarrick J. Wong return list_empty(work_q); 6042908d778SJames Bottomley clear_q: 6052908d778SJames Bottomley SAS_DPRINTK("--- Exit %s -- clear_q\n", __FUNCTION__); 606ad689233SDarrick J. Wong list_for_each_entry_safe(cmd, n, work_q, eh_entry) { 6072908d778SJames Bottomley struct sas_task *task = TO_SAS_TASK(cmd); 6082908d778SJames Bottomley list_del_init(&cmd->eh_entry); 6092908d778SJames Bottomley task->task_done(task); 6102908d778SJames Bottomley } 611ad689233SDarrick J. Wong return list_empty(work_q); 612ad689233SDarrick J. Wong } 613ad689233SDarrick J. Wong 614ad689233SDarrick J. Wong void sas_scsi_recover_host(struct Scsi_Host *shost) 615ad689233SDarrick J. Wong { 616ad689233SDarrick J. Wong struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost); 617ad689233SDarrick J. Wong unsigned long flags; 618ad689233SDarrick J. Wong LIST_HEAD(eh_work_q); 619ad689233SDarrick J. Wong 620ad689233SDarrick J. Wong spin_lock_irqsave(shost->host_lock, flags); 621ad689233SDarrick J. Wong list_splice_init(&shost->eh_cmd_q, &eh_work_q); 622ad689233SDarrick J. Wong spin_unlock_irqrestore(shost->host_lock, flags); 623ad689233SDarrick J. Wong 624ad689233SDarrick J. Wong SAS_DPRINTK("Enter %s\n", __FUNCTION__); 625ad689233SDarrick J. Wong /* 626ad689233SDarrick J. Wong * Deal with commands that still have SAS tasks (i.e. they didn't 627ad689233SDarrick J. Wong * complete via the normal sas_task completion mechanism) 628ad689233SDarrick J. Wong */ 629ad689233SDarrick J. Wong if (sas_eh_handle_sas_errors(shost, &eh_work_q, &ha->eh_done_q)) 630ad689233SDarrick J. Wong goto out; 631ad689233SDarrick J. Wong 632ad689233SDarrick J. Wong /* 633ad689233SDarrick J. Wong * Now deal with SCSI commands that completed ok but have a an error 634ad689233SDarrick J. Wong * code (and hopefully sense data) attached. This is roughly what 635ad689233SDarrick J. Wong * scsi_unjam_host does, but we skip scsi_eh_abort_cmds because any 636ad689233SDarrick J. Wong * command we see here has no sas_task and is thus unknown to the HA. 637ad689233SDarrick J. Wong */ 638ad689233SDarrick J. Wong if (!scsi_eh_get_sense(&eh_work_q, &ha->eh_done_q)) 639ad689233SDarrick J. Wong scsi_eh_ready_devs(shost, &eh_work_q, &ha->eh_done_q); 640ad689233SDarrick J. Wong 641ad689233SDarrick J. Wong out: 642ad689233SDarrick J. Wong scsi_eh_flush_done_q(&ha->eh_done_q); 643ad689233SDarrick J. Wong SAS_DPRINTK("--- Exit %s\n", __FUNCTION__); 644ad689233SDarrick J. Wong return; 6452908d778SJames Bottomley } 6462908d778SJames Bottomley 6472908d778SJames Bottomley enum scsi_eh_timer_return sas_scsi_timed_out(struct scsi_cmnd *cmd) 6482908d778SJames Bottomley { 6492908d778SJames Bottomley struct sas_task *task = TO_SAS_TASK(cmd); 6502908d778SJames Bottomley unsigned long flags; 6512908d778SJames Bottomley 6522908d778SJames Bottomley if (!task) { 6536d4dcd4dSDarrick J. Wong cmd->timeout_per_command /= 2; 6546d4dcd4dSDarrick J. Wong SAS_DPRINTK("command 0x%p, task 0x%p, gone: %s\n", 6556d4dcd4dSDarrick J. Wong cmd, task, (cmd->timeout_per_command ? 6566d4dcd4dSDarrick J. Wong "EH_RESET_TIMER" : "EH_NOT_HANDLED")); 6576d4dcd4dSDarrick J. Wong if (!cmd->timeout_per_command) 6586d4dcd4dSDarrick J. Wong return EH_NOT_HANDLED; 6596d4dcd4dSDarrick J. Wong return EH_RESET_TIMER; 6602908d778SJames Bottomley } 6612908d778SJames Bottomley 6622908d778SJames Bottomley spin_lock_irqsave(&task->task_state_lock, flags); 663396819fbSDarrick J. Wong BUG_ON(task->task_state_flags & SAS_TASK_STATE_ABORTED); 6642908d778SJames Bottomley if (task->task_state_flags & SAS_TASK_STATE_DONE) { 6652908d778SJames Bottomley spin_unlock_irqrestore(&task->task_state_lock, flags); 6662908d778SJames Bottomley SAS_DPRINTK("command 0x%p, task 0x%p, timed out: EH_HANDLED\n", 6672908d778SJames Bottomley cmd, task); 6682908d778SJames Bottomley return EH_HANDLED; 6692908d778SJames Bottomley } 670b218a0d8SDarrick J. Wong if (!(task->task_state_flags & SAS_TASK_AT_INITIATOR)) { 671b218a0d8SDarrick J. Wong spin_unlock_irqrestore(&task->task_state_lock, flags); 672b218a0d8SDarrick J. Wong SAS_DPRINTK("command 0x%p, task 0x%p, not at initiator: " 673b218a0d8SDarrick J. Wong "EH_RESET_TIMER\n", 674b218a0d8SDarrick J. Wong cmd, task); 675b218a0d8SDarrick J. Wong return EH_RESET_TIMER; 676b218a0d8SDarrick J. Wong } 6772908d778SJames Bottomley task->task_state_flags |= SAS_TASK_STATE_ABORTED; 6782908d778SJames Bottomley spin_unlock_irqrestore(&task->task_state_lock, flags); 6792908d778SJames Bottomley 6802908d778SJames Bottomley SAS_DPRINTK("command 0x%p, task 0x%p, timed out: EH_NOT_HANDLED\n", 6812908d778SJames Bottomley cmd, task); 6822908d778SJames Bottomley 6832908d778SJames Bottomley return EH_NOT_HANDLED; 6842908d778SJames Bottomley } 6852908d778SJames Bottomley 6862908d778SJames Bottomley struct domain_device *sas_find_dev_by_rphy(struct sas_rphy *rphy) 6872908d778SJames Bottomley { 6882908d778SJames Bottomley struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent); 6892908d778SJames Bottomley struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost); 6902908d778SJames Bottomley struct domain_device *found_dev = NULL; 6912908d778SJames Bottomley int i; 692980fa2f9SDarrick J. Wong unsigned long flags; 6932908d778SJames Bottomley 694980fa2f9SDarrick J. Wong spin_lock_irqsave(&ha->phy_port_lock, flags); 6952908d778SJames Bottomley for (i = 0; i < ha->num_phys; i++) { 6962908d778SJames Bottomley struct asd_sas_port *port = ha->sas_port[i]; 6972908d778SJames Bottomley struct domain_device *dev; 6982908d778SJames Bottomley 6992908d778SJames Bottomley spin_lock(&port->dev_list_lock); 7002908d778SJames Bottomley list_for_each_entry(dev, &port->dev_list, dev_list_node) { 7012908d778SJames Bottomley if (rphy == dev->rphy) { 7022908d778SJames Bottomley found_dev = dev; 7032908d778SJames Bottomley spin_unlock(&port->dev_list_lock); 7042908d778SJames Bottomley goto found; 7052908d778SJames Bottomley } 7062908d778SJames Bottomley } 7072908d778SJames Bottomley spin_unlock(&port->dev_list_lock); 7082908d778SJames Bottomley } 7092908d778SJames Bottomley found: 710980fa2f9SDarrick J. Wong spin_unlock_irqrestore(&ha->phy_port_lock, flags); 7112908d778SJames Bottomley 7122908d778SJames Bottomley return found_dev; 7132908d778SJames Bottomley } 7142908d778SJames Bottomley 7152908d778SJames Bottomley static inline struct domain_device *sas_find_target(struct scsi_target *starget) 7162908d778SJames Bottomley { 7172908d778SJames Bottomley struct sas_rphy *rphy = dev_to_rphy(starget->dev.parent); 7182908d778SJames Bottomley 7192908d778SJames Bottomley return sas_find_dev_by_rphy(rphy); 7202908d778SJames Bottomley } 7212908d778SJames Bottomley 7222908d778SJames Bottomley int sas_target_alloc(struct scsi_target *starget) 7232908d778SJames Bottomley { 7242908d778SJames Bottomley struct domain_device *found_dev = sas_find_target(starget); 7252908d778SJames Bottomley 7262908d778SJames Bottomley if (!found_dev) 7272908d778SJames Bottomley return -ENODEV; 7282908d778SJames Bottomley 7292908d778SJames Bottomley starget->hostdata = found_dev; 7302908d778SJames Bottomley return 0; 7312908d778SJames Bottomley } 7322908d778SJames Bottomley 7332908d778SJames Bottomley #define SAS_DEF_QD 32 7342908d778SJames Bottomley #define SAS_MAX_QD 64 7352908d778SJames Bottomley 7362908d778SJames Bottomley int sas_slave_configure(struct scsi_device *scsi_dev) 7372908d778SJames Bottomley { 7382908d778SJames Bottomley struct domain_device *dev = sdev_to_domain_dev(scsi_dev); 7392908d778SJames Bottomley struct sas_ha_struct *sas_ha; 7402908d778SJames Bottomley 7412908d778SJames Bottomley BUG_ON(dev->rphy->identify.device_type != SAS_END_DEVICE); 7422908d778SJames Bottomley 7432908d778SJames Bottomley sas_ha = dev->port->ha; 7442908d778SJames Bottomley 7452908d778SJames Bottomley sas_read_port_mode_page(scsi_dev); 7462908d778SJames Bottomley 7472908d778SJames Bottomley if (scsi_dev->tagged_supported) { 7482908d778SJames Bottomley scsi_set_tag_type(scsi_dev, MSG_SIMPLE_TAG); 7492908d778SJames Bottomley scsi_activate_tcq(scsi_dev, SAS_DEF_QD); 7502908d778SJames Bottomley } else { 7512908d778SJames Bottomley SAS_DPRINTK("device %llx, LUN %x doesn't support " 7522908d778SJames Bottomley "TCQ\n", SAS_ADDR(dev->sas_addr), 7532908d778SJames Bottomley scsi_dev->lun); 7542908d778SJames Bottomley scsi_dev->tagged_supported = 0; 7552908d778SJames Bottomley scsi_set_tag_type(scsi_dev, 0); 7562908d778SJames Bottomley scsi_deactivate_tcq(scsi_dev, 1); 7572908d778SJames Bottomley } 7582908d778SJames Bottomley 759f27708fcSDarrick J. Wong scsi_dev->allow_restart = 1; 760f27708fcSDarrick J. Wong 7612908d778SJames Bottomley return 0; 7622908d778SJames Bottomley } 7632908d778SJames Bottomley 7642908d778SJames Bottomley void sas_slave_destroy(struct scsi_device *scsi_dev) 7652908d778SJames Bottomley { 7662908d778SJames Bottomley } 7672908d778SJames Bottomley 7682908d778SJames Bottomley int sas_change_queue_depth(struct scsi_device *scsi_dev, int new_depth) 7692908d778SJames Bottomley { 7702908d778SJames Bottomley int res = min(new_depth, SAS_MAX_QD); 7712908d778SJames Bottomley 7722908d778SJames Bottomley if (scsi_dev->tagged_supported) 7732908d778SJames Bottomley scsi_adjust_queue_depth(scsi_dev, scsi_get_tag_type(scsi_dev), 7742908d778SJames Bottomley res); 7752908d778SJames Bottomley else { 7762908d778SJames Bottomley struct domain_device *dev = sdev_to_domain_dev(scsi_dev); 7772908d778SJames Bottomley sas_printk("device %llx LUN %x queue depth changed to 1\n", 7782908d778SJames Bottomley SAS_ADDR(dev->sas_addr), 7792908d778SJames Bottomley scsi_dev->lun); 7802908d778SJames Bottomley scsi_adjust_queue_depth(scsi_dev, 0, 1); 7812908d778SJames Bottomley res = 1; 7822908d778SJames Bottomley } 7832908d778SJames Bottomley 7842908d778SJames Bottomley return res; 7852908d778SJames Bottomley } 7862908d778SJames Bottomley 7872908d778SJames Bottomley int sas_change_queue_type(struct scsi_device *scsi_dev, int qt) 7882908d778SJames Bottomley { 7892908d778SJames Bottomley if (!scsi_dev->tagged_supported) 7902908d778SJames Bottomley return 0; 7912908d778SJames Bottomley 7922908d778SJames Bottomley scsi_deactivate_tcq(scsi_dev, 1); 7932908d778SJames Bottomley 7942908d778SJames Bottomley scsi_set_tag_type(scsi_dev, qt); 7952908d778SJames Bottomley scsi_activate_tcq(scsi_dev, scsi_dev->queue_depth); 7962908d778SJames Bottomley 7972908d778SJames Bottomley return qt; 7982908d778SJames Bottomley } 7992908d778SJames Bottomley 8002908d778SJames Bottomley int sas_bios_param(struct scsi_device *scsi_dev, 8012908d778SJames Bottomley struct block_device *bdev, 8022908d778SJames Bottomley sector_t capacity, int *hsc) 8032908d778SJames Bottomley { 8042908d778SJames Bottomley hsc[0] = 255; 8052908d778SJames Bottomley hsc[1] = 63; 8062908d778SJames Bottomley sector_div(capacity, 255*63); 8072908d778SJames Bottomley hsc[2] = capacity; 8082908d778SJames Bottomley 8092908d778SJames Bottomley return 0; 8102908d778SJames Bottomley } 8112908d778SJames Bottomley 8122908d778SJames Bottomley /* ---------- Task Collector Thread implementation ---------- */ 8132908d778SJames Bottomley 8142908d778SJames Bottomley static void sas_queue(struct sas_ha_struct *sas_ha) 8152908d778SJames Bottomley { 8162908d778SJames Bottomley struct scsi_core *core = &sas_ha->core; 8172908d778SJames Bottomley unsigned long flags; 8182908d778SJames Bottomley LIST_HEAD(q); 8192908d778SJames Bottomley int can_queue; 8202908d778SJames Bottomley int res; 8212908d778SJames Bottomley struct sas_internal *i = to_sas_internal(core->shost->transportt); 8222908d778SJames Bottomley 8232908d778SJames Bottomley spin_lock_irqsave(&core->task_queue_lock, flags); 824*d7a54e30SChristoph Hellwig while (!kthread_should_stop() && 8252908d778SJames Bottomley !list_empty(&core->task_queue)) { 8262908d778SJames Bottomley 8272908d778SJames Bottomley can_queue = sas_ha->lldd_queue_size - core->task_queue_size; 8282908d778SJames Bottomley if (can_queue >= 0) { 8292908d778SJames Bottomley can_queue = core->task_queue_size; 8302908d778SJames Bottomley list_splice_init(&core->task_queue, &q); 8312908d778SJames Bottomley } else { 8322908d778SJames Bottomley struct list_head *a, *n; 8332908d778SJames Bottomley 8342908d778SJames Bottomley can_queue = sas_ha->lldd_queue_size; 8352908d778SJames Bottomley list_for_each_safe(a, n, &core->task_queue) { 8362908d778SJames Bottomley list_move_tail(a, &q); 8372908d778SJames Bottomley if (--can_queue == 0) 8382908d778SJames Bottomley break; 8392908d778SJames Bottomley } 8402908d778SJames Bottomley can_queue = sas_ha->lldd_queue_size; 8412908d778SJames Bottomley } 8422908d778SJames Bottomley core->task_queue_size -= can_queue; 8432908d778SJames Bottomley spin_unlock_irqrestore(&core->task_queue_lock, flags); 8442908d778SJames Bottomley { 8452908d778SJames Bottomley struct sas_task *task = list_entry(q.next, 8462908d778SJames Bottomley struct sas_task, 8472908d778SJames Bottomley list); 8482908d778SJames Bottomley list_del_init(&q); 8492908d778SJames Bottomley res = i->dft->lldd_execute_task(task, can_queue, 8502908d778SJames Bottomley GFP_KERNEL); 8512908d778SJames Bottomley if (unlikely(res)) 8522908d778SJames Bottomley __list_add(&q, task->list.prev, &task->list); 8532908d778SJames Bottomley } 8542908d778SJames Bottomley spin_lock_irqsave(&core->task_queue_lock, flags); 8552908d778SJames Bottomley if (res) { 8562908d778SJames Bottomley list_splice_init(&q, &core->task_queue); /*at head*/ 8572908d778SJames Bottomley core->task_queue_size += can_queue; 8582908d778SJames Bottomley } 8592908d778SJames Bottomley } 8602908d778SJames Bottomley spin_unlock_irqrestore(&core->task_queue_lock, flags); 8612908d778SJames Bottomley } 8622908d778SJames Bottomley 8632908d778SJames Bottomley /** 8642908d778SJames Bottomley * sas_queue_thread -- The Task Collector thread 8652908d778SJames Bottomley * @_sas_ha: pointer to struct sas_ha 8662908d778SJames Bottomley */ 8672908d778SJames Bottomley static int sas_queue_thread(void *_sas_ha) 8682908d778SJames Bottomley { 8692908d778SJames Bottomley struct sas_ha_struct *sas_ha = _sas_ha; 8702908d778SJames Bottomley 8712908d778SJames Bottomley current->flags |= PF_NOFREEZE; 8722908d778SJames Bottomley 8732908d778SJames Bottomley while (1) { 874*d7a54e30SChristoph Hellwig set_current_state(TASK_INTERRUPTIBLE); 875*d7a54e30SChristoph Hellwig schedule(); 8762908d778SJames Bottomley sas_queue(sas_ha); 877*d7a54e30SChristoph Hellwig if (kthread_should_stop()) 8782908d778SJames Bottomley break; 8792908d778SJames Bottomley } 8802908d778SJames Bottomley 8812908d778SJames Bottomley return 0; 8822908d778SJames Bottomley } 8832908d778SJames Bottomley 8842908d778SJames Bottomley int sas_init_queue(struct sas_ha_struct *sas_ha) 8852908d778SJames Bottomley { 8862908d778SJames Bottomley struct scsi_core *core = &sas_ha->core; 8872908d778SJames Bottomley 8882908d778SJames Bottomley spin_lock_init(&core->task_queue_lock); 8892908d778SJames Bottomley core->task_queue_size = 0; 8902908d778SJames Bottomley INIT_LIST_HEAD(&core->task_queue); 8912908d778SJames Bottomley 892*d7a54e30SChristoph Hellwig core->queue_thread = kthread_run(sas_queue_thread, sas_ha, 893*d7a54e30SChristoph Hellwig "sas_queue_%d", core->shost->host_no); 894*d7a54e30SChristoph Hellwig if (IS_ERR(core->queue_thread)) 895*d7a54e30SChristoph Hellwig return PTR_ERR(core->queue_thread); 896*d7a54e30SChristoph Hellwig return 0; 8972908d778SJames Bottomley } 8982908d778SJames Bottomley 8992908d778SJames Bottomley void sas_shutdown_queue(struct sas_ha_struct *sas_ha) 9002908d778SJames Bottomley { 9012908d778SJames Bottomley unsigned long flags; 9022908d778SJames Bottomley struct scsi_core *core = &sas_ha->core; 9032908d778SJames Bottomley struct sas_task *task, *n; 9042908d778SJames Bottomley 905*d7a54e30SChristoph Hellwig kthread_stop(core->queue_thread); 9062908d778SJames Bottomley 9072908d778SJames Bottomley if (!list_empty(&core->task_queue)) 9082908d778SJames Bottomley SAS_DPRINTK("HA: %llx: scsi core task queue is NOT empty!?\n", 9092908d778SJames Bottomley SAS_ADDR(sas_ha->sas_addr)); 9102908d778SJames Bottomley 9112908d778SJames Bottomley spin_lock_irqsave(&core->task_queue_lock, flags); 9122908d778SJames Bottomley list_for_each_entry_safe(task, n, &core->task_queue, list) { 9132908d778SJames Bottomley struct scsi_cmnd *cmd = task->uldd_task; 9142908d778SJames Bottomley 9152908d778SJames Bottomley list_del_init(&task->list); 9162908d778SJames Bottomley 9172908d778SJames Bottomley ASSIGN_SAS_TASK(cmd, NULL); 9182908d778SJames Bottomley sas_free_task(task); 9192908d778SJames Bottomley cmd->result = DID_ABORT << 16; 9202908d778SJames Bottomley cmd->scsi_done(cmd); 9212908d778SJames Bottomley } 9222908d778SJames Bottomley spin_unlock_irqrestore(&core->task_queue_lock, flags); 9232908d778SJames Bottomley } 9242908d778SJames Bottomley 925396819fbSDarrick J. Wong /* 926396819fbSDarrick J. Wong * Call the LLDD task abort routine directly. This function is intended for 927396819fbSDarrick J. Wong * use by upper layers that need to tell the LLDD to abort a task. 928396819fbSDarrick J. Wong */ 929396819fbSDarrick J. Wong int __sas_task_abort(struct sas_task *task) 93079a5eb60SDarrick J. Wong { 93179a5eb60SDarrick J. Wong struct sas_internal *si = 93279a5eb60SDarrick J. Wong to_sas_internal(task->dev->port->ha->core.shost->transportt); 93379a5eb60SDarrick J. Wong unsigned long flags; 93479a5eb60SDarrick J. Wong int res; 93579a5eb60SDarrick J. Wong 93679a5eb60SDarrick J. Wong spin_lock_irqsave(&task->task_state_lock, flags); 937396819fbSDarrick J. Wong if (task->task_state_flags & SAS_TASK_STATE_ABORTED || 938396819fbSDarrick J. Wong task->task_state_flags & SAS_TASK_STATE_DONE) { 93979a5eb60SDarrick J. Wong spin_unlock_irqrestore(&task->task_state_lock, flags); 940396819fbSDarrick J. Wong SAS_DPRINTK("%s: Task %p already finished.\n", __FUNCTION__, 94179a5eb60SDarrick J. Wong task); 94279a5eb60SDarrick J. Wong return 0; 94379a5eb60SDarrick J. Wong } 94479a5eb60SDarrick J. Wong task->task_state_flags |= SAS_TASK_STATE_ABORTED; 94579a5eb60SDarrick J. Wong spin_unlock_irqrestore(&task->task_state_lock, flags); 94679a5eb60SDarrick J. Wong 94779a5eb60SDarrick J. Wong if (!si->dft->lldd_abort_task) 94879a5eb60SDarrick J. Wong return -ENODEV; 94979a5eb60SDarrick J. Wong 95079a5eb60SDarrick J. Wong res = si->dft->lldd_abort_task(task); 951396819fbSDarrick J. Wong 952396819fbSDarrick J. Wong spin_lock_irqsave(&task->task_state_lock, flags); 95379a5eb60SDarrick J. Wong if ((task->task_state_flags & SAS_TASK_STATE_DONE) || 95479a5eb60SDarrick J. Wong (res == TMF_RESP_FUNC_COMPLETE)) 95579a5eb60SDarrick J. Wong { 956396819fbSDarrick J. Wong spin_unlock_irqrestore(&task->task_state_lock, flags); 95779a5eb60SDarrick J. Wong task->task_done(task); 95879a5eb60SDarrick J. Wong return 0; 95979a5eb60SDarrick J. Wong } 96079a5eb60SDarrick J. Wong 96179a5eb60SDarrick J. Wong if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) 96279a5eb60SDarrick J. Wong task->task_state_flags &= ~SAS_TASK_STATE_ABORTED; 96379a5eb60SDarrick J. Wong spin_unlock_irqrestore(&task->task_state_lock, flags); 96479a5eb60SDarrick J. Wong 96579a5eb60SDarrick J. Wong return -EAGAIN; 96679a5eb60SDarrick J. Wong } 96779a5eb60SDarrick J. Wong 968396819fbSDarrick J. Wong /* 969396819fbSDarrick J. Wong * Tell an upper layer that it needs to initiate an abort for a given task. 970396819fbSDarrick J. Wong * This should only ever be called by an LLDD. 971396819fbSDarrick J. Wong */ 972396819fbSDarrick J. Wong void sas_task_abort(struct sas_task *task) 97379a5eb60SDarrick J. Wong { 974396819fbSDarrick J. Wong struct scsi_cmnd *sc = task->uldd_task; 97579a5eb60SDarrick J. Wong 976396819fbSDarrick J. Wong /* Escape for libsas internal commands */ 977396819fbSDarrick J. Wong if (!sc) { 978396819fbSDarrick J. Wong if (!del_timer(&task->timer)) 97979a5eb60SDarrick J. Wong return; 980396819fbSDarrick J. Wong task->timer.function(task->timer.data); 981396819fbSDarrick J. Wong return; 982396819fbSDarrick J. Wong } 98379a5eb60SDarrick J. Wong 984396819fbSDarrick J. Wong scsi_req_abort_cmd(sc); 985396819fbSDarrick J. Wong scsi_schedule_eh(sc->device->host); 98679a5eb60SDarrick J. Wong } 98779a5eb60SDarrick J. Wong 9882908d778SJames Bottomley EXPORT_SYMBOL_GPL(sas_queuecommand); 9892908d778SJames Bottomley EXPORT_SYMBOL_GPL(sas_target_alloc); 9902908d778SJames Bottomley EXPORT_SYMBOL_GPL(sas_slave_configure); 9912908d778SJames Bottomley EXPORT_SYMBOL_GPL(sas_slave_destroy); 9922908d778SJames Bottomley EXPORT_SYMBOL_GPL(sas_change_queue_depth); 9932908d778SJames Bottomley EXPORT_SYMBOL_GPL(sas_change_queue_type); 9942908d778SJames Bottomley EXPORT_SYMBOL_GPL(sas_bios_param); 995396819fbSDarrick J. Wong EXPORT_SYMBOL_GPL(__sas_task_abort); 99679a5eb60SDarrick J. Wong EXPORT_SYMBOL_GPL(sas_task_abort); 997dea22214SDarrick J. Wong EXPORT_SYMBOL_GPL(sas_phy_reset); 998acbf167dSDarrick J. Wong EXPORT_SYMBOL_GPL(sas_phy_enable); 999ad689233SDarrick J. Wong EXPORT_SYMBOL_GPL(sas_eh_device_reset_handler); 1000a9344e68SDarrick J. Wong EXPORT_SYMBOL_GPL(sas_eh_bus_reset_handler); 1001