xref: /openbmc/linux/drivers/ata/sata_sil24.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
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);
331931139afSDamien Le Moal static void 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 
37625df73d9SBart Van Assche static const 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,
382c3f69c7fSBart 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 
sil24_tag(int tag)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 
sil24_port_offset(struct ata_port * ap)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 
sil24_port_base(struct ata_port * ap)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 
sil24_dev_config(struct ata_device * dev)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 
sil24_read_tf(struct ata_port * ap,int tag,struct ata_taskfile * tf)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 
sil24_scr_read(struct ata_link * link,unsigned sc_reg,u32 * val)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 
sil24_scr_write(struct ata_link * link,unsigned sc_reg,u32 val)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 
sil24_config_port(struct ata_port * ap)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 
sil24_config_pmp(struct ata_port * ap,int attached)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 
sil24_clear_pmp(struct ata_port * ap)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 
sil24_init_port(struct ata_port * ap)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 
sil24_exec_polled_cmd(struct ata_port * ap,int pmp,const struct ata_taskfile * tf,int is_cmd,u32 ctrl,unsigned int timeout_msec)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,
600*671b4493SSergey Shtylyov 				 unsigned int 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 
sil24_softreset(struct ata_link * link,unsigned int * class,unsigned long deadline)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);
654*671b4493SSergey Shtylyov 	unsigned int timeout_msec = 0;
655e59f0dadSTejun Heo 	struct ata_taskfile tf;
656c6fd2807SJeff Garzik 	const char *reason;
65737b99cbaSTejun Heo 	int rc;
658c6fd2807SJeff Garzik 
659c6fd2807SJeff Garzik 	/* put the port into known state */
660c6fd2807SJeff Garzik 	if (sil24_init_port(ap)) {
661c6fd2807SJeff Garzik 		reason = "port not ready";
662c6fd2807SJeff Garzik 		goto err;
663c6fd2807SJeff Garzik 	}
664c6fd2807SJeff Garzik 
665c6fd2807SJeff Garzik 	/* do SRST */
66637b99cbaSTejun Heo 	if (time_after(deadline, jiffies))
66737b99cbaSTejun Heo 		timeout_msec = jiffies_to_msecs(deadline - jiffies);
668c6fd2807SJeff Garzik 
669cc0680a5STejun Heo 	ata_tf_init(link->device, &tf);	/* doesn't really matter */
670975530e8STejun Heo 	rc = sil24_exec_polled_cmd(ap, pmp, &tf, 0, PRB_CTRL_SRST,
671975530e8STejun Heo 				   timeout_msec);
67237b99cbaSTejun Heo 	if (rc == -EBUSY) {
673c6fd2807SJeff Garzik 		reason = "timeout";
674c6fd2807SJeff Garzik 		goto err;
67537b99cbaSTejun Heo 	} else if (rc) {
67637b99cbaSTejun Heo 		reason = "SRST command error";
67737b99cbaSTejun Heo 		goto err;
678c6fd2807SJeff Garzik 	}
679c6fd2807SJeff Garzik 
680e59f0dadSTejun Heo 	sil24_read_tf(ap, 0, &tf);
6816c952a0dSHannes Reinecke 	*class = ata_port_classify(ap, &tf);
682c6fd2807SJeff Garzik 
683c6fd2807SJeff Garzik 	return 0;
684c6fd2807SJeff Garzik 
685c6fd2807SJeff Garzik  err:
686a9a79dfeSJoe Perches 	ata_link_err(link, "softreset failed (%s)\n", reason);
687c6fd2807SJeff Garzik 	return -EIO;
688c6fd2807SJeff Garzik }
689c6fd2807SJeff Garzik 
sil24_hardreset(struct ata_link * link,unsigned int * class,unsigned long deadline)690cc0680a5STejun Heo static int sil24_hardreset(struct ata_link *link, unsigned int *class,
691d4b2bab4STejun Heo 			   unsigned long deadline)
692c6fd2807SJeff Garzik {
693cc0680a5STejun Heo 	struct ata_port *ap = link->ap;
694350756f6STejun Heo 	void __iomem *port = sil24_port_base(ap);
69523818034STejun Heo 	struct sil24_port_priv *pp = ap->private_data;
69623818034STejun Heo 	int did_port_rst = 0;
697c6fd2807SJeff Garzik 	const char *reason;
698c6fd2807SJeff Garzik 	int tout_msec, rc;
699c6fd2807SJeff Garzik 	u32 tmp;
700c6fd2807SJeff Garzik 
70123818034STejun Heo  retry:
70223818034STejun Heo 	/* Sometimes, DEV_RST is not enough to recover the controller.
70323818034STejun Heo 	 * This happens often after PM DMA CS errata.
70423818034STejun Heo 	 */
70523818034STejun Heo 	if (pp->do_port_rst) {
706a9a79dfeSJoe Perches 		ata_port_warn(ap,
707a9a79dfeSJoe Perches 			      "controller in dubious state, performing PORT_RST\n");
70823818034STejun Heo 
70923818034STejun Heo 		writel(PORT_CS_PORT_RST, port + PORT_CTRL_STAT);
71097750cebSTejun Heo 		ata_msleep(ap, 10);
71123818034STejun Heo 		writel(PORT_CS_PORT_RST, port + PORT_CTRL_CLR);
71297750cebSTejun Heo 		ata_wait_register(ap, port + PORT_CTRL_STAT, PORT_CS_RDY, 0,
71323818034STejun Heo 				  10, 5000);
71423818034STejun Heo 
71523818034STejun Heo 		/* restore port configuration */
71623818034STejun Heo 		sil24_config_port(ap);
71723818034STejun Heo 		sil24_config_pmp(ap, ap->nr_pmp_links);
71823818034STejun Heo 
71923818034STejun Heo 		pp->do_port_rst = 0;
72023818034STejun Heo 		did_port_rst = 1;
72123818034STejun Heo 	}
72223818034STejun Heo 
723c6fd2807SJeff Garzik 	/* sil24 does the right thing(tm) without any protection */
724cc0680a5STejun Heo 	sata_set_spd(link);
725c6fd2807SJeff Garzik 
726c6fd2807SJeff Garzik 	tout_msec = 100;
727cc0680a5STejun Heo 	if (ata_link_online(link))
728c6fd2807SJeff Garzik 		tout_msec = 5000;
729c6fd2807SJeff Garzik 
730c6fd2807SJeff Garzik 	writel(PORT_CS_DEV_RST, port + PORT_CTRL_STAT);
73197750cebSTejun Heo 	tmp = ata_wait_register(ap, port + PORT_CTRL_STAT,
7325796d1c4SJeff Garzik 				PORT_CS_DEV_RST, PORT_CS_DEV_RST, 10,
7335796d1c4SJeff Garzik 				tout_msec);
734c6fd2807SJeff Garzik 
735c6fd2807SJeff Garzik 	/* SStatus oscillates between zero and valid status after
736c6fd2807SJeff Garzik 	 * DEV_RST, debounce it.
737c6fd2807SJeff Garzik 	 */
738cc0680a5STejun Heo 	rc = sata_link_debounce(link, sata_deb_timing_long, deadline);
739c6fd2807SJeff Garzik 	if (rc) {
740c6fd2807SJeff Garzik 		reason = "PHY debouncing failed";
741c6fd2807SJeff Garzik 		goto err;
742c6fd2807SJeff Garzik 	}
743c6fd2807SJeff Garzik 
744c6fd2807SJeff Garzik 	if (tmp & PORT_CS_DEV_RST) {
745cc0680a5STejun Heo 		if (ata_link_offline(link))
746c6fd2807SJeff Garzik 			return 0;
747c6fd2807SJeff Garzik 		reason = "link not ready";
748c6fd2807SJeff Garzik 		goto err;
749c6fd2807SJeff Garzik 	}
750c6fd2807SJeff Garzik 
751c6fd2807SJeff Garzik 	/* Sil24 doesn't store signature FIS after hardreset, so we
752c6fd2807SJeff Garzik 	 * can't wait for BSY to clear.  Some devices take a long time
753c6fd2807SJeff Garzik 	 * to get ready and those devices will choke if we don't wait
754c6fd2807SJeff Garzik 	 * for BSY clearance here.  Tell libata to perform follow-up
755c6fd2807SJeff Garzik 	 * softreset.
756c6fd2807SJeff Garzik 	 */
757c6fd2807SJeff Garzik 	return -EAGAIN;
758c6fd2807SJeff Garzik 
759c6fd2807SJeff Garzik  err:
76023818034STejun Heo 	if (!did_port_rst) {
76123818034STejun Heo 		pp->do_port_rst = 1;
76223818034STejun Heo 		goto retry;
76323818034STejun Heo 	}
76423818034STejun Heo 
765a9a79dfeSJoe Perches 	ata_link_err(link, "hardreset failed (%s)\n", reason);
766c6fd2807SJeff Garzik 	return -EIO;
767c6fd2807SJeff Garzik }
768c6fd2807SJeff Garzik 
sil24_fill_sg(struct ata_queued_cmd * qc,struct sil24_sge * sge)769c6fd2807SJeff Garzik static inline void sil24_fill_sg(struct ata_queued_cmd *qc,
770c6fd2807SJeff Garzik 				 struct sil24_sge *sge)
771c6fd2807SJeff Garzik {
772c6fd2807SJeff Garzik 	struct scatterlist *sg;
7733be6cbd7SJeff Garzik 	struct sil24_sge *last_sge = NULL;
774ff2aeb1eSTejun Heo 	unsigned int si;
775c6fd2807SJeff Garzik 
776ff2aeb1eSTejun Heo 	for_each_sg(qc->sg, sg, qc->n_elem, si) {
777c6fd2807SJeff Garzik 		sge->addr = cpu_to_le64(sg_dma_address(sg));
778c6fd2807SJeff Garzik 		sge->cnt = cpu_to_le32(sg_dma_len(sg));
779c6fd2807SJeff Garzik 		sge->flags = 0;
7803be6cbd7SJeff Garzik 
7813be6cbd7SJeff Garzik 		last_sge = sge;
782c6fd2807SJeff Garzik 		sge++;
783c6fd2807SJeff Garzik 	}
7843be6cbd7SJeff Garzik 
7853be6cbd7SJeff Garzik 	last_sge->flags = cpu_to_le32(SGE_TRM);
786c6fd2807SJeff Garzik }
787c6fd2807SJeff Garzik 
sil24_qc_defer(struct ata_queued_cmd * qc)7883454dc69STejun Heo static int sil24_qc_defer(struct ata_queued_cmd *qc)
7893454dc69STejun Heo {
7903454dc69STejun Heo 	struct ata_link *link = qc->dev->link;
7913454dc69STejun Heo 	struct ata_port *ap = link->ap;
7923454dc69STejun Heo 	u8 prot = qc->tf.protocol;
7933454dc69STejun Heo 
79413cc546bSGwendal Grignou 	/*
79513cc546bSGwendal Grignou 	 * There is a bug in the chip:
79613cc546bSGwendal Grignou 	 * Port LRAM Causes the PRB/SGT Data to be Corrupted
79713cc546bSGwendal Grignou 	 * If the host issues a read request for LRAM and SActive registers
79813cc546bSGwendal Grignou 	 * while active commands are available in the port, PRB/SGT data in
79913cc546bSGwendal Grignou 	 * the LRAM can become corrupted. This issue applies only when
80013cc546bSGwendal Grignou 	 * reading from, but not writing to, the LRAM.
80113cc546bSGwendal Grignou 	 *
80213cc546bSGwendal Grignou 	 * Therefore, reading LRAM when there is no particular error [and
80313cc546bSGwendal Grignou 	 * other commands may be outstanding] is prohibited.
80413cc546bSGwendal Grignou 	 *
80513cc546bSGwendal Grignou 	 * To avoid this bug there are two situations where a command must run
80613cc546bSGwendal Grignou 	 * exclusive of any other commands on the port:
80713cc546bSGwendal Grignou 	 *
80813cc546bSGwendal Grignou 	 * - ATAPI commands which check the sense data
80913cc546bSGwendal Grignou 	 * - Passthrough ATA commands which always have ATA_QCFLAG_RESULT_TF
81013cc546bSGwendal Grignou 	 *   set.
81113cc546bSGwendal Grignou 	 *
8123454dc69STejun Heo  	 */
813405e66b3STejun Heo 	int is_excl = (ata_is_atapi(prot) ||
81413cc546bSGwendal Grignou 		       (qc->flags & ATA_QCFLAG_RESULT_TF));
81513cc546bSGwendal Grignou 
8163454dc69STejun Heo 	if (unlikely(ap->excl_link)) {
8173454dc69STejun Heo 		if (link == ap->excl_link) {
8183454dc69STejun Heo 			if (ap->nr_active_links)
8193454dc69STejun Heo 				return ATA_DEFER_PORT;
8203454dc69STejun Heo 			qc->flags |= ATA_QCFLAG_CLEAR_EXCL;
8213454dc69STejun Heo 		} else
8223454dc69STejun Heo 			return ATA_DEFER_PORT;
82313cc546bSGwendal Grignou 	} else if (unlikely(is_excl)) {
8243454dc69STejun Heo 		ap->excl_link = link;
8253454dc69STejun Heo 		if (ap->nr_active_links)
8263454dc69STejun Heo 			return ATA_DEFER_PORT;
8273454dc69STejun Heo 		qc->flags |= ATA_QCFLAG_CLEAR_EXCL;
8283454dc69STejun Heo 	}
8293454dc69STejun Heo 
8303454dc69STejun Heo 	return ata_std_qc_defer(qc);
8313454dc69STejun Heo }
8323454dc69STejun Heo 
sil24_qc_prep(struct ata_queued_cmd * qc)83395364f36SJiri Slaby static enum ata_completion_errors sil24_qc_prep(struct ata_queued_cmd *qc)
834c6fd2807SJeff Garzik {
835c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
836c6fd2807SJeff Garzik 	struct sil24_port_priv *pp = ap->private_data;
837c6fd2807SJeff Garzik 	union sil24_cmd_block *cb;
838c6fd2807SJeff Garzik 	struct sil24_prb *prb;
839c6fd2807SJeff Garzik 	struct sil24_sge *sge;
840c6fd2807SJeff Garzik 	u16 ctrl = 0;
841c6fd2807SJeff Garzik 
8424e5b6260SJens Axboe 	cb = &pp->cmd_block[sil24_tag(qc->hw_tag)];
843c6fd2807SJeff Garzik 
844405e66b3STejun Heo 	if (!ata_is_atapi(qc->tf.protocol)) {
845c6fd2807SJeff Garzik 		prb = &cb->ata.prb;
846c6fd2807SJeff Garzik 		sge = cb->ata.sge;
8474f1a0ee1SRobert Hancock 		if (ata_is_data(qc->tf.protocol)) {
8484f1a0ee1SRobert Hancock 			u16 prot = 0;
8494f1a0ee1SRobert Hancock 			ctrl = PRB_CTRL_PROTOCOL;
8504f1a0ee1SRobert Hancock 			if (ata_is_ncq(qc->tf.protocol))
8514f1a0ee1SRobert Hancock 				prot |= PRB_PROT_NCQ;
8524f1a0ee1SRobert Hancock 			if (qc->tf.flags & ATA_TFLAG_WRITE)
8534f1a0ee1SRobert Hancock 				prot |= PRB_PROT_WRITE;
8544f1a0ee1SRobert Hancock 			else
8554f1a0ee1SRobert Hancock 				prot |= PRB_PROT_READ;
8564f1a0ee1SRobert Hancock 			prb->prot = cpu_to_le16(prot);
8574f1a0ee1SRobert Hancock 		}
858405e66b3STejun Heo 	} else {
859c6fd2807SJeff Garzik 		prb = &cb->atapi.prb;
860c6fd2807SJeff Garzik 		sge = cb->atapi.sge;
86114e45c15SDan Carpenter 		memset(cb->atapi.cdb, 0, sizeof(cb->atapi.cdb));
862c6fd2807SJeff Garzik 		memcpy(cb->atapi.cdb, qc->cdb, qc->dev->cdb_len);
863c6fd2807SJeff Garzik 
864405e66b3STejun Heo 		if (ata_is_data(qc->tf.protocol)) {
865c6fd2807SJeff Garzik 			if (qc->tf.flags & ATA_TFLAG_WRITE)
866c6fd2807SJeff Garzik 				ctrl = PRB_CTRL_PACKET_WRITE;
867c6fd2807SJeff Garzik 			else
868c6fd2807SJeff Garzik 				ctrl = PRB_CTRL_PACKET_READ;
869c6fd2807SJeff Garzik 		}
870c6fd2807SJeff Garzik 	}
871c6fd2807SJeff Garzik 
872c6fd2807SJeff Garzik 	prb->ctrl = cpu_to_le16(ctrl);
8733454dc69STejun Heo 	ata_tf_to_fis(&qc->tf, qc->dev->link->pmp, 1, prb->fis);
874c6fd2807SJeff Garzik 
875c6fd2807SJeff Garzik 	if (qc->flags & ATA_QCFLAG_DMAMAP)
876c6fd2807SJeff Garzik 		sil24_fill_sg(qc, sge);
87795364f36SJiri Slaby 
87895364f36SJiri Slaby 	return AC_ERR_OK;
879c6fd2807SJeff Garzik }
880c6fd2807SJeff Garzik 
sil24_qc_issue(struct ata_queued_cmd * qc)881c6fd2807SJeff Garzik static unsigned int sil24_qc_issue(struct ata_queued_cmd *qc)
882c6fd2807SJeff Garzik {
883c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
884c6fd2807SJeff Garzik 	struct sil24_port_priv *pp = ap->private_data;
885350756f6STejun Heo 	void __iomem *port = sil24_port_base(ap);
8864e5b6260SJens Axboe 	unsigned int tag = sil24_tag(qc->hw_tag);
887c6fd2807SJeff Garzik 	dma_addr_t paddr;
888c6fd2807SJeff Garzik 	void __iomem *activate;
889c6fd2807SJeff Garzik 
890c6fd2807SJeff Garzik 	paddr = pp->cmd_block_dma + tag * sizeof(*pp->cmd_block);
891c6fd2807SJeff Garzik 	activate = port + PORT_CMD_ACTIVATE + tag * 8;
892c6fd2807SJeff Garzik 
89310823452SCatalin Marinas 	/*
89410823452SCatalin Marinas 	 * The barrier is required to ensure that writes to cmd_block reach
89510823452SCatalin Marinas 	 * the memory before the write to PORT_CMD_ACTIVATE.
89610823452SCatalin Marinas 	 */
89710823452SCatalin Marinas 	wmb();
898c6fd2807SJeff Garzik 	writel((u32)paddr, activate);
899c6fd2807SJeff Garzik 	writel((u64)paddr >> 32, activate + 4);
900c6fd2807SJeff Garzik 
901c6fd2807SJeff Garzik 	return 0;
902c6fd2807SJeff Garzik }
903c6fd2807SJeff Garzik 
sil24_qc_fill_rtf(struct ata_queued_cmd * qc)904931139afSDamien Le Moal static void sil24_qc_fill_rtf(struct ata_queued_cmd *qc)
90579f97dadSTejun Heo {
9064e5b6260SJens Axboe 	sil24_read_tf(qc->ap, qc->hw_tag, &qc->result_tf);
90779f97dadSTejun Heo }
90879f97dadSTejun Heo 
sil24_pmp_attach(struct ata_port * ap)9093454dc69STejun Heo static void sil24_pmp_attach(struct ata_port *ap)
9103454dc69STejun Heo {
911906c1ff4STejun Heo 	u32 *gscr = ap->link.device->gscr;
912906c1ff4STejun Heo 
9133454dc69STejun Heo 	sil24_config_pmp(ap, 1);
9143454dc69STejun Heo 	sil24_init_port(ap);
915906c1ff4STejun Heo 
916906c1ff4STejun Heo 	if (sata_pmp_gscr_vendor(gscr) == 0x11ab &&
917906c1ff4STejun Heo 	    sata_pmp_gscr_devid(gscr) == 0x4140) {
918a9a79dfeSJoe Perches 		ata_port_info(ap,
919906c1ff4STejun Heo 			"disabling NCQ support due to sil24-mv4140 quirk\n");
920906c1ff4STejun Heo 		ap->flags &= ~ATA_FLAG_NCQ;
921906c1ff4STejun Heo 	}
9223454dc69STejun Heo }
9233454dc69STejun Heo 
sil24_pmp_detach(struct ata_port * ap)9243454dc69STejun Heo static void sil24_pmp_detach(struct ata_port *ap)
9253454dc69STejun Heo {
9263454dc69STejun Heo 	sil24_init_port(ap);
9273454dc69STejun Heo 	sil24_config_pmp(ap, 0);
928906c1ff4STejun Heo 
929906c1ff4STejun Heo 	ap->flags |= ATA_FLAG_NCQ;
9303454dc69STejun Heo }
9313454dc69STejun Heo 
sil24_pmp_hardreset(struct ata_link * link,unsigned int * class,unsigned long deadline)9323454dc69STejun Heo static int sil24_pmp_hardreset(struct ata_link *link, unsigned int *class,
9333454dc69STejun Heo 			       unsigned long deadline)
9343454dc69STejun Heo {
9353454dc69STejun Heo 	int rc;
9363454dc69STejun Heo 
9373454dc69STejun Heo 	rc = sil24_init_port(link->ap);
9383454dc69STejun Heo 	if (rc) {
939a9a79dfeSJoe Perches 		ata_link_err(link, "hardreset failed (port not ready)\n");
9403454dc69STejun Heo 		return rc;
9413454dc69STejun Heo 	}
9423454dc69STejun Heo 
9435958e302STejun Heo 	return sata_std_hardreset(link, class, deadline);
9443454dc69STejun Heo }
9453454dc69STejun Heo 
sil24_freeze(struct ata_port * ap)946c6fd2807SJeff Garzik static void sil24_freeze(struct ata_port *ap)
947c6fd2807SJeff Garzik {
948350756f6STejun Heo 	void __iomem *port = sil24_port_base(ap);
949c6fd2807SJeff Garzik 
950c6fd2807SJeff Garzik 	/* Port-wide IRQ mask in HOST_CTRL doesn't really work, clear
951c6fd2807SJeff Garzik 	 * PORT_IRQ_ENABLE instead.
952c6fd2807SJeff Garzik 	 */
953c6fd2807SJeff Garzik 	writel(0xffff, port + PORT_IRQ_ENABLE_CLR);
954c6fd2807SJeff Garzik }
955c6fd2807SJeff Garzik 
sil24_thaw(struct ata_port * ap)956c6fd2807SJeff Garzik static void sil24_thaw(struct ata_port *ap)
957c6fd2807SJeff Garzik {
958350756f6STejun Heo 	void __iomem *port = sil24_port_base(ap);
959c6fd2807SJeff Garzik 	u32 tmp;
960c6fd2807SJeff Garzik 
961c6fd2807SJeff Garzik 	/* clear IRQ */
962c6fd2807SJeff Garzik 	tmp = readl(port + PORT_IRQ_STAT);
963c6fd2807SJeff Garzik 	writel(tmp, port + PORT_IRQ_STAT);
964c6fd2807SJeff Garzik 
965c6fd2807SJeff Garzik 	/* turn IRQ back on */
966c6fd2807SJeff Garzik 	writel(DEF_PORT_IRQ, port + PORT_IRQ_ENABLE_SET);
967c6fd2807SJeff Garzik }
968c6fd2807SJeff Garzik 
sil24_error_intr(struct ata_port * ap)969c6fd2807SJeff Garzik static void sil24_error_intr(struct ata_port *ap)
970c6fd2807SJeff Garzik {
971350756f6STejun Heo 	void __iomem *port = sil24_port_base(ap);
972e59f0dadSTejun Heo 	struct sil24_port_priv *pp = ap->private_data;
9733454dc69STejun Heo 	struct ata_queued_cmd *qc = NULL;
9743454dc69STejun Heo 	struct ata_link *link;
9753454dc69STejun Heo 	struct ata_eh_info *ehi;
9763454dc69STejun Heo 	int abort = 0, freeze = 0;
977c6fd2807SJeff Garzik 	u32 irq_stat;
978c6fd2807SJeff Garzik 
979c6fd2807SJeff Garzik 	/* on error, we need to clear IRQ explicitly */
980c6fd2807SJeff Garzik 	irq_stat = readl(port + PORT_IRQ_STAT);
981c6fd2807SJeff Garzik 	writel(irq_stat, port + PORT_IRQ_STAT);
982c6fd2807SJeff Garzik 
983c6fd2807SJeff Garzik 	/* first, analyze and record host port events */
9843454dc69STejun Heo 	link = &ap->link;
9853454dc69STejun Heo 	ehi = &link->eh_info;
986c6fd2807SJeff Garzik 	ata_ehi_clear_desc(ehi);
987c6fd2807SJeff Garzik 
988c6fd2807SJeff Garzik 	ata_ehi_push_desc(ehi, "irq_stat 0x%08x", irq_stat);
989c6fd2807SJeff Garzik 
990854c73a2STejun Heo 	if (irq_stat & PORT_IRQ_SDB_NOTIFY) {
991854c73a2STejun Heo 		ata_ehi_push_desc(ehi, "SDB notify");
9927d77b247STejun Heo 		sata_async_notification(ap);
993854c73a2STejun Heo 	}
994854c73a2STejun Heo 
995c6fd2807SJeff Garzik 	if (irq_stat & (PORT_IRQ_PHYRDY_CHG | PORT_IRQ_DEV_XCHG)) {
996c6fd2807SJeff Garzik 		ata_ehi_hotplugged(ehi);
997b64bbc39STejun Heo 		ata_ehi_push_desc(ehi, "%s",
998c6fd2807SJeff Garzik 				  irq_stat & PORT_IRQ_PHYRDY_CHG ?
999c6fd2807SJeff Garzik 				  "PHY RDY changed" : "device exchanged");
1000c6fd2807SJeff Garzik 		freeze = 1;
1001c6fd2807SJeff Garzik 	}
1002c6fd2807SJeff Garzik 
1003c6fd2807SJeff Garzik 	if (irq_stat & PORT_IRQ_UNK_FIS) {
1004c6fd2807SJeff Garzik 		ehi->err_mask |= AC_ERR_HSM;
1005cf480626STejun Heo 		ehi->action |= ATA_EH_RESET;
1006b64bbc39STejun Heo 		ata_ehi_push_desc(ehi, "unknown FIS");
1007c6fd2807SJeff Garzik 		freeze = 1;
1008c6fd2807SJeff Garzik 	}
1009c6fd2807SJeff Garzik 
1010c6fd2807SJeff Garzik 	/* deal with command error */
1011c6fd2807SJeff Garzik 	if (irq_stat & PORT_IRQ_ERROR) {
1012fc8cc1d5SJoe Perches 		const struct sil24_cerr_info *ci = NULL;
1013c6fd2807SJeff Garzik 		unsigned int err_mask = 0, action = 0;
10143454dc69STejun Heo 		u32 context, cerr;
10153454dc69STejun Heo 		int pmp;
10163454dc69STejun Heo 
10173454dc69STejun Heo 		abort = 1;
10183454dc69STejun Heo 
10193454dc69STejun Heo 		/* DMA Context Switch Failure in Port Multiplier Mode
10203454dc69STejun Heo 		 * errata.  If we have active commands to 3 or more
10213454dc69STejun Heo 		 * devices, any error condition on active devices can
10223454dc69STejun Heo 		 * corrupt DMA context switching.
10233454dc69STejun Heo 		 */
10243454dc69STejun Heo 		if (ap->nr_active_links >= 3) {
10253454dc69STejun Heo 			ehi->err_mask |= AC_ERR_OTHER;
1026cf480626STejun Heo 			ehi->action |= ATA_EH_RESET;
10273454dc69STejun Heo 			ata_ehi_push_desc(ehi, "PMP DMA CS errata");
102823818034STejun Heo 			pp->do_port_rst = 1;
10293454dc69STejun Heo 			freeze = 1;
10303454dc69STejun Heo 		}
10313454dc69STejun Heo 
10323454dc69STejun Heo 		/* find out the offending link and qc */
1033071f44b1STejun Heo 		if (sata_pmp_attached(ap)) {
10343454dc69STejun Heo 			context = readl(port + PORT_CONTEXT);
10353454dc69STejun Heo 			pmp = (context >> 5) & 0xf;
10363454dc69STejun Heo 
10373454dc69STejun Heo 			if (pmp < ap->nr_pmp_links) {
10383454dc69STejun Heo 				link = &ap->pmp_link[pmp];
10393454dc69STejun Heo 				ehi = &link->eh_info;
10403454dc69STejun Heo 				qc = ata_qc_from_tag(ap, link->active_tag);
10413454dc69STejun Heo 
10423454dc69STejun Heo 				ata_ehi_clear_desc(ehi);
10433454dc69STejun Heo 				ata_ehi_push_desc(ehi, "irq_stat 0x%08x",
10443454dc69STejun Heo 						  irq_stat);
10453454dc69STejun Heo 			} else {
10463454dc69STejun Heo 				err_mask |= AC_ERR_HSM;
1047cf480626STejun Heo 				action |= ATA_EH_RESET;
10483454dc69STejun Heo 				freeze = 1;
10493454dc69STejun Heo 			}
10503454dc69STejun Heo 		} else
10513454dc69STejun Heo 			qc = ata_qc_from_tag(ap, link->active_tag);
1052c6fd2807SJeff Garzik 
1053c6fd2807SJeff Garzik 		/* analyze CMD_ERR */
1054c6fd2807SJeff Garzik 		cerr = readl(port + PORT_CMD_ERR);
1055c6fd2807SJeff Garzik 		if (cerr < ARRAY_SIZE(sil24_cerr_db))
1056c6fd2807SJeff Garzik 			ci = &sil24_cerr_db[cerr];
1057c6fd2807SJeff Garzik 
1058c6fd2807SJeff Garzik 		if (ci && ci->desc) {
1059c6fd2807SJeff Garzik 			err_mask |= ci->err_mask;
1060c6fd2807SJeff Garzik 			action |= ci->action;
1061cf480626STejun Heo 			if (action & ATA_EH_RESET)
1062c2e14f11STejun Heo 				freeze = 1;
1063b64bbc39STejun Heo 			ata_ehi_push_desc(ehi, "%s", ci->desc);
1064c6fd2807SJeff Garzik 		} else {
1065c6fd2807SJeff Garzik 			err_mask |= AC_ERR_OTHER;
1066cf480626STejun Heo 			action |= ATA_EH_RESET;
1067c2e14f11STejun Heo 			freeze = 1;
1068b64bbc39STejun Heo 			ata_ehi_push_desc(ehi, "unknown command error %d",
1069c6fd2807SJeff Garzik 					  cerr);
1070c6fd2807SJeff Garzik 		}
1071c6fd2807SJeff Garzik 
1072c6fd2807SJeff Garzik 		/* record error info */
1073520d06f9STejun Heo 		if (qc)
1074c6fd2807SJeff Garzik 			qc->err_mask |= err_mask;
1075520d06f9STejun Heo 		else
1076c6fd2807SJeff Garzik 			ehi->err_mask |= err_mask;
1077c6fd2807SJeff Garzik 
1078c6fd2807SJeff Garzik 		ehi->action |= action;
10793454dc69STejun Heo 
10803454dc69STejun Heo 		/* if PMP, resume */
1081071f44b1STejun Heo 		if (sata_pmp_attached(ap))
10823454dc69STejun Heo 			writel(PORT_CS_PMP_RESUME, port + PORT_CTRL_STAT);
1083c6fd2807SJeff Garzik 	}
1084c6fd2807SJeff Garzik 
1085c6fd2807SJeff Garzik 	/* freeze or abort */
1086c6fd2807SJeff Garzik 	if (freeze)
1087c6fd2807SJeff Garzik 		ata_port_freeze(ap);
10883454dc69STejun Heo 	else if (abort) {
10893454dc69STejun Heo 		if (qc)
10903454dc69STejun Heo 			ata_link_abort(qc->dev->link);
1091c6fd2807SJeff Garzik 		else
1092c6fd2807SJeff Garzik 			ata_port_abort(ap);
1093c6fd2807SJeff Garzik 	}
10943454dc69STejun Heo }
1095c6fd2807SJeff Garzik 
sil24_host_intr(struct ata_port * ap)1096c6fd2807SJeff Garzik static inline void sil24_host_intr(struct ata_port *ap)
1097c6fd2807SJeff Garzik {
1098350756f6STejun Heo 	void __iomem *port = sil24_port_base(ap);
1099c6fd2807SJeff Garzik 	u32 slot_stat, qc_active;
1100c6fd2807SJeff Garzik 	int rc;
1101c6fd2807SJeff Garzik 
1102228f47b9STejun Heo 	/* If PCIX_IRQ_WOC, there's an inherent race window between
1103228f47b9STejun Heo 	 * clearing IRQ pending status and reading PORT_SLOT_STAT
1104228f47b9STejun Heo 	 * which may cause spurious interrupts afterwards.  This is
1105228f47b9STejun Heo 	 * unavoidable and much better than losing interrupts which
1106228f47b9STejun Heo 	 * happens if IRQ pending is cleared after reading
1107228f47b9STejun Heo 	 * PORT_SLOT_STAT.
1108228f47b9STejun Heo 	 */
1109228f47b9STejun Heo 	if (ap->flags & SIL24_FLAG_PCIX_IRQ_WOC)
1110228f47b9STejun Heo 		writel(PORT_IRQ_COMPLETE, port + PORT_IRQ_STAT);
1111228f47b9STejun Heo 
1112c6fd2807SJeff Garzik 	slot_stat = readl(port + PORT_SLOT_STAT);
1113c6fd2807SJeff Garzik 
1114c6fd2807SJeff Garzik 	if (unlikely(slot_stat & HOST_SSTAT_ATTN)) {
1115c6fd2807SJeff Garzik 		sil24_error_intr(ap);
1116c6fd2807SJeff Garzik 		return;
1117c6fd2807SJeff Garzik 	}
1118c6fd2807SJeff Garzik 
1119c6fd2807SJeff Garzik 	qc_active = slot_stat & ~HOST_SSTAT_ATTN;
112079f97dadSTejun Heo 	rc = ata_qc_complete_multiple(ap, qc_active);
1121c6fd2807SJeff Garzik 	if (rc > 0)
1122c6fd2807SJeff Garzik 		return;
1123c6fd2807SJeff Garzik 	if (rc < 0) {
11249af5c9c9STejun Heo 		struct ata_eh_info *ehi = &ap->link.eh_info;
1125c6fd2807SJeff Garzik 		ehi->err_mask |= AC_ERR_HSM;
1126cf480626STejun Heo 		ehi->action |= ATA_EH_RESET;
1127c6fd2807SJeff Garzik 		ata_port_freeze(ap);
1128c6fd2807SJeff Garzik 		return;
1129c6fd2807SJeff Garzik 	}
1130c6fd2807SJeff Garzik 
1131228f47b9STejun Heo 	/* spurious interrupts are expected if PCIX_IRQ_WOC */
1132228f47b9STejun Heo 	if (!(ap->flags & SIL24_FLAG_PCIX_IRQ_WOC) && ata_ratelimit())
1133a9a79dfeSJoe Perches 		ata_port_info(ap,
1134a9a79dfeSJoe Perches 			"spurious interrupt (slot_stat 0x%x active_tag %d sactive 0x%x)\n",
11359af5c9c9STejun Heo 			slot_stat, ap->link.active_tag, ap->link.sactive);
1136c6fd2807SJeff Garzik }
1137c6fd2807SJeff Garzik 
sil24_interrupt(int irq,void * dev_instance)11387d12e780SDavid Howells static irqreturn_t sil24_interrupt(int irq, void *dev_instance)
1139c6fd2807SJeff Garzik {
1140cca3974eSJeff Garzik 	struct ata_host *host = dev_instance;
11410d5ff566STejun Heo 	void __iomem *host_base = host->iomap[SIL24_HOST_BAR];
1142c6fd2807SJeff Garzik 	unsigned handled = 0;
1143c6fd2807SJeff Garzik 	u32 status;
1144c6fd2807SJeff Garzik 	int i;
1145c6fd2807SJeff Garzik 
11460d5ff566STejun Heo 	status = readl(host_base + HOST_IRQ_STAT);
1147c6fd2807SJeff Garzik 
1148c6fd2807SJeff Garzik 	if (status == 0xffffffff) {
114911838230STim Small 		dev_err(host->dev, "IRQ status == 0xffffffff, "
1150c6fd2807SJeff Garzik 			"PCI fault or device removal?\n");
1151c6fd2807SJeff Garzik 		goto out;
1152c6fd2807SJeff Garzik 	}
1153c6fd2807SJeff Garzik 
1154c6fd2807SJeff Garzik 	if (!(status & IRQ_STAT_4PORTS))
1155c6fd2807SJeff Garzik 		goto out;
1156c6fd2807SJeff Garzik 
1157cca3974eSJeff Garzik 	spin_lock(&host->lock);
1158c6fd2807SJeff Garzik 
1159cca3974eSJeff Garzik 	for (i = 0; i < host->n_ports; i++)
1160c6fd2807SJeff Garzik 		if (status & (1 << i)) {
11613e4ec344STejun Heo 			sil24_host_intr(host->ports[i]);
1162c6fd2807SJeff Garzik 			handled++;
1163c6fd2807SJeff Garzik 		}
1164c6fd2807SJeff Garzik 
1165cca3974eSJeff Garzik 	spin_unlock(&host->lock);
1166c6fd2807SJeff Garzik  out:
1167c6fd2807SJeff Garzik 	return IRQ_RETVAL(handled);
1168c6fd2807SJeff Garzik }
1169c6fd2807SJeff Garzik 
sil24_error_handler(struct ata_port * ap)1170c6fd2807SJeff Garzik static void sil24_error_handler(struct ata_port *ap)
1171c6fd2807SJeff Garzik {
117223818034STejun Heo 	struct sil24_port_priv *pp = ap->private_data;
117323818034STejun Heo 
11743454dc69STejun Heo 	if (sil24_init_port(ap))
1175c6fd2807SJeff Garzik 		ata_eh_freeze_port(ap);
1176c6fd2807SJeff Garzik 
1177a1efdabaSTejun Heo 	sata_pmp_error_handler(ap);
117823818034STejun Heo 
117923818034STejun Heo 	pp->do_port_rst = 0;
1180c6fd2807SJeff Garzik }
1181c6fd2807SJeff Garzik 
sil24_post_internal_cmd(struct ata_queued_cmd * qc)1182c6fd2807SJeff Garzik static void sil24_post_internal_cmd(struct ata_queued_cmd *qc)
1183c6fd2807SJeff Garzik {
1184c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
1185c6fd2807SJeff Garzik 
1186c6fd2807SJeff Garzik 	/* make DMA engine forget about the failed command */
118787629312SNiklas Cassel 	if ((qc->flags & ATA_QCFLAG_EH) && sil24_init_port(ap))
11883454dc69STejun Heo 		ata_eh_freeze_port(ap);
1189c6fd2807SJeff Garzik }
1190c6fd2807SJeff Garzik 
sil24_port_start(struct ata_port * ap)1191c6fd2807SJeff Garzik static int sil24_port_start(struct ata_port *ap)
1192c6fd2807SJeff Garzik {
1193cca3974eSJeff Garzik 	struct device *dev = ap->host->dev;
1194c6fd2807SJeff Garzik 	struct sil24_port_priv *pp;
1195c6fd2807SJeff Garzik 	union sil24_cmd_block *cb;
1196c6fd2807SJeff Garzik 	size_t cb_size = sizeof(*cb) * SIL24_MAX_CMDS;
1197c6fd2807SJeff Garzik 	dma_addr_t cb_dma;
1198c6fd2807SJeff Garzik 
119924dc5f33STejun Heo 	pp = devm_kzalloc(dev, sizeof(*pp), GFP_KERNEL);
1200c6fd2807SJeff Garzik 	if (!pp)
120124dc5f33STejun Heo 		return -ENOMEM;
1202c6fd2807SJeff Garzik 
120324dc5f33STejun Heo 	cb = dmam_alloc_coherent(dev, cb_size, &cb_dma, GFP_KERNEL);
1204c6fd2807SJeff Garzik 	if (!cb)
120524dc5f33STejun Heo 		return -ENOMEM;
1206c6fd2807SJeff Garzik 
1207c6fd2807SJeff Garzik 	pp->cmd_block = cb;
1208c6fd2807SJeff Garzik 	pp->cmd_block_dma = cb_dma;
1209c6fd2807SJeff Garzik 
1210c6fd2807SJeff Garzik 	ap->private_data = pp;
1211c6fd2807SJeff Garzik 
1212350756f6STejun Heo 	ata_port_pbar_desc(ap, SIL24_HOST_BAR, -1, "host");
1213350756f6STejun Heo 	ata_port_pbar_desc(ap, SIL24_PORT_BAR, sil24_port_offset(ap), "port");
1214350756f6STejun Heo 
1215c6fd2807SJeff Garzik 	return 0;
1216c6fd2807SJeff Garzik }
1217c6fd2807SJeff Garzik 
sil24_init_controller(struct ata_host * host)12184447d351STejun Heo static void sil24_init_controller(struct ata_host *host)
1219c6fd2807SJeff Garzik {
12204447d351STejun Heo 	void __iomem *host_base = host->iomap[SIL24_HOST_BAR];
1221c6fd2807SJeff Garzik 	u32 tmp;
1222c6fd2807SJeff Garzik 	int i;
1223c6fd2807SJeff Garzik 
1224c6fd2807SJeff Garzik 	/* GPIO off */
1225c6fd2807SJeff Garzik 	writel(0, host_base + HOST_FLASH_CMD);
1226c6fd2807SJeff Garzik 
1227c6fd2807SJeff Garzik 	/* clear global reset & mask interrupts during initialization */
1228c6fd2807SJeff Garzik 	writel(0, host_base + HOST_CTRL);
1229c6fd2807SJeff Garzik 
1230c6fd2807SJeff Garzik 	/* init ports */
12314447d351STejun Heo 	for (i = 0; i < host->n_ports; i++) {
123223818034STejun Heo 		struct ata_port *ap = host->ports[i];
1233350756f6STejun Heo 		void __iomem *port = sil24_port_base(ap);
1234350756f6STejun Heo 
1235c6fd2807SJeff Garzik 
1236c6fd2807SJeff Garzik 		/* Initial PHY setting */
1237c6fd2807SJeff Garzik 		writel(0x20c, port + PORT_PHY_CFG);
1238c6fd2807SJeff Garzik 
1239c6fd2807SJeff Garzik 		/* Clear port RST */
1240c6fd2807SJeff Garzik 		tmp = readl(port + PORT_CTRL_STAT);
1241c6fd2807SJeff Garzik 		if (tmp & PORT_CS_PORT_RST) {
1242c6fd2807SJeff Garzik 			writel(PORT_CS_PORT_RST, port + PORT_CTRL_CLR);
124397750cebSTejun Heo 			tmp = ata_wait_register(NULL, port + PORT_CTRL_STAT,
1244c6fd2807SJeff Garzik 						PORT_CS_PORT_RST,
1245c6fd2807SJeff Garzik 						PORT_CS_PORT_RST, 10, 100);
1246c6fd2807SJeff Garzik 			if (tmp & PORT_CS_PORT_RST)
1247a44fec1fSJoe Perches 				dev_err(host->dev,
1248c6fd2807SJeff Garzik 					"failed to clear port RST\n");
1249c6fd2807SJeff Garzik 		}
1250c6fd2807SJeff Garzik 
125123818034STejun Heo 		/* configure port */
125223818034STejun Heo 		sil24_config_port(ap);
1253c6fd2807SJeff Garzik 	}
1254c6fd2807SJeff Garzik 
1255c6fd2807SJeff Garzik 	/* Turn on interrupts */
1256c6fd2807SJeff Garzik 	writel(IRQ_STAT_4PORTS, host_base + HOST_CTRL);
1257c6fd2807SJeff Garzik }
1258c6fd2807SJeff Garzik 
sil24_init_one(struct pci_dev * pdev,const struct pci_device_id * ent)1259c6fd2807SJeff Garzik static int sil24_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
1260c6fd2807SJeff Garzik {
126193e2618eSTejun Heo 	extern int __MARKER__sil24_cmd_block_is_sized_wrongly;
12624447d351STejun Heo 	struct ata_port_info pi = sil24_port_info[ent->driver_data];
12634447d351STejun Heo 	const struct ata_port_info *ppi[] = { &pi, NULL };
12644447d351STejun Heo 	void __iomem * const *iomap;
12654447d351STejun Heo 	struct ata_host *host;
1266350756f6STejun Heo 	int rc;
1267c6fd2807SJeff Garzik 	u32 tmp;
1268c6fd2807SJeff Garzik 
126993e2618eSTejun Heo 	/* cause link error if sil24_cmd_block is sized wrongly */
127093e2618eSTejun Heo 	if (sizeof(union sil24_cmd_block) != PAGE_SIZE)
127193e2618eSTejun Heo 		__MARKER__sil24_cmd_block_is_sized_wrongly = 1;
127293e2618eSTejun Heo 
127306296a1eSJoe Perches 	ata_print_version_once(&pdev->dev, DRV_VERSION);
1274c6fd2807SJeff Garzik 
12754447d351STejun Heo 	/* acquire resources */
127624dc5f33STejun Heo 	rc = pcim_enable_device(pdev);
1277c6fd2807SJeff Garzik 	if (rc)
1278c6fd2807SJeff Garzik 		return rc;
1279c6fd2807SJeff Garzik 
12800d5ff566STejun Heo 	rc = pcim_iomap_regions(pdev,
12810d5ff566STejun Heo 				(1 << SIL24_HOST_BAR) | (1 << SIL24_PORT_BAR),
12820d5ff566STejun Heo 				DRV_NAME);
1283c6fd2807SJeff Garzik 	if (rc)
128424dc5f33STejun Heo 		return rc;
12854447d351STejun Heo 	iomap = pcim_iomap_table(pdev);
1286c6fd2807SJeff Garzik 
12874447d351STejun Heo 	/* apply workaround for completion IRQ loss on PCI-X errata */
12884447d351STejun Heo 	if (pi.flags & SIL24_FLAG_PCIX_IRQ_WOC) {
12894447d351STejun Heo 		tmp = readl(iomap[SIL24_HOST_BAR] + HOST_CTRL);
12904447d351STejun Heo 		if (tmp & (HOST_CTRL_TRDY | HOST_CTRL_STOP | HOST_CTRL_DEVSEL))
1291a44fec1fSJoe Perches 			dev_info(&pdev->dev,
1292a44fec1fSJoe Perches 				 "Applying completion IRQ loss on PCI-X errata fix\n");
12934447d351STejun Heo 		else
12944447d351STejun Heo 			pi.flags &= ~SIL24_FLAG_PCIX_IRQ_WOC;
12954447d351STejun Heo 	}
12964447d351STejun Heo 
12974447d351STejun Heo 	/* allocate and fill host */
12984447d351STejun Heo 	host = ata_host_alloc_pinfo(&pdev->dev, ppi,
12994447d351STejun Heo 				    SIL24_FLAG2NPORTS(ppi[0]->flags));
13004447d351STejun Heo 	if (!host)
130124dc5f33STejun Heo 		return -ENOMEM;
13024447d351STejun Heo 	host->iomap = iomap;
1303c6fd2807SJeff Garzik 
13044447d351STejun Heo 	/* configure and activate the device */
1305dcc02c19SChristoph Hellwig 	rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
1306c6fd2807SJeff Garzik 	if (rc) {
1307dcc02c19SChristoph Hellwig 		dev_err(&pdev->dev, "DMA enable failed\n");
130824dc5f33STejun Heo 		return rc;
1309c6fd2807SJeff Garzik 	}
1310c6fd2807SJeff Garzik 
1311e8b3b5e9STejun Heo 	/* Set max read request size to 4096.  This slightly increases
1312e8b3b5e9STejun Heo 	 * write throughput for pci-e variants.
1313e8b3b5e9STejun Heo 	 */
1314e8b3b5e9STejun Heo 	pcie_set_readrq(pdev, 4096);
1315e8b3b5e9STejun Heo 
13164447d351STejun Heo 	sil24_init_controller(host);
1317c6fd2807SJeff Garzik 
1318dae77214SVivek Mahajan 	if (sata_sil24_msi && !pci_enable_msi(pdev)) {
1319a44fec1fSJoe Perches 		dev_info(&pdev->dev, "Using MSI\n");
1320dae77214SVivek Mahajan 		pci_intx(pdev, 0);
1321dae77214SVivek Mahajan 	}
1322dae77214SVivek Mahajan 
1323c6fd2807SJeff Garzik 	pci_set_master(pdev);
13244447d351STejun Heo 	return ata_host_activate(host, pdev->irq, sil24_interrupt, IRQF_SHARED,
13254447d351STejun Heo 				 &sil24_sht);
1326c6fd2807SJeff Garzik }
1327c6fd2807SJeff Garzik 
132858eb8cd5SBartlomiej Zolnierkiewicz #ifdef CONFIG_PM_SLEEP
sil24_pci_device_resume(struct pci_dev * pdev)1329c6fd2807SJeff Garzik static int sil24_pci_device_resume(struct pci_dev *pdev)
1330c6fd2807SJeff Garzik {
13310a86e1c8SJingoo Han 	struct ata_host *host = pci_get_drvdata(pdev);
13320d5ff566STejun Heo 	void __iomem *host_base = host->iomap[SIL24_HOST_BAR];
1333553c4aa6STejun Heo 	int rc;
1334c6fd2807SJeff Garzik 
1335553c4aa6STejun Heo 	rc = ata_pci_device_do_resume(pdev);
1336553c4aa6STejun Heo 	if (rc)
1337553c4aa6STejun Heo 		return rc;
1338c6fd2807SJeff Garzik 
1339c6fd2807SJeff Garzik 	if (pdev->dev.power.power_state.event == PM_EVENT_SUSPEND)
13400d5ff566STejun Heo 		writel(HOST_CTRL_GLOBAL_RST, host_base + HOST_CTRL);
1341c6fd2807SJeff Garzik 
13424447d351STejun Heo 	sil24_init_controller(host);
1343c6fd2807SJeff Garzik 
1344cca3974eSJeff Garzik 	ata_host_resume(host);
1345c6fd2807SJeff Garzik 
1346c6fd2807SJeff Garzik 	return 0;
1347c6fd2807SJeff Garzik }
134858eb8cd5SBartlomiej Zolnierkiewicz #endif
13493454dc69STejun Heo 
135058eb8cd5SBartlomiej Zolnierkiewicz #ifdef CONFIG_PM
sil24_port_resume(struct ata_port * ap)13513454dc69STejun Heo static int sil24_port_resume(struct ata_port *ap)
13523454dc69STejun Heo {
13533454dc69STejun Heo 	sil24_config_pmp(ap, ap->nr_pmp_links);
13543454dc69STejun Heo 	return 0;
13553454dc69STejun Heo }
1356281d426cSAlexey Dobriyan #endif
1357c6fd2807SJeff Garzik 
13582fc75da0SAxel Lin module_pci_driver(sil24_pci_driver);
1359c6fd2807SJeff Garzik 
1360c6fd2807SJeff Garzik MODULE_AUTHOR("Tejun Heo");
1361c6fd2807SJeff Garzik MODULE_DESCRIPTION("Silicon Image 3124/3132 SATA low-level driver");
1362c6fd2807SJeff Garzik MODULE_LICENSE("GPL");
1363c6fd2807SJeff Garzik MODULE_DEVICE_TABLE(pci, sil24_pci_tbl);
1364