/drivers/scsi/libsas/sas_ata.c
C | 777 lines | 574 code | 98 blank | 105 comment | 127 complexity | 0c36aeacb328f5f433e128211ba73109 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, GPL-2.0, LGPL-2.0, AGPL-1.0
- /*
- * Support for SATA devices on Serial Attached SCSI (SAS) controllers
- *
- * Copyright (C) 2006 IBM Corporation
- *
- * Written by: Darrick J. Wong <djwong@us.ibm.com>, IBM Corporation
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License as
- * published by the Free Software Foundation; either version 2 of the
- * License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
- * USA
- */
- #include <linux/scatterlist.h>
- #include <linux/slab.h>
- #include <scsi/sas_ata.h>
- #include "sas_internal.h"
- #include <scsi/scsi_host.h>
- #include <scsi/scsi_device.h>
- #include <scsi/scsi_tcq.h>
- #include <scsi/scsi.h>
- #include <scsi/scsi_transport.h>
- #include <scsi/scsi_transport_sas.h>
- #include "../scsi_sas_internal.h"
- #include "../scsi_transport_api.h"
- #include <scsi/scsi_eh.h>
- static enum ata_completion_errors sas_to_ata_err(struct task_status_struct *ts)
- {
- /* Cheesy attempt to translate SAS errors into ATA. Hah! */
- /* transport error */
- if (ts->resp == SAS_TASK_UNDELIVERED)
- return AC_ERR_ATA_BUS;
- /* ts->resp == SAS_TASK_COMPLETE */
- /* task delivered, what happened afterwards? */
- switch (ts->stat) {
- case SAS_DEV_NO_RESPONSE:
- return AC_ERR_TIMEOUT;
- case SAS_INTERRUPTED:
- case SAS_PHY_DOWN:
- case SAS_NAK_R_ERR:
- return AC_ERR_ATA_BUS;
- case SAS_DATA_UNDERRUN:
- /*
- * Some programs that use the taskfile interface
- * (smartctl in particular) can cause underrun
- * problems. Ignore these errors, perhaps at our
- * peril.
- */
- return 0;
- case SAS_DATA_OVERRUN:
- case SAS_QUEUE_FULL:
- case SAS_DEVICE_UNKNOWN:
- case SAS_SG_ERR:
- return AC_ERR_INVALID;
- case SAM_CHECK_COND:
- case SAS_OPEN_TO:
- case SAS_OPEN_REJECT:
- SAS_DPRINTK("%s: Saw error %d. What to do?\n",
- __func__, ts->stat);
- return AC_ERR_OTHER;
- case SAS_ABORTED_TASK:
- return AC_ERR_DEV;
- case SAS_PROTO_RESPONSE:
- /* This means the ending_fis has the error
- * value; return 0 here to collect it */
- return 0;
- default:
- return 0;
- }
- }
- static void sas_ata_task_done(struct sas_task *task)
- {
- struct ata_queued_cmd *qc = task->uldd_task;
- struct domain_device *dev;
- struct task_status_struct *stat = &task->task_status;
- struct ata_task_resp *resp = (struct ata_task_resp *)stat->buf;
- struct sas_ha_struct *sas_ha;
- enum ata_completion_errors ac;
- unsigned long flags;
- if (!qc)
- goto qc_already_gone;
- dev = qc->ap->private_data;
- sas_ha = dev->port->ha;
- spin_lock_irqsave(dev->sata_dev.ap->lock, flags);
- if (stat->stat == SAS_PROTO_RESPONSE || stat->stat == SAM_GOOD) {
- ata_tf_from_fis(resp->ending_fis, &dev->sata_dev.tf);
- qc->err_mask |= ac_err_mask(dev->sata_dev.tf.command);
- dev->sata_dev.sstatus = resp->sstatus;
- dev->sata_dev.serror = resp->serror;
- dev->sata_dev.scontrol = resp->scontrol;
- } else if (stat->stat != SAM_STAT_GOOD) {
- ac = sas_to_ata_err(stat);
- if (ac) {
- SAS_DPRINTK("%s: SAS error %x\n", __func__,
- stat->stat);
- /* We saw a SAS error. Send a vague error. */
- qc->err_mask = ac;
- dev->sata_dev.tf.feature = 0x04; /* status err */
- dev->sata_dev.tf.command = ATA_ERR;
- }
- }
- qc->lldd_task = NULL;
- if (qc->scsicmd)
- ASSIGN_SAS_TASK(qc->scsicmd, NULL);
- ata_qc_complete(qc);
- spin_unlock_irqrestore(dev->sata_dev.ap->lock, flags);
- /*
- * If the sas_task has an ata qc, a scsi_cmnd and the aborted
- * flag is set, then we must have come in via the libsas EH
- * functions. When we exit this function, we need to put the
- * scsi_cmnd on the list of finished errors. The ata_qc_complete
- * call cleans up the libata side of things but we're protected
- * from the scsi_cmnd going away because the scsi_cmnd is owned
- * by the EH, making libata's call to scsi_done a NOP.
- */
- spin_lock_irqsave(&task->task_state_lock, flags);
- if (qc->scsicmd && task->task_state_flags & SAS_TASK_STATE_ABORTED)
- scsi_eh_finish_cmd(qc->scsicmd, &sas_ha->eh_done_q);
- spin_unlock_irqrestore(&task->task_state_lock, flags);
- qc_already_gone:
- list_del_init(&task->list);
- sas_free_task(task);
- }
- static unsigned int sas_ata_qc_issue(struct ata_queued_cmd *qc)
- {
- int res;
- struct sas_task *task;
- struct domain_device *dev = qc->ap->private_data;
- struct sas_ha_struct *sas_ha = dev->port->ha;
- struct Scsi_Host *host = sas_ha->core.shost;
- struct sas_internal *i = to_sas_internal(host->transportt);
- struct scatterlist *sg;
- unsigned int xfer = 0;
- unsigned int si;
- task = sas_alloc_task(GFP_ATOMIC);
- if (!task)
- return AC_ERR_SYSTEM;
- task->dev = dev;
- task->task_proto = SAS_PROTOCOL_STP;
- task->task_done = sas_ata_task_done;
- if (qc->tf.command == ATA_CMD_FPDMA_WRITE ||
- qc->tf.command == ATA_CMD_FPDMA_READ) {
- /* Need to zero out the tag libata assigned us */
- qc->tf.nsect = 0;
- }
- ata_tf_to_fis(&qc->tf, 1, 0, (u8*)&task->ata_task.fis);
- task->uldd_task = qc;
- if (ata_is_atapi(qc->tf.protocol)) {
- memcpy(task->ata_task.atapi_packet, qc->cdb, qc->dev->cdb_len);
- task->total_xfer_len = qc->nbytes;
- task->num_scatter = qc->n_elem;
- } else {
- for_each_sg(qc->sg, sg, qc->n_elem, si)
- xfer += sg->length;
- task->total_xfer_len = xfer;
- task->num_scatter = si;
- }
- task->data_dir = qc->dma_dir;
- task->scatter = qc->sg;
- task->ata_task.retry_count = 1;
- task->task_state_flags = SAS_TASK_STATE_PENDING;
- qc->lldd_task = task;
- switch (qc->tf.protocol) {
- case ATA_PROT_NCQ:
- task->ata_task.use_ncq = 1;
- /* fall through */
- case ATAPI_PROT_DMA:
- case ATA_PROT_DMA:
- task->ata_task.dma_xfer = 1;
- break;
- }
- if (qc->scsicmd)
- ASSIGN_SAS_TASK(qc->scsicmd, task);
- if (sas_ha->lldd_max_execute_num < 2)
- res = i->dft->lldd_execute_task(task, 1, GFP_ATOMIC);
- else
- res = sas_queue_up(task);
- /* Examine */
- if (res) {
- SAS_DPRINTK("lldd_execute_task returned: %d\n", res);
- if (qc->scsicmd)
- ASSIGN_SAS_TASK(qc->scsicmd, NULL);
- sas_free_task(task);
- return AC_ERR_SYSTEM;
- }
- return 0;
- }
- static bool sas_ata_qc_fill_rtf(struct ata_queued_cmd *qc)
- {
- struct domain_device *dev = qc->ap->private_data;
- memcpy(&qc->result_tf, &dev->sata_dev.tf, sizeof(qc->result_tf));
- return true;
- }
- static void sas_ata_phy_reset(struct ata_port *ap)
- {
- struct domain_device *dev = ap->private_data;
- struct sas_internal *i =
- to_sas_internal(dev->port->ha->core.shost->transportt);
- int res = TMF_RESP_FUNC_FAILED;
- if (i->dft->lldd_I_T_nexus_reset)
- res = i->dft->lldd_I_T_nexus_reset(dev);
- if (res != TMF_RESP_FUNC_COMPLETE)
-