xref: /freebsd/sys/dev/bwn/if_bwn_pci.c (revision be8aff81c11e51d25e7a134298b6aa0300f4b7fc)
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 	BWN_BCM_DEV(BCM43225_D11N2G,	"BCM43225 802.11n 2GHz",	0),
108 
109 	{ 0, 0, NULL, 0}
110 };
111 
112 /** Device configuration table */
113 static const struct bwn_pci_devcfg bwn_pci_devcfgs[] = {
114 	/* SIBA devices */
115 	{
116 		.bridge_hwcfg	= &bhndb_pci_siba_generic_hwcfg,
117 		.bridge_hwtable	= bhndb_pci_generic_hw_table,
118 		.devices	= siba_devices
119 	},
120 	/* BCMA devices */
121 	{
122 		.bridge_hwcfg	= &bhndb_pci_bcma_generic_hwcfg,
123 		.bridge_hwtable	= bhndb_pci_generic_hw_table,
124 		.devices	= bcma_devices
125 	},
126 	{ NULL, NULL, NULL }
127 };
128 
129 /** Search the device configuration table for an entry matching @p dev. */
130 static int
131 bwn_pci_find_devcfg(device_t dev, const struct bwn_pci_devcfg **cfg,
132     const struct bwn_pci_device **device)
133 {
134 	const struct bwn_pci_devcfg	*dvc;
135 	const struct bwn_pci_device	*dv;
136 
137 	for (dvc = bwn_pci_devcfgs; dvc->devices != NULL; dvc++) {
138 		for (dv = dvc->devices; dv->device != 0; dv++) {
139 			if (pci_get_vendor(dev) == dv->vendor &&
140 			    pci_get_device(dev) == dv->device)
141 			{
142 				if (cfg != NULL)
143 					*cfg = dvc;
144 
145 				if (device != NULL)
146 					*device = dv;
147 
148 				return (0);
149 			}
150 		}
151 	}
152 
153 	return (ENOENT);
154 }
155 
156 static int
157 bwn_pci_probe(device_t dev)
158 {
159 	const struct bwn_pci_device	*ident;
160 
161 	if (bwn_pci_find_devcfg(dev, NULL, &ident))
162 		return (ENXIO);
163 
164 	/* Skip untested devices */
165 	if (ident->quirks & BWN_QUIRK_UNTESTED && !attach_untested)
166 		return (ENXIO);
167 
168 	device_set_desc(dev, ident->desc);
169 
170 	/* Until this driver is complete, require explicit opt-in before
171 	 * superceding if_bwn/siba_bwn. */
172 	if (prefer_new_driver)
173 		return (BUS_PROBE_DEFAULT+1);
174 	else
175 		return (BUS_PROBE_LOW_PRIORITY);
176 
177 	// return (BUS_PROBE_DEFAULT);
178 }
179 
180 static int
181 bwn_pci_attach(device_t dev)
182 {
183 	struct bwn_pci_softc		*sc;
184 	const struct bwn_pci_device	*ident;
185 	int				 error;
186 
187 	sc = device_get_softc(dev);
188 	sc->dev = dev;
189 
190 	/* Find our hardware config */
191 	if (bwn_pci_find_devcfg(dev, &sc->devcfg, &ident))
192 		return (ENXIO);
193 
194 	/* Save quirk flags */
195 	sc->quirks = ident->quirks;
196 
197 	/* Attach bridge device */
198 	if ((error = bhndb_attach_bridge(dev, &sc->bhndb_dev, -1)))
199 		return (ENXIO);
200 
201 	/* Success */
202 	return (0);
203 }
204 
205 static int
206 bwn_pci_detach(device_t dev)
207 {
208 	return (bus_generic_detach(dev));
209 }
210 
211 static void
212 bwn_pci_probe_nomatch(device_t dev, device_t child)
213 {
214 	const char *name;
215 
216 	name = device_get_name(child);
217 	if (name == NULL)
218 		name = "unknown device";
219 
220 	device_printf(dev, "<%s> (no driver attached)\n", name);
221 }
222 
223 static const struct bhndb_hwcfg *
224 bwn_pci_get_generic_hwcfg(device_t dev, device_t child)
225 {
226 	struct bwn_pci_softc *sc = device_get_softc(dev);
227 	return (sc->devcfg->bridge_hwcfg);
228 }
229 
230 static const struct bhndb_hw *
231 bwn_pci_get_bhndb_hwtable(device_t dev, device_t child)
232 {
233 	struct bwn_pci_softc *sc = device_get_softc(dev);
234 	return (sc->devcfg->bridge_hwtable);
235 }
236 
237 static bool
238 bwn_pci_is_core_disabled(device_t dev, device_t child,
239     struct bhnd_core_info *core)
240 {
241 	struct bwn_pci_softc	*sc;
242 
243 	sc = device_get_softc(dev);
244 
245 	switch (bhnd_core_class(core)) {
246 	case BHND_DEVCLASS_WLAN:
247 		if (core->unit > 0 && !(sc->quirks & BWN_QUIRK_WLAN_DUALCORE))
248 			return (true);
249 
250 		return (false);
251 
252 	case BHND_DEVCLASS_ENET:
253 	case BHND_DEVCLASS_ENET_MAC:
254 	case BHND_DEVCLASS_ENET_PHY:
255 		return ((sc->quirks & BWN_QUIRK_ENET_HW_UNPOPULATED) != 0);
256 
257 	default:
258 		return (false);
259 	}
260 }
261 
262 static device_method_t bwn_pci_methods[] = {
263 	/* Device interface */
264 	DEVMETHOD(device_probe,			bwn_pci_probe),
265 	DEVMETHOD(device_attach,		bwn_pci_attach),
266 	DEVMETHOD(device_detach,		bwn_pci_detach),
267 	DEVMETHOD(device_shutdown,		bus_generic_shutdown),
268 	DEVMETHOD(device_suspend,		bus_generic_suspend),
269 	DEVMETHOD(device_resume,		bus_generic_resume),
270 
271 	/* Bus interface */
272 	DEVMETHOD(bus_probe_nomatch,		bwn_pci_probe_nomatch),
273 
274 	/* BHNDB_BUS Interface */
275 	DEVMETHOD(bhndb_bus_get_generic_hwcfg,	bwn_pci_get_generic_hwcfg),
276 	DEVMETHOD(bhndb_bus_get_hardware_table,	bwn_pci_get_bhndb_hwtable),
277 	DEVMETHOD(bhndb_bus_is_core_disabled,	bwn_pci_is_core_disabled),
278 
279 	DEVMETHOD_END
280 };
281 
282 static devclass_t bwn_pci_devclass;
283 
284 DEFINE_CLASS_0(bwn_pci, bwn_pci_driver, bwn_pci_methods, sizeof(struct bwn_pci_softc));
285 DRIVER_MODULE(bwn_pci, pci, bwn_pci_driver, bwn_pci_devclass, NULL, NULL);
286 DRIVER_MODULE(bhndb, bwn_pci, bhndb_pci_driver, bhndb_devclass, NULL, NULL);
287 
288 MODULE_DEPEND(bwn_pci, bwn, 1, 1, 1);
289 MODULE_DEPEND(bwn_pci, bhndb, 1, 1, 1);
290 MODULE_DEPEND(bwn_pci, bhndb_pci, 1, 1, 1);
291 MODULE_DEPEND(bwn_pci, bcma_bhndb, 1, 1, 1);
292 MODULE_DEPEND(bwn_pci, siba_bhndb, 1, 1, 1);
293