if_wi.c revision 192492
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 192492 2009-05-20 22:28:55Z imp $"); 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); 124192468Ssamstatic int wi_newstate_hostap(struct ieee80211vap *, enum ieee80211_state, 125192468Ssam int); 126178354Ssamstatic void wi_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m, 127192468Ssam int subtype, int rssi, int nf); 128178354Ssamstatic int wi_reset(struct wi_softc *); 129165089Ssamstatic void wi_watchdog(void *); 130109323Ssamstatic int wi_ioctl(struct ifnet *, u_long, caddr_t); 131109323Ssamstatic void wi_media_status(struct ifnet *, struct ifmediareq *); 13246492Swpaul 133109323Ssamstatic void wi_rx_intr(struct wi_softc *); 134109323Ssamstatic void wi_tx_intr(struct wi_softc *); 135109323Ssamstatic void wi_tx_ex_intr(struct wi_softc *); 136178354Ssam 137109323Ssamstatic void wi_info_intr(struct wi_softc *); 13846492Swpaul 139178354Ssamstatic int wi_write_txrate(struct wi_softc *, struct ieee80211vap *); 140178354Ssamstatic int wi_write_wep(struct wi_softc *, struct ieee80211vap *); 141109323Ssamstatic int wi_write_multi(struct wi_softc *); 142178354Ssamstatic void wi_update_mcast(struct ifnet *); 143192468Ssamstatic void wi_update_promisc(struct ifnet *); 144109323Ssamstatic int wi_alloc_fid(struct wi_softc *, int, int *); 145109323Ssamstatic void wi_read_nicid(struct wi_softc *); 146109323Ssamstatic int wi_write_ssid(struct wi_softc *, int, u_int8_t *, int); 14753702Swpaul 148192492Simpstatic int wi_cmd(struct wi_softc *, int, int, int, int); 149109323Ssamstatic int wi_seek_bap(struct wi_softc *, int, int); 150109323Ssamstatic int wi_read_bap(struct wi_softc *, int, int, void *, int); 151109323Ssamstatic int wi_write_bap(struct wi_softc *, int, int, void *, int); 152109323Ssamstatic int wi_mwrite_bap(struct wi_softc *, int, int, struct mbuf *, int); 153109323Ssamstatic int wi_read_rid(struct wi_softc *, int, void *, int *); 154109323Ssamstatic int wi_write_rid(struct wi_softc *, int, void *, int); 155178354Ssamstatic int wi_write_appie(struct wi_softc *, int, const struct ieee80211_appie *); 15677217Sphk 157170530Ssamstatic void wi_scan_start(struct ieee80211com *); 158170530Ssamstatic void wi_scan_end(struct ieee80211com *); 159170530Ssamstatic void wi_set_channel(struct ieee80211com *); 160170530Ssam 161109323Ssamstatic __inline int 162109323Ssamwi_write_val(struct wi_softc *sc, int rid, u_int16_t val) 163109323Ssam{ 16453702Swpaul 165109323Ssam val = htole16(val); 166109323Ssam return wi_write_rid(sc, rid, &val, sizeof(val)); 167109323Ssam} 168109323Ssam 169109592SsamSYSCTL_NODE(_hw, OID_AUTO, wi, CTLFLAG_RD, 0, "Wireless driver parameters"); 170109592Ssam 171109323Ssamstatic struct timeval lasttxerror; /* time of last tx error msg */ 172109323Ssamstatic int curtxeps; /* current tx error msgs/sec */ 173111559Ssamstatic int wi_txerate = 0; /* tx error rate: max msgs/sec */ 174109592SsamSYSCTL_INT(_hw_wi, OID_AUTO, txerate, CTLFLAG_RW, &wi_txerate, 175111559Ssam 0, "max tx error msgs/sec; 0 to disable msgs"); 176109323Ssam 177109323Ssam#define WI_DEBUG 178109323Ssam#ifdef WI_DEBUG 179109323Ssamstatic int wi_debug = 0; 180109592SsamSYSCTL_INT(_hw_wi, OID_AUTO, debug, CTLFLAG_RW, &wi_debug, 181109592Ssam 0, "control debugging printfs"); 182109323Ssam#define DPRINTF(X) if (wi_debug) printf X 183109323Ssam#else 184109323Ssam#define DPRINTF(X) 185109323Ssam#endif 186109323Ssam 187109323Ssam#define WI_INTRS (WI_EV_RX | WI_EV_ALLOC | WI_EV_INFO) 188109323Ssam 18993825Simpstruct wi_card_ident wi_card_ident[] = { 19093825Simp /* CARD_ID CARD_NAME FIRM_TYPE */ 19193825Simp { WI_NIC_LUCENT_ID, WI_NIC_LUCENT_STR, WI_LUCENT }, 19293825Simp { WI_NIC_SONY_ID, WI_NIC_SONY_STR, WI_LUCENT }, 19393825Simp { WI_NIC_LUCENT_EMB_ID, WI_NIC_LUCENT_EMB_STR, WI_LUCENT }, 19493825Simp { WI_NIC_EVB2_ID, WI_NIC_EVB2_STR, WI_INTERSIL }, 19593825Simp { WI_NIC_HWB3763_ID, WI_NIC_HWB3763_STR, WI_INTERSIL }, 19693825Simp { WI_NIC_HWB3163_ID, WI_NIC_HWB3163_STR, WI_INTERSIL }, 19793825Simp { WI_NIC_HWB3163B_ID, WI_NIC_HWB3163B_STR, WI_INTERSIL }, 19893825Simp { WI_NIC_EVB3_ID, WI_NIC_EVB3_STR, WI_INTERSIL }, 19993825Simp { WI_NIC_HWB1153_ID, WI_NIC_HWB1153_STR, WI_INTERSIL }, 20093825Simp { WI_NIC_P2_SST_ID, WI_NIC_P2_SST_STR, WI_INTERSIL }, 20193825Simp { WI_NIC_EVB2_SST_ID, WI_NIC_EVB2_SST_STR, WI_INTERSIL }, 20293825Simp { WI_NIC_3842_EVA_ID, WI_NIC_3842_EVA_STR, WI_INTERSIL }, 20393825Simp { WI_NIC_3842_PCMCIA_AMD_ID, WI_NIC_3842_PCMCIA_STR, WI_INTERSIL }, 20493825Simp { WI_NIC_3842_PCMCIA_SST_ID, WI_NIC_3842_PCMCIA_STR, WI_INTERSIL }, 205101355Simp { WI_NIC_3842_PCMCIA_ATL_ID, WI_NIC_3842_PCMCIA_STR, WI_INTERSIL }, 206101355Simp { WI_NIC_3842_PCMCIA_ATS_ID, WI_NIC_3842_PCMCIA_STR, WI_INTERSIL }, 20793825Simp { WI_NIC_3842_MINI_AMD_ID, WI_NIC_3842_MINI_STR, WI_INTERSIL }, 20893825Simp { WI_NIC_3842_MINI_SST_ID, WI_NIC_3842_MINI_STR, WI_INTERSIL }, 209101355Simp { WI_NIC_3842_MINI_ATL_ID, WI_NIC_3842_MINI_STR, WI_INTERSIL }, 210101355Simp { WI_NIC_3842_MINI_ATS_ID, WI_NIC_3842_MINI_STR, WI_INTERSIL }, 21193825Simp { WI_NIC_3842_PCI_AMD_ID, WI_NIC_3842_PCI_STR, WI_INTERSIL }, 21293825Simp { WI_NIC_3842_PCI_SST_ID, WI_NIC_3842_PCI_STR, WI_INTERSIL }, 213101355Simp { WI_NIC_3842_PCI_ATS_ID, WI_NIC_3842_PCI_STR, WI_INTERSIL }, 214101355Simp { WI_NIC_3842_PCI_ATL_ID, WI_NIC_3842_PCI_STR, WI_INTERSIL }, 21593825Simp { WI_NIC_P3_PCMCIA_AMD_ID, WI_NIC_P3_PCMCIA_STR, WI_INTERSIL }, 21693825Simp { WI_NIC_P3_PCMCIA_SST_ID, WI_NIC_P3_PCMCIA_STR, WI_INTERSIL }, 217101355Simp { WI_NIC_P3_PCMCIA_ATL_ID, WI_NIC_P3_PCMCIA_STR, WI_INTERSIL }, 218101355Simp { WI_NIC_P3_PCMCIA_ATS_ID, WI_NIC_P3_PCMCIA_STR, WI_INTERSIL }, 21993825Simp { WI_NIC_P3_MINI_AMD_ID, WI_NIC_P3_MINI_STR, WI_INTERSIL }, 22093825Simp { WI_NIC_P3_MINI_SST_ID, WI_NIC_P3_MINI_STR, WI_INTERSIL }, 221101355Simp { WI_NIC_P3_MINI_ATL_ID, WI_NIC_P3_MINI_STR, WI_INTERSIL }, 222101355Simp { WI_NIC_P3_MINI_ATS_ID, WI_NIC_P3_MINI_STR, WI_INTERSIL }, 22393825Simp { 0, NULL, 0 }, 22493825Simp}; 22593825Simp 226180919Simpstatic char *wi_firmware_names[] = { "none", "Hermes", "Intersil", "Symbol" }; 227180919Simp 228109323Ssamdevclass_t wi_devclass; 22946492Swpaul 23093611Simpint 231109323Ssamwi_attach(device_t dev) 23274906Salfred{ 233109323Ssam struct wi_softc *sc = device_get_softc(dev); 234178354Ssam struct ieee80211com *ic; 235147256Sbrooks struct ifnet *ifp; 236116951Ssam int i, nrates, buflen; 237109323Ssam u_int16_t val; 238109323Ssam u_int8_t ratebuf[2 + IEEE80211_RATE_SIZE]; 239116951Ssam struct ieee80211_rateset *rs; 240180919Simp struct sysctl_ctx_list *sctx; 241180919Simp struct sysctl_oid *soid; 242109323Ssam static const u_int8_t empty_macaddr[IEEE80211_ADDR_LEN] = { 243109323Ssam 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 244109323Ssam }; 245109323Ssam int error; 246190526Ssam uint8_t macaddr[IEEE80211_ADDR_LEN]; 24774906Salfred 248178354Ssam ifp = sc->sc_ifp = if_alloc(IFT_IEEE80211); 249147256Sbrooks if (ifp == NULL) { 250147256Sbrooks device_printf(dev, "can not if_alloc\n"); 251147256Sbrooks wi_free(dev); 252178354Ssam return ENOSPC; 253147256Sbrooks } 254178354Ssam ic = ifp->if_l2com; 255147256Sbrooks 256138571Ssam sc->sc_firmware_type = WI_NOTYPE; 257123339Simp sc->wi_cmd_count = 500; 25846492Swpaul /* Reset the NIC. */ 259178354Ssam if (wi_reset(sc) != 0) { 260178354Ssam wi_free(dev); 261109323Ssam return ENXIO; /* XXX */ 262178354Ssam } 26346492Swpaul 264178354Ssam /* Read NIC identification */ 265178354Ssam wi_read_nicid(sc); 266178354Ssam switch (sc->sc_firmware_type) { 267178354Ssam case WI_LUCENT: 268178354Ssam if (sc->sc_sta_firmware_ver < 60006) 269178354Ssam goto reject; 270178354Ssam break; 271178354Ssam case WI_INTERSIL: 272178354Ssam if (sc->sc_sta_firmware_ver < 800) 273178354Ssam goto reject; 274178354Ssam break; 275178354Ssam default: 276178354Ssam reject: 277178354Ssam device_printf(dev, "Sorry, this card is not supported " 278178354Ssam "(type %d, firmware ver %d)\n", 279178354Ssam sc->sc_firmware_type, sc->sc_sta_firmware_ver); 280178354Ssam wi_free(dev); 281178354Ssam return EOPNOTSUPP; 282178354Ssam } 283178354Ssam 284180919Simp /* Export info about the device via sysctl */ 285180919Simp sctx = device_get_sysctl_ctx(dev); 286180919Simp soid = device_get_sysctl_tree(dev); 287180919Simp SYSCTL_ADD_STRING(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, 288180919Simp "firmware_type", CTLFLAG_RD, 289180919Simp wi_firmware_names[sc->sc_firmware_type], 0, 290180919Simp "Firmware type string"); 291180919Simp SYSCTL_ADD_INT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "sta_version", 292180919Simp CTLFLAG_RD, &sc->sc_sta_firmware_ver, 0, 293180919Simp "Station Firmware version"); 294180919Simp if (sc->sc_firmware_type == WI_INTERSIL) 295180919Simp SYSCTL_ADD_INT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, 296180919Simp "pri_version", CTLFLAG_RD, &sc->sc_pri_firmware_ver, 0, 297180919Simp "Primary Firmware version"); 298180919Simp SYSCTL_ADD_XINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "nic_id", 299180919Simp CTLFLAG_RD, &sc->sc_nic_id, 0, "NIC id"); 300180919Simp SYSCTL_ADD_STRING(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "nic_name", 301180919Simp CTLFLAG_RD, sc->sc_nic_name, 0, "NIC name"); 302180919Simp 303178354Ssam mtx_init(&sc->sc_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK, 304178354Ssam MTX_DEF | MTX_RECURSE); 305178354Ssam callout_init_mtx(&sc->sc_watchdog, &sc->sc_mtx, 0); 306178354Ssam 30776438Swpaul /* 30876438Swpaul * Read the station address. 30976438Swpaul * And do it twice. I've seen PRISM-based cards that return 31076438Swpaul * an error when trying to read it the first time, which causes 31176438Swpaul * the probe to fail. 31276438Swpaul */ 313109323Ssam buflen = IEEE80211_ADDR_LEN; 314190526Ssam error = wi_read_rid(sc, WI_RID_MAC_NODE, macaddr, &buflen); 315109323Ssam if (error != 0) { 316109323Ssam buflen = IEEE80211_ADDR_LEN; 317190526Ssam error = wi_read_rid(sc, WI_RID_MAC_NODE, macaddr, &buflen); 318109323Ssam } 319190526Ssam if (error || IEEE80211_ADDR_EQ(macaddr, empty_macaddr)) { 320109323Ssam if (error != 0) 321109323Ssam device_printf(dev, "mac read failed %d\n", error); 322148714Simp else { 323109323Ssam device_printf(dev, "mac read failed (all zeros)\n"); 324148714Simp error = ENXIO; 325148714Simp } 32675149Simp wi_free(dev); 32775149Simp return (error); 32875149Simp } 32946492Swpaul 330178354Ssam ifp->if_softc = sc; 331121816Sbrooks if_initname(ifp, device_get_name(dev), device_get_unit(dev)); 33246492Swpaul ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 33346492Swpaul ifp->if_ioctl = wi_ioctl; 33446492Swpaul ifp->if_start = wi_start; 33546492Swpaul ifp->if_init = wi_init; 336132986Smlaier IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN); 337132986Smlaier ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN; 338132986Smlaier IFQ_SET_READY(&ifp->if_snd); 33946492Swpaul 340138571Ssam ic->ic_ifp = ifp; 341109323Ssam ic->ic_phytype = IEEE80211_T_DS; 342109323Ssam ic->ic_opmode = IEEE80211_M_STA; 343178957Ssam ic->ic_caps = IEEE80211_C_STA 344178957Ssam | IEEE80211_C_PMGT 345178354Ssam | IEEE80211_C_MONITOR 346138571Ssam ; 34746492Swpaul 348116951Ssam /* 349116951Ssam * Query the card for available channels and setup the 350116951Ssam * channel table. We assume these are all 11b channels. 351116951Ssam */ 352109323Ssam buflen = sizeof(val); 353109323Ssam if (wi_read_rid(sc, WI_RID_CHANNEL_LIST, &val, &buflen) != 0) 354109323Ssam val = htole16(0x1fff); /* assume 1-11 */ 355116951Ssam KASSERT(val != 0, ("wi_attach: no available channels listed!")); 356116951Ssam 357116951Ssam val <<= 1; /* shift for base 1 indices */ 358116951Ssam for (i = 1; i < 16; i++) { 359170530Ssam struct ieee80211_channel *c; 360170530Ssam 361144986Smdodd if (!isset((u_int8_t*)&val, i)) 362144986Smdodd continue; 363170530Ssam c = &ic->ic_channels[ic->ic_nchans++]; 364170530Ssam c->ic_freq = ieee80211_ieee2mhz(i, IEEE80211_CHAN_B); 365170530Ssam c->ic_flags = IEEE80211_CHAN_B; 366170530Ssam c->ic_ieee = i; 367178354Ssam /* XXX txpowers? */ 368109323Ssam } 36946492Swpaul 37046563Swpaul /* 37198440Simp * Set flags based on firmware version. 37298440Simp */ 37398440Simp switch (sc->sc_firmware_type) { 37498440Simp case WI_LUCENT: 375112363Simp sc->sc_ntxbuf = 1; 376178354Ssam ic->ic_caps |= IEEE80211_C_IBSS; 377119784Ssam 378178354Ssam sc->sc_ibss_port = WI_PORTTYPE_BSS; 379178354Ssam sc->sc_monitor_port = WI_PORTTYPE_ADHOC; 380119784Ssam sc->sc_min_rssi = WI_LUCENT_MIN_RSSI; 381119784Ssam sc->sc_max_rssi = WI_LUCENT_MAX_RSSI; 382119784Ssam sc->sc_dbm_offset = WI_LUCENT_DBM_OFFSET; 38398440Simp break; 38498440Simp case WI_INTERSIL: 385112363Simp sc->sc_ntxbuf = WI_NTXBUF; 386178354Ssam sc->sc_flags |= WI_FLAGS_HAS_FRAGTHR 387178354Ssam | WI_FLAGS_HAS_ROAMING; 388123339Simp /* 389123339Simp * Old firmware are slow, so give peace a chance. 390123339Simp */ 391123339Simp if (sc->sc_sta_firmware_ver < 10000) 392123339Simp sc->wi_cmd_count = 5000; 393109323Ssam if (sc->sc_sta_firmware_ver > 10101) 394109323Ssam sc->sc_flags |= WI_FLAGS_HAS_DBMADJUST; 395178354Ssam ic->ic_caps |= IEEE80211_C_IBSS; 396109396Simp /* 397109396Simp * version 0.8.3 and newer are the only ones that are known 398109396Simp * to currently work. Earlier versions can be made to work, 399178354Ssam * at least according to the Linux driver but we require 400178354Ssam * monitor mode so this is irrelevant. 401109396Simp */ 402178354Ssam ic->ic_caps |= IEEE80211_C_HOSTAP; 403178354Ssam if (sc->sc_sta_firmware_ver >= 10603) 404178354Ssam sc->sc_flags |= WI_FLAGS_HAS_ENHSECURITY; 405178354Ssam if (sc->sc_sta_firmware_ver >= 10700) { 406178354Ssam /* 407178354Ssam * 1.7.0+ have the necessary support for sta mode WPA. 408178354Ssam */ 409178354Ssam sc->sc_flags |= WI_FLAGS_HAS_WPASUPPORT; 410178354Ssam ic->ic_caps |= IEEE80211_C_WPA; 411178354Ssam } 412119784Ssam 413178354Ssam sc->sc_ibss_port = WI_PORTTYPE_IBSS; 414178354Ssam sc->sc_monitor_port = WI_PORTTYPE_APSILENT; 415119784Ssam sc->sc_min_rssi = WI_PRISM_MIN_RSSI; 416119784Ssam sc->sc_max_rssi = WI_PRISM_MAX_RSSI; 417119784Ssam sc->sc_dbm_offset = WI_PRISM_DBM_OFFSET; 41898440Simp break; 41998440Simp } 42098440Simp 42198440Simp /* 42256965Swpaul * Find out if we support WEP on this card. 42356965Swpaul */ 424109323Ssam buflen = sizeof(val); 425109323Ssam if (wi_read_rid(sc, WI_RID_WEP_AVAIL, &val, &buflen) == 0 && 426109323Ssam val != htole16(0)) 427178354Ssam ic->ic_cryptocaps |= IEEE80211_CRYPTO_WEP; 42856965Swpaul 429109323Ssam /* Find supported rates. */ 430109323Ssam buflen = sizeof(ratebuf); 431116951Ssam rs = &ic->ic_sup_rates[IEEE80211_MODE_11B]; 432109323Ssam if (wi_read_rid(sc, WI_RID_DATA_RATES, ratebuf, &buflen) == 0) { 433116951Ssam nrates = le16toh(*(u_int16_t *)ratebuf); 434116951Ssam if (nrates > IEEE80211_RATE_MAXSIZE) 435116951Ssam nrates = IEEE80211_RATE_MAXSIZE; 436116951Ssam rs->rs_nrates = 0; 437116951Ssam for (i = 0; i < nrates; i++) 438116951Ssam if (ratebuf[2+i]) 439116951Ssam rs->rs_rates[rs->rs_nrates++] = ratebuf[2+i]; 440109323Ssam } else { 441109323Ssam /* XXX fallback on error? */ 44298440Simp } 44377217Sphk 444109323Ssam buflen = sizeof(val); 445109323Ssam if ((sc->sc_flags & WI_FLAGS_HAS_DBMADJUST) && 446109323Ssam wi_read_rid(sc, WI_RID_DBM_ADJUST, &val, &buflen) == 0) { 447119784Ssam sc->sc_dbm_offset = le16toh(val); 448119784Ssam } 44946492Swpaul 450109323Ssam sc->sc_portnum = WI_DEFAULT_PORT; 45187383Simp 452190526Ssam ieee80211_ifattach(ic, macaddr); 453160991Ssam ic->ic_raw_xmit = wi_raw_xmit; 454170530Ssam ic->ic_scan_start = wi_scan_start; 455170530Ssam ic->ic_scan_end = wi_scan_end; 456170530Ssam ic->ic_set_channel = wi_set_channel; 457170530Ssam 458178354Ssam ic->ic_vap_create = wi_vap_create; 459178354Ssam ic->ic_vap_delete = wi_vap_delete; 460178354Ssam ic->ic_update_mcast = wi_update_mcast; 461192468Ssam ic->ic_update_promisc = wi_update_promisc; 462109323Ssam 463192468Ssam ieee80211_radiotap_attach(ic, 464192468Ssam &sc->sc_tx_th.wt_ihdr, sizeof(sc->sc_tx_th), 465192468Ssam WI_TX_RADIOTAP_PRESENT, 466192468Ssam &sc->sc_rx_th.wr_ihdr, sizeof(sc->sc_rx_th), 467192468Ssam WI_RX_RADIOTAP_PRESENT); 468119784Ssam 469138571Ssam if (bootverbose) 470138571Ssam ieee80211_announce(ic); 471138571Ssam 472180826Simp error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET | INTR_MPSAFE, 473180826Simp NULL, wi_intr, sc, &sc->wi_intrhand); 474180826Simp if (error) { 475180826Simp device_printf(dev, "bus_setup_intr() failed! (%d)\n", error); 476180826Simp ieee80211_ifdetach(ic); 477180826Simp if_free(sc->sc_ifp); 478180826Simp wi_free(dev); 479180826Simp return error; 480180826Simp } 481180826Simp 482109323Ssam return (0); 48387383Simp} 48487383Simp 485109323Ssamint 486109323Ssamwi_detach(device_t dev) 48746492Swpaul{ 488109323Ssam struct wi_softc *sc = device_get_softc(dev); 489147256Sbrooks struct ifnet *ifp = sc->sc_ifp; 490178354Ssam struct ieee80211com *ic = ifp->if_l2com; 49146492Swpaul 492109323Ssam WI_LOCK(sc); 49346492Swpaul 494109323Ssam /* check if device was removed */ 495123098Simp sc->wi_gone |= !bus_child_present(dev); 49646492Swpaul 497178354Ssam wi_stop_locked(sc, 0); 498150678Sru WI_UNLOCK(sc); 499178354Ssam ieee80211_ifdetach(ic); 50046492Swpaul 501109323Ssam bus_teardown_intr(dev, sc->irq, sc->wi_intrhand); 502150306Simp if_free(sc->sc_ifp); 503109323Ssam wi_free(dev); 504109323Ssam mtx_destroy(&sc->sc_mtx); 505109323Ssam return (0); 506109323Ssam} 50793359Simp 508178354Ssamstatic struct ieee80211vap * 509178354Ssamwi_vap_create(struct ieee80211com *ic, 510178354Ssam const char name[IFNAMSIZ], int unit, int opmode, int flags, 511178354Ssam const uint8_t bssid[IEEE80211_ADDR_LEN], 512178354Ssam const uint8_t mac[IEEE80211_ADDR_LEN]) 513109323Ssam{ 514178354Ssam struct wi_softc *sc = ic->ic_ifp->if_softc; 515178354Ssam struct wi_vap *wvp; 516178354Ssam struct ieee80211vap *vap; 51793359Simp 518178354Ssam if (!TAILQ_EMPTY(&ic->ic_vaps)) /* only one at a time */ 519178354Ssam return NULL; 520178354Ssam wvp = (struct wi_vap *) malloc(sizeof(struct wi_vap), 521178354Ssam M_80211_VAP, M_NOWAIT | M_ZERO); 522178354Ssam if (wvp == NULL) 523178354Ssam return NULL; 52493359Simp 525178354Ssam vap = &wvp->wv_vap; 526178354Ssam ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid, mac); 52793359Simp 528178354Ssam vap->iv_max_aid = WI_MAX_AID; 52993359Simp 530178354Ssam switch (opmode) { 531178354Ssam case IEEE80211_M_STA: 532178354Ssam sc->sc_porttype = WI_PORTTYPE_BSS; 533178354Ssam wvp->wv_newstate = vap->iv_newstate; 534178354Ssam vap->iv_newstate = wi_newstate_sta; 535178354Ssam /* need to filter mgt frames to avoid confusing state machine */ 536178354Ssam wvp->wv_recv_mgmt = vap->iv_recv_mgmt; 537178354Ssam vap->iv_recv_mgmt = wi_recv_mgmt; 538109323Ssam break; 539178354Ssam case IEEE80211_M_IBSS: 540178354Ssam sc->sc_porttype = sc->sc_ibss_port; 541178354Ssam wvp->wv_newstate = vap->iv_newstate; 542178354Ssam vap->iv_newstate = wi_newstate_sta; 543109323Ssam break; 544178354Ssam case IEEE80211_M_AHDEMO: 545178354Ssam sc->sc_porttype = WI_PORTTYPE_ADHOC; 546109323Ssam break; 547178354Ssam case IEEE80211_M_HOSTAP: 548178354Ssam sc->sc_porttype = WI_PORTTYPE_HOSTAP; 549178354Ssam wvp->wv_newstate = vap->iv_newstate; 550178354Ssam vap->iv_newstate = wi_newstate_hostap; 551178354Ssam break; 552178354Ssam case IEEE80211_M_MONITOR: 553178354Ssam sc->sc_porttype = sc->sc_monitor_port; 554178354Ssam break; 555178354Ssam default: 556178354Ssam break; 55793359Simp } 558178354Ssam 559178354Ssam /* complete setup */ 560178354Ssam ieee80211_vap_attach(vap, ieee80211_media_change, wi_media_status); 561178354Ssam ic->ic_opmode = opmode; 562178354Ssam return vap; 56346492Swpaul} 56446492Swpaul 565178354Ssamstatic void 566178354Ssamwi_vap_delete(struct ieee80211vap *vap) 567178354Ssam{ 568178354Ssam struct wi_vap *wvp = WI_VAP(vap); 569178354Ssam 570178354Ssam ieee80211_vap_detach(vap); 571178354Ssam free(wvp, M_80211_VAP); 572178354Ssam} 573178354Ssam 574109323Ssamvoid 575109323Ssamwi_shutdown(device_t dev) 57646492Swpaul{ 577109323Ssam struct wi_softc *sc = device_get_softc(dev); 57846492Swpaul 579178354Ssam wi_stop(sc, 1); 58046492Swpaul} 58146492Swpaul 582109323Ssamvoid 583109323Ssamwi_intr(void *arg) 58446492Swpaul{ 585109323Ssam struct wi_softc *sc = arg; 586147256Sbrooks struct ifnet *ifp = sc->sc_ifp; 587112363Simp u_int16_t status; 58846492Swpaul 589109323Ssam WI_LOCK(sc); 59046492Swpaul 591123098Simp if (sc->wi_gone || !sc->sc_enabled || (ifp->if_flags & IFF_UP) == 0) { 592113327Simp CSR_WRITE_2(sc, WI_INT_EN, 0); 593123098Simp CSR_WRITE_2(sc, WI_EVENT_ACK, 0xFFFF); 594109323Ssam WI_UNLOCK(sc); 59546492Swpaul return; 596109323Ssam } 59746492Swpaul 598112363Simp /* Disable interrupts. */ 599112363Simp CSR_WRITE_2(sc, WI_INT_EN, 0); 60046492Swpaul 601112363Simp status = CSR_READ_2(sc, WI_EVENT_STAT); 602112363Simp if (status & WI_EV_RX) 603112363Simp wi_rx_intr(sc); 604112363Simp if (status & WI_EV_ALLOC) 605112363Simp wi_tx_intr(sc); 606112363Simp if (status & WI_EV_TX_EXC) 607112363Simp wi_tx_ex_intr(sc); 608112363Simp if (status & WI_EV_INFO) 609112363Simp wi_info_intr(sc); 610148887Srwatson if ((ifp->if_drv_flags & IFF_DRV_OACTIVE) == 0 && 611132986Smlaier !IFQ_DRV_IS_EMPTY(&ifp->if_snd)) 612165087Ssam wi_start_locked(ifp); 61346492Swpaul 614112363Simp /* Re-enable interrupts. */ 615112363Simp CSR_WRITE_2(sc, WI_INT_EN, WI_INTRS); 61646492Swpaul 617109323Ssam WI_UNLOCK(sc); 61846492Swpaul 61946492Swpaul return; 62046492Swpaul} 62146492Swpaul 622178354Ssamstatic void 623178354Ssamwi_enable(struct wi_softc *sc) 624178354Ssam{ 625178354Ssam /* Enable interrupts */ 626178354Ssam CSR_WRITE_2(sc, WI_INT_EN, WI_INTRS); 627178354Ssam 628178354Ssam /* enable port */ 629178354Ssam wi_cmd(sc, WI_CMD_ENABLE | sc->sc_portnum, 0, 0, 0); 630178354Ssam sc->sc_enabled = 1; 631178354Ssam} 632178354Ssam 633178354Ssamstatic int 634178354Ssamwi_setup_locked(struct wi_softc *sc, int porttype, int mode, 635178354Ssam uint8_t mac[IEEE80211_ADDR_LEN]) 636178354Ssam{ 637178354Ssam int i; 638178354Ssam 639178354Ssam wi_reset(sc); 640178354Ssam 641178354Ssam wi_write_val(sc, WI_RID_PORTTYPE, porttype); 642178354Ssam wi_write_val(sc, WI_RID_CREATE_IBSS, mode); 643178354Ssam wi_write_val(sc, WI_RID_MAX_DATALEN, 2304); 644178354Ssam /* XXX IEEE80211_BPF_NOACK wants 0 */ 645178354Ssam wi_write_val(sc, WI_RID_ALT_RETRY_CNT, 2); 646178354Ssam if (sc->sc_flags & WI_FLAGS_HAS_ROAMING) 647178354Ssam wi_write_val(sc, WI_RID_ROAMING_MODE, 3); /* NB: disabled */ 648178354Ssam 649178354Ssam wi_write_rid(sc, WI_RID_MAC_NODE, mac, IEEE80211_ADDR_LEN); 650178354Ssam 651178354Ssam /* Allocate fids for the card */ 652178354Ssam sc->sc_buflen = IEEE80211_MAX_LEN + sizeof(struct wi_frame); 653178354Ssam for (i = 0; i < sc->sc_ntxbuf; i++) { 654178354Ssam int error = wi_alloc_fid(sc, sc->sc_buflen, 655178354Ssam &sc->sc_txd[i].d_fid); 656178354Ssam if (error) { 657178354Ssam device_printf(sc->sc_dev, 658178354Ssam "tx buffer allocation failed (error %u)\n", 659178354Ssam error); 660178354Ssam return error; 661178354Ssam } 662178354Ssam sc->sc_txd[i].d_len = 0; 663178354Ssam } 664178354Ssam sc->sc_txcur = sc->sc_txnext = 0; 665178354Ssam 666178354Ssam return 0; 667178354Ssam} 668178354Ssam 669178354Ssamstatic void 670178354Ssamwi_init_locked(struct wi_softc *sc) 671178354Ssam{ 672178354Ssam struct ifnet *ifp = sc->sc_ifp; 673178354Ssam int wasenabled; 674178354Ssam 675178354Ssam WI_LOCK_ASSERT(sc); 676178354Ssam 677178354Ssam wasenabled = sc->sc_enabled; 678178354Ssam if (wasenabled) 679178354Ssam wi_stop_locked(sc, 1); 680178354Ssam 681190526Ssam if (wi_setup_locked(sc, sc->sc_porttype, 3, IF_LLADDR(ifp)) != 0) { 682178354Ssam if_printf(ifp, "interface not running\n"); 683178354Ssam wi_stop_locked(sc, 1); 684178354Ssam return; 685178354Ssam } 686178354Ssam 687178354Ssam ifp->if_drv_flags |= IFF_DRV_RUNNING; 688178354Ssam ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 689178354Ssam 690178354Ssam callout_reset(&sc->sc_watchdog, hz, wi_watchdog, sc); 691178354Ssam 692178354Ssam wi_enable(sc); /* Enable desired port */ 693178354Ssam} 694178354Ssam 695109323Ssamvoid 696109323Ssamwi_init(void *arg) 69746492Swpaul{ 698109323Ssam struct wi_softc *sc = arg; 699147256Sbrooks struct ifnet *ifp = sc->sc_ifp; 700178354Ssam struct ieee80211com *ic = ifp->if_l2com; 70146492Swpaul 702178354Ssam WI_LOCK(sc); 703178354Ssam wi_init_locked(sc); 704178354Ssam WI_UNLOCK(sc); 70567092Swpaul 706178931Sthompsa if (ifp->if_drv_flags & IFF_DRV_RUNNING) 707178931Sthompsa ieee80211_start_all(ic); /* start all vap's */ 708178354Ssam} 709170530Ssam 710178354Ssamstatic void 711178354Ssamwi_stop_locked(struct wi_softc *sc, int disable) 712178354Ssam{ 713178354Ssam struct ifnet *ifp = sc->sc_ifp; 71446492Swpaul 715178354Ssam WI_LOCK_ASSERT(sc); 71646492Swpaul 717178354Ssam if (sc->sc_enabled && !sc->wi_gone) { 718178354Ssam CSR_WRITE_2(sc, WI_INT_EN, 0); 719178354Ssam wi_cmd(sc, WI_CMD_DISABLE | sc->sc_portnum, 0, 0, 0); 720178354Ssam if (disable) 721178354Ssam sc->sc_enabled = 0; 722178354Ssam } else if (sc->wi_gone && disable) /* gone --> not enabled */ 723178354Ssam sc->sc_enabled = 0; 724178354Ssam 725178354Ssam callout_stop(&sc->sc_watchdog); 726178354Ssam sc->sc_tx_timer = 0; 727178354Ssam sc->sc_false_syns = 0; 728178354Ssam 729178354Ssam ifp->if_drv_flags &= ~(IFF_DRV_OACTIVE | IFF_DRV_RUNNING); 730178354Ssam} 731178354Ssam 732178354Ssamvoid 733178354Ssamwi_stop(struct wi_softc *sc, int disable) 734178354Ssam{ 735170530Ssam WI_LOCK(sc); 736178354Ssam wi_stop_locked(sc, disable); 737178354Ssam WI_UNLOCK(sc); 738178354Ssam} 739170530Ssam 740178354Ssamstatic void 741178354Ssamwi_set_channel(struct ieee80211com *ic) 742178354Ssam{ 743178354Ssam struct ifnet *ifp = ic->ic_ifp; 744178354Ssam struct wi_softc *sc = ifp->if_softc; 745160991Ssam 746178354Ssam DPRINTF(("%s: channel %d, %sscanning\n", __func__, 747178354Ssam ieee80211_chan2ieee(ic, ic->ic_curchan), 748178354Ssam ic->ic_flags & IEEE80211_F_SCAN ? "" : "!")); 74946492Swpaul 750178354Ssam WI_LOCK(sc); 751116951Ssam wi_write_val(sc, WI_RID_OWN_CHNL, 752178354Ssam ieee80211_chan2ieee(ic, ic->ic_curchan)); 753178354Ssam WI_UNLOCK(sc); 754178354Ssam} 75575373Salfred 756178354Ssamstatic void 757178354Ssamwi_scan_start(struct ieee80211com *ic) 758178354Ssam{ 759178354Ssam struct ifnet *ifp = ic->ic_ifp; 760178354Ssam struct wi_softc *sc = ifp->if_softc; 761178354Ssam struct ieee80211_scan_state *ss = ic->ic_scan; 76246492Swpaul 763178354Ssam DPRINTF(("%s\n", __func__)); 76446492Swpaul 765178354Ssam WI_LOCK(sc); 766109323Ssam /* 767178354Ssam * Switch device to monitor mode. 768109323Ssam */ 769178354Ssam wi_write_val(sc, WI_RID_PORTTYPE, sc->sc_monitor_port); 770178354Ssam if (sc->sc_firmware_type == WI_INTERSIL) { 771178354Ssam wi_cmd(sc, WI_CMD_DISABLE | WI_PORT0, 0, 0, 0); 772178354Ssam wi_cmd(sc, WI_CMD_ENABLE | WI_PORT0, 0, 0, 0); 77375373Salfred } 774178354Ssam /* force full dwell time to compensate for firmware overhead */ 775178354Ssam ss->ss_mindwell = ss->ss_maxdwell = msecs_to_ticks(400); 776178354Ssam WI_UNLOCK(sc); 77746492Swpaul 778178354Ssam} 77967092Swpaul 780178354Ssamstatic void 781178354Ssamwi_scan_end(struct ieee80211com *ic) 782178354Ssam{ 783178354Ssam struct ifnet *ifp = ic->ic_ifp; 784178354Ssam struct wi_softc *sc = ifp->if_softc; 78546492Swpaul 786178354Ssam DPRINTF(("%s: restore port type %d\n", __func__, sc->sc_porttype)); 787178354Ssam 788178354Ssam WI_LOCK(sc); 789178354Ssam wi_write_val(sc, WI_RID_PORTTYPE, sc->sc_porttype); 790178354Ssam if (sc->sc_firmware_type == WI_INTERSIL) { 791178354Ssam wi_cmd(sc, WI_CMD_DISABLE | WI_PORT0, 0, 0, 0); 792178354Ssam wi_cmd(sc, WI_CMD_ENABLE | WI_PORT0, 0, 0, 0); 79370073Swpaul } 794178354Ssam WI_UNLOCK(sc); 795178354Ssam} 79670073Swpaul 797178354Ssamstatic void 798178354Ssamwi_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m, 799192468Ssam int subtype, int rssi, int nf) 800178354Ssam{ 801178354Ssam struct ieee80211vap *vap = ni->ni_vap; 80246492Swpaul 803178354Ssam switch (subtype) { 804178354Ssam case IEEE80211_FC0_SUBTYPE_AUTH: 805178354Ssam case IEEE80211_FC0_SUBTYPE_ASSOC_RESP: 806178354Ssam case IEEE80211_FC0_SUBTYPE_REASSOC_RESP: 807178354Ssam /* NB: filter frames that trigger state changes */ 808178354Ssam return; 809170530Ssam } 810192468Ssam WI_VAP(vap)->wv_recv_mgmt(ni, m, subtype, rssi, nf); 811178354Ssam} 81246492Swpaul 813178354Ssamstatic int 814178354Ssamwi_newstate_sta(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg) 815178354Ssam{ 816178354Ssam struct ieee80211com *ic = vap->iv_ic; 817178354Ssam struct ifnet *ifp = ic->ic_ifp; 818178354Ssam struct ieee80211_node *bss; 819178354Ssam struct wi_softc *sc = ifp->if_softc; 82094397Simp 821178354Ssam DPRINTF(("%s: %s -> %s\n", __func__, 822178354Ssam ieee80211_state_name[vap->iv_state], 823178354Ssam ieee80211_state_name[nstate])); 824178354Ssam 825178354Ssam if (nstate == IEEE80211_S_AUTH) { 826178354Ssam WI_LOCK(sc); 827178354Ssam wi_setup_locked(sc, WI_PORTTYPE_BSS, 3, vap->iv_myaddr); 828178354Ssam 829178354Ssam if (vap->iv_flags & IEEE80211_F_PMGTON) { 830178354Ssam wi_write_val(sc, WI_RID_MAX_SLEEP, ic->ic_lintval); 831178354Ssam wi_write_val(sc, WI_RID_PM_ENABLED, 1); 832178354Ssam } 833178354Ssam wi_write_val(sc, WI_RID_RTS_THRESH, vap->iv_rtsthreshold); 834178354Ssam if (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR) 835178354Ssam wi_write_val(sc, WI_RID_FRAG_THRESH, 836178354Ssam vap->iv_fragthreshold); 837178354Ssam wi_write_txrate(sc, vap); 838178354Ssam 839178354Ssam bss = vap->iv_bss; 840178354Ssam wi_write_ssid(sc, WI_RID_DESIRED_SSID, bss->ni_essid, bss->ni_esslen); 841178354Ssam wi_write_val(sc, WI_RID_OWN_CHNL, 842178354Ssam ieee80211_chan2ieee(ic, bss->ni_chan)); 843178354Ssam 844178354Ssam /* Configure WEP. */ 845178354Ssam if (ic->ic_cryptocaps & IEEE80211_CRYPTO_WEP) 846178354Ssam wi_write_wep(sc, vap); 847178354Ssam else 848178354Ssam sc->sc_encryption = 0; 849178354Ssam 850178354Ssam if ((sc->sc_flags & WI_FLAGS_HAS_WPASUPPORT) && 851178354Ssam (vap->iv_flags & IEEE80211_F_WPA)) { 852178354Ssam wi_write_val(sc, WI_RID_WPA_HANDLING, 1); 853178354Ssam if (vap->iv_appie_wpa != NULL) 854178354Ssam wi_write_appie(sc, WI_RID_WPA_DATA, 855178354Ssam vap->iv_appie_wpa); 856178354Ssam } 857178354Ssam 858178354Ssam wi_enable(sc); /* enable port */ 859178354Ssam 860178354Ssam /* Lucent firmware does not support the JOIN RID. */ 861178354Ssam if (sc->sc_firmware_type == WI_INTERSIL) { 862178354Ssam struct wi_joinreq join; 863178354Ssam 864178354Ssam memset(&join, 0, sizeof(join)); 865178354Ssam IEEE80211_ADDR_COPY(&join.wi_bssid, bss->ni_bssid); 866116951Ssam join.wi_chan = htole16( 867178354Ssam ieee80211_chan2ieee(ic, bss->ni_chan)); 868109323Ssam wi_write_rid(sc, WI_RID_JOIN_REQ, &join, sizeof(join)); 869178354Ssam } 870178354Ssam WI_UNLOCK(sc); 87175373Salfred 872178354Ssam /* 873178354Ssam * NB: don't go through 802.11 layer, it'll send auth frame; 874178354Ssam * instead we drive the state machine from the link status 875178354Ssam * notification we get on association. 876178354Ssam */ 877178354Ssam vap->iv_state = nstate; 878191746Sthompsa return (0); 87970073Swpaul } 880178354Ssam return WI_VAP(vap)->wv_newstate(vap, nstate, arg); 88146492Swpaul} 88246492Swpaul 883178354Ssamstatic int 884178354Ssamwi_newstate_hostap(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg) 88546492Swpaul{ 886178354Ssam struct ieee80211com *ic = vap->iv_ic; 887178354Ssam struct ifnet *ifp = ic->ic_ifp; 888178354Ssam struct ieee80211_node *bss; 889109323Ssam struct wi_softc *sc = ifp->if_softc; 890178354Ssam int error; 89146492Swpaul 892178354Ssam DPRINTF(("%s: %s -> %s\n", __func__, 893178354Ssam ieee80211_state_name[vap->iv_state], 894178354Ssam ieee80211_state_name[nstate])); 89574998Swpaul 896178354Ssam error = WI_VAP(vap)->wv_newstate(vap, nstate, arg); 897178354Ssam if (error == 0 && nstate == IEEE80211_S_RUN) { 898178354Ssam WI_LOCK(sc); 899178354Ssam wi_setup_locked(sc, WI_PORTTYPE_HOSTAP, 0, vap->iv_myaddr); 900178354Ssam 901178354Ssam bss = vap->iv_bss; 902178354Ssam wi_write_ssid(sc, WI_RID_OWN_SSID, 903178354Ssam bss->ni_essid, bss->ni_esslen); 904178354Ssam wi_write_val(sc, WI_RID_OWN_CHNL, 905178354Ssam ieee80211_chan2ieee(ic, bss->ni_chan)); 906178354Ssam wi_write_val(sc, WI_RID_BASIC_RATE, 0x3); 907178354Ssam wi_write_val(sc, WI_RID_SUPPORT_RATE, 0xf); 908178354Ssam wi_write_txrate(sc, vap); 909178354Ssam 910178354Ssam wi_write_val(sc, WI_RID_OWN_BEACON_INT, bss->ni_intval); 911178354Ssam wi_write_val(sc, WI_RID_DTIM_PERIOD, vap->iv_dtim_period); 912178354Ssam 913178354Ssam wi_write_val(sc, WI_RID_RTS_THRESH, vap->iv_rtsthreshold); 914178354Ssam if (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR) 915178354Ssam wi_write_val(sc, WI_RID_FRAG_THRESH, 916178354Ssam vap->iv_fragthreshold); 917178354Ssam 918178354Ssam if ((sc->sc_flags & WI_FLAGS_HAS_ENHSECURITY) && 919178354Ssam (vap->iv_flags & IEEE80211_F_HIDESSID)) { 920178354Ssam /* 921178354Ssam * bit 0 means hide SSID in beacons, 922178354Ssam * bit 1 means don't respond to bcast probe req 923178354Ssam */ 924178354Ssam wi_write_val(sc, WI_RID_ENH_SECURITY, 0x3); 92570073Swpaul } 92670073Swpaul 927178354Ssam if ((sc->sc_flags & WI_FLAGS_HAS_WPASUPPORT) && 928178354Ssam (vap->iv_flags & IEEE80211_F_WPA) && 929178354Ssam vap->iv_appie_wpa != NULL) 930178354Ssam wi_write_appie(sc, WI_RID_WPA_DATA, vap->iv_appie_wpa); 93146492Swpaul 932178354Ssam wi_write_val(sc, WI_RID_PROMISC, 0); 933178354Ssam 934178354Ssam /* Configure WEP. */ 935178354Ssam if (ic->ic_cryptocaps & IEEE80211_CRYPTO_WEP) 936178354Ssam wi_write_wep(sc, vap); 937178354Ssam else 938178354Ssam sc->sc_encryption = 0; 939178354Ssam 940178354Ssam wi_enable(sc); /* enable port */ 941178354Ssam WI_UNLOCK(sc); 942178354Ssam } 943178354Ssam return error; 94446492Swpaul} 94546492Swpaul 946109323Ssamstatic void 947165087Ssamwi_start_locked(struct ifnet *ifp) 94846492Swpaul{ 949109323Ssam struct wi_softc *sc = ifp->if_softc; 950119150Ssam struct ieee80211_node *ni; 951109323Ssam struct ieee80211_frame *wh; 952109323Ssam struct mbuf *m0; 953178354Ssam struct ieee80211_key *k; 954109323Ssam struct wi_frame frmhdr; 955190579Ssam const struct llc *llc; 956160991Ssam int cur; 95746492Swpaul 958165087Ssam WI_LOCK_ASSERT(sc); 95946492Swpaul 960165087Ssam if (sc->wi_gone) 961109323Ssam return; 96246492Swpaul 963109323Ssam memset(&frmhdr, 0, sizeof(frmhdr)); 964109323Ssam cur = sc->sc_txnext; 965109323Ssam for (;;) { 966178354Ssam IFQ_DRV_DEQUEUE(&ifp->if_snd, m0); 967178354Ssam if (m0 == NULL) 968178354Ssam break; 969178354Ssam if (sc->sc_txd[cur].d_len != 0) { 970178354Ssam IFQ_DRV_PREPEND(&ifp->if_snd, m0); 971178354Ssam ifp->if_drv_flags |= IFF_DRV_OACTIVE; 972178354Ssam break; 973178354Ssam } 974178354Ssam ni = (struct ieee80211_node *) m0->m_pkthdr.rcvif; 97546492Swpaul 976190579Ssam /* reconstruct 802.3 header */ 977178354Ssam wh = mtod(m0, struct ieee80211_frame *); 978190579Ssam switch (wh->i_fc[1]) { 979190579Ssam case IEEE80211_FC1_DIR_TODS: 980190579Ssam IEEE80211_ADDR_COPY(frmhdr.wi_ehdr.ether_shost, 981190579Ssam wh->i_addr2); 982190579Ssam IEEE80211_ADDR_COPY(frmhdr.wi_ehdr.ether_dhost, 983190579Ssam wh->i_addr3); 984190579Ssam break; 985190579Ssam case IEEE80211_FC1_DIR_NODS: 986190579Ssam IEEE80211_ADDR_COPY(frmhdr.wi_ehdr.ether_shost, 987190579Ssam wh->i_addr2); 988190579Ssam IEEE80211_ADDR_COPY(frmhdr.wi_ehdr.ether_dhost, 989190579Ssam wh->i_addr1); 990190579Ssam break; 991190579Ssam case IEEE80211_FC1_DIR_FROMDS: 992190579Ssam IEEE80211_ADDR_COPY(frmhdr.wi_ehdr.ether_shost, 993190579Ssam wh->i_addr3); 994190579Ssam IEEE80211_ADDR_COPY(frmhdr.wi_ehdr.ether_dhost, 995190579Ssam wh->i_addr1); 996190579Ssam break; 997190579Ssam } 998190579Ssam llc = (const struct llc *)( 999190579Ssam mtod(m0, const uint8_t *) + ieee80211_hdrsize(wh)); 1000190579Ssam frmhdr.wi_ehdr.ether_type = llc->llc_snap.ether_type; 1001109323Ssam frmhdr.wi_tx_ctl = htole16(WI_ENC_TX_802_11|WI_TXCNTL_TX_EX); 1002178354Ssam if (wh->i_fc[1] & IEEE80211_FC1_WEP) { 1003178354Ssam k = ieee80211_crypto_encap(ni, m0); 1004138571Ssam if (k == NULL) { 1005170530Ssam ieee80211_free_node(ni); 1006143299Ssam m_freem(m0); 1007109323Ssam continue; 1008109323Ssam } 1009138952Ssam frmhdr.wi_tx_ctl |= htole16(WI_TXCNTL_NOCRYPT); 1010109323Ssam } 1011178354Ssam 1012192468Ssam if (ieee80211_radiotap_active_vap(ni->ni_vap)) { 1013178354Ssam sc->sc_tx_th.wt_rate = ni->ni_txrate; 1014192468Ssam ieee80211_radiotap_tx(ni->ni_vap, m0); 1015109323Ssam } 1016178354Ssam 1017127697Ssam m_copydata(m0, 0, sizeof(struct ieee80211_frame), 1018127697Ssam (caddr_t)&frmhdr.wi_whdr); 1019127697Ssam m_adj(m0, sizeof(struct ieee80211_frame)); 1020127697Ssam frmhdr.wi_dat_len = htole16(m0->m_pkthdr.len); 1021170530Ssam ieee80211_free_node(ni); 1022160991Ssam if (wi_start_tx(ifp, &frmhdr, m0)) 1023109323Ssam continue; 1024178354Ssam 1025160991Ssam sc->sc_txnext = cur = (cur + 1) % sc->sc_ntxbuf; 1026178354Ssam ifp->if_opackets++; 1027160991Ssam } 1028165087Ssam} 1029160991Ssam 1030165087Ssamstatic void 1031165087Ssamwi_start(struct ifnet *ifp) 1032165087Ssam{ 1033165087Ssam struct wi_softc *sc = ifp->if_softc; 1034165087Ssam 1035165087Ssam WI_LOCK(sc); 1036165087Ssam wi_start_locked(ifp); 1037160991Ssam WI_UNLOCK(sc); 1038160991Ssam} 1039160991Ssam 1040160991Ssamstatic int 1041160991Ssamwi_start_tx(struct ifnet *ifp, struct wi_frame *frmhdr, struct mbuf *m0) 1042160991Ssam{ 1043160991Ssam struct wi_softc *sc = ifp->if_softc; 1044160991Ssam int cur = sc->sc_txnext; 1045160991Ssam int fid, off, error; 1046160991Ssam 1047160991Ssam fid = sc->sc_txd[cur].d_fid; 1048160991Ssam off = sizeof(*frmhdr); 1049160991Ssam error = wi_write_bap(sc, fid, 0, frmhdr, sizeof(*frmhdr)) != 0 1050160991Ssam || wi_mwrite_bap(sc, fid, off, m0, m0->m_pkthdr.len) != 0; 1051160991Ssam m_freem(m0); 1052160991Ssam if (error) { 1053160991Ssam ifp->if_oerrors++; 1054160991Ssam return -1; 1055160991Ssam } 1056160991Ssam sc->sc_txd[cur].d_len = off; 1057160991Ssam if (sc->sc_txcur == cur) { 1058160991Ssam if (wi_cmd(sc, WI_CMD_TX | WI_RECLAIM, fid, 0, 0)) { 1059160991Ssam if_printf(ifp, "xmit failed\n"); 1060160991Ssam sc->sc_txd[cur].d_len = 0; 1061160991Ssam return -1; 1062109323Ssam } 1063160991Ssam sc->sc_tx_timer = 5; 1064160991Ssam } 1065160991Ssam return 0; 1066160991Ssam} 1067160991Ssam 1068160991Ssamstatic int 1069160991Ssamwi_raw_xmit(struct ieee80211_node *ni, struct mbuf *m0, 1070160991Ssam const struct ieee80211_bpf_params *params) 1071160991Ssam{ 1072160991Ssam struct ieee80211com *ic = ni->ni_ic; 1073160991Ssam struct ifnet *ifp = ic->ic_ifp; 1074192468Ssam struct ieee80211vap *vap = ni->ni_vap; 1075160991Ssam struct wi_softc *sc = ifp->if_softc; 1076178354Ssam struct ieee80211_key *k; 1077160991Ssam struct ieee80211_frame *wh; 1078160991Ssam struct wi_frame frmhdr; 1079160991Ssam int cur; 1080160991Ssam int rc = 0; 1081160991Ssam 1082160991Ssam WI_LOCK(sc); 1083160991Ssam 1084160991Ssam if (sc->wi_gone) { 1085160991Ssam rc = ENETDOWN; 1086160991Ssam goto out; 1087160991Ssam } 1088160991Ssam memset(&frmhdr, 0, sizeof(frmhdr)); 1089160991Ssam cur = sc->sc_txnext; 1090160991Ssam if (sc->sc_txd[cur].d_len != 0) { 1091160991Ssam ifp->if_drv_flags |= IFF_DRV_OACTIVE; 1092160991Ssam rc = ENOBUFS; 1093160991Ssam goto out; 1094160991Ssam } 1095160991Ssam m0->m_pkthdr.rcvif = NULL; 1096160991Ssam 1097160991Ssam m_copydata(m0, 4, ETHER_ADDR_LEN * 2, 1098160991Ssam (caddr_t)&frmhdr.wi_ehdr); 1099160991Ssam frmhdr.wi_ehdr.ether_type = 0; 1100160991Ssam wh = mtod(m0, struct ieee80211_frame *); 1101160991Ssam 1102160991Ssam frmhdr.wi_tx_ctl = htole16(WI_ENC_TX_802_11|WI_TXCNTL_TX_EX); 1103160991Ssam if (params && (params->ibp_flags & IEEE80211_BPF_NOACK)) 1104160991Ssam frmhdr.wi_tx_ctl |= htole16(WI_TXCNTL_ALTRTRY); 1105160991Ssam if ((wh->i_fc[1] & IEEE80211_FC1_WEP) && 1106178354Ssam (!params || (params && (params->ibp_flags & IEEE80211_BPF_CRYPTO)))) { 1107178354Ssam k = ieee80211_crypto_encap(ni, m0); 1108178354Ssam if (k == NULL) { 1109178354Ssam rc = ENOMEM; 1110178354Ssam goto out; 1111109323Ssam } 1112178354Ssam frmhdr.wi_tx_ctl |= htole16(WI_TXCNTL_NOCRYPT); 111374838Salfred } 1114192468Ssam if (ieee80211_radiotap_active_vap(vap)) { 1115178354Ssam sc->sc_tx_th.wt_rate = ni->ni_txrate; 1116192468Ssam ieee80211_radiotap_tx(vap, m0); 1117160991Ssam } 1118160991Ssam m_copydata(m0, 0, sizeof(struct ieee80211_frame), 1119160991Ssam (caddr_t)&frmhdr.wi_whdr); 1120160991Ssam m_adj(m0, sizeof(struct ieee80211_frame)); 1121160991Ssam frmhdr.wi_dat_len = htole16(m0->m_pkthdr.len); 1122168860Ssephe if (wi_start_tx(ifp, &frmhdr, m0) < 0) { 1123168860Ssephe m0 = NULL; 1124168860Ssephe rc = EIO; 1125160991Ssam goto out; 1126168860Ssephe } 1127168860Ssephe m0 = NULL; 112846492Swpaul 1129160991Ssam sc->sc_txnext = cur = (cur + 1) % sc->sc_ntxbuf; 1130160991Ssamout: 1131109323Ssam WI_UNLOCK(sc); 1132160991Ssam 1133168860Ssephe if (m0 != NULL) 1134168860Ssephe m_freem(m0); 1135168860Ssephe ieee80211_free_node(ni); 1136160991Ssam return rc; 113746492Swpaul} 113846492Swpaul 113988546Salfredstatic int 1140178354Ssamwi_reset(struct wi_softc *sc) 114146492Swpaul{ 1142114124Simp#define WI_INIT_TRIES 3 1143178354Ssam int i, error = 0; 1144112362Simp 1145178354Ssam for (i = 0; i < WI_INIT_TRIES; i++) { 1146178354Ssam error = wi_cmd(sc, WI_CMD_INI, 0, 0, 0); 1147178354Ssam if (error == 0) 114846492Swpaul break; 1149109323Ssam DELAY(WI_DELAY * 1000); 115046492Swpaul } 1151114124Simp sc->sc_reset = 1; 1152178354Ssam if (i == WI_INIT_TRIES) { 1153178354Ssam if_printf(sc->sc_ifp, "reset failed\n"); 1154178354Ssam return error; 115575373Salfred } 115646492Swpaul 1157109323Ssam CSR_WRITE_2(sc, WI_INT_EN, 0); 1158114124Simp CSR_WRITE_2(sc, WI_EVENT_ACK, 0xFFFF); 115946492Swpaul 1160109323Ssam /* Calibrate timer. */ 1161114124Simp wi_write_val(sc, WI_RID_TICK_TIME, 8); 1162114124Simp 1163178354Ssam return 0; 1164109323Ssam#undef WI_INIT_TRIES 116546492Swpaul} 116646492Swpaul 116788546Salfredstatic void 1168165089Ssamwi_watchdog(void *arg) 116946492Swpaul{ 1170165089Ssam struct wi_softc *sc = arg; 1171165089Ssam struct ifnet *ifp = sc->sc_ifp; 117246492Swpaul 1173178354Ssam WI_LOCK_ASSERT(sc); 1174178354Ssam 1175109323Ssam if (!sc->sc_enabled) 1176109323Ssam return; 117746492Swpaul 1178178354Ssam if (sc->sc_tx_timer && --sc->sc_tx_timer == 0) { 1179178354Ssam if_printf(ifp, "device timeout\n"); 1180178354Ssam ifp->if_oerrors++; 1181178354Ssam wi_init_locked(ifp->if_softc); 1182178354Ssam return; 118346492Swpaul } 1184165089Ssam callout_reset(&sc->sc_watchdog, hz, wi_watchdog, sc); 118546492Swpaul} 118646492Swpaul 118788546Salfredstatic int 1188109323Ssamwi_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 118946492Swpaul{ 1190109323Ssam struct wi_softc *sc = ifp->if_softc; 1191178354Ssam struct ieee80211com *ic = ifp->if_l2com; 1192178354Ssam struct ifreq *ifr = (struct ifreq *) data; 1193178354Ssam int error = 0, startall = 0; 119446492Swpaul 1195109323Ssam switch (cmd) { 119646492Swpaul case SIOCSIFFLAGS: 1197178704Sthompsa WI_LOCK(sc); 1198100876Simp /* 1199101139Simp * Can't do promisc and hostap at the same time. If all that's 1200101139Simp * changing is the promisc flag, try to short-circuit a call to 1201101139Simp * wi_init() by just setting PROMISC in the hardware. 1202100876Simp */ 120346492Swpaul if (ifp->if_flags & IFF_UP) { 1204109323Ssam if (ic->ic_opmode != IEEE80211_M_HOSTAP && 1205148887Srwatson ifp->if_drv_flags & IFF_DRV_RUNNING) { 1206178354Ssam if ((ifp->if_flags ^ sc->sc_if_flags) & IFF_PROMISC) { 1207178354Ssam wi_write_val(sc, WI_RID_PROMISC, 1208178354Ssam (ifp->if_flags & IFF_PROMISC) != 0); 1209101139Simp } else { 1210178354Ssam wi_init_locked(sc); 1211178354Ssam startall = 1; 1212101139Simp } 1213100876Simp } else { 1214178354Ssam wi_init_locked(sc); 1215178354Ssam startall = 1; 1216100876Simp } 121746492Swpaul } else { 1218178354Ssam if (ifp->if_drv_flags & IFF_DRV_RUNNING) 1219178354Ssam wi_stop_locked(sc, 1); 1220123098Simp sc->wi_gone = 0; 122146492Swpaul } 1222109323Ssam sc->sc_if_flags = ifp->if_flags; 1223178704Sthompsa WI_UNLOCK(sc); 1224178704Sthompsa if (startall) 1225178704Sthompsa ieee80211_start_all(ic); 122646492Swpaul break; 1227178354Ssam case SIOCGIFMEDIA: 1228178354Ssam error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd); 122946492Swpaul break; 1230178704Sthompsa case SIOCGIFADDR: 1231178354Ssam error = ether_ioctl(ifp, cmd, data); 1232170530Ssam break; 1233178704Sthompsa default: 1234178704Sthompsa error = EINVAL; 1235178704Sthompsa break; 1236170530Ssam } 1237109323Ssam return error; 1238109323Ssam} 123946492Swpaul 1240109323Ssamstatic void 1241109323Ssamwi_media_status(struct ifnet *ifp, struct ifmediareq *imr) 1242109323Ssam{ 1243178354Ssam struct ieee80211vap *vap = ifp->if_softc; 1244178354Ssam struct ieee80211com *ic = vap->iv_ic; 1245178354Ssam struct wi_softc *sc = ic->ic_ifp->if_softc; 1246109323Ssam u_int16_t val; 1247109323Ssam int rate, len; 124846492Swpaul 1249109323Ssam len = sizeof(val); 1250178354Ssam if (sc->sc_enabled && 1251178354Ssam wi_read_rid(sc, WI_RID_CUR_TX_RATE, &val, &len) == 0 && 1252138571Ssam len == sizeof(val)) { 1253109323Ssam /* convert to 802.11 rate */ 1254138571Ssam val = le16toh(val); 1255109323Ssam rate = val * 2; 1256109323Ssam if (sc->sc_firmware_type == WI_LUCENT) { 1257138571Ssam if (rate == 10) 1258109323Ssam rate = 11; /* 5.5Mbps */ 1259109323Ssam } else { 1260109323Ssam if (rate == 4*2) 1261109323Ssam rate = 11; /* 5.5Mbps */ 1262109323Ssam else if (rate == 8*2) 1263109323Ssam rate = 22; /* 11Mbps */ 1264109323Ssam } 1265178354Ssam vap->iv_bss->ni_txrate = rate; 1266109323Ssam } 1267178354Ssam ieee80211_media_status(ifp, imr); 1268109323Ssam} 126946492Swpaul 1270109323Ssamstatic void 1271109323Ssamwi_sync_bssid(struct wi_softc *sc, u_int8_t new_bssid[IEEE80211_ADDR_LEN]) 1272109323Ssam{ 1273147256Sbrooks struct ifnet *ifp = sc->sc_ifp; 1274178354Ssam struct ieee80211com *ic = ifp->if_l2com; 1275178354Ssam struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps); 1276178354Ssam struct ieee80211_node *ni = vap->iv_bss; 127798440Simp 1278109323Ssam if (IEEE80211_ADDR_EQ(new_bssid, ni->ni_bssid)) 1279109323Ssam return; 128046492Swpaul 1281109323Ssam DPRINTF(("wi_sync_bssid: bssid %s -> ", ether_sprintf(ni->ni_bssid))); 1282109323Ssam DPRINTF(("%s ?\n", ether_sprintf(new_bssid))); 128346492Swpaul 1284109323Ssam /* In promiscuous mode, the BSSID field is not a reliable 1285109323Ssam * indicator of the firmware's BSSID. Damp spurious 1286109323Ssam * change-of-BSSID indications. 1287109323Ssam */ 1288109323Ssam if ((ifp->if_flags & IFF_PROMISC) != 0 && 1289138571Ssam !ppsratecheck(&sc->sc_last_syn, &sc->sc_false_syns, 1290138571Ssam WI_MAX_FALSE_SYNS)) 1291109323Ssam return; 129246492Swpaul 1293139542Ssam sc->sc_false_syns = MAX(0, sc->sc_false_syns - 1); 1294170530Ssam#if 0 1295139542Ssam /* 1296139542Ssam * XXX hack; we should create a new node with the new bssid 1297139542Ssam * and replace the existing ic_bss with it but since we don't 1298139542Ssam * process management frames to collect state we cheat by 1299139542Ssam * reusing the existing node as we know wi_newstate will be 1300139542Ssam * called and it will overwrite the node state. 1301139542Ssam */ 1302139542Ssam ieee80211_sta_join(ic, ieee80211_ref_node(ni)); 1303170530Ssam#endif 1304109323Ssam} 130546492Swpaul 1306178354Ssamstatic __noinline void 1307109323Ssamwi_rx_intr(struct wi_softc *sc) 1308109323Ssam{ 1309147256Sbrooks struct ifnet *ifp = sc->sc_ifp; 1310178354Ssam struct ieee80211com *ic = ifp->if_l2com; 1311109323Ssam struct wi_frame frmhdr; 1312109323Ssam struct mbuf *m; 1313109323Ssam struct ieee80211_frame *wh; 1314119150Ssam struct ieee80211_node *ni; 1315192468Ssam int fid, len, off; 1316109323Ssam u_int8_t dir; 1317109323Ssam u_int16_t status; 1318192468Ssam int8_t rssi, nf; 131946611Swpaul 1320109323Ssam fid = CSR_READ_2(sc, WI_RX_FID); 132146611Swpaul 1322109323Ssam /* First read in the frame header */ 1323109323Ssam if (wi_read_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr))) { 1324109323Ssam CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX); 1325109323Ssam ifp->if_ierrors++; 1326109323Ssam DPRINTF(("wi_rx_intr: read fid %x failed\n", fid)); 1327109323Ssam return; 1328109323Ssam } 132991695Simp 1330109323Ssam /* 1331109323Ssam * Drop undecryptable or packets with receive errors here 1332109323Ssam */ 1333109323Ssam status = le16toh(frmhdr.wi_status); 1334109323Ssam if (status & WI_STAT_ERRSTAT) { 1335109323Ssam CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX); 1336109323Ssam ifp->if_ierrors++; 1337109323Ssam DPRINTF(("wi_rx_intr: fid %x error status %x\n", fid, status)); 1338109323Ssam return; 1339109323Ssam } 134046492Swpaul 1341109323Ssam len = le16toh(frmhdr.wi_dat_len); 1342109323Ssam off = ALIGN(sizeof(struct ieee80211_frame)); 134346563Swpaul 1344117855Ssam /* 1345117855Ssam * Sometimes the PRISM2.x returns bogusly large frames. Except 1346117855Ssam * in monitor mode, just throw them away. 1347117855Ssam */ 1348117855Ssam if (off + len > MCLBYTES) { 1349117855Ssam if (ic->ic_opmode != IEEE80211_M_MONITOR) { 1350117855Ssam CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX); 1351117855Ssam ifp->if_ierrors++; 1352117855Ssam DPRINTF(("wi_rx_intr: oversized packet\n")); 1353117855Ssam return; 1354117855Ssam } else 1355117855Ssam len = 0; 1356117855Ssam } 1357117855Ssam 1358178354Ssam if (off + len > MHLEN) 1359178354Ssam m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR); 1360178354Ssam else 1361178354Ssam m = m_gethdr(M_DONTWAIT, MT_DATA); 1362109323Ssam if (m == NULL) { 1363109323Ssam CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX); 1364109323Ssam ifp->if_ierrors++; 1365109323Ssam DPRINTF(("wi_rx_intr: MGET failed\n")); 1366109323Ssam return; 1367109323Ssam } 1368109323Ssam m->m_data += off - sizeof(struct ieee80211_frame); 1369109323Ssam memcpy(m->m_data, &frmhdr.wi_whdr, sizeof(struct ieee80211_frame)); 1370109323Ssam wi_read_bap(sc, fid, sizeof(frmhdr), 1371109323Ssam m->m_data + sizeof(struct ieee80211_frame), len); 1372109323Ssam m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame) + len; 1373109323Ssam m->m_pkthdr.rcvif = ifp; 137494405Simp 1375109323Ssam CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX); 137646492Swpaul 1377192468Ssam rssi = frmhdr.wi_rx_signal; 1378192468Ssam nf = frmhdr.wi_rx_silence; 1379192468Ssam if (ieee80211_radiotap_active(ic)) { 1380192468Ssam struct wi_rx_radiotap_header *tap = &sc->sc_rx_th; 1381192468Ssam uint32_t rstamp; 1382192468Ssam 1383192468Ssam rstamp = (le16toh(frmhdr.wi_rx_tstamp0) << 16) | 1384192468Ssam le16toh(frmhdr.wi_rx_tstamp1); 1385192468Ssam tap->wr_tsf = htole64(rstamp); 1386123927Ssam /* XXX replace divide by table */ 1387192468Ssam tap->wr_rate = frmhdr.wi_rx_rate / 5; 1388192468Ssam tap->wr_flags = 0; 1389123927Ssam if (frmhdr.wi_status & WI_STAT_PCF) 1390192468Ssam tap->wr_flags |= IEEE80211_RADIOTAP_F_CFP; 1391178354Ssam if (m->m_flags & M_WEP) 1392192468Ssam tap->wr_flags |= IEEE80211_RADIOTAP_F_WEP; 1393192468Ssam tap->wr_antsignal = rssi; 1394192468Ssam tap->wr_antnoise = nf; 139556965Swpaul } 139656965Swpaul 1397109323Ssam /* synchronize driver's BSSID with firmware's BSSID */ 1398178354Ssam wh = mtod(m, struct ieee80211_frame *); 1399109323Ssam dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK; 1400109323Ssam if (ic->ic_opmode == IEEE80211_M_IBSS && dir == IEEE80211_FC1_DIR_NODS) 1401109323Ssam wi_sync_bssid(sc, wh->i_addr3); 140246492Swpaul 1403165088Ssam WI_UNLOCK(sc); 1404165088Ssam 1405178354Ssam ni = ieee80211_find_rxnode(ic, mtod(m, struct ieee80211_frame_min *)); 1406178354Ssam if (ni != NULL) { 1407192468Ssam (void) ieee80211_input(ni, m, rssi, nf); 1408178354Ssam ieee80211_free_node(ni); 1409178354Ssam } else 1410192468Ssam (void) ieee80211_input_all(ic, m, rssi, nf); 1411178354Ssam 1412165088Ssam WI_LOCK(sc); 1413109323Ssam} 141446492Swpaul 1415178354Ssamstatic __noinline void 1416109323Ssamwi_tx_ex_intr(struct wi_softc *sc) 1417109323Ssam{ 1418147256Sbrooks struct ifnet *ifp = sc->sc_ifp; 1419109323Ssam struct wi_frame frmhdr; 1420109323Ssam int fid; 142146492Swpaul 1422109323Ssam fid = CSR_READ_2(sc, WI_TX_CMP_FID); 1423109323Ssam /* Read in the frame header */ 1424109323Ssam if (wi_read_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr)) == 0) { 1425109323Ssam u_int16_t status = le16toh(frmhdr.wi_status); 1426109323Ssam /* 1427109323Ssam * Spontaneous station disconnects appear as xmit 1428109323Ssam * errors. Don't announce them and/or count them 1429109323Ssam * as an output error. 1430109323Ssam */ 1431109323Ssam if ((status & WI_TXSTAT_DISCONNECT) == 0) { 1432109323Ssam if (ppsratecheck(&lasttxerror, &curtxeps, wi_txerate)) { 1433109323Ssam if_printf(ifp, "tx failed"); 1434109323Ssam if (status & WI_TXSTAT_RET_ERR) 1435109323Ssam printf(", retry limit exceeded"); 1436109323Ssam if (status & WI_TXSTAT_AGED_ERR) 1437109323Ssam printf(", max transmit lifetime exceeded"); 1438109323Ssam if (status & WI_TXSTAT_DISCONNECT) 1439109323Ssam printf(", port disconnected"); 1440109323Ssam if (status & WI_TXSTAT_FORM_ERR) 1441109323Ssam printf(", invalid format (data len %u src %6D)", 1442109323Ssam le16toh(frmhdr.wi_dat_len), 1443109323Ssam frmhdr.wi_ehdr.ether_shost, ":"); 1444109323Ssam if (status & ~0xf) 1445109323Ssam printf(", status=0x%x", status); 1446109323Ssam printf("\n"); 1447109323Ssam } 1448109323Ssam ifp->if_oerrors++; 1449109323Ssam } else { 1450109323Ssam DPRINTF(("port disconnected\n")); 1451109323Ssam ifp->if_collisions++; /* XXX */ 1452109323Ssam } 1453109323Ssam } else 1454109323Ssam DPRINTF(("wi_tx_ex_intr: read fid %x failed\n", fid)); 1455109323Ssam CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_TX_EXC); 1456109323Ssam} 145746492Swpaul 1458178354Ssamstatic __noinline void 1459109323Ssamwi_tx_intr(struct wi_softc *sc) 1460109323Ssam{ 1461147256Sbrooks struct ifnet *ifp = sc->sc_ifp; 1462109323Ssam int fid, cur; 146394405Simp 1464123098Simp if (sc->wi_gone) 1465123098Simp return; 1466123098Simp 1467109323Ssam fid = CSR_READ_2(sc, WI_ALLOC_FID); 1468109323Ssam CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_ALLOC); 146946492Swpaul 1470109323Ssam cur = sc->sc_txcur; 1471109323Ssam if (sc->sc_txd[cur].d_fid != fid) { 1472109323Ssam if_printf(ifp, "bad alloc %x != %x, cur %d nxt %d\n", 1473109323Ssam fid, sc->sc_txd[cur].d_fid, cur, sc->sc_txnext); 1474109323Ssam return; 1475109323Ssam } 1476109323Ssam sc->sc_tx_timer = 0; 1477109323Ssam sc->sc_txd[cur].d_len = 0; 1478112363Simp sc->sc_txcur = cur = (cur + 1) % sc->sc_ntxbuf; 1479109323Ssam if (sc->sc_txd[cur].d_len == 0) 1480148887Srwatson ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 1481109323Ssam else { 1482109323Ssam if (wi_cmd(sc, WI_CMD_TX | WI_RECLAIM, sc->sc_txd[cur].d_fid, 1483109323Ssam 0, 0)) { 1484109323Ssam if_printf(ifp, "xmit failed\n"); 1485109323Ssam sc->sc_txd[cur].d_len = 0; 1486109323Ssam } else { 1487109323Ssam sc->sc_tx_timer = 5; 1488109323Ssam } 1489109323Ssam } 149046492Swpaul} 149146492Swpaul 1492178354Ssamstatic __noinline void 1493109323Ssamwi_info_intr(struct wi_softc *sc) 149494405Simp{ 1495147256Sbrooks struct ifnet *ifp = sc->sc_ifp; 1496178354Ssam struct ieee80211com *ic = ifp->if_l2com; 1497178354Ssam struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps); 1498109323Ssam int i, fid, len, off; 1499109323Ssam u_int16_t ltbuf[2]; 1500109323Ssam u_int16_t stat; 1501109323Ssam u_int32_t *ptr; 150294405Simp 1503109323Ssam fid = CSR_READ_2(sc, WI_INFO_FID); 1504109323Ssam wi_read_bap(sc, fid, 0, ltbuf, sizeof(ltbuf)); 150594405Simp 1506109323Ssam switch (le16toh(ltbuf[1])) { 1507109323Ssam case WI_INFO_LINK_STAT: 1508109323Ssam wi_read_bap(sc, fid, sizeof(ltbuf), &stat, sizeof(stat)); 1509109323Ssam DPRINTF(("wi_info_intr: LINK_STAT 0x%x\n", le16toh(stat))); 1510109323Ssam switch (le16toh(stat)) { 1511109323Ssam case WI_INFO_LINK_STAT_CONNECTED: 1512178354Ssam if (vap->iv_state == IEEE80211_S_RUN && 1513178354Ssam vap->iv_opmode != IEEE80211_M_IBSS) 1514109323Ssam break; 1515178354Ssam /* fall thru... */ 1516109323Ssam case WI_INFO_LINK_STAT_AP_CHG: 1517191746Sthompsa IEEE80211_LOCK(ic); 1518191746Sthompsa vap->iv_bss->ni_associd = 1 | 0xc000; /* NB: anything will do */ 1519191746Sthompsa ieee80211_new_state(vap, IEEE80211_S_RUN, 0); 1520191746Sthompsa IEEE80211_UNLOCK(ic); 1521109323Ssam break; 1522109323Ssam case WI_INFO_LINK_STAT_AP_INR: 1523109323Ssam break; 1524178354Ssam case WI_INFO_LINK_STAT_DISCONNECTED: 1525178354Ssam /* we dropped off the net; e.g. due to deauth/disassoc */ 1526191746Sthompsa IEEE80211_LOCK(ic); 1527191746Sthompsa vap->iv_bss->ni_associd = 0; 1528191746Sthompsa vap->iv_stats.is_rx_deauth++; 1529191746Sthompsa ieee80211_new_state(vap, IEEE80211_S_SCAN, 0); 1530191746Sthompsa IEEE80211_UNLOCK(ic); 1531178354Ssam break; 1532109323Ssam case WI_INFO_LINK_STAT_AP_OOR: 1533178354Ssam /* XXX does this need to be per-vap? */ 1534191746Sthompsa ieee80211_beacon_miss(ic); 1535109323Ssam break; 1536109323Ssam case WI_INFO_LINK_STAT_ASSOC_FAILED: 1537178354Ssam if (vap->iv_opmode == IEEE80211_M_STA) 1538191746Sthompsa ieee80211_new_state(vap, IEEE80211_S_SCAN, 1539191746Sthompsa IEEE80211_SCAN_FAIL_TIMEOUT); 1540109323Ssam break; 1541109323Ssam } 1542109323Ssam break; 1543109323Ssam case WI_INFO_COUNTERS: 1544109323Ssam /* some card versions have a larger stats structure */ 1545109323Ssam len = min(le16toh(ltbuf[0]) - 1, sizeof(sc->sc_stats) / 4); 1546109323Ssam ptr = (u_int32_t *)&sc->sc_stats; 1547109323Ssam off = sizeof(ltbuf); 1548109323Ssam for (i = 0; i < len; i++, off += 2, ptr++) { 1549109323Ssam wi_read_bap(sc, fid, off, &stat, sizeof(stat)); 1550109323Ssam#ifdef WI_HERMES_STATS_WAR 1551109323Ssam if (stat & 0xf000) 1552109323Ssam stat = ~stat; 1553109323Ssam#endif 1554109323Ssam *ptr += stat; 1555109323Ssam } 1556109323Ssam ifp->if_collisions = sc->sc_stats.wi_tx_single_retries + 1557109323Ssam sc->sc_stats.wi_tx_multi_retries + 1558109323Ssam sc->sc_stats.wi_tx_retry_limit; 1559109323Ssam break; 1560109323Ssam default: 1561109323Ssam DPRINTF(("wi_info_intr: got fid %x type %x len %d\n", fid, 1562109323Ssam le16toh(ltbuf[1]), le16toh(ltbuf[0]))); 1563109323Ssam break; 156494405Simp } 1565109323Ssam CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_INFO); 1566109323Ssam} 156794405Simp 1568109323Ssamstatic int 1569109323Ssamwi_write_multi(struct wi_softc *sc) 1570109323Ssam{ 1571147256Sbrooks struct ifnet *ifp = sc->sc_ifp; 1572109323Ssam int n; 1573109323Ssam struct ifmultiaddr *ifma; 1574109323Ssam struct wi_mcast mlist; 157594472Simp 1576109323Ssam if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) { 1577109323Ssamallmulti: 1578109323Ssam memset(&mlist, 0, sizeof(mlist)); 1579109323Ssam return wi_write_rid(sc, WI_RID_MCAST_LIST, &mlist, 1580109323Ssam sizeof(mlist)); 158194405Simp } 158294405Simp 1583109323Ssam n = 0; 1584148654Srwatson IF_ADDR_LOCK(ifp); 1585109323Ssam TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 1586109323Ssam if (ifma->ifma_addr->sa_family != AF_LINK) 1587109323Ssam continue; 1588109323Ssam if (n >= 16) 1589109323Ssam goto allmulti; 1590109323Ssam IEEE80211_ADDR_COPY(&mlist.wi_mcast[n], 1591109323Ssam (LLADDR((struct sockaddr_dl *)ifma->ifma_addr))); 1592109323Ssam n++; 159394405Simp } 1594148654Srwatson IF_ADDR_UNLOCK(ifp); 1595109323Ssam return wi_write_rid(sc, WI_RID_MCAST_LIST, &mlist, 1596109323Ssam IEEE80211_ADDR_LEN * n); 159794405Simp} 159894405Simp 159994405Simpstatic void 1600178354Ssamwi_update_mcast(struct ifnet *ifp) 1601178354Ssam{ 1602178354Ssam wi_write_multi(ifp->if_softc); 1603178354Ssam} 1604178354Ssam 1605178354Ssamstatic void 1606192468Ssamwi_update_promisc(struct ifnet *ifp) 1607192468Ssam{ 1608192468Ssam struct wi_softc *sc = ifp->if_softc; 1609192468Ssam struct ieee80211com *ic = ifp->if_l2com; 1610192468Ssam 1611192468Ssam WI_LOCK(sc); 1612192468Ssam /* XXX handle WEP special case handling? */ 1613192468Ssam wi_write_val(sc, WI_RID_PROMISC, 1614192468Ssam (ic->ic_opmode == IEEE80211_M_MONITOR || 1615192468Ssam (ifp->if_flags & IFF_PROMISC))); 1616192468Ssam WI_UNLOCK(sc); 1617192468Ssam} 1618192468Ssam 1619192468Ssamstatic void 1620109323Ssamwi_read_nicid(struct wi_softc *sc) 162146492Swpaul{ 1622109323Ssam struct wi_card_ident *id; 1623109323Ssam char *p; 1624109323Ssam int len; 1625109323Ssam u_int16_t ver[4]; 162646492Swpaul 1627109323Ssam /* getting chip identity */ 1628109323Ssam memset(ver, 0, sizeof(ver)); 1629109323Ssam len = sizeof(ver); 1630109323Ssam wi_read_rid(sc, WI_RID_CARD_ID, ver, &len); 163146492Swpaul 1632109323Ssam sc->sc_firmware_type = WI_NOTYPE; 1633180919Simp sc->sc_nic_id = le16toh(ver[0]); 1634109323Ssam for (id = wi_card_ident; id->card_name != NULL; id++) { 1635180919Simp if (sc->sc_nic_id == id->card_id) { 1636180919Simp sc->sc_nic_name = id->card_name; 1637109323Ssam sc->sc_firmware_type = id->firm_type; 1638109323Ssam break; 1639109323Ssam } 164067092Swpaul } 1641109323Ssam if (sc->sc_firmware_type == WI_NOTYPE) { 1642180919Simp if (sc->sc_nic_id & 0x8000) { 1643109323Ssam sc->sc_firmware_type = WI_INTERSIL; 1644180919Simp sc->sc_nic_name = "Unknown Prism chip"; 1645109323Ssam } else { 1646109323Ssam sc->sc_firmware_type = WI_LUCENT; 1647180919Simp sc->sc_nic_name = "Unknown Lucent chip"; 1648109323Ssam } 164967092Swpaul } 1650181210Simp if (bootverbose) 1651181210Simp device_printf(sc->sc_dev, "using %s\n", sc->sc_nic_name); 165246492Swpaul 1653109323Ssam /* get primary firmware version (Only Prism chips) */ 1654109323Ssam if (sc->sc_firmware_type != WI_LUCENT) { 1655109323Ssam memset(ver, 0, sizeof(ver)); 1656109323Ssam len = sizeof(ver); 1657109323Ssam wi_read_rid(sc, WI_RID_PRI_IDENTITY, ver, &len); 1658109323Ssam sc->sc_pri_firmware_ver = le16toh(ver[2]) * 10000 + 1659109323Ssam le16toh(ver[3]) * 100 + le16toh(ver[1]); 166067092Swpaul } 166146492Swpaul 1662109323Ssam /* get station firmware version */ 1663109323Ssam memset(ver, 0, sizeof(ver)); 1664109323Ssam len = sizeof(ver); 1665109323Ssam wi_read_rid(sc, WI_RID_STA_IDENTITY, ver, &len); 1666109323Ssam sc->sc_sta_firmware_ver = le16toh(ver[2]) * 10000 + 1667109323Ssam le16toh(ver[3]) * 100 + le16toh(ver[1]); 1668109323Ssam if (sc->sc_firmware_type == WI_INTERSIL && 1669109323Ssam (sc->sc_sta_firmware_ver == 10102 || 1670109323Ssam sc->sc_sta_firmware_ver == 20102)) { 1671109323Ssam char ident[12]; 1672109323Ssam memset(ident, 0, sizeof(ident)); 1673109323Ssam len = sizeof(ident); 1674109323Ssam /* value should be the format like "V2.00-11" */ 1675109323Ssam if (wi_read_rid(sc, WI_RID_SYMBOL_IDENTITY, ident, &len) == 0 && 1676109323Ssam *(p = (char *)ident) >= 'A' && 1677109323Ssam p[2] == '.' && p[5] == '-' && p[8] == '\0') { 1678109323Ssam sc->sc_firmware_type = WI_SYMBOL; 1679109323Ssam sc->sc_sta_firmware_ver = (p[1] - '0') * 10000 + 1680109323Ssam (p[3] - '0') * 1000 + (p[4] - '0') * 100 + 1681109323Ssam (p[6] - '0') * 10 + (p[7] - '0'); 168294405Simp } 168394405Simp } 1684180919Simp if (bootverbose) { 1685180919Simp device_printf(sc->sc_dev, "%s Firmware: ", 1686180919Simp wi_firmware_names[sc->sc_firmware_type]); 1687180919Simp if (sc->sc_firmware_type != WI_LUCENT) /* XXX */ 1688180919Simp printf("Primary (%u.%u.%u), ", 1689180919Simp sc->sc_pri_firmware_ver / 10000, 1690180919Simp (sc->sc_pri_firmware_ver % 10000) / 100, 1691180919Simp sc->sc_pri_firmware_ver % 100); 1692180919Simp printf("Station (%u.%u.%u)\n", 1693180919Simp sc->sc_sta_firmware_ver / 10000, 1694180919Simp (sc->sc_sta_firmware_ver % 10000) / 100, 1695180919Simp sc->sc_sta_firmware_ver % 100); 1696180919Simp } 1697109323Ssam} 169846492Swpaul 1699109323Ssamstatic int 1700109323Ssamwi_write_ssid(struct wi_softc *sc, int rid, u_int8_t *buf, int buflen) 1701109323Ssam{ 1702109323Ssam struct wi_ssid ssid; 170346492Swpaul 1704109323Ssam if (buflen > IEEE80211_NWID_LEN) 1705109323Ssam return ENOBUFS; 1706109323Ssam memset(&ssid, 0, sizeof(ssid)); 1707109323Ssam ssid.wi_len = htole16(buflen); 1708109323Ssam memcpy(ssid.wi_ssid, buf, buflen); 1709109323Ssam return wi_write_rid(sc, rid, &ssid, sizeof(ssid)); 1710109323Ssam} 171146492Swpaul 1712109323Ssamstatic int 1713178354Ssamwi_write_txrate(struct wi_softc *sc, struct ieee80211vap *vap) 1714109323Ssam{ 1715178354Ssam static const uint16_t lucent_rates[12] = { 1716178354Ssam [ 0] = 3, /* auto */ 1717178354Ssam [ 1] = 1, /* 1Mb/s */ 1718178354Ssam [ 2] = 2, /* 2Mb/s */ 1719178354Ssam [ 5] = 4, /* 5.5Mb/s */ 1720178354Ssam [11] = 5 /* 11Mb/s */ 1721178354Ssam }; 1722178354Ssam static const uint16_t intersil_rates[12] = { 1723178354Ssam [ 0] = 0xf, /* auto */ 1724178354Ssam [ 1] = 0, /* 1Mb/s */ 1725178354Ssam [ 2] = 1, /* 2Mb/s */ 1726178354Ssam [ 5] = 2, /* 5.5Mb/s */ 1727178354Ssam [11] = 3, /* 11Mb/s */ 1728178354Ssam }; 1729178354Ssam const uint16_t *rates = sc->sc_firmware_type == WI_LUCENT ? 1730178354Ssam lucent_rates : intersil_rates; 1731178354Ssam struct ieee80211com *ic = vap->iv_ic; 1732178354Ssam const struct ieee80211_txparam *tp; 173346492Swpaul 1734178354Ssam tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_bsschan)]; 1735178354Ssam return wi_write_val(sc, WI_RID_TX_RATE, 1736178354Ssam (tp->ucastrate == IEEE80211_FIXED_RATE_NONE ? 1737178354Ssam rates[0] : rates[tp->ucastrate / 2])); 173846492Swpaul} 173946492Swpaul 1740109323Ssamstatic int 1741178354Ssamwi_write_wep(struct wi_softc *sc, struct ieee80211vap *vap) 174246492Swpaul{ 1743109323Ssam int error = 0; 1744109323Ssam int i, keylen; 1745109323Ssam u_int16_t val; 1746109323Ssam struct wi_key wkey[IEEE80211_WEP_NKID]; 174746492Swpaul 1748109323Ssam switch (sc->sc_firmware_type) { 1749109323Ssam case WI_LUCENT: 1750178354Ssam val = (vap->iv_flags & IEEE80211_F_PRIVACY) ? 1 : 0; 1751109323Ssam error = wi_write_val(sc, WI_RID_ENCRYPTION, val); 1752109323Ssam if (error) 1753109323Ssam break; 1754178354Ssam if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) 1755138988Smdodd break; 1756178354Ssam error = wi_write_val(sc, WI_RID_TX_CRYPT_KEY, vap->iv_def_txkey); 1757138988Smdodd if (error) 1758138988Smdodd break; 1759138988Smdodd memset(wkey, 0, sizeof(wkey)); 1760138988Smdodd for (i = 0; i < IEEE80211_WEP_NKID; i++) { 1761178354Ssam keylen = vap->iv_nw_keys[i].wk_keylen; 1762138988Smdodd wkey[i].wi_keylen = htole16(keylen); 1763178354Ssam memcpy(wkey[i].wi_keydat, vap->iv_nw_keys[i].wk_key, 1764138988Smdodd keylen); 1765109323Ssam } 1766138988Smdodd error = wi_write_rid(sc, WI_RID_DEFLT_CRYPT_KEYS, 1767138988Smdodd wkey, sizeof(wkey)); 1768150798Savatar sc->sc_encryption = 0; 1769109323Ssam break; 1770109323Ssam 1771109323Ssam case WI_INTERSIL: 1772178354Ssam val = HOST_ENCRYPT | HOST_DECRYPT; 1773178354Ssam if (vap->iv_flags & IEEE80211_F_PRIVACY) { 1774109323Ssam /* 1775109323Ssam * ONLY HWB3163 EVAL-CARD Firmware version 1776109323Ssam * less than 0.8 variant2 1777109323Ssam * 1778109323Ssam * If promiscuous mode disable, Prism2 chip 1779109323Ssam * does not work with WEP . 1780109323Ssam * It is under investigation for details. 1781109323Ssam * (ichiro@netbsd.org) 1782109323Ssam */ 1783178354Ssam if (sc->sc_sta_firmware_ver < 802 ) { 1784109323Ssam /* firm ver < 0.8 variant 2 */ 1785109323Ssam wi_write_val(sc, WI_RID_PROMISC, 1); 1786109323Ssam } 1787109323Ssam wi_write_val(sc, WI_RID_CNFAUTHMODE, 1788178354Ssam vap->iv_bss->ni_authmode); 1789178354Ssam val |= PRIVACY_INVOKED; 1790109323Ssam } else { 1791178354Ssam wi_write_val(sc, WI_RID_CNFAUTHMODE, IEEE80211_AUTH_OPEN); 1792109323Ssam } 1793109323Ssam error = wi_write_val(sc, WI_RID_P2_ENCRYPTION, val); 1794109323Ssam if (error) 1795109323Ssam break; 1796150798Savatar sc->sc_encryption = val; 1797138988Smdodd if ((val & PRIVACY_INVOKED) == 0) 1798138988Smdodd break; 1799178354Ssam error = wi_write_val(sc, WI_RID_P2_TX_CRYPT_KEY, vap->iv_def_txkey); 1800109323Ssam break; 1801109323Ssam } 1802109323Ssam return error; 180346492Swpaul} 180446492Swpaul 1805192492Simpstatic int 1806109323Ssamwi_cmd(struct wi_softc *sc, int cmd, int val0, int val1, int val2) 180746492Swpaul{ 1808178354Ssam int i, s = 0; 1809178354Ssam 1810123098Simp if (sc->wi_gone) 1811123098Simp return (ENODEV); 1812123098Simp 1813109323Ssam /* wait for the busy bit to clear */ 1814123339Simp for (i = sc->wi_cmd_count; i > 0; i--) { /* 500ms */ 1815116206Simp if (!(CSR_READ_2(sc, WI_COMMAND) & WI_CMD_BUSY)) 1816109323Ssam break; 1817123098Simp DELAY(1*1000); /* 1ms */ 1818109323Ssam } 1819109323Ssam if (i == 0) { 1820178354Ssam device_printf(sc->sc_dev, "%s: busy bit won't clear, cmd 0x%x\n", 1821178354Ssam __func__, cmd); 1822123098Simp sc->wi_gone = 1; 1823109323Ssam return(ETIMEDOUT); 1824109323Ssam } 182590580Sbrooks 1826109323Ssam CSR_WRITE_2(sc, WI_PARAM0, val0); 1827109323Ssam CSR_WRITE_2(sc, WI_PARAM1, val1); 1828109323Ssam CSR_WRITE_2(sc, WI_PARAM2, val2); 1829109323Ssam CSR_WRITE_2(sc, WI_COMMAND, cmd); 183090580Sbrooks 1831109323Ssam if (cmd == WI_CMD_INI) { 1832109323Ssam /* XXX: should sleep here. */ 1833116206Simp DELAY(100*1000); /* 100ms delay for init */ 1834109323Ssam } 1835109323Ssam for (i = 0; i < WI_TIMEOUT; i++) { 1836109323Ssam /* 1837109323Ssam * Wait for 'command complete' bit to be 1838109323Ssam * set in the event status register. 1839109323Ssam */ 1840109323Ssam s = CSR_READ_2(sc, WI_EVENT_STAT); 1841109323Ssam if (s & WI_EV_CMD) { 1842109323Ssam /* Ack the event and read result code. */ 1843109323Ssam s = CSR_READ_2(sc, WI_STATUS); 1844109323Ssam CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_CMD); 1845109323Ssam if (s & WI_STAT_CMD_RESULT) { 1846109323Ssam return(EIO); 1847109323Ssam } 1848109323Ssam break; 184990580Sbrooks } 1850109323Ssam DELAY(WI_DELAY); 1851109323Ssam } 185290580Sbrooks 1853109323Ssam if (i == WI_TIMEOUT) { 1854178354Ssam device_printf(sc->sc_dev, "%s: timeout on cmd 0x%04x; " 1855178354Ssam "event status 0x%04x\n", __func__, cmd, s); 1856123098Simp if (s == 0xffff) 1857123098Simp sc->wi_gone = 1; 1858109323Ssam return(ETIMEDOUT); 185953702Swpaul } 1860109323Ssam return (0); 1861109323Ssam} 186253702Swpaul 1863109323Ssamstatic int 1864109323Ssamwi_seek_bap(struct wi_softc *sc, int id, int off) 1865109323Ssam{ 1866109323Ssam int i, status; 186790580Sbrooks 1868109323Ssam CSR_WRITE_2(sc, WI_SEL0, id); 1869109323Ssam CSR_WRITE_2(sc, WI_OFF0, off); 187090580Sbrooks 1871109323Ssam for (i = 0; ; i++) { 1872109323Ssam status = CSR_READ_2(sc, WI_OFF0); 1873109323Ssam if ((status & WI_OFF_BUSY) == 0) 1874109323Ssam break; 1875109323Ssam if (i == WI_TIMEOUT) { 1876178354Ssam device_printf(sc->sc_dev, "%s: timeout, id %x off %x\n", 1877178354Ssam __func__, id, off); 1878109323Ssam sc->sc_bap_off = WI_OFF_ERR; /* invalidate */ 1879123098Simp if (status == 0xffff) 1880123098Simp sc->wi_gone = 1; 1881109323Ssam return ETIMEDOUT; 1882109323Ssam } 1883109323Ssam DELAY(1); 188453702Swpaul } 1885109323Ssam if (status & WI_OFF_ERR) { 1886178354Ssam device_printf(sc->sc_dev, "%s: error, id %x off %x\n", 1887178354Ssam __func__, id, off); 1888109323Ssam sc->sc_bap_off = WI_OFF_ERR; /* invalidate */ 1889109323Ssam return EIO; 1890109323Ssam } 1891109323Ssam sc->sc_bap_id = id; 1892109323Ssam sc->sc_bap_off = off; 1893109323Ssam return 0; 189453702Swpaul} 189553702Swpaul 1896109323Ssamstatic int 1897109323Ssamwi_read_bap(struct wi_softc *sc, int id, int off, void *buf, int buflen) 189853702Swpaul{ 1899109323Ssam u_int16_t *ptr; 1900109323Ssam int i, error, cnt; 190153702Swpaul 1902109323Ssam if (buflen == 0) 1903109323Ssam return 0; 1904109323Ssam if (id != sc->sc_bap_id || off != sc->sc_bap_off) { 1905109323Ssam if ((error = wi_seek_bap(sc, id, off)) != 0) 1906109323Ssam return error; 190775219Salfred } 1908109323Ssam cnt = (buflen + 1) / 2; 1909109323Ssam ptr = (u_int16_t *)buf; 1910109323Ssam for (i = 0; i < cnt; i++) 1911109323Ssam *ptr++ = CSR_READ_2(sc, WI_DATA0); 1912109323Ssam sc->sc_bap_off += cnt * 2; 1913109323Ssam return 0; 191453702Swpaul} 191553702Swpaul 1916109323Ssamstatic int 1917109323Ssamwi_write_bap(struct wi_softc *sc, int id, int off, void *buf, int buflen) 191853702Swpaul{ 1919109323Ssam u_int16_t *ptr; 1920109323Ssam int i, error, cnt; 192146492Swpaul 1922109323Ssam if (buflen == 0) 1923109323Ssam return 0; 192446492Swpaul 1925109323Ssam if (id != sc->sc_bap_id || off != sc->sc_bap_off) { 1926109323Ssam if ((error = wi_seek_bap(sc, id, off)) != 0) 1927109323Ssam return error; 1928109323Ssam } 1929109323Ssam cnt = (buflen + 1) / 2; 1930109323Ssam ptr = (u_int16_t *)buf; 1931109323Ssam for (i = 0; i < cnt; i++) 1932109323Ssam CSR_WRITE_2(sc, WI_DATA0, ptr[i]); 1933109323Ssam sc->sc_bap_off += cnt * 2; 1934109323Ssam 1935109323Ssam return 0; 193646492Swpaul} 193753702Swpaul 1938109323Ssamstatic int 1939109323Ssamwi_mwrite_bap(struct wi_softc *sc, int id, int off, struct mbuf *m0, int totlen) 1940109323Ssam{ 1941109323Ssam int error, len; 1942109323Ssam struct mbuf *m; 194353702Swpaul 1944109323Ssam for (m = m0; m != NULL && totlen > 0; m = m->m_next) { 1945109323Ssam if (m->m_len == 0) 1946109323Ssam continue; 194753702Swpaul 1948109323Ssam len = min(m->m_len, totlen); 194953702Swpaul 1950109323Ssam if (((u_long)m->m_data) % 2 != 0 || len % 2 != 0) { 1951109323Ssam m_copydata(m, 0, totlen, (caddr_t)&sc->sc_txbuf); 1952109323Ssam return wi_write_bap(sc, id, off, (caddr_t)&sc->sc_txbuf, 1953109323Ssam totlen); 1954109323Ssam } 195553702Swpaul 1956109323Ssam if ((error = wi_write_bap(sc, id, off, m->m_data, len)) != 0) 1957109323Ssam return error; 195853702Swpaul 1959109323Ssam off += m->m_len; 1960109323Ssam totlen -= len; 1961109323Ssam } 1962109323Ssam return 0; 1963109323Ssam} 196453702Swpaul 1965109323Ssamstatic int 1966109323Ssamwi_alloc_fid(struct wi_softc *sc, int len, int *idp) 196753702Swpaul{ 196853702Swpaul int i; 196953702Swpaul 1970109323Ssam if (wi_cmd(sc, WI_CMD_ALLOC_MEM, len, 0, 0)) { 1971178354Ssam device_printf(sc->sc_dev, "%s: failed to allocate %d bytes on NIC\n", 1972178354Ssam __func__, len); 1973109323Ssam return ENOMEM; 197453702Swpaul } 197553702Swpaul 1976109323Ssam for (i = 0; i < WI_TIMEOUT; i++) { 1977109323Ssam if (CSR_READ_2(sc, WI_EVENT_STAT) & WI_EV_ALLOC) 1978109323Ssam break; 1979109323Ssam DELAY(1); 198053702Swpaul } 1981144167Ssam if (i == WI_TIMEOUT) { 1982178354Ssam device_printf(sc->sc_dev, "%s: timeout in alloc\n", __func__); 1983144167Ssam return ETIMEDOUT; 1984144167Ssam } 1985109323Ssam *idp = CSR_READ_2(sc, WI_ALLOC_FID); 1986109323Ssam CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_ALLOC); 1987109323Ssam return 0; 1988109323Ssam} 198953702Swpaul 1990109323Ssamstatic int 1991109323Ssamwi_read_rid(struct wi_softc *sc, int rid, void *buf, int *buflenp) 1992109323Ssam{ 1993109323Ssam int error, len; 1994109323Ssam u_int16_t ltbuf[2]; 199553702Swpaul 1996109323Ssam /* Tell the NIC to enter record read mode. */ 1997109323Ssam error = wi_cmd(sc, WI_CMD_ACCESS | WI_ACCESS_READ, rid, 0, 0); 1998109323Ssam if (error) 1999109323Ssam return error; 200053702Swpaul 2001109323Ssam error = wi_read_bap(sc, rid, 0, ltbuf, sizeof(ltbuf)); 2002109323Ssam if (error) 2003109323Ssam return error; 200453702Swpaul 2005109323Ssam if (le16toh(ltbuf[1]) != rid) { 2006109323Ssam device_printf(sc->sc_dev, "record read mismatch, rid=%x, got=%x\n", 2007109323Ssam rid, le16toh(ltbuf[1])); 2008109323Ssam return EIO; 200953702Swpaul } 2010109323Ssam len = (le16toh(ltbuf[0]) - 1) * 2; /* already got rid */ 2011109323Ssam if (*buflenp < len) { 2012109323Ssam device_printf(sc->sc_dev, "record buffer is too small, " 2013109323Ssam "rid=%x, size=%d, len=%d\n", 2014109323Ssam rid, *buflenp, len); 2015109323Ssam return ENOSPC; 201653702Swpaul } 2017109323Ssam *buflenp = len; 2018109323Ssam return wi_read_bap(sc, rid, sizeof(ltbuf), buf, len); 2019109323Ssam} 202053702Swpaul 2021109323Ssamstatic int 2022109323Ssamwi_write_rid(struct wi_softc *sc, int rid, void *buf, int buflen) 2023109323Ssam{ 2024109323Ssam int error; 2025109323Ssam u_int16_t ltbuf[2]; 202653702Swpaul 2027109323Ssam ltbuf[0] = htole16((buflen + 1) / 2 + 1); /* includes rid */ 2028109323Ssam ltbuf[1] = htole16(rid); 202953702Swpaul 2030109323Ssam error = wi_write_bap(sc, rid, 0, ltbuf, sizeof(ltbuf)); 2031178354Ssam if (error) { 2032178354Ssam device_printf(sc->sc_dev, "%s: bap0 write failure, rid 0x%x\n", 2033178354Ssam __func__, rid); 2034109323Ssam return error; 2035178354Ssam } 2036109323Ssam error = wi_write_bap(sc, rid, sizeof(ltbuf), buf, buflen); 2037178354Ssam if (error) { 2038178354Ssam device_printf(sc->sc_dev, "%s: bap1 write failure, rid 0x%x\n", 2039178354Ssam __func__, rid); 2040109323Ssam return error; 2041178354Ssam } 2042102206Simp 2043109323Ssam return wi_cmd(sc, WI_CMD_ACCESS | WI_ACCESS_WRITE, rid, 0, 0); 204453702Swpaul} 204577217Sphk 204688546Salfredstatic int 2047178354Ssamwi_write_appie(struct wi_softc *sc, int rid, const struct ieee80211_appie *ie) 204877217Sphk{ 2049178354Ssam /* NB: 42 bytes is probably ok to have on the stack */ 2050178354Ssam char buf[sizeof(uint16_t) + 40]; 205177217Sphk 2052178354Ssam if (ie->ie_len > 40) 2053178354Ssam return EINVAL; 2054178354Ssam /* NB: firmware requires 16-bit ie length before ie data */ 2055178354Ssam *(uint16_t *) buf = htole16(ie->ie_len); 2056178354Ssam memcpy(buf + sizeof(uint16_t), ie->ie_data, ie->ie_len); 2057178354Ssam return wi_write_rid(sc, rid, buf, ie->ie_len + sizeof(uint16_t)); 205877217Sphk} 205977217Sphk 2060109323Ssamint 2061109323Ssamwi_alloc(device_t dev, int rid) 2062109323Ssam{ 2063109323Ssam struct wi_softc *sc = device_get_softc(dev); 2064109323Ssam 2065109323Ssam if (sc->wi_bus_type != WI_BUS_PCI_NATIVE) { 2066109323Ssam sc->iobase_rid = rid; 2067109323Ssam sc->iobase = bus_alloc_resource(dev, SYS_RES_IOPORT, 2068109323Ssam &sc->iobase_rid, 0, ~0, (1 << 6), 2069109323Ssam rman_make_alignment_flags(1 << 6) | RF_ACTIVE); 2070178354Ssam if (sc->iobase == NULL) { 2071109323Ssam device_printf(dev, "No I/O space?!\n"); 2072178354Ssam return ENXIO; 207398440Simp } 2074109323Ssam 2075109323Ssam sc->wi_io_addr = rman_get_start(sc->iobase); 2076109323Ssam sc->wi_btag = rman_get_bustag(sc->iobase); 2077109323Ssam sc->wi_bhandle = rman_get_bushandle(sc->iobase); 2078109323Ssam } else { 2079109323Ssam sc->mem_rid = rid; 2080127135Snjl sc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, 2081127135Snjl &sc->mem_rid, RF_ACTIVE); 2082178354Ssam if (sc->mem == NULL) { 2083109323Ssam device_printf(dev, "No Mem space on prism2.5?\n"); 2084178354Ssam return ENXIO; 208577217Sphk } 2086109323Ssam 2087109323Ssam sc->wi_btag = rman_get_bustag(sc->mem); 2088109323Ssam sc->wi_bhandle = rman_get_bushandle(sc->mem); 208977217Sphk } 209077217Sphk 2091109323Ssam sc->irq_rid = 0; 2092127135Snjl sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irq_rid, 2093127135Snjl RF_ACTIVE | 2094109323Ssam ((sc->wi_bus_type == WI_BUS_PCCARD) ? 0 : RF_SHAREABLE)); 2095178354Ssam if (sc->irq == NULL) { 2096109323Ssam wi_free(dev); 2097109323Ssam device_printf(dev, "No irq?!\n"); 2098178354Ssam return ENXIO; 209977217Sphk } 2100109323Ssam 2101109323Ssam sc->sc_dev = dev; 2102109323Ssam sc->sc_unit = device_get_unit(dev); 2103178354Ssam return 0; 210477217Sphk} 210593359Simp 2106109323Ssamvoid 2107109323Ssamwi_free(device_t dev) 2108109323Ssam{ 2109109323Ssam struct wi_softc *sc = device_get_softc(dev); 2110109323Ssam 2111109323Ssam if (sc->iobase != NULL) { 2112109323Ssam bus_release_resource(dev, SYS_RES_IOPORT, sc->iobase_rid, sc->iobase); 2113109323Ssam sc->iobase = NULL; 2114109323Ssam } 2115109323Ssam if (sc->irq != NULL) { 2116109323Ssam bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid, sc->irq); 2117109323Ssam sc->irq = NULL; 2118109323Ssam } 2119109323Ssam if (sc->mem != NULL) { 2120109323Ssam bus_release_resource(dev, SYS_RES_MEMORY, sc->mem_rid, sc->mem); 2121109323Ssam sc->mem = NULL; 2122109323Ssam } 2123109323Ssam} 2124