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 /* XXX does not belong here */ 217 /* XXX temporary (I hope) */ 218 /* Force CCK1 for RTS / CTS frames (driver bug) */ 219 txd->txdw4 &= ~htole32(SM(R92C_TXDW4_RTSRATE, R92C_TXDW4_RTSRATE_M)); 220 txd->txdw4 &= ~htole32(R92C_TXDW4_RTS_SHORT); 221 } 222 223 static int 224 r92c_calculate_tx_agg_window(struct rtwn_softc *sc, 225 const struct ieee80211_node *ni, int tid) 226 { 227 const struct ieee80211_tx_ampdu *tap; 228 int wnd; 229 230 tap = &ni->ni_tx_ampdu[tid]; 231 232 /* 233 * BAW is (MAX_AGG * 2) + 1, hence the /2 here. 234 * Ensure we don't send 0 or more than 64. 235 */ 236 wnd = tap->txa_wnd / 2; 237 if (wnd == 0) 238 wnd = 1; 239 else if (wnd > 0x1f) 240 wnd = 0x1f; 241 242 return (wnd); 243 } 244 245 void 246 r92c_fill_tx_desc(struct rtwn_softc *sc, struct ieee80211_node *ni, 247 struct mbuf *m, void *buf, uint8_t ridx, int maxretry) 248 { 249 #ifndef RTWN_WITHOUT_UCODE 250 struct r92c_softc *rs = sc->sc_priv; 251 #endif 252 struct ieee80211com *ic = &sc->sc_ic; 253 struct ieee80211vap *vap = ni->ni_vap; 254 struct rtwn_vap *uvp = RTWN_VAP(vap); 255 struct ieee80211_frame *wh; 256 struct r92c_tx_desc *txd; 257 enum ieee80211_protmode prot; 258 uint8_t type, tid, qos, qsel; 259 int hasqos, ismcast, macid; 260 261 wh = mtod(m, struct ieee80211_frame *); 262 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK; 263 hasqos = IEEE80211_QOS_HAS_SEQ(wh); 264 ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1); 265 266 /* Select TX ring for this frame. */ 267 if (hasqos) { 268 qos = ((const struct ieee80211_qosframe *)wh)->i_qos[0]; 269 tid = qos & IEEE80211_QOS_TID; 270 } else { 271 qos = 0; 272 tid = 0; 273 } 274 275 /* Fill Tx descriptor. */ 276 txd = (struct r92c_tx_desc *)buf; 277 txd->flags0 |= R92C_FLAGS0_LSG | R92C_FLAGS0_FSG; 278 if (ismcast) 279 txd->flags0 |= R92C_FLAGS0_BMCAST; 280 281 if (!ismcast) { 282 /* Unicast frame, check if an ACK is expected. */ 283 if (!qos || (qos & IEEE80211_QOS_ACKPOLICY) != 284 IEEE80211_QOS_ACKPOLICY_NOACK) { 285 txd->txdw5 |= htole32(R92C_TXDW5_RTY_LMT_ENA); 286 txd->txdw5 |= htole32(SM(R92C_TXDW5_RTY_LMT, 287 maxretry)); 288 } 289 290 struct rtwn_node *un = RTWN_NODE(ni); 291 macid = un->id; 292 293 if (type == IEEE80211_FC0_TYPE_DATA) { 294 qsel = tid % RTWN_MAX_TID; 295 296 rtwn_r92c_tx_enable_ampdu(sc, buf, 297 (m->m_flags & M_AMPDU_MPDU) != 0); 298 if (m->m_flags & M_AMPDU_MPDU) { 299 txd->txdw2 |= htole32(SM(R92C_TXDW2_AMPDU_DEN, 300 ieee80211_ht_get_node_ampdu_density(ni))); 301 txd->txdw6 |= htole32(SM(R92C_TXDW6_MAX_AGG, 302 r92c_calculate_tx_agg_window(sc, ni, tid))); 303 } 304 if (sc->sc_ratectl == RTWN_RATECTL_NET80211) { 305 txd->txdw2 |= htole32(R92C_TXDW2_CCX_RPT); 306 sc->sc_tx_n_active++; 307 #ifndef RTWN_WITHOUT_UCODE 308 rs->rs_c2h_pending++; 309 #endif 310 } 311 312 if (RTWN_RATE_IS_CCK(ridx) && ridx != RTWN_RIDX_CCK1 && 313 (ic->ic_flags & IEEE80211_F_SHPREAMBLE)) 314 txd->txdw4 |= htole32(R92C_TXDW4_DATA_SHPRE); 315 316 prot = IEEE80211_PROT_NONE; 317 if (RTWN_RATE_IS_HT(ridx)) { 318 r92c_tx_set_ht40(sc, txd, ni); 319 r92c_tx_set_sgi(sc, txd, ni); 320 prot = ic->ic_htprotmode; 321 } else if (ic->ic_flags & IEEE80211_F_USEPROT) 322 prot = ic->ic_protmode; 323 324 /* XXX fix last comparison for A-MSDU (in net80211) */ 325 /* XXX A-MPDU? */ 326 if (m->m_pkthdr.len + IEEE80211_CRC_LEN > 327 vap->iv_rtsthreshold && 328 vap->iv_rtsthreshold != IEEE80211_RTS_MAX) 329 prot = IEEE80211_PROT_RTSCTS; 330 331 /* NB: checks for ht40 / short bits (set above). */ 332 if (prot != IEEE80211_PROT_NONE) 333 r92c_tx_protection(sc, txd, prot, ridx); 334 } else /* IEEE80211_FC0_TYPE_MGT */ 335 qsel = R92C_TXDW1_QSEL_MGNT; 336 } else { 337 macid = RTWN_MACID_BC; 338 qsel = R92C_TXDW1_QSEL_MGNT; 339 } 340 341 txd->txdw1 |= htole32(SM(R92C_TXDW1_QSEL, qsel)); 342 343 rtwn_r92c_tx_setup_macid(sc, txd, macid); 344 txd->txdw5 |= htole32(SM(R92C_TXDW5_DATARATE, ridx)); 345 /* Data rate fallback limit (max). */ 346 txd->txdw5 |= htole32(SM(R92C_TXDW5_DATARATE_FB_LMT, 0x1f)); 347 txd->txdw4 |= htole32(SM(R92C_TXDW4_PORT_ID, uvp->id)); 348 r92c_tx_raid(sc, txd, ni, ismcast); 349 350 /* Force this rate if needed. */ 351 if (sc->sc_ratectl != RTWN_RATECTL_FW) 352 txd->txdw4 |= htole32(R92C_TXDW4_DRVRATE); 353 354 if (!hasqos) { 355 /* Use HW sequence numbering for non-QoS frames. */ 356 rtwn_r92c_tx_setup_hwseq(sc, txd); 357 txd->txdw4 |= htole32(SM(R92C_TXDW4_SEQ_SEL, uvp->id)); 358 } else { 359 uint16_t seqno; 360 361 if (m->m_flags & M_AMPDU_MPDU) { 362 seqno = ni->ni_txseqs[tid] % IEEE80211_SEQ_RANGE; 363 ni->ni_txseqs[tid]++; 364 } else 365 seqno = M_SEQNO_GET(m) % IEEE80211_SEQ_RANGE; 366 367 /* Set sequence number. */ 368 txd->txdseq = htole16(seqno); 369 } 370 } 371 372 void 373 r92c_fill_tx_desc_raw(struct rtwn_softc *sc, struct ieee80211_node *ni, 374 struct mbuf *m, void *buf, const struct ieee80211_bpf_params *params) 375 { 376 struct ieee80211vap *vap = ni->ni_vap; 377 struct rtwn_vap *uvp = RTWN_VAP(vap); 378 struct ieee80211_frame *wh; 379 struct r92c_tx_desc *txd; 380 uint8_t ridx; 381 int ismcast; 382 383 /* XXX TODO: 11n checks, matching r92c_fill_tx_desc() */ 384 385 wh = mtod(m, struct ieee80211_frame *); 386 ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1); 387 ridx = rate2ridx(params->ibp_rate0); 388 389 /* Fill Tx descriptor. */ 390 txd = (struct r92c_tx_desc *)buf; 391 txd->flags0 |= R92C_FLAGS0_LSG | R92C_FLAGS0_FSG; 392 if (ismcast) 393 txd->flags0 |= R92C_FLAGS0_BMCAST; 394 395 if ((params->ibp_flags & IEEE80211_BPF_NOACK) == 0) { 396 txd->txdw5 |= htole32(R92C_TXDW5_RTY_LMT_ENA); 397 txd->txdw5 |= htole32(SM(R92C_TXDW5_RTY_LMT, 398 params->ibp_try0)); 399 } 400 if (params->ibp_flags & IEEE80211_BPF_RTS) 401 r92c_tx_protection(sc, txd, IEEE80211_PROT_RTSCTS, ridx); 402 if (params->ibp_flags & IEEE80211_BPF_CTS) 403 r92c_tx_protection(sc, txd, IEEE80211_PROT_CTSONLY, ridx); 404 405 rtwn_r92c_tx_setup_macid(sc, txd, RTWN_MACID_BC); 406 txd->txdw1 |= htole32(SM(R92C_TXDW1_QSEL, R92C_TXDW1_QSEL_MGNT)); 407 408 /* Set TX rate index. */ 409 txd->txdw5 |= htole32(SM(R92C_TXDW5_DATARATE, ridx)); 410 txd->txdw5 |= htole32(SM(R92C_TXDW5_DATARATE_FB_LMT, 0x1f)); 411 txd->txdw4 |= htole32(SM(R92C_TXDW4_PORT_ID, uvp->id)); 412 txd->txdw4 |= htole32(R92C_TXDW4_DRVRATE); 413 r92c_tx_raid(sc, txd, ni, ismcast); 414 415 if (!IEEE80211_QOS_HAS_SEQ(wh)) { 416 /* Use HW sequence numbering for non-QoS frames. */ 417 rtwn_r92c_tx_setup_hwseq(sc, txd); 418 txd->txdw4 |= htole32(SM(R92C_TXDW4_SEQ_SEL, uvp->id)); 419 } else { 420 /* Set sequence number. */ 421 txd->txdseq |= htole16(M_SEQNO_GET(m) % IEEE80211_SEQ_RANGE); 422 } 423 } 424 425 void 426 r92c_fill_tx_desc_null(struct rtwn_softc *sc, void *buf, int is11b, 427 int qos, int id) 428 { 429 struct r92c_tx_desc *txd = (struct r92c_tx_desc *)buf; 430 431 txd->flags0 = R92C_FLAGS0_FSG | R92C_FLAGS0_LSG | R92C_FLAGS0_OWN; 432 txd->txdw1 = htole32( 433 SM(R92C_TXDW1_QSEL, R92C_TXDW1_QSEL_MGNT)); 434 435 txd->txdw4 = htole32(R92C_TXDW4_DRVRATE); 436 txd->txdw4 |= htole32(SM(R92C_TXDW4_PORT_ID, id)); 437 if (is11b) { 438 txd->txdw5 = htole32(SM(R92C_TXDW5_DATARATE, 439 RTWN_RIDX_CCK1)); 440 } else { 441 txd->txdw5 = htole32(SM(R92C_TXDW5_DATARATE, 442 RTWN_RIDX_OFDM6)); 443 } 444 445 if (!qos) { 446 rtwn_r92c_tx_setup_hwseq(sc, txd); 447 txd->txdw4 |= htole32(SM(R92C_TXDW4_SEQ_SEL, id)); 448 } 449 } 450 451 uint8_t 452 r92c_tx_radiotap_flags(const void *buf) 453 { 454 const struct r92c_tx_desc *txd = buf; 455 uint8_t flags; 456 457 flags = 0; 458 if (txd->txdw4 & htole32(R92C_TXDW4_DATA_SHPRE)) 459 flags |= IEEE80211_RADIOTAP_F_SHORTPRE; 460 if (txd->txdw5 & htole32(R92C_TXDW5_SGI)) 461 flags |= IEEE80211_RADIOTAP_F_SHORTGI; 462 return (flags); 463 } 464