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>
58 r92c_tx_get_sco(struct rtwn_softc *sc, struct ieee80211_channel *c)
60 if (IEEE80211_IS_CHAN_HT40U(c))
61 return (R92C_TXDW4_SCO_SCA);
63 return (R92C_TXDW4_SCO_SCB);
67 r92c_tx_set_ht40(struct rtwn_softc *sc, void *buf, struct ieee80211_node *ni)
69 struct r92c_tx_desc *txd = (struct r92c_tx_desc *)buf;
71 if (ni->ni_chan != IEEE80211_CHAN_ANYC &&
72 IEEE80211_IS_CHAN_HT40(ni->ni_chan)) {
75 extc_offset = r92c_tx_get_sco(sc, ni->ni_chan);
76 txd->txdw4 |= htole32(R92C_TXDW4_DATA_BW40);
77 txd->txdw4 |= htole32(SM(R92C_TXDW4_DATA_SCO, extc_offset));
82 r92c_tx_protection(struct rtwn_softc *sc, struct r92c_tx_desc *txd,
83 enum ieee80211_protmode mode, uint8_t ridx)
85 struct ieee80211com *ic = &sc->sc_ic;
89 case IEEE80211_PROT_CTSONLY:
90 txd->txdw4 |= htole32(R92C_TXDW4_CTS2SELF);
92 case IEEE80211_PROT_RTSCTS:
93 txd->txdw4 |= htole32(R92C_TXDW4_RTSEN);
99 if (mode == IEEE80211_PROT_CTSONLY ||
100 mode == IEEE80211_PROT_RTSCTS) {
101 if (ridx >= RTWN_RIDX_HT_MCS(0))
102 rate = rtwn_ctl_mcsrate(ic->ic_rt, ridx);
104 rate = ieee80211_ctl_rate(ic->ic_rt, ridx2rate[ridx]);
105 ridx = rate2ridx(IEEE80211_RV(rate));
107 txd->txdw4 |= htole32(SM(R92C_TXDW4_RTSRATE, ridx));
108 /* RTS rate fallback limit (max). */
109 txd->txdw5 |= htole32(SM(R92C_TXDW5_RTSRATE_FB_LMT, 0xf));
111 if (RTWN_RATE_IS_CCK(ridx) && ridx != RTWN_RIDX_CCK1 &&
112 (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
113 txd->txdw4 |= htole32(R92C_TXDW4_RTS_SHORT);
118 r92c_tx_raid(struct rtwn_softc *sc, struct r92c_tx_desc *txd,
119 struct ieee80211_node *ni, int ismcast)
121 struct ieee80211com *ic = &sc->sc_ic;
122 struct ieee80211vap *vap = ni->ni_vap;
123 struct ieee80211_channel *chan;
124 enum ieee80211_phymode mode;
127 chan = (ni->ni_chan != IEEE80211_CHAN_ANYC) ?
128 ni->ni_chan : ic->ic_curchan;
129 mode = ieee80211_chan2mode(chan);
131 /* NB: group addressed frames are done at 11bg rates for now */
132 if (ismcast || !(ni->ni_flags & IEEE80211_NODE_HT)) {
134 case IEEE80211_MODE_11B:
135 case IEEE80211_MODE_11G:
137 case IEEE80211_MODE_11NG:
138 mode = IEEE80211_MODE_11G;
141 device_printf(sc->sc_dev, "unknown mode(1) %d!\n",
148 case IEEE80211_MODE_11B:
149 raid = R92C_RAID_11B;
151 case IEEE80211_MODE_11G:
152 if (vap->iv_flags & IEEE80211_F_PUREG)
153 raid = R92C_RAID_11G;
155 raid = R92C_RAID_11BG;
157 case IEEE80211_MODE_11NG:
158 if (vap->iv_flags_ht & IEEE80211_FHT_PUREN)
159 raid = R92C_RAID_11N;
161 raid = R92C_RAID_11BGN;
164 device_printf(sc->sc_dev, "unknown mode(2) %d!\n", mode);
168 txd->txdw1 |= htole32(SM(R92C_TXDW1_RAID, raid));
171 /* XXX move to device-independent layer */
173 r92c_tx_set_sgi(struct rtwn_softc *sc, void *buf, struct ieee80211_node *ni)
175 struct r92c_tx_desc *txd = (struct r92c_tx_desc *)buf;
176 struct ieee80211vap *vap = ni->ni_vap;
178 if ((vap->iv_flags_ht & IEEE80211_FHT_SHORTGI20) && /* HT20 */
179 (ni->ni_htcap & IEEE80211_HTCAP_SHORTGI20))
180 txd->txdw5 |= htole32(R92C_TXDW5_SGI);
181 else if (ni->ni_chan != IEEE80211_CHAN_ANYC && /* HT40 */
182 IEEE80211_IS_CHAN_HT40(ni->ni_chan) &&
183 (ni->ni_htcap & IEEE80211_HTCAP_SHORTGI40) &&
184 (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI40))
185 txd->txdw5 |= htole32(R92C_TXDW5_SGI);
189 r92c_tx_enable_ampdu(void *buf, int enable)
191 struct r92c_tx_desc *txd = (struct r92c_tx_desc *)buf;
194 txd->txdw1 |= htole32(R92C_TXDW1_AGGEN);
196 txd->txdw1 |= htole32(R92C_TXDW1_AGGBK);
200 r92c_tx_setup_hwseq(void *buf)
202 struct r92c_tx_desc *txd = (struct r92c_tx_desc *)buf;
204 txd->txdw4 |= htole32(R92C_TXDW4_HWSEQ_EN);
208 r92c_tx_setup_macid(void *buf, int id)
210 struct r92c_tx_desc *txd = (struct r92c_tx_desc *)buf;
212 txd->txdw1 |= htole32(SM(R92C_TXDW1_MACID, id));
214 /* XXX does not belong here */
215 /* XXX temporary (I hope) */
216 /* Force CCK1 for RTS / CTS frames (driver bug) */
217 txd->txdw4 &= ~htole32(SM(R92C_TXDW4_RTSRATE, R92C_TXDW4_RTSRATE_M));
218 txd->txdw4 &= ~htole32(R92C_TXDW4_RTS_SHORT);
222 r92c_fill_tx_desc(struct rtwn_softc *sc, struct ieee80211_node *ni,
223 struct mbuf *m, void *buf, uint8_t ridx, int maxretry)
225 #ifndef RTWN_WITHOUT_UCODE
226 struct r92c_softc *rs = sc->sc_priv;
228 struct ieee80211com *ic = &sc->sc_ic;
229 struct ieee80211vap *vap = ni->ni_vap;
230 struct rtwn_vap *uvp = RTWN_VAP(vap);
231 struct ieee80211_frame *wh;
232 struct r92c_tx_desc *txd;
233 enum ieee80211_protmode prot;
234 uint8_t type, tid, qos, qsel;
235 int hasqos, ismcast, macid;
237 wh = mtod(m, struct ieee80211_frame *);
238 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
239 hasqos = IEEE80211_QOS_HAS_SEQ(wh);
240 ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1);
242 /* Select TX ring for this frame. */
244 qos = ((const struct ieee80211_qosframe *)wh)->i_qos[0];
245 tid = qos & IEEE80211_QOS_TID;
251 /* Fill Tx descriptor. */
252 txd = (struct r92c_tx_desc *)buf;
253 txd->flags0 |= R92C_FLAGS0_LSG | R92C_FLAGS0_FSG;
255 txd->flags0 |= R92C_FLAGS0_BMCAST;
258 /* Unicast frame, check if an ACK is expected. */
259 if (!qos || (qos & IEEE80211_QOS_ACKPOLICY) !=
260 IEEE80211_QOS_ACKPOLICY_NOACK) {
261 txd->txdw5 |= htole32(R92C_TXDW5_RTY_LMT_ENA);
262 txd->txdw5 |= htole32(SM(R92C_TXDW5_RTY_LMT,
266 struct rtwn_node *un = RTWN_NODE(ni);
269 if (type == IEEE80211_FC0_TYPE_DATA) {
270 qsel = tid % RTWN_MAX_TID;
272 rtwn_r92c_tx_enable_ampdu(sc, buf,
273 (m->m_flags & M_AMPDU_MPDU) != 0);
274 if (m->m_flags & M_AMPDU_MPDU) {
275 txd->txdw2 |= htole32(SM(R92C_TXDW2_AMPDU_DEN,
276 vap->iv_ampdu_density));
277 txd->txdw6 |= htole32(SM(R92C_TXDW6_MAX_AGG,
280 if (sc->sc_ratectl == RTWN_RATECTL_NET80211) {
281 txd->txdw2 |= htole32(R92C_TXDW2_CCX_RPT);
282 sc->sc_tx_n_active++;
283 #ifndef RTWN_WITHOUT_UCODE
284 rs->rs_c2h_pending++;
288 if (RTWN_RATE_IS_CCK(ridx) && ridx != RTWN_RIDX_CCK1 &&
289 (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
290 txd->txdw4 |= htole32(R92C_TXDW4_DATA_SHPRE);
292 prot = IEEE80211_PROT_NONE;
293 if (ridx >= RTWN_RIDX_HT_MCS(0)) {
294 r92c_tx_set_ht40(sc, txd, ni);
295 r92c_tx_set_sgi(sc, txd, ni);
296 prot = ic->ic_htprotmode;
297 } else if (ic->ic_flags & IEEE80211_F_USEPROT)
298 prot = ic->ic_protmode;
300 /* XXX fix last comparison for A-MSDU (in net80211) */
302 if (m->m_pkthdr.len + IEEE80211_CRC_LEN >
303 vap->iv_rtsthreshold &&
304 vap->iv_rtsthreshold != IEEE80211_RTS_MAX)
305 prot = IEEE80211_PROT_RTSCTS;
307 /* NB: checks for ht40 / short bits (set above). */
308 if (prot != IEEE80211_PROT_NONE)
309 r92c_tx_protection(sc, txd, prot, ridx);
310 } else /* IEEE80211_FC0_TYPE_MGT */
311 qsel = R92C_TXDW1_QSEL_MGNT;
313 macid = RTWN_MACID_BC;
314 qsel = R92C_TXDW1_QSEL_MGNT;
317 txd->txdw1 |= htole32(SM(R92C_TXDW1_QSEL, qsel));
319 rtwn_r92c_tx_setup_macid(sc, txd, macid);
320 txd->txdw5 |= htole32(SM(R92C_TXDW5_DATARATE, ridx));
321 /* Data rate fallback limit (max). */
322 txd->txdw5 |= htole32(SM(R92C_TXDW5_DATARATE_FB_LMT, 0x1f));
323 txd->txdw4 |= htole32(SM(R92C_TXDW4_PORT_ID, uvp->id));
324 r92c_tx_raid(sc, txd, ni, ismcast);
326 /* Force this rate if needed. */
327 if (sc->sc_ratectl != RTWN_RATECTL_FW)
328 txd->txdw4 |= htole32(R92C_TXDW4_DRVRATE);
331 /* Use HW sequence numbering for non-QoS frames. */
332 rtwn_r92c_tx_setup_hwseq(sc, txd);
333 txd->txdw4 |= htole32(SM(R92C_TXDW4_SEQ_SEL, uvp->id));
337 if (m->m_flags & M_AMPDU_MPDU) {
338 seqno = ni->ni_txseqs[tid];
339 ni->ni_txseqs[tid]++;
341 seqno = M_SEQNO_GET(m) % IEEE80211_SEQ_RANGE;
343 /* Set sequence number. */
344 txd->txdseq = htole16(seqno);
349 r92c_fill_tx_desc_raw(struct rtwn_softc *sc, struct ieee80211_node *ni,
350 struct mbuf *m, void *buf, const struct ieee80211_bpf_params *params)
352 struct ieee80211vap *vap = ni->ni_vap;
353 struct rtwn_vap *uvp = RTWN_VAP(vap);
354 struct ieee80211_frame *wh;
355 struct r92c_tx_desc *txd;
359 /* XXX TODO: 11n checks, matching r92c_fill_tx_desc() */
361 wh = mtod(m, struct ieee80211_frame *);
362 ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1);
363 ridx = rate2ridx(params->ibp_rate0);
365 /* Fill Tx descriptor. */
366 txd = (struct r92c_tx_desc *)buf;
367 txd->flags0 |= R92C_FLAGS0_LSG | R92C_FLAGS0_FSG;
369 txd->flags0 |= R92C_FLAGS0_BMCAST;
371 if ((params->ibp_flags & IEEE80211_BPF_NOACK) == 0) {
372 txd->txdw5 |= htole32(R92C_TXDW5_RTY_LMT_ENA);
373 txd->txdw5 |= htole32(SM(R92C_TXDW5_RTY_LMT,
376 if (params->ibp_flags & IEEE80211_BPF_RTS)
377 r92c_tx_protection(sc, txd, IEEE80211_PROT_RTSCTS, ridx);
378 if (params->ibp_flags & IEEE80211_BPF_CTS)
379 r92c_tx_protection(sc, txd, IEEE80211_PROT_CTSONLY, ridx);
381 rtwn_r92c_tx_setup_macid(sc, txd, RTWN_MACID_BC);
382 txd->txdw1 |= htole32(SM(R92C_TXDW1_QSEL, R92C_TXDW1_QSEL_MGNT));
384 /* Set TX rate index. */
385 txd->txdw5 |= htole32(SM(R92C_TXDW5_DATARATE, ridx));
386 txd->txdw5 |= htole32(SM(R92C_TXDW5_DATARATE_FB_LMT, 0x1f));
387 txd->txdw4 |= htole32(SM(R92C_TXDW4_PORT_ID, uvp->id));
388 txd->txdw4 |= htole32(R92C_TXDW4_DRVRATE);
389 r92c_tx_raid(sc, txd, ni, ismcast);
391 if (!IEEE80211_QOS_HAS_SEQ(wh)) {
392 /* Use HW sequence numbering for non-QoS frames. */
393 rtwn_r92c_tx_setup_hwseq(sc, txd);
394 txd->txdw4 |= htole32(SM(R92C_TXDW4_SEQ_SEL, uvp->id));
396 /* Set sequence number. */
397 txd->txdseq |= htole16(M_SEQNO_GET(m) % IEEE80211_SEQ_RANGE);
402 r92c_fill_tx_desc_null(struct rtwn_softc *sc, void *buf, int is11b,
405 struct r92c_tx_desc *txd = (struct r92c_tx_desc *)buf;
407 txd->flags0 = R92C_FLAGS0_FSG | R92C_FLAGS0_LSG | R92C_FLAGS0_OWN;
408 txd->txdw1 = htole32(
409 SM(R92C_TXDW1_QSEL, R92C_TXDW1_QSEL_MGNT));
411 txd->txdw4 = htole32(R92C_TXDW4_DRVRATE);
412 txd->txdw4 |= htole32(SM(R92C_TXDW4_PORT_ID, id));
414 txd->txdw5 = htole32(SM(R92C_TXDW5_DATARATE,
417 txd->txdw5 = htole32(SM(R92C_TXDW5_DATARATE,
422 rtwn_r92c_tx_setup_hwseq(sc, txd);
423 txd->txdw4 |= htole32(SM(R92C_TXDW4_SEQ_SEL, id));
428 r92c_tx_radiotap_flags(const void *buf)
430 const struct r92c_tx_desc *txd = buf;
434 if (txd->txdw4 & htole32(R92C_TXDW4_DATA_SHPRE))
435 flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
436 if (txd->txdw5 & htole32(R92C_TXDW5_SGI))
437 flags |= IEEE80211_RADIOTAP_F_SHORTGI;