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(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));
217 r92c_fill_tx_desc(struct rtwn_softc *sc, struct ieee80211_node *ni,
218 struct mbuf *m, void *buf, uint8_t ridx, int maxretry)
220 #ifndef RTWN_WITHOUT_UCODE
221 struct r92c_softc *rs = sc->sc_priv;
223 struct ieee80211com *ic = &sc->sc_ic;
224 struct ieee80211vap *vap = ni->ni_vap;
225 struct rtwn_vap *uvp = RTWN_VAP(vap);
226 struct ieee80211_frame *wh;
227 struct r92c_tx_desc *txd;
228 enum ieee80211_protmode prot;
229 uint8_t type, tid, qos, qsel;
230 int hasqos, ismcast, macid;
232 wh = mtod(m, struct ieee80211_frame *);
233 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
234 hasqos = IEEE80211_QOS_HAS_SEQ(wh);
235 ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1);
237 /* Select TX ring for this frame. */
239 qos = ((const struct ieee80211_qosframe *)wh)->i_qos[0];
240 tid = qos & IEEE80211_QOS_TID;
246 /* Fill Tx descriptor. */
247 txd = (struct r92c_tx_desc *)buf;
248 txd->flags0 |= R92C_FLAGS0_LSG | R92C_FLAGS0_FSG;
250 txd->flags0 |= R92C_FLAGS0_BMCAST;
253 /* Unicast frame, check if an ACK is expected. */
254 if (!qos || (qos & IEEE80211_QOS_ACKPOLICY) !=
255 IEEE80211_QOS_ACKPOLICY_NOACK) {
256 txd->txdw5 |= htole32(R92C_TXDW5_RTY_LMT_ENA);
257 txd->txdw5 |= htole32(SM(R92C_TXDW5_RTY_LMT,
261 struct rtwn_node *un = RTWN_NODE(ni);
264 if (type == IEEE80211_FC0_TYPE_DATA) {
265 qsel = tid % RTWN_MAX_TID;
267 rtwn_r92c_tx_enable_ampdu(sc, buf,
268 (m->m_flags & M_AMPDU_MPDU) != 0);
269 if (m->m_flags & M_AMPDU_MPDU) {
270 txd->txdw2 |= htole32(SM(R92C_TXDW2_AMPDU_DEN,
271 vap->iv_ampdu_density));
272 txd->txdw6 |= htole32(SM(R92C_TXDW6_MAX_AGG,
275 if (sc->sc_ratectl == RTWN_RATECTL_NET80211) {
276 txd->txdw2 |= htole32(R92C_TXDW2_CCX_RPT);
277 sc->sc_tx_n_active++;
278 #ifndef RTWN_WITHOUT_UCODE
279 rs->rs_c2h_pending++;
283 if (RTWN_RATE_IS_CCK(ridx) && ridx != RTWN_RIDX_CCK1 &&
284 (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
285 txd->txdw4 |= htole32(R92C_TXDW4_DATA_SHPRE);
287 prot = IEEE80211_PROT_NONE;
288 if (ridx >= RTWN_RIDX_HT_MCS(0)) {
289 r92c_tx_set_ht40(sc, txd, ni);
290 r92c_tx_set_sgi(sc, txd, ni);
291 prot = ic->ic_htprotmode;
292 } else if (ic->ic_flags & IEEE80211_F_USEPROT)
293 prot = ic->ic_protmode;
295 /* XXX fix last comparison for A-MSDU (in net80211) */
297 if (m->m_pkthdr.len + IEEE80211_CRC_LEN >
298 vap->iv_rtsthreshold &&
299 vap->iv_rtsthreshold != IEEE80211_RTS_MAX)
300 prot = IEEE80211_PROT_RTSCTS;
302 /* NB: checks for ht40 / short bits (set above). */
303 if (prot != IEEE80211_PROT_NONE)
304 r92c_tx_protection(sc, txd, prot, ridx);
305 } else /* IEEE80211_FC0_TYPE_MGT */
306 qsel = R92C_TXDW1_QSEL_MGNT;
308 macid = RTWN_MACID_BC;
309 qsel = R92C_TXDW1_QSEL_MGNT;
312 txd->txdw1 |= htole32(SM(R92C_TXDW1_QSEL, qsel));
314 rtwn_r92c_tx_setup_macid(sc, txd, macid);
315 txd->txdw5 |= htole32(SM(R92C_TXDW5_DATARATE, ridx));
316 /* Data rate fallback limit (max). */
317 txd->txdw5 |= htole32(SM(R92C_TXDW5_DATARATE_FB_LMT, 0x1f));
318 txd->txdw4 |= htole32(SM(R92C_TXDW4_PORT_ID, uvp->id));
319 r92c_tx_raid(sc, txd, ni, ismcast);
321 /* Force this rate if needed. */
322 if (sc->sc_ratectl != RTWN_RATECTL_FW)
323 txd->txdw4 |= htole32(R92C_TXDW4_DRVRATE);
326 /* Use HW sequence numbering for non-QoS frames. */
327 rtwn_r92c_tx_setup_hwseq(sc, txd);
328 txd->txdw4 |= htole32(SM(R92C_TXDW4_SEQ_SEL, uvp->id));
332 if (m->m_flags & M_AMPDU_MPDU) {
333 seqno = ni->ni_txseqs[tid];
334 ni->ni_txseqs[tid]++;
336 seqno = M_SEQNO_GET(m) % IEEE80211_SEQ_RANGE;
338 /* Set sequence number. */
339 txd->txdseq = htole16(seqno);
344 r92c_fill_tx_desc_raw(struct rtwn_softc *sc, struct ieee80211_node *ni,
345 struct mbuf *m, void *buf, const struct ieee80211_bpf_params *params)
347 struct ieee80211vap *vap = ni->ni_vap;
348 struct rtwn_vap *uvp = RTWN_VAP(vap);
349 struct ieee80211_frame *wh;
350 struct r92c_tx_desc *txd;
354 /* XXX TODO: 11n checks, matching r92c_fill_tx_desc() */
356 wh = mtod(m, struct ieee80211_frame *);
357 ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1);
358 ridx = rate2ridx(params->ibp_rate0);
360 /* Fill Tx descriptor. */
361 txd = (struct r92c_tx_desc *)buf;
362 txd->flags0 |= R92C_FLAGS0_LSG | R92C_FLAGS0_FSG;
364 txd->flags0 |= R92C_FLAGS0_BMCAST;
366 if ((params->ibp_flags & IEEE80211_BPF_NOACK) == 0) {
367 txd->txdw5 |= htole32(R92C_TXDW5_RTY_LMT_ENA);
368 txd->txdw5 |= htole32(SM(R92C_TXDW5_RTY_LMT,
371 if (params->ibp_flags & IEEE80211_BPF_RTS)
372 r92c_tx_protection(sc, txd, IEEE80211_PROT_RTSCTS, ridx);
373 if (params->ibp_flags & IEEE80211_BPF_CTS)
374 r92c_tx_protection(sc, txd, IEEE80211_PROT_CTSONLY, ridx);
376 rtwn_r92c_tx_setup_macid(sc, txd, RTWN_MACID_BC);
377 txd->txdw1 |= htole32(SM(R92C_TXDW1_QSEL, R92C_TXDW1_QSEL_MGNT));
379 /* Set TX rate index. */
380 txd->txdw5 |= htole32(SM(R92C_TXDW5_DATARATE, ridx));
381 txd->txdw5 |= htole32(SM(R92C_TXDW5_DATARATE_FB_LMT, 0x1f));
382 txd->txdw4 |= htole32(SM(R92C_TXDW4_PORT_ID, uvp->id));
383 txd->txdw4 |= htole32(R92C_TXDW4_DRVRATE);
384 r92c_tx_raid(sc, txd, ni, ismcast);
386 if (!IEEE80211_QOS_HAS_SEQ(wh)) {
387 /* Use HW sequence numbering for non-QoS frames. */
388 rtwn_r92c_tx_setup_hwseq(sc, txd);
389 txd->txdw4 |= htole32(SM(R92C_TXDW4_SEQ_SEL, uvp->id));
391 /* Set sequence number. */
392 txd->txdseq |= htole16(M_SEQNO_GET(m) % IEEE80211_SEQ_RANGE);
397 r92c_fill_tx_desc_null(struct rtwn_softc *sc, void *buf, int is11b,
400 struct r92c_tx_desc *txd = (struct r92c_tx_desc *)buf;
402 txd->flags0 = R92C_FLAGS0_FSG | R92C_FLAGS0_LSG | R92C_FLAGS0_OWN;
403 txd->txdw1 = htole32(
404 SM(R92C_TXDW1_QSEL, R92C_TXDW1_QSEL_MGNT));
406 txd->txdw4 = htole32(R92C_TXDW4_DRVRATE);
407 txd->txdw4 |= htole32(SM(R92C_TXDW4_PORT_ID, id));
409 txd->txdw5 = htole32(SM(R92C_TXDW5_DATARATE,
412 txd->txdw5 = htole32(SM(R92C_TXDW5_DATARATE,
417 rtwn_r92c_tx_setup_hwseq(sc, txd);
418 txd->txdw4 |= htole32(SM(R92C_TXDW4_SEQ_SEL, id));
423 r92c_tx_radiotap_flags(const void *buf)
425 const struct r92c_tx_desc *txd = buf;
429 if (txd->txdw4 & htole32(R92C_TXDW4_DATA_SHPRE))
430 flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
431 if (txd->txdw5 & htole32(R92C_TXDW5_SGI))
432 flags |= IEEE80211_RADIOTAP_F_SHORTGI;