1 /* $OpenBSD: if_urtwn.c,v 1.16 2011/02/10 17:26:40 jakemsr Exp $ */ 2 3 /*- 4 * Copyright (c) 2010 Damien Bergamini <damien.bergamini@free.fr> 5 * Copyright (c) 2014 Kevin Lo <kevlo@FreeBSD.org> 6 * Copyright (c) 2015-2016 Andriy Voskoboinyk <avos@FreeBSD.org> 7 * 8 * Permission to use, copy, modify, and distribute this software for any 9 * purpose with or without fee is hereby granted, provided that the above 10 * copyright notice and this permission notice appear in all copies. 11 * 12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 19 */ 20 21 #include <sys/cdefs.h> 22 __FBSDID("$FreeBSD$"); 23 24 /* 25 * Driver for Realtek RTL8188CE-VAU/RTL8188CUS/RTL8188EU/RTL8188RU/RTL8192CU/RTL8812AU/RTL8821AU. 26 */ 27 #include "opt_wlan.h" 28 29 #include <sys/param.h> 30 #include <sys/sockio.h> 31 #include <sys/sysctl.h> 32 #include <sys/lock.h> 33 #include <sys/mutex.h> 34 #include <sys/mbuf.h> 35 #include <sys/kernel.h> 36 #include <sys/socket.h> 37 #include <sys/systm.h> 38 #include <sys/malloc.h> 39 #include <sys/module.h> 40 #include <sys/bus.h> 41 #include <sys/endian.h> 42 #include <sys/linker.h> 43 #include <sys/firmware.h> 44 #include <sys/kdb.h> 45 46 #include <net/bpf.h> 47 #include <net/if.h> 48 #include <net/if_var.h> 49 #include <net/if_arp.h> 50 #include <net/ethernet.h> 51 #include <net/if_dl.h> 52 #include <net/if_media.h> 53 #include <net/if_types.h> 54 55 #include <netinet/in.h> 56 #include <netinet/in_systm.h> 57 #include <netinet/in_var.h> 58 #include <netinet/if_ether.h> 59 #include <netinet/ip.h> 60 61 #include <net80211/ieee80211_var.h> 62 #include <net80211/ieee80211_regdomain.h> 63 #include <net80211/ieee80211_radiotap.h> 64 #include <net80211/ieee80211_ratectl.h> 65 66 #include <dev/rtwn/if_rtwnreg.h> 67 #include <dev/rtwn/if_rtwnvar.h> 68 69 #include <dev/rtwn/if_rtwn_beacon.h> 70 #include <dev/rtwn/if_rtwn_calib.h> 71 #include <dev/rtwn/if_rtwn_cam.h> 72 #include <dev/rtwn/if_rtwn_debug.h> 73 #include <dev/rtwn/if_rtwn_efuse.h> 74 #include <dev/rtwn/if_rtwn_fw.h> 75 #include <dev/rtwn/if_rtwn_ridx.h> 76 #include <dev/rtwn/if_rtwn_rx.h> 77 #include <dev/rtwn/if_rtwn_task.h> 78 #include <dev/rtwn/if_rtwn_tx.h> 79 80 #include <dev/rtwn/rtl8192c/r92c_reg.h> 81 82 83 static void rtwn_radiotap_attach(struct rtwn_softc *); 84 static void rtwn_vap_decrement_counters(struct rtwn_softc *, 85 enum ieee80211_opmode, int); 86 static void rtwn_set_ic_opmode(struct rtwn_softc *); 87 static struct ieee80211vap *rtwn_vap_create(struct ieee80211com *, 88 const char [IFNAMSIZ], int, enum ieee80211_opmode, 89 int, const uint8_t [IEEE80211_ADDR_LEN], 90 const uint8_t [IEEE80211_ADDR_LEN]); 91 static void rtwn_vap_delete(struct ieee80211vap *); 92 static int rtwn_read_chipid(struct rtwn_softc *); 93 static int rtwn_ioctl_reset(struct ieee80211vap *, u_long); 94 #ifndef RTWN_WITHOUT_UCODE 95 static void rtwn_set_media_status(struct rtwn_softc *, 96 union sec_param *); 97 static int rtwn_tx_fwpkt_check(struct rtwn_softc *, 98 struct ieee80211vap *); 99 static int rtwn_construct_nulldata(struct rtwn_softc *, 100 struct ieee80211vap *, uint8_t *, int); 101 static int rtwn_push_nulldata(struct rtwn_softc *, 102 struct ieee80211vap *); 103 static void rtwn_pwrmode_init(void *); 104 static void rtwn_set_pwrmode_cb(struct rtwn_softc *, 105 union sec_param *); 106 #endif 107 static void rtwn_tsf_sync_adhoc(void *); 108 static void rtwn_tsf_sync_adhoc_task(void *, int); 109 static void rtwn_tsf_sync_enable(struct rtwn_softc *, 110 struct ieee80211vap *); 111 static void rtwn_set_ack_preamble(struct rtwn_softc *); 112 static void rtwn_set_mode(struct rtwn_softc *, uint8_t, int); 113 static int rtwn_monitor_newstate(struct ieee80211vap *, 114 enum ieee80211_state, int); 115 static int rtwn_newstate(struct ieee80211vap *, 116 enum ieee80211_state, int); 117 static void rtwn_calc_basicrates(struct rtwn_softc *); 118 static int rtwn_run(struct rtwn_softc *, 119 struct ieee80211vap *); 120 #ifndef D4054 121 static void rtwn_watchdog(void *); 122 #endif 123 static void rtwn_parent(struct ieee80211com *); 124 static int rtwn_llt_write(struct rtwn_softc *, uint32_t, 125 uint32_t); 126 static int rtwn_llt_init(struct rtwn_softc *); 127 static int rtwn_dma_init(struct rtwn_softc *); 128 static int rtwn_mac_init(struct rtwn_softc *); 129 static void rtwn_mrr_init(struct rtwn_softc *); 130 static void rtwn_scan_start(struct ieee80211com *); 131 static void rtwn_scan_curchan(struct ieee80211_scan_state *, 132 unsigned long); 133 static void rtwn_scan_end(struct ieee80211com *); 134 static void rtwn_getradiocaps(struct ieee80211com *, int, int *, 135 struct ieee80211_channel[]); 136 static void rtwn_update_chw(struct ieee80211com *); 137 static void rtwn_set_channel(struct ieee80211com *); 138 static int rtwn_wme_update(struct ieee80211com *); 139 static void rtwn_update_slot(struct ieee80211com *); 140 static void rtwn_update_slot_cb(struct rtwn_softc *, 141 union sec_param *); 142 static void rtwn_update_aifs(struct rtwn_softc *, uint8_t); 143 static void rtwn_update_promisc(struct ieee80211com *); 144 static void rtwn_update_mcast(struct ieee80211com *); 145 static int rtwn_set_bssid(struct rtwn_softc *, 146 const uint8_t *, int); 147 static int rtwn_set_macaddr(struct rtwn_softc *, 148 const uint8_t *, int); 149 static struct ieee80211_node *rtwn_node_alloc(struct ieee80211vap *, 150 const uint8_t mac[IEEE80211_ADDR_LEN]); 151 static void rtwn_newassoc(struct ieee80211_node *, int); 152 static void rtwn_node_free(struct ieee80211_node *); 153 static void rtwn_init_beacon_reg(struct rtwn_softc *); 154 static int rtwn_init(struct rtwn_softc *); 155 static void rtwn_stop(struct rtwn_softc *); 156 157 MALLOC_DEFINE(M_RTWN_PRIV, "rtwn_priv", "rtwn driver private state"); 158 159 static const uint8_t rtwn_chan_2ghz[] = 160 { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 }; 161 162 static const uint16_t wme2reg[] = 163 { R92C_EDCA_BE_PARAM, R92C_EDCA_BK_PARAM, 164 R92C_EDCA_VI_PARAM, R92C_EDCA_VO_PARAM }; 165 166 int 167 rtwn_attach(struct rtwn_softc *sc) 168 { 169 struct ieee80211com *ic = &sc->sc_ic; 170 int error; 171 172 sc->cur_bcnq_id = RTWN_VAP_ID_INVALID; 173 174 RTWN_NT_LOCK_INIT(sc); 175 rtwn_cmdq_init(sc); 176 #ifndef D4054 177 callout_init_mtx(&sc->sc_watchdog_to, &sc->sc_mtx, 0); 178 #endif 179 callout_init(&sc->sc_calib_to, 0); 180 callout_init(&sc->sc_pwrmode_init, 0); 181 mbufq_init(&sc->sc_snd, ifqmaxlen); 182 183 RTWN_LOCK(sc); 184 error = rtwn_read_chipid(sc); 185 RTWN_UNLOCK(sc); 186 if (error != 0) { 187 device_printf(sc->sc_dev, "unsupported test chip\n"); 188 goto detach; 189 } 190 191 error = rtwn_read_rom(sc); 192 if (error != 0) { 193 device_printf(sc->sc_dev, "%s: cannot read rom, error %d\n", 194 __func__, error); 195 goto detach; 196 } 197 198 if (sc->macid_limit > RTWN_MACID_LIMIT) { 199 device_printf(sc->sc_dev, 200 "macid limit will be reduced from %d to %d\n", 201 sc->macid_limit, RTWN_MACID_LIMIT); 202 sc->macid_limit = RTWN_MACID_LIMIT; 203 } 204 if (sc->cam_entry_limit > RTWN_CAM_ENTRY_LIMIT) { 205 device_printf(sc->sc_dev, 206 "cam entry limit will be reduced from %d to %d\n", 207 sc->cam_entry_limit, RTWN_CAM_ENTRY_LIMIT); 208 sc->cam_entry_limit = RTWN_CAM_ENTRY_LIMIT; 209 } 210 if (sc->txdesc_len > RTWN_TX_DESC_SIZE) { 211 device_printf(sc->sc_dev, 212 "adjust size for Tx descriptor (current %d, needed %d)\n", 213 RTWN_TX_DESC_SIZE, sc->txdesc_len); 214 goto detach; 215 } 216 217 device_printf(sc->sc_dev, "MAC/BB %s, RF 6052 %dT%dR\n", 218 sc->name, sc->ntxchains, sc->nrxchains); 219 220 ic->ic_softc = sc; 221 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */ 222 ic->ic_opmode = IEEE80211_M_STA; /* default to BSS mode */ 223 224 /* set device capabilities */ 225 ic->ic_caps = 226 IEEE80211_C_STA /* station mode */ 227 | IEEE80211_C_MONITOR /* monitor mode */ 228 | IEEE80211_C_IBSS /* adhoc mode */ 229 | IEEE80211_C_HOSTAP /* hostap mode */ 230 #if 0 /* TODO: HRPWM register setup */ 231 #ifndef RTWN_WITHOUT_UCODE 232 | IEEE80211_C_PMGT /* Station-side power mgmt */ 233 #endif 234 #endif 235 | IEEE80211_C_SHPREAMBLE /* short preamble supported */ 236 | IEEE80211_C_SHSLOT /* short slot time supported */ 237 #if 0 238 | IEEE80211_C_BGSCAN /* capable of bg scanning */ 239 #endif 240 | IEEE80211_C_WPA /* 802.11i */ 241 | IEEE80211_C_WME /* 802.11e */ 242 | IEEE80211_C_SWAMSDUTX /* Do software A-MSDU TX */ 243 | IEEE80211_C_FF /* Atheros fast-frames */ 244 ; 245 246 if (sc->sc_hwcrypto != RTWN_CRYPTO_SW) { 247 ic->ic_cryptocaps = 248 IEEE80211_CRYPTO_WEP | 249 IEEE80211_CRYPTO_TKIP | 250 IEEE80211_CRYPTO_AES_CCM; 251 } 252 253 ic->ic_htcaps = 254 IEEE80211_HTCAP_SHORTGI20 /* short GI in 20MHz */ 255 | IEEE80211_HTCAP_MAXAMSDU_3839 /* max A-MSDU length */ 256 | IEEE80211_HTCAP_SMPS_OFF /* SM PS mode disabled */ 257 /* s/w capabilities */ 258 | IEEE80211_HTC_HT /* HT operation */ 259 | IEEE80211_HTC_AMPDU /* A-MPDU tx */ 260 | IEEE80211_HTC_AMSDU /* A-MSDU tx */ 261 ; 262 263 if (sc->sc_ht40) { 264 ic->ic_htcaps |= 265 IEEE80211_HTCAP_CHWIDTH40 /* 40 MHz channel width */ 266 | IEEE80211_HTCAP_SHORTGI40 /* short GI in 40MHz */ 267 ; 268 } 269 270 ic->ic_txstream = sc->ntxchains; 271 ic->ic_rxstream = sc->nrxchains; 272 273 /* Enable TX watchdog */ 274 #ifdef D4054 275 ic->ic_flags_ext |= IEEE80211_FEXT_WATCHDOG; 276 #endif 277 278 /* Adjust capabilities. */ 279 rtwn_adj_devcaps(sc); 280 281 rtwn_getradiocaps(ic, IEEE80211_CHAN_MAX, &ic->ic_nchans, 282 ic->ic_channels); 283 284 /* XXX TODO: setup regdomain if R92C_CHANNEL_PLAN_BY_HW bit is set. */ 285 286 ieee80211_ifattach(ic); 287 ic->ic_raw_xmit = rtwn_raw_xmit; 288 ic->ic_scan_start = rtwn_scan_start; 289 sc->sc_scan_curchan = ic->ic_scan_curchan; 290 ic->ic_scan_curchan = rtwn_scan_curchan; 291 ic->ic_scan_end = rtwn_scan_end; 292 ic->ic_getradiocaps = rtwn_getradiocaps; 293 ic->ic_update_chw = rtwn_update_chw; 294 ic->ic_set_channel = rtwn_set_channel; 295 ic->ic_transmit = rtwn_transmit; 296 ic->ic_parent = rtwn_parent; 297 ic->ic_vap_create = rtwn_vap_create; 298 ic->ic_vap_delete = rtwn_vap_delete; 299 ic->ic_wme.wme_update = rtwn_wme_update; 300 ic->ic_updateslot = rtwn_update_slot; 301 ic->ic_update_promisc = rtwn_update_promisc; 302 ic->ic_update_mcast = rtwn_update_mcast; 303 ic->ic_node_alloc = rtwn_node_alloc; 304 ic->ic_newassoc = rtwn_newassoc; 305 sc->sc_node_free = ic->ic_node_free; 306 ic->ic_node_free = rtwn_node_free; 307 308 rtwn_postattach(sc); 309 rtwn_radiotap_attach(sc); 310 311 if (bootverbose) 312 ieee80211_announce(ic); 313 314 return (0); 315 316 detach: 317 return (ENXIO); /* failure */ 318 } 319 320 static void 321 rtwn_radiotap_attach(struct rtwn_softc *sc) 322 { 323 struct rtwn_rx_radiotap_header *rxtap = &sc->sc_rxtap; 324 struct rtwn_tx_radiotap_header *txtap = &sc->sc_txtap; 325 326 ieee80211_radiotap_attach(&sc->sc_ic, 327 &txtap->wt_ihdr, sizeof(*txtap), RTWN_TX_RADIOTAP_PRESENT, 328 &rxtap->wr_ihdr, sizeof(*rxtap), RTWN_RX_RADIOTAP_PRESENT); 329 } 330 331 void 332 rtwn_sysctlattach(struct rtwn_softc *sc) 333 { 334 struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->sc_dev); 335 struct sysctl_oid *tree = device_get_sysctl_tree(sc->sc_dev); 336 337 #if 1 338 sc->sc_ht40 = 0; 339 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 340 "ht40", CTLFLAG_RDTUN, &sc->sc_ht40, 341 sc->sc_ht40, "Enable 40 MHz mode support"); 342 #endif 343 344 #ifdef RTWN_DEBUG 345 SYSCTL_ADD_U32(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 346 "debug", CTLFLAG_RWTUN, &sc->sc_debug, sc->sc_debug, 347 "Control debugging printfs"); 348 #endif 349 350 sc->sc_hwcrypto = RTWN_CRYPTO_PAIR; 351 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 352 "hwcrypto", CTLFLAG_RDTUN, &sc->sc_hwcrypto, 353 sc->sc_hwcrypto, "Enable h/w crypto: " 354 "0 - disable, 1 - pairwise keys, 2 - all keys"); 355 if (sc->sc_hwcrypto >= RTWN_CRYPTO_MAX) 356 sc->sc_hwcrypto = RTWN_CRYPTO_FULL; 357 358 sc->sc_ratectl_sysctl = RTWN_RATECTL_NET80211; 359 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 360 "ratectl", CTLFLAG_RDTUN, &sc->sc_ratectl_sysctl, 361 sc->sc_ratectl_sysctl, "Select rate control mechanism: " 362 "0 - disabled, 1 - via net80211, 2 - via firmware"); 363 if (sc->sc_ratectl_sysctl >= RTWN_RATECTL_MAX) 364 sc->sc_ratectl_sysctl = RTWN_RATECTL_FW; 365 366 sc->sc_ratectl = sc->sc_ratectl_sysctl; 367 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 368 "ratectl_selected", CTLFLAG_RD, &sc->sc_ratectl, 369 sc->sc_ratectl, 370 "Currently selected rate control mechanism (by the driver)"); 371 } 372 373 void 374 rtwn_detach(struct rtwn_softc *sc) 375 { 376 struct ieee80211com *ic = &sc->sc_ic; 377 378 if (ic->ic_softc == sc) { 379 /* Stop command queue. */ 380 RTWN_CMDQ_LOCK(sc); 381 sc->sc_detached = 1; 382 RTWN_CMDQ_UNLOCK(sc); 383 384 ieee80211_draintask(ic, &sc->cmdq_task); 385 ieee80211_ifdetach(ic); 386 } 387 388 rtwn_cmdq_destroy(sc); 389 if (RTWN_NT_LOCK_INITIALIZED(sc)) 390 RTWN_NT_LOCK_DESTROY(sc); 391 } 392 393 void 394 rtwn_suspend(struct rtwn_softc *sc) 395 { 396 struct ieee80211com *ic = &sc->sc_ic; 397 398 ieee80211_suspend_all(ic); 399 } 400 401 void 402 rtwn_resume(struct rtwn_softc *sc) 403 { 404 struct ieee80211com *ic = &sc->sc_ic; 405 406 ieee80211_resume_all(ic); 407 } 408 409 static void 410 rtwn_vap_decrement_counters(struct rtwn_softc *sc, 411 enum ieee80211_opmode opmode, int id) 412 { 413 414 RTWN_ASSERT_LOCKED(sc); 415 416 if (id != RTWN_VAP_ID_INVALID) { 417 KASSERT(id == 0 || id == 1, ("wrong vap id %d!\n", id)); 418 KASSERT(sc->vaps[id] != NULL, ("vap pointer is NULL\n")); 419 sc->vaps[id] = NULL; 420 } 421 422 switch (opmode) { 423 case IEEE80211_M_HOSTAP: 424 sc->ap_vaps--; 425 /* FALLTHROUGH */ 426 case IEEE80211_M_IBSS: 427 sc->bcn_vaps--; 428 /* FALLTHROUGH */ 429 case IEEE80211_M_STA: 430 sc->nvaps--; 431 break; 432 case IEEE80211_M_MONITOR: 433 sc->mon_vaps--; 434 break; 435 default: 436 KASSERT(0, ("wrong opmode %d\n", opmode)); 437 break; 438 } 439 440 KASSERT(sc->vaps_running >= 0 && sc->monvaps_running >= 0, 441 ("number of running vaps is negative (vaps %d, monvaps %d)\n", 442 sc->vaps_running, sc->monvaps_running)); 443 KASSERT(sc->vaps_running - sc->monvaps_running <= RTWN_PORT_COUNT, 444 ("number of running vaps is too big (vaps %d, monvaps %d)\n", 445 sc->vaps_running, sc->monvaps_running)); 446 447 KASSERT(sc->nvaps >= 0 && sc->nvaps <= RTWN_PORT_COUNT, 448 ("wrong value %d for nvaps\n", sc->nvaps)); 449 KASSERT(sc->mon_vaps >= 0, ("mon_vaps is negative (%d)\n", 450 sc->mon_vaps)); 451 KASSERT(sc->bcn_vaps >= 0 && ((RTWN_CHIP_HAS_BCNQ1(sc) && 452 sc->bcn_vaps <= RTWN_PORT_COUNT) || sc->bcn_vaps <= 1), 453 ("bcn_vaps value %d is wrong\n", sc->bcn_vaps)); 454 KASSERT(sc->ap_vaps >= 0 && ((RTWN_CHIP_HAS_BCNQ1(sc) && 455 sc->ap_vaps <= RTWN_PORT_COUNT) || sc->ap_vaps <= 1), 456 ("ap_vaps value %d is wrong\n", sc->ap_vaps)); 457 } 458 459 static void 460 rtwn_set_ic_opmode(struct rtwn_softc *sc) 461 { 462 struct ieee80211com *ic = &sc->sc_ic; 463 464 RTWN_ASSERT_LOCKED(sc); 465 466 /* for ieee80211_reset_erp() */ 467 if (sc->bcn_vaps - sc->ap_vaps > 0) 468 ic->ic_opmode = IEEE80211_M_IBSS; 469 else if (sc->ap_vaps > 0) 470 ic->ic_opmode = IEEE80211_M_HOSTAP; 471 else if (sc->nvaps > 0) 472 ic->ic_opmode = IEEE80211_M_STA; 473 else 474 ic->ic_opmode = IEEE80211_M_MONITOR; 475 } 476 477 static struct ieee80211vap * 478 rtwn_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit, 479 enum ieee80211_opmode opmode, int flags, 480 const uint8_t bssid[IEEE80211_ADDR_LEN], 481 const uint8_t mac[IEEE80211_ADDR_LEN]) 482 { 483 struct rtwn_softc *sc = ic->ic_softc; 484 struct rtwn_vap *uvp; 485 struct ieee80211vap *vap; 486 int id = RTWN_VAP_ID_INVALID; 487 488 RTWN_LOCK(sc); 489 KASSERT(sc->nvaps <= RTWN_PORT_COUNT, 490 ("nvaps overflow (%d > %d)\n", sc->nvaps, RTWN_PORT_COUNT)); 491 KASSERT(sc->ap_vaps <= RTWN_PORT_COUNT, 492 ("ap_vaps overflow (%d > %d)\n", sc->ap_vaps, RTWN_PORT_COUNT)); 493 KASSERT(sc->bcn_vaps <= RTWN_PORT_COUNT, 494 ("bcn_vaps overflow (%d > %d)\n", sc->bcn_vaps, RTWN_PORT_COUNT)); 495 496 if (opmode != IEEE80211_M_MONITOR) { 497 switch (sc->nvaps) { 498 case 0: 499 id = 0; 500 break; 501 case 1: 502 if (sc->vaps[1] == NULL) 503 id = 1; 504 else if (sc->vaps[0] == NULL) 505 id = 0; 506 KASSERT(id != RTWN_VAP_ID_INVALID, 507 ("no free ports left\n")); 508 break; 509 case 2: 510 default: 511 goto fail; 512 } 513 514 if (opmode == IEEE80211_M_IBSS || 515 opmode == IEEE80211_M_HOSTAP) { 516 if ((sc->bcn_vaps == 1 && !RTWN_CHIP_HAS_BCNQ1(sc)) || 517 sc->bcn_vaps == RTWN_PORT_COUNT) 518 goto fail; 519 } 520 } 521 522 switch (opmode) { 523 case IEEE80211_M_HOSTAP: 524 sc->ap_vaps++; 525 /* FALLTHROUGH */ 526 case IEEE80211_M_IBSS: 527 sc->bcn_vaps++; 528 /* FALLTHROUGH */ 529 case IEEE80211_M_STA: 530 sc->nvaps++; 531 break; 532 case IEEE80211_M_MONITOR: 533 sc->mon_vaps++; 534 break; 535 default: 536 KASSERT(0, ("unknown opmode %d\n", opmode)); 537 goto fail; 538 } 539 RTWN_UNLOCK(sc); 540 541 uvp = malloc(sizeof(struct rtwn_vap), M_80211_VAP, M_WAITOK | M_ZERO); 542 uvp->id = id; 543 if (id != RTWN_VAP_ID_INVALID) { 544 RTWN_LOCK(sc); 545 sc->vaps[id] = uvp; 546 RTWN_UNLOCK(sc); 547 } 548 vap = &uvp->vap; 549 /* enable s/w bmiss handling for sta mode */ 550 551 if (ieee80211_vap_setup(ic, vap, name, unit, opmode, 552 flags | IEEE80211_CLONE_NOBEACONS, bssid) != 0) { 553 /* out of memory */ 554 free(uvp, M_80211_VAP); 555 556 RTWN_LOCK(sc); 557 rtwn_vap_decrement_counters(sc, opmode, id); 558 RTWN_UNLOCK(sc); 559 560 return (NULL); 561 } 562 563 rtwn_beacon_init(sc, &uvp->bcn_desc.txd[0], uvp->id); 564 rtwn_vap_preattach(sc, vap); 565 566 /* override state transition machine */ 567 uvp->newstate = vap->iv_newstate; 568 if (opmode == IEEE80211_M_MONITOR) 569 vap->iv_newstate = rtwn_monitor_newstate; 570 else 571 vap->iv_newstate = rtwn_newstate; 572 vap->iv_update_beacon = rtwn_update_beacon; 573 vap->iv_reset = rtwn_ioctl_reset; 574 vap->iv_key_alloc = rtwn_key_alloc; 575 vap->iv_key_set = rtwn_key_set; 576 vap->iv_key_delete = rtwn_key_delete; 577 vap->iv_max_aid = sc->macid_limit; 578 579 /* 802.11n parameters */ 580 vap->iv_ampdu_density = IEEE80211_HTCAP_MPDUDENSITY_16; 581 vap->iv_ampdu_rxmax = IEEE80211_HTCAP_MAXRXAMPDU_64K; 582 583 if (opmode == IEEE80211_M_IBSS) { 584 uvp->recv_mgmt = vap->iv_recv_mgmt; 585 vap->iv_recv_mgmt = rtwn_adhoc_recv_mgmt; 586 TASK_INIT(&uvp->tsf_sync_adhoc_task, 0, 587 rtwn_tsf_sync_adhoc_task, vap); 588 callout_init(&uvp->tsf_sync_adhoc, 0); 589 } 590 591 /* 592 * NB: driver can select net80211 RA even when user requests 593 * another mechanism. 594 */ 595 ieee80211_ratectl_init(vap); 596 597 /* complete setup */ 598 ieee80211_vap_attach(vap, ieee80211_media_change, 599 ieee80211_media_status, mac); 600 601 RTWN_LOCK(sc); 602 rtwn_set_ic_opmode(sc); 603 if (sc->sc_flags & RTWN_RUNNING) { 604 if (uvp->id != RTWN_VAP_ID_INVALID) 605 rtwn_set_macaddr(sc, vap->iv_myaddr, uvp->id); 606 607 rtwn_rxfilter_update(sc); 608 } 609 RTWN_UNLOCK(sc); 610 611 return (vap); 612 613 fail: 614 RTWN_UNLOCK(sc); 615 return (NULL); 616 } 617 618 static void 619 rtwn_vap_delete(struct ieee80211vap *vap) 620 { 621 struct ieee80211com *ic = vap->iv_ic; 622 struct rtwn_softc *sc = ic->ic_softc; 623 struct rtwn_vap *uvp = RTWN_VAP(vap); 624 625 /* Put vap into INIT state + stop device if needed. */ 626 ieee80211_stop(vap); 627 ieee80211_draintask(ic, &vap->iv_nstate_task); 628 ieee80211_draintask(ic, &ic->ic_parent_task); 629 630 RTWN_LOCK(sc); 631 if (uvp->bcn_mbuf != NULL) 632 m_freem(uvp->bcn_mbuf); 633 /* Cancel any unfinished Tx. */ 634 rtwn_reset_lists(sc, vap); 635 rtwn_vap_decrement_counters(sc, vap->iv_opmode, uvp->id); 636 rtwn_set_ic_opmode(sc); 637 if (sc->sc_flags & RTWN_RUNNING) 638 rtwn_rxfilter_update(sc); 639 RTWN_UNLOCK(sc); 640 641 if (vap->iv_opmode == IEEE80211_M_IBSS) { 642 ieee80211_draintask(ic, &uvp->tsf_sync_adhoc_task); 643 callout_drain(&uvp->tsf_sync_adhoc); 644 } 645 646 ieee80211_ratectl_deinit(vap); 647 ieee80211_vap_detach(vap); 648 free(uvp, M_80211_VAP); 649 } 650 651 static int 652 rtwn_read_chipid(struct rtwn_softc *sc) 653 { 654 uint32_t reg; 655 656 reg = rtwn_read_4(sc, R92C_SYS_CFG); 657 if (reg & R92C_SYS_CFG_TRP_VAUX_EN) /* test chip */ 658 return (EOPNOTSUPP); 659 660 rtwn_read_chipid_vendor(sc, reg); 661 662 return (0); 663 } 664 665 static int 666 rtwn_ioctl_reset(struct ieee80211vap *vap, u_long cmd) 667 { 668 int error; 669 670 switch (cmd) { 671 #ifndef RTWN_WITHOUT_UCODE 672 case IEEE80211_IOC_POWERSAVE: 673 case IEEE80211_IOC_POWERSAVESLEEP: 674 { 675 struct rtwn_softc *sc = vap->iv_ic->ic_softc; 676 struct rtwn_vap *uvp = RTWN_VAP(vap); 677 678 if (vap->iv_opmode == IEEE80211_M_STA && uvp->id == 0) { 679 RTWN_LOCK(sc); 680 if (sc->sc_flags & RTWN_RUNNING) 681 error = rtwn_set_pwrmode(sc, vap, 1); 682 else 683 error = 0; 684 RTWN_UNLOCK(sc); 685 if (error != 0) 686 error = ENETRESET; 687 } else 688 error = EOPNOTSUPP; 689 break; 690 } 691 #endif 692 case IEEE80211_IOC_SHORTGI: 693 case IEEE80211_IOC_RTSTHRESHOLD: 694 case IEEE80211_IOC_PROTMODE: 695 case IEEE80211_IOC_HTPROTMODE: 696 error = 0; 697 break; 698 default: 699 error = ENETRESET; 700 break; 701 } 702 703 return (error); 704 } 705 706 #ifndef RTWN_WITHOUT_UCODE 707 static void 708 rtwn_set_media_status(struct rtwn_softc *sc, union sec_param *data) 709 { 710 sc->sc_set_media_status(sc, data->macid); 711 } 712 713 static int 714 rtwn_tx_fwpkt_check(struct rtwn_softc *sc, struct ieee80211vap *vap) 715 { 716 int ntries, error; 717 718 for (ntries = 0; ntries < 5; ntries++) { 719 error = rtwn_push_nulldata(sc, vap); 720 if (error == 0) 721 break; 722 } 723 if (ntries == 5) { 724 device_printf(sc->sc_dev, 725 "%s: cannot push f/w frames into chip, error %d!\n", 726 __func__, error); 727 return (error); 728 } 729 730 return (0); 731 } 732 733 static int 734 rtwn_construct_nulldata(struct rtwn_softc *sc, struct ieee80211vap *vap, 735 uint8_t *ptr, int qos) 736 { 737 struct rtwn_vap *uvp = RTWN_VAP(vap); 738 struct ieee80211com *ic = &sc->sc_ic; 739 struct rtwn_tx_desc_common *txd; 740 struct ieee80211_frame *wh; 741 int pktlen; 742 743 /* XXX obtain from net80211 */ 744 wh = (struct ieee80211_frame *)(ptr + sc->txdesc_len); 745 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_DATA; 746 wh->i_fc[1] = IEEE80211_FC1_DIR_TODS; 747 IEEE80211_ADDR_COPY(wh->i_addr1, vap->iv_bss->ni_bssid); 748 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr); 749 IEEE80211_ADDR_COPY(wh->i_addr3, vap->iv_bss->ni_macaddr); 750 751 txd = (struct rtwn_tx_desc_common *)ptr; 752 txd->offset = sc->txdesc_len; 753 pktlen = sc->txdesc_len; 754 if (qos) { 755 struct ieee80211_qosframe *qwh; 756 const int tid = WME_AC_TO_TID(WME_AC_BE); 757 758 qwh = (struct ieee80211_qosframe *)wh; 759 qwh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_QOS_NULL; 760 qwh->i_qos[0] = tid & IEEE80211_QOS_TID; 761 762 txd->pktlen = htole16(sizeof(struct ieee80211_qosframe)); 763 pktlen += sizeof(struct ieee80211_qosframe); 764 } else { 765 wh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_NODATA; 766 767 txd->pktlen = htole16(sizeof(struct ieee80211_frame)); 768 pktlen += sizeof(struct ieee80211_frame); 769 } 770 771 rtwn_fill_tx_desc_null(sc, ptr, 772 ic->ic_curmode == IEEE80211_MODE_11B, qos, uvp->id); 773 774 return (pktlen); 775 } 776 777 static int 778 rtwn_push_nulldata(struct rtwn_softc *sc, struct ieee80211vap *vap) 779 { 780 struct rtwn_vap *uvp = RTWN_VAP(vap); 781 struct ieee80211com *ic = vap->iv_ic; 782 struct ieee80211_channel *c = ic->ic_curchan; 783 struct mbuf *m; 784 uint8_t *ptr; 785 int required_size, bcn_size, null_size, null_data, error; 786 787 if (!(sc->sc_flags & RTWN_FW_LOADED)) 788 return (0); /* requires firmware */ 789 790 KASSERT(sc->page_size > 0, ("page size was not set!\n")); 791 792 /* Leave some space for beacon (multi-vap) */ 793 bcn_size = roundup(RTWN_BCN_MAX_SIZE, sc->page_size); 794 /* 1 page for Null Data + 1 page for Qos Null Data frames. */ 795 required_size = bcn_size + sc->page_size * 2; 796 797 m = m_get2(required_size, M_NOWAIT, MT_DATA, M_PKTHDR); 798 if (m == NULL) 799 return (ENOMEM); 800 801 /* Setup beacon descriptor. */ 802 rtwn_beacon_set_rate(sc, &uvp->bcn_desc.txd[0], 803 IEEE80211_IS_CHAN_5GHZ(c)); 804 805 ptr = mtod(m, uint8_t *); 806 memset(ptr, 0, required_size - sc->txdesc_len); 807 808 /* Construct Null Data frame. */ 809 ptr += bcn_size - sc->txdesc_len; 810 null_size = rtwn_construct_nulldata(sc, vap, ptr, 0); 811 KASSERT(null_size < sc->page_size, 812 ("recalculate size for Null Data frame\n")); 813 814 /* Construct Qos Null Data frame. */ 815 ptr += roundup(null_size, sc->page_size); 816 null_size = rtwn_construct_nulldata(sc, vap, ptr, 1); 817 KASSERT(null_size < sc->page_size, 818 ("recalculate size for Qos Null Data frame\n")); 819 820 /* Do not try to detect a beacon here. */ 821 rtwn_setbits_1_shift(sc, R92C_CR, 0, R92C_CR_ENSWBCN, 1); 822 rtwn_setbits_1_shift(sc, R92C_FWHW_TXQ_CTRL, 823 R92C_FWHW_TXQ_CTRL_REAL_BEACON, 0, 2); 824 825 if (uvp->bcn_mbuf != NULL) 826 m_freem(uvp->bcn_mbuf); 827 828 m->m_pkthdr.len = m->m_len = required_size - sc->txdesc_len; 829 uvp->bcn_mbuf = m; 830 831 error = rtwn_tx_beacon_check(sc, uvp); 832 if (error != 0) { 833 RTWN_DPRINTF(sc, RTWN_DEBUG_BEACON, 834 "%s: frame was not recognized!\n", __func__); 835 goto fail; 836 } 837 838 /* Setup addresses in firmware. */ 839 null_data = howmany(bcn_size, sc->page_size); 840 error = rtwn_set_rsvd_page(sc, 0, null_data, null_data + 1); 841 if (error != 0) { 842 device_printf(sc->sc_dev, 843 "%s: CMD_RSVD_PAGE was not sent, error %d\n", 844 __func__, error); 845 goto fail; 846 } 847 848 fail: 849 /* Re-enable beacon detection. */ 850 rtwn_setbits_1_shift(sc, R92C_FWHW_TXQ_CTRL, 851 0, R92C_FWHW_TXQ_CTRL_REAL_BEACON, 2); 852 rtwn_setbits_1_shift(sc, R92C_CR, R92C_CR_ENSWBCN, 0, 1); 853 854 /* Restore beacon (if present). */ 855 if (sc->bcn_vaps > 0 && sc->vaps[!uvp->id] != NULL) { 856 struct rtwn_vap *uvp2 = sc->vaps[!uvp->id]; 857 858 if (uvp2->curr_mode != R92C_MSR_NOLINK) 859 error = rtwn_tx_beacon_check(sc, uvp2); 860 } 861 862 return (error); 863 } 864 865 static void 866 rtwn_pwrmode_init(void *arg) 867 { 868 struct rtwn_softc *sc = arg; 869 870 rtwn_cmd_sleepable(sc, NULL, 0, rtwn_set_pwrmode_cb); 871 } 872 873 static void 874 rtwn_set_pwrmode_cb(struct rtwn_softc *sc, union sec_param *data) 875 { 876 struct ieee80211vap *vap = &sc->vaps[0]->vap; 877 878 if (vap != NULL) 879 rtwn_set_pwrmode(sc, vap, 1); 880 } 881 #endif 882 883 static void 884 rtwn_tsf_sync_adhoc(void *arg) 885 { 886 struct ieee80211vap *vap = arg; 887 struct ieee80211com *ic = vap->iv_ic; 888 struct rtwn_vap *uvp = RTWN_VAP(vap); 889 890 if (uvp->curr_mode != R92C_MSR_NOLINK) { 891 /* Do it in process context. */ 892 ieee80211_runtask(ic, &uvp->tsf_sync_adhoc_task); 893 } 894 } 895 896 /* 897 * Workaround for TSF synchronization: 898 * when BSSID filter in IBSS mode is not set 899 * (and TSF synchronization is enabled), then any beacon may update it. 900 * This routine synchronizes it when BSSID matching is enabled (IBSS merge 901 * is not possible during this period). 902 * 903 * NOTE: there is no race with rtwn_newstate(), since it uses the same 904 * taskqueue. 905 */ 906 static void 907 rtwn_tsf_sync_adhoc_task(void *arg, int pending) 908 { 909 struct ieee80211vap *vap = arg; 910 struct rtwn_vap *uvp = RTWN_VAP(vap); 911 struct rtwn_softc *sc = vap->iv_ic->ic_softc; 912 struct ieee80211_node *ni; 913 914 RTWN_LOCK(sc); 915 ni = ieee80211_ref_node(vap->iv_bss); 916 917 /* Accept beacons with the same BSSID. */ 918 rtwn_set_rx_bssid_all(sc, 0); 919 920 /* Enable synchronization. */ 921 rtwn_setbits_1(sc, R92C_BCN_CTRL(uvp->id), 922 R92C_BCN_CTRL_DIS_TSF_UDT0, 0); 923 924 /* Synchronize. */ 925 rtwn_delay(sc, ni->ni_intval * 5 * 1000); 926 927 /* Disable synchronization. */ 928 rtwn_setbits_1(sc, R92C_BCN_CTRL(uvp->id), 929 0, R92C_BCN_CTRL_DIS_TSF_UDT0); 930 931 /* Accept all beacons. */ 932 rtwn_set_rx_bssid_all(sc, 1); 933 934 /* Schedule next TSF synchronization. */ 935 callout_reset(&uvp->tsf_sync_adhoc, 60*hz, rtwn_tsf_sync_adhoc, vap); 936 937 ieee80211_free_node(ni); 938 RTWN_UNLOCK(sc); 939 } 940 941 static void 942 rtwn_tsf_sync_enable(struct rtwn_softc *sc, struct ieee80211vap *vap) 943 { 944 struct ieee80211com *ic = &sc->sc_ic; 945 struct rtwn_vap *uvp = RTWN_VAP(vap); 946 947 /* Reset TSF. */ 948 rtwn_write_1(sc, R92C_DUAL_TSF_RST, R92C_DUAL_TSF_RESET(uvp->id)); 949 950 switch (vap->iv_opmode) { 951 case IEEE80211_M_STA: 952 /* Enable TSF synchronization. */ 953 rtwn_setbits_1(sc, R92C_BCN_CTRL(uvp->id), 954 R92C_BCN_CTRL_DIS_TSF_UDT0, 0); 955 break; 956 case IEEE80211_M_IBSS: 957 ieee80211_runtask(ic, &uvp->tsf_sync_adhoc_task); 958 /* FALLTHROUGH */ 959 case IEEE80211_M_HOSTAP: 960 /* Enable beaconing. */ 961 rtwn_beacon_enable(sc, uvp->id, 1); 962 break; 963 default: 964 device_printf(sc->sc_dev, "undefined opmode %d\n", 965 vap->iv_opmode); 966 return; 967 } 968 } 969 970 static void 971 rtwn_set_ack_preamble(struct rtwn_softc *sc) 972 { 973 struct ieee80211com *ic = &sc->sc_ic; 974 uint32_t reg; 975 976 reg = rtwn_read_4(sc, R92C_WMAC_TRXPTCL_CTL); 977 if (ic->ic_flags & IEEE80211_F_SHPREAMBLE) 978 reg |= R92C_WMAC_TRXPTCL_SHPRE; 979 else 980 reg &= ~R92C_WMAC_TRXPTCL_SHPRE; 981 rtwn_write_4(sc, R92C_WMAC_TRXPTCL_CTL, reg); 982 } 983 984 static void 985 rtwn_set_mode(struct rtwn_softc *sc, uint8_t mode, int id) 986 { 987 988 rtwn_setbits_1(sc, R92C_MSR, R92C_MSR_MASK << id * 2, mode << id * 2); 989 if (sc->vaps[id] != NULL) 990 sc->vaps[id]->curr_mode = mode; 991 } 992 993 static int 994 rtwn_monitor_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, 995 int arg) 996 { 997 struct ieee80211com *ic = vap->iv_ic; 998 struct rtwn_softc *sc = ic->ic_softc; 999 struct rtwn_vap *uvp = RTWN_VAP(vap); 1000 1001 RTWN_DPRINTF(sc, RTWN_DEBUG_STATE, "%s -> %s\n", 1002 ieee80211_state_name[vap->iv_state], 1003 ieee80211_state_name[nstate]); 1004 1005 if (vap->iv_state != nstate) { 1006 IEEE80211_UNLOCK(ic); 1007 RTWN_LOCK(sc); 1008 1009 switch (nstate) { 1010 case IEEE80211_S_INIT: 1011 sc->vaps_running--; 1012 sc->monvaps_running--; 1013 1014 if (sc->vaps_running == 0) { 1015 /* Turn link LED off. */ 1016 rtwn_set_led(sc, RTWN_LED_LINK, 0); 1017 } 1018 break; 1019 case IEEE80211_S_RUN: 1020 sc->vaps_running++; 1021 sc->monvaps_running++; 1022 1023 if (sc->vaps_running == 1) { 1024 /* Turn link LED on. */ 1025 rtwn_set_led(sc, RTWN_LED_LINK, 1); 1026 } 1027 break; 1028 default: 1029 /* NOTREACHED */ 1030 break; 1031 } 1032 1033 RTWN_UNLOCK(sc); 1034 IEEE80211_LOCK(ic); 1035 } 1036 1037 return (uvp->newstate(vap, nstate, arg)); 1038 } 1039 1040 static int 1041 rtwn_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg) 1042 { 1043 struct rtwn_vap *uvp = RTWN_VAP(vap); 1044 struct ieee80211com *ic = vap->iv_ic; 1045 struct rtwn_softc *sc = ic->ic_softc; 1046 enum ieee80211_state ostate; 1047 int error, early_newstate; 1048 1049 ostate = vap->iv_state; 1050 RTWN_DPRINTF(sc, RTWN_DEBUG_STATE, "%s -> %s\n", 1051 ieee80211_state_name[ostate], ieee80211_state_name[nstate]); 1052 1053 if (vap->iv_bss->ni_chan == IEEE80211_CHAN_ANYC && 1054 ostate == IEEE80211_S_INIT && nstate == IEEE80211_S_RUN) { 1055 /* need to call iv_newstate() firstly */ 1056 error = uvp->newstate(vap, nstate, arg); 1057 if (error != 0) 1058 return (error); 1059 1060 early_newstate = 1; 1061 } else 1062 early_newstate = 0; 1063 1064 IEEE80211_UNLOCK(ic); 1065 RTWN_LOCK(sc); 1066 if (ostate == IEEE80211_S_RUN) { 1067 sc->vaps_running--; 1068 1069 /* Set media status to 'No Link'. */ 1070 rtwn_set_mode(sc, R92C_MSR_NOLINK, uvp->id); 1071 1072 if (vap->iv_opmode == IEEE80211_M_IBSS) { 1073 /* Stop periodical TSF synchronization. */ 1074 callout_stop(&uvp->tsf_sync_adhoc); 1075 } 1076 1077 /* Disable TSF synchronization / beaconing. */ 1078 rtwn_beacon_enable(sc, uvp->id, 0); 1079 rtwn_setbits_1(sc, R92C_BCN_CTRL(uvp->id), 1080 0, R92C_BCN_CTRL_DIS_TSF_UDT0); 1081 1082 /* NB: monitor mode vaps are using port 0. */ 1083 if (uvp->id != 0 || sc->monvaps_running == 0) { 1084 /* Reset TSF. */ 1085 rtwn_write_1(sc, R92C_DUAL_TSF_RST, 1086 R92C_DUAL_TSF_RESET(uvp->id)); 1087 } 1088 1089 #ifndef RTWN_WITHOUT_UCODE 1090 if ((ic->ic_caps & IEEE80211_C_PMGT) != 0 && uvp->id == 0) { 1091 /* Disable power management. */ 1092 callout_stop(&sc->sc_pwrmode_init); 1093 rtwn_set_pwrmode(sc, vap, 0); 1094 } 1095 #endif 1096 if (sc->vaps_running - sc->monvaps_running > 0) { 1097 /* Recalculate basic rates bitmap. */ 1098 rtwn_calc_basicrates(sc); 1099 } 1100 1101 if (sc->vaps_running == sc->monvaps_running) { 1102 /* Stop calibration. */ 1103 callout_stop(&sc->sc_calib_to); 1104 1105 /* Stop Rx of data frames. */ 1106 rtwn_write_2(sc, R92C_RXFLTMAP2, 0); 1107 1108 /* Reset EDCA parameters. */ 1109 rtwn_write_4(sc, R92C_EDCA_VO_PARAM, 0x002f3217); 1110 rtwn_write_4(sc, R92C_EDCA_VI_PARAM, 0x005e4317); 1111 rtwn_write_4(sc, R92C_EDCA_BE_PARAM, 0x00105320); 1112 rtwn_write_4(sc, R92C_EDCA_BK_PARAM, 0x0000a444); 1113 1114 if (sc->vaps_running == 0) { 1115 /* Turn link LED off. */ 1116 rtwn_set_led(sc, RTWN_LED_LINK, 0); 1117 } 1118 } 1119 } 1120 1121 error = 0; 1122 switch (nstate) { 1123 case IEEE80211_S_SCAN: 1124 /* Pause AC Tx queues. */ 1125 if (sc->vaps_running == 0) 1126 rtwn_setbits_1(sc, R92C_TXPAUSE, 0, R92C_TX_QUEUE_AC); 1127 break; 1128 case IEEE80211_S_RUN: 1129 error = rtwn_run(sc, vap); 1130 if (error != 0) { 1131 device_printf(sc->sc_dev, 1132 "%s: could not move to RUN state\n", __func__); 1133 break; 1134 } 1135 1136 sc->vaps_running++; 1137 break; 1138 default: 1139 break; 1140 } 1141 1142 RTWN_UNLOCK(sc); 1143 IEEE80211_LOCK(ic); 1144 if (error != 0) 1145 return (error); 1146 1147 return (early_newstate ? 0 : uvp->newstate(vap, nstate, arg)); 1148 } 1149 1150 static void 1151 rtwn_calc_basicrates(struct rtwn_softc *sc) 1152 { 1153 struct ieee80211com *ic = &sc->sc_ic; 1154 uint32_t basicrates; 1155 int i; 1156 1157 RTWN_ASSERT_LOCKED(sc); 1158 1159 if (ic->ic_flags & IEEE80211_F_SCAN) 1160 return; /* will be done by rtwn_scan_end(). */ 1161 1162 basicrates = 0; 1163 for (i = 0; i < nitems(sc->vaps); i++) { 1164 struct rtwn_vap *rvp; 1165 struct ieee80211vap *vap; 1166 struct ieee80211_node *ni; 1167 uint32_t rates; 1168 1169 rvp = sc->vaps[i]; 1170 if (rvp == NULL || rvp->curr_mode == R92C_MSR_NOLINK) 1171 continue; 1172 1173 vap = &rvp->vap; 1174 if (vap->iv_bss == NULL) 1175 continue; 1176 1177 ni = ieee80211_ref_node(vap->iv_bss); 1178 rtwn_get_rates(sc, &ni->ni_rates, NULL, &rates, NULL, 1); 1179 basicrates |= rates; 1180 ieee80211_free_node(ni); 1181 } 1182 1183 if (basicrates == 0) 1184 return; 1185 1186 /* XXX initial RTS rate? */ 1187 rtwn_set_basicrates(sc, basicrates); 1188 } 1189 1190 static int 1191 rtwn_run(struct rtwn_softc *sc, struct ieee80211vap *vap) 1192 { 1193 struct ieee80211com *ic = vap->iv_ic; 1194 struct rtwn_vap *uvp = RTWN_VAP(vap); 1195 struct ieee80211_node *ni; 1196 uint32_t reg; 1197 uint8_t mode; 1198 int error; 1199 1200 RTWN_ASSERT_LOCKED(sc); 1201 1202 error = 0; 1203 ni = ieee80211_ref_node(vap->iv_bss); 1204 1205 if (ic->ic_bsschan == IEEE80211_CHAN_ANYC || 1206 ni->ni_chan == IEEE80211_CHAN_ANYC) { 1207 error = EINVAL; 1208 goto fail; 1209 } 1210 1211 switch (vap->iv_opmode) { 1212 case IEEE80211_M_STA: 1213 mode = R92C_MSR_INFRA; 1214 break; 1215 case IEEE80211_M_IBSS: 1216 mode = R92C_MSR_ADHOC; 1217 break; 1218 case IEEE80211_M_HOSTAP: 1219 mode = R92C_MSR_AP; 1220 break; 1221 default: 1222 KASSERT(0, ("undefined opmode %d\n", vap->iv_opmode)); 1223 error = EINVAL; 1224 goto fail; 1225 } 1226 1227 /* Set media status to 'Associated'. */ 1228 rtwn_set_mode(sc, mode, uvp->id); 1229 1230 /* Set AssocID. */ 1231 /* XXX multi-vap? */ 1232 rtwn_write_2(sc, R92C_BCN_PSR_RPT, 1233 0xc000 | IEEE80211_NODE_AID(ni)); 1234 1235 /* Set BSSID. */ 1236 rtwn_set_bssid(sc, ni->ni_bssid, uvp->id); 1237 1238 /* Set beacon interval. */ 1239 rtwn_write_2(sc, R92C_BCN_INTERVAL(uvp->id), ni->ni_intval); 1240 1241 if (sc->vaps_running == sc->monvaps_running) { 1242 /* Enable Rx of data frames. */ 1243 rtwn_write_2(sc, R92C_RXFLTMAP2, 0xffff); 1244 1245 /* Flush all AC queues. */ 1246 rtwn_write_1(sc, R92C_TXPAUSE, 0); 1247 } 1248 1249 /* Allow Rx from our BSSID only. */ 1250 if (ic->ic_promisc == 0) { 1251 reg = rtwn_read_4(sc, R92C_RCR); 1252 1253 if (sc->bcn_vaps == 0) 1254 reg |= R92C_RCR_CBSSID_BCN; 1255 if (sc->ap_vaps == 0) 1256 reg |= R92C_RCR_CBSSID_DATA; 1257 1258 rtwn_write_4(sc, R92C_RCR, reg); 1259 } 1260 1261 #ifndef RTWN_WITHOUT_UCODE 1262 /* Upload (QoS) Null Data frame to firmware. */ 1263 /* Note: do this for port 0 only. */ 1264 if ((ic->ic_caps & IEEE80211_C_PMGT) != 0 && 1265 vap->iv_opmode == IEEE80211_M_STA && uvp->id == 0) { 1266 error = rtwn_tx_fwpkt_check(sc, vap); 1267 if (error != 0) 1268 goto fail; 1269 1270 /* Setup power management. */ 1271 /* 1272 * NB: it will be enabled immediately - delay it, 1273 * so 4-Way handshake will not be interrupted. 1274 */ 1275 callout_reset(&sc->sc_pwrmode_init, 5*hz, 1276 rtwn_pwrmode_init, sc); 1277 } 1278 #endif 1279 1280 if (vap->iv_opmode == IEEE80211_M_HOSTAP || 1281 vap->iv_opmode == IEEE80211_M_IBSS) { 1282 error = rtwn_setup_beacon(sc, ni); 1283 if (error != 0) { 1284 device_printf(sc->sc_dev, 1285 "unable to push beacon into the chip, " 1286 "error %d\n", error); 1287 goto fail; 1288 } 1289 } 1290 1291 /* Set ACK preamble type. */ 1292 rtwn_set_ack_preamble(sc); 1293 1294 /* Enable TSF synchronization. */ 1295 rtwn_tsf_sync_enable(sc, vap); 1296 1297 /* Set basic rates mask. */ 1298 rtwn_calc_basicrates(sc); 1299 1300 #ifdef RTWN_TODO 1301 rtwn_write_1(sc, R92C_SIFS_CCK + 1, 10); 1302 rtwn_write_1(sc, R92C_SIFS_OFDM + 1, 10); 1303 rtwn_write_1(sc, R92C_SPEC_SIFS + 1, 10); 1304 rtwn_write_1(sc, R92C_MAC_SPEC_SIFS + 1, 10); 1305 rtwn_write_1(sc, R92C_R2T_SIFS + 1, 10); 1306 rtwn_write_1(sc, R92C_T2T_SIFS + 1, 10); 1307 #endif 1308 1309 if (sc->vaps_running == sc->monvaps_running) { 1310 /* Reset temperature calibration state machine. */ 1311 sc->sc_flags &= ~RTWN_TEMP_MEASURED; 1312 sc->thcal_temp = sc->thermal_meter; 1313 1314 /* Start periodic calibration. */ 1315 callout_reset(&sc->sc_calib_to, 2*hz, rtwn_calib_to, 1316 sc); 1317 1318 if (sc->vaps_running == 0) { 1319 /* Turn link LED on. */ 1320 rtwn_set_led(sc, RTWN_LED_LINK, 1); 1321 } 1322 } 1323 1324 fail: 1325 ieee80211_free_node(ni); 1326 1327 return (error); 1328 } 1329 1330 #ifndef D4054 1331 static void 1332 rtwn_watchdog(void *arg) 1333 { 1334 struct rtwn_softc *sc = arg; 1335 struct ieee80211com *ic = &sc->sc_ic; 1336 1337 RTWN_ASSERT_LOCKED(sc); 1338 1339 KASSERT(sc->sc_flags & RTWN_RUNNING, ("not running")); 1340 1341 if (sc->sc_tx_timer != 0 && --sc->sc_tx_timer == 0) { 1342 ic_printf(ic, "device timeout\n"); 1343 ieee80211_restart_all(ic); 1344 return; 1345 } 1346 callout_reset(&sc->sc_watchdog_to, hz, rtwn_watchdog, sc); 1347 } 1348 #endif 1349 1350 static void 1351 rtwn_parent(struct ieee80211com *ic) 1352 { 1353 struct rtwn_softc *sc = ic->ic_softc; 1354 struct ieee80211vap *vap; 1355 1356 if (ic->ic_nrunning > 0) { 1357 if (rtwn_init(sc) != 0) { 1358 IEEE80211_LOCK(ic); 1359 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) 1360 ieee80211_stop_locked(vap); 1361 IEEE80211_UNLOCK(ic); 1362 } else 1363 ieee80211_start_all(ic); 1364 } else 1365 rtwn_stop(sc); 1366 } 1367 1368 1369 static int 1370 rtwn_llt_write(struct rtwn_softc *sc, uint32_t addr, uint32_t data) 1371 { 1372 int ntries, error; 1373 1374 error = rtwn_write_4(sc, R92C_LLT_INIT, 1375 SM(R92C_LLT_INIT_OP, R92C_LLT_INIT_OP_WRITE) | 1376 SM(R92C_LLT_INIT_ADDR, addr) | 1377 SM(R92C_LLT_INIT_DATA, data)); 1378 if (error != 0) 1379 return (error); 1380 /* Wait for write operation to complete. */ 1381 for (ntries = 0; ntries < 20; ntries++) { 1382 if (MS(rtwn_read_4(sc, R92C_LLT_INIT), R92C_LLT_INIT_OP) == 1383 R92C_LLT_INIT_OP_NO_ACTIVE) 1384 return (0); 1385 rtwn_delay(sc, 10); 1386 } 1387 return (ETIMEDOUT); 1388 } 1389 1390 static int 1391 rtwn_llt_init(struct rtwn_softc *sc) 1392 { 1393 int i, error; 1394 1395 /* Reserve pages [0; page_count]. */ 1396 for (i = 0; i < sc->page_count; i++) { 1397 if ((error = rtwn_llt_write(sc, i, i + 1)) != 0) 1398 return (error); 1399 } 1400 /* NB: 0xff indicates end-of-list. */ 1401 if ((error = rtwn_llt_write(sc, i, 0xff)) != 0) 1402 return (error); 1403 /* 1404 * Use pages [page_count + 1; pktbuf_count - 1] 1405 * as ring buffer. 1406 */ 1407 for (++i; i < sc->pktbuf_count - 1; i++) { 1408 if ((error = rtwn_llt_write(sc, i, i + 1)) != 0) 1409 return (error); 1410 } 1411 /* Make the last page point to the beginning of the ring buffer. */ 1412 error = rtwn_llt_write(sc, i, sc->page_count + 1); 1413 return (error); 1414 } 1415 1416 static int 1417 rtwn_dma_init(struct rtwn_softc *sc) 1418 { 1419 #define RTWN_CHK(res) do { \ 1420 if (res != 0) \ 1421 return (EIO); \ 1422 } while(0) 1423 uint16_t reg; 1424 uint8_t tx_boundary; 1425 int error; 1426 1427 /* Initialize LLT table. */ 1428 error = rtwn_llt_init(sc); 1429 if (error != 0) 1430 return (error); 1431 1432 /* Set the number of pages for each queue. */ 1433 RTWN_DPRINTF(sc, RTWN_DEBUG_RESET, 1434 "%s: pages per queue: high %d, normal %d, low %d, public %d\n", 1435 __func__, sc->nhqpages, sc->nnqpages, sc->nlqpages, 1436 sc->npubqpages); 1437 1438 RTWN_CHK(rtwn_write_1(sc, R92C_RQPN_NPQ, sc->nnqpages)); 1439 RTWN_CHK(rtwn_write_4(sc, R92C_RQPN, 1440 /* Set number of pages for public queue. */ 1441 SM(R92C_RQPN_PUBQ, sc->npubqpages) | 1442 /* Set number of pages for high priority queue. */ 1443 SM(R92C_RQPN_HPQ, sc->nhqpages) | 1444 /* Set number of pages for low priority queue. */ 1445 SM(R92C_RQPN_LPQ, sc->nlqpages) | 1446 /* Load values. */ 1447 R92C_RQPN_LD)); 1448 1449 /* Initialize TX buffer boundary. */ 1450 KASSERT(sc->page_count < 255 && sc->page_count > 0, 1451 ("page_count is %d\n", sc->page_count)); 1452 tx_boundary = sc->page_count + 1; 1453 RTWN_CHK(rtwn_write_1(sc, R92C_TXPKTBUF_BCNQ_BDNY, tx_boundary)); 1454 RTWN_CHK(rtwn_write_1(sc, R92C_TXPKTBUF_MGQ_BDNY, tx_boundary)); 1455 RTWN_CHK(rtwn_write_1(sc, R92C_TXPKTBUF_WMAC_LBK_BF_HD, tx_boundary)); 1456 RTWN_CHK(rtwn_write_1(sc, R92C_TRXFF_BNDY, tx_boundary)); 1457 RTWN_CHK(rtwn_write_1(sc, R92C_TDECTRL + 1, tx_boundary)); 1458 1459 error = rtwn_init_bcnq1_boundary(sc); 1460 if (error != 0) 1461 return (error); 1462 1463 /* Set queue to USB pipe mapping. */ 1464 /* Note: PCIe devices are using some magic number here. */ 1465 reg = rtwn_get_qmap(sc); 1466 RTWN_CHK(rtwn_setbits_2(sc, R92C_TRXDMA_CTRL, 1467 R92C_TRXDMA_CTRL_QMAP_M, reg)); 1468 1469 /* Configure Tx/Rx DMA (PCIe). */ 1470 rtwn_set_desc_addr(sc); 1471 1472 /* Set Tx/Rx transfer page boundary. */ 1473 RTWN_CHK(rtwn_write_2(sc, R92C_TRXFF_BNDY + 2, 1474 sc->rx_dma_size - 1)); 1475 1476 /* Set Tx/Rx transfer page size. */ 1477 rtwn_set_page_size(sc); 1478 1479 return (0); 1480 } 1481 1482 static int 1483 rtwn_mac_init(struct rtwn_softc *sc) 1484 { 1485 int i, error; 1486 1487 /* Write MAC initialization values. */ 1488 for (i = 0; i < sc->mac_size; i++) { 1489 error = rtwn_write_1(sc, sc->mac_prog[i].reg, 1490 sc->mac_prog[i].val); 1491 if (error != 0) 1492 return (error); 1493 } 1494 1495 return (0); 1496 } 1497 1498 static void 1499 rtwn_mrr_init(struct rtwn_softc *sc) 1500 { 1501 int i; 1502 1503 /* Drop rate index by 1 per retry. */ 1504 for (i = 0; i < R92C_DARFRC_SIZE; i++) { 1505 rtwn_write_1(sc, R92C_DARFRC + i, i + 1); 1506 rtwn_write_1(sc, R92C_RARFRC + i, i + 1); 1507 } 1508 } 1509 1510 static void 1511 rtwn_scan_start(struct ieee80211com *ic) 1512 { 1513 struct rtwn_softc *sc = ic->ic_softc; 1514 1515 RTWN_LOCK(sc); 1516 /* Receive beacons / probe responses from any BSSID. */ 1517 if (sc->bcn_vaps == 0) 1518 rtwn_set_rx_bssid_all(sc, 1); 1519 RTWN_UNLOCK(sc); 1520 } 1521 1522 static void 1523 rtwn_scan_curchan(struct ieee80211_scan_state *ss, unsigned long maxdwell) 1524 { 1525 struct rtwn_softc *sc = ss->ss_ic->ic_softc; 1526 1527 /* Make link LED blink during scan. */ 1528 RTWN_LOCK(sc); 1529 rtwn_set_led(sc, RTWN_LED_LINK, !sc->ledlink); 1530 RTWN_UNLOCK(sc); 1531 1532 sc->sc_scan_curchan(ss, maxdwell); 1533 } 1534 1535 static void 1536 rtwn_scan_end(struct ieee80211com *ic) 1537 { 1538 struct rtwn_softc *sc = ic->ic_softc; 1539 1540 RTWN_LOCK(sc); 1541 /* Restore limitations. */ 1542 if (ic->ic_promisc == 0 && sc->bcn_vaps == 0) 1543 rtwn_set_rx_bssid_all(sc, 0); 1544 1545 /* Restore LED state. */ 1546 rtwn_set_led(sc, RTWN_LED_LINK, (sc->vaps_running != 0)); 1547 1548 /* Restore basic rates mask. */ 1549 rtwn_calc_basicrates(sc); 1550 RTWN_UNLOCK(sc); 1551 } 1552 1553 static void 1554 rtwn_getradiocaps(struct ieee80211com *ic, 1555 int maxchans, int *nchans, struct ieee80211_channel chans[]) 1556 { 1557 struct rtwn_softc *sc = ic->ic_softc; 1558 uint8_t bands[IEEE80211_MODE_BYTES]; 1559 int i; 1560 1561 memset(bands, 0, sizeof(bands)); 1562 setbit(bands, IEEE80211_MODE_11B); 1563 setbit(bands, IEEE80211_MODE_11G); 1564 setbit(bands, IEEE80211_MODE_11NG); 1565 ieee80211_add_channel_list_2ghz(chans, maxchans, nchans, 1566 rtwn_chan_2ghz, nitems(rtwn_chan_2ghz), bands, 1567 !!(ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40)); 1568 1569 /* XXX workaround add_channel_list() limitations */ 1570 setbit(bands, IEEE80211_MODE_11A); 1571 setbit(bands, IEEE80211_MODE_11NA); 1572 for (i = 0; i < nitems(sc->chan_num_5ghz); i++) { 1573 if (sc->chan_num_5ghz[i] == 0) 1574 continue; 1575 1576 ieee80211_add_channel_list_5ghz(chans, maxchans, nchans, 1577 sc->chan_list_5ghz[i], sc->chan_num_5ghz[i], bands, 1578 !!(ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40)); 1579 } 1580 } 1581 1582 static void 1583 rtwn_update_chw(struct ieee80211com *ic) 1584 { 1585 } 1586 1587 static void 1588 rtwn_set_channel(struct ieee80211com *ic) 1589 { 1590 struct rtwn_softc *sc = ic->ic_softc; 1591 struct ieee80211_channel *c = ic->ic_curchan; 1592 1593 RTWN_LOCK(sc); 1594 rtwn_set_chan(sc, c); 1595 sc->sc_rxtap.wr_chan_freq = htole16(c->ic_freq); 1596 sc->sc_rxtap.wr_chan_flags = htole16(c->ic_flags); 1597 sc->sc_txtap.wt_chan_freq = htole16(c->ic_freq); 1598 sc->sc_txtap.wt_chan_flags = htole16(c->ic_flags); 1599 RTWN_UNLOCK(sc); 1600 } 1601 1602 static int 1603 rtwn_wme_update(struct ieee80211com *ic) 1604 { 1605 struct ieee80211_channel *c = ic->ic_curchan; 1606 struct rtwn_softc *sc = ic->ic_softc; 1607 struct wmeParams *wmep = sc->cap_wmeParams; 1608 uint8_t aifs, acm, slottime; 1609 int ac; 1610 1611 /* Prevent possible races. */ 1612 IEEE80211_LOCK(ic); /* XXX */ 1613 RTWN_LOCK(sc); 1614 memcpy(wmep, ic->ic_wme.wme_chanParams.cap_wmeParams, 1615 sizeof(sc->cap_wmeParams)); 1616 RTWN_UNLOCK(sc); 1617 IEEE80211_UNLOCK(ic); 1618 1619 acm = 0; 1620 slottime = IEEE80211_GET_SLOTTIME(ic); 1621 1622 RTWN_LOCK(sc); 1623 for (ac = WME_AC_BE; ac < WME_NUM_AC; ac++) { 1624 /* AIFS[AC] = AIFSN[AC] * aSlotTime + aSIFSTime. */ 1625 aifs = wmep[ac].wmep_aifsn * slottime + 1626 (IEEE80211_IS_CHAN_5GHZ(c) ? 1627 IEEE80211_DUR_OFDM_SIFS : IEEE80211_DUR_SIFS); 1628 rtwn_write_4(sc, wme2reg[ac], 1629 SM(R92C_EDCA_PARAM_TXOP, wmep[ac].wmep_txopLimit) | 1630 SM(R92C_EDCA_PARAM_ECWMIN, wmep[ac].wmep_logcwmin) | 1631 SM(R92C_EDCA_PARAM_ECWMAX, wmep[ac].wmep_logcwmax) | 1632 SM(R92C_EDCA_PARAM_AIFS, aifs)); 1633 if (ac != WME_AC_BE) 1634 acm |= wmep[ac].wmep_acm << ac; 1635 } 1636 1637 if (acm != 0) 1638 acm |= R92C_ACMHWCTRL_EN; 1639 rtwn_setbits_1(sc, R92C_ACMHWCTRL, R92C_ACMHWCTRL_ACM_MASK, acm); 1640 RTWN_UNLOCK(sc); 1641 1642 return 0; 1643 } 1644 1645 static void 1646 rtwn_update_slot(struct ieee80211com *ic) 1647 { 1648 rtwn_cmd_sleepable(ic->ic_softc, NULL, 0, rtwn_update_slot_cb); 1649 } 1650 1651 static void 1652 rtwn_update_slot_cb(struct rtwn_softc *sc, union sec_param *data) 1653 { 1654 struct ieee80211com *ic = &sc->sc_ic; 1655 uint8_t slottime; 1656 1657 slottime = IEEE80211_GET_SLOTTIME(ic); 1658 1659 RTWN_DPRINTF(sc, RTWN_DEBUG_STATE, "%s: setting slot time to %uus\n", 1660 __func__, slottime); 1661 1662 rtwn_write_1(sc, R92C_SLOT, slottime); 1663 rtwn_update_aifs(sc, slottime); 1664 } 1665 1666 static void 1667 rtwn_update_aifs(struct rtwn_softc *sc, uint8_t slottime) 1668 { 1669 struct ieee80211_channel *c = sc->sc_ic.ic_curchan; 1670 const struct wmeParams *wmep = sc->cap_wmeParams; 1671 uint8_t aifs, ac; 1672 1673 for (ac = WME_AC_BE; ac < WME_NUM_AC; ac++) { 1674 /* AIFS[AC] = AIFSN[AC] * aSlotTime + aSIFSTime. */ 1675 aifs = wmep[ac].wmep_aifsn * slottime + 1676 (IEEE80211_IS_CHAN_5GHZ(c) ? 1677 IEEE80211_DUR_OFDM_SIFS : IEEE80211_DUR_SIFS); 1678 rtwn_write_1(sc, wme2reg[ac], aifs); 1679 } 1680 } 1681 1682 static void 1683 rtwn_update_promisc(struct ieee80211com *ic) 1684 { 1685 struct rtwn_softc *sc = ic->ic_softc; 1686 1687 RTWN_LOCK(sc); 1688 if (sc->sc_flags & RTWN_RUNNING) 1689 rtwn_set_promisc(sc); 1690 RTWN_UNLOCK(sc); 1691 } 1692 1693 static void 1694 rtwn_update_mcast(struct ieee80211com *ic) 1695 { 1696 struct rtwn_softc *sc = ic->ic_softc; 1697 1698 RTWN_LOCK(sc); 1699 if (sc->sc_flags & RTWN_RUNNING) 1700 rtwn_set_multi(sc); 1701 RTWN_UNLOCK(sc); 1702 } 1703 1704 static int 1705 rtwn_set_bssid(struct rtwn_softc *sc, const uint8_t *bssid, int id) 1706 { 1707 int error; 1708 1709 error = rtwn_write_4(sc, R92C_BSSID(id), le32dec(&bssid[0])); 1710 if (error != 0) 1711 return (error); 1712 error = rtwn_write_2(sc, R92C_BSSID(id) + 4, le16dec(&bssid[4])); 1713 1714 return (error); 1715 } 1716 1717 static int 1718 rtwn_set_macaddr(struct rtwn_softc *sc, const uint8_t *addr, int id) 1719 { 1720 int error; 1721 1722 error = rtwn_write_4(sc, R92C_MACID(id), le32dec(&addr[0])); 1723 if (error != 0) 1724 return (error); 1725 error = rtwn_write_2(sc, R92C_MACID(id) + 4, le16dec(&addr[4])); 1726 1727 return (error); 1728 } 1729 1730 static struct ieee80211_node * 1731 rtwn_node_alloc(struct ieee80211vap *vap, 1732 const uint8_t mac[IEEE80211_ADDR_LEN]) 1733 { 1734 struct rtwn_node *un; 1735 1736 un = malloc(sizeof (struct rtwn_node), M_80211_NODE, 1737 M_NOWAIT | M_ZERO); 1738 1739 if (un == NULL) 1740 return NULL; 1741 1742 un->id = RTWN_MACID_UNDEFINED; 1743 un->avg_pwdb = -1; 1744 1745 return &un->ni; 1746 } 1747 1748 static void 1749 rtwn_newassoc(struct ieee80211_node *ni, int isnew) 1750 { 1751 struct rtwn_softc *sc = ni->ni_ic->ic_softc; 1752 struct rtwn_node *un = RTWN_NODE(ni); 1753 int id; 1754 1755 if (!isnew) 1756 return; 1757 1758 RTWN_NT_LOCK(sc); 1759 for (id = 0; id <= sc->macid_limit; id++) { 1760 if (id != RTWN_MACID_BC && sc->node_list[id] == NULL) { 1761 un->id = id; 1762 sc->node_list[id] = ni; 1763 break; 1764 } 1765 } 1766 RTWN_NT_UNLOCK(sc); 1767 1768 if (id > sc->macid_limit) { 1769 device_printf(sc->sc_dev, "%s: node table is full\n", 1770 __func__); 1771 return; 1772 } 1773 1774 #ifndef RTWN_WITHOUT_UCODE 1775 /* Notify firmware. */ 1776 id |= RTWN_MACID_VALID; 1777 rtwn_cmd_sleepable(sc, &id, sizeof(id), rtwn_set_media_status); 1778 #endif 1779 } 1780 1781 static void 1782 rtwn_node_free(struct ieee80211_node *ni) 1783 { 1784 struct rtwn_softc *sc = ni->ni_ic->ic_softc; 1785 struct rtwn_node *un = RTWN_NODE(ni); 1786 1787 RTWN_NT_LOCK(sc); 1788 if (un->id != RTWN_MACID_UNDEFINED) { 1789 sc->node_list[un->id] = NULL; 1790 #ifndef RTWN_WITHOUT_UCODE 1791 rtwn_cmd_sleepable(sc, &un->id, sizeof(un->id), 1792 rtwn_set_media_status); 1793 #endif 1794 } 1795 RTWN_NT_UNLOCK(sc); 1796 1797 sc->sc_node_free(ni); 1798 } 1799 1800 static void 1801 rtwn_init_beacon_reg(struct rtwn_softc *sc) 1802 { 1803 rtwn_write_1(sc, R92C_BCN_CTRL(0), R92C_BCN_CTRL_DIS_TSF_UDT0); 1804 rtwn_write_1(sc, R92C_BCN_CTRL(1), R92C_BCN_CTRL_DIS_TSF_UDT0); 1805 rtwn_write_2(sc, R92C_TBTT_PROHIBIT, 0x6404); 1806 rtwn_write_1(sc, R92C_DRVERLYINT, 0x05); 1807 rtwn_write_1(sc, R92C_BCNDMATIM, 0x02); 1808 rtwn_write_2(sc, R92C_BCNTCFG, 0x660f); 1809 } 1810 1811 static int 1812 rtwn_init(struct rtwn_softc *sc) 1813 { 1814 struct ieee80211com *ic = &sc->sc_ic; 1815 int i, error; 1816 1817 RTWN_LOCK(sc); 1818 if (sc->sc_flags & RTWN_RUNNING) { 1819 RTWN_UNLOCK(sc); 1820 return (0); 1821 } 1822 sc->sc_flags |= RTWN_STARTED; 1823 1824 /* Power on adapter. */ 1825 error = rtwn_power_on(sc); 1826 if (error != 0) 1827 goto fail; 1828 1829 #ifndef RTWN_WITHOUT_UCODE 1830 /* Load 8051 microcode. */ 1831 error = rtwn_load_firmware(sc); 1832 if (error == 0) 1833 sc->sc_flags |= RTWN_FW_LOADED; 1834 1835 /* Init firmware commands ring. */ 1836 sc->fwcur = 0; 1837 #endif 1838 1839 /* Initialize MAC block. */ 1840 error = rtwn_mac_init(sc); 1841 if (error != 0) { 1842 device_printf(sc->sc_dev, 1843 "%s: error while initializing MAC block\n", __func__); 1844 goto fail; 1845 } 1846 1847 /* Initialize DMA. */ 1848 error = rtwn_dma_init(sc); 1849 if (error != 0) 1850 goto fail; 1851 1852 /* Drop incorrect TX (USB). */ 1853 rtwn_drop_incorrect_tx(sc); 1854 1855 /* Set info size in Rx descriptors (in 64-bit words). */ 1856 rtwn_write_1(sc, R92C_RX_DRVINFO_SZ, R92C_RX_DRVINFO_SZ_DEF); 1857 1858 /* Init interrupts. */ 1859 rtwn_init_intr(sc); 1860 1861 for (i = 0; i < nitems(sc->vaps); i++) { 1862 struct rtwn_vap *uvp = sc->vaps[i]; 1863 1864 /* Set initial network type. */ 1865 rtwn_set_mode(sc, R92C_MSR_NOLINK, i); 1866 1867 if (uvp == NULL) 1868 continue; 1869 1870 /* Set MAC address. */ 1871 error = rtwn_set_macaddr(sc, uvp->vap.iv_myaddr, uvp->id); 1872 if (error != 0) 1873 goto fail; 1874 } 1875 1876 /* Initialize Rx filter. */ 1877 rtwn_rxfilter_init(sc); 1878 1879 /* Set short/long retry limits. */ 1880 rtwn_write_2(sc, R92C_RL, 1881 SM(R92C_RL_SRL, 0x30) | SM(R92C_RL_LRL, 0x30)); 1882 1883 /* Initialize EDCA parameters. */ 1884 rtwn_init_edca(sc); 1885 1886 rtwn_setbits_1(sc, R92C_FWHW_TXQ_CTRL, 0, 1887 R92C_FWHW_TXQ_CTRL_AMPDU_RTY_NEW); 1888 /* Set ACK timeout. */ 1889 rtwn_write_1(sc, R92C_ACKTO, sc->ackto); 1890 1891 /* Setup aggregation. */ 1892 /* Tx aggregation. */ 1893 rtwn_init_tx_agg(sc); 1894 rtwn_init_rx_agg(sc); 1895 1896 /* Initialize beacon parameters. */ 1897 rtwn_init_beacon_reg(sc); 1898 1899 /* Init A-MPDU parameters. */ 1900 rtwn_init_ampdu(sc); 1901 1902 /* Init MACTXEN / MACRXEN after setting RxFF boundary. */ 1903 rtwn_setbits_1(sc, R92C_CR, 0, R92C_CR_MACTXEN | R92C_CR_MACRXEN); 1904 1905 /* Initialize BB/RF blocks. */ 1906 rtwn_init_bb(sc); 1907 rtwn_init_rf(sc); 1908 1909 /* Initialize wireless band. */ 1910 rtwn_set_chan(sc, ic->ic_curchan); 1911 1912 /* Clear per-station keys table. */ 1913 rtwn_init_cam(sc); 1914 1915 /* Enable decryption / encryption. */ 1916 rtwn_init_seccfg(sc); 1917 1918 /* Install static keys (if any). */ 1919 for (i = 0; i < nitems(sc->vaps); i++) { 1920 if (sc->vaps[i] != NULL) { 1921 error = rtwn_init_static_keys(sc, sc->vaps[i]); 1922 if (error != 0) 1923 goto fail; 1924 } 1925 } 1926 1927 /* Initialize antenna selection. */ 1928 rtwn_init_antsel(sc); 1929 1930 /* Enable hardware sequence numbering. */ 1931 rtwn_write_1(sc, R92C_HWSEQ_CTRL, R92C_TX_QUEUE_ALL); 1932 1933 /* Disable BAR. */ 1934 rtwn_write_4(sc, R92C_BAR_MODE_CTRL, 0x0201ffff); 1935 1936 /* NAV limit. */ 1937 rtwn_write_1(sc, R92C_NAV_UPPER, 0); 1938 1939 /* Initialize GPIO setting. */ 1940 rtwn_setbits_1(sc, R92C_GPIO_MUXCFG, R92C_GPIO_MUXCFG_ENBT, 0); 1941 1942 /* Initialize MRR. */ 1943 rtwn_mrr_init(sc); 1944 1945 /* Device-specific post initialization. */ 1946 rtwn_post_init(sc); 1947 1948 rtwn_start_xfers(sc); 1949 1950 #ifndef D4054 1951 callout_reset(&sc->sc_watchdog_to, hz, rtwn_watchdog, sc); 1952 #endif 1953 1954 sc->sc_flags |= RTWN_RUNNING; 1955 fail: 1956 RTWN_UNLOCK(sc); 1957 1958 return (error); 1959 } 1960 1961 static void 1962 rtwn_stop(struct rtwn_softc *sc) 1963 { 1964 1965 RTWN_LOCK(sc); 1966 if (!(sc->sc_flags & RTWN_STARTED)) { 1967 RTWN_UNLOCK(sc); 1968 return; 1969 } 1970 1971 #ifndef D4054 1972 callout_stop(&sc->sc_watchdog_to); 1973 #endif 1974 sc->sc_flags &= ~(RTWN_STARTED | RTWN_RUNNING | RTWN_FW_LOADED); 1975 sc->sc_flags &= ~RTWN_TEMP_MEASURED; 1976 sc->fwver = 0; 1977 sc->thcal_temp = 0; 1978 sc->cur_bcnq_id = RTWN_VAP_ID_INVALID; 1979 1980 #ifdef D4054 1981 ieee80211_tx_watchdog_stop(&sc->sc_ic); 1982 #endif 1983 1984 rtwn_abort_xfers(sc); 1985 rtwn_drain_mbufq(sc); 1986 rtwn_power_off(sc); 1987 rtwn_reset_lists(sc, NULL); 1988 RTWN_UNLOCK(sc); 1989 } 1990 1991 MODULE_VERSION(rtwn, 2); 1992 MODULE_DEPEND(rtwn, wlan, 1, 1, 1); 1993 #ifndef RTWN_WITHOUT_UCODE 1994 MODULE_DEPEND(rtwn, firmware, 1, 1, 1); 1995 #endif 1996