xref: /freebsd/sys/dev/rtwn/rtl8188e/r88e_rx.c (revision d6eb98610fa65663bf0df4574b7cb2c5c4ffda71)
1 /*	$OpenBSD: if_urtwn.c,v 1.16 2011/02/10 17:26:40 jakemsr Exp $	*/
2 
3 /*-
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>
7  *
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.
11  *
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.
19  */
20 
21 #include <sys/cdefs.h>
22 __FBSDID("$FreeBSD$");
23 
24 #include "opt_wlan.h"
25 
26 #include <sys/param.h>
27 #include <sys/lock.h>
28 #include <sys/mutex.h>
29 #include <sys/mbuf.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>
36 #include <sys/bus.h>
37 #include <sys/endian.h>
38 #include <sys/linker.h>
39 
40 #include <net/if.h>
41 #include <net/ethernet.h>
42 #include <net/if_media.h>
43 
44 #include <net80211/ieee80211_var.h>
45 #include <net80211/ieee80211_radiotap.h>
46 #include <net80211/ieee80211_ratectl.h>
47 
48 #include <dev/rtwn/if_rtwnreg.h>
49 #include <dev/rtwn/if_rtwnvar.h>
50 
51 #include <dev/rtwn/if_rtwn_debug.h>
52 #include <dev/rtwn/if_rtwn_ridx.h>
53 
54 #include <dev/rtwn/rtl8192c/r92c.h>
55 #include <dev/rtwn/rtl8188e/r88e.h>
56 #include <dev/rtwn/rtl8188e/r88e_rx_desc.h>
57 
58 
59 int
60 r88e_classify_intr(struct rtwn_softc *sc, void *buf, int len)
61 {
62 	struct r92c_rx_stat *stat = buf;
63 	int report_sel = MS(le32toh(stat->rxdw3), R88E_RXDW3_RPT);
64 
65 	switch (report_sel) {
66 	case R88E_RXDW3_RPT_RX:
67 		return (RTWN_RX_DATA);
68 	case R88E_RXDW3_RPT_TX1:	/* per-packet Tx report */
69 	case R88E_RXDW3_RPT_TX2:	/* periodical Tx report */
70 		return (RTWN_RX_TX_REPORT);
71 	case R88E_RXDW3_RPT_HIS:
72 		return (RTWN_RX_OTHER);
73 	default:			/* shut up the compiler */
74 		return (RTWN_RX_DATA);
75 	}
76 }
77 
78 void
79 r88e_ratectl_tx_complete(struct rtwn_softc *sc, uint8_t *buf, int len)
80 {
81 #if __FreeBSD_version >= 1200012
82 	struct ieee80211_ratectl_tx_status txs;
83 #endif
84 	struct r88e_tx_rpt_ccx *rpt;
85 	struct ieee80211_node *ni;
86 	uint8_t macid;
87 	int ntries;
88 
89 	/* Skip Rx descriptor. */
90 	buf += sizeof(struct r92c_rx_stat);
91 	len -= sizeof(struct r92c_rx_stat);
92 
93 	rpt = (struct r88e_tx_rpt_ccx *)buf;
94 	if (len != sizeof(*rpt)) {
95 		RTWN_DPRINTF(sc, RTWN_DEBUG_INTR,
96 		    "%s: wrong report size (%d, must be %zu)\n",
97 		    __func__, len, sizeof(*rpt));
98 		return;
99 	}
100 
101 	RTWN_DPRINTF(sc, RTWN_DEBUG_INTR,
102 	    "%s: ccx report dump: 0: %02X, 1: %02X, 2: %02X, queue time: "
103 	    "low %02X, high %02X, final ridx: %02X, 6: %02X, 7: %02X\n",
104 	    __func__, rpt->rptb0, rpt->rptb1, rpt->rptb2, rpt->queue_time_low,
105 	    rpt->queue_time_high, rpt->final_rate, rpt->rptb6, rpt->rptb7);
106 
107 	macid = MS(rpt->rptb1, R88E_RPTB1_MACID);
108 	if (macid > sc->macid_limit) {
109 		device_printf(sc->sc_dev,
110 		    "macid %u is too big; increase MACID_MAX limit\n",
111 		    macid);
112 		return;
113 	}
114 
115 	ntries = MS(rpt->rptb2, R88E_RPTB2_RETRY_CNT);
116 
117 	ni = sc->node_list[macid];
118 	if (ni != NULL) {
119 		RTWN_DPRINTF(sc, RTWN_DEBUG_INTR, "%s: frame for macid %u was"
120 		    "%s sent (%d retries)\n", __func__, macid,
121 		    (rpt->rptb1 & R88E_RPTB1_PKT_OK) ? "" : " not",
122 		    ntries);
123 
124 #if __FreeBSD_version >= 1200012
125 		txs.flags = IEEE80211_RATECTL_STATUS_LONG_RETRY |
126 			    IEEE80211_RATECTL_STATUS_FINAL_RATE;
127 		txs.long_retries = ntries;
128 		if (rpt->final_rate > RTWN_RIDX_OFDM54) {	/* MCS */
129 			txs.final_rate =
130 			    rpt->final_rate - RTWN_RIDX_HT_MCS_SHIFT;
131 			txs.final_rate |= IEEE80211_RATE_MCS;
132 		} else
133 			txs.final_rate = ridx2rate[rpt->final_rate];
134 		if (rpt->rptb1 & R88E_RPTB1_PKT_OK)
135 			txs.status = IEEE80211_RATECTL_TX_SUCCESS;
136 		else if (rpt->rptb2 & R88E_RPTB2_RETRY_OVER)
137 			txs.status = IEEE80211_RATECTL_TX_FAIL_LONG;
138 		else if (rpt->rptb2 & R88E_RPTB2_LIFE_EXPIRE)
139 			txs.status = IEEE80211_RATECTL_TX_FAIL_EXPIRED;
140 		else
141 			txs.status = IEEE80211_RATECTL_TX_FAIL_UNSPECIFIED;
142 		ieee80211_ratectl_tx_complete(ni, &txs);
143 #else
144 		struct ieee80211vap *vap = ni->ni_vap;
145 		if (rpt->rptb1 & R88E_RPTB1_PKT_OK) {
146 			ieee80211_ratectl_tx_complete(vap, ni,
147 			    IEEE80211_RATECTL_TX_SUCCESS, &ntries, NULL);
148 		} else {
149 			ieee80211_ratectl_tx_complete(vap, ni,
150 			    IEEE80211_RATECTL_TX_FAILURE, &ntries, NULL);
151 		}
152 #endif
153 	} else {
154 		RTWN_DPRINTF(sc, RTWN_DEBUG_INTR, "%s: macid %u, ni is NULL\n",
155 		    __func__, macid);
156 	}
157 }
158 
159 void
160 r88e_handle_c2h_report(struct rtwn_softc *sc, uint8_t *buf, int len)
161 {
162 
163 	/* Skip Rx descriptor. */
164 	buf += sizeof(struct r92c_rx_stat);
165 	len -= sizeof(struct r92c_rx_stat);
166 
167 	if (len != R88E_INTR_MSG_LEN) {
168 		RTWN_DPRINTF(sc, RTWN_DEBUG_INTR,
169 		    "%s: wrong interrupt message size (%d, must be %d)\n",
170 		    __func__, len, R88E_INTR_MSG_LEN);
171 		return;
172 	}
173 
174 	/* XXX TODO */
175 }
176 
177 int8_t
178 r88e_get_rssi_cck(struct rtwn_softc *sc, void *physt)
179 {
180 	struct r88e_rx_phystat *phy = (struct r88e_rx_phystat *)physt;
181 	int8_t lna_idx, vga_idx, rssi;
182 
183 	lna_idx = (phy->agc_rpt & 0xe0) >> 5;
184 	vga_idx = (phy->agc_rpt & 0x1f);
185 	rssi = 6 - 2 * vga_idx;
186 
187 	switch (lna_idx) {
188 	case 7:
189 		if (vga_idx > 27)
190 			rssi = -100 + 6;
191 		else
192 			rssi += -100 + 2 * 27;
193 		break;
194 	case 6:
195 		rssi += -48 + 2 * 2;
196 		break;
197 	case 5:
198 		rssi += -42 + 2 * 7;
199 		break;
200 	case 4:
201 		rssi += -36 + 2 * 7;
202 		break;
203 	case 3:
204 		rssi += -24 + 2 * 7;
205 		break;
206 	case 2:
207 		rssi += -6 + 2 * 5;
208 		if (sc->sc_flags & RTWN_FLAG_CCK_HIPWR)
209 			rssi -= 6;
210 		break;
211 	case 1:
212 		rssi += 8;
213 		break;
214 	case 0:
215 		rssi += 14;
216 		break;
217 	}
218 
219 	return (rssi);
220 }
221 
222 int8_t
223 r88e_get_rssi_ofdm(struct rtwn_softc *sc, void *physt)
224 {
225 	struct r88e_rx_phystat *phy = (struct r88e_rx_phystat *)physt;
226 	int rssi;
227 
228 	/* Get average RSSI. */
229 	rssi = ((phy->sig_qual >> 1) & 0x7f) - 110;
230 
231 	return (rssi);
232 }
233 
234 void
235 r88e_get_rx_stats(struct rtwn_softc *sc, struct ieee80211_rx_stats *rxs,
236     const void *desc, const void *physt_ptr)
237 {
238 	const struct r88e_rx_phystat *physt = physt_ptr;
239 
240 	r92c_get_rx_stats(sc, rxs, desc, physt_ptr);
241 
242 	if (!sc->sc_ht40) {	/* XXX center channel */
243 		rxs->r_flags |= IEEE80211_R_IEEE | IEEE80211_R_FREQ;
244 		rxs->c_ieee = physt->chan;
245 		rxs->c_freq = ieee80211_ieee2mhz(rxs->c_ieee,
246 		    IEEE80211_CHAN_2GHZ);
247 	}
248 }
249