xref: /freebsd/sys/dev/rtwn/rtl8188e/r88e_rx.c (revision 685dc743dc3b5645e34836464128e1c0558b404b)
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 #include "opt_wlan.h"
237453645fSAndriy Voskoboinyk 
247453645fSAndriy Voskoboinyk #include <sys/param.h>
257453645fSAndriy Voskoboinyk #include <sys/lock.h>
267453645fSAndriy Voskoboinyk #include <sys/mutex.h>
277453645fSAndriy Voskoboinyk #include <sys/mbuf.h>
287453645fSAndriy Voskoboinyk #include <sys/kernel.h>
297453645fSAndriy Voskoboinyk #include <sys/socket.h>
307453645fSAndriy Voskoboinyk #include <sys/systm.h>
317453645fSAndriy Voskoboinyk #include <sys/malloc.h>
327453645fSAndriy Voskoboinyk #include <sys/queue.h>
337453645fSAndriy Voskoboinyk #include <sys/taskqueue.h>
347453645fSAndriy Voskoboinyk #include <sys/bus.h>
357453645fSAndriy Voskoboinyk #include <sys/endian.h>
367453645fSAndriy Voskoboinyk #include <sys/linker.h>
377453645fSAndriy Voskoboinyk 
387453645fSAndriy Voskoboinyk #include <net/if.h>
397453645fSAndriy Voskoboinyk #include <net/ethernet.h>
407453645fSAndriy Voskoboinyk #include <net/if_media.h>
417453645fSAndriy Voskoboinyk 
427453645fSAndriy Voskoboinyk #include <net80211/ieee80211_var.h>
437453645fSAndriy Voskoboinyk #include <net80211/ieee80211_radiotap.h>
447453645fSAndriy Voskoboinyk #include <net80211/ieee80211_ratectl.h>
457453645fSAndriy Voskoboinyk 
467453645fSAndriy Voskoboinyk #include <dev/rtwn/if_rtwnreg.h>
477453645fSAndriy Voskoboinyk #include <dev/rtwn/if_rtwnvar.h>
487453645fSAndriy Voskoboinyk 
497453645fSAndriy Voskoboinyk #include <dev/rtwn/if_rtwn_debug.h>
507453645fSAndriy Voskoboinyk #include <dev/rtwn/if_rtwn_ridx.h>
517453645fSAndriy Voskoboinyk 
5209606165SAndriy Voskoboinyk #include <dev/rtwn/rtl8192c/r92c.h>
537453645fSAndriy Voskoboinyk #include <dev/rtwn/rtl8188e/r88e.h>
547453645fSAndriy Voskoboinyk #include <dev/rtwn/rtl8188e/r88e_rx_desc.h>
557453645fSAndriy Voskoboinyk 
5617d5fbf2SAndriy Voskoboinyk int
r88e_classify_intr(struct rtwn_softc * sc,void * buf,int len)5717d5fbf2SAndriy Voskoboinyk r88e_classify_intr(struct rtwn_softc *sc, void *buf, int len)
5817d5fbf2SAndriy Voskoboinyk {
5917d5fbf2SAndriy Voskoboinyk 	struct r92c_rx_stat *stat = buf;
6017d5fbf2SAndriy Voskoboinyk 	int report_sel = MS(le32toh(stat->rxdw3), R88E_RXDW3_RPT);
6117d5fbf2SAndriy Voskoboinyk 
6217d5fbf2SAndriy Voskoboinyk 	switch (report_sel) {
6317d5fbf2SAndriy Voskoboinyk 	case R88E_RXDW3_RPT_RX:
6417d5fbf2SAndriy Voskoboinyk 		return (RTWN_RX_DATA);
6517d5fbf2SAndriy Voskoboinyk 	case R88E_RXDW3_RPT_TX1:	/* per-packet Tx report */
6617d5fbf2SAndriy Voskoboinyk 	case R88E_RXDW3_RPT_TX2:	/* periodical Tx report */
6717d5fbf2SAndriy Voskoboinyk 		return (RTWN_RX_TX_REPORT);
6817d5fbf2SAndriy Voskoboinyk 	case R88E_RXDW3_RPT_HIS:
6917d5fbf2SAndriy Voskoboinyk 		return (RTWN_RX_OTHER);
7017d5fbf2SAndriy Voskoboinyk 	default:			/* shut up the compiler */
7117d5fbf2SAndriy Voskoboinyk 		return (RTWN_RX_DATA);
7217d5fbf2SAndriy Voskoboinyk 	}
7317d5fbf2SAndriy Voskoboinyk }
7417d5fbf2SAndriy Voskoboinyk 
757453645fSAndriy Voskoboinyk void
r88e_ratectl_tx_complete(struct rtwn_softc * sc,uint8_t * buf,int len)767453645fSAndriy Voskoboinyk r88e_ratectl_tx_complete(struct rtwn_softc *sc, uint8_t *buf, int len)
777453645fSAndriy Voskoboinyk {
787453645fSAndriy Voskoboinyk 	struct ieee80211_ratectl_tx_status txs;
797453645fSAndriy Voskoboinyk 	struct r88e_tx_rpt_ccx *rpt;
807453645fSAndriy Voskoboinyk 	struct ieee80211_node *ni;
817453645fSAndriy Voskoboinyk 	uint8_t macid;
827453645fSAndriy Voskoboinyk 	int ntries;
837453645fSAndriy Voskoboinyk 
847453645fSAndriy Voskoboinyk 	/* Skip Rx descriptor. */
857453645fSAndriy Voskoboinyk 	buf += sizeof(struct r92c_rx_stat);
867453645fSAndriy Voskoboinyk 	len -= sizeof(struct r92c_rx_stat);
877453645fSAndriy Voskoboinyk 
887453645fSAndriy Voskoboinyk 	rpt = (struct r88e_tx_rpt_ccx *)buf;
897453645fSAndriy Voskoboinyk 	if (len != sizeof(*rpt)) {
907453645fSAndriy Voskoboinyk 		RTWN_DPRINTF(sc, RTWN_DEBUG_INTR,
917453645fSAndriy Voskoboinyk 		    "%s: wrong report size (%d, must be %zu)\n",
927453645fSAndriy Voskoboinyk 		    __func__, len, sizeof(*rpt));
937453645fSAndriy Voskoboinyk 		return;
947453645fSAndriy Voskoboinyk 	}
957453645fSAndriy Voskoboinyk 
967453645fSAndriy Voskoboinyk 	RTWN_DPRINTF(sc, RTWN_DEBUG_INTR,
977453645fSAndriy Voskoboinyk 	    "%s: ccx report dump: 0: %02X, 1: %02X, 2: %02X, queue time: "
987453645fSAndriy Voskoboinyk 	    "low %02X, high %02X, final ridx: %02X, 6: %02X, 7: %02X\n",
997453645fSAndriy Voskoboinyk 	    __func__, rpt->rptb0, rpt->rptb1, rpt->rptb2, rpt->queue_time_low,
1007453645fSAndriy Voskoboinyk 	    rpt->queue_time_high, rpt->final_rate, rpt->rptb6, rpt->rptb7);
1017453645fSAndriy Voskoboinyk 
1027453645fSAndriy Voskoboinyk 	macid = MS(rpt->rptb1, R88E_RPTB1_MACID);
1037453645fSAndriy Voskoboinyk 	if (macid > sc->macid_limit) {
1047453645fSAndriy Voskoboinyk 		device_printf(sc->sc_dev,
1057453645fSAndriy Voskoboinyk 		    "macid %u is too big; increase MACID_MAX limit\n",
1067453645fSAndriy Voskoboinyk 		    macid);
1077453645fSAndriy Voskoboinyk 		return;
1087453645fSAndriy Voskoboinyk 	}
1097453645fSAndriy Voskoboinyk 
1107453645fSAndriy Voskoboinyk 	ntries = MS(rpt->rptb2, R88E_RPTB2_RETRY_CNT);
1117453645fSAndriy Voskoboinyk 
1127453645fSAndriy Voskoboinyk 	ni = sc->node_list[macid];
1137453645fSAndriy Voskoboinyk 	if (ni != NULL) {
1147453645fSAndriy Voskoboinyk 		RTWN_DPRINTF(sc, RTWN_DEBUG_INTR, "%s: frame for macid %u was"
1157453645fSAndriy Voskoboinyk 		    "%s sent (%d retries)\n", __func__, macid,
1167453645fSAndriy Voskoboinyk 		    (rpt->rptb1 & R88E_RPTB1_PKT_OK) ? "" : " not",
1177453645fSAndriy Voskoboinyk 		    ntries);
1187453645fSAndriy Voskoboinyk 
1197453645fSAndriy Voskoboinyk 		txs.flags = IEEE80211_RATECTL_STATUS_LONG_RETRY |
1207453645fSAndriy Voskoboinyk 			    IEEE80211_RATECTL_STATUS_FINAL_RATE;
1217453645fSAndriy Voskoboinyk 		txs.long_retries = ntries;
1227453645fSAndriy Voskoboinyk 		if (rpt->final_rate > RTWN_RIDX_OFDM54) {	/* MCS */
1237453645fSAndriy Voskoboinyk 			txs.final_rate =
1245f792f74SAndriy Voskoboinyk 			    rpt->final_rate - RTWN_RIDX_HT_MCS_SHIFT;
1255f792f74SAndriy Voskoboinyk 			txs.final_rate |= IEEE80211_RATE_MCS;
1267453645fSAndriy Voskoboinyk 		} else
1277453645fSAndriy Voskoboinyk 			txs.final_rate = ridx2rate[rpt->final_rate];
1287453645fSAndriy Voskoboinyk 		if (rpt->rptb1 & R88E_RPTB1_PKT_OK)
1297453645fSAndriy Voskoboinyk 			txs.status = IEEE80211_RATECTL_TX_SUCCESS;
1307453645fSAndriy Voskoboinyk 		else if (rpt->rptb2 & R88E_RPTB2_RETRY_OVER)
1317453645fSAndriy Voskoboinyk 			txs.status = IEEE80211_RATECTL_TX_FAIL_LONG;
1327453645fSAndriy Voskoboinyk 		else if (rpt->rptb2 & R88E_RPTB2_LIFE_EXPIRE)
1337453645fSAndriy Voskoboinyk 			txs.status = IEEE80211_RATECTL_TX_FAIL_EXPIRED;
1347453645fSAndriy Voskoboinyk 		else
1357453645fSAndriy Voskoboinyk 			txs.status = IEEE80211_RATECTL_TX_FAIL_UNSPECIFIED;
1367453645fSAndriy Voskoboinyk 		ieee80211_ratectl_tx_complete(ni, &txs);
1377453645fSAndriy Voskoboinyk 	} else {
1387453645fSAndriy Voskoboinyk 		RTWN_DPRINTF(sc, RTWN_DEBUG_INTR, "%s: macid %u, ni is NULL\n",
1397453645fSAndriy Voskoboinyk 		    __func__, macid);
1407453645fSAndriy Voskoboinyk 	}
1417453645fSAndriy Voskoboinyk }
1427453645fSAndriy Voskoboinyk 
1437453645fSAndriy Voskoboinyk void
r88e_handle_c2h_report(struct rtwn_softc * sc,uint8_t * buf,int len)1447453645fSAndriy Voskoboinyk r88e_handle_c2h_report(struct rtwn_softc *sc, uint8_t *buf, int len)
1457453645fSAndriy Voskoboinyk {
1467453645fSAndriy Voskoboinyk 
1477453645fSAndriy Voskoboinyk 	/* Skip Rx descriptor. */
1487453645fSAndriy Voskoboinyk 	buf += sizeof(struct r92c_rx_stat);
1497453645fSAndriy Voskoboinyk 	len -= sizeof(struct r92c_rx_stat);
1507453645fSAndriy Voskoboinyk 
1517453645fSAndriy Voskoboinyk 	if (len != R88E_INTR_MSG_LEN) {
1527453645fSAndriy Voskoboinyk 		RTWN_DPRINTF(sc, RTWN_DEBUG_INTR,
1537453645fSAndriy Voskoboinyk 		    "%s: wrong interrupt message size (%d, must be %d)\n",
1547453645fSAndriy Voskoboinyk 		    __func__, len, R88E_INTR_MSG_LEN);
1557453645fSAndriy Voskoboinyk 		return;
1567453645fSAndriy Voskoboinyk 	}
1577453645fSAndriy Voskoboinyk 
1587453645fSAndriy Voskoboinyk 	/* XXX TODO */
1597453645fSAndriy Voskoboinyk }
1607453645fSAndriy Voskoboinyk 
1617453645fSAndriy Voskoboinyk int8_t
r88e_get_rssi_cck(struct rtwn_softc * sc,void * physt)1627453645fSAndriy Voskoboinyk r88e_get_rssi_cck(struct rtwn_softc *sc, void *physt)
1637453645fSAndriy Voskoboinyk {
1647453645fSAndriy Voskoboinyk 	struct r88e_rx_phystat *phy = (struct r88e_rx_phystat *)physt;
1657453645fSAndriy Voskoboinyk 	int8_t lna_idx, vga_idx, rssi;
1667453645fSAndriy Voskoboinyk 
1677453645fSAndriy Voskoboinyk 	lna_idx = (phy->agc_rpt & 0xe0) >> 5;
1687453645fSAndriy Voskoboinyk 	vga_idx = (phy->agc_rpt & 0x1f);
1697453645fSAndriy Voskoboinyk 	rssi = 6 - 2 * vga_idx;
1707453645fSAndriy Voskoboinyk 
1717453645fSAndriy Voskoboinyk 	switch (lna_idx) {
1727453645fSAndriy Voskoboinyk 	case 7:
1737453645fSAndriy Voskoboinyk 		if (vga_idx > 27)
1747453645fSAndriy Voskoboinyk 			rssi = -100 + 6;
1757453645fSAndriy Voskoboinyk 		else
1767453645fSAndriy Voskoboinyk 			rssi += -100 + 2 * 27;
1777453645fSAndriy Voskoboinyk 		break;
1787453645fSAndriy Voskoboinyk 	case 6:
1797453645fSAndriy Voskoboinyk 		rssi += -48 + 2 * 2;
1807453645fSAndriy Voskoboinyk 		break;
1817453645fSAndriy Voskoboinyk 	case 5:
1827453645fSAndriy Voskoboinyk 		rssi += -42 + 2 * 7;
1837453645fSAndriy Voskoboinyk 		break;
1847453645fSAndriy Voskoboinyk 	case 4:
1857453645fSAndriy Voskoboinyk 		rssi += -36 + 2 * 7;
1867453645fSAndriy Voskoboinyk 		break;
1877453645fSAndriy Voskoboinyk 	case 3:
1887453645fSAndriy Voskoboinyk 		rssi += -24 + 2 * 7;
1897453645fSAndriy Voskoboinyk 		break;
1907453645fSAndriy Voskoboinyk 	case 2:
1917453645fSAndriy Voskoboinyk 		rssi += -6 + 2 * 5;
1927453645fSAndriy Voskoboinyk 		if (sc->sc_flags & RTWN_FLAG_CCK_HIPWR)
1937453645fSAndriy Voskoboinyk 			rssi -= 6;
1947453645fSAndriy Voskoboinyk 		break;
1957453645fSAndriy Voskoboinyk 	case 1:
1967453645fSAndriy Voskoboinyk 		rssi += 8;
1977453645fSAndriy Voskoboinyk 		break;
1987453645fSAndriy Voskoboinyk 	case 0:
1997453645fSAndriy Voskoboinyk 		rssi += 14;
2007453645fSAndriy Voskoboinyk 		break;
2017453645fSAndriy Voskoboinyk 	}
2027453645fSAndriy Voskoboinyk 
2037453645fSAndriy Voskoboinyk 	return (rssi);
2047453645fSAndriy Voskoboinyk }
2057453645fSAndriy Voskoboinyk 
2067453645fSAndriy Voskoboinyk int8_t
r88e_get_rssi_ofdm(struct rtwn_softc * sc,void * physt)2077453645fSAndriy Voskoboinyk r88e_get_rssi_ofdm(struct rtwn_softc *sc, void *physt)
2087453645fSAndriy Voskoboinyk {
2097453645fSAndriy Voskoboinyk 	struct r88e_rx_phystat *phy = (struct r88e_rx_phystat *)physt;
2107453645fSAndriy Voskoboinyk 	int rssi;
2117453645fSAndriy Voskoboinyk 
2127453645fSAndriy Voskoboinyk 	/* Get average RSSI. */
2137453645fSAndriy Voskoboinyk 	rssi = ((phy->sig_qual >> 1) & 0x7f) - 110;
2147453645fSAndriy Voskoboinyk 
2157453645fSAndriy Voskoboinyk 	return (rssi);
2167453645fSAndriy Voskoboinyk }
21709606165SAndriy Voskoboinyk 
21809606165SAndriy Voskoboinyk void
r88e_get_rx_stats(struct rtwn_softc * sc,struct ieee80211_rx_stats * rxs,const void * desc,const void * physt_ptr)21909606165SAndriy Voskoboinyk r88e_get_rx_stats(struct rtwn_softc *sc, struct ieee80211_rx_stats *rxs,
22009606165SAndriy Voskoboinyk     const void *desc, const void *physt_ptr)
22109606165SAndriy Voskoboinyk {
22209606165SAndriy Voskoboinyk 	const struct r88e_rx_phystat *physt = physt_ptr;
22309606165SAndriy Voskoboinyk 
22409606165SAndriy Voskoboinyk 	r92c_get_rx_stats(sc, rxs, desc, physt_ptr);
22509606165SAndriy Voskoboinyk 
22609606165SAndriy Voskoboinyk 	if (!sc->sc_ht40) {	/* XXX center channel */
22709606165SAndriy Voskoboinyk 		rxs->r_flags |= IEEE80211_R_IEEE | IEEE80211_R_FREQ;
228*9a669553SBjoern A. Zeeb 		rxs->r_flags |= IEEE80211_R_BAND;
229ff97f477SAndriy Voskoboinyk 		rxs->c_ieee = physt->chan;
23009606165SAndriy Voskoboinyk 		rxs->c_freq = ieee80211_ieee2mhz(rxs->c_ieee,
23109606165SAndriy Voskoboinyk 		    IEEE80211_CHAN_2GHZ);
232*9a669553SBjoern A. Zeeb 		rxs->c_band = IEEE80211_CHAN_2GHZ;
23309606165SAndriy Voskoboinyk 	}
23409606165SAndriy Voskoboinyk }
235