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 362908d778SJames Bottomley static inline 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 542908d778SJames Bottomley static inline 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 } 972908d778SJames Bottomley for (sc = task->scatter, i = 0; i < num_sg; i++, sc++) { 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 1062908d778SJames Bottomley for (sc = task->scatter, i = 0; i < 2; i++, sc++) { 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; 1182908d778SJames Bottomley for (sc = task->scatter, i = 0; i < num_sg; i++, sc++) { 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 1342908d778SJames Bottomley static inline 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; 1902908d778SJames Bottomley if (task->task_proto == SAS_PROTO_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); 1952908d778SJames Bottomley ts->resp = SAS_TASK_COMPLETE; 1962908d778SJames Bottomley if (iu->datapres == 0) 1972908d778SJames Bottomley ts->stat = iu->status; 1982908d778SJames Bottomley else if (iu->datapres == 1) 1992908d778SJames Bottomley ts->stat = iu->resp_data[3]; 2002908d778SJames Bottomley else if (iu->datapres == 2) { 2012908d778SJames Bottomley ts->stat = SAM_CHECK_COND; 2022908d778SJames Bottomley ts->buf_valid_size = min((u32) SAS_STATUS_BUF_SIZE, 2032908d778SJames Bottomley be32_to_cpu(iu->sense_data_len)); 2042908d778SJames Bottomley memcpy(ts->buf, iu->sense_data, ts->buf_valid_size); 2052908d778SJames Bottomley if (iu->status != SAM_CHECK_COND) { 2062908d778SJames Bottomley ASD_DPRINTK("device %llx sent sense data, but " 2072908d778SJames Bottomley "stat(0x%x) is not CHECK_CONDITION" 2082908d778SJames Bottomley "\n", 2092908d778SJames Bottomley SAS_ADDR(task->dev->sas_addr), 2102908d778SJames Bottomley ts->stat); 2112908d778SJames Bottomley } 2122908d778SJames Bottomley } 2132908d778SJames Bottomley } else { 2142908d778SJames Bottomley struct ata_task_resp *resp = (void *) &ts->buf[0]; 2152908d778SJames Bottomley 2162908d778SJames Bottomley ts->residual = le32_to_cpu(*(__le32 *)r); 2172908d778SJames Bottomley 2182908d778SJames Bottomley if (SAS_STATUS_BUF_SIZE >= sizeof(*resp)) { 2192908d778SJames Bottomley resp->frame_len = le16_to_cpu(*(__le16 *)(r+6)); 2202908d778SJames Bottomley memcpy(&resp->ending_fis[0], r+16, 24); 2212908d778SJames Bottomley ts->buf_valid_size = sizeof(*resp); 2222908d778SJames Bottomley } 2232908d778SJames Bottomley } 2242908d778SJames Bottomley 2252908d778SJames Bottomley asd_invalidate_edb(escb, edb_id); 2262908d778SJames Bottomley } 2272908d778SJames Bottomley 2282908d778SJames Bottomley static void asd_task_tasklet_complete(struct asd_ascb *ascb, 2292908d778SJames Bottomley struct done_list_struct *dl) 2302908d778SJames Bottomley { 2312908d778SJames Bottomley struct sas_task *task = ascb->uldd_task; 2322908d778SJames Bottomley struct task_status_struct *ts = &task->task_status; 2332908d778SJames Bottomley unsigned long flags; 2342908d778SJames Bottomley u8 opcode = dl->opcode; 2352908d778SJames Bottomley 2362908d778SJames Bottomley asd_can_dequeue(ascb->ha, 1); 2372908d778SJames Bottomley 2382908d778SJames Bottomley Again: 2392908d778SJames Bottomley switch (opcode) { 2402908d778SJames Bottomley case TC_NO_ERROR: 2412908d778SJames Bottomley ts->resp = SAS_TASK_COMPLETE; 2422908d778SJames Bottomley ts->stat = SAM_GOOD; 2432908d778SJames Bottomley break; 2442908d778SJames Bottomley case TC_UNDERRUN: 2452908d778SJames Bottomley ts->resp = SAS_TASK_COMPLETE; 2462908d778SJames Bottomley ts->stat = SAS_DATA_UNDERRUN; 2472908d778SJames Bottomley ts->residual = le32_to_cpu(*(__le32 *)dl->status_block); 2482908d778SJames Bottomley break; 2492908d778SJames Bottomley case TC_OVERRUN: 2502908d778SJames Bottomley ts->resp = SAS_TASK_COMPLETE; 2512908d778SJames Bottomley ts->stat = SAS_DATA_OVERRUN; 2522908d778SJames Bottomley ts->residual = 0; 2532908d778SJames Bottomley break; 2542908d778SJames Bottomley case TC_SSP_RESP: 2552908d778SJames Bottomley case TC_ATA_RESP: 2562908d778SJames Bottomley ts->resp = SAS_TASK_COMPLETE; 2572908d778SJames Bottomley ts->stat = SAS_PROTO_RESPONSE; 2582908d778SJames Bottomley asd_get_response_tasklet(ascb, dl); 2592908d778SJames Bottomley break; 2602908d778SJames Bottomley case TF_OPEN_REJECT: 2612908d778SJames Bottomley ts->resp = SAS_TASK_UNDELIVERED; 2622908d778SJames Bottomley ts->stat = SAS_OPEN_REJECT; 2632908d778SJames Bottomley if (dl->status_block[1] & 2) 2642908d778SJames Bottomley ts->open_rej_reason = 1 + dl->status_block[2]; 2652908d778SJames Bottomley else if (dl->status_block[1] & 1) 2662908d778SJames Bottomley ts->open_rej_reason = (dl->status_block[2] >> 4)+10; 2672908d778SJames Bottomley else 2682908d778SJames Bottomley ts->open_rej_reason = SAS_OREJ_UNKNOWN; 2692908d778SJames Bottomley break; 2702908d778SJames Bottomley case TF_OPEN_TO: 2712908d778SJames Bottomley ts->resp = SAS_TASK_UNDELIVERED; 2722908d778SJames Bottomley ts->stat = SAS_OPEN_TO; 2732908d778SJames Bottomley break; 2742908d778SJames Bottomley case TF_PHY_DOWN: 2752908d778SJames Bottomley case TU_PHY_DOWN: 2762908d778SJames Bottomley ts->resp = SAS_TASK_UNDELIVERED; 2772908d778SJames Bottomley ts->stat = SAS_PHY_DOWN; 2782908d778SJames Bottomley break; 2792908d778SJames Bottomley case TI_PHY_DOWN: 2802908d778SJames Bottomley ts->resp = SAS_TASK_COMPLETE; 2812908d778SJames Bottomley ts->stat = SAS_PHY_DOWN; 2822908d778SJames Bottomley break; 2832908d778SJames Bottomley case TI_BREAK: 2842908d778SJames Bottomley case TI_PROTO_ERR: 2852908d778SJames Bottomley case TI_NAK: 2862908d778SJames Bottomley case TI_ACK_NAK_TO: 2872908d778SJames Bottomley case TF_SMP_XMIT_RCV_ERR: 2882908d778SJames Bottomley case TC_ATA_R_ERR_RECV: 2892908d778SJames Bottomley ts->resp = SAS_TASK_COMPLETE; 2902908d778SJames Bottomley ts->stat = SAS_INTERRUPTED; 2912908d778SJames Bottomley break; 2922908d778SJames Bottomley case TF_BREAK: 2932908d778SJames Bottomley case TU_BREAK: 2942908d778SJames Bottomley case TU_ACK_NAK_TO: 2952908d778SJames Bottomley case TF_SMPRSP_TO: 2962908d778SJames Bottomley ts->resp = SAS_TASK_UNDELIVERED; 2972908d778SJames Bottomley ts->stat = SAS_DEV_NO_RESPONSE; 2982908d778SJames Bottomley break; 2992908d778SJames Bottomley case TF_NAK_RECV: 3002908d778SJames Bottomley ts->resp = SAS_TASK_COMPLETE; 3012908d778SJames Bottomley ts->stat = SAS_NAK_R_ERR; 3022908d778SJames Bottomley break; 3032908d778SJames Bottomley case TA_I_T_NEXUS_LOSS: 3042908d778SJames Bottomley opcode = dl->status_block[0]; 3052908d778SJames Bottomley goto Again; 3062908d778SJames Bottomley break; 3072908d778SJames Bottomley case TF_INV_CONN_HANDLE: 3082908d778SJames Bottomley ts->resp = SAS_TASK_UNDELIVERED; 3092908d778SJames Bottomley ts->stat = SAS_DEVICE_UNKNOWN; 3102908d778SJames Bottomley break; 3112908d778SJames Bottomley case TF_REQUESTED_N_PENDING: 3122908d778SJames Bottomley ts->resp = SAS_TASK_UNDELIVERED; 3132908d778SJames Bottomley ts->stat = SAS_PENDING; 3142908d778SJames Bottomley break; 3152908d778SJames Bottomley case TC_TASK_CLEARED: 3162908d778SJames Bottomley case TA_ON_REQ: 3172908d778SJames Bottomley ts->resp = SAS_TASK_COMPLETE; 3182908d778SJames Bottomley ts->stat = SAS_ABORTED_TASK; 3192908d778SJames Bottomley break; 3202908d778SJames Bottomley 3212908d778SJames Bottomley case TF_NO_SMP_CONN: 3222908d778SJames Bottomley case TF_TMF_NO_CTX: 3232908d778SJames Bottomley case TF_TMF_NO_TAG: 3242908d778SJames Bottomley case TF_TMF_TAG_FREE: 3252908d778SJames Bottomley case TF_TMF_TASK_DONE: 3262908d778SJames Bottomley case TF_TMF_NO_CONN_HANDLE: 3272908d778SJames Bottomley case TF_IRTT_TO: 3282908d778SJames Bottomley case TF_IU_SHORT: 3292908d778SJames Bottomley case TF_DATA_OFFS_ERR: 3302908d778SJames Bottomley ts->resp = SAS_TASK_UNDELIVERED; 3312908d778SJames Bottomley ts->stat = SAS_DEV_NO_RESPONSE; 3322908d778SJames Bottomley break; 3332908d778SJames Bottomley 3342908d778SJames Bottomley case TC_LINK_ADM_RESP: 3352908d778SJames Bottomley case TC_CONTROL_PHY: 3362908d778SJames Bottomley case TC_RESUME: 3372908d778SJames Bottomley case TC_PARTIAL_SG_LIST: 3382908d778SJames Bottomley default: 3392908d778SJames Bottomley ASD_DPRINTK("%s: dl opcode: 0x%x?\n", __FUNCTION__, opcode); 3402908d778SJames Bottomley break; 3412908d778SJames Bottomley } 3422908d778SJames Bottomley 3432908d778SJames Bottomley switch (task->task_proto) { 3442908d778SJames Bottomley case SATA_PROTO: 3452908d778SJames Bottomley case SAS_PROTO_STP: 3462908d778SJames Bottomley asd_unbuild_ata_ascb(ascb); 3472908d778SJames Bottomley break; 3482908d778SJames Bottomley case SAS_PROTO_SMP: 3492908d778SJames Bottomley asd_unbuild_smp_ascb(ascb); 3502908d778SJames Bottomley break; 3512908d778SJames Bottomley case SAS_PROTO_SSP: 3522908d778SJames Bottomley asd_unbuild_ssp_ascb(ascb); 3532908d778SJames Bottomley default: 3542908d778SJames Bottomley break; 3552908d778SJames Bottomley } 3562908d778SJames Bottomley 3572908d778SJames Bottomley spin_lock_irqsave(&task->task_state_lock, flags); 3582908d778SJames Bottomley task->task_state_flags &= ~SAS_TASK_STATE_PENDING; 359b218a0d8SDarrick J. Wong task->task_state_flags &= ~SAS_TASK_AT_INITIATOR; 3602908d778SJames Bottomley task->task_state_flags |= SAS_TASK_STATE_DONE; 3612908d778SJames Bottomley if (unlikely((task->task_state_flags & SAS_TASK_STATE_ABORTED))) { 3622908d778SJames Bottomley spin_unlock_irqrestore(&task->task_state_lock, flags); 3632908d778SJames Bottomley ASD_DPRINTK("task 0x%p done with opcode 0x%x resp 0x%x " 3642908d778SJames Bottomley "stat 0x%x but aborted by upper layer!\n", 3652908d778SJames Bottomley task, opcode, ts->resp, ts->stat); 3662908d778SJames Bottomley complete(&ascb->completion); 3672908d778SJames Bottomley } else { 3682908d778SJames Bottomley spin_unlock_irqrestore(&task->task_state_lock, flags); 3692908d778SJames Bottomley task->lldd_task = NULL; 3702908d778SJames Bottomley asd_ascb_free(ascb); 3712908d778SJames Bottomley mb(); 3722908d778SJames Bottomley task->task_done(task); 3732908d778SJames Bottomley } 3742908d778SJames Bottomley } 3752908d778SJames Bottomley 3762908d778SJames Bottomley /* ---------- ATA ---------- */ 3772908d778SJames Bottomley 3782908d778SJames Bottomley static int asd_build_ata_ascb(struct asd_ascb *ascb, struct sas_task *task, 3793cc27547SAl Viro gfp_t gfp_flags) 3802908d778SJames Bottomley { 3812908d778SJames Bottomley struct domain_device *dev = task->dev; 3822908d778SJames Bottomley struct scb *scb; 3832908d778SJames Bottomley u8 flags; 3842908d778SJames Bottomley int res = 0; 3852908d778SJames Bottomley 3862908d778SJames Bottomley scb = ascb->scb; 3872908d778SJames Bottomley 3882908d778SJames Bottomley if (unlikely(task->ata_task.device_control_reg_update)) 3892908d778SJames Bottomley scb->header.opcode = CONTROL_ATA_DEV; 3902908d778SJames Bottomley else if (dev->sata_dev.command_set == ATA_COMMAND_SET) 3912908d778SJames Bottomley scb->header.opcode = INITIATE_ATA_TASK; 3922908d778SJames Bottomley else 3932908d778SJames Bottomley scb->header.opcode = INITIATE_ATAPI_TASK; 3942908d778SJames Bottomley 3952908d778SJames Bottomley scb->ata_task.proto_conn_rate = (1 << 5); /* STP */ 3962908d778SJames Bottomley if (dev->port->oob_mode == SAS_OOB_MODE) 3972908d778SJames Bottomley scb->ata_task.proto_conn_rate |= dev->linkrate; 3982908d778SJames Bottomley 3992908d778SJames Bottomley scb->ata_task.total_xfer_len = cpu_to_le32(task->total_xfer_len); 4002908d778SJames Bottomley scb->ata_task.fis = task->ata_task.fis; 4012908d778SJames Bottomley if (likely(!task->ata_task.device_control_reg_update)) 4022908d778SJames Bottomley scb->ata_task.fis.flags |= 0x80; /* C=1: update ATA cmd reg */ 4032908d778SJames Bottomley scb->ata_task.fis.flags &= 0xF0; /* PM_PORT field shall be 0 */ 4042908d778SJames Bottomley if (dev->sata_dev.command_set == ATAPI_COMMAND_SET) 4052908d778SJames Bottomley memcpy(scb->ata_task.atapi_packet, task->ata_task.atapi_packet, 4062908d778SJames Bottomley 16); 4072908d778SJames Bottomley scb->ata_task.sister_scb = cpu_to_le16(0xFFFF); 4082908d778SJames Bottomley scb->ata_task.conn_handle = cpu_to_le16( 4092908d778SJames Bottomley (u16)(unsigned long)dev->lldd_dev); 4102908d778SJames Bottomley 4112908d778SJames Bottomley if (likely(!task->ata_task.device_control_reg_update)) { 4122908d778SJames Bottomley flags = 0; 4132908d778SJames Bottomley if (task->ata_task.dma_xfer) 4142908d778SJames Bottomley flags |= DATA_XFER_MODE_DMA; 4152908d778SJames Bottomley if (task->ata_task.use_ncq && 4162908d778SJames Bottomley dev->sata_dev.command_set != ATAPI_COMMAND_SET) 4172908d778SJames Bottomley flags |= ATA_Q_TYPE_NCQ; 4182908d778SJames Bottomley flags |= data_dir_flags[task->data_dir]; 4192908d778SJames Bottomley scb->ata_task.ata_flags = flags; 4202908d778SJames Bottomley 4212908d778SJames Bottomley scb->ata_task.retry_count = task->ata_task.retry_count; 4222908d778SJames Bottomley 4232908d778SJames Bottomley flags = 0; 4242908d778SJames Bottomley if (task->ata_task.set_affil_pol) 4252908d778SJames Bottomley flags |= SET_AFFIL_POLICY; 4262908d778SJames Bottomley if (task->ata_task.stp_affil_pol) 4272908d778SJames Bottomley flags |= STP_AFFIL_POLICY; 4282908d778SJames Bottomley scb->ata_task.flags = flags; 4292908d778SJames Bottomley } 4302908d778SJames Bottomley ascb->tasklet_complete = asd_task_tasklet_complete; 4312908d778SJames Bottomley 4322908d778SJames Bottomley if (likely(!task->ata_task.device_control_reg_update)) 4332908d778SJames Bottomley res = asd_map_scatterlist(task, scb->ata_task.sg_element, 4342908d778SJames Bottomley gfp_flags); 4352908d778SJames Bottomley 4362908d778SJames Bottomley return res; 4372908d778SJames Bottomley } 4382908d778SJames Bottomley 4392908d778SJames Bottomley static void asd_unbuild_ata_ascb(struct asd_ascb *a) 4402908d778SJames Bottomley { 4412908d778SJames Bottomley asd_unmap_scatterlist(a); 4422908d778SJames Bottomley } 4432908d778SJames Bottomley 4442908d778SJames Bottomley /* ---------- SMP ---------- */ 4452908d778SJames Bottomley 4462908d778SJames Bottomley static int asd_build_smp_ascb(struct asd_ascb *ascb, struct sas_task *task, 4473cc27547SAl Viro gfp_t gfp_flags) 4482908d778SJames Bottomley { 4492908d778SJames Bottomley struct asd_ha_struct *asd_ha = ascb->ha; 4502908d778SJames Bottomley struct domain_device *dev = task->dev; 4512908d778SJames Bottomley struct scb *scb; 4522908d778SJames Bottomley 4532908d778SJames Bottomley pci_map_sg(asd_ha->pcidev, &task->smp_task.smp_req, 1, 4542908d778SJames Bottomley PCI_DMA_FROMDEVICE); 4552908d778SJames Bottomley pci_map_sg(asd_ha->pcidev, &task->smp_task.smp_resp, 1, 4562908d778SJames Bottomley PCI_DMA_FROMDEVICE); 4572908d778SJames Bottomley 4582908d778SJames Bottomley scb = ascb->scb; 4592908d778SJames Bottomley 4602908d778SJames Bottomley scb->header.opcode = INITIATE_SMP_TASK; 4612908d778SJames Bottomley 4622908d778SJames Bottomley scb->smp_task.proto_conn_rate = dev->linkrate; 4632908d778SJames Bottomley 4642908d778SJames Bottomley scb->smp_task.smp_req.bus_addr = 4652908d778SJames Bottomley cpu_to_le64((u64)sg_dma_address(&task->smp_task.smp_req)); 4662908d778SJames Bottomley scb->smp_task.smp_req.size = 4672908d778SJames Bottomley cpu_to_le32((u32)sg_dma_len(&task->smp_task.smp_req)-4); 4682908d778SJames Bottomley 4692908d778SJames Bottomley scb->smp_task.smp_resp.bus_addr = 4702908d778SJames Bottomley cpu_to_le64((u64)sg_dma_address(&task->smp_task.smp_resp)); 4712908d778SJames Bottomley scb->smp_task.smp_resp.size = 4722908d778SJames Bottomley cpu_to_le32((u32)sg_dma_len(&task->smp_task.smp_resp)-4); 4732908d778SJames Bottomley 4742908d778SJames Bottomley scb->smp_task.sister_scb = cpu_to_le16(0xFFFF); 4752908d778SJames Bottomley scb->smp_task.conn_handle = cpu_to_le16((u16) 4762908d778SJames Bottomley (unsigned long)dev->lldd_dev); 4772908d778SJames Bottomley 4782908d778SJames Bottomley ascb->tasklet_complete = asd_task_tasklet_complete; 4792908d778SJames Bottomley 4802908d778SJames Bottomley return 0; 4812908d778SJames Bottomley } 4822908d778SJames Bottomley 4832908d778SJames Bottomley static void asd_unbuild_smp_ascb(struct asd_ascb *a) 4842908d778SJames Bottomley { 4852908d778SJames Bottomley struct sas_task *task = a->uldd_task; 4862908d778SJames Bottomley 4872908d778SJames Bottomley BUG_ON(!task); 4882908d778SJames Bottomley pci_unmap_sg(a->ha->pcidev, &task->smp_task.smp_req, 1, 4892908d778SJames Bottomley PCI_DMA_FROMDEVICE); 4902908d778SJames Bottomley pci_unmap_sg(a->ha->pcidev, &task->smp_task.smp_resp, 1, 4912908d778SJames Bottomley PCI_DMA_FROMDEVICE); 4922908d778SJames Bottomley } 4932908d778SJames Bottomley 4942908d778SJames Bottomley /* ---------- SSP ---------- */ 4952908d778SJames Bottomley 4962908d778SJames Bottomley static int asd_build_ssp_ascb(struct asd_ascb *ascb, struct sas_task *task, 4973cc27547SAl Viro gfp_t gfp_flags) 4982908d778SJames Bottomley { 4992908d778SJames Bottomley struct domain_device *dev = task->dev; 5002908d778SJames Bottomley struct scb *scb; 5012908d778SJames Bottomley int res = 0; 5022908d778SJames Bottomley 5032908d778SJames Bottomley scb = ascb->scb; 5042908d778SJames Bottomley 5052908d778SJames Bottomley scb->header.opcode = INITIATE_SSP_TASK; 5062908d778SJames Bottomley 5072908d778SJames Bottomley scb->ssp_task.proto_conn_rate = (1 << 4); /* SSP */ 5082908d778SJames Bottomley scb->ssp_task.proto_conn_rate |= dev->linkrate; 5092908d778SJames Bottomley scb->ssp_task.total_xfer_len = cpu_to_le32(task->total_xfer_len); 5102908d778SJames Bottomley scb->ssp_task.ssp_frame.frame_type = SSP_DATA; 5112908d778SJames Bottomley memcpy(scb->ssp_task.ssp_frame.hashed_dest_addr, dev->hashed_sas_addr, 5122908d778SJames Bottomley HASHED_SAS_ADDR_SIZE); 5132908d778SJames Bottomley memcpy(scb->ssp_task.ssp_frame.hashed_src_addr, 5142908d778SJames Bottomley dev->port->ha->hashed_sas_addr, HASHED_SAS_ADDR_SIZE); 5152908d778SJames Bottomley scb->ssp_task.ssp_frame.tptt = cpu_to_be16(0xFFFF); 5162908d778SJames Bottomley 5172908d778SJames Bottomley memcpy(scb->ssp_task.ssp_cmd.lun, task->ssp_task.LUN, 8); 5182908d778SJames Bottomley if (task->ssp_task.enable_first_burst) 5192908d778SJames Bottomley scb->ssp_task.ssp_cmd.efb_prio_attr |= EFB_MASK; 5202908d778SJames Bottomley scb->ssp_task.ssp_cmd.efb_prio_attr |= (task->ssp_task.task_prio << 3); 5212908d778SJames Bottomley scb->ssp_task.ssp_cmd.efb_prio_attr |= (task->ssp_task.task_attr & 7); 5222908d778SJames Bottomley memcpy(scb->ssp_task.ssp_cmd.cdb, task->ssp_task.cdb, 16); 5232908d778SJames Bottomley 5242908d778SJames Bottomley scb->ssp_task.sister_scb = cpu_to_le16(0xFFFF); 5252908d778SJames Bottomley scb->ssp_task.conn_handle = cpu_to_le16( 5262908d778SJames Bottomley (u16)(unsigned long)dev->lldd_dev); 5272908d778SJames Bottomley scb->ssp_task.data_dir = data_dir_flags[task->data_dir]; 5282908d778SJames Bottomley scb->ssp_task.retry_count = scb->ssp_task.retry_count; 5292908d778SJames Bottomley 5302908d778SJames Bottomley ascb->tasklet_complete = asd_task_tasklet_complete; 5312908d778SJames Bottomley 5322908d778SJames Bottomley res = asd_map_scatterlist(task, scb->ssp_task.sg_element, gfp_flags); 5332908d778SJames Bottomley 5342908d778SJames Bottomley return res; 5352908d778SJames Bottomley } 5362908d778SJames Bottomley 5372908d778SJames Bottomley static void asd_unbuild_ssp_ascb(struct asd_ascb *a) 5382908d778SJames Bottomley { 5392908d778SJames Bottomley asd_unmap_scatterlist(a); 5402908d778SJames Bottomley } 5412908d778SJames Bottomley 5422908d778SJames Bottomley /* ---------- Execute Task ---------- */ 5432908d778SJames Bottomley 5442908d778SJames Bottomley static inline int asd_can_queue(struct asd_ha_struct *asd_ha, int num) 5452908d778SJames Bottomley { 5462908d778SJames Bottomley int res = 0; 5472908d778SJames Bottomley unsigned long flags; 5482908d778SJames Bottomley 5492908d778SJames Bottomley spin_lock_irqsave(&asd_ha->seq.pend_q_lock, flags); 5502908d778SJames Bottomley if ((asd_ha->seq.can_queue - num) < 0) 5512908d778SJames Bottomley res = -SAS_QUEUE_FULL; 5522908d778SJames Bottomley else 5532908d778SJames Bottomley asd_ha->seq.can_queue -= num; 5542908d778SJames Bottomley spin_unlock_irqrestore(&asd_ha->seq.pend_q_lock, flags); 5552908d778SJames Bottomley 5562908d778SJames Bottomley return res; 5572908d778SJames Bottomley } 5582908d778SJames Bottomley 5592908d778SJames Bottomley int asd_execute_task(struct sas_task *task, const int num, 5603cc27547SAl Viro gfp_t gfp_flags) 5612908d778SJames Bottomley { 5622908d778SJames Bottomley int res = 0; 5632908d778SJames Bottomley LIST_HEAD(alist); 5642908d778SJames Bottomley struct sas_task *t = task; 5652908d778SJames Bottomley struct asd_ascb *ascb = NULL, *a; 5662908d778SJames Bottomley struct asd_ha_struct *asd_ha = task->dev->port->ha->lldd_ha; 567b218a0d8SDarrick J. Wong unsigned long flags; 5682908d778SJames Bottomley 5692908d778SJames Bottomley res = asd_can_queue(asd_ha, num); 5702908d778SJames Bottomley if (res) 5712908d778SJames Bottomley return res; 5722908d778SJames Bottomley 5732908d778SJames Bottomley res = num; 5742908d778SJames Bottomley ascb = asd_ascb_alloc_list(asd_ha, &res, gfp_flags); 5752908d778SJames Bottomley if (res) { 5762908d778SJames Bottomley res = -ENOMEM; 5772908d778SJames Bottomley goto out_err; 5782908d778SJames Bottomley } 5792908d778SJames Bottomley 5802908d778SJames Bottomley __list_add(&alist, ascb->list.prev, &ascb->list); 5812908d778SJames Bottomley list_for_each_entry(a, &alist, list) { 5822908d778SJames Bottomley a->uldd_task = t; 5832908d778SJames Bottomley t->lldd_task = a; 5842908d778SJames Bottomley t = list_entry(t->list.next, struct sas_task, list); 5852908d778SJames Bottomley } 5862908d778SJames Bottomley list_for_each_entry(a, &alist, list) { 5872908d778SJames Bottomley t = a->uldd_task; 5882908d778SJames Bottomley a->uldd_timer = 1; 5892908d778SJames Bottomley if (t->task_proto & SAS_PROTO_STP) 5902908d778SJames Bottomley t->task_proto = SAS_PROTO_STP; 5912908d778SJames Bottomley switch (t->task_proto) { 5922908d778SJames Bottomley case SATA_PROTO: 5932908d778SJames Bottomley case SAS_PROTO_STP: 5942908d778SJames Bottomley res = asd_build_ata_ascb(a, t, gfp_flags); 5952908d778SJames Bottomley break; 5962908d778SJames Bottomley case SAS_PROTO_SMP: 5972908d778SJames Bottomley res = asd_build_smp_ascb(a, t, gfp_flags); 5982908d778SJames Bottomley break; 5992908d778SJames Bottomley case SAS_PROTO_SSP: 6002908d778SJames Bottomley res = asd_build_ssp_ascb(a, t, gfp_flags); 6012908d778SJames Bottomley break; 6022908d778SJames Bottomley default: 6032908d778SJames Bottomley asd_printk("unknown sas_task proto: 0x%x\n", 6042908d778SJames Bottomley t->task_proto); 6052908d778SJames Bottomley res = -ENOMEM; 6062908d778SJames Bottomley break; 6072908d778SJames Bottomley } 6082908d778SJames Bottomley if (res) 6092908d778SJames Bottomley goto out_err_unmap; 610b218a0d8SDarrick J. Wong 611b218a0d8SDarrick J. Wong spin_lock_irqsave(&t->task_state_lock, flags); 612b218a0d8SDarrick J. Wong t->task_state_flags |= SAS_TASK_AT_INITIATOR; 613b218a0d8SDarrick J. Wong spin_unlock_irqrestore(&t->task_state_lock, flags); 6142908d778SJames Bottomley } 6152908d778SJames Bottomley list_del_init(&alist); 6162908d778SJames Bottomley 6172908d778SJames Bottomley res = asd_post_ascb_list(asd_ha, ascb, num); 6182908d778SJames Bottomley if (unlikely(res)) { 6192908d778SJames Bottomley a = NULL; 6202908d778SJames Bottomley __list_add(&alist, ascb->list.prev, &ascb->list); 6212908d778SJames Bottomley goto out_err_unmap; 6222908d778SJames Bottomley } 6232908d778SJames Bottomley 6242908d778SJames Bottomley return 0; 6252908d778SJames Bottomley out_err_unmap: 6262908d778SJames Bottomley { 6272908d778SJames Bottomley struct asd_ascb *b = a; 6282908d778SJames Bottomley list_for_each_entry(a, &alist, list) { 6292908d778SJames Bottomley if (a == b) 6302908d778SJames Bottomley break; 6312908d778SJames Bottomley t = a->uldd_task; 632b218a0d8SDarrick J. Wong spin_lock_irqsave(&t->task_state_lock, flags); 633b218a0d8SDarrick J. Wong t->task_state_flags &= ~SAS_TASK_AT_INITIATOR; 634b218a0d8SDarrick J. Wong spin_unlock_irqrestore(&t->task_state_lock, flags); 6352908d778SJames Bottomley switch (t->task_proto) { 6362908d778SJames Bottomley case SATA_PROTO: 6372908d778SJames Bottomley case SAS_PROTO_STP: 6382908d778SJames Bottomley asd_unbuild_ata_ascb(a); 6392908d778SJames Bottomley break; 6402908d778SJames Bottomley case SAS_PROTO_SMP: 6412908d778SJames Bottomley asd_unbuild_smp_ascb(a); 6422908d778SJames Bottomley break; 6432908d778SJames Bottomley case SAS_PROTO_SSP: 6442908d778SJames Bottomley asd_unbuild_ssp_ascb(a); 6452908d778SJames Bottomley default: 6462908d778SJames Bottomley break; 6472908d778SJames Bottomley } 6482908d778SJames Bottomley t->lldd_task = NULL; 6492908d778SJames Bottomley } 6502908d778SJames Bottomley } 6512908d778SJames Bottomley list_del_init(&alist); 6522908d778SJames Bottomley out_err: 6532908d778SJames Bottomley if (ascb) 6542908d778SJames Bottomley asd_ascb_free_list(ascb); 6552908d778SJames Bottomley asd_can_dequeue(asd_ha, num); 6562908d778SJames Bottomley return res; 6572908d778SJames Bottomley } 658