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/param.h>
28802df3acSWarner Losh #include <sys/module.h>
29802df3acSWarner Losh #include <sys/systm.h>
30802df3acSWarner Losh #include <sys/kernel.h>
31802df3acSWarner Losh #include <sys/bus.h>
32802df3acSWarner Losh #include <sys/conf.h>
33802df3acSWarner Losh #include <sys/endian.h>
34802df3acSWarner Losh #include <sys/malloc.h>
35802df3acSWarner Losh #include <sys/lock.h>
36802df3acSWarner Losh #include <sys/mutex.h>
37c489ab61SRuslan Bukin #include <sys/sysctl.h>
38c489ab61SRuslan Bukin #include <sys/taskqueue.h>
39c489ab61SRuslan Bukin #include <sys/tree.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>
46c489ab61SRuslan Bukin #include <dev/iommu/iommu.h>
47802df3acSWarner Losh #include "ahci.h"
48802df3acSWarner Losh
49802df3acSWarner Losh static int force_ahci = 1;
50802df3acSWarner Losh TUNABLE_INT("hw.ahci.force", &force_ahci);
51802df3acSWarner Losh
5221190895SMarius Strobl static const struct {
53802df3acSWarner Losh uint32_t id;
54802df3acSWarner Losh uint8_t rev;
55802df3acSWarner Losh const char *name;
56802df3acSWarner Losh int quirks;
57802df3acSWarner Losh } ahci_ids[] = {
58802df3acSWarner Losh {0x43801002, 0x00, "AMD SB600",
59802df3acSWarner Losh AHCI_Q_NOMSI | AHCI_Q_ATI_PMP_BUG | AHCI_Q_MAXIO_64K},
60b1877a0bSSteven Hartland {0x43901002, 0x00, "AMD SB7x0/SB8x0/SB9x0",
61b1877a0bSSteven Hartland AHCI_Q_ATI_PMP_BUG | AHCI_Q_1MSI},
62b1877a0bSSteven Hartland {0x43911002, 0x00, "AMD SB7x0/SB8x0/SB9x0",
63b1877a0bSSteven Hartland AHCI_Q_ATI_PMP_BUG | AHCI_Q_1MSI},
64b1877a0bSSteven Hartland {0x43921002, 0x00, "AMD SB7x0/SB8x0/SB9x0",
65b1877a0bSSteven Hartland AHCI_Q_ATI_PMP_BUG | AHCI_Q_1MSI},
66b1877a0bSSteven Hartland {0x43931002, 0x00, "AMD SB7x0/SB8x0/SB9x0",
67b1877a0bSSteven Hartland AHCI_Q_ATI_PMP_BUG | AHCI_Q_1MSI},
68b1877a0bSSteven Hartland {0x43941002, 0x00, "AMD SB7x0/SB8x0/SB9x0",
69b1877a0bSSteven Hartland AHCI_Q_ATI_PMP_BUG | AHCI_Q_1MSI},
70802df3acSWarner Losh /* Not sure SB8x0/SB9x0 needs this quirk. Be conservative though */
71802df3acSWarner Losh {0x43951002, 0x00, "AMD SB8x0/SB9x0", AHCI_Q_ATI_PMP_BUG},
7266ad2538SConrad Meyer {0x43b61022, 0x00, "AMD X399", 0},
73ca0a66baSMark Johnston {0x43b51022, 0x00, "AMD 300 Series", 0}, /* X370 */
74ca0a66baSMark Johnston {0x43b71022, 0x00, "AMD 300 Series", 0}, /* B350 */
75*4cc4b5e2SDmitry Luhtionov {0x43c81022, 0x00, "AMD 400 Series", 0}, /* B450 */
76*4cc4b5e2SDmitry Luhtionov {0x43eb1022, 0x00, "AMD 500 Series", 0},
77*4cc4b5e2SDmitry Luhtionov {0x43f61022, 0x00, "AMD 600 Series", 0}, /* X670 */
78802df3acSWarner Losh {0x78001022, 0x00, "AMD Hudson-2", 0},
79802df3acSWarner Losh {0x78011022, 0x00, "AMD Hudson-2", 0},
80802df3acSWarner Losh {0x78021022, 0x00, "AMD Hudson-2", 0},
81802df3acSWarner Losh {0x78031022, 0x00, "AMD Hudson-2", 0},
82802df3acSWarner Losh {0x78041022, 0x00, "AMD Hudson-2", 0},
83ede4c91dSAlexander Motin {0x79001022, 0x00, "AMD KERNCZ", 0},
84ede4c91dSAlexander Motin {0x79011022, 0x00, "AMD KERNCZ", 0},
85ede4c91dSAlexander Motin {0x79021022, 0x00, "AMD KERNCZ", 0},
86ede4c91dSAlexander Motin {0x79031022, 0x00, "AMD KERNCZ", 0},
87ede4c91dSAlexander Motin {0x79041022, 0x00, "AMD KERNCZ", 0},
883afe6b52SAndrey V. Elsukov {0x79161022, 0x00, "AMD KERNCZ (RAID)", 0},
897ce90968SAlexander Motin {0x06011b21, 0x00, "ASMedia ASM1060", AHCI_Q_NOCCS|AHCI_Q_NOAUX},
907ce90968SAlexander Motin {0x06021b21, 0x00, "ASMedia ASM1060", AHCI_Q_NOCCS|AHCI_Q_NOAUX},
917ce90968SAlexander Motin {0x06111b21, 0x00, "ASMedia ASM1061", AHCI_Q_NOCCS|AHCI_Q_NOAUX},
927ce90968SAlexander Motin {0x06121b21, 0x00, "ASMedia ASM1062", AHCI_Q_NOCCS|AHCI_Q_NOAUX},
937ce90968SAlexander Motin {0x06201b21, 0x00, "ASMedia ASM106x", AHCI_Q_NOCCS|AHCI_Q_NOAUX},
947ce90968SAlexander Motin {0x06211b21, 0x00, "ASMedia ASM106x", AHCI_Q_NOCCS|AHCI_Q_NOAUX},
957ce90968SAlexander Motin {0x06221b21, 0x00, "ASMedia ASM106x", AHCI_Q_NOCCS|AHCI_Q_NOAUX},
967ce90968SAlexander Motin {0x06241b21, 0x00, "ASMedia ASM106x", AHCI_Q_NOCCS|AHCI_Q_NOAUX},
977ce90968SAlexander Motin {0x06251b21, 0x00, "ASMedia ASM106x", AHCI_Q_NOCCS|AHCI_Q_NOAUX},
985a8d32b5SAlexander Motin {0x10621b21, 0x00, "ASMedia ASM116x", 0},
995a8d32b5SAlexander Motin {0x10641b21, 0x00, "ASMedia ASM116x", 0},
1005a8d32b5SAlexander Motin {0x11641b21, 0x00, "ASMedia ASM116x", 0},
1015a8d32b5SAlexander Motin {0x11651b21, 0x00, "ASMedia ASM116x", 0},
1025a8d32b5SAlexander Motin {0x11661b21, 0x00, "ASMedia ASM116x", 0},
103a6da07a3SAlexander Motin {0x79011d94, 0x00, "Hygon KERNCZ", 0},
10405d1c205SDmitry Luhtionov {0x0f228086, 0x00, "Intel BayTrail", 0},
10505d1c205SDmitry Luhtionov {0x0f238086, 0x00, "Intel BayTrail", 0},
106802df3acSWarner Losh {0x26528086, 0x00, "Intel ICH6", AHCI_Q_NOFORCE},
107802df3acSWarner Losh {0x26538086, 0x00, "Intel ICH6M", AHCI_Q_NOFORCE},
108802df3acSWarner Losh {0x26818086, 0x00, "Intel ESB2", 0},
109802df3acSWarner Losh {0x26828086, 0x00, "Intel ESB2", 0},
110802df3acSWarner Losh {0x26838086, 0x00, "Intel ESB2", 0},
111802df3acSWarner Losh {0x27c18086, 0x00, "Intel ICH7", 0},
112802df3acSWarner Losh {0x27c38086, 0x00, "Intel ICH7", 0},
113802df3acSWarner Losh {0x27c58086, 0x00, "Intel ICH7M", 0},
114802df3acSWarner Losh {0x27c68086, 0x00, "Intel ICH7M", 0},
115802df3acSWarner Losh {0x28218086, 0x00, "Intel ICH8", 0},
116aaa9b2b3SAlexander Motin {0x28228086, 0x00, "Intel ICH8+ (RAID)", 0},
117802df3acSWarner Losh {0x28248086, 0x00, "Intel ICH8", 0},
118802df3acSWarner Losh {0x28298086, 0x00, "Intel ICH8M", 0},
119aaa9b2b3SAlexander Motin {0x282a8086, 0x00, "Intel ICH8M+ (RAID)", 0},
120802df3acSWarner Losh {0x29228086, 0x00, "Intel ICH9", 0},
121802df3acSWarner Losh {0x29238086, 0x00, "Intel ICH9", 0},
122802df3acSWarner Losh {0x29248086, 0x00, "Intel ICH9", 0},
123802df3acSWarner Losh {0x29258086, 0x00, "Intel ICH9", 0},
124802df3acSWarner Losh {0x29278086, 0x00, "Intel ICH9", 0},
125802df3acSWarner Losh {0x29298086, 0x00, "Intel ICH9M", 0},
126802df3acSWarner Losh {0x292a8086, 0x00, "Intel ICH9M", 0},
127802df3acSWarner Losh {0x292b8086, 0x00, "Intel ICH9M", 0},
128802df3acSWarner Losh {0x292c8086, 0x00, "Intel ICH9M", 0},
129802df3acSWarner Losh {0x292f8086, 0x00, "Intel ICH9M", 0},
130802df3acSWarner Losh {0x294d8086, 0x00, "Intel ICH9", 0},
131802df3acSWarner Losh {0x294e8086, 0x00, "Intel ICH9M", 0},
132f969fb53SDmitry Luhtionov {0x3a028086, 0x00, "Intel ICH10", 0},
133aaa9b2b3SAlexander Motin {0x3a058086, 0x00, "Intel ICH10 (RAID)", 0},
134802df3acSWarner Losh {0x3a228086, 0x00, "Intel ICH10", 0},
135aaa9b2b3SAlexander Motin {0x3a258086, 0x00, "Intel ICH10 (RAID)", 0},
1366750c3d0SAlexander Motin {0x3b228086, 0x00, "Intel Ibex Peak", 0},
1376750c3d0SAlexander Motin {0x3b238086, 0x00, "Intel Ibex Peak", 0},
1386750c3d0SAlexander Motin {0x3b258086, 0x00, "Intel Ibex Peak (RAID)", 0},
1396750c3d0SAlexander Motin {0x3b298086, 0x00, "Intel Ibex Peak-M", 0},
1406750c3d0SAlexander Motin {0x3b2c8086, 0x00, "Intel Ibex Peak-M (RAID)", 0},
1412b2a6eb9SAlexander Motin {0x3b2f8086, 0x00, "Intel Ibex Peak-M", 0},
142288a9225SAdrian Chadd {0x06d68086, 0x00, "Intel Comet Lake (RAID)", 0},
143eb76cfbcSAlexander Motin {0x19b08086, 0x00, "Intel Denverton", 0},
144eb76cfbcSAlexander Motin {0x19b18086, 0x00, "Intel Denverton", 0},
145eb76cfbcSAlexander Motin {0x19b28086, 0x00, "Intel Denverton", 0},
146eb76cfbcSAlexander Motin {0x19b38086, 0x00, "Intel Denverton", 0},
147eb76cfbcSAlexander Motin {0x19b48086, 0x00, "Intel Denverton", 0},
148eb76cfbcSAlexander Motin {0x19b58086, 0x00, "Intel Denverton", 0},
149eb76cfbcSAlexander Motin {0x19b68086, 0x00, "Intel Denverton", 0},
150eb76cfbcSAlexander Motin {0x19b78086, 0x00, "Intel Denverton", 0},
151eb76cfbcSAlexander Motin {0x19be8086, 0x00, "Intel Denverton", 0},
152eb76cfbcSAlexander Motin {0x19bf8086, 0x00, "Intel Denverton", 0},
153eb76cfbcSAlexander Motin {0x19c08086, 0x00, "Intel Denverton", 0},
154eb76cfbcSAlexander Motin {0x19c18086, 0x00, "Intel Denverton", 0},
155eb76cfbcSAlexander Motin {0x19c28086, 0x00, "Intel Denverton", 0},
156eb76cfbcSAlexander Motin {0x19c38086, 0x00, "Intel Denverton", 0},
157eb76cfbcSAlexander Motin {0x19c48086, 0x00, "Intel Denverton", 0},
158eb76cfbcSAlexander Motin {0x19c58086, 0x00, "Intel Denverton", 0},
159eb76cfbcSAlexander Motin {0x19c68086, 0x00, "Intel Denverton", 0},
160eb76cfbcSAlexander Motin {0x19c78086, 0x00, "Intel Denverton", 0},
161eb76cfbcSAlexander Motin {0x19ce8086, 0x00, "Intel Denverton", 0},
162eb76cfbcSAlexander Motin {0x19cf8086, 0x00, "Intel Denverton", 0},
163802df3acSWarner Losh {0x1c028086, 0x00, "Intel Cougar Point", 0},
164802df3acSWarner Losh {0x1c038086, 0x00, "Intel Cougar Point", 0},
165aaa9b2b3SAlexander Motin {0x1c048086, 0x00, "Intel Cougar Point (RAID)", 0},
166aaa9b2b3SAlexander Motin {0x1c058086, 0x00, "Intel Cougar Point (RAID)", 0},
167aaa9b2b3SAlexander Motin {0x1c068086, 0x00, "Intel Cougar Point (RAID)", 0},
168802df3acSWarner Losh {0x1d028086, 0x00, "Intel Patsburg", 0},
169802df3acSWarner Losh {0x1d048086, 0x00, "Intel Patsburg", 0},
170802df3acSWarner Losh {0x1d068086, 0x00, "Intel Patsburg", 0},
171aaa9b2b3SAlexander Motin {0x28268086, 0x00, "Intel Patsburg+ (RAID)", 0},
172802df3acSWarner Losh {0x1e028086, 0x00, "Intel Panther Point", 0},
173802df3acSWarner Losh {0x1e038086, 0x00, "Intel Panther Point", 0},
174802df3acSWarner Losh {0x1e048086, 0x00, "Intel Panther Point (RAID)", 0},
175802df3acSWarner Losh {0x1e058086, 0x00, "Intel Panther Point (RAID)", 0},
176802df3acSWarner Losh {0x1e068086, 0x00, "Intel Panther Point (RAID)", 0},
177802df3acSWarner Losh {0x1e078086, 0x00, "Intel Panther Point (RAID)", 0},
178802df3acSWarner Losh {0x1e0e8086, 0x00, "Intel Panther Point (RAID)", 0},
179802df3acSWarner Losh {0x1e0f8086, 0x00, "Intel Panther Point (RAID)", 0},
180802df3acSWarner Losh {0x1f228086, 0x00, "Intel Avoton", 0},
181802df3acSWarner Losh {0x1f238086, 0x00, "Intel Avoton", 0},
182802df3acSWarner Losh {0x1f248086, 0x00, "Intel Avoton (RAID)", 0},
183802df3acSWarner Losh {0x1f258086, 0x00, "Intel Avoton (RAID)", 0},
184802df3acSWarner Losh {0x1f268086, 0x00, "Intel Avoton (RAID)", 0},
185802df3acSWarner Losh {0x1f278086, 0x00, "Intel Avoton (RAID)", 0},
186802df3acSWarner Losh {0x1f2e8086, 0x00, "Intel Avoton (RAID)", 0},
187802df3acSWarner Losh {0x1f2f8086, 0x00, "Intel Avoton (RAID)", 0},
188802df3acSWarner Losh {0x1f328086, 0x00, "Intel Avoton", 0},
189802df3acSWarner Losh {0x1f338086, 0x00, "Intel Avoton", 0},
190802df3acSWarner Losh {0x1f348086, 0x00, "Intel Avoton (RAID)", 0},
191802df3acSWarner Losh {0x1f358086, 0x00, "Intel Avoton (RAID)", 0},
192802df3acSWarner Losh {0x1f368086, 0x00, "Intel Avoton (RAID)", 0},
193802df3acSWarner Losh {0x1f378086, 0x00, "Intel Avoton (RAID)", 0},
194802df3acSWarner Losh {0x1f3e8086, 0x00, "Intel Avoton (RAID)", 0},
195802df3acSWarner Losh {0x1f3f8086, 0x00, "Intel Avoton (RAID)", 0},
196802df3acSWarner Losh {0x23a38086, 0x00, "Intel Coleto Creek", 0},
197006e2b2bSAlexander Motin {0x31e38086, 0x00, "Intel Gemini Lake", 0},
19832e50ae4SAlexander Motin {0x5ae38086, 0x00, "Intel Apollo Lake", 0},
199dc238358SAlexander Motin {0x7ae28086, 0x00, "Intel Alder Lake", 0},
200802df3acSWarner Losh {0x8c028086, 0x00, "Intel Lynx Point", 0},
201802df3acSWarner Losh {0x8c038086, 0x00, "Intel Lynx Point", 0},
202802df3acSWarner Losh {0x8c048086, 0x00, "Intel Lynx Point (RAID)", 0},
203802df3acSWarner Losh {0x8c058086, 0x00, "Intel Lynx Point (RAID)", 0},
204802df3acSWarner Losh {0x8c068086, 0x00, "Intel Lynx Point (RAID)", 0},
205802df3acSWarner Losh {0x8c078086, 0x00, "Intel Lynx Point (RAID)", 0},
206802df3acSWarner Losh {0x8c0e8086, 0x00, "Intel Lynx Point (RAID)", 0},
207802df3acSWarner Losh {0x8c0f8086, 0x00, "Intel Lynx Point (RAID)", 0},
208e67f3becSAlexander Motin {0x8c828086, 0x00, "Intel Wildcat Point", 0},
209e67f3becSAlexander Motin {0x8c838086, 0x00, "Intel Wildcat Point", 0},
210e67f3becSAlexander Motin {0x8c848086, 0x00, "Intel Wildcat Point (RAID)", 0},
211e67f3becSAlexander Motin {0x8c858086, 0x00, "Intel Wildcat Point (RAID)", 0},
212e67f3becSAlexander Motin {0x8c868086, 0x00, "Intel Wildcat Point (RAID)", 0},
213e67f3becSAlexander Motin {0x8c878086, 0x00, "Intel Wildcat Point (RAID)", 0},
214e67f3becSAlexander Motin {0x8c8e8086, 0x00, "Intel Wildcat Point (RAID)", 0},
215e67f3becSAlexander Motin {0x8c8f8086, 0x00, "Intel Wildcat Point (RAID)", 0},
216802df3acSWarner Losh {0x8d028086, 0x00, "Intel Wellsburg", 0},
217802df3acSWarner Losh {0x8d048086, 0x00, "Intel Wellsburg (RAID)", 0},
218802df3acSWarner Losh {0x8d068086, 0x00, "Intel Wellsburg (RAID)", 0},
219802df3acSWarner Losh {0x8d628086, 0x00, "Intel Wellsburg", 0},
220802df3acSWarner Losh {0x8d648086, 0x00, "Intel Wellsburg (RAID)", 0},
221802df3acSWarner Losh {0x8d668086, 0x00, "Intel Wellsburg (RAID)", 0},
222802df3acSWarner Losh {0x8d6e8086, 0x00, "Intel Wellsburg (RAID)", 0},
223aaa9b2b3SAlexander Motin {0x28238086, 0x00, "Intel Wellsburg+ (RAID)", 0},
224aaa9b2b3SAlexander Motin {0x28278086, 0x00, "Intel Wellsburg+ (RAID)", 0},
225802df3acSWarner Losh {0x9c028086, 0x00, "Intel Lynx Point-LP", 0},
226802df3acSWarner Losh {0x9c038086, 0x00, "Intel Lynx Point-LP", 0},
227802df3acSWarner Losh {0x9c048086, 0x00, "Intel Lynx Point-LP (RAID)", 0},
228802df3acSWarner Losh {0x9c058086, 0x00, "Intel Lynx Point-LP (RAID)", 0},
229802df3acSWarner Losh {0x9c068086, 0x00, "Intel Lynx Point-LP (RAID)", 0},
230802df3acSWarner Losh {0x9c078086, 0x00, "Intel Lynx Point-LP (RAID)", 0},
231802df3acSWarner Losh {0x9c0e8086, 0x00, "Intel Lynx Point-LP (RAID)", 0},
232802df3acSWarner Losh {0x9c0f8086, 0x00, "Intel Lynx Point-LP (RAID)", 0},
233d93f6d3aSAlexander Motin {0x9c838086, 0x00, "Intel Wildcat Point-LP", 0},
234d93f6d3aSAlexander Motin {0x9c858086, 0x00, "Intel Wildcat Point-LP (RAID)", 0},
235d93f6d3aSAlexander Motin {0x9c878086, 0x00, "Intel Wildcat Point-LP (RAID)", 0},
236d93f6d3aSAlexander Motin {0x9c8f8086, 0x00, "Intel Wildcat Point-LP (RAID)", 0},
23771733a50SAlexander Motin {0x9d038086, 0x00, "Intel Sunrise Point-LP", 0},
23871733a50SAlexander Motin {0x9d058086, 0x00, "Intel Sunrise Point-LP (RAID)", 0},
23971733a50SAlexander Motin {0x9d078086, 0x00, "Intel Sunrise Point-LP (RAID)", 0},
24071733a50SAlexander Motin {0xa1028086, 0x00, "Intel Sunrise Point", 0},
24171733a50SAlexander Motin {0xa1038086, 0x00, "Intel Sunrise Point", 0},
24271733a50SAlexander Motin {0xa1058086, 0x00, "Intel Sunrise Point (RAID)", 0},
24371733a50SAlexander Motin {0xa1068086, 0x00, "Intel Sunrise Point (RAID)", 0},
24471733a50SAlexander Motin {0xa1078086, 0x00, "Intel Sunrise Point (RAID)", 0},
24571733a50SAlexander Motin {0xa10f8086, 0x00, "Intel Sunrise Point (RAID)", 0},
246aaa9b2b3SAlexander Motin {0xa1828086, 0x00, "Intel Lewisburg", 0},
247aaa9b2b3SAlexander Motin {0xa1868086, 0x00, "Intel Lewisburg (RAID)", 0},
248aaa9b2b3SAlexander Motin {0xa1d28086, 0x00, "Intel Lewisburg", 0},
249aaa9b2b3SAlexander Motin {0xa1d68086, 0x00, "Intel Lewisburg (RAID)", 0},
250aaa9b2b3SAlexander Motin {0xa2028086, 0x00, "Intel Lewisburg", 0},
251aaa9b2b3SAlexander Motin {0xa2068086, 0x00, "Intel Lewisburg (RAID)", 0},
252aaa9b2b3SAlexander Motin {0xa2528086, 0x00, "Intel Lewisburg", 0},
253aaa9b2b3SAlexander Motin {0xa2568086, 0x00, "Intel Lewisburg (RAID)", 0},
254aaa9b2b3SAlexander Motin {0xa2828086, 0x00, "Intel Union Point", 0},
255aaa9b2b3SAlexander Motin {0xa2868086, 0x00, "Intel Union Point (RAID)", 0},
256aaa9b2b3SAlexander Motin {0xa28e8086, 0x00, "Intel Union Point (RAID)", 0},
257abb7ac4dSAlexander Motin {0xa3528086, 0x00, "Intel Cannon Lake", 0},
258abb7ac4dSAlexander Motin {0xa3538086, 0x00, "Intel Cannon Lake", 0},
259802df3acSWarner Losh {0x23238086, 0x00, "Intel DH89xxCC", 0},
260802df3acSWarner Losh {0x2360197b, 0x00, "JMicron JMB360", 0},
2616975a721SAndriy Gapon {0x2361197b, 0x00, "JMicron JMB361", AHCI_Q_NOFORCE | AHCI_Q_1CH},
262802df3acSWarner Losh {0x2362197b, 0x00, "JMicron JMB362", 0},
263802df3acSWarner Losh {0x2363197b, 0x00, "JMicron JMB363", AHCI_Q_NOFORCE},
264802df3acSWarner Losh {0x2365197b, 0x00, "JMicron JMB365", AHCI_Q_NOFORCE},
265802df3acSWarner Losh {0x2366197b, 0x00, "JMicron JMB366", AHCI_Q_NOFORCE},
266802df3acSWarner Losh {0x2368197b, 0x00, "JMicron JMB368", AHCI_Q_NOFORCE},
267c489ab61SRuslan Bukin {0x2392197b, 0x00, "JMicron JMB388", AHCI_Q_IOMMU_BUSWIDE},
26816b766eeSAlexander Motin {0x0585197b, 0x00, "JMicron JMB58x", 0},
269c489ab61SRuslan Bukin {0x01221c28, 0x00, "Lite-On Plextor M6E (Marvell 88SS9183)",
270c489ab61SRuslan Bukin AHCI_Q_IOMMU_BUSWIDE},
271556e4c9aSAlexander Motin {0x611111ab, 0x00, "Marvell 88SE6111", AHCI_Q_NOFORCE | AHCI_Q_NOPMP |
272556e4c9aSAlexander Motin AHCI_Q_1CH | AHCI_Q_EDGEIS},
273556e4c9aSAlexander Motin {0x612111ab, 0x00, "Marvell 88SE6121", AHCI_Q_NOFORCE | AHCI_Q_NOPMP |
274556e4c9aSAlexander Motin AHCI_Q_2CH | AHCI_Q_EDGEIS | AHCI_Q_NONCQ | AHCI_Q_NOCOUNT},
275556e4c9aSAlexander Motin {0x614111ab, 0x00, "Marvell 88SE6141", AHCI_Q_NOFORCE | AHCI_Q_NOPMP |
276556e4c9aSAlexander Motin AHCI_Q_4CH | AHCI_Q_EDGEIS | AHCI_Q_NONCQ | AHCI_Q_NOCOUNT},
277556e4c9aSAlexander Motin {0x614511ab, 0x00, "Marvell 88SE6145", AHCI_Q_NOFORCE | AHCI_Q_NOPMP |
278556e4c9aSAlexander Motin AHCI_Q_4CH | AHCI_Q_EDGEIS | AHCI_Q_NONCQ | AHCI_Q_NOCOUNT},
279c489ab61SRuslan Bukin {0x91201b4b, 0x00, "Marvell 88SE912x", AHCI_Q_EDGEIS |
280c489ab61SRuslan Bukin AHCI_Q_IOMMU_BUSWIDE},
281c489ab61SRuslan Bukin {0x91231b4b, 0x11, "Marvell 88SE912x", AHCI_Q_ALTSIG |
282c489ab61SRuslan Bukin AHCI_Q_IOMMU_BUSWIDE},
283c489ab61SRuslan Bukin {0x91231b4b, 0x00, "Marvell 88SE912x", AHCI_Q_EDGEIS | AHCI_Q_SATA2 |
284c489ab61SRuslan Bukin AHCI_Q_IOMMU_BUSWIDE},
285c6efb4c4SAlexander Motin {0x91251b4b, 0x00, "Marvell 88SE9125", 0},
286c489ab61SRuslan Bukin {0x91281b4b, 0x00, "Marvell 88SE9128", AHCI_Q_ALTSIG |
287c489ab61SRuslan Bukin AHCI_Q_IOMMU_BUSWIDE},
288c489ab61SRuslan Bukin {0x91301b4b, 0x00, "Marvell 88SE9130", AHCI_Q_ALTSIG |
289c489ab61SRuslan Bukin AHCI_Q_IOMMU_BUSWIDE},
290c489ab61SRuslan Bukin {0x91701b4b, 0x00, "Marvell 88SE9170", AHCI_Q_IOMMU_BUSWIDE},
291c489ab61SRuslan Bukin {0x91721b4b, 0x00, "Marvell 88SE9172", AHCI_Q_IOMMU_BUSWIDE},
292c489ab61SRuslan Bukin {0x917a1b4b, 0x00, "Marvell 88SE917A", AHCI_Q_IOMMU_BUSWIDE},
293c489ab61SRuslan Bukin {0x91821b4b, 0x00, "Marvell 88SE9182", AHCI_Q_IOMMU_BUSWIDE},
294c489ab61SRuslan Bukin {0x91831b4b, 0x00, "Marvell 88SS9183", AHCI_Q_IOMMU_BUSWIDE},
295c489ab61SRuslan Bukin {0x91a01b4b, 0x00, "Marvell 88SE91Ax", AHCI_Q_IOMMU_BUSWIDE},
296c6efb4c4SAlexander Motin {0x92151b4b, 0x00, "Marvell 88SE9215", 0},
297c489ab61SRuslan Bukin {0x92201b4b, 0x00, "Marvell 88SE9220", AHCI_Q_ALTSIG |
298c489ab61SRuslan Bukin AHCI_Q_IOMMU_BUSWIDE},
299c489ab61SRuslan Bukin {0x92301b4b, 0x00, "Marvell 88SE9230", AHCI_Q_ALTSIG |
300346483b1SMariusz Zaborski AHCI_Q_IOMMU_BUSWIDE | AHCI_Q_SLOWDEV},
301c6efb4c4SAlexander Motin {0x92351b4b, 0x00, "Marvell 88SE9235", 0},
302c6efb4c4SAlexander Motin {0x06201103, 0x00, "HighPoint RocketRAID 620", 0},
303c6efb4c4SAlexander Motin {0x06201b4b, 0x00, "HighPoint RocketRAID 620", 0},
304c6efb4c4SAlexander Motin {0x06221103, 0x00, "HighPoint RocketRAID 622", 0},
305c6efb4c4SAlexander Motin {0x06221b4b, 0x00, "HighPoint RocketRAID 622", 0},
306c6efb4c4SAlexander Motin {0x06401103, 0x00, "HighPoint RocketRAID 640", 0},
307c6efb4c4SAlexander Motin {0x06401b4b, 0x00, "HighPoint RocketRAID 640", 0},
308c6efb4c4SAlexander Motin {0x06441103, 0x00, "HighPoint RocketRAID 644", 0},
309c6efb4c4SAlexander Motin {0x06441b4b, 0x00, "HighPoint RocketRAID 644", 0},
310c6efb4c4SAlexander Motin {0x06411103, 0x00, "HighPoint RocketRAID 640L", 0},
311c489ab61SRuslan Bukin {0x06421103, 0x00, "HighPoint RocketRAID 642L", AHCI_Q_IOMMU_BUSWIDE},
312c489ab61SRuslan Bukin {0x06451103, 0x00, "HighPoint RocketRAID 644L", AHCI_Q_IOMMU_BUSWIDE},
313802df3acSWarner Losh {0x044c10de, 0x00, "NVIDIA MCP65", AHCI_Q_NOAA},
314802df3acSWarner Losh {0x044d10de, 0x00, "NVIDIA MCP65", AHCI_Q_NOAA},
315802df3acSWarner Losh {0x044e10de, 0x00, "NVIDIA MCP65", AHCI_Q_NOAA},
316802df3acSWarner Losh {0x044f10de, 0x00, "NVIDIA MCP65", AHCI_Q_NOAA},
317802df3acSWarner Losh {0x045c10de, 0x00, "NVIDIA MCP65", AHCI_Q_NOAA},
318802df3acSWarner Losh {0x045d10de, 0x00, "NVIDIA MCP65", AHCI_Q_NOAA},
319802df3acSWarner Losh {0x045e10de, 0x00, "NVIDIA MCP65", AHCI_Q_NOAA},
320802df3acSWarner Losh {0x045f10de, 0x00, "NVIDIA MCP65", AHCI_Q_NOAA},
321802df3acSWarner Losh {0x055010de, 0x00, "NVIDIA MCP67", AHCI_Q_NOAA},
322802df3acSWarner Losh {0x055110de, 0x00, "NVIDIA MCP67", AHCI_Q_NOAA},
323802df3acSWarner Losh {0x055210de, 0x00, "NVIDIA MCP67", AHCI_Q_NOAA},
324802df3acSWarner Losh {0x055310de, 0x00, "NVIDIA MCP67", AHCI_Q_NOAA},
325802df3acSWarner Losh {0x055410de, 0x00, "NVIDIA MCP67", AHCI_Q_NOAA},
326802df3acSWarner Losh {0x055510de, 0x00, "NVIDIA MCP67", AHCI_Q_NOAA},
327802df3acSWarner Losh {0x055610de, 0x00, "NVIDIA MCP67", AHCI_Q_NOAA},
328802df3acSWarner Losh {0x055710de, 0x00, "NVIDIA MCP67", AHCI_Q_NOAA},
329802df3acSWarner Losh {0x055810de, 0x00, "NVIDIA MCP67", AHCI_Q_NOAA},
330802df3acSWarner Losh {0x055910de, 0x00, "NVIDIA MCP67", AHCI_Q_NOAA},
331802df3acSWarner Losh {0x055A10de, 0x00, "NVIDIA MCP67", AHCI_Q_NOAA},
332802df3acSWarner Losh {0x055B10de, 0x00, "NVIDIA MCP67", AHCI_Q_NOAA},
333802df3acSWarner Losh {0x058410de, 0x00, "NVIDIA MCP67", AHCI_Q_NOAA},
334802df3acSWarner Losh {0x07f010de, 0x00, "NVIDIA MCP73", AHCI_Q_NOAA},
335802df3acSWarner Losh {0x07f110de, 0x00, "NVIDIA MCP73", AHCI_Q_NOAA},
336802df3acSWarner Losh {0x07f210de, 0x00, "NVIDIA MCP73", AHCI_Q_NOAA},
337802df3acSWarner Losh {0x07f310de, 0x00, "NVIDIA MCP73", AHCI_Q_NOAA},
338802df3acSWarner Losh {0x07f410de, 0x00, "NVIDIA MCP73", AHCI_Q_NOAA},
339802df3acSWarner Losh {0x07f510de, 0x00, "NVIDIA MCP73", AHCI_Q_NOAA},
340802df3acSWarner Losh {0x07f610de, 0x00, "NVIDIA MCP73", AHCI_Q_NOAA},
341802df3acSWarner Losh {0x07f710de, 0x00, "NVIDIA MCP73", AHCI_Q_NOAA},
342802df3acSWarner Losh {0x07f810de, 0x00, "NVIDIA MCP73", AHCI_Q_NOAA},
343802df3acSWarner Losh {0x07f910de, 0x00, "NVIDIA MCP73", AHCI_Q_NOAA},
344802df3acSWarner Losh {0x07fa10de, 0x00, "NVIDIA MCP73", AHCI_Q_NOAA},
345802df3acSWarner Losh {0x07fb10de, 0x00, "NVIDIA MCP73", AHCI_Q_NOAA},
346802df3acSWarner Losh {0x0ad010de, 0x00, "NVIDIA MCP77", AHCI_Q_NOAA},
347802df3acSWarner Losh {0x0ad110de, 0x00, "NVIDIA MCP77", AHCI_Q_NOAA},
348802df3acSWarner Losh {0x0ad210de, 0x00, "NVIDIA MCP77", AHCI_Q_NOAA},
349802df3acSWarner Losh {0x0ad310de, 0x00, "NVIDIA MCP77", AHCI_Q_NOAA},
350802df3acSWarner Losh {0x0ad410de, 0x00, "NVIDIA MCP77", AHCI_Q_NOAA},
351802df3acSWarner Losh {0x0ad510de, 0x00, "NVIDIA MCP77", AHCI_Q_NOAA},
352802df3acSWarner Losh {0x0ad610de, 0x00, "NVIDIA MCP77", AHCI_Q_NOAA},
353802df3acSWarner Losh {0x0ad710de, 0x00, "NVIDIA MCP77", AHCI_Q_NOAA},
354802df3acSWarner Losh {0x0ad810de, 0x00, "NVIDIA MCP77", AHCI_Q_NOAA},
355802df3acSWarner Losh {0x0ad910de, 0x00, "NVIDIA MCP77", AHCI_Q_NOAA},
356802df3acSWarner Losh {0x0ada10de, 0x00, "NVIDIA MCP77", AHCI_Q_NOAA},
357802df3acSWarner Losh {0x0adb10de, 0x00, "NVIDIA MCP77", AHCI_Q_NOAA},
358802df3acSWarner Losh {0x0ab410de, 0x00, "NVIDIA MCP79", AHCI_Q_NOAA},
359802df3acSWarner Losh {0x0ab510de, 0x00, "NVIDIA MCP79", AHCI_Q_NOAA},
360802df3acSWarner Losh {0x0ab610de, 0x00, "NVIDIA MCP79", AHCI_Q_NOAA},
361802df3acSWarner Losh {0x0ab710de, 0x00, "NVIDIA MCP79", AHCI_Q_NOAA},
362802df3acSWarner Losh {0x0ab810de, 0x00, "NVIDIA MCP79", AHCI_Q_NOAA},
363802df3acSWarner Losh {0x0ab910de, 0x00, "NVIDIA MCP79", AHCI_Q_NOAA},
364802df3acSWarner Losh {0x0aba10de, 0x00, "NVIDIA MCP79", AHCI_Q_NOAA},
365802df3acSWarner Losh {0x0abb10de, 0x00, "NVIDIA MCP79", AHCI_Q_NOAA},
366802df3acSWarner Losh {0x0abc10de, 0x00, "NVIDIA MCP79", AHCI_Q_NOAA},
367802df3acSWarner Losh {0x0abd10de, 0x00, "NVIDIA MCP79", AHCI_Q_NOAA},
368802df3acSWarner Losh {0x0abe10de, 0x00, "NVIDIA MCP79", AHCI_Q_NOAA},
369802df3acSWarner Losh {0x0abf10de, 0x00, "NVIDIA MCP79", AHCI_Q_NOAA},
370802df3acSWarner Losh {0x0d8410de, 0x00, "NVIDIA MCP89", AHCI_Q_NOAA},
371802df3acSWarner Losh {0x0d8510de, 0x00, "NVIDIA MCP89", AHCI_Q_NOFORCE|AHCI_Q_NOAA},
372802df3acSWarner Losh {0x0d8610de, 0x00, "NVIDIA MCP89", AHCI_Q_NOAA},
373802df3acSWarner Losh {0x0d8710de, 0x00, "NVIDIA MCP89", AHCI_Q_NOAA},
374802df3acSWarner Losh {0x0d8810de, 0x00, "NVIDIA MCP89", AHCI_Q_NOAA},
375802df3acSWarner Losh {0x0d8910de, 0x00, "NVIDIA MCP89", AHCI_Q_NOAA},
376802df3acSWarner Losh {0x0d8a10de, 0x00, "NVIDIA MCP89", AHCI_Q_NOAA},
377802df3acSWarner Losh {0x0d8b10de, 0x00, "NVIDIA MCP89", AHCI_Q_NOAA},
378802df3acSWarner Losh {0x0d8c10de, 0x00, "NVIDIA MCP89", AHCI_Q_NOAA},
379802df3acSWarner Losh {0x0d8d10de, 0x00, "NVIDIA MCP89", AHCI_Q_NOAA},
380802df3acSWarner Losh {0x0d8e10de, 0x00, "NVIDIA MCP89", AHCI_Q_NOAA},
381802df3acSWarner Losh {0x0d8f10de, 0x00, "NVIDIA MCP89", AHCI_Q_NOAA},
38203d89c94SAlexander Motin {0x3781105a, 0x00, "Promise TX8660", 0},
383802df3acSWarner Losh {0x33491106, 0x00, "VIA VT8251", AHCI_Q_NOPMP|AHCI_Q_NONCQ},
384802df3acSWarner Losh {0x62871106, 0x00, "VIA VT8251", AHCI_Q_NOPMP|AHCI_Q_NONCQ},
385802df3acSWarner Losh {0x11841039, 0x00, "SiS 966", 0},
386802df3acSWarner Losh {0x11851039, 0x00, "SiS 968", 0},
387802df3acSWarner Losh {0x01861039, 0x00, "SiS 968", 0},
3884e56f964SEd Maste {0xa01c177d, 0x00, "ThunderX", AHCI_Q_ABAR0|AHCI_Q_1MSI},
3895bf8f182SZbigniew Bodek {0x00311c36, 0x00, "Annapurna", AHCI_Q_FORCE_PI|AHCI_Q_RESTORE_CAP|AHCI_Q_NOMSIX},
390f5a7a8cdSMartin Wilke {0x1600144d, 0x00, "Samsung", AHCI_Q_NOMSI},
3912a31a06bSAlexander Motin {0x07e015ad, 0x00, "VMware", 0},
392802df3acSWarner Losh {0x00000000, 0x00, NULL, 0}
393802df3acSWarner Losh };
394802df3acSWarner Losh
395802df3acSWarner Losh static int
ahci_pci_ctlr_reset(device_t dev)396802df3acSWarner Losh ahci_pci_ctlr_reset(device_t dev)
397802df3acSWarner Losh {
398802df3acSWarner Losh
39999f65e3eSXin LI return(ahci_ctlr_reset(dev));
400802df3acSWarner Losh }
401802df3acSWarner Losh
402802df3acSWarner Losh static int
ahci_probe(device_t dev)403802df3acSWarner Losh ahci_probe(device_t dev)
404802df3acSWarner Losh {
405802df3acSWarner Losh int i, valid = 0;
406802df3acSWarner Losh uint32_t devid = pci_get_devid(dev);
407802df3acSWarner Losh uint8_t revid = pci_get_revid(dev);
408802df3acSWarner Losh
409802df3acSWarner Losh /*
410802df3acSWarner Losh * Ensure it is not a PCI bridge (some vendors use
411802df3acSWarner Losh * the same PID and VID in PCI bridge and AHCI cards).
412802df3acSWarner Losh */
413802df3acSWarner Losh if (pci_get_class(dev) == PCIC_BRIDGE)
414802df3acSWarner Losh return (ENXIO);
415802df3acSWarner Losh
416802df3acSWarner Losh /* Is this a possible AHCI candidate? */
417802df3acSWarner Losh if (pci_get_class(dev) == PCIC_STORAGE &&
418802df3acSWarner Losh pci_get_subclass(dev) == PCIS_STORAGE_SATA &&
419802df3acSWarner Losh pci_get_progif(dev) == PCIP_STORAGE_SATA_AHCI_1_0)
420802df3acSWarner Losh valid = 1;
42198ef2a61SAlexander Motin else if (pci_get_class(dev) == PCIC_STORAGE &&
42298ef2a61SAlexander Motin pci_get_subclass(dev) == PCIS_STORAGE_RAID)
42398ef2a61SAlexander Motin valid = 2;
424802df3acSWarner Losh /* Is this a known AHCI chip? */
425802df3acSWarner Losh for (i = 0; ahci_ids[i].id != 0; i++) {
426802df3acSWarner Losh if (ahci_ids[i].id == devid &&
427802df3acSWarner Losh ahci_ids[i].rev <= revid &&
428802df3acSWarner Losh (valid || (force_ahci == 1 &&
429802df3acSWarner Losh !(ahci_ids[i].quirks & AHCI_Q_NOFORCE)))) {
430802df3acSWarner Losh /* Do not attach JMicrons with single PCI function. */
431802df3acSWarner Losh if (pci_get_vendor(dev) == 0x197b &&
43216b766eeSAlexander Motin (ahci_ids[i].quirks & AHCI_Q_NOFORCE) &&
433802df3acSWarner Losh (pci_read_config(dev, 0xdf, 1) & 0x40) == 0)
434802df3acSWarner Losh return (ENXIO);
435a74b4965SMark Johnston device_set_descf(dev, "%s AHCI SATA controller",
436802df3acSWarner Losh ahci_ids[i].name);
4373036de3cSAlexander Motin return (BUS_PROBE_DEFAULT);
438802df3acSWarner Losh }
439802df3acSWarner Losh }
44098ef2a61SAlexander Motin if (valid != 1)
441802df3acSWarner Losh return (ENXIO);
442a74b4965SMark Johnston device_set_desc(dev, "AHCI SATA controller");
4433036de3cSAlexander Motin return (BUS_PROBE_DEFAULT);
444802df3acSWarner Losh }
445802df3acSWarner Losh
446802df3acSWarner Losh static int
ahci_ata_probe(device_t dev)447802df3acSWarner Losh ahci_ata_probe(device_t dev)
448802df3acSWarner Losh {
449802df3acSWarner Losh int i;
450802df3acSWarner Losh uint32_t devid = pci_get_devid(dev);
451802df3acSWarner Losh uint8_t revid = pci_get_revid(dev);
452802df3acSWarner Losh
453802df3acSWarner Losh if ((intptr_t)device_get_ivars(dev) >= 0)
454802df3acSWarner Losh return (ENXIO);
455802df3acSWarner Losh /* Is this a known AHCI chip? */
456802df3acSWarner Losh for (i = 0; ahci_ids[i].id != 0; i++) {
457802df3acSWarner Losh if (ahci_ids[i].id == devid &&
458802df3acSWarner Losh ahci_ids[i].rev <= revid) {
459a74b4965SMark Johnston device_set_descf(dev, "%s AHCI SATA controller",
460802df3acSWarner Losh ahci_ids[i].name);
4613036de3cSAlexander Motin return (BUS_PROBE_DEFAULT);
462802df3acSWarner Losh }
463802df3acSWarner Losh }
464a74b4965SMark Johnston device_set_desc(dev, "AHCI SATA controller");
4653036de3cSAlexander Motin return (BUS_PROBE_DEFAULT);
466802df3acSWarner Losh }
467802df3acSWarner Losh
468802df3acSWarner Losh static int
ahci_pci_read_msix_bars(device_t dev,uint8_t * table_bar,uint8_t * pba_bar)4690af6011aSZbigniew Bodek ahci_pci_read_msix_bars(device_t dev, uint8_t *table_bar, uint8_t *pba_bar)
4700af6011aSZbigniew Bodek {
4710af6011aSZbigniew Bodek int cap_offset = 0, ret;
4720af6011aSZbigniew Bodek uint32_t val;
4730af6011aSZbigniew Bodek
4740af6011aSZbigniew Bodek if ((table_bar == NULL) || (pba_bar == NULL))
4750af6011aSZbigniew Bodek return (EINVAL);
4760af6011aSZbigniew Bodek
4770af6011aSZbigniew Bodek ret = pci_find_cap(dev, PCIY_MSIX, &cap_offset);
4780af6011aSZbigniew Bodek if (ret != 0)
4790af6011aSZbigniew Bodek return (EINVAL);
4800af6011aSZbigniew Bodek
4810af6011aSZbigniew Bodek val = pci_read_config(dev, cap_offset + PCIR_MSIX_TABLE, 4);
4820af6011aSZbigniew Bodek *table_bar = PCIR_BAR(val & PCIM_MSIX_BIR_MASK);
4830af6011aSZbigniew Bodek
4840af6011aSZbigniew Bodek val = pci_read_config(dev, cap_offset + PCIR_MSIX_PBA, 4);
4850af6011aSZbigniew Bodek *pba_bar = PCIR_BAR(val & PCIM_MSIX_BIR_MASK);
4860af6011aSZbigniew Bodek
4870af6011aSZbigniew Bodek return (0);
4880af6011aSZbigniew Bodek }
4890af6011aSZbigniew Bodek
4900af6011aSZbigniew Bodek static int
ahci_pci_attach(device_t dev)491802df3acSWarner Losh ahci_pci_attach(device_t dev)
492802df3acSWarner Losh {
493802df3acSWarner Losh struct ahci_controller *ctlr = device_get_softc(dev);
494802df3acSWarner Losh int error, i;
495802df3acSWarner Losh uint32_t devid = pci_get_devid(dev);
496802df3acSWarner Losh uint8_t revid = pci_get_revid(dev);
4970af6011aSZbigniew Bodek int msi_count, msix_count;
4980af6011aSZbigniew Bodek uint8_t table_bar = 0, pba_bar = 0;
499abab2155SAlexander Motin uint32_t caps, pi;
5000af6011aSZbigniew Bodek
5010af6011aSZbigniew Bodek msi_count = pci_msi_count(dev);
5020af6011aSZbigniew Bodek msix_count = pci_msix_count(dev);
503802df3acSWarner Losh
504802df3acSWarner Losh i = 0;
505802df3acSWarner Losh while (ahci_ids[i].id != 0 &&
506802df3acSWarner Losh (ahci_ids[i].id != devid ||
507802df3acSWarner Losh ahci_ids[i].rev > revid))
508802df3acSWarner Losh i++;
509802df3acSWarner Losh ctlr->quirks = ahci_ids[i].quirks;
510c489ab61SRuslan Bukin
511c489ab61SRuslan Bukin if (ctlr->quirks & AHCI_Q_IOMMU_BUSWIDE) {
512c489ab61SRuslan Bukin /*
513c489ab61SRuslan Bukin * The controller issues DMA requests from PCI function 1,
514c489ab61SRuslan Bukin * but the device is not multifunction.
515c489ab61SRuslan Bukin * Ref: https://bugzilla.kernel.org/show_bug.cgi?id=42679
516c489ab61SRuslan Bukin */
517c489ab61SRuslan Bukin bus_dma_iommu_set_buswide(dev);
518c489ab61SRuslan Bukin }
519c489ab61SRuslan Bukin
520802df3acSWarner Losh /* Limit speed for my onboard JMicron external port.
521802df3acSWarner Losh * It is not eSATA really, limit to SATA 1 */
522802df3acSWarner Losh if (pci_get_devid(dev) == 0x2363197b &&
523802df3acSWarner Losh pci_get_subvendor(dev) == 0x1043 &&
524802df3acSWarner Losh pci_get_subdevice(dev) == 0x81e4)
525802df3acSWarner Losh ctlr->quirks |= AHCI_Q_SATA1_UNIT0;
52620a908e0SAlexander Motin resource_int_value(device_get_name(dev), device_get_unit(dev),
52720a908e0SAlexander Motin "quirks", &ctlr->quirks);
528802df3acSWarner Losh ctlr->vendorid = pci_get_vendor(dev);
529802df3acSWarner Losh ctlr->deviceid = pci_get_device(dev);
530802df3acSWarner Losh ctlr->subvendorid = pci_get_subvendor(dev);
531802df3acSWarner Losh ctlr->subdeviceid = pci_get_subdevice(dev);
532378c532cSKonstantin Belousov
533378c532cSKonstantin Belousov /* Default AHCI Base Address is BAR(5), Cavium uses BAR(0) */
534378c532cSKonstantin Belousov if (ctlr->quirks & AHCI_Q_ABAR0)
535378c532cSKonstantin Belousov ctlr->r_rid = PCIR_BAR(0);
536378c532cSKonstantin Belousov else
537802df3acSWarner Losh ctlr->r_rid = PCIR_BAR(5);
538802df3acSWarner Losh if (!(ctlr->r_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
539802df3acSWarner Losh &ctlr->r_rid, RF_ACTIVE)))
540802df3acSWarner Losh return ENXIO;
5410af6011aSZbigniew Bodek
54293289cfcSWarner Losh /*
54393289cfcSWarner Losh * Intel RAID hardware can remap NVMe devices inside its BAR.
54493289cfcSWarner Losh * Try to detect this. Either we have to add the device
54593289cfcSWarner Losh * here, or the user has to change the mode in the BIOS
54693289cfcSWarner Losh * from RST to AHCI.
54793289cfcSWarner Losh */
54893289cfcSWarner Losh if (pci_get_vendor(dev) == 0x8086) {
54993289cfcSWarner Losh uint32_t vscap;
55093289cfcSWarner Losh
55193289cfcSWarner Losh vscap = ATA_INL(ctlr->r_mem, AHCI_VSCAP);
55293289cfcSWarner Losh if (vscap & 1) {
55393289cfcSWarner Losh uint32_t cap = ATA_INL(ctlr->r_mem, 0x800); /* Intel's REMAP CAP */
55493289cfcSWarner Losh int i;
55593289cfcSWarner Losh
55693289cfcSWarner Losh ctlr->remap_offset = 0x4000;
55793289cfcSWarner Losh ctlr->remap_size = 0x4000;
55893289cfcSWarner Losh
55993289cfcSWarner Losh /*
56093289cfcSWarner Losh * Check each of the devices that might be remapped to
56193289cfcSWarner Losh * make sure they are an nvme device. At the present,
56293289cfcSWarner Losh * nvme are the only known devices remapped.
56393289cfcSWarner Losh */
56493289cfcSWarner Losh for (i = 0; i < 3; i++) {
56593289cfcSWarner Losh if (cap & (1 << i) &&
56693289cfcSWarner Losh (ATA_INL(ctlr->r_mem, 0x880 + i * 0x80) ==
56793289cfcSWarner Losh ((PCIC_STORAGE << 16) |
56893289cfcSWarner Losh (PCIS_STORAGE_NVM << 8) |
56993289cfcSWarner Losh PCIP_STORAGE_NVM_ENTERPRISE_NVMHCI_1_0))) {
57093289cfcSWarner Losh ctlr->remapped_devices++;
57193289cfcSWarner Losh }
57293289cfcSWarner Losh }
57393289cfcSWarner Losh
57493289cfcSWarner Losh /* If we have any remapped device, disable MSI */
57593289cfcSWarner Losh if (ctlr->remapped_devices > 0) {
57693289cfcSWarner Losh device_printf(dev, "Detected %d nvme remapped devices\n",
57793289cfcSWarner Losh ctlr->remapped_devices);
57893289cfcSWarner Losh ctlr->quirks |= (AHCI_Q_NOMSIX | AHCI_Q_NOMSI);
57993289cfcSWarner Losh }
58093289cfcSWarner Losh }
58193289cfcSWarner Losh }
58293289cfcSWarner Losh
5835bf8f182SZbigniew Bodek if (ctlr->quirks & AHCI_Q_NOMSIX)
5845bf8f182SZbigniew Bodek msix_count = 0;
5855bf8f182SZbigniew Bodek
5860af6011aSZbigniew Bodek /* Read MSI-x BAR IDs if supported */
5870af6011aSZbigniew Bodek if (msix_count > 0) {
5880af6011aSZbigniew Bodek error = ahci_pci_read_msix_bars(dev, &table_bar, &pba_bar);
5890af6011aSZbigniew Bodek if (error == 0) {
5900af6011aSZbigniew Bodek ctlr->r_msix_tab_rid = table_bar;
5910af6011aSZbigniew Bodek ctlr->r_msix_pba_rid = pba_bar;
5920af6011aSZbigniew Bodek } else {
5930af6011aSZbigniew Bodek /* Failed to read BARs, disable MSI-x */
5940af6011aSZbigniew Bodek msix_count = 0;
5950af6011aSZbigniew Bodek }
5960af6011aSZbigniew Bodek }
5970af6011aSZbigniew Bodek
5980af6011aSZbigniew Bodek /* Allocate resources for MSI-x table and PBA */
5990af6011aSZbigniew Bodek if (msix_count > 0) {
6000af6011aSZbigniew Bodek /*
6010af6011aSZbigniew Bodek * Allocate new MSI-x table only if not
6020af6011aSZbigniew Bodek * allocated before.
6030af6011aSZbigniew Bodek */
6040af6011aSZbigniew Bodek ctlr->r_msix_table = NULL;
6050af6011aSZbigniew Bodek if (ctlr->r_msix_tab_rid != ctlr->r_rid) {
6060af6011aSZbigniew Bodek /* Separate BAR for MSI-x */
6070af6011aSZbigniew Bodek ctlr->r_msix_table = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
6080af6011aSZbigniew Bodek &ctlr->r_msix_tab_rid, RF_ACTIVE);
6090af6011aSZbigniew Bodek if (ctlr->r_msix_table == NULL) {
6100af6011aSZbigniew Bodek ahci_free_mem(dev);
6110af6011aSZbigniew Bodek return (ENXIO);
6120af6011aSZbigniew Bodek }
6130af6011aSZbigniew Bodek }
6140af6011aSZbigniew Bodek
6150af6011aSZbigniew Bodek /*
6160af6011aSZbigniew Bodek * Allocate new PBA table only if not
6170af6011aSZbigniew Bodek * allocated before.
6180af6011aSZbigniew Bodek */
6190af6011aSZbigniew Bodek ctlr->r_msix_pba = NULL;
6200af6011aSZbigniew Bodek if ((ctlr->r_msix_pba_rid != ctlr->r_msix_tab_rid) &&
6210af6011aSZbigniew Bodek (ctlr->r_msix_pba_rid != ctlr->r_rid)) {
6220af6011aSZbigniew Bodek /* Separate BAR for PBA */
6230af6011aSZbigniew Bodek ctlr->r_msix_pba = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
6240af6011aSZbigniew Bodek &ctlr->r_msix_pba_rid, RF_ACTIVE);
6250af6011aSZbigniew Bodek if (ctlr->r_msix_pba == NULL) {
6260af6011aSZbigniew Bodek ahci_free_mem(dev);
6270af6011aSZbigniew Bodek return (ENXIO);
6280af6011aSZbigniew Bodek }
6290af6011aSZbigniew Bodek }
6300af6011aSZbigniew Bodek }
6310af6011aSZbigniew Bodek
632802df3acSWarner Losh pci_enable_busmaster(dev);
633802df3acSWarner Losh /* Reset controller */
634802df3acSWarner Losh if ((error = ahci_pci_ctlr_reset(dev)) != 0) {
6350af6011aSZbigniew Bodek ahci_free_mem(dev);
636802df3acSWarner Losh return (error);
63774b8d63dSPedro F. Giffuni }
638802df3acSWarner Losh
639802df3acSWarner Losh /* Setup interrupts. */
640802df3acSWarner Losh
641802df3acSWarner Losh /* Setup MSI register parameters */
642802df3acSWarner Losh /* Process hints. */
643abab2155SAlexander Motin caps = ATA_INL(ctlr->r_mem, AHCI_CAP);
644abab2155SAlexander Motin pi = ATA_INL(ctlr->r_mem, AHCI_PI);
645802df3acSWarner Losh if (ctlr->quirks & AHCI_Q_NOMSI)
646802df3acSWarner Losh ctlr->msi = 0;
647abab2155SAlexander Motin else if ((ctlr->quirks & AHCI_Q_1MSI) ||
648abab2155SAlexander Motin ((caps & (AHCI_CAP_NPMASK | AHCI_CAP_CCCS)) == 0 && pi == 1))
649b1877a0bSSteven Hartland ctlr->msi = 1;
650b1877a0bSSteven Hartland else
651b1877a0bSSteven Hartland ctlr->msi = 2;
652802df3acSWarner Losh resource_int_value(device_get_name(dev),
653802df3acSWarner Losh device_get_unit(dev), "msi", &ctlr->msi);
654802df3acSWarner Losh ctlr->numirqs = 1;
6550af6011aSZbigniew Bodek if (msi_count == 0 && msix_count == 0)
6560af6011aSZbigniew Bodek ctlr->msi = 0;
657802df3acSWarner Losh if (ctlr->msi < 0)
658802df3acSWarner Losh ctlr->msi = 0;
6590af6011aSZbigniew Bodek else if (ctlr->msi == 1) {
6600af6011aSZbigniew Bodek msi_count = min(1, msi_count);
6610af6011aSZbigniew Bodek msix_count = min(1, msix_count);
6620af6011aSZbigniew Bodek } else if (ctlr->msi > 1)
663802df3acSWarner Losh ctlr->msi = 2;
6640af6011aSZbigniew Bodek
6650af6011aSZbigniew Bodek /* Allocate MSI/MSI-x if needed/present. */
6660af6011aSZbigniew Bodek if (ctlr->msi > 0) {
6670af6011aSZbigniew Bodek error = ENXIO;
6680af6011aSZbigniew Bodek
6690af6011aSZbigniew Bodek /* Try to allocate MSI-x first */
6700af6011aSZbigniew Bodek if (msix_count > 0) {
6710af6011aSZbigniew Bodek error = pci_alloc_msix(dev, &msix_count);
6720af6011aSZbigniew Bodek if (error == 0)
6730af6011aSZbigniew Bodek ctlr->numirqs = msix_count;
674802df3acSWarner Losh }
6750af6011aSZbigniew Bodek
6760af6011aSZbigniew Bodek /*
6770af6011aSZbigniew Bodek * Try to allocate MSI if msi_count is greater than 0
6780af6011aSZbigniew Bodek * and if MSI-x allocation failed.
6790af6011aSZbigniew Bodek */
6800af6011aSZbigniew Bodek if ((error != 0) && (msi_count > 0)) {
6810af6011aSZbigniew Bodek error = pci_alloc_msi(dev, &msi_count);
6820af6011aSZbigniew Bodek if (error == 0)
6830af6011aSZbigniew Bodek ctlr->numirqs = msi_count;
6840af6011aSZbigniew Bodek }
6850af6011aSZbigniew Bodek
6860af6011aSZbigniew Bodek /* Both MSI and MSI-x allocations failed */
6870af6011aSZbigniew Bodek if (error != 0) {
688802df3acSWarner Losh ctlr->msi = 0;
6890af6011aSZbigniew Bodek device_printf(dev, "Failed to allocate MSI/MSI-x, "
6900af6011aSZbigniew Bodek "falling back to INTx\n");
6910af6011aSZbigniew Bodek }
692802df3acSWarner Losh }
693802df3acSWarner Losh
694802df3acSWarner Losh error = ahci_attach(dev);
6950af6011aSZbigniew Bodek if (error != 0) {
6960af6011aSZbigniew Bodek if (ctlr->msi > 0)
697802df3acSWarner Losh pci_release_msi(dev);
6980af6011aSZbigniew Bodek ahci_free_mem(dev);
6990af6011aSZbigniew Bodek }
700802df3acSWarner Losh return error;
701802df3acSWarner Losh }
702802df3acSWarner Losh
703802df3acSWarner Losh static int
ahci_pci_detach(device_t dev)704802df3acSWarner Losh ahci_pci_detach(device_t dev)
705802df3acSWarner Losh {
706802df3acSWarner Losh
707802df3acSWarner Losh ahci_detach(dev);
708802df3acSWarner Losh pci_release_msi(dev);
709802df3acSWarner Losh return (0);
710802df3acSWarner Losh }
711802df3acSWarner Losh
712802df3acSWarner Losh static int
ahci_pci_suspend(device_t dev)713802df3acSWarner Losh ahci_pci_suspend(device_t dev)
714802df3acSWarner Losh {
715802df3acSWarner Losh struct ahci_controller *ctlr = device_get_softc(dev);
716802df3acSWarner Losh
717802df3acSWarner Losh bus_generic_suspend(dev);
718802df3acSWarner Losh /* Disable interupts, so the state change(s) doesn't trigger */
719802df3acSWarner Losh ATA_OUTL(ctlr->r_mem, AHCI_GHC,
720802df3acSWarner Losh ATA_INL(ctlr->r_mem, AHCI_GHC) & (~AHCI_GHC_IE));
721802df3acSWarner Losh return 0;
722802df3acSWarner Losh }
723802df3acSWarner Losh
724802df3acSWarner Losh static int
ahci_pci_resume(device_t dev)725802df3acSWarner Losh ahci_pci_resume(device_t dev)
726802df3acSWarner Losh {
727802df3acSWarner Losh int res;
728802df3acSWarner Losh
729802df3acSWarner Losh if ((res = ahci_pci_ctlr_reset(dev)) != 0)
730802df3acSWarner Losh return (res);
731802df3acSWarner Losh ahci_ctlr_setup(dev);
732802df3acSWarner Losh return (bus_generic_resume(dev));
733802df3acSWarner Losh }
734802df3acSWarner Losh
735802df3acSWarner Losh static device_method_t ahci_methods[] = {
736802df3acSWarner Losh DEVMETHOD(device_probe, ahci_probe),
737802df3acSWarner Losh DEVMETHOD(device_attach, ahci_pci_attach),
738802df3acSWarner Losh DEVMETHOD(device_detach, ahci_pci_detach),
739802df3acSWarner Losh DEVMETHOD(device_suspend, ahci_pci_suspend),
740802df3acSWarner Losh DEVMETHOD(device_resume, ahci_pci_resume),
741802df3acSWarner Losh DEVMETHOD(bus_print_child, ahci_print_child),
742802df3acSWarner Losh DEVMETHOD(bus_alloc_resource, ahci_alloc_resource),
743802df3acSWarner Losh DEVMETHOD(bus_release_resource, ahci_release_resource),
744802df3acSWarner Losh DEVMETHOD(bus_setup_intr, ahci_setup_intr),
745802df3acSWarner Losh DEVMETHOD(bus_teardown_intr,ahci_teardown_intr),
746ddfc9c4cSWarner Losh DEVMETHOD(bus_child_location, ahci_child_location),
747802df3acSWarner Losh DEVMETHOD(bus_get_dma_tag, ahci_get_dma_tag),
74821190895SMarius Strobl DEVMETHOD_END
749802df3acSWarner Losh };
75023802d41SJohn Baldwin
751802df3acSWarner Losh static driver_t ahci_driver = {
752802df3acSWarner Losh "ahci",
753802df3acSWarner Losh ahci_methods,
754802df3acSWarner Losh sizeof(struct ahci_controller)
755802df3acSWarner Losh };
75623802d41SJohn Baldwin
75723802d41SJohn Baldwin DRIVER_MODULE(ahci, pci, ahci_driver, NULL, NULL);
758491589f3SWarner Losh /* Also matches class / subclass / progid XXX need to add when we have masking support */
759491589f3SWarner Losh MODULE_PNP_INFO("W32:vendor/device", pci, ahci, ahci_ids,
760329e817fSWarner Losh nitems(ahci_ids) - 1);
76123802d41SJohn Baldwin
762802df3acSWarner Losh static device_method_t ahci_ata_methods[] = {
763802df3acSWarner Losh DEVMETHOD(device_probe, ahci_ata_probe),
764802df3acSWarner Losh DEVMETHOD(device_attach, ahci_pci_attach),
765802df3acSWarner Losh DEVMETHOD(device_detach, ahci_pci_detach),
766802df3acSWarner Losh DEVMETHOD(device_suspend, ahci_pci_suspend),
767802df3acSWarner Losh DEVMETHOD(device_resume, ahci_pci_resume),
768802df3acSWarner Losh DEVMETHOD(bus_print_child, ahci_print_child),
769802df3acSWarner Losh DEVMETHOD(bus_alloc_resource, ahci_alloc_resource),
770802df3acSWarner Losh DEVMETHOD(bus_release_resource, ahci_release_resource),
771802df3acSWarner Losh DEVMETHOD(bus_setup_intr, ahci_setup_intr),
772802df3acSWarner Losh DEVMETHOD(bus_teardown_intr,ahci_teardown_intr),
773ddfc9c4cSWarner Losh DEVMETHOD(bus_child_location, ahci_child_location),
77421190895SMarius Strobl DEVMETHOD_END
775802df3acSWarner Losh };
77623802d41SJohn Baldwin
777802df3acSWarner Losh static driver_t ahci_ata_driver = {
778802df3acSWarner Losh "ahci",
779802df3acSWarner Losh ahci_ata_methods,
780802df3acSWarner Losh sizeof(struct ahci_controller)
781802df3acSWarner Losh };
78223802d41SJohn Baldwin
78323802d41SJohn Baldwin DRIVER_MODULE(ahci, atapci, ahci_ata_driver, NULL, NULL);
784