xref: /freebsd/sys/dev/mpr/mpr_pci.c (revision d48ddc5f9676bbf050c5f5d110d672173ae82f47)
1991554f2SKenneth D. Merry /*-
2991554f2SKenneth D. Merry  * Copyright (c) 2009 Yahoo! Inc.
3991554f2SKenneth D. Merry  * All rights reserved.
4991554f2SKenneth D. Merry  *
5991554f2SKenneth D. Merry  * Redistribution and use in source and binary forms, with or without
6991554f2SKenneth D. Merry  * modification, are permitted provided that the following conditions
7991554f2SKenneth D. Merry  * are met:
8991554f2SKenneth D. Merry  * 1. Redistributions of source code must retain the above copyright
9991554f2SKenneth D. Merry  *    notice, this list of conditions and the following disclaimer.
10991554f2SKenneth D. Merry  * 2. Redistributions in binary form must reproduce the above copyright
11991554f2SKenneth D. Merry  *    notice, this list of conditions and the following disclaimer in the
12991554f2SKenneth D. Merry  *    documentation and/or other materials provided with the distribution.
13991554f2SKenneth D. Merry  *
14991554f2SKenneth D. Merry  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15991554f2SKenneth D. Merry  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16991554f2SKenneth D. Merry  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17991554f2SKenneth D. Merry  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18991554f2SKenneth D. Merry  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19991554f2SKenneth D. Merry  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20991554f2SKenneth D. Merry  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21991554f2SKenneth D. Merry  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22991554f2SKenneth D. Merry  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23991554f2SKenneth D. Merry  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24991554f2SKenneth D. Merry  * SUCH DAMAGE.
25991554f2SKenneth D. Merry  */
26991554f2SKenneth D. Merry 
27991554f2SKenneth D. Merry #include <sys/cdefs.h>
28991554f2SKenneth D. Merry __FBSDID("$FreeBSD$");
29991554f2SKenneth D. Merry 
30a2c14879SStephen McConnell /* PCI/PCI-X/PCIe bus interface for the Avago Tech (LSI) MPT3 controllers */
31991554f2SKenneth D. Merry 
32991554f2SKenneth D. Merry /* TODO Move headers to mprvar */
33991554f2SKenneth D. Merry #include <sys/types.h>
34991554f2SKenneth D. Merry #include <sys/param.h>
35991554f2SKenneth D. Merry #include <sys/systm.h>
36991554f2SKenneth D. Merry #include <sys/kernel.h>
37991554f2SKenneth D. Merry #include <sys/module.h>
38991554f2SKenneth D. Merry #include <sys/bus.h>
39991554f2SKenneth D. Merry #include <sys/conf.h>
40991554f2SKenneth D. Merry #include <sys/malloc.h>
41991554f2SKenneth D. Merry #include <sys/sysctl.h>
42991554f2SKenneth D. Merry #include <sys/uio.h>
43991554f2SKenneth D. Merry 
44991554f2SKenneth D. Merry #include <machine/bus.h>
45991554f2SKenneth D. Merry #include <machine/resource.h>
46991554f2SKenneth D. Merry #include <sys/rman.h>
47991554f2SKenneth D. Merry 
48991554f2SKenneth D. Merry #include <dev/pci/pcireg.h>
49991554f2SKenneth D. Merry #include <dev/pci/pcivar.h>
50991554f2SKenneth D. Merry #include <dev/pci/pci_private.h>
51991554f2SKenneth D. Merry 
52991554f2SKenneth D. Merry #include <dev/mpr/mpi/mpi2_type.h>
53991554f2SKenneth D. Merry #include <dev/mpr/mpi/mpi2.h>
54991554f2SKenneth D. Merry #include <dev/mpr/mpi/mpi2_ioc.h>
55991554f2SKenneth D. Merry #include <dev/mpr/mpi/mpi2_cnfg.h>
56991554f2SKenneth D. Merry #include <dev/mpr/mpi/mpi2_tool.h>
5767feec50SStephen McConnell #include <dev/mpr/mpi/mpi2_pci.h>
58991554f2SKenneth D. Merry 
59991554f2SKenneth D. Merry #include <sys/queue.h>
60991554f2SKenneth D. Merry #include <sys/kthread.h>
61991554f2SKenneth D. Merry #include <dev/mpr/mpr_ioctl.h>
62991554f2SKenneth D. Merry #include <dev/mpr/mprvar.h>
63991554f2SKenneth D. Merry 
64991554f2SKenneth D. Merry static int	mpr_pci_probe(device_t);
65991554f2SKenneth D. Merry static int	mpr_pci_attach(device_t);
66991554f2SKenneth D. Merry static int	mpr_pci_detach(device_t);
67991554f2SKenneth D. Merry static int	mpr_pci_suspend(device_t);
68991554f2SKenneth D. Merry static int	mpr_pci_resume(device_t);
69991554f2SKenneth D. Merry static void	mpr_pci_free(struct mpr_softc *);
70991554f2SKenneth D. Merry static int	mpr_alloc_msix(struct mpr_softc *sc, int msgs);
71991554f2SKenneth D. Merry static int	mpr_alloc_msi(struct mpr_softc *sc, int msgs);
72252b2b4fSScott Long static int	mpr_pci_alloc_interrupts(struct mpr_softc *sc);
73991554f2SKenneth D. Merry 
74991554f2SKenneth D. Merry static device_method_t mpr_methods[] = {
75991554f2SKenneth D. Merry 	DEVMETHOD(device_probe,		mpr_pci_probe),
76991554f2SKenneth D. Merry 	DEVMETHOD(device_attach,	mpr_pci_attach),
77991554f2SKenneth D. Merry 	DEVMETHOD(device_detach,	mpr_pci_detach),
78991554f2SKenneth D. Merry 	DEVMETHOD(device_suspend,	mpr_pci_suspend),
79991554f2SKenneth D. Merry 	DEVMETHOD(device_resume,	mpr_pci_resume),
80991554f2SKenneth D. Merry 	DEVMETHOD(bus_print_child,	bus_generic_print_child),
81991554f2SKenneth D. Merry 	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
82991554f2SKenneth D. Merry 	{ 0, 0 }
83991554f2SKenneth D. Merry };
84991554f2SKenneth D. Merry 
85991554f2SKenneth D. Merry static driver_t mpr_pci_driver = {
86991554f2SKenneth D. Merry 	"mpr",
87991554f2SKenneth D. Merry 	mpr_methods,
88991554f2SKenneth D. Merry 	sizeof(struct mpr_softc)
89991554f2SKenneth D. Merry };
90991554f2SKenneth D. Merry 
91991554f2SKenneth D. Merry struct mpr_ident {
92991554f2SKenneth D. Merry 	uint16_t	vendor;
93991554f2SKenneth D. Merry 	uint16_t	device;
94991554f2SKenneth D. Merry 	uint16_t	subvendor;
95991554f2SKenneth D. Merry 	uint16_t	subdevice;
96991554f2SKenneth D. Merry 	u_int		flags;
97991554f2SKenneth D. Merry 	const char	*desc;
98991554f2SKenneth D. Merry } mpr_identifiers[] = {
99991554f2SKenneth D. Merry 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3004,
100a2c14879SStephen McConnell 	    0xffff, 0xffff, 0, "Avago Technologies (LSI) SAS3004" },
101991554f2SKenneth D. Merry 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3008,
102a2c14879SStephen McConnell 	    0xffff, 0xffff, 0, "Avago Technologies (LSI) SAS3008" },
103991554f2SKenneth D. Merry 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3108_1,
104a2c14879SStephen McConnell 	    0xffff, 0xffff, 0, "Avago Technologies (LSI) SAS3108_1" },
105991554f2SKenneth D. Merry 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3108_2,
106a2c14879SStephen McConnell 	    0xffff, 0xffff, 0, "Avago Technologies (LSI) SAS3108_2" },
107991554f2SKenneth D. Merry 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3108_5,
108a2c14879SStephen McConnell 	    0xffff, 0xffff, 0, "Avago Technologies (LSI) SAS3108_5" },
109991554f2SKenneth D. Merry 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3108_6,
110a2c14879SStephen McConnell 	    0xffff, 0xffff, 0, "Avago Technologies (LSI) SAS3108_6" },
11167feec50SStephen McConnell 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3216,
11267feec50SStephen McConnell 	    0xffff, 0xffff, 0, "Avago Technologies (LSI) SAS3216" },
11367feec50SStephen McConnell 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3224,
11467feec50SStephen McConnell 	    0xffff, 0xffff, 0, "Avago Technologies (LSI) SAS3224" },
1152bbc5fcbSStephen McConnell 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3316_1,
1162bbc5fcbSStephen McConnell 	    0xffff, 0xffff, 0, "Avago Technologies (LSI) SAS3316_1" },
1172bbc5fcbSStephen McConnell 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3316_2,
1182bbc5fcbSStephen McConnell 	    0xffff, 0xffff, 0, "Avago Technologies (LSI) SAS3316_2" },
1192bbc5fcbSStephen McConnell 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3324_1,
1202bbc5fcbSStephen McConnell 	    0xffff, 0xffff, 0, "Avago Technologies (LSI) SAS3324_1" },
1212bbc5fcbSStephen McConnell 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3324_2,
1222bbc5fcbSStephen McConnell 	    0xffff, 0xffff, 0, "Avago Technologies (LSI) SAS3324_2" },
12367feec50SStephen McConnell 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3408,
124b618318aSScott Long 	    0xffff, 0xffff, MPR_FLAGS_GEN35_IOC,
125b618318aSScott Long 	    "Avago Technologies (LSI) SAS3408" },
12667feec50SStephen McConnell 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3416,
127b618318aSScott Long 	    0xffff, 0xffff, MPR_FLAGS_GEN35_IOC,
128b618318aSScott Long 	    "Avago Technologies (LSI) SAS3416" },
12967feec50SStephen McConnell 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3508,
130b618318aSScott Long 	    0xffff, 0xffff, MPR_FLAGS_GEN35_IOC,
131b618318aSScott Long 	    "Avago Technologies (LSI) SAS3508" },
13267feec50SStephen McConnell 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3508_1,
133b618318aSScott Long 	    0xffff, 0xffff, MPR_FLAGS_GEN35_IOC,
134b618318aSScott Long 	    "Avago Technologies (LSI) SAS3508_1" },
13567feec50SStephen McConnell 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3516,
136b618318aSScott Long 	    0xffff, 0xffff, MPR_FLAGS_GEN35_IOC,
137b618318aSScott Long 	    "Avago Technologies (LSI) SAS3516" },
13867feec50SStephen McConnell 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3516_1,
139b618318aSScott Long 	    0xffff, 0xffff, MPR_FLAGS_GEN35_IOC,
140b618318aSScott Long 	    "Avago Technologies (LSI) SAS3516_1" },
14167feec50SStephen McConnell 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3616,
142b618318aSScott Long 	    0xffff, 0xffff, MPR_FLAGS_GEN35_IOC,
143b618318aSScott Long 	    "Avago Technologies (LSI) SAS3616" },
14467feec50SStephen McConnell 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3708,
145b618318aSScott Long 	    0xffff, 0xffff, MPR_FLAGS_GEN35_IOC,
146b618318aSScott Long 	    "Avago Technologies (LSI) SAS3708" },
14767feec50SStephen McConnell 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3716,
148b618318aSScott Long 	    0xffff, 0xffff, MPR_FLAGS_GEN35_IOC,
149b618318aSScott Long 	    "Avago Technologies (LSI) SAS3716" },
150f36649b7SKashyap D Desai 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_INVALID0_SAS3816,
151f36649b7SKashyap D Desai 	    0xffff, 0xffff, (MPR_FLAGS_GEN35_IOC | MPR_FLAGS_SEA_IOC),
152f36649b7SKashyap D Desai 	    "Broadcom Inc. (LSI) INVALID0 SAS3816" },
153f36649b7SKashyap D Desai 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_CFG_SEC_SAS3816,
154f36649b7SKashyap D Desai 	    0xffff, 0xffff, (MPR_FLAGS_GEN35_IOC | MPR_FLAGS_SEA_IOC),
155f36649b7SKashyap D Desai 	    "Broadcom Inc. (LSI) CFG SEC SAS3816" },
156f36649b7SKashyap D Desai 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_HARD_SEC_SAS3816,
157f36649b7SKashyap D Desai 	    0xffff, 0xffff, (MPR_FLAGS_GEN35_IOC | MPR_FLAGS_SEA_IOC),
158f36649b7SKashyap D Desai 	    "Broadcom Inc. (LSI) HARD SEC SAS3816" },
159f36649b7SKashyap D Desai 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_INVALID1_SAS3816,
160f36649b7SKashyap D Desai 	    0xffff, 0xffff, (MPR_FLAGS_GEN35_IOC | MPR_FLAGS_SEA_IOC),
161f36649b7SKashyap D Desai 	    "Broadcom Inc. (LSI) INVALID1 SAS3816" },
162f36649b7SKashyap D Desai 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_INVALID0_SAS3916,
163f36649b7SKashyap D Desai 	    0xffff, 0xffff, (MPR_FLAGS_GEN35_IOC | MPR_FLAGS_SEA_IOC),
164f36649b7SKashyap D Desai 	    "Broadcom Inc. (LSI) INVALID0 SAS3916" },
165f36649b7SKashyap D Desai 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_CFG_SEC_SAS3916,
166f36649b7SKashyap D Desai 	    0xffff, 0xffff, (MPR_FLAGS_GEN35_IOC | MPR_FLAGS_SEA_IOC),
167f36649b7SKashyap D Desai 	    "Broadcom Inc. (LSI) CFG SEC SAS3916" },
168f36649b7SKashyap D Desai 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_HARD_SEC_SAS3916,
169f36649b7SKashyap D Desai 	    0xffff, 0xffff, (MPR_FLAGS_GEN35_IOC | MPR_FLAGS_SEA_IOC),
170f36649b7SKashyap D Desai 	    "Broadcom Inc. (LSI) HARD SEC SAS3916" },
171f36649b7SKashyap D Desai 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_INVALID1_SAS3916,
172f36649b7SKashyap D Desai 	    0xffff, 0xffff, (MPR_FLAGS_GEN35_IOC | MPR_FLAGS_SEA_IOC),
173f36649b7SKashyap D Desai 	    "Broadcom Inc. (LSI) INVALID1 SAS3916" },
174991554f2SKenneth D. Merry 	{ 0, 0, 0, 0, 0, NULL }
175991554f2SKenneth D. Merry };
176991554f2SKenneth D. Merry 
177*d48ddc5fSJohn Baldwin DRIVER_MODULE(mpr, pci, mpr_pci_driver, 0, 0);
1780dc34160SWarner Losh MODULE_PNP_INFO("U16:vendor;U16:device;U16:subvendor;U16:subdevice;D:#", pci,
1790dc34160SWarner Losh     mpr, mpr_identifiers, nitems(mpr_identifiers) - 1);
1800dc34160SWarner Losh 
1810dc34160SWarner Losh MODULE_DEPEND(mpr, cam, 1, 1, 1);
1820dc34160SWarner Losh 
183991554f2SKenneth D. Merry static struct mpr_ident *
184991554f2SKenneth D. Merry mpr_find_ident(device_t dev)
185991554f2SKenneth D. Merry {
186991554f2SKenneth D. Merry 	struct mpr_ident *m;
187991554f2SKenneth D. Merry 
188991554f2SKenneth D. Merry 	for (m = mpr_identifiers; m->vendor != 0; m++) {
189991554f2SKenneth D. Merry 		if (m->vendor != pci_get_vendor(dev))
190991554f2SKenneth D. Merry 			continue;
191991554f2SKenneth D. Merry 		if (m->device != pci_get_device(dev))
192991554f2SKenneth D. Merry 			continue;
193991554f2SKenneth D. Merry 		if ((m->subvendor != 0xffff) &&
194991554f2SKenneth D. Merry 		    (m->subvendor != pci_get_subvendor(dev)))
195991554f2SKenneth D. Merry 			continue;
196991554f2SKenneth D. Merry 		if ((m->subdevice != 0xffff) &&
197991554f2SKenneth D. Merry 		    (m->subdevice != pci_get_subdevice(dev)))
198991554f2SKenneth D. Merry 			continue;
199991554f2SKenneth D. Merry 		return (m);
200991554f2SKenneth D. Merry 	}
201991554f2SKenneth D. Merry 
202991554f2SKenneth D. Merry 	return (NULL);
203991554f2SKenneth D. Merry }
204991554f2SKenneth D. Merry 
205991554f2SKenneth D. Merry static int
206991554f2SKenneth D. Merry mpr_pci_probe(device_t dev)
207991554f2SKenneth D. Merry {
208991554f2SKenneth D. Merry 	struct mpr_ident *id;
209991554f2SKenneth D. Merry 
210991554f2SKenneth D. Merry 	if ((id = mpr_find_ident(dev)) != NULL) {
211991554f2SKenneth D. Merry 		device_set_desc(dev, id->desc);
212991554f2SKenneth D. Merry 		return (BUS_PROBE_DEFAULT);
213991554f2SKenneth D. Merry 	}
214991554f2SKenneth D. Merry 	return (ENXIO);
215991554f2SKenneth D. Merry }
216991554f2SKenneth D. Merry 
217991554f2SKenneth D. Merry static int
218991554f2SKenneth D. Merry mpr_pci_attach(device_t dev)
219991554f2SKenneth D. Merry {
22074c781edSScott Long 	bus_dma_template_t t;
221991554f2SKenneth D. Merry 	struct mpr_softc *sc;
222991554f2SKenneth D. Merry 	struct mpr_ident *m;
22367feec50SStephen McConnell 	int error, i;
224991554f2SKenneth D. Merry 
225991554f2SKenneth D. Merry 	sc = device_get_softc(dev);
226991554f2SKenneth D. Merry 	bzero(sc, sizeof(*sc));
227991554f2SKenneth D. Merry 	sc->mpr_dev = dev;
228991554f2SKenneth D. Merry 	m = mpr_find_ident(dev);
229991554f2SKenneth D. Merry 	sc->mpr_flags = m->flags;
230991554f2SKenneth D. Merry 
231f36649b7SKashyap D Desai 	switch (m->device) {
232f36649b7SKashyap D Desai 	case MPI26_MFGPAGE_DEVID_INVALID0_SAS3816:
233f36649b7SKashyap D Desai 	case MPI26_MFGPAGE_DEVID_INVALID1_SAS3816:
234f36649b7SKashyap D Desai 	case MPI26_MFGPAGE_DEVID_INVALID0_SAS3916:
235f36649b7SKashyap D Desai 	case MPI26_MFGPAGE_DEVID_INVALID1_SAS3916:
236f36649b7SKashyap D Desai 		mpr_printf(sc, "HBA is in Non Secure mode\n");
237f36649b7SKashyap D Desai 		return (ENXIO);
238f36649b7SKashyap D Desai 	case MPI26_MFGPAGE_DEVID_CFG_SEC_SAS3816:
239f36649b7SKashyap D Desai 	case MPI26_MFGPAGE_DEVID_CFG_SEC_SAS3916:
240f36649b7SKashyap D Desai 		mpr_printf(sc, "HBA is in Configurable Secure mode\n");
241f36649b7SKashyap D Desai 		break;
242f36649b7SKashyap D Desai 	default:
243f36649b7SKashyap D Desai 		break;
244f36649b7SKashyap D Desai 	}
245f36649b7SKashyap D Desai 
246252b2b4fSScott Long 	mpr_get_tunables(sc);
247252b2b4fSScott Long 
248991554f2SKenneth D. Merry 	/* Twiddle basic PCI config bits for a sanity check */
249991554f2SKenneth D. Merry 	pci_enable_busmaster(dev);
250991554f2SKenneth D. Merry 
25167feec50SStephen McConnell 	for (i = 0; i < PCI_MAXMAPS_0; i++) {
25267feec50SStephen McConnell 		sc->mpr_regs_rid = PCIR_BAR(i);
25367feec50SStephen McConnell 
254991554f2SKenneth D. Merry 		if ((sc->mpr_regs_resource = bus_alloc_resource_any(dev,
25567feec50SStephen McConnell 		    SYS_RES_MEMORY, &sc->mpr_regs_rid, RF_ACTIVE)) != NULL)
25667feec50SStephen McConnell 			break;
25767feec50SStephen McConnell 	}
25867feec50SStephen McConnell 
25967feec50SStephen McConnell 	if (sc->mpr_regs_resource == NULL) {
260991554f2SKenneth D. Merry 		mpr_printf(sc, "Cannot allocate PCI registers\n");
261991554f2SKenneth D. Merry 		return (ENXIO);
262991554f2SKenneth D. Merry 	}
26367feec50SStephen McConnell 
264991554f2SKenneth D. Merry 	sc->mpr_btag = rman_get_bustag(sc->mpr_regs_resource);
265991554f2SKenneth D. Merry 	sc->mpr_bhandle = rman_get_bushandle(sc->mpr_regs_resource);
266991554f2SKenneth D. Merry 
267991554f2SKenneth D. Merry 	/* Allocate the parent DMA tag */
268f5ead205SScott Long 	bus_dma_template_init(&t, bus_get_dma_tag(dev));
269f5ead205SScott Long 	if (bus_dma_template_tag(&t, &sc->mpr_parent_dmat)) {
270991554f2SKenneth D. Merry 		mpr_printf(sc, "Cannot allocate parent DMA tag\n");
271991554f2SKenneth D. Merry 		mpr_pci_free(sc);
272991554f2SKenneth D. Merry 		return (ENOMEM);
273991554f2SKenneth D. Merry 	}
274991554f2SKenneth D. Merry 
275252b2b4fSScott Long 	if (((error = mpr_pci_alloc_interrupts(sc)) != 0) ||
276252b2b4fSScott Long 	    ((error = mpr_attach(sc)) != 0))
277991554f2SKenneth D. Merry 		mpr_pci_free(sc);
278991554f2SKenneth D. Merry 
279991554f2SKenneth D. Merry 	return (error);
280991554f2SKenneth D. Merry }
281991554f2SKenneth D. Merry 
282252b2b4fSScott Long /*
283252b2b4fSScott Long  * Allocate, but don't assign interrupts early.  Doing it before requesting
284252b2b4fSScott Long  * the IOCFacts message informs the firmware that we want to do MSI-X
285252b2b4fSScott Long  * multiqueue.  We might not use all of the available messages, but there's
286252b2b4fSScott Long  * no reason to re-alloc if we don't.
287252b2b4fSScott Long  */
288252b2b4fSScott Long int
289252b2b4fSScott Long mpr_pci_alloc_interrupts(struct mpr_softc *sc)
290252b2b4fSScott Long {
291252b2b4fSScott Long 	device_t dev;
292252b2b4fSScott Long 	int error, msgs;
293252b2b4fSScott Long 
294252b2b4fSScott Long 	dev = sc->mpr_dev;
295252b2b4fSScott Long 	error = 0;
2962068b2aaSScott Long 	msgs = 0;
297252b2b4fSScott Long 
2983c5ac992SScott Long 	if (sc->disable_msix == 0) {
2993c5ac992SScott Long 		msgs = pci_msix_count(dev);
3003c5ac992SScott Long 		mpr_dprint(sc, MPR_INIT, "Counted %d MSI-X messages\n", msgs);
3013c5ac992SScott Long 		msgs = min(msgs, sc->max_msix);
3023c5ac992SScott Long 		msgs = min(msgs, MPR_MSIX_MAX);
3033c5ac992SScott Long 		msgs = min(msgs, 1);	/* XXX */
3043c5ac992SScott Long 		if (msgs != 0) {
3057eed4c18SScott Long 			mpr_dprint(sc, MPR_INIT, "Attempting to allocate %d "
3067eed4c18SScott Long 			    "MSI-X messages\n", msgs);
3073c5ac992SScott Long 			error = mpr_alloc_msix(sc, msgs);
3083c5ac992SScott Long 		}
3093c5ac992SScott Long 	}
3103c5ac992SScott Long 	if (((error != 0) || (msgs == 0)) && (sc->disable_msi == 0)) {
3113c5ac992SScott Long 		msgs = pci_msi_count(dev);
3123c5ac992SScott Long 		mpr_dprint(sc, MPR_INIT, "Counted %d MSI messages\n", msgs);
3133c5ac992SScott Long 		msgs = min(msgs, MPR_MSI_MAX);
3143c5ac992SScott Long 		if (msgs != 0) {
3157eed4c18SScott Long 			mpr_dprint(sc, MPR_INIT, "Attempting to allocated %d "
3167eed4c18SScott Long 			    "MSI messages\n", MPR_MSI_MAX);
3173c5ac992SScott Long 			error = mpr_alloc_msi(sc, MPR_MSI_MAX);
3183c5ac992SScott Long 		}
3193c5ac992SScott Long 	}
3203c5ac992SScott Long 	if ((error != 0) || (msgs == 0)) {
3213d96cd78SScott Long 		/*
3223d96cd78SScott Long 		 * If neither MSI or MSI-X are available, assume legacy INTx.
3233d96cd78SScott Long 		 * This also implies that there will be only 1 queue.
3243d96cd78SScott Long 		 */
3253c5ac992SScott Long 		mpr_dprint(sc, MPR_INIT, "Falling back to legacy INTx\n");
3263d96cd78SScott Long 		sc->mpr_flags |= MPR_FLAGS_INTX;
3273d96cd78SScott Long 		msgs = 1;
3283c5ac992SScott Long 	} else
3293d96cd78SScott Long 		sc->mpr_flags |= MPR_FLAGS_MSI;
330252b2b4fSScott Long 
331252b2b4fSScott Long 	sc->msi_msgs = msgs;
3323d96cd78SScott Long 	mpr_dprint(sc, MPR_INIT, "Allocated %d interrupts\n", msgs);
3333d96cd78SScott Long 
334252b2b4fSScott Long 	return (error);
335252b2b4fSScott Long }
336252b2b4fSScott Long 
337991554f2SKenneth D. Merry int
338991554f2SKenneth D. Merry mpr_pci_setup_interrupts(struct mpr_softc *sc)
339991554f2SKenneth D. Merry {
340991554f2SKenneth D. Merry 	device_t dev;
341bec09074SScott Long 	struct mpr_queue *q;
3423d96cd78SScott Long 	void *ihandler;
3433d96cd78SScott Long 	int i, error, rid, initial_rid;
344991554f2SKenneth D. Merry 
345991554f2SKenneth D. Merry 	dev = sc->mpr_dev;
346991554f2SKenneth D. Merry 	error = ENXIO;
347991554f2SKenneth D. Merry 
3483d96cd78SScott Long 	if (sc->mpr_flags & MPR_FLAGS_INTX) {
3493d96cd78SScott Long 		initial_rid = 0;
3503d96cd78SScott Long 		ihandler = mpr_intr;
3513d96cd78SScott Long 	} else if (sc->mpr_flags & MPR_FLAGS_MSI) {
3523d96cd78SScott Long 		initial_rid = 1;
3533d96cd78SScott Long 		ihandler = mpr_intr_msi;
354991554f2SKenneth D. Merry 	} else {
3553d96cd78SScott Long 		mpr_dprint(sc, MPR_ERROR|MPR_INIT,
3563d96cd78SScott Long 		    "Unable to set up interrupts\n");
3573d96cd78SScott Long 		return (EINVAL);
3583d96cd78SScott Long 	}
3593d96cd78SScott Long 
3603d96cd78SScott Long 	for (i = 0; i < sc->msi_msgs; i++) {
361bec09074SScott Long 		q = &sc->queues[i];
3623d96cd78SScott Long 		rid = i + initial_rid;
363bec09074SScott Long 		q->irq_rid = rid;
364bec09074SScott Long 		q->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
365bec09074SScott Long 		    &q->irq_rid, RF_ACTIVE);
366bec09074SScott Long 		if (q->irq == NULL) {
3673d96cd78SScott Long 			mpr_dprint(sc, MPR_ERROR|MPR_INIT,
3683d96cd78SScott Long 			    "Cannot allocate interrupt RID %d\n", rid);
3693c5ac992SScott Long 			sc->msi_msgs = i;
3703d96cd78SScott Long 			break;
371991554f2SKenneth D. Merry 		}
372bec09074SScott Long 		error = bus_setup_intr(dev, q->irq,
373bec09074SScott Long 		    INTR_TYPE_BIO | INTR_MPSAFE, NULL, ihandler,
374bec09074SScott Long 		    sc, &q->intrhand);
375991554f2SKenneth D. Merry 		if (error) {
3763d96cd78SScott Long 			mpr_dprint(sc, MPR_ERROR|MPR_INIT,
3773d96cd78SScott Long 			    "Cannot setup interrupt RID %d\n", rid);
3783c5ac992SScott Long 			sc->msi_msgs = i;
379991554f2SKenneth D. Merry 			break;
380991554f2SKenneth D. Merry 		}
381991554f2SKenneth D. Merry 	}
382991554f2SKenneth D. Merry 
3833d96cd78SScott Long         mpr_dprint(sc, MPR_INIT, "Set up %d interrupts\n", sc->msi_msgs);
384991554f2SKenneth D. Merry 	return (error);
385991554f2SKenneth D. Merry }
386991554f2SKenneth D. Merry 
387991554f2SKenneth D. Merry static int
388991554f2SKenneth D. Merry mpr_pci_detach(device_t dev)
389991554f2SKenneth D. Merry {
390991554f2SKenneth D. Merry 	struct mpr_softc *sc;
391991554f2SKenneth D. Merry 	int error;
392991554f2SKenneth D. Merry 
393991554f2SKenneth D. Merry 	sc = device_get_softc(dev);
394991554f2SKenneth D. Merry 
395991554f2SKenneth D. Merry 	if ((error = mpr_free(sc)) != 0)
396991554f2SKenneth D. Merry 		return (error);
397991554f2SKenneth D. Merry 
398991554f2SKenneth D. Merry 	mpr_pci_free(sc);
399991554f2SKenneth D. Merry 	return (0);
400991554f2SKenneth D. Merry }
401991554f2SKenneth D. Merry 
402bec09074SScott Long void
403bec09074SScott Long mpr_pci_free_interrupts(struct mpr_softc *sc)
404bec09074SScott Long {
405bec09074SScott Long 	struct mpr_queue *q;
406bec09074SScott Long 	int i;
407bec09074SScott Long 
408bec09074SScott Long 	if (sc->queues == NULL)
409bec09074SScott Long 		return;
410bec09074SScott Long 
411bec09074SScott Long 	for (i = 0; i < sc->msi_msgs; i++) {
412bec09074SScott Long 		q = &sc->queues[i];
413bec09074SScott Long 		if (q->irq != NULL) {
414bec09074SScott Long 			bus_teardown_intr(sc->mpr_dev, q->irq,
415bec09074SScott Long 			    q->intrhand);
416bec09074SScott Long 			bus_release_resource(sc->mpr_dev, SYS_RES_IRQ,
417bec09074SScott Long 			    q->irq_rid, q->irq);
418bec09074SScott Long 		}
419bec09074SScott Long 	}
420bec09074SScott Long }
421bec09074SScott Long 
422991554f2SKenneth D. Merry static void
423991554f2SKenneth D. Merry mpr_pci_free(struct mpr_softc *sc)
424991554f2SKenneth D. Merry {
425991554f2SKenneth D. Merry 
426991554f2SKenneth D. Merry 	if (sc->mpr_parent_dmat != NULL) {
427991554f2SKenneth D. Merry 		bus_dma_tag_destroy(sc->mpr_parent_dmat);
428991554f2SKenneth D. Merry 	}
429991554f2SKenneth D. Merry 
430bec09074SScott Long 	mpr_pci_free_interrupts(sc);
431991554f2SKenneth D. Merry 
4323d96cd78SScott Long 	if (sc->mpr_flags & MPR_FLAGS_MSI)
4333d96cd78SScott Long 		pci_release_msi(sc->mpr_dev);
434991554f2SKenneth D. Merry 
435991554f2SKenneth D. Merry 	if (sc->mpr_regs_resource != NULL) {
436991554f2SKenneth D. Merry 		bus_release_resource(sc->mpr_dev, SYS_RES_MEMORY,
437991554f2SKenneth D. Merry 		    sc->mpr_regs_rid, sc->mpr_regs_resource);
438991554f2SKenneth D. Merry 	}
439991554f2SKenneth D. Merry 
440991554f2SKenneth D. Merry 	return;
441991554f2SKenneth D. Merry }
442991554f2SKenneth D. Merry 
443991554f2SKenneth D. Merry static int
444991554f2SKenneth D. Merry mpr_pci_suspend(device_t dev)
445991554f2SKenneth D. Merry {
446991554f2SKenneth D. Merry 	return (EINVAL);
447991554f2SKenneth D. Merry }
448991554f2SKenneth D. Merry 
449991554f2SKenneth D. Merry static int
450991554f2SKenneth D. Merry mpr_pci_resume(device_t dev)
451991554f2SKenneth D. Merry {
452991554f2SKenneth D. Merry 	return (EINVAL);
453991554f2SKenneth D. Merry }
454991554f2SKenneth D. Merry 
455991554f2SKenneth D. Merry static int
456991554f2SKenneth D. Merry mpr_alloc_msix(struct mpr_softc *sc, int msgs)
457991554f2SKenneth D. Merry {
458991554f2SKenneth D. Merry 	int error;
459991554f2SKenneth D. Merry 
460991554f2SKenneth D. Merry 	error = pci_alloc_msix(sc->mpr_dev, &msgs);
461991554f2SKenneth D. Merry 	return (error);
462991554f2SKenneth D. Merry }
463991554f2SKenneth D. Merry 
464991554f2SKenneth D. Merry static int
465991554f2SKenneth D. Merry mpr_alloc_msi(struct mpr_softc *sc, int msgs)
466991554f2SKenneth D. Merry {
467991554f2SKenneth D. Merry 	int error;
468991554f2SKenneth D. Merry 
469991554f2SKenneth D. Merry 	error = pci_alloc_msi(sc->mpr_dev, &msgs);
470991554f2SKenneth D. Merry 	return (error);
471991554f2SKenneth D. Merry }
472991554f2SKenneth D. Merry 
473991554f2SKenneth D. Merry int
474991554f2SKenneth D. Merry mpr_pci_restore(struct mpr_softc *sc)
475991554f2SKenneth D. Merry {
476991554f2SKenneth D. Merry 	struct pci_devinfo *dinfo;
477991554f2SKenneth D. Merry 
478991554f2SKenneth D. Merry 	mpr_dprint(sc, MPR_TRACE, "%s\n", __func__);
479991554f2SKenneth D. Merry 
480991554f2SKenneth D. Merry 	dinfo = device_get_ivars(sc->mpr_dev);
481991554f2SKenneth D. Merry 	if (dinfo == NULL) {
482991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_FAULT, "%s: NULL dinfo\n", __func__);
483991554f2SKenneth D. Merry 		return (EINVAL);
484991554f2SKenneth D. Merry 	}
485991554f2SKenneth D. Merry 
486991554f2SKenneth D. Merry 	pci_cfg_restore(sc->mpr_dev, dinfo);
487991554f2SKenneth D. Merry 	return (0);
488991554f2SKenneth D. Merry }
489