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 #include <net80211/ieee80211_vht.h> 51 52 #include <dev/rtwn/if_rtwnreg.h> 53 #include <dev/rtwn/if_rtwnvar.h> 54 55 #include <dev/rtwn/if_rtwn_ridx.h> 56 57 #include <dev/rtwn/rtl8812a/r12a.h> 58 #include <dev/rtwn/rtl8812a/r12a_tx_desc.h> 59 60 /* 61 * This function actually handles the secondary channel mapping, 62 * not the primary channel mapping. It hints to the MAC where 63 * to handle duplicate transmission of the RTS/CTS and payload 64 * frames when the requested transmit channel width is less than 65 * the configured channel width. 66 * 67 * Note: the vendor driver and linux rtw88 driver both leave this 68 * field currently set to 0. 69 * 70 * See the rtl8812au vendor driver, hal/rtl8812a_xmit.c:SCMapping_8812() 71 * and where it's used (and ignored.) 72 */ 73 static int 74 r12a_get_primary_channel(struct rtwn_softc *sc, struct ieee80211_channel *c) 75 { 76 #if 0 77 /* XXX VHT80; VHT40 */ 78 if (IEEE80211_IS_CHAN_HT40U(c)) 79 return (R12A_TXDW5_PRIM_CHAN_20_80_2); 80 else 81 return (R12A_TXDW5_PRIM_CHAN_20_80_3); 82 #endif 83 84 /* 85 * For now just return the VHT_DATA_SC_DONOT_CARE value 86 * from the reference driver. 87 */ 88 return (0); 89 } 90 91 /* 92 * Configure VHT20/VHT40/VHT80 as appropriate. 93 * 94 * This is only called for VHT, not for HT. 95 */ 96 static void 97 r12a_tx_set_vht_bw(struct rtwn_softc *sc, void *buf, struct ieee80211_node *ni) 98 { 99 struct r12a_tx_desc *txd = (struct r12a_tx_desc *)buf; 100 int prim_chan; 101 102 prim_chan = r12a_get_primary_channel(sc, ni->ni_chan); 103 104 if (ieee80211_vht_check_tx_bw(ni, IEEE80211_STA_RX_BW_80)) { 105 txd->txdw5 |= htole32(SM(R12A_TXDW5_DATA_BW, 106 R12A_TXDW5_DATA_BW80)); 107 txd->txdw5 |= htole32(SM(R12A_TXDW5_DATA_PRIM_CHAN, 108 prim_chan)); 109 } else if (ieee80211_vht_check_tx_bw(ni, IEEE80211_STA_RX_BW_40)) { 110 txd->txdw5 |= htole32(SM(R12A_TXDW5_DATA_BW, 111 R12A_TXDW5_DATA_BW40)); 112 txd->txdw5 |= htole32(SM(R12A_TXDW5_DATA_PRIM_CHAN, 113 prim_chan)); 114 } 115 } 116 117 /* 118 * Configure HT20/HT40 as appropriate. 119 * 120 * This is only called for HT, not for VHT. 121 */ 122 static void 123 r12a_tx_set_ht40(struct rtwn_softc *sc, void *buf, struct ieee80211_node *ni) 124 { 125 struct r12a_tx_desc *txd = (struct r12a_tx_desc *)buf; 126 127 if (ieee80211_ht_check_tx_ht40(ni)) { 128 int prim_chan; 129 130 prim_chan = r12a_get_primary_channel(sc, ni->ni_chan); 131 txd->txdw5 |= htole32(SM(R12A_TXDW5_DATA_BW, 132 R12A_TXDW5_DATA_BW40)); 133 txd->txdw5 |= htole32(SM(R12A_TXDW5_DATA_PRIM_CHAN, 134 prim_chan)); 135 } 136 } 137 138 static void 139 r12a_tx_protection(struct rtwn_softc *sc, struct r12a_tx_desc *txd, 140 enum ieee80211_protmode mode, uint8_t ridx) 141 { 142 struct ieee80211com *ic = &sc->sc_ic; 143 uint8_t rate; 144 145 switch (mode) { 146 case IEEE80211_PROT_CTSONLY: 147 txd->txdw3 |= htole32(R12A_TXDW3_CTS2SELF); 148 break; 149 case IEEE80211_PROT_RTSCTS: 150 txd->txdw3 |= htole32(R12A_TXDW3_RTSEN); 151 break; 152 default: 153 break; 154 } 155 156 if (mode == IEEE80211_PROT_CTSONLY || 157 mode == IEEE80211_PROT_RTSCTS) { 158 /* 159 * Note: this code assumes basic rates for protection for 160 * both 802.11abg and 802.11n rates. 161 */ 162 if (RTWN_RATE_IS_VHT(ridx)) 163 rate = rtwn_ctl_vhtrate(ic->ic_rt, ridx); 164 else if (RTWN_RATE_IS_HT(ridx)) 165 rate = rtwn_ctl_mcsrate(ic->ic_rt, ridx); 166 else 167 rate = ieee80211_ctl_rate(ic->ic_rt, ridx2rate[ridx]); 168 /* Map basic rate back to ridx */ 169 ridx = rate2ridx(IEEE80211_RV(rate)); 170 171 txd->txdw4 |= htole32(SM(R12A_TXDW4_RTSRATE, ridx)); 172 /* RTS rate fallback limit (max). */ 173 txd->txdw4 |= htole32(SM(R12A_TXDW4_RTSRATE_FB_LMT, 0xf)); 174 175 if (RTWN_RATE_IS_CCK(ridx) && ridx != RTWN_RIDX_CCK1 && 176 (ic->ic_flags & IEEE80211_F_SHPREAMBLE)) 177 txd->txdw5 |= htole32(R12A_TXDW5_RTS_SHORT); 178 } 179 } 180 181 static void 182 r12a_tx_raid(struct rtwn_softc *sc, struct r12a_tx_desc *txd, 183 struct ieee80211_node *ni, int ismcast) 184 { 185 struct ieee80211com *ic = &sc->sc_ic; 186 struct ieee80211vap *vap = ni->ni_vap; 187 struct ieee80211_channel *chan; 188 enum ieee80211_phymode mode; 189 uint8_t raid; 190 191 chan = (ni->ni_chan != IEEE80211_CHAN_ANYC) ? 192 ni->ni_chan : ic->ic_curchan; 193 mode = ieee80211_chan2mode(chan); 194 195 /* NB: group addressed frames are done at 11bg rates for now */ 196 if (ismcast || !(ni->ni_flags & IEEE80211_NODE_HT)) { 197 switch (mode) { 198 case IEEE80211_MODE_11A: 199 case IEEE80211_MODE_11B: 200 case IEEE80211_MODE_11G: 201 break; 202 case IEEE80211_MODE_11NA: 203 mode = IEEE80211_MODE_11A; 204 break; 205 case IEEE80211_MODE_11NG: 206 mode = IEEE80211_MODE_11G; 207 break; 208 case IEEE80211_MODE_VHT_5GHZ: 209 mode = IEEE80211_MODE_VHT_5GHZ; 210 break; 211 default: 212 device_printf(sc->sc_dev, "unknown mode(1) %d!\n", 213 ic->ic_curmode); 214 return; 215 } 216 } 217 218 switch (mode) { 219 case IEEE80211_MODE_11A: 220 raid = R12A_RAID_11G; 221 break; 222 case IEEE80211_MODE_11B: 223 raid = R12A_RAID_11B; 224 break; 225 case IEEE80211_MODE_11G: 226 if (vap->iv_flags & IEEE80211_F_PUREG) 227 raid = R12A_RAID_11G; 228 else 229 raid = R12A_RAID_11BG; 230 break; 231 case IEEE80211_MODE_11NA: 232 if (sc->ntxchains == 1) 233 raid = R12A_RAID_11GN_1; 234 else 235 raid = R12A_RAID_11GN_2; 236 break; 237 case IEEE80211_MODE_11NG: 238 if (sc->ntxchains == 1) { 239 if (IEEE80211_IS_CHAN_HT40(chan)) 240 raid = R12A_RAID_11BGN_1_40; 241 else 242 raid = R12A_RAID_11BGN_1; 243 } else { 244 if (IEEE80211_IS_CHAN_HT40(chan)) 245 raid = R12A_RAID_11BGN_2_40; 246 else 247 raid = R12A_RAID_11BGN_2; 248 } 249 break; 250 case IEEE80211_MODE_VHT_5GHZ: 251 if (sc->ntxchains == 1) 252 raid = R12A_RAID_11AC_1; 253 else 254 raid = R12A_RAID_11AC_2; 255 break; 256 default: 257 device_printf(sc->sc_dev, "unknown mode(2) %d!\n", mode); 258 return; 259 } 260 261 txd->txdw1 |= htole32(SM(R12A_TXDW1_RAID, raid)); 262 } 263 264 static void 265 r12a_tx_set_sgi(struct rtwn_softc *sc, void *buf, struct ieee80211_node *ni) 266 { 267 struct r12a_tx_desc *txd = (struct r12a_tx_desc *)buf; 268 269 /* TODO: VHT 20/40/80 checks */ 270 271 /* 272 * Only enable short-GI if we're transmitting in that 273 * width to that node. 274 * 275 * Specifically, do not enable shortgi for 20MHz if 276 * we're attempting to transmit at 40MHz. 277 */ 278 if (ieee80211_ht_check_tx_ht40(ni)) { 279 if (ieee80211_ht_check_tx_shortgi_40(ni)) 280 txd->txdw5 |= htole32(R12A_TXDW5_DATA_SHORT); 281 } else if (ieee80211_ht_check_tx_ht(ni)) { 282 if (ieee80211_ht_check_tx_shortgi_20(ni)) 283 txd->txdw5 |= htole32(R12A_TXDW5_DATA_SHORT); 284 } 285 } 286 287 static void 288 r12a_tx_set_ldpc(struct rtwn_softc *sc, struct r12a_tx_desc *txd, 289 struct ieee80211_node *ni) 290 { 291 struct ieee80211vap *vap = ni->ni_vap; 292 293 if ((vap->iv_flags_ht & IEEE80211_FHT_LDPC_TX) && 294 (ni->ni_htcap & IEEE80211_HTCAP_LDPC)) 295 txd->txdw5 |= htole32(R12A_TXDW5_DATA_LDPC); 296 } 297 298 static int 299 r12a_calculate_tx_agg_window(struct rtwn_softc *sc, 300 const struct ieee80211_node *ni, int tid) 301 { 302 const struct ieee80211_tx_ampdu *tap; 303 int wnd; 304 305 tap = &ni->ni_tx_ampdu[tid]; 306 307 /* 308 * BAW is (MAX_AGG * 2) + 1, hence the /2 here. 309 * Ensure we don't send 0 or more than 64. 310 */ 311 wnd = tap->txa_wnd / 2; 312 if (wnd == 0) 313 wnd = 1; 314 else if (wnd > 0x1f) 315 wnd = 0x1f; 316 317 return (wnd); 318 } 319 320 void 321 r12a_fill_tx_desc(struct rtwn_softc *sc, struct ieee80211_node *ni, 322 struct mbuf *m, void *buf, uint8_t ridx, bool force_rate, int maxretry) 323 { 324 struct ieee80211com *ic = &sc->sc_ic; 325 struct ieee80211vap *vap = ni->ni_vap; 326 struct rtwn_vap *uvp = RTWN_VAP(vap); 327 struct ieee80211_frame *wh; 328 struct r12a_tx_desc *txd; 329 enum ieee80211_protmode prot; 330 uint8_t type, tid, qos, qsel; 331 int hasqos, ismcast, macid; 332 333 wh = mtod(m, struct ieee80211_frame *); 334 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK; 335 hasqos = IEEE80211_QOS_HAS_SEQ(wh); 336 ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1); 337 338 /* Select TX ring for this frame. */ 339 if (hasqos) { 340 qos = ((const struct ieee80211_qosframe *)wh)->i_qos[0]; 341 tid = qos & IEEE80211_QOS_TID; 342 } else { 343 qos = 0; 344 tid = 0; 345 } 346 347 /* Fill Tx descriptor. */ 348 txd = (struct r12a_tx_desc *)buf; 349 txd->flags0 |= R12A_FLAGS0_LSG | R12A_FLAGS0_FSG; 350 if (ismcast) 351 txd->flags0 |= R12A_FLAGS0_BMCAST; 352 353 if (!ismcast) { 354 /* Unicast frame, check if an ACK is expected. */ 355 if (!qos || (qos & IEEE80211_QOS_ACKPOLICY) != 356 IEEE80211_QOS_ACKPOLICY_NOACK) { 357 txd->txdw4 = htole32(R12A_TXDW4_RETRY_LMT_ENA); 358 txd->txdw4 |= htole32(SM(R12A_TXDW4_RETRY_LMT, 359 maxretry)); 360 } 361 362 struct rtwn_node *un = RTWN_NODE(ni); 363 macid = un->id; 364 365 if (type == IEEE80211_FC0_TYPE_DATA) { 366 qsel = tid % RTWN_MAX_TID; 367 368 if (m->m_flags & M_AMPDU_MPDU) { 369 txd->txdw2 |= htole32(R12A_TXDW2_AGGEN); 370 txd->txdw2 |= htole32(SM(R12A_TXDW2_AMPDU_DEN, 371 ieee80211_ht_get_node_ampdu_density(ni))); 372 txd->txdw3 |= htole32(SM(R12A_TXDW3_MAX_AGG, 373 r12a_calculate_tx_agg_window(sc, ni, tid))); 374 } else 375 txd->txdw2 |= htole32(R12A_TXDW2_AGGBK); 376 377 if (sc->sc_ratectl == RTWN_RATECTL_NET80211) { 378 txd->txdw2 |= htole32(R12A_TXDW2_SPE_RPT); 379 sc->sc_tx_n_active++; 380 } 381 382 if (RTWN_RATE_IS_CCK(ridx) && ridx != RTWN_RIDX_CCK1 && 383 (ic->ic_flags & IEEE80211_F_SHPREAMBLE)) 384 txd->txdw5 |= htole32(R12A_TXDW5_DATA_SHORT); 385 386 prot = IEEE80211_PROT_NONE; 387 if (RTWN_RATE_IS_VHT(ridx)) { 388 r12a_tx_set_vht_bw(sc, txd, ni); 389 /* XXX TODO: sgi */ 390 /* XXX TODO: ldpc */ 391 prot = ic->ic_htprotmode; 392 } else if (RTWN_RATE_IS_HT(ridx)) { 393 r12a_tx_set_ht40(sc, txd, ni); 394 r12a_tx_set_sgi(sc, txd, ni); 395 r12a_tx_set_ldpc(sc, txd, ni); 396 prot = ic->ic_htprotmode; 397 } else if (ic->ic_flags & IEEE80211_F_USEPROT) 398 prot = ic->ic_protmode; 399 400 /* XXX fix last comparison for A-MSDU (in net80211) */ 401 /* XXX A-MPDU? */ 402 if (m->m_pkthdr.len + IEEE80211_CRC_LEN > 403 vap->iv_rtsthreshold && 404 vap->iv_rtsthreshold != IEEE80211_RTS_MAX) 405 prot = IEEE80211_PROT_RTSCTS; 406 407 if (prot != IEEE80211_PROT_NONE) 408 r12a_tx_protection(sc, txd, prot, ridx); 409 } else /* IEEE80211_FC0_TYPE_MGT */ 410 qsel = R12A_TXDW1_QSEL_MGNT; 411 } else { 412 macid = RTWN_MACID_BC; 413 qsel = R12A_TXDW1_QSEL_MGNT; 414 } 415 416 txd->txdw1 |= htole32(SM(R12A_TXDW1_QSEL, qsel)); 417 txd->txdw1 |= htole32(SM(R12A_TXDW1_MACID, macid)); 418 txd->txdw4 |= htole32(SM(R12A_TXDW4_DATARATE, ridx)); 419 /* Data rate fallback limit (max). */ 420 txd->txdw4 |= htole32(SM(R12A_TXDW4_DATARATE_FB_LMT, 0x1f)); 421 /* XXX recheck for non-21au */ 422 txd->txdw6 |= htole32(SM(R21A_TXDW6_MBSSID, uvp->id)); 423 r12a_tx_raid(sc, txd, ni, ismcast); 424 425 /* Force this rate if needed. */ 426 if (sc->sc_ratectl != RTWN_RATECTL_FW) 427 txd->txdw3 |= htole32(R12A_TXDW3_DRVRATE); 428 429 if (!hasqos) { 430 /* Use HW sequence numbering for non-QoS frames. */ 431 txd->txdw8 |= htole32(R12A_TXDW8_HWSEQ_EN); 432 txd->txdw3 |= htole32(SM(R12A_TXDW3_SEQ_SEL, uvp->id)); 433 } else { 434 uint16_t seqno; 435 436 if (m->m_flags & M_AMPDU_MPDU) { 437 seqno = ni->ni_txseqs[tid]; 438 ni->ni_txseqs[tid]++; 439 } else 440 seqno = M_SEQNO_GET(m) % IEEE80211_SEQ_RANGE; 441 442 /* Set sequence number. */ 443 txd->txdw9 |= htole32(SM(R12A_TXDW9_SEQ, seqno)); 444 } 445 } 446 447 void 448 r12a_fill_tx_desc_raw(struct rtwn_softc *sc, struct ieee80211_node *ni, 449 struct mbuf *m, void *buf, const struct ieee80211_bpf_params *params) 450 { 451 struct ieee80211vap *vap = ni->ni_vap; 452 struct rtwn_vap *uvp = RTWN_VAP(vap); 453 struct ieee80211_frame *wh; 454 struct r12a_tx_desc *txd; 455 uint8_t ridx; 456 int ismcast; 457 458 /* XXX TODO: 11n checks, matching rtwn_fill_tx_desc() */ 459 460 wh = mtod(m, struct ieee80211_frame *); 461 ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1); 462 ridx = rate2ridx(params->ibp_rate0); 463 464 /* Fill Tx descriptor. */ 465 txd = (struct r12a_tx_desc *)buf; 466 txd->flags0 |= R12A_FLAGS0_LSG | R12A_FLAGS0_FSG; 467 if (ismcast) 468 txd->flags0 |= R12A_FLAGS0_BMCAST; 469 470 if ((params->ibp_flags & IEEE80211_BPF_NOACK) == 0) { 471 txd->txdw4 = htole32(R12A_TXDW4_RETRY_LMT_ENA); 472 txd->txdw4 |= htole32(SM(R12A_TXDW4_RETRY_LMT, 473 params->ibp_try0)); 474 } 475 if (params->ibp_flags & IEEE80211_BPF_RTS) 476 r12a_tx_protection(sc, txd, IEEE80211_PROT_RTSCTS, ridx); 477 if (params->ibp_flags & IEEE80211_BPF_CTS) 478 r12a_tx_protection(sc, txd, IEEE80211_PROT_CTSONLY, ridx); 479 480 txd->txdw1 |= htole32(SM(R12A_TXDW1_MACID, RTWN_MACID_BC)); 481 txd->txdw1 |= htole32(SM(R12A_TXDW1_QSEL, R12A_TXDW1_QSEL_MGNT)); 482 483 /* Set TX rate index. */ 484 txd->txdw4 |= htole32(SM(R12A_TXDW4_DATARATE, ridx)); 485 txd->txdw4 |= htole32(SM(R12A_TXDW4_DATARATE_FB_LMT, 0x1f)); 486 txd->txdw6 |= htole32(SM(R21A_TXDW6_MBSSID, uvp->id)); 487 txd->txdw3 |= htole32(R12A_TXDW3_DRVRATE); 488 r12a_tx_raid(sc, txd, ni, ismcast); 489 490 if (!IEEE80211_QOS_HAS_SEQ(wh)) { 491 /* Use HW sequence numbering for non-QoS frames. */ 492 txd->txdw8 |= htole32(R12A_TXDW8_HWSEQ_EN); 493 txd->txdw3 |= htole32(SM(R12A_TXDW3_SEQ_SEL, uvp->id)); 494 } else { 495 /* Set sequence number. */ 496 txd->txdw9 |= htole32(SM(R12A_TXDW9_SEQ, 497 M_SEQNO_GET(m) % IEEE80211_SEQ_RANGE)); 498 } 499 } 500 501 void 502 r12a_fill_tx_desc_null(struct rtwn_softc *sc, void *buf, int is11b, int qos, 503 int id) 504 { 505 struct r12a_tx_desc *txd = (struct r12a_tx_desc *)buf; 506 507 txd->flags0 = R12A_FLAGS0_FSG | R12A_FLAGS0_LSG | R12A_FLAGS0_OWN; 508 txd->txdw1 = htole32( 509 SM(R12A_TXDW1_QSEL, R12A_TXDW1_QSEL_MGNT)); 510 511 txd->txdw3 = htole32(R12A_TXDW3_DRVRATE); 512 txd->txdw6 = htole32(SM(R21A_TXDW6_MBSSID, id)); 513 if (is11b) { 514 txd->txdw4 = htole32(SM(R12A_TXDW4_DATARATE, 515 RTWN_RIDX_CCK1)); 516 } else { 517 txd->txdw4 = htole32(SM(R12A_TXDW4_DATARATE, 518 RTWN_RIDX_OFDM6)); 519 } 520 521 if (!qos) { 522 txd->txdw8 = htole32(R12A_TXDW8_HWSEQ_EN); 523 txd->txdw3 |= htole32(SM(R12A_TXDW3_SEQ_SEL, id)); 524 } 525 } 526 527 uint8_t 528 r12a_tx_radiotap_flags(const void *buf) 529 { 530 const struct r12a_tx_desc *txd = buf; 531 uint8_t flags, rate; 532 533 if (!(txd->txdw5 & htole32(R12A_TXDW5_DATA_SHORT))) 534 return (0); 535 536 rate = MS(le32toh(txd->txdw4), R12A_TXDW4_DATARATE); 537 if (RTWN_RATE_IS_CCK(rate)) 538 flags = IEEE80211_RADIOTAP_F_SHORTPRE; 539 else 540 flags = IEEE80211_RADIOTAP_F_SHORTGI; 541 return (flags); 542 } 543