ahci_pci.c revision 271146
1195534Sscottl/*-
2238805Smav * Copyright (c) 2009-2012 Alexander Motin <mav@FreeBSD.org>
3195534Sscottl * All rights reserved.
4195534Sscottl *
5195534Sscottl * Redistribution and use in source and binary forms, with or without
6195534Sscottl * modification, are permitted provided that the following conditions
7195534Sscottl * are met:
8195534Sscottl * 1. Redistributions of source code must retain the above copyright
9195534Sscottl *    notice, this list of conditions and the following disclaimer,
10195534Sscottl *    without modification, immediately at the beginning of the file.
11195534Sscottl * 2. Redistributions in binary form must reproduce the above copyright
12195534Sscottl *    notice, this list of conditions and the following disclaimer in the
13195534Sscottl *    documentation and/or other materials provided with the distribution.
14195534Sscottl *
15195534Sscottl * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16195534Sscottl * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17195534Sscottl * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18195534Sscottl * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19195534Sscottl * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20195534Sscottl * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21195534Sscottl * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22195534Sscottl * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23195534Sscottl * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24195534Sscottl * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25195534Sscottl */
26195534Sscottl
27195534Sscottl#include <sys/cdefs.h>
28195534Sscottl__FBSDID("$FreeBSD: head/sys/dev/ahci/ahci_pci.c 271146 2014-09-04 22:22:53Z imp $");
29195534Sscottl
30195534Sscottl#include <sys/param.h>
31195534Sscottl#include <sys/module.h>
32195534Sscottl#include <sys/systm.h>
33195534Sscottl#include <sys/kernel.h>
34195534Sscottl#include <sys/bus.h>
35220576Smav#include <sys/conf.h>
36195534Sscottl#include <sys/endian.h>
37195534Sscottl#include <sys/malloc.h>
38195534Sscottl#include <sys/lock.h>
39195534Sscottl#include <sys/mutex.h>
40195534Sscottl#include <machine/stdarg.h>
41195534Sscottl#include <machine/resource.h>
42195534Sscottl#include <machine/bus.h>
43195534Sscottl#include <sys/rman.h>
44195534Sscottl#include <dev/pci/pcivar.h>
45195534Sscottl#include <dev/pci/pcireg.h>
46195534Sscottl#include "ahci.h"
47195534Sscottl
48271146Simpstatic int force_ahci = 1;
49271146SimpTUNABLE_INT("hw.ahci.force", &force_ahci);
50195534Sscottl
51199176Smavstatic struct {
52199176Smav	uint32_t	id;
53203030Smav	uint8_t		rev;
54199176Smav	const char	*name;
55199322Smav	int		quirks;
56199176Smav} ahci_ids[] = {
57271146Simp	{0x43801002, 0x00, "AMD SB600",
58271146Simp		AHCI_Q_NOMSI | AHCI_Q_ATI_PMP_BUG | AHCI_Q_MAXIO_64K},
59271146Simp	{0x43901002, 0x00, "AMD SB7x0/SB8x0/SB9x0",	AHCI_Q_ATI_PMP_BUG},
60271146Simp	{0x43911002, 0x00, "AMD SB7x0/SB8x0/SB9x0",	AHCI_Q_ATI_PMP_BUG},
61271146Simp	{0x43921002, 0x00, "AMD SB7x0/SB8x0/SB9x0",	AHCI_Q_ATI_PMP_BUG},
62271146Simp	{0x43931002, 0x00, "AMD SB7x0/SB8x0/SB9x0",	AHCI_Q_ATI_PMP_BUG},
63271146Simp	{0x43941002, 0x00, "AMD SB7x0/SB8x0/SB9x0",	AHCI_Q_ATI_PMP_BUG},
64271146Simp	/* Not sure SB8x0/SB9x0 needs this quirk. Be conservative though */
65271146Simp	{0x43951002, 0x00, "AMD SB8x0/SB9x0",	AHCI_Q_ATI_PMP_BUG},
66244146Smav	{0x78001022, 0x00, "AMD Hudson-2",	0},
67244146Smav	{0x78011022, 0x00, "AMD Hudson-2",	0},
68244146Smav	{0x78021022, 0x00, "AMD Hudson-2",	0},
69244146Smav	{0x78031022, 0x00, "AMD Hudson-2",	0},
70244146Smav	{0x78041022, 0x00, "AMD Hudson-2",	0},
71260830Smav	{0x06111b21, 0x00, "ASMedia ASM2106",	0},
72225140Smav	{0x06121b21, 0x00, "ASMedia ASM1061",	0},
73203030Smav	{0x26528086, 0x00, "Intel ICH6",	AHCI_Q_NOFORCE},
74203030Smav	{0x26538086, 0x00, "Intel ICH6M",	AHCI_Q_NOFORCE},
75203030Smav	{0x26818086, 0x00, "Intel ESB2",	0},
76203030Smav	{0x26828086, 0x00, "Intel ESB2",	0},
77203030Smav	{0x26838086, 0x00, "Intel ESB2",	0},
78203030Smav	{0x27c18086, 0x00, "Intel ICH7",	0},
79203030Smav	{0x27c38086, 0x00, "Intel ICH7",	0},
80203030Smav	{0x27c58086, 0x00, "Intel ICH7M",	0},
81203030Smav	{0x27c68086, 0x00, "Intel ICH7M",	0},
82203030Smav	{0x28218086, 0x00, "Intel ICH8",	0},
83203030Smav	{0x28228086, 0x00, "Intel ICH8",	0},
84203030Smav	{0x28248086, 0x00, "Intel ICH8",	0},
85203030Smav	{0x28298086, 0x00, "Intel ICH8M",	0},
86203030Smav	{0x282a8086, 0x00, "Intel ICH8M",	0},
87203030Smav	{0x29228086, 0x00, "Intel ICH9",	0},
88203030Smav	{0x29238086, 0x00, "Intel ICH9",	0},
89203030Smav	{0x29248086, 0x00, "Intel ICH9",	0},
90203030Smav	{0x29258086, 0x00, "Intel ICH9",	0},
91203030Smav	{0x29278086, 0x00, "Intel ICH9",	0},
92203030Smav	{0x29298086, 0x00, "Intel ICH9M",	0},
93203030Smav	{0x292a8086, 0x00, "Intel ICH9M",	0},
94203030Smav	{0x292b8086, 0x00, "Intel ICH9M",	0},
95203030Smav	{0x292c8086, 0x00, "Intel ICH9M",	0},
96203030Smav	{0x292f8086, 0x00, "Intel ICH9M",	0},
97203030Smav	{0x294d8086, 0x00, "Intel ICH9",	0},
98203030Smav	{0x294e8086, 0x00, "Intel ICH9M",	0},
99203030Smav	{0x3a058086, 0x00, "Intel ICH10",	0},
100203030Smav	{0x3a228086, 0x00, "Intel ICH10",	0},
101203030Smav	{0x3a258086, 0x00, "Intel ICH10",	0},
102211922Smav	{0x3b228086, 0x00, "Intel 5 Series/3400 Series",	0},
103211922Smav	{0x3b238086, 0x00, "Intel 5 Series/3400 Series",	0},
104211922Smav	{0x3b258086, 0x00, "Intel 5 Series/3400 Series",	0},
105211922Smav	{0x3b298086, 0x00, "Intel 5 Series/3400 Series",	0},
106211922Smav	{0x3b2c8086, 0x00, "Intel 5 Series/3400 Series",	0},
107211922Smav	{0x3b2f8086, 0x00, "Intel 5 Series/3400 Series",	0},
108211922Smav	{0x1c028086, 0x00, "Intel Cougar Point",	0},
109211922Smav	{0x1c038086, 0x00, "Intel Cougar Point",	0},
110211922Smav	{0x1c048086, 0x00, "Intel Cougar Point",	0},
111211922Smav	{0x1c058086, 0x00, "Intel Cougar Point",	0},
112218605Smav	{0x1d028086, 0x00, "Intel Patsburg",	0},
113218605Smav	{0x1d048086, 0x00, "Intel Patsburg",	0},
114218605Smav	{0x1d068086, 0x00, "Intel Patsburg",	0},
115229671Sjimharris	{0x28268086, 0x00, "Intel Patsburg (RAID)",	0},
116221789Sjfv	{0x1e028086, 0x00, "Intel Panther Point",	0},
117221789Sjfv	{0x1e038086, 0x00, "Intel Panther Point",	0},
118258162Smav	{0x1e048086, 0x00, "Intel Panther Point (RAID)",	0},
119258162Smav	{0x1e058086, 0x00, "Intel Panther Point (RAID)",	0},
120258162Smav	{0x1e068086, 0x00, "Intel Panther Point (RAID)",	0},
121258162Smav	{0x1e078086, 0x00, "Intel Panther Point (RAID)",	0},
122258162Smav	{0x1e0e8086, 0x00, "Intel Panther Point (RAID)",	0},
123258162Smav	{0x1e0f8086, 0x00, "Intel Panther Point (RAID)",	0},
124258162Smav	{0x1f228086, 0x00, "Intel Avoton",	0},
125258162Smav	{0x1f238086, 0x00, "Intel Avoton",	0},
126258162Smav	{0x1f248086, 0x00, "Intel Avoton (RAID)",	0},
127258162Smav	{0x1f258086, 0x00, "Intel Avoton (RAID)",	0},
128258162Smav	{0x1f268086, 0x00, "Intel Avoton (RAID)",	0},
129258162Smav	{0x1f278086, 0x00, "Intel Avoton (RAID)",	0},
130258162Smav	{0x1f2e8086, 0x00, "Intel Avoton (RAID)",	0},
131258162Smav	{0x1f2f8086, 0x00, "Intel Avoton (RAID)",	0},
132258162Smav	{0x1f328086, 0x00, "Intel Avoton",	0},
133258162Smav	{0x1f338086, 0x00, "Intel Avoton",	0},
134258162Smav	{0x1f348086, 0x00, "Intel Avoton (RAID)",	0},
135258162Smav	{0x1f358086, 0x00, "Intel Avoton (RAID)",	0},
136258162Smav	{0x1f368086, 0x00, "Intel Avoton (RAID)",	0},
137258162Smav	{0x1f378086, 0x00, "Intel Avoton (RAID)",	0},
138258162Smav	{0x1f3e8086, 0x00, "Intel Avoton (RAID)",	0},
139258162Smav	{0x1f3f8086, 0x00, "Intel Avoton (RAID)",	0},
140253475Sjfv	{0x23a38086, 0x00, "Intel Coleto Creek",        0},
141258162Smav	{0x28238086, 0x00, "Intel Wellsburg (RAID)",	0},
142258162Smav	{0x28278086, 0x00, "Intel Wellsburg (RAID)",	0},
143244983Sjfv	{0x8c028086, 0x00, "Intel Lynx Point",	0},
144244983Sjfv	{0x8c038086, 0x00, "Intel Lynx Point",	0},
145258162Smav	{0x8c048086, 0x00, "Intel Lynx Point (RAID)",	0},
146258162Smav	{0x8c058086, 0x00, "Intel Lynx Point (RAID)",	0},
147258162Smav	{0x8c068086, 0x00, "Intel Lynx Point (RAID)",	0},
148258162Smav	{0x8c078086, 0x00, "Intel Lynx Point (RAID)",	0},
149258162Smav	{0x8c0e8086, 0x00, "Intel Lynx Point (RAID)",	0},
150258162Smav	{0x8c0f8086, 0x00, "Intel Lynx Point (RAID)",	0},
151258162Smav	{0x8d028086, 0x00, "Intel Wellsburg",	0},
152258162Smav	{0x8d048086, 0x00, "Intel Wellsburg (RAID)",	0},
153258162Smav	{0x8d068086, 0x00, "Intel Wellsburg (RAID)",	0},
154258162Smav	{0x8d628086, 0x00, "Intel Wellsburg",	0},
155258162Smav	{0x8d648086, 0x00, "Intel Wellsburg (RAID)",	0},
156258162Smav	{0x8d668086, 0x00, "Intel Wellsburg (RAID)",	0},
157258162Smav	{0x8d6e8086, 0x00, "Intel Wellsburg (RAID)",	0},
158258162Smav	{0x9c028086, 0x00, "Intel Lynx Point-LP",	0},
159258162Smav	{0x9c038086, 0x00, "Intel Lynx Point-LP",	0},
160258162Smav	{0x9c048086, 0x00, "Intel Lynx Point-LP (RAID)",	0},
161258162Smav	{0x9c058086, 0x00, "Intel Lynx Point-LP (RAID)",	0},
162258162Smav	{0x9c068086, 0x00, "Intel Lynx Point-LP (RAID)",	0},
163258162Smav	{0x9c078086, 0x00, "Intel Lynx Point-LP (RAID)",	0},
164258162Smav	{0x9c0e8086, 0x00, "Intel Lynx Point-LP (RAID)",	0},
165258162Smav	{0x9c0f8086, 0x00, "Intel Lynx Point-LP (RAID)",	0},
166221789Sjfv	{0x23238086, 0x00, "Intel DH89xxCC",	0},
167239907Smav	{0x2360197b, 0x00, "JMicron JMB360",	0},
168203030Smav	{0x2361197b, 0x00, "JMicron JMB361",	AHCI_Q_NOFORCE},
169239907Smav	{0x2362197b, 0x00, "JMicron JMB362",	0},
170203030Smav	{0x2363197b, 0x00, "JMicron JMB363",	AHCI_Q_NOFORCE},
171203030Smav	{0x2365197b, 0x00, "JMicron JMB365",	AHCI_Q_NOFORCE},
172203030Smav	{0x2366197b, 0x00, "JMicron JMB366",	AHCI_Q_NOFORCE},
173203030Smav	{0x2368197b, 0x00, "JMicron JMB368",	AHCI_Q_NOFORCE},
174232380Smav	{0x611111ab, 0x00, "Marvell 88SE6111",	AHCI_Q_NOFORCE | AHCI_Q_1CH |
175218596Smav	    AHCI_Q_EDGEIS},
176232380Smav	{0x612111ab, 0x00, "Marvell 88SE6121",	AHCI_Q_NOFORCE | AHCI_Q_2CH |
177218596Smav	    AHCI_Q_EDGEIS | AHCI_Q_NONCQ | AHCI_Q_NOCOUNT},
178232380Smav	{0x614111ab, 0x00, "Marvell 88SE6141",	AHCI_Q_NOFORCE | AHCI_Q_4CH |
179218596Smav	    AHCI_Q_EDGEIS | AHCI_Q_NONCQ | AHCI_Q_NOCOUNT},
180232380Smav	{0x614511ab, 0x00, "Marvell 88SE6145",	AHCI_Q_NOFORCE | AHCI_Q_4CH |
181218596Smav	    AHCI_Q_EDGEIS | AHCI_Q_NONCQ | AHCI_Q_NOCOUNT},
182220413Smav	{0x91201b4b, 0x00, "Marvell 88SE912x",	AHCI_Q_EDGEIS|AHCI_Q_NOBSYRES},
183222304Smav	{0x91231b4b, 0x11, "Marvell 88SE912x",	AHCI_Q_NOBSYRES|AHCI_Q_ALTSIG},
184203123Smav	{0x91231b4b, 0x00, "Marvell 88SE912x",	AHCI_Q_EDGEIS|AHCI_Q_SATA2|AHCI_Q_NOBSYRES},
185223699Smav	{0x91251b4b, 0x00, "Marvell 88SE9125",	AHCI_Q_NOBSYRES},
186225789Smav	{0x91281b4b, 0x00, "Marvell 88SE9128",	AHCI_Q_NOBSYRES|AHCI_Q_ALTSIG},
187236242Shselasky	{0x91301b4b, 0x00, "Marvell 88SE9130",  AHCI_Q_NOBSYRES|AHCI_Q_ALTSIG},
188222306Smav	{0x91721b4b, 0x00, "Marvell 88SE9172",	AHCI_Q_NOBSYRES},
189221504Smav	{0x91821b4b, 0x00, "Marvell 88SE9182",	AHCI_Q_NOBSYRES},
190254987Sgavin	{0x91831b4b, 0x00, "Marvell 88SS9183",	AHCI_Q_NOBSYRES},
191253069Smav	{0x91a01b4b, 0x00, "Marvell 88SE91Ax",	AHCI_Q_NOBSYRES},
192253074Smav	{0x92151b4b, 0x00, "Marvell 88SE9215",  AHCI_Q_NOBSYRES},
193236737Smav	{0x92201b4b, 0x00, "Marvell 88SE9220",  AHCI_Q_NOBSYRES|AHCI_Q_ALTSIG},
194236737Smav	{0x92301b4b, 0x00, "Marvell 88SE9230",  AHCI_Q_NOBSYRES|AHCI_Q_ALTSIG},
195236737Smav	{0x92351b4b, 0x00, "Marvell 88SE9235",  AHCI_Q_NOBSYRES},
196216309Smav	{0x06201103, 0x00, "HighPoint RocketRAID 620",	AHCI_Q_NOBSYRES},
197216309Smav	{0x06201b4b, 0x00, "HighPoint RocketRAID 620",	AHCI_Q_NOBSYRES},
198216309Smav	{0x06221103, 0x00, "HighPoint RocketRAID 622",	AHCI_Q_NOBSYRES},
199216309Smav	{0x06221b4b, 0x00, "HighPoint RocketRAID 622",	AHCI_Q_NOBSYRES},
200217245Smav	{0x06401103, 0x00, "HighPoint RocketRAID 640",	AHCI_Q_NOBSYRES},
201219341Smav	{0x06401b4b, 0x00, "HighPoint RocketRAID 640",	AHCI_Q_NOBSYRES},
202217245Smav	{0x06441103, 0x00, "HighPoint RocketRAID 644",	AHCI_Q_NOBSYRES},
203219341Smav	{0x06441b4b, 0x00, "HighPoint RocketRAID 644",	AHCI_Q_NOBSYRES},
204250185Smav	{0x06411103, 0x00, "HighPoint RocketRAID 640L",	AHCI_Q_NOBSYRES},
205250185Smav	{0x06421103, 0x00, "HighPoint RocketRAID 642L",	AHCI_Q_NOBSYRES},
206250185Smav	{0x06451103, 0x00, "HighPoint RocketRAID 644L",	AHCI_Q_NOBSYRES},
207207499Smav	{0x044c10de, 0x00, "NVIDIA MCP65",	AHCI_Q_NOAA},
208207499Smav	{0x044d10de, 0x00, "NVIDIA MCP65",	AHCI_Q_NOAA},
209207499Smav	{0x044e10de, 0x00, "NVIDIA MCP65",	AHCI_Q_NOAA},
210207499Smav	{0x044f10de, 0x00, "NVIDIA MCP65",	AHCI_Q_NOAA},
211207499Smav	{0x045c10de, 0x00, "NVIDIA MCP65",	AHCI_Q_NOAA},
212207499Smav	{0x045d10de, 0x00, "NVIDIA MCP65",	AHCI_Q_NOAA},
213207499Smav	{0x045e10de, 0x00, "NVIDIA MCP65",	AHCI_Q_NOAA},
214207499Smav	{0x045f10de, 0x00, "NVIDIA MCP65",	AHCI_Q_NOAA},
215207499Smav	{0x055010de, 0x00, "NVIDIA MCP67",	AHCI_Q_NOAA},
216207499Smav	{0x055110de, 0x00, "NVIDIA MCP67",	AHCI_Q_NOAA},
217207499Smav	{0x055210de, 0x00, "NVIDIA MCP67",	AHCI_Q_NOAA},
218207499Smav	{0x055310de, 0x00, "NVIDIA MCP67",	AHCI_Q_NOAA},
219207499Smav	{0x055410de, 0x00, "NVIDIA MCP67",	AHCI_Q_NOAA},
220207499Smav	{0x055510de, 0x00, "NVIDIA MCP67",	AHCI_Q_NOAA},
221207499Smav	{0x055610de, 0x00, "NVIDIA MCP67",	AHCI_Q_NOAA},
222207499Smav	{0x055710de, 0x00, "NVIDIA MCP67",	AHCI_Q_NOAA},
223207499Smav	{0x055810de, 0x00, "NVIDIA MCP67",	AHCI_Q_NOAA},
224207499Smav	{0x055910de, 0x00, "NVIDIA MCP67",	AHCI_Q_NOAA},
225207499Smav	{0x055A10de, 0x00, "NVIDIA MCP67",	AHCI_Q_NOAA},
226207499Smav	{0x055B10de, 0x00, "NVIDIA MCP67",	AHCI_Q_NOAA},
227207499Smav	{0x058410de, 0x00, "NVIDIA MCP67",	AHCI_Q_NOAA},
228207499Smav	{0x07f010de, 0x00, "NVIDIA MCP73",	AHCI_Q_NOAA},
229207499Smav	{0x07f110de, 0x00, "NVIDIA MCP73",	AHCI_Q_NOAA},
230207499Smav	{0x07f210de, 0x00, "NVIDIA MCP73",	AHCI_Q_NOAA},
231207499Smav	{0x07f310de, 0x00, "NVIDIA MCP73",	AHCI_Q_NOAA},
232207499Smav	{0x07f410de, 0x00, "NVIDIA MCP73",	AHCI_Q_NOAA},
233207499Smav	{0x07f510de, 0x00, "NVIDIA MCP73",	AHCI_Q_NOAA},
234207499Smav	{0x07f610de, 0x00, "NVIDIA MCP73",	AHCI_Q_NOAA},
235207499Smav	{0x07f710de, 0x00, "NVIDIA MCP73",	AHCI_Q_NOAA},
236207499Smav	{0x07f810de, 0x00, "NVIDIA MCP73",	AHCI_Q_NOAA},
237207499Smav	{0x07f910de, 0x00, "NVIDIA MCP73",	AHCI_Q_NOAA},
238207499Smav	{0x07fa10de, 0x00, "NVIDIA MCP73",	AHCI_Q_NOAA},
239207499Smav	{0x07fb10de, 0x00, "NVIDIA MCP73",	AHCI_Q_NOAA},
240207499Smav	{0x0ad010de, 0x00, "NVIDIA MCP77",	AHCI_Q_NOAA},
241207499Smav	{0x0ad110de, 0x00, "NVIDIA MCP77",	AHCI_Q_NOAA},
242207499Smav	{0x0ad210de, 0x00, "NVIDIA MCP77",	AHCI_Q_NOAA},
243207499Smav	{0x0ad310de, 0x00, "NVIDIA MCP77",	AHCI_Q_NOAA},
244207499Smav	{0x0ad410de, 0x00, "NVIDIA MCP77",	AHCI_Q_NOAA},
245207499Smav	{0x0ad510de, 0x00, "NVIDIA MCP77",	AHCI_Q_NOAA},
246207499Smav	{0x0ad610de, 0x00, "NVIDIA MCP77",	AHCI_Q_NOAA},
247207499Smav	{0x0ad710de, 0x00, "NVIDIA MCP77",	AHCI_Q_NOAA},
248207499Smav	{0x0ad810de, 0x00, "NVIDIA MCP77",	AHCI_Q_NOAA},
249207499Smav	{0x0ad910de, 0x00, "NVIDIA MCP77",	AHCI_Q_NOAA},
250207499Smav	{0x0ada10de, 0x00, "NVIDIA MCP77",	AHCI_Q_NOAA},
251207499Smav	{0x0adb10de, 0x00, "NVIDIA MCP77",	AHCI_Q_NOAA},
252207499Smav	{0x0ab410de, 0x00, "NVIDIA MCP79",	AHCI_Q_NOAA},
253207499Smav	{0x0ab510de, 0x00, "NVIDIA MCP79",	AHCI_Q_NOAA},
254207499Smav	{0x0ab610de, 0x00, "NVIDIA MCP79",	AHCI_Q_NOAA},
255207499Smav	{0x0ab710de, 0x00, "NVIDIA MCP79",	AHCI_Q_NOAA},
256207499Smav	{0x0ab810de, 0x00, "NVIDIA MCP79",	AHCI_Q_NOAA},
257207499Smav	{0x0ab910de, 0x00, "NVIDIA MCP79",	AHCI_Q_NOAA},
258207499Smav	{0x0aba10de, 0x00, "NVIDIA MCP79",	AHCI_Q_NOAA},
259207499Smav	{0x0abb10de, 0x00, "NVIDIA MCP79",	AHCI_Q_NOAA},
260207499Smav	{0x0abc10de, 0x00, "NVIDIA MCP79",	AHCI_Q_NOAA},
261207499Smav	{0x0abd10de, 0x00, "NVIDIA MCP79",	AHCI_Q_NOAA},
262207499Smav	{0x0abe10de, 0x00, "NVIDIA MCP79",	AHCI_Q_NOAA},
263207499Smav	{0x0abf10de, 0x00, "NVIDIA MCP79",	AHCI_Q_NOAA},
264207499Smav	{0x0d8410de, 0x00, "NVIDIA MCP89",	AHCI_Q_NOAA},
265224603Smav	{0x0d8510de, 0x00, "NVIDIA MCP89",	AHCI_Q_NOFORCE|AHCI_Q_NOAA},
266207499Smav	{0x0d8610de, 0x00, "NVIDIA MCP89",	AHCI_Q_NOAA},
267207499Smav	{0x0d8710de, 0x00, "NVIDIA MCP89",	AHCI_Q_NOAA},
268207499Smav	{0x0d8810de, 0x00, "NVIDIA MCP89",	AHCI_Q_NOAA},
269207499Smav	{0x0d8910de, 0x00, "NVIDIA MCP89",	AHCI_Q_NOAA},
270207499Smav	{0x0d8a10de, 0x00, "NVIDIA MCP89",	AHCI_Q_NOAA},
271207499Smav	{0x0d8b10de, 0x00, "NVIDIA MCP89",	AHCI_Q_NOAA},
272207499Smav	{0x0d8c10de, 0x00, "NVIDIA MCP89",	AHCI_Q_NOAA},
273207499Smav	{0x0d8d10de, 0x00, "NVIDIA MCP89",	AHCI_Q_NOAA},
274207499Smav	{0x0d8e10de, 0x00, "NVIDIA MCP89",	AHCI_Q_NOAA},
275207499Smav	{0x0d8f10de, 0x00, "NVIDIA MCP89",	AHCI_Q_NOAA},
276208907Smav	{0x33491106, 0x00, "VIA VT8251",	AHCI_Q_NOPMP|AHCI_Q_NONCQ},
277208907Smav	{0x62871106, 0x00, "VIA VT8251",	AHCI_Q_NOPMP|AHCI_Q_NONCQ},
278203030Smav	{0x11841039, 0x00, "SiS 966",		0},
279203030Smav	{0x11851039, 0x00, "SiS 968",		0},
280203030Smav	{0x01861039, 0x00, "SiS 968",		0},
281203030Smav	{0x00000000, 0x00, NULL,		0}
282199176Smav};
283199176Smav
284271146Simpstatic int
285271146Simpahci_pci_ctlr_reset(device_t dev)
286271146Simp{
287220565Smav
288271146Simp	if (pci_read_config(dev, PCIR_DEVVENDOR, 4) == 0x28298086 &&
289271146Simp	    (pci_read_config(dev, 0x92, 1) & 0xfe) == 0x04)
290271146Simp		pci_write_config(dev, 0x92, 0x01, 1);
291271146Simp	return ahci_ctlr_reset(dev);
292271146Simp}
293228200Smav
294195534Sscottlstatic int
295195534Sscottlahci_probe(device_t dev)
296195534Sscottl{
297199176Smav	char buf[64];
298199322Smav	int i, valid = 0;
299199322Smav	uint32_t devid = pci_get_devid(dev);
300203030Smav	uint8_t revid = pci_get_revid(dev);
301199322Smav
302260163Szbb	/*
303260163Szbb	 * Ensure it is not a PCI bridge (some vendors use
304260163Szbb	 * the same PID and VID in PCI bridge and AHCI cards).
305260163Szbb	 */
306260163Szbb	if (pci_get_class(dev) == PCIC_BRIDGE)
307260163Szbb		return (ENXIO);
308260163Szbb
309199322Smav	/* Is this a possible AHCI candidate? */
310199322Smav	if (pci_get_class(dev) == PCIC_STORAGE &&
311199322Smav	    pci_get_subclass(dev) == PCIS_STORAGE_SATA &&
312199322Smav	    pci_get_progif(dev) == PCIP_STORAGE_SATA_AHCI_1_0)
313199322Smav		valid = 1;
314199322Smav	/* Is this a known AHCI chip? */
315199322Smav	for (i = 0; ahci_ids[i].id != 0; i++) {
316199322Smav		if (ahci_ids[i].id == devid &&
317203030Smav		    ahci_ids[i].rev <= revid &&
318228200Smav		    (valid || (force_ahci == 1 &&
319228200Smav		     !(ahci_ids[i].quirks & AHCI_Q_NOFORCE)))) {
320199717Smav			/* Do not attach JMicrons with single PCI function. */
321199717Smav			if (pci_get_vendor(dev) == 0x197b &&
322199717Smav			    (pci_read_config(dev, 0xdf, 1) & 0x40) == 0)
323199717Smav				return (ENXIO);
324199322Smav			snprintf(buf, sizeof(buf), "%s AHCI SATA controller",
325199322Smav			    ahci_ids[i].name);
326199322Smav			device_set_desc_copy(dev, buf);
327199322Smav			return (BUS_PROBE_VENDOR);
328199322Smav		}
329199322Smav	}
330199322Smav	if (!valid)
331199322Smav		return (ENXIO);
332199322Smav	device_set_desc_copy(dev, "AHCI SATA controller");
333199322Smav	return (BUS_PROBE_VENDOR);
334199322Smav}
335199322Smav
336199322Smavstatic int
337199322Smavahci_ata_probe(device_t dev)
338199322Smav{
339199322Smav	char buf[64];
340199176Smav	int i;
341199176Smav	uint32_t devid = pci_get_devid(dev);
342203030Smav	uint8_t revid = pci_get_revid(dev);
343195534Sscottl
344199322Smav	if ((intptr_t)device_get_ivars(dev) >= 0)
345199322Smav		return (ENXIO);
346199176Smav	/* Is this a known AHCI chip? */
347199176Smav	for (i = 0; ahci_ids[i].id != 0; i++) {
348203030Smav		if (ahci_ids[i].id == devid &&
349203030Smav		    ahci_ids[i].rev <= revid) {
350199176Smav			snprintf(buf, sizeof(buf), "%s AHCI SATA controller",
351199176Smav			    ahci_ids[i].name);
352199176Smav			device_set_desc_copy(dev, buf);
353199176Smav			return (BUS_PROBE_VENDOR);
354199176Smav		}
355199176Smav	}
356199176Smav	device_set_desc_copy(dev, "AHCI SATA controller");
357195534Sscottl	return (BUS_PROBE_VENDOR);
358195534Sscottl}
359195534Sscottl
360195534Sscottlstatic int
361271146Simpahci_pci_attach(device_t dev)
362195534Sscottl{
363195534Sscottl	struct ahci_controller *ctlr = device_get_softc(dev);
364271146Simp	int	error, i;
365199322Smav	uint32_t devid = pci_get_devid(dev);
366203030Smav	uint8_t revid = pci_get_revid(dev);
367195534Sscottl
368199322Smav	i = 0;
369203030Smav	while (ahci_ids[i].id != 0 &&
370203030Smav	    (ahci_ids[i].id != devid ||
371203030Smav	     ahci_ids[i].rev > revid))
372199322Smav		i++;
373199322Smav	ctlr->quirks = ahci_ids[i].quirks;
374271146Simp	/* Limit speed for my onboard JMicron external port.
375271146Simp	 * It is not eSATA really, limit to SATA 1 */
376271146Simp	if (pci_get_devid(dev) == 0x2363197b &&
377271146Simp	    pci_get_subvendor(dev) == 0x1043 &&
378271146Simp	    pci_get_subdevice(dev) == 0x81e4)
379271146Simp		ctlr->quirks |= AHCI_Q_SATA1_UNIT0;
380195534Sscottl	/* if we have a memory BAR(5) we are likely on an AHCI part */
381271146Simp	ctlr->vendorid = pci_get_vendor(dev);
382271146Simp	ctlr->deviceid = pci_get_device(dev);
383271146Simp	ctlr->subvendorid = pci_get_subvendor(dev);
384271146Simp	ctlr->subdeviceid = pci_get_subdevice(dev);
385195534Sscottl	ctlr->r_rid = PCIR_BAR(5);
386195534Sscottl	if (!(ctlr->r_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
387195534Sscottl	    &ctlr->r_rid, RF_ACTIVE)))
388195534Sscottl		return ENXIO;
389207511Smav	pci_enable_busmaster(dev);
390195534Sscottl	/* Reset controller */
391271146Simp	if ((error = ahci_pci_ctlr_reset(dev)) != 0) {
392195534Sscottl		bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem);
393195534Sscottl		return (error);
394195534Sscottl	};
395222304Smav
396195534Sscottl	/* Setup interrupts. */
397195534Sscottl
398271146Simp	/* Setup MSI register parameters */
399256843Smav	ctlr->msi = 2;
400195534Sscottl	/* Process hints. */
401245875Smav	if (ctlr->quirks & AHCI_Q_NOMSI)
402256843Smav		ctlr->msi = 0;
403195534Sscottl	resource_int_value(device_get_name(dev),
404256843Smav	    device_get_unit(dev), "msi", &ctlr->msi);
405256843Smav	ctlr->numirqs = 1;
406256843Smav	if (ctlr->msi < 0)
407256843Smav		ctlr->msi = 0;
408256843Smav	else if (ctlr->msi == 1)
409256843Smav		ctlr->msi = min(1, pci_msi_count(dev));
410256843Smav	else if (ctlr->msi > 1) {
411256843Smav		ctlr->msi = 2;
412256843Smav		ctlr->numirqs = pci_msi_count(dev);
413256843Smav	}
414195534Sscottl	/* Allocate MSI if needed/present. */
415256843Smav	if (ctlr->msi && pci_alloc_msi(dev, &ctlr->numirqs) != 0) {
416256843Smav		ctlr->msi = 0;
417195534Sscottl		ctlr->numirqs = 1;
418195534Sscottl	}
419195534Sscottl
420271146Simp	if (ahci_setup_interrupt(dev)) {
421271146Simp		if (ctlr->msi)
422271146Simp			pci_release_msi(dev);
423271146Simp		bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem);
424271146Simp		return ENXIO;
425196656Smav	}
426195534Sscottl
427271146Simp	error = ahci_attach(dev);
428271146Simp	if (error != 0)
429271146Simp		if (ctlr->msi)
430271146Simp			pci_release_msi(dev);
431271146Simp	return error;
432195534Sscottl}
433195534Sscottl
434195534Sscottlstatic int
435271146Simpahci_pci_detach(device_t dev)
436195534Sscottl{
437195534Sscottl
438271146Simp	ahci_detach(dev);
439271146Simp	pci_release_msi(dev);
440195534Sscottl	return (0);
441195534Sscottl}
442195534Sscottl
443195534Sscottlstatic int
444271146Simpahci_pci_suspend(device_t dev)
445195534Sscottl{
446195534Sscottl	struct ahci_controller *ctlr = device_get_softc(dev);
447195534Sscottl
448271146Simp	bus_generic_suspend(dev);
449271146Simp	/* Disable interupts, so the state change(s) doesn't trigger */
450271146Simp	ATA_OUTL(ctlr->r_mem, AHCI_GHC,
451271146Simp	     ATA_INL(ctlr->r_mem, AHCI_GHC) & (~AHCI_GHC_IE));
452271146Simp	return 0;
453195534Sscottl}
454195534Sscottl
455195534Sscottlstatic int
456271146Simpahci_pci_resume(device_t dev)
457195534Sscottl{
458271146Simp	int res;
459195534Sscottl
460271146Simp	if ((res = ahci_pci_ctlr_reset(dev)) != 0)
461271146Simp		return (res);
462271146Simp	ahci_ctlr_setup(dev);
463271146Simp	return (bus_generic_resume(dev));
464195534Sscottl}
465195534Sscottl
466195534Sscottldevclass_t ahci_devclass;
467195534Sscottlstatic device_method_t ahci_methods[] = {
468195534Sscottl	DEVMETHOD(device_probe,     ahci_probe),
469271146Simp	DEVMETHOD(device_attach,    ahci_pci_attach),
470271146Simp	DEVMETHOD(device_detach,    ahci_pci_detach),
471271146Simp	DEVMETHOD(device_suspend,   ahci_pci_suspend),
472271146Simp	DEVMETHOD(device_resume,    ahci_pci_resume),
473195534Sscottl	DEVMETHOD(bus_print_child,  ahci_print_child),
474195534Sscottl	DEVMETHOD(bus_alloc_resource,       ahci_alloc_resource),
475195534Sscottl	DEVMETHOD(bus_release_resource,     ahci_release_resource),
476195534Sscottl	DEVMETHOD(bus_setup_intr,   ahci_setup_intr),
477195534Sscottl	DEVMETHOD(bus_teardown_intr,ahci_teardown_intr),
478208410Smav	DEVMETHOD(bus_child_location_str, ahci_child_location_str),
479249346Smav	DEVMETHOD(bus_get_dma_tag,  ahci_get_dma_tag),
480195534Sscottl	{ 0, 0 }
481195534Sscottl};
482195534Sscottlstatic driver_t ahci_driver = {
483195534Sscottl        "ahci",
484195534Sscottl        ahci_methods,
485195534Sscottl        sizeof(struct ahci_controller)
486195534Sscottl};
487195534SscottlDRIVER_MODULE(ahci, pci, ahci_driver, ahci_devclass, 0, 0);
488199322Smavstatic device_method_t ahci_ata_methods[] = {
489199322Smav	DEVMETHOD(device_probe,     ahci_ata_probe),
490271146Simp	DEVMETHOD(device_attach,    ahci_pci_attach),
491271146Simp	DEVMETHOD(device_detach,    ahci_pci_detach),
492271146Simp	DEVMETHOD(device_suspend,   ahci_pci_suspend),
493271146Simp	DEVMETHOD(device_resume,    ahci_pci_resume),
494199322Smav	DEVMETHOD(bus_print_child,  ahci_print_child),
495199322Smav	DEVMETHOD(bus_alloc_resource,       ahci_alloc_resource),
496199322Smav	DEVMETHOD(bus_release_resource,     ahci_release_resource),
497199322Smav	DEVMETHOD(bus_setup_intr,   ahci_setup_intr),
498199322Smav	DEVMETHOD(bus_teardown_intr,ahci_teardown_intr),
499208410Smav	DEVMETHOD(bus_child_location_str, ahci_child_location_str),
500199322Smav	{ 0, 0 }
501199322Smav};
502199322Smavstatic driver_t ahci_ata_driver = {
503199322Smav        "ahci",
504199322Smav        ahci_ata_methods,
505199322Smav        sizeof(struct ahci_controller)
506199322Smav};
507199322SmavDRIVER_MODULE(ahci, atapci, ahci_ata_driver, ahci_devclass, 0, 0);
508