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