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