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() */ 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 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 229931139afSDamien Le Moal static void 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 } 2354c9bf4e7STejun Heo 23636a39947SDan Williams static struct sas_internal *dev_to_sas_internal(struct domain_device *dev) 23736a39947SDan Williams { 23836a39947SDan Williams return to_sas_internal(dev->port->ha->core.shost->transportt); 23936a39947SDan Williams } 24036a39947SDan Williams 2416c90466eSJason Yan static int sas_get_ata_command_set(struct domain_device *dev) 2426c90466eSJason Yan { 2436c90466eSJason Yan struct ata_taskfile tf; 2446c90466eSJason Yan 2456c90466eSJason Yan if (dev->dev_type == SAS_SATA_PENDING) 2466c90466eSJason Yan return ATA_DEV_UNKNOWN; 2476c90466eSJason Yan 2486c90466eSJason Yan ata_tf_from_fis(dev->frame_rcvd, &tf); 2496c90466eSJason Yan 2506c90466eSJason Yan return ata_dev_classify(&tf); 2516c90466eSJason Yan } 252354cf829SDan Williams 253354cf829SDan Williams int sas_get_ata_info(struct domain_device *dev, struct ex_phy *phy) 254354cf829SDan Williams { 255354cf829SDan Williams if (phy->attached_tproto & SAS_PROTOCOL_STP) 256354cf829SDan Williams dev->tproto = phy->attached_tproto; 257354cf829SDan Williams if (phy->attached_sata_dev) 258aa9f8328SJames Bottomley dev->tproto |= SAS_SATA_DEV; 259354cf829SDan Williams 260aa9f8328SJames Bottomley if (phy->attached_dev_type == SAS_SATA_PENDING) 261aa9f8328SJames Bottomley dev->dev_type = SAS_SATA_PENDING; 262354cf829SDan Williams else { 263354cf829SDan Williams int res; 264354cf829SDan Williams 265aa9f8328SJames Bottomley dev->dev_type = SAS_SATA_DEV; 266354cf829SDan Williams res = sas_get_report_phy_sata(dev->parent, phy->phy_id, 267354cf829SDan Williams &dev->sata_dev.rps_resp); 268354cf829SDan Williams if (res) { 2693c236f8cSJohn Garry pr_debug("report phy sata to %016llx:%02d returned 0x%x\n", 27015ba7806SJohn Garry SAS_ADDR(dev->parent->sas_addr), 271354cf829SDan Williams phy->phy_id, res); 272354cf829SDan Williams return res; 273354cf829SDan Williams } 274354cf829SDan Williams memcpy(dev->frame_rcvd, &dev->sata_dev.rps_resp.rps.fis, 275354cf829SDan Williams sizeof(struct dev_to_host_fis)); 2761cbd772dSHannes Reinecke dev->sata_dev.class = sas_get_ata_command_set(dev); 277354cf829SDan Williams } 278354cf829SDan Williams return 0; 279354cf829SDan Williams } 280354cf829SDan Williams 281354cf829SDan Williams static int sas_ata_clear_pending(struct domain_device *dev, struct ex_phy *phy) 282354cf829SDan Williams { 283354cf829SDan Williams int res; 284354cf829SDan Williams 285354cf829SDan Williams /* we weren't pending, so successfully end the reset sequence now */ 286aa9f8328SJames Bottomley if (dev->dev_type != SAS_SATA_PENDING) 287354cf829SDan Williams return 1; 288354cf829SDan Williams 289354cf829SDan Williams /* hmmm, if this succeeds do we need to repost the domain_device to the 290354cf829SDan Williams * lldd so it can pick up new parameters? 291354cf829SDan Williams */ 292354cf829SDan Williams res = sas_get_ata_info(dev, phy); 293354cf829SDan Williams if (res) 294354cf829SDan Williams return 0; /* retry */ 295354cf829SDan Williams else 296354cf829SDan Williams return 1; 297354cf829SDan Williams } 298354cf829SDan Williams 2999181ce3cSJie Zhan int smp_ata_check_ready_type(struct ata_link *link) 3009181ce3cSJie Zhan { 3019181ce3cSJie Zhan struct domain_device *dev = link->ap->private_data; 3029181ce3cSJie Zhan struct sas_phy *phy = sas_get_local_phy(dev); 3039181ce3cSJie Zhan struct domain_device *ex_dev = dev->parent; 3049181ce3cSJie Zhan enum sas_device_type type = SAS_PHY_UNUSED; 3059181ce3cSJie Zhan u8 sas_addr[SAS_ADDR_SIZE]; 3069181ce3cSJie Zhan int res; 3079181ce3cSJie Zhan 3089181ce3cSJie Zhan res = sas_get_phy_attached_dev(ex_dev, phy->number, sas_addr, &type); 3099181ce3cSJie Zhan sas_put_local_phy(phy); 3109181ce3cSJie Zhan if (res) 3119181ce3cSJie Zhan return res; 3129181ce3cSJie Zhan 3139181ce3cSJie Zhan switch (type) { 3149181ce3cSJie Zhan case SAS_SATA_PENDING: 3159181ce3cSJie Zhan return 0; 3169181ce3cSJie Zhan case SAS_END_DEVICE: 3179181ce3cSJie Zhan return 1; 3189181ce3cSJie Zhan default: 3199181ce3cSJie Zhan return -ENODEV; 3209181ce3cSJie Zhan } 3219181ce3cSJie Zhan } 3229181ce3cSJie Zhan EXPORT_SYMBOL_GPL(smp_ata_check_ready_type); 3239181ce3cSJie Zhan 32436a39947SDan Williams static int smp_ata_check_ready(struct ata_link *link) 32536a39947SDan Williams { 32636a39947SDan Williams int res; 32736a39947SDan Williams struct ata_port *ap = link->ap; 32836a39947SDan Williams struct domain_device *dev = ap->private_data; 32936a39947SDan Williams struct domain_device *ex_dev = dev->parent; 330f41a0c44SDan Williams struct sas_phy *phy = sas_get_local_phy(dev); 331354cf829SDan Williams struct ex_phy *ex_phy = &ex_dev->ex_dev.ex_phy[phy->number]; 33236a39947SDan Williams 333354cf829SDan Williams res = sas_ex_phy_discover(ex_dev, phy->number); 334f41a0c44SDan Williams sas_put_local_phy(phy); 335354cf829SDan Williams 33636a39947SDan Williams /* break the wait early if the expander is unreachable, 33736a39947SDan Williams * otherwise keep polling 33836a39947SDan Williams */ 33936a39947SDan Williams if (res == -ECOMM) 34036a39947SDan Williams return res; 341354cf829SDan Williams if (res != SMP_RESP_FUNC_ACC) 34236a39947SDan Williams return 0; 343354cf829SDan Williams 344354cf829SDan Williams switch (ex_phy->attached_dev_type) { 345aa9f8328SJames Bottomley case SAS_SATA_PENDING: 346354cf829SDan Williams return 0; 347aa9f8328SJames Bottomley case SAS_END_DEVICE: 348354cf829SDan Williams if (ex_phy->attached_sata_dev) 349354cf829SDan Williams return sas_ata_clear_pending(dev, ex_phy); 350df561f66SGustavo A. R. Silva fallthrough; 351354cf829SDan Williams default: 352354cf829SDan Williams return -ENODEV; 353354cf829SDan Williams } 35436a39947SDan Williams } 35536a39947SDan Williams 35636a39947SDan Williams static int local_ata_check_ready(struct ata_link *link) 357338ec570SDarrick J. Wong { 35800dd4998SJames Bottomley struct ata_port *ap = link->ap; 359338ec570SDarrick J. Wong struct domain_device *dev = ap->private_data; 36036a39947SDan Williams struct sas_internal *i = dev_to_sas_internal(dev); 361338ec570SDarrick J. Wong 36236a39947SDan Williams if (i->dft->lldd_ata_check_ready) 36336a39947SDan Williams return i->dft->lldd_ata_check_ready(dev); 36436a39947SDan Williams else { 36536a39947SDan Williams /* lldd's that don't implement 'ready' checking get the 36636a39947SDan Williams * old default behavior of not coordinating reset 36736a39947SDan Williams * recovery with libata 36836a39947SDan Williams */ 36936a39947SDan Williams return 1; 37036a39947SDan Williams } 37100dd4998SJames Bottomley } 372338ec570SDarrick J. Wong 373d214d81eSDan Williams static int sas_ata_printk(const char *level, const struct domain_device *ddev, 374d214d81eSDan Williams const char *fmt, ...) 375d214d81eSDan Williams { 376d214d81eSDan Williams struct ata_port *ap = ddev->sata_dev.ap; 377d214d81eSDan Williams struct device *dev = &ddev->rphy->dev; 378d214d81eSDan Williams struct va_format vaf; 379d214d81eSDan Williams va_list args; 380d214d81eSDan Williams int r; 381d214d81eSDan Williams 382d214d81eSDan Williams va_start(args, fmt); 383d214d81eSDan Williams 384d214d81eSDan Williams vaf.fmt = fmt; 385d214d81eSDan Williams vaf.va = &args; 386d214d81eSDan Williams 387d188e5dbSJohn Garry r = printk("%s" SAS_FMT "ata%u: %s: %pV", 388d214d81eSDan Williams level, ap->print_id, dev_name(dev), &vaf); 389d214d81eSDan Williams 390d214d81eSDan Williams va_end(args); 391d214d81eSDan Williams 392d214d81eSDan Williams return r; 393d214d81eSDan Williams } 394d214d81eSDan Williams 3954d450cf2SJie Zhan static int sas_ata_wait_after_reset(struct domain_device *dev, unsigned long deadline) 39636a39947SDan Williams { 397057e5fc0SJohn Garry struct sata_device *sata_dev = &dev->sata_dev; 39836a39947SDan Williams int (*check_ready)(struct ata_link *link); 399057e5fc0SJohn Garry struct ata_port *ap = sata_dev->ap; 400057e5fc0SJohn Garry struct ata_link *link = &ap->link; 401057e5fc0SJohn Garry struct sas_phy *phy; 402057e5fc0SJohn Garry int ret; 40336a39947SDan Williams 404f41a0c44SDan Williams phy = sas_get_local_phy(dev); 40536a39947SDan Williams if (scsi_is_sas_phy_local(phy)) 40636a39947SDan Williams check_ready = local_ata_check_ready; 40736a39947SDan Williams else 40836a39947SDan Williams check_ready = smp_ata_check_ready; 409f41a0c44SDan Williams sas_put_local_phy(phy); 41036a39947SDan Williams 41136a39947SDan Williams ret = ata_wait_after_reset(link, deadline, check_ready); 41236a39947SDan Williams if (ret && ret != -EAGAIN) 413d214d81eSDan Williams sas_ata_printk(KERN_ERR, dev, "reset failed (errno=%d)\n", ret); 41436a39947SDan Williams 415057e5fc0SJohn Garry return ret; 416057e5fc0SJohn Garry } 417057e5fc0SJohn Garry 418057e5fc0SJohn Garry static int sas_ata_hard_reset(struct ata_link *link, unsigned int *class, 419057e5fc0SJohn Garry unsigned long deadline) 420057e5fc0SJohn Garry { 421057e5fc0SJohn Garry struct ata_port *ap = link->ap; 422057e5fc0SJohn Garry struct domain_device *dev = ap->private_data; 423057e5fc0SJohn Garry struct sas_internal *i = dev_to_sas_internal(dev); 424057e5fc0SJohn Garry int ret; 425057e5fc0SJohn Garry 426057e5fc0SJohn Garry ret = i->dft->lldd_I_T_nexus_reset(dev); 427057e5fc0SJohn Garry if (ret == -ENODEV) 428057e5fc0SJohn Garry return ret; 429057e5fc0SJohn Garry 430057e5fc0SJohn Garry if (ret != TMF_RESP_FUNC_COMPLETE) 431057e5fc0SJohn Garry sas_ata_printk(KERN_DEBUG, dev, "Unable to reset ata device?\n"); 432057e5fc0SJohn Garry 433057e5fc0SJohn Garry ret = sas_ata_wait_after_reset(dev, deadline); 434057e5fc0SJohn Garry 4351cbd772dSHannes Reinecke *class = dev->sata_dev.class; 436338ec570SDarrick J. Wong 437338ec570SDarrick J. Wong ap->cbl = ATA_CBL_SATA; 43800dd4998SJames Bottomley return ret; 439338ec570SDarrick J. Wong } 440338ec570SDarrick J. Wong 4413dff5721SDan Williams /* 4423dff5721SDan Williams * notify the lldd to forget the sas_task for this internal ata command 4433dff5721SDan Williams * that bypasses scsi-eh 4443dff5721SDan Williams */ 4453dff5721SDan Williams static void sas_ata_internal_abort(struct sas_task *task) 4463dff5721SDan Williams { 44736a39947SDan Williams struct sas_internal *si = dev_to_sas_internal(task->dev); 4483dff5721SDan Williams unsigned long flags; 4493dff5721SDan Williams int res; 4503dff5721SDan Williams 4513dff5721SDan Williams spin_lock_irqsave(&task->task_state_lock, flags); 4523dff5721SDan Williams if (task->task_state_flags & SAS_TASK_STATE_ABORTED || 4533dff5721SDan Williams task->task_state_flags & SAS_TASK_STATE_DONE) { 4543dff5721SDan Williams spin_unlock_irqrestore(&task->task_state_lock, flags); 45515ba7806SJohn Garry pr_debug("%s: Task %p already finished.\n", __func__, task); 4563dff5721SDan Williams goto out; 4573dff5721SDan Williams } 4583dff5721SDan Williams task->task_state_flags |= SAS_TASK_STATE_ABORTED; 4593dff5721SDan Williams spin_unlock_irqrestore(&task->task_state_lock, flags); 4603dff5721SDan Williams 4613dff5721SDan Williams res = si->dft->lldd_abort_task(task); 4623dff5721SDan Williams 4633dff5721SDan Williams spin_lock_irqsave(&task->task_state_lock, flags); 4643dff5721SDan Williams if (task->task_state_flags & SAS_TASK_STATE_DONE || 4653dff5721SDan Williams res == TMF_RESP_FUNC_COMPLETE) { 4663dff5721SDan Williams spin_unlock_irqrestore(&task->task_state_lock, flags); 4673dff5721SDan Williams goto out; 4683dff5721SDan Williams } 4693dff5721SDan Williams 4703dff5721SDan Williams /* XXX we are not prepared to deal with ->lldd_abort_task() 4713dff5721SDan Williams * failures. TODO: lldds need to unconditionally forget about 4723dff5721SDan Williams * aborted ata tasks, otherwise we (likely) leak the sas task 4733dff5721SDan Williams * here 4743dff5721SDan Williams */ 47515ba7806SJohn Garry pr_warn("%s: Task %p leaked.\n", __func__, task); 4763dff5721SDan Williams 4773dff5721SDan Williams if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) 4783dff5721SDan Williams task->task_state_flags &= ~SAS_TASK_STATE_ABORTED; 4793dff5721SDan Williams spin_unlock_irqrestore(&task->task_state_lock, flags); 4803dff5721SDan Williams 4813dff5721SDan Williams return; 4823dff5721SDan Williams out: 4833dff5721SDan Williams sas_free_task(task); 4843dff5721SDan Williams } 4853dff5721SDan Williams 486338ec570SDarrick J. Wong static void sas_ata_post_internal(struct ata_queued_cmd *qc) 487338ec570SDarrick J. Wong { 48887629312SNiklas Cassel if (qc->flags & ATA_QCFLAG_EH) 489338ec570SDarrick J. Wong qc->err_mask |= AC_ERR_OTHER; 490338ec570SDarrick J. Wong 4911c50dc83SDarrick J. Wong if (qc->err_mask) { 4921c50dc83SDarrick J. Wong /* 4933dff5721SDan Williams * Find the sas_task and kill it. By this point, libata 4943dff5721SDan Williams * has decided to kill the qc and has frozen the port. 4953dff5721SDan Williams * In this state sas_ata_task_done() will no longer free 4963dff5721SDan Williams * the sas_task, so we need to notify the lldd (via 4973dff5721SDan Williams * ->lldd_abort_task) that the task is dead and free it 4983dff5721SDan Williams * ourselves. 4991c50dc83SDarrick J. Wong */ 5001c50dc83SDarrick J. Wong struct sas_task *task = qc->lldd_task; 5011c50dc83SDarrick J. Wong 5021c50dc83SDarrick J. Wong qc->lldd_task = NULL; 5033a2cdf39SDan Williams if (!task) 5043a2cdf39SDan Williams return; 5051c50dc83SDarrick J. Wong task->uldd_task = NULL; 5063dff5721SDan Williams sas_ata_internal_abort(task); 5071c50dc83SDarrick J. Wong } 5081c50dc83SDarrick J. Wong } 509338ec570SDarrick J. Wong 510b91bb296SDan Williams 511b91bb296SDan Williams static void sas_ata_set_dmamode(struct ata_port *ap, struct ata_device *ata_dev) 512b91bb296SDan Williams { 513b91bb296SDan Williams struct domain_device *dev = ap->private_data; 51436a39947SDan Williams struct sas_internal *i = dev_to_sas_internal(dev); 515b91bb296SDan Williams 516b91bb296SDan Williams if (i->dft->lldd_ata_set_dmamode) 517b91bb296SDan Williams i->dft->lldd_ata_set_dmamode(dev); 518b91bb296SDan Williams } 519b91bb296SDan Williams 520e4a9c373SDan Williams static void sas_ata_sched_eh(struct ata_port *ap) 521e4a9c373SDan Williams { 522e4a9c373SDan Williams struct domain_device *dev = ap->private_data; 523e4a9c373SDan Williams struct sas_ha_struct *ha = dev->port->ha; 524e4a9c373SDan Williams unsigned long flags; 525e4a9c373SDan Williams 526e4a9c373SDan Williams spin_lock_irqsave(&ha->lock, flags); 527e4a9c373SDan Williams if (!test_and_set_bit(SAS_DEV_EH_PENDING, &dev->state)) 528e4a9c373SDan Williams ha->eh_active++; 529e4a9c373SDan Williams ata_std_sched_eh(ap); 530e4a9c373SDan Williams spin_unlock_irqrestore(&ha->lock, flags); 531e4a9c373SDan Williams } 532e4a9c373SDan Williams 533e4a9c373SDan Williams void sas_ata_end_eh(struct ata_port *ap) 534e4a9c373SDan Williams { 535e4a9c373SDan Williams struct domain_device *dev = ap->private_data; 536e4a9c373SDan Williams struct sas_ha_struct *ha = dev->port->ha; 537e4a9c373SDan Williams unsigned long flags; 538e4a9c373SDan Williams 539e4a9c373SDan Williams spin_lock_irqsave(&ha->lock, flags); 540e4a9c373SDan Williams if (test_and_clear_bit(SAS_DEV_EH_PENDING, &dev->state)) 541e4a9c373SDan Williams ha->eh_active--; 542e4a9c373SDan Williams spin_unlock_irqrestore(&ha->lock, flags); 543e4a9c373SDan Williams } 544e4a9c373SDan Williams 54538653379SLuo Jiaxing static int sas_ata_prereset(struct ata_link *link, unsigned long deadline) 54638653379SLuo Jiaxing { 54738653379SLuo Jiaxing struct ata_port *ap = link->ap; 54838653379SLuo Jiaxing struct domain_device *dev = ap->private_data; 54938653379SLuo Jiaxing struct sas_phy *local_phy = sas_get_local_phy(dev); 55038653379SLuo Jiaxing int res = 0; 55138653379SLuo Jiaxing 55238653379SLuo Jiaxing if (!local_phy->enabled || test_bit(SAS_DEV_GONE, &dev->state)) 55338653379SLuo Jiaxing res = -ENOENT; 55438653379SLuo Jiaxing sas_put_local_phy(local_phy); 55538653379SLuo Jiaxing 55638653379SLuo Jiaxing return res; 55738653379SLuo Jiaxing } 55838653379SLuo Jiaxing 559338ec570SDarrick J. Wong static struct ata_port_operations sas_sata_ops = { 56038653379SLuo Jiaxing .prereset = sas_ata_prereset, 56100dd4998SJames Bottomley .hardreset = sas_ata_hard_reset, 56200dd4998SJames Bottomley .error_handler = ata_std_error_handler, 563338ec570SDarrick J. Wong .post_internal_cmd = sas_ata_post_internal, 564f0ad30d3SDavid Milburn .qc_defer = ata_std_qc_defer, 565338ec570SDarrick J. Wong .qc_prep = ata_noop_qc_prep, 566338ec570SDarrick J. Wong .qc_issue = sas_ata_qc_issue, 5674c9bf4e7STejun Heo .qc_fill_rtf = sas_ata_qc_fill_rtf, 568b91bb296SDan Williams .set_dmamode = sas_ata_set_dmamode, 569e4a9c373SDan Williams .sched_eh = sas_ata_sched_eh, 570e4a9c373SDan Williams .end_eh = sas_ata_end_eh, 571338ec570SDarrick J. Wong }; 572338ec570SDarrick J. Wong 573338ec570SDarrick J. Wong static struct ata_port_info sata_port_info = { 5745067c046SShaohua Li .flags = ATA_FLAG_SATA | ATA_FLAG_PIO_DMA | ATA_FLAG_NCQ | 575ef026b18SHannes Reinecke ATA_FLAG_SAS_HOST | ATA_FLAG_FPDMA_AUX, 5760f2e0330SSergei Shtylyov .pio_mask = ATA_PIO4, 5770f2e0330SSergei Shtylyov .mwdma_mask = ATA_MWDMA2, 578338ec570SDarrick J. Wong .udma_mask = ATA_UDMA6, 579338ec570SDarrick J. Wong .port_ops = &sas_sata_ops 580338ec570SDarrick J. Wong }; 581338ec570SDarrick J. Wong 582b2024459SDan Williams int sas_ata_init(struct domain_device *found_dev) 583338ec570SDarrick J. Wong { 5849508a66fSDan Williams struct sas_ha_struct *ha = found_dev->port->ha; 5859508a66fSDan Williams struct Scsi_Host *shost = ha->core.shost; 5862fa4a326SJason Yan struct ata_host *ata_host; 587338ec570SDarrick J. Wong struct ata_port *ap; 588b2024459SDan Williams int rc; 589338ec570SDarrick J. Wong 5902fa4a326SJason Yan ata_host = kzalloc(sizeof(*ata_host), GFP_KERNEL); 5912fa4a326SJason Yan if (!ata_host) { 59215ba7806SJohn Garry pr_err("ata host alloc failed.\n"); 5932fa4a326SJason Yan return -ENOMEM; 5942fa4a326SJason Yan } 5952fa4a326SJason Yan 5962fa4a326SJason Yan ata_host_init(ata_host, ha->dev, &sas_sata_ops); 5972fa4a326SJason Yan 5982fa4a326SJason Yan ap = ata_sas_port_alloc(ata_host, &sata_port_info, shost); 599338ec570SDarrick J. Wong if (!ap) { 60015ba7806SJohn Garry pr_err("ata_sas_port_alloc failed.\n"); 6012fa4a326SJason Yan rc = -ENODEV; 6022fa4a326SJason Yan goto free_host; 603338ec570SDarrick J. Wong } 604338ec570SDarrick J. Wong 605338ec570SDarrick J. Wong ap->private_data = found_dev; 606338ec570SDarrick J. Wong ap->cbl = ATA_CBL_SATA; 607338ec570SDarrick J. Wong ap->scsi_host = shost; 608b2024459SDan Williams rc = ata_sas_port_init(ap); 6092fa4a326SJason Yan if (rc) 6102fa4a326SJason Yan goto destroy_port; 6112fa4a326SJason Yan 6122fa4a326SJason Yan rc = ata_sas_tport_add(ata_host->dev, ap); 6132fa4a326SJason Yan if (rc) 6142fa4a326SJason Yan goto destroy_port; 6152fa4a326SJason Yan 6162fa4a326SJason Yan found_dev->sata_dev.ata_host = ata_host; 617338ec570SDarrick J. Wong found_dev->sata_dev.ap = ap; 618338ec570SDarrick J. Wong 619338ec570SDarrick J. Wong return 0; 6202fa4a326SJason Yan 6212fa4a326SJason Yan destroy_port: 6226c2fe21eSHannes Reinecke kfree(ap); 6232fa4a326SJason Yan free_host: 6242fa4a326SJason Yan ata_host_put(ata_host); 6252fa4a326SJason Yan return rc; 626338ec570SDarrick J. Wong } 6273a2755afSDarrick J. Wong 6283a2755afSDarrick J. Wong void sas_ata_task_abort(struct sas_task *task) 6293a2755afSDarrick J. Wong { 6303a2755afSDarrick J. Wong struct ata_queued_cmd *qc = task->uldd_task; 6313a2755afSDarrick J. Wong struct completion *waiting; 6323a2755afSDarrick J. Wong 6333a2755afSDarrick J. Wong /* Bounce SCSI-initiated commands to the SCSI EH */ 6343a2755afSDarrick J. Wong if (qc->scsicmd) { 635cad1a780SBart Van Assche blk_abort_request(scsi_cmd_to_rq(qc->scsicmd)); 6363a2755afSDarrick J. Wong return; 6373a2755afSDarrick J. Wong } 6383a2755afSDarrick J. Wong 6393a2755afSDarrick J. Wong /* Internal command, fake a timeout and complete. */ 6403a2755afSDarrick J. Wong qc->flags &= ~ATA_QCFLAG_ACTIVE; 64187629312SNiklas Cassel qc->flags |= ATA_QCFLAG_EH; 6423a2755afSDarrick J. Wong qc->err_mask |= AC_ERR_TIMEOUT; 6433a2755afSDarrick J. Wong waiting = qc->private_data; 6443a2755afSDarrick J. Wong complete(waiting); 6453a2755afSDarrick J. Wong } 646b9142174SJames Bottomley 6479508a66fSDan Williams void sas_probe_sata(struct asd_sas_port *port) 6489508a66fSDan Williams { 6499508a66fSDan Williams struct domain_device *dev, *n; 6509508a66fSDan Williams 6519508a66fSDan Williams mutex_lock(&port->ha->disco_mutex); 652b2024459SDan Williams list_for_each_entry(dev, &port->disco_list, disco_list_node) { 6539508a66fSDan Williams if (!dev_is_sata(dev)) 6549508a66fSDan Williams continue; 6559508a66fSDan Williams 656*a76f1b63SHannes Reinecke ata_port_probe(dev->sata_dev.ap); 6579508a66fSDan Williams } 6589508a66fSDan Williams mutex_unlock(&port->ha->disco_mutex); 6599508a66fSDan Williams 6609508a66fSDan Williams list_for_each_entry_safe(dev, n, &port->disco_list, disco_list_node) { 6619508a66fSDan Williams if (!dev_is_sata(dev)) 6629508a66fSDan Williams continue; 6639508a66fSDan Williams 6649508a66fSDan Williams sas_ata_wait_eh(dev); 6659508a66fSDan Williams 6669508a66fSDan Williams /* if libata could not bring the link up, don't surface 6679508a66fSDan Williams * the device 6689508a66fSDan Williams */ 669437207d3SJason Yan if (!ata_dev_enabled(sas_to_ata_dev(dev))) 6709508a66fSDan Williams sas_fail_probe(dev, __func__, -ENODEV); 6719508a66fSDan Williams } 672303694eeSDan Williams 673303694eeSDan Williams } 674303694eeSDan Williams 6757cc7646bSJason Yan int sas_ata_add_dev(struct domain_device *parent, struct ex_phy *phy, 6767cc7646bSJason Yan struct domain_device *child, int phy_id) 6777cc7646bSJason Yan { 6787cc7646bSJason Yan struct sas_rphy *rphy; 6797cc7646bSJason Yan int ret; 6807cc7646bSJason Yan 6817cc7646bSJason Yan if (child->linkrate > parent->min_linkrate) { 6827cc7646bSJason Yan struct sas_phy *cphy = child->phy; 6837cc7646bSJason Yan enum sas_linkrate min_prate = cphy->minimum_linkrate, 6847cc7646bSJason Yan parent_min_lrate = parent->min_linkrate, 6857cc7646bSJason Yan min_linkrate = (min_prate > parent_min_lrate) ? 6867cc7646bSJason Yan parent_min_lrate : 0; 6877cc7646bSJason Yan struct sas_phy_linkrates rates = { 6887cc7646bSJason Yan .maximum_linkrate = parent->min_linkrate, 6897cc7646bSJason Yan .minimum_linkrate = min_linkrate, 6907cc7646bSJason Yan }; 6917cc7646bSJason Yan 6927cc7646bSJason Yan pr_notice("ex %016llx phy%02d SATA device linkrate > min pathway connection rate, attempting to lower device linkrate\n", 6937cc7646bSJason Yan SAS_ADDR(child->sas_addr), phy_id); 6947cc7646bSJason Yan ret = sas_smp_phy_control(parent, phy_id, 6957cc7646bSJason Yan PHY_FUNC_LINK_RESET, &rates); 6967cc7646bSJason Yan if (ret) { 6977cc7646bSJason Yan pr_err("ex %016llx phy%02d SATA device could not set linkrate (%d)\n", 6987cc7646bSJason Yan SAS_ADDR(child->sas_addr), phy_id, ret); 6997cc7646bSJason Yan return ret; 7007cc7646bSJason Yan } 7017cc7646bSJason Yan pr_notice("ex %016llx phy%02d SATA device set linkrate successfully\n", 7027cc7646bSJason Yan SAS_ADDR(child->sas_addr), phy_id); 7037cc7646bSJason Yan child->linkrate = child->min_linkrate; 7047cc7646bSJason Yan } 7057cc7646bSJason Yan ret = sas_get_ata_info(child, phy); 7067cc7646bSJason Yan if (ret) 7077cc7646bSJason Yan return ret; 7087cc7646bSJason Yan 7097cc7646bSJason Yan sas_init_dev(child); 7107cc7646bSJason Yan ret = sas_ata_init(child); 7117cc7646bSJason Yan if (ret) 7127cc7646bSJason Yan return ret; 7137cc7646bSJason Yan 7147cc7646bSJason Yan rphy = sas_end_device_alloc(phy->port); 7157cc7646bSJason Yan if (!rphy) 7168fe66badSDan Carpenter return -ENOMEM; 7177cc7646bSJason Yan 7187cc7646bSJason Yan rphy->identify.phy_identifier = phy_id; 7197cc7646bSJason Yan child->rphy = rphy; 7207cc7646bSJason Yan get_device(&rphy->dev); 7217cc7646bSJason Yan 7227cc7646bSJason Yan list_add_tail(&child->disco_list_node, &parent->port->disco_list); 7237cc7646bSJason Yan 7247cc7646bSJason Yan ret = sas_discover_sata(child); 7257cc7646bSJason Yan if (ret) { 7267cc7646bSJason Yan pr_notice("sas_discover_sata() for device %16llx at %016llx:%02d returned 0x%x\n", 7277cc7646bSJason Yan SAS_ADDR(child->sas_addr), 7287cc7646bSJason Yan SAS_ADDR(parent->sas_addr), phy_id, ret); 7297cc7646bSJason Yan sas_rphy_free(child->rphy); 7307cc7646bSJason Yan list_del(&child->disco_list_node); 7317cc7646bSJason Yan return ret; 7327cc7646bSJason Yan } 7337cc7646bSJason Yan 7347cc7646bSJason Yan return 0; 7357cc7646bSJason Yan } 7367cc7646bSJason Yan 737bc6e7c4bSDan Williams static void sas_ata_flush_pm_eh(struct asd_sas_port *port, const char *func) 738303694eeSDan Williams { 739303694eeSDan Williams struct domain_device *dev, *n; 740303694eeSDan Williams 741303694eeSDan Williams list_for_each_entry_safe(dev, n, &port->dev_list, dev_list_node) { 742303694eeSDan Williams if (!dev_is_sata(dev)) 743303694eeSDan Williams continue; 744303694eeSDan Williams 745303694eeSDan Williams sas_ata_wait_eh(dev); 746303694eeSDan Williams 747303694eeSDan Williams /* if libata failed to power manage the device, tear it down */ 748303694eeSDan Williams if (ata_dev_disabled(sas_to_ata_dev(dev))) 749303694eeSDan Williams sas_fail_probe(dev, func, -ENODEV); 750303694eeSDan Williams } 751303694eeSDan Williams } 752303694eeSDan Williams 753303694eeSDan Williams void sas_suspend_sata(struct asd_sas_port *port) 754303694eeSDan Williams { 755303694eeSDan Williams struct domain_device *dev; 756303694eeSDan Williams 757303694eeSDan Williams mutex_lock(&port->ha->disco_mutex); 758303694eeSDan Williams list_for_each_entry(dev, &port->dev_list, dev_list_node) { 759303694eeSDan Williams struct sata_device *sata; 760303694eeSDan Williams 761303694eeSDan Williams if (!dev_is_sata(dev)) 762303694eeSDan Williams continue; 763303694eeSDan Williams 764303694eeSDan Williams sata = &dev->sata_dev; 765303694eeSDan Williams if (sata->ap->pm_mesg.event == PM_EVENT_SUSPEND) 766303694eeSDan Williams continue; 767303694eeSDan Williams 768bc6e7c4bSDan Williams ata_sas_port_suspend(sata->ap); 769303694eeSDan Williams } 770303694eeSDan Williams mutex_unlock(&port->ha->disco_mutex); 771303694eeSDan Williams 772bc6e7c4bSDan Williams sas_ata_flush_pm_eh(port, __func__); 773303694eeSDan Williams } 774303694eeSDan Williams 775303694eeSDan Williams void sas_resume_sata(struct asd_sas_port *port) 776303694eeSDan Williams { 777303694eeSDan Williams struct domain_device *dev; 778303694eeSDan Williams 779303694eeSDan Williams mutex_lock(&port->ha->disco_mutex); 780303694eeSDan Williams list_for_each_entry(dev, &port->dev_list, dev_list_node) { 781303694eeSDan Williams struct sata_device *sata; 782303694eeSDan Williams 783303694eeSDan Williams if (!dev_is_sata(dev)) 784303694eeSDan Williams continue; 785303694eeSDan Williams 786303694eeSDan Williams sata = &dev->sata_dev; 787303694eeSDan Williams if (sata->ap->pm_mesg.event == PM_EVENT_ON) 788303694eeSDan Williams continue; 789303694eeSDan Williams 790bc6e7c4bSDan Williams ata_sas_port_resume(sata->ap); 791303694eeSDan Williams } 792303694eeSDan Williams mutex_unlock(&port->ha->disco_mutex); 793303694eeSDan Williams 794bc6e7c4bSDan Williams sas_ata_flush_pm_eh(port, __func__); 7959508a66fSDan Williams } 7969508a66fSDan Williams 797b9142174SJames Bottomley /** 798121246aeSBart Van Assche * sas_discover_sata - discover an STP/SATA domain device 799b9142174SJames Bottomley * @dev: pointer to struct domain_device of interest 800b9142174SJames Bottomley * 801b91bb296SDan Williams * Devices directly attached to a HA port, have no parents. All other 802b91bb296SDan Williams * devices do, and should have their "parent" pointer set appropriately 803b91bb296SDan Williams * before calling this function. 804b9142174SJames Bottomley */ 805b9142174SJames Bottomley int sas_discover_sata(struct domain_device *dev) 806b9142174SJames Bottomley { 807aa9f8328SJames Bottomley if (dev->dev_type == SAS_SATA_PM) 808b91bb296SDan Williams return -ENODEV; 809b91bb296SDan Williams 8101cbd772dSHannes Reinecke dev->sata_dev.class = sas_get_ata_command_set(dev); 811b91bb296SDan Williams sas_fill_in_rphy(dev, dev->rphy); 81287c8331fSDan Williams 8133d1a99e2SLiu Shixin return sas_notify_lldd_dev_found(dev); 814b9142174SJames Bottomley } 81500dd4998SJames Bottomley 81650824d6cSDan Williams static void async_sas_ata_eh(void *data, async_cookie_t cookie) 81750824d6cSDan Williams { 81850824d6cSDan Williams struct domain_device *dev = data; 81950824d6cSDan Williams struct ata_port *ap = dev->sata_dev.ap; 82050824d6cSDan Williams struct sas_ha_struct *ha = dev->port->ha; 82150824d6cSDan Williams 822d214d81eSDan Williams sas_ata_printk(KERN_DEBUG, dev, "dev error handler\n"); 82350824d6cSDan Williams ata_scsi_port_error_handler(ha->core.shost, ap); 8248abda4d2SDan Williams sas_put_device(dev); 82550824d6cSDan Williams } 82650824d6cSDan Williams 82700dd4998SJames Bottomley void sas_ata_strategy_handler(struct Scsi_Host *shost) 82800dd4998SJames Bottomley { 82987c8331fSDan Williams struct sas_ha_struct *sas_ha = SHOST_TO_SAS_HA(shost); 8302955b47dSDan Williams ASYNC_DOMAIN_EXCLUSIVE(async); 8319508a66fSDan Williams int i; 83287c8331fSDan Williams 83387c8331fSDan Williams /* it's ok to defer revalidation events during ata eh, these 83487c8331fSDan Williams * disks are in one of three states: 83587c8331fSDan Williams * 1/ present for initial domain discovery, and these 83687c8331fSDan Williams * resets will cause bcn flutters 83787c8331fSDan Williams * 2/ hot removed, we'll discover that after eh fails 83887c8331fSDan Williams * 3/ hot added after initial discovery, lost the race, and need 83987c8331fSDan Williams * to catch the next train. 84087c8331fSDan Williams */ 84187c8331fSDan Williams sas_disable_revalidation(sas_ha); 84200dd4998SJames Bottomley 8439508a66fSDan Williams spin_lock_irq(&sas_ha->phy_port_lock); 8449508a66fSDan Williams for (i = 0; i < sas_ha->num_phys; i++) { 8459508a66fSDan Williams struct asd_sas_port *port = sas_ha->sas_port[i]; 8469508a66fSDan Williams struct domain_device *dev; 84700dd4998SJames Bottomley 8489508a66fSDan Williams spin_lock(&port->dev_list_lock); 8499508a66fSDan Williams list_for_each_entry(dev, &port->dev_list, dev_list_node) { 850b2024459SDan Williams if (!dev_is_sata(dev)) 85100dd4998SJames Bottomley continue; 852e4a9c373SDan Williams 853e4a9c373SDan Williams /* hold a reference over eh since we may be 854e4a9c373SDan Williams * racing with final remove once all commands 855e4a9c373SDan Williams * are completed 856e4a9c373SDan Williams */ 857e4a9c373SDan Williams kref_get(&dev->kref); 858e4a9c373SDan Williams 8599508a66fSDan Williams async_schedule_domain(async_sas_ata_eh, dev, &async); 86000dd4998SJames Bottomley } 8619508a66fSDan Williams spin_unlock(&port->dev_list_lock); 8629508a66fSDan Williams } 8639508a66fSDan Williams spin_unlock_irq(&sas_ha->phy_port_lock); 8649508a66fSDan Williams 86550824d6cSDan Williams async_synchronize_full_domain(&async); 86687c8331fSDan Williams 86787c8331fSDan Williams sas_enable_revalidation(sas_ha); 86800dd4998SJames Bottomley } 86900dd4998SJames Bottomley 8703a20e642SXiang Chen void sas_ata_eh(struct Scsi_Host *shost, struct list_head *work_q) 87100dd4998SJames Bottomley { 87200dd4998SJames Bottomley struct scsi_cmnd *cmd, *n; 873d214d81eSDan Williams struct domain_device *eh_dev; 87400dd4998SJames Bottomley 87500dd4998SJames Bottomley do { 87600dd4998SJames Bottomley LIST_HEAD(sata_q); 877d214d81eSDan Williams eh_dev = NULL; 87800dd4998SJames Bottomley 87900dd4998SJames Bottomley list_for_each_entry_safe(cmd, n, work_q, eh_entry) { 88000dd4998SJames Bottomley struct domain_device *ddev = cmd_to_domain_dev(cmd); 88100dd4998SJames Bottomley 88200dd4998SJames Bottomley if (!dev_is_sata(ddev) || TO_SAS_TASK(cmd)) 88300dd4998SJames Bottomley continue; 884d214d81eSDan Williams if (eh_dev && eh_dev != ddev) 88500dd4998SJames Bottomley continue; 886d214d81eSDan Williams eh_dev = ddev; 88700dd4998SJames Bottomley list_move(&cmd->eh_entry, &sata_q); 88800dd4998SJames Bottomley } 88900dd4998SJames Bottomley 89000dd4998SJames Bottomley if (!list_empty(&sata_q)) { 891d214d81eSDan Williams struct ata_port *ap = eh_dev->sata_dev.ap; 892d214d81eSDan Williams 893d214d81eSDan Williams sas_ata_printk(KERN_DEBUG, eh_dev, "cmd error handler\n"); 89400dd4998SJames Bottomley ata_scsi_cmd_error_handler(shost, ap, &sata_q); 895a82058a7SJames Bottomley /* 896a82058a7SJames Bottomley * ata's error handler may leave the cmd on the list 897a82058a7SJames Bottomley * so make sure they don't remain on a stack list 898a82058a7SJames Bottomley * about to go out of scope. 899a82058a7SJames Bottomley * 900a82058a7SJames Bottomley * This looks strange, since the commands are 901a82058a7SJames Bottomley * now part of no list, but the next error 902a82058a7SJames Bottomley * action will be ata_port_error_handler() 903a82058a7SJames Bottomley * which takes no list and sweeps them up 904a82058a7SJames Bottomley * anyway from the ata tag array. 905a82058a7SJames Bottomley */ 906a82058a7SJames Bottomley while (!list_empty(&sata_q)) 907a82058a7SJames Bottomley list_del_init(sata_q.next); 90800dd4998SJames Bottomley } 909d214d81eSDan Williams } while (eh_dev); 91000dd4998SJames Bottomley } 911b52df417SDan Williams 912b52df417SDan Williams void sas_ata_schedule_reset(struct domain_device *dev) 913b52df417SDan Williams { 914b52df417SDan Williams struct ata_eh_info *ehi; 915b52df417SDan Williams struct ata_port *ap; 916b52df417SDan Williams unsigned long flags; 917b52df417SDan Williams 918b52df417SDan Williams if (!dev_is_sata(dev)) 919b52df417SDan Williams return; 920b52df417SDan Williams 921b52df417SDan Williams ap = dev->sata_dev.ap; 922b52df417SDan Williams ehi = &ap->link.eh_info; 923b52df417SDan Williams 924b52df417SDan Williams spin_lock_irqsave(ap->lock, flags); 925b52df417SDan Williams ehi->err_mask |= AC_ERR_TIMEOUT; 926b52df417SDan Williams ehi->action |= ATA_EH_RESET; 927b52df417SDan Williams ata_port_schedule_eh(ap); 928b52df417SDan Williams spin_unlock_irqrestore(ap->lock, flags); 929b52df417SDan Williams } 93043a5ab15SDan Williams EXPORT_SYMBOL_GPL(sas_ata_schedule_reset); 93181c757bcSDan Williams 93281c757bcSDan Williams void sas_ata_wait_eh(struct domain_device *dev) 93381c757bcSDan Williams { 93481c757bcSDan Williams struct ata_port *ap; 93581c757bcSDan Williams 93681c757bcSDan Williams if (!dev_is_sata(dev)) 93781c757bcSDan Williams return; 93881c757bcSDan Williams 93981c757bcSDan Williams ap = dev->sata_dev.ap; 94081c757bcSDan Williams ata_port_wait_eh(ap); 94181c757bcSDan Williams } 9423f2e252eSJohn Garry 94344112922SJohn Garry void sas_ata_device_link_abort(struct domain_device *device, bool force_reset) 94444112922SJohn Garry { 94544112922SJohn Garry struct ata_port *ap = device->sata_dev.ap; 94644112922SJohn Garry struct ata_link *link = &ap->link; 947a67aad57SXingui Yang unsigned long flags; 94844112922SJohn Garry 949a67aad57SXingui Yang spin_lock_irqsave(ap->lock, flags); 95044112922SJohn Garry device->sata_dev.fis[2] = ATA_ERR | ATA_DRDY; /* tf status */ 95144112922SJohn Garry device->sata_dev.fis[3] = ATA_ABORTED; /* tf error */ 95244112922SJohn Garry 95344112922SJohn Garry link->eh_info.err_mask |= AC_ERR_DEV; 95444112922SJohn Garry if (force_reset) 95544112922SJohn Garry link->eh_info.action |= ATA_EH_RESET; 95644112922SJohn Garry ata_link_abort(link); 957a67aad57SXingui Yang spin_unlock_irqrestore(ap->lock, flags); 95844112922SJohn Garry } 95944112922SJohn Garry EXPORT_SYMBOL_GPL(sas_ata_device_link_abort); 96044112922SJohn Garry 9613f2e252eSJohn Garry int sas_execute_ata_cmd(struct domain_device *device, u8 *fis, int force_phy_id) 9623f2e252eSJohn Garry { 9633f2e252eSJohn Garry struct sas_tmf_task tmf_task = {}; 9643f2e252eSJohn Garry return sas_execute_tmf(device, fis, sizeof(struct host_to_dev_fis), 9653f2e252eSJohn Garry force_phy_id, &tmf_task); 9663f2e252eSJohn Garry } 9673f2e252eSJohn Garry EXPORT_SYMBOL_GPL(sas_execute_ata_cmd); 968