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 case R88E_RXDW3_RPT_TX2: /* periodical Tx report */
67 return (RTWN_RX_TX_REPORT);
68 case R88E_RXDW3_RPT_HIS:
69 return (RTWN_RX_OTHER);
70 default: /* shut up the compiler */
71 return (RTWN_RX_DATA);
72 }
73 }
74
75 void
r88e_ratectl_tx_complete(struct rtwn_softc * sc,uint8_t * buf,int len)76 r88e_ratectl_tx_complete(struct rtwn_softc *sc, uint8_t *buf, int len)
77 {
78 struct ieee80211_ratectl_tx_status txs;
79 struct r88e_tx_rpt_ccx *rpt;
80 struct ieee80211_node *ni;
81 uint8_t macid;
82 int ntries;
83
84 /* Skip Rx descriptor. */
85 buf += sizeof(struct r92c_rx_stat);
86 len -= sizeof(struct r92c_rx_stat);
87
88 rpt = (struct r88e_tx_rpt_ccx *)buf;
89 if (len != sizeof(*rpt)) {
90 RTWN_DPRINTF(sc, RTWN_DEBUG_INTR,
91 "%s: wrong report size (%d, must be %zu)\n",
92 __func__, len, sizeof(*rpt));
93 return;
94 }
95
96 RTWN_DPRINTF(sc, RTWN_DEBUG_INTR,
97 "%s: ccx report dump: 0: %02X, 1: %02X, 2: %02X, queue time: "
98 "low %02X, high %02X, final ridx: %02X, 6: %02X, 7: %02X\n",
99 __func__, rpt->rptb0, rpt->rptb1, rpt->rptb2, rpt->queue_time_low,
100 rpt->queue_time_high, rpt->final_rate, rpt->rptb6, rpt->rptb7);
101
102 macid = MS(rpt->rptb1, R88E_RPTB1_MACID);
103 if (macid > sc->macid_limit) {
104 device_printf(sc->sc_dev,
105 "macid %u is too big; increase MACID_MAX limit\n",
106 macid);
107 return;
108 }
109
110 ntries = MS(rpt->rptb2, R88E_RPTB2_RETRY_CNT);
111
112 ni = sc->node_list[macid];
113 if (ni != NULL) {
114 RTWN_DPRINTF(sc, RTWN_DEBUG_INTR, "%s: frame for macid %u was"
115 "%s sent (%d retries)\n", __func__, macid,
116 (rpt->rptb1 & R88E_RPTB1_PKT_OK) ? "" : " not",
117 ntries);
118
119 txs.flags = IEEE80211_RATECTL_STATUS_LONG_RETRY |
120 IEEE80211_RATECTL_STATUS_FINAL_RATE;
121 txs.long_retries = ntries;
122 if (rpt->final_rate > RTWN_RIDX_OFDM54) { /* MCS */
123 txs.final_rate =
124 rpt->final_rate - RTWN_RIDX_HT_MCS_SHIFT;
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