xref: /dflybsd-src/sys/dev/disk/ahci/ahci_attach.c (revision 1067474a12b26a343f24a3aae09f14cdaeb5bad4)
1258223a3SMatthew Dillon /*
2258223a3SMatthew Dillon  * Copyright (c) 2006 David Gwynne <dlg@openbsd.org>
3258223a3SMatthew Dillon  *
4258223a3SMatthew Dillon  * Permission to use, copy, modify, and distribute this software for any
5258223a3SMatthew Dillon  * purpose with or without fee is hereby granted, provided that the above
6258223a3SMatthew Dillon  * copyright notice and this permission notice appear in all copies.
7258223a3SMatthew Dillon  *
8258223a3SMatthew Dillon  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9258223a3SMatthew Dillon  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10258223a3SMatthew Dillon  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11258223a3SMatthew Dillon  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12258223a3SMatthew Dillon  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13258223a3SMatthew Dillon  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14258223a3SMatthew Dillon  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15258223a3SMatthew Dillon  *
16258223a3SMatthew Dillon  *
17258223a3SMatthew Dillon  * Copyright (c) 2009 The DragonFly Project.  All rights reserved.
18258223a3SMatthew Dillon  *
19258223a3SMatthew Dillon  * This code is derived from software contributed to The DragonFly Project
20258223a3SMatthew Dillon  * by Matthew Dillon <dillon@backplane.com>
21258223a3SMatthew Dillon  *
22258223a3SMatthew Dillon  * Redistribution and use in source and binary forms, with or without
23258223a3SMatthew Dillon  * modification, are permitted provided that the following conditions
24258223a3SMatthew Dillon  * are met:
25258223a3SMatthew Dillon  *
26258223a3SMatthew Dillon  * 1. Redistributions of source code must retain the above copyright
27258223a3SMatthew Dillon  *    notice, this list of conditions and the following disclaimer.
28258223a3SMatthew Dillon  * 2. Redistributions in binary form must reproduce the above copyright
29258223a3SMatthew Dillon  *    notice, this list of conditions and the following disclaimer in
30258223a3SMatthew Dillon  *    the documentation and/or other materials provided with the
31258223a3SMatthew Dillon  *    distribution.
32258223a3SMatthew Dillon  * 3. Neither the name of The DragonFly Project nor the names of its
33258223a3SMatthew Dillon  *    contributors may be used to endorse or promote products derived
34258223a3SMatthew Dillon  *    from this software without specific, prior written permission.
35258223a3SMatthew Dillon  *
36258223a3SMatthew Dillon  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
37258223a3SMatthew Dillon  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
38258223a3SMatthew Dillon  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
39258223a3SMatthew Dillon  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
40258223a3SMatthew Dillon  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
41258223a3SMatthew Dillon  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
42258223a3SMatthew Dillon  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43258223a3SMatthew Dillon  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
44258223a3SMatthew Dillon  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45258223a3SMatthew Dillon  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46258223a3SMatthew Dillon  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47258223a3SMatthew Dillon  * SUCH DAMAGE.
48258223a3SMatthew Dillon  *
49258223a3SMatthew Dillon  * $OpenBSD: ahci.c,v 1.147 2009/02/16 21:19:07 miod Exp $
50258223a3SMatthew Dillon  */
51258223a3SMatthew Dillon 
52258223a3SMatthew Dillon #include "ahci.h"
53258223a3SMatthew Dillon 
54258223a3SMatthew Dillon static int	ahci_vt8251_attach(device_t);
55258223a3SMatthew Dillon static int	ahci_ati_sb600_attach(device_t);
56258223a3SMatthew Dillon static int	ahci_nvidia_mcp_attach(device_t);
57258223a3SMatthew Dillon static int	ahci_pci_attach(device_t);
58258223a3SMatthew Dillon static int	ahci_pci_detach(device_t);
59258223a3SMatthew Dillon 
60258223a3SMatthew Dillon static const struct ahci_device ahci_devices[] = {
61258223a3SMatthew Dillon 	{ PCI_VENDOR_VIATECH,	PCI_PRODUCT_VIATECH_VT8251_SATA,
62258223a3SMatthew Dillon 	    ahci_vt8251_attach, ahci_pci_detach, "ViaTech-VT8251-SATA" },
63258223a3SMatthew Dillon 	{ PCI_VENDOR_ATI,	PCI_PRODUCT_ATI_SB600_SATA,
64258223a3SMatthew Dillon 	    ahci_ati_sb600_attach, ahci_pci_detach, "ATI-SB600-SATA" },
65258223a3SMatthew Dillon 	{ PCI_VENDOR_NVIDIA,	PCI_PRODUCT_NVIDIA_MCP65_AHCI_2,
66258223a3SMatthew Dillon 	    ahci_nvidia_mcp_attach, ahci_pci_detach, "NVidia-MCP65-SATA" },
67258223a3SMatthew Dillon 	{ PCI_VENDOR_NVIDIA,	PCI_PRODUCT_NVIDIA_MCP67_AHCI_1,
68258223a3SMatthew Dillon 	    ahci_nvidia_mcp_attach, ahci_pci_detach, "NVidia-MCP67-SATA" },
69258223a3SMatthew Dillon 	{ PCI_VENDOR_NVIDIA,	PCI_PRODUCT_NVIDIA_MCP77_AHCI_5,
70258223a3SMatthew Dillon 	    ahci_nvidia_mcp_attach, ahci_pci_detach, "NVidia-MCP77-SATA" },
71258223a3SMatthew Dillon 	{ 0, 0,
72258223a3SMatthew Dillon 	    ahci_pci_attach, ahci_pci_detach, "AHCI-PCI-SATA" }
73258223a3SMatthew Dillon };
74258223a3SMatthew Dillon 
75258223a3SMatthew Dillon u_int32_t AhciForceGen1 = 0;    /* XXX add sysctl/kenv support */
76258223a3SMatthew Dillon 
77258223a3SMatthew Dillon /*
78258223a3SMatthew Dillon  * Match during probe and attach.  The device does not yet have a softc.
79258223a3SMatthew Dillon  */
80258223a3SMatthew Dillon const struct ahci_device *
81258223a3SMatthew Dillon ahci_lookup_device(device_t dev)
82258223a3SMatthew Dillon {
83258223a3SMatthew Dillon 	const struct ahci_device *ad;
84258223a3SMatthew Dillon 	u_int16_t vendor = pci_get_vendor(dev);
85258223a3SMatthew Dillon 	u_int16_t product = pci_get_device(dev);
86258223a3SMatthew Dillon 	u_int8_t class = pci_get_class(dev);
87258223a3SMatthew Dillon 	u_int8_t subclass = pci_get_subclass(dev);
88258223a3SMatthew Dillon 	u_int8_t progif = pci_read_config(dev, PCIR_PROGIF, 1);
89258223a3SMatthew Dillon 
90258223a3SMatthew Dillon 	for (ad = &ahci_devices[0]; ad->ad_vendor; ++ad) {
91258223a3SMatthew Dillon 		if (ad->ad_vendor == vendor && ad->ad_product == product)
92258223a3SMatthew Dillon 			return (ad);
93258223a3SMatthew Dillon 	}
94258223a3SMatthew Dillon 
95258223a3SMatthew Dillon 	/*
96258223a3SMatthew Dillon 	 * Last ad is the default match if the PCI device matches SATA.
97258223a3SMatthew Dillon 	 */
98258223a3SMatthew Dillon 	if (class == PCIC_STORAGE && subclass == PCIS_STORAGE_SATA &&
99258223a3SMatthew Dillon 	    progif == PCIP_STORAGE_SATA_AHCI_1_0) {
100258223a3SMatthew Dillon 		kprintf("match generic sata\n");
101258223a3SMatthew Dillon 		return (ad);
102258223a3SMatthew Dillon 	}
103258223a3SMatthew Dillon 
104258223a3SMatthew Dillon 	return (NULL);
105258223a3SMatthew Dillon }
106258223a3SMatthew Dillon 
107258223a3SMatthew Dillon /*
108258223a3SMatthew Dillon  * Attach functions.  They all eventually fall through to ahci_pci_attach().
109258223a3SMatthew Dillon  */
110258223a3SMatthew Dillon static int
111258223a3SMatthew Dillon ahci_vt8251_attach(device_t dev)
112258223a3SMatthew Dillon {
113258223a3SMatthew Dillon 	struct ahci_softc *sc = device_get_softc(dev);
114258223a3SMatthew Dillon 
115258223a3SMatthew Dillon 	sc->sc_flags |= AHCI_F_NO_NCQ;
116258223a3SMatthew Dillon 	return (ahci_pci_attach(dev));
117258223a3SMatthew Dillon }
118258223a3SMatthew Dillon 
119258223a3SMatthew Dillon static int
120258223a3SMatthew Dillon ahci_ati_sb600_attach(device_t dev)
121258223a3SMatthew Dillon {
122258223a3SMatthew Dillon 	struct ahci_softc *sc = device_get_softc(dev);
123258223a3SMatthew Dillon 	pcireg_t magic;
124258223a3SMatthew Dillon 	u_int8_t subclass = pci_get_subclass(dev);
125258223a3SMatthew Dillon 	u_int8_t revid;
126258223a3SMatthew Dillon 
127258223a3SMatthew Dillon 	if (subclass == PCIS_STORAGE_IDE) {
128258223a3SMatthew Dillon 		revid = pci_read_config(dev, PCIR_REVID, 1);
129258223a3SMatthew Dillon 		magic = pci_read_config(dev, AHCI_PCI_ATI_SB600_MAGIC, 4);
130258223a3SMatthew Dillon 		pci_write_config(dev, AHCI_PCI_ATI_SB600_MAGIC,
131258223a3SMatthew Dillon 				 magic | AHCI_PCI_ATI_SB600_LOCKED, 4);
132258223a3SMatthew Dillon 		pci_write_config(dev, PCIR_REVID,
133258223a3SMatthew Dillon 				 (PCIC_STORAGE << 24) |
134258223a3SMatthew Dillon 				 (PCIS_STORAGE_SATA << 16) |
135258223a3SMatthew Dillon 				 (PCIP_STORAGE_SATA_AHCI_1_0 << 8) |
136258223a3SMatthew Dillon 				 revid, 4);
137258223a3SMatthew Dillon 		pci_write_config(dev, AHCI_PCI_ATI_SB600_MAGIC, magic, 4);
138258223a3SMatthew Dillon 	}
139258223a3SMatthew Dillon 
140258223a3SMatthew Dillon 	sc->sc_flags |= AHCI_F_IGN_FR;
141258223a3SMatthew Dillon 	return (ahci_pci_attach(dev));
142258223a3SMatthew Dillon }
143258223a3SMatthew Dillon 
144258223a3SMatthew Dillon static int
145258223a3SMatthew Dillon ahci_nvidia_mcp_attach(device_t dev)
146258223a3SMatthew Dillon {
147258223a3SMatthew Dillon 	struct ahci_softc *sc = device_get_softc(dev);
148258223a3SMatthew Dillon 
149258223a3SMatthew Dillon 	sc->sc_flags |= AHCI_F_IGN_FR;
150258223a3SMatthew Dillon 	return (ahci_pci_attach(dev));
151258223a3SMatthew Dillon }
152258223a3SMatthew Dillon 
153258223a3SMatthew Dillon static int
154258223a3SMatthew Dillon ahci_pci_attach(device_t dev)
155258223a3SMatthew Dillon {
156258223a3SMatthew Dillon 	struct ahci_softc *sc = device_get_softc(dev);
157f4553de1SMatthew Dillon 	struct ahci_port *ap;
158258223a3SMatthew Dillon 	const char *gen;
159258223a3SMatthew Dillon 	u_int32_t cap, pi, reg;
160258223a3SMatthew Dillon 	bus_addr_t addr;
161258223a3SMatthew Dillon 	int i;
162258223a3SMatthew Dillon 	int error;
163258223a3SMatthew Dillon 	const char *revision;
164258223a3SMatthew Dillon 
165258223a3SMatthew Dillon 	/*
166258223a3SMatthew Dillon 	 * Map the AHCI controller's IRQ and BAR(5) (hardware registers)
167258223a3SMatthew Dillon 	 */
168258223a3SMatthew Dillon 	sc->sc_dev = dev;
169258223a3SMatthew Dillon 	sc->sc_rid_irq = AHCI_IRQ_RID;
170258223a3SMatthew Dillon 	sc->sc_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->sc_rid_irq,
171258223a3SMatthew Dillon 					    RF_SHAREABLE | RF_ACTIVE);
172258223a3SMatthew Dillon 	if (sc->sc_irq == NULL) {
173258223a3SMatthew Dillon 		device_printf(dev, "unable to map interrupt\n");
174258223a3SMatthew Dillon 		ahci_pci_detach(dev);
175258223a3SMatthew Dillon 		return (ENXIO);
176258223a3SMatthew Dillon 	}
177258223a3SMatthew Dillon 
178258223a3SMatthew Dillon 	/*
179258223a3SMatthew Dillon 	 * When mapping the register window store the tag and handle
180258223a3SMatthew Dillon 	 * separately so we can use the tag with per-port bus handle
181258223a3SMatthew Dillon 	 * sub-spaces.
182258223a3SMatthew Dillon 	 */
183258223a3SMatthew Dillon 	sc->sc_rid_regs = PCIR_BAR(5);
184258223a3SMatthew Dillon 	sc->sc_regs = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
185258223a3SMatthew Dillon 					     &sc->sc_rid_regs, RF_ACTIVE);
186258223a3SMatthew Dillon 	if (sc->sc_regs == NULL) {
187258223a3SMatthew Dillon 		device_printf(dev, "unable to map registers\n");
188258223a3SMatthew Dillon 		ahci_pci_detach(dev);
189258223a3SMatthew Dillon 		return (ENXIO);
190258223a3SMatthew Dillon 	}
191258223a3SMatthew Dillon 	sc->sc_iot = rman_get_bustag(sc->sc_regs);
192258223a3SMatthew Dillon 	sc->sc_ioh = rman_get_bushandle(sc->sc_regs);
193258223a3SMatthew Dillon 
194258223a3SMatthew Dillon 	/*
195258223a3SMatthew Dillon 	 * Initialize the chipset and then set the interrupt vector up
196258223a3SMatthew Dillon 	 */
197258223a3SMatthew Dillon 	error = ahci_init(sc);
198258223a3SMatthew Dillon 	if (error) {
199258223a3SMatthew Dillon 		ahci_pci_detach(dev);
200258223a3SMatthew Dillon 		return (ENXIO);
201258223a3SMatthew Dillon 	}
202258223a3SMatthew Dillon 
203258223a3SMatthew Dillon 	/*
204258223a3SMatthew Dillon 	 * Get the AHCI capabilities and max number of concurrent
205258223a3SMatthew Dillon 	 * command tags and set up the DMA tags.
206258223a3SMatthew Dillon 	 */
207258223a3SMatthew Dillon 	cap = ahci_read(sc, AHCI_REG_CAP);
208258223a3SMatthew Dillon 	if (sc->sc_flags & AHCI_F_NO_NCQ)
209258223a3SMatthew Dillon 		cap &= ~AHCI_REG_CAP_SNCQ;
210258223a3SMatthew Dillon 	sc->sc_cap = cap;
211*1067474aSMatthew Dillon 
212*1067474aSMatthew Dillon 	/*
213*1067474aSMatthew Dillon 	 * We assume at least 4 commands.
214*1067474aSMatthew Dillon 	 */
215258223a3SMatthew Dillon 	sc->sc_ncmds = AHCI_REG_CAP_NCS(cap);
216*1067474aSMatthew Dillon 	if (sc->sc_ncmds < 4) {
217*1067474aSMatthew Dillon 		device_printf(dev, "NCS must probe a value >= 4\n");
218*1067474aSMatthew Dillon 		ahci_pci_detach(dev);
219*1067474aSMatthew Dillon 		return (ENXIO);
220*1067474aSMatthew Dillon 	}
221258223a3SMatthew Dillon 
222258223a3SMatthew Dillon 	addr = (cap & AHCI_REG_CAP_S64A) ?
223258223a3SMatthew Dillon 		BUS_SPACE_MAXADDR : BUS_SPACE_MAXADDR_32BIT;
224258223a3SMatthew Dillon 
225258223a3SMatthew Dillon 	/*
226258223a3SMatthew Dillon 	 * DMA tags for allocation of DMA memory buffers, lists, and so
227258223a3SMatthew Dillon 	 * forth.  These are typically per-port.
228258223a3SMatthew Dillon 	 */
229258223a3SMatthew Dillon 	error = 0;
230258223a3SMatthew Dillon 	error += bus_dma_tag_create(
231258223a3SMatthew Dillon 			NULL,				/* parent tag */
232258223a3SMatthew Dillon 			256,				/* alignment */
233258223a3SMatthew Dillon 			PAGE_SIZE,			/* boundary */
234258223a3SMatthew Dillon 			addr,				/* loaddr? */
235258223a3SMatthew Dillon 			BUS_SPACE_MAXADDR,		/* hiaddr */
236258223a3SMatthew Dillon 			NULL,				/* filter */
237258223a3SMatthew Dillon 			NULL,				/* filterarg */
238258223a3SMatthew Dillon 			sizeof(struct ahci_rfis),	/* [max]size */
239258223a3SMatthew Dillon 			1,				/* maxsegs */
240258223a3SMatthew Dillon 			sizeof(struct ahci_rfis),	/* maxsegsz */
241258223a3SMatthew Dillon 			0,				/* flags */
242258223a3SMatthew Dillon 			&sc->sc_tag_rfis);		/* return tag */
243258223a3SMatthew Dillon 
244258223a3SMatthew Dillon 	error += bus_dma_tag_create(
245258223a3SMatthew Dillon 			NULL,				/* parent tag */
246258223a3SMatthew Dillon 			32,				/* alignment */
247258223a3SMatthew Dillon 			4096 * 1024,			/* boundary */
248258223a3SMatthew Dillon 			addr,				/* loaddr? */
249258223a3SMatthew Dillon 			BUS_SPACE_MAXADDR,		/* hiaddr */
250258223a3SMatthew Dillon 			NULL,				/* filter */
251258223a3SMatthew Dillon 			NULL,				/* filterarg */
252258223a3SMatthew Dillon 			sc->sc_ncmds * sizeof(struct ahci_cmd_hdr),
253258223a3SMatthew Dillon 			1,				/* maxsegs */
254258223a3SMatthew Dillon 			sc->sc_ncmds * sizeof(struct ahci_cmd_hdr),
255258223a3SMatthew Dillon 			0,				/* flags */
256258223a3SMatthew Dillon 			&sc->sc_tag_cmdh);		/* return tag */
257258223a3SMatthew Dillon 
258258223a3SMatthew Dillon 	/*
259258223a3SMatthew Dillon 	 * NOTE: ahci_cmd_table is sized to a power of 2
260258223a3SMatthew Dillon 	 */
261258223a3SMatthew Dillon 	error += bus_dma_tag_create(
262258223a3SMatthew Dillon 			NULL,				/* parent tag */
263258223a3SMatthew Dillon 			sizeof(struct ahci_cmd_table),	/* alignment */
264258223a3SMatthew Dillon 			4096 * 1024,			/* boundary */
265258223a3SMatthew Dillon 			addr,				/* loaddr? */
266258223a3SMatthew Dillon 			BUS_SPACE_MAXADDR,		/* hiaddr */
267258223a3SMatthew Dillon 			NULL,				/* filter */
268258223a3SMatthew Dillon 			NULL,				/* filterarg */
269258223a3SMatthew Dillon 			sc->sc_ncmds * sizeof(struct ahci_cmd_table),
270258223a3SMatthew Dillon 			1,				/* maxsegs */
271258223a3SMatthew Dillon 			sc->sc_ncmds * sizeof(struct ahci_cmd_table),
272258223a3SMatthew Dillon 			0,				/* flags */
273258223a3SMatthew Dillon 			&sc->sc_tag_cmdt);		/* return tag */
274258223a3SMatthew Dillon 
275258223a3SMatthew Dillon 	/*
276258223a3SMatthew Dillon 	 * The data tag is used for later dmamaps and not immediately
277258223a3SMatthew Dillon 	 * allocated.
278258223a3SMatthew Dillon 	 */
279258223a3SMatthew Dillon 	error += bus_dma_tag_create(
280258223a3SMatthew Dillon 			NULL,				/* parent tag */
281258223a3SMatthew Dillon 			4,				/* alignment */
282258223a3SMatthew Dillon 			0,				/* boundary */
283258223a3SMatthew Dillon 			addr,				/* loaddr? */
284258223a3SMatthew Dillon 			BUS_SPACE_MAXADDR,		/* hiaddr */
285258223a3SMatthew Dillon 			NULL,				/* filter */
286258223a3SMatthew Dillon 			NULL,				/* filterarg */
287258223a3SMatthew Dillon 			4096 * 1024,			/* maxiosize */
288258223a3SMatthew Dillon 			AHCI_MAX_PRDT,			/* maxsegs */
289258223a3SMatthew Dillon 			65536,				/* maxsegsz */
290258223a3SMatthew Dillon 			0,				/* flags */
291258223a3SMatthew Dillon 			&sc->sc_tag_data);		/* return tag */
292258223a3SMatthew Dillon 
293258223a3SMatthew Dillon 	if (error) {
294258223a3SMatthew Dillon 		device_printf(dev, "unable to create dma tags\n");
295258223a3SMatthew Dillon 		ahci_pci_detach(dev);
296258223a3SMatthew Dillon 		return (ENXIO);
297258223a3SMatthew Dillon 	}
298258223a3SMatthew Dillon 
299258223a3SMatthew Dillon 	switch (cap & AHCI_REG_CAP_ISS) {
300258223a3SMatthew Dillon 	case AHCI_REG_CAP_ISS_G1:
301258223a3SMatthew Dillon 		gen = "1 (1.5Gbps)";
302258223a3SMatthew Dillon 		break;
303258223a3SMatthew Dillon 	case AHCI_REG_CAP_ISS_G1_2:
304258223a3SMatthew Dillon 		gen = "1 (1.5Gbps) and 2 (3Gbps)";
305258223a3SMatthew Dillon 		break;
306258223a3SMatthew Dillon 	default:
307258223a3SMatthew Dillon 		gen = "unknown";
308258223a3SMatthew Dillon 		break;
309258223a3SMatthew Dillon 	}
310258223a3SMatthew Dillon 
311258223a3SMatthew Dillon 	/* check the revision */
312258223a3SMatthew Dillon 	reg = ahci_read(sc, AHCI_REG_VS);
313258223a3SMatthew Dillon 	switch (reg) {
314258223a3SMatthew Dillon 	case AHCI_REG_VS_0_95:
315258223a3SMatthew Dillon 		revision = "AHCI 0.95";
316258223a3SMatthew Dillon 		break;
317258223a3SMatthew Dillon 	case AHCI_REG_VS_1_0:
318258223a3SMatthew Dillon 		revision = "AHCI 1.0";
319258223a3SMatthew Dillon 		break;
320258223a3SMatthew Dillon 	case AHCI_REG_VS_1_1:
321258223a3SMatthew Dillon 		revision = "AHCI 1.1";
322258223a3SMatthew Dillon 		break;
323258223a3SMatthew Dillon 	case AHCI_REG_VS_1_2:
324258223a3SMatthew Dillon 		revision = "AHCI 1.2";
325258223a3SMatthew Dillon 		break;
326258223a3SMatthew Dillon 	default:
327258223a3SMatthew Dillon 		device_printf(sc->sc_dev,
328258223a3SMatthew Dillon 			      "Warning: Unknown AHCI revision 0x%08x\n", reg);
329258223a3SMatthew Dillon 		revision = "AHCI <unknown>";
330258223a3SMatthew Dillon 		break;
331258223a3SMatthew Dillon 	}
332258223a3SMatthew Dillon 
333258223a3SMatthew Dillon 	device_printf(dev,
334258223a3SMatthew Dillon 		      "%s capabilities 0x%b, %d ports, %d tags/port, gen %s\n",
335258223a3SMatthew Dillon 		      revision,
336258223a3SMatthew Dillon 		      cap, AHCI_FMT_CAP,
337258223a3SMatthew Dillon 		      AHCI_REG_CAP_NP(cap), sc->sc_ncmds, gen);
338258223a3SMatthew Dillon 
339258223a3SMatthew Dillon 	pi = ahci_read(sc, AHCI_REG_PI);
340258223a3SMatthew Dillon 	DPRINTF(AHCI_D_VERBOSE, "%s: ports implemented: 0x%08x\n",
341258223a3SMatthew Dillon 	    DEVNAME(sc), pi);
342258223a3SMatthew Dillon 
343258223a3SMatthew Dillon #ifdef AHCI_COALESCE
344258223a3SMatthew Dillon 	/* Naive coalescing support - enable for all ports. */
345258223a3SMatthew Dillon 	if (cap & AHCI_REG_CAP_CCCS) {
346258223a3SMatthew Dillon 		u_int16_t		ccc_timeout = 20;
347258223a3SMatthew Dillon 		u_int8_t		ccc_numcomplete = 12;
348258223a3SMatthew Dillon 		u_int32_t		ccc_ctl;
349258223a3SMatthew Dillon 
350258223a3SMatthew Dillon 		/* disable coalescing during reconfiguration. */
351258223a3SMatthew Dillon 		ccc_ctl = ahci_read(sc, AHCI_REG_CCC_CTL);
352258223a3SMatthew Dillon 		ccc_ctl &= ~0x00000001;
353258223a3SMatthew Dillon 		ahci_write(sc, AHCI_REG_CCC_CTL, ccc_ctl);
354258223a3SMatthew Dillon 
355258223a3SMatthew Dillon 		sc->sc_ccc_mask = 1 << AHCI_REG_CCC_CTL_INT(ccc_ctl);
356258223a3SMatthew Dillon 		if (pi & sc->sc_ccc_mask) {
357258223a3SMatthew Dillon 			/* A conflict with the implemented port list? */
358258223a3SMatthew Dillon 			printf("%s: coalescing interrupt/implemented port list "
359258223a3SMatthew Dillon 			    "conflict, PI: %08x, ccc_mask: %08x\n",
360258223a3SMatthew Dillon 			    DEVNAME(sc), pi, sc->sc_ccc_mask);
361258223a3SMatthew Dillon 			sc->sc_ccc_mask = 0;
362258223a3SMatthew Dillon 			goto noccc;
363258223a3SMatthew Dillon 		}
364258223a3SMatthew Dillon 
365258223a3SMatthew Dillon 		/* ahci_port_start will enable each port when it starts. */
366258223a3SMatthew Dillon 		sc->sc_ccc_ports = pi;
367258223a3SMatthew Dillon 		sc->sc_ccc_ports_cur = 0;
368258223a3SMatthew Dillon 
369258223a3SMatthew Dillon 		/* program thresholds and enable overall coalescing. */
370258223a3SMatthew Dillon 		ccc_ctl &= ~0xffffff00;
371258223a3SMatthew Dillon 		ccc_ctl |= (ccc_timeout << 16) | (ccc_numcomplete << 8);
372258223a3SMatthew Dillon 		ahci_write(sc, AHCI_REG_CCC_CTL, ccc_ctl);
373258223a3SMatthew Dillon 		ahci_write(sc, AHCI_REG_CCC_PORTS, 0);
374258223a3SMatthew Dillon 		ahci_write(sc, AHCI_REG_CCC_CTL, ccc_ctl | 1);
375258223a3SMatthew Dillon 	}
376258223a3SMatthew Dillon noccc:
377258223a3SMatthew Dillon #endif
378258223a3SMatthew Dillon 	/*
379258223a3SMatthew Dillon 	 * Allocate per-port resources
380258223a3SMatthew Dillon 	 *
381258223a3SMatthew Dillon 	 * Ignore attach errors, leave the port intact for
382258223a3SMatthew Dillon 	 * rescan and continue the loop.
383f4553de1SMatthew Dillon 	 *
384f4553de1SMatthew Dillon 	 * All ports are attached in parallel but the CAM scan-bus
385f4553de1SMatthew Dillon 	 * is held up until all ports are attached so we get a deterministic
386f4553de1SMatthew Dillon 	 * order.
387258223a3SMatthew Dillon 	 */
388258223a3SMatthew Dillon 	for (i = 0; error == 0 && i < AHCI_MAX_PORTS; i++) {
389258223a3SMatthew Dillon 		if ((pi & (1 << i)) == 0) {
390258223a3SMatthew Dillon 			/* dont allocate stuff if the port isnt implemented */
391258223a3SMatthew Dillon 			continue;
392258223a3SMatthew Dillon 		}
393258223a3SMatthew Dillon 		error = ahci_port_alloc(sc, i);
394258223a3SMatthew Dillon 	}
395258223a3SMatthew Dillon 
396258223a3SMatthew Dillon 	/*
397258223a3SMatthew Dillon 	 * Setup the interrupt vector and enable interrupts.  Note that
398258223a3SMatthew Dillon 	 * since the irq may be shared we do not set it up until we are
399258223a3SMatthew Dillon 	 * ready to go.
400258223a3SMatthew Dillon 	 */
401258223a3SMatthew Dillon 	if (error == 0) {
402258223a3SMatthew Dillon 		error = bus_setup_intr(dev, sc->sc_irq, 0, ahci_intr, sc,
403f4553de1SMatthew Dillon 				       &sc->sc_irq_handle, NULL);
404258223a3SMatthew Dillon 	}
405258223a3SMatthew Dillon 
406258223a3SMatthew Dillon 	if (error) {
407258223a3SMatthew Dillon 		device_printf(dev, "unable to install interrupt\n");
408258223a3SMatthew Dillon 		ahci_pci_detach(dev);
409258223a3SMatthew Dillon 		return (ENXIO);
410258223a3SMatthew Dillon 	}
411f4553de1SMatthew Dillon 
412f4553de1SMatthew Dillon 	/*
413f4553de1SMatthew Dillon 	 * Master interrupt enable, and call ahci_intr() in case we race
414f4553de1SMatthew Dillon 	 * our AHCI_F_INT_GOOD flag.
415f4553de1SMatthew Dillon 	 */
416f4553de1SMatthew Dillon 	crit_enter();
417258223a3SMatthew Dillon 	ahci_write(sc, AHCI_REG_GHC, AHCI_REG_GHC_AE | AHCI_REG_GHC_IE);
418f4553de1SMatthew Dillon 	sc->sc_flags |= AHCI_F_INT_GOOD;
419f4553de1SMatthew Dillon 	crit_exit();
420f4553de1SMatthew Dillon 	ahci_intr(sc);
421f4553de1SMatthew Dillon 
422f4553de1SMatthew Dillon 	/*
423f4553de1SMatthew Dillon 	 * All ports are probing in parallel.  Wait for them to finish
424f4553de1SMatthew Dillon 	 * and then issue the cam attachment and bus scan serially so
425f4553de1SMatthew Dillon 	 * the 'da' assignments are deterministic.
426f4553de1SMatthew Dillon 	 */
427f4553de1SMatthew Dillon 	for (i = 0; i < AHCI_MAX_PORTS; i++) {
428f4553de1SMatthew Dillon 		if ((ap = sc->sc_ports[i]) != NULL) {
429f4553de1SMatthew Dillon 			while (ap->ap_signal & AP_SIGF_INIT)
430f4553de1SMatthew Dillon 				tsleep(&ap->ap_signal, 0, "ahprb1", hz);
431831bc9e3SMatthew Dillon 			ahci_os_lock_port(ap);
432f4553de1SMatthew Dillon 			if (ahci_cam_attach(ap) == 0) {
433f4553de1SMatthew Dillon 				ahci_cam_changed(ap, NULL, -1);
434831bc9e3SMatthew Dillon 				ahci_os_unlock_port(ap);
435f4553de1SMatthew Dillon 				while ((ap->ap_flags & AP_F_SCAN_COMPLETED) == 0) {
436f4553de1SMatthew Dillon 					tsleep(&ap->ap_flags, 0, "ahprb2", hz);
437f4553de1SMatthew Dillon 				}
438831bc9e3SMatthew Dillon 			} else {
439831bc9e3SMatthew Dillon 				ahci_os_unlock_port(ap);
440f4553de1SMatthew Dillon 			}
441f4553de1SMatthew Dillon 		}
442f4553de1SMatthew Dillon 	}
443258223a3SMatthew Dillon 
444258223a3SMatthew Dillon 	return(0);
445258223a3SMatthew Dillon }
446258223a3SMatthew Dillon 
447258223a3SMatthew Dillon /*
448258223a3SMatthew Dillon  * Device unload / detachment
449258223a3SMatthew Dillon  */
450258223a3SMatthew Dillon static int
451258223a3SMatthew Dillon ahci_pci_detach(device_t dev)
452258223a3SMatthew Dillon {
453258223a3SMatthew Dillon 	struct ahci_softc *sc = device_get_softc(dev);
454258223a3SMatthew Dillon 	struct ahci_port *ap;
455258223a3SMatthew Dillon 	int	i;
456258223a3SMatthew Dillon 
457258223a3SMatthew Dillon 	/*
458258223a3SMatthew Dillon 	 * Disable the controller and de-register the interrupt, if any.
459258223a3SMatthew Dillon 	 *
460f4553de1SMatthew Dillon 	 * XXX interlock last interrupt?
461258223a3SMatthew Dillon 	 */
462f4553de1SMatthew Dillon 	sc->sc_flags &= ~AHCI_F_INT_GOOD;
463f4553de1SMatthew Dillon 	if (sc->sc_regs)
464258223a3SMatthew Dillon 		ahci_write(sc, AHCI_REG_GHC, 0);
465f4553de1SMatthew Dillon 
466258223a3SMatthew Dillon 	if (sc->sc_irq_handle) {
467258223a3SMatthew Dillon 		bus_teardown_intr(dev, sc->sc_irq, sc->sc_irq_handle);
468258223a3SMatthew Dillon 		sc->sc_irq_handle = NULL;
469258223a3SMatthew Dillon 	}
470258223a3SMatthew Dillon 
471258223a3SMatthew Dillon 	/*
472258223a3SMatthew Dillon 	 * Free port structures and DMA memory
473258223a3SMatthew Dillon 	 */
474258223a3SMatthew Dillon 	for (i = 0; i < AHCI_MAX_PORTS; i++) {
475258223a3SMatthew Dillon 		ap = sc->sc_ports[i];
476258223a3SMatthew Dillon 		if (ap) {
477258223a3SMatthew Dillon 			ahci_cam_detach(ap);
478258223a3SMatthew Dillon 			ahci_port_free(sc, i);
479258223a3SMatthew Dillon 		}
480258223a3SMatthew Dillon 	}
481258223a3SMatthew Dillon 
482258223a3SMatthew Dillon 	/*
483258223a3SMatthew Dillon 	 * Clean up the bus space
484258223a3SMatthew Dillon 	 */
485258223a3SMatthew Dillon 	if (sc->sc_irq) {
486258223a3SMatthew Dillon 		bus_release_resource(dev, SYS_RES_IRQ,
487258223a3SMatthew Dillon 				     sc->sc_rid_irq, sc->sc_irq);
488258223a3SMatthew Dillon 		sc->sc_irq = NULL;
489258223a3SMatthew Dillon 	}
490258223a3SMatthew Dillon 	if (sc->sc_regs) {
491258223a3SMatthew Dillon 		bus_release_resource(dev, SYS_RES_MEMORY,
492258223a3SMatthew Dillon 				     sc->sc_rid_regs, sc->sc_regs);
493258223a3SMatthew Dillon 		sc->sc_regs = NULL;
494258223a3SMatthew Dillon 	}
495258223a3SMatthew Dillon 
496258223a3SMatthew Dillon 	if (sc->sc_tag_rfis) {
497258223a3SMatthew Dillon 		bus_dma_tag_destroy(sc->sc_tag_rfis);
498258223a3SMatthew Dillon 		sc->sc_tag_rfis = NULL;
499258223a3SMatthew Dillon 	}
500258223a3SMatthew Dillon 	if (sc->sc_tag_cmdh) {
501258223a3SMatthew Dillon 		bus_dma_tag_destroy(sc->sc_tag_cmdh);
502258223a3SMatthew Dillon 		sc->sc_tag_cmdh = NULL;
503258223a3SMatthew Dillon 	}
504258223a3SMatthew Dillon 	if (sc->sc_tag_cmdt) {
505258223a3SMatthew Dillon 		bus_dma_tag_destroy(sc->sc_tag_cmdt);
506258223a3SMatthew Dillon 		sc->sc_tag_cmdt = NULL;
507258223a3SMatthew Dillon 	}
508258223a3SMatthew Dillon 	if (sc->sc_tag_data) {
509258223a3SMatthew Dillon 		bus_dma_tag_destroy(sc->sc_tag_data);
510258223a3SMatthew Dillon 		sc->sc_tag_data = NULL;
511258223a3SMatthew Dillon 	}
512258223a3SMatthew Dillon 
513258223a3SMatthew Dillon 	return (0);
514258223a3SMatthew Dillon }
515