1195534Sscottl/*-
2230132Suqs * Copyright (c) 1998 - 2008 S��ren Schmidt <sos@FreeBSD.org>
3238805Smav * Copyright (c) 2009-2012 Alexander Motin <mav@FreeBSD.org>
4195534Sscottl * All rights reserved.
5195534Sscottl *
6195534Sscottl * Redistribution and use in source and binary forms, with or without
7195534Sscottl * modification, are permitted provided that the following conditions
8195534Sscottl * are met:
9195534Sscottl * 1. Redistributions of source code must retain the above copyright
10195534Sscottl *    notice, this list of conditions and the following disclaimer,
11195534Sscottl *    without modification, immediately at the beginning of the file.
12195534Sscottl * 2. Redistributions in binary form must reproduce the above copyright
13195534Sscottl *    notice, this list of conditions and the following disclaimer in the
14195534Sscottl *    documentation and/or other materials provided with the distribution.
15195534Sscottl *
16195534Sscottl * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17195534Sscottl * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18195534Sscottl * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19195534Sscottl * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20195534Sscottl * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21195534Sscottl * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22195534Sscottl * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23195534Sscottl * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24195534Sscottl * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25195534Sscottl * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26195534Sscottl *
27195534Sscottl * $FreeBSD: stable/10/sys/dev/ahci/ahci.h 313446 2017-02-08 16:07:59Z mav $
28195534Sscottl */
29195534Sscottl
30195534Sscottl/* ATA register defines */
31195534Sscottl#define ATA_DATA                        0       /* (RW) data */
32195534Sscottl
33195534Sscottl#define ATA_FEATURE                     1       /* (W) feature */
34195534Sscottl#define         ATA_F_DMA               0x01    /* enable DMA */
35195534Sscottl#define         ATA_F_OVL               0x02    /* enable overlap */
36195534Sscottl
37195534Sscottl#define ATA_COUNT                       2       /* (W) sector count */
38195534Sscottl
39195534Sscottl#define ATA_SECTOR                      3       /* (RW) sector # */
40195534Sscottl#define ATA_CYL_LSB                     4       /* (RW) cylinder# LSB */
41195534Sscottl#define ATA_CYL_MSB                     5       /* (RW) cylinder# MSB */
42195534Sscottl#define ATA_DRIVE                       6       /* (W) Sector/Drive/Head */
43195534Sscottl#define         ATA_D_LBA               0x40    /* use LBA addressing */
44195534Sscottl#define         ATA_D_IBM               0xa0    /* 512 byte sectors, ECC */
45195534Sscottl
46195534Sscottl#define ATA_COMMAND                     7       /* (W) command */
47195534Sscottl
48195534Sscottl#define ATA_ERROR                       8       /* (R) error */
49195534Sscottl#define         ATA_E_ILI               0x01    /* illegal length */
50195534Sscottl#define         ATA_E_NM                0x02    /* no media */
51195534Sscottl#define         ATA_E_ABORT             0x04    /* command aborted */
52195534Sscottl#define         ATA_E_MCR               0x08    /* media change request */
53195534Sscottl#define         ATA_E_IDNF              0x10    /* ID not found */
54195534Sscottl#define         ATA_E_MC                0x20    /* media changed */
55195534Sscottl#define         ATA_E_UNC               0x40    /* uncorrectable data */
56195534Sscottl#define         ATA_E_ICRC              0x80    /* UDMA crc error */
57195534Sscottl#define		ATA_E_ATAPI_SENSE_MASK	0xf0	/* ATAPI sense key mask */
58195534Sscottl
59195534Sscottl#define ATA_IREASON                     9       /* (R) interrupt reason */
60195534Sscottl#define         ATA_I_CMD               0x01    /* cmd (1) | data (0) */
61195534Sscottl#define         ATA_I_IN                0x02    /* read (1) | write (0) */
62195534Sscottl#define         ATA_I_RELEASE           0x04    /* released bus (1) */
63195534Sscottl#define         ATA_I_TAGMASK           0xf8    /* tag mask */
64195534Sscottl
65195534Sscottl#define ATA_STATUS                      10      /* (R) status */
66195534Sscottl#define ATA_ALTSTAT                     11      /* (R) alternate status */
67195534Sscottl#define         ATA_S_ERROR             0x01    /* error */
68195534Sscottl#define         ATA_S_INDEX             0x02    /* index */
69195534Sscottl#define         ATA_S_CORR              0x04    /* data corrected */
70195534Sscottl#define         ATA_S_DRQ               0x08    /* data request */
71195534Sscottl#define         ATA_S_DSC               0x10    /* drive seek completed */
72195534Sscottl#define         ATA_S_SERVICE           0x10    /* drive needs service */
73195534Sscottl#define         ATA_S_DWF               0x20    /* drive write fault */
74195534Sscottl#define         ATA_S_DMA               0x20    /* DMA ready */
75195534Sscottl#define         ATA_S_READY             0x40    /* drive ready */
76195534Sscottl#define         ATA_S_BUSY              0x80    /* busy */
77195534Sscottl
78195534Sscottl#define ATA_CONTROL                     12      /* (W) control */
79195534Sscottl#define         ATA_A_IDS               0x02    /* disable interrupts */
80195534Sscottl#define         ATA_A_RESET             0x04    /* RESET controller */
81195534Sscottl#define         ATA_A_4BIT              0x08    /* 4 head bits */
82195534Sscottl#define         ATA_A_HOB               0x80    /* High Order Byte enable */
83195534Sscottl
84195534Sscottl/* SATA register defines */
85195534Sscottl#define ATA_SSTATUS                     13
86195534Sscottl#define         ATA_SS_DET_MASK         0x0000000f
87195534Sscottl#define         ATA_SS_DET_NO_DEVICE    0x00000000
88195534Sscottl#define         ATA_SS_DET_DEV_PRESENT  0x00000001
89195534Sscottl#define         ATA_SS_DET_PHY_ONLINE   0x00000003
90195534Sscottl#define         ATA_SS_DET_PHY_OFFLINE  0x00000004
91195534Sscottl
92195534Sscottl#define         ATA_SS_SPD_MASK         0x000000f0
93195534Sscottl#define         ATA_SS_SPD_NO_SPEED     0x00000000
94195534Sscottl#define         ATA_SS_SPD_GEN1         0x00000010
95195534Sscottl#define         ATA_SS_SPD_GEN2         0x00000020
96280276Smav#define         ATA_SS_SPD_GEN3         0x00000030
97195534Sscottl
98195534Sscottl#define         ATA_SS_IPM_MASK         0x00000f00
99195534Sscottl#define         ATA_SS_IPM_NO_DEVICE    0x00000000
100195534Sscottl#define         ATA_SS_IPM_ACTIVE       0x00000100
101195534Sscottl#define         ATA_SS_IPM_PARTIAL      0x00000200
102195534Sscottl#define         ATA_SS_IPM_SLUMBER      0x00000600
103253647Smav#define         ATA_SS_IPM_DEVSLEEP     0x00000800
104195534Sscottl
105195534Sscottl#define ATA_SERROR                      14
106195534Sscottl#define         ATA_SE_DATA_CORRECTED   0x00000001
107195534Sscottl#define         ATA_SE_COMM_CORRECTED   0x00000002
108195534Sscottl#define         ATA_SE_DATA_ERR         0x00000100
109195534Sscottl#define         ATA_SE_COMM_ERR         0x00000200
110195534Sscottl#define         ATA_SE_PROT_ERR         0x00000400
111195534Sscottl#define         ATA_SE_HOST_ERR         0x00000800
112195534Sscottl#define         ATA_SE_PHY_CHANGED      0x00010000
113195534Sscottl#define         ATA_SE_PHY_IERROR       0x00020000
114195534Sscottl#define         ATA_SE_COMM_WAKE        0x00040000
115195534Sscottl#define         ATA_SE_DECODE_ERR       0x00080000
116195534Sscottl#define         ATA_SE_PARITY_ERR       0x00100000
117195534Sscottl#define         ATA_SE_CRC_ERR          0x00200000
118195534Sscottl#define         ATA_SE_HANDSHAKE_ERR    0x00400000
119195534Sscottl#define         ATA_SE_LINKSEQ_ERR      0x00800000
120195534Sscottl#define         ATA_SE_TRANSPORT_ERR    0x01000000
121195534Sscottl#define         ATA_SE_UNKNOWN_FIS      0x02000000
122220657Smav#define         ATA_SE_EXCHANGED        0x04000000
123195534Sscottl
124195534Sscottl#define ATA_SCONTROL                    15
125195534Sscottl#define         ATA_SC_DET_MASK         0x0000000f
126195534Sscottl#define         ATA_SC_DET_IDLE         0x00000000
127195534Sscottl#define         ATA_SC_DET_RESET        0x00000001
128195534Sscottl#define         ATA_SC_DET_DISABLE      0x00000004
129195534Sscottl
130195534Sscottl#define         ATA_SC_SPD_MASK         0x000000f0
131195534Sscottl#define         ATA_SC_SPD_NO_SPEED     0x00000000
132195534Sscottl#define         ATA_SC_SPD_SPEED_GEN1   0x00000010
133195534Sscottl#define         ATA_SC_SPD_SPEED_GEN2   0x00000020
134280276Smav#define         ATA_SC_SPD_SPEED_GEN3   0x00000030
135195534Sscottl
136195534Sscottl#define         ATA_SC_IPM_MASK         0x00000f00
137195534Sscottl#define         ATA_SC_IPM_NONE         0x00000000
138195534Sscottl#define         ATA_SC_IPM_DIS_PARTIAL  0x00000100
139195534Sscottl#define         ATA_SC_IPM_DIS_SLUMBER  0x00000200
140260353Smav#define         ATA_SC_IPM_DIS_DEVSLEEP 0x00000400
141195534Sscottl
142195534Sscottl#define ATA_SACTIVE                     16
143195534Sscottl
144195534Sscottl#define AHCI_MAX_PORTS			32
145195534Sscottl#define AHCI_MAX_SLOTS			32
146277061Ssmh#define AHCI_MAX_IRQS			16
147195534Sscottl
148195534Sscottl/* SATA AHCI v1.0 register defines */
149195534Sscottl#define AHCI_CAP                    0x00
150195534Sscottl#define		AHCI_CAP_NPMASK	0x0000001f
151195534Sscottl#define		AHCI_CAP_SXS	0x00000020
152195534Sscottl#define		AHCI_CAP_EMS	0x00000040
153195534Sscottl#define		AHCI_CAP_CCCS	0x00000080
154195534Sscottl#define		AHCI_CAP_NCS	0x00001F00
155195534Sscottl#define		AHCI_CAP_NCS_SHIFT	8
156195534Sscottl#define		AHCI_CAP_PSC	0x00002000
157195534Sscottl#define		AHCI_CAP_SSC	0x00004000
158195534Sscottl#define		AHCI_CAP_PMD	0x00008000
159195534Sscottl#define		AHCI_CAP_FBSS	0x00010000
160195534Sscottl#define		AHCI_CAP_SPM	0x00020000
161195534Sscottl#define		AHCI_CAP_SAM	0x00080000
162195534Sscottl#define		AHCI_CAP_ISS	0x00F00000
163195534Sscottl#define		AHCI_CAP_ISS_SHIFT	20
164195534Sscottl#define		AHCI_CAP_SCLO	0x01000000
165195534Sscottl#define		AHCI_CAP_SAL	0x02000000
166195534Sscottl#define		AHCI_CAP_SALP	0x04000000
167195534Sscottl#define		AHCI_CAP_SSS	0x08000000
168195534Sscottl#define		AHCI_CAP_SMPS	0x10000000
169195534Sscottl#define		AHCI_CAP_SSNTF	0x20000000
170195534Sscottl#define		AHCI_CAP_SNCQ	0x40000000
171195534Sscottl#define		AHCI_CAP_64BIT	0x80000000
172195534Sscottl
173195534Sscottl#define AHCI_GHC                    0x04
174195534Sscottl#define         AHCI_GHC_AE         0x80000000
175195534Sscottl#define         AHCI_GHC_MRSM       0x00000004
176195534Sscottl#define         AHCI_GHC_IE         0x00000002
177195534Sscottl#define         AHCI_GHC_HR         0x00000001
178195534Sscottl
179195534Sscottl#define AHCI_IS                     0x08
180195534Sscottl#define AHCI_PI                     0x0c
181195534Sscottl#define AHCI_VS                     0x10
182195534Sscottl
183196656Smav#define AHCI_CCCC                   0x14
184196656Smav#define		AHCI_CCCC_TV_MASK	0xffff0000
185196656Smav#define		AHCI_CCCC_TV_SHIFT	16
186196656Smav#define		AHCI_CCCC_CC_MASK	0x0000ff00
187196656Smav#define		AHCI_CCCC_CC_SHIFT	8
188196656Smav#define		AHCI_CCCC_INT_MASK	0x000000f8
189196656Smav#define		AHCI_CCCC_INT_SHIFT	3
190196656Smav#define		AHCI_CCCC_EN		0x00000001
191196656Smav#define AHCI_CCCP                   0x18
192196656Smav
193203108Smav#define AHCI_EM_LOC                 0x1C
194203108Smav#define AHCI_EM_CTL                 0x20
195203108Smav#define 	AHCI_EM_MR              0x00000001
196203108Smav#define 	AHCI_EM_TM              0x00000100
197203108Smav#define 	AHCI_EM_RST             0x00000200
198203108Smav#define 	AHCI_EM_LED             0x00010000
199203108Smav#define 	AHCI_EM_SAFTE           0x00020000
200203108Smav#define 	AHCI_EM_SES2            0x00040000
201203108Smav#define 	AHCI_EM_SGPIO           0x00080000
202203108Smav#define 	AHCI_EM_SMB             0x01000000
203203108Smav#define 	AHCI_EM_XMT             0x02000000
204203108Smav#define 	AHCI_EM_ALHD            0x04000000
205203108Smav#define 	AHCI_EM_PM              0x08000000
206203108Smav
207196656Smav#define AHCI_CAP2                   0x24
208196656Smav#define		AHCI_CAP2_BOH	0x00000001
209196656Smav#define		AHCI_CAP2_NVMP	0x00000002
210196656Smav#define		AHCI_CAP2_APST	0x00000004
211253647Smav#define		AHCI_CAP2_SDS	0x00000008
212253647Smav#define		AHCI_CAP2_SADM	0x00000010
213253647Smav#define		AHCI_CAP2_DESO	0x00000020
214196656Smav
215195534Sscottl#define AHCI_OFFSET                 0x100
216195534Sscottl#define AHCI_STEP                   0x80
217195534Sscottl
218195534Sscottl#define AHCI_P_CLB                  0x00
219195534Sscottl#define AHCI_P_CLBU                 0x04
220195534Sscottl#define AHCI_P_FB                   0x08
221195534Sscottl#define AHCI_P_FBU                  0x0c
222195534Sscottl#define AHCI_P_IS                   0x10
223195534Sscottl#define AHCI_P_IE                   0x14
224195534Sscottl#define         AHCI_P_IX_DHR       0x00000001
225195534Sscottl#define         AHCI_P_IX_PS        0x00000002
226195534Sscottl#define         AHCI_P_IX_DS        0x00000004
227195534Sscottl#define         AHCI_P_IX_SDB       0x00000008
228195534Sscottl#define         AHCI_P_IX_UF        0x00000010
229195534Sscottl#define         AHCI_P_IX_DP        0x00000020
230195534Sscottl#define         AHCI_P_IX_PC        0x00000040
231220657Smav#define         AHCI_P_IX_MP        0x00000080
232195534Sscottl
233195534Sscottl#define         AHCI_P_IX_PRC       0x00400000
234195534Sscottl#define         AHCI_P_IX_IPM       0x00800000
235195534Sscottl#define         AHCI_P_IX_OF        0x01000000
236195534Sscottl#define         AHCI_P_IX_INF       0x04000000
237195534Sscottl#define         AHCI_P_IX_IF        0x08000000
238195534Sscottl#define         AHCI_P_IX_HBD       0x10000000
239195534Sscottl#define         AHCI_P_IX_HBF       0x20000000
240195534Sscottl#define         AHCI_P_IX_TFE       0x40000000
241195534Sscottl#define         AHCI_P_IX_CPD       0x80000000
242195534Sscottl
243195534Sscottl#define AHCI_P_CMD                  0x18
244195534Sscottl#define         AHCI_P_CMD_ST       0x00000001
245195534Sscottl#define         AHCI_P_CMD_SUD      0x00000002
246195534Sscottl#define         AHCI_P_CMD_POD      0x00000004
247195534Sscottl#define         AHCI_P_CMD_CLO      0x00000008
248195534Sscottl#define         AHCI_P_CMD_FRE      0x00000010
249195534Sscottl#define         AHCI_P_CMD_CCS_MASK 0x00001f00
250195534Sscottl#define         AHCI_P_CMD_CCS_SHIFT 8
251195534Sscottl#define         AHCI_P_CMD_ISS      0x00002000
252195534Sscottl#define         AHCI_P_CMD_FR       0x00004000
253195534Sscottl#define         AHCI_P_CMD_CR       0x00008000
254195534Sscottl#define         AHCI_P_CMD_CPS      0x00010000
255195534Sscottl#define         AHCI_P_CMD_PMA      0x00020000
256195534Sscottl#define         AHCI_P_CMD_HPCP     0x00040000
257203123Smav#define         AHCI_P_CMD_MPSP     0x00080000
258195534Sscottl#define         AHCI_P_CMD_CPD      0x00100000
259203123Smav#define         AHCI_P_CMD_ESP      0x00200000
260203123Smav#define         AHCI_P_CMD_FBSCP    0x00400000
261203123Smav#define         AHCI_P_CMD_APSTE    0x00800000
262195534Sscottl#define         AHCI_P_CMD_ATAPI    0x01000000
263195534Sscottl#define         AHCI_P_CMD_DLAE     0x02000000
264195534Sscottl#define         AHCI_P_CMD_ALPE     0x04000000
265195534Sscottl#define         AHCI_P_CMD_ASP      0x08000000
266195534Sscottl#define         AHCI_P_CMD_ICC_MASK 0xf0000000
267195534Sscottl#define         AHCI_P_CMD_NOOP     0x00000000
268195534Sscottl#define         AHCI_P_CMD_ACTIVE   0x10000000
269195534Sscottl#define         AHCI_P_CMD_PARTIAL  0x20000000
270195534Sscottl#define         AHCI_P_CMD_SLUMBER  0x60000000
271253647Smav#define         AHCI_P_CMD_DEVSLEEP 0x80000000
272195534Sscottl
273195534Sscottl#define AHCI_P_TFD                  0x20
274195534Sscottl#define AHCI_P_SIG                  0x24
275195534Sscottl#define AHCI_P_SSTS                 0x28
276195534Sscottl#define AHCI_P_SCTL                 0x2c
277195534Sscottl#define AHCI_P_SERR                 0x30
278195534Sscottl#define AHCI_P_SACT                 0x34
279195534Sscottl#define AHCI_P_CI                   0x38
280195534Sscottl#define AHCI_P_SNTF                 0x3C
281195534Sscottl#define AHCI_P_FBS                  0x40
282203123Smav#define 	AHCI_P_FBS_EN       0x00000001
283203123Smav#define 	AHCI_P_FBS_DEC      0x00000002
284203123Smav#define 	AHCI_P_FBS_SDE      0x00000004
285203123Smav#define 	AHCI_P_FBS_DEV      0x00000f00
286203123Smav#define 	AHCI_P_FBS_DEV_SHIFT 8
287203123Smav#define 	AHCI_P_FBS_ADO      0x0000f000
288203123Smav#define 	AHCI_P_FBS_ADO_SHIFT 12
289203123Smav#define 	AHCI_P_FBS_DWE      0x000f0000
290203123Smav#define 	AHCI_P_FBS_DWE_SHIFT 16
291260353Smav#define AHCI_P_DEVSLP               0x44
292260353Smav#define 	AHCI_P_DEVSLP_ADSE  0x00000001
293260353Smav#define 	AHCI_P_DEVSLP_DSP   0x00000002
294260353Smav#define 	AHCI_P_DEVSLP_DETO  0x000003fc
295260353Smav#define 	AHCI_P_DEVSLP_DETO_SHIFT 2
296260353Smav#define 	AHCI_P_DEVSLP_MDAT  0x00007c00
297260353Smav#define 	AHCI_P_DEVSLP_MDAT_SHIFT 10
298260353Smav#define 	AHCI_P_DEVSLP_DITO  0x01ff8000
299260353Smav#define 	AHCI_P_DEVSLP_DITO_SHIFT 15
300260353Smav#define 	AHCI_P_DEVSLP_DM    0x0e000000
301260353Smav#define 	AHCI_P_DEVSLP_DM_SHIFT 25
302195534Sscottl
303195534Sscottl/* Just to be sure, if building as module. */
304195534Sscottl#if MAXPHYS < 512 * 1024
305195534Sscottl#undef MAXPHYS
306195534Sscottl#define MAXPHYS				512 * 1024
307195534Sscottl#endif
308195534Sscottl/* Pessimistic prognosis on number of required S/G entries */
309195534Sscottl#define AHCI_SG_ENTRIES	(roundup(btoc(MAXPHYS) + 1, 8))
310195534Sscottl/* Command list. 32 commands. First, 1Kbyte aligned. */
311195534Sscottl#define AHCI_CL_OFFSET              0
312195534Sscottl#define AHCI_CL_SIZE                32
313195534Sscottl/* Command tables. Up to 32 commands, Each, 128byte aligned. */
314195534Sscottl#define AHCI_CT_OFFSET              (AHCI_CL_OFFSET + AHCI_CL_SIZE * AHCI_MAX_SLOTS)
315195534Sscottl#define AHCI_CT_SIZE                (128 + AHCI_SG_ENTRIES * 16)
316195534Sscottl/* Total main work area. */
317195534Sscottl#define AHCI_WORK_SIZE              (AHCI_CT_OFFSET + AHCI_CT_SIZE * ch->numslots)
318195534Sscottl
319195534Sscottlstruct ahci_dma_prd {
320195534Sscottl    u_int64_t                   dba;
321195534Sscottl    u_int32_t                   reserved;
322195534Sscottl    u_int32_t                   dbc;            /* 0 based */
323195534Sscottl#define AHCI_PRD_MASK		0x003fffff      /* max 4MB */
324195534Sscottl#define AHCI_PRD_MAX		(AHCI_PRD_MASK + 1)
325261455Seadler#define AHCI_PRD_IPC		(1U << 31)
326195534Sscottl} __packed;
327195534Sscottl
328195534Sscottlstruct ahci_cmd_tab {
329195534Sscottl    u_int8_t                    cfis[64];
330195534Sscottl    u_int8_t                    acmd[32];
331195534Sscottl    u_int8_t                    reserved[32];
332195534Sscottl    struct ahci_dma_prd         prd_tab[AHCI_SG_ENTRIES];
333195534Sscottl} __packed;
334195534Sscottl
335195534Sscottlstruct ahci_cmd_list {
336195534Sscottl    u_int16_t                   cmd_flags;
337195534Sscottl#define AHCI_CMD_ATAPI		0x0020
338195534Sscottl#define AHCI_CMD_WRITE		0x0040
339195534Sscottl#define AHCI_CMD_PREFETCH		0x0080
340195534Sscottl#define AHCI_CMD_RESET		0x0100
341195534Sscottl#define AHCI_CMD_BIST		0x0200
342195534Sscottl#define AHCI_CMD_CLR_BUSY		0x0400
343195534Sscottl
344195534Sscottl    u_int16_t                   prd_length;     /* PRD entries */
345195534Sscottl    u_int32_t                   bytecount;
346195534Sscottl    u_int64_t                   cmd_table_phys; /* 128byte aligned */
347195534Sscottl} __packed;
348195534Sscottl
349195534Sscottl/* misc defines */
350195534Sscottl#define ATA_IRQ_RID                     0
351195534Sscottl#define ATA_INTR_FLAGS                  (INTR_MPSAFE|INTR_TYPE_BIO|INTR_ENTROPY)
352195534Sscottl
353195534Sscottlstruct ata_dmaslot {
354195534Sscottl    bus_dmamap_t                data_map;       /* data DMA map */
355195534Sscottl    int				nsegs;		/* Number of segs loaded */
356195534Sscottl};
357195534Sscottl
358195534Sscottl/* structure holding DMA related information */
359195534Sscottlstruct ata_dma {
360195534Sscottl    bus_dma_tag_t               work_tag;       /* workspace DMA tag */
361195534Sscottl    bus_dmamap_t                work_map;       /* workspace DMA map */
362195534Sscottl    uint8_t                     *work;          /* workspace */
363195534Sscottl    bus_addr_t                  work_bus;       /* bus address of work */
364195534Sscottl    bus_dma_tag_t               rfis_tag;       /* RFIS list DMA tag */
365195534Sscottl    bus_dmamap_t                rfis_map;       /* RFIS list DMA map */
366195534Sscottl    uint8_t                     *rfis;          /* FIS receive area */
367195534Sscottl    bus_addr_t                  rfis_bus;       /* bus address of rfis */
368195534Sscottl    bus_dma_tag_t               data_tag;       /* data DMA tag */
369195534Sscottl};
370195534Sscottl
371195534Sscottlenum ahci_slot_states {
372195534Sscottl	AHCI_SLOT_EMPTY,
373195534Sscottl	AHCI_SLOT_LOADING,
374195534Sscottl	AHCI_SLOT_RUNNING,
375198319Smav	AHCI_SLOT_EXECUTING
376195534Sscottl};
377195534Sscottl
378195534Sscottlstruct ahci_slot {
379279918Smav    struct ahci_channel		*ch;		/* Channel */
380195534Sscottl    u_int8_t			slot;           /* Number of this slot */
381195534Sscottl    enum ahci_slot_states	state;          /* Slot state */
382195534Sscottl    union ccb			*ccb;		/* CCB occupying slot */
383195534Sscottl    struct ata_dmaslot          dma;            /* DMA data of this slot */
384195534Sscottl    struct callout              timeout;        /* Execution timeout */
385195534Sscottl};
386195534Sscottl
387199747Smavstruct ahci_device {
388199821Smav	int			revision;
389199747Smav	int			mode;
390199747Smav	u_int			bytecount;
391203376Smav	u_int			atapi;
392199747Smav	u_int			tags;
393207499Smav	u_int			caps;
394199747Smav};
395199747Smav
396222039Smavstruct ahci_led {
397222039Smav	device_t		dev;		/* Device handle */
398222039Smav	struct cdev		*led;
399222039Smav	uint8_t			num;		/* Number of this led */
400222039Smav	uint8_t			state;		/* State of this led */
401222039Smav};
402222039Smav
403222039Smav#define	AHCI_NUM_LEDS		3
404222039Smav
405195534Sscottl/* structure describing an ATA channel */
406195534Sscottlstruct ahci_channel {
407195534Sscottl	device_t		dev;            /* Device handle */
408195534Sscottl	int			unit;           /* Physical channel */
409195534Sscottl	struct resource		*r_mem;		/* Memory of this channel */
410195534Sscottl	struct resource		*r_irq;         /* Interrupt of this channel */
411195534Sscottl	void			*ih;            /* Interrupt handle */
412195534Sscottl	struct ata_dma		dma;            /* DMA data */
413195534Sscottl	struct cam_sim		*sim;
414195534Sscottl	struct cam_path		*path;
415195534Sscottl	uint32_t		caps;		/* Controller capabilities */
416196656Smav	uint32_t		caps2;		/* Controller capabilities */
417203123Smav	uint32_t		chcaps;		/* Channel capabilities */
418260353Smav	uint32_t		chscaps;	/* Channel sleep capabilities */
419279918Smav	uint16_t		vendorid;	/* Vendor ID from the bus */
420279918Smav	uint16_t		deviceid;	/* Device ID from the bus */
421279918Smav	uint16_t		subvendorid;	/* Subvendor ID from the bus */
422279918Smav	uint16_t		subdeviceid;	/* Subdevice ID from the bus */
423199322Smav	int			quirks;
424195534Sscottl	int			numslots;	/* Number of present slots */
425195534Sscottl	int			pm_level;	/* power management level */
426195534Sscottl	int			devices;        /* What is present */
427195534Sscottl	int			pm_present;	/* PM presence reported */
428203123Smav	int			fbs_enabled;	/* FIS-based switching enabled */
429279918Smav
430279918Smav	union ccb		*hold[AHCI_MAX_SLOTS];
431279918Smav	struct ahci_slot	slot[AHCI_MAX_SLOTS];
432199747Smav	uint32_t		oslots;		/* Occupied slots */
433195534Sscottl	uint32_t		rslots;		/* Running slots */
434195534Sscottl	uint32_t		aslots;		/* Slots with atomic commands  */
435203123Smav	uint32_t		eslots;		/* Slots in error */
436203873Smav	uint32_t		toslots;	/* Slots in timeout */
437279918Smav	int			lastslot;	/* Last used slot */
438279918Smav	int			taggedtarget;	/* Last tagged target */
439195534Sscottl	int			numrslots;	/* Number of running slots */
440203123Smav	int			numrslotspd[16];/* Number of running slots per dev */
441195534Sscottl	int			numtslots;	/* Number of tagged slots */
442203123Smav	int			numtslotspd[16];/* Number of tagged slots per dev */
443220830Smav	int			numhslots;	/* Number of held slots */
444220565Smav	int			recoverycmd;	/* Our READ LOG active */
445198851Smav	int			fatalerr;	/* Fatal error happend */
446220576Smav	int			resetting;	/* Hard-reset in progress. */
447220789Smav	int			resetpolldiv;	/* Hard-reset poll divider. */
448220657Smav	int			listening;	/* SUD bit is cleared. */
449224498Smav	int			wrongccs;	/* CCS field in CMD was wrong */
450195534Sscottl	union ccb		*frozen;	/* Frozen command */
451196656Smav	struct callout		pm_timer;	/* Power management events */
452220576Smav	struct callout		reset_timer;	/* Hard-reset timeout */
453199747Smav
454199747Smav	struct ahci_device	user[16];	/* User-specified settings */
455199747Smav	struct ahci_device	curr[16];	/* Current settings */
456279918Smav
457279918Smav	struct mtx_padalign	mtx;		/* state lock */
458279918Smav	STAILQ_HEAD(, ccb_hdr)	doneq;		/* queue of completed CCBs */
459279918Smav	int			batch;		/* doneq is in use */
460195534Sscottl};
461195534Sscottl
462238805Smavstruct ahci_enclosure {
463238805Smav	device_t		dev;            /* Device handle */
464238805Smav	struct resource		*r_memc;	/* Control register */
465238805Smav	struct resource		*r_memt;	/* Transmit buffer */
466238805Smav	struct resource		*r_memr;	/* Recieve buffer */
467238805Smav	struct cam_sim		*sim;
468238805Smav	struct cam_path		*path;
469238805Smav	struct mtx		mtx;		/* state lock */
470238805Smav	struct ahci_led		leds[AHCI_MAX_PORTS * 3];
471238805Smav	uint32_t		capsem;		/* Controller capabilities */
472238805Smav	uint8_t			status[AHCI_MAX_PORTS][4]; /* ArrayDev statuses */
473238805Smav	int			quirks;
474238805Smav	int			channels;
475302791Smav	uint32_t		ichannels;
476238805Smav};
477238805Smav
478195534Sscottl/* structure describing a AHCI controller */
479195534Sscottlstruct ahci_controller {
480195534Sscottl	device_t		dev;
481249346Smav	bus_dma_tag_t		dma_tag;
482195534Sscottl	int			r_rid;
483279918Smav	uint16_t		vendorid;	/* Vendor ID from the bus */
484279918Smav	uint16_t		deviceid;	/* Device ID from the bus */
485279918Smav	uint16_t		subvendorid;	/* Subvendor ID from the bus */
486279918Smav	uint16_t		subdeviceid;	/* Subdevice ID from the bus */
487195534Sscottl	struct resource		*r_mem;
488195534Sscottl	struct rman		sc_iomem;
489195534Sscottl	struct ahci_controller_irq {
490195534Sscottl		struct ahci_controller	*ctlr;
491195534Sscottl		struct resource		*r_irq;
492195534Sscottl		void			*handle;
493195534Sscottl		int			r_irq_rid;
494195534Sscottl		int			mode;
495195534Sscottl#define	AHCI_IRQ_MODE_ALL	0
496195534Sscottl#define	AHCI_IRQ_MODE_AFTER	1
497195534Sscottl#define	AHCI_IRQ_MODE_ONE	2
498277061Ssmh	} irqs[AHCI_MAX_IRQS];
499196656Smav	uint32_t		caps;		/* Controller capabilities */
500196656Smav	uint32_t		caps2;		/* Controller capabilities */
501203108Smav	uint32_t		capsem;		/* Controller capabilities */
502222039Smav	uint32_t		emloc;		/* EM buffer location */
503199322Smav	int			quirks;
504195534Sscottl	int			numirqs;
505195534Sscottl	int			channels;
506302791Smav	uint32_t		ichannels;
507196656Smav	int			ccc;		/* CCC timeout */
508196656Smav	int			cccv;		/* CCC vector */
509260387Sscottl	int			direct;		/* Direct command completion */
510260387Sscottl	int			msi;		/* MSI interupts */
511195534Sscottl	struct {
512195534Sscottl		void			(*function)(void *);
513195534Sscottl		void			*argument;
514195534Sscottl	} interrupt[AHCI_MAX_PORTS];
515195534Sscottl};
516195534Sscottl
517195534Sscottlenum ahci_err_type {
518195534Sscottl	AHCI_ERR_NONE,		/* No error */
519195534Sscottl	AHCI_ERR_INVALID,	/* Error detected by us before submitting. */
520195534Sscottl	AHCI_ERR_INNOCENT,	/* Innocent victim. */
521195534Sscottl	AHCI_ERR_TFE,		/* Task File Error. */
522195534Sscottl	AHCI_ERR_SATA,		/* SATA error. */
523195534Sscottl	AHCI_ERR_TIMEOUT,	/* Command execution timeout. */
524195534Sscottl	AHCI_ERR_NCQ,		/* NCQ command error. CCB should be put on hold
525195534Sscottl				 * until READ LOG executed to reveal error. */
526195534Sscottl};
527195534Sscottl
528195534Sscottl/* macros to hide busspace uglyness */
529195534Sscottl#define ATA_INB(res, offset) \
530195534Sscottl	bus_read_1((res), (offset))
531195534Sscottl#define ATA_INW(res, offset) \
532195534Sscottl	bus_read_2((res), (offset))
533195534Sscottl#define ATA_INL(res, offset) \
534195534Sscottl	bus_read_4((res), (offset))
535195534Sscottl#define ATA_INSW(res, offset, addr, count) \
536195534Sscottl	bus_read_multi_2((res), (offset), (addr), (count))
537195534Sscottl#define ATA_INSW_STRM(res, offset, addr, count) \
538195534Sscottl	bus_read_multi_stream_2((res), (offset), (addr), (count))
539195534Sscottl#define ATA_INSL(res, offset, addr, count) \
540195534Sscottl	bus_read_multi_4((res), (offset), (addr), (count))
541195534Sscottl#define ATA_INSL_STRM(res, offset, addr, count) \
542195534Sscottl	bus_read_multi_stream_4((res), (offset), (addr), (count))
543195534Sscottl#define ATA_OUTB(res, offset, value) \
544195534Sscottl	bus_write_1((res), (offset), (value))
545195534Sscottl#define ATA_OUTW(res, offset, value) \
546195534Sscottl	bus_write_2((res), (offset), (value))
547195534Sscottl#define ATA_OUTL(res, offset, value) \
548195534Sscottl	bus_write_4((res), (offset), (value))
549195534Sscottl#define ATA_OUTSW(res, offset, addr, count) \
550195534Sscottl	bus_write_multi_2((res), (offset), (addr), (count))
551195534Sscottl#define ATA_OUTSW_STRM(res, offset, addr, count) \
552195534Sscottl	bus_write_multi_stream_2((res), (offset), (addr), (count))
553195534Sscottl#define ATA_OUTSL(res, offset, addr, count) \
554195534Sscottl	bus_write_multi_4((res), (offset), (addr), (count))
555195534Sscottl#define ATA_OUTSL_STRM(res, offset, addr, count) \
556195534Sscottl	bus_write_multi_stream_4((res), (offset), (addr), (count))
557279918Smav
558279918Smav
559279918Smav#define AHCI_Q_NOFORCE		0x00000001
560279918Smav#define AHCI_Q_NOPMP		0x00000002
561279918Smav#define AHCI_Q_NONCQ		0x00000004
562279918Smav#define AHCI_Q_1CH		0x00000008
563279918Smav#define AHCI_Q_2CH		0x00000010
564279918Smav#define AHCI_Q_4CH		0x00000020
565279918Smav#define AHCI_Q_EDGEIS		0x00000040
566279918Smav#define AHCI_Q_SATA2		0x00000080
567279918Smav#define AHCI_Q_NOBSYRES		0x00000100
568279918Smav#define AHCI_Q_NOAA		0x00000200
569279918Smav#define AHCI_Q_NOCOUNT		0x00000400
570279918Smav#define AHCI_Q_ALTSIG		0x00000800
571279918Smav#define AHCI_Q_NOMSI		0x00001000
572279918Smav#define AHCI_Q_ATI_PMP_BUG	0x00002000
573279918Smav#define AHCI_Q_MAXIO_64K	0x00004000
574279918Smav#define AHCI_Q_SATA1_UNIT0	0x00008000	/* need better method for this */
575279918Smav#define AHCI_Q_ABAR0		0x00010000
576279918Smav#define AHCI_Q_1MSI		0x00020000
577313446Smav#define AHCI_Q_NOCCS		0x00400000
578279918Smav
579279918Smav#define AHCI_Q_BIT_STRING	\
580284006Smav	"\020"			\
581279918Smav	"\001NOFORCE"		\
582279918Smav	"\002NOPMP"		\
583279918Smav	"\003NONCQ"		\
584279918Smav	"\0041CH"		\
585279918Smav	"\0052CH"		\
586279918Smav	"\0064CH"		\
587279918Smav	"\007EDGEIS"		\
588279918Smav	"\010SATA2"		\
589279918Smav	"\011NOBSYRES"		\
590279918Smav	"\012NOAA"		\
591279918Smav	"\013NOCOUNT"		\
592279918Smav	"\014ALTSIG"		\
593279918Smav	"\015NOMSI"		\
594279918Smav	"\016ATI_PMP_BUG"	\
595279918Smav	"\017MAXIO_64K"		\
596279918Smav	"\020SATA1_UNIT0"	\
597279918Smav	"\021ABAR0"		\
598313446Smav	"\0221MSI"              \
599313446Smav	"\027NOCCS"
600279918Smav
601279918Smavint ahci_attach(device_t dev);
602279918Smavint ahci_detach(device_t dev);
603279918Smavint ahci_setup_interrupt(device_t dev);
604279918Smavint ahci_print_child(device_t dev, device_t child);
605279918Smavstruct resource *ahci_alloc_resource(device_t dev, device_t child, int type, int *rid,
606279918Smav    u_long start, u_long end, u_long count, u_int flags);
607279918Smavint ahci_release_resource(device_t dev, device_t child, int type, int rid,
608279918Smav    struct resource *r);
609279918Smavint ahci_setup_intr(device_t dev, device_t child, struct resource *irq,
610279918Smav    int flags, driver_filter_t *filter, driver_intr_t *function,
611279918Smav    void *argument, void **cookiep);
612279918Smavint ahci_teardown_intr(device_t dev, device_t child, struct resource *irq,
613279918Smav    void *cookie);
614279918Smavint ahci_child_location_str(device_t dev, device_t child, char *buf,
615279918Smav    size_t buflen);
616279918Smavbus_dma_tag_t ahci_get_dma_tag(device_t dev, device_t child);
617279918Smavint ahci_ctlr_reset(device_t dev);
618279918Smavint ahci_ctlr_setup(device_t dev);
619