xref: /freebsd/sys/dev/bwn/if_bwn_pci.c (revision 8ef24a0d4b28fe230e20637f56869cc4148cd2ca)
1 /*-
2  * Copyright (c) 2015 Landon Fuller <landon@landonf.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer,
10  *    without modification.
11  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
13  *    redistribution must be conditioned upon including a substantially
14  *    similar Disclaimer requirement for further binary redistribution.
15  *
16  * NO WARRANTY
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
20  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
22  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27  * THE POSSIBILITY OF SUCH DAMAGES.
28  */
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 #include "opt_bwn.h"
34 #include "opt_wlan.h"
35 
36 #include <sys/param.h>
37 #include <sys/kernel.h>
38 #include <sys/bus.h>
39 #include <sys/module.h>
40 
41 #include <dev/pci/pcireg.h>
42 #include <dev/pci/pcivar.h>
43 
44 #include <dev/bhnd/bhndb/bhndb_pcivar.h>
45 #include <dev/bhnd/bhndb/bhndb_pci_hwdata.h>
46 
47 #include <dev/bhnd/bhnd_ids.h>
48 
49 #include "bhndb_bus_if.h"
50 
51 #include "if_bwn_pcivar.h"
52 
53 /* If non-zero, enable attachment of BWN_QUIRK_UNTESTED devices */
54 static int attach_untested = 0;
55 TUNABLE_INT("hw.bwn_pci.attach_untested", &attach_untested);
56 
57 /* If non-zero, probe at a higher priority than the stable if_bwn driver. */
58 static int prefer_new_driver = 0;
59 TUNABLE_INT("hw.bwn_pci.preferred", &prefer_new_driver);
60 
61 /* SIBA Devices */
62 static const struct bwn_pci_device siba_devices[] = {
63 	BWN_BCM_DEV(BCM4301,		"BCM4301 802.11b",
64 	    BWN_QUIRK_ENET_HW_UNPOPULATED),
65 
66 	BWN_BCM_DEV(BCM4306,		"BCM4306 802.11b/g",		0),
67 	BWN_BCM_DEV(BCM4306_D11G,	"BCM4306 802.11g",		0),
68 	BWN_BCM_DEV(BCM4306_D11A,	"BCM4306 802.11a",
69 	    BWN_QUIRK_WLAN_DUALCORE),
70 	BWN_BCM_DEV(BCM4306_D11DUAL,	"BCM4306 802.11a/b",
71 	    BWN_QUIRK_WLAN_DUALCORE),
72 	BWN_BCM_DEV(BCM4306_D11G_ID2,	"BCM4306 802.11g",		0),
73 
74 	BWN_BCM_DEV(BCM4307,		"BCM4307 802.11b",		0),
75 
76 	BWN_BCM_DEV(BCM4311_D11G,	"BCM4311 802.11b/g",		0),
77 	BWN_BCM_DEV(BCM4311_D11DUAL,	"BCM4311 802.11a/b/g",		0),
78 	BWN_BCM_DEV(BCM4311_D11A,	"BCM4311 802.11a",
79 	    BWN_QUIRK_UNTESTED|BWN_QUIRK_WLAN_DUALCORE),
80 
81 	BWN_BCM_DEV(BCM4318_D11G,	"BCM4318 802.11b/g",		0),
82 	BWN_BCM_DEV(BCM4318_D11DUAL,	"BCM4318 802.11a/b/g",		0),
83 	BWN_BCM_DEV(BCM4318_D11A,	"BCM4318 802.11a",
84 	    BWN_QUIRK_UNTESTED|BWN_QUIRK_WLAN_DUALCORE),
85 
86 	BWN_BCM_DEV(BCM4321_D11N,	"BCM4321 802.11n Dual-Band",	0),
87 	BWN_BCM_DEV(BCM4321_D11N2G,	"BCM4321 802.11n 2GHz",		0),
88 	BWN_BCM_DEV(BCM4321_D11N2G,	"BCM4321 802.11n 5GHz",
89 	    BWN_QUIRK_UNTESTED),
90 
91 	BWN_BCM_DEV(BCM4322_D11N,	"BCM4322 802.11n Dual-Band",	0),
92 	BWN_BCM_DEV(BCM4322_D11N2G,	"BCM4322 802.11n 2GHz",
93 	    BWN_QUIRK_UNTESTED),
94 	BWN_BCM_DEV(BCM4322_D11N5G,	"BCM4322 802.11n 5GHz",
95 	    BWN_QUIRK_UNTESTED),
96 
97 	BWN_BCM_DEV(BCM4328_D11G,	"BCM4328/4312 802.11g",		0),
98 
99 	{ 0, 0, NULL, 0 }
100 };
101 
102 /** BCMA Devices */
103 static const struct bwn_pci_device bcma_devices[] = {
104 	BWN_BCM_DEV(BCM4331_D11N,	"BCM4331 802.11n Dual-Band",	0),
105 	BWN_BCM_DEV(BCM4331_D11N2G,	"BCM4331 802.11n 2GHz",		0),
106 	BWN_BCM_DEV(BCM4331_D11N5G,	"BCM4331 802.11n 5GHz",		0),
107 
108 	{ 0, 0, NULL, 0}
109 };
110 
111 /** Device configuration table */
112 static const struct bwn_pci_devcfg bwn_pci_devcfgs[] = {
113 	/* SIBA devices */
114 	{
115 		.bridge_hwcfg	= &bhndb_pci_siba_generic_hwcfg,
116 		.bridge_hwtable	= bhndb_pci_generic_hw_table,
117 		.devices	= siba_devices
118 	},
119 	/* BCMA devices */
120 	{
121 		.bridge_hwcfg	= &bhndb_pci_bcma_generic_hwcfg,
122 		.bridge_hwtable	= bhndb_pci_generic_hw_table,
123 		.devices	= bcma_devices
124 	},
125 	{ NULL, NULL, NULL }
126 };
127 
128 /** Search the device configuration table for an entry matching @p dev. */
129 static int
130 bwn_pci_find_devcfg(device_t dev, const struct bwn_pci_devcfg **cfg,
131     const struct bwn_pci_device **device)
132 {
133 	const struct bwn_pci_devcfg	*dvc;
134 	const struct bwn_pci_device	*dv;
135 
136 	for (dvc = bwn_pci_devcfgs; dvc->devices != NULL; dvc++) {
137 		for (dv = dvc->devices; dv->device != 0; dv++) {
138 			if (pci_get_vendor(dev) == dv->vendor &&
139 			    pci_get_device(dev) == dv->device)
140 			{
141 				if (cfg != NULL)
142 					*cfg = dvc;
143 
144 				if (device != NULL)
145 					*device = dv;
146 
147 				return (0);
148 			}
149 		}
150 	}
151 
152 	return (ENOENT);
153 }
154 
155 static int
156 bwn_pci_probe(device_t dev)
157 {
158 	const struct bwn_pci_device	*ident;
159 
160 	if (bwn_pci_find_devcfg(dev, NULL, &ident))
161 		return (ENXIO);
162 
163 	/* Skip untested devices */
164 	if (ident->quirks & BWN_QUIRK_UNTESTED && !attach_untested)
165 		return (ENXIO);
166 
167 	device_set_desc(dev, ident->desc);
168 
169 	/* Until this driver is complete, require explicit opt-in before
170 	 * superceding if_bwn/siba_bwn. */
171 	if (prefer_new_driver)
172 		return (BUS_PROBE_DEFAULT+1);
173 	else
174 		return (BUS_PROBE_LOW_PRIORITY);
175 
176 	// return (BUS_PROBE_DEFAULT);
177 }
178 
179 static int
180 bwn_pci_attach(device_t dev)
181 {
182 	struct bwn_pci_softc		*sc;
183 	const struct bwn_pci_device	*ident;
184 	int				 error;
185 
186 	sc = device_get_softc(dev);
187 	sc->dev = dev;
188 
189 	/* Find our hardware config */
190 	if (bwn_pci_find_devcfg(dev, &sc->devcfg, &ident))
191 		return (ENXIO);
192 
193 	/* Save quirk flags */
194 	sc->quirks = ident->quirks;
195 
196 	/* Attach bridge device */
197 	if ((error = bhndb_attach_bridge(dev, &sc->bhndb_dev, -1)))
198 		return (ENXIO);
199 
200 	/* Success */
201 	return (0);
202 }
203 
204 static int
205 bwn_pci_detach(device_t dev)
206 {
207 	return (bus_generic_detach(dev));
208 }
209 
210 static void
211 bwn_pci_probe_nomatch(device_t dev, device_t child)
212 {
213 	const char *name;
214 
215 	name = device_get_name(child);
216 	if (name == NULL)
217 		name = "unknown device";
218 
219 	device_printf(dev, "<%s> (no driver attached)\n", name);
220 }
221 
222 static const struct bhndb_hwcfg *
223 bwn_pci_get_generic_hwcfg(device_t dev, device_t child)
224 {
225 	struct bwn_pci_softc *sc = device_get_softc(dev);
226 	return (sc->devcfg->bridge_hwcfg);
227 }
228 
229 static const struct bhndb_hw *
230 bwn_pci_get_bhndb_hwtable(device_t dev, device_t child)
231 {
232 	struct bwn_pci_softc *sc = device_get_softc(dev);
233 	return (sc->devcfg->bridge_hwtable);
234 }
235 
236 static bool
237 bwn_pci_is_core_disabled(device_t dev, device_t child,
238     struct bhnd_core_info *core)
239 {
240 	struct bwn_pci_softc	*sc;
241 
242 	sc = device_get_softc(dev);
243 
244 	switch (bhnd_core_class(core)) {
245 	case BHND_DEVCLASS_WLAN:
246 		if (core->unit > 0 && !(sc->quirks & BWN_QUIRK_WLAN_DUALCORE))
247 			return (true);
248 
249 		return (false);
250 
251 	case BHND_DEVCLASS_ENET:
252 	case BHND_DEVCLASS_ENET_MAC:
253 	case BHND_DEVCLASS_ENET_PHY:
254 		return ((sc->quirks & BWN_QUIRK_ENET_HW_UNPOPULATED) != 0);
255 
256 	default:
257 		return (false);
258 	}
259 }
260 
261 static device_method_t bwn_pci_methods[] = {
262 	/* Device interface */
263 	DEVMETHOD(device_probe,			bwn_pci_probe),
264 	DEVMETHOD(device_attach,		bwn_pci_attach),
265 	DEVMETHOD(device_detach,		bwn_pci_detach),
266 	DEVMETHOD(device_shutdown,		bus_generic_shutdown),
267 	DEVMETHOD(device_suspend,		bus_generic_suspend),
268 	DEVMETHOD(device_resume,		bus_generic_resume),
269 
270 	/* Bus interface */
271 	DEVMETHOD(bus_probe_nomatch,		bwn_pci_probe_nomatch),
272 
273 	/* BHNDB_BUS Interface */
274 	DEVMETHOD(bhndb_bus_get_generic_hwcfg,	bwn_pci_get_generic_hwcfg),
275 	DEVMETHOD(bhndb_bus_get_hardware_table,	bwn_pci_get_bhndb_hwtable),
276 	DEVMETHOD(bhndb_bus_is_core_disabled,	bwn_pci_is_core_disabled),
277 
278 	DEVMETHOD_END
279 };
280 
281 static devclass_t bwn_pci_devclass;
282 
283 DEFINE_CLASS_0(bwn_pci, bwn_pci_driver, bwn_pci_methods, sizeof(struct bwn_pci_softc));
284 DRIVER_MODULE(bwn_pci, pci, bwn_pci_driver, bwn_pci_devclass, NULL, NULL);
285 DRIVER_MODULE(bhndb, bwn_pci, bhndb_pci_driver, bhndb_devclass, NULL, NULL);
286 
287 MODULE_DEPEND(bwn_pci, bwn, 1, 1, 1);
288 MODULE_DEPEND(bwn_pci, bhndb, 1, 1, 1);
289 MODULE_DEPEND(bwn_pci, bhndb_pci, 1, 1, 1);
290 MODULE_DEPEND(bwn_pci, bcma_bhndb, 1, 1, 1);
291 MODULE_DEPEND(bwn_pci, siba_bhndb, 1, 1, 1);
292