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