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