1 /* $NetBSD: wi.c,v 1.223 2007/12/09 20:28:00 jmcneill Exp $ */ 2 3 /*- 4 * Copyright (c) 2004 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Charles M. Hannum. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the NetBSD 21 * Foundation, Inc. and its contributors. 22 * 4. Neither the name of The NetBSD Foundation nor the names of its 23 * contributors may be used to endorse or promote products derived 24 * from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 * POSSIBILITY OF SUCH DAMAGE. 37 */ 38 39 /* 40 * Copyright (c) 1997, 1998, 1999 41 * Bill Paul <wpaul@ctr.columbia.edu>. All rights reserved. 42 * 43 * Redistribution and use in source and binary forms, with or without 44 * modification, are permitted provided that the following conditions 45 * are met: 46 * 1. Redistributions of source code must retain the above copyright 47 * notice, this list of conditions and the following disclaimer. 48 * 2. Redistributions in binary form must reproduce the above copyright 49 * notice, this list of conditions and the following disclaimer in the 50 * documentation and/or other materials provided with the distribution. 51 * 3. All advertising materials mentioning features or use of this software 52 * must display the following acknowledgement: 53 * This product includes software developed by Bill Paul. 54 * 4. Neither the name of the author nor the names of any co-contributors 55 * may be used to endorse or promote products derived from this software 56 * without specific prior written permission. 57 * 58 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND 59 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 60 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 61 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD 62 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 63 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 64 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 65 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 66 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 67 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 68 * THE POSSIBILITY OF SUCH DAMAGE. 69 */ 70 71 /* 72 * Lucent WaveLAN/IEEE 802.11 PCMCIA driver for NetBSD. 73 * 74 * Original FreeBSD driver written by Bill Paul <wpaul@ctr.columbia.edu> 75 * Electrical Engineering Department 76 * Columbia University, New York City 77 */ 78 79 /* 80 * The WaveLAN/IEEE adapter is the second generation of the WaveLAN 81 * from Lucent. Unlike the older cards, the new ones are programmed 82 * entirely via a firmware-driven controller called the Hermes. 83 * Unfortunately, Lucent will not release the Hermes programming manual 84 * without an NDA (if at all). What they do release is an API library 85 * called the HCF (Hardware Control Functions) which is supposed to 86 * do the device-specific operations of a device driver for you. The 87 * publically available version of the HCF library (the 'HCF Light') is 88 * a) extremely gross, b) lacks certain features, particularly support 89 * for 802.11 frames, and c) is contaminated by the GNU Public License. 90 * 91 * This driver does not use the HCF or HCF Light at all. Instead, it 92 * programs the Hermes controller directly, using information gleaned 93 * from the HCF Light code and corresponding documentation. 94 * 95 * This driver supports both the PCMCIA and ISA versions of the 96 * WaveLAN/IEEE cards. Note however that the ISA card isn't really 97 * anything of the sort: it's actually a PCMCIA bridge adapter 98 * that fits into an ISA slot, into which a PCMCIA WaveLAN card is 99 * inserted. Consequently, you need to use the pccard support for 100 * both the ISA and PCMCIA adapters. 101 */ 102 103 /* 104 * FreeBSD driver ported to NetBSD by Bill Sommerfeld in the back of the 105 * Oslo IETF plenary meeting. 106 */ 107 108 #include <sys/cdefs.h> 109 __KERNEL_RCSID(0, "$NetBSD: wi.c,v 1.223 2007/12/09 20:28:00 jmcneill Exp $"); 110 111 #define WI_HERMES_AUTOINC_WAR /* Work around data write autoinc bug. */ 112 #define WI_HERMES_STATS_WAR /* Work around stats counter bug. */ 113 #undef WI_HISTOGRAM 114 #undef WI_RING_DEBUG 115 #define STATIC static 116 117 #include "bpfilter.h" 118 119 #include <sys/param.h> 120 #include <sys/sysctl.h> 121 #include <sys/systm.h> 122 #include <sys/callout.h> 123 #include <sys/device.h> 124 #include <sys/socket.h> 125 #include <sys/mbuf.h> 126 #include <sys/ioctl.h> 127 #include <sys/kernel.h> /* for hz */ 128 #include <sys/proc.h> 129 #include <sys/kauth.h> 130 131 #include <net/if.h> 132 #include <net/if_dl.h> 133 #include <net/if_llc.h> 134 #include <net/if_media.h> 135 #include <net/if_ether.h> 136 #include <net/route.h> 137 138 #include <net80211/ieee80211_netbsd.h> 139 #include <net80211/ieee80211_var.h> 140 #include <net80211/ieee80211_ioctl.h> 141 #include <net80211/ieee80211_radiotap.h> 142 #include <net80211/ieee80211_rssadapt.h> 143 144 #if NBPFILTER > 0 145 #include <net/bpf.h> 146 #include <net/bpfdesc.h> 147 #endif 148 149 #include <sys/bus.h> 150 151 #include <dev/ic/wi_ieee.h> 152 #include <dev/ic/wireg.h> 153 #include <dev/ic/wivar.h> 154 155 STATIC int wi_init(struct ifnet *); 156 STATIC void wi_stop(struct ifnet *, int); 157 STATIC void wi_start(struct ifnet *); 158 STATIC int wi_reset(struct wi_softc *); 159 STATIC void wi_watchdog(struct ifnet *); 160 STATIC int wi_ioctl(struct ifnet *, u_long, void *); 161 STATIC int wi_media_change(struct ifnet *); 162 STATIC void wi_media_status(struct ifnet *, struct ifmediareq *); 163 164 STATIC struct ieee80211_node *wi_node_alloc(struct ieee80211_node_table *); 165 STATIC void wi_node_free(struct ieee80211_node *); 166 167 STATIC void wi_raise_rate(struct ieee80211com *, struct ieee80211_rssdesc *); 168 STATIC void wi_lower_rate(struct ieee80211com *, struct ieee80211_rssdesc *); 169 STATIC int wi_choose_rate(struct ieee80211com *, struct ieee80211_node *, 170 struct ieee80211_frame *, u_int); 171 STATIC void wi_rssadapt_updatestats_cb(void *, struct ieee80211_node *); 172 STATIC void wi_rssadapt_updatestats(void *); 173 STATIC void wi_rssdescs_init(struct wi_rssdesc (*)[], wi_rssdescq_t *); 174 STATIC void wi_rssdescs_reset(struct ieee80211com *, struct wi_rssdesc (*)[], 175 wi_rssdescq_t *, u_int8_t (*)[]); 176 STATIC void wi_sync_bssid(struct wi_softc *, u_int8_t new_bssid[]); 177 178 STATIC void wi_rx_intr(struct wi_softc *); 179 STATIC void wi_txalloc_intr(struct wi_softc *); 180 STATIC void wi_cmd_intr(struct wi_softc *); 181 STATIC void wi_tx_intr(struct wi_softc *); 182 STATIC void wi_tx_ex_intr(struct wi_softc *); 183 STATIC void wi_info_intr(struct wi_softc *); 184 185 STATIC int wi_key_delete(struct ieee80211com *, const struct ieee80211_key *); 186 STATIC int wi_key_set(struct ieee80211com *, const struct ieee80211_key *, 187 const u_int8_t[IEEE80211_ADDR_LEN]); 188 STATIC void wi_key_update_begin(struct ieee80211com *); 189 STATIC void wi_key_update_end(struct ieee80211com *); 190 191 STATIC void wi_push_packet(struct wi_softc *); 192 STATIC int wi_get_cfg(struct ifnet *, u_long, void *); 193 STATIC int wi_set_cfg(struct ifnet *, u_long, void *); 194 STATIC int wi_cfg_txrate(struct wi_softc *); 195 STATIC int wi_write_txrate(struct wi_softc *, int); 196 STATIC int wi_write_wep(struct wi_softc *); 197 STATIC int wi_write_multi(struct wi_softc *); 198 STATIC int wi_alloc_fid(struct wi_softc *, int, int *); 199 STATIC void wi_read_nicid(struct wi_softc *); 200 STATIC int wi_write_ssid(struct wi_softc *, int, u_int8_t *, int); 201 202 STATIC int wi_cmd(struct wi_softc *, int, int, int, int); 203 STATIC int wi_cmd_start(struct wi_softc *, int, int, int, int); 204 STATIC int wi_cmd_wait(struct wi_softc *, int, int); 205 STATIC int wi_seek_bap(struct wi_softc *, int, int); 206 STATIC int wi_read_bap(struct wi_softc *, int, int, void *, int); 207 STATIC int wi_write_bap(struct wi_softc *, int, int, void *, int); 208 STATIC int wi_mwrite_bap(struct wi_softc *, int, int, struct mbuf *, int); 209 STATIC int wi_read_rid(struct wi_softc *, int, void *, int *); 210 STATIC int wi_write_rid(struct wi_softc *, int, void *, int); 211 212 STATIC int wi_newstate(struct ieee80211com *, enum ieee80211_state, int); 213 STATIC void wi_set_tim(struct ieee80211_node *, int); 214 215 STATIC int wi_scan_ap(struct wi_softc *, u_int16_t, u_int16_t); 216 STATIC void wi_scan_result(struct wi_softc *, int, int); 217 218 STATIC void wi_dump_pkt(struct wi_frame *, struct ieee80211_node *, int rssi); 219 STATIC void wi_mend_flags(struct wi_softc *, enum ieee80211_state); 220 221 static inline int 222 wi_write_val(struct wi_softc *sc, int rid, u_int16_t val) 223 { 224 225 val = htole16(val); 226 return wi_write_rid(sc, rid, &val, sizeof(val)); 227 } 228 229 static struct timeval lasttxerror; /* time of last tx error msg */ 230 static int curtxeps = 0; /* current tx error msgs/sec */ 231 static int wi_txerate = 0; /* tx error rate: max msgs/sec */ 232 233 #ifdef WI_DEBUG 234 #define WI_DEBUG_MAX 2 235 int wi_debug = 0; 236 237 #define DPRINTF(X) if (wi_debug) printf X 238 #define DPRINTF2(X) if (wi_debug > 1) printf X 239 #define IFF_DUMPPKTS(_ifp) \ 240 (((_ifp)->if_flags & (IFF_DEBUG|IFF_LINK2)) == (IFF_DEBUG|IFF_LINK2)) 241 static int wi_sysctl_verify_debug(SYSCTLFN_PROTO); 242 #else 243 #define DPRINTF(X) 244 #define DPRINTF2(X) 245 #define IFF_DUMPPKTS(_ifp) 0 246 #endif 247 248 #define WI_INTRS (WI_EV_RX | WI_EV_ALLOC | WI_EV_INFO | \ 249 WI_EV_TX | WI_EV_TX_EXC | WI_EV_CMD) 250 251 struct wi_card_ident 252 wi_card_ident[] = { 253 /* CARD_ID CARD_NAME FIRM_TYPE */ 254 { WI_NIC_LUCENT_ID, WI_NIC_LUCENT_STR, WI_LUCENT }, 255 { WI_NIC_SONY_ID, WI_NIC_SONY_STR, WI_LUCENT }, 256 { WI_NIC_LUCENT_EMB_ID, WI_NIC_LUCENT_EMB_STR, WI_LUCENT }, 257 { WI_NIC_EVB2_ID, WI_NIC_EVB2_STR, WI_INTERSIL }, 258 { WI_NIC_HWB3763_ID, WI_NIC_HWB3763_STR, WI_INTERSIL }, 259 { WI_NIC_HWB3163_ID, WI_NIC_HWB3163_STR, WI_INTERSIL }, 260 { WI_NIC_HWB3163B_ID, WI_NIC_HWB3163B_STR, WI_INTERSIL }, 261 { WI_NIC_EVB3_ID, WI_NIC_EVB3_STR, WI_INTERSIL }, 262 { WI_NIC_HWB1153_ID, WI_NIC_HWB1153_STR, WI_INTERSIL }, 263 { WI_NIC_P2_SST_ID, WI_NIC_P2_SST_STR, WI_INTERSIL }, 264 { WI_NIC_EVB2_SST_ID, WI_NIC_EVB2_SST_STR, WI_INTERSIL }, 265 { WI_NIC_3842_EVA_ID, WI_NIC_3842_EVA_STR, WI_INTERSIL }, 266 { WI_NIC_3842_PCMCIA_AMD_ID, WI_NIC_3842_PCMCIA_STR, WI_INTERSIL }, 267 { WI_NIC_3842_PCMCIA_SST_ID, WI_NIC_3842_PCMCIA_STR, WI_INTERSIL }, 268 { WI_NIC_3842_PCMCIA_ATM_ID, WI_NIC_3842_PCMCIA_STR, WI_INTERSIL }, 269 { WI_NIC_3842_MINI_AMD_ID, WI_NIC_3842_MINI_STR, WI_INTERSIL }, 270 { WI_NIC_3842_MINI_SST_ID, WI_NIC_3842_MINI_STR, WI_INTERSIL }, 271 { WI_NIC_3842_MINI_ATM_ID, WI_NIC_3842_MINI_STR, WI_INTERSIL }, 272 { WI_NIC_3842_PCI_AMD_ID, WI_NIC_3842_PCI_STR, WI_INTERSIL }, 273 { WI_NIC_3842_PCI_SST_ID, WI_NIC_3842_PCI_STR, WI_INTERSIL }, 274 { WI_NIC_3842_PCI_ATM_ID, WI_NIC_3842_PCI_STR, WI_INTERSIL }, 275 { WI_NIC_P3_PCMCIA_AMD_ID, WI_NIC_P3_PCMCIA_STR, WI_INTERSIL }, 276 { WI_NIC_P3_PCMCIA_SST_ID, WI_NIC_P3_PCMCIA_STR, WI_INTERSIL }, 277 { WI_NIC_P3_MINI_AMD_ID, WI_NIC_P3_MINI_STR, WI_INTERSIL }, 278 { WI_NIC_P3_MINI_SST_ID, WI_NIC_P3_MINI_STR, WI_INTERSIL }, 279 { 0, NULL, 0 }, 280 }; 281 282 #ifndef _LKM 283 /* 284 * Setup sysctl(3) MIB, hw.wi.* 285 * 286 * TBD condition CTLFLAG_PERMANENT on being an LKM or not 287 */ 288 SYSCTL_SETUP(sysctl_wi, "sysctl wi(4) subtree setup") 289 { 290 int rc; 291 const struct sysctlnode *rnode; 292 #ifdef WI_DEBUG 293 const struct sysctlnode *cnode; 294 #endif /* WI_DEBUG */ 295 296 if ((rc = sysctl_createv(clog, 0, NULL, &rnode, 297 CTLFLAG_PERMANENT, CTLTYPE_NODE, "hw", NULL, 298 NULL, 0, NULL, 0, CTL_HW, CTL_EOL)) != 0) 299 goto err; 300 301 if ((rc = sysctl_createv(clog, 0, &rnode, &rnode, 302 CTLFLAG_PERMANENT, CTLTYPE_NODE, "wi", 303 "Lucent/Prism/Symbol 802.11 controls", 304 NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL)) != 0) 305 goto err; 306 307 #ifdef WI_DEBUG 308 /* control debugging printfs */ 309 if ((rc = sysctl_createv(clog, 0, &rnode, &cnode, 310 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT, 311 "debug", SYSCTL_DESCR("Enable debugging output"), 312 wi_sysctl_verify_debug, 0, &wi_debug, 0, CTL_CREATE, CTL_EOL)) != 0) 313 goto err; 314 #endif /* WI_DEBUG */ 315 return; 316 err: 317 printf("%s: sysctl_createv failed (rc = %d)\n", __func__, rc); 318 } 319 #endif 320 321 #ifdef WI_DEBUG 322 static int 323 wi_sysctl_verify(SYSCTLFN_ARGS, int lower, int upper) 324 { 325 int error, t; 326 struct sysctlnode node; 327 328 node = *rnode; 329 t = *(int*)rnode->sysctl_data; 330 node.sysctl_data = &t; 331 error = sysctl_lookup(SYSCTLFN_CALL(&node)); 332 if (error || newp == NULL) 333 return (error); 334 335 if (t < lower || t > upper) 336 return (EINVAL); 337 338 *(int*)rnode->sysctl_data = t; 339 340 return (0); 341 } 342 343 static int 344 wi_sysctl_verify_debug(SYSCTLFN_ARGS) 345 { 346 return wi_sysctl_verify(SYSCTLFN_CALL(__UNCONST(rnode)), 347 0, WI_DEBUG_MAX); 348 } 349 #endif /* WI_DEBUG */ 350 351 STATIC int 352 wi_read_xrid(struct wi_softc *sc, int rid, void *buf, int ebuflen) 353 { 354 int buflen, rc; 355 356 buflen = ebuflen; 357 if ((rc = wi_read_rid(sc, rid, buf, &buflen)) != 0) 358 return rc; 359 360 if (buflen < ebuflen) { 361 #ifdef WI_DEBUG 362 printf("%s: rid=%#04x read %d, expected %d\n", __func__, 363 rid, buflen, ebuflen); 364 #endif 365 return -1; 366 } 367 return 0; 368 } 369 370 int 371 wi_attach(struct wi_softc *sc, const u_int8_t *macaddr) 372 { 373 struct ieee80211com *ic = &sc->sc_ic; 374 struct ifnet *ifp = &sc->sc_if; 375 int chan, nrate, buflen; 376 u_int16_t val, chanavail; 377 struct { 378 u_int16_t nrates; 379 char rates[IEEE80211_RATE_SIZE]; 380 } ratebuf; 381 static const u_int8_t empty_macaddr[IEEE80211_ADDR_LEN] = { 382 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 383 }; 384 int s; 385 386 s = splnet(); 387 388 /* Make sure interrupts are disabled. */ 389 CSR_WRITE_2(sc, WI_INT_EN, 0); 390 CSR_WRITE_2(sc, WI_EVENT_ACK, ~0); 391 392 sc->sc_invalid = 0; 393 394 /* Reset the NIC. */ 395 if (wi_reset(sc) != 0) { 396 sc->sc_invalid = 1; 397 splx(s); 398 return 1; 399 } 400 401 if (wi_read_xrid(sc, WI_RID_MAC_NODE, ic->ic_myaddr, 402 IEEE80211_ADDR_LEN) != 0 || 403 IEEE80211_ADDR_EQ(ic->ic_myaddr, empty_macaddr)) { 404 if (macaddr != NULL) 405 memcpy(ic->ic_myaddr, macaddr, IEEE80211_ADDR_LEN); 406 else { 407 printf(" could not get mac address, attach failed\n"); 408 splx(s); 409 return 1; 410 } 411 } 412 413 printf(" 802.11 address %s\n", ether_sprintf(ic->ic_myaddr)); 414 415 /* Read NIC identification */ 416 wi_read_nicid(sc); 417 418 memcpy(ifp->if_xname, sc->sc_dev.dv_xname, IFNAMSIZ); 419 ifp->if_softc = sc; 420 ifp->if_start = wi_start; 421 ifp->if_ioctl = wi_ioctl; 422 ifp->if_watchdog = wi_watchdog; 423 ifp->if_init = wi_init; 424 ifp->if_stop = wi_stop; 425 ifp->if_flags = 426 IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST | IFF_NOTRAILERS; 427 IFQ_SET_READY(&ifp->if_snd); 428 429 ic->ic_ifp = ifp; 430 ic->ic_phytype = IEEE80211_T_DS; 431 ic->ic_opmode = IEEE80211_M_STA; 432 ic->ic_caps = IEEE80211_C_AHDEMO; 433 ic->ic_state = IEEE80211_S_INIT; 434 ic->ic_max_aid = WI_MAX_AID; 435 436 /* Find available channel */ 437 if (wi_read_xrid(sc, WI_RID_CHANNEL_LIST, &chanavail, 438 sizeof(chanavail)) != 0) { 439 aprint_normal("%s: using default channel list\n", sc->sc_dev.dv_xname); 440 chanavail = htole16(0x1fff); /* assume 1-13 */ 441 } 442 for (chan = 16; chan > 0; chan--) { 443 if (!isset((u_int8_t*)&chanavail, chan - 1)) 444 continue; 445 ic->ic_ibss_chan = &ic->ic_channels[chan]; 446 ic->ic_channels[chan].ic_freq = 447 ieee80211_ieee2mhz(chan, IEEE80211_CHAN_2GHZ); 448 ic->ic_channels[chan].ic_flags = IEEE80211_CHAN_B; 449 } 450 451 /* Find default IBSS channel */ 452 if (wi_read_xrid(sc, WI_RID_OWN_CHNL, &val, sizeof(val)) == 0) { 453 chan = le16toh(val); 454 if (isset((u_int8_t*)&chanavail, chan - 1)) 455 ic->ic_ibss_chan = &ic->ic_channels[chan]; 456 } 457 if (ic->ic_ibss_chan == NULL) { 458 aprint_error("%s: no available channel\n", sc->sc_dev.dv_xname); 459 return 1; 460 } 461 462 if (sc->sc_firmware_type == WI_LUCENT) { 463 sc->sc_dbm_offset = WI_LUCENT_DBM_OFFSET; 464 } else { 465 if ((sc->sc_flags & WI_FLAGS_HAS_DBMADJUST) && 466 wi_read_xrid(sc, WI_RID_DBM_ADJUST, &val, sizeof(val)) == 0) 467 sc->sc_dbm_offset = le16toh(val); 468 else 469 sc->sc_dbm_offset = WI_PRISM_DBM_OFFSET; 470 } 471 472 sc->sc_flags |= WI_FLAGS_RSSADAPTSTA; 473 474 /* 475 * Set flags based on firmware version. 476 */ 477 switch (sc->sc_firmware_type) { 478 case WI_LUCENT: 479 sc->sc_flags |= WI_FLAGS_HAS_SYSSCALE; 480 #ifdef WI_HERMES_AUTOINC_WAR 481 /* XXX: not confirmed, but never seen for recent firmware */ 482 if (sc->sc_sta_firmware_ver < 40000) { 483 sc->sc_flags |= WI_FLAGS_BUG_AUTOINC; 484 } 485 #endif 486 if (sc->sc_sta_firmware_ver >= 60000) 487 sc->sc_flags |= WI_FLAGS_HAS_MOR; 488 if (sc->sc_sta_firmware_ver >= 60006) { 489 ic->ic_caps |= IEEE80211_C_IBSS; 490 ic->ic_caps |= IEEE80211_C_MONITOR; 491 } 492 ic->ic_caps |= IEEE80211_C_PMGT; 493 sc->sc_ibss_port = 1; 494 break; 495 496 case WI_INTERSIL: 497 sc->sc_flags |= WI_FLAGS_HAS_FRAGTHR; 498 sc->sc_flags |= WI_FLAGS_HAS_ROAMING; 499 sc->sc_flags |= WI_FLAGS_HAS_SYSSCALE; 500 if (sc->sc_sta_firmware_ver > 10101) 501 sc->sc_flags |= WI_FLAGS_HAS_DBMADJUST; 502 if (sc->sc_sta_firmware_ver >= 800) { 503 if (sc->sc_sta_firmware_ver != 10402) 504 ic->ic_caps |= IEEE80211_C_HOSTAP; 505 ic->ic_caps |= IEEE80211_C_IBSS; 506 ic->ic_caps |= IEEE80211_C_MONITOR; 507 } 508 ic->ic_caps |= IEEE80211_C_PMGT; 509 sc->sc_ibss_port = 0; 510 sc->sc_alt_retry = 2; 511 break; 512 513 case WI_SYMBOL: 514 sc->sc_flags |= WI_FLAGS_HAS_DIVERSITY; 515 if (sc->sc_sta_firmware_ver >= 20000) 516 ic->ic_caps |= IEEE80211_C_IBSS; 517 sc->sc_ibss_port = 4; 518 break; 519 } 520 521 /* 522 * Find out if we support WEP on this card. 523 */ 524 if (wi_read_xrid(sc, WI_RID_WEP_AVAIL, &val, sizeof(val)) == 0 && 525 val != htole16(0)) 526 ic->ic_caps |= IEEE80211_C_WEP; 527 528 /* Find supported rates. */ 529 buflen = sizeof(ratebuf); 530 if (wi_read_rid(sc, WI_RID_DATA_RATES, &ratebuf, &buflen) == 0 && 531 buflen > 2) { 532 nrate = le16toh(ratebuf.nrates); 533 if (nrate > IEEE80211_RATE_SIZE) 534 nrate = IEEE80211_RATE_SIZE; 535 memcpy(ic->ic_sup_rates[IEEE80211_MODE_11B].rs_rates, 536 &ratebuf.rates[0], nrate); 537 ic->ic_sup_rates[IEEE80211_MODE_11B].rs_nrates = nrate; 538 } else { 539 aprint_error("%s: no supported rate list\n", sc->sc_dev.dv_xname); 540 return 1; 541 } 542 543 sc->sc_max_datalen = 2304; 544 sc->sc_rts_thresh = 2347; 545 sc->sc_frag_thresh = 2346; 546 sc->sc_system_scale = 1; 547 sc->sc_cnfauthmode = IEEE80211_AUTH_OPEN; 548 sc->sc_roaming_mode = 1; 549 550 callout_init(&sc->sc_rssadapt_ch, 0); 551 552 /* 553 * Call MI attach routines. 554 */ 555 if_attach(ifp); 556 ieee80211_ifattach(ic); 557 558 sc->sc_newstate = ic->ic_newstate; 559 sc->sc_set_tim = ic->ic_set_tim; 560 ic->ic_newstate = wi_newstate; 561 ic->ic_node_alloc = wi_node_alloc; 562 ic->ic_node_free = wi_node_free; 563 ic->ic_set_tim = wi_set_tim; 564 565 ic->ic_crypto.cs_key_delete = wi_key_delete; 566 ic->ic_crypto.cs_key_set = wi_key_set; 567 ic->ic_crypto.cs_key_update_begin = wi_key_update_begin; 568 ic->ic_crypto.cs_key_update_end = wi_key_update_end; 569 570 ieee80211_media_init(ic, wi_media_change, wi_media_status); 571 572 #if NBPFILTER > 0 573 bpfattach2(ifp, DLT_IEEE802_11_RADIO, 574 sizeof(struct ieee80211_frame) + 64, &sc->sc_drvbpf); 575 #endif 576 577 memset(&sc->sc_rxtapu, 0, sizeof(sc->sc_rxtapu)); 578 sc->sc_rxtap.wr_ihdr.it_len = htole16(sizeof(sc->sc_rxtapu)); 579 sc->sc_rxtap.wr_ihdr.it_present = htole32(WI_RX_RADIOTAP_PRESENT); 580 581 memset(&sc->sc_txtapu, 0, sizeof(sc->sc_txtapu)); 582 sc->sc_txtap.wt_ihdr.it_len = htole16(sizeof(sc->sc_txtapu)); 583 sc->sc_txtap.wt_ihdr.it_present = htole32(WI_TX_RADIOTAP_PRESENT); 584 585 /* Attach is successful. */ 586 sc->sc_attached = 1; 587 588 splx(s); 589 ieee80211_announce(ic); 590 return 0; 591 } 592 593 int 594 wi_detach(struct wi_softc *sc) 595 { 596 struct ifnet *ifp = &sc->sc_if; 597 int s; 598 599 if (!sc->sc_attached) 600 return 0; 601 602 sc->sc_invalid = 1; 603 s = splnet(); 604 605 wi_stop(ifp, 1); 606 607 ieee80211_ifdetach(&sc->sc_ic); 608 if_detach(ifp); 609 splx(s); 610 return 0; 611 } 612 613 #ifdef __NetBSD__ 614 int 615 wi_activate(struct device *self, enum devact act) 616 { 617 struct wi_softc *sc = (struct wi_softc *)self; 618 int rv = 0, s; 619 620 s = splnet(); 621 switch (act) { 622 case DVACT_ACTIVATE: 623 rv = EOPNOTSUPP; 624 break; 625 626 case DVACT_DEACTIVATE: 627 if_deactivate(&sc->sc_if); 628 break; 629 } 630 splx(s); 631 return rv; 632 } 633 #endif /* __NetBSD__ */ 634 635 int 636 wi_intr(void *arg) 637 { 638 int i; 639 struct wi_softc *sc = arg; 640 struct ifnet *ifp = &sc->sc_if; 641 u_int16_t status; 642 643 if (sc->sc_enabled == 0 || 644 !device_is_active(&sc->sc_dev) || 645 (ifp->if_flags & IFF_RUNNING) == 0) 646 return 0; 647 648 if ((ifp->if_flags & IFF_UP) == 0) { 649 CSR_WRITE_2(sc, WI_INT_EN, 0); 650 CSR_WRITE_2(sc, WI_EVENT_ACK, ~0); 651 return 1; 652 } 653 654 /* This is superfluous on Prism, but Lucent breaks if we 655 * do not disable interrupts. 656 */ 657 CSR_WRITE_2(sc, WI_INT_EN, 0); 658 659 /* maximum 10 loops per interrupt */ 660 for (i = 0; i < 10; i++) { 661 status = CSR_READ_2(sc, WI_EVENT_STAT); 662 #ifdef WI_DEBUG 663 if (wi_debug > 1) { 664 printf("%s: iter %d status %#04x\n", __func__, i, 665 status); 666 } 667 #endif /* WI_DEBUG */ 668 if ((status & WI_INTRS) == 0) 669 break; 670 671 sc->sc_status = status; 672 673 if (status & WI_EV_RX) 674 wi_rx_intr(sc); 675 676 if (status & WI_EV_ALLOC) 677 wi_txalloc_intr(sc); 678 679 if (status & WI_EV_TX) 680 wi_tx_intr(sc); 681 682 if (status & WI_EV_TX_EXC) 683 wi_tx_ex_intr(sc); 684 685 if (status & WI_EV_INFO) 686 wi_info_intr(sc); 687 688 CSR_WRITE_2(sc, WI_EVENT_ACK, sc->sc_status); 689 690 if (sc->sc_status & WI_EV_CMD) 691 wi_cmd_intr(sc); 692 693 if ((ifp->if_flags & IFF_OACTIVE) == 0 && 694 (sc->sc_flags & WI_FLAGS_OUTRANGE) == 0 && 695 !IFQ_IS_EMPTY(&ifp->if_snd)) 696 wi_start(ifp); 697 698 sc->sc_status = 0; 699 } 700 701 /* re-enable interrupts */ 702 CSR_WRITE_2(sc, WI_INT_EN, WI_INTRS); 703 704 sc->sc_status = 0; 705 706 return 1; 707 } 708 709 #define arraylen(a) (sizeof(a) / sizeof((a)[0])) 710 711 STATIC void 712 wi_rssdescs_init(struct wi_rssdesc (*rssd)[WI_NTXRSS], wi_rssdescq_t *rssdfree) 713 { 714 int i; 715 SLIST_INIT(rssdfree); 716 for (i = 0; i < arraylen(*rssd); i++) { 717 SLIST_INSERT_HEAD(rssdfree, &(*rssd)[i], rd_next); 718 } 719 } 720 721 STATIC void 722 wi_rssdescs_reset(struct ieee80211com *ic, struct wi_rssdesc (*rssd)[WI_NTXRSS], 723 wi_rssdescq_t *rssdfree, u_int8_t (*txpending)[IEEE80211_RATE_MAXSIZE]) 724 { 725 struct ieee80211_node *ni; 726 int i; 727 for (i = 0; i < arraylen(*rssd); i++) { 728 ni = (*rssd)[i].rd_desc.id_node; 729 (*rssd)[i].rd_desc.id_node = NULL; 730 if (ni != NULL && (ic->ic_ifp->if_flags & IFF_DEBUG) != 0) 731 printf("%s: cleaning outstanding rssadapt " 732 "descriptor for %s\n", 733 ic->ic_ifp->if_xname, ether_sprintf(ni->ni_macaddr)); 734 if (ni != NULL) 735 ieee80211_free_node(ni); 736 } 737 memset(*txpending, 0, sizeof(*txpending)); 738 wi_rssdescs_init(rssd, rssdfree); 739 } 740 741 STATIC int 742 wi_init(struct ifnet *ifp) 743 { 744 struct wi_softc *sc = ifp->if_softc; 745 struct ieee80211com *ic = &sc->sc_ic; 746 struct wi_joinreq join; 747 int i; 748 int error = 0, wasenabled; 749 750 DPRINTF(("wi_init: enabled %d\n", sc->sc_enabled)); 751 wasenabled = sc->sc_enabled; 752 if (!sc->sc_enabled) { 753 if ((error = (*sc->sc_enable)(sc)) != 0) 754 goto out; 755 sc->sc_enabled = 1; 756 } else 757 wi_stop(ifp, 0); 758 759 /* Symbol firmware cannot be initialized more than once */ 760 if (sc->sc_firmware_type != WI_SYMBOL || !wasenabled) 761 if ((error = wi_reset(sc)) != 0) 762 goto out; 763 764 /* common 802.11 configuration */ 765 ic->ic_flags &= ~IEEE80211_F_IBSSON; 766 sc->sc_flags &= ~WI_FLAGS_OUTRANGE; 767 switch (ic->ic_opmode) { 768 case IEEE80211_M_STA: 769 wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_BSS); 770 break; 771 case IEEE80211_M_IBSS: 772 wi_write_val(sc, WI_RID_PORTTYPE, sc->sc_ibss_port); 773 ic->ic_flags |= IEEE80211_F_IBSSON; 774 break; 775 case IEEE80211_M_AHDEMO: 776 wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_ADHOC); 777 break; 778 case IEEE80211_M_HOSTAP: 779 wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_HOSTAP); 780 break; 781 case IEEE80211_M_MONITOR: 782 if (sc->sc_firmware_type == WI_LUCENT) 783 wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_ADHOC); 784 wi_cmd(sc, WI_CMD_TEST | (WI_TEST_MONITOR << 8), 0, 0, 0); 785 break; 786 } 787 788 /* Intersil interprets this RID as joining ESS even in IBSS mode */ 789 if (sc->sc_firmware_type == WI_LUCENT && 790 (ic->ic_flags & IEEE80211_F_IBSSON) && ic->ic_des_esslen > 0) 791 wi_write_val(sc, WI_RID_CREATE_IBSS, 1); 792 else 793 wi_write_val(sc, WI_RID_CREATE_IBSS, 0); 794 wi_write_val(sc, WI_RID_MAX_SLEEP, ic->ic_lintval); 795 wi_write_ssid(sc, WI_RID_DESIRED_SSID, ic->ic_des_essid, 796 ic->ic_des_esslen); 797 wi_write_val(sc, WI_RID_OWN_CHNL, 798 ieee80211_chan2ieee(ic, ic->ic_ibss_chan)); 799 wi_write_ssid(sc, WI_RID_OWN_SSID, ic->ic_des_essid, ic->ic_des_esslen); 800 IEEE80211_ADDR_COPY(ic->ic_myaddr, LLADDR(ifp->if_sadl)); 801 wi_write_rid(sc, WI_RID_MAC_NODE, ic->ic_myaddr, IEEE80211_ADDR_LEN); 802 if (ic->ic_caps & IEEE80211_C_PMGT) 803 wi_write_val(sc, WI_RID_PM_ENABLED, 804 (ic->ic_flags & IEEE80211_F_PMGTON) ? 1 : 0); 805 806 /* not yet common 802.11 configuration */ 807 wi_write_val(sc, WI_RID_MAX_DATALEN, sc->sc_max_datalen); 808 wi_write_val(sc, WI_RID_RTS_THRESH, sc->sc_rts_thresh); 809 if (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR) 810 wi_write_val(sc, WI_RID_FRAG_THRESH, sc->sc_frag_thresh); 811 812 /* driver specific 802.11 configuration */ 813 if (sc->sc_flags & WI_FLAGS_HAS_SYSSCALE) 814 wi_write_val(sc, WI_RID_SYSTEM_SCALE, sc->sc_system_scale); 815 if (sc->sc_flags & WI_FLAGS_HAS_ROAMING) 816 wi_write_val(sc, WI_RID_ROAMING_MODE, sc->sc_roaming_mode); 817 if (sc->sc_flags & WI_FLAGS_HAS_MOR) 818 wi_write_val(sc, WI_RID_MICROWAVE_OVEN, sc->sc_microwave_oven); 819 wi_cfg_txrate(sc); 820 wi_write_ssid(sc, WI_RID_NODENAME, sc->sc_nodename, sc->sc_nodelen); 821 822 #ifndef IEEE80211_NO_HOSTAP 823 if (ic->ic_opmode == IEEE80211_M_HOSTAP && 824 sc->sc_firmware_type == WI_INTERSIL) { 825 wi_write_val(sc, WI_RID_OWN_BEACON_INT, ic->ic_lintval); 826 wi_write_val(sc, WI_RID_DTIM_PERIOD, 1); 827 } 828 #endif /* !IEEE80211_NO_HOSTAP */ 829 830 if (sc->sc_firmware_type == WI_INTERSIL) { 831 struct ieee80211_rateset *rs = 832 &ic->ic_sup_rates[IEEE80211_MODE_11B]; 833 u_int16_t basic = 0, supported = 0, rate; 834 835 for (i = 0; i < rs->rs_nrates; i++) { 836 switch (rs->rs_rates[i] & IEEE80211_RATE_VAL) { 837 case 2: 838 rate = 1; 839 break; 840 case 4: 841 rate = 2; 842 break; 843 case 11: 844 rate = 4; 845 break; 846 case 22: 847 rate = 8; 848 break; 849 default: 850 rate = 0; 851 break; 852 } 853 if (rs->rs_rates[i] & IEEE80211_RATE_BASIC) 854 basic |= rate; 855 supported |= rate; 856 } 857 wi_write_val(sc, WI_RID_BASIC_RATE, basic); 858 wi_write_val(sc, WI_RID_SUPPORT_RATE, supported); 859 wi_write_val(sc, WI_RID_ALT_RETRY_COUNT, sc->sc_alt_retry); 860 } 861 862 /* 863 * Initialize promisc mode. 864 * Being in Host-AP mode causes a great 865 * deal of pain if promiscuous mode is set. 866 * Therefore we avoid confusing the firmware 867 * and always reset promisc mode in Host-AP 868 * mode. Host-AP sees all the packets anyway. 869 */ 870 if (ic->ic_opmode != IEEE80211_M_HOSTAP && 871 (ifp->if_flags & IFF_PROMISC) != 0) { 872 wi_write_val(sc, WI_RID_PROMISC, 1); 873 } else { 874 wi_write_val(sc, WI_RID_PROMISC, 0); 875 } 876 877 /* Configure WEP. */ 878 if (ic->ic_caps & IEEE80211_C_WEP) { 879 sc->sc_cnfauthmode = ic->ic_bss->ni_authmode; 880 wi_write_wep(sc); 881 } 882 883 /* Set multicast filter. */ 884 wi_write_multi(sc); 885 886 sc->sc_txalloc = 0; 887 sc->sc_txalloced = 0; 888 sc->sc_txqueue = 0; 889 sc->sc_txqueued = 0; 890 sc->sc_txstart = 0; 891 sc->sc_txstarted = 0; 892 893 if (sc->sc_firmware_type != WI_SYMBOL || !wasenabled) { 894 sc->sc_buflen = IEEE80211_MAX_LEN + sizeof(struct wi_frame); 895 if (sc->sc_firmware_type == WI_SYMBOL) 896 sc->sc_buflen = 1585; /* XXX */ 897 for (i = 0; i < WI_NTXBUF; i++) { 898 error = wi_alloc_fid(sc, sc->sc_buflen, 899 &sc->sc_txd[i].d_fid); 900 if (error) { 901 printf("%s: tx buffer allocation failed\n", 902 sc->sc_dev.dv_xname); 903 goto out; 904 } 905 DPRINTF2(("wi_init: txbuf %d allocated %x\n", i, 906 sc->sc_txd[i].d_fid)); 907 ++sc->sc_txalloced; 908 } 909 } 910 911 wi_rssdescs_init(&sc->sc_rssd, &sc->sc_rssdfree); 912 913 /* Enable desired port */ 914 wi_cmd(sc, WI_CMD_ENABLE | sc->sc_portnum, 0, 0, 0); 915 ifp->if_flags |= IFF_RUNNING; 916 ifp->if_flags &= ~IFF_OACTIVE; 917 ic->ic_state = IEEE80211_S_INIT; 918 919 if (ic->ic_opmode == IEEE80211_M_AHDEMO || 920 ic->ic_opmode == IEEE80211_M_IBSS || 921 ic->ic_opmode == IEEE80211_M_MONITOR || 922 ic->ic_opmode == IEEE80211_M_HOSTAP) 923 ieee80211_create_ibss(ic, ic->ic_ibss_chan); 924 925 /* Enable interrupts */ 926 CSR_WRITE_2(sc, WI_INT_EN, WI_INTRS); 927 928 #ifndef IEEE80211_NO_HOSTAP 929 if (!wasenabled && 930 ic->ic_opmode == IEEE80211_M_HOSTAP && 931 sc->sc_firmware_type == WI_INTERSIL) { 932 /* XXX: some card need to be re-enabled for hostap */ 933 wi_cmd(sc, WI_CMD_DISABLE | WI_PORT0, 0, 0, 0); 934 wi_cmd(sc, WI_CMD_ENABLE | WI_PORT0, 0, 0, 0); 935 } 936 #endif /* !IEEE80211_NO_HOSTAP */ 937 938 if (ic->ic_opmode == IEEE80211_M_STA && 939 ((ic->ic_flags & IEEE80211_F_DESBSSID) || 940 ic->ic_des_chan != IEEE80211_CHAN_ANYC)) { 941 memset(&join, 0, sizeof(join)); 942 if (ic->ic_flags & IEEE80211_F_DESBSSID) 943 IEEE80211_ADDR_COPY(&join.wi_bssid, ic->ic_des_bssid); 944 if (ic->ic_des_chan != IEEE80211_CHAN_ANYC) 945 join.wi_chan = 946 htole16(ieee80211_chan2ieee(ic, ic->ic_des_chan)); 947 /* Lucent firmware does not support the JOIN RID. */ 948 if (sc->sc_firmware_type != WI_LUCENT) 949 wi_write_rid(sc, WI_RID_JOIN_REQ, &join, sizeof(join)); 950 } 951 952 out: 953 if (error) { 954 printf("%s: interface not running\n", sc->sc_dev.dv_xname); 955 wi_stop(ifp, 0); 956 } 957 DPRINTF(("wi_init: return %d\n", error)); 958 return error; 959 } 960 961 STATIC void 962 wi_txcmd_wait(struct wi_softc *sc) 963 { 964 KASSERT(sc->sc_txcmds == 1); 965 if (sc->sc_status & WI_EV_CMD) { 966 sc->sc_status &= ~WI_EV_CMD; 967 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_CMD); 968 } else 969 (void)wi_cmd_wait(sc, WI_CMD_TX | WI_RECLAIM, 0); 970 } 971 972 STATIC void 973 wi_stop(struct ifnet *ifp, int disable) 974 { 975 struct wi_softc *sc = ifp->if_softc; 976 struct ieee80211com *ic = &sc->sc_ic; 977 int s; 978 979 if (!sc->sc_enabled) 980 return; 981 982 s = splnet(); 983 984 DPRINTF(("wi_stop: disable %d\n", disable)); 985 986 ieee80211_new_state(ic, IEEE80211_S_INIT, -1); 987 988 /* wait for tx command completion (deassoc, deauth) */ 989 while (sc->sc_txcmds > 0) { 990 wi_txcmd_wait(sc); 991 wi_cmd_intr(sc); 992 } 993 994 /* TBD wait for deassoc, deauth tx completion? */ 995 996 if (!sc->sc_invalid) { 997 CSR_WRITE_2(sc, WI_INT_EN, 0); 998 wi_cmd(sc, WI_CMD_DISABLE | sc->sc_portnum, 0, 0, 0); 999 } 1000 1001 wi_rssdescs_reset(ic, &sc->sc_rssd, &sc->sc_rssdfree, 1002 &sc->sc_txpending); 1003 1004 sc->sc_tx_timer = 0; 1005 sc->sc_scan_timer = 0; 1006 sc->sc_false_syns = 0; 1007 sc->sc_naps = 0; 1008 ifp->if_flags &= ~(IFF_OACTIVE | IFF_RUNNING); 1009 ifp->if_timer = 0; 1010 1011 if (disable) { 1012 if (sc->sc_disable) 1013 (*sc->sc_disable)(sc); 1014 sc->sc_enabled = 0; 1015 } 1016 splx(s); 1017 } 1018 1019 /* 1020 * Choose a data rate for a packet len bytes long that suits the packet 1021 * type and the wireless conditions. 1022 * 1023 * TBD Adapt fragmentation threshold. 1024 */ 1025 STATIC int 1026 wi_choose_rate(struct ieee80211com *ic, struct ieee80211_node *ni, 1027 struct ieee80211_frame *wh, u_int len) 1028 { 1029 struct wi_softc *sc = ic->ic_ifp->if_softc; 1030 struct wi_node *wn = (void*)ni; 1031 struct ieee80211_rssadapt *ra = &wn->wn_rssadapt; 1032 int do_not_adapt, i, rateidx, s; 1033 1034 do_not_adapt = (ic->ic_opmode != IEEE80211_M_HOSTAP) && 1035 (sc->sc_flags & WI_FLAGS_RSSADAPTSTA) == 0; 1036 1037 s = splnet(); 1038 1039 rateidx = ieee80211_rssadapt_choose(ra, &ni->ni_rates, wh, len, 1040 ic->ic_fixed_rate, 1041 ((ic->ic_ifp->if_flags & IFF_DEBUG) == 0) ? NULL : ic->ic_ifp->if_xname, 1042 do_not_adapt); 1043 1044 ni->ni_txrate = rateidx; 1045 1046 if (ic->ic_opmode != IEEE80211_M_HOSTAP) { 1047 /* choose the slowest pending rate so that we don't 1048 * accidentally send a packet on the MAC's queue 1049 * too fast. TBD find out if the MAC labels Tx 1050 * packets w/ rate when enqueued or dequeued. 1051 */ 1052 for (i = 0; i < rateidx && sc->sc_txpending[i] == 0; i++); 1053 rateidx = i; 1054 } 1055 1056 splx(s); 1057 return (rateidx); 1058 } 1059 1060 STATIC void 1061 wi_raise_rate(struct ieee80211com *ic, struct ieee80211_rssdesc *id) 1062 { 1063 struct wi_node *wn; 1064 if (id->id_node == NULL) 1065 return; 1066 1067 wn = (void*)id->id_node; 1068 ieee80211_rssadapt_raise_rate(ic, &wn->wn_rssadapt, id); 1069 } 1070 1071 STATIC void 1072 wi_lower_rate(struct ieee80211com *ic, struct ieee80211_rssdesc *id) 1073 { 1074 struct ieee80211_node *ni; 1075 struct wi_node *wn; 1076 int s; 1077 1078 s = splnet(); 1079 1080 if ((ni = id->id_node) == NULL) { 1081 DPRINTF(("wi_lower_rate: missing node\n")); 1082 goto out; 1083 } 1084 1085 wn = (void *)ni; 1086 1087 ieee80211_rssadapt_lower_rate(ic, ni, &wn->wn_rssadapt, id); 1088 out: 1089 splx(s); 1090 return; 1091 } 1092 1093 STATIC void 1094 wi_start(struct ifnet *ifp) 1095 { 1096 struct wi_softc *sc = ifp->if_softc; 1097 struct ieee80211com *ic = &sc->sc_ic; 1098 struct ether_header *eh; 1099 struct ieee80211_node *ni; 1100 struct ieee80211_frame *wh; 1101 struct ieee80211_rateset *rs; 1102 struct wi_rssdesc *rd; 1103 struct ieee80211_rssdesc *id; 1104 struct mbuf *m0; 1105 struct wi_frame frmhdr; 1106 int cur, fid, off, rateidx; 1107 1108 if (!sc->sc_enabled || sc->sc_invalid) 1109 return; 1110 if (sc->sc_flags & WI_FLAGS_OUTRANGE) 1111 return; 1112 1113 memset(&frmhdr, 0, sizeof(frmhdr)); 1114 cur = sc->sc_txqueue; 1115 for (;;) { 1116 ni = ic->ic_bss; 1117 if (sc->sc_txalloced == 0 || SLIST_EMPTY(&sc->sc_rssdfree)) { 1118 ifp->if_flags |= IFF_OACTIVE; 1119 break; 1120 } 1121 if (!IF_IS_EMPTY(&ic->ic_mgtq)) { 1122 IF_DEQUEUE(&ic->ic_mgtq, m0); 1123 m_copydata(m0, 4, ETHER_ADDR_LEN * 2, 1124 (void *)&frmhdr.wi_ehdr); 1125 frmhdr.wi_ehdr.ether_type = 0; 1126 wh = mtod(m0, struct ieee80211_frame *); 1127 ni = (struct ieee80211_node *)m0->m_pkthdr.rcvif; 1128 m0->m_pkthdr.rcvif = NULL; 1129 } else if (ic->ic_state == IEEE80211_S_RUN) { 1130 IFQ_POLL(&ifp->if_snd, m0); 1131 if (m0 == NULL) 1132 break; 1133 IFQ_DEQUEUE(&ifp->if_snd, m0); 1134 ifp->if_opackets++; 1135 m_copydata(m0, 0, ETHER_HDR_LEN, 1136 (void *)&frmhdr.wi_ehdr); 1137 #if NBPFILTER > 0 1138 if (ifp->if_bpf) 1139 bpf_mtap(ifp->if_bpf, m0); 1140 #endif 1141 1142 eh = mtod(m0, struct ether_header *); 1143 ni = ieee80211_find_txnode(ic, eh->ether_dhost); 1144 if (ni == NULL) { 1145 ifp->if_oerrors++; 1146 continue; 1147 } 1148 if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) && 1149 (m0->m_flags & M_PWR_SAV) == 0) { 1150 ieee80211_pwrsave(ic, ni, m0); 1151 goto next; 1152 } 1153 if ((m0 = ieee80211_encap(ic, m0, ni)) == NULL) { 1154 ieee80211_free_node(ni); 1155 ifp->if_oerrors++; 1156 continue; 1157 } 1158 wh = mtod(m0, struct ieee80211_frame *); 1159 } else 1160 break; 1161 #if NBPFILTER > 0 1162 if (ic->ic_rawbpf) 1163 bpf_mtap(ic->ic_rawbpf, m0); 1164 #endif 1165 frmhdr.wi_tx_ctl = 1166 htole16(WI_ENC_TX_802_11|WI_TXCNTL_TX_EX|WI_TXCNTL_TX_OK); 1167 #ifndef IEEE80211_NO_HOSTAP 1168 if (ic->ic_opmode == IEEE80211_M_HOSTAP) 1169 frmhdr.wi_tx_ctl |= htole16(WI_TXCNTL_ALTRTRY); 1170 if (ic->ic_opmode == IEEE80211_M_HOSTAP && 1171 (wh->i_fc[1] & IEEE80211_FC1_WEP)) { 1172 if (ieee80211_crypto_encap(ic, ni, m0) == NULL) { 1173 m_freem(m0); 1174 ifp->if_oerrors++; 1175 goto next; 1176 } 1177 frmhdr.wi_tx_ctl |= htole16(WI_TXCNTL_NOCRYPT); 1178 } 1179 #endif /* !IEEE80211_NO_HOSTAP */ 1180 1181 rateidx = wi_choose_rate(ic, ni, wh, m0->m_pkthdr.len); 1182 rs = &ni->ni_rates; 1183 1184 #if NBPFILTER > 0 1185 if (sc->sc_drvbpf) { 1186 struct wi_tx_radiotap_header *tap = &sc->sc_txtap; 1187 1188 tap->wt_rate = rs->rs_rates[rateidx]; 1189 tap->wt_chan_freq = 1190 htole16(ic->ic_bss->ni_chan->ic_freq); 1191 tap->wt_chan_flags = 1192 htole16(ic->ic_bss->ni_chan->ic_flags); 1193 /* TBD tap->wt_flags */ 1194 1195 bpf_mtap2(sc->sc_drvbpf, tap, tap->wt_ihdr.it_len, m0); 1196 } 1197 #endif 1198 1199 rd = SLIST_FIRST(&sc->sc_rssdfree); 1200 id = &rd->rd_desc; 1201 id->id_len = m0->m_pkthdr.len; 1202 id->id_rateidx = ni->ni_txrate; 1203 id->id_rssi = ni->ni_rssi; 1204 1205 frmhdr.wi_tx_idx = rd - sc->sc_rssd; 1206 1207 if (ic->ic_opmode == IEEE80211_M_HOSTAP) 1208 frmhdr.wi_tx_rate = 5 * (rs->rs_rates[rateidx] & 1209 IEEE80211_RATE_VAL); 1210 else if (sc->sc_flags & WI_FLAGS_RSSADAPTSTA) 1211 (void)wi_write_txrate(sc, rs->rs_rates[rateidx]); 1212 1213 m_copydata(m0, 0, sizeof(struct ieee80211_frame), 1214 (void *)&frmhdr.wi_whdr); 1215 m_adj(m0, sizeof(struct ieee80211_frame)); 1216 frmhdr.wi_dat_len = htole16(m0->m_pkthdr.len); 1217 if (IFF_DUMPPKTS(ifp)) 1218 wi_dump_pkt(&frmhdr, ni, -1); 1219 fid = sc->sc_txd[cur].d_fid; 1220 off = sizeof(frmhdr); 1221 if (wi_write_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr)) != 0 || 1222 wi_mwrite_bap(sc, fid, off, m0, m0->m_pkthdr.len) != 0) { 1223 printf("%s: %s write fid %x failed\n", 1224 sc->sc_dev.dv_xname, __func__, fid); 1225 ifp->if_oerrors++; 1226 m_freem(m0); 1227 goto next; 1228 } 1229 m_freem(m0); 1230 sc->sc_txpending[ni->ni_txrate]++; 1231 --sc->sc_txalloced; 1232 if (sc->sc_txqueued++ == 0) { 1233 #ifdef DIAGNOSTIC 1234 if (cur != sc->sc_txstart) 1235 printf("%s: ring is desynchronized\n", 1236 sc->sc_dev.dv_xname); 1237 #endif 1238 wi_push_packet(sc); 1239 } else { 1240 #ifdef WI_RING_DEBUG 1241 printf("%s: queue %04x, alloc %d queue %d start %d alloced %d queued %d started %d\n", 1242 sc->sc_dev.dv_xname, fid, 1243 sc->sc_txalloc, sc->sc_txqueue, sc->sc_txstart, 1244 sc->sc_txalloced, sc->sc_txqueued, sc->sc_txstarted); 1245 #endif 1246 } 1247 sc->sc_txqueue = cur = (cur + 1) % WI_NTXBUF; 1248 SLIST_REMOVE_HEAD(&sc->sc_rssdfree, rd_next); 1249 id->id_node = ni; 1250 continue; 1251 next: 1252 if (ni != NULL) 1253 ieee80211_free_node(ni); 1254 } 1255 } 1256 1257 1258 STATIC int 1259 wi_reset(struct wi_softc *sc) 1260 { 1261 int i, error; 1262 1263 DPRINTF(("wi_reset\n")); 1264 1265 if (sc->sc_reset) 1266 (*sc->sc_reset)(sc); 1267 1268 error = 0; 1269 for (i = 0; i < 5; i++) { 1270 if (sc->sc_invalid) 1271 return ENXIO; 1272 DELAY(20*1000); /* XXX: way too long! */ 1273 if ((error = wi_cmd(sc, WI_CMD_INI, 0, 0, 0)) == 0) 1274 break; 1275 } 1276 if (error) { 1277 printf("%s: init failed\n", sc->sc_dev.dv_xname); 1278 return error; 1279 } 1280 CSR_WRITE_2(sc, WI_INT_EN, 0); 1281 CSR_WRITE_2(sc, WI_EVENT_ACK, ~0); 1282 1283 /* Calibrate timer. */ 1284 wi_write_val(sc, WI_RID_TICK_TIME, 0); 1285 return 0; 1286 } 1287 1288 STATIC void 1289 wi_watchdog(struct ifnet *ifp) 1290 { 1291 struct wi_softc *sc = ifp->if_softc; 1292 1293 ifp->if_timer = 0; 1294 if (!sc->sc_enabled) 1295 return; 1296 1297 if (sc->sc_tx_timer) { 1298 if (--sc->sc_tx_timer == 0) { 1299 printf("%s: device timeout\n", ifp->if_xname); 1300 ifp->if_oerrors++; 1301 wi_init(ifp); 1302 return; 1303 } 1304 ifp->if_timer = 1; 1305 } 1306 1307 if (sc->sc_scan_timer) { 1308 if (--sc->sc_scan_timer <= WI_SCAN_WAIT - WI_SCAN_INQWAIT && 1309 sc->sc_firmware_type == WI_INTERSIL) { 1310 DPRINTF(("wi_watchdog: inquire scan\n")); 1311 wi_cmd(sc, WI_CMD_INQUIRE, WI_INFO_SCAN_RESULTS, 0, 0); 1312 } 1313 if (sc->sc_scan_timer) 1314 ifp->if_timer = 1; 1315 } 1316 1317 /* TODO: rate control */ 1318 ieee80211_watchdog(&sc->sc_ic); 1319 } 1320 1321 STATIC int 1322 wi_ioctl(struct ifnet *ifp, u_long cmd, void *data) 1323 { 1324 struct wi_softc *sc = ifp->if_softc; 1325 struct ieee80211com *ic = &sc->sc_ic; 1326 struct ifreq *ifr = (struct ifreq *)data; 1327 int s, error = 0; 1328 1329 if (!device_is_active(&sc->sc_dev)) 1330 return ENXIO; 1331 1332 s = splnet(); 1333 1334 switch (cmd) { 1335 case SIOCSIFFLAGS: 1336 /* 1337 * Can't do promisc and hostap at the same time. If all that's 1338 * changing is the promisc flag, try to short-circuit a call to 1339 * wi_init() by just setting PROMISC in the hardware. 1340 */ 1341 if (ifp->if_flags & IFF_UP) { 1342 if (sc->sc_enabled) { 1343 if (ic->ic_opmode != IEEE80211_M_HOSTAP && 1344 (ifp->if_flags & IFF_PROMISC) != 0) 1345 wi_write_val(sc, WI_RID_PROMISC, 1); 1346 else 1347 wi_write_val(sc, WI_RID_PROMISC, 0); 1348 } else 1349 error = wi_init(ifp); 1350 } else if (sc->sc_enabled) 1351 wi_stop(ifp, 1); 1352 break; 1353 case SIOCSIFMEDIA: 1354 case SIOCGIFMEDIA: 1355 error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd); 1356 break; 1357 case SIOCADDMULTI: 1358 case SIOCDELMULTI: 1359 if ((error = ether_ioctl(ifp, cmd, data)) == ENETRESET) { 1360 if (ifp->if_flags & IFF_RUNNING) { 1361 /* do not rescan */ 1362 error = wi_write_multi(sc); 1363 } else 1364 error = 0; 1365 } 1366 break; 1367 case SIOCGIFGENERIC: 1368 error = wi_get_cfg(ifp, cmd, data); 1369 break; 1370 case SIOCSIFGENERIC: 1371 error = kauth_authorize_generic(curlwp->l_cred, 1372 KAUTH_GENERIC_ISSUSER, NULL); 1373 if (error) 1374 break; 1375 error = wi_set_cfg(ifp, cmd, data); 1376 if (error == ENETRESET) { 1377 if (ifp->if_flags & IFF_RUNNING) 1378 error = wi_init(ifp); 1379 else 1380 error = 0; 1381 } 1382 break; 1383 case SIOCS80211BSSID: 1384 if (sc->sc_firmware_type == WI_LUCENT) { 1385 error = ENODEV; 1386 break; 1387 } 1388 /* fall through */ 1389 default: 1390 ic->ic_flags |= sc->sc_ic_flags; 1391 error = ieee80211_ioctl(&sc->sc_ic, cmd, data); 1392 sc->sc_ic_flags = ic->ic_flags & IEEE80211_F_DROPUNENC; 1393 if (error == ENETRESET) { 1394 if (sc->sc_enabled) 1395 error = wi_init(ifp); 1396 else 1397 error = 0; 1398 } 1399 break; 1400 } 1401 wi_mend_flags(sc, ic->ic_state); 1402 splx(s); 1403 return error; 1404 } 1405 1406 STATIC int 1407 wi_media_change(struct ifnet *ifp) 1408 { 1409 struct wi_softc *sc = ifp->if_softc; 1410 struct ieee80211com *ic = &sc->sc_ic; 1411 int error; 1412 1413 error = ieee80211_media_change(ifp); 1414 if (error == ENETRESET) { 1415 if (sc->sc_enabled) 1416 error = wi_init(ifp); 1417 else 1418 error = 0; 1419 } 1420 ifp->if_baudrate = ifmedia_baudrate(ic->ic_media.ifm_cur->ifm_media); 1421 1422 return error; 1423 } 1424 1425 STATIC void 1426 wi_media_status(struct ifnet *ifp, struct ifmediareq *imr) 1427 { 1428 struct wi_softc *sc = ifp->if_softc; 1429 struct ieee80211com *ic = &sc->sc_ic; 1430 u_int16_t val; 1431 int rate; 1432 1433 if (sc->sc_enabled == 0) { 1434 imr->ifm_active = IFM_IEEE80211 | IFM_NONE; 1435 imr->ifm_status = 0; 1436 return; 1437 } 1438 1439 imr->ifm_status = IFM_AVALID; 1440 imr->ifm_active = IFM_IEEE80211; 1441 if (ic->ic_state == IEEE80211_S_RUN && 1442 (sc->sc_flags & WI_FLAGS_OUTRANGE) == 0) 1443 imr->ifm_status |= IFM_ACTIVE; 1444 if (wi_read_xrid(sc, WI_RID_CUR_TX_RATE, &val, sizeof(val)) == 0) { 1445 /* convert to 802.11 rate */ 1446 val = le16toh(val); 1447 rate = val * 2; 1448 if (sc->sc_firmware_type == WI_LUCENT) { 1449 if (rate == 10) 1450 rate = 11; /* 5.5Mbps */ 1451 } else { 1452 if (rate == 4*2) 1453 rate = 11; /* 5.5Mbps */ 1454 else if (rate == 8*2) 1455 rate = 22; /* 11Mbps */ 1456 } 1457 } else 1458 rate = 0; 1459 imr->ifm_active |= ieee80211_rate2media(ic, rate, IEEE80211_MODE_11B); 1460 switch (ic->ic_opmode) { 1461 case IEEE80211_M_STA: 1462 break; 1463 case IEEE80211_M_IBSS: 1464 imr->ifm_active |= IFM_IEEE80211_ADHOC; 1465 break; 1466 case IEEE80211_M_AHDEMO: 1467 imr->ifm_active |= IFM_IEEE80211_ADHOC | IFM_FLAG0; 1468 break; 1469 case IEEE80211_M_HOSTAP: 1470 imr->ifm_active |= IFM_IEEE80211_HOSTAP; 1471 break; 1472 case IEEE80211_M_MONITOR: 1473 imr->ifm_active |= IFM_IEEE80211_MONITOR; 1474 break; 1475 } 1476 } 1477 1478 STATIC struct ieee80211_node * 1479 wi_node_alloc(struct ieee80211_node_table *nt) 1480 { 1481 struct wi_node *wn = 1482 malloc(sizeof(struct wi_node), M_DEVBUF, M_NOWAIT | M_ZERO); 1483 return wn ? &wn->wn_node : NULL; 1484 } 1485 1486 STATIC void 1487 wi_node_free(struct ieee80211_node *ni) 1488 { 1489 struct wi_softc *sc = ni->ni_ic->ic_ifp->if_softc; 1490 int i; 1491 1492 for (i = 0; i < WI_NTXRSS; i++) { 1493 if (sc->sc_rssd[i].rd_desc.id_node == ni) 1494 sc->sc_rssd[i].rd_desc.id_node = NULL; 1495 } 1496 free(ni, M_DEVBUF); 1497 } 1498 1499 STATIC void 1500 wi_sync_bssid(struct wi_softc *sc, u_int8_t new_bssid[IEEE80211_ADDR_LEN]) 1501 { 1502 struct ieee80211com *ic = &sc->sc_ic; 1503 struct ieee80211_node *ni = ic->ic_bss; 1504 struct ifnet *ifp = &sc->sc_if; 1505 1506 if (IEEE80211_ADDR_EQ(new_bssid, ni->ni_bssid)) 1507 return; 1508 1509 DPRINTF(("wi_sync_bssid: bssid %s -> ", ether_sprintf(ni->ni_bssid))); 1510 DPRINTF(("%s ?\n", ether_sprintf(new_bssid))); 1511 1512 /* In promiscuous mode, the BSSID field is not a reliable 1513 * indicator of the firmware's BSSID. Damp spurious 1514 * change-of-BSSID indications. 1515 */ 1516 if ((ifp->if_flags & IFF_PROMISC) != 0 && 1517 !ppsratecheck(&sc->sc_last_syn, &sc->sc_false_syns, 1518 WI_MAX_FALSE_SYNS)) 1519 return; 1520 1521 sc->sc_false_syns = MAX(0, sc->sc_false_syns - 1); 1522 /* 1523 * XXX hack; we should create a new node with the new bssid 1524 * and replace the existing ic_bss with it but since we don't 1525 * process management frames to collect state we cheat by 1526 * reusing the existing node as we know wi_newstate will be 1527 * called and it will overwrite the node state. 1528 */ 1529 ieee80211_sta_join(ic, ieee80211_ref_node(ni)); 1530 } 1531 1532 static inline void 1533 wi_rssadapt_input(struct ieee80211com *ic, struct ieee80211_node *ni, 1534 struct ieee80211_frame *wh, int rssi) 1535 { 1536 struct wi_node *wn; 1537 1538 if (ni == NULL) { 1539 printf("%s: null node", __func__); 1540 return; 1541 } 1542 1543 wn = (void*)ni; 1544 ieee80211_rssadapt_input(ic, ni, &wn->wn_rssadapt, rssi); 1545 } 1546 1547 STATIC void 1548 wi_rx_intr(struct wi_softc *sc) 1549 { 1550 struct ieee80211com *ic = &sc->sc_ic; 1551 struct ifnet *ifp = &sc->sc_if; 1552 struct ieee80211_node *ni; 1553 struct wi_frame frmhdr; 1554 struct mbuf *m; 1555 struct ieee80211_frame *wh; 1556 int fid, len, off, rssi; 1557 u_int8_t dir; 1558 u_int16_t status; 1559 u_int32_t rstamp; 1560 1561 fid = CSR_READ_2(sc, WI_RX_FID); 1562 1563 /* First read in the frame header */ 1564 if (wi_read_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr))) { 1565 printf("%s: %s read fid %x failed\n", sc->sc_dev.dv_xname, 1566 __func__, fid); 1567 ifp->if_ierrors++; 1568 return; 1569 } 1570 1571 if (IFF_DUMPPKTS(ifp)) 1572 wi_dump_pkt(&frmhdr, NULL, frmhdr.wi_rx_signal); 1573 1574 /* 1575 * Drop undecryptable or packets with receive errors here 1576 */ 1577 status = le16toh(frmhdr.wi_status); 1578 if ((status & WI_STAT_ERRSTAT) != 0 && 1579 ic->ic_opmode != IEEE80211_M_MONITOR) { 1580 ifp->if_ierrors++; 1581 DPRINTF(("wi_rx_intr: fid %x error status %x\n", fid, status)); 1582 return; 1583 } 1584 rssi = frmhdr.wi_rx_signal; 1585 rstamp = (le16toh(frmhdr.wi_rx_tstamp0) << 16) | 1586 le16toh(frmhdr.wi_rx_tstamp1); 1587 1588 len = le16toh(frmhdr.wi_dat_len); 1589 off = ALIGN(sizeof(struct ieee80211_frame)); 1590 1591 /* Sometimes the PRISM2.x returns bogusly large frames. Except 1592 * in monitor mode, just throw them away. 1593 */ 1594 if (off + len > MCLBYTES) { 1595 if (ic->ic_opmode != IEEE80211_M_MONITOR) { 1596 ifp->if_ierrors++; 1597 DPRINTF(("wi_rx_intr: oversized packet\n")); 1598 return; 1599 } else 1600 len = 0; 1601 } 1602 1603 MGETHDR(m, M_DONTWAIT, MT_DATA); 1604 if (m == NULL) { 1605 ifp->if_ierrors++; 1606 DPRINTF(("wi_rx_intr: MGET failed\n")); 1607 return; 1608 } 1609 if (off + len > MHLEN) { 1610 MCLGET(m, M_DONTWAIT); 1611 if ((m->m_flags & M_EXT) == 0) { 1612 m_freem(m); 1613 ifp->if_ierrors++; 1614 DPRINTF(("wi_rx_intr: MCLGET failed\n")); 1615 return; 1616 } 1617 } 1618 1619 m->m_data += off - sizeof(struct ieee80211_frame); 1620 memcpy(m->m_data, &frmhdr.wi_whdr, sizeof(struct ieee80211_frame)); 1621 wi_read_bap(sc, fid, sizeof(frmhdr), 1622 m->m_data + sizeof(struct ieee80211_frame), len); 1623 m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame) + len; 1624 m->m_pkthdr.rcvif = ifp; 1625 1626 wh = mtod(m, struct ieee80211_frame *); 1627 if (wh->i_fc[1] & IEEE80211_FC1_WEP) { 1628 /* 1629 * WEP is decrypted by hardware. Clear WEP bit 1630 * header for ieee80211_input(). 1631 */ 1632 wh->i_fc[1] &= ~IEEE80211_FC1_WEP; 1633 } 1634 #if NBPFILTER > 0 1635 if (sc->sc_drvbpf) { 1636 struct wi_rx_radiotap_header *tap = &sc->sc_rxtap; 1637 1638 tap->wr_rate = frmhdr.wi_rx_rate / 5; 1639 tap->wr_antsignal = frmhdr.wi_rx_signal; 1640 tap->wr_antnoise = frmhdr.wi_rx_silence; 1641 tap->wr_chan_freq = htole16(ic->ic_bss->ni_chan->ic_freq); 1642 tap->wr_chan_flags = htole16(ic->ic_bss->ni_chan->ic_flags); 1643 if (frmhdr.wi_status & WI_STAT_PCF) 1644 tap->wr_flags |= IEEE80211_RADIOTAP_F_CFP; 1645 1646 /* XXX IEEE80211_RADIOTAP_F_WEP */ 1647 bpf_mtap2(sc->sc_drvbpf, tap, tap->wr_ihdr.it_len, m); 1648 } 1649 #endif 1650 1651 /* synchronize driver's BSSID with firmware's BSSID */ 1652 dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK; 1653 if (ic->ic_opmode == IEEE80211_M_IBSS && dir == IEEE80211_FC1_DIR_NODS) 1654 wi_sync_bssid(sc, wh->i_addr3); 1655 1656 ni = ieee80211_find_rxnode(ic, mtod(m, struct ieee80211_frame_min *)); 1657 1658 ieee80211_input(ic, m, ni, rssi, rstamp); 1659 1660 wi_rssadapt_input(ic, ni, wh, rssi); 1661 1662 /* 1663 * The frame may have caused the node to be marked for 1664 * reclamation (e.g. in response to a DEAUTH message) 1665 * so use release_node here instead of unref_node. 1666 */ 1667 ieee80211_free_node(ni); 1668 } 1669 1670 STATIC void 1671 wi_tx_ex_intr(struct wi_softc *sc) 1672 { 1673 struct ieee80211com *ic = &sc->sc_ic; 1674 struct ifnet *ifp = &sc->sc_if; 1675 struct ieee80211_node *ni; 1676 struct ieee80211_rssdesc *id; 1677 struct wi_rssdesc *rssd; 1678 struct wi_frame frmhdr; 1679 int fid; 1680 u_int16_t status; 1681 1682 fid = CSR_READ_2(sc, WI_TX_CMP_FID); 1683 /* Read in the frame header */ 1684 if (wi_read_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr)) != 0) { 1685 printf("%s: %s read fid %x failed\n", sc->sc_dev.dv_xname, 1686 __func__, fid); 1687 wi_rssdescs_reset(ic, &sc->sc_rssd, &sc->sc_rssdfree, 1688 &sc->sc_txpending); 1689 goto out; 1690 } 1691 1692 if (frmhdr.wi_tx_idx >= WI_NTXRSS) { 1693 printf("%s: %s bad idx %02x\n", 1694 sc->sc_dev.dv_xname, __func__, frmhdr.wi_tx_idx); 1695 wi_rssdescs_reset(ic, &sc->sc_rssd, &sc->sc_rssdfree, 1696 &sc->sc_txpending); 1697 goto out; 1698 } 1699 1700 status = le16toh(frmhdr.wi_status); 1701 1702 /* 1703 * Spontaneous station disconnects appear as xmit 1704 * errors. Don't announce them and/or count them 1705 * as an output error. 1706 */ 1707 if (ppsratecheck(&lasttxerror, &curtxeps, wi_txerate)) { 1708 printf("%s: tx failed", sc->sc_dev.dv_xname); 1709 if (status & WI_TXSTAT_RET_ERR) 1710 printf(", retry limit exceeded"); 1711 if (status & WI_TXSTAT_AGED_ERR) 1712 printf(", max transmit lifetime exceeded"); 1713 if (status & WI_TXSTAT_DISCONNECT) 1714 printf(", port disconnected"); 1715 if (status & WI_TXSTAT_FORM_ERR) 1716 printf(", invalid format (data len %u src %s)", 1717 le16toh(frmhdr.wi_dat_len), 1718 ether_sprintf(frmhdr.wi_ehdr.ether_shost)); 1719 if (status & ~0xf) 1720 printf(", status=0x%x", status); 1721 printf("\n"); 1722 } 1723 ifp->if_oerrors++; 1724 rssd = &sc->sc_rssd[frmhdr.wi_tx_idx]; 1725 id = &rssd->rd_desc; 1726 if ((status & WI_TXSTAT_RET_ERR) != 0) 1727 wi_lower_rate(ic, id); 1728 1729 ni = id->id_node; 1730 id->id_node = NULL; 1731 1732 if (ni == NULL) { 1733 printf("%s: %s null node, rssdesc %02x\n", 1734 sc->sc_dev.dv_xname, __func__, frmhdr.wi_tx_idx); 1735 goto out; 1736 } 1737 1738 if (sc->sc_txpending[id->id_rateidx]-- == 0) { 1739 printf("%s: %s txpending[%i] wraparound", sc->sc_dev.dv_xname, 1740 __func__, id->id_rateidx); 1741 sc->sc_txpending[id->id_rateidx] = 0; 1742 } 1743 if (ni != NULL) 1744 ieee80211_free_node(ni); 1745 SLIST_INSERT_HEAD(&sc->sc_rssdfree, rssd, rd_next); 1746 out: 1747 ifp->if_flags &= ~IFF_OACTIVE; 1748 } 1749 1750 STATIC void 1751 wi_txalloc_intr(struct wi_softc *sc) 1752 { 1753 int fid, cur; 1754 1755 fid = CSR_READ_2(sc, WI_ALLOC_FID); 1756 1757 cur = sc->sc_txalloc; 1758 #ifdef DIAGNOSTIC 1759 if (sc->sc_txstarted == 0) { 1760 printf("%s: spurious alloc %x != %x, alloc %d queue %d start %d alloced %d queued %d started %d\n", 1761 sc->sc_dev.dv_xname, fid, sc->sc_txd[cur].d_fid, cur, 1762 sc->sc_txqueue, sc->sc_txstart, sc->sc_txalloced, sc->sc_txqueued, sc->sc_txstarted); 1763 return; 1764 } 1765 #endif 1766 --sc->sc_txstarted; 1767 ++sc->sc_txalloced; 1768 sc->sc_txd[cur].d_fid = fid; 1769 sc->sc_txalloc = (cur + 1) % WI_NTXBUF; 1770 #ifdef WI_RING_DEBUG 1771 printf("%s: alloc %04x, alloc %d queue %d start %d alloced %d queued %d started %d\n", 1772 sc->sc_dev.dv_xname, fid, 1773 sc->sc_txalloc, sc->sc_txqueue, sc->sc_txstart, 1774 sc->sc_txalloced, sc->sc_txqueued, sc->sc_txstarted); 1775 #endif 1776 } 1777 1778 STATIC void 1779 wi_cmd_intr(struct wi_softc *sc) 1780 { 1781 struct ifnet *ifp = &sc->sc_if; 1782 1783 if (sc->sc_invalid) 1784 return; 1785 #ifdef WI_DEBUG 1786 if (wi_debug > 1) 1787 printf("%s: %d txcmds outstanding\n", __func__, sc->sc_txcmds); 1788 #endif 1789 KASSERT(sc->sc_txcmds > 0); 1790 1791 --sc->sc_txcmds; 1792 1793 if (--sc->sc_txqueued == 0) { 1794 sc->sc_tx_timer = 0; 1795 ifp->if_flags &= ~IFF_OACTIVE; 1796 #ifdef WI_RING_DEBUG 1797 printf("%s: cmd , alloc %d queue %d start %d alloced %d queued %d started %d\n", 1798 sc->sc_dev.dv_xname, 1799 sc->sc_txalloc, sc->sc_txqueue, sc->sc_txstart, 1800 sc->sc_txalloced, sc->sc_txqueued, sc->sc_txstarted); 1801 #endif 1802 } else 1803 wi_push_packet(sc); 1804 } 1805 1806 STATIC void 1807 wi_push_packet(struct wi_softc *sc) 1808 { 1809 struct ifnet *ifp = &sc->sc_if; 1810 int cur, fid; 1811 1812 cur = sc->sc_txstart; 1813 fid = sc->sc_txd[cur].d_fid; 1814 1815 KASSERT(sc->sc_txcmds == 0); 1816 1817 if (wi_cmd_start(sc, WI_CMD_TX | WI_RECLAIM, fid, 0, 0)) { 1818 printf("%s: xmit failed\n", sc->sc_dev.dv_xname); 1819 /* XXX ring might have a hole */ 1820 } 1821 1822 if (sc->sc_txcmds++ > 0) 1823 printf("%s: %d tx cmds pending!!!\n", __func__, sc->sc_txcmds); 1824 1825 ++sc->sc_txstarted; 1826 #ifdef DIAGNOSTIC 1827 if (sc->sc_txstarted > WI_NTXBUF) 1828 printf("%s: too many buffers started\n", sc->sc_dev.dv_xname); 1829 #endif 1830 sc->sc_txstart = (cur + 1) % WI_NTXBUF; 1831 sc->sc_tx_timer = 5; 1832 ifp->if_timer = 1; 1833 #ifdef WI_RING_DEBUG 1834 printf("%s: push %04x, alloc %d queue %d start %d alloced %d queued %d started %d\n", 1835 sc->sc_dev.dv_xname, fid, 1836 sc->sc_txalloc, sc->sc_txqueue, sc->sc_txstart, 1837 sc->sc_txalloced, sc->sc_txqueued, sc->sc_txstarted); 1838 #endif 1839 } 1840 1841 STATIC void 1842 wi_tx_intr(struct wi_softc *sc) 1843 { 1844 struct ieee80211com *ic = &sc->sc_ic; 1845 struct ifnet *ifp = &sc->sc_if; 1846 struct ieee80211_node *ni; 1847 struct ieee80211_rssdesc *id; 1848 struct wi_rssdesc *rssd; 1849 struct wi_frame frmhdr; 1850 int fid; 1851 1852 fid = CSR_READ_2(sc, WI_TX_CMP_FID); 1853 /* Read in the frame header */ 1854 if (wi_read_bap(sc, fid, offsetof(struct wi_frame, wi_tx_swsup2), 1855 &frmhdr.wi_tx_swsup2, 2) != 0) { 1856 printf("%s: %s read fid %x failed\n", sc->sc_dev.dv_xname, 1857 __func__, fid); 1858 wi_rssdescs_reset(ic, &sc->sc_rssd, &sc->sc_rssdfree, 1859 &sc->sc_txpending); 1860 goto out; 1861 } 1862 1863 if (frmhdr.wi_tx_idx >= WI_NTXRSS) { 1864 printf("%s: %s bad idx %02x\n", 1865 sc->sc_dev.dv_xname, __func__, frmhdr.wi_tx_idx); 1866 wi_rssdescs_reset(ic, &sc->sc_rssd, &sc->sc_rssdfree, 1867 &sc->sc_txpending); 1868 goto out; 1869 } 1870 1871 rssd = &sc->sc_rssd[frmhdr.wi_tx_idx]; 1872 id = &rssd->rd_desc; 1873 wi_raise_rate(ic, id); 1874 1875 ni = id->id_node; 1876 id->id_node = NULL; 1877 1878 if (ni == NULL) { 1879 printf("%s: %s null node, rssdesc %02x\n", 1880 sc->sc_dev.dv_xname, __func__, frmhdr.wi_tx_idx); 1881 goto out; 1882 } 1883 1884 if (sc->sc_txpending[id->id_rateidx]-- == 0) { 1885 printf("%s: %s txpending[%i] wraparound", sc->sc_dev.dv_xname, 1886 __func__, id->id_rateidx); 1887 sc->sc_txpending[id->id_rateidx] = 0; 1888 } 1889 if (ni != NULL) 1890 ieee80211_free_node(ni); 1891 SLIST_INSERT_HEAD(&sc->sc_rssdfree, rssd, rd_next); 1892 out: 1893 ifp->if_flags &= ~IFF_OACTIVE; 1894 } 1895 1896 STATIC void 1897 wi_info_intr(struct wi_softc *sc) 1898 { 1899 struct ieee80211com *ic = &sc->sc_ic; 1900 struct ifnet *ifp = &sc->sc_if; 1901 int i, fid, len, off; 1902 u_int16_t ltbuf[2]; 1903 u_int16_t stat; 1904 u_int32_t *ptr; 1905 1906 fid = CSR_READ_2(sc, WI_INFO_FID); 1907 wi_read_bap(sc, fid, 0, ltbuf, sizeof(ltbuf)); 1908 1909 switch (le16toh(ltbuf[1])) { 1910 1911 case WI_INFO_LINK_STAT: 1912 wi_read_bap(sc, fid, sizeof(ltbuf), &stat, sizeof(stat)); 1913 DPRINTF(("wi_info_intr: LINK_STAT 0x%x\n", le16toh(stat))); 1914 switch (le16toh(stat)) { 1915 case CONNECTED: 1916 sc->sc_flags &= ~WI_FLAGS_OUTRANGE; 1917 if (ic->ic_state == IEEE80211_S_RUN && 1918 ic->ic_opmode != IEEE80211_M_IBSS) 1919 break; 1920 /* FALLTHROUGH */ 1921 case AP_CHANGE: 1922 ieee80211_new_state(ic, IEEE80211_S_RUN, -1); 1923 break; 1924 case AP_IN_RANGE: 1925 sc->sc_flags &= ~WI_FLAGS_OUTRANGE; 1926 break; 1927 case AP_OUT_OF_RANGE: 1928 if (sc->sc_firmware_type == WI_SYMBOL && 1929 sc->sc_scan_timer > 0) { 1930 if (wi_cmd(sc, WI_CMD_INQUIRE, 1931 WI_INFO_HOST_SCAN_RESULTS, 0, 0) != 0) 1932 sc->sc_scan_timer = 0; 1933 break; 1934 } 1935 if (ic->ic_opmode == IEEE80211_M_STA) 1936 sc->sc_flags |= WI_FLAGS_OUTRANGE; 1937 break; 1938 case DISCONNECTED: 1939 case ASSOC_FAILED: 1940 if (ic->ic_opmode == IEEE80211_M_STA) 1941 ieee80211_new_state(ic, IEEE80211_S_INIT, -1); 1942 break; 1943 } 1944 break; 1945 1946 case WI_INFO_COUNTERS: 1947 /* some card versions have a larger stats structure */ 1948 len = min(le16toh(ltbuf[0]) - 1, sizeof(sc->sc_stats) / 4); 1949 ptr = (u_int32_t *)&sc->sc_stats; 1950 off = sizeof(ltbuf); 1951 for (i = 0; i < len; i++, off += 2, ptr++) { 1952 wi_read_bap(sc, fid, off, &stat, sizeof(stat)); 1953 stat = le16toh(stat); 1954 #ifdef WI_HERMES_STATS_WAR 1955 if (stat & 0xf000) 1956 stat = ~stat; 1957 #endif 1958 *ptr += stat; 1959 } 1960 ifp->if_collisions = sc->sc_stats.wi_tx_single_retries + 1961 sc->sc_stats.wi_tx_multi_retries + 1962 sc->sc_stats.wi_tx_retry_limit; 1963 break; 1964 1965 case WI_INFO_SCAN_RESULTS: 1966 case WI_INFO_HOST_SCAN_RESULTS: 1967 wi_scan_result(sc, fid, le16toh(ltbuf[0])); 1968 break; 1969 1970 default: 1971 DPRINTF(("wi_info_intr: got fid %x type %x len %d\n", fid, 1972 le16toh(ltbuf[1]), le16toh(ltbuf[0]))); 1973 break; 1974 } 1975 } 1976 1977 STATIC int 1978 wi_write_multi(struct wi_softc *sc) 1979 { 1980 struct ifnet *ifp = &sc->sc_if; 1981 int n; 1982 struct wi_mcast mlist; 1983 struct ether_multi *enm; 1984 struct ether_multistep estep; 1985 1986 if ((ifp->if_flags & IFF_PROMISC) != 0) { 1987 allmulti: 1988 ifp->if_flags |= IFF_ALLMULTI; 1989 memset(&mlist, 0, sizeof(mlist)); 1990 return wi_write_rid(sc, WI_RID_MCAST_LIST, &mlist, 1991 sizeof(mlist)); 1992 } 1993 1994 n = 0; 1995 ETHER_FIRST_MULTI(estep, &sc->sc_ec, enm); 1996 while (enm != NULL) { 1997 /* Punt on ranges or too many multicast addresses. */ 1998 if (!IEEE80211_ADDR_EQ(enm->enm_addrlo, enm->enm_addrhi) || 1999 n >= sizeof(mlist) / sizeof(mlist.wi_mcast[0])) 2000 goto allmulti; 2001 2002 IEEE80211_ADDR_COPY(&mlist.wi_mcast[n], enm->enm_addrlo); 2003 n++; 2004 ETHER_NEXT_MULTI(estep, enm); 2005 } 2006 ifp->if_flags &= ~IFF_ALLMULTI; 2007 return wi_write_rid(sc, WI_RID_MCAST_LIST, &mlist, 2008 IEEE80211_ADDR_LEN * n); 2009 } 2010 2011 2012 STATIC void 2013 wi_read_nicid(struct wi_softc *sc) 2014 { 2015 struct wi_card_ident *id; 2016 char *p; 2017 int len; 2018 u_int16_t ver[4]; 2019 2020 /* getting chip identity */ 2021 memset(ver, 0, sizeof(ver)); 2022 len = sizeof(ver); 2023 wi_read_rid(sc, WI_RID_CARD_ID, ver, &len); 2024 printf("%s: using ", sc->sc_dev.dv_xname); 2025 DPRINTF2(("wi_read_nicid: CARD_ID: %x %x %x %x\n", le16toh(ver[0]), le16toh(ver[1]), le16toh(ver[2]), le16toh(ver[3]))); 2026 2027 sc->sc_firmware_type = WI_NOTYPE; 2028 for (id = wi_card_ident; id->card_name != NULL; id++) { 2029 if (le16toh(ver[0]) == id->card_id) { 2030 printf("%s", id->card_name); 2031 sc->sc_firmware_type = id->firm_type; 2032 break; 2033 } 2034 } 2035 if (sc->sc_firmware_type == WI_NOTYPE) { 2036 if (le16toh(ver[0]) & 0x8000) { 2037 printf("Unknown PRISM2 chip"); 2038 sc->sc_firmware_type = WI_INTERSIL; 2039 } else { 2040 printf("Unknown Lucent chip"); 2041 sc->sc_firmware_type = WI_LUCENT; 2042 } 2043 } 2044 2045 /* get primary firmware version (Only Prism chips) */ 2046 if (sc->sc_firmware_type != WI_LUCENT) { 2047 memset(ver, 0, sizeof(ver)); 2048 len = sizeof(ver); 2049 wi_read_rid(sc, WI_RID_PRI_IDENTITY, ver, &len); 2050 sc->sc_pri_firmware_ver = le16toh(ver[2]) * 10000 + 2051 le16toh(ver[3]) * 100 + le16toh(ver[1]); 2052 } 2053 2054 /* get station firmware version */ 2055 memset(ver, 0, sizeof(ver)); 2056 len = sizeof(ver); 2057 wi_read_rid(sc, WI_RID_STA_IDENTITY, ver, &len); 2058 sc->sc_sta_firmware_ver = le16toh(ver[2]) * 10000 + 2059 le16toh(ver[3]) * 100 + le16toh(ver[1]); 2060 if (sc->sc_firmware_type == WI_INTERSIL && 2061 (sc->sc_sta_firmware_ver == 10102 || 2062 sc->sc_sta_firmware_ver == 20102)) { 2063 char ident[12]; 2064 memset(ident, 0, sizeof(ident)); 2065 len = sizeof(ident); 2066 /* value should be the format like "V2.00-11" */ 2067 if (wi_read_rid(sc, WI_RID_SYMBOL_IDENTITY, ident, &len) == 0 && 2068 *(p = (char *)ident) >= 'A' && 2069 p[2] == '.' && p[5] == '-' && p[8] == '\0') { 2070 sc->sc_firmware_type = WI_SYMBOL; 2071 sc->sc_sta_firmware_ver = (p[1] - '0') * 10000 + 2072 (p[3] - '0') * 1000 + (p[4] - '0') * 100 + 2073 (p[6] - '0') * 10 + (p[7] - '0'); 2074 } 2075 } 2076 2077 printf("\n%s: %s Firmware: ", sc->sc_dev.dv_xname, 2078 sc->sc_firmware_type == WI_LUCENT ? "Lucent" : 2079 (sc->sc_firmware_type == WI_SYMBOL ? "Symbol" : "Intersil")); 2080 if (sc->sc_firmware_type != WI_LUCENT) /* XXX */ 2081 printf("Primary (%u.%u.%u), ", 2082 sc->sc_pri_firmware_ver / 10000, 2083 (sc->sc_pri_firmware_ver % 10000) / 100, 2084 sc->sc_pri_firmware_ver % 100); 2085 printf("Station (%u.%u.%u)\n", 2086 sc->sc_sta_firmware_ver / 10000, 2087 (sc->sc_sta_firmware_ver % 10000) / 100, 2088 sc->sc_sta_firmware_ver % 100); 2089 } 2090 2091 STATIC int 2092 wi_write_ssid(struct wi_softc *sc, int rid, u_int8_t *buf, int buflen) 2093 { 2094 struct wi_ssid ssid; 2095 2096 if (buflen > IEEE80211_NWID_LEN) 2097 return ENOBUFS; 2098 memset(&ssid, 0, sizeof(ssid)); 2099 ssid.wi_len = htole16(buflen); 2100 memcpy(ssid.wi_ssid, buf, buflen); 2101 return wi_write_rid(sc, rid, &ssid, sizeof(ssid)); 2102 } 2103 2104 STATIC int 2105 wi_get_cfg(struct ifnet *ifp, u_long cmd, void *data) 2106 { 2107 struct wi_softc *sc = ifp->if_softc; 2108 struct ieee80211com *ic = &sc->sc_ic; 2109 struct ifreq *ifr = (struct ifreq *)data; 2110 struct wi_req wreq; 2111 int len, n, error; 2112 2113 error = copyin(ifr->ifr_data, &wreq, sizeof(wreq)); 2114 if (error) 2115 return error; 2116 len = (wreq.wi_len - 1) * 2; 2117 if (len < sizeof(u_int16_t)) 2118 return ENOSPC; 2119 if (len > sizeof(wreq.wi_val)) 2120 len = sizeof(wreq.wi_val); 2121 2122 switch (wreq.wi_type) { 2123 2124 case WI_RID_IFACE_STATS: 2125 memcpy(wreq.wi_val, &sc->sc_stats, sizeof(sc->sc_stats)); 2126 if (len < sizeof(sc->sc_stats)) 2127 error = ENOSPC; 2128 else 2129 len = sizeof(sc->sc_stats); 2130 break; 2131 2132 case WI_RID_ENCRYPTION: 2133 case WI_RID_TX_CRYPT_KEY: 2134 case WI_RID_DEFLT_CRYPT_KEYS: 2135 case WI_RID_TX_RATE: 2136 return ieee80211_cfgget(ic, cmd, data); 2137 2138 case WI_RID_MICROWAVE_OVEN: 2139 if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_MOR)) { 2140 error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val, 2141 &len); 2142 break; 2143 } 2144 wreq.wi_val[0] = htole16(sc->sc_microwave_oven); 2145 len = sizeof(u_int16_t); 2146 break; 2147 2148 case WI_RID_DBM_ADJUST: 2149 if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_DBMADJUST)) { 2150 error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val, 2151 &len); 2152 break; 2153 } 2154 wreq.wi_val[0] = htole16(sc->sc_dbm_offset); 2155 len = sizeof(u_int16_t); 2156 break; 2157 2158 case WI_RID_ROAMING_MODE: 2159 if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_ROAMING)) { 2160 error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val, 2161 &len); 2162 break; 2163 } 2164 wreq.wi_val[0] = htole16(sc->sc_roaming_mode); 2165 len = sizeof(u_int16_t); 2166 break; 2167 2168 case WI_RID_SYSTEM_SCALE: 2169 if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_SYSSCALE)) { 2170 error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val, 2171 &len); 2172 break; 2173 } 2174 wreq.wi_val[0] = htole16(sc->sc_system_scale); 2175 len = sizeof(u_int16_t); 2176 break; 2177 2178 case WI_RID_FRAG_THRESH: 2179 if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR)) { 2180 error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val, 2181 &len); 2182 break; 2183 } 2184 wreq.wi_val[0] = htole16(sc->sc_frag_thresh); 2185 len = sizeof(u_int16_t); 2186 break; 2187 2188 case WI_RID_READ_APS: 2189 #ifndef IEEE80211_NO_HOSTAP 2190 if (ic->ic_opmode == IEEE80211_M_HOSTAP) 2191 return ieee80211_cfgget(ic, cmd, data); 2192 #endif /* !IEEE80211_NO_HOSTAP */ 2193 if (sc->sc_scan_timer > 0) { 2194 error = EINPROGRESS; 2195 break; 2196 } 2197 n = sc->sc_naps; 2198 if (len < sizeof(n)) { 2199 error = ENOSPC; 2200 break; 2201 } 2202 if (len < sizeof(n) + sizeof(struct wi_apinfo) * n) 2203 n = (len - sizeof(n)) / sizeof(struct wi_apinfo); 2204 len = sizeof(n) + sizeof(struct wi_apinfo) * n; 2205 memcpy(wreq.wi_val, &n, sizeof(n)); 2206 memcpy((char *)wreq.wi_val + sizeof(n), sc->sc_aps, 2207 sizeof(struct wi_apinfo) * n); 2208 break; 2209 2210 default: 2211 if (sc->sc_enabled) { 2212 error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val, 2213 &len); 2214 break; 2215 } 2216 switch (wreq.wi_type) { 2217 case WI_RID_MAX_DATALEN: 2218 wreq.wi_val[0] = htole16(sc->sc_max_datalen); 2219 len = sizeof(u_int16_t); 2220 break; 2221 case WI_RID_FRAG_THRESH: 2222 wreq.wi_val[0] = htole16(sc->sc_frag_thresh); 2223 len = sizeof(u_int16_t); 2224 break; 2225 case WI_RID_RTS_THRESH: 2226 wreq.wi_val[0] = htole16(sc->sc_rts_thresh); 2227 len = sizeof(u_int16_t); 2228 break; 2229 case WI_RID_CNFAUTHMODE: 2230 wreq.wi_val[0] = htole16(sc->sc_cnfauthmode); 2231 len = sizeof(u_int16_t); 2232 break; 2233 case WI_RID_NODENAME: 2234 if (len < sc->sc_nodelen + sizeof(u_int16_t)) { 2235 error = ENOSPC; 2236 break; 2237 } 2238 len = sc->sc_nodelen + sizeof(u_int16_t); 2239 wreq.wi_val[0] = htole16((sc->sc_nodelen + 1) / 2); 2240 memcpy(&wreq.wi_val[1], sc->sc_nodename, 2241 sc->sc_nodelen); 2242 break; 2243 default: 2244 return ieee80211_cfgget(ic, cmd, data); 2245 } 2246 break; 2247 } 2248 if (error) 2249 return error; 2250 wreq.wi_len = (len + 1) / 2 + 1; 2251 return copyout(&wreq, ifr->ifr_data, (wreq.wi_len + 1) * 2); 2252 } 2253 2254 STATIC int 2255 wi_set_cfg(struct ifnet *ifp, u_long cmd, void *data) 2256 { 2257 struct wi_softc *sc = ifp->if_softc; 2258 struct ieee80211com *ic = &sc->sc_ic; 2259 struct ifreq *ifr = (struct ifreq *)data; 2260 struct ieee80211_rateset *rs = &ic->ic_sup_rates[IEEE80211_MODE_11B]; 2261 struct wi_req wreq; 2262 struct mbuf *m; 2263 int i, len, error; 2264 2265 error = copyin(ifr->ifr_data, &wreq, sizeof(wreq)); 2266 if (error) 2267 return error; 2268 len = (wreq.wi_len - 1) * 2; 2269 switch (wreq.wi_type) { 2270 case WI_RID_MAC_NODE: 2271 (void)memcpy(ic->ic_myaddr, wreq.wi_val, ETHER_ADDR_LEN); 2272 IEEE80211_ADDR_COPY(LLADDR(ifp->if_sadl),ic->ic_myaddr); 2273 wi_write_rid(sc, WI_RID_MAC_NODE, ic->ic_myaddr, 2274 IEEE80211_ADDR_LEN); 2275 break; 2276 2277 case WI_RID_DBM_ADJUST: 2278 return ENODEV; 2279 2280 case WI_RID_NODENAME: 2281 if (le16toh(wreq.wi_val[0]) * 2 > len || 2282 le16toh(wreq.wi_val[0]) > sizeof(sc->sc_nodename)) { 2283 error = ENOSPC; 2284 break; 2285 } 2286 if (sc->sc_enabled) { 2287 error = wi_write_rid(sc, wreq.wi_type, wreq.wi_val, 2288 len); 2289 if (error) 2290 break; 2291 } 2292 sc->sc_nodelen = le16toh(wreq.wi_val[0]) * 2; 2293 memcpy(sc->sc_nodename, &wreq.wi_val[1], sc->sc_nodelen); 2294 break; 2295 2296 case WI_RID_MICROWAVE_OVEN: 2297 case WI_RID_ROAMING_MODE: 2298 case WI_RID_SYSTEM_SCALE: 2299 case WI_RID_FRAG_THRESH: 2300 if (wreq.wi_type == WI_RID_MICROWAVE_OVEN && 2301 (sc->sc_flags & WI_FLAGS_HAS_MOR) == 0) 2302 break; 2303 if (wreq.wi_type == WI_RID_ROAMING_MODE && 2304 (sc->sc_flags & WI_FLAGS_HAS_ROAMING) == 0) 2305 break; 2306 if (wreq.wi_type == WI_RID_SYSTEM_SCALE && 2307 (sc->sc_flags & WI_FLAGS_HAS_SYSSCALE) == 0) 2308 break; 2309 if (wreq.wi_type == WI_RID_FRAG_THRESH && 2310 (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR) == 0) 2311 break; 2312 /* FALLTHROUGH */ 2313 case WI_RID_RTS_THRESH: 2314 case WI_RID_CNFAUTHMODE: 2315 case WI_RID_MAX_DATALEN: 2316 if (sc->sc_enabled) { 2317 error = wi_write_rid(sc, wreq.wi_type, wreq.wi_val, 2318 sizeof(u_int16_t)); 2319 if (error) 2320 break; 2321 } 2322 switch (wreq.wi_type) { 2323 case WI_RID_FRAG_THRESH: 2324 sc->sc_frag_thresh = le16toh(wreq.wi_val[0]); 2325 break; 2326 case WI_RID_RTS_THRESH: 2327 sc->sc_rts_thresh = le16toh(wreq.wi_val[0]); 2328 break; 2329 case WI_RID_MICROWAVE_OVEN: 2330 sc->sc_microwave_oven = le16toh(wreq.wi_val[0]); 2331 break; 2332 case WI_RID_ROAMING_MODE: 2333 sc->sc_roaming_mode = le16toh(wreq.wi_val[0]); 2334 break; 2335 case WI_RID_SYSTEM_SCALE: 2336 sc->sc_system_scale = le16toh(wreq.wi_val[0]); 2337 break; 2338 case WI_RID_CNFAUTHMODE: 2339 sc->sc_cnfauthmode = le16toh(wreq.wi_val[0]); 2340 break; 2341 case WI_RID_MAX_DATALEN: 2342 sc->sc_max_datalen = le16toh(wreq.wi_val[0]); 2343 break; 2344 } 2345 break; 2346 2347 case WI_RID_TX_RATE: 2348 switch (le16toh(wreq.wi_val[0])) { 2349 case 3: 2350 ic->ic_fixed_rate = -1; 2351 break; 2352 default: 2353 for (i = 0; i < IEEE80211_RATE_SIZE; i++) { 2354 if ((rs->rs_rates[i] & IEEE80211_RATE_VAL) 2355 / 2 == le16toh(wreq.wi_val[0])) 2356 break; 2357 } 2358 if (i == IEEE80211_RATE_SIZE) 2359 return EINVAL; 2360 ic->ic_fixed_rate = i; 2361 } 2362 if (sc->sc_enabled) 2363 error = wi_cfg_txrate(sc); 2364 break; 2365 2366 case WI_RID_SCAN_APS: 2367 if (sc->sc_enabled && ic->ic_opmode != IEEE80211_M_HOSTAP) 2368 error = wi_scan_ap(sc, 0x3fff, 0x000f); 2369 break; 2370 2371 case WI_RID_MGMT_XMIT: 2372 if (!sc->sc_enabled) { 2373 error = ENETDOWN; 2374 break; 2375 } 2376 if (ic->ic_mgtq.ifq_len > 5) { 2377 error = EAGAIN; 2378 break; 2379 } 2380 /* XXX wi_len looks in u_int8_t, not in u_int16_t */ 2381 m = m_devget((char *)&wreq.wi_val, wreq.wi_len, 0, ifp, NULL); 2382 if (m == NULL) { 2383 error = ENOMEM; 2384 break; 2385 } 2386 IF_ENQUEUE(&ic->ic_mgtq, m); 2387 break; 2388 2389 default: 2390 if (sc->sc_enabled) { 2391 error = wi_write_rid(sc, wreq.wi_type, wreq.wi_val, 2392 len); 2393 if (error) 2394 break; 2395 } 2396 error = ieee80211_cfgset(ic, cmd, data); 2397 break; 2398 } 2399 return error; 2400 } 2401 2402 /* Rate is 0 for hardware auto-select, otherwise rate is 2403 * 2, 4, 11, or 22 (units of 500Kbps). 2404 */ 2405 STATIC int 2406 wi_write_txrate(struct wi_softc *sc, int rate) 2407 { 2408 u_int16_t hwrate; 2409 2410 /* rate: 0, 2, 4, 11, 22 */ 2411 switch (sc->sc_firmware_type) { 2412 case WI_LUCENT: 2413 switch (rate & IEEE80211_RATE_VAL) { 2414 case 2: 2415 hwrate = 1; 2416 break; 2417 case 4: 2418 hwrate = 2; 2419 break; 2420 default: 2421 hwrate = 3; /* auto */ 2422 break; 2423 case 11: 2424 hwrate = 4; 2425 break; 2426 case 22: 2427 hwrate = 5; 2428 break; 2429 } 2430 break; 2431 default: 2432 switch (rate & IEEE80211_RATE_VAL) { 2433 case 2: 2434 hwrate = 1; 2435 break; 2436 case 4: 2437 hwrate = 2; 2438 break; 2439 case 11: 2440 hwrate = 4; 2441 break; 2442 case 22: 2443 hwrate = 8; 2444 break; 2445 default: 2446 hwrate = 15; /* auto */ 2447 break; 2448 } 2449 break; 2450 } 2451 2452 if (sc->sc_tx_rate == hwrate) 2453 return 0; 2454 2455 if (sc->sc_if.if_flags & IFF_DEBUG) 2456 printf("%s: tx rate %d -> %d (%d)\n", __func__, sc->sc_tx_rate, 2457 hwrate, rate); 2458 2459 sc->sc_tx_rate = hwrate; 2460 2461 return wi_write_val(sc, WI_RID_TX_RATE, sc->sc_tx_rate); 2462 } 2463 2464 STATIC int 2465 wi_cfg_txrate(struct wi_softc *sc) 2466 { 2467 struct ieee80211com *ic = &sc->sc_ic; 2468 struct ieee80211_rateset *rs; 2469 int rate; 2470 2471 rs = &ic->ic_sup_rates[IEEE80211_MODE_11B]; 2472 2473 sc->sc_tx_rate = 0; /* force write to RID */ 2474 2475 if (ic->ic_fixed_rate < 0) 2476 rate = 0; /* auto */ 2477 else 2478 rate = rs->rs_rates[ic->ic_fixed_rate]; 2479 2480 return wi_write_txrate(sc, rate); 2481 } 2482 2483 STATIC int 2484 wi_key_delete(struct ieee80211com *ic, const struct ieee80211_key *k) 2485 { 2486 struct wi_softc *sc = ic->ic_ifp->if_softc; 2487 u_int keyix = k->wk_keyix; 2488 2489 DPRINTF(("%s: delete key %u\n", __func__, keyix)); 2490 2491 if (keyix >= IEEE80211_WEP_NKID) 2492 return 0; 2493 if (k->wk_keylen != 0) 2494 sc->sc_flags &= ~WI_FLAGS_WEP_VALID; 2495 2496 return 1; 2497 } 2498 2499 static int 2500 wi_key_set(struct ieee80211com *ic, const struct ieee80211_key *k, 2501 const u_int8_t mac[IEEE80211_ADDR_LEN]) 2502 { 2503 struct wi_softc *sc = ic->ic_ifp->if_softc; 2504 2505 DPRINTF(("%s: set key %u\n", __func__, k->wk_keyix)); 2506 2507 if (k->wk_keyix >= IEEE80211_WEP_NKID) 2508 return 0; 2509 2510 sc->sc_flags &= ~WI_FLAGS_WEP_VALID; 2511 2512 return 1; 2513 } 2514 2515 STATIC void 2516 wi_key_update_begin(struct ieee80211com *ic) 2517 { 2518 DPRINTF(("%s:\n", __func__)); 2519 } 2520 2521 STATIC void 2522 wi_key_update_end(struct ieee80211com *ic) 2523 { 2524 struct ifnet *ifp = ic->ic_ifp; 2525 struct wi_softc *sc = ifp->if_softc; 2526 2527 DPRINTF(("%s:\n", __func__)); 2528 2529 if ((sc->sc_flags & WI_FLAGS_WEP_VALID) != 0) 2530 return; 2531 if ((ic->ic_caps & IEEE80211_C_WEP) != 0 && sc->sc_enabled && 2532 !sc->sc_invalid) 2533 (void)wi_write_wep(sc); 2534 } 2535 2536 STATIC int 2537 wi_write_wep(struct wi_softc *sc) 2538 { 2539 struct ifnet *ifp = &sc->sc_if; 2540 struct ieee80211com *ic = &sc->sc_ic; 2541 int error = 0; 2542 int i, keylen; 2543 u_int16_t val; 2544 struct wi_key wkey[IEEE80211_WEP_NKID]; 2545 2546 if ((ifp->if_flags & IFF_RUNNING) != 0) 2547 wi_cmd(sc, WI_CMD_DISABLE | sc->sc_portnum, 0, 0, 0); 2548 2549 switch (sc->sc_firmware_type) { 2550 case WI_LUCENT: 2551 val = (ic->ic_flags & IEEE80211_F_PRIVACY) ? 1 : 0; 2552 error = wi_write_val(sc, WI_RID_ENCRYPTION, val); 2553 if (error) 2554 break; 2555 error = wi_write_val(sc, WI_RID_TX_CRYPT_KEY, ic->ic_def_txkey); 2556 if (error) 2557 break; 2558 memset(wkey, 0, sizeof(wkey)); 2559 for (i = 0; i < IEEE80211_WEP_NKID; i++) { 2560 keylen = ic->ic_nw_keys[i].wk_keylen; 2561 wkey[i].wi_keylen = htole16(keylen); 2562 memcpy(wkey[i].wi_keydat, ic->ic_nw_keys[i].wk_key, 2563 keylen); 2564 } 2565 error = wi_write_rid(sc, WI_RID_DEFLT_CRYPT_KEYS, 2566 wkey, sizeof(wkey)); 2567 break; 2568 2569 case WI_INTERSIL: 2570 case WI_SYMBOL: 2571 if (ic->ic_flags & IEEE80211_F_PRIVACY) { 2572 /* 2573 * ONLY HWB3163 EVAL-CARD Firmware version 2574 * less than 0.8 variant2 2575 * 2576 * If promiscuous mode disable, Prism2 chip 2577 * does not work with WEP . 2578 * It is under investigation for details. 2579 * (ichiro@NetBSD.org) 2580 */ 2581 if (sc->sc_firmware_type == WI_INTERSIL && 2582 sc->sc_sta_firmware_ver < 802 ) { 2583 /* firm ver < 0.8 variant 2 */ 2584 wi_write_val(sc, WI_RID_PROMISC, 1); 2585 } 2586 wi_write_val(sc, WI_RID_CNFAUTHMODE, 2587 sc->sc_cnfauthmode); 2588 val = PRIVACY_INVOKED; 2589 if ((sc->sc_ic_flags & IEEE80211_F_DROPUNENC) != 0) 2590 val |= EXCLUDE_UNENCRYPTED; 2591 #ifndef IEEE80211_NO_HOSTAP 2592 /* 2593 * Encryption firmware has a bug for HostAP mode. 2594 */ 2595 if (sc->sc_firmware_type == WI_INTERSIL && 2596 ic->ic_opmode == IEEE80211_M_HOSTAP) 2597 val |= HOST_ENCRYPT; 2598 #endif /* !IEEE80211_NO_HOSTAP */ 2599 } else { 2600 wi_write_val(sc, WI_RID_CNFAUTHMODE, 2601 IEEE80211_AUTH_OPEN); 2602 val = HOST_ENCRYPT | HOST_DECRYPT; 2603 } 2604 error = wi_write_val(sc, WI_RID_P2_ENCRYPTION, val); 2605 if (error) 2606 break; 2607 error = wi_write_val(sc, WI_RID_P2_TX_CRYPT_KEY, 2608 ic->ic_def_txkey); 2609 if (error) 2610 break; 2611 /* 2612 * It seems that the firmware accept 104bit key only if 2613 * all the keys have 104bit length. We get the length of 2614 * the transmit key and use it for all other keys. 2615 * Perhaps we should use software WEP for such situation. 2616 */ 2617 if (ic->ic_def_txkey == IEEE80211_KEYIX_NONE || 2618 IEEE80211_KEY_UNDEFINED(ic->ic_nw_keys[ic->ic_def_txkey])) 2619 keylen = 13; /* No keys => 104bit ok */ 2620 else 2621 keylen = ic->ic_nw_keys[ic->ic_def_txkey].wk_keylen; 2622 2623 if (keylen > IEEE80211_WEP_KEYLEN) 2624 keylen = 13; /* 104bit keys */ 2625 else 2626 keylen = IEEE80211_WEP_KEYLEN; 2627 for (i = 0; i < IEEE80211_WEP_NKID; i++) { 2628 error = wi_write_rid(sc, WI_RID_P2_CRYPT_KEY0 + i, 2629 ic->ic_nw_keys[i].wk_key, keylen); 2630 if (error) 2631 break; 2632 } 2633 break; 2634 } 2635 if ((ifp->if_flags & IFF_RUNNING) != 0) 2636 wi_cmd(sc, WI_CMD_ENABLE | sc->sc_portnum, 0, 0, 0); 2637 if (error == 0) 2638 sc->sc_flags |= WI_FLAGS_WEP_VALID; 2639 return error; 2640 } 2641 2642 /* Must be called at proper protection level! */ 2643 STATIC int 2644 wi_cmd_start(struct wi_softc *sc, int cmd, int val0, int val1, int val2) 2645 { 2646 #ifdef WI_HISTOGRAM 2647 static int hist1[11]; 2648 static int hist1count; 2649 #endif 2650 int i; 2651 2652 /* wait for the busy bit to clear */ 2653 for (i = 500; i > 0; i--) { /* 5s */ 2654 if ((CSR_READ_2(sc, WI_COMMAND) & WI_CMD_BUSY) == 0) 2655 break; 2656 if (sc->sc_invalid) 2657 return ENXIO; 2658 DELAY(1000); /* 1 m sec */ 2659 } 2660 if (i == 0) { 2661 printf("%s: wi_cmd: busy bit won't clear.\n", 2662 sc->sc_dev.dv_xname); 2663 return(ETIMEDOUT); 2664 } 2665 #ifdef WI_HISTOGRAM 2666 if (i > 490) 2667 hist1[500 - i]++; 2668 else 2669 hist1[10]++; 2670 if (++hist1count == 1000) { 2671 hist1count = 0; 2672 printf("%s: hist1: %d %d %d %d %d %d %d %d %d %d %d\n", 2673 sc->sc_dev.dv_xname, 2674 hist1[0], hist1[1], hist1[2], hist1[3], hist1[4], 2675 hist1[5], hist1[6], hist1[7], hist1[8], hist1[9], 2676 hist1[10]); 2677 } 2678 #endif 2679 CSR_WRITE_2(sc, WI_PARAM0, val0); 2680 CSR_WRITE_2(sc, WI_PARAM1, val1); 2681 CSR_WRITE_2(sc, WI_PARAM2, val2); 2682 CSR_WRITE_2(sc, WI_COMMAND, cmd); 2683 2684 return 0; 2685 } 2686 2687 STATIC int 2688 wi_cmd(struct wi_softc *sc, int cmd, int val0, int val1, int val2) 2689 { 2690 int rc; 2691 2692 #ifdef WI_DEBUG 2693 if (wi_debug) { 2694 printf("%s: [enter] %d txcmds outstanding\n", __func__, 2695 sc->sc_txcmds); 2696 } 2697 #endif 2698 if (sc->sc_txcmds > 0) 2699 wi_txcmd_wait(sc); 2700 2701 if ((rc = wi_cmd_start(sc, cmd, val0, val1, val2)) != 0) 2702 return rc; 2703 2704 if (cmd == WI_CMD_INI) { 2705 /* XXX: should sleep here. */ 2706 if (sc->sc_invalid) 2707 return ENXIO; 2708 DELAY(100*1000); 2709 } 2710 rc = wi_cmd_wait(sc, cmd, val0); 2711 2712 #ifdef WI_DEBUG 2713 if (wi_debug) { 2714 printf("%s: [ ] %d txcmds outstanding\n", __func__, 2715 sc->sc_txcmds); 2716 } 2717 #endif 2718 if (sc->sc_txcmds > 0) 2719 wi_cmd_intr(sc); 2720 2721 #ifdef WI_DEBUG 2722 if (wi_debug) { 2723 printf("%s: [leave] %d txcmds outstanding\n", __func__, 2724 sc->sc_txcmds); 2725 } 2726 #endif 2727 return rc; 2728 } 2729 2730 STATIC int 2731 wi_cmd_wait(struct wi_softc *sc, int cmd, int val0) 2732 { 2733 #ifdef WI_HISTOGRAM 2734 static int hist2[11]; 2735 static int hist2count; 2736 #endif 2737 int i, status; 2738 #ifdef WI_DEBUG 2739 if (wi_debug > 1) 2740 printf("%s: cmd=%#x, arg=%#x\n", __func__, cmd, val0); 2741 #endif /* WI_DEBUG */ 2742 2743 /* wait for the cmd completed bit */ 2744 for (i = 0; i < WI_TIMEOUT; i++) { 2745 if (CSR_READ_2(sc, WI_EVENT_STAT) & WI_EV_CMD) 2746 break; 2747 if (sc->sc_invalid) 2748 return ENXIO; 2749 DELAY(WI_DELAY); 2750 } 2751 2752 #ifdef WI_HISTOGRAM 2753 if (i < 100) 2754 hist2[i/10]++; 2755 else 2756 hist2[10]++; 2757 if (++hist2count == 1000) { 2758 hist2count = 0; 2759 printf("%s: hist2: %d %d %d %d %d %d %d %d %d %d %d\n", 2760 sc->sc_dev.dv_xname, 2761 hist2[0], hist2[1], hist2[2], hist2[3], hist2[4], 2762 hist2[5], hist2[6], hist2[7], hist2[8], hist2[9], 2763 hist2[10]); 2764 } 2765 #endif 2766 2767 status = CSR_READ_2(sc, WI_STATUS); 2768 2769 if (i == WI_TIMEOUT) { 2770 printf("%s: command timed out, cmd=0x%x, arg=0x%x\n", 2771 sc->sc_dev.dv_xname, cmd, val0); 2772 return ETIMEDOUT; 2773 } 2774 2775 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_CMD); 2776 2777 if (status & WI_STAT_CMD_RESULT) { 2778 printf("%s: command failed, cmd=0x%x, arg=0x%x\n", 2779 sc->sc_dev.dv_xname, cmd, val0); 2780 return EIO; 2781 } 2782 return 0; 2783 } 2784 2785 STATIC int 2786 wi_seek_bap(struct wi_softc *sc, int id, int off) 2787 { 2788 #ifdef WI_HISTOGRAM 2789 static int hist4[11]; 2790 static int hist4count; 2791 #endif 2792 int i, status; 2793 2794 CSR_WRITE_2(sc, WI_SEL0, id); 2795 CSR_WRITE_2(sc, WI_OFF0, off); 2796 2797 for (i = 0; ; i++) { 2798 status = CSR_READ_2(sc, WI_OFF0); 2799 if ((status & WI_OFF_BUSY) == 0) 2800 break; 2801 if (i == WI_TIMEOUT) { 2802 printf("%s: timeout in wi_seek to %x/%x\n", 2803 sc->sc_dev.dv_xname, id, off); 2804 sc->sc_bap_off = WI_OFF_ERR; /* invalidate */ 2805 return ETIMEDOUT; 2806 } 2807 if (sc->sc_invalid) 2808 return ENXIO; 2809 DELAY(2); 2810 } 2811 #ifdef WI_HISTOGRAM 2812 if (i < 100) 2813 hist4[i/10]++; 2814 else 2815 hist4[10]++; 2816 if (++hist4count == 2500) { 2817 hist4count = 0; 2818 printf("%s: hist4: %d %d %d %d %d %d %d %d %d %d %d\n", 2819 sc->sc_dev.dv_xname, 2820 hist4[0], hist4[1], hist4[2], hist4[3], hist4[4], 2821 hist4[5], hist4[6], hist4[7], hist4[8], hist4[9], 2822 hist4[10]); 2823 } 2824 #endif 2825 if (status & WI_OFF_ERR) { 2826 printf("%s: failed in wi_seek to %x/%x\n", 2827 sc->sc_dev.dv_xname, id, off); 2828 sc->sc_bap_off = WI_OFF_ERR; /* invalidate */ 2829 return EIO; 2830 } 2831 sc->sc_bap_id = id; 2832 sc->sc_bap_off = off; 2833 return 0; 2834 } 2835 2836 STATIC int 2837 wi_read_bap(struct wi_softc *sc, int id, int off, void *buf, int buflen) 2838 { 2839 int error, cnt; 2840 2841 if (buflen == 0) 2842 return 0; 2843 if (id != sc->sc_bap_id || off != sc->sc_bap_off) { 2844 if ((error = wi_seek_bap(sc, id, off)) != 0) 2845 return error; 2846 } 2847 cnt = (buflen + 1) / 2; 2848 CSR_READ_MULTI_STREAM_2(sc, WI_DATA0, (u_int16_t *)buf, cnt); 2849 sc->sc_bap_off += cnt * 2; 2850 return 0; 2851 } 2852 2853 STATIC int 2854 wi_write_bap(struct wi_softc *sc, int id, int off, void *buf, int buflen) 2855 { 2856 int error, cnt; 2857 2858 if (buflen == 0) 2859 return 0; 2860 2861 #ifdef WI_HERMES_AUTOINC_WAR 2862 again: 2863 #endif 2864 if (id != sc->sc_bap_id || off != sc->sc_bap_off) { 2865 if ((error = wi_seek_bap(sc, id, off)) != 0) 2866 return error; 2867 } 2868 cnt = (buflen + 1) / 2; 2869 CSR_WRITE_MULTI_STREAM_2(sc, WI_DATA0, (u_int16_t *)buf, cnt); 2870 sc->sc_bap_off += cnt * 2; 2871 2872 #ifdef WI_HERMES_AUTOINC_WAR 2873 /* 2874 * According to the comments in the HCF Light code, there is a bug 2875 * in the Hermes (or possibly in certain Hermes firmware revisions) 2876 * where the chip's internal autoincrement counter gets thrown off 2877 * during data writes: the autoincrement is missed, causing one 2878 * data word to be overwritten and subsequent words to be written to 2879 * the wrong memory locations. The end result is that we could end 2880 * up transmitting bogus frames without realizing it. The workaround 2881 * for this is to write a couple of extra guard words after the end 2882 * of the transfer, then attempt to read then back. If we fail to 2883 * locate the guard words where we expect them, we preform the 2884 * transfer over again. 2885 */ 2886 if ((sc->sc_flags & WI_FLAGS_BUG_AUTOINC) && (id & 0xf000) == 0) { 2887 CSR_WRITE_2(sc, WI_DATA0, 0x1234); 2888 CSR_WRITE_2(sc, WI_DATA0, 0x5678); 2889 wi_seek_bap(sc, id, sc->sc_bap_off); 2890 sc->sc_bap_off = WI_OFF_ERR; /* invalidate */ 2891 if (CSR_READ_2(sc, WI_DATA0) != 0x1234 || 2892 CSR_READ_2(sc, WI_DATA0) != 0x5678) { 2893 printf("%s: detect auto increment bug, try again\n", 2894 sc->sc_dev.dv_xname); 2895 goto again; 2896 } 2897 } 2898 #endif 2899 return 0; 2900 } 2901 2902 STATIC int 2903 wi_mwrite_bap(struct wi_softc *sc, int id, int off, struct mbuf *m0, int totlen) 2904 { 2905 int error, len; 2906 struct mbuf *m; 2907 2908 for (m = m0; m != NULL && totlen > 0; m = m->m_next) { 2909 if (m->m_len == 0) 2910 continue; 2911 2912 len = min(m->m_len, totlen); 2913 2914 if (((u_long)m->m_data) % 2 != 0 || len % 2 != 0) { 2915 m_copydata(m, 0, totlen, (void *)&sc->sc_txbuf); 2916 return wi_write_bap(sc, id, off, (void *)&sc->sc_txbuf, 2917 totlen); 2918 } 2919 2920 if ((error = wi_write_bap(sc, id, off, m->m_data, len)) != 0) 2921 return error; 2922 2923 off += m->m_len; 2924 totlen -= len; 2925 } 2926 return 0; 2927 } 2928 2929 STATIC int 2930 wi_alloc_fid(struct wi_softc *sc, int len, int *idp) 2931 { 2932 int i; 2933 2934 if (wi_cmd(sc, WI_CMD_ALLOC_MEM, len, 0, 0)) { 2935 printf("%s: failed to allocate %d bytes on NIC\n", 2936 sc->sc_dev.dv_xname, len); 2937 return ENOMEM; 2938 } 2939 2940 for (i = 0; i < WI_TIMEOUT; i++) { 2941 if (CSR_READ_2(sc, WI_EVENT_STAT) & WI_EV_ALLOC) 2942 break; 2943 DELAY(1); 2944 } 2945 if (i == WI_TIMEOUT) { 2946 printf("%s: timeout in alloc\n", sc->sc_dev.dv_xname); 2947 return ETIMEDOUT; 2948 } 2949 *idp = CSR_READ_2(sc, WI_ALLOC_FID); 2950 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_ALLOC); 2951 return 0; 2952 } 2953 2954 STATIC int 2955 wi_read_rid(struct wi_softc *sc, int rid, void *buf, int *buflenp) 2956 { 2957 int error, len; 2958 u_int16_t ltbuf[2]; 2959 2960 /* Tell the NIC to enter record read mode. */ 2961 error = wi_cmd(sc, WI_CMD_ACCESS | WI_ACCESS_READ, rid, 0, 0); 2962 if (error) 2963 return error; 2964 2965 error = wi_read_bap(sc, rid, 0, ltbuf, sizeof(ltbuf)); 2966 if (error) 2967 return error; 2968 2969 if (le16toh(ltbuf[0]) == 0) 2970 return EOPNOTSUPP; 2971 if (le16toh(ltbuf[1]) != rid) { 2972 printf("%s: record read mismatch, rid=%x, got=%x\n", 2973 sc->sc_dev.dv_xname, rid, le16toh(ltbuf[1])); 2974 return EIO; 2975 } 2976 len = (le16toh(ltbuf[0]) - 1) * 2; /* already got rid */ 2977 if (*buflenp < len) { 2978 printf("%s: record buffer is too small, " 2979 "rid=%x, size=%d, len=%d\n", 2980 sc->sc_dev.dv_xname, rid, *buflenp, len); 2981 return ENOSPC; 2982 } 2983 *buflenp = len; 2984 return wi_read_bap(sc, rid, sizeof(ltbuf), buf, len); 2985 } 2986 2987 STATIC int 2988 wi_write_rid(struct wi_softc *sc, int rid, void *buf, int buflen) 2989 { 2990 int error; 2991 u_int16_t ltbuf[2]; 2992 2993 ltbuf[0] = htole16((buflen + 1) / 2 + 1); /* includes rid */ 2994 ltbuf[1] = htole16(rid); 2995 2996 error = wi_write_bap(sc, rid, 0, ltbuf, sizeof(ltbuf)); 2997 if (error) 2998 return error; 2999 error = wi_write_bap(sc, rid, sizeof(ltbuf), buf, buflen); 3000 if (error) 3001 return error; 3002 3003 return wi_cmd(sc, WI_CMD_ACCESS | WI_ACCESS_WRITE, rid, 0, 0); 3004 } 3005 3006 STATIC void 3007 wi_rssadapt_updatestats_cb(void *arg, struct ieee80211_node *ni) 3008 { 3009 struct wi_node *wn = (void*)ni; 3010 ieee80211_rssadapt_updatestats(&wn->wn_rssadapt); 3011 } 3012 3013 STATIC void 3014 wi_rssadapt_updatestats(void *arg) 3015 { 3016 struct wi_softc *sc = arg; 3017 struct ieee80211com *ic = &sc->sc_ic; 3018 ieee80211_iterate_nodes(&ic->ic_sta, wi_rssadapt_updatestats_cb, arg); 3019 if (ic->ic_opmode != IEEE80211_M_MONITOR && 3020 ic->ic_state == IEEE80211_S_RUN) 3021 callout_reset(&sc->sc_rssadapt_ch, hz / 10, 3022 wi_rssadapt_updatestats, arg); 3023 } 3024 3025 /* 3026 * In HOSTAP mode, restore IEEE80211_F_DROPUNENC when operating 3027 * with WEP enabled so that the AP drops unencoded frames at the 3028 * 802.11 layer. 3029 * 3030 * In all other modes, clear IEEE80211_F_DROPUNENC when operating 3031 * with WEP enabled so we don't drop unencoded frames at the 802.11 3032 * layer. This is necessary because we must strip the WEP bit from 3033 * the 802.11 header before passing frames to ieee80211_input 3034 * because the card has already stripped the WEP crypto header from 3035 * the packet. 3036 */ 3037 STATIC void 3038 wi_mend_flags(struct wi_softc *sc, enum ieee80211_state nstate) 3039 { 3040 struct ieee80211com *ic = &sc->sc_ic; 3041 3042 if (nstate == IEEE80211_S_RUN && 3043 (ic->ic_flags & IEEE80211_F_PRIVACY) != 0 && 3044 ic->ic_opmode != IEEE80211_M_HOSTAP) 3045 ic->ic_flags &= ~IEEE80211_F_DROPUNENC; 3046 else 3047 ic->ic_flags |= sc->sc_ic_flags; 3048 3049 DPRINTF(("%s: state %d, " 3050 "ic->ic_flags & IEEE80211_F_DROPUNENC = %#" PRIx32 ", " 3051 "sc->sc_ic_flags & IEEE80211_F_DROPUNENC = %#" PRIx32 "\n", 3052 __func__, nstate, 3053 ic->ic_flags & IEEE80211_F_DROPUNENC, 3054 sc->sc_ic_flags & IEEE80211_F_DROPUNENC)); 3055 } 3056 3057 STATIC int 3058 wi_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg) 3059 { 3060 struct ifnet *ifp = ic->ic_ifp; 3061 struct wi_softc *sc = ifp->if_softc; 3062 struct ieee80211_node *ni = ic->ic_bss; 3063 u_int16_t val; 3064 struct wi_ssid ssid; 3065 struct wi_macaddr bssid, old_bssid; 3066 enum ieee80211_state ostate; 3067 #ifdef WI_DEBUG 3068 static const char *stname[] = 3069 { "INIT", "SCAN", "AUTH", "ASSOC", "RUN" }; 3070 #endif /* WI_DEBUG */ 3071 3072 ostate = ic->ic_state; 3073 DPRINTF(("wi_newstate: %s -> %s\n", stname[ostate], stname[nstate])); 3074 3075 switch (nstate) { 3076 case IEEE80211_S_INIT: 3077 if (ic->ic_opmode != IEEE80211_M_MONITOR) 3078 callout_stop(&sc->sc_rssadapt_ch); 3079 ic->ic_flags &= ~IEEE80211_F_SIBSS; 3080 sc->sc_flags &= ~WI_FLAGS_OUTRANGE; 3081 break; 3082 3083 case IEEE80211_S_SCAN: 3084 case IEEE80211_S_AUTH: 3085 case IEEE80211_S_ASSOC: 3086 ic->ic_state = nstate; /* NB: skip normal ieee80211 handling */ 3087 wi_mend_flags(sc, nstate); 3088 return 0; 3089 3090 case IEEE80211_S_RUN: 3091 sc->sc_flags &= ~WI_FLAGS_OUTRANGE; 3092 IEEE80211_ADDR_COPY(old_bssid.wi_mac_addr, ni->ni_bssid); 3093 wi_read_xrid(sc, WI_RID_CURRENT_BSSID, &bssid, 3094 IEEE80211_ADDR_LEN); 3095 IEEE80211_ADDR_COPY(ni->ni_bssid, &bssid); 3096 IEEE80211_ADDR_COPY(ni->ni_macaddr, &bssid); 3097 wi_read_xrid(sc, WI_RID_CURRENT_CHAN, &val, sizeof(val)); 3098 if (!isset(ic->ic_chan_avail, le16toh(val))) 3099 panic("%s: invalid channel %d\n", sc->sc_dev.dv_xname, 3100 le16toh(val)); 3101 ni->ni_chan = &ic->ic_channels[le16toh(val)]; 3102 3103 if (ic->ic_opmode == IEEE80211_M_HOSTAP) { 3104 #ifndef IEEE80211_NO_HOSTAP 3105 ni->ni_esslen = ic->ic_des_esslen; 3106 memcpy(ni->ni_essid, ic->ic_des_essid, ni->ni_esslen); 3107 ni->ni_rates = ic->ic_sup_rates[ 3108 ieee80211_chan2mode(ic, ni->ni_chan)]; 3109 ni->ni_intval = ic->ic_lintval; 3110 ni->ni_capinfo = IEEE80211_CAPINFO_ESS; 3111 if (ic->ic_flags & IEEE80211_F_PRIVACY) 3112 ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY; 3113 #endif /* !IEEE80211_NO_HOSTAP */ 3114 } else { 3115 wi_read_xrid(sc, WI_RID_CURRENT_SSID, &ssid, 3116 sizeof(ssid)); 3117 ni->ni_esslen = le16toh(ssid.wi_len); 3118 if (ni->ni_esslen > IEEE80211_NWID_LEN) 3119 ni->ni_esslen = IEEE80211_NWID_LEN; /*XXX*/ 3120 memcpy(ni->ni_essid, ssid.wi_ssid, ni->ni_esslen); 3121 ni->ni_rates = ic->ic_sup_rates[ 3122 ieee80211_chan2mode(ic, ni->ni_chan)]; /*XXX*/ 3123 } 3124 if (ic->ic_opmode != IEEE80211_M_MONITOR) 3125 callout_reset(&sc->sc_rssadapt_ch, hz / 10, 3126 wi_rssadapt_updatestats, sc); 3127 /* Trigger routing socket messages. XXX Copied from 3128 * ieee80211_newstate. 3129 */ 3130 if (ic->ic_opmode == IEEE80211_M_STA) 3131 ieee80211_notify_node_join(ic, ic->ic_bss, 3132 arg == IEEE80211_FC0_SUBTYPE_ASSOC_RESP); 3133 break; 3134 } 3135 wi_mend_flags(sc, nstate); 3136 return (*sc->sc_newstate)(ic, nstate, arg); 3137 } 3138 3139 STATIC void 3140 wi_set_tim(struct ieee80211_node *ni, int set) 3141 { 3142 struct ieee80211com *ic = ni->ni_ic; 3143 struct wi_softc *sc = ic->ic_ifp->if_softc; 3144 3145 (*sc->sc_set_tim)(ni, set); 3146 3147 if ((ic->ic_flags & IEEE80211_F_TIMUPDATE) == 0) 3148 return; 3149 3150 ic->ic_flags &= ~IEEE80211_F_TIMUPDATE; 3151 3152 (void)wi_write_val(sc, WI_RID_SET_TIM, 3153 IEEE80211_AID(ni->ni_associd) | (set ? 0x8000 : 0)); 3154 } 3155 3156 STATIC int 3157 wi_scan_ap(struct wi_softc *sc, u_int16_t chanmask, u_int16_t txrate) 3158 { 3159 int error = 0; 3160 u_int16_t val[2]; 3161 3162 if (!sc->sc_enabled) 3163 return ENXIO; 3164 switch (sc->sc_firmware_type) { 3165 case WI_LUCENT: 3166 (void)wi_cmd(sc, WI_CMD_INQUIRE, WI_INFO_SCAN_RESULTS, 0, 0); 3167 break; 3168 case WI_INTERSIL: 3169 val[0] = htole16(chanmask); /* channel */ 3170 val[1] = htole16(txrate); /* tx rate */ 3171 error = wi_write_rid(sc, WI_RID_SCAN_REQ, val, sizeof(val)); 3172 break; 3173 case WI_SYMBOL: 3174 /* 3175 * XXX only supported on 3.x ? 3176 */ 3177 val[0] = htole16(BSCAN_BCAST | BSCAN_ONETIME); 3178 error = wi_write_rid(sc, WI_RID_BCAST_SCAN_REQ, 3179 val, sizeof(val[0])); 3180 break; 3181 } 3182 if (error == 0) { 3183 sc->sc_scan_timer = WI_SCAN_WAIT; 3184 sc->sc_if.if_timer = 1; 3185 DPRINTF(("wi_scan_ap: start scanning, " 3186 "chanmask 0x%x txrate 0x%x\n", chanmask, txrate)); 3187 } 3188 return error; 3189 } 3190 3191 STATIC void 3192 wi_scan_result(struct wi_softc *sc, int fid, int cnt) 3193 { 3194 #define N(a) (sizeof (a) / sizeof (a[0])) 3195 int i, naps, off, szbuf; 3196 struct wi_scan_header ws_hdr; /* Prism2 header */ 3197 struct wi_scan_data_p2 ws_dat; /* Prism2 scantable*/ 3198 struct wi_apinfo *ap; 3199 3200 off = sizeof(u_int16_t) * 2; 3201 memset(&ws_hdr, 0, sizeof(ws_hdr)); 3202 switch (sc->sc_firmware_type) { 3203 case WI_INTERSIL: 3204 wi_read_bap(sc, fid, off, &ws_hdr, sizeof(ws_hdr)); 3205 off += sizeof(ws_hdr); 3206 szbuf = sizeof(struct wi_scan_data_p2); 3207 break; 3208 case WI_SYMBOL: 3209 szbuf = sizeof(struct wi_scan_data_p2) + 6; 3210 break; 3211 case WI_LUCENT: 3212 szbuf = sizeof(struct wi_scan_data); 3213 break; 3214 default: 3215 printf("%s: wi_scan_result: unknown firmware type %u\n", 3216 sc->sc_dev.dv_xname, sc->sc_firmware_type); 3217 naps = 0; 3218 goto done; 3219 } 3220 naps = (cnt * 2 + 2 - off) / szbuf; 3221 if (naps > N(sc->sc_aps)) 3222 naps = N(sc->sc_aps); 3223 sc->sc_naps = naps; 3224 /* Read Data */ 3225 ap = sc->sc_aps; 3226 memset(&ws_dat, 0, sizeof(ws_dat)); 3227 for (i = 0; i < naps; i++, ap++) { 3228 wi_read_bap(sc, fid, off, &ws_dat, 3229 (sizeof(ws_dat) < szbuf ? sizeof(ws_dat) : szbuf)); 3230 DPRINTF2(("wi_scan_result: #%d: off %d bssid %s\n", i, off, 3231 ether_sprintf(ws_dat.wi_bssid))); 3232 off += szbuf; 3233 ap->scanreason = le16toh(ws_hdr.wi_reason); 3234 memcpy(ap->bssid, ws_dat.wi_bssid, sizeof(ap->bssid)); 3235 ap->channel = le16toh(ws_dat.wi_chid); 3236 ap->signal = le16toh(ws_dat.wi_signal); 3237 ap->noise = le16toh(ws_dat.wi_noise); 3238 ap->quality = ap->signal - ap->noise; 3239 ap->capinfo = le16toh(ws_dat.wi_capinfo); 3240 ap->interval = le16toh(ws_dat.wi_interval); 3241 ap->rate = le16toh(ws_dat.wi_rate); 3242 ap->namelen = le16toh(ws_dat.wi_namelen); 3243 if (ap->namelen > sizeof(ap->name)) 3244 ap->namelen = sizeof(ap->name); 3245 memcpy(ap->name, ws_dat.wi_name, ap->namelen); 3246 } 3247 done: 3248 /* Done scanning */ 3249 sc->sc_scan_timer = 0; 3250 DPRINTF(("wi_scan_result: scan complete: ap %d\n", naps)); 3251 #undef N 3252 } 3253 3254 STATIC void 3255 wi_dump_pkt(struct wi_frame *wh, struct ieee80211_node *ni, int rssi) 3256 { 3257 ieee80211_dump_pkt((u_int8_t *) &wh->wi_whdr, sizeof(wh->wi_whdr), 3258 ni ? ni->ni_rates.rs_rates[ni->ni_txrate] & IEEE80211_RATE_VAL 3259 : -1, 3260 rssi); 3261 printf(" status 0x%x rx_tstamp1 %u rx_tstamp0 0x%u rx_silence %u\n", 3262 le16toh(wh->wi_status), le16toh(wh->wi_rx_tstamp1), 3263 le16toh(wh->wi_rx_tstamp0), wh->wi_rx_silence); 3264 printf(" rx_signal %u rx_rate %u rx_flow %u\n", 3265 wh->wi_rx_signal, wh->wi_rx_rate, wh->wi_rx_flow); 3266 printf(" tx_rtry %u tx_rate %u tx_ctl 0x%x dat_len %u\n", 3267 wh->wi_tx_rtry, wh->wi_tx_rate, 3268 le16toh(wh->wi_tx_ctl), le16toh(wh->wi_dat_len)); 3269 printf(" ehdr dst %s src %s type 0x%x\n", 3270 ether_sprintf(wh->wi_ehdr.ether_dhost), 3271 ether_sprintf(wh->wi_ehdr.ether_shost), 3272 wh->wi_ehdr.ether_type); 3273 } 3274