1/*- 2 * Copyright (c) 1997, 1998, 1999 3 * Bill Paul <wpaul@ctr.columbia.edu>. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by Bill Paul. 16 * 4. Neither the name of the author nor the names of any co-contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD 24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 30 * THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33/* 34 * Lucent WaveLAN/IEEE 802.11 PCMCIA driver. 35 * 36 * Original FreeBSD driver written by Bill Paul <wpaul@ctr.columbia.edu> 37 * Electrical Engineering Department 38 * Columbia University, New York City 39 */ 40 41/* 42 * The WaveLAN/IEEE adapter is the second generation of the WaveLAN 43 * from Lucent. Unlike the older cards, the new ones are programmed 44 * entirely via a firmware-driven controller called the Hermes. 45 * Unfortunately, Lucent will not release the Hermes programming manual 46 * without an NDA (if at all). What they do release is an API library 47 * called the HCF (Hardware Control Functions) which is supposed to 48 * do the device-specific operations of a device driver for you. The 49 * publically available version of the HCF library (the 'HCF Light') is 50 * a) extremely gross, b) lacks certain features, particularly support 51 * for 802.11 frames, and c) is contaminated by the GNU Public License. 52 * 53 * This driver does not use the HCF or HCF Light at all. Instead, it 54 * programs the Hermes controller directly, using information gleaned 55 * from the HCF Light code and corresponding documentation. 56 * 57 * This driver supports the ISA, PCMCIA and PCI versions of the Lucent 58 * WaveLan cards (based on the Hermes chipset), as well as the newer 59 * Prism 2 chipsets with firmware from Intersil and Symbol. 60 */ 61 62#include <sys/cdefs.h> 63__FBSDID("$FreeBSD$"); 64 65#define WI_HERMES_STATS_WAR /* Work around stats counter bug. */ 66 67#include <sys/param.h> 68#include <sys/systm.h> 69#include <sys/endian.h> 70#include <sys/sockio.h> 71#include <sys/mbuf.h> 72#include <sys/priv.h> 73#include <sys/proc.h> 74#include <sys/kernel.h> 75#include <sys/socket.h> 76#include <sys/module.h> 77#include <sys/bus.h> 78#include <sys/random.h> 79#include <sys/syslog.h> 80#include <sys/sysctl.h> 81 82#include <machine/bus.h> 83#include <machine/resource.h> 84#include <machine/atomic.h> 85#include <sys/rman.h> 86 87#include <net/if.h> 88#include <net/if_arp.h> 89#include <net/ethernet.h> 90#include <net/if_dl.h> 91#include <net/if_llc.h> 92#include <net/if_media.h> 93#include <net/if_types.h> 94 95#include <net80211/ieee80211_var.h> 96#include <net80211/ieee80211_ioctl.h> 97#include <net80211/ieee80211_radiotap.h> 98 99#include <netinet/in.h> 100#include <netinet/in_systm.h> 101#include <netinet/in_var.h> 102#include <netinet/ip.h> 103#include <netinet/if_ether.h> 104 105#include <net/bpf.h> 106 107#include <dev/wi/if_wavelan_ieee.h> 108#include <dev/wi/if_wireg.h> 109#include <dev/wi/if_wivar.h> 110 111static struct ieee80211vap *wi_vap_create(struct ieee80211com *, 112 const char [IFNAMSIZ], int, enum ieee80211_opmode, int, 113 const uint8_t [IEEE80211_ADDR_LEN], 114 const uint8_t [IEEE80211_ADDR_LEN]); 115static void wi_vap_delete(struct ieee80211vap *vap); 116static void wi_stop_locked(struct wi_softc *sc, int disable); 117static void wi_start_locked(struct ifnet *); 118static void wi_start(struct ifnet *); 119static int wi_start_tx(struct ifnet *ifp, struct wi_frame *frmhdr, 120 struct mbuf *m0); 121static int wi_raw_xmit(struct ieee80211_node *, struct mbuf *, 122 const struct ieee80211_bpf_params *); 123static int wi_newstate_sta(struct ieee80211vap *, enum ieee80211_state, int); 124static int wi_newstate_hostap(struct ieee80211vap *, enum ieee80211_state, 125 int); 126static void wi_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m, 127 int subtype, int rssi, int nf); 128static int wi_reset(struct wi_softc *); 129static void wi_watchdog(void *); 130static int wi_ioctl(struct ifnet *, u_long, caddr_t); 131static void wi_media_status(struct ifnet *, struct ifmediareq *); 132 133static void wi_rx_intr(struct wi_softc *); 134static void wi_tx_intr(struct wi_softc *); 135static void wi_tx_ex_intr(struct wi_softc *); 136 137static void wi_info_intr(struct wi_softc *); 138 139static int wi_write_txrate(struct wi_softc *, struct ieee80211vap *); 140static int wi_write_wep(struct wi_softc *, struct ieee80211vap *); 141static int wi_write_multi(struct wi_softc *); 142static void wi_update_mcast(struct ifnet *); 143static void wi_update_promisc(struct ifnet *); 144static int wi_alloc_fid(struct wi_softc *, int, int *); 145static void wi_read_nicid(struct wi_softc *); 146static int wi_write_ssid(struct wi_softc *, int, u_int8_t *, int); 147 148static int wi_cmd(struct wi_softc *, int, int, int, int); 149static int wi_seek_bap(struct wi_softc *, int, int); 150static int wi_read_bap(struct wi_softc *, int, int, void *, int); 151static int wi_write_bap(struct wi_softc *, int, int, void *, int); 152static int wi_mwrite_bap(struct wi_softc *, int, int, struct mbuf *, int); 153static int wi_read_rid(struct wi_softc *, int, void *, int *); 154static int wi_write_rid(struct wi_softc *, int, void *, int); 155static int wi_write_appie(struct wi_softc *, int, const struct ieee80211_appie *); 156 157static void wi_scan_start(struct ieee80211com *); 158static void wi_scan_end(struct ieee80211com *); 159static void wi_set_channel(struct ieee80211com *); 160 161static __inline int 162wi_write_val(struct wi_softc *sc, int rid, u_int16_t val) 163{ 164 165 val = htole16(val); 166 return wi_write_rid(sc, rid, &val, sizeof(val)); 167} 168 169static SYSCTL_NODE(_hw, OID_AUTO, wi, CTLFLAG_RD, 0, 170 "Wireless driver parameters"); 171 172static struct timeval lasttxerror; /* time of last tx error msg */ 173static int curtxeps; /* current tx error msgs/sec */ 174static int wi_txerate = 0; /* tx error rate: max msgs/sec */ 175SYSCTL_INT(_hw_wi, OID_AUTO, txerate, CTLFLAG_RW, &wi_txerate, 176 0, "max tx error msgs/sec; 0 to disable msgs"); 177 178#define WI_DEBUG 179#ifdef WI_DEBUG 180static int wi_debug = 0; 181SYSCTL_INT(_hw_wi, OID_AUTO, debug, CTLFLAG_RW, &wi_debug, 182 0, "control debugging printfs"); 183#define DPRINTF(X) if (wi_debug) printf X 184#else 185#define DPRINTF(X) 186#endif 187 188#define WI_INTRS (WI_EV_RX | WI_EV_ALLOC | WI_EV_INFO) 189 190struct wi_card_ident wi_card_ident[] = { 191 /* CARD_ID CARD_NAME FIRM_TYPE */ 192 { WI_NIC_LUCENT_ID, WI_NIC_LUCENT_STR, WI_LUCENT }, 193 { WI_NIC_SONY_ID, WI_NIC_SONY_STR, WI_LUCENT }, 194 { WI_NIC_LUCENT_EMB_ID, WI_NIC_LUCENT_EMB_STR, WI_LUCENT }, 195 { WI_NIC_EVB2_ID, WI_NIC_EVB2_STR, WI_INTERSIL }, 196 { WI_NIC_HWB3763_ID, WI_NIC_HWB3763_STR, WI_INTERSIL }, 197 { WI_NIC_HWB3163_ID, WI_NIC_HWB3163_STR, WI_INTERSIL }, 198 { WI_NIC_HWB3163B_ID, WI_NIC_HWB3163B_STR, WI_INTERSIL }, 199 { WI_NIC_EVB3_ID, WI_NIC_EVB3_STR, WI_INTERSIL }, 200 { WI_NIC_HWB1153_ID, WI_NIC_HWB1153_STR, WI_INTERSIL }, 201 { WI_NIC_P2_SST_ID, WI_NIC_P2_SST_STR, WI_INTERSIL }, 202 { WI_NIC_EVB2_SST_ID, WI_NIC_EVB2_SST_STR, WI_INTERSIL }, 203 { WI_NIC_3842_EVA_ID, WI_NIC_3842_EVA_STR, WI_INTERSIL }, 204 { WI_NIC_3842_PCMCIA_AMD_ID, WI_NIC_3842_PCMCIA_STR, WI_INTERSIL }, 205 { WI_NIC_3842_PCMCIA_SST_ID, WI_NIC_3842_PCMCIA_STR, WI_INTERSIL }, 206 { WI_NIC_3842_PCMCIA_ATL_ID, WI_NIC_3842_PCMCIA_STR, WI_INTERSIL }, 207 { WI_NIC_3842_PCMCIA_ATS_ID, WI_NIC_3842_PCMCIA_STR, WI_INTERSIL }, 208 { WI_NIC_3842_MINI_AMD_ID, WI_NIC_3842_MINI_STR, WI_INTERSIL }, 209 { WI_NIC_3842_MINI_SST_ID, WI_NIC_3842_MINI_STR, WI_INTERSIL }, 210 { WI_NIC_3842_MINI_ATL_ID, WI_NIC_3842_MINI_STR, WI_INTERSIL }, 211 { WI_NIC_3842_MINI_ATS_ID, WI_NIC_3842_MINI_STR, WI_INTERSIL }, 212 { WI_NIC_3842_PCI_AMD_ID, WI_NIC_3842_PCI_STR, WI_INTERSIL }, 213 { WI_NIC_3842_PCI_SST_ID, WI_NIC_3842_PCI_STR, WI_INTERSIL }, 214 { WI_NIC_3842_PCI_ATS_ID, WI_NIC_3842_PCI_STR, WI_INTERSIL }, 215 { WI_NIC_3842_PCI_ATL_ID, WI_NIC_3842_PCI_STR, WI_INTERSIL }, 216 { WI_NIC_P3_PCMCIA_AMD_ID, WI_NIC_P3_PCMCIA_STR, WI_INTERSIL }, 217 { WI_NIC_P3_PCMCIA_SST_ID, WI_NIC_P3_PCMCIA_STR, WI_INTERSIL }, 218 { WI_NIC_P3_PCMCIA_ATL_ID, WI_NIC_P3_PCMCIA_STR, WI_INTERSIL }, 219 { WI_NIC_P3_PCMCIA_ATS_ID, WI_NIC_P3_PCMCIA_STR, WI_INTERSIL }, 220 { WI_NIC_P3_MINI_AMD_ID, WI_NIC_P3_MINI_STR, WI_INTERSIL }, 221 { WI_NIC_P3_MINI_SST_ID, WI_NIC_P3_MINI_STR, WI_INTERSIL }, 222 { WI_NIC_P3_MINI_ATL_ID, WI_NIC_P3_MINI_STR, WI_INTERSIL }, 223 { WI_NIC_P3_MINI_ATS_ID, WI_NIC_P3_MINI_STR, WI_INTERSIL }, 224 { 0, NULL, 0 }, 225}; 226 227static char *wi_firmware_names[] = { "none", "Hermes", "Intersil", "Symbol" }; 228 229devclass_t wi_devclass; 230 231int 232wi_attach(device_t dev) 233{ 234 struct wi_softc *sc = device_get_softc(dev); 235 struct ieee80211com *ic; 236 struct ifnet *ifp; 237 int i, nrates, buflen; 238 u_int16_t val; 239 u_int8_t ratebuf[2 + IEEE80211_RATE_SIZE]; 240 struct ieee80211_rateset *rs; 241 struct sysctl_ctx_list *sctx; 242 struct sysctl_oid *soid; 243 static const u_int8_t empty_macaddr[IEEE80211_ADDR_LEN] = { 244 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 245 }; 246 int error; 247 uint8_t macaddr[IEEE80211_ADDR_LEN]; 248 249 ifp = sc->sc_ifp = if_alloc(IFT_IEEE80211); 250 if (ifp == NULL) { 251 device_printf(dev, "can not if_alloc\n"); 252 wi_free(dev); 253 return ENOSPC; 254 } 255 ic = ifp->if_l2com; 256 257 sc->sc_firmware_type = WI_NOTYPE; 258 sc->wi_cmd_count = 500; 259 /* Reset the NIC. */ 260 if (wi_reset(sc) != 0) { 261 wi_free(dev); 262 return ENXIO; /* XXX */ 263 } 264 265 /* Read NIC identification */ 266 wi_read_nicid(sc); 267 switch (sc->sc_firmware_type) { 268 case WI_LUCENT: 269 if (sc->sc_sta_firmware_ver < 60006) 270 goto reject; 271 break; 272 case WI_INTERSIL: 273 if (sc->sc_sta_firmware_ver < 800) 274 goto reject; 275 break; 276 default: 277 reject: 278 device_printf(dev, "Sorry, this card is not supported " 279 "(type %d, firmware ver %d)\n", 280 sc->sc_firmware_type, sc->sc_sta_firmware_ver); 281 wi_free(dev); 282 return EOPNOTSUPP; 283 } 284 285 /* Export info about the device via sysctl */ 286 sctx = device_get_sysctl_ctx(dev); 287 soid = device_get_sysctl_tree(dev); 288 SYSCTL_ADD_STRING(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, 289 "firmware_type", CTLFLAG_RD, 290 wi_firmware_names[sc->sc_firmware_type], 0, 291 "Firmware type string"); 292 SYSCTL_ADD_INT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "sta_version", 293 CTLFLAG_RD, &sc->sc_sta_firmware_ver, 0, 294 "Station Firmware version"); 295 if (sc->sc_firmware_type == WI_INTERSIL) 296 SYSCTL_ADD_INT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, 297 "pri_version", CTLFLAG_RD, &sc->sc_pri_firmware_ver, 0, 298 "Primary Firmware version"); 299 SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "nic_id", 300 CTLFLAG_RD, &sc->sc_nic_id, 0, "NIC id"); 301 SYSCTL_ADD_STRING(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "nic_name", 302 CTLFLAG_RD, sc->sc_nic_name, 0, "NIC name"); 303 304 mtx_init(&sc->sc_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK, 305 MTX_DEF | MTX_RECURSE); 306 callout_init_mtx(&sc->sc_watchdog, &sc->sc_mtx, 0); 307 308 /* 309 * Read the station address. 310 * And do it twice. I've seen PRISM-based cards that return 311 * an error when trying to read it the first time, which causes 312 * the probe to fail. 313 */ 314 buflen = IEEE80211_ADDR_LEN; 315 error = wi_read_rid(sc, WI_RID_MAC_NODE, macaddr, &buflen); 316 if (error != 0) { 317 buflen = IEEE80211_ADDR_LEN; 318 error = wi_read_rid(sc, WI_RID_MAC_NODE, macaddr, &buflen); 319 } 320 if (error || IEEE80211_ADDR_EQ(macaddr, empty_macaddr)) { 321 if (error != 0) 322 device_printf(dev, "mac read failed %d\n", error); 323 else { 324 device_printf(dev, "mac read failed (all zeros)\n"); 325 error = ENXIO; 326 } 327 wi_free(dev); 328 return (error); 329 } 330 331 ifp->if_softc = sc; 332 if_initname(ifp, device_get_name(dev), device_get_unit(dev)); 333 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 334 ifp->if_ioctl = wi_ioctl; 335 ifp->if_start = wi_start; 336 ifp->if_init = wi_init; 337 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen); 338 ifp->if_snd.ifq_drv_maxlen = ifqmaxlen; 339 IFQ_SET_READY(&ifp->if_snd); 340 341 ic->ic_ifp = ifp; 342 ic->ic_phytype = IEEE80211_T_DS; 343 ic->ic_opmode = IEEE80211_M_STA; 344 ic->ic_caps = IEEE80211_C_STA 345 | IEEE80211_C_PMGT 346 | IEEE80211_C_MONITOR 347 ; 348 349 /* 350 * Query the card for available channels and setup the 351 * channel table. We assume these are all 11b channels. 352 */ 353 buflen = sizeof(val); 354 if (wi_read_rid(sc, WI_RID_CHANNEL_LIST, &val, &buflen) != 0) 355 val = htole16(0x1fff); /* assume 1-11 */ 356 KASSERT(val != 0, ("wi_attach: no available channels listed!")); 357 358 val <<= 1; /* shift for base 1 indices */ 359 for (i = 1; i < 16; i++) { 360 struct ieee80211_channel *c; 361 362 if (!isset((u_int8_t*)&val, i)) 363 continue; 364 c = &ic->ic_channels[ic->ic_nchans++]; 365 c->ic_freq = ieee80211_ieee2mhz(i, IEEE80211_CHAN_B); 366 c->ic_flags = IEEE80211_CHAN_B; 367 c->ic_ieee = i; 368 /* XXX txpowers? */ 369 } 370 371 /* 372 * Set flags based on firmware version. 373 */ 374 switch (sc->sc_firmware_type) { 375 case WI_LUCENT: 376 sc->sc_ntxbuf = 1; 377 ic->ic_caps |= IEEE80211_C_IBSS; 378 379 sc->sc_ibss_port = WI_PORTTYPE_BSS; 380 sc->sc_monitor_port = WI_PORTTYPE_ADHOC; 381 sc->sc_min_rssi = WI_LUCENT_MIN_RSSI; 382 sc->sc_max_rssi = WI_LUCENT_MAX_RSSI; 383 sc->sc_dbm_offset = WI_LUCENT_DBM_OFFSET; 384 break; 385 case WI_INTERSIL: 386 sc->sc_ntxbuf = WI_NTXBUF; 387 sc->sc_flags |= WI_FLAGS_HAS_FRAGTHR 388 | WI_FLAGS_HAS_ROAMING; 389 /* 390 * Old firmware are slow, so give peace a chance. 391 */ 392 if (sc->sc_sta_firmware_ver < 10000) 393 sc->wi_cmd_count = 5000; 394 if (sc->sc_sta_firmware_ver > 10101) 395 sc->sc_flags |= WI_FLAGS_HAS_DBMADJUST; 396 ic->ic_caps |= IEEE80211_C_IBSS; 397 /* 398 * version 0.8.3 and newer are the only ones that are known 399 * to currently work. Earlier versions can be made to work, 400 * at least according to the Linux driver but we require 401 * monitor mode so this is irrelevant. 402 */ 403 ic->ic_caps |= IEEE80211_C_HOSTAP; 404 if (sc->sc_sta_firmware_ver >= 10603) 405 sc->sc_flags |= WI_FLAGS_HAS_ENHSECURITY; 406 if (sc->sc_sta_firmware_ver >= 10700) { 407 /* 408 * 1.7.0+ have the necessary support for sta mode WPA. 409 */ 410 sc->sc_flags |= WI_FLAGS_HAS_WPASUPPORT; 411 ic->ic_caps |= IEEE80211_C_WPA; 412 } 413 414 sc->sc_ibss_port = WI_PORTTYPE_IBSS; 415 sc->sc_monitor_port = WI_PORTTYPE_APSILENT; 416 sc->sc_min_rssi = WI_PRISM_MIN_RSSI; 417 sc->sc_max_rssi = WI_PRISM_MAX_RSSI; 418 sc->sc_dbm_offset = WI_PRISM_DBM_OFFSET; 419 break; 420 } 421 422 /* 423 * Find out if we support WEP on this card. 424 */ 425 buflen = sizeof(val); 426 if (wi_read_rid(sc, WI_RID_WEP_AVAIL, &val, &buflen) == 0 && 427 val != htole16(0)) 428 ic->ic_cryptocaps |= IEEE80211_CRYPTO_WEP; 429 430 /* Find supported rates. */ 431 buflen = sizeof(ratebuf); 432 rs = &ic->ic_sup_rates[IEEE80211_MODE_11B]; 433 if (wi_read_rid(sc, WI_RID_DATA_RATES, ratebuf, &buflen) == 0) { 434 nrates = le16toh(*(u_int16_t *)ratebuf); 435 if (nrates > IEEE80211_RATE_MAXSIZE) 436 nrates = IEEE80211_RATE_MAXSIZE; 437 rs->rs_nrates = 0; 438 for (i = 0; i < nrates; i++) 439 if (ratebuf[2+i]) 440 rs->rs_rates[rs->rs_nrates++] = ratebuf[2+i]; 441 } else { 442 /* XXX fallback on error? */ 443 } 444 445 buflen = sizeof(val); 446 if ((sc->sc_flags & WI_FLAGS_HAS_DBMADJUST) && 447 wi_read_rid(sc, WI_RID_DBM_ADJUST, &val, &buflen) == 0) { 448 sc->sc_dbm_offset = le16toh(val); 449 } 450 451 sc->sc_portnum = WI_DEFAULT_PORT; 452 453 ieee80211_ifattach(ic, macaddr); 454 ic->ic_raw_xmit = wi_raw_xmit; 455 ic->ic_scan_start = wi_scan_start; 456 ic->ic_scan_end = wi_scan_end; 457 ic->ic_set_channel = wi_set_channel; 458 459 ic->ic_vap_create = wi_vap_create; 460 ic->ic_vap_delete = wi_vap_delete; 461 ic->ic_update_mcast = wi_update_mcast; 462 ic->ic_update_promisc = wi_update_promisc; 463 464 ieee80211_radiotap_attach(ic, 465 &sc->sc_tx_th.wt_ihdr, sizeof(sc->sc_tx_th), 466 WI_TX_RADIOTAP_PRESENT, 467 &sc->sc_rx_th.wr_ihdr, sizeof(sc->sc_rx_th), 468 WI_RX_RADIOTAP_PRESENT); 469 470 if (bootverbose) 471 ieee80211_announce(ic); 472 473 error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET | INTR_MPSAFE, 474 NULL, wi_intr, sc, &sc->wi_intrhand); 475 if (error) { 476 device_printf(dev, "bus_setup_intr() failed! (%d)\n", error); 477 ieee80211_ifdetach(ic); 478 if_free(sc->sc_ifp); 479 wi_free(dev); 480 return error; 481 } 482 483 return (0); 484} 485 486int 487wi_detach(device_t dev) 488{ 489 struct wi_softc *sc = device_get_softc(dev); 490 struct ifnet *ifp = sc->sc_ifp; 491 struct ieee80211com *ic = ifp->if_l2com; 492 493 WI_LOCK(sc); 494 495 /* check if device was removed */ 496 sc->wi_gone |= !bus_child_present(dev); 497 498 wi_stop_locked(sc, 0); 499 WI_UNLOCK(sc); 500 ieee80211_ifdetach(ic); 501 502 bus_teardown_intr(dev, sc->irq, sc->wi_intrhand); 503 if_free(sc->sc_ifp); 504 wi_free(dev); 505 mtx_destroy(&sc->sc_mtx); 506 return (0); 507} 508 509static struct ieee80211vap * 510wi_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit, 511 enum ieee80211_opmode opmode, int flags, 512 const uint8_t bssid[IEEE80211_ADDR_LEN], 513 const uint8_t mac[IEEE80211_ADDR_LEN]) 514{ 515 struct wi_softc *sc = ic->ic_ifp->if_softc; 516 struct wi_vap *wvp; 517 struct ieee80211vap *vap; 518 519 if (!TAILQ_EMPTY(&ic->ic_vaps)) /* only one at a time */ 520 return NULL; 521 wvp = (struct wi_vap *) malloc(sizeof(struct wi_vap), 522 M_80211_VAP, M_NOWAIT | M_ZERO); 523 if (wvp == NULL) 524 return NULL; 525 526 vap = &wvp->wv_vap; 527 ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid, mac); 528 529 vap->iv_max_aid = WI_MAX_AID; 530 531 switch (opmode) { 532 case IEEE80211_M_STA: 533 sc->sc_porttype = WI_PORTTYPE_BSS; 534 wvp->wv_newstate = vap->iv_newstate; 535 vap->iv_newstate = wi_newstate_sta; 536 /* need to filter mgt frames to avoid confusing state machine */ 537 wvp->wv_recv_mgmt = vap->iv_recv_mgmt; 538 vap->iv_recv_mgmt = wi_recv_mgmt; 539 break; 540 case IEEE80211_M_IBSS: 541 sc->sc_porttype = sc->sc_ibss_port; 542 wvp->wv_newstate = vap->iv_newstate; 543 vap->iv_newstate = wi_newstate_sta; 544 break; 545 case IEEE80211_M_AHDEMO: 546 sc->sc_porttype = WI_PORTTYPE_ADHOC; 547 break; 548 case IEEE80211_M_HOSTAP: 549 sc->sc_porttype = WI_PORTTYPE_HOSTAP; 550 wvp->wv_newstate = vap->iv_newstate; 551 vap->iv_newstate = wi_newstate_hostap; 552 break; 553 case IEEE80211_M_MONITOR: 554 sc->sc_porttype = sc->sc_monitor_port; 555 break; 556 default: 557 break; 558 } 559 560 /* complete setup */ 561 ieee80211_vap_attach(vap, ieee80211_media_change, wi_media_status); 562 ic->ic_opmode = opmode; 563 return vap; 564} 565 566static void 567wi_vap_delete(struct ieee80211vap *vap) 568{ 569 struct wi_vap *wvp = WI_VAP(vap); 570 571 ieee80211_vap_detach(vap); 572 free(wvp, M_80211_VAP); 573} 574 575int 576wi_shutdown(device_t dev) 577{ 578 struct wi_softc *sc = device_get_softc(dev); 579 580 wi_stop(sc, 1); 581 return (0); 582} 583 584void 585wi_intr(void *arg) 586{ 587 struct wi_softc *sc = arg; 588 struct ifnet *ifp = sc->sc_ifp; 589 u_int16_t status; 590 591 WI_LOCK(sc); 592 593 if (sc->wi_gone || !sc->sc_enabled || (ifp->if_flags & IFF_UP) == 0) { 594 CSR_WRITE_2(sc, WI_INT_EN, 0); 595 CSR_WRITE_2(sc, WI_EVENT_ACK, 0xFFFF); 596 WI_UNLOCK(sc); 597 return; 598 } 599 600 /* Disable interrupts. */ 601 CSR_WRITE_2(sc, WI_INT_EN, 0); 602 603 status = CSR_READ_2(sc, WI_EVENT_STAT); 604 if (status & WI_EV_RX) 605 wi_rx_intr(sc); 606 if (status & WI_EV_ALLOC) 607 wi_tx_intr(sc); 608 if (status & WI_EV_TX_EXC) 609 wi_tx_ex_intr(sc); 610 if (status & WI_EV_INFO) 611 wi_info_intr(sc); 612 if ((ifp->if_drv_flags & IFF_DRV_OACTIVE) == 0 && 613 !IFQ_DRV_IS_EMPTY(&ifp->if_snd)) 614 wi_start_locked(ifp); 615 616 /* Re-enable interrupts. */ 617 CSR_WRITE_2(sc, WI_INT_EN, WI_INTRS); 618 619 WI_UNLOCK(sc); 620 621 return; 622} 623 624static void 625wi_enable(struct wi_softc *sc) 626{ 627 /* Enable interrupts */ 628 CSR_WRITE_2(sc, WI_INT_EN, WI_INTRS); 629 630 /* enable port */ 631 wi_cmd(sc, WI_CMD_ENABLE | sc->sc_portnum, 0, 0, 0); 632 sc->sc_enabled = 1; 633} 634 635static int 636wi_setup_locked(struct wi_softc *sc, int porttype, int mode, 637 uint8_t mac[IEEE80211_ADDR_LEN]) 638{ 639 int i; 640 641 wi_reset(sc); 642 643 wi_write_val(sc, WI_RID_PORTTYPE, porttype); 644 wi_write_val(sc, WI_RID_CREATE_IBSS, mode); 645 wi_write_val(sc, WI_RID_MAX_DATALEN, 2304); 646 /* XXX IEEE80211_BPF_NOACK wants 0 */ 647 wi_write_val(sc, WI_RID_ALT_RETRY_CNT, 2); 648 if (sc->sc_flags & WI_FLAGS_HAS_ROAMING) 649 wi_write_val(sc, WI_RID_ROAMING_MODE, 3); /* NB: disabled */ 650 651 wi_write_rid(sc, WI_RID_MAC_NODE, mac, IEEE80211_ADDR_LEN); 652 653 /* Allocate fids for the card */ 654 sc->sc_buflen = IEEE80211_MAX_LEN + sizeof(struct wi_frame); 655 for (i = 0; i < sc->sc_ntxbuf; i++) { 656 int error = wi_alloc_fid(sc, sc->sc_buflen, 657 &sc->sc_txd[i].d_fid); 658 if (error) { 659 device_printf(sc->sc_dev, 660 "tx buffer allocation failed (error %u)\n", 661 error); 662 return error; 663 } 664 sc->sc_txd[i].d_len = 0; 665 } 666 sc->sc_txcur = sc->sc_txnext = 0; 667 668 return 0; 669} 670 671static void 672wi_init_locked(struct wi_softc *sc) 673{ 674 struct ifnet *ifp = sc->sc_ifp; 675 int wasenabled; 676 677 WI_LOCK_ASSERT(sc); 678 679 wasenabled = sc->sc_enabled; 680 if (wasenabled) 681 wi_stop_locked(sc, 1); 682 683 if (wi_setup_locked(sc, sc->sc_porttype, 3, IF_LLADDR(ifp)) != 0) { 684 if_printf(ifp, "interface not running\n"); 685 wi_stop_locked(sc, 1); 686 return; 687 } 688 689 ifp->if_drv_flags |= IFF_DRV_RUNNING; 690 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 691 692 callout_reset(&sc->sc_watchdog, hz, wi_watchdog, sc); 693 694 wi_enable(sc); /* Enable desired port */ 695} 696 697void 698wi_init(void *arg) 699{ 700 struct wi_softc *sc = arg; 701 struct ifnet *ifp = sc->sc_ifp; 702 struct ieee80211com *ic = ifp->if_l2com; 703 704 WI_LOCK(sc); 705 wi_init_locked(sc); 706 WI_UNLOCK(sc); 707 708 if (ifp->if_drv_flags & IFF_DRV_RUNNING) 709 ieee80211_start_all(ic); /* start all vap's */ 710} 711 712static void 713wi_stop_locked(struct wi_softc *sc, int disable) 714{ 715 struct ifnet *ifp = sc->sc_ifp; 716 717 WI_LOCK_ASSERT(sc); 718 719 if (sc->sc_enabled && !sc->wi_gone) { 720 CSR_WRITE_2(sc, WI_INT_EN, 0); 721 wi_cmd(sc, WI_CMD_DISABLE | sc->sc_portnum, 0, 0, 0); 722 if (disable) 723 sc->sc_enabled = 0; 724 } else if (sc->wi_gone && disable) /* gone --> not enabled */ 725 sc->sc_enabled = 0; 726 727 callout_stop(&sc->sc_watchdog); 728 sc->sc_tx_timer = 0; 729 sc->sc_false_syns = 0; 730 731 ifp->if_drv_flags &= ~(IFF_DRV_OACTIVE | IFF_DRV_RUNNING); 732} 733 734void 735wi_stop(struct wi_softc *sc, int disable) 736{ 737 WI_LOCK(sc); 738 wi_stop_locked(sc, disable); 739 WI_UNLOCK(sc); 740} 741 742static void 743wi_set_channel(struct ieee80211com *ic) 744{ 745 struct ifnet *ifp = ic->ic_ifp; 746 struct wi_softc *sc = ifp->if_softc; 747 748 DPRINTF(("%s: channel %d, %sscanning\n", __func__, 749 ieee80211_chan2ieee(ic, ic->ic_curchan), 750 ic->ic_flags & IEEE80211_F_SCAN ? "" : "!")); 751 752 WI_LOCK(sc); 753 wi_write_val(sc, WI_RID_OWN_CHNL, 754 ieee80211_chan2ieee(ic, ic->ic_curchan)); 755 WI_UNLOCK(sc); 756} 757 758static void 759wi_scan_start(struct ieee80211com *ic) 760{ 761 struct ifnet *ifp = ic->ic_ifp; 762 struct wi_softc *sc = ifp->if_softc; 763 struct ieee80211_scan_state *ss = ic->ic_scan; 764 765 DPRINTF(("%s\n", __func__)); 766 767 WI_LOCK(sc); 768 /* 769 * Switch device to monitor mode. 770 */ 771 wi_write_val(sc, WI_RID_PORTTYPE, sc->sc_monitor_port); 772 if (sc->sc_firmware_type == WI_INTERSIL) { 773 wi_cmd(sc, WI_CMD_DISABLE | WI_PORT0, 0, 0, 0); 774 wi_cmd(sc, WI_CMD_ENABLE | WI_PORT0, 0, 0, 0); 775 } 776 /* force full dwell time to compensate for firmware overhead */ 777 ss->ss_mindwell = ss->ss_maxdwell = msecs_to_ticks(400); 778 WI_UNLOCK(sc); 779 780} 781 782static void 783wi_scan_end(struct ieee80211com *ic) 784{ 785 struct ifnet *ifp = ic->ic_ifp; 786 struct wi_softc *sc = ifp->if_softc; 787 788 DPRINTF(("%s: restore port type %d\n", __func__, sc->sc_porttype)); 789 790 WI_LOCK(sc); 791 wi_write_val(sc, WI_RID_PORTTYPE, sc->sc_porttype); 792 if (sc->sc_firmware_type == WI_INTERSIL) { 793 wi_cmd(sc, WI_CMD_DISABLE | WI_PORT0, 0, 0, 0); 794 wi_cmd(sc, WI_CMD_ENABLE | WI_PORT0, 0, 0, 0); 795 } 796 WI_UNLOCK(sc); 797} 798 799static void 800wi_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m, 801 int subtype, int rssi, int nf) 802{ 803 struct ieee80211vap *vap = ni->ni_vap; 804 805 switch (subtype) { 806 case IEEE80211_FC0_SUBTYPE_AUTH: 807 case IEEE80211_FC0_SUBTYPE_ASSOC_RESP: 808 case IEEE80211_FC0_SUBTYPE_REASSOC_RESP: 809 /* NB: filter frames that trigger state changes */ 810 return; 811 } 812 WI_VAP(vap)->wv_recv_mgmt(ni, m, subtype, rssi, nf); 813} 814 815static int 816wi_newstate_sta(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg) 817{ 818 struct ieee80211com *ic = vap->iv_ic; 819 struct ifnet *ifp = ic->ic_ifp; 820 struct ieee80211_node *bss; 821 struct wi_softc *sc = ifp->if_softc; 822 823 DPRINTF(("%s: %s -> %s\n", __func__, 824 ieee80211_state_name[vap->iv_state], 825 ieee80211_state_name[nstate])); 826 827 if (nstate == IEEE80211_S_AUTH) { 828 WI_LOCK(sc); 829 wi_setup_locked(sc, WI_PORTTYPE_BSS, 3, vap->iv_myaddr); 830 831 if (vap->iv_flags & IEEE80211_F_PMGTON) { 832 wi_write_val(sc, WI_RID_MAX_SLEEP, ic->ic_lintval); 833 wi_write_val(sc, WI_RID_PM_ENABLED, 1); 834 } 835 wi_write_val(sc, WI_RID_RTS_THRESH, vap->iv_rtsthreshold); 836 if (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR) 837 wi_write_val(sc, WI_RID_FRAG_THRESH, 838 vap->iv_fragthreshold); 839 wi_write_txrate(sc, vap); 840 841 bss = vap->iv_bss; 842 wi_write_ssid(sc, WI_RID_DESIRED_SSID, bss->ni_essid, bss->ni_esslen); 843 wi_write_val(sc, WI_RID_OWN_CHNL, 844 ieee80211_chan2ieee(ic, bss->ni_chan)); 845 846 /* Configure WEP. */ 847 if (ic->ic_cryptocaps & IEEE80211_CRYPTO_WEP) 848 wi_write_wep(sc, vap); 849 else 850 sc->sc_encryption = 0; 851 852 if ((sc->sc_flags & WI_FLAGS_HAS_WPASUPPORT) && 853 (vap->iv_flags & IEEE80211_F_WPA)) { 854 wi_write_val(sc, WI_RID_WPA_HANDLING, 1); 855 if (vap->iv_appie_wpa != NULL) 856 wi_write_appie(sc, WI_RID_WPA_DATA, 857 vap->iv_appie_wpa); 858 } 859 860 wi_enable(sc); /* enable port */ 861 862 /* Lucent firmware does not support the JOIN RID. */ 863 if (sc->sc_firmware_type == WI_INTERSIL) { 864 struct wi_joinreq join; 865 866 memset(&join, 0, sizeof(join)); 867 IEEE80211_ADDR_COPY(&join.wi_bssid, bss->ni_bssid); 868 join.wi_chan = htole16( 869 ieee80211_chan2ieee(ic, bss->ni_chan)); 870 wi_write_rid(sc, WI_RID_JOIN_REQ, &join, sizeof(join)); 871 } 872 WI_UNLOCK(sc); 873 874 /* 875 * NB: don't go through 802.11 layer, it'll send auth frame; 876 * instead we drive the state machine from the link status 877 * notification we get on association. 878 */ 879 vap->iv_state = nstate; 880 return (0); 881 } 882 return WI_VAP(vap)->wv_newstate(vap, nstate, arg); 883} 884 885static int 886wi_newstate_hostap(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg) 887{ 888 struct ieee80211com *ic = vap->iv_ic; 889 struct ifnet *ifp = ic->ic_ifp; 890 struct ieee80211_node *bss; 891 struct wi_softc *sc = ifp->if_softc; 892 int error; 893 894 DPRINTF(("%s: %s -> %s\n", __func__, 895 ieee80211_state_name[vap->iv_state], 896 ieee80211_state_name[nstate])); 897 898 error = WI_VAP(vap)->wv_newstate(vap, nstate, arg); 899 if (error == 0 && nstate == IEEE80211_S_RUN) { 900 WI_LOCK(sc); 901 wi_setup_locked(sc, WI_PORTTYPE_HOSTAP, 0, vap->iv_myaddr); 902 903 bss = vap->iv_bss; 904 wi_write_ssid(sc, WI_RID_OWN_SSID, 905 bss->ni_essid, bss->ni_esslen); 906 wi_write_val(sc, WI_RID_OWN_CHNL, 907 ieee80211_chan2ieee(ic, bss->ni_chan)); 908 wi_write_val(sc, WI_RID_BASIC_RATE, 0x3); 909 wi_write_val(sc, WI_RID_SUPPORT_RATE, 0xf); 910 wi_write_txrate(sc, vap); 911 912 wi_write_val(sc, WI_RID_OWN_BEACON_INT, bss->ni_intval); 913 wi_write_val(sc, WI_RID_DTIM_PERIOD, vap->iv_dtim_period); 914 915 wi_write_val(sc, WI_RID_RTS_THRESH, vap->iv_rtsthreshold); 916 if (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR) 917 wi_write_val(sc, WI_RID_FRAG_THRESH, 918 vap->iv_fragthreshold); 919 920 if ((sc->sc_flags & WI_FLAGS_HAS_ENHSECURITY) && 921 (vap->iv_flags & IEEE80211_F_HIDESSID)) { 922 /* 923 * bit 0 means hide SSID in beacons, 924 * bit 1 means don't respond to bcast probe req 925 */ 926 wi_write_val(sc, WI_RID_ENH_SECURITY, 0x3); 927 } 928 929 if ((sc->sc_flags & WI_FLAGS_HAS_WPASUPPORT) && 930 (vap->iv_flags & IEEE80211_F_WPA) && 931 vap->iv_appie_wpa != NULL) 932 wi_write_appie(sc, WI_RID_WPA_DATA, vap->iv_appie_wpa); 933 934 wi_write_val(sc, WI_RID_PROMISC, 0); 935 936 /* Configure WEP. */ 937 if (ic->ic_cryptocaps & IEEE80211_CRYPTO_WEP) 938 wi_write_wep(sc, vap); 939 else 940 sc->sc_encryption = 0; 941 942 wi_enable(sc); /* enable port */ 943 WI_UNLOCK(sc); 944 } 945 return error; 946} 947 948static void 949wi_start_locked(struct ifnet *ifp) 950{ 951 struct wi_softc *sc = ifp->if_softc; 952 struct ieee80211_node *ni; 953 struct ieee80211_frame *wh; 954 struct mbuf *m0; 955 struct ieee80211_key *k; 956 struct wi_frame frmhdr; 957 const struct llc *llc; 958 int cur; 959 960 WI_LOCK_ASSERT(sc); 961 962 if (sc->wi_gone) 963 return; 964 965 memset(&frmhdr, 0, sizeof(frmhdr)); 966 cur = sc->sc_txnext; 967 for (;;) { 968 IFQ_DRV_DEQUEUE(&ifp->if_snd, m0); 969 if (m0 == NULL) 970 break; 971 if (sc->sc_txd[cur].d_len != 0) { 972 IFQ_DRV_PREPEND(&ifp->if_snd, m0); 973 ifp->if_drv_flags |= IFF_DRV_OACTIVE; 974 break; 975 } 976 ni = (struct ieee80211_node *) m0->m_pkthdr.rcvif; 977 978 /* reconstruct 802.3 header */ 979 wh = mtod(m0, struct ieee80211_frame *); 980 switch (wh->i_fc[1]) { 981 case IEEE80211_FC1_DIR_TODS: 982 IEEE80211_ADDR_COPY(frmhdr.wi_ehdr.ether_shost, 983 wh->i_addr2); 984 IEEE80211_ADDR_COPY(frmhdr.wi_ehdr.ether_dhost, 985 wh->i_addr3); 986 break; 987 case IEEE80211_FC1_DIR_NODS: 988 IEEE80211_ADDR_COPY(frmhdr.wi_ehdr.ether_shost, 989 wh->i_addr2); 990 IEEE80211_ADDR_COPY(frmhdr.wi_ehdr.ether_dhost, 991 wh->i_addr1); 992 break; 993 case IEEE80211_FC1_DIR_FROMDS: 994 IEEE80211_ADDR_COPY(frmhdr.wi_ehdr.ether_shost, 995 wh->i_addr3); 996 IEEE80211_ADDR_COPY(frmhdr.wi_ehdr.ether_dhost, 997 wh->i_addr1); 998 break; 999 } 1000 llc = (const struct llc *)( 1001 mtod(m0, const uint8_t *) + ieee80211_hdrsize(wh)); 1002 frmhdr.wi_ehdr.ether_type = llc->llc_snap.ether_type; 1003 frmhdr.wi_tx_ctl = htole16(WI_ENC_TX_802_11|WI_TXCNTL_TX_EX); 1004 if (wh->i_fc[1] & IEEE80211_FC1_WEP) { 1005 k = ieee80211_crypto_encap(ni, m0); 1006 if (k == NULL) { 1007 ieee80211_free_node(ni); 1008 m_freem(m0); 1009 continue; 1010 } 1011 frmhdr.wi_tx_ctl |= htole16(WI_TXCNTL_NOCRYPT); 1012 } 1013 1014 if (ieee80211_radiotap_active_vap(ni->ni_vap)) { 1015 sc->sc_tx_th.wt_rate = ni->ni_txrate; 1016 ieee80211_radiotap_tx(ni->ni_vap, m0); 1017 } 1018 1019 m_copydata(m0, 0, sizeof(struct ieee80211_frame), 1020 (caddr_t)&frmhdr.wi_whdr); 1021 m_adj(m0, sizeof(struct ieee80211_frame)); 1022 frmhdr.wi_dat_len = htole16(m0->m_pkthdr.len); 1023 ieee80211_free_node(ni); 1024 if (wi_start_tx(ifp, &frmhdr, m0)) 1025 continue; 1026 1027 sc->sc_txnext = cur = (cur + 1) % sc->sc_ntxbuf; 1028 ifp->if_opackets++; 1029 } 1030} 1031 1032static void 1033wi_start(struct ifnet *ifp) 1034{ 1035 struct wi_softc *sc = ifp->if_softc; 1036 1037 WI_LOCK(sc); 1038 wi_start_locked(ifp); 1039 WI_UNLOCK(sc); 1040} 1041 1042static int 1043wi_start_tx(struct ifnet *ifp, struct wi_frame *frmhdr, struct mbuf *m0) 1044{ 1045 struct wi_softc *sc = ifp->if_softc; 1046 int cur = sc->sc_txnext; 1047 int fid, off, error; 1048 1049 fid = sc->sc_txd[cur].d_fid; 1050 off = sizeof(*frmhdr); 1051 error = wi_write_bap(sc, fid, 0, frmhdr, sizeof(*frmhdr)) != 0 1052 || wi_mwrite_bap(sc, fid, off, m0, m0->m_pkthdr.len) != 0; 1053 m_freem(m0); 1054 if (error) { 1055 ifp->if_oerrors++; 1056 return -1; 1057 } 1058 sc->sc_txd[cur].d_len = off; 1059 if (sc->sc_txcur == cur) { 1060 if (wi_cmd(sc, WI_CMD_TX | WI_RECLAIM, fid, 0, 0)) { 1061 if_printf(ifp, "xmit failed\n"); 1062 sc->sc_txd[cur].d_len = 0; 1063 return -1; 1064 } 1065 sc->sc_tx_timer = 5; 1066 } 1067 return 0; 1068} 1069 1070static int 1071wi_raw_xmit(struct ieee80211_node *ni, struct mbuf *m0, 1072 const struct ieee80211_bpf_params *params) 1073{ 1074 struct ieee80211com *ic = ni->ni_ic; 1075 struct ifnet *ifp = ic->ic_ifp; 1076 struct ieee80211vap *vap = ni->ni_vap; 1077 struct wi_softc *sc = ifp->if_softc; 1078 struct ieee80211_key *k; 1079 struct ieee80211_frame *wh; 1080 struct wi_frame frmhdr; 1081 int cur; 1082 int rc = 0; 1083 1084 WI_LOCK(sc); 1085 1086 if (sc->wi_gone) { 1087 rc = ENETDOWN; 1088 goto out; 1089 } 1090 memset(&frmhdr, 0, sizeof(frmhdr)); 1091 cur = sc->sc_txnext; 1092 if (sc->sc_txd[cur].d_len != 0) { 1093 ifp->if_drv_flags |= IFF_DRV_OACTIVE; 1094 rc = ENOBUFS; 1095 goto out; 1096 } 1097 m0->m_pkthdr.rcvif = NULL; 1098 1099 m_copydata(m0, 4, ETHER_ADDR_LEN * 2, 1100 (caddr_t)&frmhdr.wi_ehdr); 1101 frmhdr.wi_ehdr.ether_type = 0; 1102 wh = mtod(m0, struct ieee80211_frame *); 1103 1104 frmhdr.wi_tx_ctl = htole16(WI_ENC_TX_802_11|WI_TXCNTL_TX_EX); 1105 if (params && (params->ibp_flags & IEEE80211_BPF_NOACK)) 1106 frmhdr.wi_tx_ctl |= htole16(WI_TXCNTL_ALTRTRY); 1107 if ((wh->i_fc[1] & IEEE80211_FC1_WEP) && 1108 (!params || (params && (params->ibp_flags & IEEE80211_BPF_CRYPTO)))) { 1109 k = ieee80211_crypto_encap(ni, m0); 1110 if (k == NULL) { 1111 rc = ENOMEM; 1112 goto out; 1113 } 1114 frmhdr.wi_tx_ctl |= htole16(WI_TXCNTL_NOCRYPT); 1115 } 1116 if (ieee80211_radiotap_active_vap(vap)) { 1117 sc->sc_tx_th.wt_rate = ni->ni_txrate; 1118 ieee80211_radiotap_tx(vap, m0); 1119 } 1120 m_copydata(m0, 0, sizeof(struct ieee80211_frame), 1121 (caddr_t)&frmhdr.wi_whdr); 1122 m_adj(m0, sizeof(struct ieee80211_frame)); 1123 frmhdr.wi_dat_len = htole16(m0->m_pkthdr.len); 1124 if (wi_start_tx(ifp, &frmhdr, m0) < 0) { 1125 m0 = NULL; 1126 rc = EIO; 1127 goto out; 1128 } 1129 m0 = NULL; 1130 1131 sc->sc_txnext = cur = (cur + 1) % sc->sc_ntxbuf; 1132out: 1133 WI_UNLOCK(sc); 1134 1135 if (m0 != NULL) 1136 m_freem(m0); 1137 ieee80211_free_node(ni); 1138 return rc; 1139} 1140 1141static int 1142wi_reset(struct wi_softc *sc) 1143{ 1144#define WI_INIT_TRIES 3 1145 int i, error = 0; 1146 1147 for (i = 0; i < WI_INIT_TRIES; i++) { 1148 error = wi_cmd(sc, WI_CMD_INI, 0, 0, 0); 1149 if (error == 0) 1150 break; 1151 DELAY(WI_DELAY * 1000); 1152 } 1153 sc->sc_reset = 1; 1154 if (i == WI_INIT_TRIES) { 1155 if_printf(sc->sc_ifp, "reset failed\n"); 1156 return error; 1157 } 1158 1159 CSR_WRITE_2(sc, WI_INT_EN, 0); 1160 CSR_WRITE_2(sc, WI_EVENT_ACK, 0xFFFF); 1161 1162 /* Calibrate timer. */ 1163 wi_write_val(sc, WI_RID_TICK_TIME, 8); 1164 1165 return 0; 1166#undef WI_INIT_TRIES 1167} 1168 1169static void 1170wi_watchdog(void *arg) 1171{ 1172 struct wi_softc *sc = arg; 1173 struct ifnet *ifp = sc->sc_ifp; 1174 1175 WI_LOCK_ASSERT(sc); 1176 1177 if (!sc->sc_enabled) 1178 return; 1179 1180 if (sc->sc_tx_timer && --sc->sc_tx_timer == 0) { 1181 if_printf(ifp, "device timeout\n"); 1182 ifp->if_oerrors++; 1183 wi_init_locked(ifp->if_softc); 1184 return; 1185 } 1186 callout_reset(&sc->sc_watchdog, hz, wi_watchdog, sc); 1187} 1188 1189static int 1190wi_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 1191{ 1192 struct wi_softc *sc = ifp->if_softc; 1193 struct ieee80211com *ic = ifp->if_l2com; 1194 struct ifreq *ifr = (struct ifreq *) data; 1195 int error = 0, startall = 0; 1196 1197 switch (cmd) { 1198 case SIOCSIFFLAGS: 1199 WI_LOCK(sc); 1200 /* 1201 * Can't do promisc and hostap at the same time. If all that's 1202 * changing is the promisc flag, try to short-circuit a call to 1203 * wi_init() by just setting PROMISC in the hardware. 1204 */ 1205 if (ifp->if_flags & IFF_UP) { 1206 if (ic->ic_opmode != IEEE80211_M_HOSTAP && 1207 ifp->if_drv_flags & IFF_DRV_RUNNING) { 1208 if ((ifp->if_flags ^ sc->sc_if_flags) & IFF_PROMISC) { 1209 wi_write_val(sc, WI_RID_PROMISC, 1210 (ifp->if_flags & IFF_PROMISC) != 0); 1211 } else { 1212 wi_init_locked(sc); 1213 startall = 1; 1214 } 1215 } else { 1216 wi_init_locked(sc); 1217 startall = 1; 1218 } 1219 } else { 1220 if (ifp->if_drv_flags & IFF_DRV_RUNNING) 1221 wi_stop_locked(sc, 1); 1222 sc->wi_gone = 0; 1223 } 1224 sc->sc_if_flags = ifp->if_flags; 1225 WI_UNLOCK(sc); 1226 if (startall) 1227 ieee80211_start_all(ic); 1228 break; 1229 case SIOCGIFMEDIA: 1230 error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd); 1231 break; 1232 case SIOCGIFADDR: 1233 error = ether_ioctl(ifp, cmd, data); 1234 break; 1235 default: 1236 error = EINVAL; 1237 break; 1238 } 1239 return error; 1240} 1241 1242static void 1243wi_media_status(struct ifnet *ifp, struct ifmediareq *imr) 1244{ 1245 struct ieee80211vap *vap = ifp->if_softc; 1246 struct ieee80211com *ic = vap->iv_ic; 1247 struct wi_softc *sc = ic->ic_ifp->if_softc; 1248 u_int16_t val; 1249 int rate, len; 1250 1251 len = sizeof(val); 1252 if (sc->sc_enabled && 1253 wi_read_rid(sc, WI_RID_CUR_TX_RATE, &val, &len) == 0 && 1254 len == sizeof(val)) { 1255 /* convert to 802.11 rate */ 1256 val = le16toh(val); 1257 rate = val * 2; 1258 if (sc->sc_firmware_type == WI_LUCENT) { 1259 if (rate == 10) 1260 rate = 11; /* 5.5Mbps */ 1261 } else { 1262 if (rate == 4*2) 1263 rate = 11; /* 5.5Mbps */ 1264 else if (rate == 8*2) 1265 rate = 22; /* 11Mbps */ 1266 } 1267 vap->iv_bss->ni_txrate = rate; 1268 } 1269 ieee80211_media_status(ifp, imr); 1270} 1271 1272static void 1273wi_sync_bssid(struct wi_softc *sc, u_int8_t new_bssid[IEEE80211_ADDR_LEN]) 1274{ 1275 struct ifnet *ifp = sc->sc_ifp; 1276 struct ieee80211com *ic = ifp->if_l2com; 1277 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps); 1278 struct ieee80211_node *ni = vap->iv_bss; 1279 1280 if (IEEE80211_ADDR_EQ(new_bssid, ni->ni_bssid)) 1281 return; 1282 1283 DPRINTF(("wi_sync_bssid: bssid %s -> ", ether_sprintf(ni->ni_bssid))); 1284 DPRINTF(("%s ?\n", ether_sprintf(new_bssid))); 1285 1286 /* In promiscuous mode, the BSSID field is not a reliable 1287 * indicator of the firmware's BSSID. Damp spurious 1288 * change-of-BSSID indications. 1289 */ 1290 if ((ifp->if_flags & IFF_PROMISC) != 0 && 1291 !ppsratecheck(&sc->sc_last_syn, &sc->sc_false_syns, 1292 WI_MAX_FALSE_SYNS)) 1293 return; 1294 1295 sc->sc_false_syns = MAX(0, sc->sc_false_syns - 1); 1296#if 0 1297 /* 1298 * XXX hack; we should create a new node with the new bssid 1299 * and replace the existing ic_bss with it but since we don't 1300 * process management frames to collect state we cheat by 1301 * reusing the existing node as we know wi_newstate will be 1302 * called and it will overwrite the node state. 1303 */ 1304 ieee80211_sta_join(ic, ieee80211_ref_node(ni)); 1305#endif 1306} 1307 1308static __noinline void 1309wi_rx_intr(struct wi_softc *sc) 1310{ 1311 struct ifnet *ifp = sc->sc_ifp; 1312 struct ieee80211com *ic = ifp->if_l2com; 1313 struct wi_frame frmhdr; 1314 struct mbuf *m; 1315 struct ieee80211_frame *wh; 1316 struct ieee80211_node *ni; 1317 int fid, len, off; 1318 u_int8_t dir; 1319 u_int16_t status; 1320 int8_t rssi, nf; 1321 1322 fid = CSR_READ_2(sc, WI_RX_FID); 1323 1324 /* First read in the frame header */ 1325 if (wi_read_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr))) { 1326 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX); 1327 ifp->if_ierrors++; 1328 DPRINTF(("wi_rx_intr: read fid %x failed\n", fid)); 1329 return; 1330 } 1331 1332 /* 1333 * Drop undecryptable or packets with receive errors here 1334 */ 1335 status = le16toh(frmhdr.wi_status); 1336 if (status & WI_STAT_ERRSTAT) { 1337 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX); 1338 ifp->if_ierrors++; 1339 DPRINTF(("wi_rx_intr: fid %x error status %x\n", fid, status)); 1340 return; 1341 } 1342 1343 len = le16toh(frmhdr.wi_dat_len); 1344 off = ALIGN(sizeof(struct ieee80211_frame)); 1345 1346 /* 1347 * Sometimes the PRISM2.x returns bogusly large frames. Except 1348 * in monitor mode, just throw them away. 1349 */ 1350 if (off + len > MCLBYTES) { 1351 if (ic->ic_opmode != IEEE80211_M_MONITOR) { 1352 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX); 1353 ifp->if_ierrors++; 1354 DPRINTF(("wi_rx_intr: oversized packet\n")); 1355 return; 1356 } else 1357 len = 0; 1358 } 1359 1360 if (off + len > MHLEN) 1361 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR); 1362 else 1363 m = m_gethdr(M_NOWAIT, MT_DATA); 1364 if (m == NULL) { 1365 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX); 1366 ifp->if_ierrors++; 1367 DPRINTF(("wi_rx_intr: MGET failed\n")); 1368 return; 1369 } 1370 m->m_data += off - sizeof(struct ieee80211_frame); 1371 memcpy(m->m_data, &frmhdr.wi_whdr, sizeof(struct ieee80211_frame)); 1372 wi_read_bap(sc, fid, sizeof(frmhdr), 1373 m->m_data + sizeof(struct ieee80211_frame), len); 1374 m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame) + len; 1375 m->m_pkthdr.rcvif = ifp; 1376 1377 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX); 1378 1379 rssi = frmhdr.wi_rx_signal; 1380 nf = frmhdr.wi_rx_silence; 1381 if (ieee80211_radiotap_active(ic)) { 1382 struct wi_rx_radiotap_header *tap = &sc->sc_rx_th; 1383 uint32_t rstamp; 1384 1385 rstamp = (le16toh(frmhdr.wi_rx_tstamp0) << 16) | 1386 le16toh(frmhdr.wi_rx_tstamp1); 1387 tap->wr_tsf = htole64((uint64_t)rstamp); 1388 /* XXX replace divide by table */ 1389 tap->wr_rate = frmhdr.wi_rx_rate / 5; 1390 tap->wr_flags = 0; 1391 if (frmhdr.wi_status & WI_STAT_PCF) 1392 tap->wr_flags |= IEEE80211_RADIOTAP_F_CFP; 1393 if (m->m_flags & M_WEP) 1394 tap->wr_flags |= IEEE80211_RADIOTAP_F_WEP; 1395 tap->wr_antsignal = rssi; 1396 tap->wr_antnoise = nf; 1397 } 1398 1399 /* synchronize driver's BSSID with firmware's BSSID */ 1400 wh = mtod(m, struct ieee80211_frame *); 1401 dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK; 1402 if (ic->ic_opmode == IEEE80211_M_IBSS && dir == IEEE80211_FC1_DIR_NODS) 1403 wi_sync_bssid(sc, wh->i_addr3); 1404 1405 WI_UNLOCK(sc); 1406 1407 ni = ieee80211_find_rxnode(ic, mtod(m, struct ieee80211_frame_min *)); 1408 if (ni != NULL) { 1409 (void) ieee80211_input(ni, m, rssi, nf); 1410 ieee80211_free_node(ni); 1411 } else 1412 (void) ieee80211_input_all(ic, m, rssi, nf); 1413 1414 WI_LOCK(sc); 1415} 1416 1417static __noinline void 1418wi_tx_ex_intr(struct wi_softc *sc) 1419{ 1420 struct ifnet *ifp = sc->sc_ifp; 1421 struct wi_frame frmhdr; 1422 int fid; 1423 1424 fid = CSR_READ_2(sc, WI_TX_CMP_FID); 1425 /* Read in the frame header */ 1426 if (wi_read_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr)) == 0) { 1427 u_int16_t status = le16toh(frmhdr.wi_status); 1428 /* 1429 * Spontaneous station disconnects appear as xmit 1430 * errors. Don't announce them and/or count them 1431 * as an output error. 1432 */ 1433 if ((status & WI_TXSTAT_DISCONNECT) == 0) { 1434 if (ppsratecheck(&lasttxerror, &curtxeps, wi_txerate)) { 1435 if_printf(ifp, "tx failed"); 1436 if (status & WI_TXSTAT_RET_ERR) 1437 printf(", retry limit exceeded"); 1438 if (status & WI_TXSTAT_AGED_ERR) 1439 printf(", max transmit lifetime exceeded"); 1440 if (status & WI_TXSTAT_DISCONNECT) 1441 printf(", port disconnected"); 1442 if (status & WI_TXSTAT_FORM_ERR) 1443 printf(", invalid format (data len %u src %6D)", 1444 le16toh(frmhdr.wi_dat_len), 1445 frmhdr.wi_ehdr.ether_shost, ":"); 1446 if (status & ~0xf) 1447 printf(", status=0x%x", status); 1448 printf("\n"); 1449 } 1450 ifp->if_oerrors++; 1451 } else { 1452 DPRINTF(("port disconnected\n")); 1453 ifp->if_collisions++; /* XXX */ 1454 } 1455 } else 1456 DPRINTF(("wi_tx_ex_intr: read fid %x failed\n", fid)); 1457 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_TX_EXC); 1458} 1459 1460static __noinline void 1461wi_tx_intr(struct wi_softc *sc) 1462{ 1463 struct ifnet *ifp = sc->sc_ifp; 1464 int fid, cur; 1465 1466 if (sc->wi_gone) 1467 return; 1468 1469 fid = CSR_READ_2(sc, WI_ALLOC_FID); 1470 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_ALLOC); 1471 1472 cur = sc->sc_txcur; 1473 if (sc->sc_txd[cur].d_fid != fid) { 1474 if_printf(ifp, "bad alloc %x != %x, cur %d nxt %d\n", 1475 fid, sc->sc_txd[cur].d_fid, cur, sc->sc_txnext); 1476 return; 1477 } 1478 sc->sc_tx_timer = 0; 1479 sc->sc_txd[cur].d_len = 0; 1480 sc->sc_txcur = cur = (cur + 1) % sc->sc_ntxbuf; 1481 if (sc->sc_txd[cur].d_len == 0) 1482 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 1483 else { 1484 if (wi_cmd(sc, WI_CMD_TX | WI_RECLAIM, sc->sc_txd[cur].d_fid, 1485 0, 0)) { 1486 if_printf(ifp, "xmit failed\n"); 1487 sc->sc_txd[cur].d_len = 0; 1488 } else { 1489 sc->sc_tx_timer = 5; 1490 } 1491 } 1492} 1493 1494static __noinline void 1495wi_info_intr(struct wi_softc *sc) 1496{ 1497 struct ifnet *ifp = sc->sc_ifp; 1498 struct ieee80211com *ic = ifp->if_l2com; 1499 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps); 1500 int i, fid, len, off; 1501 u_int16_t ltbuf[2]; 1502 u_int16_t stat; 1503 u_int32_t *ptr; 1504 1505 fid = CSR_READ_2(sc, WI_INFO_FID); 1506 wi_read_bap(sc, fid, 0, ltbuf, sizeof(ltbuf)); 1507 1508 switch (le16toh(ltbuf[1])) { 1509 case WI_INFO_LINK_STAT: 1510 wi_read_bap(sc, fid, sizeof(ltbuf), &stat, sizeof(stat)); 1511 DPRINTF(("wi_info_intr: LINK_STAT 0x%x\n", le16toh(stat))); 1512 switch (le16toh(stat)) { 1513 case WI_INFO_LINK_STAT_CONNECTED: 1514 if (vap->iv_state == IEEE80211_S_RUN && 1515 vap->iv_opmode != IEEE80211_M_IBSS) 1516 break; 1517 /* fall thru... */ 1518 case WI_INFO_LINK_STAT_AP_CHG: 1519 IEEE80211_LOCK(ic); 1520 vap->iv_bss->ni_associd = 1 | 0xc000; /* NB: anything will do */ 1521 ieee80211_new_state(vap, IEEE80211_S_RUN, 0); 1522 IEEE80211_UNLOCK(ic); 1523 break; 1524 case WI_INFO_LINK_STAT_AP_INR: 1525 break; 1526 case WI_INFO_LINK_STAT_DISCONNECTED: 1527 /* we dropped off the net; e.g. due to deauth/disassoc */ 1528 IEEE80211_LOCK(ic); 1529 vap->iv_bss->ni_associd = 0; 1530 vap->iv_stats.is_rx_deauth++; 1531 ieee80211_new_state(vap, IEEE80211_S_SCAN, 0); 1532 IEEE80211_UNLOCK(ic); 1533 break; 1534 case WI_INFO_LINK_STAT_AP_OOR: 1535 /* XXX does this need to be per-vap? */ 1536 ieee80211_beacon_miss(ic); 1537 break; 1538 case WI_INFO_LINK_STAT_ASSOC_FAILED: 1539 if (vap->iv_opmode == IEEE80211_M_STA) 1540 ieee80211_new_state(vap, IEEE80211_S_SCAN, 1541 IEEE80211_SCAN_FAIL_TIMEOUT); 1542 break; 1543 } 1544 break; 1545 case WI_INFO_COUNTERS: 1546 /* some card versions have a larger stats structure */ 1547 len = min(le16toh(ltbuf[0]) - 1, sizeof(sc->sc_stats) / 4); 1548 ptr = (u_int32_t *)&sc->sc_stats; 1549 off = sizeof(ltbuf); 1550 for (i = 0; i < len; i++, off += 2, ptr++) { 1551 wi_read_bap(sc, fid, off, &stat, sizeof(stat)); 1552#ifdef WI_HERMES_STATS_WAR 1553 if (stat & 0xf000) 1554 stat = ~stat; 1555#endif 1556 *ptr += stat; 1557 } 1558 ifp->if_collisions = sc->sc_stats.wi_tx_single_retries + 1559 sc->sc_stats.wi_tx_multi_retries + 1560 sc->sc_stats.wi_tx_retry_limit; 1561 break; 1562 default: 1563 DPRINTF(("wi_info_intr: got fid %x type %x len %d\n", fid, 1564 le16toh(ltbuf[1]), le16toh(ltbuf[0]))); 1565 break; 1566 } 1567 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_INFO); 1568} 1569 1570static int 1571wi_write_multi(struct wi_softc *sc) 1572{ 1573 struct ifnet *ifp = sc->sc_ifp; 1574 int n; 1575 struct ifmultiaddr *ifma; 1576 struct wi_mcast mlist; 1577 1578 if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) { 1579allmulti: 1580 memset(&mlist, 0, sizeof(mlist)); 1581 return wi_write_rid(sc, WI_RID_MCAST_LIST, &mlist, 1582 sizeof(mlist)); 1583 } 1584 1585 n = 0; 1586 if_maddr_rlock(ifp); 1587 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 1588 if (ifma->ifma_addr->sa_family != AF_LINK) 1589 continue; 1590 if (n >= 16) 1591 goto allmulti; 1592 IEEE80211_ADDR_COPY(&mlist.wi_mcast[n], 1593 (LLADDR((struct sockaddr_dl *)ifma->ifma_addr))); 1594 n++; 1595 } 1596 if_maddr_runlock(ifp); 1597 return wi_write_rid(sc, WI_RID_MCAST_LIST, &mlist, 1598 IEEE80211_ADDR_LEN * n); 1599} 1600 1601static void 1602wi_update_mcast(struct ifnet *ifp) 1603{ 1604 wi_write_multi(ifp->if_softc); 1605} 1606 1607static void 1608wi_update_promisc(struct ifnet *ifp) 1609{ 1610 struct wi_softc *sc = ifp->if_softc; 1611 struct ieee80211com *ic = ifp->if_l2com; 1612 1613 WI_LOCK(sc); 1614 /* XXX handle WEP special case handling? */ 1615 wi_write_val(sc, WI_RID_PROMISC, 1616 (ic->ic_opmode == IEEE80211_M_MONITOR || 1617 (ifp->if_flags & IFF_PROMISC))); 1618 WI_UNLOCK(sc); 1619} 1620 1621static void 1622wi_read_nicid(struct wi_softc *sc) 1623{ 1624 struct wi_card_ident *id; 1625 char *p; 1626 int len; 1627 u_int16_t ver[4]; 1628 1629 /* getting chip identity */ 1630 memset(ver, 0, sizeof(ver)); 1631 len = sizeof(ver); 1632 wi_read_rid(sc, WI_RID_CARD_ID, ver, &len); 1633 1634 sc->sc_firmware_type = WI_NOTYPE; 1635 sc->sc_nic_id = le16toh(ver[0]); 1636 for (id = wi_card_ident; id->card_name != NULL; id++) { 1637 if (sc->sc_nic_id == id->card_id) { 1638 sc->sc_nic_name = id->card_name; 1639 sc->sc_firmware_type = id->firm_type; 1640 break; 1641 } 1642 } 1643 if (sc->sc_firmware_type == WI_NOTYPE) { 1644 if (sc->sc_nic_id & 0x8000) { 1645 sc->sc_firmware_type = WI_INTERSIL; 1646 sc->sc_nic_name = "Unknown Prism chip"; 1647 } else { 1648 sc->sc_firmware_type = WI_LUCENT; 1649 sc->sc_nic_name = "Unknown Lucent chip"; 1650 } 1651 } 1652 if (bootverbose) 1653 device_printf(sc->sc_dev, "using %s\n", sc->sc_nic_name); 1654 1655 /* get primary firmware version (Only Prism chips) */ 1656 if (sc->sc_firmware_type != WI_LUCENT) { 1657 memset(ver, 0, sizeof(ver)); 1658 len = sizeof(ver); 1659 wi_read_rid(sc, WI_RID_PRI_IDENTITY, ver, &len); 1660 sc->sc_pri_firmware_ver = le16toh(ver[2]) * 10000 + 1661 le16toh(ver[3]) * 100 + le16toh(ver[1]); 1662 } 1663 1664 /* get station firmware version */ 1665 memset(ver, 0, sizeof(ver)); 1666 len = sizeof(ver); 1667 wi_read_rid(sc, WI_RID_STA_IDENTITY, ver, &len); 1668 sc->sc_sta_firmware_ver = le16toh(ver[2]) * 10000 + 1669 le16toh(ver[3]) * 100 + le16toh(ver[1]); 1670 if (sc->sc_firmware_type == WI_INTERSIL && 1671 (sc->sc_sta_firmware_ver == 10102 || 1672 sc->sc_sta_firmware_ver == 20102)) { 1673 char ident[12]; 1674 memset(ident, 0, sizeof(ident)); 1675 len = sizeof(ident); 1676 /* value should be the format like "V2.00-11" */ 1677 if (wi_read_rid(sc, WI_RID_SYMBOL_IDENTITY, ident, &len) == 0 && 1678 *(p = (char *)ident) >= 'A' && 1679 p[2] == '.' && p[5] == '-' && p[8] == '\0') { 1680 sc->sc_firmware_type = WI_SYMBOL; 1681 sc->sc_sta_firmware_ver = (p[1] - '0') * 10000 + 1682 (p[3] - '0') * 1000 + (p[4] - '0') * 100 + 1683 (p[6] - '0') * 10 + (p[7] - '0'); 1684 } 1685 } 1686 if (bootverbose) { 1687 device_printf(sc->sc_dev, "%s Firmware: ", 1688 wi_firmware_names[sc->sc_firmware_type]); 1689 if (sc->sc_firmware_type != WI_LUCENT) /* XXX */ 1690 printf("Primary (%u.%u.%u), ", 1691 sc->sc_pri_firmware_ver / 10000, 1692 (sc->sc_pri_firmware_ver % 10000) / 100, 1693 sc->sc_pri_firmware_ver % 100); 1694 printf("Station (%u.%u.%u)\n", 1695 sc->sc_sta_firmware_ver / 10000, 1696 (sc->sc_sta_firmware_ver % 10000) / 100, 1697 sc->sc_sta_firmware_ver % 100); 1698 } 1699} 1700 1701static int 1702wi_write_ssid(struct wi_softc *sc, int rid, u_int8_t *buf, int buflen) 1703{ 1704 struct wi_ssid ssid; 1705 1706 if (buflen > IEEE80211_NWID_LEN) 1707 return ENOBUFS; 1708 memset(&ssid, 0, sizeof(ssid)); 1709 ssid.wi_len = htole16(buflen); 1710 memcpy(ssid.wi_ssid, buf, buflen); 1711 return wi_write_rid(sc, rid, &ssid, sizeof(ssid)); 1712} 1713 1714static int 1715wi_write_txrate(struct wi_softc *sc, struct ieee80211vap *vap) 1716{ 1717 static const uint16_t lucent_rates[12] = { 1718 [ 0] = 3, /* auto */ 1719 [ 1] = 1, /* 1Mb/s */ 1720 [ 2] = 2, /* 2Mb/s */ 1721 [ 5] = 4, /* 5.5Mb/s */ 1722 [11] = 5 /* 11Mb/s */ 1723 }; 1724 static const uint16_t intersil_rates[12] = { 1725 [ 0] = 0xf, /* auto */ 1726 [ 1] = 0, /* 1Mb/s */ 1727 [ 2] = 1, /* 2Mb/s */ 1728 [ 5] = 2, /* 5.5Mb/s */ 1729 [11] = 3, /* 11Mb/s */ 1730 }; 1731 const uint16_t *rates = sc->sc_firmware_type == WI_LUCENT ? 1732 lucent_rates : intersil_rates; 1733 struct ieee80211com *ic = vap->iv_ic; 1734 const struct ieee80211_txparam *tp; 1735 1736 tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_bsschan)]; 1737 return wi_write_val(sc, WI_RID_TX_RATE, 1738 (tp->ucastrate == IEEE80211_FIXED_RATE_NONE ? 1739 rates[0] : rates[tp->ucastrate / 2])); 1740} 1741 1742static int 1743wi_write_wep(struct wi_softc *sc, struct ieee80211vap *vap) 1744{ 1745 int error = 0; 1746 int i, keylen; 1747 u_int16_t val; 1748 struct wi_key wkey[IEEE80211_WEP_NKID]; 1749 1750 switch (sc->sc_firmware_type) { 1751 case WI_LUCENT: 1752 val = (vap->iv_flags & IEEE80211_F_PRIVACY) ? 1 : 0; 1753 error = wi_write_val(sc, WI_RID_ENCRYPTION, val); 1754 if (error) 1755 break; 1756 if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) 1757 break; 1758 error = wi_write_val(sc, WI_RID_TX_CRYPT_KEY, vap->iv_def_txkey); 1759 if (error) 1760 break; 1761 memset(wkey, 0, sizeof(wkey)); 1762 for (i = 0; i < IEEE80211_WEP_NKID; i++) { 1763 keylen = vap->iv_nw_keys[i].wk_keylen; 1764 wkey[i].wi_keylen = htole16(keylen); 1765 memcpy(wkey[i].wi_keydat, vap->iv_nw_keys[i].wk_key, 1766 keylen); 1767 } 1768 error = wi_write_rid(sc, WI_RID_DEFLT_CRYPT_KEYS, 1769 wkey, sizeof(wkey)); 1770 sc->sc_encryption = 0; 1771 break; 1772 1773 case WI_INTERSIL: 1774 val = HOST_ENCRYPT | HOST_DECRYPT; 1775 if (vap->iv_flags & IEEE80211_F_PRIVACY) { 1776 /* 1777 * ONLY HWB3163 EVAL-CARD Firmware version 1778 * less than 0.8 variant2 1779 * 1780 * If promiscuous mode disable, Prism2 chip 1781 * does not work with WEP . 1782 * It is under investigation for details. 1783 * (ichiro@netbsd.org) 1784 */ 1785 if (sc->sc_sta_firmware_ver < 802 ) { 1786 /* firm ver < 0.8 variant 2 */ 1787 wi_write_val(sc, WI_RID_PROMISC, 1); 1788 } 1789 wi_write_val(sc, WI_RID_CNFAUTHMODE, 1790 vap->iv_bss->ni_authmode); 1791 val |= PRIVACY_INVOKED; 1792 } else { 1793 wi_write_val(sc, WI_RID_CNFAUTHMODE, IEEE80211_AUTH_OPEN); 1794 } 1795 error = wi_write_val(sc, WI_RID_P2_ENCRYPTION, val); 1796 if (error) 1797 break; 1798 sc->sc_encryption = val; 1799 if ((val & PRIVACY_INVOKED) == 0) 1800 break; 1801 error = wi_write_val(sc, WI_RID_P2_TX_CRYPT_KEY, vap->iv_def_txkey); 1802 break; 1803 } 1804 return error; 1805} 1806 1807static int 1808wi_cmd(struct wi_softc *sc, int cmd, int val0, int val1, int val2) 1809{ 1810 int i, s = 0; 1811 1812 if (sc->wi_gone) 1813 return (ENODEV); 1814 1815 /* wait for the busy bit to clear */ 1816 for (i = sc->wi_cmd_count; i > 0; i--) { /* 500ms */ 1817 if (!(CSR_READ_2(sc, WI_COMMAND) & WI_CMD_BUSY)) 1818 break; 1819 DELAY(1*1000); /* 1ms */ 1820 } 1821 if (i == 0) { 1822 device_printf(sc->sc_dev, "%s: busy bit won't clear, cmd 0x%x\n", 1823 __func__, cmd); 1824 sc->wi_gone = 1; 1825 return(ETIMEDOUT); 1826 } 1827 1828 CSR_WRITE_2(sc, WI_PARAM0, val0); 1829 CSR_WRITE_2(sc, WI_PARAM1, val1); 1830 CSR_WRITE_2(sc, WI_PARAM2, val2); 1831 CSR_WRITE_2(sc, WI_COMMAND, cmd); 1832 1833 if (cmd == WI_CMD_INI) { 1834 /* XXX: should sleep here. */ 1835 DELAY(100*1000); /* 100ms delay for init */ 1836 } 1837 for (i = 0; i < WI_TIMEOUT; i++) { 1838 /* 1839 * Wait for 'command complete' bit to be 1840 * set in the event status register. 1841 */ 1842 s = CSR_READ_2(sc, WI_EVENT_STAT); 1843 if (s & WI_EV_CMD) { 1844 /* Ack the event and read result code. */ 1845 s = CSR_READ_2(sc, WI_STATUS); 1846 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_CMD); 1847 if (s & WI_STAT_CMD_RESULT) { 1848 return(EIO); 1849 } 1850 break; 1851 } 1852 DELAY(WI_DELAY); 1853 } 1854 1855 if (i == WI_TIMEOUT) { 1856 device_printf(sc->sc_dev, "%s: timeout on cmd 0x%04x; " 1857 "event status 0x%04x\n", __func__, cmd, s); 1858 if (s == 0xffff) 1859 sc->wi_gone = 1; 1860 return(ETIMEDOUT); 1861 } 1862 return (0); 1863} 1864 1865static int 1866wi_seek_bap(struct wi_softc *sc, int id, int off) 1867{ 1868 int i, status; 1869 1870 CSR_WRITE_2(sc, WI_SEL0, id); 1871 CSR_WRITE_2(sc, WI_OFF0, off); 1872 1873 for (i = 0; ; i++) { 1874 status = CSR_READ_2(sc, WI_OFF0); 1875 if ((status & WI_OFF_BUSY) == 0) 1876 break; 1877 if (i == WI_TIMEOUT) { 1878 device_printf(sc->sc_dev, "%s: timeout, id %x off %x\n", 1879 __func__, id, off); 1880 sc->sc_bap_off = WI_OFF_ERR; /* invalidate */ 1881 if (status == 0xffff) 1882 sc->wi_gone = 1; 1883 return ETIMEDOUT; 1884 } 1885 DELAY(1); 1886 } 1887 if (status & WI_OFF_ERR) { 1888 device_printf(sc->sc_dev, "%s: error, id %x off %x\n", 1889 __func__, id, off); 1890 sc->sc_bap_off = WI_OFF_ERR; /* invalidate */ 1891 return EIO; 1892 } 1893 sc->sc_bap_id = id; 1894 sc->sc_bap_off = off; 1895 return 0; 1896} 1897 1898static int 1899wi_read_bap(struct wi_softc *sc, int id, int off, void *buf, int buflen) 1900{ 1901 u_int16_t *ptr; 1902 int i, error, cnt; 1903 1904 if (buflen == 0) 1905 return 0; 1906 if (id != sc->sc_bap_id || off != sc->sc_bap_off) { 1907 if ((error = wi_seek_bap(sc, id, off)) != 0) 1908 return error; 1909 } 1910 cnt = (buflen + 1) / 2; 1911 ptr = (u_int16_t *)buf; 1912 for (i = 0; i < cnt; i++) 1913 *ptr++ = CSR_READ_2(sc, WI_DATA0); 1914 sc->sc_bap_off += cnt * 2; 1915 return 0; 1916} 1917 1918static int 1919wi_write_bap(struct wi_softc *sc, int id, int off, void *buf, int buflen) 1920{ 1921 u_int16_t *ptr; 1922 int i, error, cnt; 1923 1924 if (buflen == 0) 1925 return 0; 1926 1927 if (id != sc->sc_bap_id || off != sc->sc_bap_off) { 1928 if ((error = wi_seek_bap(sc, id, off)) != 0) 1929 return error; 1930 } 1931 cnt = (buflen + 1) / 2; 1932 ptr = (u_int16_t *)buf; 1933 for (i = 0; i < cnt; i++) 1934 CSR_WRITE_2(sc, WI_DATA0, ptr[i]); 1935 sc->sc_bap_off += cnt * 2; 1936 1937 return 0; 1938} 1939 1940static int 1941wi_mwrite_bap(struct wi_softc *sc, int id, int off, struct mbuf *m0, int totlen) 1942{ 1943 int error, len; 1944 struct mbuf *m; 1945 1946 for (m = m0; m != NULL && totlen > 0; m = m->m_next) { 1947 if (m->m_len == 0) 1948 continue; 1949 1950 len = min(m->m_len, totlen); 1951 1952 if (((u_long)m->m_data) % 2 != 0 || len % 2 != 0) { 1953 m_copydata(m, 0, totlen, (caddr_t)&sc->sc_txbuf); 1954 return wi_write_bap(sc, id, off, (caddr_t)&sc->sc_txbuf, 1955 totlen); 1956 } 1957 1958 if ((error = wi_write_bap(sc, id, off, m->m_data, len)) != 0) 1959 return error; 1960 1961 off += m->m_len; 1962 totlen -= len; 1963 } 1964 return 0; 1965} 1966 1967static int 1968wi_alloc_fid(struct wi_softc *sc, int len, int *idp) 1969{ 1970 int i; 1971 1972 if (wi_cmd(sc, WI_CMD_ALLOC_MEM, len, 0, 0)) { 1973 device_printf(sc->sc_dev, "%s: failed to allocate %d bytes on NIC\n", 1974 __func__, len); 1975 return ENOMEM; 1976 } 1977 1978 for (i = 0; i < WI_TIMEOUT; i++) { 1979 if (CSR_READ_2(sc, WI_EVENT_STAT) & WI_EV_ALLOC) 1980 break; 1981 DELAY(1); 1982 } 1983 if (i == WI_TIMEOUT) { 1984 device_printf(sc->sc_dev, "%s: timeout in alloc\n", __func__); 1985 return ETIMEDOUT; 1986 } 1987 *idp = CSR_READ_2(sc, WI_ALLOC_FID); 1988 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_ALLOC); 1989 return 0; 1990} 1991 1992static int 1993wi_read_rid(struct wi_softc *sc, int rid, void *buf, int *buflenp) 1994{ 1995 int error, len; 1996 u_int16_t ltbuf[2]; 1997 1998 /* Tell the NIC to enter record read mode. */ 1999 error = wi_cmd(sc, WI_CMD_ACCESS | WI_ACCESS_READ, rid, 0, 0); 2000 if (error) 2001 return error; 2002 2003 error = wi_read_bap(sc, rid, 0, ltbuf, sizeof(ltbuf)); 2004 if (error) 2005 return error; 2006 2007 if (le16toh(ltbuf[1]) != rid) { 2008 device_printf(sc->sc_dev, "record read mismatch, rid=%x, got=%x\n", 2009 rid, le16toh(ltbuf[1])); 2010 return EIO; 2011 } 2012 len = (le16toh(ltbuf[0]) - 1) * 2; /* already got rid */ 2013 if (*buflenp < len) { 2014 device_printf(sc->sc_dev, "record buffer is too small, " 2015 "rid=%x, size=%d, len=%d\n", 2016 rid, *buflenp, len); 2017 return ENOSPC; 2018 } 2019 *buflenp = len; 2020 return wi_read_bap(sc, rid, sizeof(ltbuf), buf, len); 2021} 2022 2023static int 2024wi_write_rid(struct wi_softc *sc, int rid, void *buf, int buflen) 2025{ 2026 int error; 2027 u_int16_t ltbuf[2]; 2028 2029 ltbuf[0] = htole16((buflen + 1) / 2 + 1); /* includes rid */ 2030 ltbuf[1] = htole16(rid); 2031 2032 error = wi_write_bap(sc, rid, 0, ltbuf, sizeof(ltbuf)); 2033 if (error) { 2034 device_printf(sc->sc_dev, "%s: bap0 write failure, rid 0x%x\n", 2035 __func__, rid); 2036 return error; 2037 } 2038 error = wi_write_bap(sc, rid, sizeof(ltbuf), buf, buflen); 2039 if (error) { 2040 device_printf(sc->sc_dev, "%s: bap1 write failure, rid 0x%x\n", 2041 __func__, rid); 2042 return error; 2043 } 2044 2045 return wi_cmd(sc, WI_CMD_ACCESS | WI_ACCESS_WRITE, rid, 0, 0); 2046} 2047 2048static int 2049wi_write_appie(struct wi_softc *sc, int rid, const struct ieee80211_appie *ie) 2050{ 2051 /* NB: 42 bytes is probably ok to have on the stack */ 2052 char buf[sizeof(uint16_t) + 40]; 2053 2054 if (ie->ie_len > 40) 2055 return EINVAL; 2056 /* NB: firmware requires 16-bit ie length before ie data */ 2057 *(uint16_t *) buf = htole16(ie->ie_len); 2058 memcpy(buf + sizeof(uint16_t), ie->ie_data, ie->ie_len); 2059 return wi_write_rid(sc, rid, buf, ie->ie_len + sizeof(uint16_t)); 2060} 2061 2062int 2063wi_alloc(device_t dev, int rid) 2064{ 2065 struct wi_softc *sc = device_get_softc(dev); 2066 2067 if (sc->wi_bus_type != WI_BUS_PCI_NATIVE) { 2068 sc->iobase_rid = rid; 2069 sc->iobase = bus_alloc_resource(dev, SYS_RES_IOPORT, 2070 &sc->iobase_rid, 0, ~0, (1 << 6), 2071 rman_make_alignment_flags(1 << 6) | RF_ACTIVE); 2072 if (sc->iobase == NULL) { 2073 device_printf(dev, "No I/O space?!\n"); 2074 return ENXIO; 2075 } 2076 2077 sc->wi_io_addr = rman_get_start(sc->iobase); 2078 sc->wi_btag = rman_get_bustag(sc->iobase); 2079 sc->wi_bhandle = rman_get_bushandle(sc->iobase); 2080 } else { 2081 sc->mem_rid = rid; 2082 sc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, 2083 &sc->mem_rid, RF_ACTIVE); 2084 if (sc->mem == NULL) { 2085 device_printf(dev, "No Mem space on prism2.5?\n"); 2086 return ENXIO; 2087 } 2088 2089 sc->wi_btag = rman_get_bustag(sc->mem); 2090 sc->wi_bhandle = rman_get_bushandle(sc->mem); 2091 } 2092 2093 sc->irq_rid = 0; 2094 sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irq_rid, 2095 RF_ACTIVE | 2096 ((sc->wi_bus_type == WI_BUS_PCCARD) ? 0 : RF_SHAREABLE)); 2097 if (sc->irq == NULL) { 2098 wi_free(dev); 2099 device_printf(dev, "No irq?!\n"); 2100 return ENXIO; 2101 } 2102 2103 sc->sc_dev = dev; 2104 sc->sc_unit = device_get_unit(dev); 2105 return 0; 2106} 2107 2108void 2109wi_free(device_t dev) 2110{ 2111 struct wi_softc *sc = device_get_softc(dev); 2112 2113 if (sc->iobase != NULL) { 2114 bus_release_resource(dev, SYS_RES_IOPORT, sc->iobase_rid, sc->iobase); 2115 sc->iobase = NULL; 2116 } 2117 if (sc->irq != NULL) { 2118 bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid, sc->irq); 2119 sc->irq = NULL; 2120 } 2121 if (sc->mem != NULL) { 2122 bus_release_resource(dev, SYS_RES_MEMORY, sc->mem_rid, sc->mem); 2123 sc->mem = NULL; 2124 } 2125} 2126