1 /* $NetBSD: awi.c,v 1.46 2002/09/03 14:54:00 onoe Exp $ */ 2 3 /*- 4 * Copyright (c) 1999,2000,2001 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Bill Sommerfeld 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the NetBSD 21 * Foundation, Inc. and its contributors. 22 * 4. Neither the name of The NetBSD Foundation nor the names of its 23 * contributors may be used to endorse or promote products derived 24 * from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 * POSSIBILITY OF SUCH DAMAGE. 37 */ 38 /* 39 * Driver for AMD 802.11 firmware. 40 * Uses am79c930 chip driver to talk to firmware running on the am79c930. 41 * 42 * More-or-less a generic ethernet-like if driver, with 802.11 gorp added. 43 */ 44 45 /* 46 * todo: 47 * - flush tx queue on resynch. 48 * - clear oactive on "down". 49 * - rewrite copy-into-mbuf code 50 * - mgmt state machine gets stuck retransmitting assoc requests. 51 * - multicast filter. 52 * - fix device reset so it's more likely to work 53 * - show status goo through ifmedia. 54 * 55 * more todo: 56 * - deal with more 802.11 frames. 57 * - send reassoc request 58 * - deal with reassoc response 59 * - send/deal with disassociation 60 * - deal with "full" access points (no room for me). 61 * - power save mode 62 * 63 * later: 64 * - SSID preferences 65 * - need ioctls for poking at the MIBs 66 * - implement ad-hoc mode (including bss creation). 67 * - decide when to do "ad hoc" vs. infrastructure mode (IFF_LINK flags?) 68 * (focus on inf. mode since that will be needed for ietf) 69 * - deal with DH vs. FH versions of the card 70 * - deal with faster cards (2mb/s) 71 * - ?WEP goo (mmm, rc4) (it looks not particularly useful). 72 * - ifmedia revision. 73 * - common 802.11 mibish things. 74 * - common 802.11 media layer. 75 */ 76 77 /* 78 * Driver for AMD 802.11 PCnetMobile firmware. 79 * Uses am79c930 chip driver to talk to firmware running on the am79c930. 80 * 81 * The initial version of the driver was written by 82 * Bill Sommerfeld <sommerfeld@netbsd.org>. 83 * Then the driver module completely rewritten to support cards with DS phy 84 * and to support adhoc mode by Atsushi Onoe <onoe@netbsd.org> 85 */ 86 87 #include <sys/cdefs.h> 88 __KERNEL_RCSID(0, "$NetBSD: awi.c,v 1.46 2002/09/03 14:54:00 onoe Exp $"); 89 90 #include "opt_inet.h" 91 #include "bpfilter.h" 92 93 #include <sys/param.h> 94 #include <sys/systm.h> 95 #include <sys/kernel.h> 96 #include <sys/mbuf.h> 97 #include <sys/malloc.h> 98 #include <sys/proc.h> 99 #include <sys/socket.h> 100 #include <sys/sockio.h> 101 #include <sys/errno.h> 102 #include <sys/device.h> 103 104 #include <net/if.h> 105 #include <net/if_dl.h> 106 #include <net/if_ether.h> 107 #include <net/if_media.h> 108 #include <net/if_llc.h> 109 #include <net/if_ieee80211.h> 110 111 #ifdef INET 112 #include <netinet/in.h> 113 #include <netinet/in_systm.h> 114 #ifdef __NetBSD__ 115 #include <netinet/if_inarp.h> 116 #else 117 #include <netinet/if_ether.h> 118 #endif 119 #endif 120 121 #if NBPFILTER > 0 122 #include <net/bpf.h> 123 #endif 124 125 #include <machine/cpu.h> 126 #include <machine/bus.h> 127 #include <machine/intr.h> 128 129 #include <dev/ic/am79c930reg.h> 130 #include <dev/ic/am79c930var.h> 131 #include <dev/ic/awireg.h> 132 #include <dev/ic/awivar.h> 133 134 static int awi_init(struct ifnet *); 135 static void awi_stop(struct ifnet *, int); 136 static void awi_start(struct ifnet *); 137 static void awi_watchdog(struct ifnet *); 138 static int awi_ioctl(struct ifnet *, u_long, caddr_t); 139 static int awi_media_change(struct ifnet *); 140 static void awi_media_status(struct ifnet *, struct ifmediareq *); 141 static int awi_mode_init(struct awi_softc *); 142 static void awi_rx_int(struct awi_softc *); 143 static void awi_tx_int(struct awi_softc *); 144 static struct mbuf *awi_devget(struct awi_softc *, u_int32_t, u_int16_t); 145 static int awi_hw_init(struct awi_softc *); 146 static int awi_init_mibs(struct awi_softc *); 147 static int awi_chan_check(void *, u_char *); 148 static int awi_mib(struct awi_softc *, u_int8_t, u_int8_t, int); 149 static int awi_cmd(struct awi_softc *, u_int8_t, int); 150 static int awi_cmd_wait(struct awi_softc *); 151 static void awi_cmd_done(struct awi_softc *); 152 static int awi_next_txd(struct awi_softc *, int, u_int32_t *, u_int32_t *); 153 static int awi_lock(struct awi_softc *); 154 static void awi_unlock(struct awi_softc *); 155 static int awi_intr_lock(struct awi_softc *); 156 static void awi_intr_unlock(struct awi_softc *); 157 static int awi_newstate(void *, enum ieee80211_state); 158 static struct mbuf *awi_ether_encap(struct awi_softc *, struct mbuf *); 159 static struct mbuf *awi_ether_modcap(struct awi_softc *, struct mbuf *); 160 161 /* unalligned little endian access */ 162 #define LE_READ_2(p) \ 163 ((((u_int8_t *)(p))[0] ) | (((u_int8_t *)(p))[1] << 8)) 164 #define LE_READ_4(p) \ 165 ((((u_int8_t *)(p))[0] ) | (((u_int8_t *)(p))[1] << 8) | \ 166 (((u_int8_t *)(p))[2] << 16) | (((u_int8_t *)(p))[3] << 24)) 167 #define LE_WRITE_2(p, v) \ 168 ((((u_int8_t *)(p))[0] = (((u_int32_t)(v) ) & 0xff)), \ 169 (((u_int8_t *)(p))[1] = (((u_int32_t)(v) >> 8) & 0xff))) 170 #define LE_WRITE_4(p, v) \ 171 ((((u_int8_t *)(p))[0] = (((u_int32_t)(v) ) & 0xff)), \ 172 (((u_int8_t *)(p))[1] = (((u_int32_t)(v) >> 8) & 0xff)), \ 173 (((u_int8_t *)(p))[2] = (((u_int32_t)(v) >> 16) & 0xff)), \ 174 (((u_int8_t *)(p))[3] = (((u_int32_t)(v) >> 24) & 0xff))) 175 176 struct awi_chanset awi_chanset[] = { 177 /* PHY type domain min max def */ 178 { AWI_PHY_TYPE_FH, AWI_REG_DOMAIN_JP, 6, 17, 6 }, 179 { AWI_PHY_TYPE_FH, AWI_REG_DOMAIN_ES, 0, 26, 1 }, 180 { AWI_PHY_TYPE_FH, AWI_REG_DOMAIN_FR, 0, 32, 1 }, 181 { AWI_PHY_TYPE_FH, AWI_REG_DOMAIN_US, 0, 77, 1 }, 182 { AWI_PHY_TYPE_FH, AWI_REG_DOMAIN_CA, 0, 77, 1 }, 183 { AWI_PHY_TYPE_FH, AWI_REG_DOMAIN_EU, 0, 77, 1 }, 184 { AWI_PHY_TYPE_DS, AWI_REG_DOMAIN_JP, 14, 14, 14 }, 185 { AWI_PHY_TYPE_DS, AWI_REG_DOMAIN_ES, 10, 11, 10 }, 186 { AWI_PHY_TYPE_DS, AWI_REG_DOMAIN_FR, 10, 13, 10 }, 187 { AWI_PHY_TYPE_DS, AWI_REG_DOMAIN_US, 1, 11, 3 }, 188 { AWI_PHY_TYPE_DS, AWI_REG_DOMAIN_CA, 1, 11, 3 }, 189 { AWI_PHY_TYPE_DS, AWI_REG_DOMAIN_EU, 1, 13, 3 }, 190 { 0, 0 } 191 }; 192 193 #ifdef AWI_DEBUG 194 int awi_debug; 195 196 #define DPRINTF(X) if (awi_debug) printf X 197 #define DPRINTF2(X) if (awi_debug > 1) printf X 198 #else 199 #define DPRINTF(X) 200 #define DPRINTF2(X) 201 #endif 202 203 int 204 awi_attach(struct awi_softc *sc) 205 { 206 struct ieee80211com *ic = &sc->sc_ic; 207 struct ifnet *ifp = &ic->ic_if; 208 int s, i, error, nrate; 209 int mword; 210 struct ifmediareq imr; 211 212 s = splnet(); 213 sc->sc_busy = 1; 214 sc->sc_substate = AWI_ST_NONE; 215 if ((error = awi_hw_init(sc)) != 0) { 216 sc->sc_invalid = 1; 217 splx(s); 218 return error; 219 } 220 error = awi_init_mibs(sc); 221 if (error != 0) { 222 sc->sc_invalid = 1; 223 splx(s); 224 return error; 225 } 226 ifp->if_softc = sc; 227 ifp->if_flags = 228 IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST | IFF_NOTRAILERS; 229 ifp->if_ioctl = awi_ioctl; 230 ifp->if_start = awi_start; 231 ifp->if_init = awi_init; 232 ifp->if_stop = awi_stop; 233 ifp->if_watchdog = awi_watchdog; 234 IFQ_SET_READY(&ifp->if_snd); 235 memcpy(ifp->if_xname, sc->sc_dev.dv_xname, IFNAMSIZ); 236 237 if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH) 238 ic->ic_flags = IEEE80211_F_FH; 239 else 240 ic->ic_flags = IEEE80211_F_DS; 241 ic->ic_flags |= 242 IEEE80211_F_HASWEP | IEEE80211_F_HASIBSS | IEEE80211_F_HASHAP; 243 ic->ic_state = IEEE80211_S_INIT; 244 ic->ic_newstate = awi_newstate; 245 ic->ic_chancheck = awi_chan_check; 246 nrate = sc->sc_mib_phy.aSuprt_Data_Rates[1]; 247 memcpy(ic->ic_sup_rates, sc->sc_mib_phy.aSuprt_Data_Rates + 2, nrate); 248 memcpy(ic->ic_myaddr, sc->sc_mib_addr.aMAC_Address, IEEE80211_ADDR_LEN); 249 250 printf("%s: IEEE802.11 %s %dMbps (firmware %s)\n", 251 sc->sc_dev.dv_xname, 252 sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH ? "FH" : "DS", 253 (ic->ic_sup_rates[nrate - 1] & IEEE80211_RATE_VAL) / 2, 254 sc->sc_banner); 255 printf("%s: 802.11 address: %s\n", sc->sc_dev.dv_xname, 256 ether_sprintf(ic->ic_myaddr)); 257 258 if_attach(ifp); 259 ieee80211_ifattach(ifp); 260 261 /* probe request is handled by hardware */ 262 ic->ic_send_mgmt[IEEE80211_FC0_SUBTYPE_PROBE_REQ 263 >> IEEE80211_FC0_SUBTYPE_SHIFT] = NULL; 264 ic->ic_recv_mgmt[IEEE80211_FC0_SUBTYPE_PROBE_REQ 265 >> IEEE80211_FC0_SUBTYPE_SHIFT] = NULL; 266 267 ifmedia_init(&sc->sc_media, 0, awi_media_change, awi_media_status); 268 #define ADD(s, o) ifmedia_add(&sc->sc_media, \ 269 IFM_MAKEWORD(IFM_IEEE80211, (s), (o), 0), 0, NULL) 270 ADD(IFM_AUTO, 0); /* infra mode */ 271 ADD(IFM_AUTO, IFM_FLAG0); /* melco compat mode */ 272 ADD(IFM_AUTO, IFM_IEEE80211_ADHOC); /* IBSS mode */ 273 if (sc->sc_mib_phy.IEEE_PHY_Type != AWI_PHY_TYPE_FH) 274 ADD(IFM_AUTO, IFM_IEEE80211_ADHOC | IFM_FLAG0); 275 /* lucent compat mode */ 276 ADD(IFM_AUTO, IFM_IEEE80211_HOSTAP); 277 for (i = 0; i < nrate; i++) { 278 mword = ieee80211_rate2media(ic->ic_sup_rates[i], 279 (ic->ic_flags & (IEEE80211_F_FH | IEEE80211_F_DS))); 280 if (mword == 0) 281 continue; 282 ADD(mword, 0); 283 ADD(mword, IFM_FLAG0); 284 ADD(mword, IFM_IEEE80211_ADHOC); 285 if (sc->sc_mib_phy.IEEE_PHY_Type != AWI_PHY_TYPE_FH) 286 ADD(mword, IFM_IEEE80211_ADHOC | IFM_FLAG0); 287 ADD(mword, IFM_IEEE80211_HOSTAP); 288 } 289 #undef ADD 290 awi_media_status(ifp, &imr); 291 ifmedia_set(&sc->sc_media, imr.ifm_active); 292 293 if ((sc->sc_sdhook = shutdownhook_establish(awi_shutdown, sc)) == NULL) 294 printf("%s: WARNING: unable to establish shutdown hook\n", 295 sc->sc_dev.dv_xname); 296 if ((sc->sc_powerhook = powerhook_establish(awi_power, sc)) == NULL) 297 printf("%s: WARNING: unable to establish power hook\n", 298 sc->sc_dev.dv_xname); 299 sc->sc_attached = 1; 300 splx(s); 301 302 /* ready to accept ioctl */ 303 awi_unlock(sc); 304 305 return 0; 306 } 307 308 int 309 awi_detach(struct awi_softc *sc) 310 { 311 struct ifnet *ifp = &sc->sc_ic.ic_if; 312 int s; 313 314 if (!sc->sc_attached) 315 return 0; 316 317 s = splnet(); 318 sc->sc_invalid = 1; 319 awi_stop(ifp, 1); 320 while (sc->sc_sleep_cnt > 0) { 321 wakeup(sc); 322 (void)tsleep(sc, PWAIT, "awidet", 1); 323 } 324 ifmedia_delete_instance(&sc->sc_media, IFM_INST_ANY); 325 ieee80211_ifdetach(ifp); 326 if_detach(ifp); 327 shutdownhook_disestablish(sc->sc_sdhook); 328 powerhook_disestablish(sc->sc_powerhook); 329 splx(s); 330 return 0; 331 } 332 333 int 334 awi_activate(struct device *self, enum devact act) 335 { 336 struct awi_softc *sc = (struct awi_softc *)self; 337 struct ifnet *ifp = &sc->sc_ic.ic_if; 338 int s, error = 0; 339 340 s = splnet(); 341 switch (act) { 342 case DVACT_ACTIVATE: 343 error = EOPNOTSUPP; 344 break; 345 case DVACT_DEACTIVATE: 346 sc->sc_invalid = 1; 347 if_deactivate(ifp); 348 break; 349 } 350 splx(s); 351 return error; 352 } 353 354 void 355 awi_power(int why, void *arg) 356 { 357 struct awi_softc *sc = arg; 358 struct ifnet *ifp = &sc->sc_ic.ic_if; 359 int s; 360 int ocansleep; 361 362 DPRINTF(("awi_power: %d\n", why)); 363 s = splnet(); 364 ocansleep = sc->sc_cansleep; 365 sc->sc_cansleep = 0; 366 switch (why) { 367 case PWR_SUSPEND: 368 case PWR_STANDBY: 369 awi_stop(ifp, 1); 370 break; 371 case PWR_RESUME: 372 if (ifp->if_flags & IFF_UP) { 373 awi_init(ifp); 374 (void)awi_intr(sc); /* make sure */ 375 } 376 break; 377 case PWR_SOFTSUSPEND: 378 case PWR_SOFTSTANDBY: 379 case PWR_SOFTRESUME: 380 break; 381 } 382 sc->sc_cansleep = ocansleep; 383 splx(s); 384 } 385 386 void 387 awi_shutdown(void *arg) 388 { 389 struct awi_softc *sc = arg; 390 struct ifnet *ifp = &sc->sc_ic.ic_if; 391 392 if (sc->sc_attached) 393 awi_stop(ifp, 1); 394 } 395 396 int 397 awi_intr(void *arg) 398 { 399 struct awi_softc *sc = arg; 400 u_int16_t status; 401 int error, handled = 0, ocansleep; 402 #ifdef AWI_DEBUG 403 static const char *intname[] = { 404 "CMD", "RX", "TX", "SCAN_CMPLT", 405 "CFP_START", "DTIM", "CFP_ENDING", "GROGGY", 406 "TXDATA", "TXBCAST", "TXPS", "TXCF", 407 "TXMGT", "#13", "RXDATA", "RXMGT" 408 }; 409 #endif 410 411 if (!sc->sc_enabled || !sc->sc_enab_intr || sc->sc_invalid) 412 return 0; 413 414 am79c930_gcr_setbits(&sc->sc_chip, 415 AM79C930_GCR_DISPWDN | AM79C930_GCR_ECINT); 416 awi_write_1(sc, AWI_DIS_PWRDN, 1); 417 ocansleep = sc->sc_cansleep; 418 sc->sc_cansleep = 0; 419 420 for (;;) { 421 if ((error = awi_intr_lock(sc)) != 0) 422 break; 423 status = awi_read_1(sc, AWI_INTSTAT); 424 awi_write_1(sc, AWI_INTSTAT, 0); 425 awi_write_1(sc, AWI_INTSTAT, 0); 426 status |= awi_read_1(sc, AWI_INTSTAT2) << 8; 427 awi_write_1(sc, AWI_INTSTAT2, 0); 428 DELAY(10); 429 awi_intr_unlock(sc); 430 if (!sc->sc_cmd_inprog) 431 status &= ~AWI_INT_CMD; /* make sure */ 432 if (status == 0) 433 break; 434 #ifdef AWI_DEBUG 435 if (awi_debug > 1) { 436 int i; 437 438 printf("awi_intr: status 0x%04x", status); 439 for (i = 0; i < sizeof(intname)/sizeof(intname[0]); 440 i++) { 441 if (status & (1 << i)) 442 printf(" %s", intname[i]); 443 } 444 printf("\n"); 445 } 446 #endif 447 handled = 1; 448 if (status & AWI_INT_RX) 449 awi_rx_int(sc); 450 if (status & AWI_INT_TX) 451 awi_tx_int(sc); 452 if (status & AWI_INT_CMD) 453 awi_cmd_done(sc); 454 if (status & AWI_INT_SCAN_CMPLT) { 455 if (sc->sc_ic.ic_state == IEEE80211_S_SCAN && 456 sc->sc_substate == AWI_ST_NONE) 457 ieee80211_next_scan(&sc->sc_ic.ic_if); 458 } 459 } 460 sc->sc_cansleep = ocansleep; 461 am79c930_gcr_clearbits(&sc->sc_chip, AM79C930_GCR_DISPWDN); 462 awi_write_1(sc, AWI_DIS_PWRDN, 0); 463 return handled; 464 } 465 466 static int 467 awi_init(struct ifnet *ifp) 468 { 469 struct awi_softc *sc = ifp->if_softc; 470 struct ieee80211com *ic = &sc->sc_ic; 471 struct ieee80211_bss *bs = &ic->ic_bss; 472 int i, error; 473 474 DPRINTF(("awi_init: enabled=%d\n", sc->sc_enabled)); 475 if (sc->sc_enabled) { 476 awi_stop(ifp, 0); 477 } else { 478 if (sc->sc_enable) 479 (*sc->sc_enable)(sc); 480 sc->sc_enabled = 1; 481 if ((error = awi_hw_init(sc)) != 0) { 482 awi_stop(ifp, 1); 483 return error; 484 } 485 } 486 ic->ic_state = IEEE80211_S_INIT; 487 488 sc->sc_mib_local.Network_Mode = 489 (ic->ic_flags & IEEE80211_F_ADHOC) ? 0 : 1; 490 sc->sc_mib_local.Acting_as_AP = 491 (ic->ic_flags & IEEE80211_F_HOSTAP) ? 1 : 0; 492 memset(&sc->sc_mib_mac.aDesired_ESS_ID, 0, AWI_ESS_ID_SIZE); 493 sc->sc_mib_mac.aDesired_ESS_ID[0] = IEEE80211_ELEMID_SSID; 494 sc->sc_mib_mac.aDesired_ESS_ID[1] = ic->ic_des_esslen; 495 memcpy(&sc->sc_mib_mac.aDesired_ESS_ID[2], ic->ic_des_essid, 496 ic->ic_des_esslen); 497 498 if ((error = awi_mode_init(sc)) != 0) { 499 DPRINTF(("awi_init: awi_mode_init failed %d\n", error)); 500 awi_stop(ifp, 1); 501 return error; 502 } 503 504 /* start transmitter */ 505 sc->sc_txdone = sc->sc_txnext = sc->sc_txbase; 506 awi_write_4(sc, sc->sc_txbase + AWI_TXD_START, 0); 507 awi_write_4(sc, sc->sc_txbase + AWI_TXD_NEXT, 0); 508 awi_write_4(sc, sc->sc_txbase + AWI_TXD_LENGTH, 0); 509 awi_write_1(sc, sc->sc_txbase + AWI_TXD_RATE, 0); 510 awi_write_4(sc, sc->sc_txbase + AWI_TXD_NDA, 0); 511 awi_write_4(sc, sc->sc_txbase + AWI_TXD_NRA, 0); 512 awi_write_1(sc, sc->sc_txbase + AWI_TXD_STATE, 0); 513 awi_write_4(sc, AWI_CA_TX_DATA, sc->sc_txbase); 514 awi_write_4(sc, AWI_CA_TX_MGT, 0); 515 awi_write_4(sc, AWI_CA_TX_BCAST, 0); 516 awi_write_4(sc, AWI_CA_TX_PS, 0); 517 awi_write_4(sc, AWI_CA_TX_CF, 0); 518 if ((error = awi_cmd(sc, AWI_CMD_INIT_TX, AWI_WAIT)) != 0) { 519 DPRINTF(("awi_init: failed to start transmitter: %d\n", error)); 520 awi_stop(ifp, 1); 521 return error; 522 } 523 524 /* start receiver */ 525 if ((error = awi_cmd(sc, AWI_CMD_INIT_RX, AWI_WAIT)) != 0) { 526 DPRINTF(("awi_init: failed to start receiver: %d\n", error)); 527 awi_stop(ifp, 1); 528 return error; 529 } 530 sc->sc_rxdoff = awi_read_4(sc, AWI_CA_IRX_DATA_DESC); 531 sc->sc_rxmoff = awi_read_4(sc, AWI_CA_IRX_PS_DESC); 532 533 ifp->if_flags |= IFF_RUNNING; 534 ifp->if_flags &= ~IFF_OACTIVE; 535 536 if (((ic->ic_flags & IEEE80211_F_ADHOC) && sc->sc_no_bssid) || 537 (ic->ic_flags & IEEE80211_F_HOSTAP)) { 538 bs->bs_chan = ic->ic_ibss_chan; 539 bs->bs_intval = ic->ic_lintval; 540 bs->bs_rssi = 0; 541 bs->bs_rstamp = 0; 542 memset(bs->bs_tstamp, 0, sizeof(bs->bs_tstamp)); 543 bs->bs_nrate = 0; 544 for (i = 0; i < IEEE80211_RATE_SIZE; i++) { 545 if (ic->ic_sup_rates[i]) 546 bs->bs_rates[bs->bs_nrate++] = 547 ic->ic_sup_rates[i]; 548 } 549 memcpy(bs->bs_macaddr, ic->ic_myaddr, IEEE80211_ADDR_LEN); 550 if (ic->ic_flags & IEEE80211_F_HOSTAP) { 551 memcpy(bs->bs_bssid, ic->ic_myaddr, IEEE80211_ADDR_LEN); 552 bs->bs_esslen = ic->ic_des_esslen; 553 memcpy(bs->bs_essid, ic->ic_des_essid, bs->bs_esslen); 554 bs->bs_capinfo = IEEE80211_CAPINFO_ESS; 555 if (ic->ic_flags & IEEE80211_F_FH) { 556 bs->bs_fhdwell = 200; /* XXX */ 557 bs->bs_fhindex = 1; 558 } 559 } else { 560 bs->bs_capinfo = IEEE80211_CAPINFO_IBSS; 561 memset(bs->bs_bssid, 0, IEEE80211_ADDR_LEN); 562 bs->bs_esslen = 0; 563 } 564 if (ic->ic_flags & IEEE80211_F_WEPON) 565 bs->bs_capinfo |= IEEE80211_CAPINFO_PRIVACY; 566 ic->ic_flags |= IEEE80211_F_SIBSS; 567 ic->ic_state = IEEE80211_S_SCAN; /*XXX*/ 568 sc->sc_substate = AWI_ST_NONE; 569 ieee80211_new_state(&ic->ic_if, IEEE80211_S_RUN, -1); 570 } else { 571 bs->bs_chan = sc->sc_cur_chan; 572 ieee80211_new_state(&ic->ic_if, IEEE80211_S_SCAN, -1); 573 } 574 return 0; 575 } 576 577 static void 578 awi_stop(struct ifnet *ifp, int disable) 579 { 580 struct awi_softc *sc = ifp->if_softc; 581 582 if (!sc->sc_enabled) 583 return; 584 585 DPRINTF(("awi_stop(%d)\n", disable)); 586 587 ieee80211_new_state(&sc->sc_ic.ic_if, IEEE80211_S_INIT, -1); 588 589 if (!sc->sc_invalid) { 590 if (sc->sc_cmd_inprog) 591 (void)awi_cmd_wait(sc); 592 (void)awi_cmd(sc, AWI_CMD_KILL_RX, AWI_WAIT); 593 sc->sc_cmd_inprog = AWI_CMD_FLUSH_TX; 594 awi_write_1(sc, AWI_CA_FTX_DATA, 1); 595 awi_write_1(sc, AWI_CA_FTX_MGT, 0); 596 awi_write_1(sc, AWI_CA_FTX_BCAST, 0); 597 awi_write_1(sc, AWI_CA_FTX_PS, 0); 598 awi_write_1(sc, AWI_CA_FTX_CF, 0); 599 (void)awi_cmd(sc, AWI_CMD_FLUSH_TX, AWI_WAIT); 600 } 601 ifp->if_flags &= ~(IFF_RUNNING|IFF_OACTIVE); 602 ifp->if_timer = 0; 603 sc->sc_tx_timer = sc->sc_rx_timer = 0; 604 if (sc->sc_rxpend != NULL) { 605 m_freem(sc->sc_rxpend); 606 sc->sc_rxpend = NULL; 607 } 608 IFQ_PURGE(&ifp->if_snd); 609 610 if (disable) { 611 if (sc->sc_disable) 612 (*sc->sc_disable)(sc); 613 sc->sc_enabled = 0; 614 } 615 } 616 617 static void 618 awi_start(struct ifnet *ifp) 619 { 620 struct awi_softc *sc = ifp->if_softc; 621 struct ieee80211com *ic = &sc->sc_ic; 622 struct mbuf *m, *m0; 623 int len, dowep; 624 u_int32_t txd, frame, ntxd; 625 u_int8_t rate; 626 627 if (!sc->sc_enabled || sc->sc_invalid) 628 return; 629 630 for (;;) { 631 txd = sc->sc_txnext; 632 IF_POLL(&ic->ic_mgtq, m0); 633 dowep = 0; 634 if (m0 != NULL) { 635 len = m0->m_pkthdr.len; 636 if (awi_next_txd(sc, len, &frame, &ntxd)) { 637 ifp->if_flags |= IFF_OACTIVE; 638 break; 639 } 640 IF_DEQUEUE(&ic->ic_mgtq, m0); 641 } else { 642 if (ic->ic_state != IEEE80211_S_RUN) 643 break; 644 IFQ_POLL(&ifp->if_snd, m0); 645 if (m0 == NULL) 646 break; 647 /* 648 * Need to calculate the real length to determine 649 * if the transmit buffer has a room for the packet. 650 */ 651 len = m0->m_pkthdr.len + sizeof(struct ieee80211_frame); 652 if (!(ifp->if_flags & IFF_LINK0) && !sc->sc_adhoc_ap) 653 len += sizeof(struct llc) - 654 sizeof(struct ether_header); 655 if (ic->ic_flags & IEEE80211_F_WEPON) { 656 dowep = 1; 657 len += IEEE80211_WEP_IVLEN + 658 IEEE80211_WEP_KIDLEN + IEEE80211_WEP_CRCLEN; 659 } 660 if (awi_next_txd(sc, len, &frame, &ntxd)) { 661 ifp->if_flags |= IFF_OACTIVE; 662 break; 663 } 664 IFQ_DEQUEUE(&ifp->if_snd, m0); 665 ifp->if_opackets++; 666 #if NBPFILTER > 0 667 if (ifp->if_bpf) 668 bpf_mtap(ifp->if_bpf, m0); 669 #endif 670 if ((ifp->if_flags & IFF_LINK0) || sc->sc_adhoc_ap) 671 m0 = awi_ether_encap(sc, m0); 672 else 673 m0 = ieee80211_encap(ifp, m0); 674 if (m0 == NULL) { 675 ifp->if_oerrors++; 676 continue; 677 } 678 } 679 #if NBPFILTER > 0 680 if (ic->ic_rawbpf) 681 bpf_mtap(ic->ic_rawbpf, m0); 682 #endif 683 if (dowep) { 684 if ((m0 = ieee80211_wep_crypt(ifp, m0, 1)) == NULL) { 685 ifp->if_oerrors++; 686 continue; 687 } 688 } 689 #ifdef DIAGNOSTIC 690 if (m0->m_pkthdr.len != len) { 691 printf("%s: length %d should be %d\n", 692 ifp->if_xname, m0->m_pkthdr.len, len); 693 m_freem(m0); 694 ifp->if_oerrors++; 695 continue; 696 } 697 #endif 698 699 if ((ifp->if_flags & IFF_DEBUG) && (ifp->if_flags & IFF_LINK2)) 700 ieee80211_dump_pkt(m0->m_data, m0->m_len, 701 ic->ic_bss.bs_rates[ic->ic_bss.bs_txrate] & 702 IEEE80211_RATE_VAL, -1); 703 704 for (m = m0, len = 0; m != NULL; m = m->m_next) { 705 awi_write_bytes(sc, frame + len, mtod(m, u_int8_t *), 706 m->m_len); 707 len += m->m_len; 708 } 709 m_freem(m0); 710 rate = (ic->ic_bss.bs_rates[ic->ic_bss.bs_txrate] & 711 IEEE80211_RATE_VAL) * 5; 712 awi_write_1(sc, ntxd + AWI_TXD_STATE, 0); 713 awi_write_4(sc, txd + AWI_TXD_START, frame); 714 awi_write_4(sc, txd + AWI_TXD_NEXT, ntxd); 715 awi_write_4(sc, txd + AWI_TXD_LENGTH, len); 716 awi_write_1(sc, txd + AWI_TXD_RATE, rate); 717 awi_write_4(sc, txd + AWI_TXD_NDA, 0); 718 awi_write_4(sc, txd + AWI_TXD_NRA, 0); 719 awi_write_1(sc, txd + AWI_TXD_STATE, AWI_TXD_ST_OWN); 720 sc->sc_txnext = ntxd; 721 722 sc->sc_tx_timer = 5; 723 ifp->if_timer = 1; 724 } 725 } 726 727 static void 728 awi_watchdog(struct ifnet *ifp) 729 { 730 struct awi_softc *sc = ifp->if_softc; 731 u_int32_t prevdone; 732 int ocansleep; 733 734 ifp->if_timer = 0; 735 if (!sc->sc_enabled || sc->sc_invalid) 736 return; 737 738 ocansleep = sc->sc_cansleep; 739 sc->sc_cansleep = 0; 740 if (sc->sc_tx_timer) { 741 if (--sc->sc_tx_timer == 0) { 742 printf("%s: device timeout\n", ifp->if_xname); 743 prevdone = sc->sc_txdone; 744 awi_tx_int(sc); 745 if (sc->sc_txdone == prevdone) { 746 ifp->if_oerrors++; 747 awi_init(ifp); 748 goto out; 749 } 750 } 751 ifp->if_timer = 1; 752 } 753 if (sc->sc_rx_timer) { 754 if (--sc->sc_rx_timer == 0) { 755 if (sc->sc_ic.ic_state == IEEE80211_S_RUN) { 756 ieee80211_new_state(ifp, IEEE80211_S_SCAN, -1); 757 goto out; 758 } 759 } else 760 ifp->if_timer = 1; 761 } 762 /* TODO: rate control */ 763 ieee80211_watchdog(ifp); 764 out: 765 sc->sc_cansleep = ocansleep; 766 } 767 768 static int 769 awi_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 770 { 771 struct awi_softc *sc = ifp->if_softc; 772 struct ifreq *ifr = (struct ifreq *)data; 773 int s, error; 774 775 s = splnet(); 776 /* serialize ioctl, since we may sleep */ 777 if ((error = awi_lock(sc)) != 0) 778 goto cantlock; 779 780 switch (cmd) { 781 case SIOCSIFFLAGS: 782 if (ifp->if_flags & IFF_UP) { 783 if (sc->sc_enabled) { 784 /* 785 * To avoid rescanning another access point, 786 * do not call awi_init() here. Instead, 787 * only reflect promisc mode settings. 788 */ 789 error = awi_mode_init(sc); 790 } else 791 error = awi_init(ifp); 792 } else if (sc->sc_enabled) 793 awi_stop(ifp, 1); 794 break; 795 case SIOCSIFMEDIA: 796 case SIOCGIFMEDIA: 797 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd); 798 break; 799 case SIOCADDMULTI: 800 case SIOCDELMULTI: 801 error = (cmd == SIOCADDMULTI) ? 802 ether_addmulti(ifr, &sc->sc_ic.ic_ec) : 803 ether_delmulti(ifr, &sc->sc_ic.ic_ec); 804 if (error == ENETRESET) { 805 /* do not rescan */ 806 if (sc->sc_enabled) 807 error = awi_mode_init(sc); 808 else 809 error = 0; 810 } 811 break; 812 default: 813 error = ieee80211_ioctl(ifp, cmd, data); 814 if (error == ENETRESET) { 815 if (sc->sc_enabled) 816 error = awi_init(ifp); 817 else 818 error = 0; 819 } 820 break; 821 } 822 awi_unlock(sc); 823 cantlock: 824 splx(s); 825 return error; 826 } 827 828 /* 829 * Called from ifmedia_ioctl via awi_ioctl with lock obtained. 830 */ 831 static int 832 awi_media_change(struct ifnet *ifp) 833 { 834 struct awi_softc *sc = ifp->if_softc; 835 struct ieee80211com *ic = &sc->sc_ic; 836 struct ifmedia_entry *ime; 837 int i, rate, error = 0; 838 839 ime = sc->sc_media.ifm_cur; 840 if (IFM_SUBTYPE(ime->ifm_media) == IFM_AUTO) { 841 ic->ic_fixed_rate = -1; 842 } else { 843 rate = ieee80211_media2rate(ime->ifm_media, 844 (ic->ic_flags & (IEEE80211_F_FH | IEEE80211_F_DS))); 845 if (rate == 0) 846 return EINVAL; 847 for (i = 0; i < IEEE80211_RATE_SIZE; i++) { 848 if ((ic->ic_sup_rates[i] & IEEE80211_RATE_VAL) == rate) 849 break; 850 } 851 if (i == IEEE80211_RATE_SIZE) 852 return EINVAL; 853 ic->ic_fixed_rate = i; 854 } 855 856 /* 857 * ADHOC,-FLAG0 ADHOC|~HOSTAP, !no_bssid, !adhoc_ap IBSS 858 * ADHOC, FLAG0 ADHOC|~HOSTAP, no_bssid, !adhoc_ap WaveLAN adhoc 859 * -ADHOC, FLAG0 ADHOC|~HOSTAP, !no_bssid, adhoc_ap Melco old AP 860 * also LINK0 861 * -ADHOC,HOSTAP ~ADHOC|HOSTAP, !no_bssid, !adhoc_ap HostAP 862 * -ADHOC,-FLAG0 ~ADHOC|~HOSTAP, !no_bssid, !adhoc_ap Infra 863 */ 864 if (ime->ifm_media & IFM_IEEE80211_ADHOC) { 865 if ((ic->ic_flags & IEEE80211_F_ADHOC) == 0 || 866 (ic->ic_flags & IEEE80211_F_IBSSON) == 0 || 867 (ic->ic_flags & IEEE80211_F_HOSTAP) || 868 sc->sc_adhoc_ap) { 869 ic->ic_flags |= IEEE80211_F_ADHOC | IEEE80211_F_IBSSON; 870 ic->ic_flags &= ~IEEE80211_F_HOSTAP; 871 sc->sc_adhoc_ap = 0; 872 error = ENETRESET; 873 } 874 if (sc->sc_mib_phy.IEEE_PHY_Type != AWI_PHY_TYPE_FH && 875 (ime->ifm_media & IFM_FLAG0)) { 876 if (sc->sc_no_bssid == 0) { 877 sc->sc_no_bssid = 1; 878 error = ENETRESET; 879 } 880 } else { 881 if (sc->sc_no_bssid) { 882 sc->sc_no_bssid = 0; 883 error = ENETRESET; 884 } 885 } 886 } else if (ime->ifm_media & IFM_FLAG0) { 887 if ((ic->ic_flags & IEEE80211_F_ADHOC) == 0 || 888 (ic->ic_flags & IEEE80211_F_IBSSON) || 889 (ic->ic_flags & IEEE80211_F_HOSTAP) || 890 sc->sc_no_bssid || !sc->sc_adhoc_ap) { 891 ic->ic_flags |= IEEE80211_F_ADHOC; 892 ic->ic_flags &= 893 ~(IEEE80211_F_IBSSON | IEEE80211_F_HOSTAP); 894 sc->sc_no_bssid = 0; 895 sc->sc_adhoc_ap = 1; 896 error = ENETRESET; 897 } 898 } else if (ime->ifm_media & IFM_IEEE80211_HOSTAP) { 899 if ((ic->ic_flags & IEEE80211_F_ADHOC) || 900 (ic->ic_flags & IEEE80211_F_IBSSON) || 901 (ic->ic_flags & IEEE80211_F_HOSTAP) == 0 || 902 sc->sc_no_bssid || sc->sc_adhoc_ap) { 903 ic->ic_flags |= IEEE80211_F_HOSTAP; 904 ic->ic_flags &= 905 ~(IEEE80211_F_ADHOC | IEEE80211_F_IBSSON); 906 sc->sc_no_bssid = 0; 907 sc->sc_adhoc_ap = 0; 908 error = ENETRESET; 909 } 910 } else { 911 if ((ic->ic_flags & IEEE80211_F_ADHOC) || 912 (ic->ic_flags & IEEE80211_F_IBSSON) || 913 (ic->ic_flags & IEEE80211_F_HOSTAP) || 914 sc->sc_no_bssid || sc->sc_adhoc_ap) { 915 ic->ic_flags &= ~(IEEE80211_F_ADHOC | 916 IEEE80211_F_IBSSON | IEEE80211_F_HOSTAP); 917 sc->sc_no_bssid = 0; 918 sc->sc_adhoc_ap = 0; 919 error = ENETRESET; 920 } 921 } 922 if (error == ENETRESET) { 923 if (sc->sc_enabled) 924 error = awi_init(ifp); 925 else 926 error = 0; 927 } 928 return error; 929 } 930 931 static void 932 awi_media_status(struct ifnet *ifp, struct ifmediareq *imr) 933 { 934 struct awi_softc *sc = ifp->if_softc; 935 struct ieee80211com *ic = &sc->sc_ic; 936 int rate; 937 938 imr->ifm_status = IFM_AVALID; 939 if (ic->ic_state == IEEE80211_S_RUN) 940 imr->ifm_status |= IFM_ACTIVE; 941 imr->ifm_active = IFM_IEEE80211; 942 if (ic->ic_state == IEEE80211_S_RUN) 943 rate = ic->ic_bss.bs_rates[ic->ic_bss.bs_txrate] & 944 IEEE80211_RATE_VAL; 945 else { 946 if (ic->ic_fixed_rate == -1) 947 rate = 0; 948 else 949 rate = ic->ic_sup_rates[ic->ic_fixed_rate] & 950 IEEE80211_RATE_VAL; 951 } 952 imr->ifm_active |= ieee80211_rate2media(rate, 953 (ic->ic_flags & (IEEE80211_F_FH | IEEE80211_F_DS))); 954 if (ic->ic_flags & IEEE80211_F_HOSTAP) 955 imr->ifm_active |= IFM_IEEE80211_HOSTAP; 956 else if (ic->ic_flags & IEEE80211_F_ADHOC) { 957 if (sc->sc_adhoc_ap) 958 imr->ifm_active |= IFM_FLAG0; 959 else { 960 imr->ifm_active |= IFM_IEEE80211_ADHOC; 961 if (sc->sc_no_bssid) 962 imr->ifm_active |= IFM_FLAG0; 963 } 964 } 965 } 966 967 static int 968 awi_mode_init(struct awi_softc *sc) 969 { 970 struct ifnet *ifp = &sc->sc_ic.ic_if; 971 int n, error; 972 struct ether_multi *enm; 973 struct ether_multistep step; 974 975 /* reinitialize muticast filter */ 976 n = 0; 977 sc->sc_mib_local.Accept_All_Multicast_Dis = 0; 978 if ((sc->sc_ic.ic_flags & IEEE80211_F_HOSTAP) == 0 && 979 (ifp->if_flags & IFF_PROMISC)) { 980 sc->sc_mib_mac.aPromiscuous_Enable = 1; 981 goto set_mib; 982 } 983 sc->sc_mib_mac.aPromiscuous_Enable = 0; 984 ETHER_FIRST_MULTI(step, &sc->sc_ic.ic_ec, enm); 985 while (enm != NULL) { 986 if (n == AWI_GROUP_ADDR_SIZE || 987 memcmp(enm->enm_addrlo, enm->enm_addrhi, IEEE80211_ADDR_LEN) 988 != 0) 989 goto set_mib; 990 memcpy(sc->sc_mib_addr.aGroup_Addresses[n], enm->enm_addrlo, 991 IEEE80211_ADDR_LEN); 992 n++; 993 ETHER_NEXT_MULTI(step, enm); 994 } 995 for (; n < AWI_GROUP_ADDR_SIZE; n++) 996 memset(sc->sc_mib_addr.aGroup_Addresses[n], 0, IEEE80211_ADDR_LEN); 997 sc->sc_mib_local.Accept_All_Multicast_Dis = 1; 998 999 set_mib: 1000 if (sc->sc_mib_local.Accept_All_Multicast_Dis) 1001 ifp->if_flags &= ~IFF_ALLMULTI; 1002 else 1003 ifp->if_flags |= IFF_ALLMULTI; 1004 sc->sc_mib_mgt.Wep_Required = 1005 (sc->sc_ic.ic_flags & IEEE80211_F_WEPON) ? AWI_WEP_ON : AWI_WEP_OFF; 1006 1007 if ((error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_LOCAL, AWI_WAIT)) || 1008 (error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_ADDR, AWI_WAIT)) || 1009 (error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_MAC, AWI_WAIT)) || 1010 (error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_MGT, AWI_WAIT)) || 1011 (error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_PHY, AWI_WAIT))) { 1012 DPRINTF(("awi_mode_init: MIB set failed: %d\n", error)); 1013 return error; 1014 } 1015 return 0; 1016 } 1017 1018 static void 1019 awi_rx_int(struct awi_softc *sc) 1020 { 1021 struct ifnet *ifp = &sc->sc_ic.ic_if; 1022 u_int8_t state, rate, rssi; 1023 u_int16_t len; 1024 u_int32_t frame, next, rstamp, rxoff; 1025 struct mbuf *m; 1026 1027 rxoff = sc->sc_rxdoff; 1028 for (;;) { 1029 state = awi_read_1(sc, rxoff + AWI_RXD_HOST_DESC_STATE); 1030 if (state & AWI_RXD_ST_OWN) 1031 break; 1032 if (!(state & AWI_RXD_ST_CONSUMED)) { 1033 if (sc->sc_substate != AWI_ST_NONE) 1034 goto rx_next; 1035 if (state & AWI_RXD_ST_RXERROR) { 1036 ifp->if_ierrors++; 1037 goto rx_next; 1038 } 1039 len = awi_read_2(sc, rxoff + AWI_RXD_LEN); 1040 rate = awi_read_1(sc, rxoff + AWI_RXD_RATE); 1041 rssi = awi_read_1(sc, rxoff + AWI_RXD_RSSI); 1042 frame = awi_read_4(sc, rxoff + AWI_RXD_START_FRAME) & 1043 0x7fff; 1044 rstamp = awi_read_4(sc, rxoff + AWI_RXD_LOCALTIME); 1045 m = awi_devget(sc, frame, len); 1046 if (m == NULL) { 1047 ifp->if_ierrors++; 1048 goto rx_next; 1049 } 1050 if (state & AWI_RXD_ST_LF) { 1051 /* TODO check my bss */ 1052 if (!(sc->sc_ic.ic_flags & IEEE80211_F_SIBSS) && 1053 sc->sc_ic.ic_state == IEEE80211_S_RUN) { 1054 sc->sc_rx_timer = 10; 1055 ifp->if_timer = 1; 1056 } 1057 if ((ifp->if_flags & IFF_DEBUG) && 1058 (ifp->if_flags & IFF_LINK2)) 1059 ieee80211_dump_pkt(m->m_data, m->m_len, 1060 rate / 5, rssi); 1061 if ((ifp->if_flags & IFF_LINK0) || 1062 sc->sc_adhoc_ap) 1063 m = awi_ether_modcap(sc, m); 1064 if (m == NULL) 1065 ifp->if_ierrors++; 1066 else 1067 ieee80211_input(ifp, m, rssi, rstamp); 1068 } else 1069 sc->sc_rxpend = m; 1070 rx_next: 1071 state |= AWI_RXD_ST_CONSUMED; 1072 awi_write_1(sc, rxoff + AWI_RXD_HOST_DESC_STATE, state); 1073 } 1074 next = awi_read_4(sc, rxoff + AWI_RXD_NEXT); 1075 if (next & AWI_RXD_NEXT_LAST) 1076 break; 1077 /* make sure the next pointer is correct */ 1078 if (next != awi_read_4(sc, rxoff + AWI_RXD_NEXT)) 1079 break; 1080 state |= AWI_RXD_ST_OWN; 1081 awi_write_1(sc, rxoff + AWI_RXD_HOST_DESC_STATE, state); 1082 rxoff = next & 0x7fff; 1083 } 1084 sc->sc_rxdoff = rxoff; 1085 } 1086 1087 static void 1088 awi_tx_int(struct awi_softc *sc) 1089 { 1090 struct ifnet *ifp = &sc->sc_ic.ic_if; 1091 u_int8_t flags; 1092 1093 while (sc->sc_txdone != sc->sc_txnext) { 1094 flags = awi_read_1(sc, sc->sc_txdone + AWI_TXD_STATE); 1095 if ((flags & AWI_TXD_ST_OWN) || !(flags & AWI_TXD_ST_DONE)) 1096 break; 1097 if (flags & AWI_TXD_ST_ERROR) 1098 ifp->if_oerrors++; 1099 sc->sc_txdone = awi_read_4(sc, sc->sc_txdone + AWI_TXD_NEXT) & 1100 0x7fff; 1101 } 1102 DPRINTF2(("awi_txint: txdone %d txnext %d txbase %d txend %d\n", 1103 sc->sc_txdone, sc->sc_txnext, sc->sc_txbase, sc->sc_txend)); 1104 sc->sc_tx_timer = 0; 1105 ifp->if_flags &= ~IFF_OACTIVE; 1106 awi_start(ifp); 1107 } 1108 1109 static struct mbuf * 1110 awi_devget(struct awi_softc *sc, u_int32_t off, u_int16_t len) 1111 { 1112 struct ifnet *ifp = &sc->sc_ic.ic_if; 1113 struct mbuf *m; 1114 struct mbuf *top, **mp; 1115 u_int tlen; 1116 1117 top = sc->sc_rxpend; 1118 mp = ⊤ 1119 if (top != NULL) { 1120 sc->sc_rxpend = NULL; 1121 top->m_pkthdr.len += len; 1122 m = top; 1123 while (*mp != NULL) { 1124 m = *mp; 1125 mp = &m->m_next; 1126 } 1127 if (m->m_flags & M_EXT) 1128 tlen = m->m_ext.ext_size; 1129 else if (m->m_flags & M_PKTHDR) 1130 tlen = MHLEN; 1131 else 1132 tlen = MLEN; 1133 tlen -= m->m_len; 1134 if (tlen > len) 1135 tlen = len; 1136 awi_read_bytes(sc, off, mtod(m, u_int8_t *) + m->m_len, tlen); 1137 off += tlen; 1138 len -= tlen; 1139 } 1140 1141 while (len > 0) { 1142 if (top == NULL) { 1143 MGETHDR(m, M_DONTWAIT, MT_DATA); 1144 if (m == NULL) 1145 return NULL; 1146 m->m_pkthdr.rcvif = ifp; 1147 m->m_pkthdr.len = len; 1148 m->m_len = MHLEN; 1149 m->m_flags |= M_HASFCS; 1150 } else { 1151 MGET(m, M_DONTWAIT, MT_DATA); 1152 if (m == NULL) { 1153 m_freem(top); 1154 return NULL; 1155 } 1156 m->m_len = MLEN; 1157 } 1158 if (len >= MINCLSIZE) { 1159 MCLGET(m, M_DONTWAIT); 1160 if (m->m_flags & M_EXT) 1161 m->m_len = m->m_ext.ext_size; 1162 } 1163 if (top == NULL) { 1164 int hdrlen = sizeof(struct ieee80211_frame) + 1165 sizeof(struct llc); 1166 caddr_t newdata = (caddr_t) 1167 ALIGN(m->m_data + hdrlen) - hdrlen; 1168 m->m_len -= newdata - m->m_data; 1169 m->m_data = newdata; 1170 } 1171 if (m->m_len > len) 1172 m->m_len = len; 1173 awi_read_bytes(sc, off, mtod(m, u_int8_t *), m->m_len); 1174 off += m->m_len; 1175 len -= m->m_len; 1176 *mp = m; 1177 mp = &m->m_next; 1178 } 1179 return top; 1180 } 1181 1182 /* 1183 * Initialize hardware and start firmware to accept commands. 1184 * Called everytime after power on firmware. 1185 */ 1186 1187 static int 1188 awi_hw_init(struct awi_softc *sc) 1189 { 1190 u_int8_t status; 1191 u_int16_t intmask; 1192 int i, error; 1193 1194 sc->sc_enab_intr = 0; 1195 sc->sc_invalid = 0; /* XXX: really? */ 1196 awi_drvstate(sc, AWI_DRV_RESET); 1197 1198 /* reset firmware */ 1199 am79c930_gcr_setbits(&sc->sc_chip, AM79C930_GCR_CORESET); 1200 DELAY(100); 1201 awi_write_1(sc, AWI_SELFTEST, 0); 1202 awi_write_1(sc, AWI_CMD, 0); 1203 awi_write_1(sc, AWI_BANNER, 0); 1204 am79c930_gcr_clearbits(&sc->sc_chip, AM79C930_GCR_CORESET); 1205 DELAY(100); 1206 1207 /* wait for selftest completion */ 1208 for (i = 0; ; i++) { 1209 if (i >= AWI_SELFTEST_TIMEOUT*hz/1000) { 1210 printf("%s: failed to complete selftest (timeout)\n", 1211 sc->sc_dev.dv_xname); 1212 return ENXIO; 1213 } 1214 status = awi_read_1(sc, AWI_SELFTEST); 1215 if ((status & 0xf0) == 0xf0) 1216 break; 1217 if (sc->sc_cansleep) { 1218 sc->sc_sleep_cnt++; 1219 (void)tsleep(sc, PWAIT, "awitst", 1); 1220 sc->sc_sleep_cnt--; 1221 } else { 1222 DELAY(1000*1000/hz); 1223 } 1224 } 1225 if (status != AWI_SELFTEST_PASSED) { 1226 printf("%s: failed to complete selftest (code %x)\n", 1227 sc->sc_dev.dv_xname, status); 1228 return ENXIO; 1229 } 1230 1231 /* check banner to confirm firmware write it */ 1232 awi_read_bytes(sc, AWI_BANNER, sc->sc_banner, AWI_BANNER_LEN); 1233 if (memcmp(sc->sc_banner, "PCnetMobile:", 12) != 0) { 1234 printf("%s: failed to complete selftest (bad banner)\n", 1235 sc->sc_dev.dv_xname); 1236 for (i = 0; i < AWI_BANNER_LEN; i++) 1237 printf("%s%02x", i ? ":" : "\t", sc->sc_banner[i]); 1238 printf("\n"); 1239 return ENXIO; 1240 } 1241 1242 /* initializing interrupt */ 1243 sc->sc_enab_intr = 1; 1244 error = awi_intr_lock(sc); 1245 if (error) 1246 return error; 1247 intmask = AWI_INT_GROGGY | AWI_INT_SCAN_CMPLT | 1248 AWI_INT_TX | AWI_INT_RX | AWI_INT_CMD; 1249 awi_write_1(sc, AWI_INTMASK, ~intmask & 0xff); 1250 awi_write_1(sc, AWI_INTMASK2, 0); 1251 awi_write_1(sc, AWI_INTSTAT, 0); 1252 awi_write_1(sc, AWI_INTSTAT2, 0); 1253 awi_intr_unlock(sc); 1254 am79c930_gcr_setbits(&sc->sc_chip, AM79C930_GCR_ENECINT); 1255 1256 /* issuing interface test command */ 1257 error = awi_cmd(sc, AWI_CMD_NOP, AWI_WAIT); 1258 if (error) { 1259 printf("%s: failed to complete selftest", sc->sc_dev.dv_xname); 1260 if (error == ENXIO) 1261 printf(" (no hardware)\n"); 1262 else if (error != EWOULDBLOCK) 1263 printf(" (error %d)\n", error); 1264 else if (sc->sc_cansleep) 1265 printf(" (lost interrupt)\n"); 1266 else 1267 printf(" (command timeout)\n"); 1268 return error; 1269 } 1270 1271 /* Initialize VBM */ 1272 awi_write_1(sc, AWI_VBM_OFFSET, 0); 1273 awi_write_1(sc, AWI_VBM_LENGTH, 1); 1274 awi_write_1(sc, AWI_VBM_BITMAP, 0); 1275 return 0; 1276 } 1277 1278 /* 1279 * Extract the factory default MIB value from firmware and assign the driver 1280 * default value. 1281 * Called once at attaching the interface. 1282 */ 1283 1284 static int 1285 awi_init_mibs(struct awi_softc *sc) 1286 { 1287 int i, error; 1288 struct awi_chanset *cs; 1289 1290 if ((error = awi_mib(sc, AWI_CMD_GET_MIB, AWI_MIB_LOCAL, AWI_WAIT)) || 1291 (error = awi_mib(sc, AWI_CMD_GET_MIB, AWI_MIB_ADDR, AWI_WAIT)) || 1292 (error = awi_mib(sc, AWI_CMD_GET_MIB, AWI_MIB_MAC, AWI_WAIT)) || 1293 (error = awi_mib(sc, AWI_CMD_GET_MIB, AWI_MIB_MGT, AWI_WAIT)) || 1294 (error = awi_mib(sc, AWI_CMD_GET_MIB, AWI_MIB_PHY, AWI_WAIT))) { 1295 printf("%s: failed to get default mib value (error %d)\n", 1296 sc->sc_dev.dv_xname, error); 1297 return error; 1298 } 1299 1300 memset(&sc->sc_ic.ic_chan_avail, 0, sizeof(sc->sc_ic.ic_chan_avail)); 1301 for (cs = awi_chanset; ; cs++) { 1302 if (cs->cs_type == 0) { 1303 printf("%s: failed to set available channel\n", 1304 sc->sc_dev.dv_xname); 1305 return ENXIO; 1306 } 1307 if (cs->cs_type == sc->sc_mib_phy.IEEE_PHY_Type && 1308 cs->cs_region == sc->sc_mib_phy.aCurrent_Reg_Domain) 1309 break; 1310 } 1311 if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH) { 1312 for (i = cs->cs_min; i <= cs->cs_max; i++) { 1313 setbit(sc->sc_ic.ic_chan_avail, 1314 IEEE80211_FH_CHAN(i % 3 + 1, i)); 1315 /* 1316 * According to the IEEE 802.11 specification, 1317 * hop pattern parameter for FH phy should be 1318 * incremented by 3 for given hop chanset, i.e., 1319 * the chanset parameter is calculated for given 1320 * hop patter. However, BayStack 650 Access Points 1321 * apparently use fixed hop chanset parameter value 1322 * 1 for any hop pattern. So we also try this 1323 * combination of hop chanset and pattern. 1324 */ 1325 setbit(sc->sc_ic.ic_chan_avail, 1326 IEEE80211_FH_CHAN(1, i)); 1327 } 1328 } else { 1329 for (i = cs->cs_min; i <= cs->cs_max; i++) 1330 setbit(sc->sc_ic.ic_chan_avail, i); 1331 } 1332 sc->sc_cur_chan = cs->cs_def; 1333 1334 sc->sc_mib_local.Fragmentation_Dis = 1; 1335 sc->sc_mib_local.Add_PLCP_Dis = 0; 1336 sc->sc_mib_local.MAC_Hdr_Prsv = 1; 1337 sc->sc_mib_local.Rx_Mgmt_Que_En = 0; 1338 sc->sc_mib_local.Re_Assembly_Dis = 1; 1339 sc->sc_mib_local.Strip_PLCP_Dis = 0; 1340 sc->sc_mib_local.Power_Saving_Mode_Dis = 1; 1341 sc->sc_mib_local.Accept_All_Multicast_Dis = 1; 1342 sc->sc_mib_local.Check_Seq_Cntl_Dis = 1; 1343 sc->sc_mib_local.Flush_CFP_Queue_On_CF_End = 0; 1344 sc->sc_mib_local.Network_Mode = 1; 1345 sc->sc_mib_local.PWD_Lvl = 0; 1346 sc->sc_mib_local.CFP_Mode = 0; 1347 1348 /* allocate buffers */ 1349 sc->sc_txbase = AWI_BUFFERS; 1350 sc->sc_txend = sc->sc_txbase + 1351 (AWI_TXD_SIZE + sizeof(struct ieee80211_frame) + 1352 sizeof(struct ether_header) + ETHERMTU) * AWI_NTXBUFS; 1353 LE_WRITE_4(&sc->sc_mib_local.Tx_Buffer_Offset, sc->sc_txbase); 1354 LE_WRITE_4(&sc->sc_mib_local.Tx_Buffer_Size, 1355 sc->sc_txend - sc->sc_txbase); 1356 LE_WRITE_4(&sc->sc_mib_local.Rx_Buffer_Offset, sc->sc_txend); 1357 LE_WRITE_4(&sc->sc_mib_local.Rx_Buffer_Size, 1358 AWI_BUFFERS_END - sc->sc_txend); 1359 sc->sc_mib_local.Acting_as_AP = 0; 1360 sc->sc_mib_local.Fill_CFP = 0; 1361 1362 memset(&sc->sc_mib_mac.aDesired_ESS_ID, 0, AWI_ESS_ID_SIZE); 1363 sc->sc_mib_mac.aDesired_ESS_ID[0] = IEEE80211_ELEMID_SSID; 1364 1365 sc->sc_mib_mgt.aPower_Mgt_Mode = 0; 1366 sc->sc_mib_mgt.aDTIM_Period = 1; 1367 LE_WRITE_2(&sc->sc_mib_mgt.aATIM_Window, 0); 1368 return 0; 1369 } 1370 1371 static int 1372 awi_chan_check(void *arg, u_char *chanreq) 1373 { 1374 struct awi_softc *sc = arg; 1375 int i; 1376 struct awi_chanset *cs; 1377 u_char chanlist[(IEEE80211_CHAN_MAX+1)/NBBY]; 1378 1379 for (cs = awi_chanset; cs->cs_type != 0; cs++) { 1380 if (cs->cs_type != sc->sc_mib_phy.IEEE_PHY_Type) 1381 continue; 1382 memset(chanlist, 0, sizeof(chanlist)); 1383 for (i = 0; ; i++) { 1384 if (i == IEEE80211_CHAN_MAX) { 1385 sc->sc_mib_phy.aCurrent_Reg_Domain = 1386 cs->cs_region; 1387 memcpy(sc->sc_ic.ic_chan_avail, chanlist, 1388 sizeof(sc->sc_ic.ic_chan_avail)); 1389 sc->sc_ic.ic_bss.bs_chan = cs->cs_def; 1390 sc->sc_cur_chan = cs->cs_def; 1391 return 0; 1392 } 1393 if (i >= cs->cs_min && i <= cs->cs_max) 1394 setbit(chanlist, i); 1395 else if (isset(chanreq, i)) 1396 break; 1397 } 1398 } 1399 return EINVAL; 1400 } 1401 1402 static int 1403 awi_mib(struct awi_softc *sc, u_int8_t cmd, u_int8_t mib, int wflag) 1404 { 1405 int error; 1406 u_int8_t size, *ptr; 1407 1408 switch (mib) { 1409 case AWI_MIB_LOCAL: 1410 ptr = (u_int8_t *)&sc->sc_mib_local; 1411 size = sizeof(sc->sc_mib_local); 1412 break; 1413 case AWI_MIB_ADDR: 1414 ptr = (u_int8_t *)&sc->sc_mib_addr; 1415 size = sizeof(sc->sc_mib_addr); 1416 break; 1417 case AWI_MIB_MAC: 1418 ptr = (u_int8_t *)&sc->sc_mib_mac; 1419 size = sizeof(sc->sc_mib_mac); 1420 break; 1421 case AWI_MIB_STAT: 1422 ptr = (u_int8_t *)&sc->sc_mib_stat; 1423 size = sizeof(sc->sc_mib_stat); 1424 break; 1425 case AWI_MIB_MGT: 1426 ptr = (u_int8_t *)&sc->sc_mib_mgt; 1427 size = sizeof(sc->sc_mib_mgt); 1428 break; 1429 case AWI_MIB_PHY: 1430 ptr = (u_int8_t *)&sc->sc_mib_phy; 1431 size = sizeof(sc->sc_mib_phy); 1432 break; 1433 default: 1434 return EINVAL; 1435 } 1436 if (sc->sc_cmd_inprog) { 1437 if ((error = awi_cmd_wait(sc)) != 0) { 1438 if (error == EWOULDBLOCK) 1439 DPRINTF(("awi_mib: cmd %d inprog", 1440 sc->sc_cmd_inprog)); 1441 return error; 1442 } 1443 } 1444 sc->sc_cmd_inprog = cmd; 1445 if (cmd == AWI_CMD_SET_MIB) 1446 awi_write_bytes(sc, AWI_CA_MIB_DATA, ptr, size); 1447 awi_write_1(sc, AWI_CA_MIB_TYPE, mib); 1448 awi_write_1(sc, AWI_CA_MIB_SIZE, size); 1449 awi_write_1(sc, AWI_CA_MIB_INDEX, 0); 1450 if ((error = awi_cmd(sc, cmd, wflag)) != 0) 1451 return error; 1452 if (cmd == AWI_CMD_GET_MIB) { 1453 awi_read_bytes(sc, AWI_CA_MIB_DATA, ptr, size); 1454 #ifdef AWI_DEBUG 1455 if (awi_debug) { 1456 int i; 1457 1458 printf("awi_mib: #%d:", mib); 1459 for (i = 0; i < size; i++) 1460 printf(" %02x", ptr[i]); 1461 printf("\n"); 1462 } 1463 #endif 1464 } 1465 return 0; 1466 } 1467 1468 static int 1469 awi_cmd(struct awi_softc *sc, u_int8_t cmd, int wflag) 1470 { 1471 u_int8_t status; 1472 int error = 0; 1473 #ifdef AWI_DEBUG 1474 static const char *cmdname[] = { 1475 "IDLE", "NOP", "SET_MIB", "INIT_TX", "FLUSH_TX", "INIT_RX", 1476 "KILL_RX", "SLEEP", "WAKE", "GET_MIB", "SCAN", "SYNC", "RESUME" 1477 }; 1478 #endif 1479 1480 #ifdef AWI_DEBUG 1481 if (awi_debug > 1) { 1482 if (cmd >= sizeof(cmdname)/sizeof(cmdname[0])) 1483 printf("awi_cmd: #%d", cmd); 1484 else 1485 printf("awi_cmd: %s", cmdname[cmd]); 1486 printf(" %s\n", wflag == AWI_NOWAIT ? "nowait" : "wait"); 1487 } 1488 #endif 1489 sc->sc_cmd_inprog = cmd; 1490 awi_write_1(sc, AWI_CMD_STATUS, AWI_STAT_IDLE); 1491 awi_write_1(sc, AWI_CMD, cmd); 1492 if (wflag == AWI_NOWAIT) 1493 return EINPROGRESS; 1494 if ((error = awi_cmd_wait(sc)) != 0) 1495 return error; 1496 status = awi_read_1(sc, AWI_CMD_STATUS); 1497 awi_write_1(sc, AWI_CMD, 0); 1498 switch (status) { 1499 case AWI_STAT_OK: 1500 break; 1501 case AWI_STAT_BADPARM: 1502 return EINVAL; 1503 default: 1504 printf("%s: command %d failed %x\n", 1505 sc->sc_dev.dv_xname, cmd, status); 1506 return ENXIO; 1507 } 1508 return 0; 1509 } 1510 1511 static int 1512 awi_cmd_wait(struct awi_softc *sc) 1513 { 1514 int i, error = 0; 1515 1516 i = 0; 1517 while (sc->sc_cmd_inprog) { 1518 if (sc->sc_invalid) 1519 return ENXIO; 1520 if (awi_read_1(sc, AWI_CMD) != sc->sc_cmd_inprog) { 1521 printf("%s: failed to access hardware\n", 1522 sc->sc_dev.dv_xname); 1523 sc->sc_invalid = 1; 1524 return ENXIO; 1525 } 1526 if (sc->sc_cansleep) { 1527 sc->sc_sleep_cnt++; 1528 error = tsleep(sc, PWAIT, "awicmd", 1529 AWI_CMD_TIMEOUT*hz/1000); 1530 sc->sc_sleep_cnt--; 1531 } else { 1532 if (awi_read_1(sc, AWI_CMD_STATUS) != AWI_STAT_IDLE) { 1533 awi_cmd_done(sc); 1534 break; 1535 } 1536 if (i++ >= AWI_CMD_TIMEOUT*1000/10) 1537 error = EWOULDBLOCK; 1538 else 1539 DELAY(10); 1540 } 1541 if (error) 1542 break; 1543 } 1544 if (error) { 1545 DPRINTF(("awi_cmd_wait: cmd 0x%x, error %d\n", 1546 sc->sc_cmd_inprog, error)); 1547 } 1548 return error; 1549 } 1550 1551 static void 1552 awi_cmd_done(struct awi_softc *sc) 1553 { 1554 u_int8_t cmd, status; 1555 1556 status = awi_read_1(sc, AWI_CMD_STATUS); 1557 if (status == AWI_STAT_IDLE) 1558 return; /* stray interrupt */ 1559 1560 cmd = sc->sc_cmd_inprog; 1561 sc->sc_cmd_inprog = 0; 1562 wakeup(sc); 1563 awi_write_1(sc, AWI_CMD, 0); 1564 1565 if (status != AWI_STAT_OK) { 1566 printf("%s: command %d failed %x\n", 1567 sc->sc_dev.dv_xname, cmd, status); 1568 sc->sc_substate = AWI_ST_NONE; 1569 return; 1570 } 1571 if (sc->sc_substate != AWI_ST_NONE) 1572 (void)ieee80211_new_state(&sc->sc_ic.ic_if, sc->sc_nstate, -1); 1573 } 1574 1575 static int 1576 awi_next_txd(struct awi_softc *sc, int len, u_int32_t *framep, u_int32_t *ntxdp) 1577 { 1578 u_int32_t txd, ntxd, frame; 1579 1580 txd = sc->sc_txnext; 1581 frame = txd + AWI_TXD_SIZE; 1582 if (frame + len > sc->sc_txend) 1583 frame = sc->sc_txbase; 1584 ntxd = frame + len; 1585 if (ntxd + AWI_TXD_SIZE > sc->sc_txend) 1586 ntxd = sc->sc_txbase; 1587 *framep = frame; 1588 *ntxdp = ntxd; 1589 /* 1590 * Determine if there are any room in ring buffer. 1591 * --- send wait, === new data, +++ conflict (ENOBUFS) 1592 * base........................end 1593 * done----txd=====ntxd OK 1594 * --txd=====done++++ntxd-- full 1595 * --txd=====ntxd done-- OK 1596 * ==ntxd done----txd=== OK 1597 * ==done++++ntxd----txd=== full 1598 * ++ntxd txd=====done++ full 1599 */ 1600 if (txd < ntxd) { 1601 if (txd < sc->sc_txdone && ntxd + AWI_TXD_SIZE > sc->sc_txdone) 1602 return ENOBUFS; 1603 } else { 1604 if (txd < sc->sc_txdone || ntxd + AWI_TXD_SIZE > sc->sc_txdone) 1605 return ENOBUFS; 1606 } 1607 return 0; 1608 } 1609 1610 static int 1611 awi_lock(struct awi_softc *sc) 1612 { 1613 int error = 0; 1614 1615 if (curproc == NULL) { 1616 /* 1617 * XXX 1618 * Though driver ioctl should be called with context, 1619 * KAME ipv6 stack calls ioctl in interrupt for now. 1620 * We simply abort the request if there are other 1621 * ioctl requests in progress. 1622 */ 1623 if (sc->sc_busy) { 1624 return EWOULDBLOCK; 1625 if (sc->sc_invalid) 1626 return ENXIO; 1627 } 1628 sc->sc_busy = 1; 1629 sc->sc_cansleep = 0; 1630 return 0; 1631 } 1632 while (sc->sc_busy) { 1633 if (sc->sc_invalid) 1634 return ENXIO; 1635 sc->sc_sleep_cnt++; 1636 error = tsleep(sc, PWAIT | PCATCH, "awilck", 0); 1637 sc->sc_sleep_cnt--; 1638 if (error) 1639 return error; 1640 } 1641 sc->sc_busy = 1; 1642 sc->sc_cansleep = 1; 1643 return 0; 1644 } 1645 1646 static void 1647 awi_unlock(struct awi_softc *sc) 1648 { 1649 sc->sc_busy = 0; 1650 sc->sc_cansleep = 0; 1651 if (sc->sc_sleep_cnt) 1652 wakeup(sc); 1653 } 1654 1655 static int 1656 awi_intr_lock(struct awi_softc *sc) 1657 { 1658 u_int8_t status; 1659 int i, retry; 1660 1661 status = 1; 1662 for (retry = 0; retry < 10; retry++) { 1663 for (i = 0; i < AWI_LOCKOUT_TIMEOUT*1000/5; i++) { 1664 if ((status = awi_read_1(sc, AWI_LOCKOUT_HOST)) == 0) 1665 break; 1666 DELAY(5); 1667 } 1668 if (status != 0) 1669 break; 1670 awi_write_1(sc, AWI_LOCKOUT_MAC, 1); 1671 if ((status = awi_read_1(sc, AWI_LOCKOUT_HOST)) == 0) 1672 break; 1673 awi_write_1(sc, AWI_LOCKOUT_MAC, 0); 1674 } 1675 if (status != 0) { 1676 printf("%s: failed to lock interrupt\n", 1677 sc->sc_dev.dv_xname); 1678 return ENXIO; 1679 } 1680 return 0; 1681 } 1682 1683 static void 1684 awi_intr_unlock(struct awi_softc *sc) 1685 { 1686 1687 awi_write_1(sc, AWI_LOCKOUT_MAC, 0); 1688 } 1689 1690 static int 1691 awi_newstate(void *arg, enum ieee80211_state nstate) 1692 { 1693 struct awi_softc *sc = arg; 1694 struct ieee80211com *ic = &sc->sc_ic; 1695 struct ieee80211_bss *bs = &ic->ic_bss; 1696 struct ifnet *ifp = &ic->ic_if; 1697 int error; 1698 u_int8_t newmode; 1699 enum ieee80211_state ostate; 1700 #ifdef AWI_DEBUG 1701 static const char *stname[] = 1702 { "INIT", "SCAN", "AUTH", "ASSOC", "RUN" }; 1703 static const char *substname[] = 1704 { "NONE", "SCAN_INIT", "SCAN_SETMIB", "SCAN_SCCMD", 1705 "SUB_INIT", "SUB_SETSS", "SUB_SYNC" }; 1706 #endif /* AWI_DEBUG */ 1707 1708 ostate = ic->ic_state; 1709 DPRINTF(("awi_newstate: %s (%s/%s) -> %s\n", stname[ostate], 1710 stname[sc->sc_nstate], substname[sc->sc_substate], stname[nstate])); 1711 1712 /* set LED */ 1713 switch (nstate) { 1714 case IEEE80211_S_INIT: 1715 awi_drvstate(sc, AWI_DRV_RESET); 1716 break; 1717 case IEEE80211_S_SCAN: 1718 if (ic->ic_flags & IEEE80211_F_ADHOC) 1719 awi_drvstate(sc, AWI_DRV_ADHSC); 1720 else 1721 awi_drvstate(sc, AWI_DRV_INFSY); 1722 break; 1723 case IEEE80211_S_AUTH: 1724 awi_drvstate(sc, AWI_DRV_INFSY); 1725 break; 1726 case IEEE80211_S_ASSOC: 1727 awi_drvstate(sc, AWI_DRV_INFAUTH); 1728 break; 1729 case IEEE80211_S_RUN: 1730 if (ic->ic_flags & IEEE80211_F_ADHOC) 1731 awi_drvstate(sc, AWI_DRV_ADHSY); 1732 else 1733 awi_drvstate(sc, AWI_DRV_INFASSOC); 1734 break; 1735 } 1736 1737 if (nstate == IEEE80211_S_INIT) { 1738 sc->sc_substate = AWI_ST_NONE; 1739 ic->ic_flags &= ~IEEE80211_F_SIBSS; 1740 return 0; 1741 } 1742 1743 /* state transition */ 1744 if (nstate == IEEE80211_S_SCAN) { 1745 /* SCAN substate */ 1746 if (sc->sc_substate == AWI_ST_NONE) { 1747 sc->sc_nstate = nstate; /* next state in transition */ 1748 sc->sc_substate = AWI_ST_SCAN_INIT; 1749 } 1750 switch (sc->sc_substate) { 1751 case AWI_ST_SCAN_INIT: 1752 sc->sc_substate = AWI_ST_SCAN_SETMIB; 1753 switch (ostate) { 1754 case IEEE80211_S_RUN: 1755 /* beacon miss */ 1756 if (ifp->if_flags & IFF_DEBUG) 1757 printf("%s: no recent beacons from %s;" 1758 " rescanning\n", 1759 ifp->if_xname, 1760 ether_sprintf(ic->ic_bss.bs_bssid)); 1761 /* FALLTHRU */ 1762 case IEEE80211_S_AUTH: 1763 case IEEE80211_S_ASSOC: 1764 /* timeout restart scan */ 1765 ieee80211_free_scan(ifp); 1766 /* FALLTHRU */ 1767 case IEEE80211_S_INIT: 1768 ic->ic_flags |= IEEE80211_F_ASCAN; 1769 ic->ic_scan_timer = 0; 1770 /* FALLTHRU */ 1771 case IEEE80211_S_SCAN: 1772 /* scan next */ 1773 break; 1774 } 1775 if (ic->ic_flags & IEEE80211_F_ASCAN) 1776 newmode = AWI_SCAN_ACTIVE; 1777 else 1778 newmode = AWI_SCAN_PASSIVE; 1779 if (sc->sc_mib_mgt.aScan_Mode != newmode) { 1780 sc->sc_mib_mgt.aScan_Mode = newmode; 1781 if ((error = awi_mib(sc, AWI_CMD_SET_MIB, 1782 AWI_MIB_MGT, AWI_NOWAIT)) != 0) 1783 break; 1784 } 1785 /* FALLTHRU */ 1786 case AWI_ST_SCAN_SETMIB: 1787 sc->sc_substate = AWI_ST_SCAN_SCCMD; 1788 if (sc->sc_cmd_inprog) { 1789 if ((error = awi_cmd_wait(sc)) != 0) 1790 break; 1791 } 1792 sc->sc_cmd_inprog = AWI_CMD_SCAN; 1793 awi_write_2(sc, AWI_CA_SCAN_DURATION, 1794 (ic->ic_flags & IEEE80211_F_ASCAN) ? 1795 AWI_ASCAN_DURATION : AWI_PSCAN_DURATION); 1796 if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH) { 1797 awi_write_1(sc, AWI_CA_SCAN_SET, 1798 IEEE80211_FH_CHANSET(bs->bs_chan)); 1799 awi_write_1(sc, AWI_CA_SCAN_PATTERN, 1800 IEEE80211_FH_CHANPAT(bs->bs_chan)); 1801 awi_write_1(sc, AWI_CA_SCAN_IDX, 1); 1802 } else { 1803 awi_write_1(sc, AWI_CA_SCAN_SET, bs->bs_chan); 1804 awi_write_1(sc, AWI_CA_SCAN_PATTERN, 0); 1805 awi_write_1(sc, AWI_CA_SCAN_IDX, 0); 1806 } 1807 awi_write_1(sc, AWI_CA_SCAN_SUSP, 0); 1808 sc->sc_cur_chan = bs->bs_chan; 1809 if ((error = awi_cmd(sc, AWI_CMD_SCAN, AWI_NOWAIT)) 1810 != 0) 1811 break; 1812 /* FALLTHRU */ 1813 case AWI_ST_SCAN_SCCMD: 1814 if (ic->ic_scan_timer == 0) 1815 ic->ic_scan_timer = 1816 (ic->ic_flags & IEEE80211_F_ASCAN) ? 1817 IEEE80211_ASCAN_WAIT : IEEE80211_PSCAN_WAIT; 1818 ifp->if_timer = 1; 1819 ic->ic_state = nstate; 1820 sc->sc_substate = AWI_ST_NONE; 1821 error = EINPROGRESS; 1822 break; 1823 default: 1824 DPRINTF(("awi_newstate: unexpected state %s/%s\n", 1825 stname[nstate], substname[sc->sc_substate])); 1826 sc->sc_substate = AWI_ST_NONE; 1827 error = EIO; 1828 break; 1829 } 1830 return error; 1831 } 1832 1833 if (ostate == IEEE80211_S_SCAN) { 1834 /* set SSID and channel */ 1835 /* substate */ 1836 if (sc->sc_substate == AWI_ST_NONE) { 1837 sc->sc_nstate = nstate; /* next state in transition */ 1838 sc->sc_substate = AWI_ST_SUB_INIT; 1839 } 1840 switch (sc->sc_substate) { 1841 case AWI_ST_SUB_INIT: 1842 sc->sc_substate = AWI_ST_SUB_SETSS; 1843 memcpy(&sc->sc_mib_mgt.aCurrent_BSS_ID, bs->bs_bssid, 1844 IEEE80211_ADDR_LEN); 1845 memset(&sc->sc_mib_mgt.aCurrent_ESS_ID, 0, 1846 AWI_ESS_ID_SIZE); 1847 sc->sc_mib_mgt.aCurrent_ESS_ID[0] = 1848 IEEE80211_ELEMID_SSID; 1849 sc->sc_mib_mgt.aCurrent_ESS_ID[1] = bs->bs_esslen; 1850 memcpy(&sc->sc_mib_mgt.aCurrent_ESS_ID[2], 1851 bs->bs_essid, bs->bs_esslen); 1852 LE_WRITE_2(&sc->sc_mib_mgt.aBeacon_Period, 1853 bs->bs_intval); 1854 if ((error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_MGT, 1855 AWI_NOWAIT)) != 0) 1856 break; 1857 /* FALLTHRU */ 1858 case AWI_ST_SUB_SETSS: 1859 sc->sc_substate = AWI_ST_SUB_SYNC; 1860 if (sc->sc_cmd_inprog) { 1861 if ((error = awi_cmd_wait(sc)) != 0) 1862 break; 1863 } 1864 sc->sc_cmd_inprog = AWI_CMD_SYNC; 1865 if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH) { 1866 awi_write_1(sc, AWI_CA_SYNC_SET, 1867 IEEE80211_FH_CHANSET(bs->bs_chan)); 1868 awi_write_1(sc, AWI_CA_SYNC_PATTERN, 1869 IEEE80211_FH_CHANPAT(bs->bs_chan)); 1870 awi_write_1(sc, AWI_CA_SYNC_IDX, 1871 bs->bs_fhindex); 1872 awi_write_2(sc, AWI_CA_SYNC_DWELL, 1873 bs->bs_fhdwell); 1874 } else { 1875 awi_write_1(sc, AWI_CA_SYNC_SET, bs->bs_chan); 1876 awi_write_1(sc, AWI_CA_SYNC_PATTERN, 0); 1877 awi_write_1(sc, AWI_CA_SYNC_IDX, 0); 1878 awi_write_2(sc, AWI_CA_SYNC_DWELL, 0); 1879 } 1880 if ((ic->ic_flags & IEEE80211_F_SIBSS) && 1881 !sc->sc_no_bssid) 1882 awi_write_1(sc, AWI_CA_SYNC_STARTBSS, 1); 1883 else 1884 awi_write_1(sc, AWI_CA_SYNC_STARTBSS, 0); 1885 awi_write_2(sc, AWI_CA_SYNC_MBZ, 0); 1886 awi_write_bytes(sc, AWI_CA_SYNC_TIMESTAMP, 1887 bs->bs_tstamp, 8); 1888 awi_write_4(sc, AWI_CA_SYNC_REFTIME, bs->bs_rstamp); 1889 sc->sc_cur_chan = bs->bs_chan; 1890 if ((error = awi_cmd(sc, AWI_CMD_SYNC, AWI_NOWAIT)) 1891 != 0) 1892 break; 1893 /* FALLTHRU */ 1894 case AWI_ST_SUB_SYNC: 1895 sc->sc_substate = AWI_ST_NONE; 1896 if (ic->ic_flags & IEEE80211_F_SIBSS) { 1897 if ((error = awi_mib(sc, AWI_CMD_GET_MIB, 1898 AWI_MIB_MGT, AWI_WAIT)) != 0) 1899 break; 1900 memcpy(bs->bs_bssid, 1901 &sc->sc_mib_mgt.aCurrent_BSS_ID, 1902 IEEE80211_ADDR_LEN); 1903 } else { 1904 if (nstate == IEEE80211_S_RUN) { 1905 sc->sc_rx_timer = 10; 1906 ifp->if_timer = 1; 1907 } 1908 } 1909 error = 0; 1910 break; 1911 default: 1912 DPRINTF(("awi_newstate: unexpected state %s/%s\n", 1913 stname[nstate], substname[sc->sc_substate])); 1914 sc->sc_substate = AWI_ST_NONE; 1915 error = EIO; 1916 break; 1917 } 1918 return error; 1919 } 1920 1921 sc->sc_substate = AWI_ST_NONE; 1922 1923 return 0; 1924 } 1925 1926 static struct mbuf * 1927 awi_ether_encap(struct awi_softc *sc, struct mbuf *m) 1928 { 1929 struct ieee80211com *ic = &sc->sc_ic; 1930 struct ieee80211_bss *bs = &ic->ic_bss; 1931 struct ether_header *eh; 1932 struct ieee80211_frame *wh; 1933 1934 if (m->m_len < sizeof(struct ether_header)) { 1935 m = m_pullup(m, sizeof(struct ether_header)); 1936 if (m == NULL) 1937 return NULL; 1938 } 1939 eh = mtod(m, struct ether_header *); 1940 M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT); 1941 if (m == NULL) 1942 return NULL; 1943 wh = mtod(m, struct ieee80211_frame *); 1944 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_DATA; 1945 *(u_int16_t *)wh->i_dur = 0; 1946 *(u_int16_t *)wh->i_seq = 1947 htole16(bs->bs_txseq << IEEE80211_SEQ_SEQ_SHIFT); 1948 bs->bs_txseq++; 1949 if (ic->ic_flags & IEEE80211_F_ADHOC) { 1950 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS; 1951 if (sc->sc_adhoc_ap) 1952 memcpy(wh->i_addr1, bs->bs_macaddr, IEEE80211_ADDR_LEN); 1953 else 1954 memcpy(wh->i_addr1, eh->ether_dhost, 1955 IEEE80211_ADDR_LEN); 1956 memcpy(wh->i_addr2, eh->ether_shost, IEEE80211_ADDR_LEN); 1957 memcpy(wh->i_addr3, bs->bs_bssid, IEEE80211_ADDR_LEN); 1958 } else { 1959 wh->i_fc[1] = IEEE80211_FC1_DIR_TODS; 1960 memcpy(wh->i_addr1, bs->bs_bssid, IEEE80211_ADDR_LEN); 1961 memcpy(wh->i_addr2, eh->ether_shost, IEEE80211_ADDR_LEN); 1962 memcpy(wh->i_addr3, eh->ether_dhost, IEEE80211_ADDR_LEN); 1963 } 1964 return m; 1965 } 1966 1967 static struct mbuf * 1968 awi_ether_modcap(struct awi_softc *sc, struct mbuf *m) 1969 { 1970 struct ieee80211com *ic = &sc->sc_ic; 1971 struct ether_header eh; 1972 struct ieee80211_frame wh; 1973 struct llc *llc; 1974 1975 if (m->m_len < sizeof(wh) + sizeof(eh)) { 1976 m = m_pullup(m, sizeof(wh) + sizeof(eh)); 1977 if (m == NULL) 1978 return NULL; 1979 } 1980 memcpy(&wh, mtod(m, caddr_t), sizeof(wh)); 1981 if (wh.i_fc[0] != (IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_DATA)) 1982 return m; 1983 memcpy(&eh, mtod(m, caddr_t) + sizeof(wh), sizeof(eh)); 1984 m_adj(m, sizeof(eh) - sizeof(*llc)); 1985 if (ic->ic_flags & IEEE80211_F_ADHOC) 1986 memcpy(wh.i_addr2, eh.ether_shost, IEEE80211_ADDR_LEN); 1987 memcpy(mtod(m, caddr_t), &wh, sizeof(wh)); 1988 llc = (struct llc *)(mtod(m, caddr_t) + sizeof(wh)); 1989 llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP; 1990 llc->llc_control = LLC_UI; 1991 llc->llc_snap.org_code[0] = 0; 1992 llc->llc_snap.org_code[1] = 0; 1993 llc->llc_snap.org_code[2] = 0; 1994 llc->llc_snap.ether_type = eh.ether_type; 1995 return m; 1996 } 1997