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 (ieee80211_ht_check_tx_ht40(ni)) { 70 int extc_offset; 71 72 extc_offset = r92c_tx_get_sco(sc, ni->ni_chan); 73 txd->txdw4 |= htole32(R92C_TXDW4_DATA_BW40); 74 txd->txdw4 |= htole32(SM(R92C_TXDW4_DATA_SCO, extc_offset)); 75 } 76 } 77 78 static void 79 r92c_tx_protection(struct rtwn_softc *sc, struct r92c_tx_desc *txd, 80 enum ieee80211_protmode mode, uint8_t ridx) 81 { 82 struct ieee80211com *ic = &sc->sc_ic; 83 uint8_t rate; 84 85 switch (mode) { 86 case IEEE80211_PROT_CTSONLY: 87 txd->txdw4 |= htole32(R92C_TXDW4_CTS2SELF); 88 break; 89 case IEEE80211_PROT_RTSCTS: 90 txd->txdw4 |= htole32(R92C_TXDW4_RTSEN); 91 break; 92 default: 93 break; 94 } 95 96 if (mode == IEEE80211_PROT_CTSONLY || 97 mode == IEEE80211_PROT_RTSCTS) { 98 if (RTWN_RATE_IS_HT(ridx)) 99 rate = rtwn_ctl_mcsrate(ic->ic_rt, ridx); 100 else 101 rate = ieee80211_ctl_rate(ic->ic_rt, ridx2rate[ridx]); 102 ridx = rate2ridx(IEEE80211_RV(rate)); 103 104 txd->txdw4 |= htole32(SM(R92C_TXDW4_RTSRATE, ridx)); 105 /* RTS rate fallback limit (max). */ 106 txd->txdw5 |= htole32(SM(R92C_TXDW5_RTSRATE_FB_LMT, 0xf)); 107 108 if (RTWN_RATE_IS_CCK(ridx) && ridx != RTWN_RIDX_CCK1 && 109 (ic->ic_flags & IEEE80211_F_SHPREAMBLE)) 110 txd->txdw4 |= htole32(R92C_TXDW4_RTS_SHORT); 111 } 112 } 113 114 static void 115 r92c_tx_raid(struct rtwn_softc *sc, struct r92c_tx_desc *txd, 116 struct ieee80211_node *ni, int ismcast) 117 { 118 struct ieee80211com *ic = &sc->sc_ic; 119 struct ieee80211vap *vap = ni->ni_vap; 120 struct ieee80211_channel *chan; 121 enum ieee80211_phymode mode; 122 uint8_t raid; 123 124 chan = (ni->ni_chan != IEEE80211_CHAN_ANYC) ? 125 ni->ni_chan : ic->ic_curchan; 126 mode = ieee80211_chan2mode(chan); 127 128 /* NB: group addressed frames are done at 11bg rates for now */ 129 if (ismcast || !(ni->ni_flags & IEEE80211_NODE_HT)) { 130 switch (mode) { 131 case IEEE80211_MODE_11B: 132 case IEEE80211_MODE_11G: 133 break; 134 case IEEE80211_MODE_11NG: 135 mode = IEEE80211_MODE_11G; 136 break; 137 default: 138 device_printf(sc->sc_dev, "unknown mode(1) %d!\n", 139 ic->ic_curmode); 140 return; 141 } 142 } 143 144 switch (mode) { 145 case IEEE80211_MODE_11B: 146 raid = R92C_RAID_11B; 147 break; 148 case IEEE80211_MODE_11G: 149 if (vap->iv_flags & IEEE80211_F_PUREG) 150 raid = R92C_RAID_11G; 151 else 152 raid = R92C_RAID_11BG; 153 break; 154 case IEEE80211_MODE_11NG: 155 if (vap->iv_flags_ht & IEEE80211_FHT_PUREN) 156 raid = R92C_RAID_11N; 157 else 158 raid = R92C_RAID_11BGN; 159 break; 160 default: 161 device_printf(sc->sc_dev, "unknown mode(2) %d!\n", mode); 162 return; 163 } 164 165 txd->txdw1 |= htole32(SM(R92C_TXDW1_RAID, raid)); 166 } 167 168 /* XXX move to device-independent layer */ 169 static void 170 r92c_tx_set_sgi(struct rtwn_softc *sc, void *buf, struct ieee80211_node *ni) 171 { 172 struct r92c_tx_desc *txd = (struct r92c_tx_desc *)buf; 173 174 /* 175 * Only enable short-GI if we're transmitting in that 176 * width to that node. 177 * 178 * Specifically, do not enable shortgi for 20MHz if 179 * we're attempting to transmit at 40MHz. 180 */ 181 if (ieee80211_ht_check_tx_ht40(ni)) { 182 if (ieee80211_ht_check_tx_shortgi_40(ni)) 183 txd->txdw5 |= htole32(R92C_TXDW5_SGI); 184 } else if (ieee80211_ht_check_tx_ht(ni)) { 185 if (ieee80211_ht_check_tx_shortgi_20(ni)) 186 txd->txdw5 |= htole32(R92C_TXDW5_SGI); 187 } 188 } 189 190 void 191 r92c_tx_enable_ampdu(void *buf, int enable) 192 { 193 struct r92c_tx_desc *txd = (struct r92c_tx_desc *)buf; 194 195 if (enable) 196 txd->txdw1 |= htole32(R92C_TXDW1_AGGEN); 197 else 198 txd->txdw1 |= htole32(R92C_TXDW1_AGGBK); 199 } 200 201 void 202 r92c_tx_setup_hwseq(void *buf) 203 { 204 struct r92c_tx_desc *txd = (struct r92c_tx_desc *)buf; 205 206 txd->txdw4 |= htole32(R92C_TXDW4_HWSEQ_EN); 207 } 208 209 void 210 r92c_tx_setup_macid(void *buf, int id) 211 { 212 struct r92c_tx_desc *txd = (struct r92c_tx_desc *)buf; 213 214 txd->txdw1 |= htole32(SM(R92C_TXDW1_MACID, id)); 215 } 216 217 static int 218 r92c_calculate_tx_agg_window(struct rtwn_softc *sc, 219 const struct ieee80211_node *ni, int tid) 220 { 221 const struct ieee80211_tx_ampdu *tap; 222 int wnd; 223 224 tap = &ni->ni_tx_ampdu[tid]; 225 226 /* 227 * BAW is (MAX_AGG * 2) + 1, hence the /2 here. 228 * Ensure we don't send 0 or more than 64. 229 */ 230 wnd = tap->txa_wnd / 2; 231 if (wnd == 0) 232 wnd = 1; 233 else if (wnd > 0x1f) 234 wnd = 0x1f; 235 236 return (wnd); 237 } 238 239 void 240 r92c_fill_tx_desc(struct rtwn_softc *sc, struct ieee80211_node *ni, 241 struct mbuf *m, void *buf, uint8_t ridx, int maxretry) 242 { 243 #ifndef RTWN_WITHOUT_UCODE 244 struct r92c_softc *rs = sc->sc_priv; 245 #endif 246 struct ieee80211com *ic = &sc->sc_ic; 247 struct ieee80211vap *vap = ni->ni_vap; 248 struct rtwn_vap *uvp = RTWN_VAP(vap); 249 struct ieee80211_frame *wh; 250 struct r92c_tx_desc *txd; 251 enum ieee80211_protmode prot; 252 uint8_t type, tid, qos, qsel; 253 int hasqos, ismcast, macid; 254 255 wh = mtod(m, struct ieee80211_frame *); 256 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK; 257 hasqos = IEEE80211_QOS_HAS_SEQ(wh); 258 ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1); 259 260 /* Select TX ring for this frame. */ 261 if (hasqos) { 262 qos = ((const struct ieee80211_qosframe *)wh)->i_qos[0]; 263 tid = qos & IEEE80211_QOS_TID; 264 } else { 265 qos = 0; 266 tid = 0; 267 } 268 269 /* Fill Tx descriptor. */ 270 txd = (struct r92c_tx_desc *)buf; 271 txd->flags0 |= R92C_FLAGS0_LSG | R92C_FLAGS0_FSG; 272 if (ismcast) 273 txd->flags0 |= R92C_FLAGS0_BMCAST; 274 275 if (!ismcast) { 276 /* Unicast frame, check if an ACK is expected. */ 277 if (!qos || (qos & IEEE80211_QOS_ACKPOLICY) != 278 IEEE80211_QOS_ACKPOLICY_NOACK) { 279 txd->txdw5 |= htole32(R92C_TXDW5_RTY_LMT_ENA); 280 txd->txdw5 |= htole32(SM(R92C_TXDW5_RTY_LMT, 281 maxretry)); 282 } 283 284 struct rtwn_node *un = RTWN_NODE(ni); 285 macid = un->id; 286 287 if (type == IEEE80211_FC0_TYPE_DATA) { 288 qsel = tid % RTWN_MAX_TID; 289 290 rtwn_r92c_tx_enable_ampdu(sc, buf, 291 (m->m_flags & M_AMPDU_MPDU) != 0); 292 if (m->m_flags & M_AMPDU_MPDU) { 293 txd->txdw2 |= htole32(SM(R92C_TXDW2_AMPDU_DEN, 294 ieee80211_ht_get_node_ampdu_density(ni))); 295 txd->txdw6 |= htole32(SM(R92C_TXDW6_MAX_AGG, 296 r92c_calculate_tx_agg_window(sc, ni, tid))); 297 } 298 if (sc->sc_ratectl == RTWN_RATECTL_NET80211) { 299 txd->txdw2 |= htole32(R92C_TXDW2_CCX_RPT); 300 sc->sc_tx_n_active++; 301 #ifndef RTWN_WITHOUT_UCODE 302 rs->rs_c2h_pending++; 303 #endif 304 } 305 306 if (RTWN_RATE_IS_CCK(ridx) && ridx != RTWN_RIDX_CCK1 && 307 (ic->ic_flags & IEEE80211_F_SHPREAMBLE)) 308 txd->txdw4 |= htole32(R92C_TXDW4_DATA_SHPRE); 309 310 prot = IEEE80211_PROT_NONE; 311 if (RTWN_RATE_IS_HT(ridx)) { 312 r92c_tx_set_ht40(sc, txd, ni); 313 r92c_tx_set_sgi(sc, txd, ni); 314 prot = ic->ic_htprotmode; 315 } else if (ic->ic_flags & IEEE80211_F_USEPROT) 316 prot = ic->ic_protmode; 317 318 /* XXX fix last comparison for A-MSDU (in net80211) */ 319 /* XXX A-MPDU? */ 320 if (m->m_pkthdr.len + IEEE80211_CRC_LEN > 321 vap->iv_rtsthreshold && 322 vap->iv_rtsthreshold != IEEE80211_RTS_MAX) 323 prot = IEEE80211_PROT_RTSCTS; 324 325 /* NB: checks for ht40 / short bits (set above). */ 326 if (prot != IEEE80211_PROT_NONE) 327 r92c_tx_protection(sc, txd, prot, ridx); 328 } else /* IEEE80211_FC0_TYPE_MGT */ 329 qsel = R92C_TXDW1_QSEL_MGNT; 330 } else { 331 macid = RTWN_MACID_BC; 332 qsel = R92C_TXDW1_QSEL_MGNT; 333 } 334 335 txd->txdw1 |= htole32(SM(R92C_TXDW1_QSEL, qsel)); 336 337 rtwn_r92c_tx_setup_macid(sc, txd, macid); 338 txd->txdw5 |= htole32(SM(R92C_TXDW5_DATARATE, ridx)); 339 /* Data rate fallback limit (max). */ 340 txd->txdw5 |= htole32(SM(R92C_TXDW5_DATARATE_FB_LMT, 0x1f)); 341 txd->txdw4 |= htole32(SM(R92C_TXDW4_PORT_ID, uvp->id)); 342 r92c_tx_raid(sc, txd, ni, ismcast); 343 344 /* Force this rate if needed. */ 345 if (sc->sc_ratectl != RTWN_RATECTL_FW) 346 txd->txdw4 |= htole32(R92C_TXDW4_DRVRATE); 347 348 if (!hasqos) { 349 /* Use HW sequence numbering for non-QoS frames. */ 350 rtwn_r92c_tx_setup_hwseq(sc, txd); 351 txd->txdw4 |= htole32(SM(R92C_TXDW4_SEQ_SEL, uvp->id)); 352 } else { 353 uint16_t seqno; 354 355 if (m->m_flags & M_AMPDU_MPDU) { 356 seqno = ni->ni_txseqs[tid] % IEEE80211_SEQ_RANGE; 357 ni->ni_txseqs[tid]++; 358 } else 359 seqno = M_SEQNO_GET(m) % IEEE80211_SEQ_RANGE; 360 361 /* Set sequence number. */ 362 txd->txdseq = htole16(seqno); 363 } 364 } 365 366 void 367 r92c_fill_tx_desc_raw(struct rtwn_softc *sc, struct ieee80211_node *ni, 368 struct mbuf *m, void *buf, const struct ieee80211_bpf_params *params) 369 { 370 struct ieee80211vap *vap = ni->ni_vap; 371 struct rtwn_vap *uvp = RTWN_VAP(vap); 372 struct ieee80211_frame *wh; 373 struct r92c_tx_desc *txd; 374 uint8_t ridx; 375 int ismcast; 376 377 /* XXX TODO: 11n checks, matching r92c_fill_tx_desc() */ 378 379 wh = mtod(m, struct ieee80211_frame *); 380 ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1); 381 ridx = rate2ridx(params->ibp_rate0); 382 383 /* Fill Tx descriptor. */ 384 txd = (struct r92c_tx_desc *)buf; 385 txd->flags0 |= R92C_FLAGS0_LSG | R92C_FLAGS0_FSG; 386 if (ismcast) 387 txd->flags0 |= R92C_FLAGS0_BMCAST; 388 389 if ((params->ibp_flags & IEEE80211_BPF_NOACK) == 0) { 390 txd->txdw5 |= htole32(R92C_TXDW5_RTY_LMT_ENA); 391 txd->txdw5 |= htole32(SM(R92C_TXDW5_RTY_LMT, 392 params->ibp_try0)); 393 } 394 if (params->ibp_flags & IEEE80211_BPF_RTS) 395 r92c_tx_protection(sc, txd, IEEE80211_PROT_RTSCTS, ridx); 396 if (params->ibp_flags & IEEE80211_BPF_CTS) 397 r92c_tx_protection(sc, txd, IEEE80211_PROT_CTSONLY, ridx); 398 399 rtwn_r92c_tx_setup_macid(sc, txd, RTWN_MACID_BC); 400 txd->txdw1 |= htole32(SM(R92C_TXDW1_QSEL, R92C_TXDW1_QSEL_MGNT)); 401 402 /* Set TX rate index. */ 403 txd->txdw5 |= htole32(SM(R92C_TXDW5_DATARATE, ridx)); 404 txd->txdw5 |= htole32(SM(R92C_TXDW5_DATARATE_FB_LMT, 0x1f)); 405 txd->txdw4 |= htole32(SM(R92C_TXDW4_PORT_ID, uvp->id)); 406 txd->txdw4 |= htole32(R92C_TXDW4_DRVRATE); 407 r92c_tx_raid(sc, txd, ni, ismcast); 408 409 if (!IEEE80211_QOS_HAS_SEQ(wh)) { 410 /* Use HW sequence numbering for non-QoS frames. */ 411 rtwn_r92c_tx_setup_hwseq(sc, txd); 412 txd->txdw4 |= htole32(SM(R92C_TXDW4_SEQ_SEL, uvp->id)); 413 } else { 414 /* Set sequence number. */ 415 txd->txdseq |= htole16(M_SEQNO_GET(m) % IEEE80211_SEQ_RANGE); 416 } 417 } 418 419 void 420 r92c_fill_tx_desc_null(struct rtwn_softc *sc, void *buf, int is11b, 421 int qos, int id) 422 { 423 struct r92c_tx_desc *txd = (struct r92c_tx_desc *)buf; 424 425 txd->flags0 = R92C_FLAGS0_FSG | R92C_FLAGS0_LSG | R92C_FLAGS0_OWN; 426 txd->txdw1 = htole32( 427 SM(R92C_TXDW1_QSEL, R92C_TXDW1_QSEL_MGNT)); 428 429 txd->txdw4 = htole32(R92C_TXDW4_DRVRATE); 430 txd->txdw4 |= htole32(SM(R92C_TXDW4_PORT_ID, id)); 431 if (is11b) { 432 txd->txdw5 = htole32(SM(R92C_TXDW5_DATARATE, 433 RTWN_RIDX_CCK1)); 434 } else { 435 txd->txdw5 = htole32(SM(R92C_TXDW5_DATARATE, 436 RTWN_RIDX_OFDM6)); 437 } 438 439 if (!qos) { 440 rtwn_r92c_tx_setup_hwseq(sc, txd); 441 txd->txdw4 |= htole32(SM(R92C_TXDW4_SEQ_SEL, id)); 442 } 443 } 444 445 uint8_t 446 r92c_tx_radiotap_flags(const void *buf) 447 { 448 const struct r92c_tx_desc *txd = buf; 449 uint8_t flags; 450 451 flags = 0; 452 if (txd->txdw4 & htole32(R92C_TXDW4_DATA_SHPRE)) 453 flags |= IEEE80211_RADIOTAP_F_SHORTPRE; 454 if (txd->txdw5 & htole32(R92C_TXDW5_SGI)) 455 flags |= IEEE80211_RADIOTAP_F_SHORTGI; 456 return (flags); 457 } 458