12908d778ab3e244900c310974e1fc1c69066e450James Bottomley/*
22908d778ab3e244900c310974e1fc1c69066e450James Bottomley * Aic94xx SAS/SATA Tasks
32908d778ab3e244900c310974e1fc1c69066e450James Bottomley *
42908d778ab3e244900c310974e1fc1c69066e450James Bottomley * Copyright (C) 2005 Adaptec, Inc.  All rights reserved.
52908d778ab3e244900c310974e1fc1c69066e450James Bottomley * Copyright (C) 2005 Luben Tuikov <luben_tuikov@adaptec.com>
62908d778ab3e244900c310974e1fc1c69066e450James Bottomley *
72908d778ab3e244900c310974e1fc1c69066e450James Bottomley * This file is licensed under GPLv2.
82908d778ab3e244900c310974e1fc1c69066e450James Bottomley *
92908d778ab3e244900c310974e1fc1c69066e450James Bottomley * This file is part of the aic94xx driver.
102908d778ab3e244900c310974e1fc1c69066e450James Bottomley *
112908d778ab3e244900c310974e1fc1c69066e450James Bottomley * The aic94xx driver is free software; you can redistribute it and/or
122908d778ab3e244900c310974e1fc1c69066e450James Bottomley * modify it under the terms of the GNU General Public License as
132908d778ab3e244900c310974e1fc1c69066e450James Bottomley * published by the Free Software Foundation; version 2 of the
142908d778ab3e244900c310974e1fc1c69066e450James Bottomley * License.
152908d778ab3e244900c310974e1fc1c69066e450James Bottomley *
162908d778ab3e244900c310974e1fc1c69066e450James Bottomley * The aic94xx driver is distributed in the hope that it will be useful,
172908d778ab3e244900c310974e1fc1c69066e450James Bottomley * but WITHOUT ANY WARRANTY; without even the implied warranty of
182908d778ab3e244900c310974e1fc1c69066e450James Bottomley * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
192908d778ab3e244900c310974e1fc1c69066e450James Bottomley * General Public License for more details.
202908d778ab3e244900c310974e1fc1c69066e450James Bottomley *
212908d778ab3e244900c310974e1fc1c69066e450James Bottomley * You should have received a copy of the GNU General Public License
222908d778ab3e244900c310974e1fc1c69066e450James Bottomley * along with the aic94xx driver; if not, write to the Free Software
232908d778ab3e244900c310974e1fc1c69066e450James Bottomley * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
242908d778ab3e244900c310974e1fc1c69066e450James Bottomley *
252908d778ab3e244900c310974e1fc1c69066e450James Bottomley */
262908d778ab3e244900c310974e1fc1c69066e450James Bottomley
272908d778ab3e244900c310974e1fc1c69066e450James Bottomley#include <linux/spinlock.h>
282908d778ab3e244900c310974e1fc1c69066e450James Bottomley#include "aic94xx.h"
292908d778ab3e244900c310974e1fc1c69066e450James Bottomley#include "aic94xx_sas.h"
302908d778ab3e244900c310974e1fc1c69066e450James Bottomley#include "aic94xx_hwi.h"
312908d778ab3e244900c310974e1fc1c69066e450James Bottomley
322908d778ab3e244900c310974e1fc1c69066e450James Bottomleystatic void asd_unbuild_ata_ascb(struct asd_ascb *a);
332908d778ab3e244900c310974e1fc1c69066e450James Bottomleystatic void asd_unbuild_smp_ascb(struct asd_ascb *a);
342908d778ab3e244900c310974e1fc1c69066e450James Bottomleystatic void asd_unbuild_ssp_ascb(struct asd_ascb *a);
352908d778ab3e244900c310974e1fc1c69066e450James Bottomley
3681e56ded878aeb8730f18c1d0a70d5face788be3Adrian Bunkstatic void asd_can_dequeue(struct asd_ha_struct *asd_ha, int num)
372908d778ab3e244900c310974e1fc1c69066e450James Bottomley{
382908d778ab3e244900c310974e1fc1c69066e450James Bottomley	unsigned long flags;
392908d778ab3e244900c310974e1fc1c69066e450James Bottomley
402908d778ab3e244900c310974e1fc1c69066e450James Bottomley	spin_lock_irqsave(&asd_ha->seq.pend_q_lock, flags);
412908d778ab3e244900c310974e1fc1c69066e450James Bottomley	asd_ha->seq.can_queue += num;
422908d778ab3e244900c310974e1fc1c69066e450James Bottomley	spin_unlock_irqrestore(&asd_ha->seq.pend_q_lock, flags);
432908d778ab3e244900c310974e1fc1c69066e450James Bottomley}
442908d778ab3e244900c310974e1fc1c69066e450James Bottomley
452908d778ab3e244900c310974e1fc1c69066e450James Bottomley/* PCI_DMA_... to our direction translation.
462908d778ab3e244900c310974e1fc1c69066e450James Bottomley */
472908d778ab3e244900c310974e1fc1c69066e450James Bottomleystatic const u8 data_dir_flags[] = {
482908d778ab3e244900c310974e1fc1c69066e450James Bottomley	[PCI_DMA_BIDIRECTIONAL] = DATA_DIR_BYRECIPIENT,	/* UNSPECIFIED */
492908d778ab3e244900c310974e1fc1c69066e450James Bottomley	[PCI_DMA_TODEVICE]      = DATA_DIR_OUT, /* OUTBOUND */
502908d778ab3e244900c310974e1fc1c69066e450James Bottomley	[PCI_DMA_FROMDEVICE]    = DATA_DIR_IN, /* INBOUND */
512908d778ab3e244900c310974e1fc1c69066e450James Bottomley	[PCI_DMA_NONE]          = DATA_DIR_NONE, /* NO TRANSFER */
522908d778ab3e244900c310974e1fc1c69066e450James Bottomley};
532908d778ab3e244900c310974e1fc1c69066e450James Bottomley
5481e56ded878aeb8730f18c1d0a70d5face788be3Adrian Bunkstatic int asd_map_scatterlist(struct sas_task *task,
5581e56ded878aeb8730f18c1d0a70d5face788be3Adrian Bunk			       struct sg_el *sg_arr,
5681e56ded878aeb8730f18c1d0a70d5face788be3Adrian Bunk			       gfp_t gfp_flags)
572908d778ab3e244900c310974e1fc1c69066e450James Bottomley{
582908d778ab3e244900c310974e1fc1c69066e450James Bottomley	struct asd_ascb *ascb = task->lldd_task;
592908d778ab3e244900c310974e1fc1c69066e450James Bottomley	struct asd_ha_struct *asd_ha = ascb->ha;
602908d778ab3e244900c310974e1fc1c69066e450James Bottomley	struct scatterlist *sc;
612908d778ab3e244900c310974e1fc1c69066e450James Bottomley	int num_sg, res;
622908d778ab3e244900c310974e1fc1c69066e450James Bottomley
632908d778ab3e244900c310974e1fc1c69066e450James Bottomley	if (task->data_dir == PCI_DMA_NONE)
642908d778ab3e244900c310974e1fc1c69066e450James Bottomley		return 0;
652908d778ab3e244900c310974e1fc1c69066e450James Bottomley
662908d778ab3e244900c310974e1fc1c69066e450James Bottomley	if (task->num_scatter == 0) {
672908d778ab3e244900c310974e1fc1c69066e450James Bottomley		void *p = task->scatter;
682908d778ab3e244900c310974e1fc1c69066e450James Bottomley		dma_addr_t dma = pci_map_single(asd_ha->pcidev, p,
692908d778ab3e244900c310974e1fc1c69066e450James Bottomley						task->total_xfer_len,
702908d778ab3e244900c310974e1fc1c69066e450James Bottomley						task->data_dir);
712908d778ab3e244900c310974e1fc1c69066e450James Bottomley		sg_arr[0].bus_addr = cpu_to_le64((u64)dma);
722908d778ab3e244900c310974e1fc1c69066e450James Bottomley		sg_arr[0].size = cpu_to_le32(task->total_xfer_len);
732908d778ab3e244900c310974e1fc1c69066e450James Bottomley		sg_arr[0].flags |= ASD_SG_EL_LIST_EOL;
742908d778ab3e244900c310974e1fc1c69066e450James Bottomley		return 0;
752908d778ab3e244900c310974e1fc1c69066e450James Bottomley	}
762908d778ab3e244900c310974e1fc1c69066e450James Bottomley
77ba330ffebb43c37cabc765c7cb0a80df01554657Darrick J. Wong	/* STP tasks come from libata which has already mapped
78ba330ffebb43c37cabc765c7cb0a80df01554657Darrick J. Wong	 * the SG list */
790f05df8b3b41bc258bdf520b72e8cf7c524048b7James Bottomley	if (sas_protocol_ata(task->task_proto))
80ba330ffebb43c37cabc765c7cb0a80df01554657Darrick J. Wong		num_sg = task->num_scatter;
81ba330ffebb43c37cabc765c7cb0a80df01554657Darrick J. Wong	else
82ba330ffebb43c37cabc765c7cb0a80df01554657Darrick J. Wong		num_sg = pci_map_sg(asd_ha->pcidev, task->scatter,
83ba330ffebb43c37cabc765c7cb0a80df01554657Darrick J. Wong				    task->num_scatter, task->data_dir);
842908d778ab3e244900c310974e1fc1c69066e450James Bottomley	if (num_sg == 0)
852908d778ab3e244900c310974e1fc1c69066e450James Bottomley		return -ENOMEM;
862908d778ab3e244900c310974e1fc1c69066e450James Bottomley
872908d778ab3e244900c310974e1fc1c69066e450James Bottomley	if (num_sg > 3) {
882908d778ab3e244900c310974e1fc1c69066e450James Bottomley		int i;
892908d778ab3e244900c310974e1fc1c69066e450James Bottomley
902908d778ab3e244900c310974e1fc1c69066e450James Bottomley		ascb->sg_arr = asd_alloc_coherent(asd_ha,
912908d778ab3e244900c310974e1fc1c69066e450James Bottomley						  num_sg*sizeof(struct sg_el),
922908d778ab3e244900c310974e1fc1c69066e450James Bottomley						  gfp_flags);
932908d778ab3e244900c310974e1fc1c69066e450James Bottomley		if (!ascb->sg_arr) {
942908d778ab3e244900c310974e1fc1c69066e450James Bottomley			res = -ENOMEM;
952908d778ab3e244900c310974e1fc1c69066e450James Bottomley			goto err_unmap;
962908d778ab3e244900c310974e1fc1c69066e450James Bottomley		}
978145bfe463f4dcd644304933e6b80460557daa44Jens Axboe		for_each_sg(task->scatter, sc, num_sg, i) {
982908d778ab3e244900c310974e1fc1c69066e450James Bottomley			struct sg_el *sg =
992908d778ab3e244900c310974e1fc1c69066e450James Bottomley				&((struct sg_el *)ascb->sg_arr->vaddr)[i];
1002908d778ab3e244900c310974e1fc1c69066e450James Bottomley			sg->bus_addr = cpu_to_le64((u64)sg_dma_address(sc));
1012908d778ab3e244900c310974e1fc1c69066e450James Bottomley			sg->size = cpu_to_le32((u32)sg_dma_len(sc));
1022908d778ab3e244900c310974e1fc1c69066e450James Bottomley			if (i == num_sg-1)
1032908d778ab3e244900c310974e1fc1c69066e450James Bottomley				sg->flags |= ASD_SG_EL_LIST_EOL;
1042908d778ab3e244900c310974e1fc1c69066e450James Bottomley		}
1052908d778ab3e244900c310974e1fc1c69066e450James Bottomley
1068145bfe463f4dcd644304933e6b80460557daa44Jens Axboe		for_each_sg(task->scatter, sc, 2, i) {
1072908d778ab3e244900c310974e1fc1c69066e450James Bottomley			sg_arr[i].bus_addr =
1082908d778ab3e244900c310974e1fc1c69066e450James Bottomley				cpu_to_le64((u64)sg_dma_address(sc));
1092908d778ab3e244900c310974e1fc1c69066e450James Bottomley			sg_arr[i].size = cpu_to_le32((u32)sg_dma_len(sc));
1102908d778ab3e244900c310974e1fc1c69066e450James Bottomley		}
1112908d778ab3e244900c310974e1fc1c69066e450James Bottomley		sg_arr[1].next_sg_offs = 2 * sizeof(*sg_arr);
1122908d778ab3e244900c310974e1fc1c69066e450James Bottomley		sg_arr[1].flags |= ASD_SG_EL_LIST_EOS;
1132908d778ab3e244900c310974e1fc1c69066e450James Bottomley
1142908d778ab3e244900c310974e1fc1c69066e450James Bottomley		memset(&sg_arr[2], 0, sizeof(*sg_arr));
1152908d778ab3e244900c310974e1fc1c69066e450James Bottomley		sg_arr[2].bus_addr=cpu_to_le64((u64)ascb->sg_arr->dma_handle);
1162908d778ab3e244900c310974e1fc1c69066e450James Bottomley	} else {
1172908d778ab3e244900c310974e1fc1c69066e450James Bottomley		int i;
1188145bfe463f4dcd644304933e6b80460557daa44Jens Axboe		for_each_sg(task->scatter, sc, num_sg, i) {
1192908d778ab3e244900c310974e1fc1c69066e450James Bottomley			sg_arr[i].bus_addr =
1202908d778ab3e244900c310974e1fc1c69066e450James Bottomley				cpu_to_le64((u64)sg_dma_address(sc));
1212908d778ab3e244900c310974e1fc1c69066e450James Bottomley			sg_arr[i].size = cpu_to_le32((u32)sg_dma_len(sc));
1222908d778ab3e244900c310974e1fc1c69066e450James Bottomley		}
1232908d778ab3e244900c310974e1fc1c69066e450James Bottomley		sg_arr[i-1].flags |= ASD_SG_EL_LIST_EOL;
1242908d778ab3e244900c310974e1fc1c69066e450James Bottomley	}
1252908d778ab3e244900c310974e1fc1c69066e450James Bottomley
1262908d778ab3e244900c310974e1fc1c69066e450James Bottomley	return 0;
1272908d778ab3e244900c310974e1fc1c69066e450James Bottomleyerr_unmap:
1280f05df8b3b41bc258bdf520b72e8cf7c524048b7James Bottomley	if (sas_protocol_ata(task->task_proto))
129ba330ffebb43c37cabc765c7cb0a80df01554657Darrick J. Wong		pci_unmap_sg(asd_ha->pcidev, task->scatter, task->num_scatter,
130ba330ffebb43c37cabc765c7cb0a80df01554657Darrick J. Wong			     task->data_dir);
1312908d778ab3e244900c310974e1fc1c69066e450James Bottomley	return res;
1322908d778ab3e244900c310974e1fc1c69066e450James Bottomley}
1332908d778ab3e244900c310974e1fc1c69066e450James Bottomley
13481e56ded878aeb8730f18c1d0a70d5face788be3Adrian Bunkstatic void asd_unmap_scatterlist(struct asd_ascb *ascb)
1352908d778ab3e244900c310974e1fc1c69066e450James Bottomley{
1362908d778ab3e244900c310974e1fc1c69066e450James Bottomley	struct asd_ha_struct *asd_ha = ascb->ha;
1372908d778ab3e244900c310974e1fc1c69066e450James Bottomley	struct sas_task *task = ascb->uldd_task;
1382908d778ab3e244900c310974e1fc1c69066e450James Bottomley
1392908d778ab3e244900c310974e1fc1c69066e450James Bottomley	if (task->data_dir == PCI_DMA_NONE)
1402908d778ab3e244900c310974e1fc1c69066e450James Bottomley		return;
1412908d778ab3e244900c310974e1fc1c69066e450James Bottomley
1422908d778ab3e244900c310974e1fc1c69066e450James Bottomley	if (task->num_scatter == 0) {
1432908d778ab3e244900c310974e1fc1c69066e450James Bottomley		dma_addr_t dma = (dma_addr_t)
1442908d778ab3e244900c310974e1fc1c69066e450James Bottomley		       le64_to_cpu(ascb->scb->ssp_task.sg_element[0].bus_addr);
1452908d778ab3e244900c310974e1fc1c69066e450James Bottomley		pci_unmap_single(ascb->ha->pcidev, dma, task->total_xfer_len,
1462908d778ab3e244900c310974e1fc1c69066e450James Bottomley				 task->data_dir);
1472908d778ab3e244900c310974e1fc1c69066e450James Bottomley		return;
1482908d778ab3e244900c310974e1fc1c69066e450James Bottomley	}
1492908d778ab3e244900c310974e1fc1c69066e450James Bottomley
1502908d778ab3e244900c310974e1fc1c69066e450James Bottomley	asd_free_coherent(asd_ha, ascb->sg_arr);
151ba330ffebb43c37cabc765c7cb0a80df01554657Darrick J. Wong	if (task->task_proto != SAS_PROTOCOL_STP)
152ba330ffebb43c37cabc765c7cb0a80df01554657Darrick J. Wong		pci_unmap_sg(asd_ha->pcidev, task->scatter, task->num_scatter,
153ba330ffebb43c37cabc765c7cb0a80df01554657Darrick J. Wong			     task->data_dir);
1542908d778ab3e244900c310974e1fc1c69066e450James Bottomley}
1552908d778ab3e244900c310974e1fc1c69066e450James Bottomley
1562908d778ab3e244900c310974e1fc1c69066e450James Bottomley/* ---------- Task complete tasklet ---------- */
1572908d778ab3e244900c310974e1fc1c69066e450James Bottomley
1582908d778ab3e244900c310974e1fc1c69066e450James Bottomleystatic void asd_get_response_tasklet(struct asd_ascb *ascb,
1592908d778ab3e244900c310974e1fc1c69066e450James Bottomley				     struct done_list_struct *dl)
1602908d778ab3e244900c310974e1fc1c69066e450James Bottomley{
1612908d778ab3e244900c310974e1fc1c69066e450James Bottomley	struct asd_ha_struct *asd_ha = ascb->ha;
1622908d778ab3e244900c310974e1fc1c69066e450James Bottomley	struct sas_task *task = ascb->uldd_task;
1632908d778ab3e244900c310974e1fc1c69066e450James Bottomley	struct task_status_struct *ts = &task->task_status;
1642908d778ab3e244900c310974e1fc1c69066e450James Bottomley	unsigned long flags;
1652908d778ab3e244900c310974e1fc1c69066e450James Bottomley	struct tc_resp_sb_struct {
1662908d778ab3e244900c310974e1fc1c69066e450James Bottomley		__le16 index_escb;
1672908d778ab3e244900c310974e1fc1c69066e450James Bottomley		u8     len_lsb;
1682908d778ab3e244900c310974e1fc1c69066e450James Bottomley		u8     flags;
1692908d778ab3e244900c310974e1fc1c69066e450James Bottomley	} __attribute__ ((packed)) *resp_sb = (void *) dl->status_block;
1702908d778ab3e244900c310974e1fc1c69066e450James Bottomley
1712908d778ab3e244900c310974e1fc1c69066e450James Bottomley/* 	int  size   = ((resp_sb->flags & 7) << 8) | resp_sb->len_lsb; */
1722908d778ab3e244900c310974e1fc1c69066e450James Bottomley	int  edb_id = ((resp_sb->flags & 0x70) >> 4)-1;
1732908d778ab3e244900c310974e1fc1c69066e450James Bottomley	struct asd_ascb *escb;
1742908d778ab3e244900c310974e1fc1c69066e450James Bottomley	struct asd_dma_tok *edb;
1752908d778ab3e244900c310974e1fc1c69066e450James Bottomley	void *r;
1762908d778ab3e244900c310974e1fc1c69066e450James Bottomley
1772908d778ab3e244900c310974e1fc1c69066e450James Bottomley	spin_lock_irqsave(&asd_ha->seq.tc_index_lock, flags);
1782908d778ab3e244900c310974e1fc1c69066e450James Bottomley	escb = asd_tc_index_find(&asd_ha->seq,
1792908d778ab3e244900c310974e1fc1c69066e450James Bottomley				 (int)le16_to_cpu(resp_sb->index_escb));
1802908d778ab3e244900c310974e1fc1c69066e450James Bottomley	spin_unlock_irqrestore(&asd_ha->seq.tc_index_lock, flags);
1812908d778ab3e244900c310974e1fc1c69066e450James Bottomley
1822908d778ab3e244900c310974e1fc1c69066e450James Bottomley	if (!escb) {
1832908d778ab3e244900c310974e1fc1c69066e450James Bottomley		ASD_DPRINTK("Uh-oh! No escb for this dl?!\n");
1842908d778ab3e244900c310974e1fc1c69066e450James Bottomley		return;
1852908d778ab3e244900c310974e1fc1c69066e450James Bottomley	}
1862908d778ab3e244900c310974e1fc1c69066e450James Bottomley
1872908d778ab3e244900c310974e1fc1c69066e450James Bottomley	ts->buf_valid_size = 0;
1882908d778ab3e244900c310974e1fc1c69066e450James Bottomley	edb = asd_ha->seq.edb_arr[edb_id + escb->edb_index];
1892908d778ab3e244900c310974e1fc1c69066e450James Bottomley	r = edb->vaddr;
1905929faf3334f4c69f3bb02be59d7c127e0cefa1fDarrick J. Wong	if (task->task_proto == SAS_PROTOCOL_SSP) {
1912908d778ab3e244900c310974e1fc1c69066e450James Bottomley		struct ssp_response_iu *iu =
1922908d778ab3e244900c310974e1fc1c69066e450James Bottomley			r + 16 + sizeof(struct ssp_frame_hdr);
1932908d778ab3e244900c310974e1fc1c69066e450James Bottomley
1942908d778ab3e244900c310974e1fc1c69066e450James Bottomley		ts->residual = le32_to_cpu(*(__le32 *)r);
195366ca51f30de1cbb5b356c70b7bb22051c558e41James Bottomley
196366ca51f30de1cbb5b356c70b7bb22051c558e41James Bottomley		sas_ssp_task_response(&asd_ha->pcidev->dev, task, iu);
1972908d778ab3e244900c310974e1fc1c69066e450James Bottomley	}  else {
1982908d778ab3e244900c310974e1fc1c69066e450James Bottomley		struct ata_task_resp *resp = (void *) &ts->buf[0];
1992908d778ab3e244900c310974e1fc1c69066e450James Bottomley
2002908d778ab3e244900c310974e1fc1c69066e450James Bottomley		ts->residual = le32_to_cpu(*(__le32 *)r);
2012908d778ab3e244900c310974e1fc1c69066e450James Bottomley
2022908d778ab3e244900c310974e1fc1c69066e450James Bottomley		if (SAS_STATUS_BUF_SIZE >= sizeof(*resp)) {
2032908d778ab3e244900c310974e1fc1c69066e450James Bottomley			resp->frame_len = le16_to_cpu(*(__le16 *)(r+6));
20476c6b958de9e72947ef08b1c9509f094c34abd8fDan Williams			memcpy(&resp->ending_fis[0], r+16, ATA_RESP_FIS_SIZE);
2052908d778ab3e244900c310974e1fc1c69066e450James Bottomley			ts->buf_valid_size = sizeof(*resp);
2062908d778ab3e244900c310974e1fc1c69066e450James Bottomley		}
2072908d778ab3e244900c310974e1fc1c69066e450James Bottomley	}
2082908d778ab3e244900c310974e1fc1c69066e450James Bottomley
2092908d778ab3e244900c310974e1fc1c69066e450James Bottomley	asd_invalidate_edb(escb, edb_id);
2102908d778ab3e244900c310974e1fc1c69066e450James Bottomley}
2112908d778ab3e244900c310974e1fc1c69066e450James Bottomley
2122908d778ab3e244900c310974e1fc1c69066e450James Bottomleystatic void asd_task_tasklet_complete(struct asd_ascb *ascb,
2132908d778ab3e244900c310974e1fc1c69066e450James Bottomley				      struct done_list_struct *dl)
2142908d778ab3e244900c310974e1fc1c69066e450James Bottomley{
2152908d778ab3e244900c310974e1fc1c69066e450James Bottomley	struct sas_task *task = ascb->uldd_task;
2162908d778ab3e244900c310974e1fc1c69066e450James Bottomley	struct task_status_struct *ts = &task->task_status;
2172908d778ab3e244900c310974e1fc1c69066e450James Bottomley	unsigned long flags;
2182908d778ab3e244900c310974e1fc1c69066e450James Bottomley	u8 opcode = dl->opcode;
2192908d778ab3e244900c310974e1fc1c69066e450James Bottomley
2202908d778ab3e244900c310974e1fc1c69066e450James Bottomley	asd_can_dequeue(ascb->ha, 1);
2212908d778ab3e244900c310974e1fc1c69066e450James Bottomley
2222908d778ab3e244900c310974e1fc1c69066e450James BottomleyAgain:
2232908d778ab3e244900c310974e1fc1c69066e450James Bottomley	switch (opcode) {
2242908d778ab3e244900c310974e1fc1c69066e450James Bottomley	case TC_NO_ERROR:
2252908d778ab3e244900c310974e1fc1c69066e450James Bottomley		ts->resp = SAS_TASK_COMPLETE;
226df64d3caab8db6ae17dacd229a03d7689a10c432James Bottomley		ts->stat = SAM_STAT_GOOD;
2272908d778ab3e244900c310974e1fc1c69066e450James Bottomley		break;
2282908d778ab3e244900c310974e1fc1c69066e450James Bottomley	case TC_UNDERRUN:
2292908d778ab3e244900c310974e1fc1c69066e450James Bottomley		ts->resp = SAS_TASK_COMPLETE;
2302908d778ab3e244900c310974e1fc1c69066e450James Bottomley		ts->stat = SAS_DATA_UNDERRUN;
2312908d778ab3e244900c310974e1fc1c69066e450James Bottomley		ts->residual = le32_to_cpu(*(__le32 *)dl->status_block);
2322908d778ab3e244900c310974e1fc1c69066e450James Bottomley		break;
2332908d778ab3e244900c310974e1fc1c69066e450James Bottomley	case TC_OVERRUN:
2342908d778ab3e244900c310974e1fc1c69066e450James Bottomley		ts->resp = SAS_TASK_COMPLETE;
2352908d778ab3e244900c310974e1fc1c69066e450James Bottomley		ts->stat = SAS_DATA_OVERRUN;
2362908d778ab3e244900c310974e1fc1c69066e450James Bottomley		ts->residual = 0;
2372908d778ab3e244900c310974e1fc1c69066e450James Bottomley		break;
2382908d778ab3e244900c310974e1fc1c69066e450James Bottomley	case TC_SSP_RESP:
2392908d778ab3e244900c310974e1fc1c69066e450James Bottomley	case TC_ATA_RESP:
2402908d778ab3e244900c310974e1fc1c69066e450James Bottomley		ts->resp = SAS_TASK_COMPLETE;
2412908d778ab3e244900c310974e1fc1c69066e450James Bottomley		ts->stat = SAS_PROTO_RESPONSE;
2422908d778ab3e244900c310974e1fc1c69066e450James Bottomley		asd_get_response_tasklet(ascb, dl);
2432908d778ab3e244900c310974e1fc1c69066e450James Bottomley		break;
2442908d778ab3e244900c310974e1fc1c69066e450James Bottomley	case TF_OPEN_REJECT:
2452908d778ab3e244900c310974e1fc1c69066e450James Bottomley		ts->resp = SAS_TASK_UNDELIVERED;
2462908d778ab3e244900c310974e1fc1c69066e450James Bottomley		ts->stat = SAS_OPEN_REJECT;
2472908d778ab3e244900c310974e1fc1c69066e450James Bottomley		if (dl->status_block[1] & 2)
2482908d778ab3e244900c310974e1fc1c69066e450James Bottomley			ts->open_rej_reason = 1 + dl->status_block[2];
2492908d778ab3e244900c310974e1fc1c69066e450James Bottomley		else if (dl->status_block[1] & 1)
2502908d778ab3e244900c310974e1fc1c69066e450James Bottomley			ts->open_rej_reason = (dl->status_block[2] >> 4)+10;
2512908d778ab3e244900c310974e1fc1c69066e450James Bottomley		else
2522908d778ab3e244900c310974e1fc1c69066e450James Bottomley			ts->open_rej_reason = SAS_OREJ_UNKNOWN;
2532908d778ab3e244900c310974e1fc1c69066e450James Bottomley		break;
2542908d778ab3e244900c310974e1fc1c69066e450James Bottomley	case TF_OPEN_TO:
2552908d778ab3e244900c310974e1fc1c69066e450James Bottomley		ts->resp = SAS_TASK_UNDELIVERED;
2562908d778ab3e244900c310974e1fc1c69066e450James Bottomley		ts->stat = SAS_OPEN_TO;
2572908d778ab3e244900c310974e1fc1c69066e450James Bottomley		break;
2582908d778ab3e244900c310974e1fc1c69066e450James Bottomley	case TF_PHY_DOWN:
2592908d778ab3e244900c310974e1fc1c69066e450James Bottomley	case TU_PHY_DOWN:
2602908d778ab3e244900c310974e1fc1c69066e450James Bottomley		ts->resp = SAS_TASK_UNDELIVERED;
2612908d778ab3e244900c310974e1fc1c69066e450James Bottomley		ts->stat = SAS_PHY_DOWN;
2622908d778ab3e244900c310974e1fc1c69066e450James Bottomley		break;
2632908d778ab3e244900c310974e1fc1c69066e450James Bottomley	case TI_PHY_DOWN:
2642908d778ab3e244900c310974e1fc1c69066e450James Bottomley		ts->resp = SAS_TASK_COMPLETE;
2652908d778ab3e244900c310974e1fc1c69066e450James Bottomley		ts->stat = SAS_PHY_DOWN;
2662908d778ab3e244900c310974e1fc1c69066e450James Bottomley		break;
2672908d778ab3e244900c310974e1fc1c69066e450James Bottomley	case TI_BREAK:
2682908d778ab3e244900c310974e1fc1c69066e450James Bottomley	case TI_PROTO_ERR:
2692908d778ab3e244900c310974e1fc1c69066e450James Bottomley	case TI_NAK:
2702908d778ab3e244900c310974e1fc1c69066e450James Bottomley	case TI_ACK_NAK_TO:
2712908d778ab3e244900c310974e1fc1c69066e450James Bottomley	case TF_SMP_XMIT_RCV_ERR:
2722908d778ab3e244900c310974e1fc1c69066e450James Bottomley	case TC_ATA_R_ERR_RECV:
2732908d778ab3e244900c310974e1fc1c69066e450James Bottomley		ts->resp = SAS_TASK_COMPLETE;
2742908d778ab3e244900c310974e1fc1c69066e450James Bottomley		ts->stat = SAS_INTERRUPTED;
2752908d778ab3e244900c310974e1fc1c69066e450James Bottomley		break;
2762908d778ab3e244900c310974e1fc1c69066e450James Bottomley	case TF_BREAK:
2772908d778ab3e244900c310974e1fc1c69066e450James Bottomley	case TU_BREAK:
2782908d778ab3e244900c310974e1fc1c69066e450James Bottomley	case TU_ACK_NAK_TO:
2792908d778ab3e244900c310974e1fc1c69066e450James Bottomley	case TF_SMPRSP_TO:
2802908d778ab3e244900c310974e1fc1c69066e450James Bottomley		ts->resp = SAS_TASK_UNDELIVERED;
2812908d778ab3e244900c310974e1fc1c69066e450James Bottomley		ts->stat = SAS_DEV_NO_RESPONSE;
2822908d778ab3e244900c310974e1fc1c69066e450James Bottomley		break;
2832908d778ab3e244900c310974e1fc1c69066e450James Bottomley	case TF_NAK_RECV:
2842908d778ab3e244900c310974e1fc1c69066e450James Bottomley		ts->resp = SAS_TASK_COMPLETE;
2852908d778ab3e244900c310974e1fc1c69066e450James Bottomley		ts->stat = SAS_NAK_R_ERR;
2862908d778ab3e244900c310974e1fc1c69066e450James Bottomley		break;
2872908d778ab3e244900c310974e1fc1c69066e450James Bottomley	case TA_I_T_NEXUS_LOSS:
2882908d778ab3e244900c310974e1fc1c69066e450James Bottomley		opcode = dl->status_block[0];
2892908d778ab3e244900c310974e1fc1c69066e450James Bottomley		goto Again;
2902908d778ab3e244900c310974e1fc1c69066e450James Bottomley		break;
2912908d778ab3e244900c310974e1fc1c69066e450James Bottomley	case TF_INV_CONN_HANDLE:
2922908d778ab3e244900c310974e1fc1c69066e450James Bottomley		ts->resp = SAS_TASK_UNDELIVERED;
2932908d778ab3e244900c310974e1fc1c69066e450James Bottomley		ts->stat = SAS_DEVICE_UNKNOWN;
2942908d778ab3e244900c310974e1fc1c69066e450James Bottomley		break;
2952908d778ab3e244900c310974e1fc1c69066e450James Bottomley	case TF_REQUESTED_N_PENDING:
2962908d778ab3e244900c310974e1fc1c69066e450James Bottomley		ts->resp = SAS_TASK_UNDELIVERED;
2972908d778ab3e244900c310974e1fc1c69066e450James Bottomley		ts->stat = SAS_PENDING;
2982908d778ab3e244900c310974e1fc1c69066e450James Bottomley		break;
2992908d778ab3e244900c310974e1fc1c69066e450James Bottomley	case TC_TASK_CLEARED:
3002908d778ab3e244900c310974e1fc1c69066e450James Bottomley	case TA_ON_REQ:
3012908d778ab3e244900c310974e1fc1c69066e450James Bottomley		ts->resp = SAS_TASK_COMPLETE;
3022908d778ab3e244900c310974e1fc1c69066e450James Bottomley		ts->stat = SAS_ABORTED_TASK;
3032908d778ab3e244900c310974e1fc1c69066e450James Bottomley		break;
3042908d778ab3e244900c310974e1fc1c69066e450James Bottomley
3052908d778ab3e244900c310974e1fc1c69066e450James Bottomley	case TF_NO_SMP_CONN:
3062908d778ab3e244900c310974e1fc1c69066e450James Bottomley	case TF_TMF_NO_CTX:
3072908d778ab3e244900c310974e1fc1c69066e450James Bottomley	case TF_TMF_NO_TAG:
3082908d778ab3e244900c310974e1fc1c69066e450James Bottomley	case TF_TMF_TAG_FREE:
3092908d778ab3e244900c310974e1fc1c69066e450James Bottomley	case TF_TMF_TASK_DONE:
3102908d778ab3e244900c310974e1fc1c69066e450James Bottomley	case TF_TMF_NO_CONN_HANDLE:
3112908d778ab3e244900c310974e1fc1c69066e450James Bottomley	case TF_IRTT_TO:
3122908d778ab3e244900c310974e1fc1c69066e450James Bottomley	case TF_IU_SHORT:
3132908d778ab3e244900c310974e1fc1c69066e450James Bottomley	case TF_DATA_OFFS_ERR:
3142908d778ab3e244900c310974e1fc1c69066e450James Bottomley		ts->resp = SAS_TASK_UNDELIVERED;
3152908d778ab3e244900c310974e1fc1c69066e450James Bottomley		ts->stat = SAS_DEV_NO_RESPONSE;
3162908d778ab3e244900c310974e1fc1c69066e450James Bottomley		break;
3172908d778ab3e244900c310974e1fc1c69066e450James Bottomley
3182908d778ab3e244900c310974e1fc1c69066e450James Bottomley	case TC_LINK_ADM_RESP:
3192908d778ab3e244900c310974e1fc1c69066e450James Bottomley	case TC_CONTROL_PHY:
3202908d778ab3e244900c310974e1fc1c69066e450James Bottomley	case TC_RESUME:
3212908d778ab3e244900c310974e1fc1c69066e450James Bottomley	case TC_PARTIAL_SG_LIST:
3222908d778ab3e244900c310974e1fc1c69066e450James Bottomley	default:
323cadbd4a5e36dde7e6c49b587b2c419103c0b7218Harvey Harrison		ASD_DPRINTK("%s: dl opcode: 0x%x?\n", __func__, opcode);
3242908d778ab3e244900c310974e1fc1c69066e450James Bottomley		break;
3252908d778ab3e244900c310974e1fc1c69066e450James Bottomley	}
3262908d778ab3e244900c310974e1fc1c69066e450James Bottomley
3272908d778ab3e244900c310974e1fc1c69066e450James Bottomley	switch (task->task_proto) {
3285929faf3334f4c69f3bb02be59d7c127e0cefa1fDarrick J. Wong	case SAS_PROTOCOL_SATA:
3295929faf3334f4c69f3bb02be59d7c127e0cefa1fDarrick J. Wong	case SAS_PROTOCOL_STP:
3302908d778ab3e244900c310974e1fc1c69066e450James Bottomley		asd_unbuild_ata_ascb(ascb);
3312908d778ab3e244900c310974e1fc1c69066e450James Bottomley		break;
3325929faf3334f4c69f3bb02be59d7c127e0cefa1fDarrick J. Wong	case SAS_PROTOCOL_SMP:
3332908d778ab3e244900c310974e1fc1c69066e450James Bottomley		asd_unbuild_smp_ascb(ascb);
3342908d778ab3e244900c310974e1fc1c69066e450James Bottomley		break;
3355929faf3334f4c69f3bb02be59d7c127e0cefa1fDarrick J. Wong	case SAS_PROTOCOL_SSP:
3362908d778ab3e244900c310974e1fc1c69066e450James Bottomley		asd_unbuild_ssp_ascb(ascb);
3372908d778ab3e244900c310974e1fc1c69066e450James Bottomley	default:
3382908d778ab3e244900c310974e1fc1c69066e450James Bottomley		break;
3392908d778ab3e244900c310974e1fc1c69066e450James Bottomley	}
3402908d778ab3e244900c310974e1fc1c69066e450James Bottomley
3412908d778ab3e244900c310974e1fc1c69066e450James Bottomley	spin_lock_irqsave(&task->task_state_lock, flags);
3422908d778ab3e244900c310974e1fc1c69066e450James Bottomley	task->task_state_flags &= ~SAS_TASK_STATE_PENDING;
343b218a0d8e250e0ae8fd4d4e45bd66a588b380752Darrick J. Wong	task->task_state_flags &= ~SAS_TASK_AT_INITIATOR;
3442908d778ab3e244900c310974e1fc1c69066e450James Bottomley	task->task_state_flags |= SAS_TASK_STATE_DONE;
3452908d778ab3e244900c310974e1fc1c69066e450James Bottomley	if (unlikely((task->task_state_flags & SAS_TASK_STATE_ABORTED))) {
346e2396f1e4ecd438a15fa653a028b93e95013caa3James Bottomley		struct completion *completion = ascb->completion;
3472908d778ab3e244900c310974e1fc1c69066e450James Bottomley		spin_unlock_irqrestore(&task->task_state_lock, flags);
3482908d778ab3e244900c310974e1fc1c69066e450James Bottomley		ASD_DPRINTK("task 0x%p done with opcode 0x%x resp 0x%x "
3492908d778ab3e244900c310974e1fc1c69066e450James Bottomley			    "stat 0x%x but aborted by upper layer!\n",
3502908d778ab3e244900c310974e1fc1c69066e450James Bottomley			    task, opcode, ts->resp, ts->stat);
351e2396f1e4ecd438a15fa653a028b93e95013caa3James Bottomley		if (completion)
352e2396f1e4ecd438a15fa653a028b93e95013caa3James Bottomley			complete(completion);
3532908d778ab3e244900c310974e1fc1c69066e450James Bottomley	} else {
3542908d778ab3e244900c310974e1fc1c69066e450James Bottomley		spin_unlock_irqrestore(&task->task_state_lock, flags);
3552908d778ab3e244900c310974e1fc1c69066e450James Bottomley		task->lldd_task = NULL;
3562908d778ab3e244900c310974e1fc1c69066e450James Bottomley		asd_ascb_free(ascb);
3572908d778ab3e244900c310974e1fc1c69066e450James Bottomley		mb();
3582908d778ab3e244900c310974e1fc1c69066e450James Bottomley		task->task_done(task);
3592908d778ab3e244900c310974e1fc1c69066e450James Bottomley	}
3602908d778ab3e244900c310974e1fc1c69066e450James Bottomley}
3612908d778ab3e244900c310974e1fc1c69066e450James Bottomley
3622908d778ab3e244900c310974e1fc1c69066e450James Bottomley/* ---------- ATA ---------- */
3632908d778ab3e244900c310974e1fc1c69066e450James Bottomley
3642908d778ab3e244900c310974e1fc1c69066e450James Bottomleystatic int asd_build_ata_ascb(struct asd_ascb *ascb, struct sas_task *task,
3653cc27547d6ee2d50ecdd11e9127bc3cd1947e8ddAl Viro			      gfp_t gfp_flags)
3662908d778ab3e244900c310974e1fc1c69066e450James Bottomley{
3672908d778ab3e244900c310974e1fc1c69066e450James Bottomley	struct domain_device *dev = task->dev;
3682908d778ab3e244900c310974e1fc1c69066e450James Bottomley	struct scb *scb;
3692908d778ab3e244900c310974e1fc1c69066e450James Bottomley	u8     flags;
3702908d778ab3e244900c310974e1fc1c69066e450James Bottomley	int    res = 0;
3712908d778ab3e244900c310974e1fc1c69066e450James Bottomley
3722908d778ab3e244900c310974e1fc1c69066e450James Bottomley	scb = ascb->scb;
3732908d778ab3e244900c310974e1fc1c69066e450James Bottomley
3742908d778ab3e244900c310974e1fc1c69066e450James Bottomley	if (unlikely(task->ata_task.device_control_reg_update))
3752908d778ab3e244900c310974e1fc1c69066e450James Bottomley		scb->header.opcode = CONTROL_ATA_DEV;
3762908d778ab3e244900c310974e1fc1c69066e450James Bottomley	else if (dev->sata_dev.command_set == ATA_COMMAND_SET)
3772908d778ab3e244900c310974e1fc1c69066e450James Bottomley		scb->header.opcode = INITIATE_ATA_TASK;
3782908d778ab3e244900c310974e1fc1c69066e450James Bottomley	else
3792908d778ab3e244900c310974e1fc1c69066e450James Bottomley		scb->header.opcode = INITIATE_ATAPI_TASK;
3802908d778ab3e244900c310974e1fc1c69066e450James Bottomley
3812908d778ab3e244900c310974e1fc1c69066e450James Bottomley	scb->ata_task.proto_conn_rate = (1 << 5); /* STP */
3822908d778ab3e244900c310974e1fc1c69066e450James Bottomley	if (dev->port->oob_mode == SAS_OOB_MODE)
3832908d778ab3e244900c310974e1fc1c69066e450James Bottomley		scb->ata_task.proto_conn_rate |= dev->linkrate;
3842908d778ab3e244900c310974e1fc1c69066e450James Bottomley
3852908d778ab3e244900c310974e1fc1c69066e450James Bottomley	scb->ata_task.total_xfer_len = cpu_to_le32(task->total_xfer_len);
3862908d778ab3e244900c310974e1fc1c69066e450James Bottomley	scb->ata_task.fis = task->ata_task.fis;
3872908d778ab3e244900c310974e1fc1c69066e450James Bottomley	if (likely(!task->ata_task.device_control_reg_update))
3882908d778ab3e244900c310974e1fc1c69066e450James Bottomley		scb->ata_task.fis.flags |= 0x80; /* C=1: update ATA cmd reg */
3892908d778ab3e244900c310974e1fc1c69066e450James Bottomley	scb->ata_task.fis.flags &= 0xF0; /* PM_PORT field shall be 0 */
3902908d778ab3e244900c310974e1fc1c69066e450James Bottomley	if (dev->sata_dev.command_set == ATAPI_COMMAND_SET)
3912908d778ab3e244900c310974e1fc1c69066e450James Bottomley		memcpy(scb->ata_task.atapi_packet, task->ata_task.atapi_packet,
3922908d778ab3e244900c310974e1fc1c69066e450James Bottomley		       16);
3932908d778ab3e244900c310974e1fc1c69066e450James Bottomley	scb->ata_task.sister_scb = cpu_to_le16(0xFFFF);
3942908d778ab3e244900c310974e1fc1c69066e450James Bottomley	scb->ata_task.conn_handle = cpu_to_le16(
3952908d778ab3e244900c310974e1fc1c69066e450James Bottomley		(u16)(unsigned long)dev->lldd_dev);
3962908d778ab3e244900c310974e1fc1c69066e450James Bottomley
3972908d778ab3e244900c310974e1fc1c69066e450James Bottomley	if (likely(!task->ata_task.device_control_reg_update)) {
3982908d778ab3e244900c310974e1fc1c69066e450James Bottomley		flags = 0;
3992908d778ab3e244900c310974e1fc1c69066e450James Bottomley		if (task->ata_task.dma_xfer)
4002908d778ab3e244900c310974e1fc1c69066e450James Bottomley			flags |= DATA_XFER_MODE_DMA;
4012908d778ab3e244900c310974e1fc1c69066e450James Bottomley		if (task->ata_task.use_ncq &&
4022908d778ab3e244900c310974e1fc1c69066e450James Bottomley		    dev->sata_dev.command_set != ATAPI_COMMAND_SET)
4032908d778ab3e244900c310974e1fc1c69066e450James Bottomley			flags |= ATA_Q_TYPE_NCQ;
4042908d778ab3e244900c310974e1fc1c69066e450James Bottomley		flags |= data_dir_flags[task->data_dir];
4052908d778ab3e244900c310974e1fc1c69066e450James Bottomley		scb->ata_task.ata_flags = flags;
4062908d778ab3e244900c310974e1fc1c69066e450James Bottomley
4072908d778ab3e244900c310974e1fc1c69066e450James Bottomley		scb->ata_task.retry_count = task->ata_task.retry_count;
4082908d778ab3e244900c310974e1fc1c69066e450James Bottomley
4092908d778ab3e244900c310974e1fc1c69066e450James Bottomley		flags = 0;
4102908d778ab3e244900c310974e1fc1c69066e450James Bottomley		if (task->ata_task.set_affil_pol)
4112908d778ab3e244900c310974e1fc1c69066e450James Bottomley			flags |= SET_AFFIL_POLICY;
4122908d778ab3e244900c310974e1fc1c69066e450James Bottomley		if (task->ata_task.stp_affil_pol)
4132908d778ab3e244900c310974e1fc1c69066e450James Bottomley			flags |= STP_AFFIL_POLICY;
4142908d778ab3e244900c310974e1fc1c69066e450James Bottomley		scb->ata_task.flags = flags;
4152908d778ab3e244900c310974e1fc1c69066e450James Bottomley	}
4162908d778ab3e244900c310974e1fc1c69066e450James Bottomley	ascb->tasklet_complete = asd_task_tasklet_complete;
4172908d778ab3e244900c310974e1fc1c69066e450James Bottomley
4182908d778ab3e244900c310974e1fc1c69066e450James Bottomley	if (likely(!task->ata_task.device_control_reg_update))
4192908d778ab3e244900c310974e1fc1c69066e450James Bottomley		res = asd_map_scatterlist(task, scb->ata_task.sg_element,
4202908d778ab3e244900c310974e1fc1c69066e450James Bottomley					  gfp_flags);
4212908d778ab3e244900c310974e1fc1c69066e450James Bottomley
4222908d778ab3e244900c310974e1fc1c69066e450James Bottomley	return res;
4232908d778ab3e244900c310974e1fc1c69066e450James Bottomley}
4242908d778ab3e244900c310974e1fc1c69066e450James Bottomley
4252908d778ab3e244900c310974e1fc1c69066e450James Bottomleystatic void asd_unbuild_ata_ascb(struct asd_ascb *a)
4262908d778ab3e244900c310974e1fc1c69066e450James Bottomley{
4272908d778ab3e244900c310974e1fc1c69066e450James Bottomley	asd_unmap_scatterlist(a);
4282908d778ab3e244900c310974e1fc1c69066e450James Bottomley}
4292908d778ab3e244900c310974e1fc1c69066e450James Bottomley
4302908d778ab3e244900c310974e1fc1c69066e450James Bottomley/* ---------- SMP ---------- */
4312908d778ab3e244900c310974e1fc1c69066e450James Bottomley
4322908d778ab3e244900c310974e1fc1c69066e450James Bottomleystatic int asd_build_smp_ascb(struct asd_ascb *ascb, struct sas_task *task,
4333cc27547d6ee2d50ecdd11e9127bc3cd1947e8ddAl Viro			      gfp_t gfp_flags)
4342908d778ab3e244900c310974e1fc1c69066e450James Bottomley{
4352908d778ab3e244900c310974e1fc1c69066e450James Bottomley	struct asd_ha_struct *asd_ha = ascb->ha;
4362908d778ab3e244900c310974e1fc1c69066e450James Bottomley	struct domain_device *dev = task->dev;
4372908d778ab3e244900c310974e1fc1c69066e450James Bottomley	struct scb *scb;
4382908d778ab3e244900c310974e1fc1c69066e450James Bottomley
4392908d778ab3e244900c310974e1fc1c69066e450James Bottomley	pci_map_sg(asd_ha->pcidev, &task->smp_task.smp_req, 1,
440d136552e8beadcf5e59089292e2ba44f09e3aad8Jeff Garzik		   PCI_DMA_TODEVICE);
4412908d778ab3e244900c310974e1fc1c69066e450James Bottomley	pci_map_sg(asd_ha->pcidev, &task->smp_task.smp_resp, 1,
4422908d778ab3e244900c310974e1fc1c69066e450James Bottomley		   PCI_DMA_FROMDEVICE);
4432908d778ab3e244900c310974e1fc1c69066e450James Bottomley
4442908d778ab3e244900c310974e1fc1c69066e450James Bottomley	scb = ascb->scb;
4452908d778ab3e244900c310974e1fc1c69066e450James Bottomley
4462908d778ab3e244900c310974e1fc1c69066e450James Bottomley	scb->header.opcode = INITIATE_SMP_TASK;
4472908d778ab3e244900c310974e1fc1c69066e450James Bottomley
4482908d778ab3e244900c310974e1fc1c69066e450James Bottomley	scb->smp_task.proto_conn_rate = dev->linkrate;
4492908d778ab3e244900c310974e1fc1c69066e450James Bottomley
4502908d778ab3e244900c310974e1fc1c69066e450James Bottomley	scb->smp_task.smp_req.bus_addr =
4512908d778ab3e244900c310974e1fc1c69066e450James Bottomley		cpu_to_le64((u64)sg_dma_address(&task->smp_task.smp_req));
4522908d778ab3e244900c310974e1fc1c69066e450James Bottomley	scb->smp_task.smp_req.size =
4532908d778ab3e244900c310974e1fc1c69066e450James Bottomley		cpu_to_le32((u32)sg_dma_len(&task->smp_task.smp_req)-4);
4542908d778ab3e244900c310974e1fc1c69066e450James Bottomley
4552908d778ab3e244900c310974e1fc1c69066e450James Bottomley	scb->smp_task.smp_resp.bus_addr =
4562908d778ab3e244900c310974e1fc1c69066e450James Bottomley		cpu_to_le64((u64)sg_dma_address(&task->smp_task.smp_resp));
4572908d778ab3e244900c310974e1fc1c69066e450James Bottomley	scb->smp_task.smp_resp.size =
4582908d778ab3e244900c310974e1fc1c69066e450James Bottomley		cpu_to_le32((u32)sg_dma_len(&task->smp_task.smp_resp)-4);
4592908d778ab3e244900c310974e1fc1c69066e450James Bottomley
4602908d778ab3e244900c310974e1fc1c69066e450James Bottomley	scb->smp_task.sister_scb = cpu_to_le16(0xFFFF);
4612908d778ab3e244900c310974e1fc1c69066e450James Bottomley	scb->smp_task.conn_handle = cpu_to_le16((u16)
4622908d778ab3e244900c310974e1fc1c69066e450James Bottomley						(unsigned long)dev->lldd_dev);
4632908d778ab3e244900c310974e1fc1c69066e450James Bottomley
4642908d778ab3e244900c310974e1fc1c69066e450James Bottomley	ascb->tasklet_complete = asd_task_tasklet_complete;
4652908d778ab3e244900c310974e1fc1c69066e450James Bottomley
4662908d778ab3e244900c310974e1fc1c69066e450James Bottomley	return 0;
4672908d778ab3e244900c310974e1fc1c69066e450James Bottomley}
4682908d778ab3e244900c310974e1fc1c69066e450James Bottomley
4692908d778ab3e244900c310974e1fc1c69066e450James Bottomleystatic void asd_unbuild_smp_ascb(struct asd_ascb *a)
4702908d778ab3e244900c310974e1fc1c69066e450James Bottomley{
4712908d778ab3e244900c310974e1fc1c69066e450James Bottomley	struct sas_task *task = a->uldd_task;
4722908d778ab3e244900c310974e1fc1c69066e450James Bottomley
4732908d778ab3e244900c310974e1fc1c69066e450James Bottomley	BUG_ON(!task);
4742908d778ab3e244900c310974e1fc1c69066e450James Bottomley	pci_unmap_sg(a->ha->pcidev, &task->smp_task.smp_req, 1,
475d136552e8beadcf5e59089292e2ba44f09e3aad8Jeff Garzik		     PCI_DMA_TODEVICE);
4762908d778ab3e244900c310974e1fc1c69066e450James Bottomley	pci_unmap_sg(a->ha->pcidev, &task->smp_task.smp_resp, 1,
4772908d778ab3e244900c310974e1fc1c69066e450James Bottomley		     PCI_DMA_FROMDEVICE);
4782908d778ab3e244900c310974e1fc1c69066e450James Bottomley}
4792908d778ab3e244900c310974e1fc1c69066e450James Bottomley
4802908d778ab3e244900c310974e1fc1c69066e450James Bottomley/* ---------- SSP ---------- */
4812908d778ab3e244900c310974e1fc1c69066e450James Bottomley
4822908d778ab3e244900c310974e1fc1c69066e450James Bottomleystatic int asd_build_ssp_ascb(struct asd_ascb *ascb, struct sas_task *task,
4833cc27547d6ee2d50ecdd11e9127bc3cd1947e8ddAl Viro			      gfp_t gfp_flags)
4842908d778ab3e244900c310974e1fc1c69066e450James Bottomley{
4852908d778ab3e244900c310974e1fc1c69066e450James Bottomley	struct domain_device *dev = task->dev;
4862908d778ab3e244900c310974e1fc1c69066e450James Bottomley	struct scb *scb;
4872908d778ab3e244900c310974e1fc1c69066e450James Bottomley	int    res = 0;
4882908d778ab3e244900c310974e1fc1c69066e450James Bottomley
4892908d778ab3e244900c310974e1fc1c69066e450James Bottomley	scb = ascb->scb;
4902908d778ab3e244900c310974e1fc1c69066e450James Bottomley
4912908d778ab3e244900c310974e1fc1c69066e450James Bottomley	scb->header.opcode = INITIATE_SSP_TASK;
4922908d778ab3e244900c310974e1fc1c69066e450James Bottomley
4932908d778ab3e244900c310974e1fc1c69066e450James Bottomley	scb->ssp_task.proto_conn_rate  = (1 << 4); /* SSP */
4942908d778ab3e244900c310974e1fc1c69066e450James Bottomley	scb->ssp_task.proto_conn_rate |= dev->linkrate;
4952908d778ab3e244900c310974e1fc1c69066e450James Bottomley	scb->ssp_task.total_xfer_len = cpu_to_le32(task->total_xfer_len);
4962908d778ab3e244900c310974e1fc1c69066e450James Bottomley	scb->ssp_task.ssp_frame.frame_type = SSP_DATA;
4972908d778ab3e244900c310974e1fc1c69066e450James Bottomley	memcpy(scb->ssp_task.ssp_frame.hashed_dest_addr, dev->hashed_sas_addr,
4982908d778ab3e244900c310974e1fc1c69066e450James Bottomley	       HASHED_SAS_ADDR_SIZE);
4992908d778ab3e244900c310974e1fc1c69066e450James Bottomley	memcpy(scb->ssp_task.ssp_frame.hashed_src_addr,
5002908d778ab3e244900c310974e1fc1c69066e450James Bottomley	       dev->port->ha->hashed_sas_addr, HASHED_SAS_ADDR_SIZE);
5012908d778ab3e244900c310974e1fc1c69066e450James Bottomley	scb->ssp_task.ssp_frame.tptt = cpu_to_be16(0xFFFF);
5022908d778ab3e244900c310974e1fc1c69066e450James Bottomley
5032908d778ab3e244900c310974e1fc1c69066e450James Bottomley	memcpy(scb->ssp_task.ssp_cmd.lun, task->ssp_task.LUN, 8);
5042908d778ab3e244900c310974e1fc1c69066e450James Bottomley	if (task->ssp_task.enable_first_burst)
5052908d778ab3e244900c310974e1fc1c69066e450James Bottomley		scb->ssp_task.ssp_cmd.efb_prio_attr |= EFB_MASK;
5062908d778ab3e244900c310974e1fc1c69066e450James Bottomley	scb->ssp_task.ssp_cmd.efb_prio_attr |= (task->ssp_task.task_prio << 3);
5072908d778ab3e244900c310974e1fc1c69066e450James Bottomley	scb->ssp_task.ssp_cmd.efb_prio_attr |= (task->ssp_task.task_attr & 7);
5082908d778ab3e244900c310974e1fc1c69066e450James Bottomley	memcpy(scb->ssp_task.ssp_cmd.cdb, task->ssp_task.cdb, 16);
5092908d778ab3e244900c310974e1fc1c69066e450James Bottomley
5102908d778ab3e244900c310974e1fc1c69066e450James Bottomley	scb->ssp_task.sister_scb = cpu_to_le16(0xFFFF);
5112908d778ab3e244900c310974e1fc1c69066e450James Bottomley	scb->ssp_task.conn_handle = cpu_to_le16(
5122908d778ab3e244900c310974e1fc1c69066e450James Bottomley		(u16)(unsigned long)dev->lldd_dev);
5132908d778ab3e244900c310974e1fc1c69066e450James Bottomley	scb->ssp_task.data_dir = data_dir_flags[task->data_dir];
5142908d778ab3e244900c310974e1fc1c69066e450James Bottomley	scb->ssp_task.retry_count = scb->ssp_task.retry_count;
5152908d778ab3e244900c310974e1fc1c69066e450James Bottomley
5162908d778ab3e244900c310974e1fc1c69066e450James Bottomley	ascb->tasklet_complete = asd_task_tasklet_complete;
5172908d778ab3e244900c310974e1fc1c69066e450James Bottomley
5182908d778ab3e244900c310974e1fc1c69066e450James Bottomley	res = asd_map_scatterlist(task, scb->ssp_task.sg_element, gfp_flags);
5192908d778ab3e244900c310974e1fc1c69066e450James Bottomley
5202908d778ab3e244900c310974e1fc1c69066e450James Bottomley	return res;
5212908d778ab3e244900c310974e1fc1c69066e450James Bottomley}
5222908d778ab3e244900c310974e1fc1c69066e450James Bottomley
5232908d778ab3e244900c310974e1fc1c69066e450James Bottomleystatic void asd_unbuild_ssp_ascb(struct asd_ascb *a)
5242908d778ab3e244900c310974e1fc1c69066e450James Bottomley{
5252908d778ab3e244900c310974e1fc1c69066e450James Bottomley	asd_unmap_scatterlist(a);
5262908d778ab3e244900c310974e1fc1c69066e450James Bottomley}
5272908d778ab3e244900c310974e1fc1c69066e450James Bottomley
5282908d778ab3e244900c310974e1fc1c69066e450James Bottomley/* ---------- Execute Task ---------- */
5292908d778ab3e244900c310974e1fc1c69066e450James Bottomley
53081e56ded878aeb8730f18c1d0a70d5face788be3Adrian Bunkstatic int asd_can_queue(struct asd_ha_struct *asd_ha, int num)
5312908d778ab3e244900c310974e1fc1c69066e450James Bottomley{
5322908d778ab3e244900c310974e1fc1c69066e450James Bottomley	int res = 0;
5332908d778ab3e244900c310974e1fc1c69066e450James Bottomley	unsigned long flags;
5342908d778ab3e244900c310974e1fc1c69066e450James Bottomley
5352908d778ab3e244900c310974e1fc1c69066e450James Bottomley	spin_lock_irqsave(&asd_ha->seq.pend_q_lock, flags);
5362908d778ab3e244900c310974e1fc1c69066e450James Bottomley	if ((asd_ha->seq.can_queue - num) < 0)
5372908d778ab3e244900c310974e1fc1c69066e450James Bottomley		res = -SAS_QUEUE_FULL;
5382908d778ab3e244900c310974e1fc1c69066e450James Bottomley	else
5392908d778ab3e244900c310974e1fc1c69066e450James Bottomley		asd_ha->seq.can_queue -= num;
5402908d778ab3e244900c310974e1fc1c69066e450James Bottomley	spin_unlock_irqrestore(&asd_ha->seq.pend_q_lock, flags);
5412908d778ab3e244900c310974e1fc1c69066e450James Bottomley
5422908d778ab3e244900c310974e1fc1c69066e450James Bottomley	return res;
5432908d778ab3e244900c310974e1fc1c69066e450James Bottomley}
5442908d778ab3e244900c310974e1fc1c69066e450James Bottomley
5452908d778ab3e244900c310974e1fc1c69066e450James Bottomleyint asd_execute_task(struct sas_task *task, const int num,
5463cc27547d6ee2d50ecdd11e9127bc3cd1947e8ddAl Viro		     gfp_t gfp_flags)
5472908d778ab3e244900c310974e1fc1c69066e450James Bottomley{
5482908d778ab3e244900c310974e1fc1c69066e450James Bottomley	int res = 0;
5492908d778ab3e244900c310974e1fc1c69066e450James Bottomley	LIST_HEAD(alist);
5502908d778ab3e244900c310974e1fc1c69066e450James Bottomley	struct sas_task *t = task;
5512908d778ab3e244900c310974e1fc1c69066e450James Bottomley	struct asd_ascb *ascb = NULL, *a;
5522908d778ab3e244900c310974e1fc1c69066e450James Bottomley	struct asd_ha_struct *asd_ha = task->dev->port->ha->lldd_ha;
553b218a0d8e250e0ae8fd4d4e45bd66a588b380752Darrick J. Wong	unsigned long flags;
5542908d778ab3e244900c310974e1fc1c69066e450James Bottomley
5552908d778ab3e244900c310974e1fc1c69066e450James Bottomley	res = asd_can_queue(asd_ha, num);
5562908d778ab3e244900c310974e1fc1c69066e450James Bottomley	if (res)
5572908d778ab3e244900c310974e1fc1c69066e450James Bottomley		return res;
5582908d778ab3e244900c310974e1fc1c69066e450James Bottomley
5592908d778ab3e244900c310974e1fc1c69066e450James Bottomley	res = num;
5602908d778ab3e244900c310974e1fc1c69066e450James Bottomley	ascb = asd_ascb_alloc_list(asd_ha, &res, gfp_flags);
5612908d778ab3e244900c310974e1fc1c69066e450James Bottomley	if (res) {
5622908d778ab3e244900c310974e1fc1c69066e450James Bottomley		res = -ENOMEM;
5632908d778ab3e244900c310974e1fc1c69066e450James Bottomley		goto out_err;
5642908d778ab3e244900c310974e1fc1c69066e450James Bottomley	}
5652908d778ab3e244900c310974e1fc1c69066e450James Bottomley
5662908d778ab3e244900c310974e1fc1c69066e450James Bottomley	__list_add(&alist, ascb->list.prev, &ascb->list);
5672908d778ab3e244900c310974e1fc1c69066e450James Bottomley	list_for_each_entry(a, &alist, list) {
5682908d778ab3e244900c310974e1fc1c69066e450James Bottomley		a->uldd_task = t;
5692908d778ab3e244900c310974e1fc1c69066e450James Bottomley		t->lldd_task = a;
5702908d778ab3e244900c310974e1fc1c69066e450James Bottomley		t = list_entry(t->list.next, struct sas_task, list);
5712908d778ab3e244900c310974e1fc1c69066e450James Bottomley	}
5722908d778ab3e244900c310974e1fc1c69066e450James Bottomley	list_for_each_entry(a, &alist, list) {
5732908d778ab3e244900c310974e1fc1c69066e450James Bottomley		t = a->uldd_task;
5742908d778ab3e244900c310974e1fc1c69066e450James Bottomley		a->uldd_timer = 1;
5755929faf3334f4c69f3bb02be59d7c127e0cefa1fDarrick J. Wong		if (t->task_proto & SAS_PROTOCOL_STP)
5765929faf3334f4c69f3bb02be59d7c127e0cefa1fDarrick J. Wong			t->task_proto = SAS_PROTOCOL_STP;
5772908d778ab3e244900c310974e1fc1c69066e450James Bottomley		switch (t->task_proto) {
5785929faf3334f4c69f3bb02be59d7c127e0cefa1fDarrick J. Wong		case SAS_PROTOCOL_SATA:
5795929faf3334f4c69f3bb02be59d7c127e0cefa1fDarrick J. Wong		case SAS_PROTOCOL_STP:
5802908d778ab3e244900c310974e1fc1c69066e450James Bottomley			res = asd_build_ata_ascb(a, t, gfp_flags);
5812908d778ab3e244900c310974e1fc1c69066e450James Bottomley			break;
5825929faf3334f4c69f3bb02be59d7c127e0cefa1fDarrick J. Wong		case SAS_PROTOCOL_SMP:
5832908d778ab3e244900c310974e1fc1c69066e450James Bottomley			res = asd_build_smp_ascb(a, t, gfp_flags);
5842908d778ab3e244900c310974e1fc1c69066e450James Bottomley			break;
5855929faf3334f4c69f3bb02be59d7c127e0cefa1fDarrick J. Wong		case SAS_PROTOCOL_SSP:
5862908d778ab3e244900c310974e1fc1c69066e450James Bottomley			res = asd_build_ssp_ascb(a, t, gfp_flags);
5872908d778ab3e244900c310974e1fc1c69066e450James Bottomley			break;
5882908d778ab3e244900c310974e1fc1c69066e450James Bottomley		default:
5892908d778ab3e244900c310974e1fc1c69066e450James Bottomley			asd_printk("unknown sas_task proto: 0x%x\n",
5902908d778ab3e244900c310974e1fc1c69066e450James Bottomley				   t->task_proto);
5912908d778ab3e244900c310974e1fc1c69066e450James Bottomley			res = -ENOMEM;
5922908d778ab3e244900c310974e1fc1c69066e450James Bottomley			break;
5932908d778ab3e244900c310974e1fc1c69066e450James Bottomley		}
5942908d778ab3e244900c310974e1fc1c69066e450James Bottomley		if (res)
5952908d778ab3e244900c310974e1fc1c69066e450James Bottomley			goto out_err_unmap;
596b218a0d8e250e0ae8fd4d4e45bd66a588b380752Darrick J. Wong
597b218a0d8e250e0ae8fd4d4e45bd66a588b380752Darrick J. Wong		spin_lock_irqsave(&t->task_state_lock, flags);
598b218a0d8e250e0ae8fd4d4e45bd66a588b380752Darrick J. Wong		t->task_state_flags |= SAS_TASK_AT_INITIATOR;
599b218a0d8e250e0ae8fd4d4e45bd66a588b380752Darrick J. Wong		spin_unlock_irqrestore(&t->task_state_lock, flags);
6002908d778ab3e244900c310974e1fc1c69066e450James Bottomley	}
6012908d778ab3e244900c310974e1fc1c69066e450James Bottomley	list_del_init(&alist);
6022908d778ab3e244900c310974e1fc1c69066e450James Bottomley
6032908d778ab3e244900c310974e1fc1c69066e450James Bottomley	res = asd_post_ascb_list(asd_ha, ascb, num);
6042908d778ab3e244900c310974e1fc1c69066e450James Bottomley	if (unlikely(res)) {
6052908d778ab3e244900c310974e1fc1c69066e450James Bottomley		a = NULL;
6062908d778ab3e244900c310974e1fc1c69066e450James Bottomley		__list_add(&alist, ascb->list.prev, &ascb->list);
6072908d778ab3e244900c310974e1fc1c69066e450James Bottomley		goto out_err_unmap;
6082908d778ab3e244900c310974e1fc1c69066e450James Bottomley	}
6092908d778ab3e244900c310974e1fc1c69066e450James Bottomley
6102908d778ab3e244900c310974e1fc1c69066e450James Bottomley	return 0;
6112908d778ab3e244900c310974e1fc1c69066e450James Bottomleyout_err_unmap:
6122908d778ab3e244900c310974e1fc1c69066e450James Bottomley	{
6132908d778ab3e244900c310974e1fc1c69066e450James Bottomley		struct asd_ascb *b = a;
6142908d778ab3e244900c310974e1fc1c69066e450James Bottomley		list_for_each_entry(a, &alist, list) {
6152908d778ab3e244900c310974e1fc1c69066e450James Bottomley			if (a == b)
6162908d778ab3e244900c310974e1fc1c69066e450James Bottomley				break;
6172908d778ab3e244900c310974e1fc1c69066e450James Bottomley			t = a->uldd_task;
618b218a0d8e250e0ae8fd4d4e45bd66a588b380752Darrick J. Wong			spin_lock_irqsave(&t->task_state_lock, flags);
619b218a0d8e250e0ae8fd4d4e45bd66a588b380752Darrick J. Wong			t->task_state_flags &= ~SAS_TASK_AT_INITIATOR;
620b218a0d8e250e0ae8fd4d4e45bd66a588b380752Darrick J. Wong			spin_unlock_irqrestore(&t->task_state_lock, flags);
6212908d778ab3e244900c310974e1fc1c69066e450James Bottomley			switch (t->task_proto) {
6225929faf3334f4c69f3bb02be59d7c127e0cefa1fDarrick J. Wong			case SAS_PROTOCOL_SATA:
6235929faf3334f4c69f3bb02be59d7c127e0cefa1fDarrick J. Wong			case SAS_PROTOCOL_STP:
6242908d778ab3e244900c310974e1fc1c69066e450James Bottomley				asd_unbuild_ata_ascb(a);
6252908d778ab3e244900c310974e1fc1c69066e450James Bottomley				break;
6265929faf3334f4c69f3bb02be59d7c127e0cefa1fDarrick J. Wong			case SAS_PROTOCOL_SMP:
6272908d778ab3e244900c310974e1fc1c69066e450James Bottomley				asd_unbuild_smp_ascb(a);
6282908d778ab3e244900c310974e1fc1c69066e450James Bottomley				break;
6295929faf3334f4c69f3bb02be59d7c127e0cefa1fDarrick J. Wong			case SAS_PROTOCOL_SSP:
6302908d778ab3e244900c310974e1fc1c69066e450James Bottomley				asd_unbuild_ssp_ascb(a);
6312908d778ab3e244900c310974e1fc1c69066e450James Bottomley			default:
6322908d778ab3e244900c310974e1fc1c69066e450James Bottomley				break;
6332908d778ab3e244900c310974e1fc1c69066e450James Bottomley			}
6342908d778ab3e244900c310974e1fc1c69066e450James Bottomley			t->lldd_task = NULL;
6352908d778ab3e244900c310974e1fc1c69066e450James Bottomley		}
6362908d778ab3e244900c310974e1fc1c69066e450James Bottomley	}
6372908d778ab3e244900c310974e1fc1c69066e450James Bottomley	list_del_init(&alist);
6382908d778ab3e244900c310974e1fc1c69066e450James Bottomleyout_err:
6392908d778ab3e244900c310974e1fc1c69066e450James Bottomley	if (ascb)
6402908d778ab3e244900c310974e1fc1c69066e450James Bottomley		asd_ascb_free_list(ascb);
6412908d778ab3e244900c310974e1fc1c69066e450James Bottomley	asd_can_dequeue(asd_ha, num);
6422908d778ab3e244900c310974e1fc1c69066e450James Bottomley	return res;
6432908d778ab3e244900c310974e1fc1c69066e450James Bottomley}
644