xref: /openbmc/linux/drivers/scsi/libsas/sas_ata.c (revision 6c90466e2803d93ed47a980fbf184d35e012d895)
11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2338ec570SDarrick J. Wong /*
3338ec570SDarrick J. Wong  * Support for SATA devices on Serial Attached SCSI (SAS) controllers
4338ec570SDarrick J. Wong  *
5338ec570SDarrick J. Wong  * Copyright (C) 2006 IBM Corporation
6338ec570SDarrick J. Wong  *
7338ec570SDarrick J. Wong  * Written by: Darrick J. Wong <djwong@us.ibm.com>, IBM Corporation
8338ec570SDarrick J. Wong  */
9338ec570SDarrick J. Wong 
10b9142174SJames Bottomley #include <linux/scatterlist.h>
115a0e3ad6STejun Heo #include <linux/slab.h>
1250824d6cSDan Williams #include <linux/async.h>
1343a5ab15SDan Williams #include <linux/export.h>
14b9142174SJames Bottomley 
15338ec570SDarrick J. Wong #include <scsi/sas_ata.h>
16338ec570SDarrick J. Wong #include "sas_internal.h"
17338ec570SDarrick J. Wong #include <scsi/scsi_host.h>
18338ec570SDarrick J. Wong #include <scsi/scsi_device.h>
19338ec570SDarrick J. Wong #include <scsi/scsi_tcq.h>
20338ec570SDarrick J. Wong #include <scsi/scsi.h>
21338ec570SDarrick J. Wong #include <scsi/scsi_transport.h>
22338ec570SDarrick J. Wong #include <scsi/scsi_transport_sas.h>
23e15f669cSJason Yan #include "scsi_sas_internal.h"
24e15f669cSJason Yan #include "scsi_transport_api.h"
253a2755afSDarrick J. Wong #include <scsi/scsi_eh.h>
26338ec570SDarrick J. Wong 
27338ec570SDarrick J. Wong static enum ata_completion_errors sas_to_ata_err(struct task_status_struct *ts)
28338ec570SDarrick J. Wong {
29338ec570SDarrick J. Wong 	/* Cheesy attempt to translate SAS errors into ATA.  Hah! */
30338ec570SDarrick J. Wong 
31338ec570SDarrick J. Wong 	/* transport error */
32338ec570SDarrick J. Wong 	if (ts->resp == SAS_TASK_UNDELIVERED)
33338ec570SDarrick J. Wong 		return AC_ERR_ATA_BUS;
34338ec570SDarrick J. Wong 
35338ec570SDarrick J. Wong 	/* ts->resp == SAS_TASK_COMPLETE */
36338ec570SDarrick J. Wong 	/* task delivered, what happened afterwards? */
37338ec570SDarrick J. Wong 	switch (ts->stat) {
38338ec570SDarrick J. Wong 	case SAS_DEV_NO_RESPONSE:
39338ec570SDarrick J. Wong 		return AC_ERR_TIMEOUT;
40338ec570SDarrick J. Wong 	case SAS_INTERRUPTED:
41338ec570SDarrick J. Wong 	case SAS_PHY_DOWN:
42338ec570SDarrick J. Wong 	case SAS_NAK_R_ERR:
43338ec570SDarrick J. Wong 		return AC_ERR_ATA_BUS;
44338ec570SDarrick J. Wong 	case SAS_DATA_UNDERRUN:
45338ec570SDarrick J. Wong 		/*
46338ec570SDarrick J. Wong 		 * Some programs that use the taskfile interface
47338ec570SDarrick J. Wong 		 * (smartctl in particular) can cause underrun
48338ec570SDarrick J. Wong 		 * problems.  Ignore these errors, perhaps at our
49338ec570SDarrick J. Wong 		 * peril.
50338ec570SDarrick J. Wong 		 */
51338ec570SDarrick J. Wong 		return 0;
52338ec570SDarrick J. Wong 	case SAS_DATA_OVERRUN:
53338ec570SDarrick J. Wong 	case SAS_QUEUE_FULL:
54338ec570SDarrick J. Wong 	case SAS_DEVICE_UNKNOWN:
55338ec570SDarrick J. Wong 	case SAS_OPEN_TO:
56338ec570SDarrick J. Wong 	case SAS_OPEN_REJECT:
5715ba7806SJohn Garry 		pr_warn("%s: Saw error %d.  What to do?\n",
58cadbd4a5SHarvey Harrison 			__func__, ts->stat);
59338ec570SDarrick J. Wong 		return AC_ERR_OTHER;
6075c0b386SJames Bottomley 	case SAM_STAT_CHECK_CONDITION:
61338ec570SDarrick J. Wong 	case SAS_ABORTED_TASK:
62338ec570SDarrick J. Wong 		return AC_ERR_DEV;
63338ec570SDarrick J. Wong 	case SAS_PROTO_RESPONSE:
64338ec570SDarrick J. Wong 		/* This means the ending_fis has the error
65c03f2422SLuo Jiaxing 		 * value; return 0 here to collect it
66c03f2422SLuo Jiaxing 		 */
67338ec570SDarrick J. Wong 		return 0;
68338ec570SDarrick J. Wong 	default:
69338ec570SDarrick J. Wong 		return 0;
70338ec570SDarrick J. Wong 	}
71338ec570SDarrick J. Wong }
72338ec570SDarrick J. Wong 
73338ec570SDarrick J. Wong static void sas_ata_task_done(struct sas_task *task)
74338ec570SDarrick J. Wong {
75338ec570SDarrick J. Wong 	struct ata_queued_cmd *qc = task->uldd_task;
769095a64aSDan Williams 	struct domain_device *dev = task->dev;
77338ec570SDarrick J. Wong 	struct task_status_struct *stat = &task->task_status;
78338ec570SDarrick J. Wong 	struct ata_task_resp *resp = (struct ata_task_resp *)stat->buf;
799095a64aSDan Williams 	struct sas_ha_struct *sas_ha = dev->port->ha;
80338ec570SDarrick J. Wong 	enum ata_completion_errors ac;
813eb7a51aSDarrick J. Wong 	unsigned long flags;
82bb650a1bSXiangliang Yu 	struct ata_link *link;
833dff5721SDan Williams 	struct ata_port *ap;
84338ec570SDarrick J. Wong 
859095a64aSDan Williams 	spin_lock_irqsave(&dev->done_lock, flags);
869095a64aSDan Williams 	if (test_bit(SAS_HA_FROZEN, &sas_ha->state))
879095a64aSDan Williams 		task = NULL;
889095a64aSDan Williams 	else if (qc && qc->scsicmd)
899095a64aSDan Williams 		ASSIGN_SAS_TASK(qc->scsicmd, NULL);
909095a64aSDan Williams 	spin_unlock_irqrestore(&dev->done_lock, flags);
919095a64aSDan Williams 
929095a64aSDan Williams 	/* check if libsas-eh got to the task before us */
939095a64aSDan Williams 	if (unlikely(!task))
949095a64aSDan Williams 		return;
959095a64aSDan Williams 
961c50dc83SDarrick J. Wong 	if (!qc)
971c50dc83SDarrick J. Wong 		goto qc_already_gone;
981c50dc83SDarrick J. Wong 
993dff5721SDan Williams 	ap = qc->ap;
1003dff5721SDan Williams 	link = &ap->link;
1011c50dc83SDarrick J. Wong 
1023dff5721SDan Williams 	spin_lock_irqsave(ap->lock, flags);
1033dff5721SDan Williams 	/* check if we lost the race with libata/sas_ata_post_internal() */
1043dff5721SDan Williams 	if (unlikely(ap->pflags & ATA_PFLAG_FROZEN)) {
1053dff5721SDan Williams 		spin_unlock_irqrestore(ap->lock, flags);
1063dff5721SDan Williams 		if (qc->scsicmd)
1073dff5721SDan Williams 			goto qc_already_gone;
1083dff5721SDan Williams 		else {
1093dff5721SDan Williams 			/* if eh is not involved and the port is frozen then the
1103dff5721SDan Williams 			 * ata internal abort process has taken responsibility
1113dff5721SDan Williams 			 * for this sas_task
1123dff5721SDan Williams 			 */
1133dff5721SDan Williams 			return;
1143dff5721SDan Williams 		}
1153dff5721SDan Williams 	}
1163dff5721SDan Williams 
117d377f415SBart Van Assche 	if (stat->stat == SAS_PROTO_RESPONSE ||
118d377f415SBart Van Assche 	    stat->stat == SAS_SAM_STAT_GOOD ||
119d377f415SBart Van Assche 	    (stat->stat == SAS_SAM_STAT_CHECK_CONDITION &&
120d377f415SBart Van Assche 	      dev->sata_dev.class == ATA_DEV_ATAPI)) {
1216ef1b512SDan Williams 		memcpy(dev->sata_dev.fis, resp->ending_fis, ATA_RESP_FIS_SIZE);
122bb650a1bSXiangliang Yu 
123bb650a1bSXiangliang Yu 		if (!link->sactive) {
1246ef1b512SDan Williams 			qc->err_mask |= ac_err_mask(dev->sata_dev.fis[2]);
125bb650a1bSXiangliang Yu 		} else {
1266ef1b512SDan Williams 			link->eh_info.err_mask |= ac_err_mask(dev->sata_dev.fis[2]);
127bb650a1bSXiangliang Yu 			if (unlikely(link->eh_info.err_mask))
128bb650a1bSXiangliang Yu 				qc->flags |= ATA_QCFLAG_FAILED;
129bb650a1bSXiangliang Yu 		}
13075c0b386SJames Bottomley 	} else {
131338ec570SDarrick J. Wong 		ac = sas_to_ata_err(stat);
132338ec570SDarrick J. Wong 		if (ac) {
133b3e3d4c6SJohn Garry 			pr_warn("%s: SAS error 0x%x\n", __func__, stat->stat);
134338ec570SDarrick J. Wong 			/* We saw a SAS error. Send a vague error. */
135bb650a1bSXiangliang Yu 			if (!link->sactive) {
136338ec570SDarrick J. Wong 				qc->err_mask = ac;
137bb650a1bSXiangliang Yu 			} else {
138bb650a1bSXiangliang Yu 				link->eh_info.err_mask |= AC_ERR_DEV;
139bb650a1bSXiangliang Yu 				qc->flags |= ATA_QCFLAG_FAILED;
140bb650a1bSXiangliang Yu 			}
141bb650a1bSXiangliang Yu 
142cc22efbeSJohn Garry 			dev->sata_dev.fis[2] = ATA_ERR | ATA_DRDY; /* tf status */
143cc22efbeSJohn Garry 			dev->sata_dev.fis[3] = ATA_ABORTED; /* tf error */
144338ec570SDarrick J. Wong 		}
145338ec570SDarrick J. Wong 	}
146338ec570SDarrick J. Wong 
1471c50dc83SDarrick J. Wong 	qc->lldd_task = NULL;
148338ec570SDarrick J. Wong 	ata_qc_complete(qc);
1493dff5721SDan Williams 	spin_unlock_irqrestore(ap->lock, flags);
1503eb7a51aSDarrick J. Wong 
1511c50dc83SDarrick J. Wong qc_already_gone:
152338ec570SDarrick J. Wong 	sas_free_task(task);
153338ec570SDarrick J. Wong }
154338ec570SDarrick J. Wong 
155338ec570SDarrick J. Wong static unsigned int sas_ata_qc_issue(struct ata_queued_cmd *qc)
1561e4ffb83SJules Irenge 	__must_hold(ap->lock)
157338ec570SDarrick J. Wong {
158338ec570SDarrick J. Wong 	struct sas_task *task;
159312d3e56SDan Williams 	struct scatterlist *sg;
160312d3e56SDan Williams 	int ret = AC_ERR_SYSTEM;
161312d3e56SDan Williams 	unsigned int si, xfer = 0;
162312d3e56SDan Williams 	struct ata_port *ap = qc->ap;
163312d3e56SDan Williams 	struct domain_device *dev = ap->private_data;
164338ec570SDarrick J. Wong 	struct sas_ha_struct *sas_ha = dev->port->ha;
165338ec570SDarrick J. Wong 	struct Scsi_Host *host = sas_ha->core.shost;
166338ec570SDarrick J. Wong 	struct sas_internal *i = to_sas_internal(host->transportt);
167312d3e56SDan Williams 
1682da11d42SSebastian Andrzej Siewior 	/* TODO: we should try to remove that unlock */
169312d3e56SDan Williams 	spin_unlock(ap->lock);
170338ec570SDarrick J. Wong 
17156dd2c06SDarrick J. Wong 	/* If the device fell off, no sense in issuing commands */
172e139942dSDan Williams 	if (test_bit(SAS_DEV_GONE, &dev->state))
173312d3e56SDan Williams 		goto out;
17456dd2c06SDarrick J. Wong 
175338ec570SDarrick J. Wong 	task = sas_alloc_task(GFP_ATOMIC);
176338ec570SDarrick J. Wong 	if (!task)
177312d3e56SDan Williams 		goto out;
178338ec570SDarrick J. Wong 	task->dev = dev;
179338ec570SDarrick J. Wong 	task->task_proto = SAS_PROTOCOL_STP;
180338ec570SDarrick J. Wong 	task->task_done = sas_ata_task_done;
181338ec570SDarrick J. Wong 
182a1e7c799SDamien Le Moal 	/* For NCQ commands, zero out the tag libata assigned us */
183a1e7c799SDamien Le Moal 	if (ata_is_ncq(qc->tf.protocol))
184338ec570SDarrick J. Wong 		qc->tf.nsect = 0;
185338ec570SDarrick J. Wong 
186ae5fbae0SDan Williams 	ata_tf_to_fis(&qc->tf, qc->dev->link->pmp, 1, (u8 *)&task->ata_task.fis);
187338ec570SDarrick J. Wong 	task->uldd_task = qc;
188405e66b3STejun Heo 	if (ata_is_atapi(qc->tf.protocol)) {
189338ec570SDarrick J. Wong 		memcpy(task->ata_task.atapi_packet, qc->cdb, qc->dev->cdb_len);
190dde20207SJames Bottomley 		task->total_xfer_len = qc->nbytes;
191dde20207SJames Bottomley 		task->num_scatter = qc->n_elem;
192176ddd89SJolly Shah 		task->data_dir = qc->dma_dir;
1938454563eSDamien Le Moal 	} else if (!ata_is_data(qc->tf.protocol)) {
194176ddd89SJolly Shah 		task->data_dir = DMA_NONE;
195338ec570SDarrick J. Wong 	} else {
196ff2aeb1eSTejun Heo 		for_each_sg(qc->sg, sg, qc->n_elem, si)
1979702c67cSJohn Garry 			xfer += sg_dma_len(sg);
198338ec570SDarrick J. Wong 
199338ec570SDarrick J. Wong 		task->total_xfer_len = xfer;
200ff2aeb1eSTejun Heo 		task->num_scatter = si;
201338ec570SDarrick J. Wong 		task->data_dir = qc->dma_dir;
202176ddd89SJolly Shah 	}
203ff2aeb1eSTejun Heo 	task->scatter = qc->sg;
204338ec570SDarrick J. Wong 	task->ata_task.retry_count = 1;
2051c50dc83SDarrick J. Wong 	qc->lldd_task = task;
206338ec570SDarrick J. Wong 
207b38d4d85SHannes Reinecke 	task->ata_task.use_ncq = ata_is_ncq(qc->tf.protocol);
208b38d4d85SHannes Reinecke 	task->ata_task.dma_xfer = ata_is_dma(qc->tf.protocol);
209338ec570SDarrick J. Wong 
210fe059f12SDarrick J. Wong 	if (qc->scsicmd)
211fe059f12SDarrick J. Wong 		ASSIGN_SAS_TASK(qc->scsicmd, task);
212fe059f12SDarrick J. Wong 
21379855d17SChristoph Hellwig 	ret = i->dft->lldd_execute_task(task, GFP_ATOMIC);
214312d3e56SDan Williams 	if (ret) {
21515ba7806SJohn Garry 		pr_debug("lldd_execute_task returned: %d\n", ret);
216338ec570SDarrick J. Wong 
217fe059f12SDarrick J. Wong 		if (qc->scsicmd)
218fe059f12SDarrick J. Wong 			ASSIGN_SAS_TASK(qc->scsicmd, NULL);
219338ec570SDarrick J. Wong 		sas_free_task(task);
220354a086dSWei Fang 		qc->lldd_task = NULL;
221312d3e56SDan Williams 		ret = AC_ERR_SYSTEM;
222338ec570SDarrick J. Wong 	}
223338ec570SDarrick J. Wong 
224312d3e56SDan Williams  out:
225312d3e56SDan Williams 	spin_lock(ap->lock);
226312d3e56SDan Williams 	return ret;
227338ec570SDarrick J. Wong }
228338ec570SDarrick J. Wong 
2294c9bf4e7STejun Heo static bool sas_ata_qc_fill_rtf(struct ata_queued_cmd *qc)
2304c9bf4e7STejun Heo {
2314c9bf4e7STejun Heo 	struct domain_device *dev = qc->ap->private_data;
2324c9bf4e7STejun Heo 
2336ef1b512SDan Williams 	ata_tf_from_fis(dev->sata_dev.fis, &qc->result_tf);
2344c9bf4e7STejun Heo 	return true;
2354c9bf4e7STejun Heo }
2364c9bf4e7STejun Heo 
23736a39947SDan Williams static struct sas_internal *dev_to_sas_internal(struct domain_device *dev)
23836a39947SDan Williams {
23936a39947SDan Williams 	return to_sas_internal(dev->port->ha->core.shost->transportt);
24036a39947SDan Williams }
24136a39947SDan Williams 
242*6c90466eSJason Yan static int sas_get_ata_command_set(struct domain_device *dev)
243*6c90466eSJason Yan {
244*6c90466eSJason Yan 	struct ata_taskfile tf;
245*6c90466eSJason Yan 
246*6c90466eSJason Yan 	if (dev->dev_type == SAS_SATA_PENDING)
247*6c90466eSJason Yan 		return ATA_DEV_UNKNOWN;
248*6c90466eSJason Yan 
249*6c90466eSJason Yan 	ata_tf_from_fis(dev->frame_rcvd, &tf);
250*6c90466eSJason Yan 
251*6c90466eSJason Yan 	return ata_dev_classify(&tf);
252*6c90466eSJason Yan }
253354cf829SDan Williams 
254354cf829SDan Williams int sas_get_ata_info(struct domain_device *dev, struct ex_phy *phy)
255354cf829SDan Williams {
256354cf829SDan Williams 	if (phy->attached_tproto & SAS_PROTOCOL_STP)
257354cf829SDan Williams 		dev->tproto = phy->attached_tproto;
258354cf829SDan Williams 	if (phy->attached_sata_dev)
259aa9f8328SJames Bottomley 		dev->tproto |= SAS_SATA_DEV;
260354cf829SDan Williams 
261aa9f8328SJames Bottomley 	if (phy->attached_dev_type == SAS_SATA_PENDING)
262aa9f8328SJames Bottomley 		dev->dev_type = SAS_SATA_PENDING;
263354cf829SDan Williams 	else {
264354cf829SDan Williams 		int res;
265354cf829SDan Williams 
266aa9f8328SJames Bottomley 		dev->dev_type = SAS_SATA_DEV;
267354cf829SDan Williams 		res = sas_get_report_phy_sata(dev->parent, phy->phy_id,
268354cf829SDan Williams 					      &dev->sata_dev.rps_resp);
269354cf829SDan Williams 		if (res) {
2703c236f8cSJohn Garry 			pr_debug("report phy sata to %016llx:%02d returned 0x%x\n",
27115ba7806SJohn Garry 				 SAS_ADDR(dev->parent->sas_addr),
272354cf829SDan Williams 				 phy->phy_id, res);
273354cf829SDan Williams 			return res;
274354cf829SDan Williams 		}
275354cf829SDan Williams 		memcpy(dev->frame_rcvd, &dev->sata_dev.rps_resp.rps.fis,
276354cf829SDan Williams 		       sizeof(struct dev_to_host_fis));
2771cbd772dSHannes Reinecke 		dev->sata_dev.class = sas_get_ata_command_set(dev);
278354cf829SDan Williams 	}
279354cf829SDan Williams 	return 0;
280354cf829SDan Williams }
281354cf829SDan Williams 
282354cf829SDan Williams static int sas_ata_clear_pending(struct domain_device *dev, struct ex_phy *phy)
283354cf829SDan Williams {
284354cf829SDan Williams 	int res;
285354cf829SDan Williams 
286354cf829SDan Williams 	/* we weren't pending, so successfully end the reset sequence now */
287aa9f8328SJames Bottomley 	if (dev->dev_type != SAS_SATA_PENDING)
288354cf829SDan Williams 		return 1;
289354cf829SDan Williams 
290354cf829SDan Williams 	/* hmmm, if this succeeds do we need to repost the domain_device to the
291354cf829SDan Williams 	 * lldd so it can pick up new parameters?
292354cf829SDan Williams 	 */
293354cf829SDan Williams 	res = sas_get_ata_info(dev, phy);
294354cf829SDan Williams 	if (res)
295354cf829SDan Williams 		return 0; /* retry */
296354cf829SDan Williams 	else
297354cf829SDan Williams 		return 1;
298354cf829SDan Williams }
299354cf829SDan Williams 
3009181ce3cSJie Zhan int smp_ata_check_ready_type(struct ata_link *link)
3019181ce3cSJie Zhan {
3029181ce3cSJie Zhan 	struct domain_device *dev = link->ap->private_data;
3039181ce3cSJie Zhan 	struct sas_phy *phy = sas_get_local_phy(dev);
3049181ce3cSJie Zhan 	struct domain_device *ex_dev = dev->parent;
3059181ce3cSJie Zhan 	enum sas_device_type type = SAS_PHY_UNUSED;
3069181ce3cSJie Zhan 	u8 sas_addr[SAS_ADDR_SIZE];
3079181ce3cSJie Zhan 	int res;
3089181ce3cSJie Zhan 
3099181ce3cSJie Zhan 	res = sas_get_phy_attached_dev(ex_dev, phy->number, sas_addr, &type);
3109181ce3cSJie Zhan 	sas_put_local_phy(phy);
3119181ce3cSJie Zhan 	if (res)
3129181ce3cSJie Zhan 		return res;
3139181ce3cSJie Zhan 
3149181ce3cSJie Zhan 	switch (type) {
3159181ce3cSJie Zhan 	case SAS_SATA_PENDING:
3169181ce3cSJie Zhan 		return 0;
3179181ce3cSJie Zhan 	case SAS_END_DEVICE:
3189181ce3cSJie Zhan 		return 1;
3199181ce3cSJie Zhan 	default:
3209181ce3cSJie Zhan 		return -ENODEV;
3219181ce3cSJie Zhan 	}
3229181ce3cSJie Zhan }
3239181ce3cSJie Zhan EXPORT_SYMBOL_GPL(smp_ata_check_ready_type);
3249181ce3cSJie Zhan 
32536a39947SDan Williams static int smp_ata_check_ready(struct ata_link *link)
32636a39947SDan Williams {
32736a39947SDan Williams 	int res;
32836a39947SDan Williams 	struct ata_port *ap = link->ap;
32936a39947SDan Williams 	struct domain_device *dev = ap->private_data;
33036a39947SDan Williams 	struct domain_device *ex_dev = dev->parent;
331f41a0c44SDan Williams 	struct sas_phy *phy = sas_get_local_phy(dev);
332354cf829SDan Williams 	struct ex_phy *ex_phy = &ex_dev->ex_dev.ex_phy[phy->number];
33336a39947SDan Williams 
334354cf829SDan Williams 	res = sas_ex_phy_discover(ex_dev, phy->number);
335f41a0c44SDan Williams 	sas_put_local_phy(phy);
336354cf829SDan Williams 
33736a39947SDan Williams 	/* break the wait early if the expander is unreachable,
33836a39947SDan Williams 	 * otherwise keep polling
33936a39947SDan Williams 	 */
34036a39947SDan Williams 	if (res == -ECOMM)
34136a39947SDan Williams 		return res;
342354cf829SDan Williams 	if (res != SMP_RESP_FUNC_ACC)
34336a39947SDan Williams 		return 0;
344354cf829SDan Williams 
345354cf829SDan Williams 	switch (ex_phy->attached_dev_type) {
346aa9f8328SJames Bottomley 	case SAS_SATA_PENDING:
347354cf829SDan Williams 		return 0;
348aa9f8328SJames Bottomley 	case SAS_END_DEVICE:
349354cf829SDan Williams 		if (ex_phy->attached_sata_dev)
350354cf829SDan Williams 			return sas_ata_clear_pending(dev, ex_phy);
351df561f66SGustavo A. R. Silva 		fallthrough;
352354cf829SDan Williams 	default:
353354cf829SDan Williams 		return -ENODEV;
354354cf829SDan Williams 	}
35536a39947SDan Williams }
35636a39947SDan Williams 
35736a39947SDan Williams static int local_ata_check_ready(struct ata_link *link)
358338ec570SDarrick J. Wong {
35900dd4998SJames Bottomley 	struct ata_port *ap = link->ap;
360338ec570SDarrick J. Wong 	struct domain_device *dev = ap->private_data;
36136a39947SDan Williams 	struct sas_internal *i = dev_to_sas_internal(dev);
362338ec570SDarrick J. Wong 
36336a39947SDan Williams 	if (i->dft->lldd_ata_check_ready)
36436a39947SDan Williams 		return i->dft->lldd_ata_check_ready(dev);
36536a39947SDan Williams 	else {
36636a39947SDan Williams 		/* lldd's that don't implement 'ready' checking get the
36736a39947SDan Williams 		 * old default behavior of not coordinating reset
36836a39947SDan Williams 		 * recovery with libata
36936a39947SDan Williams 		 */
37036a39947SDan Williams 		return 1;
37136a39947SDan Williams 	}
37200dd4998SJames Bottomley }
373338ec570SDarrick J. Wong 
374d214d81eSDan Williams static int sas_ata_printk(const char *level, const struct domain_device *ddev,
375d214d81eSDan Williams 			  const char *fmt, ...)
376d214d81eSDan Williams {
377d214d81eSDan Williams 	struct ata_port *ap = ddev->sata_dev.ap;
378d214d81eSDan Williams 	struct device *dev = &ddev->rphy->dev;
379d214d81eSDan Williams 	struct va_format vaf;
380d214d81eSDan Williams 	va_list args;
381d214d81eSDan Williams 	int r;
382d214d81eSDan Williams 
383d214d81eSDan Williams 	va_start(args, fmt);
384d214d81eSDan Williams 
385d214d81eSDan Williams 	vaf.fmt = fmt;
386d214d81eSDan Williams 	vaf.va = &args;
387d214d81eSDan Williams 
388d188e5dbSJohn Garry 	r = printk("%s" SAS_FMT "ata%u: %s: %pV",
389d214d81eSDan Williams 		   level, ap->print_id, dev_name(dev), &vaf);
390d214d81eSDan Williams 
391d214d81eSDan Williams 	va_end(args);
392d214d81eSDan Williams 
393d214d81eSDan Williams 	return r;
394d214d81eSDan Williams }
395d214d81eSDan Williams 
3964d450cf2SJie Zhan static int sas_ata_wait_after_reset(struct domain_device *dev, unsigned long deadline)
39736a39947SDan Williams {
398057e5fc0SJohn Garry 	struct sata_device *sata_dev = &dev->sata_dev;
39936a39947SDan Williams 	int (*check_ready)(struct ata_link *link);
400057e5fc0SJohn Garry 	struct ata_port *ap = sata_dev->ap;
401057e5fc0SJohn Garry 	struct ata_link *link = &ap->link;
402057e5fc0SJohn Garry 	struct sas_phy *phy;
403057e5fc0SJohn Garry 	int ret;
40436a39947SDan Williams 
405f41a0c44SDan Williams 	phy = sas_get_local_phy(dev);
40636a39947SDan Williams 	if (scsi_is_sas_phy_local(phy))
40736a39947SDan Williams 		check_ready = local_ata_check_ready;
40836a39947SDan Williams 	else
40936a39947SDan Williams 		check_ready = smp_ata_check_ready;
410f41a0c44SDan Williams 	sas_put_local_phy(phy);
41136a39947SDan Williams 
41236a39947SDan Williams 	ret = ata_wait_after_reset(link, deadline, check_ready);
41336a39947SDan Williams 	if (ret && ret != -EAGAIN)
414d214d81eSDan Williams 		sas_ata_printk(KERN_ERR, dev, "reset failed (errno=%d)\n", ret);
41536a39947SDan Williams 
416057e5fc0SJohn Garry 	return ret;
417057e5fc0SJohn Garry }
418057e5fc0SJohn Garry 
419057e5fc0SJohn Garry static int sas_ata_hard_reset(struct ata_link *link, unsigned int *class,
420057e5fc0SJohn Garry 			      unsigned long deadline)
421057e5fc0SJohn Garry {
422057e5fc0SJohn Garry 	struct ata_port *ap = link->ap;
423057e5fc0SJohn Garry 	struct domain_device *dev = ap->private_data;
424057e5fc0SJohn Garry 	struct sas_internal *i = dev_to_sas_internal(dev);
425057e5fc0SJohn Garry 	int ret;
426057e5fc0SJohn Garry 
427057e5fc0SJohn Garry 	ret = i->dft->lldd_I_T_nexus_reset(dev);
428057e5fc0SJohn Garry 	if (ret == -ENODEV)
429057e5fc0SJohn Garry 		return ret;
430057e5fc0SJohn Garry 
431057e5fc0SJohn Garry 	if (ret != TMF_RESP_FUNC_COMPLETE)
432057e5fc0SJohn Garry 		sas_ata_printk(KERN_DEBUG, dev, "Unable to reset ata device?\n");
433057e5fc0SJohn Garry 
434057e5fc0SJohn Garry 	ret = sas_ata_wait_after_reset(dev, deadline);
435057e5fc0SJohn Garry 
4361cbd772dSHannes Reinecke 	*class = dev->sata_dev.class;
437338ec570SDarrick J. Wong 
438338ec570SDarrick J. Wong 	ap->cbl = ATA_CBL_SATA;
43900dd4998SJames Bottomley 	return ret;
440338ec570SDarrick J. Wong }
441338ec570SDarrick J. Wong 
4423dff5721SDan Williams /*
4433dff5721SDan Williams  * notify the lldd to forget the sas_task for this internal ata command
4443dff5721SDan Williams  * that bypasses scsi-eh
4453dff5721SDan Williams  */
4463dff5721SDan Williams static void sas_ata_internal_abort(struct sas_task *task)
4473dff5721SDan Williams {
44836a39947SDan Williams 	struct sas_internal *si = dev_to_sas_internal(task->dev);
4493dff5721SDan Williams 	unsigned long flags;
4503dff5721SDan Williams 	int res;
4513dff5721SDan Williams 
4523dff5721SDan Williams 	spin_lock_irqsave(&task->task_state_lock, flags);
4533dff5721SDan Williams 	if (task->task_state_flags & SAS_TASK_STATE_ABORTED ||
4543dff5721SDan Williams 	    task->task_state_flags & SAS_TASK_STATE_DONE) {
4553dff5721SDan Williams 		spin_unlock_irqrestore(&task->task_state_lock, flags);
45615ba7806SJohn Garry 		pr_debug("%s: Task %p already finished.\n", __func__, task);
4573dff5721SDan Williams 		goto out;
4583dff5721SDan Williams 	}
4593dff5721SDan Williams 	task->task_state_flags |= SAS_TASK_STATE_ABORTED;
4603dff5721SDan Williams 	spin_unlock_irqrestore(&task->task_state_lock, flags);
4613dff5721SDan Williams 
4623dff5721SDan Williams 	res = si->dft->lldd_abort_task(task);
4633dff5721SDan Williams 
4643dff5721SDan Williams 	spin_lock_irqsave(&task->task_state_lock, flags);
4653dff5721SDan Williams 	if (task->task_state_flags & SAS_TASK_STATE_DONE ||
4663dff5721SDan Williams 	    res == TMF_RESP_FUNC_COMPLETE) {
4673dff5721SDan Williams 		spin_unlock_irqrestore(&task->task_state_lock, flags);
4683dff5721SDan Williams 		goto out;
4693dff5721SDan Williams 	}
4703dff5721SDan Williams 
4713dff5721SDan Williams 	/* XXX we are not prepared to deal with ->lldd_abort_task()
4723dff5721SDan Williams 	 * failures.  TODO: lldds need to unconditionally forget about
4733dff5721SDan Williams 	 * aborted ata tasks, otherwise we (likely) leak the sas task
4743dff5721SDan Williams 	 * here
4753dff5721SDan Williams 	 */
47615ba7806SJohn Garry 	pr_warn("%s: Task %p leaked.\n", __func__, task);
4773dff5721SDan Williams 
4783dff5721SDan Williams 	if (!(task->task_state_flags & SAS_TASK_STATE_DONE))
4793dff5721SDan Williams 		task->task_state_flags &= ~SAS_TASK_STATE_ABORTED;
4803dff5721SDan Williams 	spin_unlock_irqrestore(&task->task_state_lock, flags);
4813dff5721SDan Williams 
4823dff5721SDan Williams 	return;
4833dff5721SDan Williams  out:
4843dff5721SDan Williams 	sas_free_task(task);
4853dff5721SDan Williams }
4863dff5721SDan Williams 
487338ec570SDarrick J. Wong static void sas_ata_post_internal(struct ata_queued_cmd *qc)
488338ec570SDarrick J. Wong {
489338ec570SDarrick J. Wong 	if (qc->flags & ATA_QCFLAG_FAILED)
490338ec570SDarrick J. Wong 		qc->err_mask |= AC_ERR_OTHER;
491338ec570SDarrick J. Wong 
4921c50dc83SDarrick J. Wong 	if (qc->err_mask) {
4931c50dc83SDarrick J. Wong 		/*
4943dff5721SDan Williams 		 * Find the sas_task and kill it.  By this point, libata
4953dff5721SDan Williams 		 * has decided to kill the qc and has frozen the port.
4963dff5721SDan Williams 		 * In this state sas_ata_task_done() will no longer free
4973dff5721SDan Williams 		 * the sas_task, so we need to notify the lldd (via
4983dff5721SDan Williams 		 * ->lldd_abort_task) that the task is dead and free it
4993dff5721SDan Williams 		 *  ourselves.
5001c50dc83SDarrick J. Wong 		 */
5011c50dc83SDarrick J. Wong 		struct sas_task *task = qc->lldd_task;
5021c50dc83SDarrick J. Wong 
5031c50dc83SDarrick J. Wong 		qc->lldd_task = NULL;
5043a2cdf39SDan Williams 		if (!task)
5053a2cdf39SDan Williams 			return;
5061c50dc83SDarrick J. Wong 		task->uldd_task = NULL;
5073dff5721SDan Williams 		sas_ata_internal_abort(task);
5081c50dc83SDarrick J. Wong 	}
5091c50dc83SDarrick J. Wong }
510338ec570SDarrick J. Wong 
511b91bb296SDan Williams 
512b91bb296SDan Williams static void sas_ata_set_dmamode(struct ata_port *ap, struct ata_device *ata_dev)
513b91bb296SDan Williams {
514b91bb296SDan Williams 	struct domain_device *dev = ap->private_data;
51536a39947SDan Williams 	struct sas_internal *i = dev_to_sas_internal(dev);
516b91bb296SDan Williams 
517b91bb296SDan Williams 	if (i->dft->lldd_ata_set_dmamode)
518b91bb296SDan Williams 		i->dft->lldd_ata_set_dmamode(dev);
519b91bb296SDan Williams }
520b91bb296SDan Williams 
521e4a9c373SDan Williams static void sas_ata_sched_eh(struct ata_port *ap)
522e4a9c373SDan Williams {
523e4a9c373SDan Williams 	struct domain_device *dev = ap->private_data;
524e4a9c373SDan Williams 	struct sas_ha_struct *ha = dev->port->ha;
525e4a9c373SDan Williams 	unsigned long flags;
526e4a9c373SDan Williams 
527e4a9c373SDan Williams 	spin_lock_irqsave(&ha->lock, flags);
528e4a9c373SDan Williams 	if (!test_and_set_bit(SAS_DEV_EH_PENDING, &dev->state))
529e4a9c373SDan Williams 		ha->eh_active++;
530e4a9c373SDan Williams 	ata_std_sched_eh(ap);
531e4a9c373SDan Williams 	spin_unlock_irqrestore(&ha->lock, flags);
532e4a9c373SDan Williams }
533e4a9c373SDan Williams 
534e4a9c373SDan Williams void sas_ata_end_eh(struct ata_port *ap)
535e4a9c373SDan Williams {
536e4a9c373SDan Williams 	struct domain_device *dev = ap->private_data;
537e4a9c373SDan Williams 	struct sas_ha_struct *ha = dev->port->ha;
538e4a9c373SDan Williams 	unsigned long flags;
539e4a9c373SDan Williams 
540e4a9c373SDan Williams 	spin_lock_irqsave(&ha->lock, flags);
541e4a9c373SDan Williams 	if (test_and_clear_bit(SAS_DEV_EH_PENDING, &dev->state))
542e4a9c373SDan Williams 		ha->eh_active--;
543e4a9c373SDan Williams 	spin_unlock_irqrestore(&ha->lock, flags);
544e4a9c373SDan Williams }
545e4a9c373SDan Williams 
54638653379SLuo Jiaxing static int sas_ata_prereset(struct ata_link *link, unsigned long deadline)
54738653379SLuo Jiaxing {
54838653379SLuo Jiaxing 	struct ata_port *ap = link->ap;
54938653379SLuo Jiaxing 	struct domain_device *dev = ap->private_data;
55038653379SLuo Jiaxing 	struct sas_phy *local_phy = sas_get_local_phy(dev);
55138653379SLuo Jiaxing 	int res = 0;
55238653379SLuo Jiaxing 
55338653379SLuo Jiaxing 	if (!local_phy->enabled || test_bit(SAS_DEV_GONE, &dev->state))
55438653379SLuo Jiaxing 		res = -ENOENT;
55538653379SLuo Jiaxing 	sas_put_local_phy(local_phy);
55638653379SLuo Jiaxing 
55738653379SLuo Jiaxing 	return res;
55838653379SLuo Jiaxing }
55938653379SLuo Jiaxing 
560338ec570SDarrick J. Wong static struct ata_port_operations sas_sata_ops = {
56138653379SLuo Jiaxing 	.prereset		= sas_ata_prereset,
56200dd4998SJames Bottomley 	.hardreset		= sas_ata_hard_reset,
56300dd4998SJames Bottomley 	.error_handler		= ata_std_error_handler,
564338ec570SDarrick J. Wong 	.post_internal_cmd	= sas_ata_post_internal,
565f0ad30d3SDavid Milburn 	.qc_defer               = ata_std_qc_defer,
566338ec570SDarrick J. Wong 	.qc_prep		= ata_noop_qc_prep,
567338ec570SDarrick J. Wong 	.qc_issue		= sas_ata_qc_issue,
5684c9bf4e7STejun Heo 	.qc_fill_rtf		= sas_ata_qc_fill_rtf,
569338ec570SDarrick J. Wong 	.port_start		= ata_sas_port_start,
570338ec570SDarrick J. Wong 	.port_stop		= ata_sas_port_stop,
571b91bb296SDan Williams 	.set_dmamode		= sas_ata_set_dmamode,
572e4a9c373SDan Williams 	.sched_eh		= sas_ata_sched_eh,
573e4a9c373SDan Williams 	.end_eh			= sas_ata_end_eh,
574338ec570SDarrick J. Wong };
575338ec570SDarrick J. Wong 
576338ec570SDarrick J. Wong static struct ata_port_info sata_port_info = {
5775067c046SShaohua Li 	.flags = ATA_FLAG_SATA | ATA_FLAG_PIO_DMA | ATA_FLAG_NCQ |
578ef026b18SHannes Reinecke 		 ATA_FLAG_SAS_HOST | ATA_FLAG_FPDMA_AUX,
5790f2e0330SSergei Shtylyov 	.pio_mask = ATA_PIO4,
5800f2e0330SSergei Shtylyov 	.mwdma_mask = ATA_MWDMA2,
581338ec570SDarrick J. Wong 	.udma_mask = ATA_UDMA6,
582338ec570SDarrick J. Wong 	.port_ops = &sas_sata_ops
583338ec570SDarrick J. Wong };
584338ec570SDarrick J. Wong 
585b2024459SDan Williams int sas_ata_init(struct domain_device *found_dev)
586338ec570SDarrick J. Wong {
5879508a66fSDan Williams 	struct sas_ha_struct *ha = found_dev->port->ha;
5889508a66fSDan Williams 	struct Scsi_Host *shost = ha->core.shost;
5892fa4a326SJason Yan 	struct ata_host *ata_host;
590338ec570SDarrick J. Wong 	struct ata_port *ap;
591b2024459SDan Williams 	int rc;
592338ec570SDarrick J. Wong 
5932fa4a326SJason Yan 	ata_host = kzalloc(sizeof(*ata_host), GFP_KERNEL);
5942fa4a326SJason Yan 	if (!ata_host)	{
59515ba7806SJohn Garry 		pr_err("ata host alloc failed.\n");
5962fa4a326SJason Yan 		return -ENOMEM;
5972fa4a326SJason Yan 	}
5982fa4a326SJason Yan 
5992fa4a326SJason Yan 	ata_host_init(ata_host, ha->dev, &sas_sata_ops);
6002fa4a326SJason Yan 
6012fa4a326SJason Yan 	ap = ata_sas_port_alloc(ata_host, &sata_port_info, shost);
602338ec570SDarrick J. Wong 	if (!ap) {
60315ba7806SJohn Garry 		pr_err("ata_sas_port_alloc failed.\n");
6042fa4a326SJason Yan 		rc = -ENODEV;
6052fa4a326SJason Yan 		goto free_host;
606338ec570SDarrick J. Wong 	}
607338ec570SDarrick J. Wong 
608338ec570SDarrick J. Wong 	ap->private_data = found_dev;
609338ec570SDarrick J. Wong 	ap->cbl = ATA_CBL_SATA;
610338ec570SDarrick J. Wong 	ap->scsi_host = shost;
611b2024459SDan Williams 	rc = ata_sas_port_init(ap);
6122fa4a326SJason Yan 	if (rc)
6132fa4a326SJason Yan 		goto destroy_port;
6142fa4a326SJason Yan 
6152fa4a326SJason Yan 	rc = ata_sas_tport_add(ata_host->dev, ap);
6162fa4a326SJason Yan 	if (rc)
6172fa4a326SJason Yan 		goto destroy_port;
6182fa4a326SJason Yan 
6192fa4a326SJason Yan 	found_dev->sata_dev.ata_host = ata_host;
620338ec570SDarrick J. Wong 	found_dev->sata_dev.ap = ap;
621338ec570SDarrick J. Wong 
622338ec570SDarrick J. Wong 	return 0;
6232fa4a326SJason Yan 
6242fa4a326SJason Yan destroy_port:
6252fa4a326SJason Yan 	ata_sas_port_destroy(ap);
6262fa4a326SJason Yan free_host:
6272fa4a326SJason Yan 	ata_host_put(ata_host);
6282fa4a326SJason Yan 	return rc;
629338ec570SDarrick J. Wong }
6303a2755afSDarrick J. Wong 
6313a2755afSDarrick J. Wong void sas_ata_task_abort(struct sas_task *task)
6323a2755afSDarrick J. Wong {
6333a2755afSDarrick J. Wong 	struct ata_queued_cmd *qc = task->uldd_task;
6343a2755afSDarrick J. Wong 	struct completion *waiting;
6353a2755afSDarrick J. Wong 
6363a2755afSDarrick J. Wong 	/* Bounce SCSI-initiated commands to the SCSI EH */
6373a2755afSDarrick J. Wong 	if (qc->scsicmd) {
638cad1a780SBart Van Assche 		blk_abort_request(scsi_cmd_to_rq(qc->scsicmd));
6393a2755afSDarrick J. Wong 		return;
6403a2755afSDarrick J. Wong 	}
6413a2755afSDarrick J. Wong 
6423a2755afSDarrick J. Wong 	/* Internal command, fake a timeout and complete. */
6433a2755afSDarrick J. Wong 	qc->flags &= ~ATA_QCFLAG_ACTIVE;
6443a2755afSDarrick J. Wong 	qc->flags |= ATA_QCFLAG_FAILED;
6453a2755afSDarrick J. Wong 	qc->err_mask |= AC_ERR_TIMEOUT;
6463a2755afSDarrick J. Wong 	waiting = qc->private_data;
6473a2755afSDarrick J. Wong 	complete(waiting);
6483a2755afSDarrick J. Wong }
649b9142174SJames Bottomley 
6509508a66fSDan Williams void sas_probe_sata(struct asd_sas_port *port)
6519508a66fSDan Williams {
6529508a66fSDan Williams 	struct domain_device *dev, *n;
6539508a66fSDan Williams 
6549508a66fSDan Williams 	mutex_lock(&port->ha->disco_mutex);
655b2024459SDan Williams 	list_for_each_entry(dev, &port->disco_list, disco_list_node) {
6569508a66fSDan Williams 		if (!dev_is_sata(dev))
6579508a66fSDan Williams 			continue;
6589508a66fSDan Williams 
659b2024459SDan Williams 		ata_sas_async_probe(dev->sata_dev.ap);
6609508a66fSDan Williams 	}
6619508a66fSDan Williams 	mutex_unlock(&port->ha->disco_mutex);
6629508a66fSDan Williams 
6639508a66fSDan Williams 	list_for_each_entry_safe(dev, n, &port->disco_list, disco_list_node) {
6649508a66fSDan Williams 		if (!dev_is_sata(dev))
6659508a66fSDan Williams 			continue;
6669508a66fSDan Williams 
6679508a66fSDan Williams 		sas_ata_wait_eh(dev);
6689508a66fSDan Williams 
6699508a66fSDan Williams 		/* if libata could not bring the link up, don't surface
6709508a66fSDan Williams 		 * the device
6719508a66fSDan Williams 		 */
672437207d3SJason Yan 		if (!ata_dev_enabled(sas_to_ata_dev(dev)))
6739508a66fSDan Williams 			sas_fail_probe(dev, __func__, -ENODEV);
6749508a66fSDan Williams 	}
675303694eeSDan Williams 
676303694eeSDan Williams }
677303694eeSDan Williams 
678bc6e7c4bSDan Williams static void sas_ata_flush_pm_eh(struct asd_sas_port *port, const char *func)
679303694eeSDan Williams {
680303694eeSDan Williams 	struct domain_device *dev, *n;
681303694eeSDan Williams 
682303694eeSDan Williams 	list_for_each_entry_safe(dev, n, &port->dev_list, dev_list_node) {
683303694eeSDan Williams 		if (!dev_is_sata(dev))
684303694eeSDan Williams 			continue;
685303694eeSDan Williams 
686303694eeSDan Williams 		sas_ata_wait_eh(dev);
687303694eeSDan Williams 
688303694eeSDan Williams 		/* if libata failed to power manage the device, tear it down */
689303694eeSDan Williams 		if (ata_dev_disabled(sas_to_ata_dev(dev)))
690303694eeSDan Williams 			sas_fail_probe(dev, func, -ENODEV);
691303694eeSDan Williams 	}
692303694eeSDan Williams }
693303694eeSDan Williams 
694303694eeSDan Williams void sas_suspend_sata(struct asd_sas_port *port)
695303694eeSDan Williams {
696303694eeSDan Williams 	struct domain_device *dev;
697303694eeSDan Williams 
698303694eeSDan Williams 	mutex_lock(&port->ha->disco_mutex);
699303694eeSDan Williams 	list_for_each_entry(dev, &port->dev_list, dev_list_node) {
700303694eeSDan Williams 		struct sata_device *sata;
701303694eeSDan Williams 
702303694eeSDan Williams 		if (!dev_is_sata(dev))
703303694eeSDan Williams 			continue;
704303694eeSDan Williams 
705303694eeSDan Williams 		sata = &dev->sata_dev;
706303694eeSDan Williams 		if (sata->ap->pm_mesg.event == PM_EVENT_SUSPEND)
707303694eeSDan Williams 			continue;
708303694eeSDan Williams 
709bc6e7c4bSDan Williams 		ata_sas_port_suspend(sata->ap);
710303694eeSDan Williams 	}
711303694eeSDan Williams 	mutex_unlock(&port->ha->disco_mutex);
712303694eeSDan Williams 
713bc6e7c4bSDan Williams 	sas_ata_flush_pm_eh(port, __func__);
714303694eeSDan Williams }
715303694eeSDan Williams 
716303694eeSDan Williams void sas_resume_sata(struct asd_sas_port *port)
717303694eeSDan Williams {
718303694eeSDan Williams 	struct domain_device *dev;
719303694eeSDan Williams 
720303694eeSDan Williams 	mutex_lock(&port->ha->disco_mutex);
721303694eeSDan Williams 	list_for_each_entry(dev, &port->dev_list, dev_list_node) {
722303694eeSDan Williams 		struct sata_device *sata;
723303694eeSDan Williams 
724303694eeSDan Williams 		if (!dev_is_sata(dev))
725303694eeSDan Williams 			continue;
726303694eeSDan Williams 
727303694eeSDan Williams 		sata = &dev->sata_dev;
728303694eeSDan Williams 		if (sata->ap->pm_mesg.event == PM_EVENT_ON)
729303694eeSDan Williams 			continue;
730303694eeSDan Williams 
731bc6e7c4bSDan Williams 		ata_sas_port_resume(sata->ap);
732303694eeSDan Williams 	}
733303694eeSDan Williams 	mutex_unlock(&port->ha->disco_mutex);
734303694eeSDan Williams 
735bc6e7c4bSDan Williams 	sas_ata_flush_pm_eh(port, __func__);
7369508a66fSDan Williams }
7379508a66fSDan Williams 
738b9142174SJames Bottomley /**
739121246aeSBart Van Assche  * sas_discover_sata - discover an STP/SATA domain device
740b9142174SJames Bottomley  * @dev: pointer to struct domain_device of interest
741b9142174SJames Bottomley  *
742b91bb296SDan Williams  * Devices directly attached to a HA port, have no parents.  All other
743b91bb296SDan Williams  * devices do, and should have their "parent" pointer set appropriately
744b91bb296SDan Williams  * before calling this function.
745b9142174SJames Bottomley  */
746b9142174SJames Bottomley int sas_discover_sata(struct domain_device *dev)
747b9142174SJames Bottomley {
748aa9f8328SJames Bottomley 	if (dev->dev_type == SAS_SATA_PM)
749b91bb296SDan Williams 		return -ENODEV;
750b91bb296SDan Williams 
7511cbd772dSHannes Reinecke 	dev->sata_dev.class = sas_get_ata_command_set(dev);
752b91bb296SDan Williams 	sas_fill_in_rphy(dev, dev->rphy);
75387c8331fSDan Williams 
7543d1a99e2SLiu Shixin 	return sas_notify_lldd_dev_found(dev);
755b9142174SJames Bottomley }
75600dd4998SJames Bottomley 
75750824d6cSDan Williams static void async_sas_ata_eh(void *data, async_cookie_t cookie)
75850824d6cSDan Williams {
75950824d6cSDan Williams 	struct domain_device *dev = data;
76050824d6cSDan Williams 	struct ata_port *ap = dev->sata_dev.ap;
76150824d6cSDan Williams 	struct sas_ha_struct *ha = dev->port->ha;
76250824d6cSDan Williams 
763d214d81eSDan Williams 	sas_ata_printk(KERN_DEBUG, dev, "dev error handler\n");
76450824d6cSDan Williams 	ata_scsi_port_error_handler(ha->core.shost, ap);
7658abda4d2SDan Williams 	sas_put_device(dev);
76650824d6cSDan Williams }
76750824d6cSDan Williams 
76800dd4998SJames Bottomley void sas_ata_strategy_handler(struct Scsi_Host *shost)
76900dd4998SJames Bottomley {
77087c8331fSDan Williams 	struct sas_ha_struct *sas_ha = SHOST_TO_SAS_HA(shost);
7712955b47dSDan Williams 	ASYNC_DOMAIN_EXCLUSIVE(async);
7729508a66fSDan Williams 	int i;
77387c8331fSDan Williams 
77487c8331fSDan Williams 	/* it's ok to defer revalidation events during ata eh, these
77587c8331fSDan Williams 	 * disks are in one of three states:
77687c8331fSDan Williams 	 * 1/ present for initial domain discovery, and these
77787c8331fSDan Williams 	 *    resets will cause bcn flutters
77887c8331fSDan Williams 	 * 2/ hot removed, we'll discover that after eh fails
77987c8331fSDan Williams 	 * 3/ hot added after initial discovery, lost the race, and need
78087c8331fSDan Williams 	 *    to catch the next train.
78187c8331fSDan Williams 	 */
78287c8331fSDan Williams 	sas_disable_revalidation(sas_ha);
78300dd4998SJames Bottomley 
7849508a66fSDan Williams 	spin_lock_irq(&sas_ha->phy_port_lock);
7859508a66fSDan Williams 	for (i = 0; i < sas_ha->num_phys; i++) {
7869508a66fSDan Williams 		struct asd_sas_port *port = sas_ha->sas_port[i];
7879508a66fSDan Williams 		struct domain_device *dev;
78800dd4998SJames Bottomley 
7899508a66fSDan Williams 		spin_lock(&port->dev_list_lock);
7909508a66fSDan Williams 		list_for_each_entry(dev, &port->dev_list, dev_list_node) {
791b2024459SDan Williams 			if (!dev_is_sata(dev))
79200dd4998SJames Bottomley 				continue;
793e4a9c373SDan Williams 
794e4a9c373SDan Williams 			/* hold a reference over eh since we may be
795e4a9c373SDan Williams 			 * racing with final remove once all commands
796e4a9c373SDan Williams 			 * are completed
797e4a9c373SDan Williams 			 */
798e4a9c373SDan Williams 			kref_get(&dev->kref);
799e4a9c373SDan Williams 
8009508a66fSDan Williams 			async_schedule_domain(async_sas_ata_eh, dev, &async);
80100dd4998SJames Bottomley 		}
8029508a66fSDan Williams 		spin_unlock(&port->dev_list_lock);
8039508a66fSDan Williams 	}
8049508a66fSDan Williams 	spin_unlock_irq(&sas_ha->phy_port_lock);
8059508a66fSDan Williams 
80650824d6cSDan Williams 	async_synchronize_full_domain(&async);
80787c8331fSDan Williams 
80887c8331fSDan Williams 	sas_enable_revalidation(sas_ha);
80900dd4998SJames Bottomley }
81000dd4998SJames Bottomley 
8113a20e642SXiang Chen void sas_ata_eh(struct Scsi_Host *shost, struct list_head *work_q)
81200dd4998SJames Bottomley {
81300dd4998SJames Bottomley 	struct scsi_cmnd *cmd, *n;
814d214d81eSDan Williams 	struct domain_device *eh_dev;
81500dd4998SJames Bottomley 
81600dd4998SJames Bottomley 	do {
81700dd4998SJames Bottomley 		LIST_HEAD(sata_q);
818d214d81eSDan Williams 		eh_dev = NULL;
81900dd4998SJames Bottomley 
82000dd4998SJames Bottomley 		list_for_each_entry_safe(cmd, n, work_q, eh_entry) {
82100dd4998SJames Bottomley 			struct domain_device *ddev = cmd_to_domain_dev(cmd);
82200dd4998SJames Bottomley 
82300dd4998SJames Bottomley 			if (!dev_is_sata(ddev) || TO_SAS_TASK(cmd))
82400dd4998SJames Bottomley 				continue;
825d214d81eSDan Williams 			if (eh_dev && eh_dev != ddev)
82600dd4998SJames Bottomley 				continue;
827d214d81eSDan Williams 			eh_dev = ddev;
82800dd4998SJames Bottomley 			list_move(&cmd->eh_entry, &sata_q);
82900dd4998SJames Bottomley 		}
83000dd4998SJames Bottomley 
83100dd4998SJames Bottomley 		if (!list_empty(&sata_q)) {
832d214d81eSDan Williams 			struct ata_port *ap = eh_dev->sata_dev.ap;
833d214d81eSDan Williams 
834d214d81eSDan Williams 			sas_ata_printk(KERN_DEBUG, eh_dev, "cmd error handler\n");
83500dd4998SJames Bottomley 			ata_scsi_cmd_error_handler(shost, ap, &sata_q);
836a82058a7SJames Bottomley 			/*
837a82058a7SJames Bottomley 			 * ata's error handler may leave the cmd on the list
838a82058a7SJames Bottomley 			 * so make sure they don't remain on a stack list
839a82058a7SJames Bottomley 			 * about to go out of scope.
840a82058a7SJames Bottomley 			 *
841a82058a7SJames Bottomley 			 * This looks strange, since the commands are
842a82058a7SJames Bottomley 			 * now part of no list, but the next error
843a82058a7SJames Bottomley 			 * action will be ata_port_error_handler()
844a82058a7SJames Bottomley 			 * which takes no list and sweeps them up
845a82058a7SJames Bottomley 			 * anyway from the ata tag array.
846a82058a7SJames Bottomley 			 */
847a82058a7SJames Bottomley 			while (!list_empty(&sata_q))
848a82058a7SJames Bottomley 				list_del_init(sata_q.next);
84900dd4998SJames Bottomley 		}
850d214d81eSDan Williams 	} while (eh_dev);
85100dd4998SJames Bottomley }
852b52df417SDan Williams 
853b52df417SDan Williams void sas_ata_schedule_reset(struct domain_device *dev)
854b52df417SDan Williams {
855b52df417SDan Williams 	struct ata_eh_info *ehi;
856b52df417SDan Williams 	struct ata_port *ap;
857b52df417SDan Williams 	unsigned long flags;
858b52df417SDan Williams 
859b52df417SDan Williams 	if (!dev_is_sata(dev))
860b52df417SDan Williams 		return;
861b52df417SDan Williams 
862b52df417SDan Williams 	ap = dev->sata_dev.ap;
863b52df417SDan Williams 	ehi = &ap->link.eh_info;
864b52df417SDan Williams 
865b52df417SDan Williams 	spin_lock_irqsave(ap->lock, flags);
866b52df417SDan Williams 	ehi->err_mask |= AC_ERR_TIMEOUT;
867b52df417SDan Williams 	ehi->action |= ATA_EH_RESET;
868b52df417SDan Williams 	ata_port_schedule_eh(ap);
869b52df417SDan Williams 	spin_unlock_irqrestore(ap->lock, flags);
870b52df417SDan Williams }
87143a5ab15SDan Williams EXPORT_SYMBOL_GPL(sas_ata_schedule_reset);
87281c757bcSDan Williams 
87381c757bcSDan Williams void sas_ata_wait_eh(struct domain_device *dev)
87481c757bcSDan Williams {
87581c757bcSDan Williams 	struct ata_port *ap;
87681c757bcSDan Williams 
87781c757bcSDan Williams 	if (!dev_is_sata(dev))
87881c757bcSDan Williams 		return;
87981c757bcSDan Williams 
88081c757bcSDan Williams 	ap = dev->sata_dev.ap;
88181c757bcSDan Williams 	ata_port_wait_eh(ap);
88281c757bcSDan Williams }
8833f2e252eSJohn Garry 
88444112922SJohn Garry void sas_ata_device_link_abort(struct domain_device *device, bool force_reset)
88544112922SJohn Garry {
88644112922SJohn Garry 	struct ata_port *ap = device->sata_dev.ap;
88744112922SJohn Garry 	struct ata_link *link = &ap->link;
88844112922SJohn Garry 
88944112922SJohn Garry 	device->sata_dev.fis[2] = ATA_ERR | ATA_DRDY; /* tf status */
89044112922SJohn Garry 	device->sata_dev.fis[3] = ATA_ABORTED; /* tf error */
89144112922SJohn Garry 
89244112922SJohn Garry 	link->eh_info.err_mask |= AC_ERR_DEV;
89344112922SJohn Garry 	if (force_reset)
89444112922SJohn Garry 		link->eh_info.action |= ATA_EH_RESET;
89544112922SJohn Garry 	ata_link_abort(link);
89644112922SJohn Garry }
89744112922SJohn Garry EXPORT_SYMBOL_GPL(sas_ata_device_link_abort);
89844112922SJohn Garry 
8993f2e252eSJohn Garry int sas_execute_ata_cmd(struct domain_device *device, u8 *fis, int force_phy_id)
9003f2e252eSJohn Garry {
9013f2e252eSJohn Garry 	struct sas_tmf_task tmf_task = {};
9023f2e252eSJohn Garry 	return sas_execute_tmf(device, fis, sizeof(struct host_to_dev_fis),
9033f2e252eSJohn Garry 			       force_phy_id, &tmf_task);
9043f2e252eSJohn Garry }
9053f2e252eSJohn Garry EXPORT_SYMBOL_GPL(sas_execute_ata_cmd);
906