if_wi.c revision 191746
1139749Simp/*- 246492Swpaul * Copyright (c) 1997, 1998, 1999 346492Swpaul * Bill Paul <wpaul@ctr.columbia.edu>. All rights reserved. 446492Swpaul * 546492Swpaul * Redistribution and use in source and binary forms, with or without 646492Swpaul * modification, are permitted provided that the following conditions 746492Swpaul * are met: 846492Swpaul * 1. Redistributions of source code must retain the above copyright 946492Swpaul * notice, this list of conditions and the following disclaimer. 1046492Swpaul * 2. Redistributions in binary form must reproduce the above copyright 1146492Swpaul * notice, this list of conditions and the following disclaimer in the 1246492Swpaul * documentation and/or other materials provided with the distribution. 1346492Swpaul * 3. All advertising materials mentioning features or use of this software 1446492Swpaul * must display the following acknowledgement: 1546492Swpaul * This product includes software developed by Bill Paul. 1646492Swpaul * 4. Neither the name of the author nor the names of any co-contributors 1746492Swpaul * may be used to endorse or promote products derived from this software 1846492Swpaul * without specific prior written permission. 1946492Swpaul * 2046492Swpaul * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND 2146492Swpaul * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2246492Swpaul * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2346492Swpaul * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD 2446492Swpaul * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 2546492Swpaul * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 2646492Swpaul * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 2746492Swpaul * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 2846492Swpaul * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 2946492Swpaul * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 3046492Swpaul * THE POSSIBILITY OF SUCH DAMAGE. 3146492Swpaul */ 3246492Swpaul 3346492Swpaul/* 34109323Ssam * Lucent WaveLAN/IEEE 802.11 PCMCIA driver. 3546492Swpaul * 36109323Ssam * Original FreeBSD driver written by Bill Paul <wpaul@ctr.columbia.edu> 3746492Swpaul * Electrical Engineering Department 3846492Swpaul * Columbia University, New York City 3946492Swpaul */ 4046492Swpaul 4146492Swpaul/* 4247401Swpaul * The WaveLAN/IEEE adapter is the second generation of the WaveLAN 4346492Swpaul * from Lucent. Unlike the older cards, the new ones are programmed 4446492Swpaul * entirely via a firmware-driven controller called the Hermes. 4546492Swpaul * Unfortunately, Lucent will not release the Hermes programming manual 4646492Swpaul * without an NDA (if at all). What they do release is an API library 4746492Swpaul * called the HCF (Hardware Control Functions) which is supposed to 4846492Swpaul * do the device-specific operations of a device driver for you. The 4946492Swpaul * publically available version of the HCF library (the 'HCF Light') is 5047401Swpaul * a) extremely gross, b) lacks certain features, particularly support 5146492Swpaul * for 802.11 frames, and c) is contaminated by the GNU Public License. 5246492Swpaul * 5346492Swpaul * This driver does not use the HCF or HCF Light at all. Instead, it 5446492Swpaul * programs the Hermes controller directly, using information gleaned 5546492Swpaul * from the HCF Light code and corresponding documentation. 5646492Swpaul * 5795534Simp * This driver supports the ISA, PCMCIA and PCI versions of the Lucent 5895534Simp * WaveLan cards (based on the Hermes chipset), as well as the newer 5995534Simp * Prism 2 chipsets with firmware from Intersil and Symbol. 6046492Swpaul */ 6146492Swpaul 62113038Sobrien#include <sys/cdefs.h> 63113038Sobrien__FBSDID("$FreeBSD: head/sys/dev/wi/if_wi.c 191746 2009-05-02 15:14:18Z thompsa $"); 64113038Sobrien 65109323Ssam#define WI_HERMES_STATS_WAR /* Work around stats counter bug. */ 66109323Ssam 6746492Swpaul#include <sys/param.h> 6846492Swpaul#include <sys/systm.h> 6995533Smike#include <sys/endian.h> 7046492Swpaul#include <sys/sockio.h> 7146492Swpaul#include <sys/mbuf.h> 72164033Srwatson#include <sys/priv.h> 7383366Sjulian#include <sys/proc.h> 7446492Swpaul#include <sys/kernel.h> 7546492Swpaul#include <sys/socket.h> 7653702Swpaul#include <sys/module.h> 7753702Swpaul#include <sys/bus.h> 7894486Simp#include <sys/random.h> 7953702Swpaul#include <sys/syslog.h> 8053702Swpaul#include <sys/sysctl.h> 8146492Swpaul 8253702Swpaul#include <machine/bus.h> 8353702Swpaul#include <machine/resource.h> 84116951Ssam#include <machine/atomic.h> 8553702Swpaul#include <sys/rman.h> 8653702Swpaul 8746492Swpaul#include <net/if.h> 8846492Swpaul#include <net/if_arp.h> 8946492Swpaul#include <net/ethernet.h> 9046492Swpaul#include <net/if_dl.h> 91190579Ssam#include <net/if_llc.h> 9246492Swpaul#include <net/if_media.h> 9346492Swpaul#include <net/if_types.h> 9446492Swpaul 95116951Ssam#include <net80211/ieee80211_var.h> 96116951Ssam#include <net80211/ieee80211_ioctl.h> 97119784Ssam#include <net80211/ieee80211_radiotap.h> 98116951Ssam 9946492Swpaul#include <netinet/in.h> 10046492Swpaul#include <netinet/in_systm.h> 10146492Swpaul#include <netinet/in_var.h> 10246492Swpaul#include <netinet/ip.h> 10346492Swpaul#include <netinet/if_ether.h> 10446492Swpaul 10546492Swpaul#include <net/bpf.h> 10646492Swpaul 10770808Speter#include <dev/wi/if_wavelan_ieee.h> 108119784Ssam#include <dev/wi/if_wireg.h> 10993611Simp#include <dev/wi/if_wivar.h> 11046492Swpaul 111178354Ssamstatic struct ieee80211vap *wi_vap_create(struct ieee80211com *ic, 112178354Ssam const char name[IFNAMSIZ], int unit, int opmode, int flags, 113178354Ssam const uint8_t bssid[IEEE80211_ADDR_LEN], 114178354Ssam const uint8_t mac[IEEE80211_ADDR_LEN]); 115178354Ssamstatic void wi_vap_delete(struct ieee80211vap *vap); 116178354Ssamstatic void wi_stop_locked(struct wi_softc *sc, int disable); 117165087Ssamstatic void wi_start_locked(struct ifnet *); 11891693Simpstatic void wi_start(struct ifnet *); 119160991Ssamstatic int wi_start_tx(struct ifnet *ifp, struct wi_frame *frmhdr, 120160991Ssam struct mbuf *m0); 121160991Ssamstatic int wi_raw_xmit(struct ieee80211_node *, struct mbuf *, 122160991Ssam const struct ieee80211_bpf_params *); 123178354Ssamstatic int wi_newstate_sta(struct ieee80211vap *, enum ieee80211_state, int); 124178354Ssamstatic int wi_newstate_hostap(struct ieee80211vap *, enum ieee80211_state, int); 125178354Ssamstatic void wi_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m, 126178354Ssam int subtype, int rssi, int noise, u_int32_t rstamp); 127178354Ssamstatic int wi_reset(struct wi_softc *); 128165089Ssamstatic void wi_watchdog(void *); 129109323Ssamstatic int wi_ioctl(struct ifnet *, u_long, caddr_t); 130109323Ssamstatic void wi_media_status(struct ifnet *, struct ifmediareq *); 13146492Swpaul 132109323Ssamstatic void wi_rx_intr(struct wi_softc *); 133109323Ssamstatic void wi_tx_intr(struct wi_softc *); 134109323Ssamstatic void wi_tx_ex_intr(struct wi_softc *); 135178354Ssam 136109323Ssamstatic void wi_info_intr(struct wi_softc *); 13746492Swpaul 138178354Ssamstatic int wi_write_txrate(struct wi_softc *, struct ieee80211vap *); 139178354Ssamstatic int wi_write_wep(struct wi_softc *, struct ieee80211vap *); 140109323Ssamstatic int wi_write_multi(struct wi_softc *); 141178354Ssamstatic void wi_update_mcast(struct ifnet *); 142109323Ssamstatic int wi_alloc_fid(struct wi_softc *, int, int *); 143109323Ssamstatic void wi_read_nicid(struct wi_softc *); 144109323Ssamstatic int wi_write_ssid(struct wi_softc *, int, u_int8_t *, int); 14553702Swpaul 146109323Ssamstatic int wi_cmd(struct wi_softc *, int, int, int, int); 147109323Ssamstatic int wi_seek_bap(struct wi_softc *, int, int); 148109323Ssamstatic int wi_read_bap(struct wi_softc *, int, int, void *, int); 149109323Ssamstatic int wi_write_bap(struct wi_softc *, int, int, void *, int); 150109323Ssamstatic int wi_mwrite_bap(struct wi_softc *, int, int, struct mbuf *, int); 151109323Ssamstatic int wi_read_rid(struct wi_softc *, int, void *, int *); 152109323Ssamstatic int wi_write_rid(struct wi_softc *, int, void *, int); 153178354Ssamstatic int wi_write_appie(struct wi_softc *, int, const struct ieee80211_appie *); 15477217Sphk 155170530Ssamstatic void wi_scan_start(struct ieee80211com *); 156170530Ssamstatic void wi_scan_end(struct ieee80211com *); 157170530Ssamstatic void wi_set_channel(struct ieee80211com *); 158170530Ssam 159109323Ssamstatic __inline int 160109323Ssamwi_write_val(struct wi_softc *sc, int rid, u_int16_t val) 161109323Ssam{ 16253702Swpaul 163109323Ssam val = htole16(val); 164109323Ssam return wi_write_rid(sc, rid, &val, sizeof(val)); 165109323Ssam} 166109323Ssam 167109592SsamSYSCTL_NODE(_hw, OID_AUTO, wi, CTLFLAG_RD, 0, "Wireless driver parameters"); 168109592Ssam 169109323Ssamstatic struct timeval lasttxerror; /* time of last tx error msg */ 170109323Ssamstatic int curtxeps; /* current tx error msgs/sec */ 171111559Ssamstatic int wi_txerate = 0; /* tx error rate: max msgs/sec */ 172109592SsamSYSCTL_INT(_hw_wi, OID_AUTO, txerate, CTLFLAG_RW, &wi_txerate, 173111559Ssam 0, "max tx error msgs/sec; 0 to disable msgs"); 174109323Ssam 175109323Ssam#define WI_DEBUG 176109323Ssam#ifdef WI_DEBUG 177109323Ssamstatic int wi_debug = 0; 178109592SsamSYSCTL_INT(_hw_wi, OID_AUTO, debug, CTLFLAG_RW, &wi_debug, 179109592Ssam 0, "control debugging printfs"); 180109323Ssam#define DPRINTF(X) if (wi_debug) printf X 181109323Ssam#else 182109323Ssam#define DPRINTF(X) 183109323Ssam#endif 184109323Ssam 185109323Ssam#define WI_INTRS (WI_EV_RX | WI_EV_ALLOC | WI_EV_INFO) 186109323Ssam 18793825Simpstruct wi_card_ident wi_card_ident[] = { 18893825Simp /* CARD_ID CARD_NAME FIRM_TYPE */ 18993825Simp { WI_NIC_LUCENT_ID, WI_NIC_LUCENT_STR, WI_LUCENT }, 19093825Simp { WI_NIC_SONY_ID, WI_NIC_SONY_STR, WI_LUCENT }, 19193825Simp { WI_NIC_LUCENT_EMB_ID, WI_NIC_LUCENT_EMB_STR, WI_LUCENT }, 19293825Simp { WI_NIC_EVB2_ID, WI_NIC_EVB2_STR, WI_INTERSIL }, 19393825Simp { WI_NIC_HWB3763_ID, WI_NIC_HWB3763_STR, WI_INTERSIL }, 19493825Simp { WI_NIC_HWB3163_ID, WI_NIC_HWB3163_STR, WI_INTERSIL }, 19593825Simp { WI_NIC_HWB3163B_ID, WI_NIC_HWB3163B_STR, WI_INTERSIL }, 19693825Simp { WI_NIC_EVB3_ID, WI_NIC_EVB3_STR, WI_INTERSIL }, 19793825Simp { WI_NIC_HWB1153_ID, WI_NIC_HWB1153_STR, WI_INTERSIL }, 19893825Simp { WI_NIC_P2_SST_ID, WI_NIC_P2_SST_STR, WI_INTERSIL }, 19993825Simp { WI_NIC_EVB2_SST_ID, WI_NIC_EVB2_SST_STR, WI_INTERSIL }, 20093825Simp { WI_NIC_3842_EVA_ID, WI_NIC_3842_EVA_STR, WI_INTERSIL }, 20193825Simp { WI_NIC_3842_PCMCIA_AMD_ID, WI_NIC_3842_PCMCIA_STR, WI_INTERSIL }, 20293825Simp { WI_NIC_3842_PCMCIA_SST_ID, WI_NIC_3842_PCMCIA_STR, WI_INTERSIL }, 203101355Simp { WI_NIC_3842_PCMCIA_ATL_ID, WI_NIC_3842_PCMCIA_STR, WI_INTERSIL }, 204101355Simp { WI_NIC_3842_PCMCIA_ATS_ID, WI_NIC_3842_PCMCIA_STR, WI_INTERSIL }, 20593825Simp { WI_NIC_3842_MINI_AMD_ID, WI_NIC_3842_MINI_STR, WI_INTERSIL }, 20693825Simp { WI_NIC_3842_MINI_SST_ID, WI_NIC_3842_MINI_STR, WI_INTERSIL }, 207101355Simp { WI_NIC_3842_MINI_ATL_ID, WI_NIC_3842_MINI_STR, WI_INTERSIL }, 208101355Simp { WI_NIC_3842_MINI_ATS_ID, WI_NIC_3842_MINI_STR, WI_INTERSIL }, 20993825Simp { WI_NIC_3842_PCI_AMD_ID, WI_NIC_3842_PCI_STR, WI_INTERSIL }, 21093825Simp { WI_NIC_3842_PCI_SST_ID, WI_NIC_3842_PCI_STR, WI_INTERSIL }, 211101355Simp { WI_NIC_3842_PCI_ATS_ID, WI_NIC_3842_PCI_STR, WI_INTERSIL }, 212101355Simp { WI_NIC_3842_PCI_ATL_ID, WI_NIC_3842_PCI_STR, WI_INTERSIL }, 21393825Simp { WI_NIC_P3_PCMCIA_AMD_ID, WI_NIC_P3_PCMCIA_STR, WI_INTERSIL }, 21493825Simp { WI_NIC_P3_PCMCIA_SST_ID, WI_NIC_P3_PCMCIA_STR, WI_INTERSIL }, 215101355Simp { WI_NIC_P3_PCMCIA_ATL_ID, WI_NIC_P3_PCMCIA_STR, WI_INTERSIL }, 216101355Simp { WI_NIC_P3_PCMCIA_ATS_ID, WI_NIC_P3_PCMCIA_STR, WI_INTERSIL }, 21793825Simp { WI_NIC_P3_MINI_AMD_ID, WI_NIC_P3_MINI_STR, WI_INTERSIL }, 21893825Simp { WI_NIC_P3_MINI_SST_ID, WI_NIC_P3_MINI_STR, WI_INTERSIL }, 219101355Simp { WI_NIC_P3_MINI_ATL_ID, WI_NIC_P3_MINI_STR, WI_INTERSIL }, 220101355Simp { WI_NIC_P3_MINI_ATS_ID, WI_NIC_P3_MINI_STR, WI_INTERSIL }, 22193825Simp { 0, NULL, 0 }, 22293825Simp}; 22393825Simp 224180919Simpstatic char *wi_firmware_names[] = { "none", "Hermes", "Intersil", "Symbol" }; 225180919Simp 226109323Ssamdevclass_t wi_devclass; 22746492Swpaul 22893611Simpint 229109323Ssamwi_attach(device_t dev) 23074906Salfred{ 231109323Ssam struct wi_softc *sc = device_get_softc(dev); 232178354Ssam struct ieee80211com *ic; 233147256Sbrooks struct ifnet *ifp; 234116951Ssam int i, nrates, buflen; 235109323Ssam u_int16_t val; 236109323Ssam u_int8_t ratebuf[2 + IEEE80211_RATE_SIZE]; 237116951Ssam struct ieee80211_rateset *rs; 238180919Simp struct sysctl_ctx_list *sctx; 239180919Simp struct sysctl_oid *soid; 240109323Ssam static const u_int8_t empty_macaddr[IEEE80211_ADDR_LEN] = { 241109323Ssam 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 242109323Ssam }; 243109323Ssam int error; 244190526Ssam uint8_t macaddr[IEEE80211_ADDR_LEN]; 24574906Salfred 246178354Ssam ifp = sc->sc_ifp = if_alloc(IFT_IEEE80211); 247147256Sbrooks if (ifp == NULL) { 248147256Sbrooks device_printf(dev, "can not if_alloc\n"); 249147256Sbrooks wi_free(dev); 250178354Ssam return ENOSPC; 251147256Sbrooks } 252178354Ssam ic = ifp->if_l2com; 253147256Sbrooks 254138571Ssam sc->sc_firmware_type = WI_NOTYPE; 255123339Simp sc->wi_cmd_count = 500; 25646492Swpaul /* Reset the NIC. */ 257178354Ssam if (wi_reset(sc) != 0) { 258178354Ssam wi_free(dev); 259109323Ssam return ENXIO; /* XXX */ 260178354Ssam } 26146492Swpaul 262178354Ssam /* Read NIC identification */ 263178354Ssam wi_read_nicid(sc); 264178354Ssam switch (sc->sc_firmware_type) { 265178354Ssam case WI_LUCENT: 266178354Ssam if (sc->sc_sta_firmware_ver < 60006) 267178354Ssam goto reject; 268178354Ssam break; 269178354Ssam case WI_INTERSIL: 270178354Ssam if (sc->sc_sta_firmware_ver < 800) 271178354Ssam goto reject; 272178354Ssam break; 273178354Ssam default: 274178354Ssam reject: 275178354Ssam device_printf(dev, "Sorry, this card is not supported " 276178354Ssam "(type %d, firmware ver %d)\n", 277178354Ssam sc->sc_firmware_type, sc->sc_sta_firmware_ver); 278178354Ssam wi_free(dev); 279178354Ssam return EOPNOTSUPP; 280178354Ssam } 281178354Ssam 282180919Simp /* Export info about the device via sysctl */ 283180919Simp sctx = device_get_sysctl_ctx(dev); 284180919Simp soid = device_get_sysctl_tree(dev); 285180919Simp SYSCTL_ADD_STRING(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, 286180919Simp "firmware_type", CTLFLAG_RD, 287180919Simp wi_firmware_names[sc->sc_firmware_type], 0, 288180919Simp "Firmware type string"); 289180919Simp SYSCTL_ADD_INT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "sta_version", 290180919Simp CTLFLAG_RD, &sc->sc_sta_firmware_ver, 0, 291180919Simp "Station Firmware version"); 292180919Simp if (sc->sc_firmware_type == WI_INTERSIL) 293180919Simp SYSCTL_ADD_INT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, 294180919Simp "pri_version", CTLFLAG_RD, &sc->sc_pri_firmware_ver, 0, 295180919Simp "Primary Firmware version"); 296180919Simp SYSCTL_ADD_XINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "nic_id", 297180919Simp CTLFLAG_RD, &sc->sc_nic_id, 0, "NIC id"); 298180919Simp SYSCTL_ADD_STRING(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "nic_name", 299180919Simp CTLFLAG_RD, sc->sc_nic_name, 0, "NIC name"); 300180919Simp 301178354Ssam mtx_init(&sc->sc_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK, 302178354Ssam MTX_DEF | MTX_RECURSE); 303178354Ssam callout_init_mtx(&sc->sc_watchdog, &sc->sc_mtx, 0); 304178354Ssam 30576438Swpaul /* 30676438Swpaul * Read the station address. 30776438Swpaul * And do it twice. I've seen PRISM-based cards that return 30876438Swpaul * an error when trying to read it the first time, which causes 30976438Swpaul * the probe to fail. 31076438Swpaul */ 311109323Ssam buflen = IEEE80211_ADDR_LEN; 312190526Ssam error = wi_read_rid(sc, WI_RID_MAC_NODE, macaddr, &buflen); 313109323Ssam if (error != 0) { 314109323Ssam buflen = IEEE80211_ADDR_LEN; 315190526Ssam error = wi_read_rid(sc, WI_RID_MAC_NODE, macaddr, &buflen); 316109323Ssam } 317190526Ssam if (error || IEEE80211_ADDR_EQ(macaddr, empty_macaddr)) { 318109323Ssam if (error != 0) 319109323Ssam device_printf(dev, "mac read failed %d\n", error); 320148714Simp else { 321109323Ssam device_printf(dev, "mac read failed (all zeros)\n"); 322148714Simp error = ENXIO; 323148714Simp } 32475149Simp wi_free(dev); 32575149Simp return (error); 32675149Simp } 32746492Swpaul 328178354Ssam ifp->if_softc = sc; 329121816Sbrooks if_initname(ifp, device_get_name(dev), device_get_unit(dev)); 33046492Swpaul ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 33146492Swpaul ifp->if_ioctl = wi_ioctl; 33246492Swpaul ifp->if_start = wi_start; 33346492Swpaul ifp->if_init = wi_init; 334132986Smlaier IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN); 335132986Smlaier ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN; 336132986Smlaier IFQ_SET_READY(&ifp->if_snd); 33746492Swpaul 338138571Ssam ic->ic_ifp = ifp; 339109323Ssam ic->ic_phytype = IEEE80211_T_DS; 340109323Ssam ic->ic_opmode = IEEE80211_M_STA; 341178957Ssam ic->ic_caps = IEEE80211_C_STA 342178957Ssam | IEEE80211_C_PMGT 343178354Ssam | IEEE80211_C_MONITOR 344138571Ssam ; 34546492Swpaul 346116951Ssam /* 347116951Ssam * Query the card for available channels and setup the 348116951Ssam * channel table. We assume these are all 11b channels. 349116951Ssam */ 350109323Ssam buflen = sizeof(val); 351109323Ssam if (wi_read_rid(sc, WI_RID_CHANNEL_LIST, &val, &buflen) != 0) 352109323Ssam val = htole16(0x1fff); /* assume 1-11 */ 353116951Ssam KASSERT(val != 0, ("wi_attach: no available channels listed!")); 354116951Ssam 355116951Ssam val <<= 1; /* shift for base 1 indices */ 356116951Ssam for (i = 1; i < 16; i++) { 357170530Ssam struct ieee80211_channel *c; 358170530Ssam 359144986Smdodd if (!isset((u_int8_t*)&val, i)) 360144986Smdodd continue; 361170530Ssam c = &ic->ic_channels[ic->ic_nchans++]; 362170530Ssam c->ic_freq = ieee80211_ieee2mhz(i, IEEE80211_CHAN_B); 363170530Ssam c->ic_flags = IEEE80211_CHAN_B; 364170530Ssam c->ic_ieee = i; 365178354Ssam /* XXX txpowers? */ 366109323Ssam } 36746492Swpaul 36846563Swpaul /* 36998440Simp * Set flags based on firmware version. 37098440Simp */ 37198440Simp switch (sc->sc_firmware_type) { 37298440Simp case WI_LUCENT: 373112363Simp sc->sc_ntxbuf = 1; 374178354Ssam ic->ic_caps |= IEEE80211_C_IBSS; 375119784Ssam 376178354Ssam sc->sc_ibss_port = WI_PORTTYPE_BSS; 377178354Ssam sc->sc_monitor_port = WI_PORTTYPE_ADHOC; 378119784Ssam sc->sc_min_rssi = WI_LUCENT_MIN_RSSI; 379119784Ssam sc->sc_max_rssi = WI_LUCENT_MAX_RSSI; 380119784Ssam sc->sc_dbm_offset = WI_LUCENT_DBM_OFFSET; 38198440Simp break; 38298440Simp case WI_INTERSIL: 383112363Simp sc->sc_ntxbuf = WI_NTXBUF; 384178354Ssam sc->sc_flags |= WI_FLAGS_HAS_FRAGTHR 385178354Ssam | WI_FLAGS_HAS_ROAMING; 386123339Simp /* 387123339Simp * Old firmware are slow, so give peace a chance. 388123339Simp */ 389123339Simp if (sc->sc_sta_firmware_ver < 10000) 390123339Simp sc->wi_cmd_count = 5000; 391109323Ssam if (sc->sc_sta_firmware_ver > 10101) 392109323Ssam sc->sc_flags |= WI_FLAGS_HAS_DBMADJUST; 393178354Ssam ic->ic_caps |= IEEE80211_C_IBSS; 394109396Simp /* 395109396Simp * version 0.8.3 and newer are the only ones that are known 396109396Simp * to currently work. Earlier versions can be made to work, 397178354Ssam * at least according to the Linux driver but we require 398178354Ssam * monitor mode so this is irrelevant. 399109396Simp */ 400178354Ssam ic->ic_caps |= IEEE80211_C_HOSTAP; 401178354Ssam if (sc->sc_sta_firmware_ver >= 10603) 402178354Ssam sc->sc_flags |= WI_FLAGS_HAS_ENHSECURITY; 403178354Ssam if (sc->sc_sta_firmware_ver >= 10700) { 404178354Ssam /* 405178354Ssam * 1.7.0+ have the necessary support for sta mode WPA. 406178354Ssam */ 407178354Ssam sc->sc_flags |= WI_FLAGS_HAS_WPASUPPORT; 408178354Ssam ic->ic_caps |= IEEE80211_C_WPA; 409178354Ssam } 410119784Ssam 411178354Ssam sc->sc_ibss_port = WI_PORTTYPE_IBSS; 412178354Ssam sc->sc_monitor_port = WI_PORTTYPE_APSILENT; 413119784Ssam sc->sc_min_rssi = WI_PRISM_MIN_RSSI; 414119784Ssam sc->sc_max_rssi = WI_PRISM_MAX_RSSI; 415119784Ssam sc->sc_dbm_offset = WI_PRISM_DBM_OFFSET; 41698440Simp break; 41798440Simp } 41898440Simp 41998440Simp /* 42056965Swpaul * Find out if we support WEP on this card. 42156965Swpaul */ 422109323Ssam buflen = sizeof(val); 423109323Ssam if (wi_read_rid(sc, WI_RID_WEP_AVAIL, &val, &buflen) == 0 && 424109323Ssam val != htole16(0)) 425178354Ssam ic->ic_cryptocaps |= IEEE80211_CRYPTO_WEP; 42656965Swpaul 427109323Ssam /* Find supported rates. */ 428109323Ssam buflen = sizeof(ratebuf); 429116951Ssam rs = &ic->ic_sup_rates[IEEE80211_MODE_11B]; 430109323Ssam if (wi_read_rid(sc, WI_RID_DATA_RATES, ratebuf, &buflen) == 0) { 431116951Ssam nrates = le16toh(*(u_int16_t *)ratebuf); 432116951Ssam if (nrates > IEEE80211_RATE_MAXSIZE) 433116951Ssam nrates = IEEE80211_RATE_MAXSIZE; 434116951Ssam rs->rs_nrates = 0; 435116951Ssam for (i = 0; i < nrates; i++) 436116951Ssam if (ratebuf[2+i]) 437116951Ssam rs->rs_rates[rs->rs_nrates++] = ratebuf[2+i]; 438109323Ssam } else { 439109323Ssam /* XXX fallback on error? */ 44098440Simp } 44177217Sphk 442109323Ssam buflen = sizeof(val); 443109323Ssam if ((sc->sc_flags & WI_FLAGS_HAS_DBMADJUST) && 444109323Ssam wi_read_rid(sc, WI_RID_DBM_ADJUST, &val, &buflen) == 0) { 445119784Ssam sc->sc_dbm_offset = le16toh(val); 446119784Ssam } 44746492Swpaul 448109323Ssam sc->sc_portnum = WI_DEFAULT_PORT; 44987383Simp 450190526Ssam ieee80211_ifattach(ic, macaddr); 451160991Ssam ic->ic_raw_xmit = wi_raw_xmit; 452170530Ssam ic->ic_scan_start = wi_scan_start; 453170530Ssam ic->ic_scan_end = wi_scan_end; 454170530Ssam ic->ic_set_channel = wi_set_channel; 455170530Ssam 456178354Ssam ic->ic_vap_create = wi_vap_create; 457178354Ssam ic->ic_vap_delete = wi_vap_delete; 458178354Ssam ic->ic_update_mcast = wi_update_mcast; 459109323Ssam 460178354Ssam bpfattach(ifp, DLT_IEEE802_11_RADIO, 461178354Ssam sizeof(struct ieee80211_frame) + sizeof(sc->sc_tx_th)); 462119784Ssam /* 463119784Ssam * Initialize constant fields. 464127698Ssam * XXX make header lengths a multiple of 32-bits so subsequent 465127698Ssam * headers are properly aligned; this is a kludge to keep 466127698Ssam * certain applications happy. 467119784Ssam * 468119784Ssam * NB: the channel is setup each time we transition to the 469119784Ssam * RUN state to avoid filling it in for each frame. 470119784Ssam */ 471127698Ssam sc->sc_tx_th_len = roundup(sizeof(sc->sc_tx_th), sizeof(u_int32_t)); 472127698Ssam sc->sc_tx_th.wt_ihdr.it_len = htole16(sc->sc_tx_th_len); 473127698Ssam sc->sc_tx_th.wt_ihdr.it_present = htole32(WI_TX_RADIOTAP_PRESENT); 474119784Ssam 475127698Ssam sc->sc_rx_th_len = roundup(sizeof(sc->sc_rx_th), sizeof(u_int32_t)); 476127698Ssam sc->sc_rx_th.wr_ihdr.it_len = htole16(sc->sc_rx_th_len); 477127698Ssam sc->sc_rx_th.wr_ihdr.it_present = htole32(WI_RX_RADIOTAP_PRESENT); 478138571Ssam 479138571Ssam if (bootverbose) 480138571Ssam ieee80211_announce(ic); 481138571Ssam 482180826Simp error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET | INTR_MPSAFE, 483180826Simp NULL, wi_intr, sc, &sc->wi_intrhand); 484180826Simp if (error) { 485180826Simp device_printf(dev, "bus_setup_intr() failed! (%d)\n", error); 486180826Simp bpfdetach(ifp); 487180826Simp ieee80211_ifdetach(ic); 488180826Simp if_free(sc->sc_ifp); 489180826Simp wi_free(dev); 490180826Simp return error; 491180826Simp } 492180826Simp 493109323Ssam return (0); 49487383Simp} 49587383Simp 496109323Ssamint 497109323Ssamwi_detach(device_t dev) 49846492Swpaul{ 499109323Ssam struct wi_softc *sc = device_get_softc(dev); 500147256Sbrooks struct ifnet *ifp = sc->sc_ifp; 501178354Ssam struct ieee80211com *ic = ifp->if_l2com; 50246492Swpaul 503109323Ssam WI_LOCK(sc); 50446492Swpaul 505109323Ssam /* check if device was removed */ 506123098Simp sc->wi_gone |= !bus_child_present(dev); 50746492Swpaul 508178354Ssam wi_stop_locked(sc, 0); 509150678Sru WI_UNLOCK(sc); 510178354Ssam bpfdetach(ifp); 511178354Ssam ieee80211_ifdetach(ic); 51246492Swpaul 513109323Ssam bus_teardown_intr(dev, sc->irq, sc->wi_intrhand); 514150306Simp if_free(sc->sc_ifp); 515109323Ssam wi_free(dev); 516109323Ssam mtx_destroy(&sc->sc_mtx); 517109323Ssam return (0); 518109323Ssam} 51993359Simp 520178354Ssamstatic struct ieee80211vap * 521178354Ssamwi_vap_create(struct ieee80211com *ic, 522178354Ssam const char name[IFNAMSIZ], int unit, int opmode, int flags, 523178354Ssam const uint8_t bssid[IEEE80211_ADDR_LEN], 524178354Ssam const uint8_t mac[IEEE80211_ADDR_LEN]) 525109323Ssam{ 526178354Ssam struct wi_softc *sc = ic->ic_ifp->if_softc; 527178354Ssam struct wi_vap *wvp; 528178354Ssam struct ieee80211vap *vap; 52993359Simp 530178354Ssam if (!TAILQ_EMPTY(&ic->ic_vaps)) /* only one at a time */ 531178354Ssam return NULL; 532178354Ssam wvp = (struct wi_vap *) malloc(sizeof(struct wi_vap), 533178354Ssam M_80211_VAP, M_NOWAIT | M_ZERO); 534178354Ssam if (wvp == NULL) 535178354Ssam return NULL; 53693359Simp 537178354Ssam vap = &wvp->wv_vap; 538178354Ssam ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid, mac); 53993359Simp 540178354Ssam vap->iv_max_aid = WI_MAX_AID; 54193359Simp 542178354Ssam switch (opmode) { 543178354Ssam case IEEE80211_M_STA: 544178354Ssam sc->sc_porttype = WI_PORTTYPE_BSS; 545178354Ssam wvp->wv_newstate = vap->iv_newstate; 546178354Ssam vap->iv_newstate = wi_newstate_sta; 547178354Ssam /* need to filter mgt frames to avoid confusing state machine */ 548178354Ssam wvp->wv_recv_mgmt = vap->iv_recv_mgmt; 549178354Ssam vap->iv_recv_mgmt = wi_recv_mgmt; 550109323Ssam break; 551178354Ssam case IEEE80211_M_IBSS: 552178354Ssam sc->sc_porttype = sc->sc_ibss_port; 553178354Ssam wvp->wv_newstate = vap->iv_newstate; 554178354Ssam vap->iv_newstate = wi_newstate_sta; 555109323Ssam break; 556178354Ssam case IEEE80211_M_AHDEMO: 557178354Ssam sc->sc_porttype = WI_PORTTYPE_ADHOC; 558109323Ssam break; 559178354Ssam case IEEE80211_M_HOSTAP: 560178354Ssam sc->sc_porttype = WI_PORTTYPE_HOSTAP; 561178354Ssam wvp->wv_newstate = vap->iv_newstate; 562178354Ssam vap->iv_newstate = wi_newstate_hostap; 563178354Ssam break; 564178354Ssam case IEEE80211_M_MONITOR: 565178354Ssam sc->sc_porttype = sc->sc_monitor_port; 566178354Ssam break; 567178354Ssam default: 568178354Ssam break; 56993359Simp } 570178354Ssam 571178354Ssam /* complete setup */ 572178354Ssam ieee80211_vap_attach(vap, ieee80211_media_change, wi_media_status); 573178354Ssam ic->ic_opmode = opmode; 574178354Ssam return vap; 57546492Swpaul} 57646492Swpaul 577178354Ssamstatic void 578178354Ssamwi_vap_delete(struct ieee80211vap *vap) 579178354Ssam{ 580178354Ssam struct wi_vap *wvp = WI_VAP(vap); 581178354Ssam 582178354Ssam ieee80211_vap_detach(vap); 583178354Ssam free(wvp, M_80211_VAP); 584178354Ssam} 585178354Ssam 586109323Ssamvoid 587109323Ssamwi_shutdown(device_t dev) 58846492Swpaul{ 589109323Ssam struct wi_softc *sc = device_get_softc(dev); 59046492Swpaul 591178354Ssam wi_stop(sc, 1); 59246492Swpaul} 59346492Swpaul 594109323Ssamvoid 595109323Ssamwi_intr(void *arg) 59646492Swpaul{ 597109323Ssam struct wi_softc *sc = arg; 598147256Sbrooks struct ifnet *ifp = sc->sc_ifp; 599112363Simp u_int16_t status; 60046492Swpaul 601109323Ssam WI_LOCK(sc); 60246492Swpaul 603123098Simp if (sc->wi_gone || !sc->sc_enabled || (ifp->if_flags & IFF_UP) == 0) { 604113327Simp CSR_WRITE_2(sc, WI_INT_EN, 0); 605123098Simp CSR_WRITE_2(sc, WI_EVENT_ACK, 0xFFFF); 606109323Ssam WI_UNLOCK(sc); 60746492Swpaul return; 608109323Ssam } 60946492Swpaul 610112363Simp /* Disable interrupts. */ 611112363Simp CSR_WRITE_2(sc, WI_INT_EN, 0); 61246492Swpaul 613112363Simp status = CSR_READ_2(sc, WI_EVENT_STAT); 614112363Simp if (status & WI_EV_RX) 615112363Simp wi_rx_intr(sc); 616112363Simp if (status & WI_EV_ALLOC) 617112363Simp wi_tx_intr(sc); 618112363Simp if (status & WI_EV_TX_EXC) 619112363Simp wi_tx_ex_intr(sc); 620112363Simp if (status & WI_EV_INFO) 621112363Simp wi_info_intr(sc); 622148887Srwatson if ((ifp->if_drv_flags & IFF_DRV_OACTIVE) == 0 && 623132986Smlaier !IFQ_DRV_IS_EMPTY(&ifp->if_snd)) 624165087Ssam wi_start_locked(ifp); 62546492Swpaul 626112363Simp /* Re-enable interrupts. */ 627112363Simp CSR_WRITE_2(sc, WI_INT_EN, WI_INTRS); 62846492Swpaul 629109323Ssam WI_UNLOCK(sc); 63046492Swpaul 63146492Swpaul return; 63246492Swpaul} 63346492Swpaul 634178354Ssamstatic void 635178354Ssamwi_enable(struct wi_softc *sc) 636178354Ssam{ 637178354Ssam /* Enable interrupts */ 638178354Ssam CSR_WRITE_2(sc, WI_INT_EN, WI_INTRS); 639178354Ssam 640178354Ssam /* enable port */ 641178354Ssam wi_cmd(sc, WI_CMD_ENABLE | sc->sc_portnum, 0, 0, 0); 642178354Ssam sc->sc_enabled = 1; 643178354Ssam} 644178354Ssam 645178354Ssamstatic int 646178354Ssamwi_setup_locked(struct wi_softc *sc, int porttype, int mode, 647178354Ssam uint8_t mac[IEEE80211_ADDR_LEN]) 648178354Ssam{ 649178354Ssam int i; 650178354Ssam 651178354Ssam wi_reset(sc); 652178354Ssam 653178354Ssam wi_write_val(sc, WI_RID_PORTTYPE, porttype); 654178354Ssam wi_write_val(sc, WI_RID_CREATE_IBSS, mode); 655178354Ssam wi_write_val(sc, WI_RID_MAX_DATALEN, 2304); 656178354Ssam /* XXX IEEE80211_BPF_NOACK wants 0 */ 657178354Ssam wi_write_val(sc, WI_RID_ALT_RETRY_CNT, 2); 658178354Ssam if (sc->sc_flags & WI_FLAGS_HAS_ROAMING) 659178354Ssam wi_write_val(sc, WI_RID_ROAMING_MODE, 3); /* NB: disabled */ 660178354Ssam 661178354Ssam wi_write_rid(sc, WI_RID_MAC_NODE, mac, IEEE80211_ADDR_LEN); 662178354Ssam 663178354Ssam /* Allocate fids for the card */ 664178354Ssam sc->sc_buflen = IEEE80211_MAX_LEN + sizeof(struct wi_frame); 665178354Ssam for (i = 0; i < sc->sc_ntxbuf; i++) { 666178354Ssam int error = wi_alloc_fid(sc, sc->sc_buflen, 667178354Ssam &sc->sc_txd[i].d_fid); 668178354Ssam if (error) { 669178354Ssam device_printf(sc->sc_dev, 670178354Ssam "tx buffer allocation failed (error %u)\n", 671178354Ssam error); 672178354Ssam return error; 673178354Ssam } 674178354Ssam sc->sc_txd[i].d_len = 0; 675178354Ssam } 676178354Ssam sc->sc_txcur = sc->sc_txnext = 0; 677178354Ssam 678178354Ssam return 0; 679178354Ssam} 680178354Ssam 681178354Ssamstatic void 682178354Ssamwi_init_locked(struct wi_softc *sc) 683178354Ssam{ 684178354Ssam struct ifnet *ifp = sc->sc_ifp; 685178354Ssam int wasenabled; 686178354Ssam 687178354Ssam WI_LOCK_ASSERT(sc); 688178354Ssam 689178354Ssam wasenabled = sc->sc_enabled; 690178354Ssam if (wasenabled) 691178354Ssam wi_stop_locked(sc, 1); 692178354Ssam 693190526Ssam if (wi_setup_locked(sc, sc->sc_porttype, 3, IF_LLADDR(ifp)) != 0) { 694178354Ssam if_printf(ifp, "interface not running\n"); 695178354Ssam wi_stop_locked(sc, 1); 696178354Ssam return; 697178354Ssam } 698178354Ssam 699178354Ssam ifp->if_drv_flags |= IFF_DRV_RUNNING; 700178354Ssam ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 701178354Ssam 702178354Ssam callout_reset(&sc->sc_watchdog, hz, wi_watchdog, sc); 703178354Ssam 704178354Ssam wi_enable(sc); /* Enable desired port */ 705178354Ssam} 706178354Ssam 707109323Ssamvoid 708109323Ssamwi_init(void *arg) 70946492Swpaul{ 710109323Ssam struct wi_softc *sc = arg; 711147256Sbrooks struct ifnet *ifp = sc->sc_ifp; 712178354Ssam struct ieee80211com *ic = ifp->if_l2com; 71346492Swpaul 714178354Ssam WI_LOCK(sc); 715178354Ssam wi_init_locked(sc); 716178354Ssam WI_UNLOCK(sc); 71767092Swpaul 718178931Sthompsa if (ifp->if_drv_flags & IFF_DRV_RUNNING) 719178931Sthompsa ieee80211_start_all(ic); /* start all vap's */ 720178354Ssam} 721170530Ssam 722178354Ssamstatic void 723178354Ssamwi_stop_locked(struct wi_softc *sc, int disable) 724178354Ssam{ 725178354Ssam struct ifnet *ifp = sc->sc_ifp; 72646492Swpaul 727178354Ssam WI_LOCK_ASSERT(sc); 72846492Swpaul 729178354Ssam if (sc->sc_enabled && !sc->wi_gone) { 730178354Ssam CSR_WRITE_2(sc, WI_INT_EN, 0); 731178354Ssam wi_cmd(sc, WI_CMD_DISABLE | sc->sc_portnum, 0, 0, 0); 732178354Ssam if (disable) 733178354Ssam sc->sc_enabled = 0; 734178354Ssam } else if (sc->wi_gone && disable) /* gone --> not enabled */ 735178354Ssam sc->sc_enabled = 0; 736178354Ssam 737178354Ssam callout_stop(&sc->sc_watchdog); 738178354Ssam sc->sc_tx_timer = 0; 739178354Ssam sc->sc_false_syns = 0; 740178354Ssam 741178354Ssam ifp->if_drv_flags &= ~(IFF_DRV_OACTIVE | IFF_DRV_RUNNING); 742178354Ssam} 743178354Ssam 744178354Ssamvoid 745178354Ssamwi_stop(struct wi_softc *sc, int disable) 746178354Ssam{ 747170530Ssam WI_LOCK(sc); 748178354Ssam wi_stop_locked(sc, disable); 749178354Ssam WI_UNLOCK(sc); 750178354Ssam} 751170530Ssam 752178354Ssamstatic void 753178354Ssamwi_set_channel(struct ieee80211com *ic) 754178354Ssam{ 755178354Ssam struct ifnet *ifp = ic->ic_ifp; 756178354Ssam struct wi_softc *sc = ifp->if_softc; 757160991Ssam 758178354Ssam DPRINTF(("%s: channel %d, %sscanning\n", __func__, 759178354Ssam ieee80211_chan2ieee(ic, ic->ic_curchan), 760178354Ssam ic->ic_flags & IEEE80211_F_SCAN ? "" : "!")); 76146492Swpaul 762178354Ssam WI_LOCK(sc); 763116951Ssam wi_write_val(sc, WI_RID_OWN_CHNL, 764178354Ssam ieee80211_chan2ieee(ic, ic->ic_curchan)); 76546492Swpaul 766178354Ssam sc->sc_tx_th.wt_chan_freq = sc->sc_rx_th.wr_chan_freq = 767178354Ssam htole16(ic->ic_curchan->ic_freq); 768178354Ssam sc->sc_tx_th.wt_chan_flags = sc->sc_rx_th.wr_chan_flags = 769178354Ssam htole16(ic->ic_curchan->ic_flags); 770178354Ssam WI_UNLOCK(sc); 771178354Ssam} 77275373Salfred 773178354Ssamstatic void 774178354Ssamwi_scan_start(struct ieee80211com *ic) 775178354Ssam{ 776178354Ssam struct ifnet *ifp = ic->ic_ifp; 777178354Ssam struct wi_softc *sc = ifp->if_softc; 778178354Ssam struct ieee80211_scan_state *ss = ic->ic_scan; 77946492Swpaul 780178354Ssam DPRINTF(("%s\n", __func__)); 78146492Swpaul 782178354Ssam WI_LOCK(sc); 783109323Ssam /* 784178354Ssam * Switch device to monitor mode. 785109323Ssam */ 786178354Ssam wi_write_val(sc, WI_RID_PORTTYPE, sc->sc_monitor_port); 787178354Ssam if (sc->sc_firmware_type == WI_INTERSIL) { 788178354Ssam wi_cmd(sc, WI_CMD_DISABLE | WI_PORT0, 0, 0, 0); 789178354Ssam wi_cmd(sc, WI_CMD_ENABLE | WI_PORT0, 0, 0, 0); 79075373Salfred } 791178354Ssam /* force full dwell time to compensate for firmware overhead */ 792178354Ssam ss->ss_mindwell = ss->ss_maxdwell = msecs_to_ticks(400); 793178354Ssam WI_UNLOCK(sc); 79446492Swpaul 795178354Ssam} 79667092Swpaul 797178354Ssamstatic void 798178354Ssamwi_scan_end(struct ieee80211com *ic) 799178354Ssam{ 800178354Ssam struct ifnet *ifp = ic->ic_ifp; 801178354Ssam struct wi_softc *sc = ifp->if_softc; 80246492Swpaul 803178354Ssam DPRINTF(("%s: restore port type %d\n", __func__, sc->sc_porttype)); 804178354Ssam 805178354Ssam WI_LOCK(sc); 806178354Ssam wi_write_val(sc, WI_RID_PORTTYPE, sc->sc_porttype); 807178354Ssam if (sc->sc_firmware_type == WI_INTERSIL) { 808178354Ssam wi_cmd(sc, WI_CMD_DISABLE | WI_PORT0, 0, 0, 0); 809178354Ssam wi_cmd(sc, WI_CMD_ENABLE | WI_PORT0, 0, 0, 0); 81070073Swpaul } 811178354Ssam WI_UNLOCK(sc); 812178354Ssam} 81370073Swpaul 814178354Ssamstatic void 815178354Ssamwi_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m, 816178354Ssam int subtype, int rssi, int noise, u_int32_t rstamp) 817178354Ssam{ 818178354Ssam struct ieee80211vap *vap = ni->ni_vap; 81946492Swpaul 820178354Ssam switch (subtype) { 821178354Ssam case IEEE80211_FC0_SUBTYPE_AUTH: 822178354Ssam case IEEE80211_FC0_SUBTYPE_ASSOC_RESP: 823178354Ssam case IEEE80211_FC0_SUBTYPE_REASSOC_RESP: 824178354Ssam /* NB: filter frames that trigger state changes */ 825178354Ssam return; 826170530Ssam } 827178354Ssam WI_VAP(vap)->wv_recv_mgmt(ni, m, subtype, rssi, noise, rstamp); 828178354Ssam} 82946492Swpaul 830178354Ssamstatic int 831178354Ssamwi_newstate_sta(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg) 832178354Ssam{ 833178354Ssam struct ieee80211com *ic = vap->iv_ic; 834178354Ssam struct ifnet *ifp = ic->ic_ifp; 835178354Ssam struct ieee80211_node *bss; 836178354Ssam struct wi_softc *sc = ifp->if_softc; 83794397Simp 838178354Ssam DPRINTF(("%s: %s -> %s\n", __func__, 839178354Ssam ieee80211_state_name[vap->iv_state], 840178354Ssam ieee80211_state_name[nstate])); 841178354Ssam 842178354Ssam if (nstate == IEEE80211_S_AUTH) { 843178354Ssam WI_LOCK(sc); 844178354Ssam wi_setup_locked(sc, WI_PORTTYPE_BSS, 3, vap->iv_myaddr); 845178354Ssam 846178354Ssam if (vap->iv_flags & IEEE80211_F_PMGTON) { 847178354Ssam wi_write_val(sc, WI_RID_MAX_SLEEP, ic->ic_lintval); 848178354Ssam wi_write_val(sc, WI_RID_PM_ENABLED, 1); 849178354Ssam } 850178354Ssam wi_write_val(sc, WI_RID_RTS_THRESH, vap->iv_rtsthreshold); 851178354Ssam if (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR) 852178354Ssam wi_write_val(sc, WI_RID_FRAG_THRESH, 853178354Ssam vap->iv_fragthreshold); 854178354Ssam wi_write_txrate(sc, vap); 855178354Ssam 856178354Ssam bss = vap->iv_bss; 857178354Ssam wi_write_ssid(sc, WI_RID_DESIRED_SSID, bss->ni_essid, bss->ni_esslen); 858178354Ssam wi_write_val(sc, WI_RID_OWN_CHNL, 859178354Ssam ieee80211_chan2ieee(ic, bss->ni_chan)); 860178354Ssam 861178354Ssam /* Configure WEP. */ 862178354Ssam if (ic->ic_cryptocaps & IEEE80211_CRYPTO_WEP) 863178354Ssam wi_write_wep(sc, vap); 864178354Ssam else 865178354Ssam sc->sc_encryption = 0; 866178354Ssam 867178354Ssam if ((sc->sc_flags & WI_FLAGS_HAS_WPASUPPORT) && 868178354Ssam (vap->iv_flags & IEEE80211_F_WPA)) { 869178354Ssam wi_write_val(sc, WI_RID_WPA_HANDLING, 1); 870178354Ssam if (vap->iv_appie_wpa != NULL) 871178354Ssam wi_write_appie(sc, WI_RID_WPA_DATA, 872178354Ssam vap->iv_appie_wpa); 873178354Ssam } 874178354Ssam 875178354Ssam wi_enable(sc); /* enable port */ 876178354Ssam 877178354Ssam /* Lucent firmware does not support the JOIN RID. */ 878178354Ssam if (sc->sc_firmware_type == WI_INTERSIL) { 879178354Ssam struct wi_joinreq join; 880178354Ssam 881178354Ssam memset(&join, 0, sizeof(join)); 882178354Ssam IEEE80211_ADDR_COPY(&join.wi_bssid, bss->ni_bssid); 883116951Ssam join.wi_chan = htole16( 884178354Ssam ieee80211_chan2ieee(ic, bss->ni_chan)); 885109323Ssam wi_write_rid(sc, WI_RID_JOIN_REQ, &join, sizeof(join)); 886178354Ssam } 887178354Ssam WI_UNLOCK(sc); 88875373Salfred 889178354Ssam /* 890178354Ssam * NB: don't go through 802.11 layer, it'll send auth frame; 891178354Ssam * instead we drive the state machine from the link status 892178354Ssam * notification we get on association. 893178354Ssam */ 894178354Ssam vap->iv_state = nstate; 895191746Sthompsa return (0); 89670073Swpaul } 897178354Ssam return WI_VAP(vap)->wv_newstate(vap, nstate, arg); 89846492Swpaul} 89946492Swpaul 900178354Ssamstatic int 901178354Ssamwi_newstate_hostap(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg) 90246492Swpaul{ 903178354Ssam struct ieee80211com *ic = vap->iv_ic; 904178354Ssam struct ifnet *ifp = ic->ic_ifp; 905178354Ssam struct ieee80211_node *bss; 906109323Ssam struct wi_softc *sc = ifp->if_softc; 907178354Ssam int error; 90846492Swpaul 909178354Ssam DPRINTF(("%s: %s -> %s\n", __func__, 910178354Ssam ieee80211_state_name[vap->iv_state], 911178354Ssam ieee80211_state_name[nstate])); 91274998Swpaul 913178354Ssam error = WI_VAP(vap)->wv_newstate(vap, nstate, arg); 914178354Ssam if (error == 0 && nstate == IEEE80211_S_RUN) { 915178354Ssam WI_LOCK(sc); 916178354Ssam wi_setup_locked(sc, WI_PORTTYPE_HOSTAP, 0, vap->iv_myaddr); 917178354Ssam 918178354Ssam bss = vap->iv_bss; 919178354Ssam wi_write_ssid(sc, WI_RID_OWN_SSID, 920178354Ssam bss->ni_essid, bss->ni_esslen); 921178354Ssam wi_write_val(sc, WI_RID_OWN_CHNL, 922178354Ssam ieee80211_chan2ieee(ic, bss->ni_chan)); 923178354Ssam wi_write_val(sc, WI_RID_BASIC_RATE, 0x3); 924178354Ssam wi_write_val(sc, WI_RID_SUPPORT_RATE, 0xf); 925178354Ssam wi_write_txrate(sc, vap); 926178354Ssam 927178354Ssam wi_write_val(sc, WI_RID_OWN_BEACON_INT, bss->ni_intval); 928178354Ssam wi_write_val(sc, WI_RID_DTIM_PERIOD, vap->iv_dtim_period); 929178354Ssam 930178354Ssam wi_write_val(sc, WI_RID_RTS_THRESH, vap->iv_rtsthreshold); 931178354Ssam if (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR) 932178354Ssam wi_write_val(sc, WI_RID_FRAG_THRESH, 933178354Ssam vap->iv_fragthreshold); 934178354Ssam 935178354Ssam if ((sc->sc_flags & WI_FLAGS_HAS_ENHSECURITY) && 936178354Ssam (vap->iv_flags & IEEE80211_F_HIDESSID)) { 937178354Ssam /* 938178354Ssam * bit 0 means hide SSID in beacons, 939178354Ssam * bit 1 means don't respond to bcast probe req 940178354Ssam */ 941178354Ssam wi_write_val(sc, WI_RID_ENH_SECURITY, 0x3); 94270073Swpaul } 94370073Swpaul 944178354Ssam if ((sc->sc_flags & WI_FLAGS_HAS_WPASUPPORT) && 945178354Ssam (vap->iv_flags & IEEE80211_F_WPA) && 946178354Ssam vap->iv_appie_wpa != NULL) 947178354Ssam wi_write_appie(sc, WI_RID_WPA_DATA, vap->iv_appie_wpa); 94846492Swpaul 949178354Ssam wi_write_val(sc, WI_RID_PROMISC, 0); 950178354Ssam 951178354Ssam /* Configure WEP. */ 952178354Ssam if (ic->ic_cryptocaps & IEEE80211_CRYPTO_WEP) 953178354Ssam wi_write_wep(sc, vap); 954178354Ssam else 955178354Ssam sc->sc_encryption = 0; 956178354Ssam 957178354Ssam wi_enable(sc); /* enable port */ 958178354Ssam WI_UNLOCK(sc); 959178354Ssam } 960178354Ssam return error; 96146492Swpaul} 96246492Swpaul 963109323Ssamstatic void 964165087Ssamwi_start_locked(struct ifnet *ifp) 96546492Swpaul{ 966109323Ssam struct wi_softc *sc = ifp->if_softc; 967119150Ssam struct ieee80211_node *ni; 968109323Ssam struct ieee80211_frame *wh; 969109323Ssam struct mbuf *m0; 970178354Ssam struct ieee80211_key *k; 971109323Ssam struct wi_frame frmhdr; 972190579Ssam const struct llc *llc; 973160991Ssam int cur; 97446492Swpaul 975165087Ssam WI_LOCK_ASSERT(sc); 97646492Swpaul 977165087Ssam if (sc->wi_gone) 978109323Ssam return; 97946492Swpaul 980109323Ssam memset(&frmhdr, 0, sizeof(frmhdr)); 981109323Ssam cur = sc->sc_txnext; 982109323Ssam for (;;) { 983178354Ssam IFQ_DRV_DEQUEUE(&ifp->if_snd, m0); 984178354Ssam if (m0 == NULL) 985178354Ssam break; 986178354Ssam if (sc->sc_txd[cur].d_len != 0) { 987178354Ssam IFQ_DRV_PREPEND(&ifp->if_snd, m0); 988178354Ssam ifp->if_drv_flags |= IFF_DRV_OACTIVE; 989178354Ssam break; 990178354Ssam } 991178354Ssam ni = (struct ieee80211_node *) m0->m_pkthdr.rcvif; 99246492Swpaul 993190579Ssam /* reconstruct 802.3 header */ 994178354Ssam wh = mtod(m0, struct ieee80211_frame *); 995190579Ssam switch (wh->i_fc[1]) { 996190579Ssam case IEEE80211_FC1_DIR_TODS: 997190579Ssam IEEE80211_ADDR_COPY(frmhdr.wi_ehdr.ether_shost, 998190579Ssam wh->i_addr2); 999190579Ssam IEEE80211_ADDR_COPY(frmhdr.wi_ehdr.ether_dhost, 1000190579Ssam wh->i_addr3); 1001190579Ssam break; 1002190579Ssam case IEEE80211_FC1_DIR_NODS: 1003190579Ssam IEEE80211_ADDR_COPY(frmhdr.wi_ehdr.ether_shost, 1004190579Ssam wh->i_addr2); 1005190579Ssam IEEE80211_ADDR_COPY(frmhdr.wi_ehdr.ether_dhost, 1006190579Ssam wh->i_addr1); 1007190579Ssam break; 1008190579Ssam case IEEE80211_FC1_DIR_FROMDS: 1009190579Ssam IEEE80211_ADDR_COPY(frmhdr.wi_ehdr.ether_shost, 1010190579Ssam wh->i_addr3); 1011190579Ssam IEEE80211_ADDR_COPY(frmhdr.wi_ehdr.ether_dhost, 1012190579Ssam wh->i_addr1); 1013190579Ssam break; 1014190579Ssam } 1015190579Ssam llc = (const struct llc *)( 1016190579Ssam mtod(m0, const uint8_t *) + ieee80211_hdrsize(wh)); 1017190579Ssam frmhdr.wi_ehdr.ether_type = llc->llc_snap.ether_type; 1018109323Ssam frmhdr.wi_tx_ctl = htole16(WI_ENC_TX_802_11|WI_TXCNTL_TX_EX); 1019178354Ssam if (wh->i_fc[1] & IEEE80211_FC1_WEP) { 1020178354Ssam k = ieee80211_crypto_encap(ni, m0); 1021138571Ssam if (k == NULL) { 1022170530Ssam ieee80211_free_node(ni); 1023143299Ssam m_freem(m0); 1024109323Ssam continue; 1025109323Ssam } 1026138952Ssam frmhdr.wi_tx_ctl |= htole16(WI_TXCNTL_NOCRYPT); 1027109323Ssam } 1028178354Ssam 1029178354Ssam if (bpf_peers_present(ifp->if_bpf)) { 1030178354Ssam sc->sc_tx_th.wt_rate = ni->ni_txrate; 1031178354Ssam bpf_mtap2(ifp->if_bpf, 1032178354Ssam &sc->sc_tx_th, sc->sc_tx_th_len, m0); 1033109323Ssam } 1034178354Ssam 1035127697Ssam m_copydata(m0, 0, sizeof(struct ieee80211_frame), 1036127697Ssam (caddr_t)&frmhdr.wi_whdr); 1037127697Ssam m_adj(m0, sizeof(struct ieee80211_frame)); 1038127697Ssam frmhdr.wi_dat_len = htole16(m0->m_pkthdr.len); 1039170530Ssam ieee80211_free_node(ni); 1040160991Ssam if (wi_start_tx(ifp, &frmhdr, m0)) 1041109323Ssam continue; 1042178354Ssam 1043160991Ssam sc->sc_txnext = cur = (cur + 1) % sc->sc_ntxbuf; 1044178354Ssam ifp->if_opackets++; 1045160991Ssam } 1046165087Ssam} 1047160991Ssam 1048165087Ssamstatic void 1049165087Ssamwi_start(struct ifnet *ifp) 1050165087Ssam{ 1051165087Ssam struct wi_softc *sc = ifp->if_softc; 1052165087Ssam 1053165087Ssam WI_LOCK(sc); 1054165087Ssam wi_start_locked(ifp); 1055160991Ssam WI_UNLOCK(sc); 1056160991Ssam} 1057160991Ssam 1058160991Ssamstatic int 1059160991Ssamwi_start_tx(struct ifnet *ifp, struct wi_frame *frmhdr, struct mbuf *m0) 1060160991Ssam{ 1061160991Ssam struct wi_softc *sc = ifp->if_softc; 1062160991Ssam int cur = sc->sc_txnext; 1063160991Ssam int fid, off, error; 1064160991Ssam 1065160991Ssam fid = sc->sc_txd[cur].d_fid; 1066160991Ssam off = sizeof(*frmhdr); 1067160991Ssam error = wi_write_bap(sc, fid, 0, frmhdr, sizeof(*frmhdr)) != 0 1068160991Ssam || wi_mwrite_bap(sc, fid, off, m0, m0->m_pkthdr.len) != 0; 1069160991Ssam m_freem(m0); 1070160991Ssam if (error) { 1071160991Ssam ifp->if_oerrors++; 1072160991Ssam return -1; 1073160991Ssam } 1074160991Ssam sc->sc_txd[cur].d_len = off; 1075160991Ssam if (sc->sc_txcur == cur) { 1076160991Ssam if (wi_cmd(sc, WI_CMD_TX | WI_RECLAIM, fid, 0, 0)) { 1077160991Ssam if_printf(ifp, "xmit failed\n"); 1078160991Ssam sc->sc_txd[cur].d_len = 0; 1079160991Ssam return -1; 1080109323Ssam } 1081160991Ssam sc->sc_tx_timer = 5; 1082160991Ssam } 1083160991Ssam return 0; 1084160991Ssam} 1085160991Ssam 1086160991Ssamstatic int 1087160991Ssamwi_raw_xmit(struct ieee80211_node *ni, struct mbuf *m0, 1088160991Ssam const struct ieee80211_bpf_params *params) 1089160991Ssam{ 1090160991Ssam struct ieee80211com *ic = ni->ni_ic; 1091160991Ssam struct ifnet *ifp = ic->ic_ifp; 1092160991Ssam struct wi_softc *sc = ifp->if_softc; 1093178354Ssam struct ieee80211_key *k; 1094160991Ssam struct ieee80211_frame *wh; 1095160991Ssam struct wi_frame frmhdr; 1096160991Ssam int cur; 1097160991Ssam int rc = 0; 1098160991Ssam 1099160991Ssam WI_LOCK(sc); 1100160991Ssam 1101160991Ssam if (sc->wi_gone) { 1102160991Ssam rc = ENETDOWN; 1103160991Ssam goto out; 1104160991Ssam } 1105160991Ssam memset(&frmhdr, 0, sizeof(frmhdr)); 1106160991Ssam cur = sc->sc_txnext; 1107160991Ssam if (sc->sc_txd[cur].d_len != 0) { 1108160991Ssam ifp->if_drv_flags |= IFF_DRV_OACTIVE; 1109160991Ssam rc = ENOBUFS; 1110160991Ssam goto out; 1111160991Ssam } 1112160991Ssam m0->m_pkthdr.rcvif = NULL; 1113160991Ssam 1114160991Ssam m_copydata(m0, 4, ETHER_ADDR_LEN * 2, 1115160991Ssam (caddr_t)&frmhdr.wi_ehdr); 1116160991Ssam frmhdr.wi_ehdr.ether_type = 0; 1117160991Ssam wh = mtod(m0, struct ieee80211_frame *); 1118160991Ssam 1119160991Ssam frmhdr.wi_tx_ctl = htole16(WI_ENC_TX_802_11|WI_TXCNTL_TX_EX); 1120160991Ssam if (params && (params->ibp_flags & IEEE80211_BPF_NOACK)) 1121160991Ssam frmhdr.wi_tx_ctl |= htole16(WI_TXCNTL_ALTRTRY); 1122160991Ssam if ((wh->i_fc[1] & IEEE80211_FC1_WEP) && 1123178354Ssam (!params || (params && (params->ibp_flags & IEEE80211_BPF_CRYPTO)))) { 1124178354Ssam k = ieee80211_crypto_encap(ni, m0); 1125178354Ssam if (k == NULL) { 1126178354Ssam rc = ENOMEM; 1127178354Ssam goto out; 1128109323Ssam } 1129178354Ssam frmhdr.wi_tx_ctl |= htole16(WI_TXCNTL_NOCRYPT); 113074838Salfred } 1131178354Ssam if (bpf_peers_present(ifp->if_bpf)) { 1132178354Ssam sc->sc_tx_th.wt_rate = ni->ni_txrate; 1133178354Ssam bpf_mtap2(ifp->if_bpf, &sc->sc_tx_th, sc->sc_tx_th_len, m0); 1134160991Ssam } 1135160991Ssam m_copydata(m0, 0, sizeof(struct ieee80211_frame), 1136160991Ssam (caddr_t)&frmhdr.wi_whdr); 1137160991Ssam m_adj(m0, sizeof(struct ieee80211_frame)); 1138160991Ssam frmhdr.wi_dat_len = htole16(m0->m_pkthdr.len); 1139168860Ssephe if (wi_start_tx(ifp, &frmhdr, m0) < 0) { 1140168860Ssephe m0 = NULL; 1141168860Ssephe rc = EIO; 1142160991Ssam goto out; 1143168860Ssephe } 1144168860Ssephe m0 = NULL; 114546492Swpaul 1146160991Ssam sc->sc_txnext = cur = (cur + 1) % sc->sc_ntxbuf; 1147160991Ssamout: 1148109323Ssam WI_UNLOCK(sc); 1149160991Ssam 1150168860Ssephe if (m0 != NULL) 1151168860Ssephe m_freem(m0); 1152168860Ssephe ieee80211_free_node(ni); 1153160991Ssam return rc; 115446492Swpaul} 115546492Swpaul 115688546Salfredstatic int 1157178354Ssamwi_reset(struct wi_softc *sc) 115846492Swpaul{ 1159114124Simp#define WI_INIT_TRIES 3 1160178354Ssam int i, error = 0; 1161112362Simp 1162178354Ssam for (i = 0; i < WI_INIT_TRIES; i++) { 1163178354Ssam error = wi_cmd(sc, WI_CMD_INI, 0, 0, 0); 1164178354Ssam if (error == 0) 116546492Swpaul break; 1166109323Ssam DELAY(WI_DELAY * 1000); 116746492Swpaul } 1168114124Simp sc->sc_reset = 1; 1169178354Ssam if (i == WI_INIT_TRIES) { 1170178354Ssam if_printf(sc->sc_ifp, "reset failed\n"); 1171178354Ssam return error; 117275373Salfred } 117346492Swpaul 1174109323Ssam CSR_WRITE_2(sc, WI_INT_EN, 0); 1175114124Simp CSR_WRITE_2(sc, WI_EVENT_ACK, 0xFFFF); 117646492Swpaul 1177109323Ssam /* Calibrate timer. */ 1178114124Simp wi_write_val(sc, WI_RID_TICK_TIME, 8); 1179114124Simp 1180178354Ssam return 0; 1181109323Ssam#undef WI_INIT_TRIES 118246492Swpaul} 118346492Swpaul 118488546Salfredstatic void 1185165089Ssamwi_watchdog(void *arg) 118646492Swpaul{ 1187165089Ssam struct wi_softc *sc = arg; 1188165089Ssam struct ifnet *ifp = sc->sc_ifp; 118946492Swpaul 1190178354Ssam WI_LOCK_ASSERT(sc); 1191178354Ssam 1192109323Ssam if (!sc->sc_enabled) 1193109323Ssam return; 119446492Swpaul 1195178354Ssam if (sc->sc_tx_timer && --sc->sc_tx_timer == 0) { 1196178354Ssam if_printf(ifp, "device timeout\n"); 1197178354Ssam ifp->if_oerrors++; 1198178354Ssam wi_init_locked(ifp->if_softc); 1199178354Ssam return; 120046492Swpaul } 1201165089Ssam callout_reset(&sc->sc_watchdog, hz, wi_watchdog, sc); 120246492Swpaul} 120346492Swpaul 120488546Salfredstatic int 1205109323Ssamwi_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 120646492Swpaul{ 1207109323Ssam struct wi_softc *sc = ifp->if_softc; 1208178354Ssam struct ieee80211com *ic = ifp->if_l2com; 1209178354Ssam struct ifreq *ifr = (struct ifreq *) data; 1210178354Ssam int error = 0, startall = 0; 121146492Swpaul 1212109323Ssam switch (cmd) { 121346492Swpaul case SIOCSIFFLAGS: 1214178704Sthompsa WI_LOCK(sc); 1215100876Simp /* 1216101139Simp * Can't do promisc and hostap at the same time. If all that's 1217101139Simp * changing is the promisc flag, try to short-circuit a call to 1218101139Simp * wi_init() by just setting PROMISC in the hardware. 1219100876Simp */ 122046492Swpaul if (ifp->if_flags & IFF_UP) { 1221109323Ssam if (ic->ic_opmode != IEEE80211_M_HOSTAP && 1222148887Srwatson ifp->if_drv_flags & IFF_DRV_RUNNING) { 1223178354Ssam if ((ifp->if_flags ^ sc->sc_if_flags) & IFF_PROMISC) { 1224178354Ssam wi_write_val(sc, WI_RID_PROMISC, 1225178354Ssam (ifp->if_flags & IFF_PROMISC) != 0); 1226101139Simp } else { 1227178354Ssam wi_init_locked(sc); 1228178354Ssam startall = 1; 1229101139Simp } 1230100876Simp } else { 1231178354Ssam wi_init_locked(sc); 1232178354Ssam startall = 1; 1233100876Simp } 123446492Swpaul } else { 1235178354Ssam if (ifp->if_drv_flags & IFF_DRV_RUNNING) 1236178354Ssam wi_stop_locked(sc, 1); 1237123098Simp sc->wi_gone = 0; 123846492Swpaul } 1239109323Ssam sc->sc_if_flags = ifp->if_flags; 1240178704Sthompsa WI_UNLOCK(sc); 1241178704Sthompsa if (startall) 1242178704Sthompsa ieee80211_start_all(ic); 124346492Swpaul break; 1244178354Ssam case SIOCGIFMEDIA: 1245178354Ssam error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd); 124646492Swpaul break; 1247178704Sthompsa case SIOCGIFADDR: 1248178354Ssam error = ether_ioctl(ifp, cmd, data); 1249170530Ssam break; 1250178704Sthompsa default: 1251178704Sthompsa error = EINVAL; 1252178704Sthompsa break; 1253170530Ssam } 1254109323Ssam return error; 1255109323Ssam} 125646492Swpaul 1257109323Ssamstatic void 1258109323Ssamwi_media_status(struct ifnet *ifp, struct ifmediareq *imr) 1259109323Ssam{ 1260178354Ssam struct ieee80211vap *vap = ifp->if_softc; 1261178354Ssam struct ieee80211com *ic = vap->iv_ic; 1262178354Ssam struct wi_softc *sc = ic->ic_ifp->if_softc; 1263109323Ssam u_int16_t val; 1264109323Ssam int rate, len; 126546492Swpaul 1266109323Ssam len = sizeof(val); 1267178354Ssam if (sc->sc_enabled && 1268178354Ssam wi_read_rid(sc, WI_RID_CUR_TX_RATE, &val, &len) == 0 && 1269138571Ssam len == sizeof(val)) { 1270109323Ssam /* convert to 802.11 rate */ 1271138571Ssam val = le16toh(val); 1272109323Ssam rate = val * 2; 1273109323Ssam if (sc->sc_firmware_type == WI_LUCENT) { 1274138571Ssam if (rate == 10) 1275109323Ssam rate = 11; /* 5.5Mbps */ 1276109323Ssam } else { 1277109323Ssam if (rate == 4*2) 1278109323Ssam rate = 11; /* 5.5Mbps */ 1279109323Ssam else if (rate == 8*2) 1280109323Ssam rate = 22; /* 11Mbps */ 1281109323Ssam } 1282178354Ssam vap->iv_bss->ni_txrate = rate; 1283109323Ssam } 1284178354Ssam ieee80211_media_status(ifp, imr); 1285109323Ssam} 128646492Swpaul 1287109323Ssamstatic void 1288109323Ssamwi_sync_bssid(struct wi_softc *sc, u_int8_t new_bssid[IEEE80211_ADDR_LEN]) 1289109323Ssam{ 1290147256Sbrooks struct ifnet *ifp = sc->sc_ifp; 1291178354Ssam struct ieee80211com *ic = ifp->if_l2com; 1292178354Ssam struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps); 1293178354Ssam struct ieee80211_node *ni = vap->iv_bss; 129498440Simp 1295109323Ssam if (IEEE80211_ADDR_EQ(new_bssid, ni->ni_bssid)) 1296109323Ssam return; 129746492Swpaul 1298109323Ssam DPRINTF(("wi_sync_bssid: bssid %s -> ", ether_sprintf(ni->ni_bssid))); 1299109323Ssam DPRINTF(("%s ?\n", ether_sprintf(new_bssid))); 130046492Swpaul 1301109323Ssam /* In promiscuous mode, the BSSID field is not a reliable 1302109323Ssam * indicator of the firmware's BSSID. Damp spurious 1303109323Ssam * change-of-BSSID indications. 1304109323Ssam */ 1305109323Ssam if ((ifp->if_flags & IFF_PROMISC) != 0 && 1306138571Ssam !ppsratecheck(&sc->sc_last_syn, &sc->sc_false_syns, 1307138571Ssam WI_MAX_FALSE_SYNS)) 1308109323Ssam return; 130946492Swpaul 1310139542Ssam sc->sc_false_syns = MAX(0, sc->sc_false_syns - 1); 1311170530Ssam#if 0 1312139542Ssam /* 1313139542Ssam * XXX hack; we should create a new node with the new bssid 1314139542Ssam * and replace the existing ic_bss with it but since we don't 1315139542Ssam * process management frames to collect state we cheat by 1316139542Ssam * reusing the existing node as we know wi_newstate will be 1317139542Ssam * called and it will overwrite the node state. 1318139542Ssam */ 1319139542Ssam ieee80211_sta_join(ic, ieee80211_ref_node(ni)); 1320170530Ssam#endif 1321109323Ssam} 132246492Swpaul 1323178354Ssamstatic __noinline void 1324109323Ssamwi_rx_intr(struct wi_softc *sc) 1325109323Ssam{ 1326147256Sbrooks struct ifnet *ifp = sc->sc_ifp; 1327178354Ssam struct ieee80211com *ic = ifp->if_l2com; 1328109323Ssam struct wi_frame frmhdr; 1329109323Ssam struct mbuf *m; 1330109323Ssam struct ieee80211_frame *wh; 1331119150Ssam struct ieee80211_node *ni; 1332109323Ssam int fid, len, off, rssi; 1333109323Ssam u_int8_t dir; 1334109323Ssam u_int16_t status; 1335109323Ssam u_int32_t rstamp; 133646611Swpaul 1337109323Ssam fid = CSR_READ_2(sc, WI_RX_FID); 133846611Swpaul 1339109323Ssam /* First read in the frame header */ 1340109323Ssam if (wi_read_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr))) { 1341109323Ssam CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX); 1342109323Ssam ifp->if_ierrors++; 1343109323Ssam DPRINTF(("wi_rx_intr: read fid %x failed\n", fid)); 1344109323Ssam return; 1345109323Ssam } 134691695Simp 1347109323Ssam /* 1348109323Ssam * Drop undecryptable or packets with receive errors here 1349109323Ssam */ 1350109323Ssam status = le16toh(frmhdr.wi_status); 1351109323Ssam if (status & WI_STAT_ERRSTAT) { 1352109323Ssam CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX); 1353109323Ssam ifp->if_ierrors++; 1354109323Ssam DPRINTF(("wi_rx_intr: fid %x error status %x\n", fid, status)); 1355109323Ssam return; 1356109323Ssam } 1357109323Ssam rssi = frmhdr.wi_rx_signal; 1358109323Ssam rstamp = (le16toh(frmhdr.wi_rx_tstamp0) << 16) | 1359109323Ssam le16toh(frmhdr.wi_rx_tstamp1); 136046492Swpaul 1361109323Ssam len = le16toh(frmhdr.wi_dat_len); 1362109323Ssam off = ALIGN(sizeof(struct ieee80211_frame)); 136346563Swpaul 1364117855Ssam /* 1365117855Ssam * Sometimes the PRISM2.x returns bogusly large frames. Except 1366117855Ssam * in monitor mode, just throw them away. 1367117855Ssam */ 1368117855Ssam if (off + len > MCLBYTES) { 1369117855Ssam if (ic->ic_opmode != IEEE80211_M_MONITOR) { 1370117855Ssam CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX); 1371117855Ssam ifp->if_ierrors++; 1372117855Ssam DPRINTF(("wi_rx_intr: oversized packet\n")); 1373117855Ssam return; 1374117855Ssam } else 1375117855Ssam len = 0; 1376117855Ssam } 1377117855Ssam 1378178354Ssam if (off + len > MHLEN) 1379178354Ssam m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR); 1380178354Ssam else 1381178354Ssam m = m_gethdr(M_DONTWAIT, MT_DATA); 1382109323Ssam if (m == NULL) { 1383109323Ssam CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX); 1384109323Ssam ifp->if_ierrors++; 1385109323Ssam DPRINTF(("wi_rx_intr: MGET failed\n")); 1386109323Ssam return; 1387109323Ssam } 1388109323Ssam m->m_data += off - sizeof(struct ieee80211_frame); 1389109323Ssam memcpy(m->m_data, &frmhdr.wi_whdr, sizeof(struct ieee80211_frame)); 1390109323Ssam wi_read_bap(sc, fid, sizeof(frmhdr), 1391109323Ssam m->m_data + sizeof(struct ieee80211_frame), len); 1392109323Ssam m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame) + len; 1393109323Ssam m->m_pkthdr.rcvif = ifp; 139494405Simp 1395109323Ssam CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX); 139646492Swpaul 1397178354Ssam if (bpf_peers_present(ifp->if_bpf)) { 1398123927Ssam /* XXX replace divide by table */ 1399123922Ssam sc->sc_rx_th.wr_rate = frmhdr.wi_rx_rate / 5; 1400127148Sgreen sc->sc_rx_th.wr_antsignal = frmhdr.wi_rx_signal; 1401127148Sgreen sc->sc_rx_th.wr_antnoise = frmhdr.wi_rx_silence; 1402123927Ssam sc->sc_rx_th.wr_flags = 0; 1403123927Ssam if (frmhdr.wi_status & WI_STAT_PCF) 1404123927Ssam sc->sc_rx_th.wr_flags |= IEEE80211_RADIOTAP_F_CFP; 1405178354Ssam if (m->m_flags & M_WEP) 1406178354Ssam sc->sc_rx_th.wr_flags |= IEEE80211_RADIOTAP_F_WEP; 1407178354Ssam bpf_mtap2(ifp->if_bpf, &sc->sc_rx_th, sc->sc_rx_th_len, m); 140856965Swpaul } 140956965Swpaul 1410109323Ssam /* synchronize driver's BSSID with firmware's BSSID */ 1411178354Ssam wh = mtod(m, struct ieee80211_frame *); 1412109323Ssam dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK; 1413109323Ssam if (ic->ic_opmode == IEEE80211_M_IBSS && dir == IEEE80211_FC1_DIR_NODS) 1414109323Ssam wi_sync_bssid(sc, wh->i_addr3); 141546492Swpaul 1416165088Ssam WI_UNLOCK(sc); 1417165088Ssam 1418178354Ssam ni = ieee80211_find_rxnode(ic, mtod(m, struct ieee80211_frame_min *)); 1419178354Ssam if (ni != NULL) { 1420178354Ssam (void) ieee80211_input(ni, m, rssi, -95/*XXX*/, rstamp); 1421178354Ssam ieee80211_free_node(ni); 1422178354Ssam } else 1423178354Ssam (void) ieee80211_input_all(ic, m, rssi, -95/*XXX*/, rstamp); 1424178354Ssam 1425165088Ssam WI_LOCK(sc); 1426109323Ssam} 142746492Swpaul 1428178354Ssamstatic __noinline void 1429109323Ssamwi_tx_ex_intr(struct wi_softc *sc) 1430109323Ssam{ 1431147256Sbrooks struct ifnet *ifp = sc->sc_ifp; 1432109323Ssam struct wi_frame frmhdr; 1433109323Ssam int fid; 143446492Swpaul 1435109323Ssam fid = CSR_READ_2(sc, WI_TX_CMP_FID); 1436109323Ssam /* Read in the frame header */ 1437109323Ssam if (wi_read_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr)) == 0) { 1438109323Ssam u_int16_t status = le16toh(frmhdr.wi_status); 1439109323Ssam /* 1440109323Ssam * Spontaneous station disconnects appear as xmit 1441109323Ssam * errors. Don't announce them and/or count them 1442109323Ssam * as an output error. 1443109323Ssam */ 1444109323Ssam if ((status & WI_TXSTAT_DISCONNECT) == 0) { 1445109323Ssam if (ppsratecheck(&lasttxerror, &curtxeps, wi_txerate)) { 1446109323Ssam if_printf(ifp, "tx failed"); 1447109323Ssam if (status & WI_TXSTAT_RET_ERR) 1448109323Ssam printf(", retry limit exceeded"); 1449109323Ssam if (status & WI_TXSTAT_AGED_ERR) 1450109323Ssam printf(", max transmit lifetime exceeded"); 1451109323Ssam if (status & WI_TXSTAT_DISCONNECT) 1452109323Ssam printf(", port disconnected"); 1453109323Ssam if (status & WI_TXSTAT_FORM_ERR) 1454109323Ssam printf(", invalid format (data len %u src %6D)", 1455109323Ssam le16toh(frmhdr.wi_dat_len), 1456109323Ssam frmhdr.wi_ehdr.ether_shost, ":"); 1457109323Ssam if (status & ~0xf) 1458109323Ssam printf(", status=0x%x", status); 1459109323Ssam printf("\n"); 1460109323Ssam } 1461109323Ssam ifp->if_oerrors++; 1462109323Ssam } else { 1463109323Ssam DPRINTF(("port disconnected\n")); 1464109323Ssam ifp->if_collisions++; /* XXX */ 1465109323Ssam } 1466109323Ssam } else 1467109323Ssam DPRINTF(("wi_tx_ex_intr: read fid %x failed\n", fid)); 1468109323Ssam CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_TX_EXC); 1469109323Ssam} 147046492Swpaul 1471178354Ssamstatic __noinline void 1472109323Ssamwi_tx_intr(struct wi_softc *sc) 1473109323Ssam{ 1474147256Sbrooks struct ifnet *ifp = sc->sc_ifp; 1475109323Ssam int fid, cur; 147694405Simp 1477123098Simp if (sc->wi_gone) 1478123098Simp return; 1479123098Simp 1480109323Ssam fid = CSR_READ_2(sc, WI_ALLOC_FID); 1481109323Ssam CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_ALLOC); 148246492Swpaul 1483109323Ssam cur = sc->sc_txcur; 1484109323Ssam if (sc->sc_txd[cur].d_fid != fid) { 1485109323Ssam if_printf(ifp, "bad alloc %x != %x, cur %d nxt %d\n", 1486109323Ssam fid, sc->sc_txd[cur].d_fid, cur, sc->sc_txnext); 1487109323Ssam return; 1488109323Ssam } 1489109323Ssam sc->sc_tx_timer = 0; 1490109323Ssam sc->sc_txd[cur].d_len = 0; 1491112363Simp sc->sc_txcur = cur = (cur + 1) % sc->sc_ntxbuf; 1492109323Ssam if (sc->sc_txd[cur].d_len == 0) 1493148887Srwatson ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 1494109323Ssam else { 1495109323Ssam if (wi_cmd(sc, WI_CMD_TX | WI_RECLAIM, sc->sc_txd[cur].d_fid, 1496109323Ssam 0, 0)) { 1497109323Ssam if_printf(ifp, "xmit failed\n"); 1498109323Ssam sc->sc_txd[cur].d_len = 0; 1499109323Ssam } else { 1500109323Ssam sc->sc_tx_timer = 5; 1501109323Ssam } 1502109323Ssam } 150346492Swpaul} 150446492Swpaul 1505178354Ssamstatic __noinline void 1506109323Ssamwi_info_intr(struct wi_softc *sc) 150794405Simp{ 1508147256Sbrooks struct ifnet *ifp = sc->sc_ifp; 1509178354Ssam struct ieee80211com *ic = ifp->if_l2com; 1510178354Ssam struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps); 1511109323Ssam int i, fid, len, off; 1512109323Ssam u_int16_t ltbuf[2]; 1513109323Ssam u_int16_t stat; 1514109323Ssam u_int32_t *ptr; 151594405Simp 1516109323Ssam fid = CSR_READ_2(sc, WI_INFO_FID); 1517109323Ssam wi_read_bap(sc, fid, 0, ltbuf, sizeof(ltbuf)); 151894405Simp 1519109323Ssam switch (le16toh(ltbuf[1])) { 1520109323Ssam case WI_INFO_LINK_STAT: 1521109323Ssam wi_read_bap(sc, fid, sizeof(ltbuf), &stat, sizeof(stat)); 1522109323Ssam DPRINTF(("wi_info_intr: LINK_STAT 0x%x\n", le16toh(stat))); 1523109323Ssam switch (le16toh(stat)) { 1524109323Ssam case WI_INFO_LINK_STAT_CONNECTED: 1525178354Ssam if (vap->iv_state == IEEE80211_S_RUN && 1526178354Ssam vap->iv_opmode != IEEE80211_M_IBSS) 1527109323Ssam break; 1528178354Ssam /* fall thru... */ 1529109323Ssam case WI_INFO_LINK_STAT_AP_CHG: 1530191746Sthompsa IEEE80211_LOCK(ic); 1531191746Sthompsa vap->iv_bss->ni_associd = 1 | 0xc000; /* NB: anything will do */ 1532191746Sthompsa ieee80211_new_state(vap, IEEE80211_S_RUN, 0); 1533191746Sthompsa IEEE80211_UNLOCK(ic); 1534109323Ssam break; 1535109323Ssam case WI_INFO_LINK_STAT_AP_INR: 1536109323Ssam break; 1537178354Ssam case WI_INFO_LINK_STAT_DISCONNECTED: 1538178354Ssam /* we dropped off the net; e.g. due to deauth/disassoc */ 1539191746Sthompsa IEEE80211_LOCK(ic); 1540191746Sthompsa vap->iv_bss->ni_associd = 0; 1541191746Sthompsa vap->iv_stats.is_rx_deauth++; 1542191746Sthompsa ieee80211_new_state(vap, IEEE80211_S_SCAN, 0); 1543191746Sthompsa IEEE80211_UNLOCK(ic); 1544178354Ssam break; 1545109323Ssam case WI_INFO_LINK_STAT_AP_OOR: 1546178354Ssam /* XXX does this need to be per-vap? */ 1547191746Sthompsa ieee80211_beacon_miss(ic); 1548109323Ssam break; 1549109323Ssam case WI_INFO_LINK_STAT_ASSOC_FAILED: 1550178354Ssam if (vap->iv_opmode == IEEE80211_M_STA) 1551191746Sthompsa ieee80211_new_state(vap, IEEE80211_S_SCAN, 1552191746Sthompsa IEEE80211_SCAN_FAIL_TIMEOUT); 1553109323Ssam break; 1554109323Ssam } 1555109323Ssam break; 1556109323Ssam case WI_INFO_COUNTERS: 1557109323Ssam /* some card versions have a larger stats structure */ 1558109323Ssam len = min(le16toh(ltbuf[0]) - 1, sizeof(sc->sc_stats) / 4); 1559109323Ssam ptr = (u_int32_t *)&sc->sc_stats; 1560109323Ssam off = sizeof(ltbuf); 1561109323Ssam for (i = 0; i < len; i++, off += 2, ptr++) { 1562109323Ssam wi_read_bap(sc, fid, off, &stat, sizeof(stat)); 1563109323Ssam#ifdef WI_HERMES_STATS_WAR 1564109323Ssam if (stat & 0xf000) 1565109323Ssam stat = ~stat; 1566109323Ssam#endif 1567109323Ssam *ptr += stat; 1568109323Ssam } 1569109323Ssam ifp->if_collisions = sc->sc_stats.wi_tx_single_retries + 1570109323Ssam sc->sc_stats.wi_tx_multi_retries + 1571109323Ssam sc->sc_stats.wi_tx_retry_limit; 1572109323Ssam break; 1573109323Ssam default: 1574109323Ssam DPRINTF(("wi_info_intr: got fid %x type %x len %d\n", fid, 1575109323Ssam le16toh(ltbuf[1]), le16toh(ltbuf[0]))); 1576109323Ssam break; 157794405Simp } 1578109323Ssam CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_INFO); 1579109323Ssam} 158094405Simp 1581109323Ssamstatic int 1582109323Ssamwi_write_multi(struct wi_softc *sc) 1583109323Ssam{ 1584147256Sbrooks struct ifnet *ifp = sc->sc_ifp; 1585109323Ssam int n; 1586109323Ssam struct ifmultiaddr *ifma; 1587109323Ssam struct wi_mcast mlist; 158894472Simp 1589109323Ssam if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) { 1590109323Ssamallmulti: 1591109323Ssam memset(&mlist, 0, sizeof(mlist)); 1592109323Ssam return wi_write_rid(sc, WI_RID_MCAST_LIST, &mlist, 1593109323Ssam sizeof(mlist)); 159494405Simp } 159594405Simp 1596109323Ssam n = 0; 1597148654Srwatson IF_ADDR_LOCK(ifp); 1598109323Ssam TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 1599109323Ssam if (ifma->ifma_addr->sa_family != AF_LINK) 1600109323Ssam continue; 1601109323Ssam if (n >= 16) 1602109323Ssam goto allmulti; 1603109323Ssam IEEE80211_ADDR_COPY(&mlist.wi_mcast[n], 1604109323Ssam (LLADDR((struct sockaddr_dl *)ifma->ifma_addr))); 1605109323Ssam n++; 160694405Simp } 1607148654Srwatson IF_ADDR_UNLOCK(ifp); 1608109323Ssam return wi_write_rid(sc, WI_RID_MCAST_LIST, &mlist, 1609109323Ssam IEEE80211_ADDR_LEN * n); 161094405Simp} 161194405Simp 161294405Simpstatic void 1613178354Ssamwi_update_mcast(struct ifnet *ifp) 1614178354Ssam{ 1615178354Ssam wi_write_multi(ifp->if_softc); 1616178354Ssam} 1617178354Ssam 1618178354Ssamstatic void 1619109323Ssamwi_read_nicid(struct wi_softc *sc) 162046492Swpaul{ 1621109323Ssam struct wi_card_ident *id; 1622109323Ssam char *p; 1623109323Ssam int len; 1624109323Ssam u_int16_t ver[4]; 162546492Swpaul 1626109323Ssam /* getting chip identity */ 1627109323Ssam memset(ver, 0, sizeof(ver)); 1628109323Ssam len = sizeof(ver); 1629109323Ssam wi_read_rid(sc, WI_RID_CARD_ID, ver, &len); 163046492Swpaul 1631109323Ssam sc->sc_firmware_type = WI_NOTYPE; 1632180919Simp sc->sc_nic_id = le16toh(ver[0]); 1633109323Ssam for (id = wi_card_ident; id->card_name != NULL; id++) { 1634180919Simp if (sc->sc_nic_id == id->card_id) { 1635180919Simp sc->sc_nic_name = id->card_name; 1636109323Ssam sc->sc_firmware_type = id->firm_type; 1637109323Ssam break; 1638109323Ssam } 163967092Swpaul } 1640109323Ssam if (sc->sc_firmware_type == WI_NOTYPE) { 1641180919Simp if (sc->sc_nic_id & 0x8000) { 1642109323Ssam sc->sc_firmware_type = WI_INTERSIL; 1643180919Simp sc->sc_nic_name = "Unknown Prism chip"; 1644109323Ssam } else { 1645109323Ssam sc->sc_firmware_type = WI_LUCENT; 1646180919Simp sc->sc_nic_name = "Unknown Lucent chip"; 1647109323Ssam } 164867092Swpaul } 1649181210Simp if (bootverbose) 1650181210Simp device_printf(sc->sc_dev, "using %s\n", sc->sc_nic_name); 165146492Swpaul 1652109323Ssam /* get primary firmware version (Only Prism chips) */ 1653109323Ssam if (sc->sc_firmware_type != WI_LUCENT) { 1654109323Ssam memset(ver, 0, sizeof(ver)); 1655109323Ssam len = sizeof(ver); 1656109323Ssam wi_read_rid(sc, WI_RID_PRI_IDENTITY, ver, &len); 1657109323Ssam sc->sc_pri_firmware_ver = le16toh(ver[2]) * 10000 + 1658109323Ssam le16toh(ver[3]) * 100 + le16toh(ver[1]); 165967092Swpaul } 166046492Swpaul 1661109323Ssam /* get station firmware version */ 1662109323Ssam memset(ver, 0, sizeof(ver)); 1663109323Ssam len = sizeof(ver); 1664109323Ssam wi_read_rid(sc, WI_RID_STA_IDENTITY, ver, &len); 1665109323Ssam sc->sc_sta_firmware_ver = le16toh(ver[2]) * 10000 + 1666109323Ssam le16toh(ver[3]) * 100 + le16toh(ver[1]); 1667109323Ssam if (sc->sc_firmware_type == WI_INTERSIL && 1668109323Ssam (sc->sc_sta_firmware_ver == 10102 || 1669109323Ssam sc->sc_sta_firmware_ver == 20102)) { 1670109323Ssam char ident[12]; 1671109323Ssam memset(ident, 0, sizeof(ident)); 1672109323Ssam len = sizeof(ident); 1673109323Ssam /* value should be the format like "V2.00-11" */ 1674109323Ssam if (wi_read_rid(sc, WI_RID_SYMBOL_IDENTITY, ident, &len) == 0 && 1675109323Ssam *(p = (char *)ident) >= 'A' && 1676109323Ssam p[2] == '.' && p[5] == '-' && p[8] == '\0') { 1677109323Ssam sc->sc_firmware_type = WI_SYMBOL; 1678109323Ssam sc->sc_sta_firmware_ver = (p[1] - '0') * 10000 + 1679109323Ssam (p[3] - '0') * 1000 + (p[4] - '0') * 100 + 1680109323Ssam (p[6] - '0') * 10 + (p[7] - '0'); 168194405Simp } 168294405Simp } 1683180919Simp if (bootverbose) { 1684180919Simp device_printf(sc->sc_dev, "%s Firmware: ", 1685180919Simp wi_firmware_names[sc->sc_firmware_type]); 1686180919Simp if (sc->sc_firmware_type != WI_LUCENT) /* XXX */ 1687180919Simp printf("Primary (%u.%u.%u), ", 1688180919Simp sc->sc_pri_firmware_ver / 10000, 1689180919Simp (sc->sc_pri_firmware_ver % 10000) / 100, 1690180919Simp sc->sc_pri_firmware_ver % 100); 1691180919Simp printf("Station (%u.%u.%u)\n", 1692180919Simp sc->sc_sta_firmware_ver / 10000, 1693180919Simp (sc->sc_sta_firmware_ver % 10000) / 100, 1694180919Simp sc->sc_sta_firmware_ver % 100); 1695180919Simp } 1696109323Ssam} 169746492Swpaul 1698109323Ssamstatic int 1699109323Ssamwi_write_ssid(struct wi_softc *sc, int rid, u_int8_t *buf, int buflen) 1700109323Ssam{ 1701109323Ssam struct wi_ssid ssid; 170246492Swpaul 1703109323Ssam if (buflen > IEEE80211_NWID_LEN) 1704109323Ssam return ENOBUFS; 1705109323Ssam memset(&ssid, 0, sizeof(ssid)); 1706109323Ssam ssid.wi_len = htole16(buflen); 1707109323Ssam memcpy(ssid.wi_ssid, buf, buflen); 1708109323Ssam return wi_write_rid(sc, rid, &ssid, sizeof(ssid)); 1709109323Ssam} 171046492Swpaul 1711109323Ssamstatic int 1712178354Ssamwi_write_txrate(struct wi_softc *sc, struct ieee80211vap *vap) 1713109323Ssam{ 1714178354Ssam static const uint16_t lucent_rates[12] = { 1715178354Ssam [ 0] = 3, /* auto */ 1716178354Ssam [ 1] = 1, /* 1Mb/s */ 1717178354Ssam [ 2] = 2, /* 2Mb/s */ 1718178354Ssam [ 5] = 4, /* 5.5Mb/s */ 1719178354Ssam [11] = 5 /* 11Mb/s */ 1720178354Ssam }; 1721178354Ssam static const uint16_t intersil_rates[12] = { 1722178354Ssam [ 0] = 0xf, /* auto */ 1723178354Ssam [ 1] = 0, /* 1Mb/s */ 1724178354Ssam [ 2] = 1, /* 2Mb/s */ 1725178354Ssam [ 5] = 2, /* 5.5Mb/s */ 1726178354Ssam [11] = 3, /* 11Mb/s */ 1727178354Ssam }; 1728178354Ssam const uint16_t *rates = sc->sc_firmware_type == WI_LUCENT ? 1729178354Ssam lucent_rates : intersil_rates; 1730178354Ssam struct ieee80211com *ic = vap->iv_ic; 1731178354Ssam const struct ieee80211_txparam *tp; 173246492Swpaul 1733178354Ssam tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_bsschan)]; 1734178354Ssam return wi_write_val(sc, WI_RID_TX_RATE, 1735178354Ssam (tp->ucastrate == IEEE80211_FIXED_RATE_NONE ? 1736178354Ssam rates[0] : rates[tp->ucastrate / 2])); 173746492Swpaul} 173846492Swpaul 1739109323Ssamstatic int 1740178354Ssamwi_write_wep(struct wi_softc *sc, struct ieee80211vap *vap) 174146492Swpaul{ 1742109323Ssam int error = 0; 1743109323Ssam int i, keylen; 1744109323Ssam u_int16_t val; 1745109323Ssam struct wi_key wkey[IEEE80211_WEP_NKID]; 174646492Swpaul 1747109323Ssam switch (sc->sc_firmware_type) { 1748109323Ssam case WI_LUCENT: 1749178354Ssam val = (vap->iv_flags & IEEE80211_F_PRIVACY) ? 1 : 0; 1750109323Ssam error = wi_write_val(sc, WI_RID_ENCRYPTION, val); 1751109323Ssam if (error) 1752109323Ssam break; 1753178354Ssam if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) 1754138988Smdodd break; 1755178354Ssam error = wi_write_val(sc, WI_RID_TX_CRYPT_KEY, vap->iv_def_txkey); 1756138988Smdodd if (error) 1757138988Smdodd break; 1758138988Smdodd memset(wkey, 0, sizeof(wkey)); 1759138988Smdodd for (i = 0; i < IEEE80211_WEP_NKID; i++) { 1760178354Ssam keylen = vap->iv_nw_keys[i].wk_keylen; 1761138988Smdodd wkey[i].wi_keylen = htole16(keylen); 1762178354Ssam memcpy(wkey[i].wi_keydat, vap->iv_nw_keys[i].wk_key, 1763138988Smdodd keylen); 1764109323Ssam } 1765138988Smdodd error = wi_write_rid(sc, WI_RID_DEFLT_CRYPT_KEYS, 1766138988Smdodd wkey, sizeof(wkey)); 1767150798Savatar sc->sc_encryption = 0; 1768109323Ssam break; 1769109323Ssam 1770109323Ssam case WI_INTERSIL: 1771178354Ssam val = HOST_ENCRYPT | HOST_DECRYPT; 1772178354Ssam if (vap->iv_flags & IEEE80211_F_PRIVACY) { 1773109323Ssam /* 1774109323Ssam * ONLY HWB3163 EVAL-CARD Firmware version 1775109323Ssam * less than 0.8 variant2 1776109323Ssam * 1777109323Ssam * If promiscuous mode disable, Prism2 chip 1778109323Ssam * does not work with WEP . 1779109323Ssam * It is under investigation for details. 1780109323Ssam * (ichiro@netbsd.org) 1781109323Ssam */ 1782178354Ssam if (sc->sc_sta_firmware_ver < 802 ) { 1783109323Ssam /* firm ver < 0.8 variant 2 */ 1784109323Ssam wi_write_val(sc, WI_RID_PROMISC, 1); 1785109323Ssam } 1786109323Ssam wi_write_val(sc, WI_RID_CNFAUTHMODE, 1787178354Ssam vap->iv_bss->ni_authmode); 1788178354Ssam val |= PRIVACY_INVOKED; 1789109323Ssam } else { 1790178354Ssam wi_write_val(sc, WI_RID_CNFAUTHMODE, IEEE80211_AUTH_OPEN); 1791109323Ssam } 1792109323Ssam error = wi_write_val(sc, WI_RID_P2_ENCRYPTION, val); 1793109323Ssam if (error) 1794109323Ssam break; 1795150798Savatar sc->sc_encryption = val; 1796138988Smdodd if ((val & PRIVACY_INVOKED) == 0) 1797138988Smdodd break; 1798178354Ssam error = wi_write_val(sc, WI_RID_P2_TX_CRYPT_KEY, vap->iv_def_txkey); 1799109323Ssam break; 1800109323Ssam } 1801109323Ssam return error; 180246492Swpaul} 180346492Swpaul 1804109323Ssamstatic int 1805109323Ssamwi_cmd(struct wi_softc *sc, int cmd, int val0, int val1, int val2) 180646492Swpaul{ 1807178354Ssam int i, s = 0; 1808178354Ssam 1809123098Simp if (sc->wi_gone) 1810123098Simp return (ENODEV); 1811123098Simp 1812109323Ssam /* wait for the busy bit to clear */ 1813123339Simp for (i = sc->wi_cmd_count; i > 0; i--) { /* 500ms */ 1814116206Simp if (!(CSR_READ_2(sc, WI_COMMAND) & WI_CMD_BUSY)) 1815109323Ssam break; 1816123098Simp DELAY(1*1000); /* 1ms */ 1817109323Ssam } 1818109323Ssam if (i == 0) { 1819178354Ssam device_printf(sc->sc_dev, "%s: busy bit won't clear, cmd 0x%x\n", 1820178354Ssam __func__, cmd); 1821123098Simp sc->wi_gone = 1; 1822109323Ssam return(ETIMEDOUT); 1823109323Ssam } 182490580Sbrooks 1825109323Ssam CSR_WRITE_2(sc, WI_PARAM0, val0); 1826109323Ssam CSR_WRITE_2(sc, WI_PARAM1, val1); 1827109323Ssam CSR_WRITE_2(sc, WI_PARAM2, val2); 1828109323Ssam CSR_WRITE_2(sc, WI_COMMAND, cmd); 182990580Sbrooks 1830109323Ssam if (cmd == WI_CMD_INI) { 1831109323Ssam /* XXX: should sleep here. */ 1832116206Simp DELAY(100*1000); /* 100ms delay for init */ 1833109323Ssam } 1834109323Ssam for (i = 0; i < WI_TIMEOUT; i++) { 1835109323Ssam /* 1836109323Ssam * Wait for 'command complete' bit to be 1837109323Ssam * set in the event status register. 1838109323Ssam */ 1839109323Ssam s = CSR_READ_2(sc, WI_EVENT_STAT); 1840109323Ssam if (s & WI_EV_CMD) { 1841109323Ssam /* Ack the event and read result code. */ 1842109323Ssam s = CSR_READ_2(sc, WI_STATUS); 1843109323Ssam CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_CMD); 1844109323Ssam if (s & WI_STAT_CMD_RESULT) { 1845109323Ssam return(EIO); 1846109323Ssam } 1847109323Ssam break; 184890580Sbrooks } 1849109323Ssam DELAY(WI_DELAY); 1850109323Ssam } 185190580Sbrooks 1852109323Ssam if (i == WI_TIMEOUT) { 1853178354Ssam device_printf(sc->sc_dev, "%s: timeout on cmd 0x%04x; " 1854178354Ssam "event status 0x%04x\n", __func__, cmd, s); 1855123098Simp if (s == 0xffff) 1856123098Simp sc->wi_gone = 1; 1857109323Ssam return(ETIMEDOUT); 185853702Swpaul } 1859109323Ssam return (0); 1860109323Ssam} 186153702Swpaul 1862109323Ssamstatic int 1863109323Ssamwi_seek_bap(struct wi_softc *sc, int id, int off) 1864109323Ssam{ 1865109323Ssam int i, status; 186690580Sbrooks 1867109323Ssam CSR_WRITE_2(sc, WI_SEL0, id); 1868109323Ssam CSR_WRITE_2(sc, WI_OFF0, off); 186990580Sbrooks 1870109323Ssam for (i = 0; ; i++) { 1871109323Ssam status = CSR_READ_2(sc, WI_OFF0); 1872109323Ssam if ((status & WI_OFF_BUSY) == 0) 1873109323Ssam break; 1874109323Ssam if (i == WI_TIMEOUT) { 1875178354Ssam device_printf(sc->sc_dev, "%s: timeout, id %x off %x\n", 1876178354Ssam __func__, id, off); 1877109323Ssam sc->sc_bap_off = WI_OFF_ERR; /* invalidate */ 1878123098Simp if (status == 0xffff) 1879123098Simp sc->wi_gone = 1; 1880109323Ssam return ETIMEDOUT; 1881109323Ssam } 1882109323Ssam DELAY(1); 188353702Swpaul } 1884109323Ssam if (status & WI_OFF_ERR) { 1885178354Ssam device_printf(sc->sc_dev, "%s: error, id %x off %x\n", 1886178354Ssam __func__, id, off); 1887109323Ssam sc->sc_bap_off = WI_OFF_ERR; /* invalidate */ 1888109323Ssam return EIO; 1889109323Ssam } 1890109323Ssam sc->sc_bap_id = id; 1891109323Ssam sc->sc_bap_off = off; 1892109323Ssam return 0; 189353702Swpaul} 189453702Swpaul 1895109323Ssamstatic int 1896109323Ssamwi_read_bap(struct wi_softc *sc, int id, int off, void *buf, int buflen) 189753702Swpaul{ 1898109323Ssam u_int16_t *ptr; 1899109323Ssam int i, error, cnt; 190053702Swpaul 1901109323Ssam if (buflen == 0) 1902109323Ssam return 0; 1903109323Ssam if (id != sc->sc_bap_id || off != sc->sc_bap_off) { 1904109323Ssam if ((error = wi_seek_bap(sc, id, off)) != 0) 1905109323Ssam return error; 190675219Salfred } 1907109323Ssam cnt = (buflen + 1) / 2; 1908109323Ssam ptr = (u_int16_t *)buf; 1909109323Ssam for (i = 0; i < cnt; i++) 1910109323Ssam *ptr++ = CSR_READ_2(sc, WI_DATA0); 1911109323Ssam sc->sc_bap_off += cnt * 2; 1912109323Ssam return 0; 191353702Swpaul} 191453702Swpaul 1915109323Ssamstatic int 1916109323Ssamwi_write_bap(struct wi_softc *sc, int id, int off, void *buf, int buflen) 191753702Swpaul{ 1918109323Ssam u_int16_t *ptr; 1919109323Ssam int i, error, cnt; 192046492Swpaul 1921109323Ssam if (buflen == 0) 1922109323Ssam return 0; 192346492Swpaul 1924109323Ssam if (id != sc->sc_bap_id || off != sc->sc_bap_off) { 1925109323Ssam if ((error = wi_seek_bap(sc, id, off)) != 0) 1926109323Ssam return error; 1927109323Ssam } 1928109323Ssam cnt = (buflen + 1) / 2; 1929109323Ssam ptr = (u_int16_t *)buf; 1930109323Ssam for (i = 0; i < cnt; i++) 1931109323Ssam CSR_WRITE_2(sc, WI_DATA0, ptr[i]); 1932109323Ssam sc->sc_bap_off += cnt * 2; 1933109323Ssam 1934109323Ssam return 0; 193546492Swpaul} 193653702Swpaul 1937109323Ssamstatic int 1938109323Ssamwi_mwrite_bap(struct wi_softc *sc, int id, int off, struct mbuf *m0, int totlen) 1939109323Ssam{ 1940109323Ssam int error, len; 1941109323Ssam struct mbuf *m; 194253702Swpaul 1943109323Ssam for (m = m0; m != NULL && totlen > 0; m = m->m_next) { 1944109323Ssam if (m->m_len == 0) 1945109323Ssam continue; 194653702Swpaul 1947109323Ssam len = min(m->m_len, totlen); 194853702Swpaul 1949109323Ssam if (((u_long)m->m_data) % 2 != 0 || len % 2 != 0) { 1950109323Ssam m_copydata(m, 0, totlen, (caddr_t)&sc->sc_txbuf); 1951109323Ssam return wi_write_bap(sc, id, off, (caddr_t)&sc->sc_txbuf, 1952109323Ssam totlen); 1953109323Ssam } 195453702Swpaul 1955109323Ssam if ((error = wi_write_bap(sc, id, off, m->m_data, len)) != 0) 1956109323Ssam return error; 195753702Swpaul 1958109323Ssam off += m->m_len; 1959109323Ssam totlen -= len; 1960109323Ssam } 1961109323Ssam return 0; 1962109323Ssam} 196353702Swpaul 1964109323Ssamstatic int 1965109323Ssamwi_alloc_fid(struct wi_softc *sc, int len, int *idp) 196653702Swpaul{ 196753702Swpaul int i; 196853702Swpaul 1969109323Ssam if (wi_cmd(sc, WI_CMD_ALLOC_MEM, len, 0, 0)) { 1970178354Ssam device_printf(sc->sc_dev, "%s: failed to allocate %d bytes on NIC\n", 1971178354Ssam __func__, len); 1972109323Ssam return ENOMEM; 197353702Swpaul } 197453702Swpaul 1975109323Ssam for (i = 0; i < WI_TIMEOUT; i++) { 1976109323Ssam if (CSR_READ_2(sc, WI_EVENT_STAT) & WI_EV_ALLOC) 1977109323Ssam break; 1978109323Ssam DELAY(1); 197953702Swpaul } 1980144167Ssam if (i == WI_TIMEOUT) { 1981178354Ssam device_printf(sc->sc_dev, "%s: timeout in alloc\n", __func__); 1982144167Ssam return ETIMEDOUT; 1983144167Ssam } 1984109323Ssam *idp = CSR_READ_2(sc, WI_ALLOC_FID); 1985109323Ssam CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_ALLOC); 1986109323Ssam return 0; 1987109323Ssam} 198853702Swpaul 1989109323Ssamstatic int 1990109323Ssamwi_read_rid(struct wi_softc *sc, int rid, void *buf, int *buflenp) 1991109323Ssam{ 1992109323Ssam int error, len; 1993109323Ssam u_int16_t ltbuf[2]; 199453702Swpaul 1995109323Ssam /* Tell the NIC to enter record read mode. */ 1996109323Ssam error = wi_cmd(sc, WI_CMD_ACCESS | WI_ACCESS_READ, rid, 0, 0); 1997109323Ssam if (error) 1998109323Ssam return error; 199953702Swpaul 2000109323Ssam error = wi_read_bap(sc, rid, 0, ltbuf, sizeof(ltbuf)); 2001109323Ssam if (error) 2002109323Ssam return error; 200353702Swpaul 2004109323Ssam if (le16toh(ltbuf[1]) != rid) { 2005109323Ssam device_printf(sc->sc_dev, "record read mismatch, rid=%x, got=%x\n", 2006109323Ssam rid, le16toh(ltbuf[1])); 2007109323Ssam return EIO; 200853702Swpaul } 2009109323Ssam len = (le16toh(ltbuf[0]) - 1) * 2; /* already got rid */ 2010109323Ssam if (*buflenp < len) { 2011109323Ssam device_printf(sc->sc_dev, "record buffer is too small, " 2012109323Ssam "rid=%x, size=%d, len=%d\n", 2013109323Ssam rid, *buflenp, len); 2014109323Ssam return ENOSPC; 201553702Swpaul } 2016109323Ssam *buflenp = len; 2017109323Ssam return wi_read_bap(sc, rid, sizeof(ltbuf), buf, len); 2018109323Ssam} 201953702Swpaul 2020109323Ssamstatic int 2021109323Ssamwi_write_rid(struct wi_softc *sc, int rid, void *buf, int buflen) 2022109323Ssam{ 2023109323Ssam int error; 2024109323Ssam u_int16_t ltbuf[2]; 202553702Swpaul 2026109323Ssam ltbuf[0] = htole16((buflen + 1) / 2 + 1); /* includes rid */ 2027109323Ssam ltbuf[1] = htole16(rid); 202853702Swpaul 2029109323Ssam error = wi_write_bap(sc, rid, 0, ltbuf, sizeof(ltbuf)); 2030178354Ssam if (error) { 2031178354Ssam device_printf(sc->sc_dev, "%s: bap0 write failure, rid 0x%x\n", 2032178354Ssam __func__, rid); 2033109323Ssam return error; 2034178354Ssam } 2035109323Ssam error = wi_write_bap(sc, rid, sizeof(ltbuf), buf, buflen); 2036178354Ssam if (error) { 2037178354Ssam device_printf(sc->sc_dev, "%s: bap1 write failure, rid 0x%x\n", 2038178354Ssam __func__, rid); 2039109323Ssam return error; 2040178354Ssam } 2041102206Simp 2042109323Ssam return wi_cmd(sc, WI_CMD_ACCESS | WI_ACCESS_WRITE, rid, 0, 0); 204353702Swpaul} 204477217Sphk 204588546Salfredstatic int 2046178354Ssamwi_write_appie(struct wi_softc *sc, int rid, const struct ieee80211_appie *ie) 204777217Sphk{ 2048178354Ssam /* NB: 42 bytes is probably ok to have on the stack */ 2049178354Ssam char buf[sizeof(uint16_t) + 40]; 205077217Sphk 2051178354Ssam if (ie->ie_len > 40) 2052178354Ssam return EINVAL; 2053178354Ssam /* NB: firmware requires 16-bit ie length before ie data */ 2054178354Ssam *(uint16_t *) buf = htole16(ie->ie_len); 2055178354Ssam memcpy(buf + sizeof(uint16_t), ie->ie_data, ie->ie_len); 2056178354Ssam return wi_write_rid(sc, rid, buf, ie->ie_len + sizeof(uint16_t)); 205777217Sphk} 205877217Sphk 2059109323Ssamint 2060109323Ssamwi_alloc(device_t dev, int rid) 2061109323Ssam{ 2062109323Ssam struct wi_softc *sc = device_get_softc(dev); 2063109323Ssam 2064109323Ssam if (sc->wi_bus_type != WI_BUS_PCI_NATIVE) { 2065109323Ssam sc->iobase_rid = rid; 2066109323Ssam sc->iobase = bus_alloc_resource(dev, SYS_RES_IOPORT, 2067109323Ssam &sc->iobase_rid, 0, ~0, (1 << 6), 2068109323Ssam rman_make_alignment_flags(1 << 6) | RF_ACTIVE); 2069178354Ssam if (sc->iobase == NULL) { 2070109323Ssam device_printf(dev, "No I/O space?!\n"); 2071178354Ssam return ENXIO; 207298440Simp } 2073109323Ssam 2074109323Ssam sc->wi_io_addr = rman_get_start(sc->iobase); 2075109323Ssam sc->wi_btag = rman_get_bustag(sc->iobase); 2076109323Ssam sc->wi_bhandle = rman_get_bushandle(sc->iobase); 2077109323Ssam } else { 2078109323Ssam sc->mem_rid = rid; 2079127135Snjl sc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, 2080127135Snjl &sc->mem_rid, RF_ACTIVE); 2081178354Ssam if (sc->mem == NULL) { 2082109323Ssam device_printf(dev, "No Mem space on prism2.5?\n"); 2083178354Ssam return ENXIO; 208477217Sphk } 2085109323Ssam 2086109323Ssam sc->wi_btag = rman_get_bustag(sc->mem); 2087109323Ssam sc->wi_bhandle = rman_get_bushandle(sc->mem); 208877217Sphk } 208977217Sphk 2090109323Ssam sc->irq_rid = 0; 2091127135Snjl sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irq_rid, 2092127135Snjl RF_ACTIVE | 2093109323Ssam ((sc->wi_bus_type == WI_BUS_PCCARD) ? 0 : RF_SHAREABLE)); 2094178354Ssam if (sc->irq == NULL) { 2095109323Ssam wi_free(dev); 2096109323Ssam device_printf(dev, "No irq?!\n"); 2097178354Ssam return ENXIO; 209877217Sphk } 2099109323Ssam 2100109323Ssam sc->sc_dev = dev; 2101109323Ssam sc->sc_unit = device_get_unit(dev); 2102178354Ssam return 0; 210377217Sphk} 210493359Simp 2105109323Ssamvoid 2106109323Ssamwi_free(device_t dev) 2107109323Ssam{ 2108109323Ssam struct wi_softc *sc = device_get_softc(dev); 2109109323Ssam 2110109323Ssam if (sc->iobase != NULL) { 2111109323Ssam bus_release_resource(dev, SYS_RES_IOPORT, sc->iobase_rid, sc->iobase); 2112109323Ssam sc->iobase = NULL; 2113109323Ssam } 2114109323Ssam if (sc->irq != NULL) { 2115109323Ssam bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid, sc->irq); 2116109323Ssam sc->irq = NULL; 2117109323Ssam } 2118109323Ssam if (sc->mem != NULL) { 2119109323Ssam bus_release_resource(dev, SYS_RES_MEMORY, sc->mem_rid, sc->mem); 2120109323Ssam sc->mem = NULL; 2121109323Ssam } 2122109323Ssam} 2123