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 __FBSDID("$FreeBSD$");
21
22 #include <sys/param.h>
23 #include <sys/lock.h>
24 #include <sys/mutex.h>
25 #include <sys/mbuf.h>
26 #include <sys/kernel.h>
27 #include <sys/socket.h>
28 #include <sys/systm.h>
29 #include <sys/malloc.h>
30 #include <sys/queue.h>
31 #include <sys/taskqueue.h>
32 #include <sys/bus.h>
33 #include <sys/endian.h>
34
35 #include <net/if.h>
36 #include <net/ethernet.h>
37 #include <net/if_media.h>
38
39 #include <net80211/ieee80211_var.h>
40 #include <net80211/ieee80211_radiotap.h>
41
42 #include <dev/rtwn/if_rtwnvar.h>
43
44 #include <dev/rtwn/if_rtwn_beacon.h>
45 #include <dev/rtwn/if_rtwn_debug.h>
46 #include <dev/rtwn/if_rtwn_tx.h>
47
48 #include <dev/rtwn/rtl8192c/r92c_reg.h>
49
50 static void
51 rtwn_reset_beacon_valid(struct rtwn_softc *sc, int id)
52 {
53
54 KASSERT (id == 0 || id == 1, ("wrong port id %d\n", id));
55
56 /* XXX cannot be cleared on RTL8188CE */
57 rtwn_setbits_1_shift(sc, sc->bcn_status_reg[id],
58 R92C_TDECTRL_BCN_VALID, 0, 2);
59
60 RTWN_DPRINTF(sc, RTWN_DEBUG_BEACON,
61 "%s: 'beacon valid' bit for vap %d was unset\n",
62 __func__, id);
63 }
64
65 static int
66 rtwn_check_beacon_valid(struct rtwn_softc *sc, int id)
67 {
68 uint16_t reg;
69 int ntries;
70
71 if (id == RTWN_VAP_ID_INVALID)
72 return (0);
73
74 reg = sc->bcn_status_reg[id];
75 for (ntries = 0; ntries < 10; ntries++) {
76 if (rtwn_read_4(sc, reg) & R92C_TDECTRL_BCN_VALID) {
77 RTWN_DPRINTF(sc, RTWN_DEBUG_BEACON,
78 "%s: beacon for vap %d was recognized\n",
79 __func__, id);
80 break;
81 }
82 rtwn_delay(sc, sc->bcn_check_interval);
83 }
84 if (ntries == 10)
85 return (ETIMEDOUT);
86
87 return (0);
88 }
89
90 void
91 rtwn_switch_bcnq(struct rtwn_softc *sc, int id)
92 {
93
94 if (sc->cur_bcnq_id != id) {
95 /* Wait until any previous transmit completes. */
96 (void) rtwn_check_beacon_valid(sc, sc->cur_bcnq_id);
97
98 /* Change current port. */
99 rtwn_beacon_select(sc, id);
100 sc->cur_bcnq_id = id;
101 }
102
103 /* Reset 'beacon valid' bit. */
104 rtwn_reset_beacon_valid(sc, id);
105 }
106
107 int
108 rtwn_setup_beacon(struct rtwn_softc *sc, struct ieee80211_node *ni)
109 {
110 struct ieee80211vap *vap = ni->ni_vap;
111 struct rtwn_vap *uvp = RTWN_VAP(vap);
112 struct mbuf *m;
113
114 RTWN_ASSERT_LOCKED(sc);
115
116 if (ni->ni_chan == IEEE80211_CHAN_ANYC)
117 return (EINVAL);
118
119 m = ieee80211_beacon_alloc(ni);
120 if (m == NULL) {
121 device_printf(sc->sc_dev,
122 "%s: could not allocate beacon frame\n", __func__);
123 return (ENOMEM);
124 }
125
126 if (uvp->bcn_mbuf != NULL) {
127 rtwn_beacon_unload(sc, uvp->id);
128 m_freem(uvp->bcn_mbuf);
129 }
130
131 uvp->bcn_mbuf = m;
132
133 rtwn_beacon_set_rate(sc, &uvp->bcn_desc.txd[0],
134 IEEE80211_IS_CHAN_5GHZ(ni->ni_chan));
135
136 return (rtwn_tx_beacon_check(sc, uvp));
137 }
138
139 /*
140 * Push a beacon frame into the chip. Beacon will
141 * be repeated by the chip every R92C_BCN_INTERVAL.
142 */
143 static int
144 rtwn_tx_beacon(struct rtwn_softc *sc, struct rtwn_vap *uvp)
145 {
146 int error;
147
148 RTWN_ASSERT_LOCKED(sc);
149
150 RTWN_DPRINTF(sc, RTWN_DEBUG_BEACON,
151 "%s: sending beacon for vap %d\n", __func__, uvp->id);
152
153 error = rtwn_tx_start(sc, NULL, uvp->bcn_mbuf, &uvp->bcn_desc.txd[0],
154 IEEE80211_FC0_TYPE_MGT, uvp->id);
155
156 return (error);
157 }
158
159 void
160 rtwn_update_beacon(struct ieee80211vap *vap, int item)
161 {
162 struct ieee80211com *ic = vap->iv_ic;
163 struct rtwn_softc *sc = ic->ic_softc;
164 struct rtwn_vap *uvp = RTWN_VAP(vap);
165 struct ieee80211_beacon_offsets *bo = &vap->iv_bcn_off;
166 struct ieee80211_node *ni = vap->iv_bss;
167 int mcast = 0;
168
169 RTWN_LOCK(sc);
170 if (uvp->bcn_mbuf == NULL) {
171 uvp->bcn_mbuf = ieee80211_beacon_alloc(ni);
172 if (uvp->bcn_mbuf == NULL) {
173 device_printf(sc->sc_dev,
174 "%s: could not allocate beacon frame\n", __func__);
175 RTWN_UNLOCK(sc);
176 return;
177 }
178 }
179
180 RTWN_DPRINTF(sc, RTWN_DEBUG_BEACON,
181 "%s: vap id %d, iv_csa_count %d, ic_csa_count %d, item %d\n",
182 __func__, uvp->id, vap->iv_csa_count, ic->ic_csa_count, item);
183
184 switch (item) {
185 case IEEE80211_BEACON_CSA:
186 if (vap->iv_csa_count != ic->ic_csa_count) {
187 /*
188 * XXX two APs with different beacon intervals
189 * are not handled properly.
190 */
191 /* XXX check TBTT? */
192 taskqueue_enqueue_timeout(taskqueue_thread,
193 &uvp->tx_beacon_csa,
194 msecs_to_ticks(ni->ni_intval));
195 }
196 break;
197 case IEEE80211_BEACON_TIM:
198 mcast = 1; /* XXX */
199 break;
200 default:
201 break;
202 }
203
204 setbit(bo->bo_flags, item);
205
206 rtwn_beacon_update_begin(sc, vap);
207 RTWN_UNLOCK(sc);
208
209 ieee80211_beacon_update(ni, uvp->bcn_mbuf, mcast);
210
211 /* XXX clear manually */
212 clrbit(bo->bo_flags, IEEE80211_BEACON_CSA);
213
214 RTWN_LOCK(sc);
215 rtwn_tx_beacon(sc, uvp);
216 rtwn_beacon_update_end(sc, vap);
217 RTWN_UNLOCK(sc);
218 }
219
220 void
221 rtwn_tx_beacon_csa(void *arg, int npending __unused)
222 {
223 struct ieee80211vap *vap = arg;
224 struct ieee80211com *ic = vap->iv_ic;
225 struct rtwn_softc *sc = ic->ic_softc;
226 struct rtwn_vap *rvp = RTWN_VAP(vap);
227
228 KASSERT (rvp->id == 0 || rvp->id == 1,
229 ("wrong port id %d\n", rvp->id));
230
231 IEEE80211_LOCK(ic);
232 if (ic->ic_flags & IEEE80211_F_CSAPENDING) {
233 RTWN_DPRINTF(sc, RTWN_DEBUG_BEACON,
234 "%s: vap id %d, iv_csa_count %d, ic_csa_count %d\n",
235 __func__, rvp->id, vap->iv_csa_count, ic->ic_csa_count);
236
237 rtwn_update_beacon(vap, IEEE80211_BEACON_CSA);
238 }
239 IEEE80211_UNLOCK(ic);
240
241 (void) rvp;
242 }
243
244 int
245 rtwn_tx_beacon_check(struct rtwn_softc *sc, struct rtwn_vap *uvp)
246 {
247 int ntries, error;
248
249 for (ntries = 0; ntries < 5; ntries++) {
250 rtwn_reset_beacon_valid(sc, uvp->id);
251
252 error = rtwn_tx_beacon(sc, uvp);
253 if (error != 0)
254 continue;
255
256 error = rtwn_check_beacon_valid(sc, uvp->id);
257 if (error == 0)
258 break;
259 }
260 if (ntries == 5) {
261 device_printf(sc->sc_dev,
262 "%s: cannot push beacon into chip, error %d!\n",
263 __func__, error);
264 return (error);
265 }
266
267 return (0);
268 }
Cache object: f6067f1fb59568f2addfdad1d3b39c26
|