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