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 #include "opt_wlan.h"
23
24 #include <sys/param.h>
25 #include <sys/lock.h>
26 #include <sys/mutex.h>
27 #include <sys/mbuf.h>
28 #include <sys/kernel.h>
29 #include <sys/socket.h>
30 #include <sys/systm.h>
31 #include <sys/malloc.h>
32 #include <sys/queue.h>
33 #include <sys/taskqueue.h>
34 #include <sys/bus.h>
35 #include <sys/endian.h>
36 #include <sys/linker.h>
37
38 #include <net/if.h>
39 #include <net/ethernet.h>
40 #include <net/if_media.h>
41
42 #include <net80211/ieee80211_var.h>
43 #include <net80211/ieee80211_radiotap.h>
44 #include <net80211/ieee80211_ratectl.h>
45
46 #include <dev/rtwn/if_rtwnreg.h>
47 #include <dev/rtwn/if_rtwnvar.h>
48
49 #include <dev/rtwn/if_rtwn_debug.h>
50 #include <dev/rtwn/if_rtwn_ridx.h>
51
52 #include <dev/rtwn/rtl8192c/r92c.h>
53 #include <dev/rtwn/rtl8188e/r88e.h>
54 #include <dev/rtwn/rtl8188e/r88e_rx_desc.h>
55
56 int
r88e_classify_intr(struct rtwn_softc * sc,void * buf,int len)57 r88e_classify_intr(struct rtwn_softc *sc, void *buf, int len)
58 {
59 struct r92c_rx_stat *stat = buf;
60 int report_sel = MS(le32toh(stat->rxdw3), R88E_RXDW3_RPT);
61
62 switch (report_sel) {
63 case R88E_RXDW3_RPT_RX:
64 return (RTWN_RX_DATA);
65 case R88E_RXDW3_RPT_TX1: /* per-packet Tx report */
66 return (RTWN_RX_TX_REPORT);
67 case R88E_RXDW3_RPT_TX2: /* periodical Tx report */
68 return (RTWN_RX_TX_REPORT2);
69 case R88E_RXDW3_RPT_HIS:
70 return (RTWN_RX_OTHER);
71 default: /* shut up the compiler */
72 return (RTWN_RX_DATA);
73 }
74 }
75
76 void
r88e_ratectl_tx_complete(struct rtwn_softc * sc,uint8_t * buf,int len)77 r88e_ratectl_tx_complete(struct rtwn_softc *sc, uint8_t *buf, int len)
78 {
79 struct ieee80211_ratectl_tx_status txs;
80 struct r88e_tx_rpt_ccx *rpt;
81 struct ieee80211_node *ni;
82 uint8_t macid;
83 int ntries;
84
85 /* Skip Rx descriptor. */
86 buf += sizeof(struct r92c_rx_stat);
87 len -= sizeof(struct r92c_rx_stat);
88
89 rpt = (struct r88e_tx_rpt_ccx *)buf;
90 if (len != sizeof(*rpt)) {
91 RTWN_DPRINTF(sc, RTWN_DEBUG_INTR,
92 "%s: wrong report size (%d, must be %zu)\n",
93 __func__, len, sizeof(*rpt));
94 return;
95 }
96
97 RTWN_DPRINTF(sc, RTWN_DEBUG_INTR,
98 "%s: ccx report dump: 0: %02X, 1: %02X, 2: %02X, queue time: "
99 "low %02X, high %02X, final ridx: %02X, 6: %02X, 7: %02X\n",
100 __func__, rpt->rptb0, rpt->rptb1, rpt->rptb2, rpt->queue_time_low,
101 rpt->queue_time_high, rpt->final_rate, rpt->rptb6, rpt->rptb7);
102
103 macid = MS(rpt->rptb1, R88E_RPTB1_MACID);
104 if (macid > sc->macid_limit) {
105 device_printf(sc->sc_dev,
106 "macid %u is too big; increase MACID_MAX limit\n",
107 macid);
108 return;
109 }
110
111 ntries = MS(rpt->rptb2, R88E_RPTB2_RETRY_CNT);
112
113 ni = sc->node_list[macid];
114 if (ni != NULL) {
115 RTWN_DPRINTF(sc, RTWN_DEBUG_INTR, "%s: frame for macid %u was"
116 "%s sent (%d retries)\n", __func__, macid,
117 (rpt->rptb1 & R88E_RPTB1_PKT_OK) ? "" : " not",
118 ntries);
119
120 txs.flags = IEEE80211_RATECTL_STATUS_LONG_RETRY |
121 IEEE80211_RATECTL_STATUS_FINAL_RATE;
122 txs.long_retries = ntries;
123 if (RTWN_RATE_IS_HT(rpt->final_rate)) { /* MCS */
124 txs.final_rate = RTWN_RIDX_TO_MCS(rpt->final_rate);
125 txs.final_rate |= IEEE80211_RATE_MCS;
126 } else
127 txs.final_rate = ridx2rate[rpt->final_rate];
128 if (rpt->rptb1 & R88E_RPTB1_PKT_OK)
129 txs.status = IEEE80211_RATECTL_TX_SUCCESS;
130 else if (rpt->rptb2 & R88E_RPTB2_RETRY_OVER)
131 txs.status = IEEE80211_RATECTL_TX_FAIL_LONG;
132 else if (rpt->rptb2 & R88E_RPTB2_LIFE_EXPIRE)
133 txs.status = IEEE80211_RATECTL_TX_FAIL_EXPIRED;
134 else
135 txs.status = IEEE80211_RATECTL_TX_FAIL_UNSPECIFIED;
136 ieee80211_ratectl_tx_complete(ni, &txs);
137 } else {
138 RTWN_DPRINTF(sc, RTWN_DEBUG_INTR, "%s: macid %u, ni is NULL\n",
139 __func__, macid);
140 }
141 }
142
143 void
r88e_handle_c2h_report(struct rtwn_softc * sc,uint8_t * buf,int len)144 r88e_handle_c2h_report(struct rtwn_softc *sc, uint8_t *buf, int len)
145 {
146
147 /* Skip Rx descriptor. */
148 buf += sizeof(struct r92c_rx_stat);
149 len -= sizeof(struct r92c_rx_stat);
150
151 if (len != R88E_INTR_MSG_LEN) {
152 RTWN_DPRINTF(sc, RTWN_DEBUG_INTR,
153 "%s: wrong interrupt message size (%d, must be %d)\n",
154 __func__, len, R88E_INTR_MSG_LEN);
155 return;
156 }
157
158 /* XXX TODO */
159 }
160
161 int8_t
r88e_get_rssi_cck(struct rtwn_softc * sc,void * physt)162 r88e_get_rssi_cck(struct rtwn_softc *sc, void *physt)
163 {
164 struct r88e_rx_phystat *phy = (struct r88e_rx_phystat *)physt;
165 int8_t lna_idx, vga_idx, rssi;
166
167 lna_idx = (phy->agc_rpt & 0xe0) >> 5;
168 vga_idx = (phy->agc_rpt & 0x1f);
169 rssi = 6 - 2 * vga_idx;
170
171 switch (lna_idx) {
172 case 7:
173 if (vga_idx > 27)
174 rssi = -100 + 6;
175 else
176 rssi += -100 + 2 * 27;
177 break;
178 case 6:
179 rssi += -48 + 2 * 2;
180 break;
181 case 5:
182 rssi += -42 + 2 * 7;
183 break;
184 case 4:
185 rssi += -36 + 2 * 7;
186 break;
187 case 3:
188 rssi += -24 + 2 * 7;
189 break;
190 case 2:
191 rssi += -6 + 2 * 5;
192 if (sc->sc_flags & RTWN_FLAG_CCK_HIPWR)
193 rssi -= 6;
194 break;
195 case 1:
196 rssi += 8;
197 break;
198 case 0:
199 rssi += 14;
200 break;
201 }
202
203 return (rssi);
204 }
205
206 int8_t
r88e_get_rssi_ofdm(struct rtwn_softc * sc,void * physt)207 r88e_get_rssi_ofdm(struct rtwn_softc *sc, void *physt)
208 {
209 struct r88e_rx_phystat *phy = (struct r88e_rx_phystat *)physt;
210 int rssi;
211
212 /* Get average RSSI. */
213 rssi = ((phy->sig_qual >> 1) & 0x7f) - 110;
214
215 return (rssi);
216 }
217
218 void
r88e_get_rx_stats(struct rtwn_softc * sc,struct ieee80211_rx_stats * rxs,const void * desc,const void * physt_ptr)219 r88e_get_rx_stats(struct rtwn_softc *sc, struct ieee80211_rx_stats *rxs,
220 const void *desc, const void *physt_ptr)
221 {
222 const struct r88e_rx_phystat *physt = physt_ptr;
223
224 r92c_get_rx_stats(sc, rxs, desc, physt_ptr);
225
226 if (!sc->sc_ht40) { /* XXX center channel */
227 rxs->r_flags |= IEEE80211_R_IEEE | IEEE80211_R_FREQ;
228 rxs->r_flags |= IEEE80211_R_BAND;
229 rxs->c_ieee = physt->chan;
230 rxs->c_freq = ieee80211_ieee2mhz(rxs->c_ieee,
231 IEEE80211_CHAN_2GHZ);
232 rxs->c_band = IEEE80211_CHAN_2GHZ;
233 }
234 }
235
236 void
r88e_ratectl_tx_complete_periodic(struct rtwn_softc * sc,uint8_t * buf,int len)237 r88e_ratectl_tx_complete_periodic(struct rtwn_softc *sc, uint8_t *buf,
238 int len)
239 {
240 const struct r92c_rx_stat *rxs;
241 uint64_t mac_bitmap;
242 int macid;
243
244 if (len < sizeof(struct r92c_rx_stat))
245 return;
246
247 rxs = (const struct r92c_rx_stat *) buf;
248
249 /* Skip Rx descriptor. */
250 buf += sizeof(struct r92c_rx_stat);
251 len -= sizeof(struct r92c_rx_stat);
252
253 /*
254 * Note: the valid macid bitmap is rx_desc[5] << 32 | rx_desc[4];
255 * Note: rx_desc[5] is the TSF, which isn't valid for this report!
256 */
257 mac_bitmap = ((uint64_t) le32toh(rxs->tsf_low) << 32)
258 | le32toh(rxs->rxdw4);
259
260 #if 0
261 RTWN_DPRINTF(sc, RTWN_DEBUG_RA,
262 "%s: mac bitmap: 0x%lx\n", __func__, mac_bitmap);
263 #endif
264
265 /*
266 * Note: the RX reports aren't sparse - invalid entries (ie,
267 * the bitmap has the macid set to 0) are just populated
268 * with random data.
269 */
270 for (macid = 0; (macid < 64) && (macid < sc->macid_rpt2_max_num) &&
271 (len >= sizeof(struct r88e_fw_c2h_txreport2_entry)); macid++) {
272 struct ieee80211_ratectl_tx_stats txs = { 0 };
273 const struct r88e_fw_c2h_txreport2_entry *rpt;
274 uint32_t ntotal, nsuccess, ndrop, nretry, nframes;
275
276 rpt = (const struct r88e_fw_c2h_txreport2_entry *) buf;
277 buf += sizeof(struct r88e_fw_c2h_txreport2_entry);
278 len -= sizeof(struct r88e_fw_c2h_txreport2_entry);
279
280 if ((mac_bitmap & (1UL << macid)) == 0)
281 continue;
282
283 txs.flags = IEEE80211_RATECTL_TX_STATS_NODE |
284 IEEE80211_RATECTL_TX_STATS_RETRIES;
285
286 /* calculate all the various combinations of things */
287 nframes = le16toh(rpt->retry0);
288 ntotal = nframes + rpt->retry1 + rpt->retry2
289 + rpt->retry3 + rpt->retry4 + rpt->drop;
290 /*
291 * Note: sometimes this is zero or 1, but the retries
292 * are all capped out at 255! That means the frame
293 * transmits are all failing.
294 */
295 nsuccess = ntotal - rpt->drop;
296 ndrop = rpt->drop;
297 nretry = rpt->retry1 + rpt->retry2 + rpt->retry3
298 + rpt->retry4;
299
300 txs.nretries = nretry + ndrop;
301 txs.nsuccess = nsuccess;
302 txs.nframes = ntotal;
303
304 RTWN_DPRINTF(sc, RTWN_DEBUG_RA,
305 "%s: MAC %d rpt retries %d %d %d %d %d, "
306 "drop %d\n",
307 __func__,
308 macid,
309 le16toh(rpt->retry0),
310 rpt->retry1,
311 rpt->retry2,
312 rpt->retry3,
313 rpt->retry4,
314 rpt->drop);
315 if (sc->node_list[macid] != NULL) {
316 struct ieee80211_node *ni;
317 ni = sc->node_list[macid];
318 txs.ni = ni;
319 ieee80211_ratectl_tx_update(ni->ni_vap, &txs);
320 }
321 }
322 }
323