2 * Support for SATA devices on Serial Attached SCSI (SAS) controllers
4 * Copyright (C) 2006 IBM Corporation
6 * Written by: Darrick J. Wong <djwong@us.ibm.com>, IBM Corporation
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of the
11 * License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
24 #include <scsi/sas_ata.h>
25 #include "sas_internal.h"
26 #include <scsi/scsi_host.h>
27 #include <scsi/scsi_device.h>
28 #include <scsi/scsi_tcq.h>
29 #include <scsi/scsi.h>
30 #include <scsi/scsi_transport.h>
31 #include <scsi/scsi_transport_sas.h>
32 #include "../scsi_sas_internal.h"
34 static enum ata_completion_errors sas_to_ata_err(struct task_status_struct *ts)
36 /* Cheesy attempt to translate SAS errors into ATA. Hah! */
39 if (ts->resp == SAS_TASK_UNDELIVERED)
40 return AC_ERR_ATA_BUS;
42 /* ts->resp == SAS_TASK_COMPLETE */
43 /* task delivered, what happened afterwards? */
45 case SAS_DEV_NO_RESPONSE:
46 return AC_ERR_TIMEOUT;
51 return AC_ERR_ATA_BUS;
54 case SAS_DATA_UNDERRUN:
56 * Some programs that use the taskfile interface
57 * (smartctl in particular) can cause underrun
58 * problems. Ignore these errors, perhaps at our
63 case SAS_DATA_OVERRUN:
65 case SAS_DEVICE_UNKNOWN:
67 return AC_ERR_INVALID;
72 SAS_DPRINTK("%s: Saw error %d. What to do?\n",
73 __FUNCTION__, ts->stat);
76 case SAS_ABORTED_TASK:
79 case SAS_PROTO_RESPONSE:
80 /* This means the ending_fis has the error
81 * value; return 0 here to collect it */
88 static void sas_ata_task_done(struct sas_task *task)
90 struct ata_queued_cmd *qc = task->uldd_task;
91 struct domain_device *dev;
92 struct task_status_struct *stat = &task->task_status;
93 struct ata_task_resp *resp = (struct ata_task_resp *)stat->buf;
94 enum ata_completion_errors ac;
100 dev = qc->ap->private_data;
102 spin_lock_irqsave(dev->sata_dev.ap->lock, flags);
103 if (stat->stat == SAS_PROTO_RESPONSE || stat->stat == SAM_GOOD) {
104 ata_tf_from_fis(resp->ending_fis, &dev->sata_dev.tf);
105 qc->err_mask |= ac_err_mask(dev->sata_dev.tf.command);
106 dev->sata_dev.sstatus = resp->sstatus;
107 dev->sata_dev.serror = resp->serror;
108 dev->sata_dev.scontrol = resp->scontrol;
109 } else if (stat->stat != SAM_STAT_GOOD) {
110 ac = sas_to_ata_err(stat);
112 SAS_DPRINTK("%s: SAS error %x\n", __FUNCTION__,
114 /* We saw a SAS error. Send a vague error. */
116 dev->sata_dev.tf.feature = 0x04; /* status err */
117 dev->sata_dev.tf.command = ATA_ERR;
121 qc->lldd_task = NULL;
123 spin_unlock_irqrestore(dev->sata_dev.ap->lock, flags);
126 list_del_init(&task->list);
130 static unsigned int sas_ata_qc_issue(struct ata_queued_cmd *qc)
133 struct sas_task *task;
134 struct domain_device *dev = qc->ap->private_data;
135 struct sas_ha_struct *sas_ha = dev->port->ha;
136 struct Scsi_Host *host = sas_ha->core.shost;
137 struct sas_internal *i = to_sas_internal(host->transportt);
138 struct scatterlist *sg;
139 unsigned int num = 0;
140 unsigned int xfer = 0;
142 task = sas_alloc_task(GFP_ATOMIC);
144 return AC_ERR_SYSTEM;
146 task->task_proto = SAS_PROTOCOL_STP;
147 task->task_done = sas_ata_task_done;
149 if (qc->tf.command == ATA_CMD_FPDMA_WRITE ||
150 qc->tf.command == ATA_CMD_FPDMA_READ) {
151 /* Need to zero out the tag libata assigned us */
155 ata_tf_to_fis(&qc->tf, (u8*)&task->ata_task.fis, 0);
156 task->uldd_task = qc;
157 if (is_atapi_taskfile(&qc->tf)) {
158 memcpy(task->ata_task.atapi_packet, qc->cdb, qc->dev->cdb_len);
159 task->total_xfer_len = qc->nbytes + qc->pad_len;
160 task->num_scatter = qc->pad_len ? qc->n_elem + 1 : qc->n_elem;
162 ata_for_each_sg(sg, qc) {
167 task->total_xfer_len = xfer;
168 task->num_scatter = num;
171 task->data_dir = qc->dma_dir;
172 task->scatter = qc->__sg;
173 task->ata_task.retry_count = 1;
174 task->task_state_flags = SAS_TASK_STATE_PENDING;
175 qc->lldd_task = task;
177 switch (qc->tf.protocol) {
179 task->ata_task.use_ncq = 1;
181 case ATA_PROT_ATAPI_DMA:
183 task->ata_task.dma_xfer = 1;
187 if (sas_ha->lldd_max_execute_num < 2)
188 res = i->dft->lldd_execute_task(task, 1, GFP_ATOMIC);
190 res = sas_queue_up(task);
194 SAS_DPRINTK("lldd_execute_task returned: %d\n", res);
197 return AC_ERR_SYSTEM;
203 static u8 sas_ata_check_status(struct ata_port *ap)
205 struct domain_device *dev = ap->private_data;
206 return dev->sata_dev.tf.command;
209 static void sas_ata_phy_reset(struct ata_port *ap)
211 struct domain_device *dev = ap->private_data;
212 struct sas_internal *i =
213 to_sas_internal(dev->port->ha->core.shost->transportt);
216 if (i->dft->lldd_I_T_nexus_reset)
217 res = i->dft->lldd_I_T_nexus_reset(dev);
220 SAS_DPRINTK("%s: Unable to reset I T nexus?\n", __FUNCTION__);
222 switch (dev->sata_dev.command_set) {
223 case ATA_COMMAND_SET:
224 SAS_DPRINTK("%s: Found ATA device.\n", __FUNCTION__);
225 ap->device[0].class = ATA_DEV_ATA;
227 case ATAPI_COMMAND_SET:
228 SAS_DPRINTK("%s: Found ATAPI device.\n", __FUNCTION__);
229 ap->device[0].class = ATA_DEV_ATAPI;
232 SAS_DPRINTK("%s: Unknown SATA command set: %d.\n",
234 dev->sata_dev.command_set);
235 ap->device[0].class = ATA_DEV_UNKNOWN;
239 ap->cbl = ATA_CBL_SATA;
242 static void sas_ata_post_internal(struct ata_queued_cmd *qc)
244 if (qc->flags & ATA_QCFLAG_FAILED)
245 qc->err_mask |= AC_ERR_OTHER;
249 * Find the sas_task and kill it. By this point,
250 * libata has decided to kill the qc, so we needn't
251 * bother with sas_ata_task_done. But we still
252 * ought to abort the task.
254 struct sas_task *task = qc->lldd_task;
255 struct domain_device *dev = qc->ap->private_data;
257 qc->lldd_task = NULL;
259 task->uldd_task = NULL;
260 __sas_task_abort(task);
263 sas_phy_reset(dev->port->phy, 1);
267 static void sas_ata_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
269 struct domain_device *dev = ap->private_data;
270 memcpy(tf, &dev->sata_dev.tf, sizeof (*tf));
273 static void sas_ata_scr_write(struct ata_port *ap, unsigned int sc_reg_in,
276 struct domain_device *dev = ap->private_data;
278 SAS_DPRINTK("STUB %s\n", __FUNCTION__);
281 dev->sata_dev.sstatus = val;
284 dev->sata_dev.scontrol = val;
287 dev->sata_dev.serror = val;
290 dev->sata_dev.ap->sactive = val;
295 static u32 sas_ata_scr_read(struct ata_port *ap, unsigned int sc_reg_in)
297 struct domain_device *dev = ap->private_data;
299 SAS_DPRINTK("STUB %s\n", __FUNCTION__);
302 return dev->sata_dev.sstatus;
304 return dev->sata_dev.scontrol;
306 return dev->sata_dev.serror;
308 return dev->sata_dev.ap->sactive;
314 static struct ata_port_operations sas_sata_ops = {
315 .port_disable = ata_port_disable,
316 .check_status = sas_ata_check_status,
317 .check_altstatus = sas_ata_check_status,
318 .dev_select = ata_noop_dev_select,
319 .phy_reset = sas_ata_phy_reset,
320 .post_internal_cmd = sas_ata_post_internal,
321 .tf_read = sas_ata_tf_read,
322 .qc_prep = ata_noop_qc_prep,
323 .qc_issue = sas_ata_qc_issue,
324 .port_start = ata_sas_port_start,
325 .port_stop = ata_sas_port_stop,
326 .scr_read = sas_ata_scr_read,
327 .scr_write = sas_ata_scr_write
330 static struct ata_port_info sata_port_info = {
331 .flags = ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY | ATA_FLAG_SATA_RESET |
332 ATA_FLAG_MMIO | ATA_FLAG_PIO_DMA | ATA_FLAG_NCQ,
333 .pio_mask = 0x1f, /* PIO0-4 */
334 .mwdma_mask = 0x07, /* MWDMA0-2 */
335 .udma_mask = ATA_UDMA6,
336 .port_ops = &sas_sata_ops
339 int sas_ata_init_host_and_port(struct domain_device *found_dev,
340 struct scsi_target *starget)
342 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
343 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
346 ata_host_init(&found_dev->sata_dev.ata_host,
348 sata_port_info.flags,
350 ap = ata_sas_port_alloc(&found_dev->sata_dev.ata_host,
354 SAS_DPRINTK("ata_sas_port_alloc failed.\n");
358 ap->private_data = found_dev;
359 ap->cbl = ATA_CBL_SATA;
360 ap->scsi_host = shost;
361 found_dev->sata_dev.ap = ap;