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>
2845e6cdf4SDarrick J. Wong #include <linux/ctype.h>
29d7a54e30SChristoph Hellwig 
302908d778SJames Bottomley #include "sas_internal.h"
312908d778SJames Bottomley 
322908d778SJames Bottomley #include <scsi/scsi_host.h>
332908d778SJames Bottomley #include <scsi/scsi_device.h>
342908d778SJames Bottomley #include <scsi/scsi_tcq.h>
352908d778SJames Bottomley #include <scsi/scsi.h>
36f456393eSDarrick J. Wong #include <scsi/scsi_eh.h>
372908d778SJames Bottomley #include <scsi/scsi_transport.h>
382908d778SJames Bottomley #include <scsi/scsi_transport_sas.h>
39338ec570SDarrick J. Wong #include <scsi/sas_ata.h>
402908d778SJames Bottomley #include "../scsi_sas_internal.h"
4179a5eb60SDarrick J. Wong #include "../scsi_transport_api.h"
42ad689233SDarrick J. Wong #include "../scsi_priv.h"
432908d778SJames Bottomley 
442908d778SJames Bottomley #include <linux/err.h>
452908d778SJames Bottomley #include <linux/blkdev.h>
4683144186SRafael J. Wysocki #include <linux/freezer.h>
475a0e3ad6STejun Heo #include <linux/gfp.h>
482908d778SJames Bottomley #include <linux/scatterlist.h>
49fa1c1e8fSDarrick J. Wong #include <linux/libata.h>
502908d778SJames Bottomley 
512908d778SJames Bottomley /* ---------- SCSI Host glue ---------- */
522908d778SJames Bottomley 
532908d778SJames Bottomley static void sas_scsi_task_done(struct sas_task *task)
542908d778SJames Bottomley {
552908d778SJames Bottomley 	struct task_status_struct *ts = &task->task_status;
562908d778SJames Bottomley 	struct scsi_cmnd *sc = task->uldd_task;
572908d778SJames Bottomley 	int hs = 0, stat = 0;
582908d778SJames Bottomley 
59a8e14fecSJames Bottomley 	if (unlikely(task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
60a8e14fecSJames Bottomley 		/* Aborted tasks will be completed by the error handler */
61a8e14fecSJames Bottomley 		SAS_DPRINTK("task done but aborted\n");
62a8e14fecSJames Bottomley 		return;
63a8e14fecSJames Bottomley 	}
64a8e14fecSJames Bottomley 
652908d778SJames Bottomley 	if (unlikely(!sc)) {
662908d778SJames Bottomley 		SAS_DPRINTK("task_done called with non existing SCSI cmnd!\n");
672908d778SJames Bottomley 		list_del_init(&task->list);
682908d778SJames Bottomley 		sas_free_task(task);
692908d778SJames Bottomley 		return;
702908d778SJames Bottomley 	}
712908d778SJames Bottomley 
722908d778SJames Bottomley 	if (ts->resp == SAS_TASK_UNDELIVERED) {
732908d778SJames Bottomley 		/* transport error */
742908d778SJames Bottomley 		hs = DID_NO_CONNECT;
752908d778SJames Bottomley 	} else { /* ts->resp == SAS_TASK_COMPLETE */
762908d778SJames Bottomley 		/* task delivered, what happened afterwards? */
772908d778SJames Bottomley 		switch (ts->stat) {
782908d778SJames Bottomley 		case SAS_DEV_NO_RESPONSE:
792908d778SJames Bottomley 		case SAS_INTERRUPTED:
802908d778SJames Bottomley 		case SAS_PHY_DOWN:
812908d778SJames Bottomley 		case SAS_NAK_R_ERR:
822908d778SJames Bottomley 		case SAS_OPEN_TO:
832908d778SJames Bottomley 			hs = DID_NO_CONNECT;
842908d778SJames Bottomley 			break;
852908d778SJames Bottomley 		case SAS_DATA_UNDERRUN:
86c13e5566SFUJITA Tomonori 			scsi_set_resid(sc, ts->residual);
87c13e5566SFUJITA Tomonori 			if (scsi_bufflen(sc) - scsi_get_resid(sc) < sc->underflow)
882908d778SJames Bottomley 				hs = DID_ERROR;
892908d778SJames Bottomley 			break;
902908d778SJames Bottomley 		case SAS_DATA_OVERRUN:
912908d778SJames Bottomley 			hs = DID_ERROR;
922908d778SJames Bottomley 			break;
932908d778SJames Bottomley 		case SAS_QUEUE_FULL:
942908d778SJames Bottomley 			hs = DID_SOFT_ERROR; /* retry */
952908d778SJames Bottomley 			break;
962908d778SJames Bottomley 		case SAS_DEVICE_UNKNOWN:
972908d778SJames Bottomley 			hs = DID_BAD_TARGET;
982908d778SJames Bottomley 			break;
992908d778SJames Bottomley 		case SAS_SG_ERR:
1002908d778SJames Bottomley 			hs = DID_PARITY;
1012908d778SJames Bottomley 			break;
1022908d778SJames Bottomley 		case SAS_OPEN_REJECT:
1032908d778SJames Bottomley 			if (ts->open_rej_reason == SAS_OREJ_RSVD_RETRY)
1042908d778SJames Bottomley 				hs = DID_SOFT_ERROR; /* retry */
1052908d778SJames Bottomley 			else
1062908d778SJames Bottomley 				hs = DID_ERROR;
1072908d778SJames Bottomley 			break;
1082908d778SJames Bottomley 		case SAS_PROTO_RESPONSE:
1092908d778SJames Bottomley 			SAS_DPRINTK("LLDD:%s sent SAS_PROTO_RESP for an SSP "
1102908d778SJames Bottomley 				    "task; please report this\n",
1112908d778SJames Bottomley 				    task->dev->port->ha->sas_ha_name);
1122908d778SJames Bottomley 			break;
1132908d778SJames Bottomley 		case SAS_ABORTED_TASK:
1142908d778SJames Bottomley 			hs = DID_ABORT;
1152908d778SJames Bottomley 			break;
116df64d3caSJames Bottomley 		case SAM_STAT_CHECK_CONDITION:
1172908d778SJames Bottomley 			memcpy(sc->sense_buffer, ts->buf,
118cc75e8abSFUJITA Tomonori 			       min(SCSI_SENSE_BUFFERSIZE, ts->buf_valid_size));
119df64d3caSJames Bottomley 			stat = SAM_STAT_CHECK_CONDITION;
1202908d778SJames Bottomley 			break;
1212908d778SJames Bottomley 		default:
1222908d778SJames Bottomley 			stat = ts->stat;
1232908d778SJames Bottomley 			break;
1242908d778SJames Bottomley 		}
1252908d778SJames Bottomley 	}
1262908d778SJames Bottomley 	ASSIGN_SAS_TASK(sc, NULL);
1272908d778SJames Bottomley 	sc->result = (hs << 16) | stat;
1282908d778SJames Bottomley 	list_del_init(&task->list);
1292908d778SJames Bottomley 	sas_free_task(task);
1302908d778SJames Bottomley 	sc->scsi_done(sc);
1312908d778SJames Bottomley }
1322908d778SJames Bottomley 
1332908d778SJames Bottomley static struct sas_task *sas_create_task(struct scsi_cmnd *cmd,
1342908d778SJames Bottomley 					       struct domain_device *dev,
1353cc27547SAl Viro 					       gfp_t gfp_flags)
1362908d778SJames Bottomley {
1372908d778SJames Bottomley 	struct sas_task *task = sas_alloc_task(gfp_flags);
1382908d778SJames Bottomley 	struct scsi_lun lun;
1392908d778SJames Bottomley 
1402908d778SJames Bottomley 	if (!task)
1412908d778SJames Bottomley 		return NULL;
1422908d778SJames Bottomley 
1432908d778SJames Bottomley 	task->uldd_task = cmd;
1442908d778SJames Bottomley 	ASSIGN_SAS_TASK(cmd, task);
1452908d778SJames Bottomley 
1462908d778SJames Bottomley 	task->dev = dev;
1472908d778SJames Bottomley 	task->task_proto = task->dev->tproto; /* BUG_ON(!SSP) */
1482908d778SJames Bottomley 
1492908d778SJames Bottomley 	task->ssp_task.retry_count = 1;
1502908d778SJames Bottomley 	int_to_scsilun(cmd->device->lun, &lun);
1512908d778SJames Bottomley 	memcpy(task->ssp_task.LUN, &lun.scsi_lun, 8);
1529cbbdca4STejun Heo 	task->ssp_task.task_attr = TASK_ATTR_SIMPLE;
1532908d778SJames Bottomley 	memcpy(task->ssp_task.cdb, cmd->cmnd, 16);
1542908d778SJames Bottomley 
155c13e5566SFUJITA Tomonori 	task->scatter = scsi_sglist(cmd);
156c13e5566SFUJITA Tomonori 	task->num_scatter = scsi_sg_count(cmd);
157c13e5566SFUJITA Tomonori 	task->total_xfer_len = scsi_bufflen(cmd);
1582908d778SJames Bottomley 	task->data_dir = cmd->sc_data_direction;
1592908d778SJames Bottomley 
1602908d778SJames Bottomley 	task->task_done = sas_scsi_task_done;
1612908d778SJames Bottomley 
1622908d778SJames Bottomley 	return task;
1632908d778SJames Bottomley }
1642908d778SJames Bottomley 
165338ec570SDarrick J. Wong int sas_queue_up(struct sas_task *task)
1662908d778SJames Bottomley {
1672908d778SJames Bottomley 	struct sas_ha_struct *sas_ha = task->dev->port->ha;
1682908d778SJames Bottomley 	struct scsi_core *core = &sas_ha->core;
1692908d778SJames Bottomley 	unsigned long flags;
1702908d778SJames Bottomley 	LIST_HEAD(list);
1712908d778SJames Bottomley 
1722908d778SJames Bottomley 	spin_lock_irqsave(&core->task_queue_lock, flags);
1732908d778SJames Bottomley 	if (sas_ha->lldd_queue_size < core->task_queue_size + 1) {
1742908d778SJames Bottomley 		spin_unlock_irqrestore(&core->task_queue_lock, flags);
1752908d778SJames Bottomley 		return -SAS_QUEUE_FULL;
1762908d778SJames Bottomley 	}
1772908d778SJames Bottomley 	list_add_tail(&task->list, &core->task_queue);
1782908d778SJames Bottomley 	core->task_queue_size += 1;
1792908d778SJames Bottomley 	spin_unlock_irqrestore(&core->task_queue_lock, flags);
180d7a54e30SChristoph Hellwig 	wake_up_process(core->queue_thread);
1812908d778SJames Bottomley 
1822908d778SJames Bottomley 	return 0;
1832908d778SJames Bottomley }
1842908d778SJames Bottomley 
1852908d778SJames Bottomley /**
1862908d778SJames Bottomley  * sas_queuecommand -- Enqueue a command for processing
1872908d778SJames Bottomley  * @parameters: See SCSI Core documentation
1882908d778SJames Bottomley  *
1892908d778SJames Bottomley  * Note: XXX: Remove the host unlock/lock pair when SCSI Core can
1902908d778SJames Bottomley  * call us without holding an IRQ spinlock...
1912908d778SJames Bottomley  */
1922908d778SJames Bottomley int sas_queuecommand(struct scsi_cmnd *cmd,
1932908d778SJames Bottomley 		     void (*scsi_done)(struct scsi_cmnd *))
1948ee24023SDarrick J. Wong 	__releases(host->host_lock)
1958ee24023SDarrick J. Wong 	__acquires(dev->sata_dev.ap->lock)
1968ee24023SDarrick J. Wong 	__releases(dev->sata_dev.ap->lock)
1978ee24023SDarrick J. Wong 	__acquires(host->host_lock)
1982908d778SJames Bottomley {
1992908d778SJames Bottomley 	int res = 0;
2002908d778SJames Bottomley 	struct domain_device *dev = cmd_to_domain_dev(cmd);
2012908d778SJames Bottomley 	struct Scsi_Host *host = cmd->device->host;
2022908d778SJames Bottomley 	struct sas_internal *i = to_sas_internal(host->transportt);
2032908d778SJames Bottomley 
2042908d778SJames Bottomley 	spin_unlock_irq(host->host_lock);
2052908d778SJames Bottomley 
2062908d778SJames Bottomley 	{
2072908d778SJames Bottomley 		struct sas_ha_struct *sas_ha = dev->port->ha;
2082908d778SJames Bottomley 		struct sas_task *task;
2092908d778SJames Bottomley 
210fa1c1e8fSDarrick J. Wong 		if (dev_is_sata(dev)) {
2113eb7a51aSDarrick J. Wong 			unsigned long flags;
2123eb7a51aSDarrick J. Wong 
2133eb7a51aSDarrick J. Wong 			spin_lock_irqsave(dev->sata_dev.ap->lock, flags);
214fa1c1e8fSDarrick J. Wong 			res = ata_sas_queuecmd(cmd, scsi_done,
215fa1c1e8fSDarrick J. Wong 					       dev->sata_dev.ap);
2163eb7a51aSDarrick J. Wong 			spin_unlock_irqrestore(dev->sata_dev.ap->lock, flags);
217fa1c1e8fSDarrick J. Wong 			goto out;
218fa1c1e8fSDarrick J. Wong 		}
219fa1c1e8fSDarrick J. Wong 
2202908d778SJames Bottomley 		res = -ENOMEM;
2212908d778SJames Bottomley 		task = sas_create_task(cmd, dev, GFP_ATOMIC);
2222908d778SJames Bottomley 		if (!task)
2232908d778SJames Bottomley 			goto out;
2242908d778SJames Bottomley 
2252908d778SJames Bottomley 		cmd->scsi_done = scsi_done;
2262908d778SJames Bottomley 		/* Queue up, Direct Mode or Task Collector Mode. */
2272908d778SJames Bottomley 		if (sas_ha->lldd_max_execute_num < 2)
2282908d778SJames Bottomley 			res = i->dft->lldd_execute_task(task, 1, GFP_ATOMIC);
2292908d778SJames Bottomley 		else
2302908d778SJames Bottomley 			res = sas_queue_up(task);
2312908d778SJames Bottomley 
2322908d778SJames Bottomley 		/* Examine */
2332908d778SJames Bottomley 		if (res) {
2342908d778SJames Bottomley 			SAS_DPRINTK("lldd_execute_task returned: %d\n", res);
2352908d778SJames Bottomley 			ASSIGN_SAS_TASK(cmd, NULL);
2362908d778SJames Bottomley 			sas_free_task(task);
2372908d778SJames Bottomley 			if (res == -SAS_QUEUE_FULL) {
2382908d778SJames Bottomley 				cmd->result = DID_SOFT_ERROR << 16; /* retry */
2392908d778SJames Bottomley 				res = 0;
2402908d778SJames Bottomley 				scsi_done(cmd);
2412908d778SJames Bottomley 			}
2422908d778SJames Bottomley 			goto out;
2432908d778SJames Bottomley 		}
2442908d778SJames Bottomley 	}
2452908d778SJames Bottomley out:
2462908d778SJames Bottomley 	spin_lock_irq(host->host_lock);
2472908d778SJames Bottomley 	return res;
2482908d778SJames Bottomley }
2492908d778SJames Bottomley 
250a8e14fecSJames Bottomley static void sas_eh_finish_cmd(struct scsi_cmnd *cmd)
251a8e14fecSJames Bottomley {
252a8e14fecSJames Bottomley 	struct sas_task *task = TO_SAS_TASK(cmd);
253a8e14fecSJames Bottomley 	struct sas_ha_struct *sas_ha = SHOST_TO_SAS_HA(cmd->device->host);
254a8e14fecSJames Bottomley 
255a8e14fecSJames Bottomley 	/* remove the aborted task flag to allow the task to be
256a8e14fecSJames Bottomley 	 * completed now. At this point, we only get called following
257a8e14fecSJames Bottomley 	 * an actual abort of the task, so we should be guaranteed not
258a8e14fecSJames Bottomley 	 * to be racing with any completions from the LLD (hence we
259a8e14fecSJames Bottomley 	 * don't need the task state lock to clear the flag) */
260a8e14fecSJames Bottomley 	task->task_state_flags &= ~SAS_TASK_STATE_ABORTED;
261a8e14fecSJames Bottomley 	/* Now call task_done.  However, task will be free'd after
262a8e14fecSJames Bottomley 	 * this */
263a8e14fecSJames Bottomley 	task->task_done(task);
264a8e14fecSJames Bottomley 	/* now finish the command and move it on to the error
265a8e14fecSJames Bottomley 	 * handler done list, this also takes it off the
266a8e14fecSJames Bottomley 	 * error handler pending list */
267a8e14fecSJames Bottomley 	scsi_eh_finish_cmd(cmd, &sas_ha->eh_done_q);
268a8e14fecSJames Bottomley }
269a8e14fecSJames Bottomley 
2702908d778SJames Bottomley static void sas_scsi_clear_queue_lu(struct list_head *error_q, struct scsi_cmnd *my_cmd)
2712908d778SJames Bottomley {
2722908d778SJames Bottomley 	struct scsi_cmnd *cmd, *n;
2732908d778SJames Bottomley 
2742908d778SJames Bottomley 	list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
27563e4563bSJames Bottomley 		if (cmd->device->sdev_target == my_cmd->device->sdev_target &&
27663e4563bSJames Bottomley 		    cmd->device->lun == my_cmd->device->lun)
277a8e14fecSJames Bottomley 			sas_eh_finish_cmd(cmd);
2782908d778SJames Bottomley 	}
2792908d778SJames Bottomley }
2802908d778SJames Bottomley 
2812908d778SJames Bottomley static void sas_scsi_clear_queue_I_T(struct list_head *error_q,
2822908d778SJames Bottomley 				     struct domain_device *dev)
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 *x = cmd_to_domain_dev(cmd);
2882908d778SJames Bottomley 
2892908d778SJames Bottomley 		if (x == dev)
290a8e14fecSJames Bottomley 			sas_eh_finish_cmd(cmd);
2912908d778SJames Bottomley 	}
2922908d778SJames Bottomley }
2932908d778SJames Bottomley 
2942908d778SJames Bottomley static void sas_scsi_clear_queue_port(struct list_head *error_q,
2952908d778SJames Bottomley 				      struct asd_sas_port *port)
2962908d778SJames Bottomley {
2972908d778SJames Bottomley 	struct scsi_cmnd *cmd, *n;
2982908d778SJames Bottomley 
2992908d778SJames Bottomley 	list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
3002908d778SJames Bottomley 		struct domain_device *dev = cmd_to_domain_dev(cmd);
3012908d778SJames Bottomley 		struct asd_sas_port *x = dev->port;
3022908d778SJames Bottomley 
3032908d778SJames Bottomley 		if (x == port)
304a8e14fecSJames Bottomley 			sas_eh_finish_cmd(cmd);
3052908d778SJames Bottomley 	}
3062908d778SJames Bottomley }
3072908d778SJames Bottomley 
3082908d778SJames Bottomley enum task_disposition {
3092908d778SJames Bottomley 	TASK_IS_DONE,
3102908d778SJames Bottomley 	TASK_IS_ABORTED,
3112908d778SJames Bottomley 	TASK_IS_AT_LU,
3122908d778SJames Bottomley 	TASK_IS_NOT_AT_LU,
31302cd743bSDarrick J. Wong 	TASK_ABORT_FAILED,
3142908d778SJames Bottomley };
3152908d778SJames Bottomley 
3162908d778SJames Bottomley static enum task_disposition sas_scsi_find_task(struct sas_task *task)
3172908d778SJames Bottomley {
3182908d778SJames Bottomley 	struct sas_ha_struct *ha = task->dev->port->ha;
3192908d778SJames Bottomley 	unsigned long flags;
3202908d778SJames Bottomley 	int i, res;
3212908d778SJames Bottomley 	struct sas_internal *si =
3222908d778SJames Bottomley 		to_sas_internal(task->dev->port->ha->core.shost->transportt);
3232908d778SJames Bottomley 
3242908d778SJames Bottomley 	if (ha->lldd_max_execute_num > 1) {
3252908d778SJames Bottomley 		struct scsi_core *core = &ha->core;
3262908d778SJames Bottomley 		struct sas_task *t, *n;
3272908d778SJames Bottomley 
3282908d778SJames Bottomley 		spin_lock_irqsave(&core->task_queue_lock, flags);
3292908d778SJames Bottomley 		list_for_each_entry_safe(t, n, &core->task_queue, list) {
3302908d778SJames Bottomley 			if (task == t) {
3312908d778SJames Bottomley 				list_del_init(&t->list);
3322908d778SJames Bottomley 				spin_unlock_irqrestore(&core->task_queue_lock,
3332908d778SJames Bottomley 						       flags);
3342908d778SJames Bottomley 				SAS_DPRINTK("%s: task 0x%p aborted from "
3352908d778SJames Bottomley 					    "task_queue\n",
336cadbd4a5SHarvey Harrison 					    __func__, task);
3372908d778SJames Bottomley 				return TASK_IS_ABORTED;
3382908d778SJames Bottomley 			}
3392908d778SJames Bottomley 		}
3402908d778SJames Bottomley 		spin_unlock_irqrestore(&core->task_queue_lock, flags);
3412908d778SJames Bottomley 	}
3422908d778SJames Bottomley 
3432908d778SJames Bottomley 	for (i = 0; i < 5; i++) {
344cadbd4a5SHarvey Harrison 		SAS_DPRINTK("%s: aborting task 0x%p\n", __func__, task);
3452908d778SJames Bottomley 		res = si->dft->lldd_abort_task(task);
3462908d778SJames Bottomley 
3472908d778SJames Bottomley 		spin_lock_irqsave(&task->task_state_lock, flags);
3482908d778SJames Bottomley 		if (task->task_state_flags & SAS_TASK_STATE_DONE) {
3492908d778SJames Bottomley 			spin_unlock_irqrestore(&task->task_state_lock, flags);
350cadbd4a5SHarvey Harrison 			SAS_DPRINTK("%s: task 0x%p is done\n", __func__,
3512908d778SJames Bottomley 				    task);
3522908d778SJames Bottomley 			return TASK_IS_DONE;
3532908d778SJames Bottomley 		}
3542908d778SJames Bottomley 		spin_unlock_irqrestore(&task->task_state_lock, flags);
3552908d778SJames Bottomley 
3562908d778SJames Bottomley 		if (res == TMF_RESP_FUNC_COMPLETE) {
3572908d778SJames Bottomley 			SAS_DPRINTK("%s: task 0x%p is aborted\n",
358cadbd4a5SHarvey Harrison 				    __func__, task);
3592908d778SJames Bottomley 			return TASK_IS_ABORTED;
3602908d778SJames Bottomley 		} else if (si->dft->lldd_query_task) {
3612908d778SJames Bottomley 			SAS_DPRINTK("%s: querying task 0x%p\n",
362cadbd4a5SHarvey Harrison 				    __func__, task);
3632908d778SJames Bottomley 			res = si->dft->lldd_query_task(task);
36402cd743bSDarrick J. Wong 			switch (res) {
36502cd743bSDarrick J. Wong 			case TMF_RESP_FUNC_SUCC:
3662908d778SJames Bottomley 				SAS_DPRINTK("%s: task 0x%p at LU\n",
367cadbd4a5SHarvey Harrison 					    __func__, task);
3682908d778SJames Bottomley 				return TASK_IS_AT_LU;
36902cd743bSDarrick J. Wong 			case TMF_RESP_FUNC_COMPLETE:
3702908d778SJames Bottomley 				SAS_DPRINTK("%s: task 0x%p not at LU\n",
371cadbd4a5SHarvey Harrison 					    __func__, task);
3722908d778SJames Bottomley 				return TASK_IS_NOT_AT_LU;
37302cd743bSDarrick J. Wong 			case TMF_RESP_FUNC_FAILED:
37402cd743bSDarrick J. Wong                                 SAS_DPRINTK("%s: task 0x%p failed to abort\n",
375cadbd4a5SHarvey Harrison                                                 __func__, task);
37602cd743bSDarrick J. Wong                                 return TASK_ABORT_FAILED;
3772908d778SJames Bottomley                         }
37802cd743bSDarrick J. Wong 
3792908d778SJames Bottomley 		}
3802908d778SJames Bottomley 	}
3812908d778SJames Bottomley 	return res;
3822908d778SJames Bottomley }
3832908d778SJames Bottomley 
3842908d778SJames Bottomley static int sas_recover_lu(struct domain_device *dev, struct scsi_cmnd *cmd)
3852908d778SJames Bottomley {
3862908d778SJames Bottomley 	int res = TMF_RESP_FUNC_FAILED;
3872908d778SJames Bottomley 	struct scsi_lun lun;
3882908d778SJames Bottomley 	struct sas_internal *i =
3892908d778SJames Bottomley 		to_sas_internal(dev->port->ha->core.shost->transportt);
3902908d778SJames Bottomley 
3912908d778SJames Bottomley 	int_to_scsilun(cmd->device->lun, &lun);
3922908d778SJames Bottomley 
3932908d778SJames Bottomley 	SAS_DPRINTK("eh: device %llx LUN %x has the task\n",
3942908d778SJames Bottomley 		    SAS_ADDR(dev->sas_addr),
3952908d778SJames Bottomley 		    cmd->device->lun);
3962908d778SJames Bottomley 
3972908d778SJames Bottomley 	if (i->dft->lldd_abort_task_set)
3982908d778SJames Bottomley 		res = i->dft->lldd_abort_task_set(dev, lun.scsi_lun);
3992908d778SJames Bottomley 
4002908d778SJames Bottomley 	if (res == TMF_RESP_FUNC_FAILED) {
4012908d778SJames Bottomley 		if (i->dft->lldd_clear_task_set)
4022908d778SJames Bottomley 			res = i->dft->lldd_clear_task_set(dev, lun.scsi_lun);
4032908d778SJames Bottomley 	}
4042908d778SJames Bottomley 
4052908d778SJames Bottomley 	if (res == TMF_RESP_FUNC_FAILED) {
4062908d778SJames Bottomley 		if (i->dft->lldd_lu_reset)
4072908d778SJames Bottomley 			res = i->dft->lldd_lu_reset(dev, lun.scsi_lun);
4082908d778SJames Bottomley 	}
4092908d778SJames Bottomley 
4102908d778SJames Bottomley 	return res;
4112908d778SJames Bottomley }
4122908d778SJames Bottomley 
4132908d778SJames Bottomley static int sas_recover_I_T(struct domain_device *dev)
4142908d778SJames Bottomley {
4152908d778SJames Bottomley 	int res = TMF_RESP_FUNC_FAILED;
4162908d778SJames Bottomley 	struct sas_internal *i =
4172908d778SJames Bottomley 		to_sas_internal(dev->port->ha->core.shost->transportt);
4182908d778SJames Bottomley 
4192908d778SJames Bottomley 	SAS_DPRINTK("I_T nexus reset for dev %016llx\n",
4202908d778SJames Bottomley 		    SAS_ADDR(dev->sas_addr));
4212908d778SJames Bottomley 
4222908d778SJames Bottomley 	if (i->dft->lldd_I_T_nexus_reset)
4232908d778SJames Bottomley 		res = i->dft->lldd_I_T_nexus_reset(dev);
4242908d778SJames Bottomley 
4252908d778SJames Bottomley 	return res;
4262908d778SJames Bottomley }
4272908d778SJames Bottomley 
428ad689233SDarrick J. Wong /* Find the sas_phy that's attached to this device */
4295319578cSJames Bottomley struct sas_phy *sas_find_local_phy(struct domain_device *dev)
4303ebf6922SDarrick J. Wong {
431ad689233SDarrick J. Wong 	struct domain_device *pdev = dev->parent;
432ad689233SDarrick J. Wong 	struct ex_phy *exphy = NULL;
433ad689233SDarrick J. Wong 	int i;
4343ebf6922SDarrick J. Wong 
435ad689233SDarrick J. Wong 	/* Directly attached device */
436ad689233SDarrick J. Wong 	if (!pdev)
437ad689233SDarrick J. Wong 		return dev->port->phy;
4383ebf6922SDarrick J. Wong 
439ad689233SDarrick J. Wong 	/* Otherwise look in the expander */
440ad689233SDarrick J. Wong 	for (i = 0; i < pdev->ex_dev.num_phys; i++)
441ad689233SDarrick J. Wong 		if (!memcmp(dev->sas_addr,
442ad689233SDarrick J. Wong 			    pdev->ex_dev.ex_phy[i].attached_sas_addr,
443ad689233SDarrick J. Wong 			    SAS_ADDR_SIZE)) {
444ad689233SDarrick J. Wong 			exphy = &pdev->ex_dev.ex_phy[i];
445ad689233SDarrick J. Wong 			break;
4463ebf6922SDarrick J. Wong 		}
4473ebf6922SDarrick J. Wong 
448ad689233SDarrick J. Wong 	BUG_ON(!exphy);
449ad689233SDarrick J. Wong 	return exphy->phy;
450ad689233SDarrick J. Wong }
4515319578cSJames Bottomley EXPORT_SYMBOL_GPL(sas_find_local_phy);
452ad689233SDarrick J. Wong 
453a9344e68SDarrick J. Wong /* Attempt to send a LUN reset message to a device */
454ad689233SDarrick J. Wong int sas_eh_device_reset_handler(struct scsi_cmnd *cmd)
4552908d778SJames Bottomley {
456ad689233SDarrick J. Wong 	struct domain_device *dev = cmd_to_domain_dev(cmd);
457a9344e68SDarrick J. Wong 	struct sas_internal *i =
458a9344e68SDarrick J. Wong 		to_sas_internal(dev->port->ha->core.shost->transportt);
459a9344e68SDarrick J. Wong 	struct scsi_lun lun;
460a9344e68SDarrick J. Wong 	int res;
461a9344e68SDarrick J. Wong 
462a9344e68SDarrick J. Wong 	int_to_scsilun(cmd->device->lun, &lun);
463a9344e68SDarrick J. Wong 
464a9344e68SDarrick J. Wong 	if (!i->dft->lldd_lu_reset)
465a9344e68SDarrick J. Wong 		return FAILED;
466a9344e68SDarrick J. Wong 
467a9344e68SDarrick J. Wong 	res = i->dft->lldd_lu_reset(dev, lun.scsi_lun);
468a9344e68SDarrick J. Wong 	if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE)
469a9344e68SDarrick J. Wong 		return SUCCESS;
470a9344e68SDarrick J. Wong 
471a9344e68SDarrick J. Wong 	return FAILED;
472a9344e68SDarrick J. Wong }
473a9344e68SDarrick J. Wong 
474a9344e68SDarrick J. Wong /* Attempt to send a phy (bus) reset */
475a9344e68SDarrick J. Wong int sas_eh_bus_reset_handler(struct scsi_cmnd *cmd)
476a9344e68SDarrick J. Wong {
477a9344e68SDarrick J. Wong 	struct domain_device *dev = cmd_to_domain_dev(cmd);
4785319578cSJames Bottomley 	struct sas_phy *phy = sas_find_local_phy(dev);
479ad689233SDarrick J. Wong 	int res;
480ad689233SDarrick J. Wong 
481ad689233SDarrick J. Wong 	res = sas_phy_reset(phy, 1);
482ad689233SDarrick J. Wong 	if (res)
483a9344e68SDarrick J. Wong 		SAS_DPRINTK("Bus reset of %s failed 0x%x\n",
484af5ca3f4SKay Sievers 			    kobject_name(&phy->dev.kobj),
485ad689233SDarrick J. Wong 			    res);
486ad689233SDarrick J. Wong 	if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE)
487ad689233SDarrick J. Wong 		return SUCCESS;
488ad689233SDarrick J. Wong 
489ad689233SDarrick J. Wong 	return FAILED;
490ad689233SDarrick J. Wong }
491ad689233SDarrick J. Wong 
492ad689233SDarrick J. Wong /* Try to reset a device */
4938de3ef25SJames Bottomley static int try_to_reset_cmd_device(struct scsi_cmnd *cmd)
494ad689233SDarrick J. Wong {
495a9344e68SDarrick J. Wong 	int res;
4968de3ef25SJames Bottomley 	struct Scsi_Host *shost = cmd->device->host;
497ad689233SDarrick J. Wong 
498a9344e68SDarrick J. Wong 	if (!shost->hostt->eh_device_reset_handler)
499a9344e68SDarrick J. Wong 		goto try_bus_reset;
500a9344e68SDarrick J. Wong 
501a9344e68SDarrick J. Wong 	res = shost->hostt->eh_device_reset_handler(cmd);
502a9344e68SDarrick J. Wong 	if (res == SUCCESS)
503a9344e68SDarrick J. Wong 		return res;
504a9344e68SDarrick J. Wong 
505a9344e68SDarrick J. Wong try_bus_reset:
506a9344e68SDarrick J. Wong 	if (shost->hostt->eh_bus_reset_handler)
507a9344e68SDarrick J. Wong 		return shost->hostt->eh_bus_reset_handler(cmd);
508a9344e68SDarrick J. Wong 
509a9344e68SDarrick J. Wong 	return FAILED;
510ad689233SDarrick J. Wong }
511ad689233SDarrick J. Wong 
512ad689233SDarrick J. Wong static int sas_eh_handle_sas_errors(struct Scsi_Host *shost,
513ad689233SDarrick J. Wong 				    struct list_head *work_q,
514ad689233SDarrick J. Wong 				    struct list_head *done_q)
515ad689233SDarrick J. Wong {
5162908d778SJames Bottomley 	struct scsi_cmnd *cmd, *n;
5172908d778SJames Bottomley 	enum task_disposition res = TASK_IS_DONE;
5183ebf6922SDarrick J. Wong 	int tmf_resp, need_reset;
5192908d778SJames Bottomley 	struct sas_internal *i = to_sas_internal(shost->transportt);
520ad689233SDarrick J. Wong 	unsigned long flags;
521ad689233SDarrick J. Wong 	struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
5222908d778SJames Bottomley 
5232908d778SJames Bottomley Again:
524ad689233SDarrick J. Wong 	list_for_each_entry_safe(cmd, n, work_q, eh_entry) {
5252908d778SJames Bottomley 		struct sas_task *task = TO_SAS_TASK(cmd);
526f456393eSDarrick J. Wong 
527ad689233SDarrick J. Wong 		if (!task)
528f456393eSDarrick J. Wong 			continue;
529ad689233SDarrick J. Wong 
530ad689233SDarrick J. Wong 		list_del_init(&cmd->eh_entry);
5313ebf6922SDarrick J. Wong 
5323ebf6922SDarrick J. Wong 		spin_lock_irqsave(&task->task_state_lock, flags);
5333ebf6922SDarrick J. Wong 		need_reset = task->task_state_flags & SAS_TASK_NEED_DEV_RESET;
5343ebf6922SDarrick J. Wong 		spin_unlock_irqrestore(&task->task_state_lock, flags);
5353ebf6922SDarrick J. Wong 
5368de3ef25SJames Bottomley 		if (need_reset) {
5378de3ef25SJames Bottomley 			SAS_DPRINTK("%s: task 0x%p requests reset\n",
538cadbd4a5SHarvey Harrison 				    __func__, task);
5398de3ef25SJames Bottomley 			goto reset;
5408de3ef25SJames Bottomley 		}
5418de3ef25SJames Bottomley 
542f456393eSDarrick J. Wong 		SAS_DPRINTK("trying to find task 0x%p\n", task);
5432908d778SJames Bottomley 		res = sas_scsi_find_task(task);
5442908d778SJames Bottomley 
5452908d778SJames Bottomley 		cmd->eh_eflags = 0;
5462908d778SJames Bottomley 
5472908d778SJames Bottomley 		switch (res) {
5482908d778SJames Bottomley 		case TASK_IS_DONE:
549cadbd4a5SHarvey Harrison 			SAS_DPRINTK("%s: task 0x%p is done\n", __func__,
5502908d778SJames Bottomley 				    task);
551a8e14fecSJames Bottomley 			sas_eh_finish_cmd(cmd);
5522908d778SJames Bottomley 			continue;
5532908d778SJames Bottomley 		case TASK_IS_ABORTED:
5542908d778SJames Bottomley 			SAS_DPRINTK("%s: task 0x%p is aborted\n",
555cadbd4a5SHarvey Harrison 				    __func__, task);
556a8e14fecSJames Bottomley 			sas_eh_finish_cmd(cmd);
5572908d778SJames Bottomley 			continue;
5582908d778SJames Bottomley 		case TASK_IS_AT_LU:
5592908d778SJames Bottomley 			SAS_DPRINTK("task 0x%p is at LU: lu recover\n", task);
5608de3ef25SJames Bottomley  reset:
5612908d778SJames Bottomley 			tmf_resp = sas_recover_lu(task->dev, cmd);
5622908d778SJames Bottomley 			if (tmf_resp == TMF_RESP_FUNC_COMPLETE) {
5632908d778SJames Bottomley 				SAS_DPRINTK("dev %016llx LU %x is "
5642908d778SJames Bottomley 					    "recovered\n",
5652908d778SJames Bottomley 					    SAS_ADDR(task->dev),
5662908d778SJames Bottomley 					    cmd->device->lun);
567a8e14fecSJames Bottomley 				sas_eh_finish_cmd(cmd);
568ad689233SDarrick J. Wong 				sas_scsi_clear_queue_lu(work_q, cmd);
5692908d778SJames Bottomley 				goto Again;
5702908d778SJames Bottomley 			}
5712908d778SJames Bottomley 			/* fallthrough */
5722908d778SJames Bottomley 		case TASK_IS_NOT_AT_LU:
57302cd743bSDarrick J. Wong 		case TASK_ABORT_FAILED:
5742908d778SJames Bottomley 			SAS_DPRINTK("task 0x%p is not at LU: I_T recover\n",
5752908d778SJames Bottomley 				    task);
5762908d778SJames Bottomley 			tmf_resp = sas_recover_I_T(task->dev);
5772908d778SJames Bottomley 			if (tmf_resp == TMF_RESP_FUNC_COMPLETE) {
5788de3ef25SJames Bottomley 				struct domain_device *dev = task->dev;
5792908d778SJames Bottomley 				SAS_DPRINTK("I_T %016llx recovered\n",
5802908d778SJames Bottomley 					    SAS_ADDR(task->dev->sas_addr));
581a8e14fecSJames Bottomley 				sas_eh_finish_cmd(cmd);
5828de3ef25SJames Bottomley 				sas_scsi_clear_queue_I_T(work_q, dev);
5832908d778SJames Bottomley 				goto Again;
5842908d778SJames Bottomley 			}
5852908d778SJames Bottomley 			/* Hammer time :-) */
5868de3ef25SJames Bottomley 			try_to_reset_cmd_device(cmd);
5872908d778SJames Bottomley 			if (i->dft->lldd_clear_nexus_port) {
5882908d778SJames Bottomley 				struct asd_sas_port *port = task->dev->port;
5892908d778SJames Bottomley 				SAS_DPRINTK("clearing nexus for port:%d\n",
5902908d778SJames Bottomley 					    port->id);
5912908d778SJames Bottomley 				res = i->dft->lldd_clear_nexus_port(port);
5922908d778SJames Bottomley 				if (res == TMF_RESP_FUNC_COMPLETE) {
5932908d778SJames Bottomley 					SAS_DPRINTK("clear nexus port:%d "
5942908d778SJames Bottomley 						    "succeeded\n", port->id);
595a8e14fecSJames Bottomley 					sas_eh_finish_cmd(cmd);
596ad689233SDarrick J. Wong 					sas_scsi_clear_queue_port(work_q,
5972908d778SJames Bottomley 								  port);
5982908d778SJames Bottomley 					goto Again;
5992908d778SJames Bottomley 				}
6002908d778SJames Bottomley 			}
6012908d778SJames Bottomley 			if (i->dft->lldd_clear_nexus_ha) {
6022908d778SJames Bottomley 				SAS_DPRINTK("clear nexus ha\n");
6032908d778SJames Bottomley 				res = i->dft->lldd_clear_nexus_ha(ha);
6042908d778SJames Bottomley 				if (res == TMF_RESP_FUNC_COMPLETE) {
6052908d778SJames Bottomley 					SAS_DPRINTK("clear nexus ha "
6062908d778SJames Bottomley 						    "succeeded\n");
607a8e14fecSJames Bottomley 					sas_eh_finish_cmd(cmd);
608a8e14fecSJames Bottomley 					goto clear_q;
6092908d778SJames Bottomley 				}
6102908d778SJames Bottomley 			}
6112908d778SJames Bottomley 			/* If we are here -- this means that no amount
6122908d778SJames Bottomley 			 * of effort could recover from errors.  Quite
6132908d778SJames Bottomley 			 * possibly the HA just disappeared.
6142908d778SJames Bottomley 			 */
6152908d778SJames Bottomley 			SAS_DPRINTK("error from  device %llx, LUN %x "
6162908d778SJames Bottomley 				    "couldn't be recovered in any way\n",
6172908d778SJames Bottomley 				    SAS_ADDR(task->dev->sas_addr),
6182908d778SJames Bottomley 				    cmd->device->lun);
6192908d778SJames Bottomley 
620a8e14fecSJames Bottomley 			sas_eh_finish_cmd(cmd);
6212908d778SJames Bottomley 			goto clear_q;
6222908d778SJames Bottomley 		}
6232908d778SJames Bottomley 	}
624ad689233SDarrick J. Wong 	return list_empty(work_q);
6252908d778SJames Bottomley clear_q:
626cadbd4a5SHarvey Harrison 	SAS_DPRINTK("--- Exit %s -- clear_q\n", __func__);
627a8e14fecSJames Bottomley 	list_for_each_entry_safe(cmd, n, work_q, eh_entry)
628a8e14fecSJames Bottomley 		sas_eh_finish_cmd(cmd);
629a8e14fecSJames Bottomley 
630ad689233SDarrick J. Wong 	return list_empty(work_q);
631ad689233SDarrick J. Wong }
632ad689233SDarrick J. Wong 
633ad689233SDarrick J. Wong void sas_scsi_recover_host(struct Scsi_Host *shost)
634ad689233SDarrick J. Wong {
635ad689233SDarrick J. Wong 	struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
636ad689233SDarrick J. Wong 	unsigned long flags;
637ad689233SDarrick J. Wong 	LIST_HEAD(eh_work_q);
638ad689233SDarrick J. Wong 
639ad689233SDarrick J. Wong 	spin_lock_irqsave(shost->host_lock, flags);
640ad689233SDarrick J. Wong 	list_splice_init(&shost->eh_cmd_q, &eh_work_q);
641ad689233SDarrick J. Wong 	spin_unlock_irqrestore(shost->host_lock, flags);
642ad689233SDarrick J. Wong 
643cadbd4a5SHarvey Harrison 	SAS_DPRINTK("Enter %s\n", __func__);
644ad689233SDarrick J. Wong 	/*
645ad689233SDarrick J. Wong 	 * Deal with commands that still have SAS tasks (i.e. they didn't
646ad689233SDarrick J. Wong 	 * complete via the normal sas_task completion mechanism)
647ad689233SDarrick J. Wong 	 */
648ad689233SDarrick J. Wong 	if (sas_eh_handle_sas_errors(shost, &eh_work_q, &ha->eh_done_q))
649ad689233SDarrick J. Wong 		goto out;
650ad689233SDarrick J. Wong 
651ad689233SDarrick J. Wong 	/*
652ad689233SDarrick J. Wong 	 * Now deal with SCSI commands that completed ok but have a an error
653ad689233SDarrick J. Wong 	 * code (and hopefully sense data) attached.  This is roughly what
654ad689233SDarrick J. Wong 	 * scsi_unjam_host does, but we skip scsi_eh_abort_cmds because any
655ad689233SDarrick J. Wong 	 * command we see here has no sas_task and is thus unknown to the HA.
656ad689233SDarrick J. Wong 	 */
657ad689233SDarrick J. Wong 	if (!scsi_eh_get_sense(&eh_work_q, &ha->eh_done_q))
658ad689233SDarrick J. Wong 		scsi_eh_ready_devs(shost, &eh_work_q, &ha->eh_done_q);
659ad689233SDarrick J. Wong 
660ad689233SDarrick J. Wong out:
661ad689233SDarrick J. Wong 	scsi_eh_flush_done_q(&ha->eh_done_q);
662cadbd4a5SHarvey Harrison 	SAS_DPRINTK("--- Exit %s\n", __func__);
663ad689233SDarrick J. Wong 	return;
6642908d778SJames Bottomley }
6652908d778SJames Bottomley 
666242f9dcbSJens Axboe enum blk_eh_timer_return sas_scsi_timed_out(struct scsi_cmnd *cmd)
6672908d778SJames Bottomley {
6682908d778SJames Bottomley 	struct sas_task *task = TO_SAS_TASK(cmd);
6692908d778SJames Bottomley 	unsigned long flags;
6702908d778SJames Bottomley 
6712908d778SJames Bottomley 	if (!task) {
672242f9dcbSJens Axboe 		cmd->request->timeout /= 2;
6736d4dcd4dSDarrick J. Wong 		SAS_DPRINTK("command 0x%p, task 0x%p, gone: %s\n",
674242f9dcbSJens Axboe 			    cmd, task, (cmd->request->timeout ?
675242f9dcbSJens Axboe 			    "BLK_EH_RESET_TIMER" : "BLK_EH_NOT_HANDLED"));
676242f9dcbSJens Axboe 		if (!cmd->request->timeout)
677242f9dcbSJens Axboe 			return BLK_EH_NOT_HANDLED;
678242f9dcbSJens Axboe 		return BLK_EH_RESET_TIMER;
6792908d778SJames Bottomley 	}
6802908d778SJames Bottomley 
6812908d778SJames Bottomley 	spin_lock_irqsave(&task->task_state_lock, flags);
682396819fbSDarrick J. Wong 	BUG_ON(task->task_state_flags & SAS_TASK_STATE_ABORTED);
6832908d778SJames Bottomley 	if (task->task_state_flags & SAS_TASK_STATE_DONE) {
6842908d778SJames Bottomley 		spin_unlock_irqrestore(&task->task_state_lock, flags);
685242f9dcbSJens Axboe 		SAS_DPRINTK("command 0x%p, task 0x%p, timed out: "
686242f9dcbSJens Axboe 			    "BLK_EH_HANDLED\n", cmd, task);
687242f9dcbSJens Axboe 		return BLK_EH_HANDLED;
6882908d778SJames Bottomley 	}
689b218a0d8SDarrick J. Wong 	if (!(task->task_state_flags & SAS_TASK_AT_INITIATOR)) {
690b218a0d8SDarrick J. Wong 		spin_unlock_irqrestore(&task->task_state_lock, flags);
691b218a0d8SDarrick J. Wong 		SAS_DPRINTK("command 0x%p, task 0x%p, not at initiator: "
692242f9dcbSJens Axboe 			    "BLK_EH_RESET_TIMER\n",
693b218a0d8SDarrick J. Wong 			    cmd, task);
694242f9dcbSJens Axboe 		return BLK_EH_RESET_TIMER;
695b218a0d8SDarrick J. Wong 	}
6962908d778SJames Bottomley 	task->task_state_flags |= SAS_TASK_STATE_ABORTED;
6972908d778SJames Bottomley 	spin_unlock_irqrestore(&task->task_state_lock, flags);
6982908d778SJames Bottomley 
699242f9dcbSJens Axboe 	SAS_DPRINTK("command 0x%p, task 0x%p, timed out: BLK_EH_NOT_HANDLED\n",
7002908d778SJames Bottomley 		    cmd, task);
7012908d778SJames Bottomley 
702242f9dcbSJens Axboe 	return BLK_EH_NOT_HANDLED;
7032908d778SJames Bottomley }
7042908d778SJames Bottomley 
705fa1c1e8fSDarrick J. Wong int sas_ioctl(struct scsi_device *sdev, int cmd, void __user *arg)
706fa1c1e8fSDarrick J. Wong {
707fa1c1e8fSDarrick J. Wong 	struct domain_device *dev = sdev_to_domain_dev(sdev);
708fa1c1e8fSDarrick J. Wong 
709fa1c1e8fSDarrick J. Wong 	if (dev_is_sata(dev))
71094be9a58SJeff Garzik 		return ata_sas_scsi_ioctl(dev->sata_dev.ap, sdev, cmd, arg);
711fa1c1e8fSDarrick J. Wong 
712fa1c1e8fSDarrick J. Wong 	return -EINVAL;
713fa1c1e8fSDarrick J. Wong }
714fa1c1e8fSDarrick J. Wong 
7152908d778SJames Bottomley struct domain_device *sas_find_dev_by_rphy(struct sas_rphy *rphy)
7162908d778SJames Bottomley {
7172908d778SJames Bottomley 	struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent);
7182908d778SJames Bottomley 	struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
7192908d778SJames Bottomley 	struct domain_device *found_dev = NULL;
7202908d778SJames Bottomley 	int i;
721980fa2f9SDarrick J. Wong 	unsigned long flags;
7222908d778SJames Bottomley 
723980fa2f9SDarrick J. Wong 	spin_lock_irqsave(&ha->phy_port_lock, flags);
7242908d778SJames Bottomley 	for (i = 0; i < ha->num_phys; i++) {
7252908d778SJames Bottomley 		struct asd_sas_port *port = ha->sas_port[i];
7262908d778SJames Bottomley 		struct domain_device *dev;
7272908d778SJames Bottomley 
7282908d778SJames Bottomley 		spin_lock(&port->dev_list_lock);
7292908d778SJames Bottomley 		list_for_each_entry(dev, &port->dev_list, dev_list_node) {
7302908d778SJames Bottomley 			if (rphy == dev->rphy) {
7312908d778SJames Bottomley 				found_dev = dev;
7322908d778SJames Bottomley 				spin_unlock(&port->dev_list_lock);
7332908d778SJames Bottomley 				goto found;
7342908d778SJames Bottomley 			}
7352908d778SJames Bottomley 		}
7362908d778SJames Bottomley 		spin_unlock(&port->dev_list_lock);
7372908d778SJames Bottomley 	}
7382908d778SJames Bottomley  found:
739980fa2f9SDarrick J. Wong 	spin_unlock_irqrestore(&ha->phy_port_lock, flags);
7402908d778SJames Bottomley 
7412908d778SJames Bottomley 	return found_dev;
7422908d778SJames Bottomley }
7432908d778SJames Bottomley 
7442908d778SJames Bottomley static inline struct domain_device *sas_find_target(struct scsi_target *starget)
7452908d778SJames Bottomley {
7462908d778SJames Bottomley 	struct sas_rphy *rphy = dev_to_rphy(starget->dev.parent);
7472908d778SJames Bottomley 
7482908d778SJames Bottomley 	return sas_find_dev_by_rphy(rphy);
7492908d778SJames Bottomley }
7502908d778SJames Bottomley 
7512908d778SJames Bottomley int sas_target_alloc(struct scsi_target *starget)
7522908d778SJames Bottomley {
7532908d778SJames Bottomley 	struct domain_device *found_dev = sas_find_target(starget);
754338ec570SDarrick J. Wong 	int res;
7552908d778SJames Bottomley 
7562908d778SJames Bottomley 	if (!found_dev)
7572908d778SJames Bottomley 		return -ENODEV;
7582908d778SJames Bottomley 
759fa1c1e8fSDarrick J. Wong 	if (dev_is_sata(found_dev)) {
760338ec570SDarrick J. Wong 		res = sas_ata_init_host_and_port(found_dev, starget);
761338ec570SDarrick J. Wong 		if (res)
762338ec570SDarrick J. Wong 			return res;
763fa1c1e8fSDarrick J. Wong 	}
764fa1c1e8fSDarrick J. Wong 
7652908d778SJames Bottomley 	starget->hostdata = found_dev;
7662908d778SJames Bottomley 	return 0;
7672908d778SJames Bottomley }
7682908d778SJames Bottomley 
7692908d778SJames Bottomley #define SAS_DEF_QD 32
7702908d778SJames Bottomley #define SAS_MAX_QD 64
7712908d778SJames Bottomley 
7722908d778SJames Bottomley int sas_slave_configure(struct scsi_device *scsi_dev)
7732908d778SJames Bottomley {
7742908d778SJames Bottomley 	struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
7752908d778SJames Bottomley 	struct sas_ha_struct *sas_ha;
7762908d778SJames Bottomley 
7772908d778SJames Bottomley 	BUG_ON(dev->rphy->identify.device_type != SAS_END_DEVICE);
7782908d778SJames Bottomley 
779fa1c1e8fSDarrick J. Wong 	if (dev_is_sata(dev)) {
780fa1c1e8fSDarrick J. Wong 		ata_sas_slave_configure(scsi_dev, dev->sata_dev.ap);
781fa1c1e8fSDarrick J. Wong 		return 0;
782fa1c1e8fSDarrick J. Wong 	}
783fa1c1e8fSDarrick J. Wong 
7842908d778SJames Bottomley 	sas_ha = dev->port->ha;
7852908d778SJames Bottomley 
7862908d778SJames Bottomley 	sas_read_port_mode_page(scsi_dev);
7872908d778SJames Bottomley 
7882908d778SJames Bottomley 	if (scsi_dev->tagged_supported) {
7892908d778SJames Bottomley 		scsi_set_tag_type(scsi_dev, MSG_SIMPLE_TAG);
7902908d778SJames Bottomley 		scsi_activate_tcq(scsi_dev, SAS_DEF_QD);
7912908d778SJames Bottomley 	} else {
7922908d778SJames Bottomley 		SAS_DPRINTK("device %llx, LUN %x doesn't support "
7932908d778SJames Bottomley 			    "TCQ\n", SAS_ADDR(dev->sas_addr),
7942908d778SJames Bottomley 			    scsi_dev->lun);
7952908d778SJames Bottomley 		scsi_dev->tagged_supported = 0;
7962908d778SJames Bottomley 		scsi_set_tag_type(scsi_dev, 0);
7972908d778SJames Bottomley 		scsi_deactivate_tcq(scsi_dev, 1);
7982908d778SJames Bottomley 	}
7992908d778SJames Bottomley 
800f27708fcSDarrick J. Wong 	scsi_dev->allow_restart = 1;
801f27708fcSDarrick J. Wong 
8022908d778SJames Bottomley 	return 0;
8032908d778SJames Bottomley }
8042908d778SJames Bottomley 
8052908d778SJames Bottomley void sas_slave_destroy(struct scsi_device *scsi_dev)
8062908d778SJames Bottomley {
807fa1c1e8fSDarrick J. Wong 	struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
808fa1c1e8fSDarrick J. Wong 
809fa1c1e8fSDarrick J. Wong 	if (dev_is_sata(dev))
8103e4ec344STejun Heo 		dev->sata_dev.ap->link.device[0].class = ATA_DEV_NONE;
8112908d778SJames Bottomley }
8122908d778SJames Bottomley 
813e881a172SMike Christie int sas_change_queue_depth(struct scsi_device *scsi_dev, int new_depth,
814e881a172SMike Christie 			   int reason)
8152908d778SJames Bottomley {
8162908d778SJames Bottomley 	int res = min(new_depth, SAS_MAX_QD);
8172908d778SJames Bottomley 
818e881a172SMike Christie 	if (reason != SCSI_QDEPTH_DEFAULT)
819e881a172SMike Christie 		return -EOPNOTSUPP;
820e881a172SMike Christie 
8212908d778SJames Bottomley 	if (scsi_dev->tagged_supported)
8222908d778SJames Bottomley 		scsi_adjust_queue_depth(scsi_dev, scsi_get_tag_type(scsi_dev),
8232908d778SJames Bottomley 					res);
8242908d778SJames Bottomley 	else {
8252908d778SJames Bottomley 		struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
8262908d778SJames Bottomley 		sas_printk("device %llx LUN %x queue depth changed to 1\n",
8272908d778SJames Bottomley 			   SAS_ADDR(dev->sas_addr),
8282908d778SJames Bottomley 			   scsi_dev->lun);
8292908d778SJames Bottomley 		scsi_adjust_queue_depth(scsi_dev, 0, 1);
8302908d778SJames Bottomley 		res = 1;
8312908d778SJames Bottomley 	}
8322908d778SJames Bottomley 
8332908d778SJames Bottomley 	return res;
8342908d778SJames Bottomley }
8352908d778SJames Bottomley 
8362908d778SJames Bottomley int sas_change_queue_type(struct scsi_device *scsi_dev, int qt)
8372908d778SJames Bottomley {
8382908d778SJames Bottomley 	if (!scsi_dev->tagged_supported)
8392908d778SJames Bottomley 		return 0;
8402908d778SJames Bottomley 
8412908d778SJames Bottomley 	scsi_deactivate_tcq(scsi_dev, 1);
8422908d778SJames Bottomley 
8432908d778SJames Bottomley 	scsi_set_tag_type(scsi_dev, qt);
8442908d778SJames Bottomley 	scsi_activate_tcq(scsi_dev, scsi_dev->queue_depth);
8452908d778SJames Bottomley 
8462908d778SJames Bottomley 	return qt;
8472908d778SJames Bottomley }
8482908d778SJames Bottomley 
8492908d778SJames Bottomley int sas_bios_param(struct scsi_device *scsi_dev,
8502908d778SJames Bottomley 			  struct block_device *bdev,
8512908d778SJames Bottomley 			  sector_t capacity, int *hsc)
8522908d778SJames Bottomley {
8532908d778SJames Bottomley 	hsc[0] = 255;
8542908d778SJames Bottomley 	hsc[1] = 63;
8552908d778SJames Bottomley 	sector_div(capacity, 255*63);
8562908d778SJames Bottomley 	hsc[2] = capacity;
8572908d778SJames Bottomley 
8582908d778SJames Bottomley 	return 0;
8592908d778SJames Bottomley }
8602908d778SJames Bottomley 
8612908d778SJames Bottomley /* ---------- Task Collector Thread implementation ---------- */
8622908d778SJames Bottomley 
8632908d778SJames Bottomley static void sas_queue(struct sas_ha_struct *sas_ha)
8642908d778SJames Bottomley {
8652908d778SJames Bottomley 	struct scsi_core *core = &sas_ha->core;
8662908d778SJames Bottomley 	unsigned long flags;
8672908d778SJames Bottomley 	LIST_HEAD(q);
8682908d778SJames Bottomley 	int can_queue;
8692908d778SJames Bottomley 	int res;
8702908d778SJames Bottomley 	struct sas_internal *i = to_sas_internal(core->shost->transportt);
8712908d778SJames Bottomley 
8722908d778SJames Bottomley 	spin_lock_irqsave(&core->task_queue_lock, flags);
873d7a54e30SChristoph Hellwig 	while (!kthread_should_stop() &&
8742908d778SJames Bottomley 	       !list_empty(&core->task_queue)) {
8752908d778SJames Bottomley 
8762908d778SJames Bottomley 		can_queue = sas_ha->lldd_queue_size - core->task_queue_size;
8772908d778SJames Bottomley 		if (can_queue >= 0) {
8782908d778SJames Bottomley 			can_queue = core->task_queue_size;
8792908d778SJames Bottomley 			list_splice_init(&core->task_queue, &q);
8802908d778SJames Bottomley 		} else {
8812908d778SJames Bottomley 			struct list_head *a, *n;
8822908d778SJames Bottomley 
8832908d778SJames Bottomley 			can_queue = sas_ha->lldd_queue_size;
8842908d778SJames Bottomley 			list_for_each_safe(a, n, &core->task_queue) {
8852908d778SJames Bottomley 				list_move_tail(a, &q);
8862908d778SJames Bottomley 				if (--can_queue == 0)
8872908d778SJames Bottomley 					break;
8882908d778SJames Bottomley 			}
8892908d778SJames Bottomley 			can_queue = sas_ha->lldd_queue_size;
8902908d778SJames Bottomley 		}
8912908d778SJames Bottomley 		core->task_queue_size -= can_queue;
8922908d778SJames Bottomley 		spin_unlock_irqrestore(&core->task_queue_lock, flags);
8932908d778SJames Bottomley 		{
8942908d778SJames Bottomley 			struct sas_task *task = list_entry(q.next,
8952908d778SJames Bottomley 							   struct sas_task,
8962908d778SJames Bottomley 							   list);
8972908d778SJames Bottomley 			list_del_init(&q);
8982908d778SJames Bottomley 			res = i->dft->lldd_execute_task(task, can_queue,
8992908d778SJames Bottomley 							GFP_KERNEL);
9002908d778SJames Bottomley 			if (unlikely(res))
9012908d778SJames Bottomley 				__list_add(&q, task->list.prev, &task->list);
9022908d778SJames Bottomley 		}
9032908d778SJames Bottomley 		spin_lock_irqsave(&core->task_queue_lock, flags);
9042908d778SJames Bottomley 		if (res) {
9052908d778SJames Bottomley 			list_splice_init(&q, &core->task_queue); /*at head*/
9062908d778SJames Bottomley 			core->task_queue_size += can_queue;
9072908d778SJames Bottomley 		}
9082908d778SJames Bottomley 	}
9092908d778SJames Bottomley 	spin_unlock_irqrestore(&core->task_queue_lock, flags);
9102908d778SJames Bottomley }
9112908d778SJames Bottomley 
9122908d778SJames Bottomley /**
9132908d778SJames Bottomley  * sas_queue_thread -- The Task Collector thread
9142908d778SJames Bottomley  * @_sas_ha: pointer to struct sas_ha
9152908d778SJames Bottomley  */
9162908d778SJames Bottomley static int sas_queue_thread(void *_sas_ha)
9172908d778SJames Bottomley {
9182908d778SJames Bottomley 	struct sas_ha_struct *sas_ha = _sas_ha;
9192908d778SJames Bottomley 
9202908d778SJames Bottomley 	while (1) {
921d7a54e30SChristoph Hellwig 		set_current_state(TASK_INTERRUPTIBLE);
922d7a54e30SChristoph Hellwig 		schedule();
9232908d778SJames Bottomley 		sas_queue(sas_ha);
924d7a54e30SChristoph Hellwig 		if (kthread_should_stop())
9252908d778SJames Bottomley 			break;
9262908d778SJames Bottomley 	}
9272908d778SJames Bottomley 
9282908d778SJames Bottomley 	return 0;
9292908d778SJames Bottomley }
9302908d778SJames Bottomley 
9312908d778SJames Bottomley int sas_init_queue(struct sas_ha_struct *sas_ha)
9322908d778SJames Bottomley {
9332908d778SJames Bottomley 	struct scsi_core *core = &sas_ha->core;
9342908d778SJames Bottomley 
9352908d778SJames Bottomley 	spin_lock_init(&core->task_queue_lock);
9362908d778SJames Bottomley 	core->task_queue_size = 0;
9372908d778SJames Bottomley 	INIT_LIST_HEAD(&core->task_queue);
9382908d778SJames Bottomley 
939d7a54e30SChristoph Hellwig 	core->queue_thread = kthread_run(sas_queue_thread, sas_ha,
940d7a54e30SChristoph Hellwig 					 "sas_queue_%d", core->shost->host_no);
941d7a54e30SChristoph Hellwig 	if (IS_ERR(core->queue_thread))
942d7a54e30SChristoph Hellwig 		return PTR_ERR(core->queue_thread);
943d7a54e30SChristoph Hellwig 	return 0;
9442908d778SJames Bottomley }
9452908d778SJames Bottomley 
9462908d778SJames Bottomley void sas_shutdown_queue(struct sas_ha_struct *sas_ha)
9472908d778SJames Bottomley {
9482908d778SJames Bottomley 	unsigned long flags;
9492908d778SJames Bottomley 	struct scsi_core *core = &sas_ha->core;
9502908d778SJames Bottomley 	struct sas_task *task, *n;
9512908d778SJames Bottomley 
952d7a54e30SChristoph Hellwig 	kthread_stop(core->queue_thread);
9532908d778SJames Bottomley 
9542908d778SJames Bottomley 	if (!list_empty(&core->task_queue))
9552908d778SJames Bottomley 		SAS_DPRINTK("HA: %llx: scsi core task queue is NOT empty!?\n",
9562908d778SJames Bottomley 			    SAS_ADDR(sas_ha->sas_addr));
9572908d778SJames Bottomley 
9582908d778SJames Bottomley 	spin_lock_irqsave(&core->task_queue_lock, flags);
9592908d778SJames Bottomley 	list_for_each_entry_safe(task, n, &core->task_queue, list) {
9602908d778SJames Bottomley 		struct scsi_cmnd *cmd = task->uldd_task;
9612908d778SJames Bottomley 
9622908d778SJames Bottomley 		list_del_init(&task->list);
9632908d778SJames Bottomley 
9642908d778SJames Bottomley 		ASSIGN_SAS_TASK(cmd, NULL);
9652908d778SJames Bottomley 		sas_free_task(task);
9662908d778SJames Bottomley 		cmd->result = DID_ABORT << 16;
9672908d778SJames Bottomley 		cmd->scsi_done(cmd);
9682908d778SJames Bottomley 	}
9692908d778SJames Bottomley 	spin_unlock_irqrestore(&core->task_queue_lock, flags);
9702908d778SJames Bottomley }
9712908d778SJames Bottomley 
972396819fbSDarrick J. Wong /*
973396819fbSDarrick J. Wong  * Call the LLDD task abort routine directly.  This function is intended for
974396819fbSDarrick J. Wong  * use by upper layers that need to tell the LLDD to abort a task.
975396819fbSDarrick J. Wong  */
976396819fbSDarrick J. Wong int __sas_task_abort(struct sas_task *task)
97779a5eb60SDarrick J. Wong {
97879a5eb60SDarrick J. Wong 	struct sas_internal *si =
97979a5eb60SDarrick J. Wong 		to_sas_internal(task->dev->port->ha->core.shost->transportt);
98079a5eb60SDarrick J. Wong 	unsigned long flags;
98179a5eb60SDarrick J. Wong 	int res;
98279a5eb60SDarrick J. Wong 
98379a5eb60SDarrick J. Wong 	spin_lock_irqsave(&task->task_state_lock, flags);
984396819fbSDarrick J. Wong 	if (task->task_state_flags & SAS_TASK_STATE_ABORTED ||
985396819fbSDarrick J. Wong 	    task->task_state_flags & SAS_TASK_STATE_DONE) {
98679a5eb60SDarrick J. Wong 		spin_unlock_irqrestore(&task->task_state_lock, flags);
987cadbd4a5SHarvey Harrison 		SAS_DPRINTK("%s: Task %p already finished.\n", __func__,
98879a5eb60SDarrick J. Wong 			    task);
98979a5eb60SDarrick J. Wong 		return 0;
99079a5eb60SDarrick J. Wong 	}
99179a5eb60SDarrick J. Wong 	task->task_state_flags |= SAS_TASK_STATE_ABORTED;
99279a5eb60SDarrick J. Wong 	spin_unlock_irqrestore(&task->task_state_lock, flags);
99379a5eb60SDarrick J. Wong 
99479a5eb60SDarrick J. Wong 	if (!si->dft->lldd_abort_task)
99579a5eb60SDarrick J. Wong 		return -ENODEV;
99679a5eb60SDarrick J. Wong 
99779a5eb60SDarrick J. Wong 	res = si->dft->lldd_abort_task(task);
998396819fbSDarrick J. Wong 
999396819fbSDarrick J. Wong 	spin_lock_irqsave(&task->task_state_lock, flags);
100079a5eb60SDarrick J. Wong 	if ((task->task_state_flags & SAS_TASK_STATE_DONE) ||
100179a5eb60SDarrick J. Wong 	    (res == TMF_RESP_FUNC_COMPLETE))
100279a5eb60SDarrick J. Wong 	{
1003396819fbSDarrick J. Wong 		spin_unlock_irqrestore(&task->task_state_lock, flags);
100479a5eb60SDarrick J. Wong 		task->task_done(task);
100579a5eb60SDarrick J. Wong 		return 0;
100679a5eb60SDarrick J. Wong 	}
100779a5eb60SDarrick J. Wong 
100879a5eb60SDarrick J. Wong 	if (!(task->task_state_flags & SAS_TASK_STATE_DONE))
100979a5eb60SDarrick J. Wong 		task->task_state_flags &= ~SAS_TASK_STATE_ABORTED;
101079a5eb60SDarrick J. Wong 	spin_unlock_irqrestore(&task->task_state_lock, flags);
101179a5eb60SDarrick J. Wong 
101279a5eb60SDarrick J. Wong 	return -EAGAIN;
101379a5eb60SDarrick J. Wong }
101479a5eb60SDarrick J. Wong 
1015396819fbSDarrick J. Wong /*
1016396819fbSDarrick J. Wong  * Tell an upper layer that it needs to initiate an abort for a given task.
1017396819fbSDarrick J. Wong  * This should only ever be called by an LLDD.
1018396819fbSDarrick J. Wong  */
1019396819fbSDarrick J. Wong void sas_task_abort(struct sas_task *task)
102079a5eb60SDarrick J. Wong {
1021396819fbSDarrick J. Wong 	struct scsi_cmnd *sc = task->uldd_task;
102279a5eb60SDarrick J. Wong 
1023396819fbSDarrick J. Wong 	/* Escape for libsas internal commands */
1024396819fbSDarrick J. Wong 	if (!sc) {
1025396819fbSDarrick J. Wong 		if (!del_timer(&task->timer))
102679a5eb60SDarrick J. Wong 			return;
1027396819fbSDarrick J. Wong 		task->timer.function(task->timer.data);
1028396819fbSDarrick J. Wong 		return;
1029396819fbSDarrick J. Wong 	}
103079a5eb60SDarrick J. Wong 
10313a2755afSDarrick J. Wong 	if (dev_is_sata(task->dev)) {
10323a2755afSDarrick J. Wong 		sas_ata_task_abort(task);
10331b4d0d8eSJames Bottomley 	} else {
10341b4d0d8eSJames Bottomley 		struct request_queue *q = sc->device->request_queue;
10351b4d0d8eSJames Bottomley 		unsigned long flags;
10363a2755afSDarrick J. Wong 
103770b25f89STejun Heo 		spin_lock_irqsave(q->queue_lock, flags);
1038242f9dcbSJens Axboe 		blk_abort_request(sc->request);
103970b25f89STejun Heo 		spin_unlock_irqrestore(q->queue_lock, flags);
1040396819fbSDarrick J. Wong 		scsi_schedule_eh(sc->device->host);
104179a5eb60SDarrick J. Wong 	}
10421b4d0d8eSJames Bottomley }
104379a5eb60SDarrick J. Wong 
1044fa1c1e8fSDarrick J. Wong int sas_slave_alloc(struct scsi_device *scsi_dev)
1045fa1c1e8fSDarrick J. Wong {
1046fa1c1e8fSDarrick J. Wong 	struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
1047fa1c1e8fSDarrick J. Wong 
1048fa1c1e8fSDarrick J. Wong 	if (dev_is_sata(dev))
1049fa1c1e8fSDarrick J. Wong 		return ata_sas_port_init(dev->sata_dev.ap);
1050fa1c1e8fSDarrick J. Wong 
1051fa1c1e8fSDarrick J. Wong 	return 0;
1052fa1c1e8fSDarrick J. Wong }
1053fa1c1e8fSDarrick J. Wong 
1054fa1c1e8fSDarrick J. Wong void sas_target_destroy(struct scsi_target *starget)
1055fa1c1e8fSDarrick J. Wong {
1056fa1c1e8fSDarrick J. Wong 	struct domain_device *found_dev = sas_find_target(starget);
1057fa1c1e8fSDarrick J. Wong 
1058fa1c1e8fSDarrick J. Wong 	if (!found_dev)
1059fa1c1e8fSDarrick J. Wong 		return;
1060fa1c1e8fSDarrick J. Wong 
1061fa1c1e8fSDarrick J. Wong 	if (dev_is_sata(found_dev))
1062fa1c1e8fSDarrick J. Wong 		ata_sas_port_destroy(found_dev->sata_dev.ap);
1063fa1c1e8fSDarrick J. Wong 
1064fa1c1e8fSDarrick J. Wong 	return;
1065fa1c1e8fSDarrick J. Wong }
1066fa1c1e8fSDarrick J. Wong 
106745e6cdf4SDarrick J. Wong static void sas_parse_addr(u8 *sas_addr, const char *p)
106845e6cdf4SDarrick J. Wong {
106945e6cdf4SDarrick J. Wong 	int i;
107045e6cdf4SDarrick J. Wong 	for (i = 0; i < SAS_ADDR_SIZE; i++) {
107145e6cdf4SDarrick J. Wong 		u8 h, l;
107245e6cdf4SDarrick J. Wong 		if (!*p)
107345e6cdf4SDarrick J. Wong 			break;
107445e6cdf4SDarrick J. Wong 		h = isdigit(*p) ? *p-'0' : toupper(*p)-'A'+10;
107545e6cdf4SDarrick J. Wong 		p++;
107645e6cdf4SDarrick J. Wong 		l = isdigit(*p) ? *p-'0' : toupper(*p)-'A'+10;
107745e6cdf4SDarrick J. Wong 		p++;
107845e6cdf4SDarrick J. Wong 		sas_addr[i] = (h<<4) | l;
107945e6cdf4SDarrick J. Wong 	}
108045e6cdf4SDarrick J. Wong }
108145e6cdf4SDarrick J. Wong 
108245e6cdf4SDarrick J. Wong #define SAS_STRING_ADDR_SIZE	16
108345e6cdf4SDarrick J. Wong 
108445e6cdf4SDarrick J. Wong int sas_request_addr(struct Scsi_Host *shost, u8 *addr)
108545e6cdf4SDarrick J. Wong {
108645e6cdf4SDarrick J. Wong 	int res;
108745e6cdf4SDarrick J. Wong 	const struct firmware *fw;
108845e6cdf4SDarrick J. Wong 
108945e6cdf4SDarrick J. Wong 	res = request_firmware(&fw, "sas_addr", &shost->shost_gendev);
109045e6cdf4SDarrick J. Wong 	if (res)
109145e6cdf4SDarrick J. Wong 		return res;
109245e6cdf4SDarrick J. Wong 
109345e6cdf4SDarrick J. Wong 	if (fw->size < SAS_STRING_ADDR_SIZE) {
109445e6cdf4SDarrick J. Wong 		res = -ENODEV;
109545e6cdf4SDarrick J. Wong 		goto out;
109645e6cdf4SDarrick J. Wong 	}
109745e6cdf4SDarrick J. Wong 
109845e6cdf4SDarrick J. Wong 	sas_parse_addr(addr, fw->data);
109945e6cdf4SDarrick J. Wong 
110045e6cdf4SDarrick J. Wong out:
110145e6cdf4SDarrick J. Wong 	release_firmware(fw);
110245e6cdf4SDarrick J. Wong 	return res;
110345e6cdf4SDarrick J. Wong }
110445e6cdf4SDarrick J. Wong EXPORT_SYMBOL_GPL(sas_request_addr);
110545e6cdf4SDarrick J. Wong 
11062908d778SJames Bottomley EXPORT_SYMBOL_GPL(sas_queuecommand);
11072908d778SJames Bottomley EXPORT_SYMBOL_GPL(sas_target_alloc);
11082908d778SJames Bottomley EXPORT_SYMBOL_GPL(sas_slave_configure);
11092908d778SJames Bottomley EXPORT_SYMBOL_GPL(sas_slave_destroy);
11102908d778SJames Bottomley EXPORT_SYMBOL_GPL(sas_change_queue_depth);
11112908d778SJames Bottomley EXPORT_SYMBOL_GPL(sas_change_queue_type);
11122908d778SJames Bottomley EXPORT_SYMBOL_GPL(sas_bios_param);
1113396819fbSDarrick J. Wong EXPORT_SYMBOL_GPL(__sas_task_abort);
111479a5eb60SDarrick J. Wong EXPORT_SYMBOL_GPL(sas_task_abort);
1115dea22214SDarrick J. Wong EXPORT_SYMBOL_GPL(sas_phy_reset);
1116acbf167dSDarrick J. Wong EXPORT_SYMBOL_GPL(sas_phy_enable);
1117ad689233SDarrick J. Wong EXPORT_SYMBOL_GPL(sas_eh_device_reset_handler);
1118a9344e68SDarrick J. Wong EXPORT_SYMBOL_GPL(sas_eh_bus_reset_handler);
1119fa1c1e8fSDarrick J. Wong EXPORT_SYMBOL_GPL(sas_slave_alloc);
1120fa1c1e8fSDarrick J. Wong EXPORT_SYMBOL_GPL(sas_target_destroy);
1121fa1c1e8fSDarrick J. Wong EXPORT_SYMBOL_GPL(sas_ioctl);
1122