xref: /freebsd/sys/dev/bwn/if_bwn_pci.c (revision 09a53ad8f1318c5daae6cfb19d97f4f6459f0013)
1 /*-
2  * Copyright (c) 2015-2016 Landon Fuller <landonf@FreeBSD.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_hwdata.h>
46 #include <dev/bhnd/bhndb/bhndb_pci_hwdata.h>
47 
48 #include <dev/bhnd/bhnd_ids.h>
49 
50 #include "bhndb_bus_if.h"
51 
52 #include "if_bwn_pcivar.h"
53 
54 /* If non-zero, enable attachment of BWN_QUIRK_UNTESTED devices */
55 static int attach_untested = 0;
56 TUNABLE_INT("hw.bwn_pci.attach_untested", &attach_untested);
57 
58 /* If non-zero, probe at a higher priority than the stable if_bwn driver. */
59 static int prefer_new_driver = 0;
60 TUNABLE_INT("hw.bwn_pci.preferred", &prefer_new_driver);
61 
62 /* SIBA Devices */
63 static const struct bwn_pci_device siba_devices[] = {
64 	BWN_BCM_DEV(BCM4301,		"BCM4301 802.11b",
65 	    BWN_QUIRK_ENET_HW_UNPOPULATED),
66 
67 	BWN_BCM_DEV(BCM4306,		"BCM4306 802.11b/g",		0),
68 	BWN_BCM_DEV(BCM4306_D11G,	"BCM4306 802.11g",		0),
69 	BWN_BCM_DEV(BCM4306_D11A,	"BCM4306 802.11a",
70 	    BWN_QUIRK_WLAN_DUALCORE),
71 	BWN_BCM_DEV(BCM4306_D11DUAL,	"BCM4306 802.11a/b",
72 	    BWN_QUIRK_WLAN_DUALCORE),
73 	BWN_BCM_DEV(BCM4306_D11G_ID2,	"BCM4306 802.11g",		0),
74 
75 	BWN_BCM_DEV(BCM4307,		"BCM4307 802.11b",		0),
76 
77 	BWN_BCM_DEV(BCM4311_D11G,	"BCM4311 802.11b/g",		0),
78 	BWN_BCM_DEV(BCM4311_D11DUAL,	"BCM4311 802.11a/b/g",		0),
79 	BWN_BCM_DEV(BCM4311_D11A,	"BCM4311 802.11a",
80 	    BWN_QUIRK_UNTESTED|BWN_QUIRK_WLAN_DUALCORE),
81 
82 	BWN_BCM_DEV(BCM4318_D11G,	"BCM4318 802.11b/g",		0),
83 	BWN_BCM_DEV(BCM4318_D11DUAL,	"BCM4318 802.11a/b/g",		0),
84 	BWN_BCM_DEV(BCM4318_D11A,	"BCM4318 802.11a",
85 	    BWN_QUIRK_UNTESTED|BWN_QUIRK_WLAN_DUALCORE),
86 
87 	BWN_BCM_DEV(BCM4321_D11N,	"BCM4321 802.11n Dual-Band",
88 	    BWN_QUIRK_USBH_UNPOPULATED),
89 	BWN_BCM_DEV(BCM4321_D11N2G,	"BCM4321 802.11n 2GHz",
90 	    BWN_QUIRK_USBH_UNPOPULATED),
91 	BWN_BCM_DEV(BCM4321_D11N2G,	"BCM4321 802.11n 5GHz",
92 	    BWN_QUIRK_UNTESTED|BWN_QUIRK_USBH_UNPOPULATED),
93 
94 	BWN_BCM_DEV(BCM4322_D11N,	"BCM4322 802.11n Dual-Band",	0),
95 	BWN_BCM_DEV(BCM4322_D11N2G,	"BCM4322 802.11n 2GHz",
96 	    BWN_QUIRK_UNTESTED),
97 	BWN_BCM_DEV(BCM4322_D11N5G,	"BCM4322 802.11n 5GHz",
98 	    BWN_QUIRK_UNTESTED),
99 
100 	BWN_BCM_DEV(BCM4328_D11G,	"BCM4328/4312 802.11g",		0),
101 
102 	{ 0, 0, NULL, 0 }
103 };
104 
105 /** BCMA Devices */
106 static const struct bwn_pci_device bcma_devices[] = {
107 	BWN_BCM_DEV(BCM4331_D11N,	"BCM4331 802.11n Dual-Band",	0),
108 	BWN_BCM_DEV(BCM4331_D11N2G,	"BCM4331 802.11n 2GHz",		0),
109 	BWN_BCM_DEV(BCM4331_D11N5G,	"BCM4331 802.11n 5GHz",		0),
110 	BWN_BCM_DEV(BCM43225_D11N2G,	"BCM43225 802.11n 2GHz",	0),
111 
112 	{ 0, 0, NULL, 0}
113 };
114 
115 /** Device configuration table */
116 static const struct bwn_pci_devcfg bwn_pci_devcfgs[] = {
117 	/* SIBA devices */
118 	{
119 		.bridge_hwcfg	= &bhndb_pci_siba_generic_hwcfg,
120 		.bridge_hwtable	= bhndb_pci_generic_hw_table,
121 		.bridge_hwprio	= bhndb_siba_priority_table,
122 		.devices	= siba_devices
123 	},
124 	/* BCMA devices */
125 	{
126 		.bridge_hwcfg	= &bhndb_pci_bcma_generic_hwcfg,
127 		.bridge_hwtable	= bhndb_pci_generic_hw_table,
128 		.bridge_hwprio	= bhndb_bcma_priority_table,
129 		.devices	= bcma_devices
130 	},
131 	{ NULL, NULL, NULL }
132 };
133 
134 /** Search the device configuration table for an entry matching @p dev. */
135 static int
136 bwn_pci_find_devcfg(device_t dev, const struct bwn_pci_devcfg **cfg,
137     const struct bwn_pci_device **device)
138 {
139 	const struct bwn_pci_devcfg	*dvc;
140 	const struct bwn_pci_device	*dv;
141 
142 	for (dvc = bwn_pci_devcfgs; dvc->devices != NULL; dvc++) {
143 		for (dv = dvc->devices; dv->device != 0; dv++) {
144 			if (pci_get_vendor(dev) == dv->vendor &&
145 			    pci_get_device(dev) == dv->device)
146 			{
147 				if (cfg != NULL)
148 					*cfg = dvc;
149 
150 				if (device != NULL)
151 					*device = dv;
152 
153 				return (0);
154 			}
155 		}
156 	}
157 
158 	return (ENOENT);
159 }
160 
161 static int
162 bwn_pci_probe(device_t dev)
163 {
164 	const struct bwn_pci_device	*ident;
165 
166 	if (bwn_pci_find_devcfg(dev, NULL, &ident))
167 		return (ENXIO);
168 
169 	/* Skip untested devices */
170 	if (ident->quirks & BWN_QUIRK_UNTESTED && !attach_untested)
171 		return (ENXIO);
172 
173 	device_set_desc(dev, ident->desc);
174 
175 	/* Until this driver is complete, require explicit opt-in before
176 	 * superceding if_bwn/siba_bwn. */
177 	if (prefer_new_driver)
178 		return (BUS_PROBE_DEFAULT+1);
179 	else
180 		return (BUS_PROBE_LOW_PRIORITY);
181 
182 	// return (BUS_PROBE_DEFAULT);
183 }
184 
185 static int
186 bwn_pci_attach(device_t dev)
187 {
188 	struct bwn_pci_softc		*sc;
189 	const struct bwn_pci_device	*ident;
190 	int				 error;
191 
192 	sc = device_get_softc(dev);
193 	sc->dev = dev;
194 
195 	/* Find our hardware config */
196 	if (bwn_pci_find_devcfg(dev, &sc->devcfg, &ident))
197 		return (ENXIO);
198 
199 	/* Save quirk flags */
200 	sc->quirks = ident->quirks;
201 
202 	/* Attach bridge device */
203 	if ((error = bhndb_attach_bridge(dev, &sc->bhndb_dev, -1)))
204 		return (ENXIO);
205 
206 	/* Success */
207 	return (0);
208 }
209 
210 static int
211 bwn_pci_detach(device_t dev)
212 {
213 	return (bus_generic_detach(dev));
214 }
215 
216 static void
217 bwn_pci_probe_nomatch(device_t dev, device_t child)
218 {
219 	const char *name;
220 
221 	name = device_get_name(child);
222 	if (name == NULL)
223 		name = "unknown device";
224 
225 	device_printf(dev, "<%s> (no driver attached)\n", name);
226 }
227 
228 static const struct bhndb_hwcfg *
229 bwn_pci_get_generic_hwcfg(device_t dev, device_t child)
230 {
231 	struct bwn_pci_softc *sc = device_get_softc(dev);
232 	return (sc->devcfg->bridge_hwcfg);
233 }
234 
235 static const struct bhndb_hw *
236 bwn_pci_get_bhndb_hwtable(device_t dev, device_t child)
237 {
238 	struct bwn_pci_softc *sc = device_get_softc(dev);
239 	return (sc->devcfg->bridge_hwtable);
240 }
241 
242 static const struct bhndb_hw_priority *
243 bwn_pci_get_bhndb_hwprio(device_t dev, device_t child)
244 {
245 	struct bwn_pci_softc *sc = device_get_softc(dev);
246 	return (sc->devcfg->bridge_hwprio);
247 }
248 
249 static bool
250 bwn_pci_is_core_disabled(device_t dev, device_t child,
251     struct bhnd_core_info *core)
252 {
253 	struct bwn_pci_softc	*sc;
254 
255 	sc = device_get_softc(dev);
256 
257 	switch (bhnd_core_class(core)) {
258 	case BHND_DEVCLASS_WLAN:
259 		if (core->unit > 0 && !(sc->quirks & BWN_QUIRK_WLAN_DUALCORE))
260 			return (true);
261 
262 		return (false);
263 
264 	case BHND_DEVCLASS_ENET:
265 	case BHND_DEVCLASS_ENET_MAC:
266 	case BHND_DEVCLASS_ENET_PHY:
267 		return ((sc->quirks & BWN_QUIRK_ENET_HW_UNPOPULATED) != 0);
268 
269 	case BHND_DEVCLASS_USB_HOST:
270 		return ((sc->quirks & BWN_QUIRK_USBH_UNPOPULATED) != 0);
271 
272 	default:
273 		return (false);
274 	}
275 }
276 
277 static device_method_t bwn_pci_methods[] = {
278 	/* Device interface */
279 	DEVMETHOD(device_probe,			bwn_pci_probe),
280 	DEVMETHOD(device_attach,		bwn_pci_attach),
281 	DEVMETHOD(device_detach,		bwn_pci_detach),
282 	DEVMETHOD(device_shutdown,		bus_generic_shutdown),
283 	DEVMETHOD(device_suspend,		bus_generic_suspend),
284 	DEVMETHOD(device_resume,		bus_generic_resume),
285 
286 	/* Bus interface */
287 	DEVMETHOD(bus_probe_nomatch,		bwn_pci_probe_nomatch),
288 
289 	/* BHNDB_BUS Interface */
290 	DEVMETHOD(bhndb_bus_get_generic_hwcfg,	bwn_pci_get_generic_hwcfg),
291 	DEVMETHOD(bhndb_bus_get_hardware_table,	bwn_pci_get_bhndb_hwtable),
292 	DEVMETHOD(bhndb_bus_get_hardware_prio,	bwn_pci_get_bhndb_hwprio),
293 	DEVMETHOD(bhndb_bus_is_core_disabled,	bwn_pci_is_core_disabled),
294 
295 	DEVMETHOD_END
296 };
297 
298 static devclass_t bwn_pci_devclass;
299 
300 DEFINE_CLASS_0(bwn_pci, bwn_pci_driver, bwn_pci_methods, sizeof(struct bwn_pci_softc));
301 DRIVER_MODULE(bwn_pci, pci, bwn_pci_driver, bwn_pci_devclass, NULL, NULL);
302 DRIVER_MODULE(bhndb, bwn_pci, bhndb_pci_driver, bhndb_devclass, NULL, NULL);
303 
304 MODULE_DEPEND(bwn_pci, bwn, 1, 1, 1);
305 MODULE_DEPEND(bwn_pci, bhndb, 1, 1, 1);
306 MODULE_DEPEND(bwn_pci, bhndb_pci, 1, 1, 1);
307 MODULE_DEPEND(bwn_pci, bcma_bhndb, 1, 1, 1);
308 MODULE_DEPEND(bwn_pci, siba_bhndb, 1, 1, 1);
309