ahci_pci.c revision 275101
1139825Simp/*-
294755Sbenno * Copyright (c) 2009-2012 Alexander Motin <mav@FreeBSD.org>
3256901Snwhitehorn * All rights reserved.
4256901Snwhitehorn *
5256901Snwhitehorn * Redistribution and use in source and binary forms, with or without
694755Sbenno * modification, are permitted provided that the following conditions
794755Sbenno * are met:
894755Sbenno * 1. Redistributions of source code must retain the above copyright
994755Sbenno *    notice, this list of conditions and the following disclaimer,
1094755Sbenno *    without modification, immediately at the beginning of the file.
1194755Sbenno * 2. Redistributions in binary form must reproduce the above copyright
1294755Sbenno *    notice, this list of conditions and the following disclaimer in the
1394755Sbenno *    documentation and/or other materials provided with the distribution.
1494755Sbenno *
1594755Sbenno * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1694755Sbenno * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1794755Sbenno * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18125702Sgrehan * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
1994755Sbenno * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2094755Sbenno * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2194755Sbenno * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2294755Sbenno * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2394755Sbenno * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2494755Sbenno * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2594755Sbenno */
2694755Sbenno
2794755Sbenno#include <sys/cdefs.h>
2894755Sbenno__FBSDID("$FreeBSD: head/sys/dev/ahci/ahci_pci.c 275101 2014-11-26 04:23:21Z mav $");
2994755Sbenno
3094755Sbenno#include <sys/param.h>
3194755Sbenno#include <sys/module.h>
3294755Sbenno#include <sys/systm.h>
3394755Sbenno#include <sys/kernel.h>
3494755Sbenno#include <sys/bus.h>
35227843Smarius#include <sys/conf.h>
36227843Smarius#include <sys/endian.h>
37227843Smarius#include <sys/malloc.h>
3894755Sbenno#include <sys/lock.h>
3994755Sbenno#include <sys/mutex.h>
4094755Sbenno#include <machine/stdarg.h>
4194755Sbenno#include <machine/resource.h>
4294755Sbenno#include <machine/bus.h>
43256901Snwhitehorn#include <sys/rman.h>
44256901Snwhitehorn#include <dev/pci/pcivar.h>
45256901Snwhitehorn#include <dev/pci/pcireg.h>
4694755Sbenno#include "ahci.h"
47256901Snwhitehorn
48256901Snwhitehornstatic int force_ahci = 1;
49256901SnwhitehornTUNABLE_INT("hw.ahci.force", &force_ahci);
50256855Snwhitehorn
51212239Smavstatic struct {
5294755Sbenno	uint32_t	id;
53280772Sian	uint8_t		rev;
5494755Sbenno	const char	*name;
5594755Sbenno	int		quirks;
5694755Sbenno} ahci_ids[] = {
5794755Sbenno	{0x43801002, 0x00, "AMD SB600",
5894755Sbenno		AHCI_Q_NOMSI | AHCI_Q_ATI_PMP_BUG | AHCI_Q_MAXIO_64K},
59261513Snwhitehorn	{0x43901002, 0x00, "AMD SB7x0/SB8x0/SB9x0",	AHCI_Q_ATI_PMP_BUG},
60256901Snwhitehorn	{0x43911002, 0x00, "AMD SB7x0/SB8x0/SB9x0",	AHCI_Q_ATI_PMP_BUG},
61256901Snwhitehorn	{0x43921002, 0x00, "AMD SB7x0/SB8x0/SB9x0",	AHCI_Q_ATI_PMP_BUG},
62256901Snwhitehorn	{0x43931002, 0x00, "AMD SB7x0/SB8x0/SB9x0",	AHCI_Q_ATI_PMP_BUG},
6394755Sbenno	{0x43941002, 0x00, "AMD SB7x0/SB8x0/SB9x0",	AHCI_Q_ATI_PMP_BUG},
6494755Sbenno	/* Not sure SB8x0/SB9x0 needs this quirk. Be conservative though */
6594755Sbenno	{0x43951002, 0x00, "AMD SB8x0/SB9x0",	AHCI_Q_ATI_PMP_BUG},
66261513Snwhitehorn	{0x78001022, 0x00, "AMD Hudson-2",	0},
67280772Sian	{0x78011022, 0x00, "AMD Hudson-2",	0},
68261513Snwhitehorn	{0x78021022, 0x00, "AMD Hudson-2",	0},
69261513Snwhitehorn	{0x78031022, 0x00, "AMD Hudson-2",	0},
70261513Snwhitehorn	{0x78041022, 0x00, "AMD Hudson-2",	0},
7199652Sbenno	{0x06111b21, 0x00, "ASMedia ASM2106",	0},
72282480Sandrew	{0x06121b21, 0x00, "ASMedia ASM1061",	0},
73261513Snwhitehorn	{0x26528086, 0x00, "Intel ICH6",	AHCI_Q_NOFORCE},
74282480Sandrew	{0x26538086, 0x00, "Intel ICH6M",	AHCI_Q_NOFORCE},
75261513Snwhitehorn	{0x26818086, 0x00, "Intel ESB2",	0},
76261513Snwhitehorn	{0x26828086, 0x00, "Intel ESB2",	0},
77261513Snwhitehorn	{0x26838086, 0x00, "Intel ESB2",	0},
78261513Snwhitehorn	{0x27c18086, 0x00, "Intel ICH7",	0},
79261513Snwhitehorn	{0x27c38086, 0x00, "Intel ICH7",	0},
8099652Sbenno	{0x27c58086, 0x00, "Intel ICH7M",	0},
81261513Snwhitehorn	{0x27c68086, 0x00, "Intel ICH7M",	0},
8294755Sbenno	{0x28218086, 0x00, "Intel ICH8",	0},
83282480Sandrew	{0x28228086, 0x00, "Intel ICH8",	0},
84261513Snwhitehorn	{0x28248086, 0x00, "Intel ICH8",	0},
85282480Sandrew	{0x28298086, 0x00, "Intel ICH8M",	0},
86261513Snwhitehorn	{0x282a8086, 0x00, "Intel ICH8M",	0},
87261513Snwhitehorn	{0x29228086, 0x00, "Intel ICH9",	0},
8894755Sbenno	{0x29238086, 0x00, "Intel ICH9",	0},
89256901Snwhitehorn	{0x29248086, 0x00, "Intel ICH9",	0},
90261513Snwhitehorn	{0x29258086, 0x00, "Intel ICH9",	0},
91261513Snwhitehorn	{0x29278086, 0x00, "Intel ICH9",	0},
92261513Snwhitehorn	{0x29298086, 0x00, "Intel ICH9M",	0},
9394755Sbenno	{0x292a8086, 0x00, "Intel ICH9M",	0},
94227843Smarius	{0x292b8086, 0x00, "Intel ICH9M",	0},
9594755Sbenno	{0x292c8086, 0x00, "Intel ICH9M",	0},
9694755Sbenno	{0x292f8086, 0x00, "Intel ICH9M",	0},
97280772Sian	{0x294d8086, 0x00, "Intel ICH9",	0},
98280772Sian	{0x294e8086, 0x00, "Intel ICH9M",	0},
99261513Snwhitehorn	{0x3a058086, 0x00, "Intel ICH10",	0},
100269594Sian	{0x3a228086, 0x00, "Intel ICH10",	0},
101269597Sian	{0x3a258086, 0x00, "Intel ICH10",	0},
102261513Snwhitehorn	{0x3b228086, 0x00, "Intel 5 Series/3400 Series",	0},
10394755Sbenno	{0x3b238086, 0x00, "Intel 5 Series/3400 Series",	0},
104282480Sandrew	{0x3b258086, 0x00, "Intel 5 Series/3400 Series",	0},
105261513Snwhitehorn	{0x3b298086, 0x00, "Intel 5 Series/3400 Series",	0},
106261513Snwhitehorn	{0x3b2c8086, 0x00, "Intel 5 Series/3400 Series",	0},
107261513Snwhitehorn	{0x3b2f8086, 0x00, "Intel 5 Series/3400 Series",	0},
108261513Snwhitehorn	{0x1c028086, 0x00, "Intel Cougar Point",	0},
109261513Snwhitehorn	{0x1c038086, 0x00, "Intel Cougar Point",	0},
110265888Snwhitehorn	{0x1c048086, 0x00, "Intel Cougar Point",	0},
111261513Snwhitehorn	{0x1c058086, 0x00, "Intel Cougar Point",	0},
112280772Sian	{0x1d028086, 0x00, "Intel Patsburg",	0},
113261513Snwhitehorn	{0x1d048086, 0x00, "Intel Patsburg",	0},
114261513Snwhitehorn	{0x1d068086, 0x00, "Intel Patsburg",	0},
115261513Snwhitehorn	{0x28268086, 0x00, "Intel Patsburg (RAID)",	0},
116282480Sandrew	{0x1e028086, 0x00, "Intel Panther Point",	0},
117261513Snwhitehorn	{0x1e038086, 0x00, "Intel Panther Point",	0},
118256901Snwhitehorn	{0x1e048086, 0x00, "Intel Panther Point (RAID)",	0},
119261513Snwhitehorn	{0x1e058086, 0x00, "Intel Panther Point (RAID)",	0},
12094755Sbenno	{0x1e068086, 0x00, "Intel Panther Point (RAID)",	0},
121256901Snwhitehorn	{0x1e078086, 0x00, "Intel Panther Point (RAID)",	0},
122282480Sandrew	{0x1e0e8086, 0x00, "Intel Panther Point (RAID)",	0},
123282480Sandrew	{0x1e0f8086, 0x00, "Intel Panther Point (RAID)",	0},
124282480Sandrew	{0x1f228086, 0x00, "Intel Avoton",	0},
125282480Sandrew	{0x1f238086, 0x00, "Intel Avoton",	0},
126282480Sandrew	{0x1f248086, 0x00, "Intel Avoton (RAID)",	0},
127261513Snwhitehorn	{0x1f258086, 0x00, "Intel Avoton (RAID)",	0},
128261513Snwhitehorn	{0x1f268086, 0x00, "Intel Avoton (RAID)",	0},
129178367Smarcel	{0x1f278086, 0x00, "Intel Avoton (RAID)",	0},
130178367Smarcel	{0x1f2e8086, 0x00, "Intel Avoton (RAID)",	0},
131178367Smarcel	{0x1f2f8086, 0x00, "Intel Avoton (RAID)",	0},
132261513Snwhitehorn	{0x1f328086, 0x00, "Intel Avoton",	0},
133178367Smarcel	{0x1f338086, 0x00, "Intel Avoton",	0},
134261513Snwhitehorn	{0x1f348086, 0x00, "Intel Avoton (RAID)",	0},
135256901Snwhitehorn	{0x1f358086, 0x00, "Intel Avoton (RAID)",	0},
136280772Sian	{0x1f368086, 0x00, "Intel Avoton (RAID)",	0},
13794755Sbenno	{0x1f378086, 0x00, "Intel Avoton (RAID)",	0},
13899652Sbenno	{0x1f3e8086, 0x00, "Intel Avoton (RAID)",	0},
13999652Sbenno	{0x1f3f8086, 0x00, "Intel Avoton (RAID)",	0},
140257075Snwhitehorn	{0x23a38086, 0x00, "Intel Coleto Creek",        0},
141171805Smarcel	{0x28238086, 0x00, "Intel Wellsburg (RAID)",	0},
142261513Snwhitehorn	{0x28278086, 0x00, "Intel Wellsburg (RAID)",	0},
143261513Snwhitehorn	{0x8c028086, 0x00, "Intel Lynx Point",	0},
144261513Snwhitehorn	{0x8c038086, 0x00, "Intel Lynx Point",	0},
145261513Snwhitehorn	{0x8c048086, 0x00, "Intel Lynx Point (RAID)",	0},
146261513Snwhitehorn	{0x8c058086, 0x00, "Intel Lynx Point (RAID)",	0},
147261513Snwhitehorn	{0x8c068086, 0x00, "Intel Lynx Point (RAID)",	0},
148280772Sian	{0x8c078086, 0x00, "Intel Lynx Point (RAID)",	0},
149280772Sian	{0x8c0e8086, 0x00, "Intel Lynx Point (RAID)",	0},
150280772Sian	{0x8c0f8086, 0x00, "Intel Lynx Point (RAID)",	0},
151280772Sian	{0x8c828086, 0x00, "Intel Wildcat Point",	0},
152280772Sian	{0x8c838086, 0x00, "Intel Wildcat Point",	0},
153257075Snwhitehorn	{0x8c848086, 0x00, "Intel Wildcat Point (RAID)",	0},
154257075Snwhitehorn	{0x8c858086, 0x00, "Intel Wildcat Point (RAID)",	0},
155257075Snwhitehorn	{0x8c868086, 0x00, "Intel Wildcat Point (RAID)",	0},
156257075Snwhitehorn	{0x8c878086, 0x00, "Intel Wildcat Point (RAID)",	0},
157257075Snwhitehorn	{0x8c8e8086, 0x00, "Intel Wildcat Point (RAID)",	0},
158257075Snwhitehorn	{0x8c8f8086, 0x00, "Intel Wildcat Point (RAID)",	0},
159257075Snwhitehorn	{0x8d028086, 0x00, "Intel Wellsburg",	0},
160257075Snwhitehorn	{0x8d048086, 0x00, "Intel Wellsburg (RAID)",	0},
161257075Snwhitehorn	{0x8d068086, 0x00, "Intel Wellsburg (RAID)",	0},
162171805Smarcel	{0x8d628086, 0x00, "Intel Wellsburg",	0},
163256901Snwhitehorn	{0x8d648086, 0x00, "Intel Wellsburg (RAID)",	0},
164256901Snwhitehorn	{0x8d668086, 0x00, "Intel Wellsburg (RAID)",	0},
165256901Snwhitehorn	{0x8d6e8086, 0x00, "Intel Wellsburg (RAID)",	0},
166256901Snwhitehorn	{0x9c028086, 0x00, "Intel Lynx Point-LP",	0},
167256901Snwhitehorn	{0x9c038086, 0x00, "Intel Lynx Point-LP",	0},
168256901Snwhitehorn	{0x9c048086, 0x00, "Intel Lynx Point-LP (RAID)",	0},
169256901Snwhitehorn	{0x9c058086, 0x00, "Intel Lynx Point-LP (RAID)",	0},
170256901Snwhitehorn	{0x9c068086, 0x00, "Intel Lynx Point-LP (RAID)",	0},
171256901Snwhitehorn	{0x9c078086, 0x00, "Intel Lynx Point-LP (RAID)",	0},
172280772Sian	{0x9c0e8086, 0x00, "Intel Lynx Point-LP (RAID)",	0},
173256901Snwhitehorn	{0x9c0f8086, 0x00, "Intel Lynx Point-LP (RAID)",	0},
174280772Sian	{0x23238086, 0x00, "Intel DH89xxCC",	0},
17594755Sbenno	{0x2360197b, 0x00, "JMicron JMB360",	0},
176178367Smarcel	{0x2361197b, 0x00, "JMicron JMB361",	AHCI_Q_NOFORCE},
17794755Sbenno	{0x2362197b, 0x00, "JMicron JMB362",	0},
17894755Sbenno	{0x2363197b, 0x00, "JMicron JMB363",	AHCI_Q_NOFORCE},
17999652Sbenno	{0x2365197b, 0x00, "JMicron JMB365",	AHCI_Q_NOFORCE},
180261513Snwhitehorn	{0x2366197b, 0x00, "JMicron JMB366",	AHCI_Q_NOFORCE},
181294883Sjhibbits	{0x2368197b, 0x00, "JMicron JMB368",	AHCI_Q_NOFORCE},
18299652Sbenno	{0x611111ab, 0x00, "Marvell 88SE6111",	AHCI_Q_NOFORCE | AHCI_Q_1CH |
183261513Snwhitehorn	    AHCI_Q_EDGEIS},
184256901Snwhitehorn	{0x612111ab, 0x00, "Marvell 88SE6121",	AHCI_Q_NOFORCE | AHCI_Q_2CH |
185171805Smarcel	    AHCI_Q_EDGEIS | AHCI_Q_NONCQ | AHCI_Q_NOCOUNT},
186256901Snwhitehorn	{0x614111ab, 0x00, "Marvell 88SE6141",	AHCI_Q_NOFORCE | AHCI_Q_4CH |
187256901Snwhitehorn	    AHCI_Q_EDGEIS | AHCI_Q_NONCQ | AHCI_Q_NOCOUNT},
18899652Sbenno	{0x614511ab, 0x00, "Marvell 88SE6145",	AHCI_Q_NOFORCE | AHCI_Q_4CH |
189256901Snwhitehorn	    AHCI_Q_EDGEIS | AHCI_Q_NONCQ | AHCI_Q_NOCOUNT},
190256901Snwhitehorn	{0x91201b4b, 0x00, "Marvell 88SE912x",	AHCI_Q_EDGEIS},
191257075Snwhitehorn	{0x91231b4b, 0x11, "Marvell 88SE912x",	AHCI_Q_ALTSIG},
192256901Snwhitehorn	{0x91231b4b, 0x00, "Marvell 88SE912x",	AHCI_Q_EDGEIS|AHCI_Q_SATA2},
193256901Snwhitehorn	{0x91251b4b, 0x00, "Marvell 88SE9125",	0},
194256901Snwhitehorn	{0x91281b4b, 0x00, "Marvell 88SE9128",	AHCI_Q_ALTSIG},
195256901Snwhitehorn	{0x91301b4b, 0x00, "Marvell 88SE9130",  AHCI_Q_ALTSIG},
196280772Sian	{0x91721b4b, 0x00, "Marvell 88SE9172",	0},
197280772Sian	{0x91821b4b, 0x00, "Marvell 88SE9182",	0},
198280772Sian	{0x91831b4b, 0x00, "Marvell 88SS9183",	0},
199280772Sian	{0x91a01b4b, 0x00, "Marvell 88SE91Ax",	0},
200256901Snwhitehorn	{0x92151b4b, 0x00, "Marvell 88SE9215",  0},
201280772Sian	{0x92201b4b, 0x00, "Marvell 88SE9220",  AHCI_Q_ALTSIG},
202256901Snwhitehorn	{0x92301b4b, 0x00, "Marvell 88SE9230",  AHCI_Q_ALTSIG},
203256901Snwhitehorn	{0x92351b4b, 0x00, "Marvell 88SE9235",  0},
204256901Snwhitehorn	{0x06201103, 0x00, "HighPoint RocketRAID 620",	0},
205256901Snwhitehorn	{0x06201b4b, 0x00, "HighPoint RocketRAID 620",	0},
206256901Snwhitehorn	{0x06221103, 0x00, "HighPoint RocketRAID 622",	0},
207256901Snwhitehorn	{0x06221b4b, 0x00, "HighPoint RocketRAID 622",	0},
208256901Snwhitehorn	{0x06401103, 0x00, "HighPoint RocketRAID 640",	0},
209256901Snwhitehorn	{0x06401b4b, 0x00, "HighPoint RocketRAID 640",	0},
210256901Snwhitehorn	{0x06441103, 0x00, "HighPoint RocketRAID 644",	0},
211256901Snwhitehorn	{0x06441b4b, 0x00, "HighPoint RocketRAID 644",	0},
212256901Snwhitehorn	{0x06411103, 0x00, "HighPoint RocketRAID 640L",	0},
213256901Snwhitehorn	{0x06421103, 0x00, "HighPoint RocketRAID 642L",	0},
214256901Snwhitehorn	{0x06451103, 0x00, "HighPoint RocketRAID 644L",	0},
21599652Sbenno	{0x044c10de, 0x00, "NVIDIA MCP65",	AHCI_Q_NOAA},
21699652Sbenno	{0x044d10de, 0x00, "NVIDIA MCP65",	AHCI_Q_NOAA},
21799652Sbenno	{0x044e10de, 0x00, "NVIDIA MCP65",	AHCI_Q_NOAA},
218256901Snwhitehorn	{0x044f10de, 0x00, "NVIDIA MCP65",	AHCI_Q_NOAA},
219256901Snwhitehorn	{0x045c10de, 0x00, "NVIDIA MCP65",	AHCI_Q_NOAA},
220256901Snwhitehorn	{0x045d10de, 0x00, "NVIDIA MCP65",	AHCI_Q_NOAA},
221171805Smarcel	{0x045e10de, 0x00, "NVIDIA MCP65",	AHCI_Q_NOAA},
222256901Snwhitehorn	{0x045f10de, 0x00, "NVIDIA MCP65",	AHCI_Q_NOAA},
223256901Snwhitehorn	{0x055010de, 0x00, "NVIDIA MCP67",	AHCI_Q_NOAA},
224256901Snwhitehorn	{0x055110de, 0x00, "NVIDIA MCP67",	AHCI_Q_NOAA},
225256901Snwhitehorn	{0x055210de, 0x00, "NVIDIA MCP67",	AHCI_Q_NOAA},
226256901Snwhitehorn	{0x055310de, 0x00, "NVIDIA MCP67",	AHCI_Q_NOAA},
227256901Snwhitehorn	{0x055410de, 0x00, "NVIDIA MCP67",	AHCI_Q_NOAA},
228171805Smarcel	{0x055510de, 0x00, "NVIDIA MCP67",	AHCI_Q_NOAA},
22999652Sbenno	{0x055610de, 0x00, "NVIDIA MCP67",	AHCI_Q_NOAA},
230256901Snwhitehorn	{0x055710de, 0x00, "NVIDIA MCP67",	AHCI_Q_NOAA},
231256901Snwhitehorn	{0x055810de, 0x00, "NVIDIA MCP67",	AHCI_Q_NOAA},
232256901Snwhitehorn	{0x055910de, 0x00, "NVIDIA MCP67",	AHCI_Q_NOAA},
233256901Snwhitehorn	{0x055A10de, 0x00, "NVIDIA MCP67",	AHCI_Q_NOAA},
234256901Snwhitehorn	{0x055B10de, 0x00, "NVIDIA MCP67",	AHCI_Q_NOAA},
235256901Snwhitehorn	{0x058410de, 0x00, "NVIDIA MCP67",	AHCI_Q_NOAA},
23699652Sbenno	{0x07f010de, 0x00, "NVIDIA MCP73",	AHCI_Q_NOAA},
23799652Sbenno	{0x07f110de, 0x00, "NVIDIA MCP73",	AHCI_Q_NOAA},
23899652Sbenno	{0x07f210de, 0x00, "NVIDIA MCP73",	AHCI_Q_NOAA},
23999652Sbenno	{0x07f310de, 0x00, "NVIDIA MCP73",	AHCI_Q_NOAA},
24099652Sbenno	{0x07f410de, 0x00, "NVIDIA MCP73",	AHCI_Q_NOAA},
241261513Snwhitehorn	{0x07f510de, 0x00, "NVIDIA MCP73",	AHCI_Q_NOAA},
242294883Sjhibbits	{0x07f610de, 0x00, "NVIDIA MCP73",	AHCI_Q_NOAA},
24399652Sbenno	{0x07f710de, 0x00, "NVIDIA MCP73",	AHCI_Q_NOAA},
244261513Snwhitehorn	{0x07f810de, 0x00, "NVIDIA MCP73",	AHCI_Q_NOAA},
245256901Snwhitehorn	{0x07f910de, 0x00, "NVIDIA MCP73",	AHCI_Q_NOAA},
246261513Snwhitehorn	{0x07fa10de, 0x00, "NVIDIA MCP73",	AHCI_Q_NOAA},
24799652Sbenno	{0x07fb10de, 0x00, "NVIDIA MCP73",	AHCI_Q_NOAA},
248261513Snwhitehorn	{0x0ad010de, 0x00, "NVIDIA MCP77",	AHCI_Q_NOAA},
249261513Snwhitehorn	{0x0ad110de, 0x00, "NVIDIA MCP77",	AHCI_Q_NOAA},
250261513Snwhitehorn	{0x0ad210de, 0x00, "NVIDIA MCP77",	AHCI_Q_NOAA},
251261513Snwhitehorn	{0x0ad310de, 0x00, "NVIDIA MCP77",	AHCI_Q_NOAA},
252256901Snwhitehorn	{0x0ad410de, 0x00, "NVIDIA MCP77",	AHCI_Q_NOAA},
253256901Snwhitehorn	{0x0ad510de, 0x00, "NVIDIA MCP77",	AHCI_Q_NOAA},
254256901Snwhitehorn	{0x0ad610de, 0x00, "NVIDIA MCP77",	AHCI_Q_NOAA},
255256901Snwhitehorn	{0x0ad710de, 0x00, "NVIDIA MCP77",	AHCI_Q_NOAA},
256256901Snwhitehorn	{0x0ad810de, 0x00, "NVIDIA MCP77",	AHCI_Q_NOAA},
257256901Snwhitehorn	{0x0ad910de, 0x00, "NVIDIA MCP77",	AHCI_Q_NOAA},
258256901Snwhitehorn	{0x0ada10de, 0x00, "NVIDIA MCP77",	AHCI_Q_NOAA},
259256901Snwhitehorn	{0x0adb10de, 0x00, "NVIDIA MCP77",	AHCI_Q_NOAA},
260124468Sgrehan	{0x0ab410de, 0x00, "NVIDIA MCP79",	AHCI_Q_NOAA},
26199652Sbenno	{0x0ab510de, 0x00, "NVIDIA MCP79",	AHCI_Q_NOAA},
262256901Snwhitehorn	{0x0ab610de, 0x00, "NVIDIA MCP79",	AHCI_Q_NOAA},
263256901Snwhitehorn	{0x0ab710de, 0x00, "NVIDIA MCP79",	AHCI_Q_NOAA},
264256901Snwhitehorn	{0x0ab810de, 0x00, "NVIDIA MCP79",	AHCI_Q_NOAA},
265256901Snwhitehorn	{0x0ab910de, 0x00, "NVIDIA MCP79",	AHCI_Q_NOAA},
266256901Snwhitehorn	{0x0aba10de, 0x00, "NVIDIA MCP79",	AHCI_Q_NOAA},
26799652Sbenno	{0x0abb10de, 0x00, "NVIDIA MCP79",	AHCI_Q_NOAA},
26899652Sbenno	{0x0abc10de, 0x00, "NVIDIA MCP79",	AHCI_Q_NOAA},
269256901Snwhitehorn	{0x0abd10de, 0x00, "NVIDIA MCP79",	AHCI_Q_NOAA},
270274249Szbb	{0x0abe10de, 0x00, "NVIDIA MCP79",	AHCI_Q_NOAA},
271256901Snwhitehorn	{0x0abf10de, 0x00, "NVIDIA MCP79",	AHCI_Q_NOAA},
27299652Sbenno	{0x0d8410de, 0x00, "NVIDIA MCP89",	AHCI_Q_NOAA},
273274249Szbb	{0x0d8510de, 0x00, "NVIDIA MCP89",	AHCI_Q_NOFORCE|AHCI_Q_NOAA},
274295754Szbb	{0x0d8610de, 0x00, "NVIDIA MCP89",	AHCI_Q_NOAA},
275256901Snwhitehorn	{0x0d8710de, 0x00, "NVIDIA MCP89",	AHCI_Q_NOAA},
27699652Sbenno	{0x0d8810de, 0x00, "NVIDIA MCP89",	AHCI_Q_NOAA},
277295754Szbb	{0x0d8910de, 0x00, "NVIDIA MCP89",	AHCI_Q_NOAA},
278295754Szbb	{0x0d8a10de, 0x00, "NVIDIA MCP89",	AHCI_Q_NOAA},
279295754Szbb	{0x0d8b10de, 0x00, "NVIDIA MCP89",	AHCI_Q_NOAA},
280295754Szbb	{0x0d8c10de, 0x00, "NVIDIA MCP89",	AHCI_Q_NOAA},
281295754Szbb	{0x0d8d10de, 0x00, "NVIDIA MCP89",	AHCI_Q_NOAA},
282295754Szbb	{0x0d8e10de, 0x00, "NVIDIA MCP89",	AHCI_Q_NOAA},
283295754Szbb	{0x0d8f10de, 0x00, "NVIDIA MCP89",	AHCI_Q_NOAA},
284295754Szbb	{0x3781105a, 0x00, "Promise TX8660",	0},
285274249Szbb	{0x33491106, 0x00, "VIA VT8251",	AHCI_Q_NOPMP|AHCI_Q_NONCQ},
286256901Snwhitehorn	{0x62871106, 0x00, "VIA VT8251",	AHCI_Q_NOPMP|AHCI_Q_NONCQ},
287256901Snwhitehorn	{0x11841039, 0x00, "SiS 966",		0},
288256901Snwhitehorn	{0x11851039, 0x00, "SiS 968",		0},
289256901Snwhitehorn	{0x01861039, 0x00, "SiS 968",		0},
290256855Snwhitehorn	{0x00000000, 0x00, NULL,		0}
291256901Snwhitehorn};
292256901Snwhitehorn
293static int
294ahci_pci_ctlr_reset(device_t dev)
295{
296
297	if (pci_read_config(dev, PCIR_DEVVENDOR, 4) == 0x28298086 &&
298	    (pci_read_config(dev, 0x92, 1) & 0xfe) == 0x04)
299		pci_write_config(dev, 0x92, 0x01, 1);
300	return ahci_ctlr_reset(dev);
301}
302
303static int
304ahci_probe(device_t dev)
305{
306	char buf[64];
307	int i, valid = 0;
308	uint32_t devid = pci_get_devid(dev);
309	uint8_t revid = pci_get_revid(dev);
310
311	/*
312	 * Ensure it is not a PCI bridge (some vendors use
313	 * the same PID and VID in PCI bridge and AHCI cards).
314	 */
315	if (pci_get_class(dev) == PCIC_BRIDGE)
316		return (ENXIO);
317
318	/* Is this a possible AHCI candidate? */
319	if (pci_get_class(dev) == PCIC_STORAGE &&
320	    pci_get_subclass(dev) == PCIS_STORAGE_SATA &&
321	    pci_get_progif(dev) == PCIP_STORAGE_SATA_AHCI_1_0)
322		valid = 1;
323	/* Is this a known AHCI chip? */
324	for (i = 0; ahci_ids[i].id != 0; i++) {
325		if (ahci_ids[i].id == devid &&
326		    ahci_ids[i].rev <= revid &&
327		    (valid || (force_ahci == 1 &&
328		     !(ahci_ids[i].quirks & AHCI_Q_NOFORCE)))) {
329			/* Do not attach JMicrons with single PCI function. */
330			if (pci_get_vendor(dev) == 0x197b &&
331			    (pci_read_config(dev, 0xdf, 1) & 0x40) == 0)
332				return (ENXIO);
333			snprintf(buf, sizeof(buf), "%s AHCI SATA controller",
334			    ahci_ids[i].name);
335			device_set_desc_copy(dev, buf);
336			return (BUS_PROBE_VENDOR);
337		}
338	}
339	if (!valid)
340		return (ENXIO);
341	device_set_desc_copy(dev, "AHCI SATA controller");
342	return (BUS_PROBE_VENDOR);
343}
344
345static int
346ahci_ata_probe(device_t dev)
347{
348	char buf[64];
349	int i;
350	uint32_t devid = pci_get_devid(dev);
351	uint8_t revid = pci_get_revid(dev);
352
353	if ((intptr_t)device_get_ivars(dev) >= 0)
354		return (ENXIO);
355	/* Is this a known AHCI chip? */
356	for (i = 0; ahci_ids[i].id != 0; i++) {
357		if (ahci_ids[i].id == devid &&
358		    ahci_ids[i].rev <= revid) {
359			snprintf(buf, sizeof(buf), "%s AHCI SATA controller",
360			    ahci_ids[i].name);
361			device_set_desc_copy(dev, buf);
362			return (BUS_PROBE_VENDOR);
363		}
364	}
365	device_set_desc_copy(dev, "AHCI SATA controller");
366	return (BUS_PROBE_VENDOR);
367}
368
369static int
370ahci_pci_attach(device_t dev)
371{
372	struct ahci_controller *ctlr = device_get_softc(dev);
373	int	error, i;
374	uint32_t devid = pci_get_devid(dev);
375	uint8_t revid = pci_get_revid(dev);
376
377	i = 0;
378	while (ahci_ids[i].id != 0 &&
379	    (ahci_ids[i].id != devid ||
380	     ahci_ids[i].rev > revid))
381		i++;
382	ctlr->quirks = ahci_ids[i].quirks;
383	/* Limit speed for my onboard JMicron external port.
384	 * It is not eSATA really, limit to SATA 1 */
385	if (pci_get_devid(dev) == 0x2363197b &&
386	    pci_get_subvendor(dev) == 0x1043 &&
387	    pci_get_subdevice(dev) == 0x81e4)
388		ctlr->quirks |= AHCI_Q_SATA1_UNIT0;
389	/* if we have a memory BAR(5) we are likely on an AHCI part */
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	ctlr->r_rid = PCIR_BAR(5);
395	if (!(ctlr->r_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
396	    &ctlr->r_rid, RF_ACTIVE)))
397		return ENXIO;
398	pci_enable_busmaster(dev);
399	/* Reset controller */
400	if ((error = ahci_pci_ctlr_reset(dev)) != 0) {
401		bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem);
402		return (error);
403	};
404
405	/* Setup interrupts. */
406
407	/* Setup MSI register parameters */
408	ctlr->msi = 2;
409	/* Process hints. */
410	if (ctlr->quirks & AHCI_Q_NOMSI)
411		ctlr->msi = 0;
412	resource_int_value(device_get_name(dev),
413	    device_get_unit(dev), "msi", &ctlr->msi);
414	ctlr->numirqs = 1;
415	if (ctlr->msi < 0)
416		ctlr->msi = 0;
417	else if (ctlr->msi == 1)
418		ctlr->msi = min(1, pci_msi_count(dev));
419	else if (ctlr->msi > 1) {
420		ctlr->msi = 2;
421		ctlr->numirqs = pci_msi_count(dev);
422	}
423	/* Allocate MSI if needed/present. */
424	if (ctlr->msi && pci_alloc_msi(dev, &ctlr->numirqs) != 0) {
425		ctlr->msi = 0;
426		ctlr->numirqs = 1;
427	}
428
429	error = ahci_attach(dev);
430	if (error != 0)
431		if (ctlr->msi)
432			pci_release_msi(dev);
433	return error;
434}
435
436static int
437ahci_pci_detach(device_t dev)
438{
439
440	ahci_detach(dev);
441	pci_release_msi(dev);
442	return (0);
443}
444
445static int
446ahci_pci_suspend(device_t dev)
447{
448	struct ahci_controller *ctlr = device_get_softc(dev);
449
450	bus_generic_suspend(dev);
451	/* Disable interupts, so the state change(s) doesn't trigger */
452	ATA_OUTL(ctlr->r_mem, AHCI_GHC,
453	     ATA_INL(ctlr->r_mem, AHCI_GHC) & (~AHCI_GHC_IE));
454	return 0;
455}
456
457static int
458ahci_pci_resume(device_t dev)
459{
460	int res;
461
462	if ((res = ahci_pci_ctlr_reset(dev)) != 0)
463		return (res);
464	ahci_ctlr_setup(dev);
465	return (bus_generic_resume(dev));
466}
467
468devclass_t ahci_devclass;
469static device_method_t ahci_methods[] = {
470	DEVMETHOD(device_probe,     ahci_probe),
471	DEVMETHOD(device_attach,    ahci_pci_attach),
472	DEVMETHOD(device_detach,    ahci_pci_detach),
473	DEVMETHOD(device_suspend,   ahci_pci_suspend),
474	DEVMETHOD(device_resume,    ahci_pci_resume),
475	DEVMETHOD(bus_print_child,  ahci_print_child),
476	DEVMETHOD(bus_alloc_resource,       ahci_alloc_resource),
477	DEVMETHOD(bus_release_resource,     ahci_release_resource),
478	DEVMETHOD(bus_setup_intr,   ahci_setup_intr),
479	DEVMETHOD(bus_teardown_intr,ahci_teardown_intr),
480	DEVMETHOD(bus_child_location_str, ahci_child_location_str),
481	DEVMETHOD(bus_get_dma_tag,  ahci_get_dma_tag),
482	{ 0, 0 }
483};
484static driver_t ahci_driver = {
485        "ahci",
486        ahci_methods,
487        sizeof(struct ahci_controller)
488};
489DRIVER_MODULE(ahci, pci, ahci_driver, ahci_devclass, 0, 0);
490static device_method_t ahci_ata_methods[] = {
491	DEVMETHOD(device_probe,     ahci_ata_probe),
492	DEVMETHOD(device_attach,    ahci_pci_attach),
493	DEVMETHOD(device_detach,    ahci_pci_detach),
494	DEVMETHOD(device_suspend,   ahci_pci_suspend),
495	DEVMETHOD(device_resume,    ahci_pci_resume),
496	DEVMETHOD(bus_print_child,  ahci_print_child),
497	DEVMETHOD(bus_alloc_resource,       ahci_alloc_resource),
498	DEVMETHOD(bus_release_resource,     ahci_release_resource),
499	DEVMETHOD(bus_setup_intr,   ahci_setup_intr),
500	DEVMETHOD(bus_teardown_intr,ahci_teardown_intr),
501	DEVMETHOD(bus_child_location_str, ahci_child_location_str),
502	{ 0, 0 }
503};
504static driver_t ahci_ata_driver = {
505        "ahci",
506        ahci_ata_methods,
507        sizeof(struct ahci_controller)
508};
509DRIVER_MODULE(ahci, atapci, ahci_ata_driver, ahci_devclass, 0, 0);
510