13e0a4e85SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 2c6fd2807SJeff Garzik /* 3c6fd2807SJeff Garzik * sata_sil24.c - Driver for Silicon Image 3124/3132 SATA-2 controllers 4c6fd2807SJeff Garzik * 5c6fd2807SJeff Garzik * Copyright 2005 Tejun Heo 6c6fd2807SJeff Garzik * 7c6fd2807SJeff Garzik * Based on preview driver from Silicon Image. 8c6fd2807SJeff Garzik */ 9c6fd2807SJeff Garzik 10c6fd2807SJeff Garzik #include <linux/kernel.h> 11c6fd2807SJeff Garzik #include <linux/module.h> 125a0e3ad6STejun Heo #include <linux/gfp.h> 13c6fd2807SJeff Garzik #include <linux/pci.h> 14c6fd2807SJeff Garzik #include <linux/blkdev.h> 15c6fd2807SJeff Garzik #include <linux/delay.h> 16c6fd2807SJeff Garzik #include <linux/interrupt.h> 17c6fd2807SJeff Garzik #include <linux/dma-mapping.h> 18c6fd2807SJeff Garzik #include <linux/device.h> 19c6fd2807SJeff Garzik #include <scsi/scsi_host.h> 20c6fd2807SJeff Garzik #include <scsi/scsi_cmnd.h> 21c6fd2807SJeff Garzik #include <linux/libata.h> 22c6fd2807SJeff Garzik 23c6fd2807SJeff Garzik #define DRV_NAME "sata_sil24" 243454dc69STejun Heo #define DRV_VERSION "1.1" 25c6fd2807SJeff Garzik 26c6fd2807SJeff Garzik /* 27c6fd2807SJeff Garzik * Port request block (PRB) 32 bytes 28c6fd2807SJeff Garzik */ 29c6fd2807SJeff Garzik struct sil24_prb { 30c6fd2807SJeff Garzik __le16 ctrl; 31c6fd2807SJeff Garzik __le16 prot; 32c6fd2807SJeff Garzik __le32 rx_cnt; 33c6fd2807SJeff Garzik u8 fis[6 * 4]; 34c6fd2807SJeff Garzik }; 35c6fd2807SJeff Garzik 36c6fd2807SJeff Garzik /* 37c6fd2807SJeff Garzik * Scatter gather entry (SGE) 16 bytes 38c6fd2807SJeff Garzik */ 39c6fd2807SJeff Garzik struct sil24_sge { 40c6fd2807SJeff Garzik __le64 addr; 41c6fd2807SJeff Garzik __le32 cnt; 42c6fd2807SJeff Garzik __le32 flags; 43c6fd2807SJeff Garzik }; 44c6fd2807SJeff Garzik 45c6fd2807SJeff Garzik 46c6fd2807SJeff Garzik enum { 470d5ff566STejun Heo SIL24_HOST_BAR = 0, 480d5ff566STejun Heo SIL24_PORT_BAR = 2, 490d5ff566STejun Heo 5093e2618eSTejun Heo /* sil24 fetches in chunks of 64bytes. The first block 5193e2618eSTejun Heo * contains the PRB and two SGEs. From the second block, it's 5293e2618eSTejun Heo * consisted of four SGEs and called SGT. Calculate the 5393e2618eSTejun Heo * number of SGTs that fit into one page. 5493e2618eSTejun Heo */ 5593e2618eSTejun Heo SIL24_PRB_SZ = sizeof(struct sil24_prb) 5693e2618eSTejun Heo + 2 * sizeof(struct sil24_sge), 5793e2618eSTejun Heo SIL24_MAX_SGT = (PAGE_SIZE - SIL24_PRB_SZ) 5893e2618eSTejun Heo / (4 * sizeof(struct sil24_sge)), 5993e2618eSTejun Heo 6093e2618eSTejun Heo /* This will give us one unused SGEs for ATA. This extra SGE 6193e2618eSTejun Heo * will be used to store CDB for ATAPI devices. 6293e2618eSTejun Heo */ 6393e2618eSTejun Heo SIL24_MAX_SGE = 4 * SIL24_MAX_SGT + 1, 6493e2618eSTejun Heo 65c6fd2807SJeff Garzik /* 66c6fd2807SJeff Garzik * Global controller registers (128 bytes @ BAR0) 67c6fd2807SJeff Garzik */ 68c6fd2807SJeff Garzik /* 32 bit regs */ 69c6fd2807SJeff Garzik HOST_SLOT_STAT = 0x00, /* 32 bit slot stat * 4 */ 70c6fd2807SJeff Garzik HOST_CTRL = 0x40, 71c6fd2807SJeff Garzik HOST_IRQ_STAT = 0x44, 72c6fd2807SJeff Garzik HOST_PHY_CFG = 0x48, 73c6fd2807SJeff Garzik HOST_BIST_CTRL = 0x50, 74c6fd2807SJeff Garzik HOST_BIST_PTRN = 0x54, 75c6fd2807SJeff Garzik HOST_BIST_STAT = 0x58, 76c6fd2807SJeff Garzik HOST_MEM_BIST_STAT = 0x5c, 77c6fd2807SJeff Garzik HOST_FLASH_CMD = 0x70, 78c6fd2807SJeff Garzik /* 8 bit regs */ 79c6fd2807SJeff Garzik HOST_FLASH_DATA = 0x74, 80c6fd2807SJeff Garzik HOST_TRANSITION_DETECT = 0x75, 81c6fd2807SJeff Garzik HOST_GPIO_CTRL = 0x76, 82c6fd2807SJeff Garzik HOST_I2C_ADDR = 0x78, /* 32 bit */ 83c6fd2807SJeff Garzik HOST_I2C_DATA = 0x7c, 84c6fd2807SJeff Garzik HOST_I2C_XFER_CNT = 0x7e, 85c6fd2807SJeff Garzik HOST_I2C_CTRL = 0x7f, 86c6fd2807SJeff Garzik 87c6fd2807SJeff Garzik /* HOST_SLOT_STAT bits */ 88c6fd2807SJeff Garzik HOST_SSTAT_ATTN = (1 << 31), 89c6fd2807SJeff Garzik 90c6fd2807SJeff Garzik /* HOST_CTRL bits */ 91c6fd2807SJeff Garzik HOST_CTRL_M66EN = (1 << 16), /* M66EN PCI bus signal */ 92c6fd2807SJeff Garzik HOST_CTRL_TRDY = (1 << 17), /* latched PCI TRDY */ 93c6fd2807SJeff Garzik HOST_CTRL_STOP = (1 << 18), /* latched PCI STOP */ 94c6fd2807SJeff Garzik HOST_CTRL_DEVSEL = (1 << 19), /* latched PCI DEVSEL */ 95c6fd2807SJeff Garzik HOST_CTRL_REQ64 = (1 << 20), /* latched PCI REQ64 */ 96c6fd2807SJeff Garzik HOST_CTRL_GLOBAL_RST = (1 << 31), /* global reset */ 97c6fd2807SJeff Garzik 98c6fd2807SJeff Garzik /* 99c6fd2807SJeff Garzik * Port registers 100c6fd2807SJeff Garzik * (8192 bytes @ +0x0000, +0x2000, +0x4000 and +0x6000 @ BAR2) 101c6fd2807SJeff Garzik */ 102c6fd2807SJeff Garzik PORT_REGS_SIZE = 0x2000, 103c6fd2807SJeff Garzik 10428c8f3b4STejun Heo PORT_LRAM = 0x0000, /* 31 LRAM slots and PMP regs */ 105c6fd2807SJeff Garzik PORT_LRAM_SLOT_SZ = 0x0080, /* 32 bytes PRB + 2 SGE, ACT... */ 106c6fd2807SJeff Garzik 10728c8f3b4STejun Heo PORT_PMP = 0x0f80, /* 8 bytes PMP * 16 (128 bytes) */ 108c0c55908STejun Heo PORT_PMP_STATUS = 0x0000, /* port device status offset */ 109c0c55908STejun Heo PORT_PMP_QACTIVE = 0x0004, /* port device QActive offset */ 110c0c55908STejun Heo PORT_PMP_SIZE = 0x0008, /* 8 bytes per PMP */ 111c0c55908STejun Heo 112c6fd2807SJeff Garzik /* 32 bit regs */ 113c6fd2807SJeff Garzik PORT_CTRL_STAT = 0x1000, /* write: ctrl-set, read: stat */ 114c6fd2807SJeff Garzik PORT_CTRL_CLR = 0x1004, /* write: ctrl-clear */ 115c6fd2807SJeff Garzik PORT_IRQ_STAT = 0x1008, /* high: status, low: interrupt */ 116c6fd2807SJeff Garzik PORT_IRQ_ENABLE_SET = 0x1010, /* write: enable-set */ 117c6fd2807SJeff Garzik PORT_IRQ_ENABLE_CLR = 0x1014, /* write: enable-clear */ 118c6fd2807SJeff Garzik PORT_ACTIVATE_UPPER_ADDR= 0x101c, 119c6fd2807SJeff Garzik PORT_EXEC_FIFO = 0x1020, /* command execution fifo */ 120c6fd2807SJeff Garzik PORT_CMD_ERR = 0x1024, /* command error number */ 121c6fd2807SJeff Garzik PORT_FIS_CFG = 0x1028, 122c6fd2807SJeff Garzik PORT_FIFO_THRES = 0x102c, 123c6fd2807SJeff Garzik /* 16 bit regs */ 124c6fd2807SJeff Garzik PORT_DECODE_ERR_CNT = 0x1040, 125c6fd2807SJeff Garzik PORT_DECODE_ERR_THRESH = 0x1042, 126c6fd2807SJeff Garzik PORT_CRC_ERR_CNT = 0x1044, 127c6fd2807SJeff Garzik PORT_CRC_ERR_THRESH = 0x1046, 128c6fd2807SJeff Garzik PORT_HSHK_ERR_CNT = 0x1048, 129c6fd2807SJeff Garzik PORT_HSHK_ERR_THRESH = 0x104a, 130c6fd2807SJeff Garzik /* 32 bit regs */ 131c6fd2807SJeff Garzik PORT_PHY_CFG = 0x1050, 132c6fd2807SJeff Garzik PORT_SLOT_STAT = 0x1800, 133c6fd2807SJeff Garzik PORT_CMD_ACTIVATE = 0x1c00, /* 64 bit cmd activate * 31 (248 bytes) */ 134c0c55908STejun Heo PORT_CONTEXT = 0x1e04, 135c6fd2807SJeff Garzik PORT_EXEC_DIAG = 0x1e00, /* 32bit exec diag * 16 (64 bytes, 0-10 used on 3124) */ 136c6fd2807SJeff Garzik PORT_PSD_DIAG = 0x1e40, /* 32bit psd diag * 16 (64 bytes, 0-8 used on 3124) */ 137c6fd2807SJeff Garzik PORT_SCONTROL = 0x1f00, 138c6fd2807SJeff Garzik PORT_SSTATUS = 0x1f04, 139c6fd2807SJeff Garzik PORT_SERROR = 0x1f08, 140c6fd2807SJeff Garzik PORT_SACTIVE = 0x1f0c, 141c6fd2807SJeff Garzik 142c6fd2807SJeff Garzik /* PORT_CTRL_STAT bits */ 143c6fd2807SJeff Garzik PORT_CS_PORT_RST = (1 << 0), /* port reset */ 144c6fd2807SJeff Garzik PORT_CS_DEV_RST = (1 << 1), /* device reset */ 145c6fd2807SJeff Garzik PORT_CS_INIT = (1 << 2), /* port initialize */ 146c6fd2807SJeff Garzik PORT_CS_IRQ_WOC = (1 << 3), /* interrupt write one to clear */ 147c6fd2807SJeff Garzik PORT_CS_CDB16 = (1 << 5), /* 0=12b cdb, 1=16b cdb */ 14828c8f3b4STejun Heo PORT_CS_PMP_RESUME = (1 << 6), /* PMP resume */ 149c6fd2807SJeff Garzik PORT_CS_32BIT_ACTV = (1 << 10), /* 32-bit activation */ 15028c8f3b4STejun Heo PORT_CS_PMP_EN = (1 << 13), /* port multiplier enable */ 151c6fd2807SJeff Garzik PORT_CS_RDY = (1 << 31), /* port ready to accept commands */ 152c6fd2807SJeff Garzik 153c6fd2807SJeff Garzik /* PORT_IRQ_STAT/ENABLE_SET/CLR */ 154c6fd2807SJeff Garzik /* bits[11:0] are masked */ 155c6fd2807SJeff Garzik PORT_IRQ_COMPLETE = (1 << 0), /* command(s) completed */ 156c6fd2807SJeff Garzik PORT_IRQ_ERROR = (1 << 1), /* command execution error */ 157c6fd2807SJeff Garzik PORT_IRQ_PORTRDY_CHG = (1 << 2), /* port ready change */ 158c6fd2807SJeff Garzik PORT_IRQ_PWR_CHG = (1 << 3), /* power management change */ 159c6fd2807SJeff Garzik PORT_IRQ_PHYRDY_CHG = (1 << 4), /* PHY ready change */ 160c6fd2807SJeff Garzik PORT_IRQ_COMWAKE = (1 << 5), /* COMWAKE received */ 161c6fd2807SJeff Garzik PORT_IRQ_UNK_FIS = (1 << 6), /* unknown FIS received */ 162c6fd2807SJeff Garzik PORT_IRQ_DEV_XCHG = (1 << 7), /* device exchanged */ 163c6fd2807SJeff Garzik PORT_IRQ_8B10B = (1 << 8), /* 8b/10b decode error threshold */ 164c6fd2807SJeff Garzik PORT_IRQ_CRC = (1 << 9), /* CRC error threshold */ 165c6fd2807SJeff Garzik PORT_IRQ_HANDSHAKE = (1 << 10), /* handshake error threshold */ 166c6fd2807SJeff Garzik PORT_IRQ_SDB_NOTIFY = (1 << 11), /* SDB notify received */ 167c6fd2807SJeff Garzik 168c6fd2807SJeff Garzik DEF_PORT_IRQ = PORT_IRQ_COMPLETE | PORT_IRQ_ERROR | 169c6fd2807SJeff Garzik PORT_IRQ_PHYRDY_CHG | PORT_IRQ_DEV_XCHG | 170854c73a2STejun Heo PORT_IRQ_UNK_FIS | PORT_IRQ_SDB_NOTIFY, 171c6fd2807SJeff Garzik 172c6fd2807SJeff Garzik /* bits[27:16] are unmasked (raw) */ 173c6fd2807SJeff Garzik PORT_IRQ_RAW_SHIFT = 16, 174c6fd2807SJeff Garzik PORT_IRQ_MASKED_MASK = 0x7ff, 175c6fd2807SJeff Garzik PORT_IRQ_RAW_MASK = (0x7ff << PORT_IRQ_RAW_SHIFT), 176c6fd2807SJeff Garzik 177c6fd2807SJeff Garzik /* ENABLE_SET/CLR specific, intr steering - 2 bit field */ 178c6fd2807SJeff Garzik PORT_IRQ_STEER_SHIFT = 30, 179c6fd2807SJeff Garzik PORT_IRQ_STEER_MASK = (3 << PORT_IRQ_STEER_SHIFT), 180c6fd2807SJeff Garzik 181c6fd2807SJeff Garzik /* PORT_CMD_ERR constants */ 182c6fd2807SJeff Garzik PORT_CERR_DEV = 1, /* Error bit in D2H Register FIS */ 183c6fd2807SJeff Garzik PORT_CERR_SDB = 2, /* Error bit in SDB FIS */ 184c6fd2807SJeff Garzik PORT_CERR_DATA = 3, /* Error in data FIS not detected by dev */ 185c6fd2807SJeff Garzik PORT_CERR_SEND = 4, /* Initial cmd FIS transmission failure */ 186c6fd2807SJeff Garzik PORT_CERR_INCONSISTENT = 5, /* Protocol mismatch */ 187c6fd2807SJeff Garzik PORT_CERR_DIRECTION = 6, /* Data direction mismatch */ 188c6fd2807SJeff Garzik PORT_CERR_UNDERRUN = 7, /* Ran out of SGEs while writing */ 189c6fd2807SJeff Garzik PORT_CERR_OVERRUN = 8, /* Ran out of SGEs while reading */ 190c6fd2807SJeff Garzik PORT_CERR_PKT_PROT = 11, /* DIR invalid in 1st PIO setup of ATAPI */ 191c6fd2807SJeff Garzik PORT_CERR_SGT_BOUNDARY = 16, /* PLD ecode 00 - SGT not on qword boundary */ 192c6fd2807SJeff Garzik PORT_CERR_SGT_TGTABRT = 17, /* PLD ecode 01 - target abort */ 193c6fd2807SJeff Garzik PORT_CERR_SGT_MSTABRT = 18, /* PLD ecode 10 - master abort */ 194c6fd2807SJeff Garzik PORT_CERR_SGT_PCIPERR = 19, /* PLD ecode 11 - PCI parity err while fetching SGT */ 195c6fd2807SJeff Garzik PORT_CERR_CMD_BOUNDARY = 24, /* ctrl[15:13] 001 - PRB not on qword boundary */ 196c6fd2807SJeff Garzik PORT_CERR_CMD_TGTABRT = 25, /* ctrl[15:13] 010 - target abort */ 197c6fd2807SJeff Garzik PORT_CERR_CMD_MSTABRT = 26, /* ctrl[15:13] 100 - master abort */ 198c6fd2807SJeff Garzik PORT_CERR_CMD_PCIPERR = 27, /* ctrl[15:13] 110 - PCI parity err while fetching PRB */ 199c6fd2807SJeff Garzik PORT_CERR_XFR_UNDEF = 32, /* PSD ecode 00 - undefined */ 200c6fd2807SJeff Garzik PORT_CERR_XFR_TGTABRT = 33, /* PSD ecode 01 - target abort */ 201c6fd2807SJeff Garzik PORT_CERR_XFR_MSTABRT = 34, /* PSD ecode 10 - master abort */ 202c6fd2807SJeff Garzik PORT_CERR_XFR_PCIPERR = 35, /* PSD ecode 11 - PCI prity err during transfer */ 203c6fd2807SJeff Garzik PORT_CERR_SENDSERVICE = 36, /* FIS received while sending service */ 204c6fd2807SJeff Garzik 205c6fd2807SJeff Garzik /* bits of PRB control field */ 206c6fd2807SJeff Garzik PRB_CTRL_PROTOCOL = (1 << 0), /* override def. ATA protocol */ 207c6fd2807SJeff Garzik PRB_CTRL_PACKET_READ = (1 << 4), /* PACKET cmd read */ 208c6fd2807SJeff Garzik PRB_CTRL_PACKET_WRITE = (1 << 5), /* PACKET cmd write */ 209c6fd2807SJeff Garzik PRB_CTRL_NIEN = (1 << 6), /* Mask completion irq */ 210c6fd2807SJeff Garzik PRB_CTRL_SRST = (1 << 7), /* Soft reset request (ign BSY?) */ 211c6fd2807SJeff Garzik 212c6fd2807SJeff Garzik /* PRB protocol field */ 213c6fd2807SJeff Garzik PRB_PROT_PACKET = (1 << 0), 214c6fd2807SJeff Garzik PRB_PROT_TCQ = (1 << 1), 215c6fd2807SJeff Garzik PRB_PROT_NCQ = (1 << 2), 216c6fd2807SJeff Garzik PRB_PROT_READ = (1 << 3), 217c6fd2807SJeff Garzik PRB_PROT_WRITE = (1 << 4), 218c6fd2807SJeff Garzik PRB_PROT_TRANSPARENT = (1 << 5), 219c6fd2807SJeff Garzik 220c6fd2807SJeff Garzik /* 221c6fd2807SJeff Garzik * Other constants 222c6fd2807SJeff Garzik */ 223c6fd2807SJeff Garzik SGE_TRM = (1 << 31), /* Last SGE in chain */ 224c6fd2807SJeff Garzik SGE_LNK = (1 << 30), /* linked list 225c6fd2807SJeff Garzik Points to SGT, not SGE */ 226c6fd2807SJeff Garzik SGE_DRD = (1 << 29), /* discard data read (/dev/null) 227c6fd2807SJeff Garzik data address ignored */ 228c6fd2807SJeff Garzik 229c6fd2807SJeff Garzik SIL24_MAX_CMDS = 31, 230c6fd2807SJeff Garzik 231c6fd2807SJeff Garzik /* board id */ 232c6fd2807SJeff Garzik BID_SIL3124 = 0, 233c6fd2807SJeff Garzik BID_SIL3132 = 1, 234c6fd2807SJeff Garzik BID_SIL3131 = 2, 235c6fd2807SJeff Garzik 236c6fd2807SJeff Garzik /* host flags */ 2379cbe056fSSergei Shtylyov SIL24_COMMON_FLAGS = ATA_FLAG_SATA | ATA_FLAG_PIO_DMA | 2389cbe056fSSergei Shtylyov ATA_FLAG_NCQ | ATA_FLAG_ACPI_SATA | 2393a028243STejun Heo ATA_FLAG_AN | ATA_FLAG_PMP, 240c6fd2807SJeff Garzik SIL24_FLAG_PCIX_IRQ_WOC = (1 << 24), /* IRQ loss errata on PCI-X */ 241c6fd2807SJeff Garzik 242c6fd2807SJeff Garzik IRQ_STAT_4PORTS = 0xf, 243c6fd2807SJeff Garzik }; 244c6fd2807SJeff Garzik 245c6fd2807SJeff Garzik struct sil24_ata_block { 246c6fd2807SJeff Garzik struct sil24_prb prb; 24793e2618eSTejun Heo struct sil24_sge sge[SIL24_MAX_SGE]; 248c6fd2807SJeff Garzik }; 249c6fd2807SJeff Garzik 250c6fd2807SJeff Garzik struct sil24_atapi_block { 251c6fd2807SJeff Garzik struct sil24_prb prb; 252c6fd2807SJeff Garzik u8 cdb[16]; 25393e2618eSTejun Heo struct sil24_sge sge[SIL24_MAX_SGE]; 254c6fd2807SJeff Garzik }; 255c6fd2807SJeff Garzik 256c6fd2807SJeff Garzik union sil24_cmd_block { 257c6fd2807SJeff Garzik struct sil24_ata_block ata; 258c6fd2807SJeff Garzik struct sil24_atapi_block atapi; 259c6fd2807SJeff Garzik }; 260c6fd2807SJeff Garzik 261fc8cc1d5SJoe Perches static const struct sil24_cerr_info { 262c6fd2807SJeff Garzik unsigned int err_mask, action; 263c6fd2807SJeff Garzik const char *desc; 264c6fd2807SJeff Garzik } sil24_cerr_db[] = { 265f90f0828STejun Heo [0] = { AC_ERR_DEV, 0, 266c6fd2807SJeff Garzik "device error" }, 267f90f0828STejun Heo [PORT_CERR_DEV] = { AC_ERR_DEV, 0, 268c6fd2807SJeff Garzik "device error via D2H FIS" }, 269f90f0828STejun Heo [PORT_CERR_SDB] = { AC_ERR_DEV, 0, 270c6fd2807SJeff Garzik "device error via SDB FIS" }, 271cf480626STejun Heo [PORT_CERR_DATA] = { AC_ERR_ATA_BUS, ATA_EH_RESET, 272c6fd2807SJeff Garzik "error in data FIS" }, 273cf480626STejun Heo [PORT_CERR_SEND] = { AC_ERR_ATA_BUS, ATA_EH_RESET, 274c6fd2807SJeff Garzik "failed to transmit command FIS" }, 275cf480626STejun Heo [PORT_CERR_INCONSISTENT] = { AC_ERR_HSM, ATA_EH_RESET, 276c6fd2807SJeff Garzik "protocol mismatch" }, 277cf480626STejun Heo [PORT_CERR_DIRECTION] = { AC_ERR_HSM, ATA_EH_RESET, 2787e437d61SColin Ian King "data direction mismatch" }, 279cf480626STejun Heo [PORT_CERR_UNDERRUN] = { AC_ERR_HSM, ATA_EH_RESET, 280c6fd2807SJeff Garzik "ran out of SGEs while writing" }, 281cf480626STejun Heo [PORT_CERR_OVERRUN] = { AC_ERR_HSM, ATA_EH_RESET, 282c6fd2807SJeff Garzik "ran out of SGEs while reading" }, 283cf480626STejun Heo [PORT_CERR_PKT_PROT] = { AC_ERR_HSM, ATA_EH_RESET, 2847e437d61SColin Ian King "invalid data direction for ATAPI CDB" }, 285cf480626STejun Heo [PORT_CERR_SGT_BOUNDARY] = { AC_ERR_SYSTEM, ATA_EH_RESET, 2867293fa8fSTejun Heo "SGT not on qword boundary" }, 287cf480626STejun Heo [PORT_CERR_SGT_TGTABRT] = { AC_ERR_HOST_BUS, ATA_EH_RESET, 288c6fd2807SJeff Garzik "PCI target abort while fetching SGT" }, 289cf480626STejun Heo [PORT_CERR_SGT_MSTABRT] = { AC_ERR_HOST_BUS, ATA_EH_RESET, 290c6fd2807SJeff Garzik "PCI master abort while fetching SGT" }, 291cf480626STejun Heo [PORT_CERR_SGT_PCIPERR] = { AC_ERR_HOST_BUS, ATA_EH_RESET, 292c6fd2807SJeff Garzik "PCI parity error while fetching SGT" }, 293cf480626STejun Heo [PORT_CERR_CMD_BOUNDARY] = { AC_ERR_SYSTEM, ATA_EH_RESET, 294c6fd2807SJeff Garzik "PRB not on qword boundary" }, 295cf480626STejun Heo [PORT_CERR_CMD_TGTABRT] = { AC_ERR_HOST_BUS, ATA_EH_RESET, 296c6fd2807SJeff Garzik "PCI target abort while fetching PRB" }, 297cf480626STejun Heo [PORT_CERR_CMD_MSTABRT] = { AC_ERR_HOST_BUS, ATA_EH_RESET, 298c6fd2807SJeff Garzik "PCI master abort while fetching PRB" }, 299cf480626STejun Heo [PORT_CERR_CMD_PCIPERR] = { AC_ERR_HOST_BUS, ATA_EH_RESET, 300c6fd2807SJeff Garzik "PCI parity error while fetching PRB" }, 301cf480626STejun Heo [PORT_CERR_XFR_UNDEF] = { AC_ERR_HOST_BUS, ATA_EH_RESET, 302c6fd2807SJeff Garzik "undefined error while transferring data" }, 303cf480626STejun Heo [PORT_CERR_XFR_TGTABRT] = { AC_ERR_HOST_BUS, ATA_EH_RESET, 304c6fd2807SJeff Garzik "PCI target abort while transferring data" }, 305cf480626STejun Heo [PORT_CERR_XFR_MSTABRT] = { AC_ERR_HOST_BUS, ATA_EH_RESET, 306c6fd2807SJeff Garzik "PCI master abort while transferring data" }, 307cf480626STejun Heo [PORT_CERR_XFR_PCIPERR] = { AC_ERR_HOST_BUS, ATA_EH_RESET, 308c6fd2807SJeff Garzik "PCI parity error while transferring data" }, 309cf480626STejun Heo [PORT_CERR_SENDSERVICE] = { AC_ERR_HSM, ATA_EH_RESET, 310c6fd2807SJeff Garzik "FIS received while sending service FIS" }, 311c6fd2807SJeff Garzik }; 312c6fd2807SJeff Garzik 313c6fd2807SJeff Garzik /* 314c6fd2807SJeff Garzik * ap->private_data 315c6fd2807SJeff Garzik * 316c6fd2807SJeff Garzik * The preview driver always returned 0 for status. We emulate it 317c6fd2807SJeff Garzik * here from the previous interrupt. 318c6fd2807SJeff Garzik */ 319c6fd2807SJeff Garzik struct sil24_port_priv { 320c6fd2807SJeff Garzik union sil24_cmd_block *cmd_block; /* 32 cmd blocks */ 321c6fd2807SJeff Garzik dma_addr_t cmd_block_dma; /* DMA base addr for them */ 32223818034STejun Heo int do_port_rst; 323c6fd2807SJeff Garzik }; 324c6fd2807SJeff Garzik 325cd0d3bbcSAlan static void sil24_dev_config(struct ata_device *dev); 32682ef04fbSTejun Heo static int sil24_scr_read(struct ata_link *link, unsigned sc_reg, u32 *val); 32782ef04fbSTejun Heo static int sil24_scr_write(struct ata_link *link, unsigned sc_reg, u32 val); 3283454dc69STejun Heo static int sil24_qc_defer(struct ata_queued_cmd *qc); 32995364f36SJiri Slaby static enum ata_completion_errors sil24_qc_prep(struct ata_queued_cmd *qc); 330c6fd2807SJeff Garzik static unsigned int sil24_qc_issue(struct ata_queued_cmd *qc); 33179f97dadSTejun Heo static bool sil24_qc_fill_rtf(struct ata_queued_cmd *qc); 3323454dc69STejun Heo static void sil24_pmp_attach(struct ata_port *ap); 3333454dc69STejun Heo static void sil24_pmp_detach(struct ata_port *ap); 334c6fd2807SJeff Garzik static void sil24_freeze(struct ata_port *ap); 335c6fd2807SJeff Garzik static void sil24_thaw(struct ata_port *ap); 336a1efdabaSTejun Heo static int sil24_softreset(struct ata_link *link, unsigned int *class, 337a1efdabaSTejun Heo unsigned long deadline); 338a1efdabaSTejun Heo static int sil24_hardreset(struct ata_link *link, unsigned int *class, 339a1efdabaSTejun Heo unsigned long deadline); 340a1efdabaSTejun Heo static int sil24_pmp_hardreset(struct ata_link *link, unsigned int *class, 341a1efdabaSTejun Heo unsigned long deadline); 342c6fd2807SJeff Garzik static void sil24_error_handler(struct ata_port *ap); 343c6fd2807SJeff Garzik static void sil24_post_internal_cmd(struct ata_queued_cmd *qc); 344c6fd2807SJeff Garzik static int sil24_port_start(struct ata_port *ap); 345c6fd2807SJeff Garzik static int sil24_init_one(struct pci_dev *pdev, const struct pci_device_id *ent); 34658eb8cd5SBartlomiej Zolnierkiewicz #ifdef CONFIG_PM_SLEEP 347c6fd2807SJeff Garzik static int sil24_pci_device_resume(struct pci_dev *pdev); 34858eb8cd5SBartlomiej Zolnierkiewicz #endif 34958eb8cd5SBartlomiej Zolnierkiewicz #ifdef CONFIG_PM 3503454dc69STejun Heo static int sil24_port_resume(struct ata_port *ap); 351281d426cSAlexey Dobriyan #endif 352c6fd2807SJeff Garzik 353c6fd2807SJeff Garzik static const struct pci_device_id sil24_pci_tbl[] = { 35454bb3a94SJeff Garzik { PCI_VDEVICE(CMD, 0x3124), BID_SIL3124 }, 35554bb3a94SJeff Garzik { PCI_VDEVICE(INTEL, 0x3124), BID_SIL3124 }, 35654bb3a94SJeff Garzik { PCI_VDEVICE(CMD, 0x3132), BID_SIL3132 }, 357722d67b6SJamie Clark { PCI_VDEVICE(CMD, 0x0242), BID_SIL3132 }, 358464b3286STejun Heo { PCI_VDEVICE(CMD, 0x0244), BID_SIL3132 }, 35954bb3a94SJeff Garzik { PCI_VDEVICE(CMD, 0x3131), BID_SIL3131 }, 36054bb3a94SJeff Garzik { PCI_VDEVICE(CMD, 0x3531), BID_SIL3131 }, 36154bb3a94SJeff Garzik 362c6fd2807SJeff Garzik { } /* terminate list */ 363c6fd2807SJeff Garzik }; 364c6fd2807SJeff Garzik 365c6fd2807SJeff Garzik static struct pci_driver sil24_pci_driver = { 366c6fd2807SJeff Garzik .name = DRV_NAME, 367c6fd2807SJeff Garzik .id_table = sil24_pci_tbl, 368c6fd2807SJeff Garzik .probe = sil24_init_one, 36924dc5f33STejun Heo .remove = ata_pci_remove_one, 37058eb8cd5SBartlomiej Zolnierkiewicz #ifdef CONFIG_PM_SLEEP 371c6fd2807SJeff Garzik .suspend = ata_pci_device_suspend, 372c6fd2807SJeff Garzik .resume = sil24_pci_device_resume, 373281d426cSAlexey Dobriyan #endif 374c6fd2807SJeff Garzik }; 375c6fd2807SJeff Garzik 376c6fd2807SJeff Garzik static struct scsi_host_template sil24_sht = { 377945a0e28SLee Jones __ATA_BASE_SHT(DRV_NAME), 378c6fd2807SJeff Garzik .can_queue = SIL24_MAX_CMDS, 37993e2618eSTejun Heo .sg_tablesize = SIL24_MAX_SGE, 380c6fd2807SJeff Garzik .dma_boundary = ATA_DMA_BOUNDARY, 3819269e234SShaohua Li .tag_alloc_policy = BLK_TAG_ALLOC_FIFO, 382*c3f69c7fSBart Van Assche .sdev_groups = ata_ncq_sdev_groups, 383945a0e28SLee Jones .change_queue_depth = ata_scsi_change_queue_depth, 384945a0e28SLee Jones .slave_configure = ata_scsi_slave_config 385c6fd2807SJeff Garzik }; 386c6fd2807SJeff Garzik 387029cfd6bSTejun Heo static struct ata_port_operations sil24_ops = { 388029cfd6bSTejun Heo .inherits = &sata_pmp_port_ops, 389c6fd2807SJeff Garzik 3903454dc69STejun Heo .qc_defer = sil24_qc_defer, 391c6fd2807SJeff Garzik .qc_prep = sil24_qc_prep, 392c6fd2807SJeff Garzik .qc_issue = sil24_qc_issue, 39379f97dadSTejun Heo .qc_fill_rtf = sil24_qc_fill_rtf, 394c6fd2807SJeff Garzik 395c6fd2807SJeff Garzik .freeze = sil24_freeze, 396c6fd2807SJeff Garzik .thaw = sil24_thaw, 397a1efdabaSTejun Heo .softreset = sil24_softreset, 398a1efdabaSTejun Heo .hardreset = sil24_hardreset, 399071f44b1STejun Heo .pmp_softreset = sil24_softreset, 400a1efdabaSTejun Heo .pmp_hardreset = sil24_pmp_hardreset, 401c6fd2807SJeff Garzik .error_handler = sil24_error_handler, 402c6fd2807SJeff Garzik .post_internal_cmd = sil24_post_internal_cmd, 403029cfd6bSTejun Heo .dev_config = sil24_dev_config, 404029cfd6bSTejun Heo 405029cfd6bSTejun Heo .scr_read = sil24_scr_read, 406029cfd6bSTejun Heo .scr_write = sil24_scr_write, 407029cfd6bSTejun Heo .pmp_attach = sil24_pmp_attach, 408029cfd6bSTejun Heo .pmp_detach = sil24_pmp_detach, 409c6fd2807SJeff Garzik 410c6fd2807SJeff Garzik .port_start = sil24_port_start, 4113454dc69STejun Heo #ifdef CONFIG_PM 4123454dc69STejun Heo .port_resume = sil24_port_resume, 4133454dc69STejun Heo #endif 414c6fd2807SJeff Garzik }; 415c6fd2807SJeff Garzik 41690ab5ee9SRusty Russell static bool sata_sil24_msi; /* Disable MSI */ 417dae77214SVivek Mahajan module_param_named(msi, sata_sil24_msi, bool, S_IRUGO); 418dae77214SVivek Mahajan MODULE_PARM_DESC(msi, "Enable MSI (Default: false)"); 419dae77214SVivek Mahajan 420c6fd2807SJeff Garzik /* 421cca3974eSJeff Garzik * Use bits 30-31 of port_flags to encode available port numbers. 422c6fd2807SJeff Garzik * Current maxium is 4. 423c6fd2807SJeff Garzik */ 424c6fd2807SJeff Garzik #define SIL24_NPORTS2FLAG(nports) ((((unsigned)(nports) - 1) & 0x3) << 30) 425c6fd2807SJeff Garzik #define SIL24_FLAG2NPORTS(flag) ((((flag) >> 30) & 0x3) + 1) 426c6fd2807SJeff Garzik 4274447d351STejun Heo static const struct ata_port_info sil24_port_info[] = { 428c6fd2807SJeff Garzik /* sil_3124 */ 429c6fd2807SJeff Garzik { 430cca3974eSJeff Garzik .flags = SIL24_COMMON_FLAGS | SIL24_NPORTS2FLAG(4) | 431c6fd2807SJeff Garzik SIL24_FLAG_PCIX_IRQ_WOC, 43214bdef98SErik Inge Bolsø .pio_mask = ATA_PIO4, 43314bdef98SErik Inge Bolsø .mwdma_mask = ATA_MWDMA2, 43414bdef98SErik Inge Bolsø .udma_mask = ATA_UDMA5, 435c6fd2807SJeff Garzik .port_ops = &sil24_ops, 436c6fd2807SJeff Garzik }, 437c6fd2807SJeff Garzik /* sil_3132 */ 438c6fd2807SJeff Garzik { 439cca3974eSJeff Garzik .flags = SIL24_COMMON_FLAGS | SIL24_NPORTS2FLAG(2), 44014bdef98SErik Inge Bolsø .pio_mask = ATA_PIO4, 44114bdef98SErik Inge Bolsø .mwdma_mask = ATA_MWDMA2, 44214bdef98SErik Inge Bolsø .udma_mask = ATA_UDMA5, 443c6fd2807SJeff Garzik .port_ops = &sil24_ops, 444c6fd2807SJeff Garzik }, 445c6fd2807SJeff Garzik /* sil_3131/sil_3531 */ 446c6fd2807SJeff Garzik { 447cca3974eSJeff Garzik .flags = SIL24_COMMON_FLAGS | SIL24_NPORTS2FLAG(1), 44814bdef98SErik Inge Bolsø .pio_mask = ATA_PIO4, 44914bdef98SErik Inge Bolsø .mwdma_mask = ATA_MWDMA2, 45014bdef98SErik Inge Bolsø .udma_mask = ATA_UDMA5, 451c6fd2807SJeff Garzik .port_ops = &sil24_ops, 452c6fd2807SJeff Garzik }, 453c6fd2807SJeff Garzik }; 454c6fd2807SJeff Garzik 455c6fd2807SJeff Garzik static int sil24_tag(int tag) 456c6fd2807SJeff Garzik { 457c6fd2807SJeff Garzik if (unlikely(ata_tag_internal(tag))) 458c6fd2807SJeff Garzik return 0; 459c6fd2807SJeff Garzik return tag; 460c6fd2807SJeff Garzik } 461c6fd2807SJeff Garzik 462350756f6STejun Heo static unsigned long sil24_port_offset(struct ata_port *ap) 463350756f6STejun Heo { 464350756f6STejun Heo return ap->port_no * PORT_REGS_SIZE; 465350756f6STejun Heo } 466350756f6STejun Heo 467350756f6STejun Heo static void __iomem *sil24_port_base(struct ata_port *ap) 468350756f6STejun Heo { 469350756f6STejun Heo return ap->host->iomap[SIL24_PORT_BAR] + sil24_port_offset(ap); 470350756f6STejun Heo } 471350756f6STejun Heo 472cd0d3bbcSAlan static void sil24_dev_config(struct ata_device *dev) 473c6fd2807SJeff Garzik { 474350756f6STejun Heo void __iomem *port = sil24_port_base(dev->link->ap); 475c6fd2807SJeff Garzik 476c6fd2807SJeff Garzik if (dev->cdb_len == 16) 477c6fd2807SJeff Garzik writel(PORT_CS_CDB16, port + PORT_CTRL_STAT); 478c6fd2807SJeff Garzik else 479c6fd2807SJeff Garzik writel(PORT_CS_CDB16, port + PORT_CTRL_CLR); 480c6fd2807SJeff Garzik } 481c6fd2807SJeff Garzik 482e59f0dadSTejun Heo static void sil24_read_tf(struct ata_port *ap, int tag, struct ata_taskfile *tf) 483c6fd2807SJeff Garzik { 484350756f6STejun Heo void __iomem *port = sil24_port_base(ap); 485e59f0dadSTejun Heo struct sil24_prb __iomem *prb; 486c6fd2807SJeff Garzik u8 fis[6 * 4]; 487c6fd2807SJeff Garzik 488e59f0dadSTejun Heo prb = port + PORT_LRAM + sil24_tag(tag) * PORT_LRAM_SLOT_SZ; 489e59f0dadSTejun Heo memcpy_fromio(fis, prb->fis, sizeof(fis)); 490e59f0dadSTejun Heo ata_tf_from_fis(fis, tf); 491c6fd2807SJeff Garzik } 492c6fd2807SJeff Garzik 493c6fd2807SJeff Garzik static int sil24_scr_map[] = { 494c6fd2807SJeff Garzik [SCR_CONTROL] = 0, 495c6fd2807SJeff Garzik [SCR_STATUS] = 1, 496c6fd2807SJeff Garzik [SCR_ERROR] = 2, 497c6fd2807SJeff Garzik [SCR_ACTIVE] = 3, 498c6fd2807SJeff Garzik }; 499c6fd2807SJeff Garzik 50082ef04fbSTejun Heo static int sil24_scr_read(struct ata_link *link, unsigned sc_reg, u32 *val) 501c6fd2807SJeff Garzik { 50282ef04fbSTejun Heo void __iomem *scr_addr = sil24_port_base(link->ap) + PORT_SCONTROL; 503da3dbb17STejun Heo 504c6fd2807SJeff Garzik if (sc_reg < ARRAY_SIZE(sil24_scr_map)) { 505da3dbb17STejun Heo *val = readl(scr_addr + sil24_scr_map[sc_reg] * 4); 506da3dbb17STejun Heo return 0; 507c6fd2807SJeff Garzik } 508da3dbb17STejun Heo return -EINVAL; 509c6fd2807SJeff Garzik } 510c6fd2807SJeff Garzik 51182ef04fbSTejun Heo static int sil24_scr_write(struct ata_link *link, unsigned sc_reg, u32 val) 512c6fd2807SJeff Garzik { 51382ef04fbSTejun Heo void __iomem *scr_addr = sil24_port_base(link->ap) + PORT_SCONTROL; 514da3dbb17STejun Heo 515c6fd2807SJeff Garzik if (sc_reg < ARRAY_SIZE(sil24_scr_map)) { 516c6fd2807SJeff Garzik writel(val, scr_addr + sil24_scr_map[sc_reg] * 4); 517da3dbb17STejun Heo return 0; 518c6fd2807SJeff Garzik } 519da3dbb17STejun Heo return -EINVAL; 520c6fd2807SJeff Garzik } 521c6fd2807SJeff Garzik 52223818034STejun Heo static void sil24_config_port(struct ata_port *ap) 52323818034STejun Heo { 524350756f6STejun Heo void __iomem *port = sil24_port_base(ap); 52523818034STejun Heo 52623818034STejun Heo /* configure IRQ WoC */ 52723818034STejun Heo if (ap->flags & SIL24_FLAG_PCIX_IRQ_WOC) 52823818034STejun Heo writel(PORT_CS_IRQ_WOC, port + PORT_CTRL_STAT); 52923818034STejun Heo else 53023818034STejun Heo writel(PORT_CS_IRQ_WOC, port + PORT_CTRL_CLR); 53123818034STejun Heo 53223818034STejun Heo /* zero error counters. */ 5337a4f876bSColin Tuckley writew(0x8000, port + PORT_DECODE_ERR_THRESH); 5347a4f876bSColin Tuckley writew(0x8000, port + PORT_CRC_ERR_THRESH); 5357a4f876bSColin Tuckley writew(0x8000, port + PORT_HSHK_ERR_THRESH); 5367a4f876bSColin Tuckley writew(0x0000, port + PORT_DECODE_ERR_CNT); 5377a4f876bSColin Tuckley writew(0x0000, port + PORT_CRC_ERR_CNT); 5387a4f876bSColin Tuckley writew(0x0000, port + PORT_HSHK_ERR_CNT); 53923818034STejun Heo 54023818034STejun Heo /* always use 64bit activation */ 54123818034STejun Heo writel(PORT_CS_32BIT_ACTV, port + PORT_CTRL_CLR); 54223818034STejun Heo 54323818034STejun Heo /* clear port multiplier enable and resume bits */ 54423818034STejun Heo writel(PORT_CS_PMP_EN | PORT_CS_PMP_RESUME, port + PORT_CTRL_CLR); 54523818034STejun Heo } 54623818034STejun Heo 5473454dc69STejun Heo static void sil24_config_pmp(struct ata_port *ap, int attached) 5483454dc69STejun Heo { 549350756f6STejun Heo void __iomem *port = sil24_port_base(ap); 5503454dc69STejun Heo 5513454dc69STejun Heo if (attached) 5523454dc69STejun Heo writel(PORT_CS_PMP_EN, port + PORT_CTRL_STAT); 5533454dc69STejun Heo else 5543454dc69STejun Heo writel(PORT_CS_PMP_EN, port + PORT_CTRL_CLR); 5553454dc69STejun Heo } 5563454dc69STejun Heo 5573454dc69STejun Heo static void sil24_clear_pmp(struct ata_port *ap) 5583454dc69STejun Heo { 559350756f6STejun Heo void __iomem *port = sil24_port_base(ap); 5603454dc69STejun Heo int i; 5613454dc69STejun Heo 5623454dc69STejun Heo writel(PORT_CS_PMP_RESUME, port + PORT_CTRL_CLR); 5633454dc69STejun Heo 5643454dc69STejun Heo for (i = 0; i < SATA_PMP_MAX_PORTS; i++) { 5653454dc69STejun Heo void __iomem *pmp_base = port + PORT_PMP + i * PORT_PMP_SIZE; 5663454dc69STejun Heo 5673454dc69STejun Heo writel(0, pmp_base + PORT_PMP_STATUS); 5683454dc69STejun Heo writel(0, pmp_base + PORT_PMP_QACTIVE); 5693454dc69STejun Heo } 5703454dc69STejun Heo } 5713454dc69STejun Heo 572c6fd2807SJeff Garzik static int sil24_init_port(struct ata_port *ap) 573c6fd2807SJeff Garzik { 574350756f6STejun Heo void __iomem *port = sil24_port_base(ap); 57523818034STejun Heo struct sil24_port_priv *pp = ap->private_data; 576c6fd2807SJeff Garzik u32 tmp; 577c6fd2807SJeff Garzik 5783454dc69STejun Heo /* clear PMP error status */ 579071f44b1STejun Heo if (sata_pmp_attached(ap)) 5803454dc69STejun Heo sil24_clear_pmp(ap); 5813454dc69STejun Heo 582c6fd2807SJeff Garzik writel(PORT_CS_INIT, port + PORT_CTRL_STAT); 58397750cebSTejun Heo ata_wait_register(ap, port + PORT_CTRL_STAT, 584c6fd2807SJeff Garzik PORT_CS_INIT, PORT_CS_INIT, 10, 100); 58597750cebSTejun Heo tmp = ata_wait_register(ap, port + PORT_CTRL_STAT, 586c6fd2807SJeff Garzik PORT_CS_RDY, 0, 10, 100); 587c6fd2807SJeff Garzik 58823818034STejun Heo if ((tmp & (PORT_CS_INIT | PORT_CS_RDY)) != PORT_CS_RDY) { 58923818034STejun Heo pp->do_port_rst = 1; 590cf480626STejun Heo ap->link.eh_context.i.action |= ATA_EH_RESET; 591c6fd2807SJeff Garzik return -EIO; 59223818034STejun Heo } 59323818034STejun Heo 594c6fd2807SJeff Garzik return 0; 595c6fd2807SJeff Garzik } 596c6fd2807SJeff Garzik 59737b99cbaSTejun Heo static int sil24_exec_polled_cmd(struct ata_port *ap, int pmp, 59837b99cbaSTejun Heo const struct ata_taskfile *tf, 59937b99cbaSTejun Heo int is_cmd, u32 ctrl, 60037b99cbaSTejun Heo unsigned long timeout_msec) 601c6fd2807SJeff Garzik { 602350756f6STejun Heo void __iomem *port = sil24_port_base(ap); 603c6fd2807SJeff Garzik struct sil24_port_priv *pp = ap->private_data; 604c6fd2807SJeff Garzik struct sil24_prb *prb = &pp->cmd_block[0].ata.prb; 605c6fd2807SJeff Garzik dma_addr_t paddr = pp->cmd_block_dma; 60637b99cbaSTejun Heo u32 irq_enabled, irq_mask, irq_stat; 60737b99cbaSTejun Heo int rc; 60837b99cbaSTejun Heo 60937b99cbaSTejun Heo prb->ctrl = cpu_to_le16(ctrl); 61037b99cbaSTejun Heo ata_tf_to_fis(tf, pmp, is_cmd, prb->fis); 61137b99cbaSTejun Heo 61237b99cbaSTejun Heo /* temporarily plug completion and error interrupts */ 61337b99cbaSTejun Heo irq_enabled = readl(port + PORT_IRQ_ENABLE_SET); 61437b99cbaSTejun Heo writel(PORT_IRQ_COMPLETE | PORT_IRQ_ERROR, port + PORT_IRQ_ENABLE_CLR); 61537b99cbaSTejun Heo 61610823452SCatalin Marinas /* 61710823452SCatalin Marinas * The barrier is required to ensure that writes to cmd_block reach 61810823452SCatalin Marinas * the memory before the write to PORT_CMD_ACTIVATE. 61910823452SCatalin Marinas */ 62010823452SCatalin Marinas wmb(); 62137b99cbaSTejun Heo writel((u32)paddr, port + PORT_CMD_ACTIVATE); 62237b99cbaSTejun Heo writel((u64)paddr >> 32, port + PORT_CMD_ACTIVATE + 4); 62337b99cbaSTejun Heo 62437b99cbaSTejun Heo irq_mask = (PORT_IRQ_COMPLETE | PORT_IRQ_ERROR) << PORT_IRQ_RAW_SHIFT; 62597750cebSTejun Heo irq_stat = ata_wait_register(ap, port + PORT_IRQ_STAT, irq_mask, 0x0, 62637b99cbaSTejun Heo 10, timeout_msec); 62737b99cbaSTejun Heo 62837b99cbaSTejun Heo writel(irq_mask, port + PORT_IRQ_STAT); /* clear IRQs */ 62937b99cbaSTejun Heo irq_stat >>= PORT_IRQ_RAW_SHIFT; 63037b99cbaSTejun Heo 63137b99cbaSTejun Heo if (irq_stat & PORT_IRQ_COMPLETE) 63237b99cbaSTejun Heo rc = 0; 63337b99cbaSTejun Heo else { 63437b99cbaSTejun Heo /* force port into known state */ 63537b99cbaSTejun Heo sil24_init_port(ap); 63637b99cbaSTejun Heo 63737b99cbaSTejun Heo if (irq_stat & PORT_IRQ_ERROR) 63837b99cbaSTejun Heo rc = -EIO; 63937b99cbaSTejun Heo else 64037b99cbaSTejun Heo rc = -EBUSY; 64137b99cbaSTejun Heo } 64237b99cbaSTejun Heo 64337b99cbaSTejun Heo /* restore IRQ enabled */ 64437b99cbaSTejun Heo writel(irq_enabled, port + PORT_IRQ_ENABLE_SET); 64537b99cbaSTejun Heo 64637b99cbaSTejun Heo return rc; 64737b99cbaSTejun Heo } 64837b99cbaSTejun Heo 649071f44b1STejun Heo static int sil24_softreset(struct ata_link *link, unsigned int *class, 650071f44b1STejun Heo unsigned long deadline) 65137b99cbaSTejun Heo { 652cc0680a5STejun Heo struct ata_port *ap = link->ap; 653071f44b1STejun Heo int pmp = sata_srst_pmp(link); 65437b99cbaSTejun Heo unsigned long timeout_msec = 0; 655e59f0dadSTejun Heo struct ata_taskfile tf; 656c6fd2807SJeff Garzik const char *reason; 65737b99cbaSTejun Heo int rc; 658c6fd2807SJeff Garzik 659c6fd2807SJeff Garzik DPRINTK("ENTER\n"); 660c6fd2807SJeff Garzik 661c6fd2807SJeff Garzik /* put the port into known state */ 662c6fd2807SJeff Garzik if (sil24_init_port(ap)) { 663c6fd2807SJeff Garzik reason = "port not ready"; 664c6fd2807SJeff Garzik goto err; 665c6fd2807SJeff Garzik } 666c6fd2807SJeff Garzik 667c6fd2807SJeff Garzik /* do SRST */ 66837b99cbaSTejun Heo if (time_after(deadline, jiffies)) 66937b99cbaSTejun Heo timeout_msec = jiffies_to_msecs(deadline - jiffies); 670c6fd2807SJeff Garzik 671cc0680a5STejun Heo ata_tf_init(link->device, &tf); /* doesn't really matter */ 672975530e8STejun Heo rc = sil24_exec_polled_cmd(ap, pmp, &tf, 0, PRB_CTRL_SRST, 673975530e8STejun Heo timeout_msec); 67437b99cbaSTejun Heo if (rc == -EBUSY) { 675c6fd2807SJeff Garzik reason = "timeout"; 676c6fd2807SJeff Garzik goto err; 67737b99cbaSTejun Heo } else if (rc) { 67837b99cbaSTejun Heo reason = "SRST command error"; 67937b99cbaSTejun Heo goto err; 680c6fd2807SJeff Garzik } 681c6fd2807SJeff Garzik 682e59f0dadSTejun Heo sil24_read_tf(ap, 0, &tf); 683e59f0dadSTejun Heo *class = ata_dev_classify(&tf); 684c6fd2807SJeff Garzik 685c6fd2807SJeff Garzik DPRINTK("EXIT, class=%u\n", *class); 686c6fd2807SJeff Garzik return 0; 687c6fd2807SJeff Garzik 688c6fd2807SJeff Garzik err: 689a9a79dfeSJoe Perches ata_link_err(link, "softreset failed (%s)\n", reason); 690c6fd2807SJeff Garzik return -EIO; 691c6fd2807SJeff Garzik } 692c6fd2807SJeff Garzik 693cc0680a5STejun Heo static int sil24_hardreset(struct ata_link *link, unsigned int *class, 694d4b2bab4STejun Heo unsigned long deadline) 695c6fd2807SJeff Garzik { 696cc0680a5STejun Heo struct ata_port *ap = link->ap; 697350756f6STejun Heo void __iomem *port = sil24_port_base(ap); 69823818034STejun Heo struct sil24_port_priv *pp = ap->private_data; 69923818034STejun Heo int did_port_rst = 0; 700c6fd2807SJeff Garzik const char *reason; 701c6fd2807SJeff Garzik int tout_msec, rc; 702c6fd2807SJeff Garzik u32 tmp; 703c6fd2807SJeff Garzik 70423818034STejun Heo retry: 70523818034STejun Heo /* Sometimes, DEV_RST is not enough to recover the controller. 70623818034STejun Heo * This happens often after PM DMA CS errata. 70723818034STejun Heo */ 70823818034STejun Heo if (pp->do_port_rst) { 709a9a79dfeSJoe Perches ata_port_warn(ap, 710a9a79dfeSJoe Perches "controller in dubious state, performing PORT_RST\n"); 71123818034STejun Heo 71223818034STejun Heo writel(PORT_CS_PORT_RST, port + PORT_CTRL_STAT); 71397750cebSTejun Heo ata_msleep(ap, 10); 71423818034STejun Heo writel(PORT_CS_PORT_RST, port + PORT_CTRL_CLR); 71597750cebSTejun Heo ata_wait_register(ap, port + PORT_CTRL_STAT, PORT_CS_RDY, 0, 71623818034STejun Heo 10, 5000); 71723818034STejun Heo 71823818034STejun Heo /* restore port configuration */ 71923818034STejun Heo sil24_config_port(ap); 72023818034STejun Heo sil24_config_pmp(ap, ap->nr_pmp_links); 72123818034STejun Heo 72223818034STejun Heo pp->do_port_rst = 0; 72323818034STejun Heo did_port_rst = 1; 72423818034STejun Heo } 72523818034STejun Heo 726c6fd2807SJeff Garzik /* sil24 does the right thing(tm) without any protection */ 727cc0680a5STejun Heo sata_set_spd(link); 728c6fd2807SJeff Garzik 729c6fd2807SJeff Garzik tout_msec = 100; 730cc0680a5STejun Heo if (ata_link_online(link)) 731c6fd2807SJeff Garzik tout_msec = 5000; 732c6fd2807SJeff Garzik 733c6fd2807SJeff Garzik writel(PORT_CS_DEV_RST, port + PORT_CTRL_STAT); 73497750cebSTejun Heo tmp = ata_wait_register(ap, port + PORT_CTRL_STAT, 7355796d1c4SJeff Garzik PORT_CS_DEV_RST, PORT_CS_DEV_RST, 10, 7365796d1c4SJeff Garzik tout_msec); 737c6fd2807SJeff Garzik 738c6fd2807SJeff Garzik /* SStatus oscillates between zero and valid status after 739c6fd2807SJeff Garzik * DEV_RST, debounce it. 740c6fd2807SJeff Garzik */ 741cc0680a5STejun Heo rc = sata_link_debounce(link, sata_deb_timing_long, deadline); 742c6fd2807SJeff Garzik if (rc) { 743c6fd2807SJeff Garzik reason = "PHY debouncing failed"; 744c6fd2807SJeff Garzik goto err; 745c6fd2807SJeff Garzik } 746c6fd2807SJeff Garzik 747c6fd2807SJeff Garzik if (tmp & PORT_CS_DEV_RST) { 748cc0680a5STejun Heo if (ata_link_offline(link)) 749c6fd2807SJeff Garzik return 0; 750c6fd2807SJeff Garzik reason = "link not ready"; 751c6fd2807SJeff Garzik goto err; 752c6fd2807SJeff Garzik } 753c6fd2807SJeff Garzik 754c6fd2807SJeff Garzik /* Sil24 doesn't store signature FIS after hardreset, so we 755c6fd2807SJeff Garzik * can't wait for BSY to clear. Some devices take a long time 756c6fd2807SJeff Garzik * to get ready and those devices will choke if we don't wait 757c6fd2807SJeff Garzik * for BSY clearance here. Tell libata to perform follow-up 758c6fd2807SJeff Garzik * softreset. 759c6fd2807SJeff Garzik */ 760c6fd2807SJeff Garzik return -EAGAIN; 761c6fd2807SJeff Garzik 762c6fd2807SJeff Garzik err: 76323818034STejun Heo if (!did_port_rst) { 76423818034STejun Heo pp->do_port_rst = 1; 76523818034STejun Heo goto retry; 76623818034STejun Heo } 76723818034STejun Heo 768a9a79dfeSJoe Perches ata_link_err(link, "hardreset failed (%s)\n", reason); 769c6fd2807SJeff Garzik return -EIO; 770c6fd2807SJeff Garzik } 771c6fd2807SJeff Garzik 772c6fd2807SJeff Garzik static inline void sil24_fill_sg(struct ata_queued_cmd *qc, 773c6fd2807SJeff Garzik struct sil24_sge *sge) 774c6fd2807SJeff Garzik { 775c6fd2807SJeff Garzik struct scatterlist *sg; 7763be6cbd7SJeff Garzik struct sil24_sge *last_sge = NULL; 777ff2aeb1eSTejun Heo unsigned int si; 778c6fd2807SJeff Garzik 779ff2aeb1eSTejun Heo for_each_sg(qc->sg, sg, qc->n_elem, si) { 780c6fd2807SJeff Garzik sge->addr = cpu_to_le64(sg_dma_address(sg)); 781c6fd2807SJeff Garzik sge->cnt = cpu_to_le32(sg_dma_len(sg)); 782c6fd2807SJeff Garzik sge->flags = 0; 7833be6cbd7SJeff Garzik 7843be6cbd7SJeff Garzik last_sge = sge; 785c6fd2807SJeff Garzik sge++; 786c6fd2807SJeff Garzik } 7873be6cbd7SJeff Garzik 7883be6cbd7SJeff Garzik last_sge->flags = cpu_to_le32(SGE_TRM); 789c6fd2807SJeff Garzik } 790c6fd2807SJeff Garzik 7913454dc69STejun Heo static int sil24_qc_defer(struct ata_queued_cmd *qc) 7923454dc69STejun Heo { 7933454dc69STejun Heo struct ata_link *link = qc->dev->link; 7943454dc69STejun Heo struct ata_port *ap = link->ap; 7953454dc69STejun Heo u8 prot = qc->tf.protocol; 7963454dc69STejun Heo 79713cc546bSGwendal Grignou /* 79813cc546bSGwendal Grignou * There is a bug in the chip: 79913cc546bSGwendal Grignou * Port LRAM Causes the PRB/SGT Data to be Corrupted 80013cc546bSGwendal Grignou * If the host issues a read request for LRAM and SActive registers 80113cc546bSGwendal Grignou * while active commands are available in the port, PRB/SGT data in 80213cc546bSGwendal Grignou * the LRAM can become corrupted. This issue applies only when 80313cc546bSGwendal Grignou * reading from, but not writing to, the LRAM. 80413cc546bSGwendal Grignou * 80513cc546bSGwendal Grignou * Therefore, reading LRAM when there is no particular error [and 80613cc546bSGwendal Grignou * other commands may be outstanding] is prohibited. 80713cc546bSGwendal Grignou * 80813cc546bSGwendal Grignou * To avoid this bug there are two situations where a command must run 80913cc546bSGwendal Grignou * exclusive of any other commands on the port: 81013cc546bSGwendal Grignou * 81113cc546bSGwendal Grignou * - ATAPI commands which check the sense data 81213cc546bSGwendal Grignou * - Passthrough ATA commands which always have ATA_QCFLAG_RESULT_TF 81313cc546bSGwendal Grignou * set. 81413cc546bSGwendal Grignou * 8153454dc69STejun Heo */ 816405e66b3STejun Heo int is_excl = (ata_is_atapi(prot) || 81713cc546bSGwendal Grignou (qc->flags & ATA_QCFLAG_RESULT_TF)); 81813cc546bSGwendal Grignou 8193454dc69STejun Heo if (unlikely(ap->excl_link)) { 8203454dc69STejun Heo if (link == ap->excl_link) { 8213454dc69STejun Heo if (ap->nr_active_links) 8223454dc69STejun Heo return ATA_DEFER_PORT; 8233454dc69STejun Heo qc->flags |= ATA_QCFLAG_CLEAR_EXCL; 8243454dc69STejun Heo } else 8253454dc69STejun Heo return ATA_DEFER_PORT; 82613cc546bSGwendal Grignou } else if (unlikely(is_excl)) { 8273454dc69STejun Heo ap->excl_link = link; 8283454dc69STejun Heo if (ap->nr_active_links) 8293454dc69STejun Heo return ATA_DEFER_PORT; 8303454dc69STejun Heo qc->flags |= ATA_QCFLAG_CLEAR_EXCL; 8313454dc69STejun Heo } 8323454dc69STejun Heo 8333454dc69STejun Heo return ata_std_qc_defer(qc); 8343454dc69STejun Heo } 8353454dc69STejun Heo 83695364f36SJiri Slaby static enum ata_completion_errors sil24_qc_prep(struct ata_queued_cmd *qc) 837c6fd2807SJeff Garzik { 838c6fd2807SJeff Garzik struct ata_port *ap = qc->ap; 839c6fd2807SJeff Garzik struct sil24_port_priv *pp = ap->private_data; 840c6fd2807SJeff Garzik union sil24_cmd_block *cb; 841c6fd2807SJeff Garzik struct sil24_prb *prb; 842c6fd2807SJeff Garzik struct sil24_sge *sge; 843c6fd2807SJeff Garzik u16 ctrl = 0; 844c6fd2807SJeff Garzik 8454e5b6260SJens Axboe cb = &pp->cmd_block[sil24_tag(qc->hw_tag)]; 846c6fd2807SJeff Garzik 847405e66b3STejun Heo if (!ata_is_atapi(qc->tf.protocol)) { 848c6fd2807SJeff Garzik prb = &cb->ata.prb; 849c6fd2807SJeff Garzik sge = cb->ata.sge; 8504f1a0ee1SRobert Hancock if (ata_is_data(qc->tf.protocol)) { 8514f1a0ee1SRobert Hancock u16 prot = 0; 8524f1a0ee1SRobert Hancock ctrl = PRB_CTRL_PROTOCOL; 8534f1a0ee1SRobert Hancock if (ata_is_ncq(qc->tf.protocol)) 8544f1a0ee1SRobert Hancock prot |= PRB_PROT_NCQ; 8554f1a0ee1SRobert Hancock if (qc->tf.flags & ATA_TFLAG_WRITE) 8564f1a0ee1SRobert Hancock prot |= PRB_PROT_WRITE; 8574f1a0ee1SRobert Hancock else 8584f1a0ee1SRobert Hancock prot |= PRB_PROT_READ; 8594f1a0ee1SRobert Hancock prb->prot = cpu_to_le16(prot); 8604f1a0ee1SRobert Hancock } 861405e66b3STejun Heo } else { 862c6fd2807SJeff Garzik prb = &cb->atapi.prb; 863c6fd2807SJeff Garzik sge = cb->atapi.sge; 86414e45c15SDan Carpenter memset(cb->atapi.cdb, 0, sizeof(cb->atapi.cdb)); 865c6fd2807SJeff Garzik memcpy(cb->atapi.cdb, qc->cdb, qc->dev->cdb_len); 866c6fd2807SJeff Garzik 867405e66b3STejun Heo if (ata_is_data(qc->tf.protocol)) { 868c6fd2807SJeff Garzik if (qc->tf.flags & ATA_TFLAG_WRITE) 869c6fd2807SJeff Garzik ctrl = PRB_CTRL_PACKET_WRITE; 870c6fd2807SJeff Garzik else 871c6fd2807SJeff Garzik ctrl = PRB_CTRL_PACKET_READ; 872c6fd2807SJeff Garzik } 873c6fd2807SJeff Garzik } 874c6fd2807SJeff Garzik 875c6fd2807SJeff Garzik prb->ctrl = cpu_to_le16(ctrl); 8763454dc69STejun Heo ata_tf_to_fis(&qc->tf, qc->dev->link->pmp, 1, prb->fis); 877c6fd2807SJeff Garzik 878c6fd2807SJeff Garzik if (qc->flags & ATA_QCFLAG_DMAMAP) 879c6fd2807SJeff Garzik sil24_fill_sg(qc, sge); 88095364f36SJiri Slaby 88195364f36SJiri Slaby return AC_ERR_OK; 882c6fd2807SJeff Garzik } 883c6fd2807SJeff Garzik 884c6fd2807SJeff Garzik static unsigned int sil24_qc_issue(struct ata_queued_cmd *qc) 885c6fd2807SJeff Garzik { 886c6fd2807SJeff Garzik struct ata_port *ap = qc->ap; 887c6fd2807SJeff Garzik struct sil24_port_priv *pp = ap->private_data; 888350756f6STejun Heo void __iomem *port = sil24_port_base(ap); 8894e5b6260SJens Axboe unsigned int tag = sil24_tag(qc->hw_tag); 890c6fd2807SJeff Garzik dma_addr_t paddr; 891c6fd2807SJeff Garzik void __iomem *activate; 892c6fd2807SJeff Garzik 893c6fd2807SJeff Garzik paddr = pp->cmd_block_dma + tag * sizeof(*pp->cmd_block); 894c6fd2807SJeff Garzik activate = port + PORT_CMD_ACTIVATE + tag * 8; 895c6fd2807SJeff Garzik 89610823452SCatalin Marinas /* 89710823452SCatalin Marinas * The barrier is required to ensure that writes to cmd_block reach 89810823452SCatalin Marinas * the memory before the write to PORT_CMD_ACTIVATE. 89910823452SCatalin Marinas */ 90010823452SCatalin Marinas wmb(); 901c6fd2807SJeff Garzik writel((u32)paddr, activate); 902c6fd2807SJeff Garzik writel((u64)paddr >> 32, activate + 4); 903c6fd2807SJeff Garzik 904c6fd2807SJeff Garzik return 0; 905c6fd2807SJeff Garzik } 906c6fd2807SJeff Garzik 90779f97dadSTejun Heo static bool sil24_qc_fill_rtf(struct ata_queued_cmd *qc) 90879f97dadSTejun Heo { 9094e5b6260SJens Axboe sil24_read_tf(qc->ap, qc->hw_tag, &qc->result_tf); 91079f97dadSTejun Heo return true; 91179f97dadSTejun Heo } 91279f97dadSTejun Heo 9133454dc69STejun Heo static void sil24_pmp_attach(struct ata_port *ap) 9143454dc69STejun Heo { 915906c1ff4STejun Heo u32 *gscr = ap->link.device->gscr; 916906c1ff4STejun Heo 9173454dc69STejun Heo sil24_config_pmp(ap, 1); 9183454dc69STejun Heo sil24_init_port(ap); 919906c1ff4STejun Heo 920906c1ff4STejun Heo if (sata_pmp_gscr_vendor(gscr) == 0x11ab && 921906c1ff4STejun Heo sata_pmp_gscr_devid(gscr) == 0x4140) { 922a9a79dfeSJoe Perches ata_port_info(ap, 923906c1ff4STejun Heo "disabling NCQ support due to sil24-mv4140 quirk\n"); 924906c1ff4STejun Heo ap->flags &= ~ATA_FLAG_NCQ; 925906c1ff4STejun Heo } 9263454dc69STejun Heo } 9273454dc69STejun Heo 9283454dc69STejun Heo static void sil24_pmp_detach(struct ata_port *ap) 9293454dc69STejun Heo { 9303454dc69STejun Heo sil24_init_port(ap); 9313454dc69STejun Heo sil24_config_pmp(ap, 0); 932906c1ff4STejun Heo 933906c1ff4STejun Heo ap->flags |= ATA_FLAG_NCQ; 9343454dc69STejun Heo } 9353454dc69STejun Heo 9363454dc69STejun Heo static int sil24_pmp_hardreset(struct ata_link *link, unsigned int *class, 9373454dc69STejun Heo unsigned long deadline) 9383454dc69STejun Heo { 9393454dc69STejun Heo int rc; 9403454dc69STejun Heo 9413454dc69STejun Heo rc = sil24_init_port(link->ap); 9423454dc69STejun Heo if (rc) { 943a9a79dfeSJoe Perches ata_link_err(link, "hardreset failed (port not ready)\n"); 9443454dc69STejun Heo return rc; 9453454dc69STejun Heo } 9463454dc69STejun Heo 9475958e302STejun Heo return sata_std_hardreset(link, class, deadline); 9483454dc69STejun Heo } 9493454dc69STejun Heo 950c6fd2807SJeff Garzik static void sil24_freeze(struct ata_port *ap) 951c6fd2807SJeff Garzik { 952350756f6STejun Heo void __iomem *port = sil24_port_base(ap); 953c6fd2807SJeff Garzik 954c6fd2807SJeff Garzik /* Port-wide IRQ mask in HOST_CTRL doesn't really work, clear 955c6fd2807SJeff Garzik * PORT_IRQ_ENABLE instead. 956c6fd2807SJeff Garzik */ 957c6fd2807SJeff Garzik writel(0xffff, port + PORT_IRQ_ENABLE_CLR); 958c6fd2807SJeff Garzik } 959c6fd2807SJeff Garzik 960c6fd2807SJeff Garzik static void sil24_thaw(struct ata_port *ap) 961c6fd2807SJeff Garzik { 962350756f6STejun Heo void __iomem *port = sil24_port_base(ap); 963c6fd2807SJeff Garzik u32 tmp; 964c6fd2807SJeff Garzik 965c6fd2807SJeff Garzik /* clear IRQ */ 966c6fd2807SJeff Garzik tmp = readl(port + PORT_IRQ_STAT); 967c6fd2807SJeff Garzik writel(tmp, port + PORT_IRQ_STAT); 968c6fd2807SJeff Garzik 969c6fd2807SJeff Garzik /* turn IRQ back on */ 970c6fd2807SJeff Garzik writel(DEF_PORT_IRQ, port + PORT_IRQ_ENABLE_SET); 971c6fd2807SJeff Garzik } 972c6fd2807SJeff Garzik 973c6fd2807SJeff Garzik static void sil24_error_intr(struct ata_port *ap) 974c6fd2807SJeff Garzik { 975350756f6STejun Heo void __iomem *port = sil24_port_base(ap); 976e59f0dadSTejun Heo struct sil24_port_priv *pp = ap->private_data; 9773454dc69STejun Heo struct ata_queued_cmd *qc = NULL; 9783454dc69STejun Heo struct ata_link *link; 9793454dc69STejun Heo struct ata_eh_info *ehi; 9803454dc69STejun Heo int abort = 0, freeze = 0; 981c6fd2807SJeff Garzik u32 irq_stat; 982c6fd2807SJeff Garzik 983c6fd2807SJeff Garzik /* on error, we need to clear IRQ explicitly */ 984c6fd2807SJeff Garzik irq_stat = readl(port + PORT_IRQ_STAT); 985c6fd2807SJeff Garzik writel(irq_stat, port + PORT_IRQ_STAT); 986c6fd2807SJeff Garzik 987c6fd2807SJeff Garzik /* first, analyze and record host port events */ 9883454dc69STejun Heo link = &ap->link; 9893454dc69STejun Heo ehi = &link->eh_info; 990c6fd2807SJeff Garzik ata_ehi_clear_desc(ehi); 991c6fd2807SJeff Garzik 992c6fd2807SJeff Garzik ata_ehi_push_desc(ehi, "irq_stat 0x%08x", irq_stat); 993c6fd2807SJeff Garzik 994854c73a2STejun Heo if (irq_stat & PORT_IRQ_SDB_NOTIFY) { 995854c73a2STejun Heo ata_ehi_push_desc(ehi, "SDB notify"); 9967d77b247STejun Heo sata_async_notification(ap); 997854c73a2STejun Heo } 998854c73a2STejun Heo 999c6fd2807SJeff Garzik if (irq_stat & (PORT_IRQ_PHYRDY_CHG | PORT_IRQ_DEV_XCHG)) { 1000c6fd2807SJeff Garzik ata_ehi_hotplugged(ehi); 1001b64bbc39STejun Heo ata_ehi_push_desc(ehi, "%s", 1002c6fd2807SJeff Garzik irq_stat & PORT_IRQ_PHYRDY_CHG ? 1003c6fd2807SJeff Garzik "PHY RDY changed" : "device exchanged"); 1004c6fd2807SJeff Garzik freeze = 1; 1005c6fd2807SJeff Garzik } 1006c6fd2807SJeff Garzik 1007c6fd2807SJeff Garzik if (irq_stat & PORT_IRQ_UNK_FIS) { 1008c6fd2807SJeff Garzik ehi->err_mask |= AC_ERR_HSM; 1009cf480626STejun Heo ehi->action |= ATA_EH_RESET; 1010b64bbc39STejun Heo ata_ehi_push_desc(ehi, "unknown FIS"); 1011c6fd2807SJeff Garzik freeze = 1; 1012c6fd2807SJeff Garzik } 1013c6fd2807SJeff Garzik 1014c6fd2807SJeff Garzik /* deal with command error */ 1015c6fd2807SJeff Garzik if (irq_stat & PORT_IRQ_ERROR) { 1016fc8cc1d5SJoe Perches const struct sil24_cerr_info *ci = NULL; 1017c6fd2807SJeff Garzik unsigned int err_mask = 0, action = 0; 10183454dc69STejun Heo u32 context, cerr; 10193454dc69STejun Heo int pmp; 10203454dc69STejun Heo 10213454dc69STejun Heo abort = 1; 10223454dc69STejun Heo 10233454dc69STejun Heo /* DMA Context Switch Failure in Port Multiplier Mode 10243454dc69STejun Heo * errata. If we have active commands to 3 or more 10253454dc69STejun Heo * devices, any error condition on active devices can 10263454dc69STejun Heo * corrupt DMA context switching. 10273454dc69STejun Heo */ 10283454dc69STejun Heo if (ap->nr_active_links >= 3) { 10293454dc69STejun Heo ehi->err_mask |= AC_ERR_OTHER; 1030cf480626STejun Heo ehi->action |= ATA_EH_RESET; 10313454dc69STejun Heo ata_ehi_push_desc(ehi, "PMP DMA CS errata"); 103223818034STejun Heo pp->do_port_rst = 1; 10333454dc69STejun Heo freeze = 1; 10343454dc69STejun Heo } 10353454dc69STejun Heo 10363454dc69STejun Heo /* find out the offending link and qc */ 1037071f44b1STejun Heo if (sata_pmp_attached(ap)) { 10383454dc69STejun Heo context = readl(port + PORT_CONTEXT); 10393454dc69STejun Heo pmp = (context >> 5) & 0xf; 10403454dc69STejun Heo 10413454dc69STejun Heo if (pmp < ap->nr_pmp_links) { 10423454dc69STejun Heo link = &ap->pmp_link[pmp]; 10433454dc69STejun Heo ehi = &link->eh_info; 10443454dc69STejun Heo qc = ata_qc_from_tag(ap, link->active_tag); 10453454dc69STejun Heo 10463454dc69STejun Heo ata_ehi_clear_desc(ehi); 10473454dc69STejun Heo ata_ehi_push_desc(ehi, "irq_stat 0x%08x", 10483454dc69STejun Heo irq_stat); 10493454dc69STejun Heo } else { 10503454dc69STejun Heo err_mask |= AC_ERR_HSM; 1051cf480626STejun Heo action |= ATA_EH_RESET; 10523454dc69STejun Heo freeze = 1; 10533454dc69STejun Heo } 10543454dc69STejun Heo } else 10553454dc69STejun Heo qc = ata_qc_from_tag(ap, link->active_tag); 1056c6fd2807SJeff Garzik 1057c6fd2807SJeff Garzik /* analyze CMD_ERR */ 1058c6fd2807SJeff Garzik cerr = readl(port + PORT_CMD_ERR); 1059c6fd2807SJeff Garzik if (cerr < ARRAY_SIZE(sil24_cerr_db)) 1060c6fd2807SJeff Garzik ci = &sil24_cerr_db[cerr]; 1061c6fd2807SJeff Garzik 1062c6fd2807SJeff Garzik if (ci && ci->desc) { 1063c6fd2807SJeff Garzik err_mask |= ci->err_mask; 1064c6fd2807SJeff Garzik action |= ci->action; 1065cf480626STejun Heo if (action & ATA_EH_RESET) 1066c2e14f11STejun Heo freeze = 1; 1067b64bbc39STejun Heo ata_ehi_push_desc(ehi, "%s", ci->desc); 1068c6fd2807SJeff Garzik } else { 1069c6fd2807SJeff Garzik err_mask |= AC_ERR_OTHER; 1070cf480626STejun Heo action |= ATA_EH_RESET; 1071c2e14f11STejun Heo freeze = 1; 1072b64bbc39STejun Heo ata_ehi_push_desc(ehi, "unknown command error %d", 1073c6fd2807SJeff Garzik cerr); 1074c6fd2807SJeff Garzik } 1075c6fd2807SJeff Garzik 1076c6fd2807SJeff Garzik /* record error info */ 1077520d06f9STejun Heo if (qc) 1078c6fd2807SJeff Garzik qc->err_mask |= err_mask; 1079520d06f9STejun Heo else 1080c6fd2807SJeff Garzik ehi->err_mask |= err_mask; 1081c6fd2807SJeff Garzik 1082c6fd2807SJeff Garzik ehi->action |= action; 10833454dc69STejun Heo 10843454dc69STejun Heo /* if PMP, resume */ 1085071f44b1STejun Heo if (sata_pmp_attached(ap)) 10863454dc69STejun Heo writel(PORT_CS_PMP_RESUME, port + PORT_CTRL_STAT); 1087c6fd2807SJeff Garzik } 1088c6fd2807SJeff Garzik 1089c6fd2807SJeff Garzik /* freeze or abort */ 1090c6fd2807SJeff Garzik if (freeze) 1091c6fd2807SJeff Garzik ata_port_freeze(ap); 10923454dc69STejun Heo else if (abort) { 10933454dc69STejun Heo if (qc) 10943454dc69STejun Heo ata_link_abort(qc->dev->link); 1095c6fd2807SJeff Garzik else 1096c6fd2807SJeff Garzik ata_port_abort(ap); 1097c6fd2807SJeff Garzik } 10983454dc69STejun Heo } 1099c6fd2807SJeff Garzik 1100c6fd2807SJeff Garzik static inline void sil24_host_intr(struct ata_port *ap) 1101c6fd2807SJeff Garzik { 1102350756f6STejun Heo void __iomem *port = sil24_port_base(ap); 1103c6fd2807SJeff Garzik u32 slot_stat, qc_active; 1104c6fd2807SJeff Garzik int rc; 1105c6fd2807SJeff Garzik 1106228f47b9STejun Heo /* If PCIX_IRQ_WOC, there's an inherent race window between 1107228f47b9STejun Heo * clearing IRQ pending status and reading PORT_SLOT_STAT 1108228f47b9STejun Heo * which may cause spurious interrupts afterwards. This is 1109228f47b9STejun Heo * unavoidable and much better than losing interrupts which 1110228f47b9STejun Heo * happens if IRQ pending is cleared after reading 1111228f47b9STejun Heo * PORT_SLOT_STAT. 1112228f47b9STejun Heo */ 1113228f47b9STejun Heo if (ap->flags & SIL24_FLAG_PCIX_IRQ_WOC) 1114228f47b9STejun Heo writel(PORT_IRQ_COMPLETE, port + PORT_IRQ_STAT); 1115228f47b9STejun Heo 1116c6fd2807SJeff Garzik slot_stat = readl(port + PORT_SLOT_STAT); 1117c6fd2807SJeff Garzik 1118c6fd2807SJeff Garzik if (unlikely(slot_stat & HOST_SSTAT_ATTN)) { 1119c6fd2807SJeff Garzik sil24_error_intr(ap); 1120c6fd2807SJeff Garzik return; 1121c6fd2807SJeff Garzik } 1122c6fd2807SJeff Garzik 1123c6fd2807SJeff Garzik qc_active = slot_stat & ~HOST_SSTAT_ATTN; 112479f97dadSTejun Heo rc = ata_qc_complete_multiple(ap, qc_active); 1125c6fd2807SJeff Garzik if (rc > 0) 1126c6fd2807SJeff Garzik return; 1127c6fd2807SJeff Garzik if (rc < 0) { 11289af5c9c9STejun Heo struct ata_eh_info *ehi = &ap->link.eh_info; 1129c6fd2807SJeff Garzik ehi->err_mask |= AC_ERR_HSM; 1130cf480626STejun Heo ehi->action |= ATA_EH_RESET; 1131c6fd2807SJeff Garzik ata_port_freeze(ap); 1132c6fd2807SJeff Garzik return; 1133c6fd2807SJeff Garzik } 1134c6fd2807SJeff Garzik 1135228f47b9STejun Heo /* spurious interrupts are expected if PCIX_IRQ_WOC */ 1136228f47b9STejun Heo if (!(ap->flags & SIL24_FLAG_PCIX_IRQ_WOC) && ata_ratelimit()) 1137a9a79dfeSJoe Perches ata_port_info(ap, 1138a9a79dfeSJoe Perches "spurious interrupt (slot_stat 0x%x active_tag %d sactive 0x%x)\n", 11399af5c9c9STejun Heo slot_stat, ap->link.active_tag, ap->link.sactive); 1140c6fd2807SJeff Garzik } 1141c6fd2807SJeff Garzik 11427d12e780SDavid Howells static irqreturn_t sil24_interrupt(int irq, void *dev_instance) 1143c6fd2807SJeff Garzik { 1144cca3974eSJeff Garzik struct ata_host *host = dev_instance; 11450d5ff566STejun Heo void __iomem *host_base = host->iomap[SIL24_HOST_BAR]; 1146c6fd2807SJeff Garzik unsigned handled = 0; 1147c6fd2807SJeff Garzik u32 status; 1148c6fd2807SJeff Garzik int i; 1149c6fd2807SJeff Garzik 11500d5ff566STejun Heo status = readl(host_base + HOST_IRQ_STAT); 1151c6fd2807SJeff Garzik 1152c6fd2807SJeff Garzik if (status == 0xffffffff) { 115311838230STim Small dev_err(host->dev, "IRQ status == 0xffffffff, " 1154c6fd2807SJeff Garzik "PCI fault or device removal?\n"); 1155c6fd2807SJeff Garzik goto out; 1156c6fd2807SJeff Garzik } 1157c6fd2807SJeff Garzik 1158c6fd2807SJeff Garzik if (!(status & IRQ_STAT_4PORTS)) 1159c6fd2807SJeff Garzik goto out; 1160c6fd2807SJeff Garzik 1161cca3974eSJeff Garzik spin_lock(&host->lock); 1162c6fd2807SJeff Garzik 1163cca3974eSJeff Garzik for (i = 0; i < host->n_ports; i++) 1164c6fd2807SJeff Garzik if (status & (1 << i)) { 11653e4ec344STejun Heo sil24_host_intr(host->ports[i]); 1166c6fd2807SJeff Garzik handled++; 1167c6fd2807SJeff Garzik } 1168c6fd2807SJeff Garzik 1169cca3974eSJeff Garzik spin_unlock(&host->lock); 1170c6fd2807SJeff Garzik out: 1171c6fd2807SJeff Garzik return IRQ_RETVAL(handled); 1172c6fd2807SJeff Garzik } 1173c6fd2807SJeff Garzik 1174c6fd2807SJeff Garzik static void sil24_error_handler(struct ata_port *ap) 1175c6fd2807SJeff Garzik { 117623818034STejun Heo struct sil24_port_priv *pp = ap->private_data; 117723818034STejun Heo 11783454dc69STejun Heo if (sil24_init_port(ap)) 1179c6fd2807SJeff Garzik ata_eh_freeze_port(ap); 1180c6fd2807SJeff Garzik 1181a1efdabaSTejun Heo sata_pmp_error_handler(ap); 118223818034STejun Heo 118323818034STejun Heo pp->do_port_rst = 0; 1184c6fd2807SJeff Garzik } 1185c6fd2807SJeff Garzik 1186c6fd2807SJeff Garzik static void sil24_post_internal_cmd(struct ata_queued_cmd *qc) 1187c6fd2807SJeff Garzik { 1188c6fd2807SJeff Garzik struct ata_port *ap = qc->ap; 1189c6fd2807SJeff Garzik 1190c6fd2807SJeff Garzik /* make DMA engine forget about the failed command */ 11913454dc69STejun Heo if ((qc->flags & ATA_QCFLAG_FAILED) && sil24_init_port(ap)) 11923454dc69STejun Heo ata_eh_freeze_port(ap); 1193c6fd2807SJeff Garzik } 1194c6fd2807SJeff Garzik 1195c6fd2807SJeff Garzik static int sil24_port_start(struct ata_port *ap) 1196c6fd2807SJeff Garzik { 1197cca3974eSJeff Garzik struct device *dev = ap->host->dev; 1198c6fd2807SJeff Garzik struct sil24_port_priv *pp; 1199c6fd2807SJeff Garzik union sil24_cmd_block *cb; 1200c6fd2807SJeff Garzik size_t cb_size = sizeof(*cb) * SIL24_MAX_CMDS; 1201c6fd2807SJeff Garzik dma_addr_t cb_dma; 1202c6fd2807SJeff Garzik 120324dc5f33STejun Heo pp = devm_kzalloc(dev, sizeof(*pp), GFP_KERNEL); 1204c6fd2807SJeff Garzik if (!pp) 120524dc5f33STejun Heo return -ENOMEM; 1206c6fd2807SJeff Garzik 120724dc5f33STejun Heo cb = dmam_alloc_coherent(dev, cb_size, &cb_dma, GFP_KERNEL); 1208c6fd2807SJeff Garzik if (!cb) 120924dc5f33STejun Heo return -ENOMEM; 1210c6fd2807SJeff Garzik 1211c6fd2807SJeff Garzik pp->cmd_block = cb; 1212c6fd2807SJeff Garzik pp->cmd_block_dma = cb_dma; 1213c6fd2807SJeff Garzik 1214c6fd2807SJeff Garzik ap->private_data = pp; 1215c6fd2807SJeff Garzik 1216350756f6STejun Heo ata_port_pbar_desc(ap, SIL24_HOST_BAR, -1, "host"); 1217350756f6STejun Heo ata_port_pbar_desc(ap, SIL24_PORT_BAR, sil24_port_offset(ap), "port"); 1218350756f6STejun Heo 1219c6fd2807SJeff Garzik return 0; 1220c6fd2807SJeff Garzik } 1221c6fd2807SJeff Garzik 12224447d351STejun Heo static void sil24_init_controller(struct ata_host *host) 1223c6fd2807SJeff Garzik { 12244447d351STejun Heo void __iomem *host_base = host->iomap[SIL24_HOST_BAR]; 1225c6fd2807SJeff Garzik u32 tmp; 1226c6fd2807SJeff Garzik int i; 1227c6fd2807SJeff Garzik 1228c6fd2807SJeff Garzik /* GPIO off */ 1229c6fd2807SJeff Garzik writel(0, host_base + HOST_FLASH_CMD); 1230c6fd2807SJeff Garzik 1231c6fd2807SJeff Garzik /* clear global reset & mask interrupts during initialization */ 1232c6fd2807SJeff Garzik writel(0, host_base + HOST_CTRL); 1233c6fd2807SJeff Garzik 1234c6fd2807SJeff Garzik /* init ports */ 12354447d351STejun Heo for (i = 0; i < host->n_ports; i++) { 123623818034STejun Heo struct ata_port *ap = host->ports[i]; 1237350756f6STejun Heo void __iomem *port = sil24_port_base(ap); 1238350756f6STejun Heo 1239c6fd2807SJeff Garzik 1240c6fd2807SJeff Garzik /* Initial PHY setting */ 1241c6fd2807SJeff Garzik writel(0x20c, port + PORT_PHY_CFG); 1242c6fd2807SJeff Garzik 1243c6fd2807SJeff Garzik /* Clear port RST */ 1244c6fd2807SJeff Garzik tmp = readl(port + PORT_CTRL_STAT); 1245c6fd2807SJeff Garzik if (tmp & PORT_CS_PORT_RST) { 1246c6fd2807SJeff Garzik writel(PORT_CS_PORT_RST, port + PORT_CTRL_CLR); 124797750cebSTejun Heo tmp = ata_wait_register(NULL, port + PORT_CTRL_STAT, 1248c6fd2807SJeff Garzik PORT_CS_PORT_RST, 1249c6fd2807SJeff Garzik PORT_CS_PORT_RST, 10, 100); 1250c6fd2807SJeff Garzik if (tmp & PORT_CS_PORT_RST) 1251a44fec1fSJoe Perches dev_err(host->dev, 1252c6fd2807SJeff Garzik "failed to clear port RST\n"); 1253c6fd2807SJeff Garzik } 1254c6fd2807SJeff Garzik 125523818034STejun Heo /* configure port */ 125623818034STejun Heo sil24_config_port(ap); 1257c6fd2807SJeff Garzik } 1258c6fd2807SJeff Garzik 1259c6fd2807SJeff Garzik /* Turn on interrupts */ 1260c6fd2807SJeff Garzik writel(IRQ_STAT_4PORTS, host_base + HOST_CTRL); 1261c6fd2807SJeff Garzik } 1262c6fd2807SJeff Garzik 1263c6fd2807SJeff Garzik static int sil24_init_one(struct pci_dev *pdev, const struct pci_device_id *ent) 1264c6fd2807SJeff Garzik { 126593e2618eSTejun Heo extern int __MARKER__sil24_cmd_block_is_sized_wrongly; 12664447d351STejun Heo struct ata_port_info pi = sil24_port_info[ent->driver_data]; 12674447d351STejun Heo const struct ata_port_info *ppi[] = { &pi, NULL }; 12684447d351STejun Heo void __iomem * const *iomap; 12694447d351STejun Heo struct ata_host *host; 1270350756f6STejun Heo int rc; 1271c6fd2807SJeff Garzik u32 tmp; 1272c6fd2807SJeff Garzik 127393e2618eSTejun Heo /* cause link error if sil24_cmd_block is sized wrongly */ 127493e2618eSTejun Heo if (sizeof(union sil24_cmd_block) != PAGE_SIZE) 127593e2618eSTejun Heo __MARKER__sil24_cmd_block_is_sized_wrongly = 1; 127693e2618eSTejun Heo 127706296a1eSJoe Perches ata_print_version_once(&pdev->dev, DRV_VERSION); 1278c6fd2807SJeff Garzik 12794447d351STejun Heo /* acquire resources */ 128024dc5f33STejun Heo rc = pcim_enable_device(pdev); 1281c6fd2807SJeff Garzik if (rc) 1282c6fd2807SJeff Garzik return rc; 1283c6fd2807SJeff Garzik 12840d5ff566STejun Heo rc = pcim_iomap_regions(pdev, 12850d5ff566STejun Heo (1 << SIL24_HOST_BAR) | (1 << SIL24_PORT_BAR), 12860d5ff566STejun Heo DRV_NAME); 1287c6fd2807SJeff Garzik if (rc) 128824dc5f33STejun Heo return rc; 12894447d351STejun Heo iomap = pcim_iomap_table(pdev); 1290c6fd2807SJeff Garzik 12914447d351STejun Heo /* apply workaround for completion IRQ loss on PCI-X errata */ 12924447d351STejun Heo if (pi.flags & SIL24_FLAG_PCIX_IRQ_WOC) { 12934447d351STejun Heo tmp = readl(iomap[SIL24_HOST_BAR] + HOST_CTRL); 12944447d351STejun Heo if (tmp & (HOST_CTRL_TRDY | HOST_CTRL_STOP | HOST_CTRL_DEVSEL)) 1295a44fec1fSJoe Perches dev_info(&pdev->dev, 1296a44fec1fSJoe Perches "Applying completion IRQ loss on PCI-X errata fix\n"); 12974447d351STejun Heo else 12984447d351STejun Heo pi.flags &= ~SIL24_FLAG_PCIX_IRQ_WOC; 12994447d351STejun Heo } 13004447d351STejun Heo 13014447d351STejun Heo /* allocate and fill host */ 13024447d351STejun Heo host = ata_host_alloc_pinfo(&pdev->dev, ppi, 13034447d351STejun Heo SIL24_FLAG2NPORTS(ppi[0]->flags)); 13044447d351STejun Heo if (!host) 130524dc5f33STejun Heo return -ENOMEM; 13064447d351STejun Heo host->iomap = iomap; 1307c6fd2807SJeff Garzik 13084447d351STejun Heo /* configure and activate the device */ 1309dcc02c19SChristoph Hellwig rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)); 1310c6fd2807SJeff Garzik if (rc) { 1311dcc02c19SChristoph Hellwig dev_err(&pdev->dev, "DMA enable failed\n"); 131224dc5f33STejun Heo return rc; 1313c6fd2807SJeff Garzik } 1314c6fd2807SJeff Garzik 1315e8b3b5e9STejun Heo /* Set max read request size to 4096. This slightly increases 1316e8b3b5e9STejun Heo * write throughput for pci-e variants. 1317e8b3b5e9STejun Heo */ 1318e8b3b5e9STejun Heo pcie_set_readrq(pdev, 4096); 1319e8b3b5e9STejun Heo 13204447d351STejun Heo sil24_init_controller(host); 1321c6fd2807SJeff Garzik 1322dae77214SVivek Mahajan if (sata_sil24_msi && !pci_enable_msi(pdev)) { 1323a44fec1fSJoe Perches dev_info(&pdev->dev, "Using MSI\n"); 1324dae77214SVivek Mahajan pci_intx(pdev, 0); 1325dae77214SVivek Mahajan } 1326dae77214SVivek Mahajan 1327c6fd2807SJeff Garzik pci_set_master(pdev); 13284447d351STejun Heo return ata_host_activate(host, pdev->irq, sil24_interrupt, IRQF_SHARED, 13294447d351STejun Heo &sil24_sht); 1330c6fd2807SJeff Garzik } 1331c6fd2807SJeff Garzik 133258eb8cd5SBartlomiej Zolnierkiewicz #ifdef CONFIG_PM_SLEEP 1333c6fd2807SJeff Garzik static int sil24_pci_device_resume(struct pci_dev *pdev) 1334c6fd2807SJeff Garzik { 13350a86e1c8SJingoo Han struct ata_host *host = pci_get_drvdata(pdev); 13360d5ff566STejun Heo void __iomem *host_base = host->iomap[SIL24_HOST_BAR]; 1337553c4aa6STejun Heo int rc; 1338c6fd2807SJeff Garzik 1339553c4aa6STejun Heo rc = ata_pci_device_do_resume(pdev); 1340553c4aa6STejun Heo if (rc) 1341553c4aa6STejun Heo return rc; 1342c6fd2807SJeff Garzik 1343c6fd2807SJeff Garzik if (pdev->dev.power.power_state.event == PM_EVENT_SUSPEND) 13440d5ff566STejun Heo writel(HOST_CTRL_GLOBAL_RST, host_base + HOST_CTRL); 1345c6fd2807SJeff Garzik 13464447d351STejun Heo sil24_init_controller(host); 1347c6fd2807SJeff Garzik 1348cca3974eSJeff Garzik ata_host_resume(host); 1349c6fd2807SJeff Garzik 1350c6fd2807SJeff Garzik return 0; 1351c6fd2807SJeff Garzik } 135258eb8cd5SBartlomiej Zolnierkiewicz #endif 13533454dc69STejun Heo 135458eb8cd5SBartlomiej Zolnierkiewicz #ifdef CONFIG_PM 13553454dc69STejun Heo static int sil24_port_resume(struct ata_port *ap) 13563454dc69STejun Heo { 13573454dc69STejun Heo sil24_config_pmp(ap, ap->nr_pmp_links); 13583454dc69STejun Heo return 0; 13593454dc69STejun Heo } 1360281d426cSAlexey Dobriyan #endif 1361c6fd2807SJeff Garzik 13622fc75da0SAxel Lin module_pci_driver(sil24_pci_driver); 1363c6fd2807SJeff Garzik 1364c6fd2807SJeff Garzik MODULE_AUTHOR("Tejun Heo"); 1365c6fd2807SJeff Garzik MODULE_DESCRIPTION("Silicon Image 3124/3132 SATA low-level driver"); 1366c6fd2807SJeff Garzik MODULE_LICENSE("GPL"); 1367c6fd2807SJeff Garzik MODULE_DEVICE_TABLE(pci, sil24_pci_tbl); 1368