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 void 218 r12a_fill_tx_desc(struct rtwn_softc *sc, struct ieee80211_node *ni, 219 struct mbuf *m, void *buf, uint8_t ridx, int maxretry) 220 { 221 struct ieee80211com *ic = &sc->sc_ic; 222 struct ieee80211vap *vap = ni->ni_vap; 223 struct rtwn_vap *uvp = RTWN_VAP(vap); 224 struct ieee80211_frame *wh; 225 struct r12a_tx_desc *txd; 226 enum ieee80211_protmode prot; 227 uint8_t type, tid, qos, qsel; 228 int hasqos, ismcast, macid; 229 230 wh = mtod(m, struct ieee80211_frame *); 231 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK; 232 hasqos = IEEE80211_QOS_HAS_SEQ(wh); 233 ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1); 234 235 /* Select TX ring for this frame. */ 236 if (hasqos) { 237 qos = ((const struct ieee80211_qosframe *)wh)->i_qos[0]; 238 tid = qos & IEEE80211_QOS_TID; 239 } else { 240 qos = 0; 241 tid = 0; 242 } 243 244 /* Fill Tx descriptor. */ 245 txd = (struct r12a_tx_desc *)buf; 246 txd->flags0 |= R12A_FLAGS0_LSG | R12A_FLAGS0_FSG; 247 if (ismcast) 248 txd->flags0 |= R12A_FLAGS0_BMCAST; 249 250 if (!ismcast) { 251 /* Unicast frame, check if an ACK is expected. */ 252 if (!qos || (qos & IEEE80211_QOS_ACKPOLICY) != 253 IEEE80211_QOS_ACKPOLICY_NOACK) { 254 txd->txdw4 = htole32(R12A_TXDW4_RETRY_LMT_ENA); 255 txd->txdw4 |= htole32(SM(R12A_TXDW4_RETRY_LMT, 256 maxretry)); 257 } 258 259 struct rtwn_node *un = RTWN_NODE(ni); 260 macid = un->id; 261 262 if (type == IEEE80211_FC0_TYPE_DATA) { 263 qsel = tid % RTWN_MAX_TID; 264 265 if (m->m_flags & M_AMPDU_MPDU) { 266 txd->txdw2 |= htole32(R12A_TXDW2_AGGEN); 267 txd->txdw2 |= htole32(SM(R12A_TXDW2_AMPDU_DEN, 268 vap->iv_ampdu_density)); 269 txd->txdw3 |= htole32(SM(R12A_TXDW3_MAX_AGG, 270 0x1f)); /* XXX */ 271 } else 272 txd->txdw2 |= htole32(R12A_TXDW2_AGGBK); 273 274 if (sc->sc_ratectl == RTWN_RATECTL_NET80211) { 275 txd->txdw2 |= htole32(R12A_TXDW2_SPE_RPT); 276 sc->sc_tx_n_active++; 277 } 278 279 if (RTWN_RATE_IS_CCK(ridx) && ridx != RTWN_RIDX_CCK1 && 280 (ic->ic_flags & IEEE80211_F_SHPREAMBLE)) 281 txd->txdw5 |= htole32(R12A_TXDW5_DATA_SHORT); 282 283 prot = IEEE80211_PROT_NONE; 284 if (ridx >= RTWN_RIDX_MCS(0)) { 285 r12a_tx_set_ht40(sc, txd, ni); 286 r12a_tx_set_sgi(sc, txd, ni); 287 prot = ic->ic_htprotmode; 288 } else if (ic->ic_flags & IEEE80211_F_USEPROT) 289 prot = ic->ic_protmode; 290 291 /* XXX fix last comparison for A-MSDU (in net80211) */ 292 /* XXX A-MPDU? */ 293 if (m->m_pkthdr.len + IEEE80211_CRC_LEN > 294 vap->iv_rtsthreshold && 295 vap->iv_rtsthreshold != IEEE80211_RTS_MAX) 296 prot = IEEE80211_PROT_RTSCTS; 297 298 if (prot != IEEE80211_PROT_NONE) 299 r12a_tx_protection(sc, txd, prot, ridx); 300 } else /* IEEE80211_FC0_TYPE_MGT */ 301 qsel = R12A_TXDW1_QSEL_MGNT; 302 } else { 303 macid = RTWN_MACID_BC; 304 qsel = R12A_TXDW1_QSEL_MGNT; 305 } 306 307 txd->txdw1 |= htole32(SM(R12A_TXDW1_QSEL, qsel)); 308 txd->txdw1 |= htole32(SM(R12A_TXDW1_MACID, macid)); 309 txd->txdw4 |= htole32(SM(R12A_TXDW4_DATARATE, ridx)); 310 /* Data rate fallback limit (max). */ 311 txd->txdw4 |= htole32(SM(R12A_TXDW4_DATARATE_FB_LMT, 0x1f)); 312 /* XXX recheck for non-21au */ 313 txd->txdw6 |= htole32(SM(R21A_TXDW6_MBSSID, uvp->id)); 314 r12a_tx_raid(sc, txd, ni, ismcast); 315 316 /* Force this rate if needed. */ 317 if (sc->sc_ratectl != RTWN_RATECTL_FW) 318 txd->txdw3 |= htole32(R12A_TXDW3_DRVRATE); 319 320 if (!hasqos) { 321 /* Use HW sequence numbering for non-QoS frames. */ 322 txd->txdw8 |= htole32(R12A_TXDW8_HWSEQ_EN); 323 txd->txdw3 |= htole32(SM(R12A_TXDW3_SEQ_SEL, uvp->id)); 324 } else { 325 uint16_t seqno; 326 327 if (m->m_flags & M_AMPDU_MPDU) { 328 seqno = ni->ni_txseqs[tid]; 329 /* NB: clear Fragment Number field. */ 330 *(uint16_t *)wh->i_seq = 0; 331 ni->ni_txseqs[tid]++; 332 } else 333 seqno = M_SEQNO_GET(m) % IEEE80211_SEQ_RANGE; 334 335 /* Set sequence number. */ 336 txd->txdw9 |= htole32(SM(R12A_TXDW9_SEQ, seqno)); 337 } 338 } 339 340 void 341 r12a_fill_tx_desc_raw(struct rtwn_softc *sc, struct ieee80211_node *ni, 342 struct mbuf *m, void *buf, const struct ieee80211_bpf_params *params) 343 { 344 struct ieee80211vap *vap = ni->ni_vap; 345 struct rtwn_vap *uvp = RTWN_VAP(vap); 346 struct ieee80211_frame *wh; 347 struct r12a_tx_desc *txd; 348 uint8_t ridx; 349 int ismcast; 350 351 /* XXX TODO: 11n checks, matching rtwn_fill_tx_desc() */ 352 353 wh = mtod(m, struct ieee80211_frame *); 354 ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1); 355 ridx = rate2ridx(params->ibp_rate0); 356 357 /* Fill Tx descriptor. */ 358 txd = (struct r12a_tx_desc *)buf; 359 txd->flags0 |= R12A_FLAGS0_LSG | R12A_FLAGS0_FSG; 360 if (ismcast) 361 txd->flags0 |= R12A_FLAGS0_BMCAST; 362 363 if ((params->ibp_flags & IEEE80211_BPF_NOACK) == 0) { 364 txd->txdw4 = htole32(R12A_TXDW4_RETRY_LMT_ENA); 365 txd->txdw4 |= htole32(SM(R12A_TXDW4_RETRY_LMT, 366 params->ibp_try0)); 367 } 368 if (params->ibp_flags & IEEE80211_BPF_RTS) 369 r12a_tx_protection(sc, txd, IEEE80211_PROT_RTSCTS, ridx); 370 if (params->ibp_flags & IEEE80211_BPF_CTS) 371 r12a_tx_protection(sc, txd, IEEE80211_PROT_CTSONLY, ridx); 372 373 txd->txdw1 |= htole32(SM(R12A_TXDW1_MACID, RTWN_MACID_BC)); 374 txd->txdw1 |= htole32(SM(R12A_TXDW1_QSEL, R12A_TXDW1_QSEL_MGNT)); 375 376 /* Set TX rate index. */ 377 txd->txdw4 |= htole32(SM(R12A_TXDW4_DATARATE, ridx)); 378 txd->txdw4 |= htole32(SM(R12A_TXDW4_DATARATE_FB_LMT, 0x1f)); 379 txd->txdw6 |= htole32(SM(R21A_TXDW6_MBSSID, uvp->id)); 380 txd->txdw3 |= htole32(R12A_TXDW3_DRVRATE); 381 r12a_tx_raid(sc, txd, ni, ismcast); 382 383 if (!IEEE80211_QOS_HAS_SEQ(wh)) { 384 /* Use HW sequence numbering for non-QoS frames. */ 385 txd->txdw8 |= htole32(R12A_TXDW8_HWSEQ_EN); 386 txd->txdw3 |= htole32(SM(R12A_TXDW3_SEQ_SEL, uvp->id)); 387 } else { 388 /* Set sequence number. */ 389 txd->txdw9 |= htole32(SM(R12A_TXDW9_SEQ, 390 M_SEQNO_GET(m) % IEEE80211_SEQ_RANGE)); 391 } 392 } 393 394 void 395 r12a_fill_tx_desc_null(struct rtwn_softc *sc, void *buf, int is11b, int qos, 396 int id) 397 { 398 struct r12a_tx_desc *txd = (struct r12a_tx_desc *)buf; 399 400 txd->flags0 = R12A_FLAGS0_FSG | R12A_FLAGS0_LSG | R12A_FLAGS0_OWN; 401 txd->txdw1 = htole32( 402 SM(R12A_TXDW1_QSEL, R12A_TXDW1_QSEL_MGNT)); 403 404 txd->txdw3 = htole32(R12A_TXDW3_DRVRATE); 405 txd->txdw6 = htole32(SM(R21A_TXDW6_MBSSID, id)); 406 if (is11b) { 407 txd->txdw4 = htole32(SM(R12A_TXDW4_DATARATE, 408 RTWN_RIDX_CCK1)); 409 } else { 410 txd->txdw4 = htole32(SM(R12A_TXDW4_DATARATE, 411 RTWN_RIDX_OFDM6)); 412 } 413 414 if (!qos) { 415 txd->txdw8 = htole32(R12A_TXDW8_HWSEQ_EN); 416 txd->txdw3 |= htole32(SM(R12A_TXDW3_SEQ_SEL, id)); 417 418 } 419 } 420 421 uint8_t 422 r12a_tx_radiotap_flags(const void *buf) 423 { 424 const struct r12a_tx_desc *txd = buf; 425 uint8_t flags, rate; 426 427 if (!(txd->txdw5 & htole32(R12A_TXDW5_DATA_SHORT))) 428 return (0); 429 430 rate = MS(le32toh(txd->txdw4), R12A_TXDW4_DATARATE); 431 if (RTWN_RATE_IS_CCK(rate)) 432 flags = IEEE80211_RADIOTAP_F_SHORTPRE; 433 else 434 flags = IEEE80211_RADIOTAP_F_SHORTGI; 435 return (flags); 436 } 437