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