1 /* $OpenBSD: if_urtwn.c,v 1.16 2011/02/10 17:26:40 jakemsr Exp $ */
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>
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.
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.
21 #include <sys/cdefs.h>
22 __FBSDID("$FreeBSD$");
26 #include <sys/param.h>
28 #include <sys/mutex.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>
37 #include <sys/endian.h>
38 #include <sys/linker.h>
41 #include <net/ethernet.h>
42 #include <net/if_media.h>
44 #include <net80211/ieee80211_var.h>
45 #include <net80211/ieee80211_radiotap.h>
47 #include <dev/rtwn/if_rtwnreg.h>
48 #include <dev/rtwn/if_rtwnvar.h>
50 #include <dev/rtwn/if_rtwn_ridx.h>
51 #include <dev/rtwn/if_rtwn_tx.h>
53 #include <dev/rtwn/rtl8192c/r92c.h>
54 #include <dev/rtwn/rtl8192c/r92c_var.h>
55 #include <dev/rtwn/rtl8192c/r92c_tx_desc.h>
59 r92c_tx_get_sco(struct rtwn_softc *sc, struct ieee80211_channel *c)
61 if (IEEE80211_IS_CHAN_HT40U(c))
62 return (R92C_TXDW4_SCO_SCA);
64 return (R92C_TXDW4_SCO_SCB);
68 r92c_tx_set_ht40(struct rtwn_softc *sc, void *buf, struct ieee80211_node *ni)
70 struct r92c_tx_desc *txd = (struct r92c_tx_desc *)buf;
72 if (ni->ni_chan != IEEE80211_CHAN_ANYC &&
73 IEEE80211_IS_CHAN_HT40(ni->ni_chan)) {
76 extc_offset = r92c_tx_get_sco(sc, ni->ni_chan);
77 txd->txdw4 |= htole32(R92C_TXDW4_DATA_BW40);
78 txd->txdw4 |= htole32(SM(R92C_TXDW4_DATA_SCO, extc_offset));
83 r92c_tx_protection(struct rtwn_softc *sc, struct r92c_tx_desc *txd,
84 enum ieee80211_protmode mode, uint8_t ridx)
86 struct ieee80211com *ic = &sc->sc_ic;
90 case IEEE80211_PROT_CTSONLY:
91 txd->txdw4 |= htole32(R92C_TXDW4_CTS2SELF);
93 case IEEE80211_PROT_RTSCTS:
94 txd->txdw4 |= htole32(R92C_TXDW4_RTSEN);
100 if (mode == IEEE80211_PROT_CTSONLY ||
101 mode == IEEE80211_PROT_RTSCTS) {
102 if (ridx >= RTWN_RIDX_HT_MCS(0))
103 rate = rtwn_ctl_mcsrate(ic->ic_rt, ridx);
105 rate = ieee80211_ctl_rate(ic->ic_rt, ridx2rate[ridx]);
106 ridx = rate2ridx(IEEE80211_RV(rate));
108 txd->txdw4 |= htole32(SM(R92C_TXDW4_RTSRATE, ridx));
109 /* RTS rate fallback limit (max). */
110 txd->txdw5 |= htole32(SM(R92C_TXDW5_RTSRATE_FB_LMT, 0xf));
112 if (RTWN_RATE_IS_CCK(ridx) && ridx != RTWN_RIDX_CCK1 &&
113 (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
114 txd->txdw4 |= htole32(R92C_TXDW4_RTS_SHORT);
119 r92c_tx_raid(struct rtwn_softc *sc, struct r92c_tx_desc *txd,
120 struct ieee80211_node *ni, int ismcast)
122 struct ieee80211com *ic = &sc->sc_ic;
123 struct ieee80211vap *vap = ni->ni_vap;
124 struct ieee80211_channel *chan;
125 enum ieee80211_phymode mode;
128 chan = (ni->ni_chan != IEEE80211_CHAN_ANYC) ?
129 ni->ni_chan : ic->ic_curchan;
130 mode = ieee80211_chan2mode(chan);
132 /* NB: group addressed frames are done at 11bg rates for now */
133 if (ismcast || !(ni->ni_flags & IEEE80211_NODE_HT)) {
135 case IEEE80211_MODE_11B:
136 case IEEE80211_MODE_11G:
138 case IEEE80211_MODE_11NG:
139 mode = IEEE80211_MODE_11G;
142 device_printf(sc->sc_dev, "unknown mode(1) %d!\n",
149 case IEEE80211_MODE_11B:
150 raid = R92C_RAID_11B;
152 case IEEE80211_MODE_11G:
153 if (vap->iv_flags & IEEE80211_F_PUREG)
154 raid = R92C_RAID_11G;
156 raid = R92C_RAID_11BG;
158 case IEEE80211_MODE_11NG:
159 if (vap->iv_flags_ht & IEEE80211_FHT_PUREN)
160 raid = R92C_RAID_11N;
162 raid = R92C_RAID_11BGN;
165 device_printf(sc->sc_dev, "unknown mode(2) %d!\n", mode);
169 txd->txdw1 |= htole32(SM(R92C_TXDW1_RAID, raid));
172 /* XXX move to device-independent layer */
174 r92c_tx_set_sgi(struct rtwn_softc *sc, void *buf, struct ieee80211_node *ni)
176 struct r92c_tx_desc *txd = (struct r92c_tx_desc *)buf;
177 struct ieee80211vap *vap = ni->ni_vap;
179 if ((vap->iv_flags_ht & IEEE80211_FHT_SHORTGI20) && /* HT20 */
180 (ni->ni_htcap & IEEE80211_HTCAP_SHORTGI20))
181 txd->txdw5 |= htole32(R92C_TXDW5_SGI);
182 else if (ni->ni_chan != IEEE80211_CHAN_ANYC && /* HT40 */
183 IEEE80211_IS_CHAN_HT40(ni->ni_chan) &&
184 (ni->ni_htcap & IEEE80211_HTCAP_SHORTGI40) &&
185 (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI40))
186 txd->txdw5 |= htole32(R92C_TXDW5_SGI);
190 r92c_tx_enable_ampdu(void *buf, int enable)
192 struct r92c_tx_desc *txd = (struct r92c_tx_desc *)buf;
195 txd->txdw1 |= htole32(R92C_TXDW1_AGGEN);
197 txd->txdw1 |= htole32(R92C_TXDW1_AGGBK);
201 r92c_tx_setup_hwseq(void *buf)
203 struct r92c_tx_desc *txd = (struct r92c_tx_desc *)buf;
205 txd->txdw4 |= htole32(R92C_TXDW4_HWSEQ_EN);
209 r92c_tx_setup_macid(void *buf, int id)
211 struct r92c_tx_desc *txd = (struct r92c_tx_desc *)buf;
213 txd->txdw1 |= htole32(SM(R92C_TXDW1_MACID, id));
215 /* XXX does not belong here */
216 /* XXX temporary (I hope) */
217 /* Force CCK1 for RTS / CTS frames (driver bug) */
218 txd->txdw4 &= ~htole32(SM(R92C_TXDW4_RTSRATE, R92C_TXDW4_RTSRATE_M));
219 txd->txdw4 &= ~htole32(R92C_TXDW4_RTS_SHORT);
223 r92c_fill_tx_desc(struct rtwn_softc *sc, struct ieee80211_node *ni,
224 struct mbuf *m, void *buf, uint8_t ridx, int maxretry)
226 #ifndef RTWN_WITHOUT_UCODE
227 struct r92c_softc *rs = sc->sc_priv;
229 struct ieee80211com *ic = &sc->sc_ic;
230 struct ieee80211vap *vap = ni->ni_vap;
231 struct rtwn_vap *uvp = RTWN_VAP(vap);
232 struct ieee80211_frame *wh;
233 struct r92c_tx_desc *txd;
234 enum ieee80211_protmode prot;
235 uint8_t type, tid, qos, qsel;
236 int hasqos, ismcast, macid;
238 wh = mtod(m, struct ieee80211_frame *);
239 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
240 hasqos = IEEE80211_QOS_HAS_SEQ(wh);
241 ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1);
243 /* Select TX ring for this frame. */
245 qos = ((const struct ieee80211_qosframe *)wh)->i_qos[0];
246 tid = qos & IEEE80211_QOS_TID;
252 /* Fill Tx descriptor. */
253 txd = (struct r92c_tx_desc *)buf;
254 txd->flags0 |= R92C_FLAGS0_LSG | R92C_FLAGS0_FSG;
256 txd->flags0 |= R92C_FLAGS0_BMCAST;
259 /* Unicast frame, check if an ACK is expected. */
260 if (!qos || (qos & IEEE80211_QOS_ACKPOLICY) !=
261 IEEE80211_QOS_ACKPOLICY_NOACK) {
262 txd->txdw5 |= htole32(R92C_TXDW5_RTY_LMT_ENA);
263 txd->txdw5 |= htole32(SM(R92C_TXDW5_RTY_LMT,
267 struct rtwn_node *un = RTWN_NODE(ni);
270 if (type == IEEE80211_FC0_TYPE_DATA) {
271 qsel = tid % RTWN_MAX_TID;
273 rtwn_r92c_tx_enable_ampdu(sc, buf,
274 (m->m_flags & M_AMPDU_MPDU) != 0);
275 if (m->m_flags & M_AMPDU_MPDU) {
276 txd->txdw2 |= htole32(SM(R92C_TXDW2_AMPDU_DEN,
277 vap->iv_ampdu_density));
278 txd->txdw6 |= htole32(SM(R92C_TXDW6_MAX_AGG,
281 if (sc->sc_ratectl == RTWN_RATECTL_NET80211) {
282 txd->txdw2 |= htole32(R92C_TXDW2_CCX_RPT);
283 sc->sc_tx_n_active++;
284 #ifndef RTWN_WITHOUT_UCODE
285 rs->rs_c2h_pending++;
289 if (RTWN_RATE_IS_CCK(ridx) && ridx != RTWN_RIDX_CCK1 &&
290 (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
291 txd->txdw4 |= htole32(R92C_TXDW4_DATA_SHPRE);
293 prot = IEEE80211_PROT_NONE;
294 if (ridx >= RTWN_RIDX_HT_MCS(0)) {
295 r92c_tx_set_ht40(sc, txd, ni);
296 r92c_tx_set_sgi(sc, txd, ni);
297 prot = ic->ic_htprotmode;
298 } else if (ic->ic_flags & IEEE80211_F_USEPROT)
299 prot = ic->ic_protmode;
301 /* XXX fix last comparison for A-MSDU (in net80211) */
303 if (m->m_pkthdr.len + IEEE80211_CRC_LEN >
304 vap->iv_rtsthreshold &&
305 vap->iv_rtsthreshold != IEEE80211_RTS_MAX)
306 prot = IEEE80211_PROT_RTSCTS;
308 /* NB: checks for ht40 / short bits (set above). */
309 if (prot != IEEE80211_PROT_NONE)
310 r92c_tx_protection(sc, txd, prot, ridx);
311 } else /* IEEE80211_FC0_TYPE_MGT */
312 qsel = R92C_TXDW1_QSEL_MGNT;
314 macid = RTWN_MACID_BC;
315 qsel = R92C_TXDW1_QSEL_MGNT;
318 txd->txdw1 |= htole32(SM(R92C_TXDW1_QSEL, qsel));
320 rtwn_r92c_tx_setup_macid(sc, txd, macid);
321 txd->txdw5 |= htole32(SM(R92C_TXDW5_DATARATE, ridx));
322 /* Data rate fallback limit (max). */
323 txd->txdw5 |= htole32(SM(R92C_TXDW5_DATARATE_FB_LMT, 0x1f));
324 txd->txdw4 |= htole32(SM(R92C_TXDW4_PORT_ID, uvp->id));
325 r92c_tx_raid(sc, txd, ni, ismcast);
327 /* Force this rate if needed. */
328 if (sc->sc_ratectl != RTWN_RATECTL_FW)
329 txd->txdw4 |= htole32(R92C_TXDW4_DRVRATE);
332 /* Use HW sequence numbering for non-QoS frames. */
333 rtwn_r92c_tx_setup_hwseq(sc, txd);
334 txd->txdw4 |= htole32(SM(R92C_TXDW4_SEQ_SEL, uvp->id));
338 if (m->m_flags & M_AMPDU_MPDU) {
339 seqno = ni->ni_txseqs[tid];
340 ni->ni_txseqs[tid]++;
342 seqno = M_SEQNO_GET(m) % IEEE80211_SEQ_RANGE;
344 /* Set sequence number. */
345 txd->txdseq = htole16(seqno);
350 r92c_fill_tx_desc_raw(struct rtwn_softc *sc, struct ieee80211_node *ni,
351 struct mbuf *m, void *buf, const struct ieee80211_bpf_params *params)
353 struct ieee80211vap *vap = ni->ni_vap;
354 struct rtwn_vap *uvp = RTWN_VAP(vap);
355 struct ieee80211_frame *wh;
356 struct r92c_tx_desc *txd;
360 /* XXX TODO: 11n checks, matching r92c_fill_tx_desc() */
362 wh = mtod(m, struct ieee80211_frame *);
363 ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1);
364 ridx = rate2ridx(params->ibp_rate0);
366 /* Fill Tx descriptor. */
367 txd = (struct r92c_tx_desc *)buf;
368 txd->flags0 |= R92C_FLAGS0_LSG | R92C_FLAGS0_FSG;
370 txd->flags0 |= R92C_FLAGS0_BMCAST;
372 if ((params->ibp_flags & IEEE80211_BPF_NOACK) == 0) {
373 txd->txdw5 |= htole32(R92C_TXDW5_RTY_LMT_ENA);
374 txd->txdw5 |= htole32(SM(R92C_TXDW5_RTY_LMT,
377 if (params->ibp_flags & IEEE80211_BPF_RTS)
378 r92c_tx_protection(sc, txd, IEEE80211_PROT_RTSCTS, ridx);
379 if (params->ibp_flags & IEEE80211_BPF_CTS)
380 r92c_tx_protection(sc, txd, IEEE80211_PROT_CTSONLY, ridx);
382 rtwn_r92c_tx_setup_macid(sc, txd, RTWN_MACID_BC);
383 txd->txdw1 |= htole32(SM(R92C_TXDW1_QSEL, R92C_TXDW1_QSEL_MGNT));
385 /* Set TX rate index. */
386 txd->txdw5 |= htole32(SM(R92C_TXDW5_DATARATE, ridx));
387 txd->txdw5 |= htole32(SM(R92C_TXDW5_DATARATE_FB_LMT, 0x1f));
388 txd->txdw4 |= htole32(SM(R92C_TXDW4_PORT_ID, uvp->id));
389 txd->txdw4 |= htole32(R92C_TXDW4_DRVRATE);
390 r92c_tx_raid(sc, txd, ni, ismcast);
392 if (!IEEE80211_QOS_HAS_SEQ(wh)) {
393 /* Use HW sequence numbering for non-QoS frames. */
394 rtwn_r92c_tx_setup_hwseq(sc, txd);
395 txd->txdw4 |= htole32(SM(R92C_TXDW4_SEQ_SEL, uvp->id));
397 /* Set sequence number. */
398 txd->txdseq |= htole16(M_SEQNO_GET(m) % IEEE80211_SEQ_RANGE);
403 r92c_fill_tx_desc_null(struct rtwn_softc *sc, void *buf, int is11b,
406 struct r92c_tx_desc *txd = (struct r92c_tx_desc *)buf;
408 txd->flags0 = R92C_FLAGS0_FSG | R92C_FLAGS0_LSG | R92C_FLAGS0_OWN;
409 txd->txdw1 = htole32(
410 SM(R92C_TXDW1_QSEL, R92C_TXDW1_QSEL_MGNT));
412 txd->txdw4 = htole32(R92C_TXDW4_DRVRATE);
413 txd->txdw4 |= htole32(SM(R92C_TXDW4_PORT_ID, id));
415 txd->txdw5 = htole32(SM(R92C_TXDW5_DATARATE,
418 txd->txdw5 = htole32(SM(R92C_TXDW5_DATARATE,
423 rtwn_r92c_tx_setup_hwseq(sc, txd);
424 txd->txdw4 |= htole32(SM(R92C_TXDW4_SEQ_SEL, id));
429 r92c_tx_radiotap_flags(const void *buf)
431 const struct r92c_tx_desc *txd = buf;
435 if (txd->txdw4 & htole32(R92C_TXDW4_DATA_SHPRE))
436 flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
437 if (txd->txdw5 & htole32(R92C_TXDW5_SGI))
438 flags |= IEEE80211_RADIOTAP_F_SHORTGI;