xref: /freebsd/sys/dev/rtwn/rtl8812a/r12a_tx.c (revision b64c5a0ace59af62eff52bfe110a521dc73c937b)
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 VHT80; VHT40 */
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 VHT80; VHT40; VHT20 */
75 	if (ieee80211_ht_check_tx_ht40(ni)) {
76 		int prim_chan;
77 
78 		prim_chan = r12a_get_primary_channel(sc, ni->ni_chan);
79 		txd->txdw5 |= htole32(SM(R12A_TXDW5_DATA_BW,
80 		    R12A_TXDW5_DATA_BW40));
81 		txd->txdw5 |= htole32(SM(R12A_TXDW5_DATA_PRIM_CHAN,
82 		    prim_chan));
83 	}
84 }
85 
86 static void
87 r12a_tx_protection(struct rtwn_softc *sc, struct r12a_tx_desc *txd,
88     enum ieee80211_protmode mode, uint8_t ridx)
89 {
90 	struct ieee80211com *ic = &sc->sc_ic;
91 	uint8_t rate;
92 
93 	switch (mode) {
94 	case IEEE80211_PROT_CTSONLY:
95 		txd->txdw3 |= htole32(R12A_TXDW3_CTS2SELF);
96 		break;
97 	case IEEE80211_PROT_RTSCTS:
98 		txd->txdw3 |= htole32(R12A_TXDW3_RTSEN);
99 		break;
100 	default:
101 		break;
102 	}
103 
104 	if (mode == IEEE80211_PROT_CTSONLY ||
105 	    mode == IEEE80211_PROT_RTSCTS) {
106 		/* TODO: VHT */
107 		if (RTWN_RATE_IS_HT(ridx))
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 		case IEEE80211_MODE_VHT_5GHZ:
151 			mode = IEEE80211_MODE_VHT_5GHZ;
152 			break;
153 		default:
154 			device_printf(sc->sc_dev, "unknown mode(1) %d!\n",
155 			    ic->ic_curmode);
156 			return;
157 		}
158 	}
159 
160 	switch (mode) {
161 	case IEEE80211_MODE_11A:
162 		raid = R12A_RAID_11G;
163 		break;
164 	case IEEE80211_MODE_11B:
165 		raid = R12A_RAID_11B;
166 		break;
167 	case IEEE80211_MODE_11G:
168 		if (vap->iv_flags & IEEE80211_F_PUREG)
169 			raid = R12A_RAID_11G;
170 		else
171 			raid = R12A_RAID_11BG;
172 		break;
173 	case IEEE80211_MODE_11NA:
174 		if (sc->ntxchains == 1)
175 			raid = R12A_RAID_11GN_1;
176 		else
177 			raid = R12A_RAID_11GN_2;
178 		break;
179 	case IEEE80211_MODE_11NG:
180 		if (sc->ntxchains == 1) {
181 			if (IEEE80211_IS_CHAN_HT40(chan))
182 				raid = R12A_RAID_11BGN_1_40;
183 			else
184 				raid = R12A_RAID_11BGN_1;
185 		} else {
186 			if (IEEE80211_IS_CHAN_HT40(chan))
187 				raid = R12A_RAID_11BGN_2_40;
188 			else
189 				raid = R12A_RAID_11BGN_2;
190 		}
191 		break;
192 	case IEEE80211_MODE_VHT_5GHZ:
193 		if (sc->ntxchains == 1)
194 			raid = R12A_RAID_11AC_1;
195 		else
196 			raid = R12A_RAID_11AC_2;
197 		break;
198 	default:
199 		device_printf(sc->sc_dev, "unknown mode(2) %d!\n", mode);
200 		return;
201 	}
202 
203 	txd->txdw1 |= htole32(SM(R12A_TXDW1_RAID, raid));
204 }
205 
206 static void
207 r12a_tx_set_sgi(struct rtwn_softc *sc, void *buf, struct ieee80211_node *ni)
208 {
209 	struct r12a_tx_desc *txd = (struct r12a_tx_desc *)buf;
210 
211 	/* TODO: VHT 20/40/80 checks */
212 
213 	/*
214 	 * Only enable short-GI if we're transmitting in that
215 	 * width to that node.
216 	 *
217 	 * Specifically, do not enable shortgi for 20MHz if
218 	 * we're attempting to transmit at 40MHz.
219 	 */
220 	if (ieee80211_ht_check_tx_ht40(ni)) {
221 		if (ieee80211_ht_check_tx_shortgi_40(ni))
222 			txd->txdw5 |= htole32(R12A_TXDW5_DATA_SHORT);
223 	} else if (ieee80211_ht_check_tx_ht(ni)) {
224 		if (ieee80211_ht_check_tx_shortgi_20(ni))
225 			txd->txdw5 |= htole32(R12A_TXDW5_DATA_SHORT);
226 	}
227 }
228 
229 static void
230 r12a_tx_set_ldpc(struct rtwn_softc *sc, struct r12a_tx_desc *txd,
231     struct ieee80211_node *ni)
232 {
233 	struct ieee80211vap *vap = ni->ni_vap;
234 
235 	if ((vap->iv_flags_ht & IEEE80211_FHT_LDPC_TX) &&
236 	    (ni->ni_htcap & IEEE80211_HTCAP_LDPC))
237 		txd->txdw5 |= htole32(R12A_TXDW5_DATA_LDPC);
238 }
239 
240 static int
241 r12a_calculate_tx_agg_window(struct rtwn_softc *sc,
242     const struct ieee80211_node *ni, int tid)
243 {
244 	const struct ieee80211_tx_ampdu *tap;
245 	int wnd;
246 
247 	tap = &ni->ni_tx_ampdu[tid];
248 
249 	/*
250 	 * BAW is (MAX_AGG * 2) + 1, hence the /2 here.
251 	 * Ensure we don't send 0 or more than 64.
252 	 */
253 	wnd = tap->txa_wnd / 2;
254 	if (wnd == 0)
255 		wnd = 1;
256 	else if (wnd > 0x1f)
257 		wnd = 0x1f;
258 
259 	return (wnd);
260 }
261 
262 void
263 r12a_fill_tx_desc(struct rtwn_softc *sc, struct ieee80211_node *ni,
264     struct mbuf *m, void *buf, uint8_t ridx, int maxretry)
265 {
266 	struct ieee80211com *ic = &sc->sc_ic;
267 	struct ieee80211vap *vap = ni->ni_vap;
268 	struct rtwn_vap *uvp = RTWN_VAP(vap);
269 	struct ieee80211_frame *wh;
270 	struct r12a_tx_desc *txd;
271 	enum ieee80211_protmode prot;
272 	uint8_t type, tid, qos, qsel;
273 	int hasqos, ismcast, macid;
274 
275 	wh = mtod(m, struct ieee80211_frame *);
276 	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
277 	hasqos = IEEE80211_QOS_HAS_SEQ(wh);
278 	ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1);
279 
280 	/* Select TX ring for this frame. */
281 	if (hasqos) {
282 		qos = ((const struct ieee80211_qosframe *)wh)->i_qos[0];
283 		tid = qos & IEEE80211_QOS_TID;
284 	} else {
285 		qos = 0;
286 		tid = 0;
287 	}
288 
289 	/* Fill Tx descriptor. */
290 	txd = (struct r12a_tx_desc *)buf;
291 	txd->flags0 |= R12A_FLAGS0_LSG | R12A_FLAGS0_FSG;
292 	if (ismcast)
293 		txd->flags0 |= R12A_FLAGS0_BMCAST;
294 
295 	if (!ismcast) {
296 		/* Unicast frame, check if an ACK is expected. */
297 		if (!qos || (qos & IEEE80211_QOS_ACKPOLICY) !=
298 		    IEEE80211_QOS_ACKPOLICY_NOACK) {
299 			txd->txdw4 = htole32(R12A_TXDW4_RETRY_LMT_ENA);
300 			txd->txdw4 |= htole32(SM(R12A_TXDW4_RETRY_LMT,
301 			    maxretry));
302 		}
303 
304 		struct rtwn_node *un = RTWN_NODE(ni);
305 		macid = un->id;
306 
307 		if (type == IEEE80211_FC0_TYPE_DATA) {
308 			qsel = tid % RTWN_MAX_TID;
309 
310 			if (m->m_flags & M_AMPDU_MPDU) {
311 				txd->txdw2 |= htole32(R12A_TXDW2_AGGEN);
312 				txd->txdw2 |= htole32(SM(R12A_TXDW2_AMPDU_DEN,
313 				    ieee80211_ht_get_node_ampdu_density(ni)));
314 				txd->txdw3 |= htole32(SM(R12A_TXDW3_MAX_AGG,
315 				    r12a_calculate_tx_agg_window(sc, ni, tid)));
316 			} else
317 				txd->txdw2 |= htole32(R12A_TXDW2_AGGBK);
318 
319 			if (sc->sc_ratectl == RTWN_RATECTL_NET80211) {
320 				txd->txdw2 |= htole32(R12A_TXDW2_SPE_RPT);
321 				sc->sc_tx_n_active++;
322 			}
323 
324 			if (RTWN_RATE_IS_CCK(ridx) && ridx != RTWN_RIDX_CCK1 &&
325 			    (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
326 				txd->txdw5 |= htole32(R12A_TXDW5_DATA_SHORT);
327 
328 			prot = IEEE80211_PROT_NONE;
329 			/* TODO: VHT */
330 			if (RTWN_RATE_IS_HT(ridx)) {
331 				r12a_tx_set_ht40(sc, txd, ni);
332 				r12a_tx_set_sgi(sc, txd, ni);
333 				r12a_tx_set_ldpc(sc, txd, ni);
334 				prot = ic->ic_htprotmode;
335 			} else if (ic->ic_flags & IEEE80211_F_USEPROT)
336 				prot = ic->ic_protmode;
337 
338 			/* XXX fix last comparison for A-MSDU (in net80211) */
339 			/* XXX A-MPDU? */
340 			if (m->m_pkthdr.len + IEEE80211_CRC_LEN >
341 			    vap->iv_rtsthreshold &&
342 			    vap->iv_rtsthreshold != IEEE80211_RTS_MAX)
343 				prot = IEEE80211_PROT_RTSCTS;
344 
345 			if (prot != IEEE80211_PROT_NONE)
346 				r12a_tx_protection(sc, txd, prot, ridx);
347 		} else	/* IEEE80211_FC0_TYPE_MGT */
348 			qsel = R12A_TXDW1_QSEL_MGNT;
349 	} else {
350 		macid = RTWN_MACID_BC;
351 		qsel = R12A_TXDW1_QSEL_MGNT;
352 	}
353 
354 	txd->txdw1 |= htole32(SM(R12A_TXDW1_QSEL, qsel));
355 	txd->txdw1 |= htole32(SM(R12A_TXDW1_MACID, macid));
356 	txd->txdw4 |= htole32(SM(R12A_TXDW4_DATARATE, ridx));
357 	/* Data rate fallback limit (max). */
358 	txd->txdw4 |= htole32(SM(R12A_TXDW4_DATARATE_FB_LMT, 0x1f));
359 	/* XXX recheck for non-21au */
360 	txd->txdw6 |= htole32(SM(R21A_TXDW6_MBSSID, uvp->id));
361 	r12a_tx_raid(sc, txd, ni, ismcast);
362 
363 	/* Force this rate if needed. */
364 	if (sc->sc_ratectl != RTWN_RATECTL_FW)
365 		txd->txdw3 |= htole32(R12A_TXDW3_DRVRATE);
366 
367 	if (!hasqos) {
368 		/* Use HW sequence numbering for non-QoS frames. */
369 		txd->txdw8 |= htole32(R12A_TXDW8_HWSEQ_EN);
370 		txd->txdw3 |= htole32(SM(R12A_TXDW3_SEQ_SEL, uvp->id));
371 	} else {
372 		uint16_t seqno;
373 
374 		if (m->m_flags & M_AMPDU_MPDU) {
375 			seqno = ni->ni_txseqs[tid];
376 			ni->ni_txseqs[tid]++;
377 		} else
378 			seqno = M_SEQNO_GET(m) % IEEE80211_SEQ_RANGE;
379 
380 		/* Set sequence number. */
381 		txd->txdw9 |= htole32(SM(R12A_TXDW9_SEQ, seqno));
382 	}
383 }
384 
385 void
386 r12a_fill_tx_desc_raw(struct rtwn_softc *sc, struct ieee80211_node *ni,
387     struct mbuf *m, void *buf, const struct ieee80211_bpf_params *params)
388 {
389 	struct ieee80211vap *vap = ni->ni_vap;
390 	struct rtwn_vap *uvp = RTWN_VAP(vap);
391 	struct ieee80211_frame *wh;
392 	struct r12a_tx_desc *txd;
393 	uint8_t ridx;
394 	int ismcast;
395 
396 	/* XXX TODO: 11n checks, matching rtwn_fill_tx_desc() */
397 
398 	wh = mtod(m, struct ieee80211_frame *);
399 	ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1);
400 	ridx = rate2ridx(params->ibp_rate0);
401 
402 	/* Fill Tx descriptor. */
403 	txd = (struct r12a_tx_desc *)buf;
404 	txd->flags0 |= R12A_FLAGS0_LSG | R12A_FLAGS0_FSG;
405 	if (ismcast)
406 		txd->flags0 |= R12A_FLAGS0_BMCAST;
407 
408 	if ((params->ibp_flags & IEEE80211_BPF_NOACK) == 0) {
409 		txd->txdw4 = htole32(R12A_TXDW4_RETRY_LMT_ENA);
410 		txd->txdw4 |= htole32(SM(R12A_TXDW4_RETRY_LMT,
411 		    params->ibp_try0));
412 	}
413 	if (params->ibp_flags & IEEE80211_BPF_RTS)
414 		r12a_tx_protection(sc, txd, IEEE80211_PROT_RTSCTS, ridx);
415 	if (params->ibp_flags & IEEE80211_BPF_CTS)
416 		r12a_tx_protection(sc, txd, IEEE80211_PROT_CTSONLY, ridx);
417 
418 	txd->txdw1 |= htole32(SM(R12A_TXDW1_MACID, RTWN_MACID_BC));
419 	txd->txdw1 |= htole32(SM(R12A_TXDW1_QSEL, R12A_TXDW1_QSEL_MGNT));
420 
421 	/* Set TX rate index. */
422 	txd->txdw4 |= htole32(SM(R12A_TXDW4_DATARATE, ridx));
423 	txd->txdw4 |= htole32(SM(R12A_TXDW4_DATARATE_FB_LMT, 0x1f));
424 	txd->txdw6 |= htole32(SM(R21A_TXDW6_MBSSID, uvp->id));
425 	txd->txdw3 |= htole32(R12A_TXDW3_DRVRATE);
426 	r12a_tx_raid(sc, txd, ni, ismcast);
427 
428 	if (!IEEE80211_QOS_HAS_SEQ(wh)) {
429 		/* Use HW sequence numbering for non-QoS frames. */
430 		txd->txdw8 |= htole32(R12A_TXDW8_HWSEQ_EN);
431 		txd->txdw3 |= htole32(SM(R12A_TXDW3_SEQ_SEL, uvp->id));
432 	} else {
433 		/* Set sequence number. */
434 		txd->txdw9 |= htole32(SM(R12A_TXDW9_SEQ,
435 		    M_SEQNO_GET(m) % IEEE80211_SEQ_RANGE));
436 	}
437 }
438 
439 void
440 r12a_fill_tx_desc_null(struct rtwn_softc *sc, void *buf, int is11b, int qos,
441     int id)
442 {
443 	struct r12a_tx_desc *txd = (struct r12a_tx_desc *)buf;
444 
445 	txd->flags0 = R12A_FLAGS0_FSG | R12A_FLAGS0_LSG | R12A_FLAGS0_OWN;
446 	txd->txdw1 = htole32(
447 	    SM(R12A_TXDW1_QSEL, R12A_TXDW1_QSEL_MGNT));
448 
449 	txd->txdw3 = htole32(R12A_TXDW3_DRVRATE);
450 	txd->txdw6 = htole32(SM(R21A_TXDW6_MBSSID, id));
451 	if (is11b) {
452 		txd->txdw4 = htole32(SM(R12A_TXDW4_DATARATE,
453 		    RTWN_RIDX_CCK1));
454 	} else {
455 		txd->txdw4 = htole32(SM(R12A_TXDW4_DATARATE,
456 		    RTWN_RIDX_OFDM6));
457 	}
458 
459 	if (!qos) {
460 		txd->txdw8 = htole32(R12A_TXDW8_HWSEQ_EN);
461 		txd->txdw3 |= htole32(SM(R12A_TXDW3_SEQ_SEL, id));
462 	}
463 }
464 
465 uint8_t
466 r12a_tx_radiotap_flags(const void *buf)
467 {
468 	const struct r12a_tx_desc *txd = buf;
469 	uint8_t flags, rate;
470 
471 	if (!(txd->txdw5 & htole32(R12A_TXDW5_DATA_SHORT)))
472 		return (0);
473 
474 	rate = MS(le32toh(txd->txdw4), R12A_TXDW4_DATARATE);
475 	if (RTWN_RATE_IS_CCK(rate))
476 		flags = IEEE80211_RADIOTAP_F_SHORTPRE;
477 	else
478 		flags = IEEE80211_RADIOTAP_F_SHORTGI;
479 	return (flags);
480 }
481