xref: /freebsd/sys/dev/rtwn/rtl8812a/r12a_tx.c (revision a4e5e0106ac7145f56eb39a691e302cabb4635be)
1 /*-
2  * Copyright (c) 2016 Andriy Voskoboinyk <avos@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 #include "opt_wlan.h"
29 
30 #include <sys/param.h>
31 #include <sys/lock.h>
32 #include <sys/mutex.h>
33 #include <sys/mbuf.h>
34 #include <sys/kernel.h>
35 #include <sys/socket.h>
36 #include <sys/systm.h>
37 #include <sys/malloc.h>
38 #include <sys/queue.h>
39 #include <sys/taskqueue.h>
40 #include <sys/bus.h>
41 #include <sys/endian.h>
42 #include <sys/linker.h>
43 
44 #include <net/if.h>
45 #include <net/ethernet.h>
46 #include <net/if_media.h>
47 
48 #include <net80211/ieee80211_var.h>
49 #include <net80211/ieee80211_radiotap.h>
50 
51 #include <dev/rtwn/if_rtwnreg.h>
52 #include <dev/rtwn/if_rtwnvar.h>
53 
54 #include <dev/rtwn/if_rtwn_ridx.h>
55 
56 #include <dev/rtwn/rtl8812a/r12a.h>
57 #include <dev/rtwn/rtl8812a/r12a_tx_desc.h>
58 
59 static int
60 r12a_get_primary_channel(struct rtwn_softc *sc, struct ieee80211_channel *c)
61 {
62 	/* XXX 80 MHz */
63 	if (IEEE80211_IS_CHAN_HT40U(c))
64 		return (R12A_TXDW5_PRIM_CHAN_20_80_2);
65 	else
66 		return (R12A_TXDW5_PRIM_CHAN_20_80_3);
67 }
68 
69 static void
70 r12a_tx_set_ht40(struct rtwn_softc *sc, void *buf, struct ieee80211_node *ni)
71 {
72 	struct r12a_tx_desc *txd = (struct r12a_tx_desc *)buf;
73 
74 	/* XXX 80 Mhz */
75 	if (ni->ni_chan != IEEE80211_CHAN_ANYC &&
76 	    IEEE80211_IS_CHAN_HT40(ni->ni_chan)) {
77 		int prim_chan;
78 
79 		prim_chan = r12a_get_primary_channel(sc, ni->ni_chan);
80 		txd->txdw5 |= htole32(SM(R12A_TXDW5_DATA_BW,
81 		    R12A_TXDW5_DATA_BW40));
82 		txd->txdw5 |= htole32(SM(R12A_TXDW5_DATA_PRIM_CHAN,
83 		    prim_chan));
84 	}
85 }
86 
87 static void
88 r12a_tx_protection(struct rtwn_softc *sc, struct r12a_tx_desc *txd,
89     enum ieee80211_protmode mode, uint8_t ridx)
90 {
91 	struct ieee80211com *ic = &sc->sc_ic;
92 	uint8_t rate;
93 
94 	switch (mode) {
95 	case IEEE80211_PROT_CTSONLY:
96 		txd->txdw3 |= htole32(R12A_TXDW3_CTS2SELF);
97 		break;
98 	case IEEE80211_PROT_RTSCTS:
99 		txd->txdw3 |= htole32(R12A_TXDW3_RTSEN);
100 		break;
101 	default:
102 		break;
103 	}
104 
105 	if (mode == IEEE80211_PROT_CTSONLY ||
106 	    mode == IEEE80211_PROT_RTSCTS) {
107 		if (ridx >= RTWN_RIDX_HT_MCS(0))
108 			rate = rtwn_ctl_mcsrate(ic->ic_rt, ridx);
109 		else
110 			rate = ieee80211_ctl_rate(ic->ic_rt, ridx2rate[ridx]);
111 		ridx = rate2ridx(IEEE80211_RV(rate));
112 
113 		txd->txdw4 |= htole32(SM(R12A_TXDW4_RTSRATE, ridx));
114 		/* RTS rate fallback limit (max). */
115 		txd->txdw4 |= htole32(SM(R12A_TXDW4_RTSRATE_FB_LMT, 0xf));
116 
117 		if (RTWN_RATE_IS_CCK(ridx) && ridx != RTWN_RIDX_CCK1 &&
118 		    (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
119 			txd->txdw5 |= htole32(R12A_TXDW5_RTS_SHORT);
120 	}
121 }
122 
123 static void
124 r12a_tx_raid(struct rtwn_softc *sc, struct r12a_tx_desc *txd,
125     struct ieee80211_node *ni, int ismcast)
126 {
127 	struct ieee80211com *ic = &sc->sc_ic;
128 	struct ieee80211vap *vap = ni->ni_vap;
129 	struct ieee80211_channel *chan;
130 	enum ieee80211_phymode mode;
131 	uint8_t raid;
132 
133 	chan = (ni->ni_chan != IEEE80211_CHAN_ANYC) ?
134 		ni->ni_chan : ic->ic_curchan;
135 	mode = ieee80211_chan2mode(chan);
136 
137 	/* NB: group addressed frames are done at 11bg rates for now */
138 	if (ismcast || !(ni->ni_flags & IEEE80211_NODE_HT)) {
139 		switch (mode) {
140 		case IEEE80211_MODE_11A:
141 		case IEEE80211_MODE_11B:
142 		case IEEE80211_MODE_11G:
143 			break;
144 		case IEEE80211_MODE_11NA:
145 			mode = IEEE80211_MODE_11A;
146 			break;
147 		case IEEE80211_MODE_11NG:
148 			mode = IEEE80211_MODE_11G;
149 			break;
150 		default:
151 			device_printf(sc->sc_dev, "unknown mode(1) %d!\n",
152 			    ic->ic_curmode);
153 			return;
154 		}
155 	}
156 
157 	switch (mode) {
158 	case IEEE80211_MODE_11A:
159 		raid = R12A_RAID_11G;
160 		break;
161 	case IEEE80211_MODE_11B:
162 		raid = R12A_RAID_11B;
163 		break;
164 	case IEEE80211_MODE_11G:
165 		if (vap->iv_flags & IEEE80211_F_PUREG)
166 			raid = R12A_RAID_11G;
167 		else
168 			raid = R12A_RAID_11BG;
169 		break;
170 	case IEEE80211_MODE_11NA:
171 		if (sc->ntxchains == 1)
172 			raid = R12A_RAID_11GN_1;
173 		else
174 			raid = R12A_RAID_11GN_2;
175 		break;
176 	case IEEE80211_MODE_11NG:
177 		if (sc->ntxchains == 1) {
178 			if (IEEE80211_IS_CHAN_HT40(chan))
179 				raid = R12A_RAID_11BGN_1_40;
180 			else
181 				raid = R12A_RAID_11BGN_1;
182 		} else {
183 			if (IEEE80211_IS_CHAN_HT40(chan))
184 				raid = R12A_RAID_11BGN_2_40;
185 			else
186 				raid = R12A_RAID_11BGN_2;
187 		}
188 		break;
189 	default:
190 		/* TODO: 80 MHz / 11ac */
191 		device_printf(sc->sc_dev, "unknown mode(2) %d!\n", mode);
192 		return;
193 	}
194 
195 	txd->txdw1 |= htole32(SM(R12A_TXDW1_RAID, raid));
196 }
197 
198 static void
199 r12a_tx_set_sgi(struct rtwn_softc *sc, void *buf, struct ieee80211_node *ni)
200 {
201 	struct r12a_tx_desc *txd = (struct r12a_tx_desc *)buf;
202 	struct ieee80211vap *vap = ni->ni_vap;
203 
204 	if ((vap->iv_flags_ht & IEEE80211_FHT_SHORTGI20) &&	/* HT20 */
205 	    (ni->ni_htcap & IEEE80211_HTCAP_SHORTGI20))
206 		txd->txdw5 |= htole32(R12A_TXDW5_DATA_SHORT);
207 	else if (ni->ni_chan != IEEE80211_CHAN_ANYC &&		/* HT40 */
208 	    IEEE80211_IS_CHAN_HT40(ni->ni_chan) &&
209 	    (ni->ni_htcap & IEEE80211_HTCAP_SHORTGI40) &&
210 	    (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI40))
211 		txd->txdw5 |= htole32(R12A_TXDW5_DATA_SHORT);
212 }
213 
214 static void
215 r12a_tx_set_ldpc(struct rtwn_softc *sc, struct r12a_tx_desc *txd,
216     struct ieee80211_node *ni)
217 {
218 	struct ieee80211vap *vap = ni->ni_vap;
219 
220 	if ((vap->iv_flags_ht & IEEE80211_FHT_LDPC_TX) &&
221 	    (ni->ni_htcap & IEEE80211_HTCAP_LDPC))
222 		txd->txdw5 |= htole32(R12A_TXDW5_DATA_LDPC);
223 }
224 
225 void
226 r12a_fill_tx_desc(struct rtwn_softc *sc, struct ieee80211_node *ni,
227     struct mbuf *m, void *buf, uint8_t ridx, int maxretry)
228 {
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 r12a_tx_desc *txd;
234 	enum ieee80211_protmode prot;
235 	uint8_t type, tid, qos, qsel;
236 	int hasqos, ismcast, macid;
237 
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);
242 
243 	/* Select TX ring for this frame. */
244 	if (hasqos) {
245 		qos = ((const struct ieee80211_qosframe *)wh)->i_qos[0];
246 		tid = qos & IEEE80211_QOS_TID;
247 	} else {
248 		qos = 0;
249 		tid = 0;
250 	}
251 
252 	/* Fill Tx descriptor. */
253 	txd = (struct r12a_tx_desc *)buf;
254 	txd->flags0 |= R12A_FLAGS0_LSG | R12A_FLAGS0_FSG;
255 	if (ismcast)
256 		txd->flags0 |= R12A_FLAGS0_BMCAST;
257 
258 	if (!ismcast) {
259 		/* Unicast frame, check if an ACK is expected. */
260 		if (!qos || (qos & IEEE80211_QOS_ACKPOLICY) !=
261 		    IEEE80211_QOS_ACKPOLICY_NOACK) {
262 			txd->txdw4 = htole32(R12A_TXDW4_RETRY_LMT_ENA);
263 			txd->txdw4 |= htole32(SM(R12A_TXDW4_RETRY_LMT,
264 			    maxretry));
265 		}
266 
267 		struct rtwn_node *un = RTWN_NODE(ni);
268 		macid = un->id;
269 
270 		if (type == IEEE80211_FC0_TYPE_DATA) {
271 			qsel = tid % RTWN_MAX_TID;
272 
273 			if (m->m_flags & M_AMPDU_MPDU) {
274 				txd->txdw2 |= htole32(R12A_TXDW2_AGGEN);
275 				txd->txdw2 |= htole32(SM(R12A_TXDW2_AMPDU_DEN,
276 				    vap->iv_ampdu_density));
277 				txd->txdw3 |= htole32(SM(R12A_TXDW3_MAX_AGG,
278 				    0x1f));	/* XXX */
279 			} else
280 				txd->txdw2 |= htole32(R12A_TXDW2_AGGBK);
281 
282 			if (sc->sc_ratectl == RTWN_RATECTL_NET80211) {
283 				txd->txdw2 |= htole32(R12A_TXDW2_SPE_RPT);
284 				sc->sc_tx_n_active++;
285 			}
286 
287 			if (RTWN_RATE_IS_CCK(ridx) && ridx != RTWN_RIDX_CCK1 &&
288 			    (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
289 				txd->txdw5 |= htole32(R12A_TXDW5_DATA_SHORT);
290 
291 			prot = IEEE80211_PROT_NONE;
292 			if (ridx >= RTWN_RIDX_HT_MCS(0)) {
293 				r12a_tx_set_ht40(sc, txd, ni);
294 				r12a_tx_set_sgi(sc, txd, ni);
295 				r12a_tx_set_ldpc(sc, txd, ni);
296 				prot = ic->ic_htprotmode;
297 			} else if (ic->ic_flags & IEEE80211_F_USEPROT)
298 				prot = ic->ic_protmode;
299 
300 			/* XXX fix last comparison for A-MSDU (in net80211) */
301 			/* XXX A-MPDU? */
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;
306 
307 			if (prot != IEEE80211_PROT_NONE)
308 				r12a_tx_protection(sc, txd, prot, ridx);
309 		} else	/* IEEE80211_FC0_TYPE_MGT */
310 			qsel = R12A_TXDW1_QSEL_MGNT;
311 	} else {
312 		macid = RTWN_MACID_BC;
313 		qsel = R12A_TXDW1_QSEL_MGNT;
314 	}
315 
316 	txd->txdw1 |= htole32(SM(R12A_TXDW1_QSEL, qsel));
317 	txd->txdw1 |= htole32(SM(R12A_TXDW1_MACID, macid));
318 	txd->txdw4 |= htole32(SM(R12A_TXDW4_DATARATE, ridx));
319 	/* Data rate fallback limit (max). */
320 	txd->txdw4 |= htole32(SM(R12A_TXDW4_DATARATE_FB_LMT, 0x1f));
321 	/* XXX recheck for non-21au */
322 	txd->txdw6 |= htole32(SM(R21A_TXDW6_MBSSID, uvp->id));
323 	r12a_tx_raid(sc, txd, ni, ismcast);
324 
325 	/* Force this rate if needed. */
326 	if (sc->sc_ratectl != RTWN_RATECTL_FW)
327 		txd->txdw3 |= htole32(R12A_TXDW3_DRVRATE);
328 
329 	if (!hasqos) {
330 		/* Use HW sequence numbering for non-QoS frames. */
331 		txd->txdw8 |= htole32(R12A_TXDW8_HWSEQ_EN);
332 		txd->txdw3 |= htole32(SM(R12A_TXDW3_SEQ_SEL, uvp->id));
333 	} else {
334 		uint16_t seqno;
335 
336 		if (m->m_flags & M_AMPDU_MPDU) {
337 			seqno = ni->ni_txseqs[tid];
338 			ni->ni_txseqs[tid]++;
339 		} else
340 			seqno = M_SEQNO_GET(m) % IEEE80211_SEQ_RANGE;
341 
342 		/* Set sequence number. */
343 		txd->txdw9 |= htole32(SM(R12A_TXDW9_SEQ, seqno));
344 	}
345 }
346 
347 void
348 r12a_fill_tx_desc_raw(struct rtwn_softc *sc, struct ieee80211_node *ni,
349     struct mbuf *m, void *buf, const struct ieee80211_bpf_params *params)
350 {
351 	struct ieee80211vap *vap = ni->ni_vap;
352 	struct rtwn_vap *uvp = RTWN_VAP(vap);
353 	struct ieee80211_frame *wh;
354 	struct r12a_tx_desc *txd;
355 	uint8_t ridx;
356 	int ismcast;
357 
358 	/* XXX TODO: 11n checks, matching rtwn_fill_tx_desc() */
359 
360 	wh = mtod(m, struct ieee80211_frame *);
361 	ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1);
362 	ridx = rate2ridx(params->ibp_rate0);
363 
364 	/* Fill Tx descriptor. */
365 	txd = (struct r12a_tx_desc *)buf;
366 	txd->flags0 |= R12A_FLAGS0_LSG | R12A_FLAGS0_FSG;
367 	if (ismcast)
368 		txd->flags0 |= R12A_FLAGS0_BMCAST;
369 
370 	if ((params->ibp_flags & IEEE80211_BPF_NOACK) == 0) {
371 		txd->txdw4 = htole32(R12A_TXDW4_RETRY_LMT_ENA);
372 		txd->txdw4 |= htole32(SM(R12A_TXDW4_RETRY_LMT,
373 		    params->ibp_try0));
374 	}
375 	if (params->ibp_flags & IEEE80211_BPF_RTS)
376 		r12a_tx_protection(sc, txd, IEEE80211_PROT_RTSCTS, ridx);
377 	if (params->ibp_flags & IEEE80211_BPF_CTS)
378 		r12a_tx_protection(sc, txd, IEEE80211_PROT_CTSONLY, ridx);
379 
380 	txd->txdw1 |= htole32(SM(R12A_TXDW1_MACID, RTWN_MACID_BC));
381 	txd->txdw1 |= htole32(SM(R12A_TXDW1_QSEL, R12A_TXDW1_QSEL_MGNT));
382 
383 	/* Set TX rate index. */
384 	txd->txdw4 |= htole32(SM(R12A_TXDW4_DATARATE, ridx));
385 	txd->txdw4 |= htole32(SM(R12A_TXDW4_DATARATE_FB_LMT, 0x1f));
386 	txd->txdw6 |= htole32(SM(R21A_TXDW6_MBSSID, uvp->id));
387 	txd->txdw3 |= htole32(R12A_TXDW3_DRVRATE);
388 	r12a_tx_raid(sc, txd, ni, ismcast);
389 
390 	if (!IEEE80211_QOS_HAS_SEQ(wh)) {
391 		/* Use HW sequence numbering for non-QoS frames. */
392 		txd->txdw8 |= htole32(R12A_TXDW8_HWSEQ_EN);
393 		txd->txdw3 |= htole32(SM(R12A_TXDW3_SEQ_SEL, uvp->id));
394 	} else {
395 		/* Set sequence number. */
396 		txd->txdw9 |= htole32(SM(R12A_TXDW9_SEQ,
397 		    M_SEQNO_GET(m) % IEEE80211_SEQ_RANGE));
398 	}
399 }
400 
401 void
402 r12a_fill_tx_desc_null(struct rtwn_softc *sc, void *buf, int is11b, int qos,
403     int id)
404 {
405 	struct r12a_tx_desc *txd = (struct r12a_tx_desc *)buf;
406 
407 	txd->flags0 = R12A_FLAGS0_FSG | R12A_FLAGS0_LSG | R12A_FLAGS0_OWN;
408 	txd->txdw1 = htole32(
409 	    SM(R12A_TXDW1_QSEL, R12A_TXDW1_QSEL_MGNT));
410 
411 	txd->txdw3 = htole32(R12A_TXDW3_DRVRATE);
412 	txd->txdw6 = htole32(SM(R21A_TXDW6_MBSSID, id));
413 	if (is11b) {
414 		txd->txdw4 = htole32(SM(R12A_TXDW4_DATARATE,
415 		    RTWN_RIDX_CCK1));
416 	} else {
417 		txd->txdw4 = htole32(SM(R12A_TXDW4_DATARATE,
418 		    RTWN_RIDX_OFDM6));
419 	}
420 
421 	if (!qos) {
422 		txd->txdw8 = htole32(R12A_TXDW8_HWSEQ_EN);
423 		txd->txdw3 |= htole32(SM(R12A_TXDW3_SEQ_SEL, id));
424 	}
425 }
426 
427 uint8_t
428 r12a_tx_radiotap_flags(const void *buf)
429 {
430 	const struct r12a_tx_desc *txd = buf;
431 	uint8_t flags, rate;
432 
433 	if (!(txd->txdw5 & htole32(R12A_TXDW5_DATA_SHORT)))
434 		return (0);
435 
436 	rate = MS(le32toh(txd->txdw4), R12A_TXDW4_DATARATE);
437 	if (RTWN_RATE_IS_CCK(rate))
438 		flags = IEEE80211_RADIOTAP_F_SHORTPRE;
439 	else
440 		flags = IEEE80211_RADIOTAP_F_SHORTGI;
441 	return (flags);
442 }
443