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