1 /* $OpenBSD: an.c,v 1.58 2010/08/27 17:08:00 jsg Exp $ */ 2 /* $NetBSD: an.c,v 1.34 2005/06/20 02:49:18 atatat Exp $ */ 3 /* 4 * Copyright (c) 1997, 1998, 1999 5 * Bill Paul <wpaul@ctr.columbia.edu>. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by Bill Paul. 18 * 4. Neither the name of the author nor the names of any co-contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD 26 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 32 * THE POSSIBILITY OF SUCH DAMAGE. 33 * 34 * $FreeBSD: src/sys/dev/an/if_an.c,v 1.12 2000/11/13 23:04:12 wpaul Exp $ 35 */ 36 /* 37 * Copyright (c) 2004, 2005 David Young. All rights reserved. 38 * Copyright (c) 2004, 2005 OJC Technologies. All rights reserved. 39 * Copyright (c) 2004, 2005 Dayton Data Center Services, LLC. All 40 * rights reserved. 41 * 42 * Redistribution and use in source and binary forms, with or without 43 * modification, are permitted provided that the following conditions 44 * are met: 45 * 1. Redistributions of source code must retain the above copyright 46 * notice, this list of conditions and the following disclaimer. 47 * 2. Redistributions in binary form must reproduce the above copyright 48 * notice, this list of conditions and the following disclaimer in the 49 * documentation and/or other materials provided with the distribution. 50 * 3. Neither the name of the author nor the names of any co-contributors 51 * may be used to endorse or promote products derived from this software 52 * without specific prior written permission. 53 * 54 * THIS SOFTWARE IS PROVIDED BY David Young AND CONTRIBUTORS ``AS IS'' AND 55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 57 * ARE DISCLAIMED. IN NO EVENT SHALL David Young AND CONTRIBUTORS 58 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 59 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 60 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 61 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 62 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 63 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 64 * THE POSSIBILITY OF SUCH DAMAGE. 65 */ 66 67 /* 68 * Aironet 4500/4800 802.11 PCMCIA/ISA/PCI driver for FreeBSD. 69 * 70 * Written by Bill Paul <wpaul@ctr.columbia.edu> 71 * Electrical Engineering Department 72 * Columbia University, New York City 73 */ 74 75 /* 76 * Ported to NetBSD from FreeBSD by Atsushi Onoe at the San Diego 77 * IETF meeting. 78 */ 79 80 #include <sys/cdefs.h> 81 82 #include "bpfilter.h" 83 84 #include <sys/param.h> 85 #include <sys/systm.h> 86 #include <sys/sockio.h> 87 #include <sys/mbuf.h> 88 #include <sys/kernel.h> 89 #include <sys/ucred.h> 90 #include <sys/socket.h> 91 #include <sys/timeout.h> 92 #include <sys/device.h> 93 #include <sys/proc.h> 94 #include <sys/endian.h> 95 #include <sys/tree.h> 96 97 #include <machine/bus.h> 98 99 #include <net/if.h> 100 #include <net/if_dl.h> 101 #include <net/if_llc.h> 102 #include <net/if_media.h> 103 #include <net/if_types.h> 104 105 #ifdef INET 106 #include <netinet/in.h> 107 #include <netinet/in_systm.h> 108 #include <netinet/in_var.h> 109 #include <netinet/ip.h> 110 #include <netinet/if_ether.h> 111 #endif 112 113 #include <net80211/ieee80211_radiotap.h> 114 #include <net80211/ieee80211_var.h> 115 116 #if NBPFILTER > 0 117 #include <net/bpf.h> 118 #endif 119 120 #include <dev/ic/anreg.h> 121 #include <dev/ic/anvar.h> 122 123 struct cfdriver an_cd = { 124 NULL, "an", DV_IFNET 125 }; 126 127 int an_reset(struct an_softc *); 128 void an_wait(struct an_softc *); 129 int an_init(struct ifnet *); 130 void an_stop(struct ifnet *, int); 131 void an_start(struct ifnet *); 132 void an_watchdog(struct ifnet *); 133 int an_ioctl(struct ifnet *, u_long, caddr_t); 134 int an_media_change(struct ifnet *); 135 void an_media_status(struct ifnet *, struct ifmediareq *); 136 137 int an_set_nwkey(struct an_softc *, struct ieee80211_nwkey *); 138 int an_set_nwkey_wep(struct an_softc *, struct ieee80211_nwkey *); 139 int an_get_nwkey(struct an_softc *, struct ieee80211_nwkey *); 140 int an_write_wepkey(struct an_softc *, int, struct an_wepkey *, 141 int); 142 143 void an_rxeof(struct an_softc *); 144 void an_txeof(struct an_softc *, u_int16_t); 145 void an_linkstat_intr(struct an_softc *); 146 147 int an_cmd(struct an_softc *, int, int); 148 int an_seek_bap(struct an_softc *, int, int); 149 int an_read_bap(struct an_softc *, int, int, void *, int, int); 150 int an_write_bap(struct an_softc *, int, int, void *, int); 151 int an_mwrite_bap(struct an_softc *, int, int, struct mbuf *, int); 152 int an_read_rid(struct an_softc *, int, void *, int *); 153 int an_write_rid(struct an_softc *, int, void *, int); 154 155 int an_alloc_nicmem(struct an_softc *, int, int *); 156 157 int an_newstate(struct ieee80211com *, enum ieee80211_state, int); 158 159 #ifdef AN_DEBUG 160 int an_debug = 0; 161 162 #define DPRINTF(X) if (an_debug) printf X 163 #define DPRINTF2(X) if (an_debug > 1) printf X 164 #else 165 #define DPRINTF(X) 166 #define DPRINTF2(X) 167 #endif 168 169 #if BYTE_ORDER == BIG_ENDIAN 170 static __inline void 171 an_swap16(u_int16_t *p, int cnt) 172 { 173 for (; cnt--; p++) 174 *p = swap16(*p); 175 } 176 #define an_switch32(val) (val >> 16 | (val & 0xFFFF) << 16) 177 #else 178 #define an_swap16(p, cnt) 179 #define an_switch32(val) val 180 #endif 181 182 int 183 an_attach(struct an_softc *sc) 184 { 185 struct ieee80211com *ic = &sc->sc_ic; 186 struct ifnet *ifp = &ic->ic_if; 187 int i; 188 struct an_rid_wepkey *akey; 189 int buflen, kid, rid; 190 int chan, chan_min, chan_max; 191 192 sc->sc_invalid = 0; 193 194 /* disable interrupts */ 195 CSR_WRITE_2(sc, AN_INT_EN, 0); 196 CSR_WRITE_2(sc, AN_EVENT_ACK, 0xffff); 197 198 // an_wait(sc); 199 if (an_reset(sc) != 0) { 200 sc->sc_invalid = 1; 201 return 1; 202 } 203 204 /* Load factory config */ 205 if (an_cmd(sc, AN_CMD_READCFG, 0) != 0) { 206 printf("%s: failed to load config data\n", 207 sc->sc_dev.dv_xname); 208 return (EIO); 209 } 210 211 /* Read the current configuration */ 212 buflen = sizeof(sc->sc_config); 213 if (an_read_rid(sc, AN_RID_GENCONFIG, &sc->sc_config, &buflen) != 0) { 214 printf("%s: read config failed\n", sc->sc_dev.dv_xname); 215 return(EIO); 216 } 217 218 an_swap16((u_int16_t *)&sc->sc_config.an_macaddr, 3); 219 220 /* Read the card capabilities */ 221 buflen = sizeof(sc->sc_caps); 222 if (an_read_rid(sc, AN_RID_CAPABILITIES, &sc->sc_caps, &buflen) != 0) { 223 printf("%s: read caps failed\n", sc->sc_dev.dv_xname); 224 return(EIO); 225 } 226 227 an_swap16((u_int16_t *)&sc->sc_caps.an_oemaddr, 3); 228 an_swap16((u_int16_t *)&sc->sc_caps.an_rates, 4); 229 230 /* Read WEP settings from persistent memory */ 231 akey = &sc->sc_buf.sc_wepkey; 232 buflen = sizeof(struct an_rid_wepkey); 233 rid = AN_RID_WEP_VOLATILE; /* first persistent key */ 234 while (an_read_rid(sc, rid, akey, &buflen) == 0) { 235 an_swap16((u_int16_t *)&akey->an_mac_addr, 3); 236 an_swap16((u_int16_t *)&akey->an_key, 8); 237 kid = akey->an_key_index; 238 DPRINTF(("an_attach: wep rid=0x%x len=%d(%d) index=0x%04x " 239 "mac[0]=%02x keylen=%d\n", 240 rid, buflen, sizeof(*akey), kid, 241 akey->an_mac_addr[0], akey->an_key_len)); 242 if (kid == 0xffff) { 243 sc->sc_tx_perskey = akey->an_mac_addr[0]; 244 sc->sc_tx_key = -1; 245 break; 246 } 247 if (kid >= IEEE80211_WEP_NKID) 248 break; 249 sc->sc_perskeylen[kid] = akey->an_key_len; 250 sc->sc_wepkeys[kid].an_wep_keylen = -1; 251 rid = AN_RID_WEP_PERSISTENT; /* for next key */ 252 buflen = sizeof(struct an_rid_wepkey); 253 } 254 255 IEEE80211_ADDR_COPY(ic->ic_myaddr, sc->sc_caps.an_oemaddr); 256 bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ); 257 258 printf("%s: Firmware %x.%02x.%02x, Radio: ", ifp->if_xname, 259 sc->sc_caps.an_fwrev >> 8, 260 sc->sc_caps.an_fwrev & 0xff, 261 sc->sc_caps.an_fwsubrev); 262 263 if (sc->sc_config.an_radiotype & AN_RADIOTYPE_80211_FH) 264 printf("802.11 FH"); 265 else if (sc->sc_config.an_radiotype & AN_RADIOTYPE_80211_DS) 266 printf("802.11 DS"); 267 else if (sc->sc_config.an_radiotype & AN_RADIOTYPE_LM2000_DS) 268 printf("LM2000 DS"); 269 else 270 printf("unknown (%x)", sc->sc_config.an_radiotype); 271 272 printf(", address %s\n", ether_sprintf(ic->ic_myaddr)); 273 274 ifp->if_softc = sc; 275 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 276 ifp->if_ioctl = an_ioctl; 277 ifp->if_start = an_start; 278 ifp->if_watchdog = an_watchdog; 279 IFQ_SET_READY(&ifp->if_snd); 280 281 ic->ic_phytype = IEEE80211_T_DS; 282 ic->ic_opmode = IEEE80211_M_STA; 283 ic->ic_caps = IEEE80211_C_WEP | IEEE80211_C_PMGT | IEEE80211_C_MONITOR; 284 #ifndef IEEE80211_STA_ONLY 285 ic->ic_caps |= IEEE80211_C_IBSS; 286 #endif 287 ic->ic_state = IEEE80211_S_INIT; 288 IEEE80211_ADDR_COPY(ic->ic_myaddr, sc->sc_caps.an_oemaddr); 289 290 switch (sc->sc_caps.an_regdomain) { 291 default: 292 case AN_REGDOMAIN_USA: 293 case AN_REGDOMAIN_CANADA: 294 chan_min = 1; chan_max = 11; break; 295 case AN_REGDOMAIN_EUROPE: 296 case AN_REGDOMAIN_AUSTRALIA: 297 chan_min = 1; chan_max = 13; break; 298 case AN_REGDOMAIN_JAPAN: 299 chan_min = 14; chan_max = 14; break; 300 case AN_REGDOMAIN_SPAIN: 301 chan_min = 10; chan_max = 11; break; 302 case AN_REGDOMAIN_FRANCE: 303 chan_min = 10; chan_max = 13; break; 304 case AN_REGDOMAIN_JAPANWIDE: 305 chan_min = 1; chan_max = 14; break; 306 } 307 308 for (chan = chan_min; chan <= chan_max; chan++) { 309 ic->ic_channels[chan].ic_freq = 310 ieee80211_ieee2mhz(chan, IEEE80211_CHAN_2GHZ); 311 ic->ic_channels[chan].ic_flags = IEEE80211_CHAN_B; 312 } 313 ic->ic_ibss_chan = &ic->ic_channels[chan_min]; 314 315 /* Find supported rate */ 316 for (i = 0; i < sizeof(sc->sc_caps.an_rates); i++) { 317 if (sc->sc_caps.an_rates[i] == 0) 318 continue; 319 ic->ic_sup_rates[IEEE80211_MODE_11B].rs_rates[ 320 ic->ic_sup_rates[IEEE80211_MODE_11B].rs_nrates++] = 321 sc->sc_caps.an_rates[i]; 322 } 323 324 /* 325 * Call MI attach routine. 326 */ 327 if_attach(ifp); 328 ieee80211_ifattach(ifp); 329 330 sc->sc_newstate = ic->ic_newstate; 331 ic->ic_newstate = an_newstate; 332 333 ieee80211_media_init(ifp, an_media_change, an_media_status); 334 335 #if NBPFILTER > 0 336 bzero(&sc->sc_rxtapu, sizeof(sc->sc_rxtapu)); 337 sc->sc_rxtap.ar_ihdr.it_len = sizeof(sc->sc_rxtapu); 338 sc->sc_rxtap.ar_ihdr.it_present = AN_RX_RADIOTAP_PRESENT; 339 340 bzero(&sc->sc_txtapu, sizeof(sc->sc_txtapu)); 341 sc->sc_txtap.at_ihdr.it_len = sizeof(sc->sc_txtapu); 342 sc->sc_txtap.at_ihdr.it_present = AN_TX_RADIOTAP_PRESENT; 343 344 bpfattach(&sc->sc_drvbpf, ifp, DLT_IEEE802_11_RADIO, 345 sizeof(struct ieee80211_frame) + 64); 346 #endif 347 348 sc->sc_attached = 1; 349 350 return(0); 351 } 352 353 void 354 an_rxeof(struct an_softc *sc) 355 { 356 struct ieee80211com *ic = &sc->sc_ic; 357 struct ifnet *ifp = &ic->ic_if; 358 struct ieee80211_frame *wh; 359 struct ieee80211_rxinfo rxi; 360 struct ieee80211_node *ni; 361 struct an_rxframe frmhdr; 362 struct mbuf *m; 363 u_int16_t status; 364 int fid, gaplen, len, off; 365 uint8_t *gap; 366 367 fid = CSR_READ_2(sc, AN_RX_FID); 368 369 /* First read in the frame header */ 370 if (an_read_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr), sizeof(frmhdr)) != 0) { 371 CSR_WRITE_2(sc, AN_EVENT_ACK, AN_EV_RX); 372 ifp->if_ierrors++; 373 DPRINTF(("an_rxeof: read fid %x failed\n", fid)); 374 return; 375 } 376 an_swap16((u_int16_t *)&frmhdr.an_whdr, sizeof(struct ieee80211_frame)/2); 377 378 status = frmhdr.an_rx_status; 379 if ((status & AN_STAT_ERRSTAT) != 0 && 380 ic->ic_opmode != IEEE80211_M_MONITOR) { 381 CSR_WRITE_2(sc, AN_EVENT_ACK, AN_EV_RX); 382 ifp->if_ierrors++; 383 DPRINTF(("an_rxeof: fid %x status %x\n", fid, status)); 384 return; 385 } 386 387 /* the payload length field includes a 16-bit "mystery field" */ 388 len = frmhdr.an_rx_payload_len - sizeof(uint16_t); 389 off = ALIGN(sizeof(struct ieee80211_frame)); 390 391 if (off + len > MCLBYTES) { 392 if (ic->ic_opmode != IEEE80211_M_MONITOR) { 393 CSR_WRITE_2(sc, AN_EVENT_ACK, AN_EV_RX); 394 ifp->if_ierrors++; 395 DPRINTF(("an_rxeof: oversized packet %d\n", len)); 396 return; 397 } 398 len = 0; 399 } 400 401 MGETHDR(m, M_DONTWAIT, MT_DATA); 402 if (m == NULL) { 403 CSR_WRITE_2(sc, AN_EVENT_ACK, AN_EV_RX); 404 ifp->if_ierrors++; 405 DPRINTF(("an_rxeof: MGET failed\n")); 406 return; 407 } 408 if (off + len + AN_GAPLEN_MAX > MHLEN) { 409 MCLGET(m, M_DONTWAIT); 410 if ((m->m_flags & M_EXT) == 0) { 411 CSR_WRITE_2(sc, AN_EVENT_ACK, AN_EV_RX); 412 m_freem(m); 413 ifp->if_ierrors++; 414 DPRINTF(("an_rxeof: MCLGET failed\n")); 415 return; 416 } 417 } 418 m->m_data += off - sizeof(struct ieee80211_frame); 419 420 if (ic->ic_opmode != IEEE80211_M_MONITOR) { 421 gaplen = frmhdr.an_gaplen; 422 if (gaplen > AN_GAPLEN_MAX) { 423 CSR_WRITE_2(sc, AN_EVENT_ACK, AN_EV_RX); 424 m_freem(m); 425 ifp->if_ierrors++; 426 DPRINTF(("%s: gap too long\n", __func__)); 427 return; 428 } 429 /* 430 * We don't need the 16-bit mystery field (payload length?), 431 * so read it into the region reserved for the 802.11 header. 432 * 433 * When Cisco Aironet 350 cards w/ firmware version 5 or 434 * greater operate with certain Cisco 350 APs, 435 * the "gap" is filled with the SNAP header. Read 436 * it in after the 802.11 header. 437 */ 438 gap = m->m_data + sizeof(struct ieee80211_frame) - 439 sizeof(uint16_t); 440 an_read_bap(sc, fid, -1, gap, gaplen + sizeof(u_int16_t), 441 gaplen + sizeof(u_int16_t)); 442 } else 443 gaplen = 0; 444 445 an_read_bap(sc, fid, -1, 446 m->m_data + sizeof(struct ieee80211_frame) + gaplen, len, len); 447 an_swap16((u_int16_t *)(m->m_data + sizeof(struct ieee80211_frame) + gaplen), (len+1)/2); 448 m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame) + gaplen + 449 len; 450 451 memcpy(m->m_data, &frmhdr.an_whdr, sizeof(struct ieee80211_frame)); 452 m->m_pkthdr.rcvif = ifp; 453 CSR_WRITE_2(sc, AN_EVENT_ACK, AN_EV_RX); 454 455 #if NBPFILTER > 0 456 if (sc->sc_drvbpf) { 457 struct mbuf mb; 458 struct an_rx_radiotap_header *tap = &sc->sc_rxtap; 459 460 tap->ar_rate = frmhdr.an_rx_rate; 461 tap->ar_antsignal = frmhdr.an_rx_signal_strength; 462 tap->ar_chan_freq = ic->ic_bss->ni_chan->ic_freq; 463 tap->ar_chan_flags = ic->ic_bss->ni_chan->ic_flags; 464 465 466 mb.m_data = (caddr_t)tap; 467 mb.m_len = sizeof(sc->sc_rxtapu); 468 mb.m_next = m; 469 mb.m_nextpkt = NULL; 470 mb.m_type = 0; 471 mb.m_flags = 0; 472 bpf_mtap(sc->sc_drvbpf, &mb, BPF_DIRECTION_IN); 473 } 474 #endif /* NBPFILTER > 0 */ 475 476 wh = mtod(m, struct ieee80211_frame *); 477 rxi.rxi_flags = 0; 478 if (wh->i_fc[1] & IEEE80211_FC1_WEP) { 479 /* 480 * WEP is decrypted by hardware. Clear WEP bit 481 * header for ieee80211_input(). 482 */ 483 wh->i_fc[1] &= ~IEEE80211_FC1_WEP; 484 485 rxi.rxi_flags |= IEEE80211_RXI_HWDEC; 486 } 487 488 ni = ieee80211_find_rxnode(ic, wh); 489 rxi.rxi_rssi = frmhdr.an_rx_signal_strength; 490 rxi.rxi_tstamp = an_switch32(frmhdr.an_rx_time); 491 ieee80211_input(ifp, m, ni, &rxi); 492 ieee80211_release_node(ic, ni); 493 } 494 495 void 496 an_txeof(struct an_softc *sc, u_int16_t status) 497 { 498 struct ifnet *ifp = &sc->sc_ic.ic_if; 499 int cur, id; 500 501 sc->sc_tx_timer = 0; 502 ifp->if_flags &= ~IFF_OACTIVE; 503 504 id = CSR_READ_2(sc, AN_TX_CMP_FID); 505 CSR_WRITE_2(sc, AN_EVENT_ACK, status & (AN_EV_TX | AN_EV_TX_EXC)); 506 507 if (status & AN_EV_TX_EXC) 508 ifp->if_oerrors++; 509 else 510 ifp->if_opackets++; 511 512 cur = sc->sc_txcur; 513 if (sc->sc_txd[cur].d_fid == id) { 514 sc->sc_txd[cur].d_inuse = 0; 515 DPRINTF2(("an_txeof: sent %x/%d\n", id, cur)); 516 AN_INC(cur, AN_TX_RING_CNT); 517 sc->sc_txcur = cur; 518 } else { 519 for (cur = 0; cur < AN_TX_RING_CNT; cur++) { 520 if (id == sc->sc_txd[cur].d_fid) { 521 sc->sc_txd[cur].d_inuse = 0; 522 break; 523 } 524 } 525 if (ifp->if_flags & IFF_DEBUG) 526 printf("%s: tx mismatch: " 527 "expected %x(%d), actual %x(%d)\n", 528 sc->sc_dev.dv_xname, 529 sc->sc_txd[sc->sc_txcur].d_fid, sc->sc_txcur, 530 id, cur); 531 } 532 } 533 534 int 535 an_intr(void *arg) 536 { 537 struct an_softc *sc = arg; 538 struct ifnet *ifp = &sc->sc_ic.ic_if; 539 int i; 540 u_int16_t status; 541 542 if (!sc->sc_enabled || sc->sc_invalid || 543 (sc->sc_dev.dv_flags & DVF_ACTIVE) == 0 || 544 (ifp->if_flags & IFF_RUNNING) == 0) 545 return 0; 546 547 if ((ifp->if_flags & IFF_UP) == 0) { 548 CSR_WRITE_2(sc, AN_INT_EN, 0); 549 CSR_WRITE_2(sc, AN_EVENT_ACK, ~0); 550 return 1; 551 } 552 553 /* maximum 10 loops per interrupt */ 554 for (i = 0; i < 10; i++) { 555 if (!sc->sc_enabled || sc->sc_invalid) 556 return 1; 557 if (CSR_READ_2(sc, AN_SW0) != AN_MAGIC) { 558 DPRINTF(("an_intr: magic number changed: %x\n", 559 CSR_READ_2(sc, AN_SW0))); 560 sc->sc_invalid = 1; 561 return 1; 562 } 563 status = CSR_READ_2(sc, AN_EVENT_STAT); 564 CSR_WRITE_2(sc, AN_EVENT_ACK, status & ~(AN_INTRS)); 565 if ((status & AN_INTRS) == 0) 566 break; 567 568 if (status & AN_EV_RX) 569 an_rxeof(sc); 570 571 if (status & (AN_EV_TX | AN_EV_TX_EXC)) 572 an_txeof(sc, status); 573 574 if (status & AN_EV_LINKSTAT) 575 an_linkstat_intr(sc); 576 577 if ((ifp->if_flags & IFF_OACTIVE) == 0 && 578 sc->sc_ic.ic_state == IEEE80211_S_RUN && 579 !IFQ_IS_EMPTY(&ifp->if_snd)) 580 an_start(ifp); 581 } 582 583 return 1; 584 } 585 586 /* Must be called at proper protection level! */ 587 int 588 an_cmd(struct an_softc *sc, int cmd, int val) 589 { 590 int i, stat; 591 592 /* make sure previous command completed */ 593 if (CSR_READ_2(sc, AN_COMMAND) & AN_CMD_BUSY) { 594 if (sc->sc_ic.ic_if.if_flags & IFF_DEBUG) 595 printf("%s: command 0x%x busy\n", sc->sc_dev.dv_xname, 596 CSR_READ_2(sc, AN_COMMAND)); 597 CSR_WRITE_2(sc, AN_EVENT_ACK, AN_EV_CLR_STUCK_BUSY); 598 } 599 600 CSR_WRITE_2(sc, AN_PARAM0, val); 601 CSR_WRITE_2(sc, AN_PARAM1, 0); 602 CSR_WRITE_2(sc, AN_PARAM2, 0); 603 CSR_WRITE_2(sc, AN_COMMAND, cmd); 604 605 if (cmd == AN_CMD_FW_RESTART) { 606 /* XXX: should sleep here */ 607 DELAY(100*1000); 608 } 609 610 for (i = 0; i < AN_TIMEOUT; i++) { 611 if (CSR_READ_2(sc, AN_EVENT_STAT) & AN_EV_CMD) 612 break; 613 DELAY(10); 614 } 615 616 stat = CSR_READ_2(sc, AN_STATUS); 617 618 /* clear stuck command busy if necessary */ 619 if (CSR_READ_2(sc, AN_COMMAND) & AN_CMD_BUSY) 620 CSR_WRITE_2(sc, AN_EVENT_ACK, AN_EV_CLR_STUCK_BUSY); 621 622 /* Ack the command */ 623 CSR_WRITE_2(sc, AN_EVENT_ACK, AN_EV_CMD); 624 625 if (i == AN_TIMEOUT) { 626 if (sc->sc_ic.ic_if.if_flags & IFF_DEBUG) 627 printf("%s: command 0x%x param 0x%x timeout\n", 628 sc->sc_dev.dv_xname, cmd, val); 629 return ETIMEDOUT; 630 } 631 if (stat & AN_STAT_CMD_RESULT) { 632 if (sc->sc_ic.ic_if.if_flags & IFF_DEBUG) 633 printf("%s: command 0x%x param 0x%x status 0x%x " 634 "resp 0x%x 0x%x 0x%x\n", 635 sc->sc_dev.dv_xname, cmd, val, stat, 636 CSR_READ_2(sc, AN_RESP0), CSR_READ_2(sc, AN_RESP1), 637 CSR_READ_2(sc, AN_RESP2)); 638 return EIO; 639 } 640 641 return 0; 642 } 643 644 int 645 an_reset(struct an_softc *sc) 646 { 647 648 DPRINTF(("an_reset\n")); 649 650 if (!sc->sc_enabled) 651 return ENXIO; 652 653 an_cmd(sc, AN_CMD_ENABLE, 0); 654 an_cmd(sc, AN_CMD_FW_RESTART, 0); 655 an_cmd(sc, AN_CMD_NOOP2, 0); 656 657 if (an_cmd(sc, AN_CMD_FORCE_SYNCLOSS, 0) == ETIMEDOUT) { 658 printf("%s: reset failed\n", sc->sc_dev.dv_xname); 659 return ETIMEDOUT; 660 } 661 662 an_cmd(sc, AN_CMD_DISABLE, 0); 663 return 0; 664 } 665 666 void 667 an_linkstat_intr(struct an_softc *sc) 668 { 669 struct ieee80211com *ic = &sc->sc_ic; 670 u_int16_t status; 671 672 status = CSR_READ_2(sc, AN_LINKSTAT); 673 CSR_WRITE_2(sc, AN_EVENT_ACK, AN_EV_LINKSTAT); 674 DPRINTF(("an_linkstat_intr: status 0x%x\n", status)); 675 676 if (status == AN_LINKSTAT_ASSOCIATED) { 677 if (ic->ic_state != IEEE80211_S_RUN 678 #ifndef IEEE80211_STA_ONLY 679 || ic->ic_opmode == IEEE80211_M_IBSS 680 #endif 681 ) 682 ieee80211_new_state(ic, IEEE80211_S_RUN, -1); 683 } else { 684 if (ic->ic_opmode == IEEE80211_M_STA) 685 ieee80211_new_state(ic, IEEE80211_S_INIT, -1); 686 } 687 } 688 689 /* 690 * Wait for firmware come up after power enabled. 691 */ 692 void 693 an_wait(struct an_softc *sc) 694 { 695 int i; 696 697 CSR_WRITE_2(sc, AN_COMMAND, AN_CMD_NOOP2); 698 for (i = 0; i < 3*hz; i++) { 699 if (CSR_READ_2(sc, AN_EVENT_STAT) & AN_EV_CMD) 700 break; 701 (void)tsleep(sc, PWAIT, "anatch", 1); 702 } 703 CSR_WRITE_2(sc, AN_EVENT_ACK, AN_EV_CMD); 704 } 705 706 int 707 an_read_bap(struct an_softc *sc, int id, int off, void *buf, int len, int blen) 708 { 709 int error, cnt, cnt2; 710 711 if (len == 0 || blen == 0) 712 return 0; 713 if (off == -1) 714 off = sc->sc_bap_off; 715 if (id != sc->sc_bap_id || off != sc->sc_bap_off) { 716 if ((error = an_seek_bap(sc, id, off)) != 0) 717 return EIO; 718 } 719 720 cnt = (blen + 1) / 2; 721 CSR_READ_MULTI_STREAM_2(sc, AN_DATA0, (u_int16_t *)buf, cnt); 722 for (cnt2 = (len + 1) / 2; cnt < cnt2; cnt++) 723 (void) CSR_READ_2(sc, AN_DATA0); 724 sc->sc_bap_off += cnt * 2; 725 726 return 0; 727 } 728 729 int 730 an_write_bap(struct an_softc *sc, int id, int off, void *buf, int buflen) 731 { 732 int error, cnt; 733 734 if (buflen == 0) 735 return 0; 736 if (off == -1) 737 off = sc->sc_bap_off; 738 if (id != sc->sc_bap_id || off != sc->sc_bap_off) { 739 if ((error = an_seek_bap(sc, id, off)) != 0) 740 return EIO; 741 } 742 743 cnt = (buflen + 1) / 2; 744 CSR_WRITE_MULTI_STREAM_2(sc, AN_DATA0, (u_int16_t *)buf, cnt); 745 sc->sc_bap_off += cnt * 2; 746 return 0; 747 } 748 749 int 750 an_seek_bap(struct an_softc *sc, int id, int off) 751 { 752 int i, status; 753 754 CSR_WRITE_2(sc, AN_SEL0, id); 755 CSR_WRITE_2(sc, AN_OFF0, off); 756 757 for (i = 0; ; i++) { 758 status = CSR_READ_2(sc, AN_OFF0); 759 if ((status & AN_OFF_BUSY) == 0) 760 break; 761 if (i == AN_TIMEOUT) { 762 printf("%s: timeout in an_seek_bap to 0x%x/0x%x\n", 763 sc->sc_dev.dv_xname, id, off); 764 sc->sc_bap_off = AN_OFF_ERR; /* invalidate */ 765 return ETIMEDOUT; 766 } 767 DELAY(10); 768 } 769 if (status & AN_OFF_ERR) { 770 printf("%s: failed in an_seek_bap to 0x%x/0x%x\n", 771 sc->sc_dev.dv_xname, id, off); 772 sc->sc_bap_off = AN_OFF_ERR; /* invalidate */ 773 return EIO; 774 } 775 sc->sc_bap_id = id; 776 sc->sc_bap_off = off; 777 return 0; 778 } 779 780 int 781 an_mwrite_bap(struct an_softc *sc, int id, int off, struct mbuf *m, int totlen) 782 { 783 int error, len, cnt; 784 785 if (off == -1) 786 off = sc->sc_bap_off; 787 if (id != sc->sc_bap_id || off != sc->sc_bap_off) { 788 if ((error = an_seek_bap(sc, id, off)) != 0) 789 return EIO; 790 } 791 792 for (len = 0; m != NULL; m = m->m_next) { 793 if (m->m_len == 0) 794 continue; 795 len = min(m->m_len, totlen); 796 797 if ((mtod(m, u_long) & 0x1) || (len & 0x1)) { 798 m_copydata(m, 0, totlen, (caddr_t)&sc->sc_buf.sc_txbuf); 799 cnt = (totlen + 1) / 2; 800 an_swap16((u_int16_t *)&sc->sc_buf.sc_txbuf, cnt); 801 CSR_WRITE_MULTI_STREAM_2(sc, AN_DATA0, 802 sc->sc_buf.sc_val, cnt); 803 off += cnt * 2; 804 break; 805 } 806 cnt = len / 2; 807 an_swap16((u_int16_t *)mtod(m, u_int16_t *), cnt); 808 CSR_WRITE_MULTI_STREAM_2(sc, AN_DATA0, mtod(m, u_int16_t *), 809 cnt); 810 off += len; 811 totlen -= len; 812 } 813 sc->sc_bap_off = off; 814 return 0; 815 } 816 817 int 818 an_alloc_nicmem(struct an_softc *sc, int len, int *idp) 819 { 820 int i; 821 822 if (an_cmd(sc, AN_CMD_ALLOC_MEM, len)) { 823 printf("%s: failed to allocate %d bytes on NIC\n", 824 sc->sc_dev.dv_xname, len); 825 return(ENOMEM); 826 } 827 828 for (i = 0; i < AN_TIMEOUT; i++) { 829 if (CSR_READ_2(sc, AN_EVENT_STAT) & AN_EV_ALLOC) 830 break; 831 if (i == AN_TIMEOUT) { 832 printf("%s: timeout in alloc\n", sc->sc_dev.dv_xname); 833 return ETIMEDOUT; 834 } 835 DELAY(10); 836 } 837 838 *idp = CSR_READ_2(sc, AN_ALLOC_FID); 839 CSR_WRITE_2(sc, AN_EVENT_ACK, AN_EV_ALLOC); 840 return 0; 841 } 842 843 int 844 an_read_rid(struct an_softc *sc, int rid, void *buf, int *buflenp) 845 { 846 int error; 847 u_int16_t len; 848 849 /* Tell the NIC to enter record read mode. */ 850 error = an_cmd(sc, AN_CMD_ACCESS | AN_ACCESS_READ, rid); 851 if (error) 852 return error; 853 854 /* length in byte, including length itself */ 855 error = an_read_bap(sc, rid, 0, &len, sizeof(len), sizeof(len)); 856 if (error) 857 return error; 858 859 len -= 2; 860 return an_read_bap(sc, rid, sizeof(len), buf, len, *buflenp); 861 } 862 863 int 864 an_write_rid(struct an_softc *sc, int rid, void *buf, int buflen) 865 { 866 int error; 867 u_int16_t len; 868 869 /* length in byte, including length itself */ 870 len = buflen + 2; 871 872 error = an_write_bap(sc, rid, 0, &len, sizeof(len)); 873 if (error) 874 return error; 875 error = an_write_bap(sc, rid, sizeof(len), buf, buflen); 876 if (error) 877 return error; 878 879 return an_cmd(sc, AN_CMD_ACCESS | AN_ACCESS_WRITE, rid); 880 } 881 882 int 883 an_ioctl(struct ifnet *ifp, u_long command, caddr_t data) 884 { 885 struct an_softc *sc = ifp->if_softc; 886 struct ifaddr *ifa = (struct ifaddr *)data; 887 int s, error = 0; 888 889 if ((sc->sc_dev.dv_flags & DVF_ACTIVE) == 0) 890 return ENXIO; 891 892 s = splnet(); 893 894 switch(command) { 895 case SIOCSIFADDR: 896 ifp->if_flags |= IFF_UP; 897 switch (ifa->ifa_addr->sa_family) { 898 #ifdef INET 899 case AF_INET: 900 error = an_init(ifp); 901 arp_ifinit(&sc->sc_ic.ic_ac, ifa); 902 break; 903 #endif 904 default: 905 error = an_init(ifp); 906 break; 907 } 908 break; 909 case SIOCSIFFLAGS: 910 if (ifp->if_flags & IFF_UP) { 911 if (sc->sc_enabled) { 912 /* 913 * To avoid rescanning another access point, 914 * do not call an_init() here. Instead, only 915 * reflect promisc mode settings. 916 */ 917 error = an_cmd(sc, AN_CMD_SET_MODE, 918 (ifp->if_flags & IFF_PROMISC) ? 0xffff : 0); 919 } else 920 error = an_init(ifp); 921 } else if (sc->sc_enabled) 922 an_stop(ifp, 1); 923 break; 924 case SIOCADDMULTI: 925 case SIOCDELMULTI: 926 /* The Aironet has no multicast filter. */ 927 error = 0; 928 break; 929 case SIOCS80211NWKEY: 930 error = an_set_nwkey(sc, (struct ieee80211_nwkey *)data); 931 break; 932 case SIOCG80211NWKEY: 933 error = an_get_nwkey(sc, (struct ieee80211_nwkey *)data); 934 break; 935 default: 936 error = ieee80211_ioctl(ifp, command, data); 937 break; 938 } 939 if (error == ENETRESET) { 940 if (sc->sc_enabled) 941 error = an_init(ifp); 942 else 943 error = 0; 944 } 945 splx(s); 946 return(error); 947 } 948 949 int 950 an_init(struct ifnet *ifp) 951 { 952 struct an_softc *sc = ifp->if_softc; 953 struct ieee80211com *ic = &sc->sc_ic; 954 int i, error, fid; 955 956 DPRINTF(("an_init: enabled %d\n", sc->sc_enabled)); 957 if (!sc->sc_enabled) { 958 if (sc->sc_enable) 959 (*sc->sc_enable)(sc); 960 an_wait(sc); 961 sc->sc_enabled = 1; 962 } else { 963 an_stop(ifp, 0); 964 if ((error = an_reset(sc)) != 0) { 965 printf("%s: failed to reset\n", ifp->if_xname); 966 an_stop(ifp, 1); 967 return error; 968 } 969 } 970 CSR_WRITE_2(sc, AN_SW0, AN_MAGIC); 971 972 /* Allocate the TX buffers */ 973 for (i = 0; i < AN_TX_RING_CNT; i++) { 974 if ((error = an_alloc_nicmem(sc, AN_TX_MAX_LEN, &fid)) != 0) { 975 printf("%s: failed to allocate nic memory\n", 976 ifp->if_xname); 977 an_stop(ifp, 1); 978 return error; 979 } 980 DPRINTF2(("an_init: txbuf %d allocated %x\n", i, fid)); 981 sc->sc_txd[i].d_fid = fid; 982 sc->sc_txd[i].d_inuse = 0; 983 } 984 sc->sc_txcur = sc->sc_txnext = 0; 985 986 IEEE80211_ADDR_COPY(sc->sc_config.an_macaddr, ic->ic_myaddr); 987 an_swap16((u_int16_t *)&sc->sc_config.an_macaddr, 3); 988 sc->sc_config.an_scanmode = AN_SCANMODE_ACTIVE; 989 sc->sc_config.an_authtype = AN_AUTHTYPE_OPEN; /*XXX*/ 990 if (ic->ic_flags & IEEE80211_F_WEPON) { 991 sc->sc_config.an_authtype |= 992 AN_AUTHTYPE_PRIVACY_IN_USE; 993 } 994 sc->sc_config.an_listen_interval = ic->ic_lintval; 995 sc->sc_config.an_beacon_period = ic->ic_lintval; 996 if (ic->ic_flags & IEEE80211_F_PMGTON) 997 sc->sc_config.an_psave_mode = AN_PSAVE_PSP; 998 else 999 sc->sc_config.an_psave_mode = AN_PSAVE_CAM; 1000 sc->sc_config.an_ds_channel = 1001 ieee80211_chan2ieee(ic, ic->ic_ibss_chan); 1002 1003 switch (ic->ic_opmode) { 1004 case IEEE80211_M_STA: 1005 sc->sc_config.an_opmode = 1006 AN_OPMODE_INFRASTRUCTURE_STATION; 1007 sc->sc_config.an_rxmode = AN_RXMODE_BC_MC_ADDR; 1008 break; 1009 #ifndef IEEE80211_STA_ONLY 1010 case IEEE80211_M_IBSS: 1011 sc->sc_config.an_opmode = AN_OPMODE_IBSS_ADHOC; 1012 sc->sc_config.an_rxmode = AN_RXMODE_BC_MC_ADDR; 1013 break; 1014 #endif 1015 case IEEE80211_M_MONITOR: 1016 sc->sc_config.an_opmode = 1017 AN_OPMODE_INFRASTRUCTURE_STATION; 1018 sc->sc_config.an_rxmode = 1019 AN_RXMODE_80211_MONITOR_ANYBSS; 1020 sc->sc_config.an_authtype = AN_AUTHTYPE_NONE; 1021 if (ic->ic_flags & IEEE80211_F_WEPON) 1022 sc->sc_config.an_authtype |= 1023 AN_AUTHTYPE_PRIVACY_IN_USE | 1024 AN_AUTHTYPE_ALLOW_UNENCRYPTED; 1025 break; 1026 default: 1027 printf("%s: bad opmode %d\n", ifp->if_xname, ic->ic_opmode); 1028 an_stop(ifp, 1); 1029 return EIO; 1030 } 1031 sc->sc_config.an_rxmode |= AN_RXMODE_NO_8023_HEADER; 1032 1033 /* Set the ssid list */ 1034 memset(&sc->sc_buf, 0, sizeof(sc->sc_buf.sc_ssidlist)); 1035 sc->sc_buf.sc_ssidlist.an_entry[0].an_ssid_len = 1036 ic->ic_des_esslen; 1037 if (ic->ic_des_esslen) 1038 memcpy(sc->sc_buf.sc_ssidlist.an_entry[0].an_ssid, 1039 ic->ic_des_essid, ic->ic_des_esslen); 1040 an_swap16((u_int16_t *)&sc->sc_buf.sc_ssidlist.an_entry[0].an_ssid, 16); 1041 if (an_write_rid(sc, AN_RID_SSIDLIST, &sc->sc_buf, 1042 sizeof(sc->sc_buf.sc_ssidlist)) != 0) { 1043 printf("%s: failed to write ssid list\n", ifp->if_xname); 1044 an_stop(ifp, 1); 1045 return error; 1046 } 1047 1048 /* Set the AP list */ 1049 memset(&sc->sc_buf, 0, sizeof(sc->sc_buf.sc_aplist)); 1050 (void)an_write_rid(sc, AN_RID_APLIST, &sc->sc_buf, 1051 sizeof(sc->sc_buf.sc_aplist)); 1052 1053 /* Set the encapsulation */ 1054 for (i = 0; i < AN_ENCAP_NENTS; i++) { 1055 sc->sc_buf.sc_encap.an_entry[i].an_ethertype = 0; 1056 sc->sc_buf.sc_encap.an_entry[i].an_action = 1057 AN_RXENCAP_RFC1024 | AN_TXENCAP_RFC1024; 1058 } 1059 (void)an_write_rid(sc, AN_RID_ENCAP, &sc->sc_buf, 1060 sizeof(sc->sc_buf.sc_encap)); 1061 1062 /* Set the WEP Keys */ 1063 if (ic->ic_flags & IEEE80211_F_WEPON) 1064 an_write_wepkey(sc, AN_RID_WEP_VOLATILE, sc->sc_wepkeys, 1065 sc->sc_tx_key); 1066 1067 /* Set the configuration */ 1068 if (an_write_rid(sc, AN_RID_GENCONFIG, &sc->sc_config, 1069 sizeof(sc->sc_config)) != 0) { 1070 printf("%s: failed to write config\n", ifp->if_xname); 1071 an_stop(ifp, 1); 1072 return error; 1073 } 1074 1075 /* Enable the MAC */ 1076 if (an_cmd(sc, AN_CMD_ENABLE, 0)) { 1077 printf("%s: failed to enable MAC\n", sc->sc_dev.dv_xname); 1078 an_stop(ifp, 1); 1079 return ENXIO; 1080 } 1081 if (ifp->if_flags & IFF_PROMISC) 1082 an_cmd(sc, AN_CMD_SET_MODE, 0xffff); 1083 1084 ifp->if_flags |= IFF_RUNNING; 1085 ifp->if_flags &= ~IFF_OACTIVE; 1086 ic->ic_state = IEEE80211_S_INIT; 1087 if (ic->ic_opmode == IEEE80211_M_MONITOR) 1088 ieee80211_new_state(ic, IEEE80211_S_RUN, -1); 1089 1090 /* enable interrupts */ 1091 CSR_WRITE_2(sc, AN_INT_EN, AN_INTRS); 1092 return 0; 1093 } 1094 1095 void 1096 an_start(struct ifnet *ifp) 1097 { 1098 struct an_softc *sc = (struct an_softc *)ifp->if_softc; 1099 struct ieee80211com *ic = &sc->sc_ic; 1100 struct ieee80211_node *ni; 1101 struct ieee80211_frame *wh; 1102 struct an_txframe frmhdr; 1103 struct mbuf *m; 1104 u_int16_t len; 1105 int cur, fid; 1106 1107 if (!sc->sc_enabled || sc->sc_invalid) { 1108 DPRINTF(("an_start: noop: enabled %d invalid %d\n", 1109 sc->sc_enabled, sc->sc_invalid)); 1110 return; 1111 } 1112 1113 memset(&frmhdr, 0, sizeof(frmhdr)); 1114 cur = sc->sc_txnext; 1115 for (;;) { 1116 if (ic->ic_state != IEEE80211_S_RUN) { 1117 DPRINTF(("an_start: not running %d\n", ic->ic_state)); 1118 break; 1119 } 1120 IFQ_POLL(&ifp->if_snd, m); 1121 if (m == NULL) { 1122 DPRINTF2(("an_start: no pending mbuf\n")); 1123 break; 1124 } 1125 if (sc->sc_txd[cur].d_inuse) { 1126 DPRINTF2(("an_start: %x/%d busy\n", 1127 sc->sc_txd[cur].d_fid, cur)); 1128 ifp->if_flags |= IFF_OACTIVE; 1129 break; 1130 } 1131 IFQ_DEQUEUE(&ifp->if_snd, m); 1132 ifp->if_opackets++; 1133 #if NBPFILTER > 0 1134 if (ifp->if_bpf) 1135 bpf_mtap(ifp->if_bpf, m, BPF_DIRECTION_OUT); 1136 #endif 1137 if ((m = ieee80211_encap(ifp, m, &ni)) == NULL) { 1138 ifp->if_oerrors++; 1139 continue; 1140 } 1141 if (ni != NULL) 1142 ieee80211_release_node(ic, ni); 1143 #if NBPFILTER > 0 1144 if (ic->ic_rawbpf) 1145 bpf_mtap(ic->ic_rawbpf, m, BPF_DIRECTION_OUT); 1146 #endif 1147 1148 wh = mtod(m, struct ieee80211_frame *); 1149 if (ic->ic_flags & IEEE80211_F_WEPON) 1150 wh->i_fc[1] |= IEEE80211_FC1_WEP; 1151 m_copydata(m, 0, sizeof(struct ieee80211_frame), 1152 (caddr_t)&frmhdr.an_whdr); 1153 an_swap16((u_int16_t *)&frmhdr.an_whdr, sizeof(struct ieee80211_frame)/2); 1154 1155 /* insert payload length in front of llc/snap */ 1156 len = htons(m->m_pkthdr.len - sizeof(struct ieee80211_frame)); 1157 m_adj(m, sizeof(struct ieee80211_frame) - sizeof(len)); 1158 if (mtod(m, u_long) & 0x01) 1159 memcpy(mtod(m, caddr_t), &len, sizeof(len)); 1160 else 1161 *mtod(m, u_int16_t *) = len; 1162 1163 /* 1164 * XXX Aironet firmware apparently convert the packet 1165 * with longer than 1500 bytes in length into LLC/SNAP. 1166 * If we have 1500 bytes in ethernet payload, it is 1167 * 1508 bytes including LLC/SNAP and will be inserted 1168 * additional LLC/SNAP header with 1501-1508 in its 1169 * ethertype !! 1170 * So we skip LLC/SNAP header and force firmware to 1171 * convert it to LLC/SNAP again. 1172 */ 1173 m_adj(m, sizeof(struct llc)); 1174 1175 frmhdr.an_tx_ctl = AN_TXCTL_80211; 1176 frmhdr.an_tx_payload_len = m->m_pkthdr.len; 1177 frmhdr.an_gaplen = AN_TXGAP_802_11; 1178 1179 if (ic->ic_fixed_rate != -1) 1180 frmhdr.an_tx_rate = 1181 ic->ic_sup_rates[IEEE80211_MODE_11B].rs_rates[ 1182 ic->ic_fixed_rate] & IEEE80211_RATE_VAL; 1183 else 1184 frmhdr.an_tx_rate = 0; 1185 1186 if (sizeof(frmhdr) + AN_TXGAP_802_11 + sizeof(len) + 1187 m->m_pkthdr.len > AN_TX_MAX_LEN) { 1188 ifp->if_oerrors++; 1189 m_freem(m); 1190 continue; 1191 } 1192 1193 #if NBPFILTER > 0 1194 if (sc->sc_drvbpf) { 1195 struct mbuf mb; 1196 struct an_tx_radiotap_header *tap = &sc->sc_txtap; 1197 1198 tap->at_rate = 1199 ic->ic_bss->ni_rates.rs_rates[ic->ic_bss->ni_txrate]; 1200 tap->at_chan_freq = 1201 ic->ic_bss->ni_chan->ic_freq; 1202 tap->at_chan_flags = 1203 ic->ic_bss->ni_chan->ic_flags; 1204 1205 mb.m_data = (caddr_t)tap; 1206 mb.m_len = sizeof(sc->sc_txtapu); 1207 mb.m_next = m; 1208 mb.m_nextpkt = NULL; 1209 mb.m_type = 0; 1210 mb.m_flags = 0; 1211 bpf_mtap(sc->sc_drvbpf, m, BPF_DIRECTION_OUT); 1212 } 1213 #endif 1214 1215 fid = sc->sc_txd[cur].d_fid; 1216 if (an_write_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr)) != 0) { 1217 ifp->if_oerrors++; 1218 m_freem(m); 1219 continue; 1220 } 1221 /* dummy write to avoid seek. */ 1222 an_write_bap(sc, fid, -1, &frmhdr, AN_TXGAP_802_11); 1223 an_mwrite_bap(sc, fid, -1, m, m->m_pkthdr.len); 1224 m_freem(m); 1225 1226 DPRINTF2(("an_start: send %d byte via %x/%d\n", 1227 ntohs(len) + sizeof(struct ieee80211_frame), 1228 fid, cur)); 1229 sc->sc_txd[cur].d_inuse = 1; 1230 if (an_cmd(sc, AN_CMD_TX, fid)) { 1231 printf("%s: xmit failed\n", ifp->if_xname); 1232 sc->sc_txd[cur].d_inuse = 0; 1233 continue; 1234 } 1235 sc->sc_tx_timer = 5; 1236 ifp->if_timer = 1; 1237 AN_INC(cur, AN_TX_RING_CNT); 1238 sc->sc_txnext = cur; 1239 } 1240 } 1241 1242 void 1243 an_stop(struct ifnet *ifp, int disable) 1244 { 1245 struct an_softc *sc = ifp->if_softc; 1246 int i, s; 1247 1248 if (!sc->sc_enabled) 1249 return; 1250 1251 DPRINTF(("an_stop: disable %d\n", disable)); 1252 1253 s = splnet(); 1254 ieee80211_new_state(&sc->sc_ic, IEEE80211_S_INIT, -1); 1255 if (!sc->sc_invalid) { 1256 an_cmd(sc, AN_CMD_FORCE_SYNCLOSS, 0); 1257 CSR_WRITE_2(sc, AN_INT_EN, 0); 1258 an_cmd(sc, AN_CMD_DISABLE, 0); 1259 1260 for (i = 0; i < AN_TX_RING_CNT; i++) 1261 an_cmd(sc, AN_CMD_DEALLOC_MEM, sc->sc_txd[i].d_fid); 1262 } 1263 1264 sc->sc_tx_timer = 0; 1265 ifp->if_timer = 0; 1266 ifp->if_flags &= ~(IFF_RUNNING|IFF_OACTIVE); 1267 1268 if (disable) { 1269 if (sc->sc_disable) 1270 (*sc->sc_disable)(sc); 1271 sc->sc_enabled = 0; 1272 } 1273 splx(s); 1274 } 1275 1276 void 1277 an_watchdog(struct ifnet *ifp) 1278 { 1279 struct an_softc *sc = ifp->if_softc; 1280 1281 if (!sc->sc_enabled) 1282 return; 1283 1284 if (sc->sc_tx_timer) { 1285 if (--sc->sc_tx_timer == 0) { 1286 printf("%s: device timeout\n", ifp->if_xname); 1287 ifp->if_oerrors++; 1288 an_init(ifp); 1289 return; 1290 } 1291 ifp->if_timer = 1; 1292 } 1293 ieee80211_watchdog(ifp); 1294 } 1295 1296 /* TBD factor with ieee80211_media_change */ 1297 int 1298 an_media_change(struct ifnet *ifp) 1299 { 1300 struct an_softc *sc = ifp->if_softc; 1301 struct ieee80211com *ic = &sc->sc_ic; 1302 struct ifmedia_entry *ime; 1303 enum ieee80211_opmode newmode; 1304 int i, rate, error = 0; 1305 1306 ime = ic->ic_media.ifm_cur; 1307 if (IFM_SUBTYPE(ime->ifm_media) == IFM_AUTO) { 1308 i = -1; 1309 } else { 1310 struct ieee80211_rateset *rs = 1311 &ic->ic_sup_rates[IEEE80211_MODE_11B]; 1312 rate = ieee80211_media2rate(ime->ifm_media); 1313 if (rate == 0) 1314 return EINVAL; 1315 for (i = 0; i < rs->rs_nrates; i++) { 1316 if ((rs->rs_rates[i] & IEEE80211_RATE_VAL) == rate) 1317 break; 1318 } 1319 if (i == rs->rs_nrates) 1320 return EINVAL; 1321 } 1322 if (ic->ic_fixed_rate != i) { 1323 ic->ic_fixed_rate = i; 1324 error = ENETRESET; 1325 } 1326 1327 #ifndef IEEE80211_STA_ONLY 1328 if (ime->ifm_media & IFM_IEEE80211_ADHOC) 1329 newmode = IEEE80211_M_IBSS; 1330 else if (ime->ifm_media & IFM_IEEE80211_HOSTAP) 1331 newmode = IEEE80211_M_HOSTAP; 1332 else 1333 #endif 1334 if (ime->ifm_media & IFM_IEEE80211_MONITOR) 1335 newmode = IEEE80211_M_MONITOR; 1336 else 1337 newmode = IEEE80211_M_STA; 1338 if (ic->ic_opmode != newmode) { 1339 ic->ic_opmode = newmode; 1340 error = ENETRESET; 1341 } 1342 if (error == ENETRESET) { 1343 if (sc->sc_enabled) 1344 error = an_init(ifp); 1345 else 1346 error = 0; 1347 } 1348 ifp->if_baudrate = ifmedia_baudrate(ic->ic_media.ifm_cur->ifm_media); 1349 1350 return error; 1351 } 1352 1353 void 1354 an_media_status(struct ifnet *ifp, struct ifmediareq *imr) 1355 { 1356 struct an_softc *sc = ifp->if_softc; 1357 struct ieee80211com *ic = &sc->sc_ic; 1358 int rate, buflen; 1359 1360 if (sc->sc_enabled == 0) { 1361 imr->ifm_active = IFM_IEEE80211 | IFM_NONE; 1362 imr->ifm_status = 0; 1363 return; 1364 } 1365 1366 imr->ifm_status = IFM_AVALID; 1367 imr->ifm_active = IFM_IEEE80211; 1368 if (ic->ic_state == IEEE80211_S_RUN) 1369 imr->ifm_status |= IFM_ACTIVE; 1370 buflen = sizeof(sc->sc_buf); 1371 if (ic->ic_fixed_rate != -1) 1372 rate = ic->ic_sup_rates[IEEE80211_MODE_11B].rs_rates[ 1373 ic->ic_fixed_rate] & IEEE80211_RATE_VAL; 1374 else if (an_read_rid(sc, AN_RID_STATUS, &sc->sc_buf, &buflen) != 0) 1375 rate = 0; 1376 else 1377 rate = sc->sc_buf.sc_status.an_current_tx_rate; 1378 imr->ifm_active |= ieee80211_rate2media(ic, rate, IEEE80211_MODE_11B); 1379 switch (ic->ic_opmode) { 1380 case IEEE80211_M_STA: 1381 break; 1382 #ifndef IEEE80211_STA_ONLY 1383 case IEEE80211_M_IBSS: 1384 imr->ifm_active |= IFM_IEEE80211_ADHOC; 1385 break; 1386 case IEEE80211_M_HOSTAP: 1387 imr->ifm_active |= IFM_IEEE80211_HOSTAP; 1388 break; 1389 #endif 1390 case IEEE80211_M_MONITOR: 1391 imr->ifm_active |= IFM_IEEE80211_MONITOR; 1392 break; 1393 default: 1394 break; 1395 } 1396 } 1397 1398 int 1399 an_set_nwkey(struct an_softc *sc, struct ieee80211_nwkey *nwkey) 1400 { 1401 int error; 1402 struct ieee80211com *ic = &sc->sc_ic; 1403 u_int16_t prevauth; 1404 1405 error = 0; 1406 prevauth = sc->sc_config.an_authtype; 1407 1408 switch (nwkey->i_wepon) { 1409 case IEEE80211_NWKEY_OPEN: 1410 sc->sc_config.an_authtype = AN_AUTHTYPE_OPEN; 1411 ic->ic_flags &= ~IEEE80211_F_WEPON; 1412 break; 1413 1414 case IEEE80211_NWKEY_WEP: 1415 case IEEE80211_NWKEY_WEP | IEEE80211_NWKEY_PERSIST: 1416 error = an_set_nwkey_wep(sc, nwkey); 1417 if (error == 0 || error == ENETRESET) { 1418 sc->sc_config.an_authtype = 1419 AN_AUTHTYPE_OPEN | AN_AUTHTYPE_PRIVACY_IN_USE; 1420 ic->ic_flags |= IEEE80211_F_WEPON; 1421 } 1422 break; 1423 1424 default: 1425 error = EINVAL; 1426 break; 1427 } 1428 if (error == 0 && prevauth != sc->sc_config.an_authtype) 1429 error = ENETRESET; 1430 return error; 1431 } 1432 1433 int 1434 an_set_nwkey_wep(struct an_softc *sc, struct ieee80211_nwkey *nwkey) 1435 { 1436 int i, txkey, anysetkey, needreset, error; 1437 struct an_wepkey keys[IEEE80211_WEP_NKID]; 1438 1439 error = 0; 1440 memset(keys, 0, sizeof(keys)); 1441 anysetkey = needreset = 0; 1442 1443 /* load argument and sanity check */ 1444 for (i = 0; i < IEEE80211_WEP_NKID; i++) { 1445 keys[i].an_wep_keylen = nwkey->i_key[i].i_keylen; 1446 if (keys[i].an_wep_keylen < 0) 1447 continue; 1448 if (keys[i].an_wep_keylen != 0 && 1449 keys[i].an_wep_keylen < IEEE80211_WEP_KEYLEN) 1450 return EINVAL; 1451 if (keys[i].an_wep_keylen > sizeof(keys[i].an_wep_key)) 1452 return EINVAL; 1453 if ((error = copyin(nwkey->i_key[i].i_keydat, 1454 keys[i].an_wep_key, keys[i].an_wep_keylen)) != 0) 1455 return error; 1456 anysetkey++; 1457 } 1458 txkey = nwkey->i_defkid - 1; 1459 if (txkey >= 0) { 1460 if (txkey >= IEEE80211_WEP_NKID) 1461 return EINVAL; 1462 /* default key must have a valid value */ 1463 if (keys[txkey].an_wep_keylen == 0 || 1464 (keys[txkey].an_wep_keylen < 0 && 1465 sc->sc_perskeylen[txkey] == 0)) 1466 return EINVAL; 1467 anysetkey++; 1468 } 1469 DPRINTF(("an_set_nwkey_wep: %s: %sold(%d:%d,%d,%d,%d) " 1470 "pers(%d:%d,%d,%d,%d) new(%d:%d,%d,%d,%d)\n", 1471 sc->sc_dev.dv_xname, 1472 ((nwkey->i_wepon & IEEE80211_NWKEY_PERSIST) ? "persist: " : ""), 1473 sc->sc_tx_key, 1474 sc->sc_wepkeys[0].an_wep_keylen, sc->sc_wepkeys[1].an_wep_keylen, 1475 sc->sc_wepkeys[2].an_wep_keylen, sc->sc_wepkeys[3].an_wep_keylen, 1476 sc->sc_tx_perskey, 1477 sc->sc_perskeylen[0], sc->sc_perskeylen[1], 1478 sc->sc_perskeylen[2], sc->sc_perskeylen[3], 1479 txkey, 1480 keys[0].an_wep_keylen, keys[1].an_wep_keylen, 1481 keys[2].an_wep_keylen, keys[3].an_wep_keylen)); 1482 if (!(nwkey->i_wepon & IEEE80211_NWKEY_PERSIST)) { 1483 /* set temporary keys */ 1484 sc->sc_tx_key = txkey; 1485 for (i = 0; i < IEEE80211_WEP_NKID; i++) { 1486 if (keys[i].an_wep_keylen < 0) 1487 continue; 1488 memcpy(&sc->sc_wepkeys[i], &keys[i], sizeof(keys[i])); 1489 } 1490 } else { 1491 /* set persist keys */ 1492 if (anysetkey) { 1493 /* prepare to write nvram */ 1494 if (!sc->sc_enabled) { 1495 if (sc->sc_enable) 1496 (*sc->sc_enable)(sc); 1497 an_wait(sc); 1498 sc->sc_enabled = 1; 1499 error = an_write_wepkey(sc, 1500 AN_RID_WEP_PERSISTENT, keys, txkey); 1501 if (sc->sc_disable) 1502 (*sc->sc_disable)(sc); 1503 sc->sc_enabled = 0; 1504 } else { 1505 an_cmd(sc, AN_CMD_DISABLE, 0); 1506 error = an_write_wepkey(sc, 1507 AN_RID_WEP_PERSISTENT, keys, txkey); 1508 an_cmd(sc, AN_CMD_ENABLE, 0); 1509 } 1510 if (error) 1511 return error; 1512 } 1513 if (txkey >= 0) 1514 sc->sc_tx_perskey = txkey; 1515 if (sc->sc_tx_key >= 0) { 1516 sc->sc_tx_key = -1; 1517 needreset++; 1518 } 1519 for (i = 0; i < IEEE80211_WEP_NKID; i++) { 1520 if (sc->sc_wepkeys[i].an_wep_keylen >= 0) { 1521 memset(&sc->sc_wepkeys[i].an_wep_key, 0, 1522 sizeof(sc->sc_wepkeys[i].an_wep_key)); 1523 sc->sc_wepkeys[i].an_wep_keylen = -1; 1524 needreset++; 1525 } 1526 if (keys[i].an_wep_keylen >= 0) 1527 sc->sc_perskeylen[i] = keys[i].an_wep_keylen; 1528 } 1529 } 1530 if (needreset) { 1531 /* firmware restart to reload persistent key */ 1532 an_reset(sc); 1533 } 1534 if (anysetkey || needreset) 1535 error = ENETRESET; 1536 return error; 1537 } 1538 1539 int 1540 an_get_nwkey(struct an_softc *sc, struct ieee80211_nwkey *nwkey) 1541 { 1542 int i, error; 1543 1544 error = 0; 1545 if (sc->sc_config.an_authtype & AN_AUTHTYPE_LEAP) 1546 nwkey->i_wepon = IEEE80211_NWKEY_EAP; 1547 else if (sc->sc_config.an_authtype & AN_AUTHTYPE_PRIVACY_IN_USE) 1548 nwkey->i_wepon = IEEE80211_NWKEY_WEP; 1549 else 1550 nwkey->i_wepon = IEEE80211_NWKEY_OPEN; 1551 if (sc->sc_tx_key == -1) 1552 nwkey->i_defkid = sc->sc_tx_perskey + 1; 1553 else 1554 nwkey->i_defkid = sc->sc_tx_key + 1; 1555 if (nwkey->i_key[0].i_keydat == NULL) 1556 return 0; 1557 for (i = 0; i < IEEE80211_WEP_NKID; i++) { 1558 if (nwkey->i_key[i].i_keydat == NULL) 1559 continue; 1560 /* do not show any keys to non-root user */ 1561 if ((error = suser(curproc, 0)) != 0) 1562 break; 1563 nwkey->i_key[i].i_keylen = sc->sc_wepkeys[i].an_wep_keylen; 1564 if (nwkey->i_key[i].i_keylen < 0) { 1565 if (sc->sc_perskeylen[i] == 0) 1566 nwkey->i_key[i].i_keylen = 0; 1567 continue; 1568 } 1569 if ((error = copyout(sc->sc_wepkeys[i].an_wep_key, 1570 nwkey->i_key[i].i_keydat, 1571 sc->sc_wepkeys[i].an_wep_keylen)) != 0) 1572 break; 1573 } 1574 return error; 1575 } 1576 1577 int 1578 an_write_wepkey(struct an_softc *sc, int type, struct an_wepkey *keys, int kid) 1579 { 1580 int i, error; 1581 struct an_rid_wepkey *akey; 1582 1583 error = 0; 1584 akey = &sc->sc_buf.sc_wepkey; 1585 for (i = 0; i < IEEE80211_WEP_NKID; i++) { 1586 memset(akey, 0, sizeof(struct an_rid_wepkey)); 1587 if (keys[i].an_wep_keylen < 0 || 1588 keys[i].an_wep_keylen > sizeof(akey->an_key)) 1589 continue; 1590 akey->an_key_len = keys[i].an_wep_keylen; 1591 akey->an_key_index = i; 1592 akey->an_mac_addr[0] = 1; /* default mac */ 1593 an_swap16((u_int16_t *)&akey->an_mac_addr, 3); 1594 memcpy(akey->an_key, keys[i].an_wep_key, keys[i].an_wep_keylen); 1595 an_swap16((u_int16_t *)&akey->an_key, 8); 1596 if ((error = an_write_rid(sc, type, akey, sizeof(*akey))) != 0) 1597 return error; 1598 } 1599 if (kid >= 0) { 1600 memset(akey, 0, sizeof(struct an_rid_wepkey)); 1601 akey->an_key_index = 0xffff; 1602 akey->an_mac_addr[0] = kid; 1603 an_swap16((u_int16_t *)&akey->an_mac_addr, 3); 1604 akey->an_key_len = 0; 1605 memset(akey->an_key, 0, sizeof(akey->an_key)); 1606 error = an_write_rid(sc, type, akey, sizeof(*akey)); 1607 } 1608 return error; 1609 } 1610 1611 int 1612 an_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg) 1613 { 1614 struct an_softc *sc = ic->ic_softc; 1615 struct ieee80211_node *ni = ic->ic_bss; 1616 enum ieee80211_state ostate; 1617 int buflen; 1618 1619 ostate = ic->ic_state; 1620 DPRINTF(("an_newstate: %s -> %s\n", ieee80211_state_name[ostate], 1621 ieee80211_state_name[nstate])); 1622 1623 switch (nstate) { 1624 case IEEE80211_S_INIT: 1625 ic->ic_flags &= ~IEEE80211_F_IBSSON; 1626 return (*sc->sc_newstate)(ic, nstate, arg); 1627 1628 case IEEE80211_S_RUN: 1629 buflen = sizeof(sc->sc_buf); 1630 an_read_rid(sc, AN_RID_STATUS, &sc->sc_buf, &buflen); 1631 an_swap16((u_int16_t *)&sc->sc_buf.sc_status.an_cur_bssid, 3); 1632 an_swap16((u_int16_t *)&sc->sc_buf.sc_status.an_ssid, 16); 1633 IEEE80211_ADDR_COPY(ni->ni_bssid, 1634 sc->sc_buf.sc_status.an_cur_bssid); 1635 IEEE80211_ADDR_COPY(ni->ni_macaddr, ni->ni_bssid); 1636 ni->ni_chan = &ic->ic_channels[ 1637 sc->sc_buf.sc_status.an_cur_channel]; 1638 ni->ni_esslen = sc->sc_buf.sc_status.an_ssidlen; 1639 if (ni->ni_esslen > IEEE80211_NWID_LEN) 1640 ni->ni_esslen = IEEE80211_NWID_LEN; /*XXX*/ 1641 memcpy(ni->ni_essid, sc->sc_buf.sc_status.an_ssid, 1642 ni->ni_esslen); 1643 ni->ni_rates = ic->ic_sup_rates[IEEE80211_MODE_11B]; /*XXX*/ 1644 if (ic->ic_if.if_flags & IFF_DEBUG) { 1645 printf("%s: ", sc->sc_dev.dv_xname); 1646 if (ic->ic_opmode == IEEE80211_M_STA) 1647 printf("associated "); 1648 else 1649 printf("synchronized "); 1650 printf("with %s ssid ", ether_sprintf(ni->ni_bssid)); 1651 ieee80211_print_essid(ni->ni_essid, ni->ni_esslen); 1652 printf(" channel %u start %uMb\n", 1653 sc->sc_buf.sc_status.an_cur_channel, 1654 sc->sc_buf.sc_status.an_current_tx_rate/2); 1655 } 1656 break; 1657 1658 default: 1659 break; 1660 } 1661 ic->ic_state = nstate; 1662 /* skip standard ieee80211 handling */ 1663 return 0; 1664 } 1665 1666 int 1667 an_detach(struct an_softc *sc) 1668 { 1669 struct ifnet *ifp = &sc->sc_ic.ic_if; 1670 int s; 1671 1672 if (!sc->sc_attached) 1673 return 0; 1674 1675 s = splnet(); 1676 sc->sc_invalid = 1; 1677 an_stop(ifp, 1); 1678 ifmedia_delete_instance(&sc->sc_ic.ic_media, IFM_INST_ANY); 1679 ieee80211_ifdetach(ifp); 1680 if_detach(ifp); 1681 splx(s); 1682 return 0; 1683 } 1684 1685