xref: /dflybsd-src/sys/dev/disk/sili/sili_attach.c (revision 030b0c8c4cf27c560ccec70410c8e21934ae677d)
11ac8d5baSMatthew Dillon /*
2fb00c6edSMatthew Dillon  * (MPSAFE)
3fb00c6edSMatthew Dillon  *
41ac8d5baSMatthew Dillon  * Copyright (c) 2006 David Gwynne <dlg@openbsd.org>
51ac8d5baSMatthew Dillon  *
61ac8d5baSMatthew Dillon  * Permission to use, copy, modify, and distribute this software for any
71ac8d5baSMatthew Dillon  * purpose with or without fee is hereby granted, provided that the above
81ac8d5baSMatthew Dillon  * copyright notice and this permission notice appear in all copies.
91ac8d5baSMatthew Dillon  *
101ac8d5baSMatthew Dillon  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
111ac8d5baSMatthew Dillon  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
121ac8d5baSMatthew Dillon  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
131ac8d5baSMatthew Dillon  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
141ac8d5baSMatthew Dillon  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
151ac8d5baSMatthew Dillon  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
161ac8d5baSMatthew Dillon  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
171ac8d5baSMatthew Dillon  *
181ac8d5baSMatthew Dillon  *
191ac8d5baSMatthew Dillon  * Copyright (c) 2009 The DragonFly Project.  All rights reserved.
201ac8d5baSMatthew Dillon  *
211ac8d5baSMatthew Dillon  * This code is derived from software contributed to The DragonFly Project
221ac8d5baSMatthew Dillon  * by Matthew Dillon <dillon@backplane.com>
231ac8d5baSMatthew Dillon  *
241ac8d5baSMatthew Dillon  * Redistribution and use in source and binary forms, with or without
251ac8d5baSMatthew Dillon  * modification, are permitted provided that the following conditions
261ac8d5baSMatthew Dillon  * are met:
271ac8d5baSMatthew Dillon  *
281ac8d5baSMatthew Dillon  * 1. Redistributions of source code must retain the above copyright
291ac8d5baSMatthew Dillon  *    notice, this list of conditions and the following disclaimer.
301ac8d5baSMatthew Dillon  * 2. Redistributions in binary form must reproduce the above copyright
311ac8d5baSMatthew Dillon  *    notice, this list of conditions and the following disclaimer in
321ac8d5baSMatthew Dillon  *    the documentation and/or other materials provided with the
331ac8d5baSMatthew Dillon  *    distribution.
341ac8d5baSMatthew Dillon  * 3. Neither the name of The DragonFly Project nor the names of its
351ac8d5baSMatthew Dillon  *    contributors may be used to endorse or promote products derived
361ac8d5baSMatthew Dillon  *    from this software without specific, prior written permission.
371ac8d5baSMatthew Dillon  *
381ac8d5baSMatthew Dillon  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
391ac8d5baSMatthew Dillon  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
401ac8d5baSMatthew Dillon  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
411ac8d5baSMatthew Dillon  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
421ac8d5baSMatthew Dillon  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
431ac8d5baSMatthew Dillon  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
441ac8d5baSMatthew Dillon  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
451ac8d5baSMatthew Dillon  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
461ac8d5baSMatthew Dillon  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
471ac8d5baSMatthew Dillon  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
481ac8d5baSMatthew Dillon  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
491ac8d5baSMatthew Dillon  * SUCH DAMAGE.
501ac8d5baSMatthew Dillon  *
511ac8d5baSMatthew Dillon  * $OpenBSD: sili.c,v 1.147 2009/02/16 21:19:07 miod Exp $
521ac8d5baSMatthew Dillon  */
531ac8d5baSMatthew Dillon 
541ac8d5baSMatthew Dillon #include "sili.h"
551ac8d5baSMatthew Dillon 
561ac8d5baSMatthew Dillon static int	sili_pci_attach(device_t);
571ac8d5baSMatthew Dillon static int	sili_pci_detach(device_t);
581ac8d5baSMatthew Dillon 
591ac8d5baSMatthew Dillon static const struct sili_device sili_devices[] = {
601ac8d5baSMatthew Dillon 	{
611ac8d5baSMatthew Dillon 		.ad_vendor = PCI_VENDOR_SII,
621ac8d5baSMatthew Dillon 		.ad_product = PCI_PRODUCT_SII_3132,
631ac8d5baSMatthew Dillon 		.ad_nports = 2,
641ac8d5baSMatthew Dillon 		.ad_attach = sili_pci_attach,
651ac8d5baSMatthew Dillon 		.ad_detach = sili_pci_detach,
661ac8d5baSMatthew Dillon 		.name = "SiliconImage-3132-SATA"
671ac8d5baSMatthew Dillon 	},
68a68ed61dSMatthew Dillon 	{
69a68ed61dSMatthew Dillon 		.ad_vendor = PCI_VENDOR_SII,
70a68ed61dSMatthew Dillon 		.ad_product = 0x3124,
71a68ed61dSMatthew Dillon 		.ad_nports = 4,
72a68ed61dSMatthew Dillon 		.ad_attach = sili_pci_attach,
73a68ed61dSMatthew Dillon 		.ad_detach = sili_pci_detach,
74a68ed61dSMatthew Dillon 		.name = "Rosewill-3124-SATA"
75a68ed61dSMatthew Dillon 	},
761ac8d5baSMatthew Dillon 	{ 0, 0, 0, NULL, NULL, NULL }
771ac8d5baSMatthew Dillon };
781ac8d5baSMatthew Dillon 
791ac8d5baSMatthew Dillon /*
80*c2a27f53SSepherosa Ziehau  * Don't enable MSI by default; it does not seem to
81*c2a27f53SSepherosa Ziehau  * work at all on Silicon Image 3132.
82*c2a27f53SSepherosa Ziehau  */
83*c2a27f53SSepherosa Ziehau static int sili_msi_enable = 0;
84*c2a27f53SSepherosa Ziehau TUNABLE_INT("hw.sili.msi.enable", &sili_msi_enable);
85*c2a27f53SSepherosa Ziehau 
86*c2a27f53SSepherosa Ziehau /*
871ac8d5baSMatthew Dillon  * Match during probe and attach.  The device does not yet have a softc.
881ac8d5baSMatthew Dillon  */
891ac8d5baSMatthew Dillon const struct sili_device *
sili_lookup_device(device_t dev)901ac8d5baSMatthew Dillon sili_lookup_device(device_t dev)
911ac8d5baSMatthew Dillon {
921ac8d5baSMatthew Dillon 	const struct sili_device *ad;
931ac8d5baSMatthew Dillon 	u_int16_t vendor = pci_get_vendor(dev);
941ac8d5baSMatthew Dillon 	u_int16_t product = pci_get_device(dev);
951ac8d5baSMatthew Dillon #if 0
961ac8d5baSMatthew Dillon 	u_int8_t class = pci_get_class(dev);
971ac8d5baSMatthew Dillon 	u_int8_t subclass = pci_get_subclass(dev);
981ac8d5baSMatthew Dillon 	u_int8_t progif = pci_read_config(dev, PCIR_PROGIF, 1);
991ac8d5baSMatthew Dillon #endif
1001ac8d5baSMatthew Dillon 
1011ac8d5baSMatthew Dillon 	for (ad = &sili_devices[0]; ad->ad_vendor; ++ad) {
1021ac8d5baSMatthew Dillon 		if (ad->ad_vendor == vendor && ad->ad_product == product)
1031ac8d5baSMatthew Dillon 			return (ad);
1041ac8d5baSMatthew Dillon 	}
1051ac8d5baSMatthew Dillon 	return (NULL);
1061ac8d5baSMatthew Dillon #if 0
1071ac8d5baSMatthew Dillon 	/*
1081ac8d5baSMatthew Dillon 	 * Last ad is the default match if the PCI device matches SATA.
1091ac8d5baSMatthew Dillon 	 */
1101ac8d5baSMatthew Dillon 	if (class == PCIC_STORAGE && subclass == PCIS_STORAGE_SATA &&
1111ac8d5baSMatthew Dillon 	    progif == PCIP_STORAGE_SATA_SILI_1_0) {
1121ac8d5baSMatthew Dillon 		return (ad);
1131ac8d5baSMatthew Dillon 	}
1141ac8d5baSMatthew Dillon 	return (NULL);
1151ac8d5baSMatthew Dillon #endif
1161ac8d5baSMatthew Dillon }
1171ac8d5baSMatthew Dillon 
1181ac8d5baSMatthew Dillon static int
sili_pci_attach(device_t dev)1191ac8d5baSMatthew Dillon sili_pci_attach(device_t dev)
1201ac8d5baSMatthew Dillon {
1211ac8d5baSMatthew Dillon 	struct sili_softc *sc = device_get_softc(dev);
1221ac8d5baSMatthew Dillon 	struct sili_port *ap;
1231ac8d5baSMatthew Dillon 	const char *gen;
1241ac8d5baSMatthew Dillon 	u_int32_t nports, reg;
1251ac8d5baSMatthew Dillon 	bus_addr_t addr;
126*c2a27f53SSepherosa Ziehau 	int i, error, msi_enable;
127*c2a27f53SSepherosa Ziehau 	u_int irq_flags;
1281ac8d5baSMatthew Dillon 
1291ac8d5baSMatthew Dillon 	/*
1301ac8d5baSMatthew Dillon 	 * Map the SILI controller's IRQ, BAR(0) (global regs),
1311ac8d5baSMatthew Dillon 	 * and BAR(1) (port regs and lram).
1321ac8d5baSMatthew Dillon 	 */
133*c2a27f53SSepherosa Ziehau 
134*c2a27f53SSepherosa Ziehau 	msi_enable = sili_msi_enable;
135*c2a27f53SSepherosa Ziehau 	if (!pci_is_pcie(dev)) {
136*c2a27f53SSepherosa Ziehau 		/*
137*c2a27f53SSepherosa Ziehau 		 * Don't enable MSI on PCI devices by default;
138*c2a27f53SSepherosa Ziehau 		 * well, this may cause less trouble.
139*c2a27f53SSepherosa Ziehau 		 */
140*c2a27f53SSepherosa Ziehau 		msi_enable = 0;
141*c2a27f53SSepherosa Ziehau 	}
142*c2a27f53SSepherosa Ziehau 	sc->sc_irq_type = pci_alloc_1intr(dev, msi_enable, &sc->sc_rid_irq,
143*c2a27f53SSepherosa Ziehau 	    &irq_flags);
144*c2a27f53SSepherosa Ziehau 
1451ac8d5baSMatthew Dillon 	sc->sc_dev = dev;
1461ac8d5baSMatthew Dillon 	sc->sc_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->sc_rid_irq,
147*c2a27f53SSepherosa Ziehau 	    irq_flags);
1481ac8d5baSMatthew Dillon 	if (sc->sc_irq == NULL) {
1491ac8d5baSMatthew Dillon 		device_printf(dev, "unable to map interrupt\n");
1501ac8d5baSMatthew Dillon 		sili_pci_detach(dev);
1511ac8d5baSMatthew Dillon 		return (ENXIO);
1521ac8d5baSMatthew Dillon 	}
1531ac8d5baSMatthew Dillon 
1541ac8d5baSMatthew Dillon 	/*
1551ac8d5baSMatthew Dillon 	 * When mapping the register window store the tag and handle
1561ac8d5baSMatthew Dillon 	 * separately so we can use the tag with per-port bus handle
1571ac8d5baSMatthew Dillon 	 * sub-spaces.
1581ac8d5baSMatthew Dillon 	 */
1591ac8d5baSMatthew Dillon 	sc->sc_rid_regs = PCIR_BAR(0);
1601ac8d5baSMatthew Dillon 	sc->sc_regs = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
1611ac8d5baSMatthew Dillon 					     &sc->sc_rid_regs, RF_ACTIVE);
1621ac8d5baSMatthew Dillon 	if (sc->sc_regs == NULL) {
1631ac8d5baSMatthew Dillon 		device_printf(dev, "unable to map registers\n");
1641ac8d5baSMatthew Dillon 		sili_pci_detach(dev);
1651ac8d5baSMatthew Dillon 		return (ENXIO);
1661ac8d5baSMatthew Dillon 	}
1671ac8d5baSMatthew Dillon 	sc->sc_iot = rman_get_bustag(sc->sc_regs);
1681ac8d5baSMatthew Dillon 	sc->sc_ioh = rman_get_bushandle(sc->sc_regs);
1691ac8d5baSMatthew Dillon 
1701ac8d5baSMatthew Dillon 	sc->sc_rid_pregs = PCIR_BAR(2);
1711ac8d5baSMatthew Dillon 	sc->sc_pregs = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
1721ac8d5baSMatthew Dillon 					      &sc->sc_rid_pregs, RF_ACTIVE);
1731ac8d5baSMatthew Dillon 	if (sc->sc_pregs == NULL) {
1741ac8d5baSMatthew Dillon 		device_printf(dev, "unable to map port registers\n");
1751ac8d5baSMatthew Dillon 		sili_pci_detach(dev);
1761ac8d5baSMatthew Dillon 		return (ENXIO);
1771ac8d5baSMatthew Dillon 	}
1781ac8d5baSMatthew Dillon 	sc->sc_piot = rman_get_bustag(sc->sc_pregs);
1791ac8d5baSMatthew Dillon 	sc->sc_pioh = rman_get_bushandle(sc->sc_pregs);
1801ac8d5baSMatthew Dillon 
1811ac8d5baSMatthew Dillon 	/*
1821ac8d5baSMatthew Dillon 	 * Initialize the chipset and then set the interrupt vector up
1831ac8d5baSMatthew Dillon 	 */
1841ac8d5baSMatthew Dillon 	error = sili_init(sc);
1851ac8d5baSMatthew Dillon 	if (error) {
1861ac8d5baSMatthew Dillon 		sili_pci_detach(dev);
1871ac8d5baSMatthew Dillon 		return (ENXIO);
1881ac8d5baSMatthew Dillon 	}
1891ac8d5baSMatthew Dillon 
1901ac8d5baSMatthew Dillon 	/*
1911ac8d5baSMatthew Dillon 	 * We assume at least 4 commands.
1921ac8d5baSMatthew Dillon 	 */
1931ac8d5baSMatthew Dillon 	sc->sc_ncmds = SILI_MAX_CMDS;
1941ac8d5baSMatthew Dillon 	sc->sc_flags |= SILI_F_64BIT;
1951ac8d5baSMatthew Dillon 	sc->sc_flags |= SILI_F_NCQ;
1961ac8d5baSMatthew Dillon 	sc->sc_flags |= SILI_F_SSNTF;
1971ac8d5baSMatthew Dillon 	sc->sc_flags |= SILI_F_SPM;
1981ac8d5baSMatthew Dillon 
1991ac8d5baSMatthew Dillon 	addr = (sc->sc_flags & SILI_F_64BIT) ?
2001ac8d5baSMatthew Dillon 		BUS_SPACE_MAXADDR : BUS_SPACE_MAXADDR_32BIT;
2011ac8d5baSMatthew Dillon 
2021ac8d5baSMatthew Dillon 	/*
2031ac8d5baSMatthew Dillon 	 * DMA tags for allocation of DMA memory buffers, lists, and so
2041ac8d5baSMatthew Dillon 	 * forth.  These are typically per-port.
2051ac8d5baSMatthew Dillon 	 *
2061ac8d5baSMatthew Dillon 	 * The stuff is mostly built into the BAR mappings.  We only need
2071ac8d5baSMatthew Dillon 	 * tags for our external SGE list and data.
2081ac8d5baSMatthew Dillon 	 */
2091ac8d5baSMatthew Dillon 	error = 0;
2101ac8d5baSMatthew Dillon 	error += bus_dma_tag_create(
2111ac8d5baSMatthew Dillon 			NULL,				/* parent tag */
2121ac8d5baSMatthew Dillon 			256,				/* alignment */
2131ac8d5baSMatthew Dillon 			65536,				/* boundary */
2141ac8d5baSMatthew Dillon 			addr,				/* loaddr? */
2151ac8d5baSMatthew Dillon 			BUS_SPACE_MAXADDR,		/* hiaddr */
2162102f407SMatthew Dillon 			sizeof(struct sili_prb) * SILI_MAX_CMDS,
2171ac8d5baSMatthew Dillon 							/* [max]size */
2181ac8d5baSMatthew Dillon 			1,				/* maxsegs */
2192102f407SMatthew Dillon 			sizeof(struct sili_prb) * SILI_MAX_CMDS,
2201ac8d5baSMatthew Dillon 							/* maxsegsz */
2211ac8d5baSMatthew Dillon 			0,				/* flags */
2222102f407SMatthew Dillon 			&sc->sc_tag_prbs);		/* return tag */
2231ac8d5baSMatthew Dillon 
2241ac8d5baSMatthew Dillon 	/*
2251ac8d5baSMatthew Dillon 	 * The data tag is used for later dmamaps and not immediately
2261ac8d5baSMatthew Dillon 	 * allocated.
2271ac8d5baSMatthew Dillon 	 */
2281ac8d5baSMatthew Dillon 	error += bus_dma_tag_create(
2291ac8d5baSMatthew Dillon 			NULL,				/* parent tag */
2301ac8d5baSMatthew Dillon 			4,				/* alignment */
2311ac8d5baSMatthew Dillon 			0,				/* boundary */
2321ac8d5baSMatthew Dillon 			addr,				/* loaddr? */
2331ac8d5baSMatthew Dillon 			BUS_SPACE_MAXADDR,		/* hiaddr */
2341ac8d5baSMatthew Dillon 			4096 * 1024,			/* maxiosize */
2351ac8d5baSMatthew Dillon 			SILI_MAX_SGET,			/* maxsegs */
2361ac8d5baSMatthew Dillon 			65536,				/* maxsegsz */
2371ac8d5baSMatthew Dillon 			0,				/* flags */
2381ac8d5baSMatthew Dillon 			&sc->sc_tag_data);		/* return tag */
2391ac8d5baSMatthew Dillon 
2401ac8d5baSMatthew Dillon 	if (error) {
2411ac8d5baSMatthew Dillon 		device_printf(dev, "unable to create dma tags\n");
2421ac8d5baSMatthew Dillon 		sili_pci_detach(dev);
2431ac8d5baSMatthew Dillon 		return (ENXIO);
2441ac8d5baSMatthew Dillon 	}
2451ac8d5baSMatthew Dillon 
2461ac8d5baSMatthew Dillon 	if (sili_read(sc, SILI_REG_GCTL) & SILI_REG_GCTL_300CAP) {
2471ac8d5baSMatthew Dillon 		gen = "1 (1.5Gbps) and 2 (3Gbps)";
2481ac8d5baSMatthew Dillon 		sc->sc_flags |= SILI_F_300;
2491ac8d5baSMatthew Dillon 	} else {
2501ac8d5baSMatthew Dillon 		gen = "1 (1.5Gbps)";
2511ac8d5baSMatthew Dillon 	}
2521ac8d5baSMatthew Dillon 
2531ac8d5baSMatthew Dillon 	nports = sc->sc_ad->ad_nports;
2541ac8d5baSMatthew Dillon 	KKASSERT(nports <= SILI_MAX_PORTS);
2551ac8d5baSMatthew Dillon 
2566bed247fSSascha Wildner 	device_printf(dev, "ports=%d tags=31, gen %s, cap=NCQ,FBSS,SPM\n",
2576bed247fSSascha Wildner 	    nports, gen);
2581ac8d5baSMatthew Dillon 
2591ac8d5baSMatthew Dillon 	/*
2601ac8d5baSMatthew Dillon 	 * Allocate per-port resources
2611ac8d5baSMatthew Dillon 	 *
2621ac8d5baSMatthew Dillon 	 * All ports are attached in parallel but the CAM scan-bus
2631ac8d5baSMatthew Dillon 	 * is held up until all ports are attached so we get a deterministic
2641ac8d5baSMatthew Dillon 	 * order.
2651ac8d5baSMatthew Dillon 	 */
2661ac8d5baSMatthew Dillon 	for (i = 0; error == 0 && i < nports; i++) {
2671ac8d5baSMatthew Dillon 		error = sili_port_alloc(sc, i);
2681ac8d5baSMatthew Dillon 	}
2691ac8d5baSMatthew Dillon 
2701ac8d5baSMatthew Dillon 	/*
2711ac8d5baSMatthew Dillon 	 * Setup the interrupt vector and enable interrupts.  Note that
2721ac8d5baSMatthew Dillon 	 * since the irq may be shared we do not set it up until we are
2731ac8d5baSMatthew Dillon 	 * ready to go.
2741ac8d5baSMatthew Dillon 	 */
2751ac8d5baSMatthew Dillon 	if (error == 0) {
276fb00c6edSMatthew Dillon 		error = bus_setup_intr(dev, sc->sc_irq, INTR_MPSAFE,
277fb00c6edSMatthew Dillon 				       sili_intr, sc,
2781ac8d5baSMatthew Dillon 				       &sc->sc_irq_handle, NULL);
2791ac8d5baSMatthew Dillon 	}
2801ac8d5baSMatthew Dillon 
2811ac8d5baSMatthew Dillon 	if (error) {
2821ac8d5baSMatthew Dillon 		device_printf(dev, "unable to install interrupt\n");
2831ac8d5baSMatthew Dillon 		sili_pci_detach(dev);
2841ac8d5baSMatthew Dillon 		return (ENXIO);
2851ac8d5baSMatthew Dillon 	}
2861ac8d5baSMatthew Dillon 
2871ac8d5baSMatthew Dillon 	/*
2881ac8d5baSMatthew Dillon 	 * Interrupt subsystem is good to go now, enable all port interrupts
2891ac8d5baSMatthew Dillon 	 */
2901ac8d5baSMatthew Dillon 	crit_enter();
2911ac8d5baSMatthew Dillon 	reg = sili_read(sc, SILI_REG_GCTL);
2921ac8d5baSMatthew Dillon 	for (i = 0; i < nports; ++i)
2931ac8d5baSMatthew Dillon 		reg |= SILI_REG_GCTL_PORTEN(i);
2941ac8d5baSMatthew Dillon 	sili_write(sc, SILI_REG_GCTL, reg);
2951ac8d5baSMatthew Dillon 	sc->sc_flags |= SILI_F_INT_GOOD;
2961ac8d5baSMatthew Dillon 	crit_exit();
2971ac8d5baSMatthew Dillon 	sili_intr(sc);
2981ac8d5baSMatthew Dillon 
2991ac8d5baSMatthew Dillon 	/*
3001ac8d5baSMatthew Dillon 	 * All ports are probing in parallel.  Wait for them to finish
3011ac8d5baSMatthew Dillon 	 * and then issue the cam attachment and bus scan serially so
3021ac8d5baSMatthew Dillon 	 * the 'da' assignments are deterministic.
3031ac8d5baSMatthew Dillon 	 */
3041ac8d5baSMatthew Dillon 	for (i = 0; i < nports; i++) {
3051ac8d5baSMatthew Dillon 		if ((ap = sc->sc_ports[i]) != NULL) {
3061ac8d5baSMatthew Dillon 			while (ap->ap_signal & AP_SIGF_INIT)
3071ac8d5baSMatthew Dillon 				tsleep(&ap->ap_signal, 0, "ahprb1", hz);
3081ac8d5baSMatthew Dillon 			sili_os_lock_port(ap);
3091ac8d5baSMatthew Dillon 			if (sili_cam_attach(ap) == 0) {
3101ac8d5baSMatthew Dillon 				sili_cam_changed(ap, NULL, -1);
3111ac8d5baSMatthew Dillon 				sili_os_unlock_port(ap);
3121ac8d5baSMatthew Dillon 				while ((ap->ap_flags & AP_F_SCAN_COMPLETED) == 0) {
3131ac8d5baSMatthew Dillon 					tsleep(&ap->ap_flags, 0, "ahprb2", hz);
3141ac8d5baSMatthew Dillon 				}
3151ac8d5baSMatthew Dillon 			} else {
3161ac8d5baSMatthew Dillon 				sili_os_unlock_port(ap);
3171ac8d5baSMatthew Dillon 			}
3181ac8d5baSMatthew Dillon 		}
3191ac8d5baSMatthew Dillon 	}
3201ac8d5baSMatthew Dillon 
3211ac8d5baSMatthew Dillon 	return(0);
3221ac8d5baSMatthew Dillon }
3231ac8d5baSMatthew Dillon 
3241ac8d5baSMatthew Dillon /*
3251ac8d5baSMatthew Dillon  * Device unload / detachment
3261ac8d5baSMatthew Dillon  */
3271ac8d5baSMatthew Dillon static int
sili_pci_detach(device_t dev)3281ac8d5baSMatthew Dillon sili_pci_detach(device_t dev)
3291ac8d5baSMatthew Dillon {
3301ac8d5baSMatthew Dillon 	struct sili_softc *sc = device_get_softc(dev);
3311ac8d5baSMatthew Dillon 	struct sili_port *ap;
3321ac8d5baSMatthew Dillon 	int	i;
3331ac8d5baSMatthew Dillon 
3341ac8d5baSMatthew Dillon 	/*
3351ac8d5baSMatthew Dillon 	 * Disable the controller and de-register the interrupt, if any.
3361ac8d5baSMatthew Dillon 	 *
3371ac8d5baSMatthew Dillon 	 * XXX interlock last interrupt?
3381ac8d5baSMatthew Dillon 	 */
3391ac8d5baSMatthew Dillon 	sc->sc_flags &= ~SILI_F_INT_GOOD;
3401ac8d5baSMatthew Dillon 	if (sc->sc_regs)
3411ac8d5baSMatthew Dillon 		sili_write(sc, SILI_REG_GCTL, SILI_REG_GCTL_GRESET);
3421ac8d5baSMatthew Dillon 
3431ac8d5baSMatthew Dillon 	if (sc->sc_irq_handle) {
3441ac8d5baSMatthew Dillon 		bus_teardown_intr(dev, sc->sc_irq, sc->sc_irq_handle);
3451ac8d5baSMatthew Dillon 		sc->sc_irq_handle = NULL;
3461ac8d5baSMatthew Dillon 	}
3471ac8d5baSMatthew Dillon 
3481ac8d5baSMatthew Dillon 	/*
3491ac8d5baSMatthew Dillon 	 * Free port structures and DMA memory
3501ac8d5baSMatthew Dillon 	 */
3511ac8d5baSMatthew Dillon 	for (i = 0; i < SILI_MAX_PORTS; i++) {
3521ac8d5baSMatthew Dillon 		ap = sc->sc_ports[i];
3531ac8d5baSMatthew Dillon 		if (ap) {
3541ac8d5baSMatthew Dillon 			sili_cam_detach(ap);
3551ac8d5baSMatthew Dillon 			sili_port_free(sc, i);
3561ac8d5baSMatthew Dillon 		}
3571ac8d5baSMatthew Dillon 	}
3581ac8d5baSMatthew Dillon 
3591ac8d5baSMatthew Dillon 	/*
3601ac8d5baSMatthew Dillon 	 * Clean up the bus space
3611ac8d5baSMatthew Dillon 	 */
3621ac8d5baSMatthew Dillon 	if (sc->sc_irq) {
3631ac8d5baSMatthew Dillon 		bus_release_resource(dev, SYS_RES_IRQ,
3641ac8d5baSMatthew Dillon 				     sc->sc_rid_irq, sc->sc_irq);
3651ac8d5baSMatthew Dillon 		sc->sc_irq = NULL;
3661ac8d5baSMatthew Dillon 	}
367*c2a27f53SSepherosa Ziehau 	if (sc->sc_irq_type == PCI_INTR_TYPE_MSI)
368*c2a27f53SSepherosa Ziehau 		pci_release_msi(dev);
369*c2a27f53SSepherosa Ziehau 
3701ac8d5baSMatthew Dillon 	if (sc->sc_regs) {
3711ac8d5baSMatthew Dillon 		bus_release_resource(dev, SYS_RES_MEMORY,
3721ac8d5baSMatthew Dillon 				     sc->sc_rid_regs, sc->sc_regs);
3731ac8d5baSMatthew Dillon 		sc->sc_regs = NULL;
3741ac8d5baSMatthew Dillon 	}
3751ac8d5baSMatthew Dillon 	if (sc->sc_pregs) {
3761ac8d5baSMatthew Dillon 		bus_release_resource(dev, SYS_RES_MEMORY,
3771ac8d5baSMatthew Dillon 				     sc->sc_rid_pregs, sc->sc_pregs);
3781ac8d5baSMatthew Dillon 		sc->sc_regs = NULL;
3791ac8d5baSMatthew Dillon 	}
3801ac8d5baSMatthew Dillon 
3812102f407SMatthew Dillon 	if (sc->sc_tag_prbs) {
3822102f407SMatthew Dillon 		bus_dma_tag_destroy(sc->sc_tag_prbs);
3832102f407SMatthew Dillon 		sc->sc_tag_prbs = NULL;
3841ac8d5baSMatthew Dillon 	}
3851ac8d5baSMatthew Dillon 	if (sc->sc_tag_data) {
3861ac8d5baSMatthew Dillon 		bus_dma_tag_destroy(sc->sc_tag_data);
3871ac8d5baSMatthew Dillon 		sc->sc_tag_data = NULL;
3881ac8d5baSMatthew Dillon 	}
3891ac8d5baSMatthew Dillon 
3901ac8d5baSMatthew Dillon 	return (0);
3911ac8d5baSMatthew Dillon }
392