Index: head/sys/dev/rtwn/if_rtwn.c =================================================================== --- head/sys/dev/rtwn/if_rtwn.c +++ head/sys/dev/rtwn/if_rtwn.c @@ -580,6 +580,8 @@ vap->iv_ampdu_density = IEEE80211_HTCAP_MPDUDENSITY_16; vap->iv_ampdu_rxmax = IEEE80211_HTCAP_MAXRXAMPDU_64K; + TIMEOUT_TASK_INIT(taskqueue_thread, &uvp->tx_beacon_csa, 0, + rtwn_tx_beacon_csa, vap); if (opmode == IEEE80211_M_IBSS) { uvp->recv_mgmt = vap->iv_recv_mgmt; vap->iv_recv_mgmt = rtwn_adhoc_recv_mgmt; @@ -1067,9 +1069,26 @@ } else early_newstate = 0; + if (ostate == IEEE80211_S_CSA) { + taskqueue_cancel_timeout(taskqueue_thread, + &uvp->tx_beacon_csa, NULL); + + /* + * In multi-vap case second counter may not be cleared + * properly. + */ + vap->iv_csa_count = 0; + } IEEE80211_UNLOCK(ic); RTWN_LOCK(sc); - if (ostate == IEEE80211_S_RUN) { + + if (ostate == IEEE80211_S_CSA) { + /* Unblock all queues (multi-vap case). */ + rtwn_write_1(sc, R92C_TXPAUSE, 0); + } + + if ((ostate == IEEE80211_S_RUN && nstate != IEEE80211_S_CSA) || + ostate == IEEE80211_S_CSA) { sc->vaps_running--; /* Set media status to 'No Link'. */ @@ -1141,6 +1160,11 @@ sc->vaps_running++; break; + case IEEE80211_S_CSA: + /* Block all Tx queues (except beacon queue). */ + rtwn_setbits_1(sc, R92C_TXPAUSE, 0, + R92C_TX_QUEUE_AC | R92C_TX_QUEUE_MGT | R92C_TX_QUEUE_HIGH); + break; default: break; } Index: head/sys/dev/rtwn/if_rtwn_beacon.h =================================================================== --- head/sys/dev/rtwn/if_rtwn_beacon.h +++ head/sys/dev/rtwn/if_rtwn_beacon.h @@ -22,6 +22,7 @@ void rtwn_switch_bcnq(struct rtwn_softc *, int); int rtwn_setup_beacon(struct rtwn_softc *, struct ieee80211_node *); void rtwn_update_beacon(struct ieee80211vap *, int); +void rtwn_tx_beacon_csa(void *arg, int); int rtwn_tx_beacon_check(struct rtwn_softc *, struct rtwn_vap *); #endif /* IF_RTWN_BEACON_H */ Index: head/sys/dev/rtwn/if_rtwn_beacon.c =================================================================== --- head/sys/dev/rtwn/if_rtwn_beacon.c +++ head/sys/dev/rtwn/if_rtwn_beacon.c @@ -160,7 +160,8 @@ void rtwn_update_beacon(struct ieee80211vap *vap, int item) { - struct rtwn_softc *sc = vap->iv_ic->ic_softc; + struct ieee80211com *ic = vap->iv_ic; + struct rtwn_softc *sc = ic->ic_softc; struct rtwn_vap *uvp = RTWN_VAP(vap); struct ieee80211_beacon_offsets *bo = &vap->iv_bcn_off; struct ieee80211_node *ni = vap->iv_bss; @@ -176,21 +177,71 @@ return; } } - rtwn_beacon_update_begin(sc, vap); - RTWN_UNLOCK(sc); - if (item == IEEE80211_BEACON_TIM) + RTWN_DPRINTF(sc, RTWN_DEBUG_BEACON, + "%s: vap id %d, iv_csa_count %d, ic_csa_count %d, item %d\n", + __func__, uvp->id, vap->iv_csa_count, ic->ic_csa_count, item); + + switch (item) { + case IEEE80211_BEACON_CSA: + if (vap->iv_csa_count != ic->ic_csa_count) { + /* + * XXX two APs with different beacon intervals + * are not handled properly. + */ + /* XXX check TBTT? */ + taskqueue_enqueue_timeout(taskqueue_thread, + &uvp->tx_beacon_csa, + msecs_to_ticks(ni->ni_intval)); + } + break; + case IEEE80211_BEACON_TIM: mcast = 1; /* XXX */ + break; + default: + break; + } setbit(bo->bo_flags, item); + + rtwn_beacon_update_begin(sc, vap); + RTWN_UNLOCK(sc); + ieee80211_beacon_update(ni, uvp->bcn_mbuf, mcast); + /* XXX clear manually */ + clrbit(bo->bo_flags, IEEE80211_BEACON_CSA); + RTWN_LOCK(sc); rtwn_tx_beacon(sc, uvp); rtwn_beacon_update_end(sc, vap); RTWN_UNLOCK(sc); } +void +rtwn_tx_beacon_csa(void *arg, int npending __unused) +{ + struct ieee80211vap *vap = arg; + struct ieee80211com *ic = vap->iv_ic; + struct rtwn_softc *sc = ic->ic_softc; + struct rtwn_vap *rvp = RTWN_VAP(vap); + + KASSERT (rvp->id == 0 || rvp->id == 1, + ("wrong port id %d\n", rvp->id)); + + IEEE80211_LOCK(ic); + if (ic->ic_flags & IEEE80211_F_CSAPENDING) { + RTWN_DPRINTF(sc, RTWN_DEBUG_BEACON, + "%s: vap id %d, iv_csa_count %d, ic_csa_count %d\n", + __func__, rvp->id, vap->iv_csa_count, ic->ic_csa_count); + + rtwn_update_beacon(vap, IEEE80211_BEACON_CSA); + } + IEEE80211_UNLOCK(ic); + + (void) rvp; +} + int rtwn_tx_beacon_check(struct rtwn_softc *sc, struct rtwn_vap *uvp) { Index: head/sys/dev/rtwn/if_rtwn_debug.h =================================================================== --- head/sys/dev/rtwn/if_rtwn_debug.h +++ head/sys/dev/rtwn/if_rtwn_debug.h @@ -44,6 +44,7 @@ RTWN_DEBUG_RSSI = 0x00004000, /* dump RSSI lookups */ RTWN_DEBUG_RESET = 0x00008000, /* initialization progress */ RTWN_DEBUG_CALIB = 0x00010000, /* calibration progress */ + RTWN_DEBUG_RADAR = 0x00020000, /* radar detection status */ RTWN_DEBUG_ANY = 0xffffffff }; Index: head/sys/dev/rtwn/if_rtwnvar.h =================================================================== --- head/sys/dev/rtwn/if_rtwnvar.h +++ head/sys/dev/rtwn/if_rtwnvar.h @@ -109,6 +109,7 @@ struct rtwn_tx_buf bcn_desc; struct mbuf *bcn_mbuf; + struct timeout_task tx_beacon_csa; struct callout tsf_sync_adhoc; struct task tsf_sync_adhoc_task; Index: head/sys/dev/rtwn/rtl8812a/r12a_var.h =================================================================== --- head/sys/dev/rtwn/rtl8812a/r12a_var.h +++ head/sys/dev/rtwn/rtl8812a/r12a_var.h @@ -39,6 +39,10 @@ #define R12A_RXCKSUM_EN 0x01 #define R12A_RXCKSUM6_EN 0x02 #define R12A_IQK_RUNNING 0x04 +#define R12A_RADAR_ENABLED 0x08 + + int rs_radar; + struct timeout_task rs_chan_check; /* ROM variables */ int ext_pa_2g:1, @@ -77,6 +81,11 @@ int sc_ant; + int (*rs_newstate[RTWN_PORT_COUNT])(struct ieee80211vap *, + enum ieee80211_state, int); + void (*rs_scan_start)(struct ieee80211com *); + void (*rs_scan_end)(struct ieee80211com *); + void (*rs_crystalcap_write)(struct rtwn_softc *); void (*rs_fix_spur)(struct rtwn_softc *, struct ieee80211_channel *); Index: head/sys/dev/rtwn/rtl8821a/r21a_init.c =================================================================== --- head/sys/dev/rtwn/rtl8821a/r21a_init.c +++ head/sys/dev/rtwn/rtl8821a/r21a_init.c @@ -280,7 +280,7 @@ /* Enable GPIO9 as EXT WAKEUP. */ rtwn_setbits_1(sc, R92C_GPIO_INTM + 2, 0, 0x01); - rs->rs_flags &= ~R12A_IQK_RUNNING; + rs->rs_flags &= ~(R12A_IQK_RUNNING | R12A_RADAR_ENABLED); } int Index: head/sys/dev/rtwn/rtl8821a/usb/r21au.h =================================================================== --- head/sys/dev/rtwn/rtl8821a/usb/r21au.h +++ head/sys/dev/rtwn/rtl8821a/usb/r21au.h @@ -39,4 +39,10 @@ void r21au_init_tx_agg(struct rtwn_softc *); void r21au_init_burstlen(struct rtwn_softc *); +/* r21au_dfs.c */ +void r21au_chan_check(void *, int); +int r21au_newstate(struct ieee80211vap *, enum ieee80211_state, int); +void r21au_scan_start(struct ieee80211com *); +void r21au_scan_end(struct ieee80211com *); + #endif /* RTL8821AU_H */ Index: head/sys/dev/rtwn/rtl8821a/usb/r21au_attach.c =================================================================== --- head/sys/dev/rtwn/rtl8821a/usb/r21au_attach.c +++ head/sys/dev/rtwn/rtl8821a/usb/r21au_attach.c @@ -76,6 +76,7 @@ static void r21a_postattach(struct rtwn_softc *sc) { + struct ieee80211com *ic = &sc->sc_ic; struct r12a_softc *rs = sc->sc_priv; if (rs->board_type == R92C_BOARD_TYPE_MINICARD || @@ -85,18 +86,52 @@ else sc->sc_set_led = r21a_set_led; - sc->sc_ic.ic_ioctl = r12a_ioctl_net; + TIMEOUT_TASK_INIT(taskqueue_thread, &rs->rs_chan_check, 0, + r21au_chan_check, sc); + + /* RXCKSUM */ + ic->ic_ioctl = r12a_ioctl_net; + /* DFS */ + rs->rs_scan_start = ic->ic_scan_start; + ic->ic_scan_start = r21au_scan_start; + rs->rs_scan_end = ic->ic_scan_end; + ic->ic_scan_end = r21au_scan_end; +} + +static void +r21au_vap_preattach(struct rtwn_softc *sc, struct ieee80211vap *vap) +{ + struct rtwn_vap *rvp = RTWN_VAP(vap); + struct r12a_softc *rs = sc->sc_priv; + + r12a_vap_preattach(sc, vap); + + /* Install DFS newstate handler (non-monitor vaps only). */ + if (rvp->id != RTWN_VAP_ID_INVALID) { + KASSERT(rvp->id >= 0 && rvp->id <= nitems(rs->rs_newstate), + ("%s: wrong vap id %d\n", __func__, rvp->id)); + + rs->rs_newstate[rvp->id] = vap->iv_newstate; + vap->iv_newstate = r21au_newstate; + } } static void r21a_attach_private(struct rtwn_softc *sc) { + struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->sc_dev); + struct sysctl_oid *tree = device_get_sysctl_tree(sc->sc_dev); struct r12a_softc *rs; rs = malloc(sizeof(struct r12a_softc), M_RTWN_PRIV, M_WAITOK | M_ZERO); rs->rs_flags = R12A_RXCKSUM_EN | R12A_RXCKSUM6_EN; + rs->rs_radar = 0; + SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, + "radar_detection", CTLFLAG_RDTUN, &rs->rs_radar, + rs->rs_radar, "Enable radar detection (untested)"); + rs->rs_fix_spur = rtwn_nop_softc_chan; rs->rs_set_band_2ghz = r21a_set_band_2ghz; rs->rs_set_band_5ghz = r21a_set_band_5ghz; @@ -119,7 +154,13 @@ static void r21au_adj_devcaps(struct rtwn_softc *sc) { - /* TODO: DFS, LDPC etc */ + struct ieee80211com *ic = &sc->sc_ic; + struct r12a_softc *rs = sc->sc_priv; + + if (rs->rs_radar != 0) + ic->ic_caps |= IEEE80211_C_DFS; + + /* TODO: LDPC etc */ } void @@ -163,7 +204,7 @@ sc->sc_iq_calib = r12a_iq_calib; sc->sc_read_chipid_vendor = rtwn_nop_softc_uint32; sc->sc_adj_devcaps = r21au_adj_devcaps; - sc->sc_vap_preattach = r12a_vap_preattach; + sc->sc_vap_preattach = r21au_vap_preattach; sc->sc_postattach = r21a_postattach; sc->sc_detach_private = r12a_detach_private; #ifndef RTWN_WITHOUT_UCODE Index: head/sys/dev/rtwn/rtl8821a/usb/r21au_dfs.c =================================================================== --- head/sys/dev/rtwn/rtl8821a/usb/r21au_dfs.c +++ head/sys/dev/rtwn/rtl8821a/usb/r21au_dfs.c @@ -0,0 +1,280 @@ +/*- + * Copyright (c) 2016 Andriy Voskoboinyk + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +__FBSDID("$FreeBSD$"); + +#include "opt_wlan.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#include +#include + +#include +#include + +#include + +#include + +#include +#include + + +#define R21AU_RADAR_CHECK_PERIOD (2 * hz) + +static void +r21au_dfs_radar_disable(struct rtwn_softc *sc) +{ + rtwn_bb_setbits(sc, 0x924, 0x00008000, 0); +} + +static int +r21au_dfs_radar_is_enabled(struct rtwn_softc *sc) +{ + return !!(rtwn_bb_read(sc, 0x924) & 0x00008000); +} + +static int +r21au_dfs_radar_reset(struct rtwn_softc *sc) +{ + int error; + + error = rtwn_bb_setbits(sc, 0x924, 0x00008000, 0); + if (error != 0) + return (error); + + return (rtwn_bb_setbits(sc, 0x924, 0, 0x00008000)); +} + +static int +r21au_dfs_radar_enable(struct rtwn_softc *sc) +{ +#define RTWN_CHK(res) do { \ + if (res != 0) \ + return (EIO); \ +} while(0) + + RTWN_ASSERT_LOCKED(sc); + + RTWN_CHK(rtwn_bb_setbits(sc, 0x814, 0x3fffffff, 0x04cc4d10)); + RTWN_CHK(rtwn_bb_setbits(sc, R12A_BW_INDICATION, 0xff, 0x06)); + RTWN_CHK(rtwn_bb_write(sc, 0x918, 0x1c16ecdf)); + RTWN_CHK(rtwn_bb_write(sc, 0x924, 0x0152a400)); + RTWN_CHK(rtwn_bb_write(sc, 0x91c, 0x0fa21a20)); + RTWN_CHK(rtwn_bb_write(sc, 0x920, 0xe0f57204)); + + return (r21au_dfs_radar_reset(sc)); + +#undef RTWN_CHK +} + +static int +r21au_dfs_radar_is_detected(struct rtwn_softc *sc) +{ + return !!(rtwn_bb_read(sc, 0xf98) & 0x00020000); +} + +void +r21au_chan_check(void *arg, int npending __unused) +{ + struct rtwn_softc *sc = arg; + struct r12a_softc *rs = sc->sc_priv; + struct ieee80211com *ic = &sc->sc_ic; + + RTWN_LOCK(sc); +#ifdef DIAGNOSTIC + RTWN_DPRINTF(sc, RTWN_DEBUG_STATE, + "%s: periodical radar detection task\n", __func__); +#endif + + if (!r21au_dfs_radar_is_enabled(sc)) { + if (rs->rs_flags & R12A_RADAR_ENABLED) { + /* should not happen */ + device_printf(sc->sc_dev, + "%s: radar detection was turned off " + "unexpectedly, resetting...\n", __func__); + + /* XXX something more appropriate? */ + ieee80211_restart_all(ic); + } + RTWN_UNLOCK(sc); + return; + } + + if (r21au_dfs_radar_is_detected(sc)) { + r21au_dfs_radar_reset(sc); + + RTWN_DPRINTF(sc, RTWN_DEBUG_RADAR, "%s: got radar event\n", + __func__); + + RTWN_UNLOCK(sc); + IEEE80211_LOCK(ic); + + ieee80211_dfs_notify_radar(ic, ic->ic_curchan); + + IEEE80211_UNLOCK(ic); + RTWN_LOCK(sc); + } + + if (rs->rs_flags & R12A_RADAR_ENABLED) { + taskqueue_enqueue_timeout(taskqueue_thread, + &rs->rs_chan_check, R21AU_RADAR_CHECK_PERIOD); + } + RTWN_UNLOCK(sc); +} + +int +r21au_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg) +{ + struct ieee80211com *ic = vap->iv_ic; + struct rtwn_softc *sc = ic->ic_softc; + struct rtwn_vap *rvp = RTWN_VAP(vap); + struct r12a_softc *rs = sc->sc_priv; + int error; + + KASSERT(rvp->id == 0 || rvp->id == 1, + ("%s: unexpected vap id %d\n", __func__, rvp->id)); + + IEEE80211_UNLOCK(ic); + RTWN_LOCK(sc); + + error = 0; + if (nstate == IEEE80211_S_CAC && + !(rs->rs_flags & R12A_RADAR_ENABLED)) { + error = r21au_dfs_radar_enable(sc); + if (error != 0) { + device_printf(sc->sc_dev, + "%s: cannot enable radar detection\n", __func__); + goto fail; + } + rs->rs_flags |= R12A_RADAR_ENABLED; + + RTWN_DPRINTF(sc, RTWN_DEBUG_RADAR, + "%s: radar detection was enabled\n", __func__); + + taskqueue_enqueue_timeout(taskqueue_thread, + &rs->rs_chan_check, R21AU_RADAR_CHECK_PERIOD); + } + + if ((nstate < IEEE80211_S_CAC || nstate == IEEE80211_S_CSA) && + (rs->rs_flags & R12A_RADAR_ENABLED) && + (sc->vaps[!rvp->id] == NULL || + sc->vaps[!rvp->id]->vap.iv_state < IEEE80211_S_CAC || + sc->vaps[!rvp->id]->vap.iv_state == IEEE80211_S_CSA)) { + taskqueue_cancel_timeout(taskqueue_thread, &rs->rs_chan_check, + NULL); + + rs->rs_flags &= ~R12A_RADAR_ENABLED; + r21au_dfs_radar_disable(sc); + + RTWN_DPRINTF(sc, RTWN_DEBUG_RADAR, + "%s: radar detection was disabled\n", __func__); + } + +fail: + RTWN_UNLOCK(sc); + IEEE80211_LOCK(ic); + + if (error != 0) + return (error); + + return (rs->rs_newstate[rvp->id](vap, nstate, arg)); +} + +void +r21au_scan_start(struct ieee80211com *ic) +{ + struct rtwn_softc *sc = ic->ic_softc; + struct r12a_softc *rs = sc->sc_priv; + + RTWN_LOCK(sc); + if (rs->rs_flags & R12A_RADAR_ENABLED) { + RTWN_UNLOCK(sc); + while (taskqueue_cancel_timeout(taskqueue_thread, + &rs->rs_chan_check, NULL) != 0) { + taskqueue_drain_timeout(taskqueue_thread, + &rs->rs_chan_check); + } + RTWN_LOCK(sc); + + r21au_dfs_radar_disable(sc); + RTWN_DPRINTF(sc, RTWN_DEBUG_RADAR, + "%s: radar detection was (temporarily) disabled\n", + __func__); + } + RTWN_UNLOCK(sc); + + rs->rs_scan_start(ic); +} + +void +r21au_scan_end(struct ieee80211com *ic) +{ + struct rtwn_softc *sc = ic->ic_softc; + struct r12a_softc *rs = sc->sc_priv; + int error; + + RTWN_LOCK(sc); + if (rs->rs_flags & R12A_RADAR_ENABLED) { + error = r21au_dfs_radar_enable(sc); + if (error != 0) { + device_printf(sc->sc_dev, + "%s: cannot re-enable radar detection\n", + __func__); + + /* XXX */ + ieee80211_restart_all(ic); + RTWN_UNLOCK(sc); + return; + } + RTWN_DPRINTF(sc, RTWN_DEBUG_RADAR, + "%s: radar detection was re-enabled\n", __func__); + + taskqueue_enqueue_timeout(taskqueue_thread, + &rs->rs_chan_check, R21AU_RADAR_CHECK_PERIOD); + } + RTWN_UNLOCK(sc); + + rs->rs_scan_end(ic); +} Index: head/sys/modules/rtwn_usb/Makefile =================================================================== --- head/sys/modules/rtwn_usb/Makefile +++ head/sys/modules/rtwn_usb/Makefile @@ -25,7 +25,7 @@ r12au.h r12au_reg.h r12au_tx_desc.h .PATH: ${.CURDIR}/../../dev/rtwn/rtl8821a/usb -SRCS += r21au_attach.c r21au_init.c \ +SRCS += r21au_attach.c r21au_init.c r21au_dfs.c \ r21au.h r21au_reg.h opt_rtwn.h: