12908d778SJames Bottomley /*
22908d778SJames Bottomley  * Serial Attached SCSI (SAS) class SCSI Host glue.
32908d778SJames Bottomley  *
42908d778SJames Bottomley  * Copyright (C) 2005 Adaptec, Inc.  All rights reserved.
52908d778SJames Bottomley  * Copyright (C) 2005 Luben Tuikov <luben_tuikov@adaptec.com>
62908d778SJames Bottomley  *
72908d778SJames Bottomley  * This file is licensed under GPLv2.
82908d778SJames Bottomley  *
92908d778SJames Bottomley  * This program is free software; you can redistribute it and/or
102908d778SJames Bottomley  * modify it under the terms of the GNU General Public License as
112908d778SJames Bottomley  * published by the Free Software Foundation; either version 2 of the
122908d778SJames Bottomley  * License, or (at your option) any later version.
132908d778SJames Bottomley  *
142908d778SJames Bottomley  * This program is distributed in the hope that it will be useful, but
152908d778SJames Bottomley  * WITHOUT ANY WARRANTY; without even the implied warranty of
162908d778SJames Bottomley  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
172908d778SJames Bottomley  * General Public License for more details.
182908d778SJames Bottomley  *
192908d778SJames Bottomley  * You should have received a copy of the GNU General Public License
202908d778SJames Bottomley  * along with this program; if not, write to the Free Software
212908d778SJames Bottomley  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
222908d778SJames Bottomley  * USA
232908d778SJames Bottomley  *
242908d778SJames Bottomley  */
252908d778SJames Bottomley 
26d7a54e30SChristoph Hellwig #include <linux/kthread.h>
2745e6cdf4SDarrick J. Wong #include <linux/firmware.h>
2809703660SPaul Gortmaker #include <linux/export.h>
2945e6cdf4SDarrick J. Wong #include <linux/ctype.h>
30d7a54e30SChristoph Hellwig 
312908d778SJames Bottomley #include "sas_internal.h"
322908d778SJames Bottomley 
332908d778SJames Bottomley #include <scsi/scsi_host.h>
342908d778SJames Bottomley #include <scsi/scsi_device.h>
352908d778SJames Bottomley #include <scsi/scsi_tcq.h>
362908d778SJames Bottomley #include <scsi/scsi.h>
37f456393eSDarrick J. Wong #include <scsi/scsi_eh.h>
382908d778SJames Bottomley #include <scsi/scsi_transport.h>
392908d778SJames Bottomley #include <scsi/scsi_transport_sas.h>
40338ec570SDarrick J. Wong #include <scsi/sas_ata.h>
412908d778SJames Bottomley #include "../scsi_sas_internal.h"
4279a5eb60SDarrick J. Wong #include "../scsi_transport_api.h"
43ad689233SDarrick J. Wong #include "../scsi_priv.h"
442908d778SJames Bottomley 
452908d778SJames Bottomley #include <linux/err.h>
462908d778SJames Bottomley #include <linux/blkdev.h>
4783144186SRafael J. Wysocki #include <linux/freezer.h>
485a0e3ad6STejun Heo #include <linux/gfp.h>
492908d778SJames Bottomley #include <linux/scatterlist.h>
50fa1c1e8fSDarrick J. Wong #include <linux/libata.h>
512908d778SJames Bottomley 
52a3a14252SDan Williams /* record final status and free the task */
53a3a14252SDan Williams static void sas_end_task(struct scsi_cmnd *sc, struct sas_task *task)
542908d778SJames Bottomley {
552908d778SJames Bottomley 	struct task_status_struct *ts = &task->task_status;
562908d778SJames Bottomley 	int hs = 0, stat = 0;
572908d778SJames Bottomley 
582908d778SJames Bottomley 	if (ts->resp == SAS_TASK_UNDELIVERED) {
592908d778SJames Bottomley 		/* transport error */
602908d778SJames Bottomley 		hs = DID_NO_CONNECT;
612908d778SJames Bottomley 	} else { /* ts->resp == SAS_TASK_COMPLETE */
622908d778SJames Bottomley 		/* task delivered, what happened afterwards? */
632908d778SJames Bottomley 		switch (ts->stat) {
642908d778SJames Bottomley 		case SAS_DEV_NO_RESPONSE:
652908d778SJames Bottomley 		case SAS_INTERRUPTED:
662908d778SJames Bottomley 		case SAS_PHY_DOWN:
672908d778SJames Bottomley 		case SAS_NAK_R_ERR:
682908d778SJames Bottomley 		case SAS_OPEN_TO:
692908d778SJames Bottomley 			hs = DID_NO_CONNECT;
702908d778SJames Bottomley 			break;
712908d778SJames Bottomley 		case SAS_DATA_UNDERRUN:
72c13e5566SFUJITA Tomonori 			scsi_set_resid(sc, ts->residual);
73c13e5566SFUJITA Tomonori 			if (scsi_bufflen(sc) - scsi_get_resid(sc) < sc->underflow)
742908d778SJames Bottomley 				hs = DID_ERROR;
752908d778SJames Bottomley 			break;
762908d778SJames Bottomley 		case SAS_DATA_OVERRUN:
772908d778SJames Bottomley 			hs = DID_ERROR;
782908d778SJames Bottomley 			break;
792908d778SJames Bottomley 		case SAS_QUEUE_FULL:
802908d778SJames Bottomley 			hs = DID_SOFT_ERROR; /* retry */
812908d778SJames Bottomley 			break;
822908d778SJames Bottomley 		case SAS_DEVICE_UNKNOWN:
832908d778SJames Bottomley 			hs = DID_BAD_TARGET;
842908d778SJames Bottomley 			break;
852908d778SJames Bottomley 		case SAS_SG_ERR:
862908d778SJames Bottomley 			hs = DID_PARITY;
872908d778SJames Bottomley 			break;
882908d778SJames Bottomley 		case SAS_OPEN_REJECT:
892908d778SJames Bottomley 			if (ts->open_rej_reason == SAS_OREJ_RSVD_RETRY)
902908d778SJames Bottomley 				hs = DID_SOFT_ERROR; /* retry */
912908d778SJames Bottomley 			else
922908d778SJames Bottomley 				hs = DID_ERROR;
932908d778SJames Bottomley 			break;
942908d778SJames Bottomley 		case SAS_PROTO_RESPONSE:
952908d778SJames Bottomley 			SAS_DPRINTK("LLDD:%s sent SAS_PROTO_RESP for an SSP "
962908d778SJames Bottomley 				    "task; please report this\n",
972908d778SJames Bottomley 				    task->dev->port->ha->sas_ha_name);
982908d778SJames Bottomley 			break;
992908d778SJames Bottomley 		case SAS_ABORTED_TASK:
1002908d778SJames Bottomley 			hs = DID_ABORT;
1012908d778SJames Bottomley 			break;
102df64d3caSJames Bottomley 		case SAM_STAT_CHECK_CONDITION:
1032908d778SJames Bottomley 			memcpy(sc->sense_buffer, ts->buf,
104cc75e8abSFUJITA Tomonori 			       min(SCSI_SENSE_BUFFERSIZE, ts->buf_valid_size));
105df64d3caSJames Bottomley 			stat = SAM_STAT_CHECK_CONDITION;
1062908d778SJames Bottomley 			break;
1072908d778SJames Bottomley 		default:
1082908d778SJames Bottomley 			stat = ts->stat;
1092908d778SJames Bottomley 			break;
1102908d778SJames Bottomley 		}
1112908d778SJames Bottomley 	}
112a3a14252SDan Williams 
1132908d778SJames Bottomley 	sc->result = (hs << 16) | stat;
114a3a14252SDan Williams 	ASSIGN_SAS_TASK(sc, NULL);
1152908d778SJames Bottomley 	list_del_init(&task->list);
1162908d778SJames Bottomley 	sas_free_task(task);
117a3a14252SDan Williams }
118a3a14252SDan Williams 
119a3a14252SDan Williams static void sas_scsi_task_done(struct sas_task *task)
120a3a14252SDan Williams {
121a3a14252SDan Williams 	struct scsi_cmnd *sc = task->uldd_task;
1229095a64aSDan Williams 	struct domain_device *dev = task->dev;
1239095a64aSDan Williams 	struct sas_ha_struct *ha = dev->port->ha;
1249095a64aSDan Williams 	unsigned long flags;
125a3a14252SDan Williams 
1269095a64aSDan Williams 	spin_lock_irqsave(&dev->done_lock, flags);
1279095a64aSDan Williams 	if (test_bit(SAS_HA_FROZEN, &ha->state))
1289095a64aSDan Williams 		task = NULL;
1299095a64aSDan Williams 	else
1309095a64aSDan Williams 		ASSIGN_SAS_TASK(sc, NULL);
1319095a64aSDan Williams 	spin_unlock_irqrestore(&dev->done_lock, flags);
1329095a64aSDan Williams 
1339095a64aSDan Williams 	if (unlikely(!task)) {
1349095a64aSDan Williams 		/* task will be completed by the error handler */
135a3a14252SDan Williams 		SAS_DPRINTK("task done but aborted\n");
136a3a14252SDan Williams 		return;
137a3a14252SDan Williams 	}
138a3a14252SDan Williams 
139a3a14252SDan Williams 	if (unlikely(!sc)) {
140a3a14252SDan Williams 		SAS_DPRINTK("task_done called with non existing SCSI cmnd!\n");
141a3a14252SDan Williams 		list_del_init(&task->list);
142a3a14252SDan Williams 		sas_free_task(task);
143a3a14252SDan Williams 		return;
144a3a14252SDan Williams 	}
145a3a14252SDan Williams 
146a3a14252SDan Williams 	sas_end_task(sc, task);
1472908d778SJames Bottomley 	sc->scsi_done(sc);
1482908d778SJames Bottomley }
1492908d778SJames Bottomley 
1502908d778SJames Bottomley static struct sas_task *sas_create_task(struct scsi_cmnd *cmd,
1512908d778SJames Bottomley 					       struct domain_device *dev,
1523cc27547SAl Viro 					       gfp_t gfp_flags)
1532908d778SJames Bottomley {
1542908d778SJames Bottomley 	struct sas_task *task = sas_alloc_task(gfp_flags);
1552908d778SJames Bottomley 	struct scsi_lun lun;
1562908d778SJames Bottomley 
1572908d778SJames Bottomley 	if (!task)
1582908d778SJames Bottomley 		return NULL;
1592908d778SJames Bottomley 
1602908d778SJames Bottomley 	task->uldd_task = cmd;
1612908d778SJames Bottomley 	ASSIGN_SAS_TASK(cmd, task);
1622908d778SJames Bottomley 
1632908d778SJames Bottomley 	task->dev = dev;
1642908d778SJames Bottomley 	task->task_proto = task->dev->tproto; /* BUG_ON(!SSP) */
1652908d778SJames Bottomley 
1662908d778SJames Bottomley 	task->ssp_task.retry_count = 1;
1672908d778SJames Bottomley 	int_to_scsilun(cmd->device->lun, &lun);
1682908d778SJames Bottomley 	memcpy(task->ssp_task.LUN, &lun.scsi_lun, 8);
1699cbbdca4STejun Heo 	task->ssp_task.task_attr = TASK_ATTR_SIMPLE;
1702908d778SJames Bottomley 	memcpy(task->ssp_task.cdb, cmd->cmnd, 16);
1712908d778SJames Bottomley 
172c13e5566SFUJITA Tomonori 	task->scatter = scsi_sglist(cmd);
173c13e5566SFUJITA Tomonori 	task->num_scatter = scsi_sg_count(cmd);
174c13e5566SFUJITA Tomonori 	task->total_xfer_len = scsi_bufflen(cmd);
1752908d778SJames Bottomley 	task->data_dir = cmd->sc_data_direction;
1762908d778SJames Bottomley 
1772908d778SJames Bottomley 	task->task_done = sas_scsi_task_done;
1782908d778SJames Bottomley 
1792908d778SJames Bottomley 	return task;
1802908d778SJames Bottomley }
1812908d778SJames Bottomley 
182338ec570SDarrick J. Wong int sas_queue_up(struct sas_task *task)
1832908d778SJames Bottomley {
1842908d778SJames Bottomley 	struct sas_ha_struct *sas_ha = task->dev->port->ha;
1852908d778SJames Bottomley 	struct scsi_core *core = &sas_ha->core;
1862908d778SJames Bottomley 	unsigned long flags;
1872908d778SJames Bottomley 	LIST_HEAD(list);
1882908d778SJames Bottomley 
1892908d778SJames Bottomley 	spin_lock_irqsave(&core->task_queue_lock, flags);
1902908d778SJames Bottomley 	if (sas_ha->lldd_queue_size < core->task_queue_size + 1) {
1912908d778SJames Bottomley 		spin_unlock_irqrestore(&core->task_queue_lock, flags);
1922908d778SJames Bottomley 		return -SAS_QUEUE_FULL;
1932908d778SJames Bottomley 	}
1942908d778SJames Bottomley 	list_add_tail(&task->list, &core->task_queue);
1952908d778SJames Bottomley 	core->task_queue_size += 1;
1962908d778SJames Bottomley 	spin_unlock_irqrestore(&core->task_queue_lock, flags);
197d7a54e30SChristoph Hellwig 	wake_up_process(core->queue_thread);
1982908d778SJames Bottomley 
1992908d778SJames Bottomley 	return 0;
2002908d778SJames Bottomley }
2012908d778SJames Bottomley 
20292bd401bSChristoph Hellwig int sas_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
2032908d778SJames Bottomley {
2042908d778SJames Bottomley 	struct sas_internal *i = to_sas_internal(host->transportt);
205a923f756SChristoph Hellwig 	struct domain_device *dev = cmd_to_domain_dev(cmd);
2062908d778SJames Bottomley 	struct sas_ha_struct *sas_ha = dev->port->ha;
2072908d778SJames Bottomley 	struct sas_task *task;
208a923f756SChristoph Hellwig 	int res = 0;
2092908d778SJames Bottomley 
2103673f4bfSDan Williams 	/* If the device fell off, no sense in issuing commands */
211e139942dSDan Williams 	if (test_bit(SAS_DEV_GONE, &dev->state)) {
2123673f4bfSDan Williams 		cmd->result = DID_BAD_TARGET << 16;
213a923f756SChristoph Hellwig 		goto out_done;
2143673f4bfSDan Williams 	}
2153673f4bfSDan Williams 
216fa1c1e8fSDarrick J. Wong 	if (dev_is_sata(dev)) {
217312d3e56SDan Williams 		spin_lock_irq(dev->sata_dev.ap->lock);
218b27dcfb0SJeff Garzik 		res = ata_sas_queuecmd(cmd, dev->sata_dev.ap);
219312d3e56SDan Williams 		spin_unlock_irq(dev->sata_dev.ap->lock);
220a923f756SChristoph Hellwig 		return res;
221fa1c1e8fSDarrick J. Wong 	}
222fa1c1e8fSDarrick J. Wong 
2232908d778SJames Bottomley 	task = sas_create_task(cmd, dev, GFP_ATOMIC);
2242908d778SJames Bottomley 	if (!task)
225ac81c6a8SChristoph Hellwig 		return SCSI_MLQUEUE_HOST_BUSY;
2262908d778SJames Bottomley 
2272908d778SJames Bottomley 	/* Queue up, Direct Mode or Task Collector Mode. */
2282908d778SJames Bottomley 	if (sas_ha->lldd_max_execute_num < 2)
2292908d778SJames Bottomley 		res = i->dft->lldd_execute_task(task, 1, GFP_ATOMIC);
2302908d778SJames Bottomley 	else
2312908d778SJames Bottomley 		res = sas_queue_up(task);
2322908d778SJames Bottomley 
233a923f756SChristoph Hellwig 	if (res)
234a923f756SChristoph Hellwig 		goto out_free_task;
235a923f756SChristoph Hellwig 	return 0;
236a923f756SChristoph Hellwig 
237a923f756SChristoph Hellwig out_free_task:
2382908d778SJames Bottomley 	SAS_DPRINTK("lldd_execute_task returned: %d\n", res);
2392908d778SJames Bottomley 	ASSIGN_SAS_TASK(cmd, NULL);
2402908d778SJames Bottomley 	sas_free_task(task);
241ac81c6a8SChristoph Hellwig 	if (res == -SAS_QUEUE_FULL)
242a923f756SChristoph Hellwig 		cmd->result = DID_SOFT_ERROR << 16; /* retry */
243ac81c6a8SChristoph Hellwig 	else
244ac81c6a8SChristoph Hellwig 		cmd->result = DID_ERROR << 16;
245a923f756SChristoph Hellwig out_done:
246a923f756SChristoph Hellwig 	cmd->scsi_done(cmd);
247a923f756SChristoph Hellwig 	return 0;
2482908d778SJames Bottomley }
2492908d778SJames Bottomley 
250a8e14fecSJames Bottomley static void sas_eh_finish_cmd(struct scsi_cmnd *cmd)
251a8e14fecSJames Bottomley {
252a8e14fecSJames Bottomley 	struct sas_ha_struct *sas_ha = SHOST_TO_SAS_HA(cmd->device->host);
25345c73b65SDan Williams 	struct sas_task *task = TO_SAS_TASK(cmd);
254a8e14fecSJames Bottomley 
255a3a14252SDan Williams 	/* At this point, we only get called following an actual abort
256a3a14252SDan Williams 	 * of the task, so we should be guaranteed not to be racing with
257a3a14252SDan Williams 	 * any completions from the LLD.  Task is freed after this.
258a3a14252SDan Williams 	 */
259a3a14252SDan Williams 	sas_end_task(cmd, task);
260a3a14252SDan Williams 
261a8e14fecSJames Bottomley 	/* now finish the command and move it on to the error
262a8e14fecSJames Bottomley 	 * handler done list, this also takes it off the
263a3a14252SDan Williams 	 * error handler pending list.
264a3a14252SDan Williams 	 */
265a8e14fecSJames Bottomley 	scsi_eh_finish_cmd(cmd, &sas_ha->eh_done_q);
266a8e14fecSJames Bottomley }
267a8e14fecSJames Bottomley 
2683944f509SDan Williams static void sas_eh_defer_cmd(struct scsi_cmnd *cmd)
2693944f509SDan Williams {
27045c73b65SDan Williams 	struct domain_device *dev = cmd_to_domain_dev(cmd);
2713944f509SDan Williams 	struct sas_ha_struct *ha = dev->port->ha;
27245c73b65SDan Williams 	struct sas_task *task = TO_SAS_TASK(cmd);
2733944f509SDan Williams 
2743944f509SDan Williams 	if (!dev_is_sata(dev)) {
2753944f509SDan Williams 		sas_eh_finish_cmd(cmd);
2763944f509SDan Williams 		return;
2773944f509SDan Williams 	}
2783944f509SDan Williams 
2793944f509SDan Williams 	/* report the timeout to libata */
2803944f509SDan Williams 	sas_end_task(cmd, task);
2813944f509SDan Williams 	list_move_tail(&cmd->eh_entry, &ha->eh_ata_q);
2823944f509SDan Williams }
2833944f509SDan Williams 
2842908d778SJames Bottomley static void sas_scsi_clear_queue_lu(struct list_head *error_q, struct scsi_cmnd *my_cmd)
2852908d778SJames Bottomley {
2862908d778SJames Bottomley 	struct scsi_cmnd *cmd, *n;
2872908d778SJames Bottomley 
2882908d778SJames Bottomley 	list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
28963e4563bSJames Bottomley 		if (cmd->device->sdev_target == my_cmd->device->sdev_target &&
29063e4563bSJames Bottomley 		    cmd->device->lun == my_cmd->device->lun)
2913a2cdf39SDan Williams 			sas_eh_defer_cmd(cmd);
2922908d778SJames Bottomley 	}
2932908d778SJames Bottomley }
2942908d778SJames Bottomley 
2952908d778SJames Bottomley static void sas_scsi_clear_queue_I_T(struct list_head *error_q,
2962908d778SJames Bottomley 				     struct domain_device *dev)
2972908d778SJames Bottomley {
2982908d778SJames Bottomley 	struct scsi_cmnd *cmd, *n;
2992908d778SJames Bottomley 
3002908d778SJames Bottomley 	list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
3012908d778SJames Bottomley 		struct domain_device *x = cmd_to_domain_dev(cmd);
3022908d778SJames Bottomley 
3032908d778SJames Bottomley 		if (x == dev)
304a8e14fecSJames Bottomley 			sas_eh_finish_cmd(cmd);
3052908d778SJames Bottomley 	}
3062908d778SJames Bottomley }
3072908d778SJames Bottomley 
3082908d778SJames Bottomley static void sas_scsi_clear_queue_port(struct list_head *error_q,
3092908d778SJames Bottomley 				      struct asd_sas_port *port)
3102908d778SJames Bottomley {
3112908d778SJames Bottomley 	struct scsi_cmnd *cmd, *n;
3122908d778SJames Bottomley 
3132908d778SJames Bottomley 	list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
3142908d778SJames Bottomley 		struct domain_device *dev = cmd_to_domain_dev(cmd);
3152908d778SJames Bottomley 		struct asd_sas_port *x = dev->port;
3162908d778SJames Bottomley 
3172908d778SJames Bottomley 		if (x == port)
318a8e14fecSJames Bottomley 			sas_eh_finish_cmd(cmd);
3192908d778SJames Bottomley 	}
3202908d778SJames Bottomley }
3212908d778SJames Bottomley 
3222908d778SJames Bottomley enum task_disposition {
3232908d778SJames Bottomley 	TASK_IS_DONE,
3242908d778SJames Bottomley 	TASK_IS_ABORTED,
3252908d778SJames Bottomley 	TASK_IS_AT_LU,
3269095a64aSDan Williams 	TASK_IS_NOT_AT_HA,
3272908d778SJames Bottomley 	TASK_IS_NOT_AT_LU,
32802cd743bSDarrick J. Wong 	TASK_ABORT_FAILED,
3292908d778SJames Bottomley };
3302908d778SJames Bottomley 
3312908d778SJames Bottomley static enum task_disposition sas_scsi_find_task(struct sas_task *task)
3322908d778SJames Bottomley {
3332908d778SJames Bottomley 	struct sas_ha_struct *ha = task->dev->port->ha;
3342908d778SJames Bottomley 	unsigned long flags;
3352908d778SJames Bottomley 	int i, res;
3362908d778SJames Bottomley 	struct sas_internal *si =
3372908d778SJames Bottomley 		to_sas_internal(task->dev->port->ha->core.shost->transportt);
3382908d778SJames Bottomley 
3392908d778SJames Bottomley 	if (ha->lldd_max_execute_num > 1) {
3402908d778SJames Bottomley 		struct scsi_core *core = &ha->core;
3412908d778SJames Bottomley 		struct sas_task *t, *n;
3422908d778SJames Bottomley 
3439095a64aSDan Williams 		mutex_lock(&core->task_queue_flush);
3442908d778SJames Bottomley 		spin_lock_irqsave(&core->task_queue_lock, flags);
3459095a64aSDan Williams 		list_for_each_entry_safe(t, n, &core->task_queue, list)
3462908d778SJames Bottomley 			if (task == t) {
3472908d778SJames Bottomley 				list_del_init(&t->list);
3489095a64aSDan Williams 				break;
3492908d778SJames Bottomley 			}
3502908d778SJames Bottomley 		spin_unlock_irqrestore(&core->task_queue_lock, flags);
3519095a64aSDan Williams 		mutex_unlock(&core->task_queue_flush);
3529095a64aSDan Williams 
3539095a64aSDan Williams 		if (task == t)
3549095a64aSDan Williams 			return TASK_IS_NOT_AT_HA;
3552908d778SJames Bottomley 	}
3562908d778SJames Bottomley 
3572908d778SJames Bottomley 	for (i = 0; i < 5; i++) {
358cadbd4a5SHarvey Harrison 		SAS_DPRINTK("%s: aborting task 0x%p\n", __func__, task);
3592908d778SJames Bottomley 		res = si->dft->lldd_abort_task(task);
3602908d778SJames Bottomley 
3612908d778SJames Bottomley 		spin_lock_irqsave(&task->task_state_lock, flags);
3622908d778SJames Bottomley 		if (task->task_state_flags & SAS_TASK_STATE_DONE) {
3632908d778SJames Bottomley 			spin_unlock_irqrestore(&task->task_state_lock, flags);
364cadbd4a5SHarvey Harrison 			SAS_DPRINTK("%s: task 0x%p is done\n", __func__,
3652908d778SJames Bottomley 				    task);
3662908d778SJames Bottomley 			return TASK_IS_DONE;
3672908d778SJames Bottomley 		}
3682908d778SJames Bottomley 		spin_unlock_irqrestore(&task->task_state_lock, flags);
3692908d778SJames Bottomley 
3702908d778SJames Bottomley 		if (res == TMF_RESP_FUNC_COMPLETE) {
3712908d778SJames Bottomley 			SAS_DPRINTK("%s: task 0x%p is aborted\n",
372cadbd4a5SHarvey Harrison 				    __func__, task);
3732908d778SJames Bottomley 			return TASK_IS_ABORTED;
3742908d778SJames Bottomley 		} else if (si->dft->lldd_query_task) {
3752908d778SJames Bottomley 			SAS_DPRINTK("%s: querying task 0x%p\n",
376cadbd4a5SHarvey Harrison 				    __func__, task);
3772908d778SJames Bottomley 			res = si->dft->lldd_query_task(task);
37802cd743bSDarrick J. Wong 			switch (res) {
37902cd743bSDarrick J. Wong 			case TMF_RESP_FUNC_SUCC:
3802908d778SJames Bottomley 				SAS_DPRINTK("%s: task 0x%p at LU\n",
381cadbd4a5SHarvey Harrison 					    __func__, task);
3822908d778SJames Bottomley 				return TASK_IS_AT_LU;
38302cd743bSDarrick J. Wong 			case TMF_RESP_FUNC_COMPLETE:
3842908d778SJames Bottomley 				SAS_DPRINTK("%s: task 0x%p not at LU\n",
385cadbd4a5SHarvey Harrison 					    __func__, task);
3862908d778SJames Bottomley 				return TASK_IS_NOT_AT_LU;
38702cd743bSDarrick J. Wong 			case TMF_RESP_FUNC_FAILED:
38802cd743bSDarrick J. Wong                                 SAS_DPRINTK("%s: task 0x%p failed to abort\n",
389cadbd4a5SHarvey Harrison                                                 __func__, task);
39002cd743bSDarrick J. Wong                                 return TASK_ABORT_FAILED;
3912908d778SJames Bottomley                         }
39202cd743bSDarrick J. Wong 
3932908d778SJames Bottomley 		}
3942908d778SJames Bottomley 	}
3952908d778SJames Bottomley 	return res;
3962908d778SJames Bottomley }
3972908d778SJames Bottomley 
3982908d778SJames Bottomley static int sas_recover_lu(struct domain_device *dev, struct scsi_cmnd *cmd)
3992908d778SJames Bottomley {
4002908d778SJames Bottomley 	int res = TMF_RESP_FUNC_FAILED;
4012908d778SJames Bottomley 	struct scsi_lun lun;
4022908d778SJames Bottomley 	struct sas_internal *i =
4032908d778SJames Bottomley 		to_sas_internal(dev->port->ha->core.shost->transportt);
4042908d778SJames Bottomley 
4052908d778SJames Bottomley 	int_to_scsilun(cmd->device->lun, &lun);
4062908d778SJames Bottomley 
4072908d778SJames Bottomley 	SAS_DPRINTK("eh: device %llx LUN %x has the task\n",
4082908d778SJames Bottomley 		    SAS_ADDR(dev->sas_addr),
4092908d778SJames Bottomley 		    cmd->device->lun);
4102908d778SJames Bottomley 
4112908d778SJames Bottomley 	if (i->dft->lldd_abort_task_set)
4122908d778SJames Bottomley 		res = i->dft->lldd_abort_task_set(dev, lun.scsi_lun);
4132908d778SJames Bottomley 
4142908d778SJames Bottomley 	if (res == TMF_RESP_FUNC_FAILED) {
4152908d778SJames Bottomley 		if (i->dft->lldd_clear_task_set)
4162908d778SJames Bottomley 			res = i->dft->lldd_clear_task_set(dev, lun.scsi_lun);
4172908d778SJames Bottomley 	}
4182908d778SJames Bottomley 
4192908d778SJames Bottomley 	if (res == TMF_RESP_FUNC_FAILED) {
4202908d778SJames Bottomley 		if (i->dft->lldd_lu_reset)
4212908d778SJames Bottomley 			res = i->dft->lldd_lu_reset(dev, lun.scsi_lun);
4222908d778SJames Bottomley 	}
4232908d778SJames Bottomley 
4242908d778SJames Bottomley 	return res;
4252908d778SJames Bottomley }
4262908d778SJames Bottomley 
4272908d778SJames Bottomley static int sas_recover_I_T(struct domain_device *dev)
4282908d778SJames Bottomley {
4292908d778SJames Bottomley 	int res = TMF_RESP_FUNC_FAILED;
4302908d778SJames Bottomley 	struct sas_internal *i =
4312908d778SJames Bottomley 		to_sas_internal(dev->port->ha->core.shost->transportt);
4322908d778SJames Bottomley 
4332908d778SJames Bottomley 	SAS_DPRINTK("I_T nexus reset for dev %016llx\n",
4342908d778SJames Bottomley 		    SAS_ADDR(dev->sas_addr));
4352908d778SJames Bottomley 
4362908d778SJames Bottomley 	if (i->dft->lldd_I_T_nexus_reset)
4372908d778SJames Bottomley 		res = i->dft->lldd_I_T_nexus_reset(dev);
4382908d778SJames Bottomley 
4392908d778SJames Bottomley 	return res;
4402908d778SJames Bottomley }
4412908d778SJames Bottomley 
442f41a0c44SDan Williams /* take a reference on the last known good phy for this device */
443f41a0c44SDan Williams struct sas_phy *sas_get_local_phy(struct domain_device *dev)
4443ebf6922SDarrick J. Wong {
445f41a0c44SDan Williams 	struct sas_ha_struct *ha = dev->port->ha;
446f41a0c44SDan Williams 	struct sas_phy *phy;
447f41a0c44SDan Williams 	unsigned long flags;
4483ebf6922SDarrick J. Wong 
449f41a0c44SDan Williams 	/* a published domain device always has a valid phy, it may be
450f41a0c44SDan Williams 	 * stale, but it is never NULL
451f41a0c44SDan Williams 	 */
452f41a0c44SDan Williams 	BUG_ON(!dev->phy);
4533ebf6922SDarrick J. Wong 
454f41a0c44SDan Williams 	spin_lock_irqsave(&ha->phy_port_lock, flags);
455f41a0c44SDan Williams 	phy = dev->phy;
456f41a0c44SDan Williams 	get_device(&phy->dev);
457f41a0c44SDan Williams 	spin_unlock_irqrestore(&ha->phy_port_lock, flags);
458f41a0c44SDan Williams 
459f41a0c44SDan Williams 	return phy;
4603ebf6922SDarrick J. Wong }
461f41a0c44SDan Williams EXPORT_SYMBOL_GPL(sas_get_local_phy);
462ad689233SDarrick J. Wong 
4635db45bdcSDan Williams static void sas_wait_eh(struct domain_device *dev)
4645db45bdcSDan Williams {
4655db45bdcSDan Williams 	struct sas_ha_struct *ha = dev->port->ha;
4665db45bdcSDan Williams 	DEFINE_WAIT(wait);
4675db45bdcSDan Williams 
4685db45bdcSDan Williams 	if (dev_is_sata(dev)) {
4695db45bdcSDan Williams 		ata_port_wait_eh(dev->sata_dev.ap);
4705db45bdcSDan Williams 		return;
4715db45bdcSDan Williams 	}
4725db45bdcSDan Williams  retry:
4735db45bdcSDan Williams 	spin_lock_irq(&ha->lock);
4745db45bdcSDan Williams 
4755db45bdcSDan Williams 	while (test_bit(SAS_DEV_EH_PENDING, &dev->state)) {
4765db45bdcSDan Williams 		prepare_to_wait(&ha->eh_wait_q, &wait, TASK_UNINTERRUPTIBLE);
4775db45bdcSDan Williams 		spin_unlock_irq(&ha->lock);
4785db45bdcSDan Williams 		schedule();
4795db45bdcSDan Williams 		spin_lock_irq(&ha->lock);
4805db45bdcSDan Williams 	}
4815db45bdcSDan Williams 	finish_wait(&ha->eh_wait_q, &wait);
4825db45bdcSDan Williams 
4835db45bdcSDan Williams 	spin_unlock_irq(&ha->lock);
4845db45bdcSDan Williams 
4855db45bdcSDan Williams 	/* make sure SCSI EH is complete */
4865db45bdcSDan Williams 	if (scsi_host_in_recovery(ha->core.shost)) {
4875db45bdcSDan Williams 		msleep(10);
4885db45bdcSDan Williams 		goto retry;
4895db45bdcSDan Williams 	}
4905db45bdcSDan Williams }
4915db45bdcSDan Williams EXPORT_SYMBOL(sas_wait_eh);
4925db45bdcSDan Williams 
4935db45bdcSDan Williams static int sas_queue_reset(struct domain_device *dev, int reset_type, int lun, int wait)
4945db45bdcSDan Williams {
4955db45bdcSDan Williams 	struct sas_ha_struct *ha = dev->port->ha;
4965db45bdcSDan Williams 	int scheduled = 0, tries = 100;
4975db45bdcSDan Williams 
4985db45bdcSDan Williams 	/* ata: promote lun reset to bus reset */
4995db45bdcSDan Williams 	if (dev_is_sata(dev)) {
5005db45bdcSDan Williams 		sas_ata_schedule_reset(dev);
5015db45bdcSDan Williams 		if (wait)
5025db45bdcSDan Williams 			sas_ata_wait_eh(dev);
5035db45bdcSDan Williams 		return SUCCESS;
5045db45bdcSDan Williams 	}
5055db45bdcSDan Williams 
5065db45bdcSDan Williams 	while (!scheduled && tries--) {
5075db45bdcSDan Williams 		spin_lock_irq(&ha->lock);
5085db45bdcSDan Williams 		if (!test_bit(SAS_DEV_EH_PENDING, &dev->state) &&
5095db45bdcSDan Williams 		    !test_bit(reset_type, &dev->state)) {
5105db45bdcSDan Williams 			scheduled = 1;
5115db45bdcSDan Williams 			ha->eh_active++;
5125db45bdcSDan Williams 			list_add_tail(&dev->ssp_dev.eh_list_node, &ha->eh_dev_q);
5135db45bdcSDan Williams 			set_bit(SAS_DEV_EH_PENDING, &dev->state);
5145db45bdcSDan Williams 			set_bit(reset_type, &dev->state);
5155db45bdcSDan Williams 			int_to_scsilun(lun, &dev->ssp_dev.reset_lun);
5165db45bdcSDan Williams 			scsi_schedule_eh(ha->core.shost);
5175db45bdcSDan Williams 		}
5185db45bdcSDan Williams 		spin_unlock_irq(&ha->lock);
5195db45bdcSDan Williams 
5205db45bdcSDan Williams 		if (wait)
5215db45bdcSDan Williams 			sas_wait_eh(dev);
5225db45bdcSDan Williams 
5235db45bdcSDan Williams 		if (scheduled)
5245db45bdcSDan Williams 			return SUCCESS;
5255db45bdcSDan Williams 	}
5265db45bdcSDan Williams 
5275db45bdcSDan Williams 	SAS_DPRINTK("%s reset of %s failed\n",
5285db45bdcSDan Williams 		    reset_type == SAS_DEV_LU_RESET ? "LUN" : "Bus",
5295db45bdcSDan Williams 		    dev_name(&dev->rphy->dev));
5305db45bdcSDan Williams 
5315db45bdcSDan Williams 	return FAILED;
5325db45bdcSDan Williams }
5335db45bdcSDan Williams 
5349524c682SDan Williams int sas_eh_abort_handler(struct scsi_cmnd *cmd)
5359524c682SDan Williams {
5369524c682SDan Williams 	int res;
5379524c682SDan Williams 	struct sas_task *task = TO_SAS_TASK(cmd);
5389524c682SDan Williams 	struct Scsi_Host *host = cmd->device->host;
5399524c682SDan Williams 	struct sas_internal *i = to_sas_internal(host->transportt);
5409524c682SDan Williams 
5419524c682SDan Williams 	if (current != host->ehandler)
5429524c682SDan Williams 		return FAILED;
5439524c682SDan Williams 
5449524c682SDan Williams 	if (!i->dft->lldd_abort_task)
5459524c682SDan Williams 		return FAILED;
5469524c682SDan Williams 
5479524c682SDan Williams 	res = i->dft->lldd_abort_task(task);
5489524c682SDan Williams 	if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE)
5499524c682SDan Williams 		return SUCCESS;
5509524c682SDan Williams 
5519524c682SDan Williams 	return FAILED;
5529524c682SDan Williams }
5539524c682SDan Williams EXPORT_SYMBOL_GPL(sas_eh_abort_handler);
5549524c682SDan Williams 
555a9344e68SDarrick J. Wong /* Attempt to send a LUN reset message to a device */
556ad689233SDarrick J. Wong int sas_eh_device_reset_handler(struct scsi_cmnd *cmd)
5572908d778SJames Bottomley {
558a9344e68SDarrick J. Wong 	int res;
5595db45bdcSDan Williams 	struct scsi_lun lun;
5605db45bdcSDan Williams 	struct Scsi_Host *host = cmd->device->host;
5615db45bdcSDan Williams 	struct domain_device *dev = cmd_to_domain_dev(cmd);
5625db45bdcSDan Williams 	struct sas_internal *i = to_sas_internal(host->transportt);
5635db45bdcSDan Williams 
5645db45bdcSDan Williams 	if (current != host->ehandler)
5655db45bdcSDan Williams 		return sas_queue_reset(dev, SAS_DEV_LU_RESET, cmd->device->lun, 0);
566a9344e68SDarrick J. Wong 
567a9344e68SDarrick J. Wong 	int_to_scsilun(cmd->device->lun, &lun);
568a9344e68SDarrick J. Wong 
569a9344e68SDarrick J. Wong 	if (!i->dft->lldd_lu_reset)
570a9344e68SDarrick J. Wong 		return FAILED;
571a9344e68SDarrick J. Wong 
572a9344e68SDarrick J. Wong 	res = i->dft->lldd_lu_reset(dev, lun.scsi_lun);
573a9344e68SDarrick J. Wong 	if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE)
574a9344e68SDarrick J. Wong 		return SUCCESS;
575a9344e68SDarrick J. Wong 
576a9344e68SDarrick J. Wong 	return FAILED;
577a9344e68SDarrick J. Wong }
578a9344e68SDarrick J. Wong 
579a9344e68SDarrick J. Wong int sas_eh_bus_reset_handler(struct scsi_cmnd *cmd)
580a9344e68SDarrick J. Wong {
581ad689233SDarrick J. Wong 	int res;
582e7db8229SDan Williams 	struct Scsi_Host *host = cmd->device->host;
583e7db8229SDan Williams 	struct domain_device *dev = cmd_to_domain_dev(cmd);
584e7db8229SDan Williams 	struct sas_internal *i = to_sas_internal(host->transportt);
585ad689233SDarrick J. Wong 
5865db45bdcSDan Williams 	if (current != host->ehandler)
5875db45bdcSDan Williams 		return sas_queue_reset(dev, SAS_DEV_RESET, 0, 0);
5885db45bdcSDan Williams 
589e7db8229SDan Williams 	if (!i->dft->lldd_I_T_nexus_reset)
590e7db8229SDan Williams 		return FAILED;
591f41a0c44SDan Williams 
592e7db8229SDan Williams 	res = i->dft->lldd_I_T_nexus_reset(dev);
593e7db8229SDan Williams 	if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE ||
594e7db8229SDan Williams 	    res == -ENODEV)
595ad689233SDarrick J. Wong 		return SUCCESS;
596ad689233SDarrick J. Wong 
597ad689233SDarrick J. Wong 	return FAILED;
598ad689233SDarrick J. Wong }
599ad689233SDarrick J. Wong 
600ad689233SDarrick J. Wong /* Try to reset a device */
6018de3ef25SJames Bottomley static int try_to_reset_cmd_device(struct scsi_cmnd *cmd)
602ad689233SDarrick J. Wong {
603a9344e68SDarrick J. Wong 	int res;
6048de3ef25SJames Bottomley 	struct Scsi_Host *shost = cmd->device->host;
605ad689233SDarrick J. Wong 
606a9344e68SDarrick J. Wong 	if (!shost->hostt->eh_device_reset_handler)
607a9344e68SDarrick J. Wong 		goto try_bus_reset;
608a9344e68SDarrick J. Wong 
609a9344e68SDarrick J. Wong 	res = shost->hostt->eh_device_reset_handler(cmd);
610a9344e68SDarrick J. Wong 	if (res == SUCCESS)
611a9344e68SDarrick J. Wong 		return res;
612a9344e68SDarrick J. Wong 
613a9344e68SDarrick J. Wong try_bus_reset:
614a9344e68SDarrick J. Wong 	if (shost->hostt->eh_bus_reset_handler)
615a9344e68SDarrick J. Wong 		return shost->hostt->eh_bus_reset_handler(cmd);
616a9344e68SDarrick J. Wong 
617a9344e68SDarrick J. Wong 	return FAILED;
618ad689233SDarrick J. Wong }
619ad689233SDarrick J. Wong 
62084023474SDan Williams static void sas_eh_handle_sas_errors(struct Scsi_Host *shost, struct list_head *work_q)
621ad689233SDarrick J. Wong {
6222908d778SJames Bottomley 	struct scsi_cmnd *cmd, *n;
6232908d778SJames Bottomley 	enum task_disposition res = TASK_IS_DONE;
6243ebf6922SDarrick J. Wong 	int tmf_resp, need_reset;
6252908d778SJames Bottomley 	struct sas_internal *i = to_sas_internal(shost->transportt);
626ad689233SDarrick J. Wong 	unsigned long flags;
627ad689233SDarrick J. Wong 	struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
62845c73b65SDan Williams 	LIST_HEAD(done);
6292908d778SJames Bottomley 
63045c73b65SDan Williams 	/* clean out any commands that won the completion vs eh race */
631ad689233SDarrick J. Wong 	list_for_each_entry_safe(cmd, n, work_q, eh_entry) {
6329095a64aSDan Williams 		struct domain_device *dev = cmd_to_domain_dev(cmd);
6339095a64aSDan Williams 		struct sas_task *task;
6349095a64aSDan Williams 
6359095a64aSDan Williams 		spin_lock_irqsave(&dev->done_lock, flags);
6369095a64aSDan Williams 		/* by this point the lldd has either observed
6379095a64aSDan Williams 		 * SAS_HA_FROZEN and is leaving the task alone, or has
6389095a64aSDan Williams 		 * won the race with eh and decided to complete it
6399095a64aSDan Williams 		 */
6409095a64aSDan Williams 		task = TO_SAS_TASK(cmd);
6419095a64aSDan Williams 		spin_unlock_irqrestore(&dev->done_lock, flags);
642f456393eSDarrick J. Wong 
643ad689233SDarrick J. Wong 		if (!task)
64445c73b65SDan Williams 			list_move_tail(&cmd->eh_entry, &done);
64545c73b65SDan Williams 	}
64645c73b65SDan Williams 
64745c73b65SDan Williams  Again:
64845c73b65SDan Williams 	list_for_each_entry_safe(cmd, n, work_q, eh_entry) {
64945c73b65SDan Williams 		struct sas_task *task = TO_SAS_TASK(cmd);
650ad689233SDarrick J. Wong 
651ad689233SDarrick J. Wong 		list_del_init(&cmd->eh_entry);
6523ebf6922SDarrick J. Wong 
6533ebf6922SDarrick J. Wong 		spin_lock_irqsave(&task->task_state_lock, flags);
6543ebf6922SDarrick J. Wong 		need_reset = task->task_state_flags & SAS_TASK_NEED_DEV_RESET;
6553ebf6922SDarrick J. Wong 		spin_unlock_irqrestore(&task->task_state_lock, flags);
6563ebf6922SDarrick J. Wong 
6578de3ef25SJames Bottomley 		if (need_reset) {
6588de3ef25SJames Bottomley 			SAS_DPRINTK("%s: task 0x%p requests reset\n",
659cadbd4a5SHarvey Harrison 				    __func__, task);
6608de3ef25SJames Bottomley 			goto reset;
6618de3ef25SJames Bottomley 		}
6628de3ef25SJames Bottomley 
663f456393eSDarrick J. Wong 		SAS_DPRINTK("trying to find task 0x%p\n", task);
6642908d778SJames Bottomley 		res = sas_scsi_find_task(task);
6652908d778SJames Bottomley 
6662908d778SJames Bottomley 		cmd->eh_eflags = 0;
6672908d778SJames Bottomley 
6682908d778SJames Bottomley 		switch (res) {
6699095a64aSDan Williams 		case TASK_IS_NOT_AT_HA:
6709095a64aSDan Williams 			SAS_DPRINTK("%s: task 0x%p is not at ha: %s\n",
6719095a64aSDan Williams 				    __func__, task,
6729095a64aSDan Williams 				    cmd->retries ? "retry" : "aborted");
6739095a64aSDan Williams 			if (cmd->retries)
6749095a64aSDan Williams 				cmd->retries--;
6759095a64aSDan Williams 			sas_eh_finish_cmd(cmd);
6769095a64aSDan Williams 			continue;
6772908d778SJames Bottomley 		case TASK_IS_DONE:
678cadbd4a5SHarvey Harrison 			SAS_DPRINTK("%s: task 0x%p is done\n", __func__,
6792908d778SJames Bottomley 				    task);
6803944f509SDan Williams 			sas_eh_defer_cmd(cmd);
6812908d778SJames Bottomley 			continue;
6822908d778SJames Bottomley 		case TASK_IS_ABORTED:
6832908d778SJames Bottomley 			SAS_DPRINTK("%s: task 0x%p is aborted\n",
684cadbd4a5SHarvey Harrison 				    __func__, task);
6853944f509SDan Williams 			sas_eh_defer_cmd(cmd);
6862908d778SJames Bottomley 			continue;
6872908d778SJames Bottomley 		case TASK_IS_AT_LU:
6882908d778SJames Bottomley 			SAS_DPRINTK("task 0x%p is at LU: lu recover\n", task);
6898de3ef25SJames Bottomley  reset:
6902908d778SJames Bottomley 			tmf_resp = sas_recover_lu(task->dev, cmd);
6912908d778SJames Bottomley 			if (tmf_resp == TMF_RESP_FUNC_COMPLETE) {
6922908d778SJames Bottomley 				SAS_DPRINTK("dev %016llx LU %x is "
6932908d778SJames Bottomley 					    "recovered\n",
6942908d778SJames Bottomley 					    SAS_ADDR(task->dev),
6952908d778SJames Bottomley 					    cmd->device->lun);
6963a2cdf39SDan Williams 				sas_eh_defer_cmd(cmd);
697ad689233SDarrick J. Wong 				sas_scsi_clear_queue_lu(work_q, cmd);
6982908d778SJames Bottomley 				goto Again;
6992908d778SJames Bottomley 			}
7002908d778SJames Bottomley 			/* fallthrough */
7012908d778SJames Bottomley 		case TASK_IS_NOT_AT_LU:
70202cd743bSDarrick J. Wong 		case TASK_ABORT_FAILED:
7032908d778SJames Bottomley 			SAS_DPRINTK("task 0x%p is not at LU: I_T recover\n",
7042908d778SJames Bottomley 				    task);
7052908d778SJames Bottomley 			tmf_resp = sas_recover_I_T(task->dev);
70626a2e68fSDan Williams 			if (tmf_resp == TMF_RESP_FUNC_COMPLETE ||
70726a2e68fSDan Williams 			    tmf_resp == -ENODEV) {
7088de3ef25SJames Bottomley 				struct domain_device *dev = task->dev;
7092908d778SJames Bottomley 				SAS_DPRINTK("I_T %016llx recovered\n",
7102908d778SJames Bottomley 					    SAS_ADDR(task->dev->sas_addr));
711a8e14fecSJames Bottomley 				sas_eh_finish_cmd(cmd);
7128de3ef25SJames Bottomley 				sas_scsi_clear_queue_I_T(work_q, dev);
7132908d778SJames Bottomley 				goto Again;
7142908d778SJames Bottomley 			}
7152908d778SJames Bottomley 			/* Hammer time :-) */
7168de3ef25SJames Bottomley 			try_to_reset_cmd_device(cmd);
7172908d778SJames Bottomley 			if (i->dft->lldd_clear_nexus_port) {
7182908d778SJames Bottomley 				struct asd_sas_port *port = task->dev->port;
7192908d778SJames Bottomley 				SAS_DPRINTK("clearing nexus for port:%d\n",
7202908d778SJames Bottomley 					    port->id);
7212908d778SJames Bottomley 				res = i->dft->lldd_clear_nexus_port(port);
7222908d778SJames Bottomley 				if (res == TMF_RESP_FUNC_COMPLETE) {
7232908d778SJames Bottomley 					SAS_DPRINTK("clear nexus port:%d "
7242908d778SJames Bottomley 						    "succeeded\n", port->id);
725a8e14fecSJames Bottomley 					sas_eh_finish_cmd(cmd);
726ad689233SDarrick J. Wong 					sas_scsi_clear_queue_port(work_q,
7272908d778SJames Bottomley 								  port);
7282908d778SJames Bottomley 					goto Again;
7292908d778SJames Bottomley 				}
7302908d778SJames Bottomley 			}
7312908d778SJames Bottomley 			if (i->dft->lldd_clear_nexus_ha) {
7322908d778SJames Bottomley 				SAS_DPRINTK("clear nexus ha\n");
7332908d778SJames Bottomley 				res = i->dft->lldd_clear_nexus_ha(ha);
7342908d778SJames Bottomley 				if (res == TMF_RESP_FUNC_COMPLETE) {
7352908d778SJames Bottomley 					SAS_DPRINTK("clear nexus ha "
7362908d778SJames Bottomley 						    "succeeded\n");
737a8e14fecSJames Bottomley 					sas_eh_finish_cmd(cmd);
738a8e14fecSJames Bottomley 					goto clear_q;
7392908d778SJames Bottomley 				}
7402908d778SJames Bottomley 			}
7412908d778SJames Bottomley 			/* If we are here -- this means that no amount
7422908d778SJames Bottomley 			 * of effort could recover from errors.  Quite
7432908d778SJames Bottomley 			 * possibly the HA just disappeared.
7442908d778SJames Bottomley 			 */
7452908d778SJames Bottomley 			SAS_DPRINTK("error from  device %llx, LUN %x "
7462908d778SJames Bottomley 				    "couldn't be recovered in any way\n",
7472908d778SJames Bottomley 				    SAS_ADDR(task->dev->sas_addr),
7482908d778SJames Bottomley 				    cmd->device->lun);
7492908d778SJames Bottomley 
750a8e14fecSJames Bottomley 			sas_eh_finish_cmd(cmd);
7512908d778SJames Bottomley 			goto clear_q;
7522908d778SJames Bottomley 		}
7532908d778SJames Bottomley 	}
75445c73b65SDan Williams  out:
75545c73b65SDan Williams 	list_splice_tail(&done, work_q);
7563944f509SDan Williams 	list_splice_tail_init(&ha->eh_ata_q, work_q);
75784023474SDan Williams 	return;
75845c73b65SDan Williams 
7592908d778SJames Bottomley  clear_q:
760cadbd4a5SHarvey Harrison 	SAS_DPRINTK("--- Exit %s -- clear_q\n", __func__);
761a8e14fecSJames Bottomley 	list_for_each_entry_safe(cmd, n, work_q, eh_entry)
762a8e14fecSJames Bottomley 		sas_eh_finish_cmd(cmd);
76345c73b65SDan Williams 	goto out;
764ad689233SDarrick J. Wong }
765ad689233SDarrick J. Wong 
7665db45bdcSDan Williams static void sas_eh_handle_resets(struct Scsi_Host *shost)
7675db45bdcSDan Williams {
7685db45bdcSDan Williams 	struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
7695db45bdcSDan Williams 	struct sas_internal *i = to_sas_internal(shost->transportt);
7705db45bdcSDan Williams 
7715db45bdcSDan Williams 	/* handle directed resets to sas devices */
7725db45bdcSDan Williams 	spin_lock_irq(&ha->lock);
7735db45bdcSDan Williams 	while (!list_empty(&ha->eh_dev_q)) {
7745db45bdcSDan Williams 		struct domain_device *dev;
7755db45bdcSDan Williams 		struct ssp_device *ssp;
7765db45bdcSDan Williams 
7775db45bdcSDan Williams 		ssp = list_entry(ha->eh_dev_q.next, typeof(*ssp), eh_list_node);
7785db45bdcSDan Williams 		list_del_init(&ssp->eh_list_node);
7795db45bdcSDan Williams 		dev = container_of(ssp, typeof(*dev), ssp_dev);
7805db45bdcSDan Williams 		kref_get(&dev->kref);
7815db45bdcSDan Williams 		WARN_ONCE(dev_is_sata(dev), "ssp reset to ata device?\n");
7825db45bdcSDan Williams 
7835db45bdcSDan Williams 		spin_unlock_irq(&ha->lock);
7845db45bdcSDan Williams 
7855db45bdcSDan Williams 		if (test_and_clear_bit(SAS_DEV_LU_RESET, &dev->state))
7865db45bdcSDan Williams 			i->dft->lldd_lu_reset(dev, ssp->reset_lun.scsi_lun);
7875db45bdcSDan Williams 
7885db45bdcSDan Williams 		if (test_and_clear_bit(SAS_DEV_RESET, &dev->state))
7895db45bdcSDan Williams 			i->dft->lldd_I_T_nexus_reset(dev);
7905db45bdcSDan Williams 
7915db45bdcSDan Williams 		sas_put_device(dev);
7925db45bdcSDan Williams 		spin_lock_irq(&ha->lock);
7935db45bdcSDan Williams 		clear_bit(SAS_DEV_EH_PENDING, &dev->state);
7945db45bdcSDan Williams 		ha->eh_active--;
7955db45bdcSDan Williams 	}
7965db45bdcSDan Williams 	spin_unlock_irq(&ha->lock);
7975db45bdcSDan Williams }
7985db45bdcSDan Williams 
799e4a9c373SDan Williams 
800ad689233SDarrick J. Wong void sas_scsi_recover_host(struct Scsi_Host *shost)
801ad689233SDarrick J. Wong {
802ad689233SDarrick J. Wong 	struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
803ad689233SDarrick J. Wong 	LIST_HEAD(eh_work_q);
804e4a9c373SDan Williams 	int tries = 0;
805e4a9c373SDan Williams 	bool retry;
806ad689233SDarrick J. Wong 
807e4a9c373SDan Williams retry:
808e4a9c373SDan Williams 	tries++;
809e4a9c373SDan Williams 	retry = true;
810e4a9c373SDan Williams 	spin_lock_irq(shost->host_lock);
811ad689233SDarrick J. Wong 	list_splice_init(&shost->eh_cmd_q, &eh_work_q);
812e4a9c373SDan Williams 	spin_unlock_irq(shost->host_lock);
813ad689233SDarrick J. Wong 
814d230ce69SDan Williams 	SAS_DPRINTK("Enter %s busy: %d failed: %d\n",
815d230ce69SDan Williams 		    __func__, shost->host_busy, shost->host_failed);
816ad689233SDarrick J. Wong 	/*
817ad689233SDarrick J. Wong 	 * Deal with commands that still have SAS tasks (i.e. they didn't
81884023474SDan Williams 	 * complete via the normal sas_task completion mechanism),
81984023474SDan Williams 	 * SAS_HA_FROZEN gives eh dominion over all sas_task completion.
820ad689233SDarrick J. Wong 	 */
8219095a64aSDan Williams 	set_bit(SAS_HA_FROZEN, &ha->state);
82284023474SDan Williams 	sas_eh_handle_sas_errors(shost, &eh_work_q);
82384023474SDan Williams 	clear_bit(SAS_HA_FROZEN, &ha->state);
82484023474SDan Williams 	if (list_empty(&eh_work_q))
825ad689233SDarrick J. Wong 		goto out;
826ad689233SDarrick J. Wong 
827ad689233SDarrick J. Wong 	/*
828ad689233SDarrick J. Wong 	 * Now deal with SCSI commands that completed ok but have a an error
829ad689233SDarrick J. Wong 	 * code (and hopefully sense data) attached.  This is roughly what
830ad689233SDarrick J. Wong 	 * scsi_unjam_host does, but we skip scsi_eh_abort_cmds because any
831ad689233SDarrick J. Wong 	 * command we see here has no sas_task and is thus unknown to the HA.
832ad689233SDarrick J. Wong 	 */
833d230ce69SDan Williams 	sas_ata_eh(shost, &eh_work_q, &ha->eh_done_q);
834ad689233SDarrick J. Wong 	if (!scsi_eh_get_sense(&eh_work_q, &ha->eh_done_q))
835ad689233SDarrick J. Wong 		scsi_eh_ready_devs(shost, &eh_work_q, &ha->eh_done_q);
836ad689233SDarrick J. Wong 
837ad689233SDarrick J. Wong out:
8389095a64aSDan Williams 	if (ha->lldd_max_execute_num > 1)
8399095a64aSDan Williams 		wake_up_process(ha->core.queue_thread);
8409095a64aSDan Williams 
8415db45bdcSDan Williams 	sas_eh_handle_resets(shost);
8425db45bdcSDan Williams 
84300dd4998SJames Bottomley 	/* now link into libata eh --- if we have any ata devices */
84400dd4998SJames Bottomley 	sas_ata_strategy_handler(shost);
84500dd4998SJames Bottomley 
846ad689233SDarrick J. Wong 	scsi_eh_flush_done_q(&ha->eh_done_q);
84700dd4998SJames Bottomley 
848e4a9c373SDan Williams 	/* check if any new eh work was scheduled during the last run */
849e4a9c373SDan Williams 	spin_lock_irq(&ha->lock);
850e4a9c373SDan Williams 	if (ha->eh_active == 0) {
851e4a9c373SDan Williams 		shost->host_eh_scheduled = 0;
852e4a9c373SDan Williams 		retry = false;
853e4a9c373SDan Williams 	}
854e4a9c373SDan Williams 	spin_unlock_irq(&ha->lock);
855e4a9c373SDan Williams 
856e4a9c373SDan Williams 	if (retry)
857e4a9c373SDan Williams 		goto retry;
858e4a9c373SDan Williams 
859e4a9c373SDan Williams 	SAS_DPRINTK("--- Exit %s: busy: %d failed: %d tries: %d\n",
860e4a9c373SDan Williams 		    __func__, shost->host_busy, shost->host_failed, tries);
8612908d778SJames Bottomley }
8622908d778SJames Bottomley 
863242f9dcbSJens Axboe enum blk_eh_timer_return sas_scsi_timed_out(struct scsi_cmnd *cmd)
8642908d778SJames Bottomley {
8659095a64aSDan Williams 	scmd_printk(KERN_DEBUG, cmd, "command %p timed out\n", cmd);
8662908d778SJames Bottomley 
867242f9dcbSJens Axboe 	return BLK_EH_NOT_HANDLED;
8682908d778SJames Bottomley }
8692908d778SJames Bottomley 
870fa1c1e8fSDarrick J. Wong int sas_ioctl(struct scsi_device *sdev, int cmd, void __user *arg)
871fa1c1e8fSDarrick J. Wong {
872fa1c1e8fSDarrick J. Wong 	struct domain_device *dev = sdev_to_domain_dev(sdev);
873fa1c1e8fSDarrick J. Wong 
874fa1c1e8fSDarrick J. Wong 	if (dev_is_sata(dev))
87594be9a58SJeff Garzik 		return ata_sas_scsi_ioctl(dev->sata_dev.ap, sdev, cmd, arg);
876fa1c1e8fSDarrick J. Wong 
877fa1c1e8fSDarrick J. Wong 	return -EINVAL;
878fa1c1e8fSDarrick J. Wong }
879fa1c1e8fSDarrick J. Wong 
8802908d778SJames Bottomley struct domain_device *sas_find_dev_by_rphy(struct sas_rphy *rphy)
8812908d778SJames Bottomley {
8822908d778SJames Bottomley 	struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent);
8832908d778SJames Bottomley 	struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
8842908d778SJames Bottomley 	struct domain_device *found_dev = NULL;
8852908d778SJames Bottomley 	int i;
886980fa2f9SDarrick J. Wong 	unsigned long flags;
8872908d778SJames Bottomley 
888980fa2f9SDarrick J. Wong 	spin_lock_irqsave(&ha->phy_port_lock, flags);
8892908d778SJames Bottomley 	for (i = 0; i < ha->num_phys; i++) {
8902908d778SJames Bottomley 		struct asd_sas_port *port = ha->sas_port[i];
8912908d778SJames Bottomley 		struct domain_device *dev;
8922908d778SJames Bottomley 
8932908d778SJames Bottomley 		spin_lock(&port->dev_list_lock);
8942908d778SJames Bottomley 		list_for_each_entry(dev, &port->dev_list, dev_list_node) {
8952908d778SJames Bottomley 			if (rphy == dev->rphy) {
8962908d778SJames Bottomley 				found_dev = dev;
8972908d778SJames Bottomley 				spin_unlock(&port->dev_list_lock);
8982908d778SJames Bottomley 				goto found;
8992908d778SJames Bottomley 			}
9002908d778SJames Bottomley 		}
9012908d778SJames Bottomley 		spin_unlock(&port->dev_list_lock);
9022908d778SJames Bottomley 	}
9032908d778SJames Bottomley  found:
904980fa2f9SDarrick J. Wong 	spin_unlock_irqrestore(&ha->phy_port_lock, flags);
9052908d778SJames Bottomley 
9062908d778SJames Bottomley 	return found_dev;
9072908d778SJames Bottomley }
9082908d778SJames Bottomley 
9092908d778SJames Bottomley int sas_target_alloc(struct scsi_target *starget)
9102908d778SJames Bottomley {
911735f7d2fSDan Williams 	struct sas_rphy *rphy = dev_to_rphy(starget->dev.parent);
912735f7d2fSDan Williams 	struct domain_device *found_dev = sas_find_dev_by_rphy(rphy);
9132908d778SJames Bottomley 
9142908d778SJames Bottomley 	if (!found_dev)
9152908d778SJames Bottomley 		return -ENODEV;
9162908d778SJames Bottomley 
917735f7d2fSDan Williams 	kref_get(&found_dev->kref);
9182908d778SJames Bottomley 	starget->hostdata = found_dev;
9192908d778SJames Bottomley 	return 0;
9202908d778SJames Bottomley }
9212908d778SJames Bottomley 
92297a1420dSDan Williams #define SAS_DEF_QD 256
9232908d778SJames Bottomley 
9242908d778SJames Bottomley int sas_slave_configure(struct scsi_device *scsi_dev)
9252908d778SJames Bottomley {
9262908d778SJames Bottomley 	struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
9272908d778SJames Bottomley 	struct sas_ha_struct *sas_ha;
9282908d778SJames Bottomley 
9292908d778SJames Bottomley 	BUG_ON(dev->rphy->identify.device_type != SAS_END_DEVICE);
9302908d778SJames Bottomley 
931fa1c1e8fSDarrick J. Wong 	if (dev_is_sata(dev)) {
932fa1c1e8fSDarrick J. Wong 		ata_sas_slave_configure(scsi_dev, dev->sata_dev.ap);
933fa1c1e8fSDarrick J. Wong 		return 0;
934fa1c1e8fSDarrick J. Wong 	}
935fa1c1e8fSDarrick J. Wong 
9362908d778SJames Bottomley 	sas_ha = dev->port->ha;
9372908d778SJames Bottomley 
9382908d778SJames Bottomley 	sas_read_port_mode_page(scsi_dev);
9392908d778SJames Bottomley 
9402908d778SJames Bottomley 	if (scsi_dev->tagged_supported) {
9412908d778SJames Bottomley 		scsi_set_tag_type(scsi_dev, MSG_SIMPLE_TAG);
9422908d778SJames Bottomley 		scsi_activate_tcq(scsi_dev, SAS_DEF_QD);
9432908d778SJames Bottomley 	} else {
9442908d778SJames Bottomley 		SAS_DPRINTK("device %llx, LUN %x doesn't support "
9452908d778SJames Bottomley 			    "TCQ\n", SAS_ADDR(dev->sas_addr),
9462908d778SJames Bottomley 			    scsi_dev->lun);
9472908d778SJames Bottomley 		scsi_dev->tagged_supported = 0;
9482908d778SJames Bottomley 		scsi_set_tag_type(scsi_dev, 0);
9492908d778SJames Bottomley 		scsi_deactivate_tcq(scsi_dev, 1);
9502908d778SJames Bottomley 	}
9512908d778SJames Bottomley 
952f27708fcSDarrick J. Wong 	scsi_dev->allow_restart = 1;
953f27708fcSDarrick J. Wong 
9542908d778SJames Bottomley 	return 0;
9552908d778SJames Bottomley }
9562908d778SJames Bottomley 
95797a1420dSDan Williams int sas_change_queue_depth(struct scsi_device *sdev, int depth, int reason)
9582908d778SJames Bottomley {
95997a1420dSDan Williams 	struct domain_device *dev = sdev_to_domain_dev(sdev);
9602908d778SJames Bottomley 
961f6e67035SDan Williams 	if (dev_is_sata(dev))
96297a1420dSDan Williams 		return __ata_change_queue_depth(dev->sata_dev.ap, sdev, depth,
96397a1420dSDan Williams 						reason);
964f6e67035SDan Williams 
96597a1420dSDan Williams 	switch (reason) {
96697a1420dSDan Williams 	case SCSI_QDEPTH_DEFAULT:
96797a1420dSDan Williams 	case SCSI_QDEPTH_RAMP_UP:
96897a1420dSDan Williams 		if (!sdev->tagged_supported)
96997a1420dSDan Williams 			depth = 1;
97097a1420dSDan Williams 		scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
97197a1420dSDan Williams 		break;
97297a1420dSDan Williams 	case SCSI_QDEPTH_QFULL:
97397a1420dSDan Williams 		scsi_track_queue_full(sdev, depth);
97497a1420dSDan Williams 		break;
97597a1420dSDan Williams 	default:
976e881a172SMike Christie 		return -EOPNOTSUPP;
9772908d778SJames Bottomley 	}
9782908d778SJames Bottomley 
97997a1420dSDan Williams 	return depth;
9802908d778SJames Bottomley }
9812908d778SJames Bottomley 
9822908d778SJames Bottomley int sas_change_queue_type(struct scsi_device *scsi_dev, int qt)
9832908d778SJames Bottomley {
984f6e67035SDan Williams 	struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
985f6e67035SDan Williams 
986f6e67035SDan Williams 	if (dev_is_sata(dev))
987f6e67035SDan Williams 		return -EINVAL;
988f6e67035SDan Williams 
9892908d778SJames Bottomley 	if (!scsi_dev->tagged_supported)
9902908d778SJames Bottomley 		return 0;
9912908d778SJames Bottomley 
9922908d778SJames Bottomley 	scsi_deactivate_tcq(scsi_dev, 1);
9932908d778SJames Bottomley 
9942908d778SJames Bottomley 	scsi_set_tag_type(scsi_dev, qt);
9952908d778SJames Bottomley 	scsi_activate_tcq(scsi_dev, scsi_dev->queue_depth);
9962908d778SJames Bottomley 
9972908d778SJames Bottomley 	return qt;
9982908d778SJames Bottomley }
9992908d778SJames Bottomley 
10002908d778SJames Bottomley int sas_bios_param(struct scsi_device *scsi_dev,
10012908d778SJames Bottomley 			  struct block_device *bdev,
10022908d778SJames Bottomley 			  sector_t capacity, int *hsc)
10032908d778SJames Bottomley {
10042908d778SJames Bottomley 	hsc[0] = 255;
10052908d778SJames Bottomley 	hsc[1] = 63;
10062908d778SJames Bottomley 	sector_div(capacity, 255*63);
10072908d778SJames Bottomley 	hsc[2] = capacity;
10082908d778SJames Bottomley 
10092908d778SJames Bottomley 	return 0;
10102908d778SJames Bottomley }
10112908d778SJames Bottomley 
10122908d778SJames Bottomley /* ---------- Task Collector Thread implementation ---------- */
10132908d778SJames Bottomley 
10142908d778SJames Bottomley static void sas_queue(struct sas_ha_struct *sas_ha)
10152908d778SJames Bottomley {
10162908d778SJames Bottomley 	struct scsi_core *core = &sas_ha->core;
10172908d778SJames Bottomley 	unsigned long flags;
10182908d778SJames Bottomley 	LIST_HEAD(q);
10192908d778SJames Bottomley 	int can_queue;
10202908d778SJames Bottomley 	int res;
10212908d778SJames Bottomley 	struct sas_internal *i = to_sas_internal(core->shost->transportt);
10222908d778SJames Bottomley 
10239095a64aSDan Williams 	mutex_lock(&core->task_queue_flush);
10242908d778SJames Bottomley 	spin_lock_irqsave(&core->task_queue_lock, flags);
1025d7a54e30SChristoph Hellwig 	while (!kthread_should_stop() &&
10269095a64aSDan Williams 	       !list_empty(&core->task_queue) &&
10279095a64aSDan Williams 	       !test_bit(SAS_HA_FROZEN, &sas_ha->state)) {
10282908d778SJames Bottomley 
10292908d778SJames Bottomley 		can_queue = sas_ha->lldd_queue_size - core->task_queue_size;
10302908d778SJames Bottomley 		if (can_queue >= 0) {
10312908d778SJames Bottomley 			can_queue = core->task_queue_size;
10322908d778SJames Bottomley 			list_splice_init(&core->task_queue, &q);
10332908d778SJames Bottomley 		} else {
10342908d778SJames Bottomley 			struct list_head *a, *n;
10352908d778SJames Bottomley 
10362908d778SJames Bottomley 			can_queue = sas_ha->lldd_queue_size;
10372908d778SJames Bottomley 			list_for_each_safe(a, n, &core->task_queue) {
10382908d778SJames Bottomley 				list_move_tail(a, &q);
10392908d778SJames Bottomley 				if (--can_queue == 0)
10402908d778SJames Bottomley 					break;
10412908d778SJames Bottomley 			}
10422908d778SJames Bottomley 			can_queue = sas_ha->lldd_queue_size;
10432908d778SJames Bottomley 		}
10442908d778SJames Bottomley 		core->task_queue_size -= can_queue;
10452908d778SJames Bottomley 		spin_unlock_irqrestore(&core->task_queue_lock, flags);
10462908d778SJames Bottomley 		{
10472908d778SJames Bottomley 			struct sas_task *task = list_entry(q.next,
10482908d778SJames Bottomley 							   struct sas_task,
10492908d778SJames Bottomley 							   list);
10502908d778SJames Bottomley 			list_del_init(&q);
10512908d778SJames Bottomley 			res = i->dft->lldd_execute_task(task, can_queue,
10522908d778SJames Bottomley 							GFP_KERNEL);
10532908d778SJames Bottomley 			if (unlikely(res))
10542908d778SJames Bottomley 				__list_add(&q, task->list.prev, &task->list);
10552908d778SJames Bottomley 		}
10562908d778SJames Bottomley 		spin_lock_irqsave(&core->task_queue_lock, flags);
10572908d778SJames Bottomley 		if (res) {
10582908d778SJames Bottomley 			list_splice_init(&q, &core->task_queue); /*at head*/
10592908d778SJames Bottomley 			core->task_queue_size += can_queue;
10602908d778SJames Bottomley 		}
10612908d778SJames Bottomley 	}
10622908d778SJames Bottomley 	spin_unlock_irqrestore(&core->task_queue_lock, flags);
10639095a64aSDan Williams 	mutex_unlock(&core->task_queue_flush);
10642908d778SJames Bottomley }
10652908d778SJames Bottomley 
10662908d778SJames Bottomley /**
10672908d778SJames Bottomley  * sas_queue_thread -- The Task Collector thread
10682908d778SJames Bottomley  * @_sas_ha: pointer to struct sas_ha
10692908d778SJames Bottomley  */
10702908d778SJames Bottomley static int sas_queue_thread(void *_sas_ha)
10712908d778SJames Bottomley {
10722908d778SJames Bottomley 	struct sas_ha_struct *sas_ha = _sas_ha;
10732908d778SJames Bottomley 
10742908d778SJames Bottomley 	while (1) {
1075d7a54e30SChristoph Hellwig 		set_current_state(TASK_INTERRUPTIBLE);
1076d7a54e30SChristoph Hellwig 		schedule();
10772908d778SJames Bottomley 		sas_queue(sas_ha);
1078d7a54e30SChristoph Hellwig 		if (kthread_should_stop())
10792908d778SJames Bottomley 			break;
10802908d778SJames Bottomley 	}
10812908d778SJames Bottomley 
10822908d778SJames Bottomley 	return 0;
10832908d778SJames Bottomley }
10842908d778SJames Bottomley 
10852908d778SJames Bottomley int sas_init_queue(struct sas_ha_struct *sas_ha)
10862908d778SJames Bottomley {
10872908d778SJames Bottomley 	struct scsi_core *core = &sas_ha->core;
10882908d778SJames Bottomley 
10892908d778SJames Bottomley 	spin_lock_init(&core->task_queue_lock);
10909095a64aSDan Williams 	mutex_init(&core->task_queue_flush);
10912908d778SJames Bottomley 	core->task_queue_size = 0;
10922908d778SJames Bottomley 	INIT_LIST_HEAD(&core->task_queue);
10932908d778SJames Bottomley 
1094d7a54e30SChristoph Hellwig 	core->queue_thread = kthread_run(sas_queue_thread, sas_ha,
1095d7a54e30SChristoph Hellwig 					 "sas_queue_%d", core->shost->host_no);
1096d7a54e30SChristoph Hellwig 	if (IS_ERR(core->queue_thread))
1097d7a54e30SChristoph Hellwig 		return PTR_ERR(core->queue_thread);
1098d7a54e30SChristoph Hellwig 	return 0;
10992908d778SJames Bottomley }
11002908d778SJames Bottomley 
11012908d778SJames Bottomley void sas_shutdown_queue(struct sas_ha_struct *sas_ha)
11022908d778SJames Bottomley {
11032908d778SJames Bottomley 	unsigned long flags;
11042908d778SJames Bottomley 	struct scsi_core *core = &sas_ha->core;
11052908d778SJames Bottomley 	struct sas_task *task, *n;
11062908d778SJames Bottomley 
1107d7a54e30SChristoph Hellwig 	kthread_stop(core->queue_thread);
11082908d778SJames Bottomley 
11092908d778SJames Bottomley 	if (!list_empty(&core->task_queue))
11102908d778SJames Bottomley 		SAS_DPRINTK("HA: %llx: scsi core task queue is NOT empty!?\n",
11112908d778SJames Bottomley 			    SAS_ADDR(sas_ha->sas_addr));
11122908d778SJames Bottomley 
11132908d778SJames Bottomley 	spin_lock_irqsave(&core->task_queue_lock, flags);
11142908d778SJames Bottomley 	list_for_each_entry_safe(task, n, &core->task_queue, list) {
11152908d778SJames Bottomley 		struct scsi_cmnd *cmd = task->uldd_task;
11162908d778SJames Bottomley 
11172908d778SJames Bottomley 		list_del_init(&task->list);
11182908d778SJames Bottomley 
11192908d778SJames Bottomley 		ASSIGN_SAS_TASK(cmd, NULL);
11202908d778SJames Bottomley 		sas_free_task(task);
11212908d778SJames Bottomley 		cmd->result = DID_ABORT << 16;
11222908d778SJames Bottomley 		cmd->scsi_done(cmd);
11232908d778SJames Bottomley 	}
11242908d778SJames Bottomley 	spin_unlock_irqrestore(&core->task_queue_lock, flags);
11252908d778SJames Bottomley }
11262908d778SJames Bottomley 
1127396819fbSDarrick J. Wong /*
1128396819fbSDarrick J. Wong  * Tell an upper layer that it needs to initiate an abort for a given task.
1129396819fbSDarrick J. Wong  * This should only ever be called by an LLDD.
1130396819fbSDarrick J. Wong  */
1131396819fbSDarrick J. Wong void sas_task_abort(struct sas_task *task)
113279a5eb60SDarrick J. Wong {
1133396819fbSDarrick J. Wong 	struct scsi_cmnd *sc = task->uldd_task;
113479a5eb60SDarrick J. Wong 
1135396819fbSDarrick J. Wong 	/* Escape for libsas internal commands */
1136396819fbSDarrick J. Wong 	if (!sc) {
1137396819fbSDarrick J. Wong 		if (!del_timer(&task->timer))
113879a5eb60SDarrick J. Wong 			return;
1139396819fbSDarrick J. Wong 		task->timer.function(task->timer.data);
1140396819fbSDarrick J. Wong 		return;
1141396819fbSDarrick J. Wong 	}
114279a5eb60SDarrick J. Wong 
11433a2755afSDarrick J. Wong 	if (dev_is_sata(task->dev)) {
11443a2755afSDarrick J. Wong 		sas_ata_task_abort(task);
11451b4d0d8eSJames Bottomley 	} else {
11461b4d0d8eSJames Bottomley 		struct request_queue *q = sc->device->request_queue;
11471b4d0d8eSJames Bottomley 		unsigned long flags;
11483a2755afSDarrick J. Wong 
114970b25f89STejun Heo 		spin_lock_irqsave(q->queue_lock, flags);
1150242f9dcbSJens Axboe 		blk_abort_request(sc->request);
115170b25f89STejun Heo 		spin_unlock_irqrestore(q->queue_lock, flags);
115279a5eb60SDarrick J. Wong 	}
11531b4d0d8eSJames Bottomley }
115479a5eb60SDarrick J. Wong 
1155fa1c1e8fSDarrick J. Wong void sas_target_destroy(struct scsi_target *starget)
1156fa1c1e8fSDarrick J. Wong {
1157735f7d2fSDan Williams 	struct domain_device *found_dev = starget->hostdata;
1158fa1c1e8fSDarrick J. Wong 
1159fa1c1e8fSDarrick J. Wong 	if (!found_dev)
1160fa1c1e8fSDarrick J. Wong 		return;
1161fa1c1e8fSDarrick J. Wong 
1162735f7d2fSDan Williams 	starget->hostdata = NULL;
1163735f7d2fSDan Williams 	sas_put_device(found_dev);
1164fa1c1e8fSDarrick J. Wong }
1165fa1c1e8fSDarrick J. Wong 
116645e6cdf4SDarrick J. Wong static void sas_parse_addr(u8 *sas_addr, const char *p)
116745e6cdf4SDarrick J. Wong {
116845e6cdf4SDarrick J. Wong 	int i;
116945e6cdf4SDarrick J. Wong 	for (i = 0; i < SAS_ADDR_SIZE; i++) {
117045e6cdf4SDarrick J. Wong 		u8 h, l;
117145e6cdf4SDarrick J. Wong 		if (!*p)
117245e6cdf4SDarrick J. Wong 			break;
117345e6cdf4SDarrick J. Wong 		h = isdigit(*p) ? *p-'0' : toupper(*p)-'A'+10;
117445e6cdf4SDarrick J. Wong 		p++;
117545e6cdf4SDarrick J. Wong 		l = isdigit(*p) ? *p-'0' : toupper(*p)-'A'+10;
117645e6cdf4SDarrick J. Wong 		p++;
117745e6cdf4SDarrick J. Wong 		sas_addr[i] = (h<<4) | l;
117845e6cdf4SDarrick J. Wong 	}
117945e6cdf4SDarrick J. Wong }
118045e6cdf4SDarrick J. Wong 
118145e6cdf4SDarrick J. Wong #define SAS_STRING_ADDR_SIZE	16
118245e6cdf4SDarrick J. Wong 
118345e6cdf4SDarrick J. Wong int sas_request_addr(struct Scsi_Host *shost, u8 *addr)
118445e6cdf4SDarrick J. Wong {
118545e6cdf4SDarrick J. Wong 	int res;
118645e6cdf4SDarrick J. Wong 	const struct firmware *fw;
118745e6cdf4SDarrick J. Wong 
118845e6cdf4SDarrick J. Wong 	res = request_firmware(&fw, "sas_addr", &shost->shost_gendev);
118945e6cdf4SDarrick J. Wong 	if (res)
119045e6cdf4SDarrick J. Wong 		return res;
119145e6cdf4SDarrick J. Wong 
119245e6cdf4SDarrick J. Wong 	if (fw->size < SAS_STRING_ADDR_SIZE) {
119345e6cdf4SDarrick J. Wong 		res = -ENODEV;
119445e6cdf4SDarrick J. Wong 		goto out;
119545e6cdf4SDarrick J. Wong 	}
119645e6cdf4SDarrick J. Wong 
119745e6cdf4SDarrick J. Wong 	sas_parse_addr(addr, fw->data);
119845e6cdf4SDarrick J. Wong 
119945e6cdf4SDarrick J. Wong out:
120045e6cdf4SDarrick J. Wong 	release_firmware(fw);
120145e6cdf4SDarrick J. Wong 	return res;
120245e6cdf4SDarrick J. Wong }
120345e6cdf4SDarrick J. Wong EXPORT_SYMBOL_GPL(sas_request_addr);
120445e6cdf4SDarrick J. Wong 
12052908d778SJames Bottomley EXPORT_SYMBOL_GPL(sas_queuecommand);
12062908d778SJames Bottomley EXPORT_SYMBOL_GPL(sas_target_alloc);
12072908d778SJames Bottomley EXPORT_SYMBOL_GPL(sas_slave_configure);
12082908d778SJames Bottomley EXPORT_SYMBOL_GPL(sas_change_queue_depth);
12092908d778SJames Bottomley EXPORT_SYMBOL_GPL(sas_change_queue_type);
12102908d778SJames Bottomley EXPORT_SYMBOL_GPL(sas_bios_param);
121179a5eb60SDarrick J. Wong EXPORT_SYMBOL_GPL(sas_task_abort);
1212dea22214SDarrick J. Wong EXPORT_SYMBOL_GPL(sas_phy_reset);
1213ad689233SDarrick J. Wong EXPORT_SYMBOL_GPL(sas_eh_device_reset_handler);
1214a9344e68SDarrick J. Wong EXPORT_SYMBOL_GPL(sas_eh_bus_reset_handler);
1215fa1c1e8fSDarrick J. Wong EXPORT_SYMBOL_GPL(sas_target_destroy);
1216fa1c1e8fSDarrick J. Wong EXPORT_SYMBOL_GPL(sas_ioctl);
1217