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