xref: /freebsd/sys/dev/rtwn/rtl8192c/r92c_tx.c (revision 8277c790179304159c2e4dcb1d99552518d5be8e)
1 /*	$OpenBSD: if_urtwn.c,v 1.16 2011/02/10 17:26:40 jakemsr Exp $	*/
2 
3 /*-
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>
7  *
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.
11  *
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.
19  */
20 
21 #include <sys/cdefs.h>
22 #include "opt_wlan.h"
23 
24 #include <sys/param.h>
25 #include <sys/lock.h>
26 #include <sys/mutex.h>
27 #include <sys/mbuf.h>
28 #include <sys/kernel.h>
29 #include <sys/socket.h>
30 #include <sys/systm.h>
31 #include <sys/malloc.h>
32 #include <sys/queue.h>
33 #include <sys/taskqueue.h>
34 #include <sys/bus.h>
35 #include <sys/endian.h>
36 #include <sys/linker.h>
37 
38 #include <net/if.h>
39 #include <net/ethernet.h>
40 #include <net/if_media.h>
41 
42 #include <net80211/ieee80211_var.h>
43 #include <net80211/ieee80211_radiotap.h>
44 
45 #include <dev/rtwn/if_rtwnreg.h>
46 #include <dev/rtwn/if_rtwnvar.h>
47 
48 #include <dev/rtwn/if_rtwn_ridx.h>
49 #include <dev/rtwn/if_rtwn_tx.h>
50 
51 #include <dev/rtwn/rtl8192c/r92c.h>
52 #include <dev/rtwn/rtl8192c/r92c_var.h>
53 #include <dev/rtwn/rtl8192c/r92c_tx_desc.h>
54 
55 static int
56 r92c_tx_get_sco(struct rtwn_softc *sc, struct ieee80211_channel *c)
57 {
58 	if (IEEE80211_IS_CHAN_HT40U(c))
59 		return (R92C_TXDW4_SCO_SCA);
60 	else
61 		return (R92C_TXDW4_SCO_SCB);
62 }
63 
64 static void
65 r92c_tx_set_ht40(struct rtwn_softc *sc, void *buf, struct ieee80211_node *ni)
66 {
67 	struct r92c_tx_desc *txd = (struct r92c_tx_desc *)buf;
68 
69 	if (ni->ni_chan != IEEE80211_CHAN_ANYC &&
70 	    IEEE80211_IS_CHAN_HT40(ni->ni_chan)) {
71 		int extc_offset;
72 
73 		extc_offset = r92c_tx_get_sco(sc, ni->ni_chan);
74 		txd->txdw4 |= htole32(R92C_TXDW4_DATA_BW40);
75 		txd->txdw4 |= htole32(SM(R92C_TXDW4_DATA_SCO, extc_offset));
76 	}
77 }
78 
79 static void
80 r92c_tx_protection(struct rtwn_softc *sc, struct r92c_tx_desc *txd,
81     enum ieee80211_protmode mode, uint8_t ridx)
82 {
83 	struct ieee80211com *ic = &sc->sc_ic;
84 	uint8_t rate;
85 
86 	switch (mode) {
87 	case IEEE80211_PROT_CTSONLY:
88 		txd->txdw4 |= htole32(R92C_TXDW4_CTS2SELF);
89 		break;
90 	case IEEE80211_PROT_RTSCTS:
91 		txd->txdw4 |= htole32(R92C_TXDW4_RTSEN);
92 		break;
93 	default:
94 		break;
95 	}
96 
97 	if (mode == IEEE80211_PROT_CTSONLY ||
98 	    mode == IEEE80211_PROT_RTSCTS) {
99 		if (ridx >= RTWN_RIDX_HT_MCS(0))
100 			rate = rtwn_ctl_mcsrate(ic->ic_rt, ridx);
101 		else
102 			rate = ieee80211_ctl_rate(ic->ic_rt, ridx2rate[ridx]);
103 		ridx = rate2ridx(IEEE80211_RV(rate));
104 
105 		txd->txdw4 |= htole32(SM(R92C_TXDW4_RTSRATE, ridx));
106 		/* RTS rate fallback limit (max). */
107 		txd->txdw5 |= htole32(SM(R92C_TXDW5_RTSRATE_FB_LMT, 0xf));
108 
109 		if (RTWN_RATE_IS_CCK(ridx) && ridx != RTWN_RIDX_CCK1 &&
110 		    (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
111 			txd->txdw4 |= htole32(R92C_TXDW4_RTS_SHORT);
112 	}
113 }
114 
115 static void
116 r92c_tx_raid(struct rtwn_softc *sc, struct r92c_tx_desc *txd,
117     struct ieee80211_node *ni, int ismcast)
118 {
119 	struct ieee80211com *ic = &sc->sc_ic;
120 	struct ieee80211vap *vap = ni->ni_vap;
121 	struct ieee80211_channel *chan;
122 	enum ieee80211_phymode mode;
123 	uint8_t raid;
124 
125 	chan = (ni->ni_chan != IEEE80211_CHAN_ANYC) ?
126 		ni->ni_chan : ic->ic_curchan;
127 	mode = ieee80211_chan2mode(chan);
128 
129 	/* NB: group addressed frames are done at 11bg rates for now */
130 	if (ismcast || !(ni->ni_flags & IEEE80211_NODE_HT)) {
131 		switch (mode) {
132 		case IEEE80211_MODE_11B:
133 		case IEEE80211_MODE_11G:
134 			break;
135 		case IEEE80211_MODE_11NG:
136 			mode = IEEE80211_MODE_11G;
137 			break;
138 		default:
139 			device_printf(sc->sc_dev, "unknown mode(1) %d!\n",
140 			    ic->ic_curmode);
141 			return;
142 		}
143 	}
144 
145 	switch (mode) {
146 	case IEEE80211_MODE_11B:
147 		raid = R92C_RAID_11B;
148 		break;
149 	case IEEE80211_MODE_11G:
150 		if (vap->iv_flags & IEEE80211_F_PUREG)
151 			raid = R92C_RAID_11G;
152 		else
153 			raid = R92C_RAID_11BG;
154 		break;
155 	case IEEE80211_MODE_11NG:
156 		if (vap->iv_flags_ht & IEEE80211_FHT_PUREN)
157 			raid = R92C_RAID_11N;
158 		else
159 			raid = R92C_RAID_11BGN;
160 		break;
161 	default:
162 		device_printf(sc->sc_dev, "unknown mode(2) %d!\n", mode);
163 		return;
164 	}
165 
166 	txd->txdw1 |= htole32(SM(R92C_TXDW1_RAID, raid));
167 }
168 
169 /* XXX move to device-independent layer */
170 static void
171 r92c_tx_set_sgi(struct rtwn_softc *sc, void *buf, struct ieee80211_node *ni)
172 {
173 	struct r92c_tx_desc *txd = (struct r92c_tx_desc *)buf;
174 
175 	/*
176 	 * Only enable short-GI if we're transmitting in that
177 	 * width to that node.
178 	 *
179 	 * Specifically, do not enable shortgi for 20MHz if
180 	 * we're attempting to transmit at 40MHz.
181 	 */
182 	if (ieee80211_ht_check_tx_ht40(ni)) {
183 		if (ieee80211_ht_check_tx_shortgi_40(ni))
184 			txd->txdw5 |= htole32(R92C_TXDW5_SGI);
185 	} else if (ieee80211_ht_check_tx_ht(ni)) {
186 		if (ieee80211_ht_check_tx_shortgi_20(ni))
187 			txd->txdw5 |= htole32(R92C_TXDW5_SGI);
188 	}
189 }
190 
191 void
192 r92c_tx_enable_ampdu(void *buf, int enable)
193 {
194 	struct r92c_tx_desc *txd = (struct r92c_tx_desc *)buf;
195 
196 	if (enable)
197 		txd->txdw1 |= htole32(R92C_TXDW1_AGGEN);
198 	else
199 		txd->txdw1 |= htole32(R92C_TXDW1_AGGBK);
200 }
201 
202 void
203 r92c_tx_setup_hwseq(void *buf)
204 {
205 	struct r92c_tx_desc *txd = (struct r92c_tx_desc *)buf;
206 
207 	txd->txdw4 |= htole32(R92C_TXDW4_HWSEQ_EN);
208 }
209 
210 void
211 r92c_tx_setup_macid(void *buf, int id)
212 {
213 	struct r92c_tx_desc *txd = (struct r92c_tx_desc *)buf;
214 
215 	txd->txdw1 |= htole32(SM(R92C_TXDW1_MACID, id));
216 
217 	/* XXX does not belong here */
218 	/* XXX temporary (I hope) */
219 	/* Force CCK1 for RTS / CTS frames (driver bug) */
220 	txd->txdw4 &= ~htole32(SM(R92C_TXDW4_RTSRATE, R92C_TXDW4_RTSRATE_M));
221 	txd->txdw4 &= ~htole32(R92C_TXDW4_RTS_SHORT);
222 }
223 
224 void
225 r92c_fill_tx_desc(struct rtwn_softc *sc, struct ieee80211_node *ni,
226     struct mbuf *m, void *buf, uint8_t ridx, int maxretry)
227 {
228 #ifndef RTWN_WITHOUT_UCODE
229 	struct r92c_softc *rs = sc->sc_priv;
230 #endif
231 	struct ieee80211com *ic = &sc->sc_ic;
232 	struct ieee80211vap *vap = ni->ni_vap;
233 	struct rtwn_vap *uvp = RTWN_VAP(vap);
234 	struct ieee80211_frame *wh;
235 	struct r92c_tx_desc *txd;
236 	enum ieee80211_protmode prot;
237 	uint8_t type, tid, qos, qsel;
238 	int hasqos, ismcast, macid;
239 
240 	wh = mtod(m, struct ieee80211_frame *);
241 	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
242 	hasqos = IEEE80211_QOS_HAS_SEQ(wh);
243 	ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1);
244 
245 	/* Select TX ring for this frame. */
246 	if (hasqos) {
247 		qos = ((const struct ieee80211_qosframe *)wh)->i_qos[0];
248 		tid = qos & IEEE80211_QOS_TID;
249 	} else {
250 		qos = 0;
251 		tid = 0;
252 	}
253 
254 	/* Fill Tx descriptor. */
255 	txd = (struct r92c_tx_desc *)buf;
256 	txd->flags0 |= R92C_FLAGS0_LSG | R92C_FLAGS0_FSG;
257 	if (ismcast)
258 		txd->flags0 |= R92C_FLAGS0_BMCAST;
259 
260 	if (!ismcast) {
261 		/* Unicast frame, check if an ACK is expected. */
262 		if (!qos || (qos & IEEE80211_QOS_ACKPOLICY) !=
263 		    IEEE80211_QOS_ACKPOLICY_NOACK) {
264 			txd->txdw5 |= htole32(R92C_TXDW5_RTY_LMT_ENA);
265 			txd->txdw5 |= htole32(SM(R92C_TXDW5_RTY_LMT,
266 			    maxretry));
267 		}
268 
269 		struct rtwn_node *un = RTWN_NODE(ni);
270 		macid = un->id;
271 
272 		if (type == IEEE80211_FC0_TYPE_DATA) {
273 			qsel = tid % RTWN_MAX_TID;
274 
275 			rtwn_r92c_tx_enable_ampdu(sc, buf,
276 			    (m->m_flags & M_AMPDU_MPDU) != 0);
277 			if (m->m_flags & M_AMPDU_MPDU) {
278 				txd->txdw2 |= htole32(SM(R92C_TXDW2_AMPDU_DEN,
279 				    vap->iv_ampdu_density));
280 				txd->txdw6 |= htole32(SM(R92C_TXDW6_MAX_AGG,
281 				    0x1f));	/* XXX */
282 			}
283 			if (sc->sc_ratectl == RTWN_RATECTL_NET80211) {
284 				txd->txdw2 |= htole32(R92C_TXDW2_CCX_RPT);
285 				sc->sc_tx_n_active++;
286 #ifndef RTWN_WITHOUT_UCODE
287 				rs->rs_c2h_pending++;
288 #endif
289 			}
290 
291 			if (RTWN_RATE_IS_CCK(ridx) && ridx != RTWN_RIDX_CCK1 &&
292 			    (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
293 				txd->txdw4 |= htole32(R92C_TXDW4_DATA_SHPRE);
294 
295 			prot = IEEE80211_PROT_NONE;
296 			if (ridx >= RTWN_RIDX_HT_MCS(0)) {
297 				r92c_tx_set_ht40(sc, txd, ni);
298 				r92c_tx_set_sgi(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 			/* NB: checks for ht40 / short bits (set above). */
311 			if (prot != IEEE80211_PROT_NONE)
312 				r92c_tx_protection(sc, txd, prot, ridx);
313 		} else	/* IEEE80211_FC0_TYPE_MGT */
314 			qsel = R92C_TXDW1_QSEL_MGNT;
315 	} else {
316 		macid = RTWN_MACID_BC;
317 		qsel = R92C_TXDW1_QSEL_MGNT;
318 	}
319 
320 	txd->txdw1 |= htole32(SM(R92C_TXDW1_QSEL, qsel));
321 
322 	rtwn_r92c_tx_setup_macid(sc, txd, macid);
323 	txd->txdw5 |= htole32(SM(R92C_TXDW5_DATARATE, ridx));
324 	/* Data rate fallback limit (max). */
325 	txd->txdw5 |= htole32(SM(R92C_TXDW5_DATARATE_FB_LMT, 0x1f));
326 	txd->txdw4 |= htole32(SM(R92C_TXDW4_PORT_ID, uvp->id));
327 	r92c_tx_raid(sc, txd, ni, ismcast);
328 
329 	/* Force this rate if needed. */
330 	if (sc->sc_ratectl != RTWN_RATECTL_FW)
331 		txd->txdw4 |= htole32(R92C_TXDW4_DRVRATE);
332 
333 	if (!hasqos) {
334 		/* Use HW sequence numbering for non-QoS frames. */
335 		rtwn_r92c_tx_setup_hwseq(sc, txd);
336 		txd->txdw4 |= htole32(SM(R92C_TXDW4_SEQ_SEL, uvp->id));
337 	} else {
338 		uint16_t seqno;
339 
340 		if (m->m_flags & M_AMPDU_MPDU) {
341 			seqno = ni->ni_txseqs[tid] % IEEE80211_SEQ_RANGE;
342 			ni->ni_txseqs[tid]++;
343 		} else
344 			seqno = M_SEQNO_GET(m) % IEEE80211_SEQ_RANGE;
345 
346 		/* Set sequence number. */
347 		txd->txdseq = htole16(seqno);
348 	}
349 }
350 
351 void
352 r92c_fill_tx_desc_raw(struct rtwn_softc *sc, struct ieee80211_node *ni,
353     struct mbuf *m, void *buf, const struct ieee80211_bpf_params *params)
354 {
355 	struct ieee80211vap *vap = ni->ni_vap;
356 	struct rtwn_vap *uvp = RTWN_VAP(vap);
357 	struct ieee80211_frame *wh;
358 	struct r92c_tx_desc *txd;
359 	uint8_t ridx;
360 	int ismcast;
361 
362 	/* XXX TODO: 11n checks, matching r92c_fill_tx_desc() */
363 
364 	wh = mtod(m, struct ieee80211_frame *);
365 	ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1);
366 	ridx = rate2ridx(params->ibp_rate0);
367 
368 	/* Fill Tx descriptor. */
369 	txd = (struct r92c_tx_desc *)buf;
370 	txd->flags0 |= R92C_FLAGS0_LSG | R92C_FLAGS0_FSG;
371 	if (ismcast)
372 		txd->flags0 |= R92C_FLAGS0_BMCAST;
373 
374 	if ((params->ibp_flags & IEEE80211_BPF_NOACK) == 0) {
375 		txd->txdw5 |= htole32(R92C_TXDW5_RTY_LMT_ENA);
376 		txd->txdw5 |= htole32(SM(R92C_TXDW5_RTY_LMT,
377 		    params->ibp_try0));
378 	}
379 	if (params->ibp_flags & IEEE80211_BPF_RTS)
380 		r92c_tx_protection(sc, txd, IEEE80211_PROT_RTSCTS, ridx);
381 	if (params->ibp_flags & IEEE80211_BPF_CTS)
382 		r92c_tx_protection(sc, txd, IEEE80211_PROT_CTSONLY, ridx);
383 
384 	rtwn_r92c_tx_setup_macid(sc, txd, RTWN_MACID_BC);
385 	txd->txdw1 |= htole32(SM(R92C_TXDW1_QSEL, R92C_TXDW1_QSEL_MGNT));
386 
387 	/* Set TX rate index. */
388 	txd->txdw5 |= htole32(SM(R92C_TXDW5_DATARATE, ridx));
389 	txd->txdw5 |= htole32(SM(R92C_TXDW5_DATARATE_FB_LMT, 0x1f));
390 	txd->txdw4 |= htole32(SM(R92C_TXDW4_PORT_ID, uvp->id));
391 	txd->txdw4 |= htole32(R92C_TXDW4_DRVRATE);
392 	r92c_tx_raid(sc, txd, ni, ismcast);
393 
394 	if (!IEEE80211_QOS_HAS_SEQ(wh)) {
395 		/* Use HW sequence numbering for non-QoS frames. */
396 		rtwn_r92c_tx_setup_hwseq(sc, txd);
397 		txd->txdw4 |= htole32(SM(R92C_TXDW4_SEQ_SEL, uvp->id));
398 	} else {
399 		/* Set sequence number. */
400 		txd->txdseq |= htole16(M_SEQNO_GET(m) % IEEE80211_SEQ_RANGE);
401 	}
402 }
403 
404 void
405 r92c_fill_tx_desc_null(struct rtwn_softc *sc, void *buf, int is11b,
406     int qos, int id)
407 {
408 	struct r92c_tx_desc *txd = (struct r92c_tx_desc *)buf;
409 
410 	txd->flags0 = R92C_FLAGS0_FSG | R92C_FLAGS0_LSG | R92C_FLAGS0_OWN;
411 	txd->txdw1 = htole32(
412 	    SM(R92C_TXDW1_QSEL, R92C_TXDW1_QSEL_MGNT));
413 
414 	txd->txdw4 = htole32(R92C_TXDW4_DRVRATE);
415 	txd->txdw4 |= htole32(SM(R92C_TXDW4_PORT_ID, id));
416 	if (is11b) {
417 		txd->txdw5 = htole32(SM(R92C_TXDW5_DATARATE,
418 		    RTWN_RIDX_CCK1));
419 	} else {
420 		txd->txdw5 = htole32(SM(R92C_TXDW5_DATARATE,
421 		    RTWN_RIDX_OFDM6));
422 	}
423 
424 	if (!qos) {
425 		rtwn_r92c_tx_setup_hwseq(sc, txd);
426 		txd->txdw4 |= htole32(SM(R92C_TXDW4_SEQ_SEL, id));
427 	}
428 }
429 
430 uint8_t
431 r92c_tx_radiotap_flags(const void *buf)
432 {
433 	const struct r92c_tx_desc *txd = buf;
434 	uint8_t flags;
435 
436 	flags = 0;
437 	if (txd->txdw4 & htole32(R92C_TXDW4_DATA_SHPRE))
438 		flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
439 	if (txd->txdw5 & htole32(R92C_TXDW5_SGI))
440 		flags |= IEEE80211_RADIOTAP_F_SHORTGI;
441 	return (flags);
442 }
443