1 /*- 2 * Copyright (c) 2010 Damien Bergamini <damien.bergamini@free.fr> 3 * Copyright (c) 2014 Kevin Lo <kevlo@FreeBSD.org> 4 * Copyright (c) 2015-2016 Andriy Voskoboinyk <avos@FreeBSD.org> 5 * 6 * Permission to use, copy, modify, and distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 */ 18 19 #include <sys/cdefs.h> 20 #include <sys/param.h> 21 #include <sys/lock.h> 22 #include <sys/mutex.h> 23 #include <sys/mbuf.h> 24 #include <sys/kernel.h> 25 #include <sys/socket.h> 26 #include <sys/systm.h> 27 #include <sys/malloc.h> 28 #include <sys/queue.h> 29 #include <sys/taskqueue.h> 30 #include <sys/bus.h> 31 #include <sys/endian.h> 32 33 #include <net/if.h> 34 #include <net/ethernet.h> 35 #include <net/if_media.h> 36 37 #include <net80211/ieee80211_var.h> 38 #include <net80211/ieee80211_radiotap.h> 39 40 #include <dev/rtwn/if_rtwnvar.h> 41 42 #include <dev/rtwn/if_rtwn_beacon.h> 43 #include <dev/rtwn/if_rtwn_debug.h> 44 #include <dev/rtwn/if_rtwn_tx.h> 45 46 #include <dev/rtwn/rtl8192c/r92c_reg.h> 47 48 static void 49 rtwn_reset_beacon_valid(struct rtwn_softc *sc, int id) 50 { 51 52 KASSERT (id == 0 || id == 1, ("wrong port id %d\n", id)); 53 54 /* XXX cannot be cleared on RTL8188CE */ 55 rtwn_setbits_1_shift(sc, sc->bcn_status_reg[id], 56 R92C_TDECTRL_BCN_VALID, 0, 2); 57 58 RTWN_DPRINTF(sc, RTWN_DEBUG_BEACON, 59 "%s: 'beacon valid' bit for vap %d was unset\n", 60 __func__, id); 61 } 62 63 static int 64 rtwn_check_beacon_valid(struct rtwn_softc *sc, int id) 65 { 66 uint16_t reg; 67 int ntries; 68 69 if (id == RTWN_VAP_ID_INVALID) 70 return (0); 71 72 reg = sc->bcn_status_reg[id]; 73 for (ntries = 0; ntries < 10; ntries++) { 74 if (rtwn_read_4(sc, reg) & R92C_TDECTRL_BCN_VALID) { 75 RTWN_DPRINTF(sc, RTWN_DEBUG_BEACON, 76 "%s: beacon for vap %d was recognized\n", 77 __func__, id); 78 break; 79 } 80 rtwn_delay(sc, sc->bcn_check_interval); 81 } 82 if (ntries == 10) 83 return (ETIMEDOUT); 84 85 return (0); 86 } 87 88 void 89 rtwn_switch_bcnq(struct rtwn_softc *sc, int id) 90 { 91 92 if (sc->cur_bcnq_id != id) { 93 /* Wait until any previous transmit completes. */ 94 (void) rtwn_check_beacon_valid(sc, sc->cur_bcnq_id); 95 96 /* Change current port. */ 97 rtwn_beacon_select(sc, id); 98 sc->cur_bcnq_id = id; 99 } 100 101 /* Reset 'beacon valid' bit. */ 102 rtwn_reset_beacon_valid(sc, id); 103 } 104 105 int 106 rtwn_setup_beacon(struct rtwn_softc *sc, struct ieee80211_node *ni) 107 { 108 struct ieee80211vap *vap = ni->ni_vap; 109 struct rtwn_vap *uvp = RTWN_VAP(vap); 110 struct mbuf *m; 111 112 RTWN_ASSERT_LOCKED(sc); 113 114 if (ni->ni_chan == IEEE80211_CHAN_ANYC) 115 return (EINVAL); 116 117 m = ieee80211_beacon_alloc(ni); 118 if (m == NULL) { 119 device_printf(sc->sc_dev, 120 "%s: could not allocate beacon frame\n", __func__); 121 return (ENOMEM); 122 } 123 124 if (uvp->bcn_mbuf != NULL) { 125 rtwn_beacon_unload(sc, uvp->id); 126 m_freem(uvp->bcn_mbuf); 127 } 128 129 uvp->bcn_mbuf = m; 130 131 rtwn_beacon_set_rate(sc, &uvp->bcn_desc.txd[0], 132 IEEE80211_IS_CHAN_5GHZ(ni->ni_chan)); 133 134 return (rtwn_tx_beacon_check(sc, uvp)); 135 } 136 137 /* 138 * Push a beacon frame into the chip. Beacon will 139 * be repeated by the chip every R92C_BCN_INTERVAL. 140 */ 141 static int 142 rtwn_tx_beacon(struct rtwn_softc *sc, struct rtwn_vap *uvp) 143 { 144 int error; 145 146 RTWN_ASSERT_LOCKED(sc); 147 148 RTWN_DPRINTF(sc, RTWN_DEBUG_BEACON, 149 "%s: sending beacon for vap %d\n", __func__, uvp->id); 150 151 error = rtwn_tx_start(sc, NULL, uvp->bcn_mbuf, &uvp->bcn_desc.txd[0], 152 IEEE80211_FC0_TYPE_MGT, uvp->id); 153 154 return (error); 155 } 156 157 void 158 rtwn_update_beacon(struct ieee80211vap *vap, int item) 159 { 160 struct ieee80211com *ic = vap->iv_ic; 161 struct rtwn_softc *sc = ic->ic_softc; 162 struct rtwn_vap *uvp = RTWN_VAP(vap); 163 struct ieee80211_beacon_offsets *bo = &vap->iv_bcn_off; 164 struct ieee80211_node *ni = vap->iv_bss; 165 int mcast = 0; 166 167 RTWN_LOCK(sc); 168 if (uvp->bcn_mbuf == NULL) { 169 uvp->bcn_mbuf = ieee80211_beacon_alloc(ni); 170 if (uvp->bcn_mbuf == NULL) { 171 device_printf(sc->sc_dev, 172 "%s: could not allocate beacon frame\n", __func__); 173 RTWN_UNLOCK(sc); 174 return; 175 } 176 } 177 178 RTWN_DPRINTF(sc, RTWN_DEBUG_BEACON, 179 "%s: vap id %d, iv_csa_count %d, ic_csa_count %d, item %d\n", 180 __func__, uvp->id, vap->iv_csa_count, ic->ic_csa_count, item); 181 182 switch (item) { 183 case IEEE80211_BEACON_CSA: 184 if (vap->iv_csa_count != ic->ic_csa_count) { 185 /* 186 * XXX two APs with different beacon intervals 187 * are not handled properly. 188 */ 189 /* XXX check TBTT? */ 190 taskqueue_enqueue_timeout(taskqueue_thread, 191 &uvp->tx_beacon_csa, 192 msecs_to_ticks(ni->ni_intval)); 193 } 194 break; 195 case IEEE80211_BEACON_TIM: 196 mcast = 1; /* XXX */ 197 break; 198 default: 199 break; 200 } 201 202 setbit(bo->bo_flags, item); 203 204 rtwn_beacon_update_begin(sc, vap); 205 RTWN_UNLOCK(sc); 206 207 ieee80211_beacon_update(ni, uvp->bcn_mbuf, mcast); 208 209 /* XXX clear manually */ 210 clrbit(bo->bo_flags, IEEE80211_BEACON_CSA); 211 212 RTWN_LOCK(sc); 213 rtwn_tx_beacon(sc, uvp); 214 rtwn_beacon_update_end(sc, vap); 215 RTWN_UNLOCK(sc); 216 } 217 218 void 219 rtwn_tx_beacon_csa(void *arg, int npending __unused) 220 { 221 struct ieee80211vap *vap = arg; 222 struct ieee80211com *ic = vap->iv_ic; 223 struct rtwn_softc *sc = ic->ic_softc; 224 struct rtwn_vap *rvp = RTWN_VAP(vap); 225 226 KASSERT (rvp->id == 0 || rvp->id == 1, 227 ("wrong port id %d\n", rvp->id)); 228 229 IEEE80211_LOCK(ic); 230 if (ic->ic_flags & IEEE80211_F_CSAPENDING) { 231 RTWN_DPRINTF(sc, RTWN_DEBUG_BEACON, 232 "%s: vap id %d, iv_csa_count %d, ic_csa_count %d\n", 233 __func__, rvp->id, vap->iv_csa_count, ic->ic_csa_count); 234 235 rtwn_update_beacon(vap, IEEE80211_BEACON_CSA); 236 } 237 IEEE80211_UNLOCK(ic); 238 239 (void) rvp; 240 } 241 242 int 243 rtwn_tx_beacon_check(struct rtwn_softc *sc, struct rtwn_vap *uvp) 244 { 245 int ntries, error; 246 247 for (ntries = 0; ntries < 5; ntries++) { 248 rtwn_reset_beacon_valid(sc, uvp->id); 249 250 error = rtwn_tx_beacon(sc, uvp); 251 if (error != 0) 252 continue; 253 254 error = rtwn_check_beacon_valid(sc, uvp->id); 255 if (error == 0) 256 break; 257 } 258 if (ntries == 5) { 259 device_printf(sc->sc_dev, 260 "%s: cannot push beacon into chip, error %d!\n", 261 __func__, error); 262 return (error); 263 } 264 265 return (0); 266 } 267