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