xref: /freebsd/sys/dev/ahci/ahci_pci.c (revision b1877a0b397cfb5e96d8b3d319e80e68d04f7950)
1802df3acSWarner Losh /*-
2802df3acSWarner Losh  * Copyright (c) 2009-2012 Alexander Motin <mav@FreeBSD.org>
3802df3acSWarner Losh  * All rights reserved.
4802df3acSWarner Losh  *
5802df3acSWarner Losh  * Redistribution and use in source and binary forms, with or without
6802df3acSWarner Losh  * modification, are permitted provided that the following conditions
7802df3acSWarner Losh  * are met:
8802df3acSWarner Losh  * 1. Redistributions of source code must retain the above copyright
9802df3acSWarner Losh  *    notice, this list of conditions and the following disclaimer,
10802df3acSWarner Losh  *    without modification, immediately at the beginning of the file.
11802df3acSWarner Losh  * 2. Redistributions in binary form must reproduce the above copyright
12802df3acSWarner Losh  *    notice, this list of conditions and the following disclaimer in the
13802df3acSWarner Losh  *    documentation and/or other materials provided with the distribution.
14802df3acSWarner Losh  *
15802df3acSWarner Losh  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16802df3acSWarner Losh  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17802df3acSWarner Losh  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18802df3acSWarner Losh  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19802df3acSWarner Losh  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20802df3acSWarner Losh  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21802df3acSWarner Losh  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22802df3acSWarner Losh  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23802df3acSWarner Losh  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24802df3acSWarner Losh  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25802df3acSWarner Losh  */
26802df3acSWarner Losh 
27802df3acSWarner Losh #include <sys/cdefs.h>
28802df3acSWarner Losh __FBSDID("$FreeBSD$");
29802df3acSWarner Losh 
30802df3acSWarner Losh #include <sys/param.h>
31802df3acSWarner Losh #include <sys/module.h>
32802df3acSWarner Losh #include <sys/systm.h>
33802df3acSWarner Losh #include <sys/kernel.h>
34802df3acSWarner Losh #include <sys/bus.h>
35802df3acSWarner Losh #include <sys/conf.h>
36802df3acSWarner Losh #include <sys/endian.h>
37802df3acSWarner Losh #include <sys/malloc.h>
38802df3acSWarner Losh #include <sys/lock.h>
39802df3acSWarner Losh #include <sys/mutex.h>
40802df3acSWarner Losh #include <machine/stdarg.h>
41802df3acSWarner Losh #include <machine/resource.h>
42802df3acSWarner Losh #include <machine/bus.h>
43802df3acSWarner Losh #include <sys/rman.h>
44802df3acSWarner Losh #include <dev/pci/pcivar.h>
45802df3acSWarner Losh #include <dev/pci/pcireg.h>
46802df3acSWarner Losh #include "ahci.h"
47802df3acSWarner Losh 
48802df3acSWarner Losh static int force_ahci = 1;
49802df3acSWarner Losh TUNABLE_INT("hw.ahci.force", &force_ahci);
50802df3acSWarner Losh 
5121190895SMarius Strobl static const struct {
52802df3acSWarner Losh 	uint32_t	id;
53802df3acSWarner Losh 	uint8_t		rev;
54802df3acSWarner Losh 	const char	*name;
55802df3acSWarner Losh 	int		quirks;
56802df3acSWarner Losh } ahci_ids[] = {
57802df3acSWarner Losh 	{0x43801002, 0x00, "AMD SB600",
58802df3acSWarner Losh 	    AHCI_Q_NOMSI | AHCI_Q_ATI_PMP_BUG | AHCI_Q_MAXIO_64K},
59*b1877a0bSSteven Hartland 	{0x43901002, 0x00, "AMD SB7x0/SB8x0/SB9x0",
60*b1877a0bSSteven Hartland 	    AHCI_Q_ATI_PMP_BUG | AHCI_Q_1MSI},
61*b1877a0bSSteven Hartland 	{0x43911002, 0x00, "AMD SB7x0/SB8x0/SB9x0",
62*b1877a0bSSteven Hartland 	    AHCI_Q_ATI_PMP_BUG | AHCI_Q_1MSI},
63*b1877a0bSSteven Hartland 	{0x43921002, 0x00, "AMD SB7x0/SB8x0/SB9x0",
64*b1877a0bSSteven Hartland 	    AHCI_Q_ATI_PMP_BUG | AHCI_Q_1MSI},
65*b1877a0bSSteven Hartland 	{0x43931002, 0x00, "AMD SB7x0/SB8x0/SB9x0",
66*b1877a0bSSteven Hartland 	    AHCI_Q_ATI_PMP_BUG | AHCI_Q_1MSI},
67*b1877a0bSSteven Hartland 	{0x43941002, 0x00, "AMD SB7x0/SB8x0/SB9x0",
68*b1877a0bSSteven Hartland 	    AHCI_Q_ATI_PMP_BUG | AHCI_Q_1MSI},
69802df3acSWarner Losh 	/* Not sure SB8x0/SB9x0 needs this quirk. Be conservative though */
70802df3acSWarner Losh 	{0x43951002, 0x00, "AMD SB8x0/SB9x0",	AHCI_Q_ATI_PMP_BUG},
71802df3acSWarner Losh 	{0x78001022, 0x00, "AMD Hudson-2",	0},
72802df3acSWarner Losh 	{0x78011022, 0x00, "AMD Hudson-2",	0},
73802df3acSWarner Losh 	{0x78021022, 0x00, "AMD Hudson-2",	0},
74802df3acSWarner Losh 	{0x78031022, 0x00, "AMD Hudson-2",	0},
75802df3acSWarner Losh 	{0x78041022, 0x00, "AMD Hudson-2",	0},
76802df3acSWarner Losh 	{0x06111b21, 0x00, "ASMedia ASM2106",	0},
77802df3acSWarner Losh 	{0x06121b21, 0x00, "ASMedia ASM1061",	0},
78802df3acSWarner Losh 	{0x26528086, 0x00, "Intel ICH6",	AHCI_Q_NOFORCE},
79802df3acSWarner Losh 	{0x26538086, 0x00, "Intel ICH6M",	AHCI_Q_NOFORCE},
80802df3acSWarner Losh 	{0x26818086, 0x00, "Intel ESB2",	0},
81802df3acSWarner Losh 	{0x26828086, 0x00, "Intel ESB2",	0},
82802df3acSWarner Losh 	{0x26838086, 0x00, "Intel ESB2",	0},
83802df3acSWarner Losh 	{0x27c18086, 0x00, "Intel ICH7",	0},
84802df3acSWarner Losh 	{0x27c38086, 0x00, "Intel ICH7",	0},
85802df3acSWarner Losh 	{0x27c58086, 0x00, "Intel ICH7M",	0},
86802df3acSWarner Losh 	{0x27c68086, 0x00, "Intel ICH7M",	0},
87802df3acSWarner Losh 	{0x28218086, 0x00, "Intel ICH8",	0},
88802df3acSWarner Losh 	{0x28228086, 0x00, "Intel ICH8",	0},
89802df3acSWarner Losh 	{0x28248086, 0x00, "Intel ICH8",	0},
90802df3acSWarner Losh 	{0x28298086, 0x00, "Intel ICH8M",	0},
91802df3acSWarner Losh 	{0x282a8086, 0x00, "Intel ICH8M",	0},
92802df3acSWarner Losh 	{0x29228086, 0x00, "Intel ICH9",	0},
93802df3acSWarner Losh 	{0x29238086, 0x00, "Intel ICH9",	0},
94802df3acSWarner Losh 	{0x29248086, 0x00, "Intel ICH9",	0},
95802df3acSWarner Losh 	{0x29258086, 0x00, "Intel ICH9",	0},
96802df3acSWarner Losh 	{0x29278086, 0x00, "Intel ICH9",	0},
97802df3acSWarner Losh 	{0x29298086, 0x00, "Intel ICH9M",	0},
98802df3acSWarner Losh 	{0x292a8086, 0x00, "Intel ICH9M",	0},
99802df3acSWarner Losh 	{0x292b8086, 0x00, "Intel ICH9M",	0},
100802df3acSWarner Losh 	{0x292c8086, 0x00, "Intel ICH9M",	0},
101802df3acSWarner Losh 	{0x292f8086, 0x00, "Intel ICH9M",	0},
102802df3acSWarner Losh 	{0x294d8086, 0x00, "Intel ICH9",	0},
103802df3acSWarner Losh 	{0x294e8086, 0x00, "Intel ICH9M",	0},
104802df3acSWarner Losh 	{0x3a058086, 0x00, "Intel ICH10",	0},
105802df3acSWarner Losh 	{0x3a228086, 0x00, "Intel ICH10",	0},
106802df3acSWarner Losh 	{0x3a258086, 0x00, "Intel ICH10",	0},
107802df3acSWarner Losh 	{0x3b228086, 0x00, "Intel 5 Series/3400 Series",	0},
108802df3acSWarner Losh 	{0x3b238086, 0x00, "Intel 5 Series/3400 Series",	0},
109802df3acSWarner Losh 	{0x3b258086, 0x00, "Intel 5 Series/3400 Series",	0},
110802df3acSWarner Losh 	{0x3b298086, 0x00, "Intel 5 Series/3400 Series",	0},
111802df3acSWarner Losh 	{0x3b2c8086, 0x00, "Intel 5 Series/3400 Series",	0},
112802df3acSWarner Losh 	{0x3b2f8086, 0x00, "Intel 5 Series/3400 Series",	0},
113802df3acSWarner Losh 	{0x1c028086, 0x00, "Intel Cougar Point",	0},
114802df3acSWarner Losh 	{0x1c038086, 0x00, "Intel Cougar Point",	0},
115802df3acSWarner Losh 	{0x1c048086, 0x00, "Intel Cougar Point",	0},
116802df3acSWarner Losh 	{0x1c058086, 0x00, "Intel Cougar Point",	0},
117802df3acSWarner Losh 	{0x1d028086, 0x00, "Intel Patsburg",	0},
118802df3acSWarner Losh 	{0x1d048086, 0x00, "Intel Patsburg",	0},
119802df3acSWarner Losh 	{0x1d068086, 0x00, "Intel Patsburg",	0},
120802df3acSWarner Losh 	{0x28268086, 0x00, "Intel Patsburg (RAID)",	0},
121802df3acSWarner Losh 	{0x1e028086, 0x00, "Intel Panther Point",	0},
122802df3acSWarner Losh 	{0x1e038086, 0x00, "Intel Panther Point",	0},
123802df3acSWarner Losh 	{0x1e048086, 0x00, "Intel Panther Point (RAID)",	0},
124802df3acSWarner Losh 	{0x1e058086, 0x00, "Intel Panther Point (RAID)",	0},
125802df3acSWarner Losh 	{0x1e068086, 0x00, "Intel Panther Point (RAID)",	0},
126802df3acSWarner Losh 	{0x1e078086, 0x00, "Intel Panther Point (RAID)",	0},
127802df3acSWarner Losh 	{0x1e0e8086, 0x00, "Intel Panther Point (RAID)",	0},
128802df3acSWarner Losh 	{0x1e0f8086, 0x00, "Intel Panther Point (RAID)",	0},
129802df3acSWarner Losh 	{0x1f228086, 0x00, "Intel Avoton",	0},
130802df3acSWarner Losh 	{0x1f238086, 0x00, "Intel Avoton",	0},
131802df3acSWarner Losh 	{0x1f248086, 0x00, "Intel Avoton (RAID)",	0},
132802df3acSWarner Losh 	{0x1f258086, 0x00, "Intel Avoton (RAID)",	0},
133802df3acSWarner Losh 	{0x1f268086, 0x00, "Intel Avoton (RAID)",	0},
134802df3acSWarner Losh 	{0x1f278086, 0x00, "Intel Avoton (RAID)",	0},
135802df3acSWarner Losh 	{0x1f2e8086, 0x00, "Intel Avoton (RAID)",	0},
136802df3acSWarner Losh 	{0x1f2f8086, 0x00, "Intel Avoton (RAID)",	0},
137802df3acSWarner Losh 	{0x1f328086, 0x00, "Intel Avoton",	0},
138802df3acSWarner Losh 	{0x1f338086, 0x00, "Intel Avoton",	0},
139802df3acSWarner Losh 	{0x1f348086, 0x00, "Intel Avoton (RAID)",	0},
140802df3acSWarner Losh 	{0x1f358086, 0x00, "Intel Avoton (RAID)",	0},
141802df3acSWarner Losh 	{0x1f368086, 0x00, "Intel Avoton (RAID)",	0},
142802df3acSWarner Losh 	{0x1f378086, 0x00, "Intel Avoton (RAID)",	0},
143802df3acSWarner Losh 	{0x1f3e8086, 0x00, "Intel Avoton (RAID)",	0},
144802df3acSWarner Losh 	{0x1f3f8086, 0x00, "Intel Avoton (RAID)",	0},
145802df3acSWarner Losh 	{0x23a38086, 0x00, "Intel Coleto Creek",	0},
146802df3acSWarner Losh 	{0x28238086, 0x00, "Intel Wellsburg (RAID)",	0},
147802df3acSWarner Losh 	{0x28278086, 0x00, "Intel Wellsburg (RAID)",	0},
148802df3acSWarner Losh 	{0x8c028086, 0x00, "Intel Lynx Point",	0},
149802df3acSWarner Losh 	{0x8c038086, 0x00, "Intel Lynx Point",	0},
150802df3acSWarner Losh 	{0x8c048086, 0x00, "Intel Lynx Point (RAID)",	0},
151802df3acSWarner Losh 	{0x8c058086, 0x00, "Intel Lynx Point (RAID)",	0},
152802df3acSWarner Losh 	{0x8c068086, 0x00, "Intel Lynx Point (RAID)",	0},
153802df3acSWarner Losh 	{0x8c078086, 0x00, "Intel Lynx Point (RAID)",	0},
154802df3acSWarner Losh 	{0x8c0e8086, 0x00, "Intel Lynx Point (RAID)",	0},
155802df3acSWarner Losh 	{0x8c0f8086, 0x00, "Intel Lynx Point (RAID)",	0},
156e67f3becSAlexander Motin 	{0x8c828086, 0x00, "Intel Wildcat Point",	0},
157e67f3becSAlexander Motin 	{0x8c838086, 0x00, "Intel Wildcat Point",	0},
158e67f3becSAlexander Motin 	{0x8c848086, 0x00, "Intel Wildcat Point (RAID)",	0},
159e67f3becSAlexander Motin 	{0x8c858086, 0x00, "Intel Wildcat Point (RAID)",	0},
160e67f3becSAlexander Motin 	{0x8c868086, 0x00, "Intel Wildcat Point (RAID)",	0},
161e67f3becSAlexander Motin 	{0x8c878086, 0x00, "Intel Wildcat Point (RAID)",	0},
162e67f3becSAlexander Motin 	{0x8c8e8086, 0x00, "Intel Wildcat Point (RAID)",	0},
163e67f3becSAlexander Motin 	{0x8c8f8086, 0x00, "Intel Wildcat Point (RAID)",	0},
164802df3acSWarner Losh 	{0x8d028086, 0x00, "Intel Wellsburg",	0},
165802df3acSWarner Losh 	{0x8d048086, 0x00, "Intel Wellsburg (RAID)",	0},
166802df3acSWarner Losh 	{0x8d068086, 0x00, "Intel Wellsburg (RAID)",	0},
167802df3acSWarner Losh 	{0x8d628086, 0x00, "Intel Wellsburg",	0},
168802df3acSWarner Losh 	{0x8d648086, 0x00, "Intel Wellsburg (RAID)",	0},
169802df3acSWarner Losh 	{0x8d668086, 0x00, "Intel Wellsburg (RAID)",	0},
170802df3acSWarner Losh 	{0x8d6e8086, 0x00, "Intel Wellsburg (RAID)",	0},
171802df3acSWarner Losh 	{0x9c028086, 0x00, "Intel Lynx Point-LP",	0},
172802df3acSWarner Losh 	{0x9c038086, 0x00, "Intel Lynx Point-LP",	0},
173802df3acSWarner Losh 	{0x9c048086, 0x00, "Intel Lynx Point-LP (RAID)",	0},
174802df3acSWarner Losh 	{0x9c058086, 0x00, "Intel Lynx Point-LP (RAID)",	0},
175802df3acSWarner Losh 	{0x9c068086, 0x00, "Intel Lynx Point-LP (RAID)",	0},
176802df3acSWarner Losh 	{0x9c078086, 0x00, "Intel Lynx Point-LP (RAID)",	0},
177802df3acSWarner Losh 	{0x9c0e8086, 0x00, "Intel Lynx Point-LP (RAID)",	0},
178802df3acSWarner Losh 	{0x9c0f8086, 0x00, "Intel Lynx Point-LP (RAID)",	0},
179802df3acSWarner Losh 	{0x23238086, 0x00, "Intel DH89xxCC",	0},
180802df3acSWarner Losh 	{0x2360197b, 0x00, "JMicron JMB360",	0},
181802df3acSWarner Losh 	{0x2361197b, 0x00, "JMicron JMB361",	AHCI_Q_NOFORCE},
182802df3acSWarner Losh 	{0x2362197b, 0x00, "JMicron JMB362",	0},
183802df3acSWarner Losh 	{0x2363197b, 0x00, "JMicron JMB363",	AHCI_Q_NOFORCE},
184802df3acSWarner Losh 	{0x2365197b, 0x00, "JMicron JMB365",	AHCI_Q_NOFORCE},
185802df3acSWarner Losh 	{0x2366197b, 0x00, "JMicron JMB366",	AHCI_Q_NOFORCE},
186802df3acSWarner Losh 	{0x2368197b, 0x00, "JMicron JMB368",	AHCI_Q_NOFORCE},
187802df3acSWarner Losh 	{0x611111ab, 0x00, "Marvell 88SE6111",	AHCI_Q_NOFORCE | AHCI_Q_1CH |
188802df3acSWarner Losh 	    AHCI_Q_EDGEIS},
189802df3acSWarner Losh 	{0x612111ab, 0x00, "Marvell 88SE6121",	AHCI_Q_NOFORCE | AHCI_Q_2CH |
190802df3acSWarner Losh 	    AHCI_Q_EDGEIS | AHCI_Q_NONCQ | AHCI_Q_NOCOUNT},
191802df3acSWarner Losh 	{0x614111ab, 0x00, "Marvell 88SE6141",	AHCI_Q_NOFORCE | AHCI_Q_4CH |
192802df3acSWarner Losh 	    AHCI_Q_EDGEIS | AHCI_Q_NONCQ | AHCI_Q_NOCOUNT},
193802df3acSWarner Losh 	{0x614511ab, 0x00, "Marvell 88SE6145",	AHCI_Q_NOFORCE | AHCI_Q_4CH |
194802df3acSWarner Losh 	    AHCI_Q_EDGEIS | AHCI_Q_NONCQ | AHCI_Q_NOCOUNT},
195c6efb4c4SAlexander Motin 	{0x91201b4b, 0x00, "Marvell 88SE912x",	AHCI_Q_EDGEIS},
196c6efb4c4SAlexander Motin 	{0x91231b4b, 0x11, "Marvell 88SE912x",	AHCI_Q_ALTSIG},
197c6efb4c4SAlexander Motin 	{0x91231b4b, 0x00, "Marvell 88SE912x",	AHCI_Q_EDGEIS|AHCI_Q_SATA2},
198c6efb4c4SAlexander Motin 	{0x91251b4b, 0x00, "Marvell 88SE9125",	0},
199c6efb4c4SAlexander Motin 	{0x91281b4b, 0x00, "Marvell 88SE9128",	AHCI_Q_ALTSIG},
200c6efb4c4SAlexander Motin 	{0x91301b4b, 0x00, "Marvell 88SE9130",  AHCI_Q_ALTSIG},
201c6efb4c4SAlexander Motin 	{0x91721b4b, 0x00, "Marvell 88SE9172",	0},
202c6efb4c4SAlexander Motin 	{0x91821b4b, 0x00, "Marvell 88SE9182",	0},
203c6efb4c4SAlexander Motin 	{0x91831b4b, 0x00, "Marvell 88SS9183",	0},
204c6efb4c4SAlexander Motin 	{0x91a01b4b, 0x00, "Marvell 88SE91Ax",	0},
205c6efb4c4SAlexander Motin 	{0x92151b4b, 0x00, "Marvell 88SE9215",  0},
206c6efb4c4SAlexander Motin 	{0x92201b4b, 0x00, "Marvell 88SE9220",  AHCI_Q_ALTSIG},
207c6efb4c4SAlexander Motin 	{0x92301b4b, 0x00, "Marvell 88SE9230",  AHCI_Q_ALTSIG},
208c6efb4c4SAlexander Motin 	{0x92351b4b, 0x00, "Marvell 88SE9235",  0},
209c6efb4c4SAlexander Motin 	{0x06201103, 0x00, "HighPoint RocketRAID 620",	0},
210c6efb4c4SAlexander Motin 	{0x06201b4b, 0x00, "HighPoint RocketRAID 620",	0},
211c6efb4c4SAlexander Motin 	{0x06221103, 0x00, "HighPoint RocketRAID 622",	0},
212c6efb4c4SAlexander Motin 	{0x06221b4b, 0x00, "HighPoint RocketRAID 622",	0},
213c6efb4c4SAlexander Motin 	{0x06401103, 0x00, "HighPoint RocketRAID 640",	0},
214c6efb4c4SAlexander Motin 	{0x06401b4b, 0x00, "HighPoint RocketRAID 640",	0},
215c6efb4c4SAlexander Motin 	{0x06441103, 0x00, "HighPoint RocketRAID 644",	0},
216c6efb4c4SAlexander Motin 	{0x06441b4b, 0x00, "HighPoint RocketRAID 644",	0},
217c6efb4c4SAlexander Motin 	{0x06411103, 0x00, "HighPoint RocketRAID 640L",	0},
218c6efb4c4SAlexander Motin 	{0x06421103, 0x00, "HighPoint RocketRAID 642L",	0},
219c6efb4c4SAlexander Motin 	{0x06451103, 0x00, "HighPoint RocketRAID 644L",	0},
220802df3acSWarner Losh 	{0x044c10de, 0x00, "NVIDIA MCP65",	AHCI_Q_NOAA},
221802df3acSWarner Losh 	{0x044d10de, 0x00, "NVIDIA MCP65",	AHCI_Q_NOAA},
222802df3acSWarner Losh 	{0x044e10de, 0x00, "NVIDIA MCP65",	AHCI_Q_NOAA},
223802df3acSWarner Losh 	{0x044f10de, 0x00, "NVIDIA MCP65",	AHCI_Q_NOAA},
224802df3acSWarner Losh 	{0x045c10de, 0x00, "NVIDIA MCP65",	AHCI_Q_NOAA},
225802df3acSWarner Losh 	{0x045d10de, 0x00, "NVIDIA MCP65",	AHCI_Q_NOAA},
226802df3acSWarner Losh 	{0x045e10de, 0x00, "NVIDIA MCP65",	AHCI_Q_NOAA},
227802df3acSWarner Losh 	{0x045f10de, 0x00, "NVIDIA MCP65",	AHCI_Q_NOAA},
228802df3acSWarner Losh 	{0x055010de, 0x00, "NVIDIA MCP67",	AHCI_Q_NOAA},
229802df3acSWarner Losh 	{0x055110de, 0x00, "NVIDIA MCP67",	AHCI_Q_NOAA},
230802df3acSWarner Losh 	{0x055210de, 0x00, "NVIDIA MCP67",	AHCI_Q_NOAA},
231802df3acSWarner Losh 	{0x055310de, 0x00, "NVIDIA MCP67",	AHCI_Q_NOAA},
232802df3acSWarner Losh 	{0x055410de, 0x00, "NVIDIA MCP67",	AHCI_Q_NOAA},
233802df3acSWarner Losh 	{0x055510de, 0x00, "NVIDIA MCP67",	AHCI_Q_NOAA},
234802df3acSWarner Losh 	{0x055610de, 0x00, "NVIDIA MCP67",	AHCI_Q_NOAA},
235802df3acSWarner Losh 	{0x055710de, 0x00, "NVIDIA MCP67",	AHCI_Q_NOAA},
236802df3acSWarner Losh 	{0x055810de, 0x00, "NVIDIA MCP67",	AHCI_Q_NOAA},
237802df3acSWarner Losh 	{0x055910de, 0x00, "NVIDIA MCP67",	AHCI_Q_NOAA},
238802df3acSWarner Losh 	{0x055A10de, 0x00, "NVIDIA MCP67",	AHCI_Q_NOAA},
239802df3acSWarner Losh 	{0x055B10de, 0x00, "NVIDIA MCP67",	AHCI_Q_NOAA},
240802df3acSWarner Losh 	{0x058410de, 0x00, "NVIDIA MCP67",	AHCI_Q_NOAA},
241802df3acSWarner Losh 	{0x07f010de, 0x00, "NVIDIA MCP73",	AHCI_Q_NOAA},
242802df3acSWarner Losh 	{0x07f110de, 0x00, "NVIDIA MCP73",	AHCI_Q_NOAA},
243802df3acSWarner Losh 	{0x07f210de, 0x00, "NVIDIA MCP73",	AHCI_Q_NOAA},
244802df3acSWarner Losh 	{0x07f310de, 0x00, "NVIDIA MCP73",	AHCI_Q_NOAA},
245802df3acSWarner Losh 	{0x07f410de, 0x00, "NVIDIA MCP73",	AHCI_Q_NOAA},
246802df3acSWarner Losh 	{0x07f510de, 0x00, "NVIDIA MCP73",	AHCI_Q_NOAA},
247802df3acSWarner Losh 	{0x07f610de, 0x00, "NVIDIA MCP73",	AHCI_Q_NOAA},
248802df3acSWarner Losh 	{0x07f710de, 0x00, "NVIDIA MCP73",	AHCI_Q_NOAA},
249802df3acSWarner Losh 	{0x07f810de, 0x00, "NVIDIA MCP73",	AHCI_Q_NOAA},
250802df3acSWarner Losh 	{0x07f910de, 0x00, "NVIDIA MCP73",	AHCI_Q_NOAA},
251802df3acSWarner Losh 	{0x07fa10de, 0x00, "NVIDIA MCP73",	AHCI_Q_NOAA},
252802df3acSWarner Losh 	{0x07fb10de, 0x00, "NVIDIA MCP73",	AHCI_Q_NOAA},
253802df3acSWarner Losh 	{0x0ad010de, 0x00, "NVIDIA MCP77",	AHCI_Q_NOAA},
254802df3acSWarner Losh 	{0x0ad110de, 0x00, "NVIDIA MCP77",	AHCI_Q_NOAA},
255802df3acSWarner Losh 	{0x0ad210de, 0x00, "NVIDIA MCP77",	AHCI_Q_NOAA},
256802df3acSWarner Losh 	{0x0ad310de, 0x00, "NVIDIA MCP77",	AHCI_Q_NOAA},
257802df3acSWarner Losh 	{0x0ad410de, 0x00, "NVIDIA MCP77",	AHCI_Q_NOAA},
258802df3acSWarner Losh 	{0x0ad510de, 0x00, "NVIDIA MCP77",	AHCI_Q_NOAA},
259802df3acSWarner Losh 	{0x0ad610de, 0x00, "NVIDIA MCP77",	AHCI_Q_NOAA},
260802df3acSWarner Losh 	{0x0ad710de, 0x00, "NVIDIA MCP77",	AHCI_Q_NOAA},
261802df3acSWarner Losh 	{0x0ad810de, 0x00, "NVIDIA MCP77",	AHCI_Q_NOAA},
262802df3acSWarner Losh 	{0x0ad910de, 0x00, "NVIDIA MCP77",	AHCI_Q_NOAA},
263802df3acSWarner Losh 	{0x0ada10de, 0x00, "NVIDIA MCP77",	AHCI_Q_NOAA},
264802df3acSWarner Losh 	{0x0adb10de, 0x00, "NVIDIA MCP77",	AHCI_Q_NOAA},
265802df3acSWarner Losh 	{0x0ab410de, 0x00, "NVIDIA MCP79",	AHCI_Q_NOAA},
266802df3acSWarner Losh 	{0x0ab510de, 0x00, "NVIDIA MCP79",	AHCI_Q_NOAA},
267802df3acSWarner Losh 	{0x0ab610de, 0x00, "NVIDIA MCP79",	AHCI_Q_NOAA},
268802df3acSWarner Losh 	{0x0ab710de, 0x00, "NVIDIA MCP79",	AHCI_Q_NOAA},
269802df3acSWarner Losh 	{0x0ab810de, 0x00, "NVIDIA MCP79",	AHCI_Q_NOAA},
270802df3acSWarner Losh 	{0x0ab910de, 0x00, "NVIDIA MCP79",	AHCI_Q_NOAA},
271802df3acSWarner Losh 	{0x0aba10de, 0x00, "NVIDIA MCP79",	AHCI_Q_NOAA},
272802df3acSWarner Losh 	{0x0abb10de, 0x00, "NVIDIA MCP79",	AHCI_Q_NOAA},
273802df3acSWarner Losh 	{0x0abc10de, 0x00, "NVIDIA MCP79",	AHCI_Q_NOAA},
274802df3acSWarner Losh 	{0x0abd10de, 0x00, "NVIDIA MCP79",	AHCI_Q_NOAA},
275802df3acSWarner Losh 	{0x0abe10de, 0x00, "NVIDIA MCP79",	AHCI_Q_NOAA},
276802df3acSWarner Losh 	{0x0abf10de, 0x00, "NVIDIA MCP79",	AHCI_Q_NOAA},
277802df3acSWarner Losh 	{0x0d8410de, 0x00, "NVIDIA MCP89",	AHCI_Q_NOAA},
278802df3acSWarner Losh 	{0x0d8510de, 0x00, "NVIDIA MCP89",	AHCI_Q_NOFORCE|AHCI_Q_NOAA},
279802df3acSWarner Losh 	{0x0d8610de, 0x00, "NVIDIA MCP89",	AHCI_Q_NOAA},
280802df3acSWarner Losh 	{0x0d8710de, 0x00, "NVIDIA MCP89",	AHCI_Q_NOAA},
281802df3acSWarner Losh 	{0x0d8810de, 0x00, "NVIDIA MCP89",	AHCI_Q_NOAA},
282802df3acSWarner Losh 	{0x0d8910de, 0x00, "NVIDIA MCP89",	AHCI_Q_NOAA},
283802df3acSWarner Losh 	{0x0d8a10de, 0x00, "NVIDIA MCP89",	AHCI_Q_NOAA},
284802df3acSWarner Losh 	{0x0d8b10de, 0x00, "NVIDIA MCP89",	AHCI_Q_NOAA},
285802df3acSWarner Losh 	{0x0d8c10de, 0x00, "NVIDIA MCP89",	AHCI_Q_NOAA},
286802df3acSWarner Losh 	{0x0d8d10de, 0x00, "NVIDIA MCP89",	AHCI_Q_NOAA},
287802df3acSWarner Losh 	{0x0d8e10de, 0x00, "NVIDIA MCP89",	AHCI_Q_NOAA},
288802df3acSWarner Losh 	{0x0d8f10de, 0x00, "NVIDIA MCP89",	AHCI_Q_NOAA},
28903d89c94SAlexander Motin 	{0x3781105a, 0x00, "Promise TX8660",	0},
290802df3acSWarner Losh 	{0x33491106, 0x00, "VIA VT8251",	AHCI_Q_NOPMP|AHCI_Q_NONCQ},
291802df3acSWarner Losh 	{0x62871106, 0x00, "VIA VT8251",	AHCI_Q_NOPMP|AHCI_Q_NONCQ},
292802df3acSWarner Losh 	{0x11841039, 0x00, "SiS 966",		0},
293802df3acSWarner Losh 	{0x11851039, 0x00, "SiS 968",		0},
294802df3acSWarner Losh 	{0x01861039, 0x00, "SiS 968",		0},
295378c532cSKonstantin Belousov 	{0xa01c177d, 0x00, "ThunderX SATA",	AHCI_Q_ABAR0},
296802df3acSWarner Losh 	{0x00000000, 0x00, NULL,		0}
297802df3acSWarner Losh };
298802df3acSWarner Losh 
299802df3acSWarner Losh static int
300802df3acSWarner Losh ahci_pci_ctlr_reset(device_t dev)
301802df3acSWarner Losh {
302802df3acSWarner Losh 
303802df3acSWarner Losh 	if (pci_read_config(dev, PCIR_DEVVENDOR, 4) == 0x28298086 &&
304802df3acSWarner Losh 	    (pci_read_config(dev, 0x92, 1) & 0xfe) == 0x04)
305802df3acSWarner Losh 		pci_write_config(dev, 0x92, 0x01, 1);
306802df3acSWarner Losh 	return ahci_ctlr_reset(dev);
307802df3acSWarner Losh }
308802df3acSWarner Losh 
309802df3acSWarner Losh static int
310802df3acSWarner Losh ahci_probe(device_t dev)
311802df3acSWarner Losh {
312802df3acSWarner Losh 	char buf[64];
313802df3acSWarner Losh 	int i, valid = 0;
314802df3acSWarner Losh 	uint32_t devid = pci_get_devid(dev);
315802df3acSWarner Losh 	uint8_t revid = pci_get_revid(dev);
316802df3acSWarner Losh 
317802df3acSWarner Losh 	/*
318802df3acSWarner Losh 	 * Ensure it is not a PCI bridge (some vendors use
319802df3acSWarner Losh 	 * the same PID and VID in PCI bridge and AHCI cards).
320802df3acSWarner Losh 	 */
321802df3acSWarner Losh 	if (pci_get_class(dev) == PCIC_BRIDGE)
322802df3acSWarner Losh 		return (ENXIO);
323802df3acSWarner Losh 
324802df3acSWarner Losh 	/* Is this a possible AHCI candidate? */
325802df3acSWarner Losh 	if (pci_get_class(dev) == PCIC_STORAGE &&
326802df3acSWarner Losh 	    pci_get_subclass(dev) == PCIS_STORAGE_SATA &&
327802df3acSWarner Losh 	    pci_get_progif(dev) == PCIP_STORAGE_SATA_AHCI_1_0)
328802df3acSWarner Losh 		valid = 1;
329802df3acSWarner Losh 	/* Is this a known AHCI chip? */
330802df3acSWarner Losh 	for (i = 0; ahci_ids[i].id != 0; i++) {
331802df3acSWarner Losh 		if (ahci_ids[i].id == devid &&
332802df3acSWarner Losh 		    ahci_ids[i].rev <= revid &&
333802df3acSWarner Losh 		    (valid || (force_ahci == 1 &&
334802df3acSWarner Losh 		     !(ahci_ids[i].quirks & AHCI_Q_NOFORCE)))) {
335802df3acSWarner Losh 			/* Do not attach JMicrons with single PCI function. */
336802df3acSWarner Losh 			if (pci_get_vendor(dev) == 0x197b &&
337802df3acSWarner Losh 			    (pci_read_config(dev, 0xdf, 1) & 0x40) == 0)
338802df3acSWarner Losh 				return (ENXIO);
339802df3acSWarner Losh 			snprintf(buf, sizeof(buf), "%s AHCI SATA controller",
340802df3acSWarner Losh 			    ahci_ids[i].name);
341802df3acSWarner Losh 			device_set_desc_copy(dev, buf);
342802df3acSWarner Losh 			return (BUS_PROBE_VENDOR);
343802df3acSWarner Losh 		}
344802df3acSWarner Losh 	}
345802df3acSWarner Losh 	if (!valid)
346802df3acSWarner Losh 		return (ENXIO);
347802df3acSWarner Losh 	device_set_desc_copy(dev, "AHCI SATA controller");
348802df3acSWarner Losh 	return (BUS_PROBE_VENDOR);
349802df3acSWarner Losh }
350802df3acSWarner Losh 
351802df3acSWarner Losh static int
352802df3acSWarner Losh ahci_ata_probe(device_t dev)
353802df3acSWarner Losh {
354802df3acSWarner Losh 	char buf[64];
355802df3acSWarner Losh 	int i;
356802df3acSWarner Losh 	uint32_t devid = pci_get_devid(dev);
357802df3acSWarner Losh 	uint8_t revid = pci_get_revid(dev);
358802df3acSWarner Losh 
359802df3acSWarner Losh 	if ((intptr_t)device_get_ivars(dev) >= 0)
360802df3acSWarner Losh 		return (ENXIO);
361802df3acSWarner Losh 	/* Is this a known AHCI chip? */
362802df3acSWarner Losh 	for (i = 0; ahci_ids[i].id != 0; i++) {
363802df3acSWarner Losh 		if (ahci_ids[i].id == devid &&
364802df3acSWarner Losh 		    ahci_ids[i].rev <= revid) {
365802df3acSWarner Losh 			snprintf(buf, sizeof(buf), "%s AHCI SATA controller",
366802df3acSWarner Losh 			    ahci_ids[i].name);
367802df3acSWarner Losh 			device_set_desc_copy(dev, buf);
368802df3acSWarner Losh 			return (BUS_PROBE_VENDOR);
369802df3acSWarner Losh 		}
370802df3acSWarner Losh 	}
371802df3acSWarner Losh 	device_set_desc_copy(dev, "AHCI SATA controller");
372802df3acSWarner Losh 	return (BUS_PROBE_VENDOR);
373802df3acSWarner Losh }
374802df3acSWarner Losh 
375802df3acSWarner Losh static int
376802df3acSWarner Losh ahci_pci_attach(device_t dev)
377802df3acSWarner Losh {
378802df3acSWarner Losh 	struct ahci_controller *ctlr = device_get_softc(dev);
379802df3acSWarner Losh 	int	error, i;
380802df3acSWarner Losh 	uint32_t devid = pci_get_devid(dev);
381802df3acSWarner Losh 	uint8_t revid = pci_get_revid(dev);
382802df3acSWarner Losh 
383802df3acSWarner Losh 	i = 0;
384802df3acSWarner Losh 	while (ahci_ids[i].id != 0 &&
385802df3acSWarner Losh 	    (ahci_ids[i].id != devid ||
386802df3acSWarner Losh 	     ahci_ids[i].rev > revid))
387802df3acSWarner Losh 		i++;
388802df3acSWarner Losh 	ctlr->quirks = ahci_ids[i].quirks;
389802df3acSWarner Losh 	/* Limit speed for my onboard JMicron external port.
390802df3acSWarner Losh 	 * It is not eSATA really, limit to SATA 1 */
391802df3acSWarner Losh 	if (pci_get_devid(dev) == 0x2363197b &&
392802df3acSWarner Losh 	    pci_get_subvendor(dev) == 0x1043 &&
393802df3acSWarner Losh 	    pci_get_subdevice(dev) == 0x81e4)
394802df3acSWarner Losh 		ctlr->quirks |= AHCI_Q_SATA1_UNIT0;
395802df3acSWarner Losh 	ctlr->vendorid = pci_get_vendor(dev);
396802df3acSWarner Losh 	ctlr->deviceid = pci_get_device(dev);
397802df3acSWarner Losh 	ctlr->subvendorid = pci_get_subvendor(dev);
398802df3acSWarner Losh 	ctlr->subdeviceid = pci_get_subdevice(dev);
399378c532cSKonstantin Belousov 
400378c532cSKonstantin Belousov 	/* Default AHCI Base Address is BAR(5), Cavium uses BAR(0) */
401378c532cSKonstantin Belousov 	if (ctlr->quirks & AHCI_Q_ABAR0)
402378c532cSKonstantin Belousov 		ctlr->r_rid = PCIR_BAR(0);
403378c532cSKonstantin Belousov 	else
404802df3acSWarner Losh 		ctlr->r_rid = PCIR_BAR(5);
405802df3acSWarner Losh 	if (!(ctlr->r_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
406802df3acSWarner Losh 	    &ctlr->r_rid, RF_ACTIVE)))
407802df3acSWarner Losh 		return ENXIO;
408802df3acSWarner Losh 	pci_enable_busmaster(dev);
409802df3acSWarner Losh 	/* Reset controller */
410802df3acSWarner Losh 	if ((error = ahci_pci_ctlr_reset(dev)) != 0) {
411802df3acSWarner Losh 		bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem);
412802df3acSWarner Losh 		return (error);
413802df3acSWarner Losh 	};
414802df3acSWarner Losh 
415802df3acSWarner Losh 	/* Setup interrupts. */
416802df3acSWarner Losh 
417802df3acSWarner Losh 	/* Setup MSI register parameters */
418802df3acSWarner Losh 	/* Process hints. */
419802df3acSWarner Losh 	if (ctlr->quirks & AHCI_Q_NOMSI)
420802df3acSWarner Losh 		ctlr->msi = 0;
421*b1877a0bSSteven Hartland 	else if (ctlr->quirks & AHCI_Q_1MSI)
422*b1877a0bSSteven Hartland 		ctlr->msi = 1;
423*b1877a0bSSteven Hartland 	else
424*b1877a0bSSteven Hartland 		ctlr->msi = 2;
425802df3acSWarner Losh 	resource_int_value(device_get_name(dev),
426802df3acSWarner Losh 	    device_get_unit(dev), "msi", &ctlr->msi);
427802df3acSWarner Losh 	ctlr->numirqs = 1;
428802df3acSWarner Losh 	if (ctlr->msi < 0)
429802df3acSWarner Losh 		ctlr->msi = 0;
430802df3acSWarner Losh 	else if (ctlr->msi == 1)
431802df3acSWarner Losh 		ctlr->msi = min(1, pci_msi_count(dev));
432802df3acSWarner Losh 	else if (ctlr->msi > 1) {
433802df3acSWarner Losh 		ctlr->msi = 2;
434802df3acSWarner Losh 		ctlr->numirqs = pci_msi_count(dev);
435802df3acSWarner Losh 	}
436802df3acSWarner Losh 	/* Allocate MSI if needed/present. */
437802df3acSWarner Losh 	if (ctlr->msi && pci_alloc_msi(dev, &ctlr->numirqs) != 0) {
438802df3acSWarner Losh 		ctlr->msi = 0;
439802df3acSWarner Losh 		ctlr->numirqs = 1;
440802df3acSWarner Losh 	}
441802df3acSWarner Losh 
442802df3acSWarner Losh 	error = ahci_attach(dev);
443802df3acSWarner Losh 	if (error != 0)
444802df3acSWarner Losh 		if (ctlr->msi)
445802df3acSWarner Losh 			pci_release_msi(dev);
446802df3acSWarner Losh 	return error;
447802df3acSWarner Losh }
448802df3acSWarner Losh 
449802df3acSWarner Losh static int
450802df3acSWarner Losh ahci_pci_detach(device_t dev)
451802df3acSWarner Losh {
452802df3acSWarner Losh 
453802df3acSWarner Losh 	ahci_detach(dev);
454802df3acSWarner Losh 	pci_release_msi(dev);
455802df3acSWarner Losh 	return (0);
456802df3acSWarner Losh }
457802df3acSWarner Losh 
458802df3acSWarner Losh static int
459802df3acSWarner Losh ahci_pci_suspend(device_t dev)
460802df3acSWarner Losh {
461802df3acSWarner Losh 	struct ahci_controller *ctlr = device_get_softc(dev);
462802df3acSWarner Losh 
463802df3acSWarner Losh 	bus_generic_suspend(dev);
464802df3acSWarner Losh 	/* Disable interupts, so the state change(s) doesn't trigger */
465802df3acSWarner Losh 	ATA_OUTL(ctlr->r_mem, AHCI_GHC,
466802df3acSWarner Losh 	     ATA_INL(ctlr->r_mem, AHCI_GHC) & (~AHCI_GHC_IE));
467802df3acSWarner Losh 	return 0;
468802df3acSWarner Losh }
469802df3acSWarner Losh 
470802df3acSWarner Losh static int
471802df3acSWarner Losh ahci_pci_resume(device_t dev)
472802df3acSWarner Losh {
473802df3acSWarner Losh 	int res;
474802df3acSWarner Losh 
475802df3acSWarner Losh 	if ((res = ahci_pci_ctlr_reset(dev)) != 0)
476802df3acSWarner Losh 		return (res);
477802df3acSWarner Losh 	ahci_ctlr_setup(dev);
478802df3acSWarner Losh 	return (bus_generic_resume(dev));
479802df3acSWarner Losh }
480802df3acSWarner Losh 
481802df3acSWarner Losh devclass_t ahci_devclass;
482802df3acSWarner Losh static device_method_t ahci_methods[] = {
483802df3acSWarner Losh 	DEVMETHOD(device_probe,     ahci_probe),
484802df3acSWarner Losh 	DEVMETHOD(device_attach,    ahci_pci_attach),
485802df3acSWarner Losh 	DEVMETHOD(device_detach,    ahci_pci_detach),
486802df3acSWarner Losh 	DEVMETHOD(device_suspend,   ahci_pci_suspend),
487802df3acSWarner Losh 	DEVMETHOD(device_resume,    ahci_pci_resume),
488802df3acSWarner Losh 	DEVMETHOD(bus_print_child,  ahci_print_child),
489802df3acSWarner Losh 	DEVMETHOD(bus_alloc_resource,       ahci_alloc_resource),
490802df3acSWarner Losh 	DEVMETHOD(bus_release_resource,     ahci_release_resource),
491802df3acSWarner Losh 	DEVMETHOD(bus_setup_intr,   ahci_setup_intr),
492802df3acSWarner Losh 	DEVMETHOD(bus_teardown_intr,ahci_teardown_intr),
493802df3acSWarner Losh 	DEVMETHOD(bus_child_location_str, ahci_child_location_str),
494802df3acSWarner Losh 	DEVMETHOD(bus_get_dma_tag,  ahci_get_dma_tag),
49521190895SMarius Strobl 	DEVMETHOD_END
496802df3acSWarner Losh };
497802df3acSWarner Losh static driver_t ahci_driver = {
498802df3acSWarner Losh         "ahci",
499802df3acSWarner Losh         ahci_methods,
500802df3acSWarner Losh         sizeof(struct ahci_controller)
501802df3acSWarner Losh };
50221190895SMarius Strobl DRIVER_MODULE(ahci, pci, ahci_driver, ahci_devclass, NULL, NULL);
503802df3acSWarner Losh static device_method_t ahci_ata_methods[] = {
504802df3acSWarner Losh 	DEVMETHOD(device_probe,     ahci_ata_probe),
505802df3acSWarner Losh 	DEVMETHOD(device_attach,    ahci_pci_attach),
506802df3acSWarner Losh 	DEVMETHOD(device_detach,    ahci_pci_detach),
507802df3acSWarner Losh 	DEVMETHOD(device_suspend,   ahci_pci_suspend),
508802df3acSWarner Losh 	DEVMETHOD(device_resume,    ahci_pci_resume),
509802df3acSWarner Losh 	DEVMETHOD(bus_print_child,  ahci_print_child),
510802df3acSWarner Losh 	DEVMETHOD(bus_alloc_resource,       ahci_alloc_resource),
511802df3acSWarner Losh 	DEVMETHOD(bus_release_resource,     ahci_release_resource),
512802df3acSWarner Losh 	DEVMETHOD(bus_setup_intr,   ahci_setup_intr),
513802df3acSWarner Losh 	DEVMETHOD(bus_teardown_intr,ahci_teardown_intr),
514802df3acSWarner Losh 	DEVMETHOD(bus_child_location_str, ahci_child_location_str),
51521190895SMarius Strobl 	DEVMETHOD_END
516802df3acSWarner Losh };
517802df3acSWarner Losh static driver_t ahci_ata_driver = {
518802df3acSWarner Losh         "ahci",
519802df3acSWarner Losh         ahci_ata_methods,
520802df3acSWarner Losh         sizeof(struct ahci_controller)
521802df3acSWarner Losh };
52221190895SMarius Strobl DRIVER_MODULE(ahci, atapci, ahci_ata_driver, ahci_devclass, NULL, NULL);
523