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