1148ed571SAdrian Chadd /*- 2*8d14ca9cSLandon J. Fuller * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3*8d14ca9cSLandon J. Fuller * 4111d7cb2SLandon J. Fuller * Copyright (c) 2015-2016 Landon Fuller <landonf@FreeBSD.org> 5148ed571SAdrian Chadd * All rights reserved. 6148ed571SAdrian Chadd * 7148ed571SAdrian Chadd * Redistribution and use in source and binary forms, with or without 8148ed571SAdrian Chadd * modification, are permitted provided that the following conditions 9148ed571SAdrian Chadd * are met: 10148ed571SAdrian Chadd * 1. Redistributions of source code must retain the above copyright 11*8d14ca9cSLandon J. Fuller * notice, this list of conditions and the following disclaimer. 12*8d14ca9cSLandon J. Fuller * 2. Redistributions in binary form must reproduce the above copyright 13*8d14ca9cSLandon J. Fuller * notice, this list of conditions and the following disclaimer in the 14*8d14ca9cSLandon J. Fuller * documentation and/or other materials provided with the distribution. 15148ed571SAdrian Chadd * 16*8d14ca9cSLandon J. Fuller * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17*8d14ca9cSLandon J. Fuller * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18*8d14ca9cSLandon J. Fuller * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19*8d14ca9cSLandon J. Fuller * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20*8d14ca9cSLandon J. Fuller * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21*8d14ca9cSLandon J. Fuller * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22*8d14ca9cSLandon J. Fuller * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23*8d14ca9cSLandon J. Fuller * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24*8d14ca9cSLandon J. Fuller * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25*8d14ca9cSLandon J. Fuller * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26*8d14ca9cSLandon J. Fuller * SUCH DAMAGE. 27148ed571SAdrian Chadd */ 28148ed571SAdrian Chadd 29148ed571SAdrian Chadd #include <sys/cdefs.h> 30148ed571SAdrian Chadd __FBSDID("$FreeBSD$"); 31148ed571SAdrian Chadd 325025b8d5SAdrian Chadd #include "opt_bwn.h" 335025b8d5SAdrian Chadd #include "opt_wlan.h" 345025b8d5SAdrian Chadd 35148ed571SAdrian Chadd #include <sys/param.h> 36148ed571SAdrian Chadd #include <sys/kernel.h> 37148ed571SAdrian Chadd #include <sys/bus.h> 38148ed571SAdrian Chadd #include <sys/module.h> 39148ed571SAdrian Chadd 40148ed571SAdrian Chadd #include <dev/pci/pcireg.h> 41148ed571SAdrian Chadd #include <dev/pci/pcivar.h> 42148ed571SAdrian Chadd 43148ed571SAdrian Chadd #include <dev/bhnd/bhndb/bhndb_pcivar.h> 44111d7cb2SLandon J. Fuller #include <dev/bhnd/bhndb/bhndb_hwdata.h> 45148ed571SAdrian Chadd #include <dev/bhnd/bhndb/bhndb_pci_hwdata.h> 46148ed571SAdrian Chadd 47148ed571SAdrian Chadd #include <dev/bhnd/bhnd_ids.h> 48148ed571SAdrian Chadd 49148ed571SAdrian Chadd #include "bhndb_bus_if.h" 50148ed571SAdrian Chadd 51148ed571SAdrian Chadd #include "if_bwn_pcivar.h" 52148ed571SAdrian Chadd 53148ed571SAdrian Chadd /* If non-zero, enable attachment of BWN_QUIRK_UNTESTED devices */ 54148ed571SAdrian Chadd static int attach_untested = 0; 55148ed571SAdrian Chadd TUNABLE_INT("hw.bwn_pci.attach_untested", &attach_untested); 56148ed571SAdrian Chadd 57148ed571SAdrian Chadd /* If non-zero, probe at a higher priority than the stable if_bwn driver. */ 58148ed571SAdrian Chadd static int prefer_new_driver = 0; 59148ed571SAdrian Chadd TUNABLE_INT("hw.bwn_pci.preferred", &prefer_new_driver); 60148ed571SAdrian Chadd 61148ed571SAdrian Chadd /* SIBA Devices */ 62148ed571SAdrian Chadd static const struct bwn_pci_device siba_devices[] = { 63148ed571SAdrian Chadd BWN_BCM_DEV(BCM4306_D11A, "BCM4306 802.11a", 64148ed571SAdrian Chadd BWN_QUIRK_WLAN_DUALCORE), 65148ed571SAdrian Chadd BWN_BCM_DEV(BCM4307, "BCM4307 802.11b", 0), 66148ed571SAdrian Chadd 67148ed571SAdrian Chadd BWN_BCM_DEV(BCM4311_D11G, "BCM4311 802.11b/g", 0), 68148ed571SAdrian Chadd BWN_BCM_DEV(BCM4311_D11DUAL, "BCM4311 802.11a/b/g", 0), 69148ed571SAdrian Chadd BWN_BCM_DEV(BCM4311_D11A, "BCM4311 802.11a", 70148ed571SAdrian Chadd BWN_QUIRK_UNTESTED|BWN_QUIRK_WLAN_DUALCORE), 71148ed571SAdrian Chadd 72148ed571SAdrian Chadd BWN_BCM_DEV(BCM4318_D11G, "BCM4318 802.11b/g", 0), 73148ed571SAdrian Chadd BWN_BCM_DEV(BCM4318_D11DUAL, "BCM4318 802.11a/b/g", 0), 74148ed571SAdrian Chadd BWN_BCM_DEV(BCM4318_D11A, "BCM4318 802.11a", 75148ed571SAdrian Chadd BWN_QUIRK_UNTESTED|BWN_QUIRK_WLAN_DUALCORE), 76148ed571SAdrian Chadd 777d616280SLandon J. Fuller BWN_BCM_DEV(BCM4321_D11N, "BCM4321 802.11n Dual-Band", 787d616280SLandon J. Fuller BWN_QUIRK_USBH_UNPOPULATED), 797d616280SLandon J. Fuller BWN_BCM_DEV(BCM4321_D11N2G, "BCM4321 802.11n 2GHz", 807d616280SLandon J. Fuller BWN_QUIRK_USBH_UNPOPULATED), 81*8d14ca9cSLandon J. Fuller BWN_BCM_DEV(BCM4321_D11N5G, "BCM4321 802.11n 5GHz", 827d616280SLandon J. Fuller BWN_QUIRK_UNTESTED|BWN_QUIRK_USBH_UNPOPULATED), 83148ed571SAdrian Chadd 84148ed571SAdrian Chadd BWN_BCM_DEV(BCM4322_D11N, "BCM4322 802.11n Dual-Band", 0), 85148ed571SAdrian Chadd BWN_BCM_DEV(BCM4322_D11N2G, "BCM4322 802.11n 2GHz", 86148ed571SAdrian Chadd BWN_QUIRK_UNTESTED), 87148ed571SAdrian Chadd BWN_BCM_DEV(BCM4322_D11N5G, "BCM4322 802.11n 5GHz", 88148ed571SAdrian Chadd BWN_QUIRK_UNTESTED), 89148ed571SAdrian Chadd 90148ed571SAdrian Chadd BWN_BCM_DEV(BCM4328_D11G, "BCM4328/4312 802.11g", 0), 91148ed571SAdrian Chadd 92148ed571SAdrian Chadd { 0, 0, NULL, 0 } 93148ed571SAdrian Chadd }; 94148ed571SAdrian Chadd 95148ed571SAdrian Chadd /** BCMA Devices */ 96148ed571SAdrian Chadd static const struct bwn_pci_device bcma_devices[] = { 97148ed571SAdrian Chadd BWN_BCM_DEV(BCM4331_D11N, "BCM4331 802.11n Dual-Band", 0), 98148ed571SAdrian Chadd BWN_BCM_DEV(BCM4331_D11N2G, "BCM4331 802.11n 2GHz", 0), 99148ed571SAdrian Chadd BWN_BCM_DEV(BCM4331_D11N5G, "BCM4331 802.11n 5GHz", 0), 100*8d14ca9cSLandon J. Fuller BWN_BCM_DEV(BCM43224_D11N, "BCM43224 802.11n Dual-Band", 0), 101972459a6SAdrian Chadd BWN_BCM_DEV(BCM43225_D11N2G, "BCM43225 802.11n 2GHz", 0), 102148ed571SAdrian Chadd 103148ed571SAdrian Chadd { 0, 0, NULL, 0} 104148ed571SAdrian Chadd }; 105148ed571SAdrian Chadd 106148ed571SAdrian Chadd /** Device configuration table */ 107148ed571SAdrian Chadd static const struct bwn_pci_devcfg bwn_pci_devcfgs[] = { 108148ed571SAdrian Chadd /* SIBA devices */ 109148ed571SAdrian Chadd { 110148ed571SAdrian Chadd .bridge_hwcfg = &bhndb_pci_siba_generic_hwcfg, 111148ed571SAdrian Chadd .bridge_hwtable = bhndb_pci_generic_hw_table, 112111d7cb2SLandon J. Fuller .bridge_hwprio = bhndb_siba_priority_table, 113148ed571SAdrian Chadd .devices = siba_devices 114148ed571SAdrian Chadd }, 115148ed571SAdrian Chadd /* BCMA devices */ 116148ed571SAdrian Chadd { 117148ed571SAdrian Chadd .bridge_hwcfg = &bhndb_pci_bcma_generic_hwcfg, 118148ed571SAdrian Chadd .bridge_hwtable = bhndb_pci_generic_hw_table, 119111d7cb2SLandon J. Fuller .bridge_hwprio = bhndb_bcma_priority_table, 120148ed571SAdrian Chadd .devices = bcma_devices 121148ed571SAdrian Chadd }, 122148ed571SAdrian Chadd { NULL, NULL, NULL } 123148ed571SAdrian Chadd }; 124148ed571SAdrian Chadd 125148ed571SAdrian Chadd /** Search the device configuration table for an entry matching @p dev. */ 126148ed571SAdrian Chadd static int 127148ed571SAdrian Chadd bwn_pci_find_devcfg(device_t dev, const struct bwn_pci_devcfg **cfg, 128148ed571SAdrian Chadd const struct bwn_pci_device **device) 129148ed571SAdrian Chadd { 130148ed571SAdrian Chadd const struct bwn_pci_devcfg *dvc; 131148ed571SAdrian Chadd const struct bwn_pci_device *dv; 132148ed571SAdrian Chadd 133148ed571SAdrian Chadd for (dvc = bwn_pci_devcfgs; dvc->devices != NULL; dvc++) { 134148ed571SAdrian Chadd for (dv = dvc->devices; dv->device != 0; dv++) { 135148ed571SAdrian Chadd if (pci_get_vendor(dev) == dv->vendor && 136148ed571SAdrian Chadd pci_get_device(dev) == dv->device) 137148ed571SAdrian Chadd { 138148ed571SAdrian Chadd if (cfg != NULL) 139148ed571SAdrian Chadd *cfg = dvc; 140148ed571SAdrian Chadd 141148ed571SAdrian Chadd if (device != NULL) 142148ed571SAdrian Chadd *device = dv; 143148ed571SAdrian Chadd 144148ed571SAdrian Chadd return (0); 145148ed571SAdrian Chadd } 146148ed571SAdrian Chadd } 147148ed571SAdrian Chadd } 148148ed571SAdrian Chadd 149148ed571SAdrian Chadd return (ENOENT); 150148ed571SAdrian Chadd } 151148ed571SAdrian Chadd 152148ed571SAdrian Chadd static int 153148ed571SAdrian Chadd bwn_pci_probe(device_t dev) 154148ed571SAdrian Chadd { 155148ed571SAdrian Chadd const struct bwn_pci_device *ident; 156148ed571SAdrian Chadd 157148ed571SAdrian Chadd if (bwn_pci_find_devcfg(dev, NULL, &ident)) 158148ed571SAdrian Chadd return (ENXIO); 159148ed571SAdrian Chadd 160148ed571SAdrian Chadd /* Skip untested devices */ 161148ed571SAdrian Chadd if (ident->quirks & BWN_QUIRK_UNTESTED && !attach_untested) 162148ed571SAdrian Chadd return (ENXIO); 163148ed571SAdrian Chadd 164148ed571SAdrian Chadd device_set_desc(dev, ident->desc); 165148ed571SAdrian Chadd 166148ed571SAdrian Chadd /* Until this driver is complete, require explicit opt-in before 167148ed571SAdrian Chadd * superceding if_bwn/siba_bwn. */ 168148ed571SAdrian Chadd if (prefer_new_driver) 169148ed571SAdrian Chadd return (BUS_PROBE_DEFAULT+1); 170148ed571SAdrian Chadd else 171148ed571SAdrian Chadd return (BUS_PROBE_LOW_PRIORITY); 172148ed571SAdrian Chadd 173148ed571SAdrian Chadd // return (BUS_PROBE_DEFAULT); 174148ed571SAdrian Chadd } 175148ed571SAdrian Chadd 176148ed571SAdrian Chadd static int 177148ed571SAdrian Chadd bwn_pci_attach(device_t dev) 178148ed571SAdrian Chadd { 179148ed571SAdrian Chadd struct bwn_pci_softc *sc; 180148ed571SAdrian Chadd const struct bwn_pci_device *ident; 181148ed571SAdrian Chadd int error; 182148ed571SAdrian Chadd 183148ed571SAdrian Chadd sc = device_get_softc(dev); 184148ed571SAdrian Chadd sc->dev = dev; 185148ed571SAdrian Chadd 186148ed571SAdrian Chadd /* Find our hardware config */ 187148ed571SAdrian Chadd if (bwn_pci_find_devcfg(dev, &sc->devcfg, &ident)) 188148ed571SAdrian Chadd return (ENXIO); 189148ed571SAdrian Chadd 190148ed571SAdrian Chadd /* Save quirk flags */ 191148ed571SAdrian Chadd sc->quirks = ident->quirks; 192148ed571SAdrian Chadd 193148ed571SAdrian Chadd /* Attach bridge device */ 194148ed571SAdrian Chadd if ((error = bhndb_attach_bridge(dev, &sc->bhndb_dev, -1))) 195148ed571SAdrian Chadd return (ENXIO); 196148ed571SAdrian Chadd 197148ed571SAdrian Chadd /* Success */ 198148ed571SAdrian Chadd return (0); 199148ed571SAdrian Chadd } 200148ed571SAdrian Chadd 201148ed571SAdrian Chadd static int 202148ed571SAdrian Chadd bwn_pci_detach(device_t dev) 203148ed571SAdrian Chadd { 204*8d14ca9cSLandon J. Fuller int error; 205*8d14ca9cSLandon J. Fuller 206*8d14ca9cSLandon J. Fuller if ((error = bus_generic_detach(dev))) 207*8d14ca9cSLandon J. Fuller return (error); 208*8d14ca9cSLandon J. Fuller 209*8d14ca9cSLandon J. Fuller return (device_delete_children(dev)); 210148ed571SAdrian Chadd } 211148ed571SAdrian Chadd 212148ed571SAdrian Chadd static void 213148ed571SAdrian Chadd bwn_pci_probe_nomatch(device_t dev, device_t child) 214148ed571SAdrian Chadd { 215148ed571SAdrian Chadd const char *name; 216148ed571SAdrian Chadd 217148ed571SAdrian Chadd name = device_get_name(child); 218148ed571SAdrian Chadd if (name == NULL) 219148ed571SAdrian Chadd name = "unknown device"; 220148ed571SAdrian Chadd 221148ed571SAdrian Chadd device_printf(dev, "<%s> (no driver attached)\n", name); 222148ed571SAdrian Chadd } 223148ed571SAdrian Chadd 224148ed571SAdrian Chadd static const struct bhndb_hwcfg * 225148ed571SAdrian Chadd bwn_pci_get_generic_hwcfg(device_t dev, device_t child) 226148ed571SAdrian Chadd { 227148ed571SAdrian Chadd struct bwn_pci_softc *sc = device_get_softc(dev); 228148ed571SAdrian Chadd return (sc->devcfg->bridge_hwcfg); 229148ed571SAdrian Chadd } 230148ed571SAdrian Chadd 231148ed571SAdrian Chadd static const struct bhndb_hw * 232148ed571SAdrian Chadd bwn_pci_get_bhndb_hwtable(device_t dev, device_t child) 233148ed571SAdrian Chadd { 234148ed571SAdrian Chadd struct bwn_pci_softc *sc = device_get_softc(dev); 235148ed571SAdrian Chadd return (sc->devcfg->bridge_hwtable); 236148ed571SAdrian Chadd } 237148ed571SAdrian Chadd 238111d7cb2SLandon J. Fuller static const struct bhndb_hw_priority * 239111d7cb2SLandon J. Fuller bwn_pci_get_bhndb_hwprio(device_t dev, device_t child) 240111d7cb2SLandon J. Fuller { 241111d7cb2SLandon J. Fuller struct bwn_pci_softc *sc = device_get_softc(dev); 242111d7cb2SLandon J. Fuller return (sc->devcfg->bridge_hwprio); 243111d7cb2SLandon J. Fuller } 244111d7cb2SLandon J. Fuller 245148ed571SAdrian Chadd static bool 246148ed571SAdrian Chadd bwn_pci_is_core_disabled(device_t dev, device_t child, 247148ed571SAdrian Chadd struct bhnd_core_info *core) 248148ed571SAdrian Chadd { 249148ed571SAdrian Chadd struct bwn_pci_softc *sc; 250148ed571SAdrian Chadd 251148ed571SAdrian Chadd sc = device_get_softc(dev); 252148ed571SAdrian Chadd 253148ed571SAdrian Chadd switch (bhnd_core_class(core)) { 254148ed571SAdrian Chadd case BHND_DEVCLASS_WLAN: 255148ed571SAdrian Chadd if (core->unit > 0 && !(sc->quirks & BWN_QUIRK_WLAN_DUALCORE)) 256148ed571SAdrian Chadd return (true); 257148ed571SAdrian Chadd 258148ed571SAdrian Chadd return (false); 259148ed571SAdrian Chadd 260148ed571SAdrian Chadd case BHND_DEVCLASS_ENET: 261148ed571SAdrian Chadd case BHND_DEVCLASS_ENET_MAC: 262148ed571SAdrian Chadd case BHND_DEVCLASS_ENET_PHY: 263148ed571SAdrian Chadd return ((sc->quirks & BWN_QUIRK_ENET_HW_UNPOPULATED) != 0); 264148ed571SAdrian Chadd 2657d616280SLandon J. Fuller case BHND_DEVCLASS_USB_HOST: 2667d616280SLandon J. Fuller return ((sc->quirks & BWN_QUIRK_USBH_UNPOPULATED) != 0); 2677d616280SLandon J. Fuller 268148ed571SAdrian Chadd default: 269148ed571SAdrian Chadd return (false); 270148ed571SAdrian Chadd } 271148ed571SAdrian Chadd } 272148ed571SAdrian Chadd 273148ed571SAdrian Chadd static device_method_t bwn_pci_methods[] = { 274148ed571SAdrian Chadd /* Device interface */ 275148ed571SAdrian Chadd DEVMETHOD(device_probe, bwn_pci_probe), 276148ed571SAdrian Chadd DEVMETHOD(device_attach, bwn_pci_attach), 277148ed571SAdrian Chadd DEVMETHOD(device_detach, bwn_pci_detach), 278148ed571SAdrian Chadd DEVMETHOD(device_shutdown, bus_generic_shutdown), 279148ed571SAdrian Chadd DEVMETHOD(device_suspend, bus_generic_suspend), 280148ed571SAdrian Chadd DEVMETHOD(device_resume, bus_generic_resume), 281148ed571SAdrian Chadd 282148ed571SAdrian Chadd /* Bus interface */ 283148ed571SAdrian Chadd DEVMETHOD(bus_probe_nomatch, bwn_pci_probe_nomatch), 284148ed571SAdrian Chadd 285148ed571SAdrian Chadd /* BHNDB_BUS Interface */ 286148ed571SAdrian Chadd DEVMETHOD(bhndb_bus_get_generic_hwcfg, bwn_pci_get_generic_hwcfg), 287148ed571SAdrian Chadd DEVMETHOD(bhndb_bus_get_hardware_table, bwn_pci_get_bhndb_hwtable), 288111d7cb2SLandon J. Fuller DEVMETHOD(bhndb_bus_get_hardware_prio, bwn_pci_get_bhndb_hwprio), 289148ed571SAdrian Chadd DEVMETHOD(bhndb_bus_is_core_disabled, bwn_pci_is_core_disabled), 290148ed571SAdrian Chadd 291148ed571SAdrian Chadd DEVMETHOD_END 292148ed571SAdrian Chadd }; 293148ed571SAdrian Chadd 294148ed571SAdrian Chadd static devclass_t bwn_pci_devclass; 295148ed571SAdrian Chadd 296*8d14ca9cSLandon J. Fuller DEFINE_CLASS_0(bwn_pci, bwn_pci_driver, bwn_pci_methods, 297*8d14ca9cSLandon J. Fuller sizeof(struct bwn_pci_softc)); 298*8d14ca9cSLandon J. Fuller DRIVER_MODULE_ORDERED(bwn_pci, pci, bwn_pci_driver, bwn_pci_devclass, NULL, 299*8d14ca9cSLandon J. Fuller NULL, SI_ORDER_ANY); 300148ed571SAdrian Chadd DRIVER_MODULE(bhndb, bwn_pci, bhndb_pci_driver, bhndb_devclass, NULL, NULL); 301148ed571SAdrian Chadd 302148ed571SAdrian Chadd MODULE_DEPEND(bwn_pci, bwn, 1, 1, 1); 303*8d14ca9cSLandon J. Fuller MODULE_DEPEND(bwn_pci, bhnd, 1, 1, 1); 304148ed571SAdrian Chadd MODULE_DEPEND(bwn_pci, bhndb, 1, 1, 1); 305148ed571SAdrian Chadd MODULE_DEPEND(bwn_pci, bhndb_pci, 1, 1, 1); 306148ed571SAdrian Chadd MODULE_DEPEND(bwn_pci, bcma_bhndb, 1, 1, 1); 307148ed571SAdrian Chadd MODULE_DEPEND(bwn_pci, siba_bhndb, 1, 1, 1); 308