xref: /openbmc/linux/drivers/scsi/libsas/sas_scsi_host.c (revision 3ebf6922b0833807e54c73f4794c74baf9945fc8)
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 
262908d778SJames Bottomley #include "sas_internal.h"
272908d778SJames Bottomley 
282908d778SJames Bottomley #include <scsi/scsi_host.h>
292908d778SJames Bottomley #include <scsi/scsi_device.h>
302908d778SJames Bottomley #include <scsi/scsi_tcq.h>
312908d778SJames Bottomley #include <scsi/scsi.h>
32f456393eSDarrick J. Wong #include <scsi/scsi_eh.h>
332908d778SJames Bottomley #include <scsi/scsi_transport.h>
342908d778SJames Bottomley #include <scsi/scsi_transport_sas.h>
352908d778SJames Bottomley #include "../scsi_sas_internal.h"
3679a5eb60SDarrick J. Wong #include "../scsi_transport_api.h"
372908d778SJames Bottomley 
382908d778SJames Bottomley #include <linux/err.h>
392908d778SJames Bottomley #include <linux/blkdev.h>
402908d778SJames Bottomley #include <linux/scatterlist.h>
412908d778SJames Bottomley 
422908d778SJames Bottomley /* ---------- SCSI Host glue ---------- */
432908d778SJames Bottomley 
442908d778SJames Bottomley #define TO_SAS_TASK(_scsi_cmd)  ((void *)(_scsi_cmd)->host_scribble)
452908d778SJames Bottomley #define ASSIGN_SAS_TASK(_sc, _t) do { (_sc)->host_scribble = (void *) _t; } while (0)
462908d778SJames Bottomley 
472908d778SJames Bottomley static void sas_scsi_task_done(struct sas_task *task)
482908d778SJames Bottomley {
492908d778SJames Bottomley 	struct task_status_struct *ts = &task->task_status;
502908d778SJames Bottomley 	struct scsi_cmnd *sc = task->uldd_task;
51f456393eSDarrick J. Wong 	struct sas_ha_struct *sas_ha = SHOST_TO_SAS_HA(sc->device->host);
522908d778SJames Bottomley 	unsigned ts_flags = task->task_state_flags;
532908d778SJames Bottomley 	int hs = 0, stat = 0;
542908d778SJames Bottomley 
552908d778SJames Bottomley 	if (unlikely(!sc)) {
562908d778SJames Bottomley 		SAS_DPRINTK("task_done called with non existing SCSI cmnd!\n");
572908d778SJames Bottomley 		list_del_init(&task->list);
582908d778SJames Bottomley 		sas_free_task(task);
592908d778SJames Bottomley 		return;
602908d778SJames Bottomley 	}
612908d778SJames Bottomley 
622908d778SJames Bottomley 	if (ts->resp == SAS_TASK_UNDELIVERED) {
632908d778SJames Bottomley 		/* transport error */
642908d778SJames Bottomley 		hs = DID_NO_CONNECT;
652908d778SJames Bottomley 	} else { /* ts->resp == SAS_TASK_COMPLETE */
662908d778SJames Bottomley 		/* task delivered, what happened afterwards? */
672908d778SJames Bottomley 		switch (ts->stat) {
682908d778SJames Bottomley 		case SAS_DEV_NO_RESPONSE:
692908d778SJames Bottomley 		case SAS_INTERRUPTED:
702908d778SJames Bottomley 		case SAS_PHY_DOWN:
712908d778SJames Bottomley 		case SAS_NAK_R_ERR:
722908d778SJames Bottomley 		case SAS_OPEN_TO:
732908d778SJames Bottomley 			hs = DID_NO_CONNECT;
742908d778SJames Bottomley 			break;
752908d778SJames Bottomley 		case SAS_DATA_UNDERRUN:
762908d778SJames Bottomley 			sc->resid = ts->residual;
772908d778SJames Bottomley 			if (sc->request_bufflen - sc->resid < sc->underflow)
782908d778SJames Bottomley 				hs = DID_ERROR;
792908d778SJames Bottomley 			break;
802908d778SJames Bottomley 		case SAS_DATA_OVERRUN:
812908d778SJames Bottomley 			hs = DID_ERROR;
822908d778SJames Bottomley 			break;
832908d778SJames Bottomley 		case SAS_QUEUE_FULL:
842908d778SJames Bottomley 			hs = DID_SOFT_ERROR; /* retry */
852908d778SJames Bottomley 			break;
862908d778SJames Bottomley 		case SAS_DEVICE_UNKNOWN:
872908d778SJames Bottomley 			hs = DID_BAD_TARGET;
882908d778SJames Bottomley 			break;
892908d778SJames Bottomley 		case SAS_SG_ERR:
902908d778SJames Bottomley 			hs = DID_PARITY;
912908d778SJames Bottomley 			break;
922908d778SJames Bottomley 		case SAS_OPEN_REJECT:
932908d778SJames Bottomley 			if (ts->open_rej_reason == SAS_OREJ_RSVD_RETRY)
942908d778SJames Bottomley 				hs = DID_SOFT_ERROR; /* retry */
952908d778SJames Bottomley 			else
962908d778SJames Bottomley 				hs = DID_ERROR;
972908d778SJames Bottomley 			break;
982908d778SJames Bottomley 		case SAS_PROTO_RESPONSE:
992908d778SJames Bottomley 			SAS_DPRINTK("LLDD:%s sent SAS_PROTO_RESP for an SSP "
1002908d778SJames Bottomley 				    "task; please report this\n",
1012908d778SJames Bottomley 				    task->dev->port->ha->sas_ha_name);
1022908d778SJames Bottomley 			break;
1032908d778SJames Bottomley 		case SAS_ABORTED_TASK:
1042908d778SJames Bottomley 			hs = DID_ABORT;
1052908d778SJames Bottomley 			break;
1062908d778SJames Bottomley 		case SAM_CHECK_COND:
1072908d778SJames Bottomley 			memcpy(sc->sense_buffer, ts->buf,
1082908d778SJames Bottomley 			       max(SCSI_SENSE_BUFFERSIZE, ts->buf_valid_size));
1092908d778SJames Bottomley 			stat = SAM_CHECK_COND;
1102908d778SJames Bottomley 			break;
1112908d778SJames Bottomley 		default:
1122908d778SJames Bottomley 			stat = ts->stat;
1132908d778SJames Bottomley 			break;
1142908d778SJames Bottomley 		}
1152908d778SJames Bottomley 	}
1162908d778SJames Bottomley 	ASSIGN_SAS_TASK(sc, NULL);
1172908d778SJames Bottomley 	sc->result = (hs << 16) | stat;
1182908d778SJames Bottomley 	list_del_init(&task->list);
1192908d778SJames Bottomley 	sas_free_task(task);
1202908d778SJames Bottomley 	/* This is very ugly but this is how SCSI Core works. */
1212908d778SJames Bottomley 	if (ts_flags & SAS_TASK_STATE_ABORTED)
122f456393eSDarrick J. Wong 		scsi_eh_finish_cmd(sc, &sas_ha->eh_done_q);
1232908d778SJames Bottomley 	else
1242908d778SJames Bottomley 		sc->scsi_done(sc);
1252908d778SJames Bottomley }
1262908d778SJames Bottomley 
1272908d778SJames Bottomley static enum task_attribute sas_scsi_get_task_attr(struct scsi_cmnd *cmd)
1282908d778SJames Bottomley {
1292908d778SJames Bottomley 	enum task_attribute ta = TASK_ATTR_SIMPLE;
1302908d778SJames Bottomley 	if (cmd->request && blk_rq_tagged(cmd->request)) {
1312908d778SJames Bottomley 		if (cmd->device->ordered_tags &&
1321bdfd554SJeff Garzik 		    (cmd->request->cmd_flags & REQ_HARDBARRIER))
1332908d778SJames Bottomley 			ta = TASK_ATTR_HOQ;
1342908d778SJames Bottomley 	}
1352908d778SJames Bottomley 	return ta;
1362908d778SJames Bottomley }
1372908d778SJames Bottomley 
1382908d778SJames Bottomley static struct sas_task *sas_create_task(struct scsi_cmnd *cmd,
1392908d778SJames Bottomley 					       struct domain_device *dev,
1403cc27547SAl Viro 					       gfp_t gfp_flags)
1412908d778SJames Bottomley {
1422908d778SJames Bottomley 	struct sas_task *task = sas_alloc_task(gfp_flags);
1432908d778SJames Bottomley 	struct scsi_lun lun;
1442908d778SJames Bottomley 
1452908d778SJames Bottomley 	if (!task)
1462908d778SJames Bottomley 		return NULL;
1472908d778SJames Bottomley 
1482908d778SJames Bottomley 	*(u32 *)cmd->sense_buffer = 0;
1492908d778SJames Bottomley 	task->uldd_task = cmd;
1502908d778SJames Bottomley 	ASSIGN_SAS_TASK(cmd, task);
1512908d778SJames Bottomley 
1522908d778SJames Bottomley 	task->dev = dev;
1532908d778SJames Bottomley 	task->task_proto = task->dev->tproto; /* BUG_ON(!SSP) */
1542908d778SJames Bottomley 
1552908d778SJames Bottomley 	task->ssp_task.retry_count = 1;
1562908d778SJames Bottomley 	int_to_scsilun(cmd->device->lun, &lun);
1572908d778SJames Bottomley 	memcpy(task->ssp_task.LUN, &lun.scsi_lun, 8);
1582908d778SJames Bottomley 	task->ssp_task.task_attr = sas_scsi_get_task_attr(cmd);
1592908d778SJames Bottomley 	memcpy(task->ssp_task.cdb, cmd->cmnd, 16);
1602908d778SJames Bottomley 
1612908d778SJames Bottomley 	task->scatter = cmd->request_buffer;
1622908d778SJames Bottomley 	task->num_scatter = cmd->use_sg;
1632908d778SJames Bottomley 	task->total_xfer_len = cmd->request_bufflen;
1642908d778SJames Bottomley 	task->data_dir = cmd->sc_data_direction;
1652908d778SJames Bottomley 
1662908d778SJames Bottomley 	task->task_done = sas_scsi_task_done;
1672908d778SJames Bottomley 
1682908d778SJames Bottomley 	return task;
1692908d778SJames Bottomley }
1702908d778SJames Bottomley 
1712908d778SJames Bottomley static int sas_queue_up(struct sas_task *task)
1722908d778SJames Bottomley {
1732908d778SJames Bottomley 	struct sas_ha_struct *sas_ha = task->dev->port->ha;
1742908d778SJames Bottomley 	struct scsi_core *core = &sas_ha->core;
1752908d778SJames Bottomley 	unsigned long flags;
1762908d778SJames Bottomley 	LIST_HEAD(list);
1772908d778SJames Bottomley 
1782908d778SJames Bottomley 	spin_lock_irqsave(&core->task_queue_lock, flags);
1792908d778SJames Bottomley 	if (sas_ha->lldd_queue_size < core->task_queue_size + 1) {
1802908d778SJames Bottomley 		spin_unlock_irqrestore(&core->task_queue_lock, flags);
1812908d778SJames Bottomley 		return -SAS_QUEUE_FULL;
1822908d778SJames Bottomley 	}
1832908d778SJames Bottomley 	list_add_tail(&task->list, &core->task_queue);
1842908d778SJames Bottomley 	core->task_queue_size += 1;
1852908d778SJames Bottomley 	spin_unlock_irqrestore(&core->task_queue_lock, flags);
1862908d778SJames Bottomley 	up(&core->queue_thread_sema);
1872908d778SJames Bottomley 
1882908d778SJames Bottomley 	return 0;
1892908d778SJames Bottomley }
1902908d778SJames Bottomley 
1912908d778SJames Bottomley /**
1922908d778SJames Bottomley  * sas_queuecommand -- Enqueue a command for processing
1932908d778SJames Bottomley  * @parameters: See SCSI Core documentation
1942908d778SJames Bottomley  *
1952908d778SJames Bottomley  * Note: XXX: Remove the host unlock/lock pair when SCSI Core can
1962908d778SJames Bottomley  * call us without holding an IRQ spinlock...
1972908d778SJames Bottomley  */
1982908d778SJames Bottomley int sas_queuecommand(struct scsi_cmnd *cmd,
1992908d778SJames Bottomley 		     void (*scsi_done)(struct scsi_cmnd *))
2002908d778SJames Bottomley {
2012908d778SJames Bottomley 	int res = 0;
2022908d778SJames Bottomley 	struct domain_device *dev = cmd_to_domain_dev(cmd);
2032908d778SJames Bottomley 	struct Scsi_Host *host = cmd->device->host;
2042908d778SJames Bottomley 	struct sas_internal *i = to_sas_internal(host->transportt);
2052908d778SJames Bottomley 
2062908d778SJames Bottomley 	spin_unlock_irq(host->host_lock);
2072908d778SJames Bottomley 
2082908d778SJames Bottomley 	{
2092908d778SJames Bottomley 		struct sas_ha_struct *sas_ha = dev->port->ha;
2102908d778SJames Bottomley 		struct sas_task *task;
2112908d778SJames Bottomley 
2122908d778SJames Bottomley 		res = -ENOMEM;
2132908d778SJames Bottomley 		task = sas_create_task(cmd, dev, GFP_ATOMIC);
2142908d778SJames Bottomley 		if (!task)
2152908d778SJames Bottomley 			goto out;
2162908d778SJames Bottomley 
2172908d778SJames Bottomley 		cmd->scsi_done = scsi_done;
2182908d778SJames Bottomley 		/* Queue up, Direct Mode or Task Collector Mode. */
2192908d778SJames Bottomley 		if (sas_ha->lldd_max_execute_num < 2)
2202908d778SJames Bottomley 			res = i->dft->lldd_execute_task(task, 1, GFP_ATOMIC);
2212908d778SJames Bottomley 		else
2222908d778SJames Bottomley 			res = sas_queue_up(task);
2232908d778SJames Bottomley 
2242908d778SJames Bottomley 		/* Examine */
2252908d778SJames Bottomley 		if (res) {
2262908d778SJames Bottomley 			SAS_DPRINTK("lldd_execute_task returned: %d\n", res);
2272908d778SJames Bottomley 			ASSIGN_SAS_TASK(cmd, NULL);
2282908d778SJames Bottomley 			sas_free_task(task);
2292908d778SJames Bottomley 			if (res == -SAS_QUEUE_FULL) {
2302908d778SJames Bottomley 				cmd->result = DID_SOFT_ERROR << 16; /* retry */
2312908d778SJames Bottomley 				res = 0;
2322908d778SJames Bottomley 				scsi_done(cmd);
2332908d778SJames Bottomley 			}
2342908d778SJames Bottomley 			goto out;
2352908d778SJames Bottomley 		}
2362908d778SJames Bottomley 	}
2372908d778SJames Bottomley out:
2382908d778SJames Bottomley 	spin_lock_irq(host->host_lock);
2392908d778SJames Bottomley 	return res;
2402908d778SJames Bottomley }
2412908d778SJames Bottomley 
2422908d778SJames Bottomley static void sas_scsi_clear_queue_lu(struct list_head *error_q, struct scsi_cmnd *my_cmd)
2432908d778SJames Bottomley {
2442908d778SJames Bottomley 	struct scsi_cmnd *cmd, *n;
2452908d778SJames Bottomley 
2462908d778SJames Bottomley 	list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
2472908d778SJames Bottomley 		if (cmd == my_cmd)
2482908d778SJames Bottomley 			list_del_init(&cmd->eh_entry);
2492908d778SJames Bottomley 	}
2502908d778SJames Bottomley }
2512908d778SJames Bottomley 
2522908d778SJames Bottomley static void sas_scsi_clear_queue_I_T(struct list_head *error_q,
2532908d778SJames Bottomley 				     struct domain_device *dev)
2542908d778SJames Bottomley {
2552908d778SJames Bottomley 	struct scsi_cmnd *cmd, *n;
2562908d778SJames Bottomley 
2572908d778SJames Bottomley 	list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
2582908d778SJames Bottomley 		struct domain_device *x = cmd_to_domain_dev(cmd);
2592908d778SJames Bottomley 
2602908d778SJames Bottomley 		if (x == dev)
2612908d778SJames Bottomley 			list_del_init(&cmd->eh_entry);
2622908d778SJames Bottomley 	}
2632908d778SJames Bottomley }
2642908d778SJames Bottomley 
2652908d778SJames Bottomley static void sas_scsi_clear_queue_port(struct list_head *error_q,
2662908d778SJames Bottomley 				      struct asd_sas_port *port)
2672908d778SJames Bottomley {
2682908d778SJames Bottomley 	struct scsi_cmnd *cmd, *n;
2692908d778SJames Bottomley 
2702908d778SJames Bottomley 	list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
2712908d778SJames Bottomley 		struct domain_device *dev = cmd_to_domain_dev(cmd);
2722908d778SJames Bottomley 		struct asd_sas_port *x = dev->port;
2732908d778SJames Bottomley 
2742908d778SJames Bottomley 		if (x == port)
2752908d778SJames Bottomley 			list_del_init(&cmd->eh_entry);
2762908d778SJames Bottomley 	}
2772908d778SJames Bottomley }
2782908d778SJames Bottomley 
2792908d778SJames Bottomley enum task_disposition {
2802908d778SJames Bottomley 	TASK_IS_DONE,
2812908d778SJames Bottomley 	TASK_IS_ABORTED,
2822908d778SJames Bottomley 	TASK_IS_AT_LU,
2832908d778SJames Bottomley 	TASK_IS_NOT_AT_LU,
2842908d778SJames Bottomley };
2852908d778SJames Bottomley 
2862908d778SJames Bottomley static enum task_disposition sas_scsi_find_task(struct sas_task *task)
2872908d778SJames Bottomley {
2882908d778SJames Bottomley 	struct sas_ha_struct *ha = task->dev->port->ha;
2892908d778SJames Bottomley 	unsigned long flags;
2902908d778SJames Bottomley 	int i, res;
2912908d778SJames Bottomley 	struct sas_internal *si =
2922908d778SJames Bottomley 		to_sas_internal(task->dev->port->ha->core.shost->transportt);
2932908d778SJames Bottomley 
2942908d778SJames Bottomley 	if (ha->lldd_max_execute_num > 1) {
2952908d778SJames Bottomley 		struct scsi_core *core = &ha->core;
2962908d778SJames Bottomley 		struct sas_task *t, *n;
2972908d778SJames Bottomley 
2982908d778SJames Bottomley 		spin_lock_irqsave(&core->task_queue_lock, flags);
2992908d778SJames Bottomley 		list_for_each_entry_safe(t, n, &core->task_queue, list) {
3002908d778SJames Bottomley 			if (task == t) {
3012908d778SJames Bottomley 				list_del_init(&t->list);
3022908d778SJames Bottomley 				spin_unlock_irqrestore(&core->task_queue_lock,
3032908d778SJames Bottomley 						       flags);
3042908d778SJames Bottomley 				SAS_DPRINTK("%s: task 0x%p aborted from "
3052908d778SJames Bottomley 					    "task_queue\n",
3062908d778SJames Bottomley 					    __FUNCTION__, task);
3072908d778SJames Bottomley 				return TASK_IS_ABORTED;
3082908d778SJames Bottomley 			}
3092908d778SJames Bottomley 		}
3102908d778SJames Bottomley 		spin_unlock_irqrestore(&core->task_queue_lock, flags);
3112908d778SJames Bottomley 	}
3122908d778SJames Bottomley 
3132908d778SJames Bottomley 	for (i = 0; i < 5; i++) {
3142908d778SJames Bottomley 		SAS_DPRINTK("%s: aborting task 0x%p\n", __FUNCTION__, task);
3152908d778SJames Bottomley 		res = si->dft->lldd_abort_task(task);
3162908d778SJames Bottomley 
3172908d778SJames Bottomley 		spin_lock_irqsave(&task->task_state_lock, flags);
3182908d778SJames Bottomley 		if (task->task_state_flags & SAS_TASK_STATE_DONE) {
3192908d778SJames Bottomley 			spin_unlock_irqrestore(&task->task_state_lock, flags);
3202908d778SJames Bottomley 			SAS_DPRINTK("%s: task 0x%p is done\n", __FUNCTION__,
3212908d778SJames Bottomley 				    task);
3222908d778SJames Bottomley 			return TASK_IS_DONE;
3232908d778SJames Bottomley 		}
3242908d778SJames Bottomley 		spin_unlock_irqrestore(&task->task_state_lock, flags);
3252908d778SJames Bottomley 
3262908d778SJames Bottomley 		if (res == TMF_RESP_FUNC_COMPLETE) {
3272908d778SJames Bottomley 			SAS_DPRINTK("%s: task 0x%p is aborted\n",
3282908d778SJames Bottomley 				    __FUNCTION__, task);
3292908d778SJames Bottomley 			return TASK_IS_ABORTED;
3302908d778SJames Bottomley 		} else if (si->dft->lldd_query_task) {
3312908d778SJames Bottomley 			SAS_DPRINTK("%s: querying task 0x%p\n",
3322908d778SJames Bottomley 				    __FUNCTION__, task);
3332908d778SJames Bottomley 			res = si->dft->lldd_query_task(task);
3342908d778SJames Bottomley 			if (res == TMF_RESP_FUNC_SUCC) {
3352908d778SJames Bottomley 				SAS_DPRINTK("%s: task 0x%p at LU\n",
3362908d778SJames Bottomley 					    __FUNCTION__, task);
3372908d778SJames Bottomley 				return TASK_IS_AT_LU;
3382908d778SJames Bottomley 			} else if (res == TMF_RESP_FUNC_COMPLETE) {
3392908d778SJames Bottomley 				SAS_DPRINTK("%s: task 0x%p not at LU\n",
3402908d778SJames Bottomley 					    __FUNCTION__, task);
3412908d778SJames Bottomley 				return TASK_IS_NOT_AT_LU;
3422908d778SJames Bottomley 			}
3432908d778SJames Bottomley 		}
3442908d778SJames Bottomley 	}
3452908d778SJames Bottomley 	return res;
3462908d778SJames Bottomley }
3472908d778SJames Bottomley 
3482908d778SJames Bottomley static int sas_recover_lu(struct domain_device *dev, struct scsi_cmnd *cmd)
3492908d778SJames Bottomley {
3502908d778SJames Bottomley 	int res = TMF_RESP_FUNC_FAILED;
3512908d778SJames Bottomley 	struct scsi_lun lun;
3522908d778SJames Bottomley 	struct sas_internal *i =
3532908d778SJames Bottomley 		to_sas_internal(dev->port->ha->core.shost->transportt);
3542908d778SJames Bottomley 
3552908d778SJames Bottomley 	int_to_scsilun(cmd->device->lun, &lun);
3562908d778SJames Bottomley 
3572908d778SJames Bottomley 	SAS_DPRINTK("eh: device %llx LUN %x has the task\n",
3582908d778SJames Bottomley 		    SAS_ADDR(dev->sas_addr),
3592908d778SJames Bottomley 		    cmd->device->lun);
3602908d778SJames Bottomley 
3612908d778SJames Bottomley 	if (i->dft->lldd_abort_task_set)
3622908d778SJames Bottomley 		res = i->dft->lldd_abort_task_set(dev, lun.scsi_lun);
3632908d778SJames Bottomley 
3642908d778SJames Bottomley 	if (res == TMF_RESP_FUNC_FAILED) {
3652908d778SJames Bottomley 		if (i->dft->lldd_clear_task_set)
3662908d778SJames Bottomley 			res = i->dft->lldd_clear_task_set(dev, lun.scsi_lun);
3672908d778SJames Bottomley 	}
3682908d778SJames Bottomley 
3692908d778SJames Bottomley 	if (res == TMF_RESP_FUNC_FAILED) {
3702908d778SJames Bottomley 		if (i->dft->lldd_lu_reset)
3712908d778SJames Bottomley 			res = i->dft->lldd_lu_reset(dev, lun.scsi_lun);
3722908d778SJames Bottomley 	}
3732908d778SJames Bottomley 
3742908d778SJames Bottomley 	return res;
3752908d778SJames Bottomley }
3762908d778SJames Bottomley 
3772908d778SJames Bottomley static int sas_recover_I_T(struct domain_device *dev)
3782908d778SJames Bottomley {
3792908d778SJames Bottomley 	int res = TMF_RESP_FUNC_FAILED;
3802908d778SJames Bottomley 	struct sas_internal *i =
3812908d778SJames Bottomley 		to_sas_internal(dev->port->ha->core.shost->transportt);
3822908d778SJames Bottomley 
3832908d778SJames Bottomley 	SAS_DPRINTK("I_T nexus reset for dev %016llx\n",
3842908d778SJames Bottomley 		    SAS_ADDR(dev->sas_addr));
3852908d778SJames Bottomley 
3862908d778SJames Bottomley 	if (i->dft->lldd_I_T_nexus_reset)
3872908d778SJames Bottomley 		res = i->dft->lldd_I_T_nexus_reset(dev);
3882908d778SJames Bottomley 
3892908d778SJames Bottomley 	return res;
3902908d778SJames Bottomley }
3912908d778SJames Bottomley 
392*3ebf6922SDarrick J. Wong static int eh_reset_phy_helper(struct sas_phy *phy)
393*3ebf6922SDarrick J. Wong {
394*3ebf6922SDarrick J. Wong 	int tmf_resp;
395*3ebf6922SDarrick J. Wong 
396*3ebf6922SDarrick J. Wong 	tmf_resp = sas_phy_reset(phy, 1);
397*3ebf6922SDarrick J. Wong 	if (tmf_resp)
398*3ebf6922SDarrick J. Wong 		SAS_DPRINTK("Hard reset of phy %d failed 0x%x\n",
399*3ebf6922SDarrick J. Wong 			    phy->identify.phy_identifier,
400*3ebf6922SDarrick J. Wong 			    tmf_resp);
401*3ebf6922SDarrick J. Wong 
402*3ebf6922SDarrick J. Wong 	return tmf_resp;
403*3ebf6922SDarrick J. Wong }
404*3ebf6922SDarrick J. Wong 
4052908d778SJames Bottomley void sas_scsi_recover_host(struct Scsi_Host *shost)
4062908d778SJames Bottomley {
4072908d778SJames Bottomley 	struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
4082908d778SJames Bottomley 	unsigned long flags;
4092908d778SJames Bottomley 	LIST_HEAD(error_q);
4102908d778SJames Bottomley 	struct scsi_cmnd *cmd, *n;
4112908d778SJames Bottomley 	enum task_disposition res = TASK_IS_DONE;
412*3ebf6922SDarrick J. Wong 	int tmf_resp, need_reset;
4132908d778SJames Bottomley 	struct sas_internal *i = to_sas_internal(shost->transportt);
414*3ebf6922SDarrick J. Wong 	struct sas_phy *task_sas_phy = NULL;
4152908d778SJames Bottomley 
4162908d778SJames Bottomley 	spin_lock_irqsave(shost->host_lock, flags);
4172908d778SJames Bottomley 	list_splice_init(&shost->eh_cmd_q, &error_q);
4182908d778SJames Bottomley 	spin_unlock_irqrestore(shost->host_lock, flags);
4192908d778SJames Bottomley 
4202908d778SJames Bottomley 	SAS_DPRINTK("Enter %s\n", __FUNCTION__);
4212908d778SJames Bottomley 
4222908d778SJames Bottomley 	/* All tasks on this list were marked SAS_TASK_STATE_ABORTED
4232908d778SJames Bottomley 	 * by sas_scsi_timed_out() callback.
4242908d778SJames Bottomley 	 */
4252908d778SJames Bottomley Again:
4262908d778SJames Bottomley 	SAS_DPRINTK("going over list...\n");
4272908d778SJames Bottomley 	list_for_each_entry_safe(cmd, n, &error_q, eh_entry) {
4282908d778SJames Bottomley 		struct sas_task *task = TO_SAS_TASK(cmd);
4292908d778SJames Bottomley 		list_del_init(&cmd->eh_entry);
430f456393eSDarrick J. Wong 
431f456393eSDarrick J. Wong 		if (!task) {
432f456393eSDarrick J. Wong 			SAS_DPRINTK("%s: taskless cmd?!\n", __FUNCTION__);
433f456393eSDarrick J. Wong 			continue;
434f456393eSDarrick J. Wong 		}
435*3ebf6922SDarrick J. Wong 
436*3ebf6922SDarrick J. Wong 		spin_lock_irqsave(&task->task_state_lock, flags);
437*3ebf6922SDarrick J. Wong 		need_reset = task->task_state_flags & SAS_TASK_NEED_DEV_RESET;
438*3ebf6922SDarrick J. Wong 		if (need_reset)
439*3ebf6922SDarrick J. Wong 			task_sas_phy = task->dev->port->phy;
440*3ebf6922SDarrick J. Wong 		spin_unlock_irqrestore(&task->task_state_lock, flags);
441*3ebf6922SDarrick J. Wong 
442f456393eSDarrick J. Wong 		SAS_DPRINTK("trying to find task 0x%p\n", task);
4432908d778SJames Bottomley 		res = sas_scsi_find_task(task);
4442908d778SJames Bottomley 
4452908d778SJames Bottomley 		cmd->eh_eflags = 0;
4462908d778SJames Bottomley 
4472908d778SJames Bottomley 		switch (res) {
4482908d778SJames Bottomley 		case TASK_IS_DONE:
4492908d778SJames Bottomley 			SAS_DPRINTK("%s: task 0x%p is done\n", __FUNCTION__,
4502908d778SJames Bottomley 				    task);
4512908d778SJames Bottomley 			task->task_done(task);
452*3ebf6922SDarrick J. Wong 			if (need_reset)
453*3ebf6922SDarrick J. Wong 				eh_reset_phy_helper(task_sas_phy);
4542908d778SJames Bottomley 			continue;
4552908d778SJames Bottomley 		case TASK_IS_ABORTED:
4562908d778SJames Bottomley 			SAS_DPRINTK("%s: task 0x%p is aborted\n",
4572908d778SJames Bottomley 				    __FUNCTION__, task);
4582908d778SJames Bottomley 			task->task_done(task);
459*3ebf6922SDarrick J. Wong 			if (need_reset)
460*3ebf6922SDarrick J. Wong 				eh_reset_phy_helper(task_sas_phy);
4612908d778SJames Bottomley 			continue;
4622908d778SJames Bottomley 		case TASK_IS_AT_LU:
4632908d778SJames Bottomley 			SAS_DPRINTK("task 0x%p is at LU: lu recover\n", task);
4642908d778SJames Bottomley 			tmf_resp = sas_recover_lu(task->dev, cmd);
4652908d778SJames Bottomley 			if (tmf_resp == TMF_RESP_FUNC_COMPLETE) {
4662908d778SJames Bottomley 				SAS_DPRINTK("dev %016llx LU %x is "
4672908d778SJames Bottomley 					    "recovered\n",
4682908d778SJames Bottomley 					    SAS_ADDR(task->dev),
4692908d778SJames Bottomley 					    cmd->device->lun);
4702908d778SJames Bottomley 				task->task_done(task);
471*3ebf6922SDarrick J. Wong 				if (need_reset)
472*3ebf6922SDarrick J. Wong 					eh_reset_phy_helper(task_sas_phy);
4732908d778SJames Bottomley 				sas_scsi_clear_queue_lu(&error_q, cmd);
4742908d778SJames Bottomley 				goto Again;
4752908d778SJames Bottomley 			}
4762908d778SJames Bottomley 			/* fallthrough */
4772908d778SJames Bottomley 		case TASK_IS_NOT_AT_LU:
4782908d778SJames Bottomley 			SAS_DPRINTK("task 0x%p is not at LU: I_T recover\n",
4792908d778SJames Bottomley 				    task);
4802908d778SJames Bottomley 			tmf_resp = sas_recover_I_T(task->dev);
4812908d778SJames Bottomley 			if (tmf_resp == TMF_RESP_FUNC_COMPLETE) {
4822908d778SJames Bottomley 				SAS_DPRINTK("I_T %016llx recovered\n",
4832908d778SJames Bottomley 					    SAS_ADDR(task->dev->sas_addr));
4842908d778SJames Bottomley 				task->task_done(task);
485*3ebf6922SDarrick J. Wong 				if (need_reset)
486*3ebf6922SDarrick J. Wong 					eh_reset_phy_helper(task_sas_phy);
4872908d778SJames Bottomley 				sas_scsi_clear_queue_I_T(&error_q, task->dev);
4882908d778SJames Bottomley 				goto Again;
4892908d778SJames Bottomley 			}
4902908d778SJames Bottomley 			/* Hammer time :-) */
4912908d778SJames Bottomley 			if (i->dft->lldd_clear_nexus_port) {
4922908d778SJames Bottomley 				struct asd_sas_port *port = task->dev->port;
4932908d778SJames Bottomley 				SAS_DPRINTK("clearing nexus for port:%d\n",
4942908d778SJames Bottomley 					    port->id);
4952908d778SJames Bottomley 				res = i->dft->lldd_clear_nexus_port(port);
4962908d778SJames Bottomley 				if (res == TMF_RESP_FUNC_COMPLETE) {
4972908d778SJames Bottomley 					SAS_DPRINTK("clear nexus port:%d "
4982908d778SJames Bottomley 						    "succeeded\n", port->id);
4992908d778SJames Bottomley 					task->task_done(task);
500*3ebf6922SDarrick J. Wong 					if (need_reset)
501*3ebf6922SDarrick J. Wong 						eh_reset_phy_helper(task_sas_phy);
5022908d778SJames Bottomley 					sas_scsi_clear_queue_port(&error_q,
5032908d778SJames Bottomley 								  port);
5042908d778SJames Bottomley 					goto Again;
5052908d778SJames Bottomley 				}
5062908d778SJames Bottomley 			}
5072908d778SJames Bottomley 			if (i->dft->lldd_clear_nexus_ha) {
5082908d778SJames Bottomley 				SAS_DPRINTK("clear nexus ha\n");
5092908d778SJames Bottomley 				res = i->dft->lldd_clear_nexus_ha(ha);
5102908d778SJames Bottomley 				if (res == TMF_RESP_FUNC_COMPLETE) {
5112908d778SJames Bottomley 					SAS_DPRINTK("clear nexus ha "
5122908d778SJames Bottomley 						    "succeeded\n");
5132908d778SJames Bottomley 					task->task_done(task);
514*3ebf6922SDarrick J. Wong 					if (need_reset)
515*3ebf6922SDarrick J. Wong 						eh_reset_phy_helper(task_sas_phy);
5162908d778SJames Bottomley 					goto out;
5172908d778SJames Bottomley 				}
5182908d778SJames Bottomley 			}
5192908d778SJames Bottomley 			/* If we are here -- this means that no amount
5202908d778SJames Bottomley 			 * of effort could recover from errors.  Quite
5212908d778SJames Bottomley 			 * possibly the HA just disappeared.
5222908d778SJames Bottomley 			 */
5232908d778SJames Bottomley 			SAS_DPRINTK("error from  device %llx, LUN %x "
5242908d778SJames Bottomley 				    "couldn't be recovered in any way\n",
5252908d778SJames Bottomley 				    SAS_ADDR(task->dev->sas_addr),
5262908d778SJames Bottomley 				    cmd->device->lun);
5272908d778SJames Bottomley 
5282908d778SJames Bottomley 			task->task_done(task);
529*3ebf6922SDarrick J. Wong 			if (need_reset)
530*3ebf6922SDarrick J. Wong 				eh_reset_phy_helper(task_sas_phy);
5312908d778SJames Bottomley 			goto clear_q;
5322908d778SJames Bottomley 		}
5332908d778SJames Bottomley 	}
5342908d778SJames Bottomley out:
535f456393eSDarrick J. Wong 	scsi_eh_flush_done_q(&ha->eh_done_q);
5362908d778SJames Bottomley 	SAS_DPRINTK("--- Exit %s\n", __FUNCTION__);
5372908d778SJames Bottomley 	return;
5382908d778SJames Bottomley clear_q:
5392908d778SJames Bottomley 	SAS_DPRINTK("--- Exit %s -- clear_q\n", __FUNCTION__);
5402908d778SJames Bottomley 	list_for_each_entry_safe(cmd, n, &error_q, eh_entry) {
5412908d778SJames Bottomley 		struct sas_task *task = TO_SAS_TASK(cmd);
5422908d778SJames Bottomley 		list_del_init(&cmd->eh_entry);
5432908d778SJames Bottomley 		task->task_done(task);
5442908d778SJames Bottomley 	}
5452908d778SJames Bottomley }
5462908d778SJames Bottomley 
5472908d778SJames Bottomley enum scsi_eh_timer_return sas_scsi_timed_out(struct scsi_cmnd *cmd)
5482908d778SJames Bottomley {
5492908d778SJames Bottomley 	struct sas_task *task = TO_SAS_TASK(cmd);
5502908d778SJames Bottomley 	unsigned long flags;
5512908d778SJames Bottomley 
5522908d778SJames Bottomley 	if (!task) {
5536d4dcd4dSDarrick J. Wong 		cmd->timeout_per_command /= 2;
5546d4dcd4dSDarrick J. Wong 		SAS_DPRINTK("command 0x%p, task 0x%p, gone: %s\n",
5556d4dcd4dSDarrick J. Wong 			    cmd, task, (cmd->timeout_per_command ?
5566d4dcd4dSDarrick J. Wong 			    "EH_RESET_TIMER" : "EH_NOT_HANDLED"));
5576d4dcd4dSDarrick J. Wong 		if (!cmd->timeout_per_command)
5586d4dcd4dSDarrick J. Wong 			return EH_NOT_HANDLED;
5596d4dcd4dSDarrick J. Wong 		return EH_RESET_TIMER;
5602908d778SJames Bottomley 	}
5612908d778SJames Bottomley 
5622908d778SJames Bottomley 	spin_lock_irqsave(&task->task_state_lock, flags);
5632908d778SJames Bottomley 	if (task->task_state_flags & SAS_TASK_STATE_DONE) {
5642908d778SJames Bottomley 		spin_unlock_irqrestore(&task->task_state_lock, flags);
5652908d778SJames Bottomley 		SAS_DPRINTK("command 0x%p, task 0x%p, timed out: EH_HANDLED\n",
5662908d778SJames Bottomley 			    cmd, task);
5672908d778SJames Bottomley 		return EH_HANDLED;
5682908d778SJames Bottomley 	}
569b218a0d8SDarrick J. Wong 	if (!(task->task_state_flags & SAS_TASK_AT_INITIATOR)) {
570b218a0d8SDarrick J. Wong 		spin_unlock_irqrestore(&task->task_state_lock, flags);
571b218a0d8SDarrick J. Wong 		SAS_DPRINTK("command 0x%p, task 0x%p, not at initiator: "
572b218a0d8SDarrick J. Wong 			    "EH_RESET_TIMER\n",
573b218a0d8SDarrick J. Wong 			    cmd, task);
574b218a0d8SDarrick J. Wong 		return EH_RESET_TIMER;
575b218a0d8SDarrick J. Wong 	}
5762908d778SJames Bottomley 	task->task_state_flags |= SAS_TASK_STATE_ABORTED;
5772908d778SJames Bottomley 	spin_unlock_irqrestore(&task->task_state_lock, flags);
5782908d778SJames Bottomley 
5792908d778SJames Bottomley 	SAS_DPRINTK("command 0x%p, task 0x%p, timed out: EH_NOT_HANDLED\n",
5802908d778SJames Bottomley 		    cmd, task);
5812908d778SJames Bottomley 
5822908d778SJames Bottomley 	return EH_NOT_HANDLED;
5832908d778SJames Bottomley }
5842908d778SJames Bottomley 
5852908d778SJames Bottomley struct domain_device *sas_find_dev_by_rphy(struct sas_rphy *rphy)
5862908d778SJames Bottomley {
5872908d778SJames Bottomley 	struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent);
5882908d778SJames Bottomley 	struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
5892908d778SJames Bottomley 	struct domain_device *found_dev = NULL;
5902908d778SJames Bottomley 	int i;
5912908d778SJames Bottomley 
5922908d778SJames Bottomley 	spin_lock(&ha->phy_port_lock);
5932908d778SJames Bottomley 	for (i = 0; i < ha->num_phys; i++) {
5942908d778SJames Bottomley 		struct asd_sas_port *port = ha->sas_port[i];
5952908d778SJames Bottomley 		struct domain_device *dev;
5962908d778SJames Bottomley 
5972908d778SJames Bottomley 		spin_lock(&port->dev_list_lock);
5982908d778SJames Bottomley 		list_for_each_entry(dev, &port->dev_list, dev_list_node) {
5992908d778SJames Bottomley 			if (rphy == dev->rphy) {
6002908d778SJames Bottomley 				found_dev = dev;
6012908d778SJames Bottomley 				spin_unlock(&port->dev_list_lock);
6022908d778SJames Bottomley 				goto found;
6032908d778SJames Bottomley 			}
6042908d778SJames Bottomley 		}
6052908d778SJames Bottomley 		spin_unlock(&port->dev_list_lock);
6062908d778SJames Bottomley 	}
6072908d778SJames Bottomley  found:
6082908d778SJames Bottomley 	spin_unlock(&ha->phy_port_lock);
6092908d778SJames Bottomley 
6102908d778SJames Bottomley 	return found_dev;
6112908d778SJames Bottomley }
6122908d778SJames Bottomley 
6132908d778SJames Bottomley static inline struct domain_device *sas_find_target(struct scsi_target *starget)
6142908d778SJames Bottomley {
6152908d778SJames Bottomley 	struct sas_rphy *rphy = dev_to_rphy(starget->dev.parent);
6162908d778SJames Bottomley 
6172908d778SJames Bottomley 	return sas_find_dev_by_rphy(rphy);
6182908d778SJames Bottomley }
6192908d778SJames Bottomley 
6202908d778SJames Bottomley int sas_target_alloc(struct scsi_target *starget)
6212908d778SJames Bottomley {
6222908d778SJames Bottomley 	struct domain_device *found_dev = sas_find_target(starget);
6232908d778SJames Bottomley 
6242908d778SJames Bottomley 	if (!found_dev)
6252908d778SJames Bottomley 		return -ENODEV;
6262908d778SJames Bottomley 
6272908d778SJames Bottomley 	starget->hostdata = found_dev;
6282908d778SJames Bottomley 	return 0;
6292908d778SJames Bottomley }
6302908d778SJames Bottomley 
6312908d778SJames Bottomley #define SAS_DEF_QD 32
6322908d778SJames Bottomley #define SAS_MAX_QD 64
6332908d778SJames Bottomley 
6342908d778SJames Bottomley int sas_slave_configure(struct scsi_device *scsi_dev)
6352908d778SJames Bottomley {
6362908d778SJames Bottomley 	struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
6372908d778SJames Bottomley 	struct sas_ha_struct *sas_ha;
6382908d778SJames Bottomley 
6392908d778SJames Bottomley 	BUG_ON(dev->rphy->identify.device_type != SAS_END_DEVICE);
6402908d778SJames Bottomley 
6412908d778SJames Bottomley 	sas_ha = dev->port->ha;
6422908d778SJames Bottomley 
6432908d778SJames Bottomley 	sas_read_port_mode_page(scsi_dev);
6442908d778SJames Bottomley 
6452908d778SJames Bottomley 	if (scsi_dev->tagged_supported) {
6462908d778SJames Bottomley 		scsi_set_tag_type(scsi_dev, MSG_SIMPLE_TAG);
6472908d778SJames Bottomley 		scsi_activate_tcq(scsi_dev, SAS_DEF_QD);
6482908d778SJames Bottomley 	} else {
6492908d778SJames Bottomley 		SAS_DPRINTK("device %llx, LUN %x doesn't support "
6502908d778SJames Bottomley 			    "TCQ\n", SAS_ADDR(dev->sas_addr),
6512908d778SJames Bottomley 			    scsi_dev->lun);
6522908d778SJames Bottomley 		scsi_dev->tagged_supported = 0;
6532908d778SJames Bottomley 		scsi_set_tag_type(scsi_dev, 0);
6542908d778SJames Bottomley 		scsi_deactivate_tcq(scsi_dev, 1);
6552908d778SJames Bottomley 	}
6562908d778SJames Bottomley 
6572908d778SJames Bottomley 	return 0;
6582908d778SJames Bottomley }
6592908d778SJames Bottomley 
6602908d778SJames Bottomley void sas_slave_destroy(struct scsi_device *scsi_dev)
6612908d778SJames Bottomley {
6622908d778SJames Bottomley }
6632908d778SJames Bottomley 
6642908d778SJames Bottomley int sas_change_queue_depth(struct scsi_device *scsi_dev, int new_depth)
6652908d778SJames Bottomley {
6662908d778SJames Bottomley 	int res = min(new_depth, SAS_MAX_QD);
6672908d778SJames Bottomley 
6682908d778SJames Bottomley 	if (scsi_dev->tagged_supported)
6692908d778SJames Bottomley 		scsi_adjust_queue_depth(scsi_dev, scsi_get_tag_type(scsi_dev),
6702908d778SJames Bottomley 					res);
6712908d778SJames Bottomley 	else {
6722908d778SJames Bottomley 		struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
6732908d778SJames Bottomley 		sas_printk("device %llx LUN %x queue depth changed to 1\n",
6742908d778SJames Bottomley 			   SAS_ADDR(dev->sas_addr),
6752908d778SJames Bottomley 			   scsi_dev->lun);
6762908d778SJames Bottomley 		scsi_adjust_queue_depth(scsi_dev, 0, 1);
6772908d778SJames Bottomley 		res = 1;
6782908d778SJames Bottomley 	}
6792908d778SJames Bottomley 
6802908d778SJames Bottomley 	return res;
6812908d778SJames Bottomley }
6822908d778SJames Bottomley 
6832908d778SJames Bottomley int sas_change_queue_type(struct scsi_device *scsi_dev, int qt)
6842908d778SJames Bottomley {
6852908d778SJames Bottomley 	if (!scsi_dev->tagged_supported)
6862908d778SJames Bottomley 		return 0;
6872908d778SJames Bottomley 
6882908d778SJames Bottomley 	scsi_deactivate_tcq(scsi_dev, 1);
6892908d778SJames Bottomley 
6902908d778SJames Bottomley 	scsi_set_tag_type(scsi_dev, qt);
6912908d778SJames Bottomley 	scsi_activate_tcq(scsi_dev, scsi_dev->queue_depth);
6922908d778SJames Bottomley 
6932908d778SJames Bottomley 	return qt;
6942908d778SJames Bottomley }
6952908d778SJames Bottomley 
6962908d778SJames Bottomley int sas_bios_param(struct scsi_device *scsi_dev,
6972908d778SJames Bottomley 			  struct block_device *bdev,
6982908d778SJames Bottomley 			  sector_t capacity, int *hsc)
6992908d778SJames Bottomley {
7002908d778SJames Bottomley 	hsc[0] = 255;
7012908d778SJames Bottomley 	hsc[1] = 63;
7022908d778SJames Bottomley 	sector_div(capacity, 255*63);
7032908d778SJames Bottomley 	hsc[2] = capacity;
7042908d778SJames Bottomley 
7052908d778SJames Bottomley 	return 0;
7062908d778SJames Bottomley }
7072908d778SJames Bottomley 
7082908d778SJames Bottomley /* ---------- Task Collector Thread implementation ---------- */
7092908d778SJames Bottomley 
7102908d778SJames Bottomley static void sas_queue(struct sas_ha_struct *sas_ha)
7112908d778SJames Bottomley {
7122908d778SJames Bottomley 	struct scsi_core *core = &sas_ha->core;
7132908d778SJames Bottomley 	unsigned long flags;
7142908d778SJames Bottomley 	LIST_HEAD(q);
7152908d778SJames Bottomley 	int can_queue;
7162908d778SJames Bottomley 	int res;
7172908d778SJames Bottomley 	struct sas_internal *i = to_sas_internal(core->shost->transportt);
7182908d778SJames Bottomley 
7192908d778SJames Bottomley 	spin_lock_irqsave(&core->task_queue_lock, flags);
7202908d778SJames Bottomley 	while (!core->queue_thread_kill &&
7212908d778SJames Bottomley 	       !list_empty(&core->task_queue)) {
7222908d778SJames Bottomley 
7232908d778SJames Bottomley 		can_queue = sas_ha->lldd_queue_size - core->task_queue_size;
7242908d778SJames Bottomley 		if (can_queue >= 0) {
7252908d778SJames Bottomley 			can_queue = core->task_queue_size;
7262908d778SJames Bottomley 			list_splice_init(&core->task_queue, &q);
7272908d778SJames Bottomley 		} else {
7282908d778SJames Bottomley 			struct list_head *a, *n;
7292908d778SJames Bottomley 
7302908d778SJames Bottomley 			can_queue = sas_ha->lldd_queue_size;
7312908d778SJames Bottomley 			list_for_each_safe(a, n, &core->task_queue) {
7322908d778SJames Bottomley 				list_move_tail(a, &q);
7332908d778SJames Bottomley 				if (--can_queue == 0)
7342908d778SJames Bottomley 					break;
7352908d778SJames Bottomley 			}
7362908d778SJames Bottomley 			can_queue = sas_ha->lldd_queue_size;
7372908d778SJames Bottomley 		}
7382908d778SJames Bottomley 		core->task_queue_size -= can_queue;
7392908d778SJames Bottomley 		spin_unlock_irqrestore(&core->task_queue_lock, flags);
7402908d778SJames Bottomley 		{
7412908d778SJames Bottomley 			struct sas_task *task = list_entry(q.next,
7422908d778SJames Bottomley 							   struct sas_task,
7432908d778SJames Bottomley 							   list);
7442908d778SJames Bottomley 			list_del_init(&q);
7452908d778SJames Bottomley 			res = i->dft->lldd_execute_task(task, can_queue,
7462908d778SJames Bottomley 							GFP_KERNEL);
7472908d778SJames Bottomley 			if (unlikely(res))
7482908d778SJames Bottomley 				__list_add(&q, task->list.prev, &task->list);
7492908d778SJames Bottomley 		}
7502908d778SJames Bottomley 		spin_lock_irqsave(&core->task_queue_lock, flags);
7512908d778SJames Bottomley 		if (res) {
7522908d778SJames Bottomley 			list_splice_init(&q, &core->task_queue); /*at head*/
7532908d778SJames Bottomley 			core->task_queue_size += can_queue;
7542908d778SJames Bottomley 		}
7552908d778SJames Bottomley 	}
7562908d778SJames Bottomley 	spin_unlock_irqrestore(&core->task_queue_lock, flags);
7572908d778SJames Bottomley }
7582908d778SJames Bottomley 
7592908d778SJames Bottomley static DECLARE_COMPLETION(queue_th_comp);
7602908d778SJames Bottomley 
7612908d778SJames Bottomley /**
7622908d778SJames Bottomley  * sas_queue_thread -- The Task Collector thread
7632908d778SJames Bottomley  * @_sas_ha: pointer to struct sas_ha
7642908d778SJames Bottomley  */
7652908d778SJames Bottomley static int sas_queue_thread(void *_sas_ha)
7662908d778SJames Bottomley {
7672908d778SJames Bottomley 	struct sas_ha_struct *sas_ha = _sas_ha;
7682908d778SJames Bottomley 	struct scsi_core *core = &sas_ha->core;
7692908d778SJames Bottomley 
7702908d778SJames Bottomley 	daemonize("sas_queue_%d", core->shost->host_no);
7712908d778SJames Bottomley 	current->flags |= PF_NOFREEZE;
7722908d778SJames Bottomley 
7732908d778SJames Bottomley 	complete(&queue_th_comp);
7742908d778SJames Bottomley 
7752908d778SJames Bottomley 	while (1) {
7762908d778SJames Bottomley 		down_interruptible(&core->queue_thread_sema);
7772908d778SJames Bottomley 		sas_queue(sas_ha);
7782908d778SJames Bottomley 		if (core->queue_thread_kill)
7792908d778SJames Bottomley 			break;
7802908d778SJames Bottomley 	}
7812908d778SJames Bottomley 
7822908d778SJames Bottomley 	complete(&queue_th_comp);
7832908d778SJames Bottomley 
7842908d778SJames Bottomley 	return 0;
7852908d778SJames Bottomley }
7862908d778SJames Bottomley 
7872908d778SJames Bottomley int sas_init_queue(struct sas_ha_struct *sas_ha)
7882908d778SJames Bottomley {
7892908d778SJames Bottomley 	int res;
7902908d778SJames Bottomley 	struct scsi_core *core = &sas_ha->core;
7912908d778SJames Bottomley 
7922908d778SJames Bottomley 	spin_lock_init(&core->task_queue_lock);
7932908d778SJames Bottomley 	core->task_queue_size = 0;
7942908d778SJames Bottomley 	INIT_LIST_HEAD(&core->task_queue);
7952908d778SJames Bottomley 	init_MUTEX_LOCKED(&core->queue_thread_sema);
7962908d778SJames Bottomley 
7972908d778SJames Bottomley 	res = kernel_thread(sas_queue_thread, sas_ha, 0);
7982908d778SJames Bottomley 	if (res >= 0)
7992908d778SJames Bottomley 		wait_for_completion(&queue_th_comp);
8002908d778SJames Bottomley 
8012908d778SJames Bottomley 	return res < 0 ? res : 0;
8022908d778SJames Bottomley }
8032908d778SJames Bottomley 
8042908d778SJames Bottomley void sas_shutdown_queue(struct sas_ha_struct *sas_ha)
8052908d778SJames Bottomley {
8062908d778SJames Bottomley 	unsigned long flags;
8072908d778SJames Bottomley 	struct scsi_core *core = &sas_ha->core;
8082908d778SJames Bottomley 	struct sas_task *task, *n;
8092908d778SJames Bottomley 
8102908d778SJames Bottomley 	init_completion(&queue_th_comp);
8112908d778SJames Bottomley 	core->queue_thread_kill = 1;
8122908d778SJames Bottomley 	up(&core->queue_thread_sema);
8132908d778SJames Bottomley 	wait_for_completion(&queue_th_comp);
8142908d778SJames Bottomley 
8152908d778SJames Bottomley 	if (!list_empty(&core->task_queue))
8162908d778SJames Bottomley 		SAS_DPRINTK("HA: %llx: scsi core task queue is NOT empty!?\n",
8172908d778SJames Bottomley 			    SAS_ADDR(sas_ha->sas_addr));
8182908d778SJames Bottomley 
8192908d778SJames Bottomley 	spin_lock_irqsave(&core->task_queue_lock, flags);
8202908d778SJames Bottomley 	list_for_each_entry_safe(task, n, &core->task_queue, list) {
8212908d778SJames Bottomley 		struct scsi_cmnd *cmd = task->uldd_task;
8222908d778SJames Bottomley 
8232908d778SJames Bottomley 		list_del_init(&task->list);
8242908d778SJames Bottomley 
8252908d778SJames Bottomley 		ASSIGN_SAS_TASK(cmd, NULL);
8262908d778SJames Bottomley 		sas_free_task(task);
8272908d778SJames Bottomley 		cmd->result = DID_ABORT << 16;
8282908d778SJames Bottomley 		cmd->scsi_done(cmd);
8292908d778SJames Bottomley 	}
8302908d778SJames Bottomley 	spin_unlock_irqrestore(&core->task_queue_lock, flags);
8312908d778SJames Bottomley }
8322908d778SJames Bottomley 
83379a5eb60SDarrick J. Wong static int do_sas_task_abort(struct sas_task *task)
83479a5eb60SDarrick J. Wong {
83579a5eb60SDarrick J. Wong 	struct scsi_cmnd *sc = task->uldd_task;
83679a5eb60SDarrick J. Wong 	struct sas_internal *si =
83779a5eb60SDarrick J. Wong 		to_sas_internal(task->dev->port->ha->core.shost->transportt);
83879a5eb60SDarrick J. Wong 	unsigned long flags;
83979a5eb60SDarrick J. Wong 	int res;
84079a5eb60SDarrick J. Wong 
84179a5eb60SDarrick J. Wong 	spin_lock_irqsave(&task->task_state_lock, flags);
84279a5eb60SDarrick J. Wong 	if (task->task_state_flags & SAS_TASK_STATE_ABORTED) {
84379a5eb60SDarrick J. Wong 		spin_unlock_irqrestore(&task->task_state_lock, flags);
84479a5eb60SDarrick J. Wong 		SAS_DPRINTK("%s: Task %p already aborted.\n", __FUNCTION__,
84579a5eb60SDarrick J. Wong 			    task);
84679a5eb60SDarrick J. Wong 		return 0;
84779a5eb60SDarrick J. Wong 	}
84879a5eb60SDarrick J. Wong 
84979a5eb60SDarrick J. Wong 	if (!(task->task_state_flags & SAS_TASK_STATE_DONE))
85079a5eb60SDarrick J. Wong 		task->task_state_flags |= SAS_TASK_STATE_ABORTED;
85179a5eb60SDarrick J. Wong 	spin_unlock_irqrestore(&task->task_state_lock, flags);
85279a5eb60SDarrick J. Wong 
85379a5eb60SDarrick J. Wong 	if (!si->dft->lldd_abort_task)
85479a5eb60SDarrick J. Wong 		return -ENODEV;
85579a5eb60SDarrick J. Wong 
85679a5eb60SDarrick J. Wong 	res = si->dft->lldd_abort_task(task);
85779a5eb60SDarrick J. Wong 	if ((task->task_state_flags & SAS_TASK_STATE_DONE) ||
85879a5eb60SDarrick J. Wong 	    (res == TMF_RESP_FUNC_COMPLETE))
85979a5eb60SDarrick J. Wong 	{
86079a5eb60SDarrick J. Wong 		/* SMP commands don't have scsi_cmds(?) */
86179a5eb60SDarrick J. Wong 		if (!sc) {
86279a5eb60SDarrick J. Wong 			task->task_done(task);
86379a5eb60SDarrick J. Wong 			return 0;
86479a5eb60SDarrick J. Wong 		}
86579a5eb60SDarrick J. Wong 		scsi_req_abort_cmd(sc);
86679a5eb60SDarrick J. Wong 		scsi_schedule_eh(sc->device->host);
86779a5eb60SDarrick J. Wong 		return 0;
86879a5eb60SDarrick J. Wong 	}
86979a5eb60SDarrick J. Wong 
87079a5eb60SDarrick J. Wong 	spin_lock_irqsave(&task->task_state_lock, flags);
87179a5eb60SDarrick J. Wong 	if (!(task->task_state_flags & SAS_TASK_STATE_DONE))
87279a5eb60SDarrick J. Wong 		task->task_state_flags &= ~SAS_TASK_STATE_ABORTED;
87379a5eb60SDarrick J. Wong 	spin_unlock_irqrestore(&task->task_state_lock, flags);
87479a5eb60SDarrick J. Wong 
87579a5eb60SDarrick J. Wong 	return -EAGAIN;
87679a5eb60SDarrick J. Wong }
87779a5eb60SDarrick J. Wong 
87806328b4fSDavid Howells void sas_task_abort(struct work_struct *work)
87979a5eb60SDarrick J. Wong {
88006328b4fSDavid Howells 	struct sas_task *task =
88106328b4fSDavid Howells 		container_of(work, struct sas_task, abort_work);
88279a5eb60SDarrick J. Wong 	int i;
88379a5eb60SDarrick J. Wong 
88479a5eb60SDarrick J. Wong 	for (i = 0; i < 5; i++)
88579a5eb60SDarrick J. Wong 		if (!do_sas_task_abort(task))
88679a5eb60SDarrick J. Wong 			return;
88779a5eb60SDarrick J. Wong 
88879a5eb60SDarrick J. Wong 	SAS_DPRINTK("%s: Could not kill task!\n", __FUNCTION__);
88979a5eb60SDarrick J. Wong }
89079a5eb60SDarrick J. Wong 
8912908d778SJames Bottomley EXPORT_SYMBOL_GPL(sas_queuecommand);
8922908d778SJames Bottomley EXPORT_SYMBOL_GPL(sas_target_alloc);
8932908d778SJames Bottomley EXPORT_SYMBOL_GPL(sas_slave_configure);
8942908d778SJames Bottomley EXPORT_SYMBOL_GPL(sas_slave_destroy);
8952908d778SJames Bottomley EXPORT_SYMBOL_GPL(sas_change_queue_depth);
8962908d778SJames Bottomley EXPORT_SYMBOL_GPL(sas_change_queue_type);
8972908d778SJames Bottomley EXPORT_SYMBOL_GPL(sas_bios_param);
89879a5eb60SDarrick J. Wong EXPORT_SYMBOL_GPL(sas_task_abort);
899dea22214SDarrick J. Wong EXPORT_SYMBOL_GPL(sas_phy_reset);
900acbf167dSDarrick J. Wong EXPORT_SYMBOL_GPL(sas_phy_enable);
901