Commit | Line | Data |
---|---|---|
edb33667 TH |
1 | /* |
2 | * sata_sil24.c - Driver for Silicon Image 3124/3132 SATA-2 controllers | |
3 | * | |
4 | * Copyright 2005 Tejun Heo | |
5 | * | |
6 | * Based on preview driver from Silicon Image. | |
7 | * | |
edb33667 TH |
8 | * This program is free software; you can redistribute it and/or modify it |
9 | * under the terms of the GNU General Public License as published by the | |
10 | * Free Software Foundation; either version 2, or (at your option) any | |
11 | * later version. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, but | |
14 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | * General Public License for more details. | |
17 | * | |
18 | */ | |
19 | ||
20 | #include <linux/kernel.h> | |
21 | #include <linux/module.h> | |
22 | #include <linux/pci.h> | |
23 | #include <linux/blkdev.h> | |
24 | #include <linux/delay.h> | |
25 | #include <linux/interrupt.h> | |
26 | #include <linux/dma-mapping.h> | |
a9524a76 | 27 | #include <linux/device.h> |
edb33667 | 28 | #include <scsi/scsi_host.h> |
193515d5 | 29 | #include <scsi/scsi_cmnd.h> |
edb33667 TH |
30 | #include <linux/libata.h> |
31 | #include <asm/io.h> | |
32 | ||
33 | #define DRV_NAME "sata_sil24" | |
8676ce07 | 34 | #define DRV_VERSION "0.3" |
edb33667 | 35 | |
edb33667 TH |
36 | /* |
37 | * Port request block (PRB) 32 bytes | |
38 | */ | |
39 | struct sil24_prb { | |
b4772574 AD |
40 | __le16 ctrl; |
41 | __le16 prot; | |
42 | __le32 rx_cnt; | |
edb33667 TH |
43 | u8 fis[6 * 4]; |
44 | }; | |
45 | ||
46 | /* | |
47 | * Scatter gather entry (SGE) 16 bytes | |
48 | */ | |
49 | struct sil24_sge { | |
b4772574 AD |
50 | __le64 addr; |
51 | __le32 cnt; | |
52 | __le32 flags; | |
edb33667 TH |
53 | }; |
54 | ||
55 | /* | |
56 | * Port multiplier | |
57 | */ | |
58 | struct sil24_port_multiplier { | |
b4772574 AD |
59 | __le32 diag; |
60 | __le32 sactive; | |
edb33667 TH |
61 | }; |
62 | ||
63 | enum { | |
64 | /* | |
65 | * Global controller registers (128 bytes @ BAR0) | |
66 | */ | |
67 | /* 32 bit regs */ | |
68 | HOST_SLOT_STAT = 0x00, /* 32 bit slot stat * 4 */ | |
69 | HOST_CTRL = 0x40, | |
70 | HOST_IRQ_STAT = 0x44, | |
71 | HOST_PHY_CFG = 0x48, | |
72 | HOST_BIST_CTRL = 0x50, | |
73 | HOST_BIST_PTRN = 0x54, | |
74 | HOST_BIST_STAT = 0x58, | |
75 | HOST_MEM_BIST_STAT = 0x5c, | |
76 | HOST_FLASH_CMD = 0x70, | |
77 | /* 8 bit regs */ | |
78 | HOST_FLASH_DATA = 0x74, | |
79 | HOST_TRANSITION_DETECT = 0x75, | |
80 | HOST_GPIO_CTRL = 0x76, | |
81 | HOST_I2C_ADDR = 0x78, /* 32 bit */ | |
82 | HOST_I2C_DATA = 0x7c, | |
83 | HOST_I2C_XFER_CNT = 0x7e, | |
84 | HOST_I2C_CTRL = 0x7f, | |
85 | ||
86 | /* HOST_SLOT_STAT bits */ | |
87 | HOST_SSTAT_ATTN = (1 << 31), | |
88 | ||
7dafc3fd TH |
89 | /* HOST_CTRL bits */ |
90 | HOST_CTRL_M66EN = (1 << 16), /* M66EN PCI bus signal */ | |
91 | HOST_CTRL_TRDY = (1 << 17), /* latched PCI TRDY */ | |
92 | HOST_CTRL_STOP = (1 << 18), /* latched PCI STOP */ | |
93 | HOST_CTRL_DEVSEL = (1 << 19), /* latched PCI DEVSEL */ | |
94 | HOST_CTRL_REQ64 = (1 << 20), /* latched PCI REQ64 */ | |
95 | ||
edb33667 TH |
96 | /* |
97 | * Port registers | |
98 | * (8192 bytes @ +0x0000, +0x2000, +0x4000 and +0x6000 @ BAR2) | |
99 | */ | |
100 | PORT_REGS_SIZE = 0x2000, | |
135da345 TH |
101 | |
102 | PORT_LRAM = 0x0000, /* 31 LRAM slots and PM regs */ | |
103 | PORT_LRAM_SLOT_SZ = 0x0080, /* 32 bytes PRB + 2 SGE, ACT... */ | |
edb33667 TH |
104 | |
105 | PORT_PM = 0x0f80, /* 8 bytes PM * 16 (128 bytes) */ | |
106 | /* 32 bit regs */ | |
83bbecc9 TH |
107 | PORT_CTRL_STAT = 0x1000, /* write: ctrl-set, read: stat */ |
108 | PORT_CTRL_CLR = 0x1004, /* write: ctrl-clear */ | |
109 | PORT_IRQ_STAT = 0x1008, /* high: status, low: interrupt */ | |
110 | PORT_IRQ_ENABLE_SET = 0x1010, /* write: enable-set */ | |
111 | PORT_IRQ_ENABLE_CLR = 0x1014, /* write: enable-clear */ | |
edb33667 | 112 | PORT_ACTIVATE_UPPER_ADDR= 0x101c, |
83bbecc9 TH |
113 | PORT_EXEC_FIFO = 0x1020, /* command execution fifo */ |
114 | PORT_CMD_ERR = 0x1024, /* command error number */ | |
edb33667 TH |
115 | PORT_FIS_CFG = 0x1028, |
116 | PORT_FIFO_THRES = 0x102c, | |
117 | /* 16 bit regs */ | |
118 | PORT_DECODE_ERR_CNT = 0x1040, | |
119 | PORT_DECODE_ERR_THRESH = 0x1042, | |
120 | PORT_CRC_ERR_CNT = 0x1044, | |
121 | PORT_CRC_ERR_THRESH = 0x1046, | |
122 | PORT_HSHK_ERR_CNT = 0x1048, | |
123 | PORT_HSHK_ERR_THRESH = 0x104a, | |
124 | /* 32 bit regs */ | |
125 | PORT_PHY_CFG = 0x1050, | |
126 | PORT_SLOT_STAT = 0x1800, | |
127 | PORT_CMD_ACTIVATE = 0x1c00, /* 64 bit cmd activate * 31 (248 bytes) */ | |
128 | PORT_EXEC_DIAG = 0x1e00, /* 32bit exec diag * 16 (64 bytes, 0-10 used on 3124) */ | |
129 | PORT_PSD_DIAG = 0x1e40, /* 32bit psd diag * 16 (64 bytes, 0-8 used on 3124) */ | |
130 | PORT_SCONTROL = 0x1f00, | |
131 | PORT_SSTATUS = 0x1f04, | |
132 | PORT_SERROR = 0x1f08, | |
133 | PORT_SACTIVE = 0x1f0c, | |
134 | ||
135 | /* PORT_CTRL_STAT bits */ | |
136 | PORT_CS_PORT_RST = (1 << 0), /* port reset */ | |
137 | PORT_CS_DEV_RST = (1 << 1), /* device reset */ | |
138 | PORT_CS_INIT = (1 << 2), /* port initialize */ | |
139 | PORT_CS_IRQ_WOC = (1 << 3), /* interrupt write one to clear */ | |
d10cb35a | 140 | PORT_CS_CDB16 = (1 << 5), /* 0=12b cdb, 1=16b cdb */ |
e382eb1d TH |
141 | PORT_CS_RESUME = (1 << 6), /* port resume */ |
142 | PORT_CS_32BIT_ACTV = (1 << 10), /* 32-bit activation */ | |
143 | PORT_CS_PM_EN = (1 << 13), /* port multiplier enable */ | |
144 | PORT_CS_RDY = (1 << 31), /* port ready to accept commands */ | |
edb33667 TH |
145 | |
146 | /* PORT_IRQ_STAT/ENABLE_SET/CLR */ | |
147 | /* bits[11:0] are masked */ | |
148 | PORT_IRQ_COMPLETE = (1 << 0), /* command(s) completed */ | |
149 | PORT_IRQ_ERROR = (1 << 1), /* command execution error */ | |
150 | PORT_IRQ_PORTRDY_CHG = (1 << 2), /* port ready change */ | |
151 | PORT_IRQ_PWR_CHG = (1 << 3), /* power management change */ | |
152 | PORT_IRQ_PHYRDY_CHG = (1 << 4), /* PHY ready change */ | |
153 | PORT_IRQ_COMWAKE = (1 << 5), /* COMWAKE received */ | |
7dafc3fd TH |
154 | PORT_IRQ_UNK_FIS = (1 << 6), /* unknown FIS received */ |
155 | PORT_IRQ_DEV_XCHG = (1 << 7), /* device exchanged */ | |
156 | PORT_IRQ_8B10B = (1 << 8), /* 8b/10b decode error threshold */ | |
157 | PORT_IRQ_CRC = (1 << 9), /* CRC error threshold */ | |
158 | PORT_IRQ_HANDSHAKE = (1 << 10), /* handshake error threshold */ | |
3b9f1d0f | 159 | PORT_IRQ_SDB_NOTIFY = (1 << 11), /* SDB notify received */ |
edb33667 | 160 | |
88ce7550 | 161 | DEF_PORT_IRQ = PORT_IRQ_COMPLETE | PORT_IRQ_ERROR | |
0542925b TH |
162 | PORT_IRQ_PHYRDY_CHG | PORT_IRQ_DEV_XCHG | |
163 | PORT_IRQ_UNK_FIS, | |
88ce7550 | 164 | |
edb33667 TH |
165 | /* bits[27:16] are unmasked (raw) */ |
166 | PORT_IRQ_RAW_SHIFT = 16, | |
167 | PORT_IRQ_MASKED_MASK = 0x7ff, | |
168 | PORT_IRQ_RAW_MASK = (0x7ff << PORT_IRQ_RAW_SHIFT), | |
169 | ||
170 | /* ENABLE_SET/CLR specific, intr steering - 2 bit field */ | |
171 | PORT_IRQ_STEER_SHIFT = 30, | |
172 | PORT_IRQ_STEER_MASK = (3 << PORT_IRQ_STEER_SHIFT), | |
173 | ||
174 | /* PORT_CMD_ERR constants */ | |
175 | PORT_CERR_DEV = 1, /* Error bit in D2H Register FIS */ | |
176 | PORT_CERR_SDB = 2, /* Error bit in SDB FIS */ | |
177 | PORT_CERR_DATA = 3, /* Error in data FIS not detected by dev */ | |
178 | PORT_CERR_SEND = 4, /* Initial cmd FIS transmission failure */ | |
179 | PORT_CERR_INCONSISTENT = 5, /* Protocol mismatch */ | |
180 | PORT_CERR_DIRECTION = 6, /* Data direction mismatch */ | |
181 | PORT_CERR_UNDERRUN = 7, /* Ran out of SGEs while writing */ | |
182 | PORT_CERR_OVERRUN = 8, /* Ran out of SGEs while reading */ | |
183 | PORT_CERR_PKT_PROT = 11, /* DIR invalid in 1st PIO setup of ATAPI */ | |
184 | PORT_CERR_SGT_BOUNDARY = 16, /* PLD ecode 00 - SGT not on qword boundary */ | |
185 | PORT_CERR_SGT_TGTABRT = 17, /* PLD ecode 01 - target abort */ | |
186 | PORT_CERR_SGT_MSTABRT = 18, /* PLD ecode 10 - master abort */ | |
187 | PORT_CERR_SGT_PCIPERR = 19, /* PLD ecode 11 - PCI parity err while fetching SGT */ | |
188 | PORT_CERR_CMD_BOUNDARY = 24, /* ctrl[15:13] 001 - PRB not on qword boundary */ | |
189 | PORT_CERR_CMD_TGTABRT = 25, /* ctrl[15:13] 010 - target abort */ | |
190 | PORT_CERR_CMD_MSTABRT = 26, /* ctrl[15:13] 100 - master abort */ | |
191 | PORT_CERR_CMD_PCIPERR = 27, /* ctrl[15:13] 110 - PCI parity err while fetching PRB */ | |
192 | PORT_CERR_XFR_UNDEF = 32, /* PSD ecode 00 - undefined */ | |
193 | PORT_CERR_XFR_TGTABRT = 33, /* PSD ecode 01 - target abort */ | |
64008802 | 194 | PORT_CERR_XFR_MSTABRT = 34, /* PSD ecode 10 - master abort */ |
edb33667 | 195 | PORT_CERR_XFR_PCIPERR = 35, /* PSD ecode 11 - PCI prity err during transfer */ |
83bbecc9 | 196 | PORT_CERR_SENDSERVICE = 36, /* FIS received while sending service */ |
edb33667 | 197 | |
d10cb35a TH |
198 | /* bits of PRB control field */ |
199 | PRB_CTRL_PROTOCOL = (1 << 0), /* override def. ATA protocol */ | |
200 | PRB_CTRL_PACKET_READ = (1 << 4), /* PACKET cmd read */ | |
201 | PRB_CTRL_PACKET_WRITE = (1 << 5), /* PACKET cmd write */ | |
202 | PRB_CTRL_NIEN = (1 << 6), /* Mask completion irq */ | |
203 | PRB_CTRL_SRST = (1 << 7), /* Soft reset request (ign BSY?) */ | |
204 | ||
205 | /* PRB protocol field */ | |
206 | PRB_PROT_PACKET = (1 << 0), | |
207 | PRB_PROT_TCQ = (1 << 1), | |
208 | PRB_PROT_NCQ = (1 << 2), | |
209 | PRB_PROT_READ = (1 << 3), | |
210 | PRB_PROT_WRITE = (1 << 4), | |
211 | PRB_PROT_TRANSPARENT = (1 << 5), | |
212 | ||
edb33667 TH |
213 | /* |
214 | * Other constants | |
215 | */ | |
216 | SGE_TRM = (1 << 31), /* Last SGE in chain */ | |
d10cb35a TH |
217 | SGE_LNK = (1 << 30), /* linked list |
218 | Points to SGT, not SGE */ | |
219 | SGE_DRD = (1 << 29), /* discard data read (/dev/null) | |
220 | data address ignored */ | |
edb33667 | 221 | |
aee10a03 TH |
222 | SIL24_MAX_CMDS = 31, |
223 | ||
edb33667 TH |
224 | /* board id */ |
225 | BID_SIL3124 = 0, | |
226 | BID_SIL3132 = 1, | |
042c21fd | 227 | BID_SIL3131 = 2, |
edb33667 | 228 | |
9466d85b TH |
229 | /* host flags */ |
230 | SIL24_COMMON_FLAGS = ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY | | |
aee10a03 | 231 | ATA_FLAG_MMIO | ATA_FLAG_PIO_DMA | |
0542925b | 232 | ATA_FLAG_NCQ | ATA_FLAG_SKIP_D2H_BSY, |
37024e8e | 233 | SIL24_FLAG_PCIX_IRQ_WOC = (1 << 24), /* IRQ loss errata on PCI-X */ |
9466d85b | 234 | |
edb33667 TH |
235 | IRQ_STAT_4PORTS = 0xf, |
236 | }; | |
237 | ||
69ad185f | 238 | struct sil24_ata_block { |
edb33667 TH |
239 | struct sil24_prb prb; |
240 | struct sil24_sge sge[LIBATA_MAX_PRD]; | |
241 | }; | |
242 | ||
69ad185f TH |
243 | struct sil24_atapi_block { |
244 | struct sil24_prb prb; | |
245 | u8 cdb[16]; | |
246 | struct sil24_sge sge[LIBATA_MAX_PRD - 1]; | |
247 | }; | |
248 | ||
249 | union sil24_cmd_block { | |
250 | struct sil24_ata_block ata; | |
251 | struct sil24_atapi_block atapi; | |
252 | }; | |
253 | ||
88ce7550 TH |
254 | static struct sil24_cerr_info { |
255 | unsigned int err_mask, action; | |
256 | const char *desc; | |
257 | } sil24_cerr_db[] = { | |
258 | [0] = { AC_ERR_DEV, ATA_EH_REVALIDATE, | |
259 | "device error" }, | |
260 | [PORT_CERR_DEV] = { AC_ERR_DEV, ATA_EH_REVALIDATE, | |
261 | "device error via D2H FIS" }, | |
262 | [PORT_CERR_SDB] = { AC_ERR_DEV, ATA_EH_REVALIDATE, | |
263 | "device error via SDB FIS" }, | |
264 | [PORT_CERR_DATA] = { AC_ERR_ATA_BUS, ATA_EH_SOFTRESET, | |
265 | "error in data FIS" }, | |
266 | [PORT_CERR_SEND] = { AC_ERR_ATA_BUS, ATA_EH_SOFTRESET, | |
267 | "failed to transmit command FIS" }, | |
268 | [PORT_CERR_INCONSISTENT] = { AC_ERR_HSM, ATA_EH_SOFTRESET, | |
269 | "protocol mismatch" }, | |
270 | [PORT_CERR_DIRECTION] = { AC_ERR_HSM, ATA_EH_SOFTRESET, | |
271 | "data directon mismatch" }, | |
272 | [PORT_CERR_UNDERRUN] = { AC_ERR_HSM, ATA_EH_SOFTRESET, | |
273 | "ran out of SGEs while writing" }, | |
274 | [PORT_CERR_OVERRUN] = { AC_ERR_HSM, ATA_EH_SOFTRESET, | |
275 | "ran out of SGEs while reading" }, | |
276 | [PORT_CERR_PKT_PROT] = { AC_ERR_HSM, ATA_EH_SOFTRESET, | |
277 | "invalid data directon for ATAPI CDB" }, | |
278 | [PORT_CERR_SGT_BOUNDARY] = { AC_ERR_SYSTEM, ATA_EH_SOFTRESET, | |
279 | "SGT no on qword boundary" }, | |
280 | [PORT_CERR_SGT_TGTABRT] = { AC_ERR_HOST_BUS, ATA_EH_SOFTRESET, | |
281 | "PCI target abort while fetching SGT" }, | |
282 | [PORT_CERR_SGT_MSTABRT] = { AC_ERR_HOST_BUS, ATA_EH_SOFTRESET, | |
283 | "PCI master abort while fetching SGT" }, | |
284 | [PORT_CERR_SGT_PCIPERR] = { AC_ERR_HOST_BUS, ATA_EH_SOFTRESET, | |
285 | "PCI parity error while fetching SGT" }, | |
286 | [PORT_CERR_CMD_BOUNDARY] = { AC_ERR_SYSTEM, ATA_EH_SOFTRESET, | |
287 | "PRB not on qword boundary" }, | |
288 | [PORT_CERR_CMD_TGTABRT] = { AC_ERR_HOST_BUS, ATA_EH_SOFTRESET, | |
289 | "PCI target abort while fetching PRB" }, | |
290 | [PORT_CERR_CMD_MSTABRT] = { AC_ERR_HOST_BUS, ATA_EH_SOFTRESET, | |
291 | "PCI master abort while fetching PRB" }, | |
292 | [PORT_CERR_CMD_PCIPERR] = { AC_ERR_HOST_BUS, ATA_EH_SOFTRESET, | |
293 | "PCI parity error while fetching PRB" }, | |
294 | [PORT_CERR_XFR_UNDEF] = { AC_ERR_HOST_BUS, ATA_EH_SOFTRESET, | |
295 | "undefined error while transferring data" }, | |
296 | [PORT_CERR_XFR_TGTABRT] = { AC_ERR_HOST_BUS, ATA_EH_SOFTRESET, | |
297 | "PCI target abort while transferring data" }, | |
298 | [PORT_CERR_XFR_MSTABRT] = { AC_ERR_HOST_BUS, ATA_EH_SOFTRESET, | |
299 | "PCI master abort while transferring data" }, | |
300 | [PORT_CERR_XFR_PCIPERR] = { AC_ERR_HOST_BUS, ATA_EH_SOFTRESET, | |
301 | "PCI parity error while transferring data" }, | |
302 | [PORT_CERR_SENDSERVICE] = { AC_ERR_HSM, ATA_EH_SOFTRESET, | |
303 | "FIS received while sending service FIS" }, | |
304 | }; | |
305 | ||
edb33667 TH |
306 | /* |
307 | * ap->private_data | |
308 | * | |
309 | * The preview driver always returned 0 for status. We emulate it | |
310 | * here from the previous interrupt. | |
311 | */ | |
312 | struct sil24_port_priv { | |
69ad185f | 313 | union sil24_cmd_block *cmd_block; /* 32 cmd blocks */ |
edb33667 | 314 | dma_addr_t cmd_block_dma; /* DMA base addr for them */ |
6a575fa9 | 315 | struct ata_taskfile tf; /* Cached taskfile registers */ |
edb33667 TH |
316 | }; |
317 | ||
318 | /* ap->host_set->private_data */ | |
319 | struct sil24_host_priv { | |
4b4a5eae AV |
320 | void __iomem *host_base; /* global controller control (128 bytes @BAR0) */ |
321 | void __iomem *port_base; /* port registers (4 * 8192 bytes @BAR2) */ | |
edb33667 TH |
322 | }; |
323 | ||
69ad185f | 324 | static void sil24_dev_config(struct ata_port *ap, struct ata_device *dev); |
edb33667 | 325 | static u8 sil24_check_status(struct ata_port *ap); |
edb33667 TH |
326 | static u32 sil24_scr_read(struct ata_port *ap, unsigned sc_reg); |
327 | static void sil24_scr_write(struct ata_port *ap, unsigned sc_reg, u32 val); | |
7f726d12 | 328 | static void sil24_tf_read(struct ata_port *ap, struct ata_taskfile *tf); |
edb33667 | 329 | static void sil24_qc_prep(struct ata_queued_cmd *qc); |
9a3d9eb0 | 330 | static unsigned int sil24_qc_issue(struct ata_queued_cmd *qc); |
edb33667 | 331 | static void sil24_irq_clear(struct ata_port *ap); |
edb33667 | 332 | static irqreturn_t sil24_interrupt(int irq, void *dev_instance, struct pt_regs *regs); |
88ce7550 TH |
333 | static void sil24_freeze(struct ata_port *ap); |
334 | static void sil24_thaw(struct ata_port *ap); | |
335 | static void sil24_error_handler(struct ata_port *ap); | |
336 | static void sil24_post_internal_cmd(struct ata_queued_cmd *qc); | |
edb33667 TH |
337 | static int sil24_port_start(struct ata_port *ap); |
338 | static void sil24_port_stop(struct ata_port *ap); | |
339 | static void sil24_host_stop(struct ata_host_set *host_set); | |
340 | static int sil24_init_one(struct pci_dev *pdev, const struct pci_device_id *ent); | |
341 | ||
3b7d697d | 342 | static const struct pci_device_id sil24_pci_tbl[] = { |
edb33667 | 343 | { 0x1095, 0x3124, PCI_ANY_ID, PCI_ANY_ID, 0, 0, BID_SIL3124 }, |
4b9d7e04 | 344 | { 0x8086, 0x3124, PCI_ANY_ID, PCI_ANY_ID, 0, 0, BID_SIL3124 }, |
edb33667 | 345 | { 0x1095, 0x3132, PCI_ANY_ID, PCI_ANY_ID, 0, 0, BID_SIL3132 }, |
042c21fd TH |
346 | { 0x1095, 0x3131, PCI_ANY_ID, PCI_ANY_ID, 0, 0, BID_SIL3131 }, |
347 | { 0x1095, 0x3531, PCI_ANY_ID, PCI_ANY_ID, 0, 0, BID_SIL3131 }, | |
1fcce839 | 348 | { } /* terminate list */ |
edb33667 TH |
349 | }; |
350 | ||
351 | static struct pci_driver sil24_pci_driver = { | |
352 | .name = DRV_NAME, | |
353 | .id_table = sil24_pci_tbl, | |
354 | .probe = sil24_init_one, | |
355 | .remove = ata_pci_remove_one, /* safe? */ | |
356 | }; | |
357 | ||
193515d5 | 358 | static struct scsi_host_template sil24_sht = { |
edb33667 TH |
359 | .module = THIS_MODULE, |
360 | .name = DRV_NAME, | |
361 | .ioctl = ata_scsi_ioctl, | |
362 | .queuecommand = ata_scsi_queuecmd, | |
aee10a03 TH |
363 | .change_queue_depth = ata_scsi_change_queue_depth, |
364 | .can_queue = SIL24_MAX_CMDS, | |
edb33667 TH |
365 | .this_id = ATA_SHT_THIS_ID, |
366 | .sg_tablesize = LIBATA_MAX_PRD, | |
edb33667 TH |
367 | .cmd_per_lun = ATA_SHT_CMD_PER_LUN, |
368 | .emulated = ATA_SHT_EMULATED, | |
369 | .use_clustering = ATA_SHT_USE_CLUSTERING, | |
370 | .proc_name = DRV_NAME, | |
371 | .dma_boundary = ATA_DMA_BOUNDARY, | |
372 | .slave_configure = ata_scsi_slave_config, | |
ccf68c34 | 373 | .slave_destroy = ata_scsi_slave_destroy, |
edb33667 | 374 | .bios_param = ata_std_bios_param, |
edb33667 TH |
375 | }; |
376 | ||
057ace5e | 377 | static const struct ata_port_operations sil24_ops = { |
edb33667 TH |
378 | .port_disable = ata_port_disable, |
379 | ||
69ad185f TH |
380 | .dev_config = sil24_dev_config, |
381 | ||
edb33667 TH |
382 | .check_status = sil24_check_status, |
383 | .check_altstatus = sil24_check_status, | |
edb33667 TH |
384 | .dev_select = ata_noop_dev_select, |
385 | ||
7f726d12 TH |
386 | .tf_read = sil24_tf_read, |
387 | ||
edb33667 TH |
388 | .qc_prep = sil24_qc_prep, |
389 | .qc_issue = sil24_qc_issue, | |
390 | ||
edb33667 TH |
391 | .irq_handler = sil24_interrupt, |
392 | .irq_clear = sil24_irq_clear, | |
393 | ||
394 | .scr_read = sil24_scr_read, | |
395 | .scr_write = sil24_scr_write, | |
396 | ||
88ce7550 TH |
397 | .freeze = sil24_freeze, |
398 | .thaw = sil24_thaw, | |
399 | .error_handler = sil24_error_handler, | |
400 | .post_internal_cmd = sil24_post_internal_cmd, | |
401 | ||
edb33667 TH |
402 | .port_start = sil24_port_start, |
403 | .port_stop = sil24_port_stop, | |
404 | .host_stop = sil24_host_stop, | |
405 | }; | |
406 | ||
042c21fd TH |
407 | /* |
408 | * Use bits 30-31 of host_flags to encode available port numbers. | |
409 | * Current maxium is 4. | |
410 | */ | |
411 | #define SIL24_NPORTS2FLAG(nports) ((((unsigned)(nports) - 1) & 0x3) << 30) | |
412 | #define SIL24_FLAG2NPORTS(flag) ((((flag) >> 30) & 0x3) + 1) | |
413 | ||
edb33667 TH |
414 | static struct ata_port_info sil24_port_info[] = { |
415 | /* sil_3124 */ | |
416 | { | |
417 | .sht = &sil24_sht, | |
37024e8e TH |
418 | .host_flags = SIL24_COMMON_FLAGS | SIL24_NPORTS2FLAG(4) | |
419 | SIL24_FLAG_PCIX_IRQ_WOC, | |
edb33667 TH |
420 | .pio_mask = 0x1f, /* pio0-4 */ |
421 | .mwdma_mask = 0x07, /* mwdma0-2 */ | |
422 | .udma_mask = 0x3f, /* udma0-5 */ | |
423 | .port_ops = &sil24_ops, | |
424 | }, | |
2e9edbf8 | 425 | /* sil_3132 */ |
edb33667 TH |
426 | { |
427 | .sht = &sil24_sht, | |
9466d85b | 428 | .host_flags = SIL24_COMMON_FLAGS | SIL24_NPORTS2FLAG(2), |
042c21fd TH |
429 | .pio_mask = 0x1f, /* pio0-4 */ |
430 | .mwdma_mask = 0x07, /* mwdma0-2 */ | |
431 | .udma_mask = 0x3f, /* udma0-5 */ | |
432 | .port_ops = &sil24_ops, | |
433 | }, | |
434 | /* sil_3131/sil_3531 */ | |
435 | { | |
436 | .sht = &sil24_sht, | |
9466d85b | 437 | .host_flags = SIL24_COMMON_FLAGS | SIL24_NPORTS2FLAG(1), |
edb33667 TH |
438 | .pio_mask = 0x1f, /* pio0-4 */ |
439 | .mwdma_mask = 0x07, /* mwdma0-2 */ | |
440 | .udma_mask = 0x3f, /* udma0-5 */ | |
441 | .port_ops = &sil24_ops, | |
442 | }, | |
443 | }; | |
444 | ||
aee10a03 TH |
445 | static int sil24_tag(int tag) |
446 | { | |
447 | if (unlikely(ata_tag_internal(tag))) | |
448 | return 0; | |
449 | return tag; | |
450 | } | |
451 | ||
69ad185f TH |
452 | static void sil24_dev_config(struct ata_port *ap, struct ata_device *dev) |
453 | { | |
454 | void __iomem *port = (void __iomem *)ap->ioaddr.cmd_addr; | |
455 | ||
6e7846e9 | 456 | if (dev->cdb_len == 16) |
69ad185f TH |
457 | writel(PORT_CS_CDB16, port + PORT_CTRL_STAT); |
458 | else | |
459 | writel(PORT_CS_CDB16, port + PORT_CTRL_CLR); | |
460 | } | |
461 | ||
6a575fa9 TH |
462 | static inline void sil24_update_tf(struct ata_port *ap) |
463 | { | |
464 | struct sil24_port_priv *pp = ap->private_data; | |
4b4a5eae AV |
465 | void __iomem *port = (void __iomem *)ap->ioaddr.cmd_addr; |
466 | struct sil24_prb __iomem *prb = port; | |
467 | u8 fis[6 * 4]; | |
6a575fa9 | 468 | |
4b4a5eae AV |
469 | memcpy_fromio(fis, prb->fis, 6 * 4); |
470 | ata_tf_from_fis(fis, &pp->tf); | |
6a575fa9 TH |
471 | } |
472 | ||
edb33667 TH |
473 | static u8 sil24_check_status(struct ata_port *ap) |
474 | { | |
6a575fa9 TH |
475 | struct sil24_port_priv *pp = ap->private_data; |
476 | return pp->tf.command; | |
edb33667 TH |
477 | } |
478 | ||
edb33667 TH |
479 | static int sil24_scr_map[] = { |
480 | [SCR_CONTROL] = 0, | |
481 | [SCR_STATUS] = 1, | |
482 | [SCR_ERROR] = 2, | |
483 | [SCR_ACTIVE] = 3, | |
484 | }; | |
485 | ||
486 | static u32 sil24_scr_read(struct ata_port *ap, unsigned sc_reg) | |
487 | { | |
4b4a5eae | 488 | void __iomem *scr_addr = (void __iomem *)ap->ioaddr.scr_addr; |
edb33667 | 489 | if (sc_reg < ARRAY_SIZE(sil24_scr_map)) { |
4b4a5eae | 490 | void __iomem *addr; |
edb33667 TH |
491 | addr = scr_addr + sil24_scr_map[sc_reg] * 4; |
492 | return readl(scr_addr + sil24_scr_map[sc_reg] * 4); | |
493 | } | |
494 | return 0xffffffffU; | |
495 | } | |
496 | ||
497 | static void sil24_scr_write(struct ata_port *ap, unsigned sc_reg, u32 val) | |
498 | { | |
4b4a5eae | 499 | void __iomem *scr_addr = (void __iomem *)ap->ioaddr.scr_addr; |
edb33667 | 500 | if (sc_reg < ARRAY_SIZE(sil24_scr_map)) { |
4b4a5eae | 501 | void __iomem *addr; |
edb33667 TH |
502 | addr = scr_addr + sil24_scr_map[sc_reg] * 4; |
503 | writel(val, scr_addr + sil24_scr_map[sc_reg] * 4); | |
504 | } | |
505 | } | |
506 | ||
7f726d12 TH |
507 | static void sil24_tf_read(struct ata_port *ap, struct ata_taskfile *tf) |
508 | { | |
509 | struct sil24_port_priv *pp = ap->private_data; | |
510 | *tf = pp->tf; | |
511 | } | |
512 | ||
b5bc421c TH |
513 | static int sil24_init_port(struct ata_port *ap) |
514 | { | |
515 | void __iomem *port = (void __iomem *)ap->ioaddr.cmd_addr; | |
516 | u32 tmp; | |
517 | ||
518 | writel(PORT_CS_INIT, port + PORT_CTRL_STAT); | |
519 | ata_wait_register(port + PORT_CTRL_STAT, | |
520 | PORT_CS_INIT, PORT_CS_INIT, 10, 100); | |
521 | tmp = ata_wait_register(port + PORT_CTRL_STAT, | |
522 | PORT_CS_RDY, 0, 10, 100); | |
523 | ||
524 | if ((tmp & (PORT_CS_INIT | PORT_CS_RDY)) != PORT_CS_RDY) | |
525 | return -EIO; | |
526 | return 0; | |
527 | } | |
528 | ||
2bf2cb26 | 529 | static int sil24_softreset(struct ata_port *ap, unsigned int *class) |
edb33667 | 530 | { |
ca45160d TH |
531 | void __iomem *port = (void __iomem *)ap->ioaddr.cmd_addr; |
532 | struct sil24_port_priv *pp = ap->private_data; | |
69ad185f | 533 | struct sil24_prb *prb = &pp->cmd_block[0].ata.prb; |
ca45160d | 534 | dma_addr_t paddr = pp->cmd_block_dma; |
88ce7550 | 535 | u32 mask, irq_stat; |
643be977 | 536 | const char *reason; |
ca45160d | 537 | |
07b73470 TH |
538 | DPRINTK("ENTER\n"); |
539 | ||
81952c54 | 540 | if (ata_port_offline(ap)) { |
10d996ad TH |
541 | DPRINTK("PHY reports no device\n"); |
542 | *class = ATA_DEV_NONE; | |
543 | goto out; | |
544 | } | |
545 | ||
2555d6c2 TH |
546 | /* put the port into known state */ |
547 | if (sil24_init_port(ap)) { | |
548 | reason ="port not ready"; | |
549 | goto err; | |
550 | } | |
551 | ||
0eaa6058 | 552 | /* do SRST */ |
bad28a37 | 553 | prb->ctrl = cpu_to_le16(PRB_CTRL_SRST); |
ca45160d TH |
554 | prb->fis[1] = 0; /* no PM yet */ |
555 | ||
556 | writel((u32)paddr, port + PORT_CMD_ACTIVATE); | |
26ec634c | 557 | writel((u64)paddr >> 32, port + PORT_CMD_ACTIVATE + 4); |
ca45160d | 558 | |
7dd29dd6 TH |
559 | mask = (PORT_IRQ_COMPLETE | PORT_IRQ_ERROR) << PORT_IRQ_RAW_SHIFT; |
560 | irq_stat = ata_wait_register(port + PORT_IRQ_STAT, mask, 0x0, | |
561 | 100, ATA_TMOUT_BOOT / HZ * 1000); | |
ca45160d | 562 | |
7dd29dd6 TH |
563 | writel(irq_stat, port + PORT_IRQ_STAT); /* clear IRQs */ |
564 | irq_stat >>= PORT_IRQ_RAW_SHIFT; | |
ca45160d | 565 | |
10d996ad | 566 | if (!(irq_stat & PORT_IRQ_COMPLETE)) { |
643be977 TH |
567 | if (irq_stat & PORT_IRQ_ERROR) |
568 | reason = "SRST command error"; | |
569 | else | |
570 | reason = "timeout"; | |
571 | goto err; | |
07b73470 | 572 | } |
10d996ad TH |
573 | |
574 | sil24_update_tf(ap); | |
575 | *class = ata_dev_classify(&pp->tf); | |
576 | ||
07b73470 TH |
577 | if (*class == ATA_DEV_UNKNOWN) |
578 | *class = ATA_DEV_NONE; | |
ca45160d | 579 | |
10d996ad | 580 | out: |
07b73470 | 581 | DPRINTK("EXIT, class=%u\n", *class); |
ca45160d | 582 | return 0; |
643be977 TH |
583 | |
584 | err: | |
f15a1daf | 585 | ata_port_printk(ap, KERN_ERR, "softreset failed (%s)\n", reason); |
643be977 | 586 | return -EIO; |
ca45160d TH |
587 | } |
588 | ||
2bf2cb26 | 589 | static int sil24_hardreset(struct ata_port *ap, unsigned int *class) |
489ff4c7 | 590 | { |
ecc2e2b9 TH |
591 | void __iomem *port = (void __iomem *)ap->ioaddr.cmd_addr; |
592 | const char *reason; | |
e8e008e7 | 593 | int tout_msec, rc; |
ecc2e2b9 TH |
594 | u32 tmp; |
595 | ||
596 | /* sil24 does the right thing(tm) without any protection */ | |
3c567b7d | 597 | sata_set_spd(ap); |
ecc2e2b9 TH |
598 | |
599 | tout_msec = 100; | |
81952c54 | 600 | if (ata_port_online(ap)) |
ecc2e2b9 TH |
601 | tout_msec = 5000; |
602 | ||
603 | writel(PORT_CS_DEV_RST, port + PORT_CTRL_STAT); | |
604 | tmp = ata_wait_register(port + PORT_CTRL_STAT, | |
605 | PORT_CS_DEV_RST, PORT_CS_DEV_RST, 10, tout_msec); | |
606 | ||
e8e008e7 TH |
607 | /* SStatus oscillates between zero and valid status after |
608 | * DEV_RST, debounce it. | |
ecc2e2b9 | 609 | */ |
e8e008e7 TH |
610 | rc = sata_phy_debounce(ap, sata_deb_timing_before_fsrst); |
611 | if (rc) { | |
612 | reason = "PHY debouncing failed"; | |
613 | goto err; | |
614 | } | |
ecc2e2b9 TH |
615 | |
616 | if (tmp & PORT_CS_DEV_RST) { | |
81952c54 | 617 | if (ata_port_offline(ap)) |
ecc2e2b9 TH |
618 | return 0; |
619 | reason = "link not ready"; | |
620 | goto err; | |
621 | } | |
622 | ||
e8e008e7 TH |
623 | /* Sil24 doesn't store signature FIS after hardreset, so we |
624 | * can't wait for BSY to clear. Some devices take a long time | |
625 | * to get ready and those devices will choke if we don't wait | |
626 | * for BSY clearance here. Tell libata to perform follow-up | |
627 | * softreset. | |
ecc2e2b9 | 628 | */ |
e8e008e7 | 629 | return -EAGAIN; |
ecc2e2b9 TH |
630 | |
631 | err: | |
f15a1daf | 632 | ata_port_printk(ap, KERN_ERR, "hardreset failed (%s)\n", reason); |
ecc2e2b9 | 633 | return -EIO; |
489ff4c7 TH |
634 | } |
635 | ||
edb33667 | 636 | static inline void sil24_fill_sg(struct ata_queued_cmd *qc, |
69ad185f | 637 | struct sil24_sge *sge) |
edb33667 | 638 | { |
972c26bd JG |
639 | struct scatterlist *sg; |
640 | unsigned int idx = 0; | |
edb33667 | 641 | |
972c26bd | 642 | ata_for_each_sg(sg, qc) { |
edb33667 TH |
643 | sge->addr = cpu_to_le64(sg_dma_address(sg)); |
644 | sge->cnt = cpu_to_le32(sg_dma_len(sg)); | |
972c26bd JG |
645 | if (ata_sg_is_last(sg, qc)) |
646 | sge->flags = cpu_to_le32(SGE_TRM); | |
647 | else | |
648 | sge->flags = 0; | |
649 | ||
650 | sge++; | |
651 | idx++; | |
edb33667 TH |
652 | } |
653 | } | |
654 | ||
655 | static void sil24_qc_prep(struct ata_queued_cmd *qc) | |
656 | { | |
657 | struct ata_port *ap = qc->ap; | |
658 | struct sil24_port_priv *pp = ap->private_data; | |
aee10a03 | 659 | union sil24_cmd_block *cb; |
69ad185f TH |
660 | struct sil24_prb *prb; |
661 | struct sil24_sge *sge; | |
bad28a37 | 662 | u16 ctrl = 0; |
edb33667 | 663 | |
aee10a03 TH |
664 | cb = &pp->cmd_block[sil24_tag(qc->tag)]; |
665 | ||
edb33667 TH |
666 | switch (qc->tf.protocol) { |
667 | case ATA_PROT_PIO: | |
668 | case ATA_PROT_DMA: | |
aee10a03 | 669 | case ATA_PROT_NCQ: |
edb33667 | 670 | case ATA_PROT_NODATA: |
69ad185f TH |
671 | prb = &cb->ata.prb; |
672 | sge = cb->ata.sge; | |
edb33667 | 673 | break; |
69ad185f TH |
674 | |
675 | case ATA_PROT_ATAPI: | |
676 | case ATA_PROT_ATAPI_DMA: | |
677 | case ATA_PROT_ATAPI_NODATA: | |
678 | prb = &cb->atapi.prb; | |
679 | sge = cb->atapi.sge; | |
680 | memset(cb->atapi.cdb, 0, 32); | |
6e7846e9 | 681 | memcpy(cb->atapi.cdb, qc->cdb, qc->dev->cdb_len); |
69ad185f TH |
682 | |
683 | if (qc->tf.protocol != ATA_PROT_ATAPI_NODATA) { | |
684 | if (qc->tf.flags & ATA_TFLAG_WRITE) | |
bad28a37 | 685 | ctrl = PRB_CTRL_PACKET_WRITE; |
69ad185f | 686 | else |
bad28a37 TH |
687 | ctrl = PRB_CTRL_PACKET_READ; |
688 | } | |
69ad185f TH |
689 | break; |
690 | ||
edb33667 | 691 | default: |
69ad185f TH |
692 | prb = NULL; /* shut up, gcc */ |
693 | sge = NULL; | |
edb33667 TH |
694 | BUG(); |
695 | } | |
696 | ||
bad28a37 | 697 | prb->ctrl = cpu_to_le16(ctrl); |
edb33667 TH |
698 | ata_tf_to_fis(&qc->tf, prb->fis, 0); |
699 | ||
700 | if (qc->flags & ATA_QCFLAG_DMAMAP) | |
69ad185f | 701 | sil24_fill_sg(qc, sge); |
edb33667 TH |
702 | } |
703 | ||
9a3d9eb0 | 704 | static unsigned int sil24_qc_issue(struct ata_queued_cmd *qc) |
edb33667 TH |
705 | { |
706 | struct ata_port *ap = qc->ap; | |
707 | struct sil24_port_priv *pp = ap->private_data; | |
aee10a03 TH |
708 | void __iomem *port = (void __iomem *)ap->ioaddr.cmd_addr; |
709 | unsigned int tag = sil24_tag(qc->tag); | |
710 | dma_addr_t paddr; | |
711 | void __iomem *activate; | |
edb33667 | 712 | |
aee10a03 TH |
713 | paddr = pp->cmd_block_dma + tag * sizeof(*pp->cmd_block); |
714 | activate = port + PORT_CMD_ACTIVATE + tag * 8; | |
715 | ||
716 | writel((u32)paddr, activate); | |
717 | writel((u64)paddr >> 32, activate + 4); | |
26ec634c | 718 | |
edb33667 TH |
719 | return 0; |
720 | } | |
721 | ||
722 | static void sil24_irq_clear(struct ata_port *ap) | |
723 | { | |
724 | /* unused */ | |
725 | } | |
726 | ||
88ce7550 | 727 | static void sil24_freeze(struct ata_port *ap) |
7d1ce682 | 728 | { |
88ce7550 | 729 | void __iomem *port = (void __iomem *)ap->ioaddr.cmd_addr; |
7d1ce682 | 730 | |
88ce7550 TH |
731 | /* Port-wide IRQ mask in HOST_CTRL doesn't really work, clear |
732 | * PORT_IRQ_ENABLE instead. | |
733 | */ | |
734 | writel(0xffff, port + PORT_IRQ_ENABLE_CLR); | |
7d1ce682 TH |
735 | } |
736 | ||
88ce7550 | 737 | static void sil24_thaw(struct ata_port *ap) |
edb33667 | 738 | { |
88ce7550 | 739 | void __iomem *port = (void __iomem *)ap->ioaddr.cmd_addr; |
edb33667 TH |
740 | u32 tmp; |
741 | ||
88ce7550 TH |
742 | /* clear IRQ */ |
743 | tmp = readl(port + PORT_IRQ_STAT); | |
744 | writel(tmp, port + PORT_IRQ_STAT); | |
edb33667 | 745 | |
88ce7550 TH |
746 | /* turn IRQ back on */ |
747 | writel(DEF_PORT_IRQ, port + PORT_IRQ_ENABLE_SET); | |
edb33667 TH |
748 | } |
749 | ||
88ce7550 | 750 | static void sil24_error_intr(struct ata_port *ap) |
8746618d | 751 | { |
4b4a5eae | 752 | void __iomem *port = (void __iomem *)ap->ioaddr.cmd_addr; |
88ce7550 TH |
753 | struct ata_eh_info *ehi = &ap->eh_info; |
754 | int freeze = 0; | |
755 | u32 irq_stat; | |
8746618d | 756 | |
88ce7550 | 757 | /* on error, we need to clear IRQ explicitly */ |
8746618d | 758 | irq_stat = readl(port + PORT_IRQ_STAT); |
88ce7550 | 759 | writel(irq_stat, port + PORT_IRQ_STAT); |
ad6e90f6 | 760 | |
88ce7550 TH |
761 | /* first, analyze and record host port events */ |
762 | ata_ehi_clear_desc(ehi); | |
ad6e90f6 | 763 | |
88ce7550 | 764 | ata_ehi_push_desc(ehi, "irq_stat 0x%08x", irq_stat); |
8746618d | 765 | |
0542925b TH |
766 | if (irq_stat & (PORT_IRQ_PHYRDY_CHG | PORT_IRQ_DEV_XCHG)) { |
767 | ata_ehi_hotplugged(ehi); | |
768 | ata_ehi_push_desc(ehi, ", %s", | |
769 | irq_stat & PORT_IRQ_PHYRDY_CHG ? | |
770 | "PHY RDY changed" : "device exchanged"); | |
88ce7550 | 771 | freeze = 1; |
6a575fa9 TH |
772 | } |
773 | ||
88ce7550 TH |
774 | if (irq_stat & PORT_IRQ_UNK_FIS) { |
775 | ehi->err_mask |= AC_ERR_HSM; | |
776 | ehi->action |= ATA_EH_SOFTRESET; | |
777 | ata_ehi_push_desc(ehi , ", unknown FIS"); | |
778 | freeze = 1; | |
779 | } | |
780 | ||
781 | /* deal with command error */ | |
782 | if (irq_stat & PORT_IRQ_ERROR) { | |
783 | struct sil24_cerr_info *ci = NULL; | |
784 | unsigned int err_mask = 0, action = 0; | |
785 | struct ata_queued_cmd *qc; | |
786 | u32 cerr; | |
787 | ||
788 | /* analyze CMD_ERR */ | |
789 | cerr = readl(port + PORT_CMD_ERR); | |
790 | if (cerr < ARRAY_SIZE(sil24_cerr_db)) | |
791 | ci = &sil24_cerr_db[cerr]; | |
792 | ||
793 | if (ci && ci->desc) { | |
794 | err_mask |= ci->err_mask; | |
795 | action |= ci->action; | |
796 | ata_ehi_push_desc(ehi, ", %s", ci->desc); | |
797 | } else { | |
798 | err_mask |= AC_ERR_OTHER; | |
799 | action |= ATA_EH_SOFTRESET; | |
800 | ata_ehi_push_desc(ehi, ", unknown command error %d", | |
801 | cerr); | |
802 | } | |
803 | ||
804 | /* record error info */ | |
805 | qc = ata_qc_from_tag(ap, ap->active_tag); | |
806 | if (qc) { | |
88ce7550 TH |
807 | sil24_update_tf(ap); |
808 | qc->err_mask |= err_mask; | |
809 | } else | |
810 | ehi->err_mask |= err_mask; | |
811 | ||
812 | ehi->action |= action; | |
a22e2eb0 | 813 | } |
88ce7550 TH |
814 | |
815 | /* freeze or abort */ | |
816 | if (freeze) | |
817 | ata_port_freeze(ap); | |
818 | else | |
819 | ata_port_abort(ap); | |
8746618d TH |
820 | } |
821 | ||
aee10a03 TH |
822 | static void sil24_finish_qc(struct ata_queued_cmd *qc) |
823 | { | |
824 | if (qc->flags & ATA_QCFLAG_RESULT_TF) | |
825 | sil24_update_tf(qc->ap); | |
826 | } | |
827 | ||
edb33667 TH |
828 | static inline void sil24_host_intr(struct ata_port *ap) |
829 | { | |
4b4a5eae | 830 | void __iomem *port = (void __iomem *)ap->ioaddr.cmd_addr; |
aee10a03 TH |
831 | u32 slot_stat, qc_active; |
832 | int rc; | |
edb33667 TH |
833 | |
834 | slot_stat = readl(port + PORT_SLOT_STAT); | |
37024e8e | 835 | |
88ce7550 TH |
836 | if (unlikely(slot_stat & HOST_SSTAT_ATTN)) { |
837 | sil24_error_intr(ap); | |
838 | return; | |
839 | } | |
840 | ||
841 | if (ap->flags & SIL24_FLAG_PCIX_IRQ_WOC) | |
842 | writel(PORT_IRQ_COMPLETE, port + PORT_IRQ_STAT); | |
37024e8e | 843 | |
aee10a03 TH |
844 | qc_active = slot_stat & ~HOST_SSTAT_ATTN; |
845 | rc = ata_qc_complete_multiple(ap, qc_active, sil24_finish_qc); | |
846 | if (rc > 0) | |
847 | return; | |
848 | if (rc < 0) { | |
849 | struct ata_eh_info *ehi = &ap->eh_info; | |
850 | ehi->err_mask |= AC_ERR_HSM; | |
851 | ehi->action |= ATA_EH_SOFTRESET; | |
852 | ata_port_freeze(ap); | |
88ce7550 TH |
853 | return; |
854 | } | |
855 | ||
856 | if (ata_ratelimit()) | |
857 | ata_port_printk(ap, KERN_INFO, "spurious interrupt " | |
aee10a03 TH |
858 | "(slot_stat 0x%x active_tag %d sactive 0x%x)\n", |
859 | slot_stat, ap->active_tag, ap->sactive); | |
edb33667 TH |
860 | } |
861 | ||
862 | static irqreturn_t sil24_interrupt(int irq, void *dev_instance, struct pt_regs *regs) | |
863 | { | |
864 | struct ata_host_set *host_set = dev_instance; | |
865 | struct sil24_host_priv *hpriv = host_set->private_data; | |
866 | unsigned handled = 0; | |
867 | u32 status; | |
868 | int i; | |
869 | ||
870 | status = readl(hpriv->host_base + HOST_IRQ_STAT); | |
871 | ||
06460aea TH |
872 | if (status == 0xffffffff) { |
873 | printk(KERN_ERR DRV_NAME ": IRQ status == 0xffffffff, " | |
874 | "PCI fault or device removal?\n"); | |
875 | goto out; | |
876 | } | |
877 | ||
edb33667 TH |
878 | if (!(status & IRQ_STAT_4PORTS)) |
879 | goto out; | |
880 | ||
881 | spin_lock(&host_set->lock); | |
882 | ||
883 | for (i = 0; i < host_set->n_ports; i++) | |
884 | if (status & (1 << i)) { | |
885 | struct ata_port *ap = host_set->ports[i]; | |
198e0fed | 886 | if (ap && !(ap->flags & ATA_FLAG_DISABLED)) { |
edb33667 | 887 | sil24_host_intr(host_set->ports[i]); |
3cc4571c TH |
888 | handled++; |
889 | } else | |
890 | printk(KERN_ERR DRV_NAME | |
891 | ": interrupt from disabled port %d\n", i); | |
edb33667 TH |
892 | } |
893 | ||
894 | spin_unlock(&host_set->lock); | |
895 | out: | |
896 | return IRQ_RETVAL(handled); | |
897 | } | |
898 | ||
88ce7550 TH |
899 | static void sil24_error_handler(struct ata_port *ap) |
900 | { | |
901 | struct ata_eh_context *ehc = &ap->eh_context; | |
902 | ||
903 | if (sil24_init_port(ap)) { | |
904 | ata_eh_freeze_port(ap); | |
905 | ehc->i.action |= ATA_EH_HARDRESET; | |
906 | } | |
907 | ||
908 | /* perform recovery */ | |
f5914a46 TH |
909 | ata_do_eh(ap, ata_std_prereset, sil24_softreset, sil24_hardreset, |
910 | ata_std_postreset); | |
88ce7550 TH |
911 | } |
912 | ||
913 | static void sil24_post_internal_cmd(struct ata_queued_cmd *qc) | |
914 | { | |
915 | struct ata_port *ap = qc->ap; | |
916 | ||
917 | if (qc->flags & ATA_QCFLAG_FAILED) | |
918 | qc->err_mask |= AC_ERR_OTHER; | |
919 | ||
920 | /* make DMA engine forget about the failed command */ | |
921 | if (qc->err_mask) | |
922 | sil24_init_port(ap); | |
923 | } | |
924 | ||
6037d6bb JG |
925 | static inline void sil24_cblk_free(struct sil24_port_priv *pp, struct device *dev) |
926 | { | |
aee10a03 | 927 | const size_t cb_size = sizeof(*pp->cmd_block) * SIL24_MAX_CMDS; |
6037d6bb JG |
928 | |
929 | dma_free_coherent(dev, cb_size, pp->cmd_block, pp->cmd_block_dma); | |
930 | } | |
931 | ||
edb33667 TH |
932 | static int sil24_port_start(struct ata_port *ap) |
933 | { | |
934 | struct device *dev = ap->host_set->dev; | |
edb33667 | 935 | struct sil24_port_priv *pp; |
69ad185f | 936 | union sil24_cmd_block *cb; |
aee10a03 | 937 | size_t cb_size = sizeof(*cb) * SIL24_MAX_CMDS; |
edb33667 | 938 | dma_addr_t cb_dma; |
6037d6bb | 939 | int rc = -ENOMEM; |
edb33667 | 940 | |
6037d6bb | 941 | pp = kzalloc(sizeof(*pp), GFP_KERNEL); |
edb33667 | 942 | if (!pp) |
6037d6bb | 943 | goto err_out; |
edb33667 | 944 | |
6a575fa9 TH |
945 | pp->tf.command = ATA_DRDY; |
946 | ||
edb33667 | 947 | cb = dma_alloc_coherent(dev, cb_size, &cb_dma, GFP_KERNEL); |
6037d6bb JG |
948 | if (!cb) |
949 | goto err_out_pp; | |
edb33667 TH |
950 | memset(cb, 0, cb_size); |
951 | ||
6037d6bb JG |
952 | rc = ata_pad_alloc(ap, dev); |
953 | if (rc) | |
954 | goto err_out_pad; | |
955 | ||
edb33667 TH |
956 | pp->cmd_block = cb; |
957 | pp->cmd_block_dma = cb_dma; | |
958 | ||
959 | ap->private_data = pp; | |
960 | ||
961 | return 0; | |
6037d6bb JG |
962 | |
963 | err_out_pad: | |
964 | sil24_cblk_free(pp, dev); | |
965 | err_out_pp: | |
966 | kfree(pp); | |
967 | err_out: | |
968 | return rc; | |
edb33667 TH |
969 | } |
970 | ||
971 | static void sil24_port_stop(struct ata_port *ap) | |
972 | { | |
973 | struct device *dev = ap->host_set->dev; | |
974 | struct sil24_port_priv *pp = ap->private_data; | |
edb33667 | 975 | |
6037d6bb | 976 | sil24_cblk_free(pp, dev); |
e9c05afa | 977 | ata_pad_free(ap, dev); |
edb33667 TH |
978 | kfree(pp); |
979 | } | |
980 | ||
981 | static void sil24_host_stop(struct ata_host_set *host_set) | |
982 | { | |
983 | struct sil24_host_priv *hpriv = host_set->private_data; | |
142877b0 | 984 | struct pci_dev *pdev = to_pci_dev(host_set->dev); |
edb33667 | 985 | |
142877b0 JG |
986 | pci_iounmap(pdev, hpriv->host_base); |
987 | pci_iounmap(pdev, hpriv->port_base); | |
edb33667 TH |
988 | kfree(hpriv); |
989 | } | |
990 | ||
991 | static int sil24_init_one(struct pci_dev *pdev, const struct pci_device_id *ent) | |
992 | { | |
993 | static int printed_version = 0; | |
994 | unsigned int board_id = (unsigned int)ent->driver_data; | |
042c21fd | 995 | struct ata_port_info *pinfo = &sil24_port_info[board_id]; |
edb33667 TH |
996 | struct ata_probe_ent *probe_ent = NULL; |
997 | struct sil24_host_priv *hpriv = NULL; | |
4b4a5eae AV |
998 | void __iomem *host_base = NULL; |
999 | void __iomem *port_base = NULL; | |
edb33667 | 1000 | int i, rc; |
37024e8e | 1001 | u32 tmp; |
edb33667 TH |
1002 | |
1003 | if (!printed_version++) | |
a9524a76 | 1004 | dev_printk(KERN_DEBUG, &pdev->dev, "version " DRV_VERSION "\n"); |
edb33667 TH |
1005 | |
1006 | rc = pci_enable_device(pdev); | |
1007 | if (rc) | |
1008 | return rc; | |
1009 | ||
1010 | rc = pci_request_regions(pdev, DRV_NAME); | |
1011 | if (rc) | |
1012 | goto out_disable; | |
1013 | ||
1014 | rc = -ENOMEM; | |
142877b0 JG |
1015 | /* map mmio registers */ |
1016 | host_base = pci_iomap(pdev, 0, 0); | |
edb33667 TH |
1017 | if (!host_base) |
1018 | goto out_free; | |
142877b0 | 1019 | port_base = pci_iomap(pdev, 2, 0); |
edb33667 TH |
1020 | if (!port_base) |
1021 | goto out_free; | |
1022 | ||
1023 | /* allocate & init probe_ent and hpriv */ | |
142877b0 | 1024 | probe_ent = kzalloc(sizeof(*probe_ent), GFP_KERNEL); |
edb33667 TH |
1025 | if (!probe_ent) |
1026 | goto out_free; | |
1027 | ||
142877b0 | 1028 | hpriv = kzalloc(sizeof(*hpriv), GFP_KERNEL); |
edb33667 TH |
1029 | if (!hpriv) |
1030 | goto out_free; | |
1031 | ||
edb33667 TH |
1032 | probe_ent->dev = pci_dev_to_dev(pdev); |
1033 | INIT_LIST_HEAD(&probe_ent->node); | |
1034 | ||
042c21fd TH |
1035 | probe_ent->sht = pinfo->sht; |
1036 | probe_ent->host_flags = pinfo->host_flags; | |
1037 | probe_ent->pio_mask = pinfo->pio_mask; | |
fbfda6e7 | 1038 | probe_ent->mwdma_mask = pinfo->mwdma_mask; |
042c21fd TH |
1039 | probe_ent->udma_mask = pinfo->udma_mask; |
1040 | probe_ent->port_ops = pinfo->port_ops; | |
1041 | probe_ent->n_ports = SIL24_FLAG2NPORTS(pinfo->host_flags); | |
edb33667 TH |
1042 | |
1043 | probe_ent->irq = pdev->irq; | |
1044 | probe_ent->irq_flags = SA_SHIRQ; | |
1045 | probe_ent->mmio_base = port_base; | |
1046 | probe_ent->private_data = hpriv; | |
1047 | ||
edb33667 TH |
1048 | hpriv->host_base = host_base; |
1049 | hpriv->port_base = port_base; | |
1050 | ||
1051 | /* | |
1052 | * Configure the device | |
1053 | */ | |
26ec634c TH |
1054 | if (!pci_set_dma_mask(pdev, DMA_64BIT_MASK)) { |
1055 | rc = pci_set_consistent_dma_mask(pdev, DMA_64BIT_MASK); | |
1056 | if (rc) { | |
1057 | rc = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK); | |
1058 | if (rc) { | |
1059 | dev_printk(KERN_ERR, &pdev->dev, | |
1060 | "64-bit DMA enable failed\n"); | |
1061 | goto out_free; | |
1062 | } | |
1063 | } | |
1064 | } else { | |
1065 | rc = pci_set_dma_mask(pdev, DMA_32BIT_MASK); | |
1066 | if (rc) { | |
1067 | dev_printk(KERN_ERR, &pdev->dev, | |
1068 | "32-bit DMA enable failed\n"); | |
1069 | goto out_free; | |
1070 | } | |
1071 | rc = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK); | |
1072 | if (rc) { | |
1073 | dev_printk(KERN_ERR, &pdev->dev, | |
1074 | "32-bit consistent DMA enable failed\n"); | |
1075 | goto out_free; | |
1076 | } | |
edb33667 TH |
1077 | } |
1078 | ||
1079 | /* GPIO off */ | |
1080 | writel(0, host_base + HOST_FLASH_CMD); | |
1081 | ||
37024e8e TH |
1082 | /* Apply workaround for completion IRQ loss on PCI-X errata */ |
1083 | if (probe_ent->host_flags & SIL24_FLAG_PCIX_IRQ_WOC) { | |
1084 | tmp = readl(host_base + HOST_CTRL); | |
1085 | if (tmp & (HOST_CTRL_TRDY | HOST_CTRL_STOP | HOST_CTRL_DEVSEL)) | |
1086 | dev_printk(KERN_INFO, &pdev->dev, | |
1087 | "Applying completion IRQ loss on PCI-X " | |
1088 | "errata fix\n"); | |
1089 | else | |
1090 | probe_ent->host_flags &= ~SIL24_FLAG_PCIX_IRQ_WOC; | |
1091 | } | |
1092 | ||
7dd29dd6 | 1093 | /* clear global reset & mask interrupts during initialization */ |
edb33667 TH |
1094 | writel(0, host_base + HOST_CTRL); |
1095 | ||
1096 | for (i = 0; i < probe_ent->n_ports; i++) { | |
4b4a5eae | 1097 | void __iomem *port = port_base + i * PORT_REGS_SIZE; |
edb33667 | 1098 | unsigned long portu = (unsigned long)port; |
edb33667 | 1099 | |
135da345 | 1100 | probe_ent->port[i].cmd_addr = portu; |
edb33667 TH |
1101 | probe_ent->port[i].scr_addr = portu + PORT_SCONTROL; |
1102 | ||
1103 | ata_std_ports(&probe_ent->port[i]); | |
1104 | ||
1105 | /* Initial PHY setting */ | |
1106 | writel(0x20c, port + PORT_PHY_CFG); | |
1107 | ||
1108 | /* Clear port RST */ | |
1109 | tmp = readl(port + PORT_CTRL_STAT); | |
1110 | if (tmp & PORT_CS_PORT_RST) { | |
1111 | writel(PORT_CS_PORT_RST, port + PORT_CTRL_CLR); | |
7dd29dd6 TH |
1112 | tmp = ata_wait_register(port + PORT_CTRL_STAT, |
1113 | PORT_CS_PORT_RST, | |
1114 | PORT_CS_PORT_RST, 10, 100); | |
edb33667 | 1115 | if (tmp & PORT_CS_PORT_RST) |
a9524a76 JG |
1116 | dev_printk(KERN_ERR, &pdev->dev, |
1117 | "failed to clear port RST\n"); | |
edb33667 TH |
1118 | } |
1119 | ||
37024e8e TH |
1120 | /* Configure IRQ WoC */ |
1121 | if (probe_ent->host_flags & SIL24_FLAG_PCIX_IRQ_WOC) | |
1122 | writel(PORT_CS_IRQ_WOC, port + PORT_CTRL_STAT); | |
1123 | else | |
1124 | writel(PORT_CS_IRQ_WOC, port + PORT_CTRL_CLR); | |
1125 | ||
edb33667 TH |
1126 | /* Zero error counters. */ |
1127 | writel(0x8000, port + PORT_DECODE_ERR_THRESH); | |
1128 | writel(0x8000, port + PORT_CRC_ERR_THRESH); | |
1129 | writel(0x8000, port + PORT_HSHK_ERR_THRESH); | |
1130 | writel(0x0000, port + PORT_DECODE_ERR_CNT); | |
1131 | writel(0x0000, port + PORT_CRC_ERR_CNT); | |
1132 | writel(0x0000, port + PORT_HSHK_ERR_CNT); | |
1133 | ||
26ec634c TH |
1134 | /* Always use 64bit activation */ |
1135 | writel(PORT_CS_32BIT_ACTV, port + PORT_CTRL_CLR); | |
edb33667 | 1136 | |
923f1225 TH |
1137 | /* Clear port multiplier enable and resume bits */ |
1138 | writel(PORT_CS_PM_EN | PORT_CS_RESUME, port + PORT_CTRL_CLR); | |
edb33667 TH |
1139 | } |
1140 | ||
1141 | /* Turn on interrupts */ | |
1142 | writel(IRQ_STAT_4PORTS, host_base + HOST_CTRL); | |
1143 | ||
1144 | pci_set_master(pdev); | |
1145 | ||
1483467f | 1146 | /* FIXME: check ata_device_add return value */ |
edb33667 TH |
1147 | ata_device_add(probe_ent); |
1148 | ||
1149 | kfree(probe_ent); | |
1150 | return 0; | |
1151 | ||
1152 | out_free: | |
1153 | if (host_base) | |
142877b0 | 1154 | pci_iounmap(pdev, host_base); |
edb33667 | 1155 | if (port_base) |
142877b0 | 1156 | pci_iounmap(pdev, port_base); |
edb33667 TH |
1157 | kfree(probe_ent); |
1158 | kfree(hpriv); | |
1159 | pci_release_regions(pdev); | |
1160 | out_disable: | |
1161 | pci_disable_device(pdev); | |
1162 | return rc; | |
1163 | } | |
1164 | ||
1165 | static int __init sil24_init(void) | |
1166 | { | |
1167 | return pci_module_init(&sil24_pci_driver); | |
1168 | } | |
1169 | ||
1170 | static void __exit sil24_exit(void) | |
1171 | { | |
1172 | pci_unregister_driver(&sil24_pci_driver); | |
1173 | } | |
1174 | ||
1175 | MODULE_AUTHOR("Tejun Heo"); | |
1176 | MODULE_DESCRIPTION("Silicon Image 3124/3132 SATA low-level driver"); | |
1177 | MODULE_LICENSE("GPL"); | |
1178 | MODULE_DEVICE_TABLE(pci, sil24_pci_tbl); | |
1179 | ||
1180 | module_init(sil24_init); | |
1181 | module_exit(sil24_exit); |