xref: /freebsd/sys/dev/rtwn/rtl8188e/r88e_rx.c (revision 9a6695532b3997e4e2bc3fe57481cc49be5e9e93)
17453645fSAndriy Voskoboinyk /*	$OpenBSD: if_urtwn.c,v 1.16 2011/02/10 17:26:40 jakemsr Exp $	*/
27453645fSAndriy Voskoboinyk 
37453645fSAndriy Voskoboinyk /*-
47453645fSAndriy Voskoboinyk  * Copyright (c) 2010 Damien Bergamini <damien.bergamini@free.fr>
57453645fSAndriy Voskoboinyk  * Copyright (c) 2014 Kevin Lo <kevlo@FreeBSD.org>
67453645fSAndriy Voskoboinyk  * Copyright (c) 2015-2016 Andriy Voskoboinyk <avos@FreeBSD.org>
77453645fSAndriy Voskoboinyk  *
87453645fSAndriy Voskoboinyk  * Permission to use, copy, modify, and distribute this software for any
97453645fSAndriy Voskoboinyk  * purpose with or without fee is hereby granted, provided that the above
107453645fSAndriy Voskoboinyk  * copyright notice and this permission notice appear in all copies.
117453645fSAndriy Voskoboinyk  *
127453645fSAndriy Voskoboinyk  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
137453645fSAndriy Voskoboinyk  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
147453645fSAndriy Voskoboinyk  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
157453645fSAndriy Voskoboinyk  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
167453645fSAndriy Voskoboinyk  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
177453645fSAndriy Voskoboinyk  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
187453645fSAndriy Voskoboinyk  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
197453645fSAndriy Voskoboinyk  */
207453645fSAndriy Voskoboinyk 
217453645fSAndriy Voskoboinyk #include <sys/cdefs.h>
227453645fSAndriy Voskoboinyk __FBSDID("$FreeBSD$");
237453645fSAndriy Voskoboinyk 
247453645fSAndriy Voskoboinyk #include "opt_wlan.h"
257453645fSAndriy Voskoboinyk 
267453645fSAndriy Voskoboinyk #include <sys/param.h>
277453645fSAndriy Voskoboinyk #include <sys/lock.h>
287453645fSAndriy Voskoboinyk #include <sys/mutex.h>
297453645fSAndriy Voskoboinyk #include <sys/mbuf.h>
307453645fSAndriy Voskoboinyk #include <sys/kernel.h>
317453645fSAndriy Voskoboinyk #include <sys/socket.h>
327453645fSAndriy Voskoboinyk #include <sys/systm.h>
337453645fSAndriy Voskoboinyk #include <sys/malloc.h>
347453645fSAndriy Voskoboinyk #include <sys/queue.h>
357453645fSAndriy Voskoboinyk #include <sys/taskqueue.h>
367453645fSAndriy Voskoboinyk #include <sys/bus.h>
377453645fSAndriy Voskoboinyk #include <sys/endian.h>
387453645fSAndriy Voskoboinyk #include <sys/linker.h>
397453645fSAndriy Voskoboinyk 
407453645fSAndriy Voskoboinyk #include <net/if.h>
417453645fSAndriy Voskoboinyk #include <net/ethernet.h>
427453645fSAndriy Voskoboinyk #include <net/if_media.h>
437453645fSAndriy Voskoboinyk 
447453645fSAndriy Voskoboinyk #include <net80211/ieee80211_var.h>
457453645fSAndriy Voskoboinyk #include <net80211/ieee80211_radiotap.h>
467453645fSAndriy Voskoboinyk #include <net80211/ieee80211_ratectl.h>
477453645fSAndriy Voskoboinyk 
487453645fSAndriy Voskoboinyk #include <dev/rtwn/if_rtwnreg.h>
497453645fSAndriy Voskoboinyk #include <dev/rtwn/if_rtwnvar.h>
507453645fSAndriy Voskoboinyk 
517453645fSAndriy Voskoboinyk #include <dev/rtwn/if_rtwn_debug.h>
527453645fSAndriy Voskoboinyk #include <dev/rtwn/if_rtwn_ridx.h>
537453645fSAndriy Voskoboinyk 
5409606165SAndriy Voskoboinyk #include <dev/rtwn/rtl8192c/r92c.h>
557453645fSAndriy Voskoboinyk #include <dev/rtwn/rtl8188e/r88e.h>
567453645fSAndriy Voskoboinyk #include <dev/rtwn/rtl8188e/r88e_rx_desc.h>
577453645fSAndriy Voskoboinyk 
5817d5fbf2SAndriy Voskoboinyk int
5917d5fbf2SAndriy Voskoboinyk r88e_classify_intr(struct rtwn_softc *sc, void *buf, int len)
6017d5fbf2SAndriy Voskoboinyk {
6117d5fbf2SAndriy Voskoboinyk 	struct r92c_rx_stat *stat = buf;
6217d5fbf2SAndriy Voskoboinyk 	int report_sel = MS(le32toh(stat->rxdw3), R88E_RXDW3_RPT);
6317d5fbf2SAndriy Voskoboinyk 
6417d5fbf2SAndriy Voskoboinyk 	switch (report_sel) {
6517d5fbf2SAndriy Voskoboinyk 	case R88E_RXDW3_RPT_RX:
6617d5fbf2SAndriy Voskoboinyk 		return (RTWN_RX_DATA);
6717d5fbf2SAndriy Voskoboinyk 	case R88E_RXDW3_RPT_TX1:	/* per-packet Tx report */
6817d5fbf2SAndriy Voskoboinyk 	case R88E_RXDW3_RPT_TX2:	/* periodical Tx report */
6917d5fbf2SAndriy Voskoboinyk 		return (RTWN_RX_TX_REPORT);
7017d5fbf2SAndriy Voskoboinyk 	case R88E_RXDW3_RPT_HIS:
7117d5fbf2SAndriy Voskoboinyk 		return (RTWN_RX_OTHER);
7217d5fbf2SAndriy Voskoboinyk 	default:			/* shut up the compiler */
7317d5fbf2SAndriy Voskoboinyk 		return (RTWN_RX_DATA);
7417d5fbf2SAndriy Voskoboinyk 	}
7517d5fbf2SAndriy Voskoboinyk }
7617d5fbf2SAndriy Voskoboinyk 
777453645fSAndriy Voskoboinyk void
787453645fSAndriy Voskoboinyk r88e_ratectl_tx_complete(struct rtwn_softc *sc, uint8_t *buf, int len)
797453645fSAndriy Voskoboinyk {
807453645fSAndriy Voskoboinyk #if __FreeBSD_version >= 1200012
817453645fSAndriy Voskoboinyk 	struct ieee80211_ratectl_tx_status txs;
827453645fSAndriy Voskoboinyk #endif
837453645fSAndriy Voskoboinyk 	struct r88e_tx_rpt_ccx *rpt;
847453645fSAndriy Voskoboinyk 	struct ieee80211_node *ni;
857453645fSAndriy Voskoboinyk 	uint8_t macid;
867453645fSAndriy Voskoboinyk 	int ntries;
877453645fSAndriy Voskoboinyk 
887453645fSAndriy Voskoboinyk 	/* Skip Rx descriptor. */
897453645fSAndriy Voskoboinyk 	buf += sizeof(struct r92c_rx_stat);
907453645fSAndriy Voskoboinyk 	len -= sizeof(struct r92c_rx_stat);
917453645fSAndriy Voskoboinyk 
927453645fSAndriy Voskoboinyk 	rpt = (struct r88e_tx_rpt_ccx *)buf;
937453645fSAndriy Voskoboinyk 	if (len != sizeof(*rpt)) {
947453645fSAndriy Voskoboinyk 		RTWN_DPRINTF(sc, RTWN_DEBUG_INTR,
957453645fSAndriy Voskoboinyk 		    "%s: wrong report size (%d, must be %zu)\n",
967453645fSAndriy Voskoboinyk 		    __func__, len, sizeof(*rpt));
977453645fSAndriy Voskoboinyk 		return;
987453645fSAndriy Voskoboinyk 	}
997453645fSAndriy Voskoboinyk 
1007453645fSAndriy Voskoboinyk 	RTWN_DPRINTF(sc, RTWN_DEBUG_INTR,
1017453645fSAndriy Voskoboinyk 	    "%s: ccx report dump: 0: %02X, 1: %02X, 2: %02X, queue time: "
1027453645fSAndriy Voskoboinyk 	    "low %02X, high %02X, final ridx: %02X, 6: %02X, 7: %02X\n",
1037453645fSAndriy Voskoboinyk 	    __func__, rpt->rptb0, rpt->rptb1, rpt->rptb2, rpt->queue_time_low,
1047453645fSAndriy Voskoboinyk 	    rpt->queue_time_high, rpt->final_rate, rpt->rptb6, rpt->rptb7);
1057453645fSAndriy Voskoboinyk 
1067453645fSAndriy Voskoboinyk 	macid = MS(rpt->rptb1, R88E_RPTB1_MACID);
1077453645fSAndriy Voskoboinyk 	if (macid > sc->macid_limit) {
1087453645fSAndriy Voskoboinyk 		device_printf(sc->sc_dev,
1097453645fSAndriy Voskoboinyk 		    "macid %u is too big; increase MACID_MAX limit\n",
1107453645fSAndriy Voskoboinyk 		    macid);
1117453645fSAndriy Voskoboinyk 		return;
1127453645fSAndriy Voskoboinyk 	}
1137453645fSAndriy Voskoboinyk 
1147453645fSAndriy Voskoboinyk 	ntries = MS(rpt->rptb2, R88E_RPTB2_RETRY_CNT);
1157453645fSAndriy Voskoboinyk 
1167453645fSAndriy Voskoboinyk 	ni = sc->node_list[macid];
1177453645fSAndriy Voskoboinyk 	if (ni != NULL) {
1187453645fSAndriy Voskoboinyk 		RTWN_DPRINTF(sc, RTWN_DEBUG_INTR, "%s: frame for macid %u was"
1197453645fSAndriy Voskoboinyk 		    "%s sent (%d retries)\n", __func__, macid,
1207453645fSAndriy Voskoboinyk 		    (rpt->rptb1 & R88E_RPTB1_PKT_OK) ? "" : " not",
1217453645fSAndriy Voskoboinyk 		    ntries);
1227453645fSAndriy Voskoboinyk 
1237453645fSAndriy Voskoboinyk #if __FreeBSD_version >= 1200012
1247453645fSAndriy Voskoboinyk 		txs.flags = IEEE80211_RATECTL_STATUS_LONG_RETRY |
1257453645fSAndriy Voskoboinyk 			    IEEE80211_RATECTL_STATUS_FINAL_RATE;
1267453645fSAndriy Voskoboinyk 		txs.long_retries = ntries;
1277453645fSAndriy Voskoboinyk 		if (rpt->final_rate > RTWN_RIDX_OFDM54) {	/* MCS */
1287453645fSAndriy Voskoboinyk 			txs.final_rate =
1295f792f74SAndriy Voskoboinyk 			    rpt->final_rate - RTWN_RIDX_HT_MCS_SHIFT;
1305f792f74SAndriy Voskoboinyk 			txs.final_rate |= IEEE80211_RATE_MCS;
1317453645fSAndriy Voskoboinyk 		} else
1327453645fSAndriy Voskoboinyk 			txs.final_rate = ridx2rate[rpt->final_rate];
1337453645fSAndriy Voskoboinyk 		if (rpt->rptb1 & R88E_RPTB1_PKT_OK)
1347453645fSAndriy Voskoboinyk 			txs.status = IEEE80211_RATECTL_TX_SUCCESS;
1357453645fSAndriy Voskoboinyk 		else if (rpt->rptb2 & R88E_RPTB2_RETRY_OVER)
1367453645fSAndriy Voskoboinyk 			txs.status = IEEE80211_RATECTL_TX_FAIL_LONG;
1377453645fSAndriy Voskoboinyk 		else if (rpt->rptb2 & R88E_RPTB2_LIFE_EXPIRE)
1387453645fSAndriy Voskoboinyk 			txs.status = IEEE80211_RATECTL_TX_FAIL_EXPIRED;
1397453645fSAndriy Voskoboinyk 		else
1407453645fSAndriy Voskoboinyk 			txs.status = IEEE80211_RATECTL_TX_FAIL_UNSPECIFIED;
1417453645fSAndriy Voskoboinyk 		ieee80211_ratectl_tx_complete(ni, &txs);
1427453645fSAndriy Voskoboinyk #else
1437453645fSAndriy Voskoboinyk 		struct ieee80211vap *vap = ni->ni_vap;
1447453645fSAndriy Voskoboinyk 		if (rpt->rptb1 & R88E_RPTB1_PKT_OK) {
1457453645fSAndriy Voskoboinyk 			ieee80211_ratectl_tx_complete(vap, ni,
1467453645fSAndriy Voskoboinyk 			    IEEE80211_RATECTL_TX_SUCCESS, &ntries, NULL);
1477453645fSAndriy Voskoboinyk 		} else {
1487453645fSAndriy Voskoboinyk 			ieee80211_ratectl_tx_complete(vap, ni,
1497453645fSAndriy Voskoboinyk 			    IEEE80211_RATECTL_TX_FAILURE, &ntries, NULL);
1507453645fSAndriy Voskoboinyk 		}
1517453645fSAndriy Voskoboinyk #endif
1527453645fSAndriy Voskoboinyk 	} else {
1537453645fSAndriy Voskoboinyk 		RTWN_DPRINTF(sc, RTWN_DEBUG_INTR, "%s: macid %u, ni is NULL\n",
1547453645fSAndriy Voskoboinyk 		    __func__, macid);
1557453645fSAndriy Voskoboinyk 	}
1567453645fSAndriy Voskoboinyk }
1577453645fSAndriy Voskoboinyk 
1587453645fSAndriy Voskoboinyk void
1597453645fSAndriy Voskoboinyk r88e_handle_c2h_report(struct rtwn_softc *sc, uint8_t *buf, int len)
1607453645fSAndriy Voskoboinyk {
1617453645fSAndriy Voskoboinyk 
1627453645fSAndriy Voskoboinyk 	/* Skip Rx descriptor. */
1637453645fSAndriy Voskoboinyk 	buf += sizeof(struct r92c_rx_stat);
1647453645fSAndriy Voskoboinyk 	len -= sizeof(struct r92c_rx_stat);
1657453645fSAndriy Voskoboinyk 
1667453645fSAndriy Voskoboinyk 	if (len != R88E_INTR_MSG_LEN) {
1677453645fSAndriy Voskoboinyk 		RTWN_DPRINTF(sc, RTWN_DEBUG_INTR,
1687453645fSAndriy Voskoboinyk 		    "%s: wrong interrupt message size (%d, must be %d)\n",
1697453645fSAndriy Voskoboinyk 		    __func__, len, R88E_INTR_MSG_LEN);
1707453645fSAndriy Voskoboinyk 		return;
1717453645fSAndriy Voskoboinyk 	}
1727453645fSAndriy Voskoboinyk 
1737453645fSAndriy Voskoboinyk 	/* XXX TODO */
1747453645fSAndriy Voskoboinyk }
1757453645fSAndriy Voskoboinyk 
1767453645fSAndriy Voskoboinyk int8_t
1777453645fSAndriy Voskoboinyk r88e_get_rssi_cck(struct rtwn_softc *sc, void *physt)
1787453645fSAndriy Voskoboinyk {
1797453645fSAndriy Voskoboinyk 	struct r88e_rx_phystat *phy = (struct r88e_rx_phystat *)physt;
1807453645fSAndriy Voskoboinyk 	int8_t lna_idx, vga_idx, rssi;
1817453645fSAndriy Voskoboinyk 
1827453645fSAndriy Voskoboinyk 	lna_idx = (phy->agc_rpt & 0xe0) >> 5;
1837453645fSAndriy Voskoboinyk 	vga_idx = (phy->agc_rpt & 0x1f);
1847453645fSAndriy Voskoboinyk 	rssi = 6 - 2 * vga_idx;
1857453645fSAndriy Voskoboinyk 
1867453645fSAndriy Voskoboinyk 	switch (lna_idx) {
1877453645fSAndriy Voskoboinyk 	case 7:
1887453645fSAndriy Voskoboinyk 		if (vga_idx > 27)
1897453645fSAndriy Voskoboinyk 			rssi = -100 + 6;
1907453645fSAndriy Voskoboinyk 		else
1917453645fSAndriy Voskoboinyk 			rssi += -100 + 2 * 27;
1927453645fSAndriy Voskoboinyk 		break;
1937453645fSAndriy Voskoboinyk 	case 6:
1947453645fSAndriy Voskoboinyk 		rssi += -48 + 2 * 2;
1957453645fSAndriy Voskoboinyk 		break;
1967453645fSAndriy Voskoboinyk 	case 5:
1977453645fSAndriy Voskoboinyk 		rssi += -42 + 2 * 7;
1987453645fSAndriy Voskoboinyk 		break;
1997453645fSAndriy Voskoboinyk 	case 4:
2007453645fSAndriy Voskoboinyk 		rssi += -36 + 2 * 7;
2017453645fSAndriy Voskoboinyk 		break;
2027453645fSAndriy Voskoboinyk 	case 3:
2037453645fSAndriy Voskoboinyk 		rssi += -24 + 2 * 7;
2047453645fSAndriy Voskoboinyk 		break;
2057453645fSAndriy Voskoboinyk 	case 2:
2067453645fSAndriy Voskoboinyk 		rssi += -6 + 2 * 5;
2077453645fSAndriy Voskoboinyk 		if (sc->sc_flags & RTWN_FLAG_CCK_HIPWR)
2087453645fSAndriy Voskoboinyk 			rssi -= 6;
2097453645fSAndriy Voskoboinyk 		break;
2107453645fSAndriy Voskoboinyk 	case 1:
2117453645fSAndriy Voskoboinyk 		rssi += 8;
2127453645fSAndriy Voskoboinyk 		break;
2137453645fSAndriy Voskoboinyk 	case 0:
2147453645fSAndriy Voskoboinyk 		rssi += 14;
2157453645fSAndriy Voskoboinyk 		break;
2167453645fSAndriy Voskoboinyk 	}
2177453645fSAndriy Voskoboinyk 
2187453645fSAndriy Voskoboinyk 	return (rssi);
2197453645fSAndriy Voskoboinyk }
2207453645fSAndriy Voskoboinyk 
2217453645fSAndriy Voskoboinyk int8_t
2227453645fSAndriy Voskoboinyk r88e_get_rssi_ofdm(struct rtwn_softc *sc, void *physt)
2237453645fSAndriy Voskoboinyk {
2247453645fSAndriy Voskoboinyk 	struct r88e_rx_phystat *phy = (struct r88e_rx_phystat *)physt;
2257453645fSAndriy Voskoboinyk 	int rssi;
2267453645fSAndriy Voskoboinyk 
2277453645fSAndriy Voskoboinyk 	/* Get average RSSI. */
2287453645fSAndriy Voskoboinyk 	rssi = ((phy->sig_qual >> 1) & 0x7f) - 110;
2297453645fSAndriy Voskoboinyk 
2307453645fSAndriy Voskoboinyk 	return (rssi);
2317453645fSAndriy Voskoboinyk }
23209606165SAndriy Voskoboinyk 
23309606165SAndriy Voskoboinyk void
23409606165SAndriy Voskoboinyk r88e_get_rx_stats(struct rtwn_softc *sc, struct ieee80211_rx_stats *rxs,
23509606165SAndriy Voskoboinyk     const void *desc, const void *physt_ptr)
23609606165SAndriy Voskoboinyk {
23709606165SAndriy Voskoboinyk 	const struct r88e_rx_phystat *physt = physt_ptr;
23809606165SAndriy Voskoboinyk 
23909606165SAndriy Voskoboinyk 	r92c_get_rx_stats(sc, rxs, desc, physt_ptr);
24009606165SAndriy Voskoboinyk 
24109606165SAndriy Voskoboinyk 	if (!sc->sc_ht40) {	/* XXX center channel */
24209606165SAndriy Voskoboinyk 		rxs->r_flags |= IEEE80211_R_IEEE | IEEE80211_R_FREQ;
243*9a669553SBjoern A. Zeeb 		rxs->r_flags |= IEEE80211_R_BAND;
244ff97f477SAndriy Voskoboinyk 		rxs->c_ieee = physt->chan;
24509606165SAndriy Voskoboinyk 		rxs->c_freq = ieee80211_ieee2mhz(rxs->c_ieee,
24609606165SAndriy Voskoboinyk 		    IEEE80211_CHAN_2GHZ);
247*9a669553SBjoern A. Zeeb 		rxs->c_band = IEEE80211_CHAN_2GHZ;
24809606165SAndriy Voskoboinyk 	}
24909606165SAndriy Voskoboinyk }
250