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