ahci_generic.c revision 277100
1/*-
2 * Copyright (c) 2009-2012 Alexander Motin <mav@FreeBSD.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer,
10 *    without modification, immediately at the beginning of the file.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: head/sys/dev/ahci/ahci_pci.c 277100 2015-01-13 00:11:56Z kib $");
29
30#include <sys/param.h>
31#include <sys/module.h>
32#include <sys/systm.h>
33#include <sys/kernel.h>
34#include <sys/bus.h>
35#include <sys/conf.h>
36#include <sys/endian.h>
37#include <sys/malloc.h>
38#include <sys/lock.h>
39#include <sys/mutex.h>
40#include <machine/stdarg.h>
41#include <machine/resource.h>
42#include <machine/bus.h>
43#include <sys/rman.h>
44#include <dev/pci/pcivar.h>
45#include <dev/pci/pcireg.h>
46#include "ahci.h"
47
48static int force_ahci = 1;
49TUNABLE_INT("hw.ahci.force", &force_ahci);
50
51static const struct {
52	uint32_t	id;
53	uint8_t		rev;
54	const char	*name;
55	int		quirks;
56} ahci_ids[] = {
57	{0x43801002, 0x00, "AMD SB600",
58		AHCI_Q_NOMSI | AHCI_Q_ATI_PMP_BUG | AHCI_Q_MAXIO_64K},
59	{0x43901002, 0x00, "AMD SB7x0/SB8x0/SB9x0",	AHCI_Q_ATI_PMP_BUG},
60	{0x43911002, 0x00, "AMD SB7x0/SB8x0/SB9x0",	AHCI_Q_ATI_PMP_BUG},
61	{0x43921002, 0x00, "AMD SB7x0/SB8x0/SB9x0",	AHCI_Q_ATI_PMP_BUG},
62	{0x43931002, 0x00, "AMD SB7x0/SB8x0/SB9x0",	AHCI_Q_ATI_PMP_BUG},
63	{0x43941002, 0x00, "AMD SB7x0/SB8x0/SB9x0",	AHCI_Q_ATI_PMP_BUG},
64	/* Not sure SB8x0/SB9x0 needs this quirk. Be conservative though */
65	{0x43951002, 0x00, "AMD SB8x0/SB9x0",	AHCI_Q_ATI_PMP_BUG},
66	{0x78001022, 0x00, "AMD Hudson-2",	0},
67	{0x78011022, 0x00, "AMD Hudson-2",	0},
68	{0x78021022, 0x00, "AMD Hudson-2",	0},
69	{0x78031022, 0x00, "AMD Hudson-2",	0},
70	{0x78041022, 0x00, "AMD Hudson-2",	0},
71	{0x06111b21, 0x00, "ASMedia ASM2106",	0},
72	{0x06121b21, 0x00, "ASMedia ASM1061",	0},
73	{0x26528086, 0x00, "Intel ICH6",	AHCI_Q_NOFORCE},
74	{0x26538086, 0x00, "Intel ICH6M",	AHCI_Q_NOFORCE},
75	{0x26818086, 0x00, "Intel ESB2",	0},
76	{0x26828086, 0x00, "Intel ESB2",	0},
77	{0x26838086, 0x00, "Intel ESB2",	0},
78	{0x27c18086, 0x00, "Intel ICH7",	0},
79	{0x27c38086, 0x00, "Intel ICH7",	0},
80	{0x27c58086, 0x00, "Intel ICH7M",	0},
81	{0x27c68086, 0x00, "Intel ICH7M",	0},
82	{0x28218086, 0x00, "Intel ICH8",	0},
83	{0x28228086, 0x00, "Intel ICH8",	0},
84	{0x28248086, 0x00, "Intel ICH8",	0},
85	{0x28298086, 0x00, "Intel ICH8M",	0},
86	{0x282a8086, 0x00, "Intel ICH8M",	0},
87	{0x29228086, 0x00, "Intel ICH9",	0},
88	{0x29238086, 0x00, "Intel ICH9",	0},
89	{0x29248086, 0x00, "Intel ICH9",	0},
90	{0x29258086, 0x00, "Intel ICH9",	0},
91	{0x29278086, 0x00, "Intel ICH9",	0},
92	{0x29298086, 0x00, "Intel ICH9M",	0},
93	{0x292a8086, 0x00, "Intel ICH9M",	0},
94	{0x292b8086, 0x00, "Intel ICH9M",	0},
95	{0x292c8086, 0x00, "Intel ICH9M",	0},
96	{0x292f8086, 0x00, "Intel ICH9M",	0},
97	{0x294d8086, 0x00, "Intel ICH9",	0},
98	{0x294e8086, 0x00, "Intel ICH9M",	0},
99	{0x3a058086, 0x00, "Intel ICH10",	0},
100	{0x3a228086, 0x00, "Intel ICH10",	0},
101	{0x3a258086, 0x00, "Intel ICH10",	0},
102	{0x3b228086, 0x00, "Intel 5 Series/3400 Series",	0},
103	{0x3b238086, 0x00, "Intel 5 Series/3400 Series",	0},
104	{0x3b258086, 0x00, "Intel 5 Series/3400 Series",	0},
105	{0x3b298086, 0x00, "Intel 5 Series/3400 Series",	0},
106	{0x3b2c8086, 0x00, "Intel 5 Series/3400 Series",	0},
107	{0x3b2f8086, 0x00, "Intel 5 Series/3400 Series",	0},
108	{0x1c028086, 0x00, "Intel Cougar Point",	0},
109	{0x1c038086, 0x00, "Intel Cougar Point",	0},
110	{0x1c048086, 0x00, "Intel Cougar Point",	0},
111	{0x1c058086, 0x00, "Intel Cougar Point",	0},
112	{0x1d028086, 0x00, "Intel Patsburg",	0},
113	{0x1d048086, 0x00, "Intel Patsburg",	0},
114	{0x1d068086, 0x00, "Intel Patsburg",	0},
115	{0x28268086, 0x00, "Intel Patsburg (RAID)",	0},
116	{0x1e028086, 0x00, "Intel Panther Point",	0},
117	{0x1e038086, 0x00, "Intel Panther Point",	0},
118	{0x1e048086, 0x00, "Intel Panther Point (RAID)",	0},
119	{0x1e058086, 0x00, "Intel Panther Point (RAID)",	0},
120	{0x1e068086, 0x00, "Intel Panther Point (RAID)",	0},
121	{0x1e078086, 0x00, "Intel Panther Point (RAID)",	0},
122	{0x1e0e8086, 0x00, "Intel Panther Point (RAID)",	0},
123	{0x1e0f8086, 0x00, "Intel Panther Point (RAID)",	0},
124	{0x1f228086, 0x00, "Intel Avoton",	0},
125	{0x1f238086, 0x00, "Intel Avoton",	0},
126	{0x1f248086, 0x00, "Intel Avoton (RAID)",	0},
127	{0x1f258086, 0x00, "Intel Avoton (RAID)",	0},
128	{0x1f268086, 0x00, "Intel Avoton (RAID)",	0},
129	{0x1f278086, 0x00, "Intel Avoton (RAID)",	0},
130	{0x1f2e8086, 0x00, "Intel Avoton (RAID)",	0},
131	{0x1f2f8086, 0x00, "Intel Avoton (RAID)",	0},
132	{0x1f328086, 0x00, "Intel Avoton",	0},
133	{0x1f338086, 0x00, "Intel Avoton",	0},
134	{0x1f348086, 0x00, "Intel Avoton (RAID)",	0},
135	{0x1f358086, 0x00, "Intel Avoton (RAID)",	0},
136	{0x1f368086, 0x00, "Intel Avoton (RAID)",	0},
137	{0x1f378086, 0x00, "Intel Avoton (RAID)",	0},
138	{0x1f3e8086, 0x00, "Intel Avoton (RAID)",	0},
139	{0x1f3f8086, 0x00, "Intel Avoton (RAID)",	0},
140	{0x23a38086, 0x00, "Intel Coleto Creek",        0},
141	{0x28238086, 0x00, "Intel Wellsburg (RAID)",	0},
142	{0x28278086, 0x00, "Intel Wellsburg (RAID)",	0},
143	{0x8c028086, 0x00, "Intel Lynx Point",	0},
144	{0x8c038086, 0x00, "Intel Lynx Point",	0},
145	{0x8c048086, 0x00, "Intel Lynx Point (RAID)",	0},
146	{0x8c058086, 0x00, "Intel Lynx Point (RAID)",	0},
147	{0x8c068086, 0x00, "Intel Lynx Point (RAID)",	0},
148	{0x8c078086, 0x00, "Intel Lynx Point (RAID)",	0},
149	{0x8c0e8086, 0x00, "Intel Lynx Point (RAID)",	0},
150	{0x8c0f8086, 0x00, "Intel Lynx Point (RAID)",	0},
151	{0x8c828086, 0x00, "Intel Wildcat Point",	0},
152	{0x8c838086, 0x00, "Intel Wildcat Point",	0},
153	{0x8c848086, 0x00, "Intel Wildcat Point (RAID)",	0},
154	{0x8c858086, 0x00, "Intel Wildcat Point (RAID)",	0},
155	{0x8c868086, 0x00, "Intel Wildcat Point (RAID)",	0},
156	{0x8c878086, 0x00, "Intel Wildcat Point (RAID)",	0},
157	{0x8c8e8086, 0x00, "Intel Wildcat Point (RAID)",	0},
158	{0x8c8f8086, 0x00, "Intel Wildcat Point (RAID)",	0},
159	{0x8d028086, 0x00, "Intel Wellsburg",	0},
160	{0x8d048086, 0x00, "Intel Wellsburg (RAID)",	0},
161	{0x8d068086, 0x00, "Intel Wellsburg (RAID)",	0},
162	{0x8d628086, 0x00, "Intel Wellsburg",	0},
163	{0x8d648086, 0x00, "Intel Wellsburg (RAID)",	0},
164	{0x8d668086, 0x00, "Intel Wellsburg (RAID)",	0},
165	{0x8d6e8086, 0x00, "Intel Wellsburg (RAID)",	0},
166	{0x9c028086, 0x00, "Intel Lynx Point-LP",	0},
167	{0x9c038086, 0x00, "Intel Lynx Point-LP",	0},
168	{0x9c048086, 0x00, "Intel Lynx Point-LP (RAID)",	0},
169	{0x9c058086, 0x00, "Intel Lynx Point-LP (RAID)",	0},
170	{0x9c068086, 0x00, "Intel Lynx Point-LP (RAID)",	0},
171	{0x9c078086, 0x00, "Intel Lynx Point-LP (RAID)",	0},
172	{0x9c0e8086, 0x00, "Intel Lynx Point-LP (RAID)",	0},
173	{0x9c0f8086, 0x00, "Intel Lynx Point-LP (RAID)",	0},
174	{0x23238086, 0x00, "Intel DH89xxCC",	0},
175	{0x2360197b, 0x00, "JMicron JMB360",	0},
176	{0x2361197b, 0x00, "JMicron JMB361",	AHCI_Q_NOFORCE},
177	{0x2362197b, 0x00, "JMicron JMB362",	0},
178	{0x2363197b, 0x00, "JMicron JMB363",	AHCI_Q_NOFORCE},
179	{0x2365197b, 0x00, "JMicron JMB365",	AHCI_Q_NOFORCE},
180	{0x2366197b, 0x00, "JMicron JMB366",	AHCI_Q_NOFORCE},
181	{0x2368197b, 0x00, "JMicron JMB368",	AHCI_Q_NOFORCE},
182	{0x611111ab, 0x00, "Marvell 88SE6111",	AHCI_Q_NOFORCE | AHCI_Q_1CH |
183	    AHCI_Q_EDGEIS},
184	{0x612111ab, 0x00, "Marvell 88SE6121",	AHCI_Q_NOFORCE | AHCI_Q_2CH |
185	    AHCI_Q_EDGEIS | AHCI_Q_NONCQ | AHCI_Q_NOCOUNT},
186	{0x614111ab, 0x00, "Marvell 88SE6141",	AHCI_Q_NOFORCE | AHCI_Q_4CH |
187	    AHCI_Q_EDGEIS | AHCI_Q_NONCQ | AHCI_Q_NOCOUNT},
188	{0x614511ab, 0x00, "Marvell 88SE6145",	AHCI_Q_NOFORCE | AHCI_Q_4CH |
189	    AHCI_Q_EDGEIS | AHCI_Q_NONCQ | AHCI_Q_NOCOUNT},
190	{0x91201b4b, 0x00, "Marvell 88SE912x",	AHCI_Q_EDGEIS},
191	{0x91231b4b, 0x11, "Marvell 88SE912x",	AHCI_Q_ALTSIG},
192	{0x91231b4b, 0x00, "Marvell 88SE912x",	AHCI_Q_EDGEIS|AHCI_Q_SATA2},
193	{0x91251b4b, 0x00, "Marvell 88SE9125",	0},
194	{0x91281b4b, 0x00, "Marvell 88SE9128",	AHCI_Q_ALTSIG},
195	{0x91301b4b, 0x00, "Marvell 88SE9130",  AHCI_Q_ALTSIG},
196	{0x91721b4b, 0x00, "Marvell 88SE9172",	0},
197	{0x91821b4b, 0x00, "Marvell 88SE9182",	0},
198	{0x91831b4b, 0x00, "Marvell 88SS9183",	0},
199	{0x91a01b4b, 0x00, "Marvell 88SE91Ax",	0},
200	{0x92151b4b, 0x00, "Marvell 88SE9215",  0},
201	{0x92201b4b, 0x00, "Marvell 88SE9220",  AHCI_Q_ALTSIG},
202	{0x92301b4b, 0x00, "Marvell 88SE9230",  AHCI_Q_ALTSIG},
203	{0x92351b4b, 0x00, "Marvell 88SE9235",  0},
204	{0x06201103, 0x00, "HighPoint RocketRAID 620",	0},
205	{0x06201b4b, 0x00, "HighPoint RocketRAID 620",	0},
206	{0x06221103, 0x00, "HighPoint RocketRAID 622",	0},
207	{0x06221b4b, 0x00, "HighPoint RocketRAID 622",	0},
208	{0x06401103, 0x00, "HighPoint RocketRAID 640",	0},
209	{0x06401b4b, 0x00, "HighPoint RocketRAID 640",	0},
210	{0x06441103, 0x00, "HighPoint RocketRAID 644",	0},
211	{0x06441b4b, 0x00, "HighPoint RocketRAID 644",	0},
212	{0x06411103, 0x00, "HighPoint RocketRAID 640L",	0},
213	{0x06421103, 0x00, "HighPoint RocketRAID 642L",	0},
214	{0x06451103, 0x00, "HighPoint RocketRAID 644L",	0},
215	{0x044c10de, 0x00, "NVIDIA MCP65",	AHCI_Q_NOAA},
216	{0x044d10de, 0x00, "NVIDIA MCP65",	AHCI_Q_NOAA},
217	{0x044e10de, 0x00, "NVIDIA MCP65",	AHCI_Q_NOAA},
218	{0x044f10de, 0x00, "NVIDIA MCP65",	AHCI_Q_NOAA},
219	{0x045c10de, 0x00, "NVIDIA MCP65",	AHCI_Q_NOAA},
220	{0x045d10de, 0x00, "NVIDIA MCP65",	AHCI_Q_NOAA},
221	{0x045e10de, 0x00, "NVIDIA MCP65",	AHCI_Q_NOAA},
222	{0x045f10de, 0x00, "NVIDIA MCP65",	AHCI_Q_NOAA},
223	{0x055010de, 0x00, "NVIDIA MCP67",	AHCI_Q_NOAA},
224	{0x055110de, 0x00, "NVIDIA MCP67",	AHCI_Q_NOAA},
225	{0x055210de, 0x00, "NVIDIA MCP67",	AHCI_Q_NOAA},
226	{0x055310de, 0x00, "NVIDIA MCP67",	AHCI_Q_NOAA},
227	{0x055410de, 0x00, "NVIDIA MCP67",	AHCI_Q_NOAA},
228	{0x055510de, 0x00, "NVIDIA MCP67",	AHCI_Q_NOAA},
229	{0x055610de, 0x00, "NVIDIA MCP67",	AHCI_Q_NOAA},
230	{0x055710de, 0x00, "NVIDIA MCP67",	AHCI_Q_NOAA},
231	{0x055810de, 0x00, "NVIDIA MCP67",	AHCI_Q_NOAA},
232	{0x055910de, 0x00, "NVIDIA MCP67",	AHCI_Q_NOAA},
233	{0x055A10de, 0x00, "NVIDIA MCP67",	AHCI_Q_NOAA},
234	{0x055B10de, 0x00, "NVIDIA MCP67",	AHCI_Q_NOAA},
235	{0x058410de, 0x00, "NVIDIA MCP67",	AHCI_Q_NOAA},
236	{0x07f010de, 0x00, "NVIDIA MCP73",	AHCI_Q_NOAA},
237	{0x07f110de, 0x00, "NVIDIA MCP73",	AHCI_Q_NOAA},
238	{0x07f210de, 0x00, "NVIDIA MCP73",	AHCI_Q_NOAA},
239	{0x07f310de, 0x00, "NVIDIA MCP73",	AHCI_Q_NOAA},
240	{0x07f410de, 0x00, "NVIDIA MCP73",	AHCI_Q_NOAA},
241	{0x07f510de, 0x00, "NVIDIA MCP73",	AHCI_Q_NOAA},
242	{0x07f610de, 0x00, "NVIDIA MCP73",	AHCI_Q_NOAA},
243	{0x07f710de, 0x00, "NVIDIA MCP73",	AHCI_Q_NOAA},
244	{0x07f810de, 0x00, "NVIDIA MCP73",	AHCI_Q_NOAA},
245	{0x07f910de, 0x00, "NVIDIA MCP73",	AHCI_Q_NOAA},
246	{0x07fa10de, 0x00, "NVIDIA MCP73",	AHCI_Q_NOAA},
247	{0x07fb10de, 0x00, "NVIDIA MCP73",	AHCI_Q_NOAA},
248	{0x0ad010de, 0x00, "NVIDIA MCP77",	AHCI_Q_NOAA},
249	{0x0ad110de, 0x00, "NVIDIA MCP77",	AHCI_Q_NOAA},
250	{0x0ad210de, 0x00, "NVIDIA MCP77",	AHCI_Q_NOAA},
251	{0x0ad310de, 0x00, "NVIDIA MCP77",	AHCI_Q_NOAA},
252	{0x0ad410de, 0x00, "NVIDIA MCP77",	AHCI_Q_NOAA},
253	{0x0ad510de, 0x00, "NVIDIA MCP77",	AHCI_Q_NOAA},
254	{0x0ad610de, 0x00, "NVIDIA MCP77",	AHCI_Q_NOAA},
255	{0x0ad710de, 0x00, "NVIDIA MCP77",	AHCI_Q_NOAA},
256	{0x0ad810de, 0x00, "NVIDIA MCP77",	AHCI_Q_NOAA},
257	{0x0ad910de, 0x00, "NVIDIA MCP77",	AHCI_Q_NOAA},
258	{0x0ada10de, 0x00, "NVIDIA MCP77",	AHCI_Q_NOAA},
259	{0x0adb10de, 0x00, "NVIDIA MCP77",	AHCI_Q_NOAA},
260	{0x0ab410de, 0x00, "NVIDIA MCP79",	AHCI_Q_NOAA},
261	{0x0ab510de, 0x00, "NVIDIA MCP79",	AHCI_Q_NOAA},
262	{0x0ab610de, 0x00, "NVIDIA MCP79",	AHCI_Q_NOAA},
263	{0x0ab710de, 0x00, "NVIDIA MCP79",	AHCI_Q_NOAA},
264	{0x0ab810de, 0x00, "NVIDIA MCP79",	AHCI_Q_NOAA},
265	{0x0ab910de, 0x00, "NVIDIA MCP79",	AHCI_Q_NOAA},
266	{0x0aba10de, 0x00, "NVIDIA MCP79",	AHCI_Q_NOAA},
267	{0x0abb10de, 0x00, "NVIDIA MCP79",	AHCI_Q_NOAA},
268	{0x0abc10de, 0x00, "NVIDIA MCP79",	AHCI_Q_NOAA},
269	{0x0abd10de, 0x00, "NVIDIA MCP79",	AHCI_Q_NOAA},
270	{0x0abe10de, 0x00, "NVIDIA MCP79",	AHCI_Q_NOAA},
271	{0x0abf10de, 0x00, "NVIDIA MCP79",	AHCI_Q_NOAA},
272	{0x0d8410de, 0x00, "NVIDIA MCP89",	AHCI_Q_NOAA},
273	{0x0d8510de, 0x00, "NVIDIA MCP89",	AHCI_Q_NOFORCE|AHCI_Q_NOAA},
274	{0x0d8610de, 0x00, "NVIDIA MCP89",	AHCI_Q_NOAA},
275	{0x0d8710de, 0x00, "NVIDIA MCP89",	AHCI_Q_NOAA},
276	{0x0d8810de, 0x00, "NVIDIA MCP89",	AHCI_Q_NOAA},
277	{0x0d8910de, 0x00, "NVIDIA MCP89",	AHCI_Q_NOAA},
278	{0x0d8a10de, 0x00, "NVIDIA MCP89",	AHCI_Q_NOAA},
279	{0x0d8b10de, 0x00, "NVIDIA MCP89",	AHCI_Q_NOAA},
280	{0x0d8c10de, 0x00, "NVIDIA MCP89",	AHCI_Q_NOAA},
281	{0x0d8d10de, 0x00, "NVIDIA MCP89",	AHCI_Q_NOAA},
282	{0x0d8e10de, 0x00, "NVIDIA MCP89",	AHCI_Q_NOAA},
283	{0x0d8f10de, 0x00, "NVIDIA MCP89",	AHCI_Q_NOAA},
284	{0x3781105a, 0x00, "Promise TX8660",	0},
285	{0x33491106, 0x00, "VIA VT8251",	AHCI_Q_NOPMP|AHCI_Q_NONCQ},
286	{0x62871106, 0x00, "VIA VT8251",	AHCI_Q_NOPMP|AHCI_Q_NONCQ},
287	{0x11841039, 0x00, "SiS 966",		0},
288	{0x11851039, 0x00, "SiS 968",		0},
289	{0x01861039, 0x00, "SiS 968",		0},
290	{0xa01c177d, 0x00, "ThunderX SATA",	AHCI_Q_ABAR0},
291	{0x00000000, 0x00, NULL,		0}
292};
293
294static int
295ahci_pci_ctlr_reset(device_t dev)
296{
297
298	if (pci_read_config(dev, PCIR_DEVVENDOR, 4) == 0x28298086 &&
299	    (pci_read_config(dev, 0x92, 1) & 0xfe) == 0x04)
300		pci_write_config(dev, 0x92, 0x01, 1);
301	return ahci_ctlr_reset(dev);
302}
303
304static int
305ahci_probe(device_t dev)
306{
307	char buf[64];
308	int i, valid = 0;
309	uint32_t devid = pci_get_devid(dev);
310	uint8_t revid = pci_get_revid(dev);
311
312	/*
313	 * Ensure it is not a PCI bridge (some vendors use
314	 * the same PID and VID in PCI bridge and AHCI cards).
315	 */
316	if (pci_get_class(dev) == PCIC_BRIDGE)
317		return (ENXIO);
318
319	/* Is this a possible AHCI candidate? */
320	if (pci_get_class(dev) == PCIC_STORAGE &&
321	    pci_get_subclass(dev) == PCIS_STORAGE_SATA &&
322	    pci_get_progif(dev) == PCIP_STORAGE_SATA_AHCI_1_0)
323		valid = 1;
324	/* Is this a known AHCI chip? */
325	for (i = 0; ahci_ids[i].id != 0; i++) {
326		if (ahci_ids[i].id == devid &&
327		    ahci_ids[i].rev <= revid &&
328		    (valid || (force_ahci == 1 &&
329		     !(ahci_ids[i].quirks & AHCI_Q_NOFORCE)))) {
330			/* Do not attach JMicrons with single PCI function. */
331			if (pci_get_vendor(dev) == 0x197b &&
332			    (pci_read_config(dev, 0xdf, 1) & 0x40) == 0)
333				return (ENXIO);
334			snprintf(buf, sizeof(buf), "%s AHCI SATA controller",
335			    ahci_ids[i].name);
336			device_set_desc_copy(dev, buf);
337			return (BUS_PROBE_VENDOR);
338		}
339	}
340	if (!valid)
341		return (ENXIO);
342	device_set_desc_copy(dev, "AHCI SATA controller");
343	return (BUS_PROBE_VENDOR);
344}
345
346static int
347ahci_ata_probe(device_t dev)
348{
349	char buf[64];
350	int i;
351	uint32_t devid = pci_get_devid(dev);
352	uint8_t revid = pci_get_revid(dev);
353
354	if ((intptr_t)device_get_ivars(dev) >= 0)
355		return (ENXIO);
356	/* Is this a known AHCI chip? */
357	for (i = 0; ahci_ids[i].id != 0; i++) {
358		if (ahci_ids[i].id == devid &&
359		    ahci_ids[i].rev <= revid) {
360			snprintf(buf, sizeof(buf), "%s AHCI SATA controller",
361			    ahci_ids[i].name);
362			device_set_desc_copy(dev, buf);
363			return (BUS_PROBE_VENDOR);
364		}
365	}
366	device_set_desc_copy(dev, "AHCI SATA controller");
367	return (BUS_PROBE_VENDOR);
368}
369
370static int
371ahci_pci_attach(device_t dev)
372{
373	struct ahci_controller *ctlr = device_get_softc(dev);
374	int	error, i;
375	uint32_t devid = pci_get_devid(dev);
376	uint8_t revid = pci_get_revid(dev);
377
378	i = 0;
379	while (ahci_ids[i].id != 0 &&
380	    (ahci_ids[i].id != devid ||
381	     ahci_ids[i].rev > revid))
382		i++;
383	ctlr->quirks = ahci_ids[i].quirks;
384	/* Limit speed for my onboard JMicron external port.
385	 * It is not eSATA really, limit to SATA 1 */
386	if (pci_get_devid(dev) == 0x2363197b &&
387	    pci_get_subvendor(dev) == 0x1043 &&
388	    pci_get_subdevice(dev) == 0x81e4)
389		ctlr->quirks |= AHCI_Q_SATA1_UNIT0;
390	ctlr->vendorid = pci_get_vendor(dev);
391	ctlr->deviceid = pci_get_device(dev);
392	ctlr->subvendorid = pci_get_subvendor(dev);
393	ctlr->subdeviceid = pci_get_subdevice(dev);
394
395	/* Default AHCI Base Address is BAR(5), Cavium uses BAR(0) */
396	if (ctlr->quirks & AHCI_Q_ABAR0)
397		ctlr->r_rid = PCIR_BAR(0);
398	else
399		ctlr->r_rid = PCIR_BAR(5);
400	if (!(ctlr->r_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
401	    &ctlr->r_rid, RF_ACTIVE)))
402		return ENXIO;
403	pci_enable_busmaster(dev);
404	/* Reset controller */
405	if ((error = ahci_pci_ctlr_reset(dev)) != 0) {
406		bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem);
407		return (error);
408	};
409
410	/* Setup interrupts. */
411
412	/* Setup MSI register parameters */
413	ctlr->msi = 2;
414	/* Process hints. */
415	if (ctlr->quirks & AHCI_Q_NOMSI)
416		ctlr->msi = 0;
417	resource_int_value(device_get_name(dev),
418	    device_get_unit(dev), "msi", &ctlr->msi);
419	ctlr->numirqs = 1;
420	if (ctlr->msi < 0)
421		ctlr->msi = 0;
422	else if (ctlr->msi == 1)
423		ctlr->msi = min(1, pci_msi_count(dev));
424	else if (ctlr->msi > 1) {
425		ctlr->msi = 2;
426		ctlr->numirqs = pci_msi_count(dev);
427	}
428	/* Allocate MSI if needed/present. */
429	if (ctlr->msi && pci_alloc_msi(dev, &ctlr->numirqs) != 0) {
430		ctlr->msi = 0;
431		ctlr->numirqs = 1;
432	}
433
434	error = ahci_attach(dev);
435	if (error != 0)
436		if (ctlr->msi)
437			pci_release_msi(dev);
438	return error;
439}
440
441static int
442ahci_pci_detach(device_t dev)
443{
444
445	ahci_detach(dev);
446	pci_release_msi(dev);
447	return (0);
448}
449
450static int
451ahci_pci_suspend(device_t dev)
452{
453	struct ahci_controller *ctlr = device_get_softc(dev);
454
455	bus_generic_suspend(dev);
456	/* Disable interupts, so the state change(s) doesn't trigger */
457	ATA_OUTL(ctlr->r_mem, AHCI_GHC,
458	     ATA_INL(ctlr->r_mem, AHCI_GHC) & (~AHCI_GHC_IE));
459	return 0;
460}
461
462static int
463ahci_pci_resume(device_t dev)
464{
465	int res;
466
467	if ((res = ahci_pci_ctlr_reset(dev)) != 0)
468		return (res);
469	ahci_ctlr_setup(dev);
470	return (bus_generic_resume(dev));
471}
472
473devclass_t ahci_devclass;
474static device_method_t ahci_methods[] = {
475	DEVMETHOD(device_probe,     ahci_probe),
476	DEVMETHOD(device_attach,    ahci_pci_attach),
477	DEVMETHOD(device_detach,    ahci_pci_detach),
478	DEVMETHOD(device_suspend,   ahci_pci_suspend),
479	DEVMETHOD(device_resume,    ahci_pci_resume),
480	DEVMETHOD(bus_print_child,  ahci_print_child),
481	DEVMETHOD(bus_alloc_resource,       ahci_alloc_resource),
482	DEVMETHOD(bus_release_resource,     ahci_release_resource),
483	DEVMETHOD(bus_setup_intr,   ahci_setup_intr),
484	DEVMETHOD(bus_teardown_intr,ahci_teardown_intr),
485	DEVMETHOD(bus_child_location_str, ahci_child_location_str),
486	DEVMETHOD(bus_get_dma_tag,  ahci_get_dma_tag),
487	DEVMETHOD_END
488};
489static driver_t ahci_driver = {
490        "ahci",
491        ahci_methods,
492        sizeof(struct ahci_controller)
493};
494DRIVER_MODULE(ahci, pci, ahci_driver, ahci_devclass, NULL, NULL);
495static device_method_t ahci_ata_methods[] = {
496	DEVMETHOD(device_probe,     ahci_ata_probe),
497	DEVMETHOD(device_attach,    ahci_pci_attach),
498	DEVMETHOD(device_detach,    ahci_pci_detach),
499	DEVMETHOD(device_suspend,   ahci_pci_suspend),
500	DEVMETHOD(device_resume,    ahci_pci_resume),
501	DEVMETHOD(bus_print_child,  ahci_print_child),
502	DEVMETHOD(bus_alloc_resource,       ahci_alloc_resource),
503	DEVMETHOD(bus_release_resource,     ahci_release_resource),
504	DEVMETHOD(bus_setup_intr,   ahci_setup_intr),
505	DEVMETHOD(bus_teardown_intr,ahci_teardown_intr),
506	DEVMETHOD(bus_child_location_str, ahci_child_location_str),
507	DEVMETHOD_END
508};
509static driver_t ahci_ata_driver = {
510        "ahci",
511        ahci_ata_methods,
512        sizeof(struct ahci_controller)
513};
514DRIVER_MODULE(ahci, atapci, ahci_ata_driver, ahci_devclass, NULL, NULL);
515