xref: /freebsd/sys/dev/rtwn/rtl8812a/r12a_tx.c (revision 076ad2f836d5f49dc1375f1677335a48fe0d4b82)
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 __FBSDID("$FreeBSD$");
29 
30 #include "opt_wlan.h"
31 
32 #include <sys/param.h>
33 #include <sys/lock.h>
34 #include <sys/mutex.h>
35 #include <sys/mbuf.h>
36 #include <sys/kernel.h>
37 #include <sys/socket.h>
38 #include <sys/systm.h>
39 #include <sys/malloc.h>
40 #include <sys/queue.h>
41 #include <sys/taskqueue.h>
42 #include <sys/bus.h>
43 #include <sys/endian.h>
44 #include <sys/linker.h>
45 
46 #include <net/if.h>
47 #include <net/ethernet.h>
48 #include <net/if_media.h>
49 
50 #include <net80211/ieee80211_var.h>
51 #include <net80211/ieee80211_radiotap.h>
52 
53 #include <dev/rtwn/if_rtwnreg.h>
54 #include <dev/rtwn/if_rtwnvar.h>
55 
56 #include <dev/rtwn/if_rtwn_ridx.h>
57 
58 #include <dev/rtwn/rtl8812a/r12a.h>
59 #include <dev/rtwn/rtl8812a/r12a_tx_desc.h>
60 
61 
62 static int
63 r12a_get_primary_channel(struct rtwn_softc *sc, struct ieee80211_channel *c)
64 {
65 	/* XXX 80 MHz */
66 	if (IEEE80211_IS_CHAN_HT40U(c))
67 		return (R12A_TXDW5_PRIM_CHAN_20_80_2);
68 	else
69 		return (R12A_TXDW5_PRIM_CHAN_20_80_3);
70 }
71 
72 static void
73 r12a_tx_set_ht40(struct rtwn_softc *sc, void *buf, struct ieee80211_node *ni)
74 {
75 	struct r12a_tx_desc *txd = (struct r12a_tx_desc *)buf;
76 
77 	/* XXX 80 Mhz */
78 	if (ni->ni_chan != IEEE80211_CHAN_ANYC &&
79 	    IEEE80211_IS_CHAN_HT40(ni->ni_chan)) {
80 		int prim_chan;
81 
82 		prim_chan = r12a_get_primary_channel(sc, ni->ni_chan);
83 		txd->txdw5 |= htole32(SM(R12A_TXDW5_DATA_BW,
84 		    R12A_TXDW5_DATA_BW40));
85 		txd->txdw5 |= htole32(SM(R12A_TXDW5_DATA_PRIM_CHAN,
86 		    prim_chan));
87 	}
88 }
89 
90 static void
91 r12a_tx_protection(struct rtwn_softc *sc, struct r12a_tx_desc *txd,
92     enum ieee80211_protmode mode, uint8_t ridx)
93 {
94 	struct ieee80211com *ic = &sc->sc_ic;
95 	uint8_t rate;
96 
97 	switch (mode) {
98 	case IEEE80211_PROT_CTSONLY:
99 		txd->txdw3 |= htole32(R12A_TXDW3_CTS2SELF);
100 		break;
101 	case IEEE80211_PROT_RTSCTS:
102 		txd->txdw3 |= htole32(R12A_TXDW3_RTSEN);
103 		break;
104 	default:
105 		break;
106 	}
107 
108 	if (mode == IEEE80211_PROT_CTSONLY ||
109 	    mode == IEEE80211_PROT_RTSCTS) {
110 		if (ridx >= RTWN_RIDX_MCS(0))
111 			rate = rtwn_ctl_mcsrate(ic->ic_rt, ridx);
112 		else
113 			rate = ieee80211_ctl_rate(ic->ic_rt, ridx2rate[ridx]);
114 		ridx = rate2ridx(rate);
115 
116 		txd->txdw4 |= htole32(SM(R12A_TXDW4_RTSRATE, ridx));
117 		/* RTS rate fallback limit (max). */
118 		txd->txdw4 |= htole32(SM(R12A_TXDW4_RTSRATE_FB_LMT, 0xf));
119 
120 		if (RTWN_RATE_IS_CCK(ridx) && ridx != RTWN_RIDX_CCK1 &&
121 		    (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
122 			txd->txdw5 |= htole32(R12A_TXDW5_RTS_SHORT);
123 	}
124 }
125 
126 static void
127 r12a_tx_raid(struct rtwn_softc *sc, struct r12a_tx_desc *txd,
128     struct ieee80211_node *ni, int ismcast)
129 {
130 	struct ieee80211com *ic = &sc->sc_ic;
131 	struct ieee80211vap *vap = ni->ni_vap;
132 	struct ieee80211_channel *chan;
133 	enum ieee80211_phymode mode;
134 	uint8_t raid;
135 
136 	chan = (ni->ni_chan != IEEE80211_CHAN_ANYC) ?
137 		ni->ni_chan : ic->ic_curchan;
138 	mode = ieee80211_chan2mode(chan);
139 
140 	/* NB: group addressed frames are done at 11bg rates for now */
141 	if (ismcast || !(ni->ni_flags & IEEE80211_NODE_HT)) {
142 		switch (mode) {
143 		case IEEE80211_MODE_11A:
144 		case IEEE80211_MODE_11B:
145 		case IEEE80211_MODE_11G:
146 			break;
147 		case IEEE80211_MODE_11NA:
148 			mode = IEEE80211_MODE_11A;
149 			break;
150 		case IEEE80211_MODE_11NG:
151 			mode = IEEE80211_MODE_11G;
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 	default:
193 		/* TODO: 80 MHz / 11ac */
194 		device_printf(sc->sc_dev, "unknown mode(2) %d!\n", mode);
195 		return;
196 	}
197 
198 	txd->txdw1 |= htole32(SM(R12A_TXDW1_RAID, raid));
199 }
200 
201 static void
202 r12a_tx_set_sgi(struct rtwn_softc *sc, void *buf, struct ieee80211_node *ni)
203 {
204 	struct r12a_tx_desc *txd = (struct r12a_tx_desc *)buf;
205 	struct ieee80211vap *vap = ni->ni_vap;
206 
207 	if ((vap->iv_flags_ht & IEEE80211_FHT_SHORTGI20) &&	/* HT20 */
208 	    (ni->ni_htcap & IEEE80211_HTCAP_SHORTGI20))
209 		txd->txdw5 |= htole32(R12A_TXDW5_DATA_SHORT);
210 	else if (ni->ni_chan != IEEE80211_CHAN_ANYC &&		/* HT40 */
211 	    IEEE80211_IS_CHAN_HT40(ni->ni_chan) &&
212 	    (ni->ni_htcap & IEEE80211_HTCAP_SHORTGI40) &&
213 	    (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI40))
214 		txd->txdw5 |= htole32(R12A_TXDW5_DATA_SHORT);
215 }
216 
217 static void
218 r12a_tx_set_ldpc(struct rtwn_softc *sc, struct r12a_tx_desc *txd,
219     struct ieee80211_node *ni)
220 {
221 	struct ieee80211vap *vap = ni->ni_vap;
222 
223 	if ((vap->iv_flags_ht & IEEE80211_FHT_LDPC_TX) &&
224 	    (ni->ni_htcap & IEEE80211_HTCAP_LDPC))
225 		txd->txdw5 |= htole32(R12A_TXDW5_DATA_LDPC);
226 }
227 
228 void
229 r12a_fill_tx_desc(struct rtwn_softc *sc, struct ieee80211_node *ni,
230     struct mbuf *m, void *buf, uint8_t ridx, int maxretry)
231 {
232 	struct ieee80211com *ic = &sc->sc_ic;
233 	struct ieee80211vap *vap = ni->ni_vap;
234 	struct rtwn_vap *uvp = RTWN_VAP(vap);
235 	struct ieee80211_frame *wh;
236 	struct r12a_tx_desc *txd;
237 	enum ieee80211_protmode prot;
238 	uint8_t type, tid, qos, qsel;
239 	int hasqos, ismcast, macid;
240 
241 	wh = mtod(m, struct ieee80211_frame *);
242 	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
243 	hasqos = IEEE80211_QOS_HAS_SEQ(wh);
244 	ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1);
245 
246 	/* Select TX ring for this frame. */
247 	if (hasqos) {
248 		qos = ((const struct ieee80211_qosframe *)wh)->i_qos[0];
249 		tid = qos & IEEE80211_QOS_TID;
250 	} else {
251 		qos = 0;
252 		tid = 0;
253 	}
254 
255 	/* Fill Tx descriptor. */
256 	txd = (struct r12a_tx_desc *)buf;
257 	txd->flags0 |= R12A_FLAGS0_LSG | R12A_FLAGS0_FSG;
258 	if (ismcast)
259 		txd->flags0 |= R12A_FLAGS0_BMCAST;
260 
261 	if (!ismcast) {
262 		/* Unicast frame, check if an ACK is expected. */
263 		if (!qos || (qos & IEEE80211_QOS_ACKPOLICY) !=
264 		    IEEE80211_QOS_ACKPOLICY_NOACK) {
265 			txd->txdw4 = htole32(R12A_TXDW4_RETRY_LMT_ENA);
266 			txd->txdw4 |= htole32(SM(R12A_TXDW4_RETRY_LMT,
267 			    maxretry));
268 		}
269 
270 		struct rtwn_node *un = RTWN_NODE(ni);
271 		macid = un->id;
272 
273 		if (type == IEEE80211_FC0_TYPE_DATA) {
274 			qsel = tid % RTWN_MAX_TID;
275 
276 			if (m->m_flags & M_AMPDU_MPDU) {
277 				txd->txdw2 |= htole32(R12A_TXDW2_AGGEN);
278 				txd->txdw2 |= htole32(SM(R12A_TXDW2_AMPDU_DEN,
279 				    vap->iv_ampdu_density));
280 				txd->txdw3 |= htole32(SM(R12A_TXDW3_MAX_AGG,
281 				    0x1f));	/* XXX */
282 			} else
283 				txd->txdw2 |= htole32(R12A_TXDW2_AGGBK);
284 
285 			if (sc->sc_ratectl == RTWN_RATECTL_NET80211) {
286 				txd->txdw2 |= htole32(R12A_TXDW2_SPE_RPT);
287 				sc->sc_tx_n_active++;
288 			}
289 
290 			if (RTWN_RATE_IS_CCK(ridx) && ridx != RTWN_RIDX_CCK1 &&
291 			    (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
292 				txd->txdw5 |= htole32(R12A_TXDW5_DATA_SHORT);
293 
294 			prot = IEEE80211_PROT_NONE;
295 			if (ridx >= RTWN_RIDX_MCS(0)) {
296 				r12a_tx_set_ht40(sc, txd, ni);
297 				r12a_tx_set_sgi(sc, txd, ni);
298 				r12a_tx_set_ldpc(sc, txd, ni);
299 				prot = ic->ic_htprotmode;
300 			} else if (ic->ic_flags & IEEE80211_F_USEPROT)
301 				prot = ic->ic_protmode;
302 
303 			/* XXX fix last comparison for A-MSDU (in net80211) */
304 			/* XXX A-MPDU? */
305 			if (m->m_pkthdr.len + IEEE80211_CRC_LEN >
306 			    vap->iv_rtsthreshold &&
307 			    vap->iv_rtsthreshold != IEEE80211_RTS_MAX)
308 				prot = IEEE80211_PROT_RTSCTS;
309 
310 			if (prot != IEEE80211_PROT_NONE)
311 				r12a_tx_protection(sc, txd, prot, ridx);
312 		} else	/* IEEE80211_FC0_TYPE_MGT */
313 			qsel = R12A_TXDW1_QSEL_MGNT;
314 	} else {
315 		macid = RTWN_MACID_BC;
316 		qsel = R12A_TXDW1_QSEL_MGNT;
317 	}
318 
319 	txd->txdw1 |= htole32(SM(R12A_TXDW1_QSEL, qsel));
320 	txd->txdw1 |= htole32(SM(R12A_TXDW1_MACID, macid));
321 	txd->txdw4 |= htole32(SM(R12A_TXDW4_DATARATE, ridx));
322 	/* Data rate fallback limit (max). */
323 	txd->txdw4 |= htole32(SM(R12A_TXDW4_DATARATE_FB_LMT, 0x1f));
324 	/* XXX recheck for non-21au */
325 	txd->txdw6 |= htole32(SM(R21A_TXDW6_MBSSID, uvp->id));
326 	r12a_tx_raid(sc, txd, ni, ismcast);
327 
328 	/* Force this rate if needed. */
329 	if (sc->sc_ratectl != RTWN_RATECTL_FW)
330 		txd->txdw3 |= htole32(R12A_TXDW3_DRVRATE);
331 
332 	if (!hasqos) {
333 		/* Use HW sequence numbering for non-QoS frames. */
334 		txd->txdw8 |= htole32(R12A_TXDW8_HWSEQ_EN);
335 		txd->txdw3 |= htole32(SM(R12A_TXDW3_SEQ_SEL, uvp->id));
336 	} else {
337 		uint16_t seqno;
338 
339 		if (m->m_flags & M_AMPDU_MPDU) {
340 			seqno = ni->ni_txseqs[tid];
341 			/* NB: clear Fragment Number field. */
342 			*(uint16_t *)wh->i_seq = 0;
343 			ni->ni_txseqs[tid]++;
344 		} else
345 			seqno = M_SEQNO_GET(m) % IEEE80211_SEQ_RANGE;
346 
347 		/* Set sequence number. */
348 		txd->txdw9 |= htole32(SM(R12A_TXDW9_SEQ, seqno));
349 	}
350 }
351 
352 void
353 r12a_fill_tx_desc_raw(struct rtwn_softc *sc, struct ieee80211_node *ni,
354     struct mbuf *m, void *buf, const struct ieee80211_bpf_params *params)
355 {
356 	struct ieee80211vap *vap = ni->ni_vap;
357 	struct rtwn_vap *uvp = RTWN_VAP(vap);
358 	struct ieee80211_frame *wh;
359 	struct r12a_tx_desc *txd;
360 	uint8_t ridx;
361 	int ismcast;
362 
363 	/* XXX TODO: 11n checks, matching rtwn_fill_tx_desc() */
364 
365 	wh = mtod(m, struct ieee80211_frame *);
366 	ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1);
367 	ridx = rate2ridx(params->ibp_rate0);
368 
369 	/* Fill Tx descriptor. */
370 	txd = (struct r12a_tx_desc *)buf;
371 	txd->flags0 |= R12A_FLAGS0_LSG | R12A_FLAGS0_FSG;
372 	if (ismcast)
373 		txd->flags0 |= R12A_FLAGS0_BMCAST;
374 
375 	if ((params->ibp_flags & IEEE80211_BPF_NOACK) == 0) {
376 		txd->txdw4 = htole32(R12A_TXDW4_RETRY_LMT_ENA);
377 		txd->txdw4 |= htole32(SM(R12A_TXDW4_RETRY_LMT,
378 		    params->ibp_try0));
379 	}
380 	if (params->ibp_flags & IEEE80211_BPF_RTS)
381 		r12a_tx_protection(sc, txd, IEEE80211_PROT_RTSCTS, ridx);
382 	if (params->ibp_flags & IEEE80211_BPF_CTS)
383 		r12a_tx_protection(sc, txd, IEEE80211_PROT_CTSONLY, ridx);
384 
385 	txd->txdw1 |= htole32(SM(R12A_TXDW1_MACID, RTWN_MACID_BC));
386 	txd->txdw1 |= htole32(SM(R12A_TXDW1_QSEL, R12A_TXDW1_QSEL_MGNT));
387 
388 	/* Set TX rate index. */
389 	txd->txdw4 |= htole32(SM(R12A_TXDW4_DATARATE, ridx));
390 	txd->txdw4 |= htole32(SM(R12A_TXDW4_DATARATE_FB_LMT, 0x1f));
391 	txd->txdw6 |= htole32(SM(R21A_TXDW6_MBSSID, uvp->id));
392 	txd->txdw3 |= htole32(R12A_TXDW3_DRVRATE);
393 	r12a_tx_raid(sc, txd, ni, ismcast);
394 
395 	if (!IEEE80211_QOS_HAS_SEQ(wh)) {
396 		/* Use HW sequence numbering for non-QoS frames. */
397 		txd->txdw8 |= htole32(R12A_TXDW8_HWSEQ_EN);
398 		txd->txdw3 |= htole32(SM(R12A_TXDW3_SEQ_SEL, uvp->id));
399 	} else {
400 		/* Set sequence number. */
401 		txd->txdw9 |= htole32(SM(R12A_TXDW9_SEQ,
402 		    M_SEQNO_GET(m) % IEEE80211_SEQ_RANGE));
403 	}
404 }
405 
406 void
407 r12a_fill_tx_desc_null(struct rtwn_softc *sc, void *buf, int is11b, int qos,
408     int id)
409 {
410 	struct r12a_tx_desc *txd = (struct r12a_tx_desc *)buf;
411 
412 	txd->flags0 = R12A_FLAGS0_FSG | R12A_FLAGS0_LSG | R12A_FLAGS0_OWN;
413 	txd->txdw1 = htole32(
414 	    SM(R12A_TXDW1_QSEL, R12A_TXDW1_QSEL_MGNT));
415 
416 	txd->txdw3 = htole32(R12A_TXDW3_DRVRATE);
417 	txd->txdw6 = htole32(SM(R21A_TXDW6_MBSSID, id));
418 	if (is11b) {
419 		txd->txdw4 = htole32(SM(R12A_TXDW4_DATARATE,
420 		    RTWN_RIDX_CCK1));
421 	} else {
422 		txd->txdw4 = htole32(SM(R12A_TXDW4_DATARATE,
423 		    RTWN_RIDX_OFDM6));
424 	}
425 
426 	if (!qos) {
427 		txd->txdw8 = htole32(R12A_TXDW8_HWSEQ_EN);
428 		txd->txdw3 |= htole32(SM(R12A_TXDW3_SEQ_SEL, id));
429 
430 	}
431 }
432 
433 uint8_t
434 r12a_tx_radiotap_flags(const void *buf)
435 {
436 	const struct r12a_tx_desc *txd = buf;
437 	uint8_t flags, rate;
438 
439 	if (!(txd->txdw5 & htole32(R12A_TXDW5_DATA_SHORT)))
440 		return (0);
441 
442 	rate = MS(le32toh(txd->txdw4), R12A_TXDW4_DATARATE);
443 	if (RTWN_RATE_IS_CCK(rate))
444 		flags = IEEE80211_RADIOTAP_F_SHORTPRE;
445 	else
446 		flags = IEEE80211_RADIOTAP_F_SHORTGI;
447 	return (flags);
448 }
449