1 /* $OpenBSD: if_urtwn.c,v 1.16 2011/02/10 17:26:40 jakemsr Exp $ */
4 * Copyright (c) 2010 Damien Bergamini <damien.bergamini@free.fr>
5 * Copyright (c) 2014 Kevin Lo <kevlo@FreeBSD.org>
6 * Copyright (c) 2015-2016 Andriy Voskoboinyk <avos@FreeBSD.org>
8 * Permission to use, copy, modify, and distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 #include <sys/cdefs.h>
22 __FBSDID("$FreeBSD$");
26 #include <sys/param.h>
28 #include <sys/mutex.h>
30 #include <sys/kernel.h>
31 #include <sys/socket.h>
32 #include <sys/systm.h>
33 #include <sys/malloc.h>
34 #include <sys/queue.h>
35 #include <sys/taskqueue.h>
37 #include <sys/endian.h>
40 #include <net/if_var.h>
41 #include <net/ethernet.h>
42 #include <net/if_dl.h>
43 #include <net/if_media.h>
45 #include <net80211/ieee80211_var.h>
46 #include <net80211/ieee80211_radiotap.h>
48 #include <dev/rtwn/if_rtwnreg.h>
49 #include <dev/rtwn/if_rtwnvar.h>
51 #include <dev/rtwn/if_rtwn_debug.h>
52 #include <dev/rtwn/if_rtwn_ridx.h>
53 #include <dev/rtwn/if_rtwn_rx.h>
55 #include <dev/rtwn/rtl8192c/r92c_reg.h>
59 rtwn_get_rates(struct rtwn_softc *sc, const struct ieee80211_rateset *rs,
60 const struct ieee80211_htrateset *rs_ht, uint32_t *rates_p,
61 int *maxrate_p, int basic_rates)
71 /* This is for 11bg */
72 for (i = 0; i < rs->rs_nrates; i++) {
73 /* Convert 802.11 rate to HW rate index. */
74 ridx = rate2ridx(IEEE80211_RV(rs->rs_rates[i]));
75 if (ridx == RTWN_RIDX_UNKNOWN) /* Unknown rate, skip. */
77 if (((rs->rs_rates[i] & IEEE80211_RATE_BASIC) != 0) ||
85 /* If we're doing 11n, enable 11n rates */
86 if (rs_ht != NULL && !basic_rates) {
87 for (i = 0; i < rs_ht->rs_nrates; i++) {
88 if ((rs_ht->rs_rates[i] & 0x7f) > 0xf)
90 /* 11n rates start at index 12 */
91 ridx = RTWN_RIDX_HT_MCS((rs_ht->rs_rates[i]) & 0xf);
94 /* Guard against the rate table being oddly ordered */
100 RTWN_DPRINTF(sc, RTWN_DEBUG_RA,
101 "%s: rates 0x%08X, maxrate %d\n", __func__, rates, maxrate);
105 if (maxrate_p != NULL)
106 *maxrate_p = maxrate;
110 rtwn_set_basicrates(struct rtwn_softc *sc, uint32_t rates)
113 RTWN_DPRINTF(sc, RTWN_DEBUG_RA, "%s: rates 0x%08X\n", __func__, rates);
115 rtwn_setbits_4(sc, R92C_RRSR, R92C_RRSR_RATE_BITMAP_M, rates);
119 rtwn_update_avgrssi(struct rtwn_softc *sc, struct rtwn_node *un, int8_t rssi,
124 /* Convert antenna signal to percentage. */
125 if (rssi <= -100 || rssi >= 20)
132 /* CCK gain is smaller than OFDM/MCS gain. */
146 if (un->avg_pwdb == -1) /* Init. */
148 else if (un->avg_pwdb < pwdb)
149 un->avg_pwdb = ((un->avg_pwdb * 19 + pwdb) / 20) + 1;
151 un->avg_pwdb = ((un->avg_pwdb * 19 + pwdb) / 20);
153 RTWN_DPRINTF(sc, RTWN_DEBUG_RSSI,
154 "MACID %d, PWDB %d, EMA %d\n", un->id, pwdb, un->avg_pwdb);
158 rtwn_get_rssi(struct rtwn_softc *sc, void *physt, int is_cck)
163 rssi = rtwn_get_rssi_cck(sc, physt);
165 rssi = rtwn_get_rssi_ofdm(sc, physt);
171 rtwn_get_tsf_low(struct rtwn_softc *sc, int id)
173 return (rtwn_read_4(sc, R92C_TSFTR(id)));
177 rtwn_get_tsf_high(struct rtwn_softc *sc, int id)
179 return (rtwn_read_4(sc, R92C_TSFTR(id) + 4));
183 rtwn_get_tsf(struct rtwn_softc *sc, uint64_t *buf, int id)
185 /* NB: we cannot read it at once. */
186 *buf = rtwn_get_tsf_high(sc, id);
188 *buf += rtwn_get_tsf_low(sc, id);
192 rtwn_extend_rx_tsf(struct rtwn_softc *sc,
193 const struct rtwn_rx_stat_common *stat)
196 uint32_t rxdw3, tsfl, tsfl_curr;
199 rxdw3 = le32toh(stat->rxdw3);
200 tsfl = le32toh(stat->tsf_low);
201 id = MS(rxdw3, RTWN_RXDW3_BSSID01_FIT);
207 tsfl_curr = rtwn_get_tsf_low(sc, id);
211 uint32_t tsfl0, tsfl1;
213 tsfl0 = rtwn_get_tsf_low(sc, 0);
214 tsfl1 = rtwn_get_tsf_low(sc, 1);
216 if (abs(tsfl0 - tsfl) < abs(tsfl1 - tsfl)) {
227 tsft = rtwn_get_tsf_high(sc, id);
228 if (tsfl > tsfl_curr && tsfl > 0xffff0000)
236 struct ieee80211_node *
237 rtwn_rx_common(struct rtwn_softc *sc, struct mbuf *m, void *desc)
239 struct ieee80211com *ic = &sc->sc_ic;
240 struct ieee80211_node *ni;
241 struct ieee80211_frame_min *wh;
242 struct ieee80211_rx_stats rxs;
243 struct rtwn_node *un;
244 struct rtwn_rx_stat_common *stat;
248 int cipher, infosz, is_cck, pktlen, shift;
251 rxdw0 = le32toh(stat->rxdw0);
253 cipher = MS(rxdw0, RTWN_RXDW0_CIPHER);
254 infosz = MS(rxdw0, RTWN_RXDW0_INFOSZ) * 8;
255 pktlen = MS(rxdw0, RTWN_RXDW0_PKTLEN);
256 shift = MS(rxdw0, RTWN_RXDW0_SHIFT);
258 wh = (struct ieee80211_frame_min *)(mtodo(m, shift + infosz));
259 if ((wh->i_fc[1] & IEEE80211_FC1_PROTECTED) &&
260 cipher != R92C_CAM_ALGO_NONE)
263 if (pktlen >= sizeof(*wh)) {
264 ni = ieee80211_find_rxnode(ic, wh);
265 if (ni != NULL && (ni->ni_flags & IEEE80211_NODE_HT))
266 m->m_flags |= M_AMPDU;
271 if (infosz != 0 && (rxdw0 & RTWN_RXDW0_PHYST))
272 physt = (void *)mtodo(m, shift);
274 physt = (un != NULL) ? &un->last_physt : &sc->last_physt;
276 bzero(&rxs, sizeof(rxs));
277 rtwn_get_rx_stats(sc, &rxs, desc, physt);
278 if (rxs.c_pktflags & IEEE80211_RX_F_AMPDU) {
279 /* Next MPDU will come without PHY info. */
280 memcpy(&sc->last_physt, physt, sizeof(sc->last_physt));
282 memcpy(&un->last_physt, physt, sizeof(sc->last_physt));
285 /* Add some common bits. */
286 /* NB: should not happen. */
287 if (rxdw0 & RTWN_RXDW0_CRCERR)
288 rxs.c_pktflags |= IEEE80211_RX_F_FAIL_FCSCRC;
290 rxs.r_flags |= IEEE80211_R_TSF_START; /* XXX undocumented */
291 rxs.r_flags |= IEEE80211_R_TSF64;
292 rxs.c_rx_tsf = rtwn_extend_rx_tsf(sc, stat);
294 /* Get RSSI from PHY status descriptor. */
295 is_cck = (rxs.c_pktflags & IEEE80211_RX_F_CCK) != 0;
296 rssi = rtwn_get_rssi(sc, physt, is_cck);
298 /* XXX TODO: we really need a rate-to-string method */
299 RTWN_DPRINTF(sc, RTWN_DEBUG_RSSI, "%s: rssi %d, rate %d\n",
300 __func__, rssi, rxs.c_rate);
301 if (un != NULL && infosz != 0 && (rxdw0 & RTWN_RXDW0_PHYST)) {
302 /* Update our average RSSI. */
303 rtwn_update_avgrssi(sc, un, rssi, is_cck);
306 rxs.r_flags |= IEEE80211_R_NF | IEEE80211_R_RSSI;
307 rxs.c_nf = RTWN_NOISE_FLOOR;
308 rxs.c_rssi = rssi - rxs.c_nf;
309 (void) ieee80211_add_rx_params(m, &rxs);
311 if (ieee80211_radiotap_active(ic)) {
312 struct rtwn_rx_radiotap_header *tap = &sc->sc_rxtap;
314 tap->wr_flags = rtwn_rx_radiotap_flags(sc, desc);
315 tap->wr_tsft = htole64(rxs.c_rx_tsf);
316 tap->wr_rate = rxs.c_rate;
317 tap->wr_dbm_antsignal = rssi;
318 tap->wr_dbm_antnoise = rxs.c_nf;
321 /* Drop PHY descriptor. */
322 m_adj(m, infosz + shift);
328 rtwn_adhoc_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m, int subtype,
329 const struct ieee80211_rx_stats *rxs,
332 struct ieee80211vap *vap = ni->ni_vap;
333 struct rtwn_softc *sc = vap->iv_ic->ic_softc;
334 struct rtwn_vap *uvp = RTWN_VAP(vap);
335 uint64_t ni_tstamp, curr_tstamp;
337 uvp->recv_mgmt(ni, m, subtype, rxs, rssi, nf);
339 if (vap->iv_state == IEEE80211_S_RUN &&
340 (subtype == IEEE80211_FC0_SUBTYPE_BEACON ||
341 subtype == IEEE80211_FC0_SUBTYPE_PROBE_RESP)) {
342 ni_tstamp = le64toh(ni->ni_tstamp.tsf);
344 rtwn_get_tsf(sc, &curr_tstamp, uvp->id);
347 if (ni_tstamp >= curr_tstamp)
348 (void) ieee80211_ibss_merge(ni);
353 rtwn_get_multi_pos(const uint8_t maddr[])
355 uint64_t mask = 0x00004d101df481b4;
356 uint8_t pos = 0x27; /* initial value */
359 for (i = 0; i < IEEE80211_ADDR_LEN; i++)
360 for (j = (i == 0) ? 1 : 0; j < 8; j++)
361 if ((maddr[i] >> j) & 1)
362 pos ^= (mask >> (i * 8 + j - 1));
370 rtwn_set_multi(struct rtwn_softc *sc)
372 struct ieee80211com *ic = &sc->sc_ic;
375 RTWN_ASSERT_LOCKED(sc);
377 /* general structure was copied from ath(4). */
378 if (ic->ic_allmulti == 0) {
379 struct ieee80211vap *vap;
381 struct ifmultiaddr *ifma;
384 * Merge multicast addresses to form the hardware filter.
386 mfilt[0] = mfilt[1] = 0;
387 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
390 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
394 dl = LLADDR((struct sockaddr_dl *)
396 pos = rtwn_get_multi_pos(dl);
398 mfilt[pos / 32] |= (1 << (pos % 32));
400 if_maddr_runlock(ifp);
403 mfilt[0] = mfilt[1] = ~0;
406 rtwn_write_4(sc, R92C_MAR + 0, mfilt[0]);
407 rtwn_write_4(sc, R92C_MAR + 4, mfilt[1]);
409 RTWN_DPRINTF(sc, RTWN_DEBUG_STATE, "%s: MC filter %08x:%08x\n",
410 __func__, mfilt[0], mfilt[1]);
414 rtwn_rxfilter_update_mgt(struct rtwn_softc *sc)
419 if (sc->bcn_vaps == 0) { /* STA and/or MONITOR mode vaps */
421 R92C_RXFLTMAP_SUBTYPE(IEEE80211_FC0_SUBTYPE_ASSOC_REQ) |
422 R92C_RXFLTMAP_SUBTYPE(IEEE80211_FC0_SUBTYPE_REASSOC_REQ) |
423 R92C_RXFLTMAP_SUBTYPE(IEEE80211_FC0_SUBTYPE_PROBE_REQ));
425 if (sc->ap_vaps == sc->nvaps - sc->mon_vaps) { /* AP vaps only */
427 R92C_RXFLTMAP_SUBTYPE(IEEE80211_FC0_SUBTYPE_ASSOC_RESP) |
428 R92C_RXFLTMAP_SUBTYPE(IEEE80211_FC0_SUBTYPE_REASSOC_RESP));
430 rtwn_write_2(sc, R92C_RXFLTMAP0, filter);
434 rtwn_rxfilter_update(struct rtwn_softc *sc)
437 RTWN_ASSERT_LOCKED(sc);
439 /* Filter for management frames. */
440 rtwn_rxfilter_update_mgt(sc);
442 /* Update Rx filter. */
443 rtwn_set_promisc(sc);
447 rtwn_rxfilter_init(struct rtwn_softc *sc)
450 RTWN_ASSERT_LOCKED(sc);
452 /* Setup multicast filter. */
455 /* Reject all control frames. */
456 rtwn_write_2(sc, R92C_RXFLTMAP1, 0x0000);
458 /* Reject all data frames. */
459 rtwn_write_2(sc, R92C_RXFLTMAP2, 0x0000);
461 /* Append generic Rx filter bits. */
462 sc->rcr |= R92C_RCR_AM | R92C_RCR_AB | R92C_RCR_APM |
463 R92C_RCR_HTC_LOC_CTRL | R92C_RCR_APP_PHYSTS |
464 R92C_RCR_APP_ICV | R92C_RCR_APP_MIC;
466 /* Update dynamic Rx filter parts. */
467 rtwn_rxfilter_update(sc);
471 rtwn_rxfilter_set(struct rtwn_softc *sc)
473 if (!(sc->sc_flags & RTWN_RCR_LOCKED))
474 rtwn_write_4(sc, R92C_RCR, sc->rcr);
478 rtwn_set_rx_bssid_all(struct rtwn_softc *sc, int enable)
482 sc->rcr &= ~R92C_RCR_CBSSID_BCN;
484 sc->rcr |= R92C_RCR_CBSSID_BCN;
485 rtwn_rxfilter_set(sc);
489 rtwn_set_promisc(struct rtwn_softc *sc)
491 struct ieee80211com *ic = &sc->sc_ic;
492 uint32_t mask_all, mask_min;
494 RTWN_ASSERT_LOCKED(sc);
496 mask_all = R92C_RCR_ACF | R92C_RCR_ADF | R92C_RCR_AMF | R92C_RCR_AAP;
497 mask_min = R92C_RCR_APM;
499 if (sc->bcn_vaps == 0)
500 mask_min |= R92C_RCR_CBSSID_BCN;
501 if (sc->ap_vaps == 0)
502 mask_min |= R92C_RCR_CBSSID_DATA;
504 if (ic->ic_promisc == 0 && sc->mon_vaps == 0) {
505 if (sc->bcn_vaps != 0)
506 mask_all |= R92C_RCR_CBSSID_BCN;
507 if (sc->ap_vaps != 0) /* for Null data frames */
508 mask_all |= R92C_RCR_CBSSID_DATA;
510 sc->rcr &= ~mask_all;
513 sc->rcr &= ~mask_min;
516 rtwn_rxfilter_set(sc);