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
sas_to_ata_err(struct task_status_struct * ts)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
sas_ata_task_done(struct sas_task * task)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() */
1047834589eSNiklas Cassel if (unlikely(ata_port_is_frozen(ap))) {
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))
12887629312SNiklas Cassel qc->flags |= ATA_QCFLAG_EH;
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;
13987629312SNiklas Cassel qc->flags |= ATA_QCFLAG_EH;
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
sas_ata_qc_issue(struct ata_queued_cmd * qc)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;
1651136a022SJohn Garry struct Scsi_Host *host = sas_ha->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;
2041c50dc83SDarrick J. Wong qc->lldd_task = task;
205338ec570SDarrick J. Wong
206b38d4d85SHannes Reinecke task->ata_task.use_ncq = ata_is_ncq(qc->tf.protocol);
207b38d4d85SHannes Reinecke task->ata_task.dma_xfer = ata_is_dma(qc->tf.protocol);
208338ec570SDarrick J. Wong
20972875018SIgor Pylypiv if (qc->flags & ATA_QCFLAG_RESULT_TF)
21072875018SIgor Pylypiv task->ata_task.return_fis_on_success = 1;
21172875018SIgor Pylypiv
212fe059f12SDarrick J. Wong if (qc->scsicmd)
213fe059f12SDarrick J. Wong ASSIGN_SAS_TASK(qc->scsicmd, task);
214fe059f12SDarrick J. Wong
21579855d17SChristoph Hellwig ret = i->dft->lldd_execute_task(task, GFP_ATOMIC);
216312d3e56SDan Williams if (ret) {
21715ba7806SJohn Garry pr_debug("lldd_execute_task returned: %d\n", ret);
218338ec570SDarrick J. Wong
219fe059f12SDarrick J. Wong if (qc->scsicmd)
220fe059f12SDarrick J. Wong ASSIGN_SAS_TASK(qc->scsicmd, NULL);
221338ec570SDarrick J. Wong sas_free_task(task);
222354a086dSWei Fang qc->lldd_task = NULL;
223312d3e56SDan Williams ret = AC_ERR_SYSTEM;
224338ec570SDarrick J. Wong }
225338ec570SDarrick J. Wong
226312d3e56SDan Williams out:
227312d3e56SDan Williams spin_lock(ap->lock);
228312d3e56SDan Williams return ret;
229338ec570SDarrick J. Wong }
230338ec570SDarrick J. Wong
sas_ata_qc_fill_rtf(struct ata_queued_cmd * qc)231931139afSDamien Le Moal static void sas_ata_qc_fill_rtf(struct ata_queued_cmd *qc)
2324c9bf4e7STejun Heo {
2334c9bf4e7STejun Heo struct domain_device *dev = qc->ap->private_data;
2344c9bf4e7STejun Heo
2356ef1b512SDan Williams ata_tf_from_fis(dev->sata_dev.fis, &qc->result_tf);
2364c9bf4e7STejun Heo }
2374c9bf4e7STejun Heo
dev_to_sas_internal(struct domain_device * dev)23836a39947SDan Williams static struct sas_internal *dev_to_sas_internal(struct domain_device *dev)
23936a39947SDan Williams {
2401136a022SJohn Garry return to_sas_internal(dev->port->ha->shost->transportt);
24136a39947SDan Williams }
24236a39947SDan Williams
sas_get_ata_command_set(struct domain_device * dev)2436c90466eSJason Yan static int sas_get_ata_command_set(struct domain_device *dev)
2446c90466eSJason Yan {
2456c90466eSJason Yan struct ata_taskfile tf;
2466c90466eSJason Yan
2476c90466eSJason Yan if (dev->dev_type == SAS_SATA_PENDING)
2486c90466eSJason Yan return ATA_DEV_UNKNOWN;
2496c90466eSJason Yan
2506c90466eSJason Yan ata_tf_from_fis(dev->frame_rcvd, &tf);
2516c90466eSJason Yan
2526c90466eSJason Yan return ata_dev_classify(&tf);
2536c90466eSJason Yan }
254354cf829SDan Williams
sas_get_ata_info(struct domain_device * dev,struct ex_phy * phy)255354cf829SDan Williams int sas_get_ata_info(struct domain_device *dev, struct ex_phy *phy)
256354cf829SDan Williams {
257354cf829SDan Williams if (phy->attached_tproto & SAS_PROTOCOL_STP)
258354cf829SDan Williams dev->tproto = phy->attached_tproto;
259354cf829SDan Williams if (phy->attached_sata_dev)
260aa9f8328SJames Bottomley dev->tproto |= SAS_SATA_DEV;
261354cf829SDan Williams
262aa9f8328SJames Bottomley if (phy->attached_dev_type == SAS_SATA_PENDING)
263aa9f8328SJames Bottomley dev->dev_type = SAS_SATA_PENDING;
264354cf829SDan Williams else {
265354cf829SDan Williams int res;
266354cf829SDan Williams
267aa9f8328SJames Bottomley dev->dev_type = SAS_SATA_DEV;
268354cf829SDan Williams res = sas_get_report_phy_sata(dev->parent, phy->phy_id,
269354cf829SDan Williams &dev->sata_dev.rps_resp);
270354cf829SDan Williams if (res) {
2713c236f8cSJohn Garry pr_debug("report phy sata to %016llx:%02d returned 0x%x\n",
27215ba7806SJohn Garry SAS_ADDR(dev->parent->sas_addr),
273354cf829SDan Williams phy->phy_id, res);
274354cf829SDan Williams return res;
275354cf829SDan Williams }
276354cf829SDan Williams memcpy(dev->frame_rcvd, &dev->sata_dev.rps_resp.rps.fis,
277354cf829SDan Williams sizeof(struct dev_to_host_fis));
2781cbd772dSHannes Reinecke dev->sata_dev.class = sas_get_ata_command_set(dev);
279354cf829SDan Williams }
280354cf829SDan Williams return 0;
281354cf829SDan Williams }
282354cf829SDan Williams
sas_ata_clear_pending(struct domain_device * dev,struct ex_phy * phy)283354cf829SDan Williams static int sas_ata_clear_pending(struct domain_device *dev, struct ex_phy *phy)
284354cf829SDan Williams {
285354cf829SDan Williams int res;
286354cf829SDan Williams
287354cf829SDan Williams /* we weren't pending, so successfully end the reset sequence now */
288aa9f8328SJames Bottomley if (dev->dev_type != SAS_SATA_PENDING)
289354cf829SDan Williams return 1;
290354cf829SDan Williams
291354cf829SDan Williams /* hmmm, if this succeeds do we need to repost the domain_device to the
292354cf829SDan Williams * lldd so it can pick up new parameters?
293354cf829SDan Williams */
294354cf829SDan Williams res = sas_get_ata_info(dev, phy);
295354cf829SDan Williams if (res)
296354cf829SDan Williams return 0; /* retry */
297354cf829SDan Williams else
298354cf829SDan Williams return 1;
299354cf829SDan Williams }
300354cf829SDan Williams
smp_ata_check_ready_type(struct ata_link * link)3019181ce3cSJie Zhan int smp_ata_check_ready_type(struct ata_link *link)
3029181ce3cSJie Zhan {
3039181ce3cSJie Zhan struct domain_device *dev = link->ap->private_data;
3049181ce3cSJie Zhan struct sas_phy *phy = sas_get_local_phy(dev);
3059181ce3cSJie Zhan struct domain_device *ex_dev = dev->parent;
3069181ce3cSJie Zhan enum sas_device_type type = SAS_PHY_UNUSED;
3079181ce3cSJie Zhan u8 sas_addr[SAS_ADDR_SIZE];
3089181ce3cSJie Zhan int res;
3099181ce3cSJie Zhan
3109181ce3cSJie Zhan res = sas_get_phy_attached_dev(ex_dev, phy->number, sas_addr, &type);
3119181ce3cSJie Zhan sas_put_local_phy(phy);
3129181ce3cSJie Zhan if (res)
3139181ce3cSJie Zhan return res;
3149181ce3cSJie Zhan
3159181ce3cSJie Zhan switch (type) {
3169181ce3cSJie Zhan case SAS_SATA_PENDING:
3179181ce3cSJie Zhan return 0;
3189181ce3cSJie Zhan case SAS_END_DEVICE:
3199181ce3cSJie Zhan return 1;
3209181ce3cSJie Zhan default:
3219181ce3cSJie Zhan return -ENODEV;
3229181ce3cSJie Zhan }
3239181ce3cSJie Zhan }
3249181ce3cSJie Zhan EXPORT_SYMBOL_GPL(smp_ata_check_ready_type);
3259181ce3cSJie Zhan
smp_ata_check_ready(struct ata_link * link)32636a39947SDan Williams static int smp_ata_check_ready(struct ata_link *link)
32736a39947SDan Williams {
32836a39947SDan Williams int res;
32936a39947SDan Williams struct ata_port *ap = link->ap;
33036a39947SDan Williams struct domain_device *dev = ap->private_data;
33136a39947SDan Williams struct domain_device *ex_dev = dev->parent;
332f41a0c44SDan Williams struct sas_phy *phy = sas_get_local_phy(dev);
333354cf829SDan Williams struct ex_phy *ex_phy = &ex_dev->ex_dev.ex_phy[phy->number];
33436a39947SDan Williams
335354cf829SDan Williams res = sas_ex_phy_discover(ex_dev, phy->number);
336f41a0c44SDan Williams sas_put_local_phy(phy);
337354cf829SDan Williams
33836a39947SDan Williams /* break the wait early if the expander is unreachable,
33936a39947SDan Williams * otherwise keep polling
34036a39947SDan Williams */
34136a39947SDan Williams if (res == -ECOMM)
34236a39947SDan Williams return res;
343354cf829SDan Williams if (res != SMP_RESP_FUNC_ACC)
34436a39947SDan Williams return 0;
345354cf829SDan Williams
346354cf829SDan Williams switch (ex_phy->attached_dev_type) {
347aa9f8328SJames Bottomley case SAS_SATA_PENDING:
348354cf829SDan Williams return 0;
349aa9f8328SJames Bottomley case SAS_END_DEVICE:
350354cf829SDan Williams if (ex_phy->attached_sata_dev)
351354cf829SDan Williams return sas_ata_clear_pending(dev, ex_phy);
352df561f66SGustavo A. R. Silva fallthrough;
353354cf829SDan Williams default:
354354cf829SDan Williams return -ENODEV;
355354cf829SDan Williams }
35636a39947SDan Williams }
35736a39947SDan Williams
local_ata_check_ready(struct ata_link * link)35836a39947SDan Williams static int local_ata_check_ready(struct ata_link *link)
359338ec570SDarrick J. Wong {
36000dd4998SJames Bottomley struct ata_port *ap = link->ap;
361338ec570SDarrick J. Wong struct domain_device *dev = ap->private_data;
36236a39947SDan Williams struct sas_internal *i = dev_to_sas_internal(dev);
363338ec570SDarrick J. Wong
36436a39947SDan Williams if (i->dft->lldd_ata_check_ready)
36536a39947SDan Williams return i->dft->lldd_ata_check_ready(dev);
36636a39947SDan Williams else {
36736a39947SDan Williams /* lldd's that don't implement 'ready' checking get the
36836a39947SDan Williams * old default behavior of not coordinating reset
36936a39947SDan Williams * recovery with libata
37036a39947SDan Williams */
37136a39947SDan Williams return 1;
37236a39947SDan Williams }
37300dd4998SJames Bottomley }
374338ec570SDarrick J. Wong
sas_ata_printk(const char * level,const struct domain_device * ddev,const char * fmt,...)375d214d81eSDan Williams static int sas_ata_printk(const char *level, const struct domain_device *ddev,
376d214d81eSDan Williams const char *fmt, ...)
377d214d81eSDan Williams {
378d214d81eSDan Williams struct ata_port *ap = ddev->sata_dev.ap;
379d214d81eSDan Williams struct device *dev = &ddev->rphy->dev;
380d214d81eSDan Williams struct va_format vaf;
381d214d81eSDan Williams va_list args;
382d214d81eSDan Williams int r;
383d214d81eSDan Williams
384d214d81eSDan Williams va_start(args, fmt);
385d214d81eSDan Williams
386d214d81eSDan Williams vaf.fmt = fmt;
387d214d81eSDan Williams vaf.va = &args;
388d214d81eSDan Williams
389d188e5dbSJohn Garry r = printk("%s" SAS_FMT "ata%u: %s: %pV",
390d214d81eSDan Williams level, ap->print_id, dev_name(dev), &vaf);
391d214d81eSDan Williams
392d214d81eSDan Williams va_end(args);
393d214d81eSDan Williams
394d214d81eSDan Williams return r;
395d214d81eSDan Williams }
396d214d81eSDan Williams
sas_ata_wait_after_reset(struct domain_device * dev,unsigned long deadline)3974d450cf2SJie Zhan static int sas_ata_wait_after_reset(struct domain_device *dev, unsigned long deadline)
39836a39947SDan Williams {
399057e5fc0SJohn Garry struct sata_device *sata_dev = &dev->sata_dev;
40036a39947SDan Williams int (*check_ready)(struct ata_link *link);
401057e5fc0SJohn Garry struct ata_port *ap = sata_dev->ap;
402057e5fc0SJohn Garry struct ata_link *link = &ap->link;
403057e5fc0SJohn Garry struct sas_phy *phy;
404057e5fc0SJohn Garry int ret;
40536a39947SDan Williams
406f41a0c44SDan Williams phy = sas_get_local_phy(dev);
40736a39947SDan Williams if (scsi_is_sas_phy_local(phy))
40836a39947SDan Williams check_ready = local_ata_check_ready;
40936a39947SDan Williams else
41036a39947SDan Williams check_ready = smp_ata_check_ready;
411f41a0c44SDan Williams sas_put_local_phy(phy);
41236a39947SDan Williams
41336a39947SDan Williams ret = ata_wait_after_reset(link, deadline, check_ready);
41436a39947SDan Williams if (ret && ret != -EAGAIN)
415d214d81eSDan Williams sas_ata_printk(KERN_ERR, dev, "reset failed (errno=%d)\n", ret);
41636a39947SDan Williams
417057e5fc0SJohn Garry return ret;
418057e5fc0SJohn Garry }
419057e5fc0SJohn Garry
sas_ata_hard_reset(struct ata_link * link,unsigned int * class,unsigned long deadline)420057e5fc0SJohn Garry static int sas_ata_hard_reset(struct ata_link *link, unsigned int *class,
421057e5fc0SJohn Garry unsigned long deadline)
422057e5fc0SJohn Garry {
423057e5fc0SJohn Garry struct ata_port *ap = link->ap;
424057e5fc0SJohn Garry struct domain_device *dev = ap->private_data;
425057e5fc0SJohn Garry struct sas_internal *i = dev_to_sas_internal(dev);
426057e5fc0SJohn Garry int ret;
427057e5fc0SJohn Garry
428057e5fc0SJohn Garry ret = i->dft->lldd_I_T_nexus_reset(dev);
429057e5fc0SJohn Garry if (ret == -ENODEV)
430057e5fc0SJohn Garry return ret;
431057e5fc0SJohn Garry
432057e5fc0SJohn Garry if (ret != TMF_RESP_FUNC_COMPLETE)
433057e5fc0SJohn Garry sas_ata_printk(KERN_DEBUG, dev, "Unable to reset ata device?\n");
434057e5fc0SJohn Garry
435057e5fc0SJohn Garry ret = sas_ata_wait_after_reset(dev, deadline);
436057e5fc0SJohn Garry
4371cbd772dSHannes Reinecke *class = dev->sata_dev.class;
438338ec570SDarrick J. Wong
439338ec570SDarrick J. Wong ap->cbl = ATA_CBL_SATA;
44000dd4998SJames Bottomley return ret;
441338ec570SDarrick J. Wong }
442338ec570SDarrick J. Wong
4433dff5721SDan Williams /*
4443dff5721SDan Williams * notify the lldd to forget the sas_task for this internal ata command
4453dff5721SDan Williams * that bypasses scsi-eh
4463dff5721SDan Williams */
sas_ata_internal_abort(struct sas_task * task)4473dff5721SDan Williams static void sas_ata_internal_abort(struct sas_task *task)
4483dff5721SDan Williams {
44936a39947SDan Williams struct sas_internal *si = dev_to_sas_internal(task->dev);
4503dff5721SDan Williams unsigned long flags;
4513dff5721SDan Williams int res;
4523dff5721SDan Williams
4533dff5721SDan Williams spin_lock_irqsave(&task->task_state_lock, flags);
4543dff5721SDan Williams if (task->task_state_flags & SAS_TASK_STATE_ABORTED ||
4553dff5721SDan Williams task->task_state_flags & SAS_TASK_STATE_DONE) {
4563dff5721SDan Williams spin_unlock_irqrestore(&task->task_state_lock, flags);
45715ba7806SJohn Garry pr_debug("%s: Task %p already finished.\n", __func__, task);
4583dff5721SDan Williams goto out;
4593dff5721SDan Williams }
4603dff5721SDan Williams task->task_state_flags |= SAS_TASK_STATE_ABORTED;
4613dff5721SDan Williams spin_unlock_irqrestore(&task->task_state_lock, flags);
4623dff5721SDan Williams
4633dff5721SDan Williams res = si->dft->lldd_abort_task(task);
4643dff5721SDan Williams
4653dff5721SDan Williams spin_lock_irqsave(&task->task_state_lock, flags);
4663dff5721SDan Williams if (task->task_state_flags & SAS_TASK_STATE_DONE ||
4673dff5721SDan Williams res == TMF_RESP_FUNC_COMPLETE) {
4683dff5721SDan Williams spin_unlock_irqrestore(&task->task_state_lock, flags);
4693dff5721SDan Williams goto out;
4703dff5721SDan Williams }
4713dff5721SDan Williams
4723dff5721SDan Williams /* XXX we are not prepared to deal with ->lldd_abort_task()
4733dff5721SDan Williams * failures. TODO: lldds need to unconditionally forget about
4743dff5721SDan Williams * aborted ata tasks, otherwise we (likely) leak the sas task
4753dff5721SDan Williams * here
4763dff5721SDan Williams */
47715ba7806SJohn Garry pr_warn("%s: Task %p leaked.\n", __func__, task);
4783dff5721SDan Williams
4793dff5721SDan Williams if (!(task->task_state_flags & SAS_TASK_STATE_DONE))
4803dff5721SDan Williams task->task_state_flags &= ~SAS_TASK_STATE_ABORTED;
4813dff5721SDan Williams spin_unlock_irqrestore(&task->task_state_lock, flags);
4823dff5721SDan Williams
4833dff5721SDan Williams return;
4843dff5721SDan Williams out:
4853dff5721SDan Williams sas_free_task(task);
4863dff5721SDan Williams }
4873dff5721SDan Williams
sas_ata_post_internal(struct ata_queued_cmd * qc)488338ec570SDarrick J. Wong static void sas_ata_post_internal(struct ata_queued_cmd *qc)
489338ec570SDarrick J. Wong {
49087629312SNiklas Cassel if (qc->flags & ATA_QCFLAG_EH)
491338ec570SDarrick J. Wong qc->err_mask |= AC_ERR_OTHER;
492338ec570SDarrick J. Wong
4931c50dc83SDarrick J. Wong if (qc->err_mask) {
4941c50dc83SDarrick J. Wong /*
4953dff5721SDan Williams * Find the sas_task and kill it. By this point, libata
4963dff5721SDan Williams * has decided to kill the qc and has frozen the port.
4973dff5721SDan Williams * In this state sas_ata_task_done() will no longer free
4983dff5721SDan Williams * the sas_task, so we need to notify the lldd (via
4993dff5721SDan Williams * ->lldd_abort_task) that the task is dead and free it
5003dff5721SDan Williams * ourselves.
5011c50dc83SDarrick J. Wong */
5021c50dc83SDarrick J. Wong struct sas_task *task = qc->lldd_task;
5031c50dc83SDarrick J. Wong
5041c50dc83SDarrick J. Wong qc->lldd_task = NULL;
5053a2cdf39SDan Williams if (!task)
5063a2cdf39SDan Williams return;
5071c50dc83SDarrick J. Wong task->uldd_task = NULL;
5083dff5721SDan Williams sas_ata_internal_abort(task);
5091c50dc83SDarrick J. Wong }
5101c50dc83SDarrick J. Wong }
511338ec570SDarrick J. Wong
512b91bb296SDan Williams
sas_ata_set_dmamode(struct ata_port * ap,struct ata_device * ata_dev)513b91bb296SDan Williams static void sas_ata_set_dmamode(struct ata_port *ap, struct ata_device *ata_dev)
514b91bb296SDan Williams {
515b91bb296SDan Williams struct domain_device *dev = ap->private_data;
51636a39947SDan Williams struct sas_internal *i = dev_to_sas_internal(dev);
517b91bb296SDan Williams
518b91bb296SDan Williams if (i->dft->lldd_ata_set_dmamode)
519b91bb296SDan Williams i->dft->lldd_ata_set_dmamode(dev);
520b91bb296SDan Williams }
521b91bb296SDan Williams
sas_ata_sched_eh(struct ata_port * ap)522e4a9c373SDan Williams static void sas_ata_sched_eh(struct ata_port *ap)
523e4a9c373SDan Williams {
524e4a9c373SDan Williams struct domain_device *dev = ap->private_data;
525e4a9c373SDan Williams struct sas_ha_struct *ha = dev->port->ha;
526e4a9c373SDan Williams unsigned long flags;
527e4a9c373SDan Williams
528e4a9c373SDan Williams spin_lock_irqsave(&ha->lock, flags);
529e4a9c373SDan Williams if (!test_and_set_bit(SAS_DEV_EH_PENDING, &dev->state))
530e4a9c373SDan Williams ha->eh_active++;
531e4a9c373SDan Williams ata_std_sched_eh(ap);
532e4a9c373SDan Williams spin_unlock_irqrestore(&ha->lock, flags);
533e4a9c373SDan Williams }
534e4a9c373SDan Williams
sas_ata_end_eh(struct ata_port * ap)535e4a9c373SDan Williams void sas_ata_end_eh(struct ata_port *ap)
536e4a9c373SDan Williams {
537e4a9c373SDan Williams struct domain_device *dev = ap->private_data;
538e4a9c373SDan Williams struct sas_ha_struct *ha = dev->port->ha;
539e4a9c373SDan Williams unsigned long flags;
540e4a9c373SDan Williams
541e4a9c373SDan Williams spin_lock_irqsave(&ha->lock, flags);
542e4a9c373SDan Williams if (test_and_clear_bit(SAS_DEV_EH_PENDING, &dev->state))
543e4a9c373SDan Williams ha->eh_active--;
544e4a9c373SDan Williams spin_unlock_irqrestore(&ha->lock, flags);
545e4a9c373SDan Williams }
546e4a9c373SDan Williams
sas_ata_prereset(struct ata_link * link,unsigned long deadline)54738653379SLuo Jiaxing static int sas_ata_prereset(struct ata_link *link, unsigned long deadline)
54838653379SLuo Jiaxing {
54938653379SLuo Jiaxing struct ata_port *ap = link->ap;
55038653379SLuo Jiaxing struct domain_device *dev = ap->private_data;
55138653379SLuo Jiaxing struct sas_phy *local_phy = sas_get_local_phy(dev);
55238653379SLuo Jiaxing int res = 0;
55338653379SLuo Jiaxing
55438653379SLuo Jiaxing if (!local_phy->enabled || test_bit(SAS_DEV_GONE, &dev->state))
55538653379SLuo Jiaxing res = -ENOENT;
55638653379SLuo Jiaxing sas_put_local_phy(local_phy);
55738653379SLuo Jiaxing
55838653379SLuo Jiaxing return res;
55938653379SLuo Jiaxing }
56038653379SLuo Jiaxing
561338ec570SDarrick J. Wong static struct ata_port_operations sas_sata_ops = {
56238653379SLuo Jiaxing .prereset = sas_ata_prereset,
56300dd4998SJames Bottomley .hardreset = sas_ata_hard_reset,
56400dd4998SJames Bottomley .error_handler = ata_std_error_handler,
565338ec570SDarrick J. Wong .post_internal_cmd = sas_ata_post_internal,
566f0ad30d3SDavid Milburn .qc_defer = ata_std_qc_defer,
567338ec570SDarrick J. Wong .qc_prep = ata_noop_qc_prep,
568338ec570SDarrick J. Wong .qc_issue = sas_ata_qc_issue,
5694c9bf4e7STejun Heo .qc_fill_rtf = sas_ata_qc_fill_rtf,
570b91bb296SDan Williams .set_dmamode = sas_ata_set_dmamode,
571e4a9c373SDan Williams .sched_eh = sas_ata_sched_eh,
572e4a9c373SDan Williams .end_eh = sas_ata_end_eh,
573338ec570SDarrick J. Wong };
574338ec570SDarrick J. Wong
575338ec570SDarrick J. Wong static struct ata_port_info sata_port_info = {
5765067c046SShaohua Li .flags = ATA_FLAG_SATA | ATA_FLAG_PIO_DMA | ATA_FLAG_NCQ |
577ef026b18SHannes Reinecke ATA_FLAG_SAS_HOST | ATA_FLAG_FPDMA_AUX,
5780f2e0330SSergei Shtylyov .pio_mask = ATA_PIO4,
5790f2e0330SSergei Shtylyov .mwdma_mask = ATA_MWDMA2,
580338ec570SDarrick J. Wong .udma_mask = ATA_UDMA6,
581338ec570SDarrick J. Wong .port_ops = &sas_sata_ops
582338ec570SDarrick J. Wong };
583338ec570SDarrick J. Wong
sas_ata_init(struct domain_device * found_dev)584b2024459SDan Williams int sas_ata_init(struct domain_device *found_dev)
585338ec570SDarrick J. Wong {
5869508a66fSDan Williams struct sas_ha_struct *ha = found_dev->port->ha;
5871136a022SJohn Garry struct Scsi_Host *shost = ha->shost;
5882fa4a326SJason Yan struct ata_host *ata_host;
589338ec570SDarrick J. Wong struct ata_port *ap;
590b2024459SDan Williams int rc;
591338ec570SDarrick J. Wong
5922fa4a326SJason Yan ata_host = kzalloc(sizeof(*ata_host), GFP_KERNEL);
5932fa4a326SJason Yan if (!ata_host) {
59415ba7806SJohn Garry pr_err("ata host alloc failed.\n");
5952fa4a326SJason Yan return -ENOMEM;
5962fa4a326SJason Yan }
5972fa4a326SJason Yan
5982fa4a326SJason Yan ata_host_init(ata_host, ha->dev, &sas_sata_ops);
5992fa4a326SJason Yan
6002fa4a326SJason Yan ap = ata_sas_port_alloc(ata_host, &sata_port_info, shost);
601338ec570SDarrick J. Wong if (!ap) {
60215ba7806SJohn Garry pr_err("ata_sas_port_alloc failed.\n");
6032fa4a326SJason Yan rc = -ENODEV;
6042fa4a326SJason Yan goto free_host;
605338ec570SDarrick J. Wong }
606338ec570SDarrick J. Wong
607338ec570SDarrick J. Wong ap->private_data = found_dev;
608338ec570SDarrick J. Wong ap->cbl = ATA_CBL_SATA;
609338ec570SDarrick J. Wong ap->scsi_host = shost;
6102fa4a326SJason Yan
6112fa4a326SJason Yan rc = ata_sas_tport_add(ata_host->dev, ap);
6122fa4a326SJason Yan if (rc)
613*8c65da9bSNiklas Cassel goto free_port;
6142fa4a326SJason Yan
6152fa4a326SJason Yan found_dev->sata_dev.ata_host = ata_host;
616338ec570SDarrick J. Wong found_dev->sata_dev.ap = ap;
617338ec570SDarrick J. Wong
618338ec570SDarrick J. Wong return 0;
6192fa4a326SJason Yan
620*8c65da9bSNiklas Cassel free_port:
621*8c65da9bSNiklas Cassel ata_port_free(ap);
6222fa4a326SJason Yan free_host:
6232fa4a326SJason Yan ata_host_put(ata_host);
6242fa4a326SJason Yan return rc;
625338ec570SDarrick J. Wong }
6263a2755afSDarrick J. Wong
sas_ata_task_abort(struct sas_task * task)6273a2755afSDarrick J. Wong void sas_ata_task_abort(struct sas_task *task)
6283a2755afSDarrick J. Wong {
6293a2755afSDarrick J. Wong struct ata_queued_cmd *qc = task->uldd_task;
6303a2755afSDarrick J. Wong struct completion *waiting;
6313a2755afSDarrick J. Wong
6323a2755afSDarrick J. Wong /* Bounce SCSI-initiated commands to the SCSI EH */
6333a2755afSDarrick J. Wong if (qc->scsicmd) {
634cad1a780SBart Van Assche blk_abort_request(scsi_cmd_to_rq(qc->scsicmd));
6353a2755afSDarrick J. Wong return;
6363a2755afSDarrick J. Wong }
6373a2755afSDarrick J. Wong
6383a2755afSDarrick J. Wong /* Internal command, fake a timeout and complete. */
6393a2755afSDarrick J. Wong qc->flags &= ~ATA_QCFLAG_ACTIVE;
64087629312SNiklas Cassel qc->flags |= ATA_QCFLAG_EH;
6413a2755afSDarrick J. Wong qc->err_mask |= AC_ERR_TIMEOUT;
6423a2755afSDarrick J. Wong waiting = qc->private_data;
6433a2755afSDarrick J. Wong complete(waiting);
6443a2755afSDarrick J. Wong }
645b9142174SJames Bottomley
sas_probe_sata(struct asd_sas_port * port)6469508a66fSDan Williams void sas_probe_sata(struct asd_sas_port *port)
6479508a66fSDan Williams {
6489508a66fSDan Williams struct domain_device *dev, *n;
6499508a66fSDan Williams
6509508a66fSDan Williams mutex_lock(&port->ha->disco_mutex);
651b2024459SDan Williams list_for_each_entry(dev, &port->disco_list, disco_list_node) {
6529508a66fSDan Williams if (!dev_is_sata(dev))
6539508a66fSDan Williams continue;
6549508a66fSDan Williams
655a76f1b63SHannes Reinecke ata_port_probe(dev->sata_dev.ap);
6569508a66fSDan Williams }
6579508a66fSDan Williams mutex_unlock(&port->ha->disco_mutex);
6589508a66fSDan Williams
6599508a66fSDan Williams list_for_each_entry_safe(dev, n, &port->disco_list, disco_list_node) {
6609508a66fSDan Williams if (!dev_is_sata(dev))
6619508a66fSDan Williams continue;
6629508a66fSDan Williams
6639508a66fSDan Williams sas_ata_wait_eh(dev);
6649508a66fSDan Williams
6659508a66fSDan Williams /* if libata could not bring the link up, don't surface
6669508a66fSDan Williams * the device
6679508a66fSDan Williams */
668437207d3SJason Yan if (!ata_dev_enabled(sas_to_ata_dev(dev)))
6699508a66fSDan Williams sas_fail_probe(dev, __func__, -ENODEV);
6709508a66fSDan Williams }
671303694eeSDan Williams
672303694eeSDan Williams }
673303694eeSDan Williams
sas_ata_add_dev(struct domain_device * parent,struct ex_phy * phy,struct domain_device * child,int phy_id)6747cc7646bSJason Yan int sas_ata_add_dev(struct domain_device *parent, struct ex_phy *phy,
6757cc7646bSJason Yan struct domain_device *child, int phy_id)
6767cc7646bSJason Yan {
6777cc7646bSJason Yan struct sas_rphy *rphy;
6787cc7646bSJason Yan int ret;
6797cc7646bSJason Yan
6807cc7646bSJason Yan if (child->linkrate > parent->min_linkrate) {
6817cc7646bSJason Yan struct sas_phy *cphy = child->phy;
6827cc7646bSJason Yan enum sas_linkrate min_prate = cphy->minimum_linkrate,
6837cc7646bSJason Yan parent_min_lrate = parent->min_linkrate,
6847cc7646bSJason Yan min_linkrate = (min_prate > parent_min_lrate) ?
6857cc7646bSJason Yan parent_min_lrate : 0;
6867cc7646bSJason Yan struct sas_phy_linkrates rates = {
6877cc7646bSJason Yan .maximum_linkrate = parent->min_linkrate,
6887cc7646bSJason Yan .minimum_linkrate = min_linkrate,
6897cc7646bSJason Yan };
6907cc7646bSJason Yan
6917cc7646bSJason Yan pr_notice("ex %016llx phy%02d SATA device linkrate > min pathway connection rate, attempting to lower device linkrate\n",
6927cc7646bSJason Yan SAS_ADDR(child->sas_addr), phy_id);
6937cc7646bSJason Yan ret = sas_smp_phy_control(parent, phy_id,
6947cc7646bSJason Yan PHY_FUNC_LINK_RESET, &rates);
6957cc7646bSJason Yan if (ret) {
6967cc7646bSJason Yan pr_err("ex %016llx phy%02d SATA device could not set linkrate (%d)\n",
6977cc7646bSJason Yan SAS_ADDR(child->sas_addr), phy_id, ret);
6987cc7646bSJason Yan return ret;
6997cc7646bSJason Yan }
7007cc7646bSJason Yan pr_notice("ex %016llx phy%02d SATA device set linkrate successfully\n",
7017cc7646bSJason Yan SAS_ADDR(child->sas_addr), phy_id);
7027cc7646bSJason Yan child->linkrate = child->min_linkrate;
7037cc7646bSJason Yan }
7047cc7646bSJason Yan ret = sas_get_ata_info(child, phy);
7057cc7646bSJason Yan if (ret)
7067cc7646bSJason Yan return ret;
7077cc7646bSJason Yan
7087cc7646bSJason Yan sas_init_dev(child);
7097cc7646bSJason Yan ret = sas_ata_init(child);
7107cc7646bSJason Yan if (ret)
7117cc7646bSJason Yan return ret;
7127cc7646bSJason Yan
7137cc7646bSJason Yan rphy = sas_end_device_alloc(phy->port);
7147cc7646bSJason Yan if (!rphy)
7158fe66badSDan Carpenter return -ENOMEM;
7167cc7646bSJason Yan
7177cc7646bSJason Yan rphy->identify.phy_identifier = phy_id;
7187cc7646bSJason Yan child->rphy = rphy;
7197cc7646bSJason Yan get_device(&rphy->dev);
7207cc7646bSJason Yan
7217cc7646bSJason Yan list_add_tail(&child->disco_list_node, &parent->port->disco_list);
7227cc7646bSJason Yan
7237cc7646bSJason Yan ret = sas_discover_sata(child);
7247cc7646bSJason Yan if (ret) {
7257cc7646bSJason Yan pr_notice("sas_discover_sata() for device %16llx at %016llx:%02d returned 0x%x\n",
7267cc7646bSJason Yan SAS_ADDR(child->sas_addr),
7277cc7646bSJason Yan SAS_ADDR(parent->sas_addr), phy_id, ret);
7287cc7646bSJason Yan sas_rphy_free(child->rphy);
7297cc7646bSJason Yan list_del(&child->disco_list_node);
7307cc7646bSJason Yan return ret;
7317cc7646bSJason Yan }
7327cc7646bSJason Yan
7337cc7646bSJason Yan return 0;
7347cc7646bSJason Yan }
7357cc7646bSJason Yan
sas_ata_flush_pm_eh(struct asd_sas_port * port,const char * func)736bc6e7c4bSDan Williams static void sas_ata_flush_pm_eh(struct asd_sas_port *port, const char *func)
737303694eeSDan Williams {
738303694eeSDan Williams struct domain_device *dev, *n;
739303694eeSDan Williams
740303694eeSDan Williams list_for_each_entry_safe(dev, n, &port->dev_list, dev_list_node) {
741303694eeSDan Williams if (!dev_is_sata(dev))
742303694eeSDan Williams continue;
743303694eeSDan Williams
744303694eeSDan Williams sas_ata_wait_eh(dev);
745303694eeSDan Williams
746303694eeSDan Williams /* if libata failed to power manage the device, tear it down */
747303694eeSDan Williams if (ata_dev_disabled(sas_to_ata_dev(dev)))
748303694eeSDan Williams sas_fail_probe(dev, func, -ENODEV);
749303694eeSDan Williams }
750303694eeSDan Williams }
751303694eeSDan Williams
sas_suspend_sata(struct asd_sas_port * port)752303694eeSDan Williams void sas_suspend_sata(struct asd_sas_port *port)
753303694eeSDan Williams {
754303694eeSDan Williams struct domain_device *dev;
755303694eeSDan Williams
756303694eeSDan Williams mutex_lock(&port->ha->disco_mutex);
757303694eeSDan Williams list_for_each_entry(dev, &port->dev_list, dev_list_node) {
758303694eeSDan Williams struct sata_device *sata;
759303694eeSDan Williams
760303694eeSDan Williams if (!dev_is_sata(dev))
761303694eeSDan Williams continue;
762303694eeSDan Williams
763303694eeSDan Williams sata = &dev->sata_dev;
764303694eeSDan Williams if (sata->ap->pm_mesg.event == PM_EVENT_SUSPEND)
765303694eeSDan Williams continue;
766303694eeSDan Williams
767bc6e7c4bSDan Williams ata_sas_port_suspend(sata->ap);
768303694eeSDan Williams }
769303694eeSDan Williams mutex_unlock(&port->ha->disco_mutex);
770303694eeSDan Williams
771bc6e7c4bSDan Williams sas_ata_flush_pm_eh(port, __func__);
772303694eeSDan Williams }
773303694eeSDan Williams
sas_resume_sata(struct asd_sas_port * port)774303694eeSDan Williams void sas_resume_sata(struct asd_sas_port *port)
775303694eeSDan Williams {
776303694eeSDan Williams struct domain_device *dev;
777303694eeSDan Williams
778303694eeSDan Williams mutex_lock(&port->ha->disco_mutex);
779303694eeSDan Williams list_for_each_entry(dev, &port->dev_list, dev_list_node) {
780303694eeSDan Williams struct sata_device *sata;
781303694eeSDan Williams
782303694eeSDan Williams if (!dev_is_sata(dev))
783303694eeSDan Williams continue;
784303694eeSDan Williams
785303694eeSDan Williams sata = &dev->sata_dev;
786303694eeSDan Williams if (sata->ap->pm_mesg.event == PM_EVENT_ON)
787303694eeSDan Williams continue;
788303694eeSDan Williams
789bc6e7c4bSDan Williams ata_sas_port_resume(sata->ap);
790303694eeSDan Williams }
791303694eeSDan Williams mutex_unlock(&port->ha->disco_mutex);
792303694eeSDan Williams
793bc6e7c4bSDan Williams sas_ata_flush_pm_eh(port, __func__);
7949508a66fSDan Williams }
7959508a66fSDan Williams
796b9142174SJames Bottomley /**
797121246aeSBart Van Assche * sas_discover_sata - discover an STP/SATA domain device
798b9142174SJames Bottomley * @dev: pointer to struct domain_device of interest
799b9142174SJames Bottomley *
800b91bb296SDan Williams * Devices directly attached to a HA port, have no parents. All other
801b91bb296SDan Williams * devices do, and should have their "parent" pointer set appropriately
802b91bb296SDan Williams * before calling this function.
803b9142174SJames Bottomley */
sas_discover_sata(struct domain_device * dev)804b9142174SJames Bottomley int sas_discover_sata(struct domain_device *dev)
805b9142174SJames Bottomley {
806aa9f8328SJames Bottomley if (dev->dev_type == SAS_SATA_PM)
807b91bb296SDan Williams return -ENODEV;
808b91bb296SDan Williams
8091cbd772dSHannes Reinecke dev->sata_dev.class = sas_get_ata_command_set(dev);
810b91bb296SDan Williams sas_fill_in_rphy(dev, dev->rphy);
81187c8331fSDan Williams
8123d1a99e2SLiu Shixin return sas_notify_lldd_dev_found(dev);
813b9142174SJames Bottomley }
81400dd4998SJames Bottomley
async_sas_ata_eh(void * data,async_cookie_t cookie)81550824d6cSDan Williams static void async_sas_ata_eh(void *data, async_cookie_t cookie)
81650824d6cSDan Williams {
81750824d6cSDan Williams struct domain_device *dev = data;
81850824d6cSDan Williams struct ata_port *ap = dev->sata_dev.ap;
81950824d6cSDan Williams struct sas_ha_struct *ha = dev->port->ha;
82050824d6cSDan Williams
821d214d81eSDan Williams sas_ata_printk(KERN_DEBUG, dev, "dev error handler\n");
8221136a022SJohn Garry ata_scsi_port_error_handler(ha->shost, ap);
8238abda4d2SDan Williams sas_put_device(dev);
82450824d6cSDan Williams }
82550824d6cSDan Williams
sas_ata_strategy_handler(struct Scsi_Host * shost)82600dd4998SJames Bottomley void sas_ata_strategy_handler(struct Scsi_Host *shost)
82700dd4998SJames Bottomley {
82887c8331fSDan Williams struct sas_ha_struct *sas_ha = SHOST_TO_SAS_HA(shost);
8292955b47dSDan Williams ASYNC_DOMAIN_EXCLUSIVE(async);
8309508a66fSDan Williams int i;
83187c8331fSDan Williams
83287c8331fSDan Williams /* it's ok to defer revalidation events during ata eh, these
83387c8331fSDan Williams * disks are in one of three states:
83487c8331fSDan Williams * 1/ present for initial domain discovery, and these
83587c8331fSDan Williams * resets will cause bcn flutters
83687c8331fSDan Williams * 2/ hot removed, we'll discover that after eh fails
83787c8331fSDan Williams * 3/ hot added after initial discovery, lost the race, and need
83887c8331fSDan Williams * to catch the next train.
83987c8331fSDan Williams */
84087c8331fSDan Williams sas_disable_revalidation(sas_ha);
84100dd4998SJames Bottomley
8429508a66fSDan Williams spin_lock_irq(&sas_ha->phy_port_lock);
8439508a66fSDan Williams for (i = 0; i < sas_ha->num_phys; i++) {
8449508a66fSDan Williams struct asd_sas_port *port = sas_ha->sas_port[i];
8459508a66fSDan Williams struct domain_device *dev;
84600dd4998SJames Bottomley
8479508a66fSDan Williams spin_lock(&port->dev_list_lock);
8489508a66fSDan Williams list_for_each_entry(dev, &port->dev_list, dev_list_node) {
849b2024459SDan Williams if (!dev_is_sata(dev))
85000dd4998SJames Bottomley continue;
851e4a9c373SDan Williams
852e4a9c373SDan Williams /* hold a reference over eh since we may be
853e4a9c373SDan Williams * racing with final remove once all commands
854e4a9c373SDan Williams * are completed
855e4a9c373SDan Williams */
856e4a9c373SDan Williams kref_get(&dev->kref);
857e4a9c373SDan Williams
8589508a66fSDan Williams async_schedule_domain(async_sas_ata_eh, dev, &async);
85900dd4998SJames Bottomley }
8609508a66fSDan Williams spin_unlock(&port->dev_list_lock);
8619508a66fSDan Williams }
8629508a66fSDan Williams spin_unlock_irq(&sas_ha->phy_port_lock);
8639508a66fSDan Williams
86450824d6cSDan Williams async_synchronize_full_domain(&async);
86587c8331fSDan Williams
86687c8331fSDan Williams sas_enable_revalidation(sas_ha);
86700dd4998SJames Bottomley }
86800dd4998SJames Bottomley
sas_ata_eh(struct Scsi_Host * shost,struct list_head * work_q)8693a20e642SXiang Chen void sas_ata_eh(struct Scsi_Host *shost, struct list_head *work_q)
87000dd4998SJames Bottomley {
87100dd4998SJames Bottomley struct scsi_cmnd *cmd, *n;
872d214d81eSDan Williams struct domain_device *eh_dev;
87300dd4998SJames Bottomley
87400dd4998SJames Bottomley do {
87500dd4998SJames Bottomley LIST_HEAD(sata_q);
876d214d81eSDan Williams eh_dev = NULL;
87700dd4998SJames Bottomley
87800dd4998SJames Bottomley list_for_each_entry_safe(cmd, n, work_q, eh_entry) {
87900dd4998SJames Bottomley struct domain_device *ddev = cmd_to_domain_dev(cmd);
88000dd4998SJames Bottomley
88100dd4998SJames Bottomley if (!dev_is_sata(ddev) || TO_SAS_TASK(cmd))
88200dd4998SJames Bottomley continue;
883d214d81eSDan Williams if (eh_dev && eh_dev != ddev)
88400dd4998SJames Bottomley continue;
885d214d81eSDan Williams eh_dev = ddev;
88600dd4998SJames Bottomley list_move(&cmd->eh_entry, &sata_q);
88700dd4998SJames Bottomley }
88800dd4998SJames Bottomley
88900dd4998SJames Bottomley if (!list_empty(&sata_q)) {
890d214d81eSDan Williams struct ata_port *ap = eh_dev->sata_dev.ap;
891d214d81eSDan Williams
892d214d81eSDan Williams sas_ata_printk(KERN_DEBUG, eh_dev, "cmd error handler\n");
89300dd4998SJames Bottomley ata_scsi_cmd_error_handler(shost, ap, &sata_q);
894a82058a7SJames Bottomley /*
895a82058a7SJames Bottomley * ata's error handler may leave the cmd on the list
896a82058a7SJames Bottomley * so make sure they don't remain on a stack list
897a82058a7SJames Bottomley * about to go out of scope.
898a82058a7SJames Bottomley *
899a82058a7SJames Bottomley * This looks strange, since the commands are
900a82058a7SJames Bottomley * now part of no list, but the next error
901a82058a7SJames Bottomley * action will be ata_port_error_handler()
902a82058a7SJames Bottomley * which takes no list and sweeps them up
903a82058a7SJames Bottomley * anyway from the ata tag array.
904a82058a7SJames Bottomley */
905a82058a7SJames Bottomley while (!list_empty(&sata_q))
906a82058a7SJames Bottomley list_del_init(sata_q.next);
90700dd4998SJames Bottomley }
908d214d81eSDan Williams } while (eh_dev);
90900dd4998SJames Bottomley }
910b52df417SDan Williams
sas_ata_schedule_reset(struct domain_device * dev)911b52df417SDan Williams void sas_ata_schedule_reset(struct domain_device *dev)
912b52df417SDan Williams {
913b52df417SDan Williams struct ata_eh_info *ehi;
914b52df417SDan Williams struct ata_port *ap;
915b52df417SDan Williams unsigned long flags;
916b52df417SDan Williams
917b52df417SDan Williams if (!dev_is_sata(dev))
918b52df417SDan Williams return;
919b52df417SDan Williams
920b52df417SDan Williams ap = dev->sata_dev.ap;
921b52df417SDan Williams ehi = &ap->link.eh_info;
922b52df417SDan Williams
923b52df417SDan Williams spin_lock_irqsave(ap->lock, flags);
924b52df417SDan Williams ehi->err_mask |= AC_ERR_TIMEOUT;
925b52df417SDan Williams ehi->action |= ATA_EH_RESET;
926b52df417SDan Williams ata_port_schedule_eh(ap);
927b52df417SDan Williams spin_unlock_irqrestore(ap->lock, flags);
928b52df417SDan Williams }
92943a5ab15SDan Williams EXPORT_SYMBOL_GPL(sas_ata_schedule_reset);
93081c757bcSDan Williams
sas_ata_wait_eh(struct domain_device * dev)93181c757bcSDan Williams void sas_ata_wait_eh(struct domain_device *dev)
93281c757bcSDan Williams {
93381c757bcSDan Williams struct ata_port *ap;
93481c757bcSDan Williams
93581c757bcSDan Williams if (!dev_is_sata(dev))
93681c757bcSDan Williams return;
93781c757bcSDan Williams
93881c757bcSDan Williams ap = dev->sata_dev.ap;
93981c757bcSDan Williams ata_port_wait_eh(ap);
94081c757bcSDan Williams }
9413f2e252eSJohn Garry
sas_ata_device_link_abort(struct domain_device * device,bool force_reset)94244112922SJohn Garry void sas_ata_device_link_abort(struct domain_device *device, bool force_reset)
94344112922SJohn Garry {
94444112922SJohn Garry struct ata_port *ap = device->sata_dev.ap;
94544112922SJohn Garry struct ata_link *link = &ap->link;
946a67aad57SXingui Yang unsigned long flags;
94744112922SJohn Garry
948a67aad57SXingui Yang spin_lock_irqsave(ap->lock, flags);
94944112922SJohn Garry device->sata_dev.fis[2] = ATA_ERR | ATA_DRDY; /* tf status */
95044112922SJohn Garry device->sata_dev.fis[3] = ATA_ABORTED; /* tf error */
95144112922SJohn Garry
95244112922SJohn Garry link->eh_info.err_mask |= AC_ERR_DEV;
95344112922SJohn Garry if (force_reset)
95444112922SJohn Garry link->eh_info.action |= ATA_EH_RESET;
95544112922SJohn Garry ata_link_abort(link);
956a67aad57SXingui Yang spin_unlock_irqrestore(ap->lock, flags);
95744112922SJohn Garry }
95844112922SJohn Garry EXPORT_SYMBOL_GPL(sas_ata_device_link_abort);
95944112922SJohn Garry
sas_execute_ata_cmd(struct domain_device * device,u8 * fis,int force_phy_id)9603f2e252eSJohn Garry int sas_execute_ata_cmd(struct domain_device *device, u8 *fis, int force_phy_id)
9613f2e252eSJohn Garry {
9623f2e252eSJohn Garry struct sas_tmf_task tmf_task = {};
9633f2e252eSJohn Garry return sas_execute_tmf(device, fis, sizeof(struct host_to_dev_fis),
9643f2e252eSJohn Garry force_phy_id, &tmf_task);
9653f2e252eSJohn Garry }
9663f2e252eSJohn Garry EXPORT_SYMBOL_GPL(sas_execute_ata_cmd);
967