xref: /dflybsd-src/sys/dev/disk/ahci/ahci.h (revision d90e4fd11dfe04d50a912628b090cd5d02cdea2d)
1258223a3SMatthew Dillon /*
2258223a3SMatthew Dillon  * Copyright (c) 2006 David Gwynne <dlg@openbsd.org>
3258223a3SMatthew Dillon  *
4258223a3SMatthew Dillon  * Permission to use, copy, modify, and distribute this software for any
5258223a3SMatthew Dillon  * purpose with or without fee is hereby granted, provided that the above
6258223a3SMatthew Dillon  * copyright notice and this permission notice appear in all copies.
7258223a3SMatthew Dillon  *
8258223a3SMatthew Dillon  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9258223a3SMatthew Dillon  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10258223a3SMatthew Dillon  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11258223a3SMatthew Dillon  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12258223a3SMatthew Dillon  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13258223a3SMatthew Dillon  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14258223a3SMatthew Dillon  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15258223a3SMatthew Dillon  *
16258223a3SMatthew Dillon  * $OpenBSD: ahci.c,v 1.147 2009/02/16 21:19:07 miod Exp $
17258223a3SMatthew Dillon  */
18258223a3SMatthew Dillon 
19258223a3SMatthew Dillon #if defined(__DragonFly__)
20258223a3SMatthew Dillon #include "ahci_dragonfly.h"
21258223a3SMatthew Dillon #else
22258223a3SMatthew Dillon #error "build for OS unknown"
23258223a3SMatthew Dillon #endif
242cc2e845SMatthew Dillon #include "pmreg.h"
25258223a3SMatthew Dillon #include "atascsi.h"
26258223a3SMatthew Dillon 
27258223a3SMatthew Dillon /* change to AHCI_DEBUG for dmesg spam */
28258223a3SMatthew Dillon #define NO_AHCI_DEBUG
29258223a3SMatthew Dillon 
30258223a3SMatthew Dillon #ifdef AHCI_DEBUG
31258223a3SMatthew Dillon #define DPRINTF(m, f...) do { if ((ahcidebug & (m)) == (m)) kprintf(f); } \
32258223a3SMatthew Dillon     while (0)
33258223a3SMatthew Dillon #define AHCI_D_TIMEOUT		0x00
34258223a3SMatthew Dillon #define AHCI_D_VERBOSE		0x01
35258223a3SMatthew Dillon #define AHCI_D_INTR		0x02
36258223a3SMatthew Dillon #define AHCI_D_XFER		0x08
37258223a3SMatthew Dillon int ahcidebug = AHCI_D_VERBOSE;
38258223a3SMatthew Dillon #else
39258223a3SMatthew Dillon #define DPRINTF(m, f...)
40258223a3SMatthew Dillon #endif
41258223a3SMatthew Dillon 
42258223a3SMatthew Dillon #define AHCI_PCI_ATI_SB600_MAGIC	0x40
43258223a3SMatthew Dillon #define AHCI_PCI_ATI_SB600_LOCKED	0x01
44258223a3SMatthew Dillon 
45258223a3SMatthew Dillon #define AHCI_REG_CAP		0x000 /* HBA Capabilities */
46258223a3SMatthew Dillon #define  AHCI_REG_CAP_NP(_r)		(((_r) & 0x1f)+1) /* Number of Ports */
47258223a3SMatthew Dillon #define  AHCI_REG_CAP_SXS		(1<<5) /* External SATA */
48258223a3SMatthew Dillon #define  AHCI_REG_CAP_EMS		(1<<6) /* Enclosure Mgmt */
49258223a3SMatthew Dillon #define  AHCI_REG_CAP_CCCS		(1<<7) /* Cmd Coalescing */
50258223a3SMatthew Dillon #define  AHCI_REG_CAP_NCS(_r)		((((_r) & 0x1f00)>>8)+1) /* NCmds*/
51258223a3SMatthew Dillon #define  AHCI_REG_CAP_PSC		(1<<13) /* Partial State Capable */
52258223a3SMatthew Dillon #define  AHCI_REG_CAP_SSC		(1<<14) /* Slumber State Capable */
53258223a3SMatthew Dillon #define  AHCI_REG_CAP_PMD		(1<<15) /* PIO Multiple DRQ Block */
544b450139SMatthew Dillon #define  AHCI_REG_CAP_FBSS		(1<<16) /* FIS-Based Switching Supp */
55258223a3SMatthew Dillon #define  AHCI_REG_CAP_SPM		(1<<17) /* Port Multiplier */
56258223a3SMatthew Dillon #define  AHCI_REG_CAP_SAM		(1<<18) /* AHCI Only mode */
57258223a3SMatthew Dillon #define  AHCI_REG_CAP_SNZO		(1<<19) /* Non Zero DMA Offsets */
58258223a3SMatthew Dillon #define  AHCI_REG_CAP_ISS		(0xf<<20) /* Interface Speed Support */
59258223a3SMatthew Dillon #define  AHCI_REG_CAP_ISS_G1		(0x1<<20) /* Gen 1 (1.5 Gbps) */
608986d351SMatthew Dillon #define  AHCI_REG_CAP_ISS_G2		(0x2<<20) /* Gen 2 (3 Gbps) */
618986d351SMatthew Dillon #define  AHCI_REG_CAP_ISS_G3		(0x3<<20) /* Gen 3 (6 Gbps) */
62258223a3SMatthew Dillon #define  AHCI_REG_CAP_SCLO		(1<<24) /* Cmd List Override */
63258223a3SMatthew Dillon #define  AHCI_REG_CAP_SAL		(1<<25) /* Activity LED */
64258223a3SMatthew Dillon #define  AHCI_REG_CAP_SALP		(1<<26) /* Aggressive Link Pwr Mgmt */
65258223a3SMatthew Dillon #define  AHCI_REG_CAP_SSS		(1<<27) /* Staggered Spinup */
66258223a3SMatthew Dillon #define  AHCI_REG_CAP_SMPS		(1<<28) /* Mech Presence Switch */
67258223a3SMatthew Dillon #define  AHCI_REG_CAP_SSNTF		(1<<29) /* SNotification Register */
68258223a3SMatthew Dillon #define  AHCI_REG_CAP_SNCQ		(1<<30) /* Native Cmd Queuing */
69258223a3SMatthew Dillon #define  AHCI_REG_CAP_S64A		(1<<31) /* 64bit Addressing */
70258223a3SMatthew Dillon #define  AHCI_FMT_CAP		"\020" "\040S64A" "\037NCQ" "\036SSNTF" \
71258223a3SMatthew Dillon 				    "\035SMPS" "\034SSS" "\033SALP" "\032SAL" \
72258223a3SMatthew Dillon 				    "\031SCLO" "\024SNZO" "\023SAM" "\022SPM" \
73258223a3SMatthew Dillon 				    "\021FBSS" "\020PMD" "\017SSC" "\016PSC" \
74258223a3SMatthew Dillon 				    "\010CCCS" "\007EMS" "\006SXS"
75258223a3SMatthew Dillon 
76258223a3SMatthew Dillon #define AHCI_REG_GHC		0x004 /* Global HBA Control */
77258223a3SMatthew Dillon #define  AHCI_REG_GHC_HR		(1<<0) /* HBA Reset */
78258223a3SMatthew Dillon #define  AHCI_REG_GHC_IE		(1<<1) /* Interrupt Enable */
79258223a3SMatthew Dillon #define  AHCI_REG_GHC_MRSM		(1<<2) /* MSI Revert to Single Msg */
80258223a3SMatthew Dillon #define  AHCI_REG_GHC_AE		(1<<31) /* AHCI Enable */
81258223a3SMatthew Dillon #define AHCI_FMT_GHC		"\020" "\040AE" "\003MRSM" "\002IE" "\001HR"
82258223a3SMatthew Dillon 
83258223a3SMatthew Dillon #define AHCI_REG_IS		0x008 /* Interrupt Status */
84258223a3SMatthew Dillon #define AHCI_REG_PI		0x00c /* Ports Implemented */
85258223a3SMatthew Dillon 
86258223a3SMatthew Dillon #define AHCI_REG_VS		0x010 /* AHCI Version */
87258223a3SMatthew Dillon #define  AHCI_REG_VS_0_95		0x00000905 /* 0.95 */
88258223a3SMatthew Dillon #define  AHCI_REG_VS_1_0		0x00010000 /* 1.0 */
89258223a3SMatthew Dillon #define  AHCI_REG_VS_1_1		0x00010100 /* 1.1 */
90258223a3SMatthew Dillon #define  AHCI_REG_VS_1_2		0x00010200 /* 1.2 */
91c520c99bSMatthew Dillon #define  AHCI_REG_VS_1_3		0x00010300 /* 1.3 */
92c520c99bSMatthew Dillon #define  AHCI_REG_VS_1_4		0x00010400 /* 1.4 */
93c520c99bSMatthew Dillon #define  AHCI_REG_VS_1_5		0x00010500 /* 1.5 (future...) */
94258223a3SMatthew Dillon 
95258223a3SMatthew Dillon #define AHCI_REG_CCC_CTL	0x014 /* Coalescing Control */
96258223a3SMatthew Dillon #define  AHCI_REG_CCC_CTL_INT(_r)	(((_r) & 0xf8) >> 3) /* CCC INT slot */
97258223a3SMatthew Dillon 
98258223a3SMatthew Dillon #define AHCI_REG_CCC_PORTS	0x018 /* Coalescing Ports */
99258223a3SMatthew Dillon #define AHCI_REG_EM_LOC		0x01c /* Enclosure Mgmt Location */
100258223a3SMatthew Dillon #define AHCI_REG_EM_CTL		0x020 /* Enclosure Mgmt Control */
101258223a3SMatthew Dillon 
1024b450139SMatthew Dillon #define AHCI_REG_CAP2		0x024 /* Host Capabilities Extended */
1034b450139SMatthew Dillon #define  AHCI_REG_CAP2_BOH		(1<<0) /* BIOS/OS Handoff */
1044b450139SMatthew Dillon #define  AHCI_REG_CAP2_NVMP		(1<<1) /* NVMHCI Present */
1054b450139SMatthew Dillon #define  AHCI_REG_CAP2_APST		(1<<2) /* A-Partial to Slumber Trans */
106*d90e4fd1SImre Vadász #define  AHCI_REG_CAP2_SDS		(1<<3) /* Supports DevSleep */
107*d90e4fd1SImre Vadász #define  AHCI_REG_CAP2_SADM		(1<<4) /* Supports A-DevSleep Mgmt */
108*d90e4fd1SImre Vadász #define  AHCI_REG_CAP2_DESO		(1<<5) /* DevSleep only from Slumber */
109*d90e4fd1SImre Vadász #define  AHCI_FMT_CAP2		"\020" "\006DESO" "\005SADM" "\004SDS"	\
110*d90e4fd1SImre Vadász 				"\003APST" "\002NVMP" "\001BOH"
1114b450139SMatthew Dillon 
1124b450139SMatthew Dillon #define AHCI_REG_BOHC		0x028 /* BIOS/OS Handoff Control and Status */
1134b450139SMatthew Dillon #define  AHCI_REG_BOHC_BOS		(1<<0) /* BIOS Owned Semaphore */
1144b450139SMatthew Dillon #define  AHCI_REG_BOHC_OOS		(1<<1) /* OS Owned Semaphore */
1154b450139SMatthew Dillon #define  AHCI_REG_BOHC_SOOE		(1<<2) /* SMI on OS Own chg enable */
1164b450139SMatthew Dillon #define  AHCI_REG_BOHC_OOC		(1<<3) /* OS Ownership Change */
1174b450139SMatthew Dillon #define  AHCI_REG_BOHC_BB		(1<<4) /* BIOS Busy */
1184b450139SMatthew Dillon #define  AHCI_FMT_BOHC		"\020" "\005BB" "\004OOC" "\003SOOE" \
1194b450139SMatthew Dillon 				"\002OOS" "\001BOS"
1204b450139SMatthew Dillon 
121258223a3SMatthew Dillon #define AHCI_PORT_REGION(_p)	(0x100 + ((_p) * 0x80))
122258223a3SMatthew Dillon #define AHCI_PORT_SIZE		0x80
123258223a3SMatthew Dillon 
124258223a3SMatthew Dillon #define AHCI_PREG_CLB		0x00 /* Cmd List Base Addr */
125258223a3SMatthew Dillon #define AHCI_PREG_CLBU		0x04 /* Cmd List Base Hi Addr */
126258223a3SMatthew Dillon #define AHCI_PREG_FB		0x08 /* FIS Base Addr */
127258223a3SMatthew Dillon #define AHCI_PREG_FBU		0x0c /* FIS Base Hi Addr */
128258223a3SMatthew Dillon 
129258223a3SMatthew Dillon #define AHCI_PREG_IS		0x10 /* Interrupt Status */
130258223a3SMatthew Dillon #define  AHCI_PREG_IS_DHRS		(1<<0) /* Device to Host FIS */
131258223a3SMatthew Dillon #define  AHCI_PREG_IS_PSS		(1<<1) /* PIO Setup FIS */
132258223a3SMatthew Dillon #define  AHCI_PREG_IS_DSS		(1<<2) /* DMA Setup FIS */
133258223a3SMatthew Dillon #define  AHCI_PREG_IS_SDBS		(1<<3) /* Set Device Bits FIS */
134258223a3SMatthew Dillon #define  AHCI_PREG_IS_UFS		(1<<4) /* Unknown FIS */
135258223a3SMatthew Dillon #define  AHCI_PREG_IS_DPS		(1<<5) /* Descriptor Processed */
136258223a3SMatthew Dillon #define  AHCI_PREG_IS_PCS		(1<<6) /* Port Change */
137258223a3SMatthew Dillon #define  AHCI_PREG_IS_DMPS		(1<<7) /* Device Mechanical Presence */
138258223a3SMatthew Dillon #define  AHCI_PREG_IS_PRCS		(1<<22) /* PhyRdy Change */
139258223a3SMatthew Dillon #define  AHCI_PREG_IS_IPMS		(1<<23) /* Incorrect Port Multiplier */
140258223a3SMatthew Dillon #define  AHCI_PREG_IS_OFS		(1<<24) /* Overflow */
141258223a3SMatthew Dillon #define  AHCI_PREG_IS_INFS		(1<<26) /* Interface Non-fatal Error */
142258223a3SMatthew Dillon #define  AHCI_PREG_IS_IFS		(1<<27) /* Interface Fatal Error */
143258223a3SMatthew Dillon #define  AHCI_PREG_IS_HBDS		(1<<28) /* Host Bus Data Error */
144258223a3SMatthew Dillon #define  AHCI_PREG_IS_HBFS		(1<<29) /* Host Bus Fatal Error */
145258223a3SMatthew Dillon #define  AHCI_PREG_IS_TFES		(1<<30) /* Task File Error */
146258223a3SMatthew Dillon #define  AHCI_PREG_IS_CPDS		(1<<31) /* Cold Presence Detect */
147258223a3SMatthew Dillon #define AHCI_PFMT_IS		"\20" "\040CPDS" "\037TFES" "\036HBFS" \
148258223a3SMatthew Dillon 				    "\035HBDS" "\034IFS" "\033INFS" "\031OFS" \
149258223a3SMatthew Dillon 				    "\030IPMS" "\027PRCS" "\010DMPS" "\006DPS" \
150258223a3SMatthew Dillon 				    "\007PCS" "\005UFS" "\004SDBS" "\003DSS" \
151258223a3SMatthew Dillon 				    "\002PSS" "\001DHRS"
152258223a3SMatthew Dillon 
153258223a3SMatthew Dillon #define AHCI_PREG_IE		0x14 /* Interrupt Enable */
154258223a3SMatthew Dillon #define  AHCI_PREG_IE_DHRE		(1<<0) /* Device to Host FIS */
155258223a3SMatthew Dillon #define  AHCI_PREG_IE_PSE		(1<<1) /* PIO Setup FIS */
156258223a3SMatthew Dillon #define  AHCI_PREG_IE_DSE		(1<<2) /* DMA Setup FIS */
157258223a3SMatthew Dillon #define  AHCI_PREG_IE_SDBE		(1<<3) /* Set Device Bits FIS */
158258223a3SMatthew Dillon #define  AHCI_PREG_IE_UFE		(1<<4) /* Unknown FIS */
159258223a3SMatthew Dillon #define  AHCI_PREG_IE_DPE		(1<<5) /* Descriptor Processed */
160258223a3SMatthew Dillon #define  AHCI_PREG_IE_PCE		(1<<6) /* Port Change */
161258223a3SMatthew Dillon #define  AHCI_PREG_IE_DMPE		(1<<7) /* Device Mechanical Presence */
162258223a3SMatthew Dillon #define  AHCI_PREG_IE_PRCE		(1<<22) /* PhyRdy Change */
163258223a3SMatthew Dillon #define  AHCI_PREG_IE_IPME		(1<<23) /* Incorrect Port Multiplier */
164258223a3SMatthew Dillon #define  AHCI_PREG_IE_OFE		(1<<24) /* Overflow */
165258223a3SMatthew Dillon #define  AHCI_PREG_IE_INFE		(1<<26) /* Interface Non-fatal Error */
166258223a3SMatthew Dillon #define  AHCI_PREG_IE_IFE		(1<<27) /* Interface Fatal Error */
167258223a3SMatthew Dillon #define  AHCI_PREG_IE_HBDE		(1<<28) /* Host Bus Data Error */
168258223a3SMatthew Dillon #define  AHCI_PREG_IE_HBFE		(1<<29) /* Host Bus Fatal Error */
169258223a3SMatthew Dillon #define  AHCI_PREG_IE_TFEE		(1<<30) /* Task File Error */
170258223a3SMatthew Dillon #define  AHCI_PREG_IE_CPDE		(1<<31) /* Cold Presence Detect */
171258223a3SMatthew Dillon #define AHCI_PFMT_IE		"\20" "\040CPDE" "\037TFEE" "\036HBFE" \
172258223a3SMatthew Dillon 				    "\035HBDE" "\034IFE" "\033INFE" "\031OFE" \
173258223a3SMatthew Dillon 				    "\030IPME" "\027PRCE" "\010DMPE" "\007PCE" \
174258223a3SMatthew Dillon 				    "\006DPE" "\005UFE" "\004SDBE" "\003DSE" \
175258223a3SMatthew Dillon 				    "\002PSE" "\001DHRE"
176258223a3SMatthew Dillon 
1774b450139SMatthew Dillon /*
1784b450139SMatthew Dillon  * NOTE: bits 22, 21, 20, 19, 18, 16, 15, 14, 13, 12:08, 07:05 are always
1794b450139SMatthew Dillon  *       read-only.  Other bits may be read-only when the related feature
1804b450139SMatthew Dillon  *	 is not supported by the HBA.
1814b450139SMatthew Dillon  */
182258223a3SMatthew Dillon #define AHCI_PREG_CMD		0x18 /* Command and Status */
183258223a3SMatthew Dillon #define  AHCI_PREG_CMD_ST		(1<<0) /* Start */
184258223a3SMatthew Dillon #define  AHCI_PREG_CMD_SUD		(1<<1) /* Spin Up Device */
185258223a3SMatthew Dillon #define  AHCI_PREG_CMD_POD		(1<<2) /* Power On Device */
186258223a3SMatthew Dillon #define  AHCI_PREG_CMD_CLO		(1<<3) /* Command List Override */
187258223a3SMatthew Dillon #define  AHCI_PREG_CMD_FRE		(1<<4) /* FIS Receive Enable */
188258223a3SMatthew Dillon #define  AHCI_PREG_CMD_CCS(_r)		(((_r) >> 8) & 0x1f) /* Curr CmdSlot# */
189258223a3SMatthew Dillon #define  AHCI_PREG_CMD_MPSS		(1<<13) /* Mech Presence State */
190258223a3SMatthew Dillon #define  AHCI_PREG_CMD_FR		(1<<14) /* FIS Receive Running */
191258223a3SMatthew Dillon #define  AHCI_PREG_CMD_CR		(1<<15) /* Command List Running */
192258223a3SMatthew Dillon #define  AHCI_PREG_CMD_CPS		(1<<16) /* Cold Presence State */
193258223a3SMatthew Dillon #define  AHCI_PREG_CMD_PMA		(1<<17) /* Port Multiplier Attached */
194258223a3SMatthew Dillon #define  AHCI_PREG_CMD_HPCP		(1<<18) /* Hot Plug Capable */
195258223a3SMatthew Dillon #define  AHCI_PREG_CMD_MPSP		(1<<19) /* Mech Presence Switch */
196258223a3SMatthew Dillon #define  AHCI_PREG_CMD_CPD		(1<<20) /* Cold Presence Detection */
197258223a3SMatthew Dillon #define  AHCI_PREG_CMD_ESP		(1<<21) /* External SATA Port */
1984b450139SMatthew Dillon #define  AHCI_PREG_CMD_FBSCP		(1<<22) /* FIS-based sw capable port */
1994b450139SMatthew Dillon #define  AHCI_PREG_CMD_APSTE		(1<<23) /* Auto Partial to Slumber */
200258223a3SMatthew Dillon #define  AHCI_PREG_CMD_ATAPI		(1<<24) /* Device is ATAPI */
201258223a3SMatthew Dillon #define  AHCI_PREG_CMD_DLAE		(1<<25) /* Drv LED on ATAPI Enable */
202258223a3SMatthew Dillon #define  AHCI_PREG_CMD_ALPE		(1<<26) /* Aggro Pwr Mgmt Enable */
203258223a3SMatthew Dillon #define  AHCI_PREG_CMD_ASP		(1<<27) /* Aggro Slumber/Partial */
204258223a3SMatthew Dillon #define  AHCI_PREG_CMD_ICC		0xf0000000 /* Interface Comm Ctrl */
205258223a3SMatthew Dillon #define  AHCI_PREG_CMD_ICC_SLUMBER	0x60000000
206258223a3SMatthew Dillon #define  AHCI_PREG_CMD_ICC_PARTIAL	0x20000000
207258223a3SMatthew Dillon #define  AHCI_PREG_CMD_ICC_ACTIVE	0x10000000
208258223a3SMatthew Dillon #define  AHCI_PREG_CMD_ICC_IDLE		0x00000000
209258223a3SMatthew Dillon #define  AHCI_PFMT_CMD		"\020" "\034ASP" "\033ALPE" "\032DLAE" \
2104b450139SMatthew Dillon 				    "\031ATAPI" "\030APSTE" "\027FBSCP" \
2114b450139SMatthew Dillon 				    "\026ESP" "\025CPD" "\024MPSP" \
212258223a3SMatthew Dillon 				    "\023HPCP" "\022PMA" "\021CPS" "\020CR" \
213258223a3SMatthew Dillon 				    "\017FR" "\016MPSS" "\005FRE" "\004CLO" \
214258223a3SMatthew Dillon 				    "\003POD" "\002SUD" "\001ST"
215258223a3SMatthew Dillon 
216258223a3SMatthew Dillon #define AHCI_PREG_TFD		0x20 /* Task File Data*/
217258223a3SMatthew Dillon #define  AHCI_PREG_TFD_STS		0xff
218258223a3SMatthew Dillon #define  AHCI_PREG_TFD_STS_ERR		(1<<0)
219258223a3SMatthew Dillon #define  AHCI_PREG_TFD_STS_DRQ		(1<<3)
220258223a3SMatthew Dillon #define  AHCI_PREG_TFD_STS_BSY		(1<<7)
221258223a3SMatthew Dillon #define  AHCI_PREG_TFD_ERR		0xff00
222258223a3SMatthew Dillon 
223258223a3SMatthew Dillon #define AHCI_PFMT_TFD_STS	"\20" "\010BSY" "\004DRQ" "\001ERR"
224258223a3SMatthew Dillon #define AHCI_PREG_SIG		0x24 /* Signature */
225258223a3SMatthew Dillon 
226258223a3SMatthew Dillon #define AHCI_PREG_SSTS		0x28 /* SATA Status */
227258223a3SMatthew Dillon #define  AHCI_PREG_SSTS_DET		0xf /* Device Detection */
228258223a3SMatthew Dillon #define  AHCI_PREG_SSTS_DET_NONE	0x0
229258223a3SMatthew Dillon #define  AHCI_PREG_SSTS_DET_DEV_NE	0x1
230258223a3SMatthew Dillon #define  AHCI_PREG_SSTS_DET_DEV		0x3
231258223a3SMatthew Dillon #define  AHCI_PREG_SSTS_DET_PHYOFFLINE	0x4
232258223a3SMatthew Dillon #define  AHCI_PREG_SSTS_SPD		0xf0 /* Current Interface Speed */
233258223a3SMatthew Dillon #define  AHCI_PREG_SSTS_SPD_NONE	0x00
234258223a3SMatthew Dillon #define  AHCI_PREG_SSTS_SPD_GEN1	0x10
235258223a3SMatthew Dillon #define  AHCI_PREG_SSTS_SPD_GEN2	0x20
2368986d351SMatthew Dillon #define  AHCI_PREG_SSTS_SPD_GEN3	0x30
237258223a3SMatthew Dillon #define  AHCI_PREG_SSTS_IPM		0xf00 /* Interface Power Management */
238258223a3SMatthew Dillon #define  AHCI_PREG_SSTS_IPM_NONE	0x000
239258223a3SMatthew Dillon #define  AHCI_PREG_SSTS_IPM_ACTIVE	0x100
240258223a3SMatthew Dillon #define  AHCI_PREG_SSTS_IPM_PARTIAL	0x200
241258223a3SMatthew Dillon #define  AHCI_PREG_SSTS_IPM_SLUMBER	0x600
242*d90e4fd1SImre Vadász #define  AHCI_PREG_SSTS_IPM_DEVSLEEP	0x800
243258223a3SMatthew Dillon 
244258223a3SMatthew Dillon #define AHCI_PREG_SCTL		0x2c /* SATA Control */
245258223a3SMatthew Dillon #define  AHCI_PREG_SCTL_DET		0xf /* Device Detection */
246258223a3SMatthew Dillon #define  AHCI_PREG_SCTL_DET_NONE	0x0
247258223a3SMatthew Dillon #define  AHCI_PREG_SCTL_DET_INIT	0x1
248258223a3SMatthew Dillon #define  AHCI_PREG_SCTL_DET_DISABLE	0x4
249258223a3SMatthew Dillon #define  AHCI_PREG_SCTL_SPD		0xf0 /* Speed Allowed */
250258223a3SMatthew Dillon #define  AHCI_PREG_SCTL_SPD_ANY		0x00
251258223a3SMatthew Dillon #define  AHCI_PREG_SCTL_SPD_GEN1	0x10
252258223a3SMatthew Dillon #define  AHCI_PREG_SCTL_SPD_GEN2	0x20
2538986d351SMatthew Dillon #define  AHCI_PREG_SCTL_SPD_GEN3	0x30
254258223a3SMatthew Dillon #define  AHCI_PREG_SCTL_IPM		0xf00 /* Interface Power Management */
255258223a3SMatthew Dillon #define  AHCI_PREG_SCTL_IPM_NONE	0x000
256258223a3SMatthew Dillon #define  AHCI_PREG_SCTL_IPM_NOPARTIAL	0x100
257258223a3SMatthew Dillon #define  AHCI_PREG_SCTL_IPM_NOSLUMBER	0x200
258258223a3SMatthew Dillon #define  AHCI_PREG_SCTL_IPM_DISABLED	0x300
2593209f581SMatthew Dillon #define	 AHCI_PREG_SCTL_SPM		0xf000	/* Select Power Management */
2603209f581SMatthew Dillon #define	 AHCI_PREG_SCTL_SPM_NONE	0x0000
2613209f581SMatthew Dillon #define	 AHCI_PREG_SCTL_SPM_NOPARTIAL	0x1000
2623209f581SMatthew Dillon #define	 AHCI_PREG_SCTL_SPM_NOSLUMBER	0x2000
2633209f581SMatthew Dillon #define	 AHCI_PREG_SCTL_SPM_DISABLED	0x3000
2643209f581SMatthew Dillon #define  AHCI_PREG_SCTL_PMP		0xf0000	/* Set PM port for xmit FISes */
2653209f581SMatthew Dillon #define  AHCI_PREG_SCTL_PMP_SHIFT	16
266258223a3SMatthew Dillon 
267258223a3SMatthew Dillon #define AHCI_PREG_SERR		0x30 /* SATA Error */
268258223a3SMatthew Dillon #define  AHCI_PREG_SERR_ERR_I		(1<<0) /* Recovered Data Integrity */
269258223a3SMatthew Dillon #define  AHCI_PREG_SERR_ERR_M		(1<<1) /* Recovered Communications */
270258223a3SMatthew Dillon #define  AHCI_PREG_SERR_ERR_T		(1<<8) /* Transient Data Integrity */
271258223a3SMatthew Dillon #define  AHCI_PREG_SERR_ERR_C		(1<<9) /* Persistent Comm/Data */
272258223a3SMatthew Dillon #define  AHCI_PREG_SERR_ERR_P		(1<<10) /* Protocol */
273258223a3SMatthew Dillon #define  AHCI_PREG_SERR_ERR_E		(1<<11) /* Internal */
2741980eff3SMatthew Dillon #define  AHCI_PREG_SERR_DIAG_N		(1<<16) /* PhyRdy Change */
2751980eff3SMatthew Dillon #define  AHCI_PREG_SERR_DIAG_I		(1<<17) /* Phy Internal Error */
2761980eff3SMatthew Dillon #define  AHCI_PREG_SERR_DIAG_W		(1<<18) /* Comm Wake */
2771980eff3SMatthew Dillon #define  AHCI_PREG_SERR_DIAG_B		(1<<19) /* 10B to 8B Decode Error */
2781980eff3SMatthew Dillon #define  AHCI_PREG_SERR_DIAG_D		(1<<20) /* Disparity Error */
2791980eff3SMatthew Dillon #define  AHCI_PREG_SERR_DIAG_C		(1<<21) /* CRC Error */
2801980eff3SMatthew Dillon #define  AHCI_PREG_SERR_DIAG_H		(1<<22) /* Handshake Error */
2811980eff3SMatthew Dillon #define  AHCI_PREG_SERR_DIAG_S		(1<<23) /* Link Sequence Error */
2821980eff3SMatthew Dillon #define  AHCI_PREG_SERR_DIAG_T		(1<<24) /* Transport State Trans Err */
2831980eff3SMatthew Dillon #define  AHCI_PREG_SERR_DIAG_F		(1<<25) /* Unknown FIS Type */
2841980eff3SMatthew Dillon #define  AHCI_PREG_SERR_DIAG_X		(1<<26) /* Exchanged */
2851980eff3SMatthew Dillon 
2861980eff3SMatthew Dillon #define  AHCI_PFMT_SERR	"\020" 	\
2871980eff3SMatthew Dillon 			"\033DIAG.X" "\032DIAG.F" "\031DIAG.T" "\030DIAG.S" \
2881980eff3SMatthew Dillon 			"\027DIAG.H" "\026DIAG.C" "\025DIAG.D" "\024DIAG.B" \
2891980eff3SMatthew Dillon 			"\023DIAG.W" "\022DIAG.I" "\021DIAG.N"		    \
2901980eff3SMatthew Dillon 			"\014ERR.E" "\013ERR.P" "\012ERR.C" "\011ERR.T"	    \
2911980eff3SMatthew Dillon 			"\002ERR.M" "\001ERR.I"
292258223a3SMatthew Dillon 
293258223a3SMatthew Dillon #define AHCI_PREG_SACT		0x34 /* SATA Active */
294258223a3SMatthew Dillon #define AHCI_PREG_CI		0x38 /* Command Issue */
295258223a3SMatthew Dillon #define  AHCI_PREG_CI_ALL_SLOTS	0xffffffff
296258223a3SMatthew Dillon #define AHCI_PREG_SNTF		0x3c /* SNotification */
297258223a3SMatthew Dillon 
2981980eff3SMatthew Dillon /*
2994b450139SMatthew Dillon  * EN	- Enable FIS based switch, can only be changed when ST is clear
3004b450139SMatthew Dillon  *
3014b450139SMatthew Dillon  * DEC	- Device Error Clear, state machine.  Set to 1 by software only
3024b450139SMatthew Dillon  *	  for the EN+SDE case, then poll until hardware sets it back to 0.
3034b450139SMatthew Dillon  *	  Writing 0 has no effect.
3044b450139SMatthew Dillon  *
3054b450139SMatthew Dillon  * SDE	- Set by hardware indicating a single device error occurred.  If
3064b450139SMatthew Dillon  *	  not set and an error occurred then the error was whole-port.
3074b450139SMatthew Dillon  *
3084b450139SMatthew Dillon  * DEV	- Set by software to the PM target of the next command to issue
3094b450139SMatthew Dillon  *	  via the PREG_CI registers.  Software should not issue multiple
3104b450139SMatthew Dillon  *	  commands covering different targets in a single write.  This
3114b450139SMatthew Dillon  *	  basically causes writes to PREG_CI to index within the hardware.
3124b450139SMatthew Dillon  *
3134b450139SMatthew Dillon  * ADO	- (read only) Indicate how many concurrent devices commands may
3144b450139SMatthew Dillon  *	  be issued to at once.  Degredation may occur if commands are
3154b450139SMatthew Dillon  *	  issued to more devices but the case is allowed.
3164b450139SMatthew Dillon  *
3174b450139SMatthew Dillon  * DWE	- (read only) Only valid on SDE errors.  Hardware indicates which
3184b450139SMatthew Dillon  *	  PM target generated the error in this field.
3194b450139SMatthew Dillon  *
3204b450139SMatthew Dillon  */
3214b450139SMatthew Dillon #define AHCI_PREG_FBS		0x40 /* FIS-Based Switching Control */
3224b450139SMatthew Dillon #define  AHCI_PREG_FBS_EN		(1<<0) /* FIS-Based switching enable */
3234b450139SMatthew Dillon #define  AHCI_PREG_FBS_DEC		(1<<1) /* Device Error Clear */
3244b450139SMatthew Dillon #define  AHCI_PREG_FBS_SDE		(1<<2) /* Single-device Error */
3254b450139SMatthew Dillon #define  AHCI_PREG_FBS_DEV		0x00000F00 /* Device to Issue mask */
3264b450139SMatthew Dillon #define  AHCI_PREG_FBS_ADO		0x0000F000 /* Active Dev Optimize */
3274b450139SMatthew Dillon #define  AHCI_PREG_FBS_DWE		0x000F0000 /* Device With Error */
3284b450139SMatthew Dillon #define  AHCI_PREG_FBS_DEV_SHIFT	8
3294b450139SMatthew Dillon #define  AHCI_PREG_FBS_ADO_SHIFT	12
3304b450139SMatthew Dillon #define  AHCI_PREG_FBS_DWE_SHIFT	16
3314b450139SMatthew Dillon 
332*d90e4fd1SImre Vadász #define AHCI_PREG_DEVSLP	0x44 /* Device Sleep */
333*d90e4fd1SImre Vadász #define  AHCI_PREG_DEVSLP_DSP		0x00000002 /* Device Sleep Present */
334*d90e4fd1SImre Vadász #define  AHCI_PREG_DEVSLP_ADSE		0x00000001 /* A-Device Sleep Enable*/
335*d90e4fd1SImre Vadász 
3364b450139SMatthew Dillon /*
3371980eff3SMatthew Dillon  * AHCI mapped structures
3381980eff3SMatthew Dillon  */
339258223a3SMatthew Dillon struct ahci_cmd_hdr {
340258223a3SMatthew Dillon 	u_int16_t		flags;
341258223a3SMatthew Dillon #define AHCI_CMD_LIST_FLAG_CFL		0x001f /* Command FIS Length */
342258223a3SMatthew Dillon #define AHCI_CMD_LIST_FLAG_A		(1<<5) /* ATAPI */
343258223a3SMatthew Dillon #define AHCI_CMD_LIST_FLAG_W		(1<<6) /* Write */
344258223a3SMatthew Dillon #define AHCI_CMD_LIST_FLAG_P		(1<<7) /* Prefetchable */
345258223a3SMatthew Dillon #define AHCI_CMD_LIST_FLAG_R		(1<<8) /* Reset */
346258223a3SMatthew Dillon #define AHCI_CMD_LIST_FLAG_B		(1<<9) /* BIST */
347258223a3SMatthew Dillon #define AHCI_CMD_LIST_FLAG_C		(1<<10) /* Clear Busy upon R_OK */
348258223a3SMatthew Dillon #define AHCI_CMD_LIST_FLAG_PMP		0xf000 /* Port Multiplier Port */
3491980eff3SMatthew Dillon #define AHCI_CMD_LIST_FLAG_PMP_SHIFT	12
350258223a3SMatthew Dillon 	u_int16_t		prdtl; /* sgl len */
351258223a3SMatthew Dillon 
352258223a3SMatthew Dillon 	u_int32_t		prdbc; /* transferred byte count */
353258223a3SMatthew Dillon 
354258223a3SMatthew Dillon 	u_int32_t		ctba_lo;
355258223a3SMatthew Dillon 	u_int32_t		ctba_hi;
356258223a3SMatthew Dillon 
357258223a3SMatthew Dillon 	u_int32_t		reserved[4];
358258223a3SMatthew Dillon } __packed;
359258223a3SMatthew Dillon 
360258223a3SMatthew Dillon struct ahci_rfis {
361258223a3SMatthew Dillon 	u_int8_t		dsfis[28];
362258223a3SMatthew Dillon 	u_int8_t		reserved1[4];
363258223a3SMatthew Dillon 	u_int8_t		psfis[24];
364258223a3SMatthew Dillon 	u_int8_t		reserved2[8];
365258223a3SMatthew Dillon 	u_int8_t		rfis[24];
366258223a3SMatthew Dillon 	u_int8_t		reserved3[4];
367258223a3SMatthew Dillon 	u_int8_t		sdbfis[4];
368258223a3SMatthew Dillon 	u_int8_t		ufis[64];
369258223a3SMatthew Dillon 	u_int8_t		reserved4[96];
370258223a3SMatthew Dillon } __packed;
371258223a3SMatthew Dillon 
372258223a3SMatthew Dillon struct ahci_prdt {
373258223a3SMatthew Dillon 	u_int32_t		dba_lo;
374258223a3SMatthew Dillon 	u_int32_t		dba_hi;
375258223a3SMatthew Dillon 	u_int32_t		reserved;
376258223a3SMatthew Dillon 	u_int32_t		flags;
377258223a3SMatthew Dillon #define AHCI_PRDT_FLAG_INTR		(1<<31) /* interrupt on completion */
378258223a3SMatthew Dillon } __packed;
379258223a3SMatthew Dillon 
380258223a3SMatthew Dillon /*
381258223a3SMatthew Dillon  * The base command table structure is 128 bytes.  Each prdt is 16 bytes.
382b4faa036SFrançois Tigeot  * We need to accomodate a 2MB maximum I/O transfer size, which is at least
383b4faa036SFrançois Tigeot  * 512 entries, plus one for page slop.
384258223a3SMatthew Dillon  *
385b4faa036SFrançois Tigeot  * Making the ahci_cmd_table 16384 bytes (a reasonable power of 2)
386b4faa036SFrançois Tigeot  * thus requires MAX_PRDT to be set to 1016.
387258223a3SMatthew Dillon  */
388b4faa036SFrançois Tigeot #define AHCI_MAX_PRDT		1016
3891980eff3SMatthew Dillon #define AHCI_MAX_PMPORTS	16
390258223a3SMatthew Dillon 
391b4faa036SFrançois Tigeot #define AHCI_MAXPHYS		(2 * 1024 * 1024)	/* 2MB */
392b4faa036SFrançois Tigeot #if AHCI_MAXPHYS / PAGE_SIZE + 1 > AHCI_MAX_PRDT
393258223a3SMatthew Dillon #error "AHCI_MAX_PRDT is not big enough"
394258223a3SMatthew Dillon #endif
395258223a3SMatthew Dillon 
396258223a3SMatthew Dillon struct ahci_cmd_table {
397258223a3SMatthew Dillon 	u_int8_t		cfis[64];	/* Command FIS */
398258223a3SMatthew Dillon 	u_int8_t		acmd[16];	/* ATAPI Command */
399258223a3SMatthew Dillon 	u_int8_t		reserved[48];
400258223a3SMatthew Dillon 
401258223a3SMatthew Dillon 	struct ahci_prdt	prdt[AHCI_MAX_PRDT];
402258223a3SMatthew Dillon } __packed;
403258223a3SMatthew Dillon 
404258223a3SMatthew Dillon #define AHCI_MAX_PORTS		32
405258223a3SMatthew Dillon 
406258223a3SMatthew Dillon struct ahci_dmamem {
407258223a3SMatthew Dillon 	bus_dma_tag_t		adm_tag;
408258223a3SMatthew Dillon 	bus_dmamap_t		adm_map;
409258223a3SMatthew Dillon 	bus_dma_segment_t	adm_seg;
410258223a3SMatthew Dillon 	bus_addr_t		adm_busaddr;
411258223a3SMatthew Dillon 	caddr_t			adm_kva;
412258223a3SMatthew Dillon };
413258223a3SMatthew Dillon #define AHCI_DMA_MAP(_adm)	((_adm)->adm_map)
414258223a3SMatthew Dillon #define AHCI_DMA_DVA(_adm)	((_adm)->adm_busaddr)
415258223a3SMatthew Dillon #define AHCI_DMA_KVA(_adm)	((void *)(_adm)->adm_kva)
416258223a3SMatthew Dillon 
417258223a3SMatthew Dillon struct ahci_softc;
418258223a3SMatthew Dillon struct ahci_port;
419258223a3SMatthew Dillon struct ahci_device;
420258223a3SMatthew Dillon 
421258223a3SMatthew Dillon struct ahci_ccb {
422258223a3SMatthew Dillon 	/* ATA xfer associated with this CCB.  Must be 1st struct member. */
423258223a3SMatthew Dillon 	struct ata_xfer		ccb_xa;
424258223a3SMatthew Dillon 	struct callout          ccb_timeout;
425258223a3SMatthew Dillon 
426258223a3SMatthew Dillon 	int			ccb_slot;
427258223a3SMatthew Dillon 	struct ahci_port	*ccb_port;
428258223a3SMatthew Dillon 
429258223a3SMatthew Dillon 	bus_dmamap_t		ccb_dmamap;
430258223a3SMatthew Dillon 	struct ahci_cmd_hdr	*ccb_cmd_hdr;
431258223a3SMatthew Dillon 	struct ahci_cmd_table	*ccb_cmd_table;
432258223a3SMatthew Dillon 
433258223a3SMatthew Dillon 	void			(*ccb_done)(struct ahci_ccb *);
434258223a3SMatthew Dillon 
435258223a3SMatthew Dillon 	TAILQ_ENTRY(ahci_ccb)	ccb_entry;
436258223a3SMatthew Dillon };
437258223a3SMatthew Dillon 
438258223a3SMatthew Dillon struct ahci_port {
439258223a3SMatthew Dillon 	struct ahci_softc	*ap_sc;
440258223a3SMatthew Dillon 	bus_space_handle_t	ap_ioh;
441258223a3SMatthew Dillon 
442258223a3SMatthew Dillon 	int			ap_num;
4431980eff3SMatthew Dillon 	int			ap_pmcount;
444258223a3SMatthew Dillon 	int			ap_flags;
445258223a3SMatthew Dillon #define AP_F_BUS_REGISTERED	0x0001
446258223a3SMatthew Dillon #define AP_F_CAM_ATTACHED	0x0002
4471980eff3SMatthew Dillon #define AP_F_IN_RESET		0x0004
4483209f581SMatthew Dillon #define AP_F_SCAN_RUNNING	0x0008
4493209f581SMatthew Dillon #define AP_F_SCAN_REQUESTED	0x0010
450f4553de1SMatthew Dillon #define AP_F_SCAN_COMPLETED	0x0020
451f4553de1SMatthew Dillon #define AP_F_IGNORE_IFS		0x0040
452f4553de1SMatthew Dillon #define AP_F_IFS_IGNORED	0x0080
453492bffafSMatthew Dillon #define AP_F_UNUSED_0100	0x0100
454831bc9e3SMatthew Dillon #define AP_F_EXCLUSIVE_ACCESS	0x0200
455baef7501SMatthew Dillon #define AP_F_ERR_CCB_RESERVED	0x0400
456492bffafSMatthew Dillon #define AP_F_HARSH_REINIT	0x0800
457f4553de1SMatthew Dillon 	int			ap_signal;	/* os per-port thread sig */
458f4553de1SMatthew Dillon 	thread_t		ap_thread;	/* os per-port thread */
459f4553de1SMatthew Dillon 	struct lock		ap_lock;	/* os per-port lock */
460fb00c6edSMatthew Dillon 	struct lock		ap_sim_lock;	/* cam sim lock */
461fb00c6edSMatthew Dillon 	struct lock		ap_sig_lock;	/* signal thread */
462f4553de1SMatthew Dillon #define AP_SIGF_INIT		0x0001
463f4553de1SMatthew Dillon #define AP_SIGF_TIMEOUT		0x0002
464f4553de1SMatthew Dillon #define AP_SIGF_PORTINT		0x0004
465e8cf3f55SMatthew Dillon #define AP_SIGF_THREAD_SYNC	0x0008
466f4553de1SMatthew Dillon #define AP_SIGF_STOP		0x8000
467258223a3SMatthew Dillon 	struct cam_sim		*ap_sim;
468258223a3SMatthew Dillon 
469258223a3SMatthew Dillon 	struct ahci_rfis	*ap_rfis;
470258223a3SMatthew Dillon 	struct ahci_dmamem	*ap_dmamem_rfis;
471258223a3SMatthew Dillon 
472258223a3SMatthew Dillon 	struct ahci_dmamem	*ap_dmamem_cmd_list;
473258223a3SMatthew Dillon 	struct ahci_dmamem	*ap_dmamem_cmd_table;
474258223a3SMatthew Dillon 
475831bc9e3SMatthew Dillon 	u_int32_t		ap_active;	/* active CI command bmask */
476831bc9e3SMatthew Dillon 	u_int32_t		ap_active_cnt;	/* active CI command count */
477831bc9e3SMatthew Dillon 	u_int32_t		ap_sactive;	/* active SACT command bmask */
4784c339a5fSMatthew Dillon 	u_int32_t		ap_expired;	/* deferred expired bmask */
47912feb904SMatthew Dillon 	u_int32_t		ap_intmask;	/* interrupts we care about */
480258223a3SMatthew Dillon 	struct ahci_ccb		*ap_ccbs;
4811067474aSMatthew Dillon 	struct ahci_ccb		*ap_err_ccb;	/* always CCB SLOT 1 */
48212feb904SMatthew Dillon 	int			ap_run_flags;	/* used to check excl mode */
483258223a3SMatthew Dillon 
484258223a3SMatthew Dillon 	TAILQ_HEAD(, ahci_ccb)	ap_ccb_free;
485258223a3SMatthew Dillon 	TAILQ_HEAD(, ahci_ccb)	ap_ccb_pending;
486258223a3SMatthew Dillon 	struct lock		ap_ccb_lock;
487258223a3SMatthew Dillon 
4881980eff3SMatthew Dillon 	int			ap_type;	/* ATA_PORT_T_xxx */
4891980eff3SMatthew Dillon 	int			ap_probe;	/* ATA_PROBE_xxx */
490b012a2caSMatthew Dillon 	struct ata_port		*ap_ata[AHCI_MAX_PMPORTS];
491258223a3SMatthew Dillon 
492258223a3SMatthew Dillon 	u_int32_t		ap_state;
493258223a3SMatthew Dillon #define AP_S_NORMAL			0
494258223a3SMatthew Dillon #define AP_S_FATAL_ERROR		1
495258223a3SMatthew Dillon 
496258223a3SMatthew Dillon 	/* For error recovery. */
497258223a3SMatthew Dillon 	u_int32_t		ap_err_saved_sactive;
498258223a3SMatthew Dillon 	u_int32_t		ap_err_saved_active;
499258223a3SMatthew Dillon 	u_int32_t		ap_err_saved_active_cnt;
500258223a3SMatthew Dillon 
50112feb904SMatthew Dillon 	u_int8_t		*ap_err_scratch;
502258223a3SMatthew Dillon 
503f17a0cedSMatthew Dillon 	int			link_pwr_mgmt;
504f17a0cedSMatthew Dillon 
505f17a0cedSMatthew Dillon 	struct sysctl_ctx_list	sysctl_ctx;
506f17a0cedSMatthew Dillon 	struct sysctl_oid	*sysctl_tree;
507f17a0cedSMatthew Dillon 
508258223a3SMatthew Dillon 	char			ap_name[16];
509258223a3SMatthew Dillon };
510258223a3SMatthew Dillon 
511258223a3SMatthew Dillon #define PORTNAME(_ap)		((_ap)->ap_name)
5121980eff3SMatthew Dillon #define ATANAME(_ap, _at)	((_at) ? (_at)->at_name : (_ap)->ap_name)
513258223a3SMatthew Dillon 
514258223a3SMatthew Dillon struct ahci_softc {
515258223a3SMatthew Dillon 	device_t		sc_dev;
516258223a3SMatthew Dillon 	const struct ahci_device *sc_ad;	/* special casing */
517258223a3SMatthew Dillon 
518258223a3SMatthew Dillon 	struct resource		*sc_irq;	/* bus resources */
519258223a3SMatthew Dillon 	struct resource		*sc_regs;	/* bus resources */
520258223a3SMatthew Dillon 	bus_space_tag_t		sc_iot;		/* split from sc_regs */
521258223a3SMatthew Dillon 	bus_space_handle_t	sc_ioh;		/* split from sc_regs */
522258223a3SMatthew Dillon 
5239783883aSSepherosa Ziehau 	int			sc_irq_type;
524258223a3SMatthew Dillon 	int			sc_rid_irq;	/* saved bus RIDs */
525258223a3SMatthew Dillon 	int			sc_rid_regs;
526258223a3SMatthew Dillon 	u_int32_t		sc_cap;		/* capabilities */
5274b450139SMatthew Dillon 	u_int32_t		sc_cap2;	/* capabilities */
5284b450139SMatthew Dillon 	u_int32_t		sc_vers;	/* AHCI version */
52912feb904SMatthew Dillon 	int			sc_numports;
53012feb904SMatthew Dillon 	u_int32_t		sc_portmask;
531258223a3SMatthew Dillon 
532258223a3SMatthew Dillon 	void			*sc_irq_handle;	/* installed irq vector */
533258223a3SMatthew Dillon 
534258223a3SMatthew Dillon 	bus_dma_tag_t		sc_tag_rfis;	/* bus DMA tags */
535258223a3SMatthew Dillon 	bus_dma_tag_t		sc_tag_cmdh;
536258223a3SMatthew Dillon 	bus_dma_tag_t		sc_tag_cmdt;
537258223a3SMatthew Dillon 	bus_dma_tag_t		sc_tag_data;
538258223a3SMatthew Dillon 
539258223a3SMatthew Dillon 	int			sc_flags;
5404b450139SMatthew Dillon #define AHCI_F_NO_NCQ			0x00000001
5414b450139SMatthew Dillon #define AHCI_F_IGN_FR			0x00000002
5424b450139SMatthew Dillon #define AHCI_F_INT_GOOD			0x00000004
5434b450139SMatthew Dillon #define AHCI_F_FORCE_FBSS		0x00000008
544258223a3SMatthew Dillon 
545258223a3SMatthew Dillon 	u_int			sc_ncmds;
546258223a3SMatthew Dillon 
547258223a3SMatthew Dillon 	struct ahci_port	*sc_ports[AHCI_MAX_PORTS];
548258223a3SMatthew Dillon 
549258223a3SMatthew Dillon #ifdef AHCI_COALESCE
550258223a3SMatthew Dillon 	u_int32_t		sc_ccc_mask;
551258223a3SMatthew Dillon 	u_int32_t		sc_ccc_ports;
552258223a3SMatthew Dillon 	u_int32_t		sc_ccc_ports_cur;
553258223a3SMatthew Dillon #endif
554258223a3SMatthew Dillon };
555258223a3SMatthew Dillon #define DEVNAME(_s)		((_s)->sc_dev.dv_xname)
556258223a3SMatthew Dillon 
557258223a3SMatthew Dillon struct ahci_device {
558258223a3SMatthew Dillon 	pci_vendor_id_t		ad_vendor;
559258223a3SMatthew Dillon 	pci_product_id_t	ad_product;
560258223a3SMatthew Dillon 	int			(*ad_attach)(device_t dev);
561258223a3SMatthew Dillon 	int			(*ad_detach)(device_t dev);
562258223a3SMatthew Dillon 	char			*name;
563258223a3SMatthew Dillon };
564258223a3SMatthew Dillon 
56512feb904SMatthew Dillon /* Wait for all bits in _b to be cleared */
56612feb904SMatthew Dillon #define ahci_pwait_clr(_ap, _r, _b) \
56712feb904SMatthew Dillon 	ahci_pwait_eq((_ap), AHCI_PWAIT_TIMEOUT, (_r), (_b), 0)
56812feb904SMatthew Dillon #define ahci_pwait_clr_to(_ap, _to,  _r, _b) \
56912feb904SMatthew Dillon 	ahci_pwait_eq((_ap), _to, (_r), (_b), 0)
57012feb904SMatthew Dillon 
57112feb904SMatthew Dillon /* Wait for all bits in _b to be set */
57212feb904SMatthew Dillon #define ahci_pwait_set(_ap, _r, _b) \
57312feb904SMatthew Dillon 	ahci_pwait_eq((_ap), AHCI_PWAIT_TIMEOUT, (_r), (_b), (_b))
57412feb904SMatthew Dillon #define ahci_pwait_set_to(_ap, _to, _r, _b) \
57512feb904SMatthew Dillon 	ahci_pwait_eq((_ap), _to, (_r), (_b), (_b))
57612feb904SMatthew Dillon 
57712feb904SMatthew Dillon #define AHCI_PWAIT_TIMEOUT      1000
57812feb904SMatthew Dillon 
579258223a3SMatthew Dillon const struct ahci_device *ahci_lookup_device(device_t dev);
580258223a3SMatthew Dillon int	ahci_init(struct ahci_softc *);
58112feb904SMatthew Dillon int	ahci_port_init(struct ahci_port *ap);
582258223a3SMatthew Dillon int	ahci_port_alloc(struct ahci_softc *, u_int);
583831bc9e3SMatthew Dillon void	ahci_port_state_machine(struct ahci_port *ap, int initial);
584258223a3SMatthew Dillon void	ahci_port_free(struct ahci_softc *, u_int);
5851980eff3SMatthew Dillon int	ahci_port_reset(struct ahci_port *, struct ata_port *at, int);
586f17a0cedSMatthew Dillon void	ahci_port_link_pwr_mgmt(struct ahci_port *, int link_pwr_mgmt);
587795adb22SMatthew Dillon int	ahci_port_link_pwr_state(struct ahci_port *);
588fd8bd957SMatthew Dillon 
589258223a3SMatthew Dillon u_int32_t ahci_read(struct ahci_softc *, bus_size_t);
590258223a3SMatthew Dillon void	ahci_write(struct ahci_softc *, bus_size_t, u_int32_t);
591258223a3SMatthew Dillon int	ahci_wait_ne(struct ahci_softc *, bus_size_t, u_int32_t, u_int32_t);
592258223a3SMatthew Dillon u_int32_t ahci_pread(struct ahci_port *, bus_size_t);
593258223a3SMatthew Dillon void	ahci_pwrite(struct ahci_port *, bus_size_t, u_int32_t);
594cec85a37SMatthew Dillon int	ahci_pwait_eq(struct ahci_port *, int, bus_size_t,
595cec85a37SMatthew Dillon 			u_int32_t, u_int32_t);
596258223a3SMatthew Dillon void	ahci_intr(void *);
597f4553de1SMatthew Dillon void	ahci_port_intr(struct ahci_port *ap, int blockable);
598258223a3SMatthew Dillon 
59912feb904SMatthew Dillon int	ahci_port_start(struct ahci_port *ap);
60012feb904SMatthew Dillon int	ahci_port_stop(struct ahci_port *ap, int stop_fis_rx);
60112feb904SMatthew Dillon int	ahci_port_clo(struct ahci_port *ap);
60212feb904SMatthew Dillon void	ahci_flush_tfd(struct ahci_port *ap);
603795adb22SMatthew Dillon int	ahci_set_feature(struct ahci_port *ap, struct ata_port *atx,
604795adb22SMatthew Dillon 			int feature, int enable);
60512feb904SMatthew Dillon 
606258223a3SMatthew Dillon int	ahci_cam_attach(struct ahci_port *ap);
6073209f581SMatthew Dillon void	ahci_cam_changed(struct ahci_port *ap, struct ata_port *at, int found);
608258223a3SMatthew Dillon void	ahci_cam_detach(struct ahci_port *ap);
6093209f581SMatthew Dillon int	ahci_cam_probe(struct ahci_port *ap, struct ata_port *at);
610258223a3SMatthew Dillon 
6111980eff3SMatthew Dillon struct ata_xfer *ahci_ata_get_xfer(struct ahci_port *ap, struct ata_port *at);
612258223a3SMatthew Dillon void	ahci_ata_put_xfer(struct ata_xfer *xa);
613258223a3SMatthew Dillon int	ahci_ata_cmd(struct ata_xfer *xa);
614258223a3SMatthew Dillon 
61512feb904SMatthew Dillon int     ahci_pm_port_probe(struct ahci_port *ap, int);
61612feb904SMatthew Dillon int	ahci_pm_port_init(struct ahci_port *ap, struct ata_port *at);
6171980eff3SMatthew Dillon int	ahci_pm_identify(struct ahci_port *ap);
6181980eff3SMatthew Dillon int	ahci_pm_hardreset(struct ahci_port *ap, int target, int hard);
6191980eff3SMatthew Dillon int	ahci_pm_softreset(struct ahci_port *ap, int target);
6201980eff3SMatthew Dillon int	ahci_pm_phy_status(struct ahci_port *ap, int target, u_int32_t *datap);
6211980eff3SMatthew Dillon int	ahci_pm_read(struct ahci_port *ap, int target,
6221980eff3SMatthew Dillon 			int which, u_int32_t *res);
6231980eff3SMatthew Dillon int	ahci_pm_write(struct ahci_port *ap, int target,
6241980eff3SMatthew Dillon 			int which, u_int32_t data);
6253209f581SMatthew Dillon void	ahci_pm_check_good(struct ahci_port *ap, int target);
626831bc9e3SMatthew Dillon void	ahci_ata_cmd_timeout(struct ahci_ccb *ccb);
62712feb904SMatthew Dillon void	ahci_quick_timeout(struct ahci_ccb *ccb);
6281980eff3SMatthew Dillon struct ahci_ccb *ahci_get_ccb(struct ahci_port *ap);
6291980eff3SMatthew Dillon void	ahci_put_ccb(struct ahci_ccb *ccb);
630baef7501SMatthew Dillon struct ahci_ccb *ahci_get_err_ccb(struct ahci_port *);
631baef7501SMatthew Dillon void	ahci_put_err_ccb(struct ahci_ccb *);
6321980eff3SMatthew Dillon int	ahci_poll(struct ahci_ccb *ccb, int timeout,
633831bc9e3SMatthew Dillon 			void (*timeout_fn)(struct ahci_ccb *));
634831bc9e3SMatthew Dillon 
6351980eff3SMatthew Dillon int     ahci_port_signature_detect(struct ahci_port *ap, struct ata_port *at);
636f4553de1SMatthew Dillon void	ahci_port_thread_core(struct ahci_port *ap, int mask);
6371980eff3SMatthew Dillon 
638831bc9e3SMatthew Dillon void	ahci_os_sleep(int ms);
639831bc9e3SMatthew Dillon void	ahci_os_hardsleep(int us);
640831bc9e3SMatthew Dillon int	ahci_os_softsleep(void);
641f4553de1SMatthew Dillon void	ahci_os_start_port(struct ahci_port *ap);
642f4553de1SMatthew Dillon void	ahci_os_stop_port(struct ahci_port *ap);
643f4553de1SMatthew Dillon void	ahci_os_signal_port_thread(struct ahci_port *ap, int mask);
644f4553de1SMatthew Dillon void	ahci_os_lock_port(struct ahci_port *ap);
645f4553de1SMatthew Dillon int	ahci_os_lock_port_nb(struct ahci_port *ap);
646f4553de1SMatthew Dillon void	ahci_os_unlock_port(struct ahci_port *ap);
6471980eff3SMatthew Dillon 
6488986d351SMatthew Dillon extern u_int32_t AhciForceGen;
649afa796d2SMatthew Dillon extern u_int32_t AhciNoFeatures;
650f17a0cedSMatthew Dillon 
651f17a0cedSMatthew Dillon enum {AHCI_LINK_PWR_MGMT_NONE, AHCI_LINK_PWR_MGMT_MEDIUM,
652f17a0cedSMatthew Dillon       AHCI_LINK_PWR_MGMT_AGGR};
653