xref: /dragonfly/sys/dev/disk/ahci/ahci_attach.c (revision 030b0c8c)
1258223a3SMatthew Dillon /*
2fb00c6edSMatthew Dillon  * (MPSAFE)
3fb00c6edSMatthew Dillon  *
4258223a3SMatthew Dillon  * Copyright (c) 2006 David Gwynne <dlg@openbsd.org>
5258223a3SMatthew Dillon  *
6258223a3SMatthew Dillon  * Permission to use, copy, modify, and distribute this software for any
7258223a3SMatthew Dillon  * purpose with or without fee is hereby granted, provided that the above
8258223a3SMatthew Dillon  * copyright notice and this permission notice appear in all copies.
9258223a3SMatthew Dillon  *
10258223a3SMatthew Dillon  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11258223a3SMatthew Dillon  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12258223a3SMatthew Dillon  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13258223a3SMatthew Dillon  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14258223a3SMatthew Dillon  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15258223a3SMatthew Dillon  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16258223a3SMatthew Dillon  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17258223a3SMatthew Dillon  *
18258223a3SMatthew Dillon  *
19258223a3SMatthew Dillon  * Copyright (c) 2009 The DragonFly Project.  All rights reserved.
20258223a3SMatthew Dillon  *
21258223a3SMatthew Dillon  * This code is derived from software contributed to The DragonFly Project
22258223a3SMatthew Dillon  * by Matthew Dillon <dillon@backplane.com>
23258223a3SMatthew Dillon  *
24258223a3SMatthew Dillon  * Redistribution and use in source and binary forms, with or without
25258223a3SMatthew Dillon  * modification, are permitted provided that the following conditions
26258223a3SMatthew Dillon  * are met:
27258223a3SMatthew Dillon  *
28258223a3SMatthew Dillon  * 1. Redistributions of source code must retain the above copyright
29258223a3SMatthew Dillon  *    notice, this list of conditions and the following disclaimer.
30258223a3SMatthew Dillon  * 2. Redistributions in binary form must reproduce the above copyright
31258223a3SMatthew Dillon  *    notice, this list of conditions and the following disclaimer in
32258223a3SMatthew Dillon  *    the documentation and/or other materials provided with the
33258223a3SMatthew Dillon  *    distribution.
34258223a3SMatthew Dillon  * 3. Neither the name of The DragonFly Project nor the names of its
35258223a3SMatthew Dillon  *    contributors may be used to endorse or promote products derived
36258223a3SMatthew Dillon  *    from this software without specific, prior written permission.
37258223a3SMatthew Dillon  *
38258223a3SMatthew Dillon  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
39258223a3SMatthew Dillon  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
40258223a3SMatthew Dillon  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
41258223a3SMatthew Dillon  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
42258223a3SMatthew Dillon  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
43258223a3SMatthew Dillon  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
44258223a3SMatthew Dillon  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
45258223a3SMatthew Dillon  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
46258223a3SMatthew Dillon  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
47258223a3SMatthew Dillon  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
48258223a3SMatthew Dillon  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
49258223a3SMatthew Dillon  * SUCH DAMAGE.
50258223a3SMatthew Dillon  *
51258223a3SMatthew Dillon  * $OpenBSD: ahci.c,v 1.147 2009/02/16 21:19:07 miod Exp $
52258223a3SMatthew Dillon  */
53258223a3SMatthew Dillon 
54258223a3SMatthew Dillon #include "ahci.h"
55258223a3SMatthew Dillon 
56258223a3SMatthew Dillon static int	ahci_vt8251_attach(device_t);
57258223a3SMatthew Dillon static int	ahci_ati_sb600_attach(device_t);
58258223a3SMatthew Dillon static int	ahci_nvidia_mcp_attach(device_t);
59258223a3SMatthew Dillon static int	ahci_pci_attach(device_t);
60258223a3SMatthew Dillon static int	ahci_pci_detach(device_t);
61258223a3SMatthew Dillon 
62258223a3SMatthew Dillon static const struct ahci_device ahci_devices[] = {
63258223a3SMatthew Dillon 	{ PCI_VENDOR_VIATECH,	PCI_PRODUCT_VIATECH_VT8251_SATA,
64258223a3SMatthew Dillon 	    ahci_vt8251_attach, ahci_pci_detach, "ViaTech-VT8251-SATA" },
65258223a3SMatthew Dillon 	{ PCI_VENDOR_ATI,	PCI_PRODUCT_ATI_SB600_SATA,
66258223a3SMatthew Dillon 	    ahci_ati_sb600_attach, ahci_pci_detach, "ATI-SB600-SATA" },
67258223a3SMatthew Dillon 	{ PCI_VENDOR_NVIDIA,	PCI_PRODUCT_NVIDIA_MCP65_AHCI_2,
68258223a3SMatthew Dillon 	    ahci_nvidia_mcp_attach, ahci_pci_detach, "NVidia-MCP65-SATA" },
69258223a3SMatthew Dillon 	{ PCI_VENDOR_NVIDIA,	PCI_PRODUCT_NVIDIA_MCP67_AHCI_1,
70258223a3SMatthew Dillon 	    ahci_nvidia_mcp_attach, ahci_pci_detach, "NVidia-MCP67-SATA" },
71258223a3SMatthew Dillon 	{ PCI_VENDOR_NVIDIA,	PCI_PRODUCT_NVIDIA_MCP77_AHCI_5,
72258223a3SMatthew Dillon 	    ahci_nvidia_mcp_attach, ahci_pci_detach, "NVidia-MCP77-SATA" },
73f1d54ca5SSascha Wildner 	{ PCI_VENDOR_NVIDIA,	PCI_PRODUCT_NVIDIA_MCP79_AHCI_1,
74f1d54ca5SSascha Wildner 	    ahci_nvidia_mcp_attach, ahci_pci_detach, "NVidia-MCP79-SATA" },
75f6520cceSMatthew Dillon 	{ PCI_VENDOR_NVIDIA,	PCI_PRODUCT_NVIDIA_MCP79_AHCI_9,
76f6520cceSMatthew Dillon 	    ahci_nvidia_mcp_attach, ahci_pci_detach, "NVidia-MCP79-SATA" },
77258223a3SMatthew Dillon 	{ 0, 0,
78258223a3SMatthew Dillon 	    ahci_pci_attach, ahci_pci_detach, "AHCI-PCI-SATA" }
79258223a3SMatthew Dillon };
80258223a3SMatthew Dillon 
81887d793aSSepherosa Ziehau struct ahci_pciid {
82887d793aSSepherosa Ziehau 	uint16_t	ahci_vid;
83887d793aSSepherosa Ziehau 	uint16_t	ahci_did;
84c7f4c6e4SSepherosa Ziehau 	int		ahci_rev;
85887d793aSSepherosa Ziehau };
86887d793aSSepherosa Ziehau 
87887d793aSSepherosa Ziehau static const struct ahci_pciid ahci_msi_blacklist[] = {
88c7f4c6e4SSepherosa Ziehau 	{ PCI_VENDOR_ATI,	PCI_PRODUCT_ATI_SB600_SATA, -1 },
89c7f4c6e4SSepherosa Ziehau 	{ PCI_VENDOR_ATI,	PCI_PRODUCT_ATI_SB700_AHCI, -1 },
90c7f4c6e4SSepherosa Ziehau 
91c7f4c6e4SSepherosa Ziehau 	{ PCI_VENDOR_MARVELL,	PCI_PRODUCT_MARVELL_88SE6121, -1 },
92c7f4c6e4SSepherosa Ziehau 	{ PCI_VENDOR_MARVELL,	PCI_PRODUCT_MARVELL_88SE6145, -1 },
93c7f4c6e4SSepherosa Ziehau 
94c7f4c6e4SSepherosa Ziehau 	{ PCI_VENDOR_NVIDIA,	PCI_PRODUCT_NVIDIA_MCP65_AHCI_1, 0xa1 },
95c7f4c6e4SSepherosa Ziehau 	{ PCI_VENDOR_NVIDIA,	PCI_PRODUCT_NVIDIA_MCP65_AHCI_2, 0xa1 },
96c7f4c6e4SSepherosa Ziehau 	{ PCI_VENDOR_NVIDIA,	PCI_PRODUCT_NVIDIA_MCP65_AHCI_3, 0xa1 },
97c7f4c6e4SSepherosa Ziehau 	{ PCI_VENDOR_NVIDIA,	PCI_PRODUCT_NVIDIA_MCP65_AHCI_4, 0xa1 },
98c7f4c6e4SSepherosa Ziehau 	{ PCI_VENDOR_NVIDIA,	PCI_PRODUCT_NVIDIA_MCP65_AHCI_5, 0xa1 },
99c7f4c6e4SSepherosa Ziehau 	{ PCI_VENDOR_NVIDIA,	PCI_PRODUCT_NVIDIA_MCP65_AHCI_6, 0xa1 },
100c7f4c6e4SSepherosa Ziehau 	{ PCI_VENDOR_NVIDIA,	PCI_PRODUCT_NVIDIA_MCP65_AHCI_7, 0xa1 },
101c7f4c6e4SSepherosa Ziehau 	{ PCI_VENDOR_NVIDIA,	PCI_PRODUCT_NVIDIA_MCP65_AHCI_8, 0xa1 },
102c7f4c6e4SSepherosa Ziehau 
103c7f4c6e4SSepherosa Ziehau 	{ PCI_VENDOR_NVIDIA,	PCI_PRODUCT_NVIDIA_MCP65_AHCI_1, 0xa2 },
104c7f4c6e4SSepherosa Ziehau 	{ PCI_VENDOR_NVIDIA,	PCI_PRODUCT_NVIDIA_MCP65_AHCI_2, 0xa2 },
105c7f4c6e4SSepherosa Ziehau 	{ PCI_VENDOR_NVIDIA,	PCI_PRODUCT_NVIDIA_MCP65_AHCI_3, 0xa2 },
106c7f4c6e4SSepherosa Ziehau 	{ PCI_VENDOR_NVIDIA,	PCI_PRODUCT_NVIDIA_MCP65_AHCI_4, 0xa2 },
107c7f4c6e4SSepherosa Ziehau 	{ PCI_VENDOR_NVIDIA,	PCI_PRODUCT_NVIDIA_MCP65_AHCI_5, 0xa2 },
108c7f4c6e4SSepherosa Ziehau 	{ PCI_VENDOR_NVIDIA,	PCI_PRODUCT_NVIDIA_MCP65_AHCI_6, 0xa2 },
109c7f4c6e4SSepherosa Ziehau 	{ PCI_VENDOR_NVIDIA,	PCI_PRODUCT_NVIDIA_MCP65_AHCI_7, 0xa2 },
110c7f4c6e4SSepherosa Ziehau 	{ PCI_VENDOR_NVIDIA,	PCI_PRODUCT_NVIDIA_MCP65_AHCI_8, 0xa2 }
111887d793aSSepherosa Ziehau };
112887d793aSSepherosa Ziehau 
1139783883aSSepherosa Ziehau static int	ahci_msi_enable = 1;
11431075e01SMatthew Dillon int	ahci_synchronous_boot = 1;
1159783883aSSepherosa Ziehau TUNABLE_INT("hw.ahci.msi.enable", &ahci_msi_enable);
11631075e01SMatthew Dillon TUNABLE_INT("hw.ahci.synchronous_boot", &ahci_synchronous_boot);
1179783883aSSepherosa Ziehau 
118258223a3SMatthew Dillon /*
119258223a3SMatthew Dillon  * Match during probe and attach.  The device does not yet have a softc.
120258223a3SMatthew Dillon  */
121258223a3SMatthew Dillon const struct ahci_device *
ahci_lookup_device(device_t dev)122258223a3SMatthew Dillon ahci_lookup_device(device_t dev)
123258223a3SMatthew Dillon {
124258223a3SMatthew Dillon 	const struct ahci_device *ad;
125258223a3SMatthew Dillon 	u_int16_t vendor = pci_get_vendor(dev);
126258223a3SMatthew Dillon 	u_int16_t product = pci_get_device(dev);
127258223a3SMatthew Dillon 	u_int8_t class = pci_get_class(dev);
128258223a3SMatthew Dillon 	u_int8_t subclass = pci_get_subclass(dev);
129258223a3SMatthew Dillon 	u_int8_t progif = pci_read_config(dev, PCIR_PROGIF, 1);
130e95151e4SMatthew Dillon 	int is_ahci;
131258223a3SMatthew Dillon 
132e95151e4SMatthew Dillon 	/*
133e95151e4SMatthew Dillon 	 * Generally speaking if the pci device does not identify as
134e95151e4SMatthew Dillon 	 * AHCI we skip it.
135e95151e4SMatthew Dillon 	 */
136e95151e4SMatthew Dillon 	if (class == PCIC_STORAGE && subclass == PCIS_STORAGE_SATA &&
137e95151e4SMatthew Dillon 	    progif == PCIP_STORAGE_SATA_AHCI_1_0) {
138e95151e4SMatthew Dillon 		is_ahci = 1;
139e95151e4SMatthew Dillon 	} else {
140e95151e4SMatthew Dillon 		is_ahci = 0;
141e95151e4SMatthew Dillon 	}
14212feb904SMatthew Dillon 
143258223a3SMatthew Dillon 	for (ad = &ahci_devices[0]; ad->ad_vendor; ++ad) {
144258223a3SMatthew Dillon 		if (ad->ad_vendor == vendor && ad->ad_product == product)
145258223a3SMatthew Dillon 			return (ad);
146258223a3SMatthew Dillon 	}
147258223a3SMatthew Dillon 
148258223a3SMatthew Dillon 	/*
149258223a3SMatthew Dillon 	 * Last ad is the default match if the PCI device matches SATA.
150258223a3SMatthew Dillon 	 */
151e95151e4SMatthew Dillon 	if (is_ahci == 0)
152e95151e4SMatthew Dillon 		ad = NULL;
153258223a3SMatthew Dillon 	return (ad);
154258223a3SMatthew Dillon }
155258223a3SMatthew Dillon 
156258223a3SMatthew Dillon /*
157258223a3SMatthew Dillon  * Attach functions.  They all eventually fall through to ahci_pci_attach().
158258223a3SMatthew Dillon  */
159258223a3SMatthew Dillon static int
ahci_vt8251_attach(device_t dev)160258223a3SMatthew Dillon ahci_vt8251_attach(device_t dev)
161258223a3SMatthew Dillon {
162258223a3SMatthew Dillon 	struct ahci_softc *sc = device_get_softc(dev);
163258223a3SMatthew Dillon 
164258223a3SMatthew Dillon 	sc->sc_flags |= AHCI_F_NO_NCQ;
165258223a3SMatthew Dillon 	return (ahci_pci_attach(dev));
166258223a3SMatthew Dillon }
167258223a3SMatthew Dillon 
168258223a3SMatthew Dillon static int
ahci_ati_sb600_attach(device_t dev)169258223a3SMatthew Dillon ahci_ati_sb600_attach(device_t dev)
170258223a3SMatthew Dillon {
171258223a3SMatthew Dillon 	struct ahci_softc *sc = device_get_softc(dev);
172258223a3SMatthew Dillon 	pcireg_t magic;
173258223a3SMatthew Dillon 	u_int8_t subclass = pci_get_subclass(dev);
174258223a3SMatthew Dillon 	u_int8_t revid;
175258223a3SMatthew Dillon 
176258223a3SMatthew Dillon 	if (subclass == PCIS_STORAGE_IDE) {
177258223a3SMatthew Dillon 		revid = pci_read_config(dev, PCIR_REVID, 1);
178258223a3SMatthew Dillon 		magic = pci_read_config(dev, AHCI_PCI_ATI_SB600_MAGIC, 4);
179258223a3SMatthew Dillon 		pci_write_config(dev, AHCI_PCI_ATI_SB600_MAGIC,
180258223a3SMatthew Dillon 				 magic | AHCI_PCI_ATI_SB600_LOCKED, 4);
181258223a3SMatthew Dillon 		pci_write_config(dev, PCIR_REVID,
182258223a3SMatthew Dillon 				 (PCIC_STORAGE << 24) |
183258223a3SMatthew Dillon 				 (PCIS_STORAGE_SATA << 16) |
184258223a3SMatthew Dillon 				 (PCIP_STORAGE_SATA_AHCI_1_0 << 8) |
185258223a3SMatthew Dillon 				 revid, 4);
186258223a3SMatthew Dillon 		pci_write_config(dev, AHCI_PCI_ATI_SB600_MAGIC, magic, 4);
187258223a3SMatthew Dillon 	}
188258223a3SMatthew Dillon 
189258223a3SMatthew Dillon 	sc->sc_flags |= AHCI_F_IGN_FR;
190258223a3SMatthew Dillon 	return (ahci_pci_attach(dev));
191258223a3SMatthew Dillon }
192258223a3SMatthew Dillon 
193258223a3SMatthew Dillon static int
ahci_nvidia_mcp_attach(device_t dev)194258223a3SMatthew Dillon ahci_nvidia_mcp_attach(device_t dev)
195258223a3SMatthew Dillon {
196258223a3SMatthew Dillon 	struct ahci_softc *sc = device_get_softc(dev);
197258223a3SMatthew Dillon 
198258223a3SMatthew Dillon 	sc->sc_flags |= AHCI_F_IGN_FR;
199258223a3SMatthew Dillon 	return (ahci_pci_attach(dev));
200258223a3SMatthew Dillon }
201258223a3SMatthew Dillon 
202258223a3SMatthew Dillon static int
ahci_pci_attach(device_t dev)203258223a3SMatthew Dillon ahci_pci_attach(device_t dev)
204258223a3SMatthew Dillon {
205258223a3SMatthew Dillon 	struct ahci_softc *sc = device_get_softc(dev);
206f4553de1SMatthew Dillon 	struct ahci_port *ap;
207258223a3SMatthew Dillon 	const char *gen;
208887d793aSSepherosa Ziehau 	uint16_t vid, did;
2094b450139SMatthew Dillon 	u_int32_t pi, reg;
2104b450139SMatthew Dillon 	u_int32_t cap, cap2;
211eb9f4c83SMatthew Dillon 	u_int32_t chip;
2129783883aSSepherosa Ziehau 	u_int irq_flags;
213258223a3SMatthew Dillon 	bus_addr_t addr;
2144b450139SMatthew Dillon 	int i, error, msi_enable, rev, fbs;
215e20b81f0SMatthew Dillon 	char revbuf[32];
216258223a3SMatthew Dillon 
21712feb904SMatthew Dillon 	if (pci_read_config(dev, PCIR_COMMAND, 2) & 0x0400) {
21877f3425bSMatthew Dillon 		device_printf(dev, "BIOS disabled PCI interrupt, "
21977f3425bSMatthew Dillon 				   "re-enabling\n");
22012feb904SMatthew Dillon 		pci_write_config(dev, PCIR_COMMAND,
22112feb904SMatthew Dillon 			pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400, 2);
22212feb904SMatthew Dillon 	}
22312feb904SMatthew Dillon 
224eb9f4c83SMatthew Dillon 	/*
225eb9f4c83SMatthew Dillon 	 * Chip quirks.  Sigh.  The AHCI spec is not in the least confusing
226eb9f4c83SMatthew Dillon 	 * when it comes to how the FR and CR bits work, but some AHCI
227eb9f4c83SMatthew Dillon 	 * chipsets (aka Marvell) either don't have the bits at all or they
228eb9f4c83SMatthew Dillon 	 * implement them poorly.
229eb9f4c83SMatthew Dillon 	 */
230eb9f4c83SMatthew Dillon 	chip = ((uint16_t)pci_get_device(dev) << 16) |
231eb9f4c83SMatthew Dillon 		(uint16_t)pci_get_vendor(dev);
232eb9f4c83SMatthew Dillon 
233eb9f4c83SMatthew Dillon 	switch(chip) {
234eb9f4c83SMatthew Dillon 	case 0x91721b4b:
235eb9f4c83SMatthew Dillon 		device_printf(dev,
236eb9f4c83SMatthew Dillon 			      "Enable 88SE9172 workarounds for broken chip\n");
237eb9f4c83SMatthew Dillon 		sc->sc_flags |= AHCI_F_IGN_FR;
238eb9f4c83SMatthew Dillon 		sc->sc_flags |= AHCI_F_IGN_CR;
239eb9f4c83SMatthew Dillon 		break;
240f0e79919SMatthew Dillon 	case 0x92151b4b:
241f0e79919SMatthew Dillon 		device_printf(dev,
242f0e79919SMatthew Dillon 			      "Enable 88SE9215 workarounds for broken chip\n");
243f0e79919SMatthew Dillon 		sc->sc_flags |= AHCI_F_IGN_FR;
244f0e79919SMatthew Dillon 		sc->sc_flags |= AHCI_F_IGN_CR;
245f0e79919SMatthew Dillon 		break;
246eb9f4c83SMatthew Dillon 	case 0x92301b4b:
247eb9f4c83SMatthew Dillon 		device_printf(dev,
248eb9f4c83SMatthew Dillon 			      "Enable 88SE9230 workarounds for broken chip\n");
249eb9f4c83SMatthew Dillon 		sc->sc_flags |= AHCI_F_CYCLE_FR;
250eb9f4c83SMatthew Dillon 		break;
251*47b52f8bSAntonio Huete Jimenez 	case 0x07f410de:
252*47b52f8bSAntonio Huete Jimenez 		device_printf(dev,
253*47b52f8bSAntonio Huete Jimenez 			      "Enable nForce 630i workarounds for broken chip\n");
254*47b52f8bSAntonio Huete Jimenez 		sc->sc_flags |= AHCI_F_IGN_FR;
255*47b52f8bSAntonio Huete Jimenez 		sc->sc_flags |= AHCI_F_IGN_CR;
256*47b52f8bSAntonio Huete Jimenez 		break;
257eb9f4c83SMatthew Dillon 	}
258eb9f4c83SMatthew Dillon 
2599783883aSSepherosa Ziehau 	sc->sc_dev = dev;
26012feb904SMatthew Dillon 
261258223a3SMatthew Dillon 	/*
262258223a3SMatthew Dillon 	 * Map the AHCI controller's IRQ and BAR(5) (hardware registers)
263258223a3SMatthew Dillon 	 */
264887d793aSSepherosa Ziehau 	msi_enable = ahci_msi_enable;
265887d793aSSepherosa Ziehau 
266887d793aSSepherosa Ziehau 	vid = pci_get_vendor(dev);
267887d793aSSepherosa Ziehau 	did = pci_get_device(dev);
268c7f4c6e4SSepherosa Ziehau 	rev = pci_get_revid(dev);
269887d793aSSepherosa Ziehau 	for (i = 0; i < NELEM(ahci_msi_blacklist); ++i) {
270c7f4c6e4SSepherosa Ziehau 		const struct ahci_pciid *id = &ahci_msi_blacklist[i];
271c7f4c6e4SSepherosa Ziehau 
272c7f4c6e4SSepherosa Ziehau 		if (vid == id->ahci_vid && did == id->ahci_did) {
273c7f4c6e4SSepherosa Ziehau 			if (id->ahci_rev < 0 || id->ahci_rev == rev) {
274887d793aSSepherosa Ziehau 				msi_enable = 0;
275887d793aSSepherosa Ziehau 				break;
276887d793aSSepherosa Ziehau 			}
277887d793aSSepherosa Ziehau 		}
278c7f4c6e4SSepherosa Ziehau 	}
279887d793aSSepherosa Ziehau 
280887d793aSSepherosa Ziehau 	sc->sc_irq_type = pci_alloc_1intr(dev, msi_enable,
2817fb43956SSepherosa Ziehau 	    &sc->sc_rid_irq, &irq_flags);
2829783883aSSepherosa Ziehau 
283258223a3SMatthew Dillon 	sc->sc_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->sc_rid_irq,
2849783883aSSepherosa Ziehau 	    irq_flags);
285258223a3SMatthew Dillon 	if (sc->sc_irq == NULL) {
286258223a3SMatthew Dillon 		device_printf(dev, "unable to map interrupt\n");
287258223a3SMatthew Dillon 		ahci_pci_detach(dev);
288258223a3SMatthew Dillon 		return (ENXIO);
289258223a3SMatthew Dillon 	}
290258223a3SMatthew Dillon 
291258223a3SMatthew Dillon 	/*
292258223a3SMatthew Dillon 	 * When mapping the register window store the tag and handle
293258223a3SMatthew Dillon 	 * separately so we can use the tag with per-port bus handle
294258223a3SMatthew Dillon 	 * sub-spaces.
295258223a3SMatthew Dillon 	 */
296258223a3SMatthew Dillon 	sc->sc_rid_regs = PCIR_BAR(5);
297258223a3SMatthew Dillon 	sc->sc_regs = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
298258223a3SMatthew Dillon 					     &sc->sc_rid_regs, RF_ACTIVE);
299258223a3SMatthew Dillon 	if (sc->sc_regs == NULL) {
300258223a3SMatthew Dillon 		device_printf(dev, "unable to map registers\n");
301258223a3SMatthew Dillon 		ahci_pci_detach(dev);
302258223a3SMatthew Dillon 		return (ENXIO);
303258223a3SMatthew Dillon 	}
304258223a3SMatthew Dillon 	sc->sc_iot = rman_get_bustag(sc->sc_regs);
305258223a3SMatthew Dillon 	sc->sc_ioh = rman_get_bushandle(sc->sc_regs);
306258223a3SMatthew Dillon 
307258223a3SMatthew Dillon 	/*
308258223a3SMatthew Dillon 	 * Initialize the chipset and then set the interrupt vector up
309258223a3SMatthew Dillon 	 */
310258223a3SMatthew Dillon 	error = ahci_init(sc);
311258223a3SMatthew Dillon 	if (error) {
312258223a3SMatthew Dillon 		ahci_pci_detach(dev);
313258223a3SMatthew Dillon 		return (ENXIO);
314258223a3SMatthew Dillon 	}
315258223a3SMatthew Dillon 
316258223a3SMatthew Dillon 	/*
317258223a3SMatthew Dillon 	 * Get the AHCI capabilities and max number of concurrent
3184b450139SMatthew Dillon 	 * command tags and set up the DMA tags.  Adjust the saved
3194b450139SMatthew Dillon 	 * sc_cap according to override flags.
320258223a3SMatthew Dillon 	 */
3218119d5f5SMatthew Dillon 	cap = ahci_read(sc, AHCI_REG_CAP);
322258223a3SMatthew Dillon 	if (sc->sc_flags & AHCI_F_NO_NCQ)
3238119d5f5SMatthew Dillon 		cap &= ~AHCI_REG_CAP_SNCQ;
3244b450139SMatthew Dillon 	if (sc->sc_flags & AHCI_F_FORCE_FBSS)
3258119d5f5SMatthew Dillon 		cap |= AHCI_REG_CAP_FBSS;
3268119d5f5SMatthew Dillon 	if (sc->sc_flags & AHCI_F_FORCE_SCLO)
3278119d5f5SMatthew Dillon 		cap |= AHCI_REG_CAP_SCLO;
3288119d5f5SMatthew Dillon 	sc->sc_cap = cap;
3291067474aSMatthew Dillon 
3301067474aSMatthew Dillon 	/*
3311067474aSMatthew Dillon 	 * We assume at least 4 commands.
3321067474aSMatthew Dillon 	 */
333258223a3SMatthew Dillon 	sc->sc_ncmds = AHCI_REG_CAP_NCS(cap);
3341067474aSMatthew Dillon 	if (sc->sc_ncmds < 4) {
3351067474aSMatthew Dillon 		device_printf(dev, "NCS must probe a value >= 4\n");
3361067474aSMatthew Dillon 		ahci_pci_detach(dev);
3371067474aSMatthew Dillon 		return (ENXIO);
3381067474aSMatthew Dillon 	}
339258223a3SMatthew Dillon 
340258223a3SMatthew Dillon 	addr = (cap & AHCI_REG_CAP_S64A) ?
341258223a3SMatthew Dillon 		BUS_SPACE_MAXADDR : BUS_SPACE_MAXADDR_32BIT;
342258223a3SMatthew Dillon 
343258223a3SMatthew Dillon 	/*
344258223a3SMatthew Dillon 	 * DMA tags for allocation of DMA memory buffers, lists, and so
345258223a3SMatthew Dillon 	 * forth.  These are typically per-port.
3464b450139SMatthew Dillon 	 *
3474b450139SMatthew Dillon 	 * When FIS-based switching is supported we need a rfis for
3484b450139SMatthew Dillon 	 * each target (4K total).  The spec also requires 4K alignment
3494b450139SMatthew Dillon 	 * for this case.
350258223a3SMatthew Dillon 	 */
3514b450139SMatthew Dillon 	fbs = (cap & AHCI_REG_CAP_FBSS) ? 16 : 1;
352258223a3SMatthew Dillon 	error = 0;
3534b450139SMatthew Dillon 
3540e589b85SMatthew Dillon 	sc->sc_rfis_size = sizeof(struct ahci_rfis) * fbs;
3550e589b85SMatthew Dillon 
356258223a3SMatthew Dillon 	error += bus_dma_tag_create(
357258223a3SMatthew Dillon 			NULL,				/* parent tag */
3580e589b85SMatthew Dillon 			sc->sc_rfis_size,		/* alignment */
359258223a3SMatthew Dillon 			PAGE_SIZE,			/* boundary */
360258223a3SMatthew Dillon 			addr,				/* loaddr? */
361258223a3SMatthew Dillon 			BUS_SPACE_MAXADDR,		/* hiaddr */
3620e589b85SMatthew Dillon 			sc->sc_rfis_size,		/* [max]size */
363258223a3SMatthew Dillon 			1,				/* maxsegs */
3640e589b85SMatthew Dillon 			sc->sc_rfis_size,		/* maxsegsz */
365258223a3SMatthew Dillon 			0,				/* flags */
366258223a3SMatthew Dillon 			&sc->sc_tag_rfis);		/* return tag */
367258223a3SMatthew Dillon 
3680e589b85SMatthew Dillon 	sc->sc_cmdlist_size = sc->sc_ncmds * sizeof(struct ahci_cmd_hdr);
3690e589b85SMatthew Dillon 
370258223a3SMatthew Dillon 	error += bus_dma_tag_create(
371258223a3SMatthew Dillon 			NULL,				/* parent tag */
372258223a3SMatthew Dillon 			32,				/* alignment */
373258223a3SMatthew Dillon 			4096 * 1024,			/* boundary */
374258223a3SMatthew Dillon 			addr,				/* loaddr? */
375258223a3SMatthew Dillon 			BUS_SPACE_MAXADDR,		/* hiaddr */
3760e589b85SMatthew Dillon 			sc->sc_cmdlist_size,
377258223a3SMatthew Dillon 			1,				/* maxsegs */
3780e589b85SMatthew Dillon 			sc->sc_cmdlist_size,
379258223a3SMatthew Dillon 			0,				/* flags */
380258223a3SMatthew Dillon 			&sc->sc_tag_cmdh);		/* return tag */
381258223a3SMatthew Dillon 
382258223a3SMatthew Dillon 	/*
383258223a3SMatthew Dillon 	 * NOTE: ahci_cmd_table is sized to a power of 2
384258223a3SMatthew Dillon 	 */
385258223a3SMatthew Dillon 	error += bus_dma_tag_create(
386258223a3SMatthew Dillon 			NULL,				/* parent tag */
387258223a3SMatthew Dillon 			sizeof(struct ahci_cmd_table),	/* alignment */
388258223a3SMatthew Dillon 			4096 * 1024,			/* boundary */
389258223a3SMatthew Dillon 			addr,				/* loaddr? */
390258223a3SMatthew Dillon 			BUS_SPACE_MAXADDR,		/* hiaddr */
391258223a3SMatthew Dillon 			sc->sc_ncmds * sizeof(struct ahci_cmd_table),
392258223a3SMatthew Dillon 			1,				/* maxsegs */
393258223a3SMatthew Dillon 			sc->sc_ncmds * sizeof(struct ahci_cmd_table),
394258223a3SMatthew Dillon 			0,				/* flags */
395258223a3SMatthew Dillon 			&sc->sc_tag_cmdt);		/* return tag */
396258223a3SMatthew Dillon 
397258223a3SMatthew Dillon 	/*
398258223a3SMatthew Dillon 	 * The data tag is used for later dmamaps and not immediately
399258223a3SMatthew Dillon 	 * allocated.
400258223a3SMatthew Dillon 	 */
401258223a3SMatthew Dillon 	error += bus_dma_tag_create(
402258223a3SMatthew Dillon 			NULL,				/* parent tag */
403258223a3SMatthew Dillon 			4,				/* alignment */
404258223a3SMatthew Dillon 			0,				/* boundary */
405258223a3SMatthew Dillon 			addr,				/* loaddr? */
406258223a3SMatthew Dillon 			BUS_SPACE_MAXADDR,		/* hiaddr */
407258223a3SMatthew Dillon 			4096 * 1024,			/* maxiosize */
408258223a3SMatthew Dillon 			AHCI_MAX_PRDT,			/* maxsegs */
409258223a3SMatthew Dillon 			65536,				/* maxsegsz */
410258223a3SMatthew Dillon 			0,				/* flags */
411258223a3SMatthew Dillon 			&sc->sc_tag_data);		/* return tag */
412258223a3SMatthew Dillon 
413258223a3SMatthew Dillon 	if (error) {
414258223a3SMatthew Dillon 		device_printf(dev, "unable to create dma tags\n");
415258223a3SMatthew Dillon 		ahci_pci_detach(dev);
416258223a3SMatthew Dillon 		return (ENXIO);
417258223a3SMatthew Dillon 	}
418258223a3SMatthew Dillon 
419258223a3SMatthew Dillon 	switch (cap & AHCI_REG_CAP_ISS) {
420258223a3SMatthew Dillon 	case AHCI_REG_CAP_ISS_G1:
421258223a3SMatthew Dillon 		gen = "1 (1.5Gbps)";
422258223a3SMatthew Dillon 		break;
4238986d351SMatthew Dillon 	case AHCI_REG_CAP_ISS_G2:
4248986d351SMatthew Dillon 		gen = "2 (3Gbps)";
4258986d351SMatthew Dillon 		break;
4268986d351SMatthew Dillon 	case AHCI_REG_CAP_ISS_G3:
4278986d351SMatthew Dillon 		gen = "3 (6Gbps)";
428258223a3SMatthew Dillon 		break;
429258223a3SMatthew Dillon 	default:
430258223a3SMatthew Dillon 		gen = "unknown";
431258223a3SMatthew Dillon 		break;
432258223a3SMatthew Dillon 	}
433258223a3SMatthew Dillon 
434258223a3SMatthew Dillon 	/* check the revision */
435258223a3SMatthew Dillon 	reg = ahci_read(sc, AHCI_REG_VS);
4364b450139SMatthew Dillon 
437e20b81f0SMatthew Dillon 	if (reg & 0x0000FF) {
438e20b81f0SMatthew Dillon 		ksnprintf(revbuf, sizeof(revbuf), "AHCI %d.%d.%d",
439e20b81f0SMatthew Dillon 			  (reg >> 16), (uint8_t)(reg >> 8), (uint8_t)reg);
440e20b81f0SMatthew Dillon 	} else {
441e20b81f0SMatthew Dillon 		ksnprintf(revbuf, sizeof(revbuf), "AHCI %d.%d",
442e20b81f0SMatthew Dillon 			  (reg >> 16), (uint8_t)(reg >> 8));
443258223a3SMatthew Dillon 	}
4444b450139SMatthew Dillon 	sc->sc_vers = reg;
445258223a3SMatthew Dillon 
4464b450139SMatthew Dillon 	if (reg >= AHCI_REG_VS_1_3) {
4474b450139SMatthew Dillon 		cap2 = ahci_read(sc, AHCI_REG_CAP2);
448258223a3SMatthew Dillon 		device_printf(dev,
449c3783d8fSzrj 			      "%s cap 0x%pb%i cap2 0x%pb%i, %d ports, "
4504b450139SMatthew Dillon 			      "%d tags/port, gen %s\n",
4513dfb4b7bSMatthew Dillon 			      revbuf,
452c3783d8fSzrj 			      AHCI_FMT_CAP, cap,
453c3783d8fSzrj 			      AHCI_FMT_CAP2, cap2,
4544b450139SMatthew Dillon 			      AHCI_REG_CAP_NP(cap), sc->sc_ncmds, gen);
4554b450139SMatthew Dillon 	} else {
4564b450139SMatthew Dillon 		cap2 = 0;
4574b450139SMatthew Dillon 		device_printf(dev,
458c3783d8fSzrj 			      "%s cap 0x%pb%i, %d ports, "
4594b450139SMatthew Dillon 			      "%d tags/port, gen %s\n",
4603dfb4b7bSMatthew Dillon 			      revbuf,
461c3783d8fSzrj 			      AHCI_FMT_CAP, cap,
462258223a3SMatthew Dillon 			      AHCI_REG_CAP_NP(cap), sc->sc_ncmds, gen);
4634b450139SMatthew Dillon 	}
4644b450139SMatthew Dillon 	sc->sc_cap2 = cap2;
465258223a3SMatthew Dillon 
466258223a3SMatthew Dillon 	pi = ahci_read(sc, AHCI_REG_PI);
467258223a3SMatthew Dillon 	DPRINTF(AHCI_D_VERBOSE, "%s: ports implemented: 0x%08x\n",
468258223a3SMatthew Dillon 	    DEVNAME(sc), pi);
469258223a3SMatthew Dillon 
4709abd2bb8SImre Vadász 	sc->sc_ipm_disable = AHCI_PREG_SCTL_IPM_NOPARTIAL |
4719abd2bb8SImre Vadász 			     AHCI_PREG_SCTL_IPM_NOSLUMBER;
4729abd2bb8SImre Vadász 	if (sc->sc_cap2 & AHCI_REG_CAP2_SDS)
4739abd2bb8SImre Vadász 		sc->sc_ipm_disable |= AHCI_PREG_SCTL_IPM_NODEVSLP;
4749abd2bb8SImre Vadász 
475258223a3SMatthew Dillon #ifdef AHCI_COALESCE
476258223a3SMatthew Dillon 	/* Naive coalescing support - enable for all ports. */
477258223a3SMatthew Dillon 	if (cap & AHCI_REG_CAP_CCCS) {
478258223a3SMatthew Dillon 		u_int16_t		ccc_timeout = 20;
479258223a3SMatthew Dillon 		u_int8_t		ccc_numcomplete = 12;
480258223a3SMatthew Dillon 		u_int32_t		ccc_ctl;
481258223a3SMatthew Dillon 
482258223a3SMatthew Dillon 		/* disable coalescing during reconfiguration. */
483258223a3SMatthew Dillon 		ccc_ctl = ahci_read(sc, AHCI_REG_CCC_CTL);
484258223a3SMatthew Dillon 		ccc_ctl &= ~0x00000001;
485258223a3SMatthew Dillon 		ahci_write(sc, AHCI_REG_CCC_CTL, ccc_ctl);
486258223a3SMatthew Dillon 
487258223a3SMatthew Dillon 		sc->sc_ccc_mask = 1 << AHCI_REG_CCC_CTL_INT(ccc_ctl);
488258223a3SMatthew Dillon 		if (pi & sc->sc_ccc_mask) {
489258223a3SMatthew Dillon 			/* A conflict with the implemented port list? */
490258223a3SMatthew Dillon 			printf("%s: coalescing interrupt/implemented port list "
491258223a3SMatthew Dillon 			    "conflict, PI: %08x, ccc_mask: %08x\n",
492258223a3SMatthew Dillon 			    DEVNAME(sc), pi, sc->sc_ccc_mask);
493258223a3SMatthew Dillon 			sc->sc_ccc_mask = 0;
494258223a3SMatthew Dillon 			goto noccc;
495258223a3SMatthew Dillon 		}
496258223a3SMatthew Dillon 
497258223a3SMatthew Dillon 		/* ahci_port_start will enable each port when it starts. */
498258223a3SMatthew Dillon 		sc->sc_ccc_ports = pi;
499258223a3SMatthew Dillon 		sc->sc_ccc_ports_cur = 0;
500258223a3SMatthew Dillon 
501258223a3SMatthew Dillon 		/* program thresholds and enable overall coalescing. */
502258223a3SMatthew Dillon 		ccc_ctl &= ~0xffffff00;
503258223a3SMatthew Dillon 		ccc_ctl |= (ccc_timeout << 16) | (ccc_numcomplete << 8);
504258223a3SMatthew Dillon 		ahci_write(sc, AHCI_REG_CCC_CTL, ccc_ctl);
505258223a3SMatthew Dillon 		ahci_write(sc, AHCI_REG_CCC_PORTS, 0);
506258223a3SMatthew Dillon 		ahci_write(sc, AHCI_REG_CCC_CTL, ccc_ctl | 1);
507258223a3SMatthew Dillon 	}
508258223a3SMatthew Dillon noccc:
509258223a3SMatthew Dillon #endif
510258223a3SMatthew Dillon 	/*
511258223a3SMatthew Dillon 	 * Allocate per-port resources
512258223a3SMatthew Dillon 	 *
513258223a3SMatthew Dillon 	 * Ignore attach errors, leave the port intact for
514258223a3SMatthew Dillon 	 * rescan and continue the loop.
515f4553de1SMatthew Dillon 	 *
516f4553de1SMatthew Dillon 	 * All ports are attached in parallel but the CAM scan-bus
517f4553de1SMatthew Dillon 	 * is held up until all ports are attached so we get a deterministic
518f4553de1SMatthew Dillon 	 * order.
519258223a3SMatthew Dillon 	 */
520258223a3SMatthew Dillon 	for (i = 0; error == 0 && i < AHCI_MAX_PORTS; i++) {
521258223a3SMatthew Dillon 		if ((pi & (1 << i)) == 0) {
522258223a3SMatthew Dillon 			/* dont allocate stuff if the port isnt implemented */
523258223a3SMatthew Dillon 			continue;
524258223a3SMatthew Dillon 		}
525258223a3SMatthew Dillon 		error = ahci_port_alloc(sc, i);
526258223a3SMatthew Dillon 	}
527258223a3SMatthew Dillon 
528258223a3SMatthew Dillon 	/*
529258223a3SMatthew Dillon 	 * Setup the interrupt vector and enable interrupts.  Note that
530258223a3SMatthew Dillon 	 * since the irq may be shared we do not set it up until we are
531258223a3SMatthew Dillon 	 * ready to go.
532258223a3SMatthew Dillon 	 */
533258223a3SMatthew Dillon 	if (error == 0) {
5348f191e54SMatthew Dillon 		error = bus_setup_intr(dev, sc->sc_irq, INTR_MPSAFE |
5358f191e54SMatthew Dillon 							INTR_HIFREQ,
536fb00c6edSMatthew Dillon 				       ahci_intr, sc,
537f4553de1SMatthew Dillon 				       &sc->sc_irq_handle, NULL);
538258223a3SMatthew Dillon 	}
539258223a3SMatthew Dillon 
540258223a3SMatthew Dillon 	if (error) {
541258223a3SMatthew Dillon 		device_printf(dev, "unable to install interrupt\n");
542258223a3SMatthew Dillon 		ahci_pci_detach(dev);
543258223a3SMatthew Dillon 		return (ENXIO);
544258223a3SMatthew Dillon 	}
545f4553de1SMatthew Dillon 
546f4553de1SMatthew Dillon 	/*
547e8cf3f55SMatthew Dillon 	 * Before marking the sc as good, which allows the interrupt
548e8cf3f55SMatthew Dillon 	 * subsystem to operate on the ports, wait for all the port threads
549e8cf3f55SMatthew Dillon 	 * to get past their initial pre-probe init.  Otherwise an interrupt
550e8cf3f55SMatthew Dillon 	 * may try to process the port before it has been initialized.
551e8cf3f55SMatthew Dillon 	 */
552e8cf3f55SMatthew Dillon 	for (i = 0; i < AHCI_MAX_PORTS; i++) {
553e8cf3f55SMatthew Dillon 		if ((ap = sc->sc_ports[i]) != NULL) {
554e8cf3f55SMatthew Dillon 			while (ap->ap_signal & AP_SIGF_THREAD_SYNC)
555e8cf3f55SMatthew Dillon 				tsleep(&ap->ap_signal, 0, "ahprb1", hz);
556e8cf3f55SMatthew Dillon 		}
557e8cf3f55SMatthew Dillon 	}
558e8cf3f55SMatthew Dillon 
559e8cf3f55SMatthew Dillon 	/*
560f4553de1SMatthew Dillon 	 * Master interrupt enable, and call ahci_intr() in case we race
561f4553de1SMatthew Dillon 	 * our AHCI_F_INT_GOOD flag.
562f4553de1SMatthew Dillon 	 */
563f4553de1SMatthew Dillon 	crit_enter();
564258223a3SMatthew Dillon 	ahci_write(sc, AHCI_REG_GHC, AHCI_REG_GHC_AE | AHCI_REG_GHC_IE);
565f4553de1SMatthew Dillon 	sc->sc_flags |= AHCI_F_INT_GOOD;
566f4553de1SMatthew Dillon 	crit_exit();
567f4553de1SMatthew Dillon 	ahci_intr(sc);
568f4553de1SMatthew Dillon 
569f4553de1SMatthew Dillon 	/*
57031075e01SMatthew Dillon 	 * Synchronously wait for some of the AHCI devices to initialize.
57131075e01SMatthew Dillon 	 *
572f4553de1SMatthew Dillon 	 * All ports are probing in parallel.  Wait for them to finish
573f4553de1SMatthew Dillon 	 * and then issue the cam attachment and bus scan serially so
574f4553de1SMatthew Dillon 	 * the 'da' assignments are deterministic.
575f4553de1SMatthew Dillon 	 */
57631075e01SMatthew Dillon 	for (i = 0; i < AHCI_MAX_PORTS && ahci_synchronous_boot; i++) {
577f4553de1SMatthew Dillon 		if ((ap = sc->sc_ports[i]) != NULL) {
578f4553de1SMatthew Dillon 			while (ap->ap_signal & AP_SIGF_INIT)
579e8cf3f55SMatthew Dillon 				tsleep(&ap->ap_signal, 0, "ahprb2", hz);
580831bc9e3SMatthew Dillon 			ahci_os_lock_port(ap);
581f4553de1SMatthew Dillon 			if (ahci_cam_attach(ap) == 0) {
582f4553de1SMatthew Dillon 				ahci_cam_changed(ap, NULL, -1);
583831bc9e3SMatthew Dillon 				ahci_os_unlock_port(ap);
584f4553de1SMatthew Dillon 				while ((ap->ap_flags & AP_F_SCAN_COMPLETED) == 0) {
58531075e01SMatthew Dillon 					tsleep(&ap->ap_flags, 0, "ahprb3", hz);
586f4553de1SMatthew Dillon 				}
587831bc9e3SMatthew Dillon 			} else {
588831bc9e3SMatthew Dillon 				ahci_os_unlock_port(ap);
589f4553de1SMatthew Dillon 			}
590f4553de1SMatthew Dillon 		}
591f4553de1SMatthew Dillon 	}
592258223a3SMatthew Dillon 
593258223a3SMatthew Dillon 	return(0);
594258223a3SMatthew Dillon }
595258223a3SMatthew Dillon 
596258223a3SMatthew Dillon /*
597258223a3SMatthew Dillon  * Device unload / detachment
598258223a3SMatthew Dillon  */
599258223a3SMatthew Dillon static int
ahci_pci_detach(device_t dev)600258223a3SMatthew Dillon ahci_pci_detach(device_t dev)
601258223a3SMatthew Dillon {
602258223a3SMatthew Dillon 	struct ahci_softc *sc = device_get_softc(dev);
603258223a3SMatthew Dillon 	struct ahci_port *ap;
604258223a3SMatthew Dillon 	int	i;
605258223a3SMatthew Dillon 
606258223a3SMatthew Dillon 	/*
607258223a3SMatthew Dillon 	 * Disable the controller and de-register the interrupt, if any.
608258223a3SMatthew Dillon 	 *
609f4553de1SMatthew Dillon 	 * XXX interlock last interrupt?
610258223a3SMatthew Dillon 	 */
611f4553de1SMatthew Dillon 	sc->sc_flags &= ~AHCI_F_INT_GOOD;
612f4553de1SMatthew Dillon 	if (sc->sc_regs)
613258223a3SMatthew Dillon 		ahci_write(sc, AHCI_REG_GHC, 0);
614f4553de1SMatthew Dillon 
615258223a3SMatthew Dillon 	if (sc->sc_irq_handle) {
616258223a3SMatthew Dillon 		bus_teardown_intr(dev, sc->sc_irq, sc->sc_irq_handle);
617258223a3SMatthew Dillon 		sc->sc_irq_handle = NULL;
618258223a3SMatthew Dillon 	}
619258223a3SMatthew Dillon 
620258223a3SMatthew Dillon 	/*
621258223a3SMatthew Dillon 	 * Free port structures and DMA memory
622258223a3SMatthew Dillon 	 */
623258223a3SMatthew Dillon 	for (i = 0; i < AHCI_MAX_PORTS; i++) {
624258223a3SMatthew Dillon 		ap = sc->sc_ports[i];
625258223a3SMatthew Dillon 		if (ap) {
626258223a3SMatthew Dillon 			ahci_cam_detach(ap);
627258223a3SMatthew Dillon 			ahci_port_free(sc, i);
628258223a3SMatthew Dillon 		}
629258223a3SMatthew Dillon 	}
630258223a3SMatthew Dillon 
631258223a3SMatthew Dillon 	/*
632258223a3SMatthew Dillon 	 * Clean up the bus space
633258223a3SMatthew Dillon 	 */
634258223a3SMatthew Dillon 	if (sc->sc_irq) {
635258223a3SMatthew Dillon 		bus_release_resource(dev, SYS_RES_IRQ,
636258223a3SMatthew Dillon 				     sc->sc_rid_irq, sc->sc_irq);
637258223a3SMatthew Dillon 		sc->sc_irq = NULL;
638258223a3SMatthew Dillon 	}
6399783883aSSepherosa Ziehau 
6407fb43956SSepherosa Ziehau 	if (sc->sc_irq_type == PCI_INTR_TYPE_MSI)
6419783883aSSepherosa Ziehau 		pci_release_msi(dev);
6429783883aSSepherosa Ziehau 
643258223a3SMatthew Dillon 	if (sc->sc_regs) {
644258223a3SMatthew Dillon 		bus_release_resource(dev, SYS_RES_MEMORY,
645258223a3SMatthew Dillon 				     sc->sc_rid_regs, sc->sc_regs);
646258223a3SMatthew Dillon 		sc->sc_regs = NULL;
647258223a3SMatthew Dillon 	}
648258223a3SMatthew Dillon 
649258223a3SMatthew Dillon 	if (sc->sc_tag_rfis) {
650258223a3SMatthew Dillon 		bus_dma_tag_destroy(sc->sc_tag_rfis);
651258223a3SMatthew Dillon 		sc->sc_tag_rfis = NULL;
652258223a3SMatthew Dillon 	}
653258223a3SMatthew Dillon 	if (sc->sc_tag_cmdh) {
654258223a3SMatthew Dillon 		bus_dma_tag_destroy(sc->sc_tag_cmdh);
655258223a3SMatthew Dillon 		sc->sc_tag_cmdh = NULL;
656258223a3SMatthew Dillon 	}
657258223a3SMatthew Dillon 	if (sc->sc_tag_cmdt) {
658258223a3SMatthew Dillon 		bus_dma_tag_destroy(sc->sc_tag_cmdt);
659258223a3SMatthew Dillon 		sc->sc_tag_cmdt = NULL;
660258223a3SMatthew Dillon 	}
661258223a3SMatthew Dillon 	if (sc->sc_tag_data) {
662258223a3SMatthew Dillon 		bus_dma_tag_destroy(sc->sc_tag_data);
663258223a3SMatthew Dillon 		sc->sc_tag_data = NULL;
664258223a3SMatthew Dillon 	}
665258223a3SMatthew Dillon 
666258223a3SMatthew Dillon 	return (0);
667258223a3SMatthew Dillon }
668