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> 27d7a54e30SChristoph 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> 4383144186SRafael J. Wysocki #include <linux/freezer.h> 442908d778SJames Bottomley #include <linux/scatterlist.h> 45fa1c1e8fSDarrick J. Wong #include <linux/libata.h> 462908d778SJames Bottomley 472908d778SJames Bottomley /* ---------- SCSI Host glue ---------- */ 482908d778SJames Bottomley 492908d778SJames Bottomley #define TO_SAS_TASK(_scsi_cmd) ((void *)(_scsi_cmd)->host_scribble) 502908d778SJames Bottomley #define ASSIGN_SAS_TASK(_sc, _t) do { (_sc)->host_scribble = (void *) _t; } while (0) 512908d778SJames Bottomley 522908d778SJames Bottomley static void sas_scsi_task_done(struct sas_task *task) 532908d778SJames Bottomley { 542908d778SJames Bottomley struct task_status_struct *ts = &task->task_status; 552908d778SJames Bottomley struct scsi_cmnd *sc = task->uldd_task; 56f456393eSDarrick J. Wong struct sas_ha_struct *sas_ha = SHOST_TO_SAS_HA(sc->device->host); 572908d778SJames Bottomley unsigned ts_flags = task->task_state_flags; 582908d778SJames Bottomley int hs = 0, stat = 0; 592908d778SJames Bottomley 602908d778SJames Bottomley if (unlikely(!sc)) { 612908d778SJames Bottomley SAS_DPRINTK("task_done called with non existing SCSI cmnd!\n"); 622908d778SJames Bottomley list_del_init(&task->list); 632908d778SJames Bottomley sas_free_task(task); 642908d778SJames Bottomley return; 652908d778SJames Bottomley } 662908d778SJames Bottomley 672908d778SJames Bottomley if (ts->resp == SAS_TASK_UNDELIVERED) { 682908d778SJames Bottomley /* transport error */ 692908d778SJames Bottomley hs = DID_NO_CONNECT; 702908d778SJames Bottomley } else { /* ts->resp == SAS_TASK_COMPLETE */ 712908d778SJames Bottomley /* task delivered, what happened afterwards? */ 722908d778SJames Bottomley switch (ts->stat) { 732908d778SJames Bottomley case SAS_DEV_NO_RESPONSE: 742908d778SJames Bottomley case SAS_INTERRUPTED: 752908d778SJames Bottomley case SAS_PHY_DOWN: 762908d778SJames Bottomley case SAS_NAK_R_ERR: 772908d778SJames Bottomley case SAS_OPEN_TO: 782908d778SJames Bottomley hs = DID_NO_CONNECT; 792908d778SJames Bottomley break; 802908d778SJames Bottomley case SAS_DATA_UNDERRUN: 81c13e5566SFUJITA Tomonori scsi_set_resid(sc, ts->residual); 82c13e5566SFUJITA Tomonori if (scsi_bufflen(sc) - scsi_get_resid(sc) < sc->underflow) 832908d778SJames Bottomley hs = DID_ERROR; 842908d778SJames Bottomley break; 852908d778SJames Bottomley case SAS_DATA_OVERRUN: 862908d778SJames Bottomley hs = DID_ERROR; 872908d778SJames Bottomley break; 882908d778SJames Bottomley case SAS_QUEUE_FULL: 892908d778SJames Bottomley hs = DID_SOFT_ERROR; /* retry */ 902908d778SJames Bottomley break; 912908d778SJames Bottomley case SAS_DEVICE_UNKNOWN: 922908d778SJames Bottomley hs = DID_BAD_TARGET; 932908d778SJames Bottomley break; 942908d778SJames Bottomley case SAS_SG_ERR: 952908d778SJames Bottomley hs = DID_PARITY; 962908d778SJames Bottomley break; 972908d778SJames Bottomley case SAS_OPEN_REJECT: 982908d778SJames Bottomley if (ts->open_rej_reason == SAS_OREJ_RSVD_RETRY) 992908d778SJames Bottomley hs = DID_SOFT_ERROR; /* retry */ 1002908d778SJames Bottomley else 1012908d778SJames Bottomley hs = DID_ERROR; 1022908d778SJames Bottomley break; 1032908d778SJames Bottomley case SAS_PROTO_RESPONSE: 1042908d778SJames Bottomley SAS_DPRINTK("LLDD:%s sent SAS_PROTO_RESP for an SSP " 1052908d778SJames Bottomley "task; please report this\n", 1062908d778SJames Bottomley task->dev->port->ha->sas_ha_name); 1072908d778SJames Bottomley break; 1082908d778SJames Bottomley case SAS_ABORTED_TASK: 1092908d778SJames Bottomley hs = DID_ABORT; 1102908d778SJames Bottomley break; 1112908d778SJames Bottomley case SAM_CHECK_COND: 1122908d778SJames Bottomley memcpy(sc->sense_buffer, ts->buf, 1132908d778SJames Bottomley max(SCSI_SENSE_BUFFERSIZE, ts->buf_valid_size)); 1142908d778SJames Bottomley stat = SAM_CHECK_COND; 1152908d778SJames Bottomley break; 1162908d778SJames Bottomley default: 1172908d778SJames Bottomley stat = ts->stat; 1182908d778SJames Bottomley break; 1192908d778SJames Bottomley } 1202908d778SJames Bottomley } 1212908d778SJames Bottomley ASSIGN_SAS_TASK(sc, NULL); 1222908d778SJames Bottomley sc->result = (hs << 16) | stat; 1232908d778SJames Bottomley list_del_init(&task->list); 1242908d778SJames Bottomley sas_free_task(task); 1252908d778SJames Bottomley /* This is very ugly but this is how SCSI Core works. */ 1262908d778SJames Bottomley if (ts_flags & SAS_TASK_STATE_ABORTED) 127f456393eSDarrick J. Wong scsi_eh_finish_cmd(sc, &sas_ha->eh_done_q); 1282908d778SJames Bottomley else 1292908d778SJames Bottomley sc->scsi_done(sc); 1302908d778SJames Bottomley } 1312908d778SJames Bottomley 1322908d778SJames Bottomley static enum task_attribute sas_scsi_get_task_attr(struct scsi_cmnd *cmd) 1332908d778SJames Bottomley { 1342908d778SJames Bottomley enum task_attribute ta = TASK_ATTR_SIMPLE; 1352908d778SJames Bottomley if (cmd->request && blk_rq_tagged(cmd->request)) { 1362908d778SJames Bottomley if (cmd->device->ordered_tags && 1371bdfd554SJeff Garzik (cmd->request->cmd_flags & REQ_HARDBARRIER)) 13863bb1bf0SFUJITA Tomonori ta = TASK_ATTR_ORDERED; 1392908d778SJames Bottomley } 1402908d778SJames Bottomley return ta; 1412908d778SJames Bottomley } 1422908d778SJames Bottomley 1432908d778SJames Bottomley static struct sas_task *sas_create_task(struct scsi_cmnd *cmd, 1442908d778SJames Bottomley struct domain_device *dev, 1453cc27547SAl Viro gfp_t gfp_flags) 1462908d778SJames Bottomley { 1472908d778SJames Bottomley struct sas_task *task = sas_alloc_task(gfp_flags); 1482908d778SJames Bottomley struct scsi_lun lun; 1492908d778SJames Bottomley 1502908d778SJames Bottomley if (!task) 1512908d778SJames Bottomley return NULL; 1522908d778SJames Bottomley 1532908d778SJames Bottomley *(u32 *)cmd->sense_buffer = 0; 1542908d778SJames Bottomley task->uldd_task = cmd; 1552908d778SJames Bottomley ASSIGN_SAS_TASK(cmd, task); 1562908d778SJames Bottomley 1572908d778SJames Bottomley task->dev = dev; 1582908d778SJames Bottomley task->task_proto = task->dev->tproto; /* BUG_ON(!SSP) */ 1592908d778SJames Bottomley 1602908d778SJames Bottomley task->ssp_task.retry_count = 1; 1612908d778SJames Bottomley int_to_scsilun(cmd->device->lun, &lun); 1622908d778SJames Bottomley memcpy(task->ssp_task.LUN, &lun.scsi_lun, 8); 1632908d778SJames Bottomley task->ssp_task.task_attr = sas_scsi_get_task_attr(cmd); 1642908d778SJames Bottomley memcpy(task->ssp_task.cdb, cmd->cmnd, 16); 1652908d778SJames Bottomley 166c13e5566SFUJITA Tomonori task->scatter = scsi_sglist(cmd); 167c13e5566SFUJITA Tomonori task->num_scatter = scsi_sg_count(cmd); 168c13e5566SFUJITA Tomonori task->total_xfer_len = scsi_bufflen(cmd); 1692908d778SJames Bottomley task->data_dir = cmd->sc_data_direction; 1702908d778SJames Bottomley 1712908d778SJames Bottomley task->task_done = sas_scsi_task_done; 1722908d778SJames Bottomley 1732908d778SJames Bottomley return task; 1742908d778SJames Bottomley } 1752908d778SJames Bottomley 1762908d778SJames Bottomley static int sas_queue_up(struct sas_task *task) 1772908d778SJames Bottomley { 1782908d778SJames Bottomley struct sas_ha_struct *sas_ha = task->dev->port->ha; 1792908d778SJames Bottomley struct scsi_core *core = &sas_ha->core; 1802908d778SJames Bottomley unsigned long flags; 1812908d778SJames Bottomley LIST_HEAD(list); 1822908d778SJames Bottomley 1832908d778SJames Bottomley spin_lock_irqsave(&core->task_queue_lock, flags); 1842908d778SJames Bottomley if (sas_ha->lldd_queue_size < core->task_queue_size + 1) { 1852908d778SJames Bottomley spin_unlock_irqrestore(&core->task_queue_lock, flags); 1862908d778SJames Bottomley return -SAS_QUEUE_FULL; 1872908d778SJames Bottomley } 1882908d778SJames Bottomley list_add_tail(&task->list, &core->task_queue); 1892908d778SJames Bottomley core->task_queue_size += 1; 1902908d778SJames Bottomley spin_unlock_irqrestore(&core->task_queue_lock, flags); 191d7a54e30SChristoph Hellwig wake_up_process(core->queue_thread); 1922908d778SJames Bottomley 1932908d778SJames Bottomley return 0; 1942908d778SJames Bottomley } 1952908d778SJames Bottomley 196fa1c1e8fSDarrick J. Wong static inline int dev_is_sata(struct domain_device *dev) 197fa1c1e8fSDarrick J. Wong { 198fa1c1e8fSDarrick J. Wong return (dev->rphy->identify.target_port_protocols & SAS_PROTOCOL_SATA); 199fa1c1e8fSDarrick J. Wong } 200fa1c1e8fSDarrick J. Wong 2012908d778SJames Bottomley /** 2022908d778SJames Bottomley * sas_queuecommand -- Enqueue a command for processing 2032908d778SJames Bottomley * @parameters: See SCSI Core documentation 2042908d778SJames Bottomley * 2052908d778SJames Bottomley * Note: XXX: Remove the host unlock/lock pair when SCSI Core can 2062908d778SJames Bottomley * call us without holding an IRQ spinlock... 2072908d778SJames Bottomley */ 2082908d778SJames Bottomley int sas_queuecommand(struct scsi_cmnd *cmd, 2092908d778SJames Bottomley void (*scsi_done)(struct scsi_cmnd *)) 2102908d778SJames Bottomley { 2112908d778SJames Bottomley int res = 0; 2122908d778SJames Bottomley struct domain_device *dev = cmd_to_domain_dev(cmd); 2132908d778SJames Bottomley struct Scsi_Host *host = cmd->device->host; 2142908d778SJames Bottomley struct sas_internal *i = to_sas_internal(host->transportt); 2152908d778SJames Bottomley 2162908d778SJames Bottomley spin_unlock_irq(host->host_lock); 2172908d778SJames Bottomley 2182908d778SJames Bottomley { 2192908d778SJames Bottomley struct sas_ha_struct *sas_ha = dev->port->ha; 2202908d778SJames Bottomley struct sas_task *task; 2212908d778SJames Bottomley 222fa1c1e8fSDarrick J. Wong if (dev_is_sata(dev)) { 223fa1c1e8fSDarrick J. Wong res = ata_sas_queuecmd(cmd, scsi_done, 224fa1c1e8fSDarrick J. Wong dev->sata_dev.ap); 225fa1c1e8fSDarrick J. Wong goto out; 226fa1c1e8fSDarrick J. Wong } 227fa1c1e8fSDarrick J. Wong 2282908d778SJames Bottomley res = -ENOMEM; 2292908d778SJames Bottomley task = sas_create_task(cmd, dev, GFP_ATOMIC); 2302908d778SJames Bottomley if (!task) 2312908d778SJames Bottomley goto out; 2322908d778SJames Bottomley 2332908d778SJames Bottomley cmd->scsi_done = scsi_done; 2342908d778SJames Bottomley /* Queue up, Direct Mode or Task Collector Mode. */ 2352908d778SJames Bottomley if (sas_ha->lldd_max_execute_num < 2) 2362908d778SJames Bottomley res = i->dft->lldd_execute_task(task, 1, GFP_ATOMIC); 2372908d778SJames Bottomley else 2382908d778SJames Bottomley res = sas_queue_up(task); 2392908d778SJames Bottomley 2402908d778SJames Bottomley /* Examine */ 2412908d778SJames Bottomley if (res) { 2422908d778SJames Bottomley SAS_DPRINTK("lldd_execute_task returned: %d\n", res); 2432908d778SJames Bottomley ASSIGN_SAS_TASK(cmd, NULL); 2442908d778SJames Bottomley sas_free_task(task); 2452908d778SJames Bottomley if (res == -SAS_QUEUE_FULL) { 2462908d778SJames Bottomley cmd->result = DID_SOFT_ERROR << 16; /* retry */ 2472908d778SJames Bottomley res = 0; 2482908d778SJames Bottomley scsi_done(cmd); 2492908d778SJames Bottomley } 2502908d778SJames Bottomley goto out; 2512908d778SJames Bottomley } 2522908d778SJames Bottomley } 2532908d778SJames Bottomley out: 2542908d778SJames Bottomley spin_lock_irq(host->host_lock); 2552908d778SJames Bottomley return res; 2562908d778SJames Bottomley } 2572908d778SJames Bottomley 2582908d778SJames Bottomley static void sas_scsi_clear_queue_lu(struct list_head *error_q, struct scsi_cmnd *my_cmd) 2592908d778SJames Bottomley { 2602908d778SJames Bottomley struct scsi_cmnd *cmd, *n; 2612908d778SJames Bottomley 2622908d778SJames Bottomley list_for_each_entry_safe(cmd, n, error_q, eh_entry) { 2632908d778SJames Bottomley if (cmd == my_cmd) 2642908d778SJames Bottomley list_del_init(&cmd->eh_entry); 2652908d778SJames Bottomley } 2662908d778SJames Bottomley } 2672908d778SJames Bottomley 2682908d778SJames Bottomley static void sas_scsi_clear_queue_I_T(struct list_head *error_q, 2692908d778SJames Bottomley struct domain_device *dev) 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 *x = cmd_to_domain_dev(cmd); 2752908d778SJames Bottomley 2762908d778SJames Bottomley if (x == dev) 2772908d778SJames Bottomley list_del_init(&cmd->eh_entry); 2782908d778SJames Bottomley } 2792908d778SJames Bottomley } 2802908d778SJames Bottomley 2812908d778SJames Bottomley static void sas_scsi_clear_queue_port(struct list_head *error_q, 2822908d778SJames Bottomley struct asd_sas_port *port) 2832908d778SJames Bottomley { 2842908d778SJames Bottomley struct scsi_cmnd *cmd, *n; 2852908d778SJames Bottomley 2862908d778SJames Bottomley list_for_each_entry_safe(cmd, n, error_q, eh_entry) { 2872908d778SJames Bottomley struct domain_device *dev = cmd_to_domain_dev(cmd); 2882908d778SJames Bottomley struct asd_sas_port *x = dev->port; 2892908d778SJames Bottomley 2902908d778SJames Bottomley if (x == port) 2912908d778SJames Bottomley list_del_init(&cmd->eh_entry); 2922908d778SJames Bottomley } 2932908d778SJames Bottomley } 2942908d778SJames Bottomley 2952908d778SJames Bottomley enum task_disposition { 2962908d778SJames Bottomley TASK_IS_DONE, 2972908d778SJames Bottomley TASK_IS_ABORTED, 2982908d778SJames Bottomley TASK_IS_AT_LU, 2992908d778SJames Bottomley TASK_IS_NOT_AT_LU, 30002cd743bSDarrick J. Wong TASK_ABORT_FAILED, 3012908d778SJames Bottomley }; 3022908d778SJames Bottomley 3032908d778SJames Bottomley static enum task_disposition sas_scsi_find_task(struct sas_task *task) 3042908d778SJames Bottomley { 3052908d778SJames Bottomley struct sas_ha_struct *ha = task->dev->port->ha; 3062908d778SJames Bottomley unsigned long flags; 3072908d778SJames Bottomley int i, res; 3082908d778SJames Bottomley struct sas_internal *si = 3092908d778SJames Bottomley to_sas_internal(task->dev->port->ha->core.shost->transportt); 3102908d778SJames Bottomley 3112908d778SJames Bottomley if (ha->lldd_max_execute_num > 1) { 3122908d778SJames Bottomley struct scsi_core *core = &ha->core; 3132908d778SJames Bottomley struct sas_task *t, *n; 3142908d778SJames Bottomley 3152908d778SJames Bottomley spin_lock_irqsave(&core->task_queue_lock, flags); 3162908d778SJames Bottomley list_for_each_entry_safe(t, n, &core->task_queue, list) { 3172908d778SJames Bottomley if (task == t) { 3182908d778SJames Bottomley list_del_init(&t->list); 3192908d778SJames Bottomley spin_unlock_irqrestore(&core->task_queue_lock, 3202908d778SJames Bottomley flags); 3212908d778SJames Bottomley SAS_DPRINTK("%s: task 0x%p aborted from " 3222908d778SJames Bottomley "task_queue\n", 3232908d778SJames Bottomley __FUNCTION__, task); 3242908d778SJames Bottomley return TASK_IS_ABORTED; 3252908d778SJames Bottomley } 3262908d778SJames Bottomley } 3272908d778SJames Bottomley spin_unlock_irqrestore(&core->task_queue_lock, flags); 3282908d778SJames Bottomley } 3292908d778SJames Bottomley 3302908d778SJames Bottomley for (i = 0; i < 5; i++) { 3312908d778SJames Bottomley SAS_DPRINTK("%s: aborting task 0x%p\n", __FUNCTION__, task); 3322908d778SJames Bottomley res = si->dft->lldd_abort_task(task); 3332908d778SJames Bottomley 3342908d778SJames Bottomley spin_lock_irqsave(&task->task_state_lock, flags); 3352908d778SJames Bottomley if (task->task_state_flags & SAS_TASK_STATE_DONE) { 3362908d778SJames Bottomley spin_unlock_irqrestore(&task->task_state_lock, flags); 3372908d778SJames Bottomley SAS_DPRINTK("%s: task 0x%p is done\n", __FUNCTION__, 3382908d778SJames Bottomley task); 3392908d778SJames Bottomley return TASK_IS_DONE; 3402908d778SJames Bottomley } 3412908d778SJames Bottomley spin_unlock_irqrestore(&task->task_state_lock, flags); 3422908d778SJames Bottomley 3432908d778SJames Bottomley if (res == TMF_RESP_FUNC_COMPLETE) { 3442908d778SJames Bottomley SAS_DPRINTK("%s: task 0x%p is aborted\n", 3452908d778SJames Bottomley __FUNCTION__, task); 3462908d778SJames Bottomley return TASK_IS_ABORTED; 3472908d778SJames Bottomley } else if (si->dft->lldd_query_task) { 3482908d778SJames Bottomley SAS_DPRINTK("%s: querying task 0x%p\n", 3492908d778SJames Bottomley __FUNCTION__, task); 3502908d778SJames Bottomley res = si->dft->lldd_query_task(task); 35102cd743bSDarrick J. Wong switch (res) { 35202cd743bSDarrick J. Wong case TMF_RESP_FUNC_SUCC: 3532908d778SJames Bottomley SAS_DPRINTK("%s: task 0x%p at LU\n", 3542908d778SJames Bottomley __FUNCTION__, task); 3552908d778SJames Bottomley return TASK_IS_AT_LU; 35602cd743bSDarrick J. Wong case TMF_RESP_FUNC_COMPLETE: 3572908d778SJames Bottomley SAS_DPRINTK("%s: task 0x%p not at LU\n", 3582908d778SJames Bottomley __FUNCTION__, task); 3592908d778SJames Bottomley return TASK_IS_NOT_AT_LU; 36002cd743bSDarrick J. Wong case TMF_RESP_FUNC_FAILED: 36102cd743bSDarrick J. Wong SAS_DPRINTK("%s: task 0x%p failed to abort\n", 36202cd743bSDarrick J. Wong __FUNCTION__, task); 36302cd743bSDarrick J. Wong return TASK_ABORT_FAILED; 3642908d778SJames Bottomley } 36502cd743bSDarrick J. Wong 3662908d778SJames Bottomley } 3672908d778SJames Bottomley } 3682908d778SJames Bottomley return res; 3692908d778SJames Bottomley } 3702908d778SJames Bottomley 3712908d778SJames Bottomley static int sas_recover_lu(struct domain_device *dev, struct scsi_cmnd *cmd) 3722908d778SJames Bottomley { 3732908d778SJames Bottomley int res = TMF_RESP_FUNC_FAILED; 3742908d778SJames Bottomley struct scsi_lun lun; 3752908d778SJames Bottomley struct sas_internal *i = 3762908d778SJames Bottomley to_sas_internal(dev->port->ha->core.shost->transportt); 3772908d778SJames Bottomley 3782908d778SJames Bottomley int_to_scsilun(cmd->device->lun, &lun); 3792908d778SJames Bottomley 3802908d778SJames Bottomley SAS_DPRINTK("eh: device %llx LUN %x has the task\n", 3812908d778SJames Bottomley SAS_ADDR(dev->sas_addr), 3822908d778SJames Bottomley cmd->device->lun); 3832908d778SJames Bottomley 3842908d778SJames Bottomley if (i->dft->lldd_abort_task_set) 3852908d778SJames Bottomley res = i->dft->lldd_abort_task_set(dev, lun.scsi_lun); 3862908d778SJames Bottomley 3872908d778SJames Bottomley if (res == TMF_RESP_FUNC_FAILED) { 3882908d778SJames Bottomley if (i->dft->lldd_clear_task_set) 3892908d778SJames Bottomley res = i->dft->lldd_clear_task_set(dev, lun.scsi_lun); 3902908d778SJames Bottomley } 3912908d778SJames Bottomley 3922908d778SJames Bottomley if (res == TMF_RESP_FUNC_FAILED) { 3932908d778SJames Bottomley if (i->dft->lldd_lu_reset) 3942908d778SJames Bottomley res = i->dft->lldd_lu_reset(dev, lun.scsi_lun); 3952908d778SJames Bottomley } 3962908d778SJames Bottomley 3972908d778SJames Bottomley return res; 3982908d778SJames Bottomley } 3992908d778SJames Bottomley 4002908d778SJames Bottomley static int sas_recover_I_T(struct domain_device *dev) 4012908d778SJames Bottomley { 4022908d778SJames Bottomley int res = TMF_RESP_FUNC_FAILED; 4032908d778SJames Bottomley struct sas_internal *i = 4042908d778SJames Bottomley to_sas_internal(dev->port->ha->core.shost->transportt); 4052908d778SJames Bottomley 4062908d778SJames Bottomley SAS_DPRINTK("I_T nexus reset for dev %016llx\n", 4072908d778SJames Bottomley SAS_ADDR(dev->sas_addr)); 4082908d778SJames Bottomley 4092908d778SJames Bottomley if (i->dft->lldd_I_T_nexus_reset) 4102908d778SJames Bottomley res = i->dft->lldd_I_T_nexus_reset(dev); 4112908d778SJames Bottomley 4122908d778SJames Bottomley return res; 4132908d778SJames Bottomley } 4142908d778SJames Bottomley 415ad689233SDarrick J. Wong /* Find the sas_phy that's attached to this device */ 416ad689233SDarrick J. Wong struct sas_phy *find_local_sas_phy(struct domain_device *dev) 4173ebf6922SDarrick J. Wong { 418ad689233SDarrick J. Wong struct domain_device *pdev = dev->parent; 419ad689233SDarrick J. Wong struct ex_phy *exphy = NULL; 420ad689233SDarrick J. Wong int i; 4213ebf6922SDarrick J. Wong 422ad689233SDarrick J. Wong /* Directly attached device */ 423ad689233SDarrick J. Wong if (!pdev) 424ad689233SDarrick J. Wong return dev->port->phy; 4253ebf6922SDarrick J. Wong 426ad689233SDarrick J. Wong /* Otherwise look in the expander */ 427ad689233SDarrick J. Wong for (i = 0; i < pdev->ex_dev.num_phys; i++) 428ad689233SDarrick J. Wong if (!memcmp(dev->sas_addr, 429ad689233SDarrick J. Wong pdev->ex_dev.ex_phy[i].attached_sas_addr, 430ad689233SDarrick J. Wong SAS_ADDR_SIZE)) { 431ad689233SDarrick J. Wong exphy = &pdev->ex_dev.ex_phy[i]; 432ad689233SDarrick J. Wong break; 4333ebf6922SDarrick J. Wong } 4343ebf6922SDarrick J. Wong 435ad689233SDarrick J. Wong BUG_ON(!exphy); 436ad689233SDarrick J. Wong return exphy->phy; 437ad689233SDarrick J. Wong } 438ad689233SDarrick J. Wong 439a9344e68SDarrick J. Wong /* Attempt to send a LUN reset message to a device */ 440ad689233SDarrick J. Wong int sas_eh_device_reset_handler(struct scsi_cmnd *cmd) 4412908d778SJames Bottomley { 442ad689233SDarrick J. Wong struct domain_device *dev = cmd_to_domain_dev(cmd); 443a9344e68SDarrick J. Wong struct sas_internal *i = 444a9344e68SDarrick J. Wong to_sas_internal(dev->port->ha->core.shost->transportt); 445a9344e68SDarrick J. Wong struct scsi_lun lun; 446a9344e68SDarrick J. Wong int res; 447a9344e68SDarrick J. Wong 448a9344e68SDarrick J. Wong int_to_scsilun(cmd->device->lun, &lun); 449a9344e68SDarrick J. Wong 450a9344e68SDarrick J. Wong if (!i->dft->lldd_lu_reset) 451a9344e68SDarrick J. Wong return FAILED; 452a9344e68SDarrick J. Wong 453a9344e68SDarrick J. Wong res = i->dft->lldd_lu_reset(dev, lun.scsi_lun); 454a9344e68SDarrick J. Wong if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE) 455a9344e68SDarrick J. Wong return SUCCESS; 456a9344e68SDarrick J. Wong 457a9344e68SDarrick J. Wong return FAILED; 458a9344e68SDarrick J. Wong } 459a9344e68SDarrick J. Wong 460a9344e68SDarrick J. Wong /* Attempt to send a phy (bus) reset */ 461a9344e68SDarrick J. Wong int sas_eh_bus_reset_handler(struct scsi_cmnd *cmd) 462a9344e68SDarrick J. Wong { 463a9344e68SDarrick J. Wong struct domain_device *dev = cmd_to_domain_dev(cmd); 464ad689233SDarrick J. Wong struct sas_phy *phy = find_local_sas_phy(dev); 465ad689233SDarrick J. Wong int res; 466ad689233SDarrick J. Wong 467ad689233SDarrick J. Wong res = sas_phy_reset(phy, 1); 468ad689233SDarrick J. Wong if (res) 469a9344e68SDarrick J. Wong SAS_DPRINTK("Bus reset of %s failed 0x%x\n", 470ad689233SDarrick J. Wong phy->dev.kobj.k_name, 471ad689233SDarrick J. Wong res); 472ad689233SDarrick J. Wong if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE) 473ad689233SDarrick J. Wong return SUCCESS; 474ad689233SDarrick J. Wong 475ad689233SDarrick J. Wong return FAILED; 476ad689233SDarrick J. Wong } 477ad689233SDarrick J. Wong 478ad689233SDarrick J. Wong /* Try to reset a device */ 479ad689233SDarrick J. Wong static int try_to_reset_cmd_device(struct Scsi_Host *shost, 480ad689233SDarrick J. Wong struct scsi_cmnd *cmd) 481ad689233SDarrick J. Wong { 482a9344e68SDarrick J. Wong int res; 483ad689233SDarrick J. Wong 484a9344e68SDarrick J. Wong if (!shost->hostt->eh_device_reset_handler) 485a9344e68SDarrick J. Wong goto try_bus_reset; 486a9344e68SDarrick J. Wong 487a9344e68SDarrick J. Wong res = shost->hostt->eh_device_reset_handler(cmd); 488a9344e68SDarrick J. Wong if (res == SUCCESS) 489a9344e68SDarrick J. Wong return res; 490a9344e68SDarrick J. Wong 491a9344e68SDarrick J. Wong try_bus_reset: 492a9344e68SDarrick J. Wong if (shost->hostt->eh_bus_reset_handler) 493a9344e68SDarrick J. Wong return shost->hostt->eh_bus_reset_handler(cmd); 494a9344e68SDarrick J. Wong 495a9344e68SDarrick J. Wong return FAILED; 496ad689233SDarrick J. Wong } 497ad689233SDarrick J. Wong 498ad689233SDarrick J. Wong static int sas_eh_handle_sas_errors(struct Scsi_Host *shost, 499ad689233SDarrick J. Wong struct list_head *work_q, 500ad689233SDarrick J. Wong struct list_head *done_q) 501ad689233SDarrick J. Wong { 5022908d778SJames Bottomley struct scsi_cmnd *cmd, *n; 5032908d778SJames Bottomley enum task_disposition res = TASK_IS_DONE; 5043ebf6922SDarrick J. Wong int tmf_resp, need_reset; 5052908d778SJames Bottomley struct sas_internal *i = to_sas_internal(shost->transportt); 506ad689233SDarrick J. Wong unsigned long flags; 507ad689233SDarrick J. Wong struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost); 5082908d778SJames Bottomley 5092908d778SJames Bottomley Again: 510ad689233SDarrick J. Wong list_for_each_entry_safe(cmd, n, work_q, eh_entry) { 5112908d778SJames Bottomley struct sas_task *task = TO_SAS_TASK(cmd); 512f456393eSDarrick J. Wong 513ad689233SDarrick J. Wong if (!task) 514f456393eSDarrick J. Wong continue; 515ad689233SDarrick J. Wong 516ad689233SDarrick J. Wong list_del_init(&cmd->eh_entry); 5173ebf6922SDarrick J. Wong 5183ebf6922SDarrick J. Wong spin_lock_irqsave(&task->task_state_lock, flags); 5193ebf6922SDarrick J. Wong need_reset = task->task_state_flags & SAS_TASK_NEED_DEV_RESET; 5203ebf6922SDarrick J. Wong spin_unlock_irqrestore(&task->task_state_lock, flags); 5213ebf6922SDarrick J. Wong 522f456393eSDarrick J. Wong SAS_DPRINTK("trying to find task 0x%p\n", task); 5232908d778SJames Bottomley res = sas_scsi_find_task(task); 5242908d778SJames Bottomley 5252908d778SJames Bottomley cmd->eh_eflags = 0; 5262908d778SJames Bottomley 5272908d778SJames Bottomley switch (res) { 5282908d778SJames Bottomley case TASK_IS_DONE: 5292908d778SJames Bottomley SAS_DPRINTK("%s: task 0x%p is done\n", __FUNCTION__, 5302908d778SJames Bottomley task); 5312908d778SJames Bottomley task->task_done(task); 5323ebf6922SDarrick J. Wong if (need_reset) 533ad689233SDarrick J. Wong try_to_reset_cmd_device(shost, cmd); 5342908d778SJames Bottomley continue; 5352908d778SJames Bottomley case TASK_IS_ABORTED: 5362908d778SJames Bottomley SAS_DPRINTK("%s: task 0x%p is aborted\n", 5372908d778SJames Bottomley __FUNCTION__, task); 5382908d778SJames Bottomley task->task_done(task); 5393ebf6922SDarrick J. Wong if (need_reset) 540ad689233SDarrick J. Wong try_to_reset_cmd_device(shost, cmd); 5412908d778SJames Bottomley continue; 5422908d778SJames Bottomley case TASK_IS_AT_LU: 5432908d778SJames Bottomley SAS_DPRINTK("task 0x%p is at LU: lu recover\n", task); 5442908d778SJames Bottomley tmf_resp = sas_recover_lu(task->dev, cmd); 5452908d778SJames Bottomley if (tmf_resp == TMF_RESP_FUNC_COMPLETE) { 5462908d778SJames Bottomley SAS_DPRINTK("dev %016llx LU %x is " 5472908d778SJames Bottomley "recovered\n", 5482908d778SJames Bottomley SAS_ADDR(task->dev), 5492908d778SJames Bottomley cmd->device->lun); 5502908d778SJames Bottomley task->task_done(task); 5513ebf6922SDarrick J. Wong if (need_reset) 552ad689233SDarrick J. Wong try_to_reset_cmd_device(shost, cmd); 553ad689233SDarrick J. Wong sas_scsi_clear_queue_lu(work_q, cmd); 5542908d778SJames Bottomley goto Again; 5552908d778SJames Bottomley } 5562908d778SJames Bottomley /* fallthrough */ 5572908d778SJames Bottomley case TASK_IS_NOT_AT_LU: 55802cd743bSDarrick J. Wong case TASK_ABORT_FAILED: 5592908d778SJames Bottomley SAS_DPRINTK("task 0x%p is not at LU: I_T recover\n", 5602908d778SJames Bottomley task); 5612908d778SJames Bottomley tmf_resp = sas_recover_I_T(task->dev); 5622908d778SJames Bottomley if (tmf_resp == TMF_RESP_FUNC_COMPLETE) { 5632908d778SJames Bottomley SAS_DPRINTK("I_T %016llx recovered\n", 5642908d778SJames Bottomley SAS_ADDR(task->dev->sas_addr)); 5652908d778SJames Bottomley task->task_done(task); 5663ebf6922SDarrick J. Wong if (need_reset) 567ad689233SDarrick J. Wong try_to_reset_cmd_device(shost, cmd); 568ad689233SDarrick J. Wong sas_scsi_clear_queue_I_T(work_q, task->dev); 5692908d778SJames Bottomley goto Again; 5702908d778SJames Bottomley } 5712908d778SJames Bottomley /* Hammer time :-) */ 5722908d778SJames Bottomley if (i->dft->lldd_clear_nexus_port) { 5732908d778SJames Bottomley struct asd_sas_port *port = task->dev->port; 5742908d778SJames Bottomley SAS_DPRINTK("clearing nexus for port:%d\n", 5752908d778SJames Bottomley port->id); 5762908d778SJames Bottomley res = i->dft->lldd_clear_nexus_port(port); 5772908d778SJames Bottomley if (res == TMF_RESP_FUNC_COMPLETE) { 5782908d778SJames Bottomley SAS_DPRINTK("clear nexus port:%d " 5792908d778SJames Bottomley "succeeded\n", port->id); 5802908d778SJames Bottomley task->task_done(task); 5813ebf6922SDarrick J. Wong if (need_reset) 582ad689233SDarrick J. Wong try_to_reset_cmd_device(shost, cmd); 583ad689233SDarrick J. Wong sas_scsi_clear_queue_port(work_q, 5842908d778SJames Bottomley port); 5852908d778SJames Bottomley goto Again; 5862908d778SJames Bottomley } 5872908d778SJames Bottomley } 5882908d778SJames Bottomley if (i->dft->lldd_clear_nexus_ha) { 5892908d778SJames Bottomley SAS_DPRINTK("clear nexus ha\n"); 5902908d778SJames Bottomley res = i->dft->lldd_clear_nexus_ha(ha); 5912908d778SJames Bottomley if (res == TMF_RESP_FUNC_COMPLETE) { 5922908d778SJames Bottomley SAS_DPRINTK("clear nexus ha " 5932908d778SJames Bottomley "succeeded\n"); 5942908d778SJames Bottomley task->task_done(task); 5953ebf6922SDarrick J. Wong if (need_reset) 596ad689233SDarrick J. Wong try_to_reset_cmd_device(shost, cmd); 5972908d778SJames Bottomley goto out; 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 6092908d778SJames Bottomley task->task_done(task); 6103ebf6922SDarrick J. Wong if (need_reset) 611ad689233SDarrick J. Wong try_to_reset_cmd_device(shost, cmd); 6122908d778SJames Bottomley goto clear_q; 6132908d778SJames Bottomley } 6142908d778SJames Bottomley } 6152908d778SJames Bottomley out: 616ad689233SDarrick J. Wong return list_empty(work_q); 6172908d778SJames Bottomley clear_q: 6182908d778SJames Bottomley SAS_DPRINTK("--- Exit %s -- clear_q\n", __FUNCTION__); 619ad689233SDarrick J. Wong list_for_each_entry_safe(cmd, n, work_q, eh_entry) { 6202908d778SJames Bottomley struct sas_task *task = TO_SAS_TASK(cmd); 6212908d778SJames Bottomley list_del_init(&cmd->eh_entry); 6222908d778SJames Bottomley task->task_done(task); 6232908d778SJames Bottomley } 624ad689233SDarrick J. Wong return list_empty(work_q); 625ad689233SDarrick J. Wong } 626ad689233SDarrick J. Wong 627ad689233SDarrick J. Wong void sas_scsi_recover_host(struct Scsi_Host *shost) 628ad689233SDarrick J. Wong { 629ad689233SDarrick J. Wong struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost); 630ad689233SDarrick J. Wong unsigned long flags; 631ad689233SDarrick J. Wong LIST_HEAD(eh_work_q); 632ad689233SDarrick J. Wong 633ad689233SDarrick J. Wong spin_lock_irqsave(shost->host_lock, flags); 634ad689233SDarrick J. Wong list_splice_init(&shost->eh_cmd_q, &eh_work_q); 635ad689233SDarrick J. Wong spin_unlock_irqrestore(shost->host_lock, flags); 636ad689233SDarrick J. Wong 637ad689233SDarrick J. Wong SAS_DPRINTK("Enter %s\n", __FUNCTION__); 638ad689233SDarrick J. Wong /* 639ad689233SDarrick J. Wong * Deal with commands that still have SAS tasks (i.e. they didn't 640ad689233SDarrick J. Wong * complete via the normal sas_task completion mechanism) 641ad689233SDarrick J. Wong */ 642ad689233SDarrick J. Wong if (sas_eh_handle_sas_errors(shost, &eh_work_q, &ha->eh_done_q)) 643ad689233SDarrick J. Wong goto out; 644ad689233SDarrick J. Wong 645ad689233SDarrick J. Wong /* 646ad689233SDarrick J. Wong * Now deal with SCSI commands that completed ok but have a an error 647ad689233SDarrick J. Wong * code (and hopefully sense data) attached. This is roughly what 648ad689233SDarrick J. Wong * scsi_unjam_host does, but we skip scsi_eh_abort_cmds because any 649ad689233SDarrick J. Wong * command we see here has no sas_task and is thus unknown to the HA. 650ad689233SDarrick J. Wong */ 651ad689233SDarrick J. Wong if (!scsi_eh_get_sense(&eh_work_q, &ha->eh_done_q)) 652ad689233SDarrick J. Wong scsi_eh_ready_devs(shost, &eh_work_q, &ha->eh_done_q); 653ad689233SDarrick J. Wong 654ad689233SDarrick J. Wong out: 655ad689233SDarrick J. Wong scsi_eh_flush_done_q(&ha->eh_done_q); 656ad689233SDarrick J. Wong SAS_DPRINTK("--- Exit %s\n", __FUNCTION__); 657ad689233SDarrick J. Wong return; 6582908d778SJames Bottomley } 6592908d778SJames Bottomley 6602908d778SJames Bottomley enum scsi_eh_timer_return sas_scsi_timed_out(struct scsi_cmnd *cmd) 6612908d778SJames Bottomley { 6622908d778SJames Bottomley struct sas_task *task = TO_SAS_TASK(cmd); 6632908d778SJames Bottomley unsigned long flags; 6642908d778SJames Bottomley 6652908d778SJames Bottomley if (!task) { 6666d4dcd4dSDarrick J. Wong cmd->timeout_per_command /= 2; 6676d4dcd4dSDarrick J. Wong SAS_DPRINTK("command 0x%p, task 0x%p, gone: %s\n", 6686d4dcd4dSDarrick J. Wong cmd, task, (cmd->timeout_per_command ? 6696d4dcd4dSDarrick J. Wong "EH_RESET_TIMER" : "EH_NOT_HANDLED")); 6706d4dcd4dSDarrick J. Wong if (!cmd->timeout_per_command) 6716d4dcd4dSDarrick J. Wong return EH_NOT_HANDLED; 6726d4dcd4dSDarrick J. Wong return EH_RESET_TIMER; 6732908d778SJames Bottomley } 6742908d778SJames Bottomley 6752908d778SJames Bottomley spin_lock_irqsave(&task->task_state_lock, flags); 676396819fbSDarrick J. Wong BUG_ON(task->task_state_flags & SAS_TASK_STATE_ABORTED); 6772908d778SJames Bottomley if (task->task_state_flags & SAS_TASK_STATE_DONE) { 6782908d778SJames Bottomley spin_unlock_irqrestore(&task->task_state_lock, flags); 6792908d778SJames Bottomley SAS_DPRINTK("command 0x%p, task 0x%p, timed out: EH_HANDLED\n", 6802908d778SJames Bottomley cmd, task); 6812908d778SJames Bottomley return EH_HANDLED; 6822908d778SJames Bottomley } 683b218a0d8SDarrick J. Wong if (!(task->task_state_flags & SAS_TASK_AT_INITIATOR)) { 684b218a0d8SDarrick J. Wong spin_unlock_irqrestore(&task->task_state_lock, flags); 685b218a0d8SDarrick J. Wong SAS_DPRINTK("command 0x%p, task 0x%p, not at initiator: " 686b218a0d8SDarrick J. Wong "EH_RESET_TIMER\n", 687b218a0d8SDarrick J. Wong cmd, task); 688b218a0d8SDarrick J. Wong return EH_RESET_TIMER; 689b218a0d8SDarrick J. Wong } 6902908d778SJames Bottomley task->task_state_flags |= SAS_TASK_STATE_ABORTED; 6912908d778SJames Bottomley spin_unlock_irqrestore(&task->task_state_lock, flags); 6922908d778SJames Bottomley 6932908d778SJames Bottomley SAS_DPRINTK("command 0x%p, task 0x%p, timed out: EH_NOT_HANDLED\n", 6942908d778SJames Bottomley cmd, task); 6952908d778SJames Bottomley 6962908d778SJames Bottomley return EH_NOT_HANDLED; 6972908d778SJames Bottomley } 6982908d778SJames Bottomley 699fa1c1e8fSDarrick J. Wong 700fa1c1e8fSDarrick J. Wong static enum ata_completion_errors sas_to_ata_err(struct task_status_struct *ts) 701fa1c1e8fSDarrick J. Wong { 702fa1c1e8fSDarrick J. Wong /* Cheesy attempt to translate SAS errors into ATA. Hah! */ 703fa1c1e8fSDarrick J. Wong 704fa1c1e8fSDarrick J. Wong /* transport error */ 705fa1c1e8fSDarrick J. Wong if (ts->resp == SAS_TASK_UNDELIVERED) 706fa1c1e8fSDarrick J. Wong return AC_ERR_ATA_BUS; 707fa1c1e8fSDarrick J. Wong 708fa1c1e8fSDarrick J. Wong /* ts->resp == SAS_TASK_COMPLETE */ 709fa1c1e8fSDarrick J. Wong /* task delivered, what happened afterwards? */ 710fa1c1e8fSDarrick J. Wong switch (ts->stat) { 711fa1c1e8fSDarrick J. Wong case SAS_DEV_NO_RESPONSE: 712fa1c1e8fSDarrick J. Wong return AC_ERR_TIMEOUT; 713fa1c1e8fSDarrick J. Wong 714fa1c1e8fSDarrick J. Wong case SAS_INTERRUPTED: 715fa1c1e8fSDarrick J. Wong case SAS_PHY_DOWN: 716fa1c1e8fSDarrick J. Wong case SAS_NAK_R_ERR: 717fa1c1e8fSDarrick J. Wong return AC_ERR_ATA_BUS; 718fa1c1e8fSDarrick J. Wong 719fa1c1e8fSDarrick J. Wong 720fa1c1e8fSDarrick J. Wong case SAS_DATA_UNDERRUN: 721fa1c1e8fSDarrick J. Wong /* 722fa1c1e8fSDarrick J. Wong * Some programs that use the taskfile interface 723fa1c1e8fSDarrick J. Wong * (smartctl in particular) can cause underrun 724fa1c1e8fSDarrick J. Wong * problems. Ignore these errors, perhaps at our 725fa1c1e8fSDarrick J. Wong * peril. 726fa1c1e8fSDarrick J. Wong */ 727fa1c1e8fSDarrick J. Wong return 0; 728fa1c1e8fSDarrick J. Wong 729fa1c1e8fSDarrick J. Wong case SAS_DATA_OVERRUN: 730fa1c1e8fSDarrick J. Wong case SAS_QUEUE_FULL: 731fa1c1e8fSDarrick J. Wong case SAS_DEVICE_UNKNOWN: 732fa1c1e8fSDarrick J. Wong case SAS_SG_ERR: 733fa1c1e8fSDarrick J. Wong return AC_ERR_INVALID; 734fa1c1e8fSDarrick J. Wong 735fa1c1e8fSDarrick J. Wong case SAM_CHECK_COND: 736fa1c1e8fSDarrick J. Wong case SAS_OPEN_TO: 737fa1c1e8fSDarrick J. Wong case SAS_OPEN_REJECT: 738fa1c1e8fSDarrick J. Wong case SAS_PROTO_RESPONSE: 739fa1c1e8fSDarrick J. Wong SAS_DPRINTK("%s: Saw error %d. What to do?\n", 740fa1c1e8fSDarrick J. Wong __FUNCTION__, ts->stat); 741fa1c1e8fSDarrick J. Wong return AC_ERR_OTHER; 742fa1c1e8fSDarrick J. Wong 743fa1c1e8fSDarrick J. Wong case SAS_ABORTED_TASK: 744fa1c1e8fSDarrick J. Wong return AC_ERR_DEV; 745fa1c1e8fSDarrick J. Wong 746fa1c1e8fSDarrick J. Wong default: 747fa1c1e8fSDarrick J. Wong return 0; 748fa1c1e8fSDarrick J. Wong } 749fa1c1e8fSDarrick J. Wong } 750fa1c1e8fSDarrick J. Wong 751fa1c1e8fSDarrick J. Wong static void sas_ata_task_done(struct sas_task *task) 752fa1c1e8fSDarrick J. Wong { 753fa1c1e8fSDarrick J. Wong struct ata_queued_cmd *qc = task->uldd_task; 754fa1c1e8fSDarrick J. Wong struct domain_device *dev = qc->ap->private_data; 755fa1c1e8fSDarrick J. Wong struct task_status_struct *stat = &task->task_status; 756fa1c1e8fSDarrick J. Wong struct ata_task_resp *resp = (struct ata_task_resp *)stat->buf; 757fa1c1e8fSDarrick J. Wong enum ata_completion_errors ac; 758fa1c1e8fSDarrick J. Wong 759fa1c1e8fSDarrick J. Wong ac = sas_to_ata_err(stat); 760fa1c1e8fSDarrick J. Wong if (ac) { 761fa1c1e8fSDarrick J. Wong SAS_DPRINTK("%s: SAS error %x\n", __FUNCTION__, stat->stat); 762fa1c1e8fSDarrick J. Wong /* We saw a SAS error. Send a vague error. */ 763fa1c1e8fSDarrick J. Wong qc->err_mask = ac; 764fa1c1e8fSDarrick J. Wong dev->sata_dev.tf.feature = 0x04; /* status err */ 765fa1c1e8fSDarrick J. Wong dev->sata_dev.tf.command = ATA_ERR; 766fa1c1e8fSDarrick J. Wong goto end; 767fa1c1e8fSDarrick J. Wong } 768fa1c1e8fSDarrick J. Wong 769fa1c1e8fSDarrick J. Wong ata_tf_from_fis(resp->ending_fis, &dev->sata_dev.tf); 770fa1c1e8fSDarrick J. Wong qc->err_mask |= ac_err_mask(dev->sata_dev.tf.command); 771fa1c1e8fSDarrick J. Wong dev->sata_dev.sstatus = resp->sstatus; 772fa1c1e8fSDarrick J. Wong dev->sata_dev.serror = resp->serror; 773fa1c1e8fSDarrick J. Wong dev->sata_dev.scontrol = resp->scontrol; 774fa1c1e8fSDarrick J. Wong dev->sata_dev.ap->sactive = resp->sactive; 775fa1c1e8fSDarrick J. Wong end: 776fa1c1e8fSDarrick J. Wong ata_qc_complete(qc); 777fa1c1e8fSDarrick J. Wong list_del_init(&task->list); 778fa1c1e8fSDarrick J. Wong sas_free_task(task); 779fa1c1e8fSDarrick J. Wong } 780fa1c1e8fSDarrick J. Wong 781fa1c1e8fSDarrick J. Wong int sas_ioctl(struct scsi_device *sdev, int cmd, void __user *arg) 782fa1c1e8fSDarrick J. Wong { 783fa1c1e8fSDarrick J. Wong struct domain_device *dev = sdev_to_domain_dev(sdev); 784fa1c1e8fSDarrick J. Wong 785fa1c1e8fSDarrick J. Wong if (dev_is_sata(dev)) 786fa1c1e8fSDarrick J. Wong return ata_scsi_ioctl(sdev, cmd, arg); 787fa1c1e8fSDarrick J. Wong 788fa1c1e8fSDarrick J. Wong return -EINVAL; 789fa1c1e8fSDarrick J. Wong } 790fa1c1e8fSDarrick J. Wong 791fa1c1e8fSDarrick J. Wong static unsigned int sas_ata_qc_issue(struct ata_queued_cmd *qc) 792fa1c1e8fSDarrick J. Wong { 793fa1c1e8fSDarrick J. Wong int res = -ENOMEM; 794fa1c1e8fSDarrick J. Wong struct sas_task *task; 795fa1c1e8fSDarrick J. Wong struct domain_device *dev = qc->ap->private_data; 796fa1c1e8fSDarrick J. Wong struct sas_ha_struct *sas_ha = dev->port->ha; 797fa1c1e8fSDarrick J. Wong struct Scsi_Host *host = sas_ha->core.shost; 798fa1c1e8fSDarrick J. Wong struct sas_internal *i = to_sas_internal(host->transportt); 799fa1c1e8fSDarrick J. Wong struct scatterlist *sg; 800fa1c1e8fSDarrick J. Wong unsigned int num = 0; 801fa1c1e8fSDarrick J. Wong unsigned int xfer = 0; 802fa1c1e8fSDarrick J. Wong 803fa1c1e8fSDarrick J. Wong task = sas_alloc_task(GFP_ATOMIC); 804fa1c1e8fSDarrick J. Wong if (!task) 805fa1c1e8fSDarrick J. Wong goto out; 806fa1c1e8fSDarrick J. Wong task->dev = dev; 807fa1c1e8fSDarrick J. Wong task->task_proto = SAS_PROTOCOL_STP; 808fa1c1e8fSDarrick J. Wong task->task_done = sas_ata_task_done; 809fa1c1e8fSDarrick J. Wong 810fa1c1e8fSDarrick J. Wong ata_tf_to_fis(&qc->tf, (u8*)&task->ata_task.fis, 0); 811fa1c1e8fSDarrick J. Wong task->uldd_task = qc; 812fa1c1e8fSDarrick J. Wong if (is_atapi_taskfile(&qc->tf)) { 813fa1c1e8fSDarrick J. Wong memcpy(task->ata_task.atapi_packet, qc->cdb, ATAPI_CDB_LEN); 814fa1c1e8fSDarrick J. Wong task->total_xfer_len = qc->nbytes + qc->pad_len; 815fa1c1e8fSDarrick J. Wong task->num_scatter = qc->pad_len ? qc->n_elem + 1 : qc->n_elem; 816fa1c1e8fSDarrick J. Wong } else { 817fa1c1e8fSDarrick J. Wong ata_for_each_sg(sg, qc) { 818fa1c1e8fSDarrick J. Wong num++; 819fa1c1e8fSDarrick J. Wong xfer += sg->length; 820fa1c1e8fSDarrick J. Wong } 821fa1c1e8fSDarrick J. Wong 822fa1c1e8fSDarrick J. Wong task->total_xfer_len = xfer; 823fa1c1e8fSDarrick J. Wong task->num_scatter = num; 824fa1c1e8fSDarrick J. Wong } 825fa1c1e8fSDarrick J. Wong 826fa1c1e8fSDarrick J. Wong task->data_dir = qc->dma_dir; 827fa1c1e8fSDarrick J. Wong task->scatter = qc->__sg; 828fa1c1e8fSDarrick J. Wong task->ata_task.retry_count = 1; 829fa1c1e8fSDarrick J. Wong task->task_state_flags = SAS_TASK_STATE_PENDING; 830fa1c1e8fSDarrick J. Wong 831fa1c1e8fSDarrick J. Wong if (qc->tf.protocol == ATA_PROT_DMA) 832fa1c1e8fSDarrick J. Wong task->ata_task.dma_xfer = 1; 833fa1c1e8fSDarrick J. Wong 834fa1c1e8fSDarrick J. Wong if (sas_ha->lldd_max_execute_num < 2) 835fa1c1e8fSDarrick J. Wong res = i->dft->lldd_execute_task(task, 1, GFP_ATOMIC); 836fa1c1e8fSDarrick J. Wong else 837fa1c1e8fSDarrick J. Wong res = sas_queue_up(task); 838fa1c1e8fSDarrick J. Wong 839fa1c1e8fSDarrick J. Wong /* Examine */ 840fa1c1e8fSDarrick J. Wong if (res) { 841fa1c1e8fSDarrick J. Wong SAS_DPRINTK("lldd_execute_task returned: %d\n", res); 842fa1c1e8fSDarrick J. Wong 843fa1c1e8fSDarrick J. Wong sas_free_task(task); 844fa1c1e8fSDarrick J. Wong if (res == -SAS_QUEUE_FULL) 845fa1c1e8fSDarrick J. Wong return -ENOMEM; 846fa1c1e8fSDarrick J. Wong } 847fa1c1e8fSDarrick J. Wong 848fa1c1e8fSDarrick J. Wong out: 849fa1c1e8fSDarrick J. Wong return res; 850fa1c1e8fSDarrick J. Wong } 851fa1c1e8fSDarrick J. Wong 852fa1c1e8fSDarrick J. Wong static u8 sas_ata_check_status(struct ata_port *ap) 853fa1c1e8fSDarrick J. Wong { 854fa1c1e8fSDarrick J. Wong struct domain_device *dev = ap->private_data; 855fa1c1e8fSDarrick J. Wong return dev->sata_dev.tf.command; 856fa1c1e8fSDarrick J. Wong } 857fa1c1e8fSDarrick J. Wong 858fa1c1e8fSDarrick J. Wong static void sas_ata_phy_reset(struct ata_port *ap) 859fa1c1e8fSDarrick J. Wong { 860fa1c1e8fSDarrick J. Wong struct domain_device *dev = ap->private_data; 861fa1c1e8fSDarrick J. Wong struct sas_internal *i = 862fa1c1e8fSDarrick J. Wong to_sas_internal(dev->port->ha->core.shost->transportt); 863fa1c1e8fSDarrick J. Wong int res = 0; 864fa1c1e8fSDarrick J. Wong 865fa1c1e8fSDarrick J. Wong if (i->dft->lldd_I_T_nexus_reset) 866fa1c1e8fSDarrick J. Wong res = i->dft->lldd_I_T_nexus_reset(dev); 867fa1c1e8fSDarrick J. Wong 868fa1c1e8fSDarrick J. Wong if (res) 869fa1c1e8fSDarrick J. Wong SAS_DPRINTK("%s: Unable to reset I T nexus?\n", __FUNCTION__); 870fa1c1e8fSDarrick J. Wong 871fa1c1e8fSDarrick J. Wong switch (dev->sata_dev.command_set) { 872fa1c1e8fSDarrick J. Wong case ATA_COMMAND_SET: 873fa1c1e8fSDarrick J. Wong SAS_DPRINTK("%s: Found ATA device.\n", __FUNCTION__); 874fa1c1e8fSDarrick J. Wong ap->device[0].class = ATA_DEV_ATA; 875fa1c1e8fSDarrick J. Wong break; 876fa1c1e8fSDarrick J. Wong case ATAPI_COMMAND_SET: 877fa1c1e8fSDarrick J. Wong SAS_DPRINTK("%s: Found ATAPI device.\n", __FUNCTION__); 878fa1c1e8fSDarrick J. Wong ap->device[0].class = ATA_DEV_ATAPI; 879fa1c1e8fSDarrick J. Wong break; 880fa1c1e8fSDarrick J. Wong default: 881fa1c1e8fSDarrick J. Wong SAS_DPRINTK("%s: Unknown SATA command set: %d.\n", 882fa1c1e8fSDarrick J. Wong __FUNCTION__, 883fa1c1e8fSDarrick J. Wong dev->sata_dev.command_set); 884fa1c1e8fSDarrick J. Wong ap->device[0].class = ATA_DEV_ATA; 885fa1c1e8fSDarrick J. Wong break; 886fa1c1e8fSDarrick J. Wong } 887fa1c1e8fSDarrick J. Wong 888fa1c1e8fSDarrick J. Wong ap->cbl = ATA_CBL_SATA; 889fa1c1e8fSDarrick J. Wong } 890fa1c1e8fSDarrick J. Wong 891fa1c1e8fSDarrick J. Wong static void sas_ata_post_internal(struct ata_queued_cmd *qc) 892fa1c1e8fSDarrick J. Wong { 893fa1c1e8fSDarrick J. Wong if (qc->flags & ATA_QCFLAG_FAILED) 894fa1c1e8fSDarrick J. Wong qc->err_mask |= AC_ERR_OTHER; 895fa1c1e8fSDarrick J. Wong 896fa1c1e8fSDarrick J. Wong if (qc->err_mask) 897fa1c1e8fSDarrick J. Wong SAS_DPRINTK("%s: Failure; reset phy!\n", __FUNCTION__); 898fa1c1e8fSDarrick J. Wong } 899fa1c1e8fSDarrick J. Wong 900fa1c1e8fSDarrick J. Wong static void sas_ata_tf_read(struct ata_port *ap, struct ata_taskfile *tf) 901fa1c1e8fSDarrick J. Wong { 902fa1c1e8fSDarrick J. Wong struct domain_device *dev = ap->private_data; 903fa1c1e8fSDarrick J. Wong memcpy(tf, &dev->sata_dev.tf, sizeof (*tf)); 904fa1c1e8fSDarrick J. Wong } 905fa1c1e8fSDarrick J. Wong 906fa1c1e8fSDarrick J. Wong static void sas_ata_scr_write(struct ata_port *ap, unsigned int sc_reg_in, 907fa1c1e8fSDarrick J. Wong u32 val) 908fa1c1e8fSDarrick J. Wong { 909fa1c1e8fSDarrick J. Wong struct domain_device *dev = ap->private_data; 910fa1c1e8fSDarrick J. Wong 911fa1c1e8fSDarrick J. Wong SAS_DPRINTK("STUB %s\n", __FUNCTION__); 912fa1c1e8fSDarrick J. Wong switch (sc_reg_in) { 913fa1c1e8fSDarrick J. Wong case SCR_STATUS: 914fa1c1e8fSDarrick J. Wong dev->sata_dev.sstatus = val; 915fa1c1e8fSDarrick J. Wong break; 916fa1c1e8fSDarrick J. Wong case SCR_CONTROL: 917fa1c1e8fSDarrick J. Wong dev->sata_dev.scontrol = val; 918fa1c1e8fSDarrick J. Wong break; 919fa1c1e8fSDarrick J. Wong case SCR_ERROR: 920fa1c1e8fSDarrick J. Wong dev->sata_dev.serror = val; 921fa1c1e8fSDarrick J. Wong break; 922fa1c1e8fSDarrick J. Wong case SCR_ACTIVE: 923fa1c1e8fSDarrick J. Wong dev->sata_dev.ap->sactive = val; 924fa1c1e8fSDarrick J. Wong break; 925fa1c1e8fSDarrick J. Wong } 926fa1c1e8fSDarrick J. Wong } 927fa1c1e8fSDarrick J. Wong 928fa1c1e8fSDarrick J. Wong static u32 sas_ata_scr_read(struct ata_port *ap, unsigned int sc_reg_in) 929fa1c1e8fSDarrick J. Wong { 930fa1c1e8fSDarrick J. Wong struct domain_device *dev = ap->private_data; 931fa1c1e8fSDarrick J. Wong 932fa1c1e8fSDarrick J. Wong SAS_DPRINTK("STUB %s\n", __FUNCTION__); 933fa1c1e8fSDarrick J. Wong switch (sc_reg_in) { 934fa1c1e8fSDarrick J. Wong case SCR_STATUS: 935fa1c1e8fSDarrick J. Wong return dev->sata_dev.sstatus; 936fa1c1e8fSDarrick J. Wong case SCR_CONTROL: 937fa1c1e8fSDarrick J. Wong return dev->sata_dev.scontrol; 938fa1c1e8fSDarrick J. Wong case SCR_ERROR: 939fa1c1e8fSDarrick J. Wong return dev->sata_dev.serror; 940fa1c1e8fSDarrick J. Wong case SCR_ACTIVE: 941fa1c1e8fSDarrick J. Wong return dev->sata_dev.ap->sactive; 942fa1c1e8fSDarrick J. Wong default: 943fa1c1e8fSDarrick J. Wong return 0xffffffffU; 944fa1c1e8fSDarrick J. Wong } 945fa1c1e8fSDarrick J. Wong } 946fa1c1e8fSDarrick J. Wong 947fa1c1e8fSDarrick J. Wong static struct ata_port_operations sas_sata_ops = { 948fa1c1e8fSDarrick J. Wong .port_disable = ata_port_disable, 949fa1c1e8fSDarrick J. Wong .check_status = sas_ata_check_status, 950fa1c1e8fSDarrick J. Wong .check_altstatus = sas_ata_check_status, 951fa1c1e8fSDarrick J. Wong .dev_select = ata_noop_dev_select, 952fa1c1e8fSDarrick J. Wong .phy_reset = sas_ata_phy_reset, 953fa1c1e8fSDarrick J. Wong .post_internal_cmd = sas_ata_post_internal, 954fa1c1e8fSDarrick J. Wong .tf_read = sas_ata_tf_read, 955fa1c1e8fSDarrick J. Wong .qc_prep = ata_noop_qc_prep, 956fa1c1e8fSDarrick J. Wong .qc_issue = sas_ata_qc_issue, 957fa1c1e8fSDarrick J. Wong .port_start = ata_sas_port_start, 958fa1c1e8fSDarrick J. Wong .port_stop = ata_sas_port_stop, 959fa1c1e8fSDarrick J. Wong .scr_read = sas_ata_scr_read, 960fa1c1e8fSDarrick J. Wong .scr_write = sas_ata_scr_write 961fa1c1e8fSDarrick J. Wong }; 962fa1c1e8fSDarrick J. Wong 963fa1c1e8fSDarrick J. Wong static struct ata_port_info sata_port_info = { 964fa1c1e8fSDarrick J. Wong .flags = ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY | ATA_FLAG_SATA_RESET | 965fa1c1e8fSDarrick J. Wong ATA_FLAG_MMIO | ATA_FLAG_PIO_DMA, 966fa1c1e8fSDarrick J. Wong .pio_mask = 0x1f, /* PIO0-4 */ 967fa1c1e8fSDarrick J. Wong .mwdma_mask = 0x07, /* MWDMA0-2 */ 968fa1c1e8fSDarrick J. Wong .udma_mask = ATA_UDMA6, 969fa1c1e8fSDarrick J. Wong .port_ops = &sas_sata_ops 970fa1c1e8fSDarrick J. Wong }; 971fa1c1e8fSDarrick J. Wong 9722908d778SJames Bottomley struct domain_device *sas_find_dev_by_rphy(struct sas_rphy *rphy) 9732908d778SJames Bottomley { 9742908d778SJames Bottomley struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent); 9752908d778SJames Bottomley struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost); 9762908d778SJames Bottomley struct domain_device *found_dev = NULL; 9772908d778SJames Bottomley int i; 978980fa2f9SDarrick J. Wong unsigned long flags; 9792908d778SJames Bottomley 980980fa2f9SDarrick J. Wong spin_lock_irqsave(&ha->phy_port_lock, flags); 9812908d778SJames Bottomley for (i = 0; i < ha->num_phys; i++) { 9822908d778SJames Bottomley struct asd_sas_port *port = ha->sas_port[i]; 9832908d778SJames Bottomley struct domain_device *dev; 9842908d778SJames Bottomley 9852908d778SJames Bottomley spin_lock(&port->dev_list_lock); 9862908d778SJames Bottomley list_for_each_entry(dev, &port->dev_list, dev_list_node) { 9872908d778SJames Bottomley if (rphy == dev->rphy) { 9882908d778SJames Bottomley found_dev = dev; 9892908d778SJames Bottomley spin_unlock(&port->dev_list_lock); 9902908d778SJames Bottomley goto found; 9912908d778SJames Bottomley } 9922908d778SJames Bottomley } 9932908d778SJames Bottomley spin_unlock(&port->dev_list_lock); 9942908d778SJames Bottomley } 9952908d778SJames Bottomley found: 996980fa2f9SDarrick J. Wong spin_unlock_irqrestore(&ha->phy_port_lock, flags); 9972908d778SJames Bottomley 9982908d778SJames Bottomley return found_dev; 9992908d778SJames Bottomley } 10002908d778SJames Bottomley 10012908d778SJames Bottomley static inline struct domain_device *sas_find_target(struct scsi_target *starget) 10022908d778SJames Bottomley { 10032908d778SJames Bottomley struct sas_rphy *rphy = dev_to_rphy(starget->dev.parent); 10042908d778SJames Bottomley 10052908d778SJames Bottomley return sas_find_dev_by_rphy(rphy); 10062908d778SJames Bottomley } 10072908d778SJames Bottomley 10082908d778SJames Bottomley int sas_target_alloc(struct scsi_target *starget) 10092908d778SJames Bottomley { 1010fa1c1e8fSDarrick J. Wong struct Scsi_Host *shost = dev_to_shost(&starget->dev); 1011fa1c1e8fSDarrick J. Wong struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost); 10122908d778SJames Bottomley struct domain_device *found_dev = sas_find_target(starget); 10132908d778SJames Bottomley 10142908d778SJames Bottomley if (!found_dev) 10152908d778SJames Bottomley return -ENODEV; 10162908d778SJames Bottomley 1017fa1c1e8fSDarrick J. Wong if (dev_is_sata(found_dev)) { 1018fa1c1e8fSDarrick J. Wong struct ata_port *ap; 1019fa1c1e8fSDarrick J. Wong 1020fa1c1e8fSDarrick J. Wong ata_host_init(&found_dev->sata_dev.ata_host, 1021fa1c1e8fSDarrick J. Wong &ha->pcidev->dev, 1022fa1c1e8fSDarrick J. Wong sata_port_info.flags, 1023fa1c1e8fSDarrick J. Wong &sas_sata_ops); 1024fa1c1e8fSDarrick J. Wong ap = ata_sas_port_alloc(&found_dev->sata_dev.ata_host, 1025fa1c1e8fSDarrick J. Wong &sata_port_info, 1026fa1c1e8fSDarrick J. Wong shost); 1027fa1c1e8fSDarrick J. Wong if (!ap) { 1028fa1c1e8fSDarrick J. Wong SAS_DPRINTK("ata_sas_port_alloc failed.\n"); 1029fa1c1e8fSDarrick J. Wong return -ENODEV; 1030fa1c1e8fSDarrick J. Wong } 1031fa1c1e8fSDarrick J. Wong 1032fa1c1e8fSDarrick J. Wong ap->private_data = found_dev; 1033fa1c1e8fSDarrick J. Wong ap->cbl = ATA_CBL_SATA; 1034fa1c1e8fSDarrick J. Wong found_dev->sata_dev.ap = ap; 1035fa1c1e8fSDarrick J. Wong } 1036fa1c1e8fSDarrick J. Wong 10372908d778SJames Bottomley starget->hostdata = found_dev; 10382908d778SJames Bottomley return 0; 10392908d778SJames Bottomley } 10402908d778SJames Bottomley 10412908d778SJames Bottomley #define SAS_DEF_QD 32 10422908d778SJames Bottomley #define SAS_MAX_QD 64 10432908d778SJames Bottomley 10442908d778SJames Bottomley int sas_slave_configure(struct scsi_device *scsi_dev) 10452908d778SJames Bottomley { 10462908d778SJames Bottomley struct domain_device *dev = sdev_to_domain_dev(scsi_dev); 10472908d778SJames Bottomley struct sas_ha_struct *sas_ha; 10482908d778SJames Bottomley 10492908d778SJames Bottomley BUG_ON(dev->rphy->identify.device_type != SAS_END_DEVICE); 10502908d778SJames Bottomley 1051fa1c1e8fSDarrick J. Wong if (dev_is_sata(dev)) { 1052fa1c1e8fSDarrick J. Wong ata_sas_slave_configure(scsi_dev, dev->sata_dev.ap); 1053fa1c1e8fSDarrick J. Wong return 0; 1054fa1c1e8fSDarrick J. Wong } 1055fa1c1e8fSDarrick J. Wong 10562908d778SJames Bottomley sas_ha = dev->port->ha; 10572908d778SJames Bottomley 10582908d778SJames Bottomley sas_read_port_mode_page(scsi_dev); 10592908d778SJames Bottomley 10602908d778SJames Bottomley if (scsi_dev->tagged_supported) { 10612908d778SJames Bottomley scsi_set_tag_type(scsi_dev, MSG_SIMPLE_TAG); 10622908d778SJames Bottomley scsi_activate_tcq(scsi_dev, SAS_DEF_QD); 10632908d778SJames Bottomley } else { 10642908d778SJames Bottomley SAS_DPRINTK("device %llx, LUN %x doesn't support " 10652908d778SJames Bottomley "TCQ\n", SAS_ADDR(dev->sas_addr), 10662908d778SJames Bottomley scsi_dev->lun); 10672908d778SJames Bottomley scsi_dev->tagged_supported = 0; 10682908d778SJames Bottomley scsi_set_tag_type(scsi_dev, 0); 10692908d778SJames Bottomley scsi_deactivate_tcq(scsi_dev, 1); 10702908d778SJames Bottomley } 10712908d778SJames Bottomley 1072f27708fcSDarrick J. Wong scsi_dev->allow_restart = 1; 1073f27708fcSDarrick J. Wong 10742908d778SJames Bottomley return 0; 10752908d778SJames Bottomley } 10762908d778SJames Bottomley 10772908d778SJames Bottomley void sas_slave_destroy(struct scsi_device *scsi_dev) 10782908d778SJames Bottomley { 1079fa1c1e8fSDarrick J. Wong struct domain_device *dev = sdev_to_domain_dev(scsi_dev); 1080fa1c1e8fSDarrick J. Wong 1081fa1c1e8fSDarrick J. Wong if (dev_is_sata(dev)) 1082fa1c1e8fSDarrick J. Wong ata_port_disable(dev->sata_dev.ap); 10832908d778SJames Bottomley } 10842908d778SJames Bottomley 10852908d778SJames Bottomley int sas_change_queue_depth(struct scsi_device *scsi_dev, int new_depth) 10862908d778SJames Bottomley { 10872908d778SJames Bottomley int res = min(new_depth, SAS_MAX_QD); 10882908d778SJames Bottomley 10892908d778SJames Bottomley if (scsi_dev->tagged_supported) 10902908d778SJames Bottomley scsi_adjust_queue_depth(scsi_dev, scsi_get_tag_type(scsi_dev), 10912908d778SJames Bottomley res); 10922908d778SJames Bottomley else { 10932908d778SJames Bottomley struct domain_device *dev = sdev_to_domain_dev(scsi_dev); 10942908d778SJames Bottomley sas_printk("device %llx LUN %x queue depth changed to 1\n", 10952908d778SJames Bottomley SAS_ADDR(dev->sas_addr), 10962908d778SJames Bottomley scsi_dev->lun); 10972908d778SJames Bottomley scsi_adjust_queue_depth(scsi_dev, 0, 1); 10982908d778SJames Bottomley res = 1; 10992908d778SJames Bottomley } 11002908d778SJames Bottomley 11012908d778SJames Bottomley return res; 11022908d778SJames Bottomley } 11032908d778SJames Bottomley 11042908d778SJames Bottomley int sas_change_queue_type(struct scsi_device *scsi_dev, int qt) 11052908d778SJames Bottomley { 11062908d778SJames Bottomley if (!scsi_dev->tagged_supported) 11072908d778SJames Bottomley return 0; 11082908d778SJames Bottomley 11092908d778SJames Bottomley scsi_deactivate_tcq(scsi_dev, 1); 11102908d778SJames Bottomley 11112908d778SJames Bottomley scsi_set_tag_type(scsi_dev, qt); 11122908d778SJames Bottomley scsi_activate_tcq(scsi_dev, scsi_dev->queue_depth); 11132908d778SJames Bottomley 11142908d778SJames Bottomley return qt; 11152908d778SJames Bottomley } 11162908d778SJames Bottomley 11172908d778SJames Bottomley int sas_bios_param(struct scsi_device *scsi_dev, 11182908d778SJames Bottomley struct block_device *bdev, 11192908d778SJames Bottomley sector_t capacity, int *hsc) 11202908d778SJames Bottomley { 11212908d778SJames Bottomley hsc[0] = 255; 11222908d778SJames Bottomley hsc[1] = 63; 11232908d778SJames Bottomley sector_div(capacity, 255*63); 11242908d778SJames Bottomley hsc[2] = capacity; 11252908d778SJames Bottomley 11262908d778SJames Bottomley return 0; 11272908d778SJames Bottomley } 11282908d778SJames Bottomley 11292908d778SJames Bottomley /* ---------- Task Collector Thread implementation ---------- */ 11302908d778SJames Bottomley 11312908d778SJames Bottomley static void sas_queue(struct sas_ha_struct *sas_ha) 11322908d778SJames Bottomley { 11332908d778SJames Bottomley struct scsi_core *core = &sas_ha->core; 11342908d778SJames Bottomley unsigned long flags; 11352908d778SJames Bottomley LIST_HEAD(q); 11362908d778SJames Bottomley int can_queue; 11372908d778SJames Bottomley int res; 11382908d778SJames Bottomley struct sas_internal *i = to_sas_internal(core->shost->transportt); 11392908d778SJames Bottomley 11402908d778SJames Bottomley spin_lock_irqsave(&core->task_queue_lock, flags); 1141d7a54e30SChristoph Hellwig while (!kthread_should_stop() && 11422908d778SJames Bottomley !list_empty(&core->task_queue)) { 11432908d778SJames Bottomley 11442908d778SJames Bottomley can_queue = sas_ha->lldd_queue_size - core->task_queue_size; 11452908d778SJames Bottomley if (can_queue >= 0) { 11462908d778SJames Bottomley can_queue = core->task_queue_size; 11472908d778SJames Bottomley list_splice_init(&core->task_queue, &q); 11482908d778SJames Bottomley } else { 11492908d778SJames Bottomley struct list_head *a, *n; 11502908d778SJames Bottomley 11512908d778SJames Bottomley can_queue = sas_ha->lldd_queue_size; 11522908d778SJames Bottomley list_for_each_safe(a, n, &core->task_queue) { 11532908d778SJames Bottomley list_move_tail(a, &q); 11542908d778SJames Bottomley if (--can_queue == 0) 11552908d778SJames Bottomley break; 11562908d778SJames Bottomley } 11572908d778SJames Bottomley can_queue = sas_ha->lldd_queue_size; 11582908d778SJames Bottomley } 11592908d778SJames Bottomley core->task_queue_size -= can_queue; 11602908d778SJames Bottomley spin_unlock_irqrestore(&core->task_queue_lock, flags); 11612908d778SJames Bottomley { 11622908d778SJames Bottomley struct sas_task *task = list_entry(q.next, 11632908d778SJames Bottomley struct sas_task, 11642908d778SJames Bottomley list); 11652908d778SJames Bottomley list_del_init(&q); 11662908d778SJames Bottomley res = i->dft->lldd_execute_task(task, can_queue, 11672908d778SJames Bottomley GFP_KERNEL); 11682908d778SJames Bottomley if (unlikely(res)) 11692908d778SJames Bottomley __list_add(&q, task->list.prev, &task->list); 11702908d778SJames Bottomley } 11712908d778SJames Bottomley spin_lock_irqsave(&core->task_queue_lock, flags); 11722908d778SJames Bottomley if (res) { 11732908d778SJames Bottomley list_splice_init(&q, &core->task_queue); /*at head*/ 11742908d778SJames Bottomley core->task_queue_size += can_queue; 11752908d778SJames Bottomley } 11762908d778SJames Bottomley } 11772908d778SJames Bottomley spin_unlock_irqrestore(&core->task_queue_lock, flags); 11782908d778SJames Bottomley } 11792908d778SJames Bottomley 11802908d778SJames Bottomley /** 11812908d778SJames Bottomley * sas_queue_thread -- The Task Collector thread 11822908d778SJames Bottomley * @_sas_ha: pointer to struct sas_ha 11832908d778SJames Bottomley */ 11842908d778SJames Bottomley static int sas_queue_thread(void *_sas_ha) 11852908d778SJames Bottomley { 11862908d778SJames Bottomley struct sas_ha_struct *sas_ha = _sas_ha; 11872908d778SJames Bottomley 11882908d778SJames Bottomley while (1) { 1189d7a54e30SChristoph Hellwig set_current_state(TASK_INTERRUPTIBLE); 1190d7a54e30SChristoph Hellwig schedule(); 11912908d778SJames Bottomley sas_queue(sas_ha); 1192d7a54e30SChristoph Hellwig if (kthread_should_stop()) 11932908d778SJames Bottomley break; 11942908d778SJames Bottomley } 11952908d778SJames Bottomley 11962908d778SJames Bottomley return 0; 11972908d778SJames Bottomley } 11982908d778SJames Bottomley 11992908d778SJames Bottomley int sas_init_queue(struct sas_ha_struct *sas_ha) 12002908d778SJames Bottomley { 12012908d778SJames Bottomley struct scsi_core *core = &sas_ha->core; 12022908d778SJames Bottomley 12032908d778SJames Bottomley spin_lock_init(&core->task_queue_lock); 12042908d778SJames Bottomley core->task_queue_size = 0; 12052908d778SJames Bottomley INIT_LIST_HEAD(&core->task_queue); 12062908d778SJames Bottomley 1207d7a54e30SChristoph Hellwig core->queue_thread = kthread_run(sas_queue_thread, sas_ha, 1208d7a54e30SChristoph Hellwig "sas_queue_%d", core->shost->host_no); 1209d7a54e30SChristoph Hellwig if (IS_ERR(core->queue_thread)) 1210d7a54e30SChristoph Hellwig return PTR_ERR(core->queue_thread); 1211d7a54e30SChristoph Hellwig return 0; 12122908d778SJames Bottomley } 12132908d778SJames Bottomley 12142908d778SJames Bottomley void sas_shutdown_queue(struct sas_ha_struct *sas_ha) 12152908d778SJames Bottomley { 12162908d778SJames Bottomley unsigned long flags; 12172908d778SJames Bottomley struct scsi_core *core = &sas_ha->core; 12182908d778SJames Bottomley struct sas_task *task, *n; 12192908d778SJames Bottomley 1220d7a54e30SChristoph Hellwig kthread_stop(core->queue_thread); 12212908d778SJames Bottomley 12222908d778SJames Bottomley if (!list_empty(&core->task_queue)) 12232908d778SJames Bottomley SAS_DPRINTK("HA: %llx: scsi core task queue is NOT empty!?\n", 12242908d778SJames Bottomley SAS_ADDR(sas_ha->sas_addr)); 12252908d778SJames Bottomley 12262908d778SJames Bottomley spin_lock_irqsave(&core->task_queue_lock, flags); 12272908d778SJames Bottomley list_for_each_entry_safe(task, n, &core->task_queue, list) { 12282908d778SJames Bottomley struct scsi_cmnd *cmd = task->uldd_task; 12292908d778SJames Bottomley 12302908d778SJames Bottomley list_del_init(&task->list); 12312908d778SJames Bottomley 12322908d778SJames Bottomley ASSIGN_SAS_TASK(cmd, NULL); 12332908d778SJames Bottomley sas_free_task(task); 12342908d778SJames Bottomley cmd->result = DID_ABORT << 16; 12352908d778SJames Bottomley cmd->scsi_done(cmd); 12362908d778SJames Bottomley } 12372908d778SJames Bottomley spin_unlock_irqrestore(&core->task_queue_lock, flags); 12382908d778SJames Bottomley } 12392908d778SJames Bottomley 1240396819fbSDarrick J. Wong /* 1241396819fbSDarrick J. Wong * Call the LLDD task abort routine directly. This function is intended for 1242396819fbSDarrick J. Wong * use by upper layers that need to tell the LLDD to abort a task. 1243396819fbSDarrick J. Wong */ 1244396819fbSDarrick J. Wong int __sas_task_abort(struct sas_task *task) 124579a5eb60SDarrick J. Wong { 124679a5eb60SDarrick J. Wong struct sas_internal *si = 124779a5eb60SDarrick J. Wong to_sas_internal(task->dev->port->ha->core.shost->transportt); 124879a5eb60SDarrick J. Wong unsigned long flags; 124979a5eb60SDarrick J. Wong int res; 125079a5eb60SDarrick J. Wong 125179a5eb60SDarrick J. Wong spin_lock_irqsave(&task->task_state_lock, flags); 1252396819fbSDarrick J. Wong if (task->task_state_flags & SAS_TASK_STATE_ABORTED || 1253396819fbSDarrick J. Wong task->task_state_flags & SAS_TASK_STATE_DONE) { 125479a5eb60SDarrick J. Wong spin_unlock_irqrestore(&task->task_state_lock, flags); 1255396819fbSDarrick J. Wong SAS_DPRINTK("%s: Task %p already finished.\n", __FUNCTION__, 125679a5eb60SDarrick J. Wong task); 125779a5eb60SDarrick J. Wong return 0; 125879a5eb60SDarrick J. Wong } 125979a5eb60SDarrick J. Wong task->task_state_flags |= SAS_TASK_STATE_ABORTED; 126079a5eb60SDarrick J. Wong spin_unlock_irqrestore(&task->task_state_lock, flags); 126179a5eb60SDarrick J. Wong 126279a5eb60SDarrick J. Wong if (!si->dft->lldd_abort_task) 126379a5eb60SDarrick J. Wong return -ENODEV; 126479a5eb60SDarrick J. Wong 126579a5eb60SDarrick J. Wong res = si->dft->lldd_abort_task(task); 1266396819fbSDarrick J. Wong 1267396819fbSDarrick J. Wong spin_lock_irqsave(&task->task_state_lock, flags); 126879a5eb60SDarrick J. Wong if ((task->task_state_flags & SAS_TASK_STATE_DONE) || 126979a5eb60SDarrick J. Wong (res == TMF_RESP_FUNC_COMPLETE)) 127079a5eb60SDarrick J. Wong { 1271396819fbSDarrick J. Wong spin_unlock_irqrestore(&task->task_state_lock, flags); 127279a5eb60SDarrick J. Wong task->task_done(task); 127379a5eb60SDarrick J. Wong return 0; 127479a5eb60SDarrick J. Wong } 127579a5eb60SDarrick J. Wong 127679a5eb60SDarrick J. Wong if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) 127779a5eb60SDarrick J. Wong task->task_state_flags &= ~SAS_TASK_STATE_ABORTED; 127879a5eb60SDarrick J. Wong spin_unlock_irqrestore(&task->task_state_lock, flags); 127979a5eb60SDarrick J. Wong 128079a5eb60SDarrick J. Wong return -EAGAIN; 128179a5eb60SDarrick J. Wong } 128279a5eb60SDarrick J. Wong 1283396819fbSDarrick J. Wong /* 1284396819fbSDarrick J. Wong * Tell an upper layer that it needs to initiate an abort for a given task. 1285396819fbSDarrick J. Wong * This should only ever be called by an LLDD. 1286396819fbSDarrick J. Wong */ 1287396819fbSDarrick J. Wong void sas_task_abort(struct sas_task *task) 128879a5eb60SDarrick J. Wong { 1289396819fbSDarrick J. Wong struct scsi_cmnd *sc = task->uldd_task; 129079a5eb60SDarrick J. Wong 1291396819fbSDarrick J. Wong /* Escape for libsas internal commands */ 1292396819fbSDarrick J. Wong if (!sc) { 1293396819fbSDarrick J. Wong if (!del_timer(&task->timer)) 129479a5eb60SDarrick J. Wong return; 1295396819fbSDarrick J. Wong task->timer.function(task->timer.data); 1296396819fbSDarrick J. Wong return; 1297396819fbSDarrick J. Wong } 129879a5eb60SDarrick J. Wong 1299396819fbSDarrick J. Wong scsi_req_abort_cmd(sc); 1300396819fbSDarrick J. Wong scsi_schedule_eh(sc->device->host); 130179a5eb60SDarrick J. Wong } 130279a5eb60SDarrick J. Wong 1303fa1c1e8fSDarrick J. Wong int sas_slave_alloc(struct scsi_device *scsi_dev) 1304fa1c1e8fSDarrick J. Wong { 1305fa1c1e8fSDarrick J. Wong struct domain_device *dev = sdev_to_domain_dev(scsi_dev); 1306fa1c1e8fSDarrick J. Wong 1307fa1c1e8fSDarrick J. Wong if (dev_is_sata(dev)) 1308fa1c1e8fSDarrick J. Wong return ata_sas_port_init(dev->sata_dev.ap); 1309fa1c1e8fSDarrick J. Wong 1310fa1c1e8fSDarrick J. Wong return 0; 1311fa1c1e8fSDarrick J. Wong } 1312fa1c1e8fSDarrick J. Wong 1313fa1c1e8fSDarrick J. Wong void sas_target_destroy(struct scsi_target *starget) 1314fa1c1e8fSDarrick J. Wong { 1315fa1c1e8fSDarrick J. Wong struct domain_device *found_dev = sas_find_target(starget); 1316fa1c1e8fSDarrick J. Wong 1317fa1c1e8fSDarrick J. Wong if (!found_dev) 1318fa1c1e8fSDarrick J. Wong return; 1319fa1c1e8fSDarrick J. Wong 1320fa1c1e8fSDarrick J. Wong if (dev_is_sata(found_dev)) 1321fa1c1e8fSDarrick J. Wong ata_sas_port_destroy(found_dev->sata_dev.ap); 1322fa1c1e8fSDarrick J. Wong 1323fa1c1e8fSDarrick J. Wong return; 1324fa1c1e8fSDarrick J. Wong } 1325fa1c1e8fSDarrick J. Wong 13262908d778SJames Bottomley EXPORT_SYMBOL_GPL(sas_queuecommand); 13272908d778SJames Bottomley EXPORT_SYMBOL_GPL(sas_target_alloc); 13282908d778SJames Bottomley EXPORT_SYMBOL_GPL(sas_slave_configure); 13292908d778SJames Bottomley EXPORT_SYMBOL_GPL(sas_slave_destroy); 13302908d778SJames Bottomley EXPORT_SYMBOL_GPL(sas_change_queue_depth); 13312908d778SJames Bottomley EXPORT_SYMBOL_GPL(sas_change_queue_type); 13322908d778SJames Bottomley EXPORT_SYMBOL_GPL(sas_bios_param); 1333396819fbSDarrick J. Wong EXPORT_SYMBOL_GPL(__sas_task_abort); 133479a5eb60SDarrick J. Wong EXPORT_SYMBOL_GPL(sas_task_abort); 1335dea22214SDarrick J. Wong EXPORT_SYMBOL_GPL(sas_phy_reset); 1336acbf167dSDarrick J. Wong EXPORT_SYMBOL_GPL(sas_phy_enable); 1337ad689233SDarrick J. Wong EXPORT_SYMBOL_GPL(sas_eh_device_reset_handler); 1338a9344e68SDarrick J. Wong EXPORT_SYMBOL_GPL(sas_eh_bus_reset_handler); 1339fa1c1e8fSDarrick J. Wong EXPORT_SYMBOL_GPL(sas_slave_alloc); 1340fa1c1e8fSDarrick J. Wong EXPORT_SYMBOL_GPL(sas_target_destroy); 1341fa1c1e8fSDarrick J. Wong EXPORT_SYMBOL_GPL(sas_ioctl); 1342