1 // SPDX-License-Identifier: BSD-3-Clause-Clear 2 /* 3 * Copyright (C) 2016 Felix Fietkau <nbd@nbd.name> 4 * Copyright (C) 2018 Stanislaw Gruszka <stf_xl@wp.pl> 5 */ 6 7 #include "mt76x02.h" 8 #include "mt76x02_trace.h" 9 #include "trace.h" 10 11 void mt76x02_mac_reset_counters(struct mt76x02_dev *dev) 12 { 13 int i; 14 15 mt76_rr(dev, MT_RX_STAT_0); 16 mt76_rr(dev, MT_RX_STAT_1); 17 mt76_rr(dev, MT_RX_STAT_2); 18 mt76_rr(dev, MT_TX_STA_0); 19 mt76_rr(dev, MT_TX_STA_1); 20 mt76_rr(dev, MT_TX_STA_2); 21 22 for (i = 0; i < 16; i++) 23 mt76_rr(dev, MT_TX_AGG_CNT(i)); 24 25 for (i = 0; i < 16; i++) 26 mt76_rr(dev, MT_TX_STAT_FIFO); 27 28 memset(dev->mphy.aggr_stats, 0, sizeof(dev->mphy.aggr_stats)); 29 } 30 EXPORT_SYMBOL_GPL(mt76x02_mac_reset_counters); 31 32 static enum mt76x02_cipher_type 33 mt76x02_mac_get_key_info(struct ieee80211_key_conf *key, u8 *key_data) 34 { 35 memset(key_data, 0, 32); 36 if (!key) 37 return MT76X02_CIPHER_NONE; 38 39 if (key->keylen > 32) 40 return MT76X02_CIPHER_NONE; 41 42 memcpy(key_data, key->key, key->keylen); 43 44 switch (key->cipher) { 45 case WLAN_CIPHER_SUITE_WEP40: 46 return MT76X02_CIPHER_WEP40; 47 case WLAN_CIPHER_SUITE_WEP104: 48 return MT76X02_CIPHER_WEP104; 49 case WLAN_CIPHER_SUITE_TKIP: 50 return MT76X02_CIPHER_TKIP; 51 case WLAN_CIPHER_SUITE_CCMP: 52 return MT76X02_CIPHER_AES_CCMP; 53 default: 54 return MT76X02_CIPHER_NONE; 55 } 56 } 57 58 int mt76x02_mac_shared_key_setup(struct mt76x02_dev *dev, u8 vif_idx, 59 u8 key_idx, struct ieee80211_key_conf *key) 60 { 61 enum mt76x02_cipher_type cipher; 62 u8 key_data[32]; 63 u32 val; 64 65 cipher = mt76x02_mac_get_key_info(key, key_data); 66 if (cipher == MT76X02_CIPHER_NONE && key) 67 return -EOPNOTSUPP; 68 69 val = mt76_rr(dev, MT_SKEY_MODE(vif_idx)); 70 val &= ~(MT_SKEY_MODE_MASK << MT_SKEY_MODE_SHIFT(vif_idx, key_idx)); 71 val |= cipher << MT_SKEY_MODE_SHIFT(vif_idx, key_idx); 72 mt76_wr(dev, MT_SKEY_MODE(vif_idx), val); 73 74 mt76_wr_copy(dev, MT_SKEY(vif_idx, key_idx), key_data, 75 sizeof(key_data)); 76 77 return 0; 78 } 79 EXPORT_SYMBOL_GPL(mt76x02_mac_shared_key_setup); 80 81 void mt76x02_mac_wcid_sync_pn(struct mt76x02_dev *dev, u8 idx, 82 struct ieee80211_key_conf *key) 83 { 84 enum mt76x02_cipher_type cipher; 85 u8 key_data[32]; 86 u32 iv, eiv; 87 u64 pn; 88 89 cipher = mt76x02_mac_get_key_info(key, key_data); 90 iv = mt76_rr(dev, MT_WCID_IV(idx)); 91 eiv = mt76_rr(dev, MT_WCID_IV(idx) + 4); 92 93 pn = (u64)eiv << 16; 94 if (cipher == MT76X02_CIPHER_TKIP) { 95 pn |= (iv >> 16) & 0xff; 96 pn |= (iv & 0xff) << 8; 97 } else if (cipher >= MT76X02_CIPHER_AES_CCMP) { 98 pn |= iv & 0xffff; 99 } else { 100 return; 101 } 102 103 atomic64_set(&key->tx_pn, pn); 104 } 105 106 int mt76x02_mac_wcid_set_key(struct mt76x02_dev *dev, u8 idx, 107 struct ieee80211_key_conf *key) 108 { 109 enum mt76x02_cipher_type cipher; 110 u8 key_data[32]; 111 u8 iv_data[8]; 112 u64 pn; 113 114 cipher = mt76x02_mac_get_key_info(key, key_data); 115 if (cipher == MT76X02_CIPHER_NONE && key) 116 return -EOPNOTSUPP; 117 118 mt76_wr_copy(dev, MT_WCID_KEY(idx), key_data, sizeof(key_data)); 119 mt76_rmw_field(dev, MT_WCID_ATTR(idx), MT_WCID_ATTR_PKEY_MODE, cipher); 120 121 memset(iv_data, 0, sizeof(iv_data)); 122 if (key) { 123 mt76_rmw_field(dev, MT_WCID_ATTR(idx), MT_WCID_ATTR_PAIRWISE, 124 !!(key->flags & IEEE80211_KEY_FLAG_PAIRWISE)); 125 126 pn = atomic64_read(&key->tx_pn); 127 128 iv_data[3] = key->keyidx << 6; 129 if (cipher >= MT76X02_CIPHER_TKIP) { 130 iv_data[3] |= 0x20; 131 put_unaligned_le32(pn >> 16, &iv_data[4]); 132 } 133 134 if (cipher == MT76X02_CIPHER_TKIP) { 135 iv_data[0] = (pn >> 8) & 0xff; 136 iv_data[1] = (iv_data[0] | 0x20) & 0x7f; 137 iv_data[2] = pn & 0xff; 138 } else if (cipher >= MT76X02_CIPHER_AES_CCMP) { 139 put_unaligned_le16((pn & 0xffff), &iv_data[0]); 140 } 141 } 142 143 mt76_wr_copy(dev, MT_WCID_IV(idx), iv_data, sizeof(iv_data)); 144 145 return 0; 146 } 147 148 void mt76x02_mac_wcid_setup(struct mt76x02_dev *dev, u8 idx, 149 u8 vif_idx, u8 *mac) 150 { 151 struct mt76_wcid_addr addr = {}; 152 u32 attr; 153 154 attr = FIELD_PREP(MT_WCID_ATTR_BSS_IDX, vif_idx & 7) | 155 FIELD_PREP(MT_WCID_ATTR_BSS_IDX_EXT, !!(vif_idx & 8)); 156 157 mt76_wr(dev, MT_WCID_ATTR(idx), attr); 158 159 if (idx >= 128) 160 return; 161 162 if (mac) 163 memcpy(addr.macaddr, mac, ETH_ALEN); 164 165 mt76_wr_copy(dev, MT_WCID_ADDR(idx), &addr, sizeof(addr)); 166 } 167 EXPORT_SYMBOL_GPL(mt76x02_mac_wcid_setup); 168 169 void mt76x02_mac_wcid_set_drop(struct mt76x02_dev *dev, u8 idx, bool drop) 170 { 171 u32 val = mt76_rr(dev, MT_WCID_DROP(idx)); 172 u32 bit = MT_WCID_DROP_MASK(idx); 173 174 /* prevent unnecessary writes */ 175 if ((val & bit) != (bit * drop)) 176 mt76_wr(dev, MT_WCID_DROP(idx), (val & ~bit) | (bit * drop)); 177 } 178 179 static u16 180 mt76x02_mac_tx_rate_val(struct mt76x02_dev *dev, 181 const struct ieee80211_tx_rate *rate, u8 *nss_val) 182 { 183 u8 phy, rate_idx, nss, bw = 0; 184 u16 rateval; 185 186 if (rate->flags & IEEE80211_TX_RC_VHT_MCS) { 187 rate_idx = rate->idx; 188 nss = 1 + (rate->idx >> 4); 189 phy = MT_PHY_TYPE_VHT; 190 if (rate->flags & IEEE80211_TX_RC_80_MHZ_WIDTH) 191 bw = 2; 192 else if (rate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH) 193 bw = 1; 194 } else if (rate->flags & IEEE80211_TX_RC_MCS) { 195 rate_idx = rate->idx; 196 nss = 1 + (rate->idx >> 3); 197 phy = MT_PHY_TYPE_HT; 198 if (rate->flags & IEEE80211_TX_RC_GREEN_FIELD) 199 phy = MT_PHY_TYPE_HT_GF; 200 if (rate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH) 201 bw = 1; 202 } else { 203 const struct ieee80211_rate *r; 204 int band = dev->mphy.chandef.chan->band; 205 u16 val; 206 207 r = &dev->mt76.hw->wiphy->bands[band]->bitrates[rate->idx]; 208 if (rate->flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE) 209 val = r->hw_value_short; 210 else 211 val = r->hw_value; 212 213 phy = val >> 8; 214 rate_idx = val & 0xff; 215 nss = 1; 216 } 217 218 rateval = FIELD_PREP(MT_RXWI_RATE_INDEX, rate_idx); 219 rateval |= FIELD_PREP(MT_RXWI_RATE_PHY, phy); 220 rateval |= FIELD_PREP(MT_RXWI_RATE_BW, bw); 221 if (rate->flags & IEEE80211_TX_RC_SHORT_GI) 222 rateval |= MT_RXWI_RATE_SGI; 223 224 *nss_val = nss; 225 return rateval; 226 } 227 228 void mt76x02_mac_wcid_set_rate(struct mt76x02_dev *dev, struct mt76_wcid *wcid, 229 const struct ieee80211_tx_rate *rate) 230 { 231 s8 max_txpwr_adj = mt76x02_tx_get_max_txpwr_adj(dev, rate); 232 u16 rateval; 233 u32 tx_info; 234 s8 nss; 235 236 rateval = mt76x02_mac_tx_rate_val(dev, rate, &nss); 237 tx_info = FIELD_PREP(MT_WCID_TX_INFO_RATE, rateval) | 238 FIELD_PREP(MT_WCID_TX_INFO_NSS, nss) | 239 FIELD_PREP(MT_WCID_TX_INFO_TXPWR_ADJ, max_txpwr_adj) | 240 MT_WCID_TX_INFO_SET; 241 wcid->tx_info = tx_info; 242 } 243 244 void mt76x02_mac_set_short_preamble(struct mt76x02_dev *dev, bool enable) 245 { 246 if (enable) 247 mt76_set(dev, MT_AUTO_RSP_CFG, MT_AUTO_RSP_PREAMB_SHORT); 248 else 249 mt76_clear(dev, MT_AUTO_RSP_CFG, MT_AUTO_RSP_PREAMB_SHORT); 250 } 251 252 bool mt76x02_mac_load_tx_status(struct mt76x02_dev *dev, 253 struct mt76x02_tx_status *stat) 254 { 255 u32 stat1, stat2; 256 257 stat2 = mt76_rr(dev, MT_TX_STAT_FIFO_EXT); 258 stat1 = mt76_rr(dev, MT_TX_STAT_FIFO); 259 260 stat->valid = !!(stat1 & MT_TX_STAT_FIFO_VALID); 261 if (!stat->valid) 262 return false; 263 264 stat->success = !!(stat1 & MT_TX_STAT_FIFO_SUCCESS); 265 stat->aggr = !!(stat1 & MT_TX_STAT_FIFO_AGGR); 266 stat->ack_req = !!(stat1 & MT_TX_STAT_FIFO_ACKREQ); 267 stat->wcid = FIELD_GET(MT_TX_STAT_FIFO_WCID, stat1); 268 stat->rate = FIELD_GET(MT_TX_STAT_FIFO_RATE, stat1); 269 270 stat->retry = FIELD_GET(MT_TX_STAT_FIFO_EXT_RETRY, stat2); 271 stat->pktid = FIELD_GET(MT_TX_STAT_FIFO_EXT_PKTID, stat2); 272 273 trace_mac_txstat_fetch(dev, stat); 274 275 return true; 276 } 277 278 static int 279 mt76x02_mac_process_tx_rate(struct ieee80211_tx_rate *txrate, u16 rate, 280 enum nl80211_band band) 281 { 282 u8 idx = FIELD_GET(MT_RXWI_RATE_INDEX, rate); 283 284 txrate->idx = 0; 285 txrate->flags = 0; 286 txrate->count = 1; 287 288 switch (FIELD_GET(MT_RXWI_RATE_PHY, rate)) { 289 case MT_PHY_TYPE_OFDM: 290 if (band == NL80211_BAND_2GHZ) 291 idx += 4; 292 293 txrate->idx = idx; 294 return 0; 295 case MT_PHY_TYPE_CCK: 296 if (idx >= 8) 297 idx -= 8; 298 299 txrate->idx = idx; 300 return 0; 301 case MT_PHY_TYPE_HT_GF: 302 txrate->flags |= IEEE80211_TX_RC_GREEN_FIELD; 303 fallthrough; 304 case MT_PHY_TYPE_HT: 305 if (idx > 31) 306 return -EINVAL; 307 txrate->flags |= IEEE80211_TX_RC_MCS; 308 txrate->idx = idx; 309 break; 310 case MT_PHY_TYPE_VHT: 311 if ((idx & 0xf) > 9) 312 return -EINVAL; 313 txrate->flags |= IEEE80211_TX_RC_VHT_MCS; 314 txrate->idx = idx; 315 break; 316 default: 317 return -EINVAL; 318 } 319 320 switch (FIELD_GET(MT_RXWI_RATE_BW, rate)) { 321 case MT_PHY_BW_20: 322 break; 323 case MT_PHY_BW_40: 324 txrate->flags |= IEEE80211_TX_RC_40_MHZ_WIDTH; 325 break; 326 case MT_PHY_BW_80: 327 txrate->flags |= IEEE80211_TX_RC_80_MHZ_WIDTH; 328 break; 329 default: 330 return -EINVAL; 331 } 332 333 if (rate & MT_RXWI_RATE_SGI) 334 txrate->flags |= IEEE80211_TX_RC_SHORT_GI; 335 336 return 0; 337 } 338 339 void mt76x02_mac_write_txwi(struct mt76x02_dev *dev, struct mt76x02_txwi *txwi, 340 struct sk_buff *skb, struct mt76_wcid *wcid, 341 struct ieee80211_sta *sta, int len) 342 { 343 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data; 344 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); 345 struct ieee80211_tx_rate *rate = &info->control.rates[0]; 346 struct ieee80211_key_conf *key = info->control.hw_key; 347 u32 wcid_tx_info; 348 u16 rate_ht_mask = FIELD_PREP(MT_RXWI_RATE_PHY, BIT(1) | BIT(2)); 349 u16 txwi_flags = 0, rateval; 350 u8 nss; 351 s8 txpwr_adj, max_txpwr_adj; 352 u8 ccmp_pn[8], nstreams = dev->mphy.chainmask & 0xf; 353 354 memset(txwi, 0, sizeof(*txwi)); 355 356 mt76_tx_check_agg_ssn(sta, skb); 357 358 if (!info->control.hw_key && wcid && wcid->hw_key_idx != 0xff && 359 ieee80211_has_protected(hdr->frame_control)) { 360 wcid = NULL; 361 ieee80211_get_tx_rates(info->control.vif, sta, skb, 362 info->control.rates, 1); 363 } 364 365 if (wcid) 366 txwi->wcid = wcid->idx; 367 else 368 txwi->wcid = 0xff; 369 370 if (wcid && wcid->sw_iv && key) { 371 u64 pn = atomic64_inc_return(&key->tx_pn); 372 373 ccmp_pn[0] = pn; 374 ccmp_pn[1] = pn >> 8; 375 ccmp_pn[2] = 0; 376 ccmp_pn[3] = 0x20 | (key->keyidx << 6); 377 ccmp_pn[4] = pn >> 16; 378 ccmp_pn[5] = pn >> 24; 379 ccmp_pn[6] = pn >> 32; 380 ccmp_pn[7] = pn >> 40; 381 txwi->iv = *((__le32 *)&ccmp_pn[0]); 382 txwi->eiv = *((__le32 *)&ccmp_pn[4]); 383 } 384 385 if (wcid && (rate->idx < 0 || !rate->count)) { 386 wcid_tx_info = wcid->tx_info; 387 rateval = FIELD_GET(MT_WCID_TX_INFO_RATE, wcid_tx_info); 388 max_txpwr_adj = FIELD_GET(MT_WCID_TX_INFO_TXPWR_ADJ, 389 wcid_tx_info); 390 nss = FIELD_GET(MT_WCID_TX_INFO_NSS, wcid_tx_info); 391 } else { 392 rateval = mt76x02_mac_tx_rate_val(dev, rate, &nss); 393 max_txpwr_adj = mt76x02_tx_get_max_txpwr_adj(dev, rate); 394 } 395 txwi->rate = cpu_to_le16(rateval); 396 397 txpwr_adj = mt76x02_tx_get_txpwr_adj(dev, dev->txpower_conf, 398 max_txpwr_adj); 399 txwi->ctl2 = FIELD_PREP(MT_TX_PWR_ADJ, txpwr_adj); 400 401 if (nstreams > 1 && mt76_rev(&dev->mt76) >= MT76XX_REV_E4) 402 txwi->txstream = 0x13; 403 else if (nstreams > 1 && mt76_rev(&dev->mt76) >= MT76XX_REV_E3 && 404 !(txwi->rate & cpu_to_le16(rate_ht_mask))) 405 txwi->txstream = 0x93; 406 407 if (is_mt76x2(dev) && (info->flags & IEEE80211_TX_CTL_LDPC)) 408 txwi->rate |= cpu_to_le16(MT_RXWI_RATE_LDPC); 409 if ((info->flags & IEEE80211_TX_CTL_STBC) && nss == 1) 410 txwi->rate |= cpu_to_le16(MT_RXWI_RATE_STBC); 411 if (nss > 1 && sta && sta->deflink.smps_mode == IEEE80211_SMPS_DYNAMIC) 412 txwi_flags |= MT_TXWI_FLAGS_MMPS; 413 if (!(info->flags & IEEE80211_TX_CTL_NO_ACK)) 414 txwi->ack_ctl |= MT_TXWI_ACK_CTL_REQ; 415 if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) 416 txwi->ack_ctl |= MT_TXWI_ACK_CTL_NSEQ; 417 if ((info->flags & IEEE80211_TX_CTL_AMPDU) && sta) { 418 u8 ba_size = IEEE80211_MIN_AMPDU_BUF; 419 u8 ampdu_density = sta->deflink.ht_cap.ampdu_density; 420 421 ba_size <<= sta->deflink.ht_cap.ampdu_factor; 422 ba_size = min_t(int, 63, ba_size - 1); 423 if (info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE) 424 ba_size = 0; 425 txwi->ack_ctl |= FIELD_PREP(MT_TXWI_ACK_CTL_BA_WINDOW, ba_size); 426 427 if (ampdu_density < IEEE80211_HT_MPDU_DENSITY_4) 428 ampdu_density = IEEE80211_HT_MPDU_DENSITY_4; 429 430 txwi_flags |= MT_TXWI_FLAGS_AMPDU | 431 FIELD_PREP(MT_TXWI_FLAGS_MPDU_DENSITY, ampdu_density); 432 } 433 434 if (ieee80211_is_probe_resp(hdr->frame_control) || 435 ieee80211_is_beacon(hdr->frame_control)) 436 txwi_flags |= MT_TXWI_FLAGS_TS; 437 438 txwi->flags |= cpu_to_le16(txwi_flags); 439 txwi->len_ctl = cpu_to_le16(len); 440 } 441 EXPORT_SYMBOL_GPL(mt76x02_mac_write_txwi); 442 443 static void 444 mt76x02_tx_rate_fallback(struct ieee80211_tx_rate *rates, int idx, int phy) 445 { 446 u8 mcs, nss; 447 448 if (!idx) 449 return; 450 451 rates += idx - 1; 452 rates[1] = rates[0]; 453 switch (phy) { 454 case MT_PHY_TYPE_VHT: 455 mcs = ieee80211_rate_get_vht_mcs(rates); 456 nss = ieee80211_rate_get_vht_nss(rates); 457 458 if (mcs == 0) 459 nss = max_t(int, nss - 1, 1); 460 else 461 mcs--; 462 463 ieee80211_rate_set_vht(rates + 1, mcs, nss); 464 break; 465 case MT_PHY_TYPE_HT_GF: 466 case MT_PHY_TYPE_HT: 467 /* MCS 8 falls back to MCS 0 */ 468 if (rates[0].idx == 8) { 469 rates[1].idx = 0; 470 break; 471 } 472 fallthrough; 473 default: 474 rates[1].idx = max_t(int, rates[0].idx - 1, 0); 475 break; 476 } 477 } 478 479 static void 480 mt76x02_mac_fill_tx_status(struct mt76x02_dev *dev, struct mt76x02_sta *msta, 481 struct ieee80211_tx_info *info, 482 struct mt76x02_tx_status *st, int n_frames) 483 { 484 struct ieee80211_tx_rate *rate = info->status.rates; 485 struct ieee80211_tx_rate last_rate; 486 u16 first_rate; 487 int retry = st->retry; 488 int phy; 489 int i; 490 491 if (!n_frames) 492 return; 493 494 phy = FIELD_GET(MT_RXWI_RATE_PHY, st->rate); 495 496 if (st->pktid & MT_PACKET_ID_HAS_RATE) { 497 first_rate = st->rate & ~MT_PKTID_RATE; 498 first_rate |= st->pktid & MT_PKTID_RATE; 499 500 if (mt76x02_mac_process_tx_rate(&rate[0], first_rate, 501 dev->mphy.chandef.chan->band)) 502 return; 503 } else if (rate[0].idx < 0) { 504 if (!msta) 505 return; 506 507 if (mt76x02_mac_process_tx_rate(&rate[0], msta->wcid.tx_info, 508 dev->mphy.chandef.chan->band)) 509 return; 510 } 511 512 if (mt76x02_mac_process_tx_rate(&last_rate, st->rate, 513 dev->mphy.chandef.chan->band)) 514 return; 515 516 for (i = 0; i < ARRAY_SIZE(info->status.rates); i++) { 517 retry--; 518 if (i + 1 == ARRAY_SIZE(info->status.rates)) { 519 info->status.rates[i] = last_rate; 520 info->status.rates[i].count = max_t(int, retry, 1); 521 break; 522 } 523 524 mt76x02_tx_rate_fallback(info->status.rates, i, phy); 525 if (info->status.rates[i].idx == last_rate.idx) 526 break; 527 } 528 529 if (i + 1 < ARRAY_SIZE(info->status.rates)) { 530 info->status.rates[i + 1].idx = -1; 531 info->status.rates[i + 1].count = 0; 532 } 533 534 info->status.ampdu_len = n_frames; 535 info->status.ampdu_ack_len = st->success ? n_frames : 0; 536 537 if (st->aggr) 538 info->flags |= IEEE80211_TX_CTL_AMPDU | 539 IEEE80211_TX_STAT_AMPDU; 540 541 if (!st->ack_req) 542 info->flags |= IEEE80211_TX_CTL_NO_ACK; 543 else if (st->success) 544 info->flags |= IEEE80211_TX_STAT_ACK; 545 } 546 547 void mt76x02_send_tx_status(struct mt76x02_dev *dev, 548 struct mt76x02_tx_status *stat, u8 *update) 549 { 550 struct ieee80211_tx_info info = {}; 551 struct ieee80211_tx_status status = { 552 .info = &info 553 }; 554 static const u8 ac_to_tid[4] = { 555 [IEEE80211_AC_BE] = 0, 556 [IEEE80211_AC_BK] = 1, 557 [IEEE80211_AC_VI] = 4, 558 [IEEE80211_AC_VO] = 6 559 }; 560 struct mt76_wcid *wcid = NULL; 561 struct mt76x02_sta *msta = NULL; 562 struct mt76_dev *mdev = &dev->mt76; 563 struct sk_buff_head list; 564 u32 duration = 0; 565 u8 cur_pktid; 566 u32 ac = 0; 567 int len = 0; 568 569 if (stat->pktid == MT_PACKET_ID_NO_ACK) 570 return; 571 572 rcu_read_lock(); 573 574 wcid = mt76_wcid_ptr(dev, stat->wcid); 575 if (wcid && wcid->sta) { 576 void *priv; 577 578 priv = msta = container_of(wcid, struct mt76x02_sta, wcid); 579 status.sta = container_of(priv, struct ieee80211_sta, 580 drv_priv); 581 } 582 583 mt76_tx_status_lock(mdev, &list); 584 585 if (wcid) { 586 if (mt76_is_skb_pktid(stat->pktid)) 587 status.skb = mt76_tx_status_skb_get(mdev, wcid, 588 stat->pktid, &list); 589 if (status.skb) 590 status.info = IEEE80211_SKB_CB(status.skb); 591 } 592 593 if (!status.skb && !(stat->pktid & MT_PACKET_ID_HAS_RATE)) { 594 mt76_tx_status_unlock(mdev, &list); 595 goto out; 596 } 597 598 599 if (msta && stat->aggr && !status.skb) { 600 u32 stat_val, stat_cache; 601 602 stat_val = stat->rate; 603 stat_val |= ((u32)stat->retry) << 16; 604 stat_cache = msta->status.rate; 605 stat_cache |= ((u32)msta->status.retry) << 16; 606 607 if (*update == 0 && stat_val == stat_cache && 608 stat->wcid == msta->status.wcid && msta->n_frames < 32) { 609 msta->n_frames++; 610 mt76_tx_status_unlock(mdev, &list); 611 goto out; 612 } 613 614 cur_pktid = msta->status.pktid; 615 mt76x02_mac_fill_tx_status(dev, msta, status.info, 616 &msta->status, msta->n_frames); 617 618 msta->status = *stat; 619 msta->n_frames = 1; 620 *update = 0; 621 } else { 622 cur_pktid = stat->pktid; 623 mt76x02_mac_fill_tx_status(dev, msta, status.info, stat, 1); 624 *update = 1; 625 } 626 627 if (status.skb) { 628 info = *status.info; 629 len = status.skb->len; 630 ac = skb_get_queue_mapping(status.skb); 631 mt76_tx_status_skb_done(mdev, status.skb, &list); 632 } else if (msta) { 633 len = status.info->status.ampdu_len * ewma_pktlen_read(&msta->pktlen); 634 ac = FIELD_GET(MT_PKTID_AC, cur_pktid); 635 } 636 637 mt76_tx_status_unlock(mdev, &list); 638 639 if (!status.skb) { 640 spin_lock_bh(&dev->mt76.rx_lock); 641 ieee80211_tx_status_ext(mt76_hw(dev), &status); 642 spin_unlock_bh(&dev->mt76.rx_lock); 643 } 644 645 if (!len) 646 goto out; 647 648 duration = ieee80211_calc_tx_airtime(mt76_hw(dev), &info, len); 649 650 spin_lock_bh(&dev->mt76.cc_lock); 651 dev->tx_airtime += duration; 652 spin_unlock_bh(&dev->mt76.cc_lock); 653 654 if (msta) 655 ieee80211_sta_register_airtime(status.sta, ac_to_tid[ac], duration, 0); 656 657 out: 658 rcu_read_unlock(); 659 } 660 661 static int 662 mt76x02_mac_process_rate(struct mt76x02_dev *dev, 663 struct mt76_rx_status *status, 664 u16 rate) 665 { 666 u8 idx = FIELD_GET(MT_RXWI_RATE_INDEX, rate); 667 668 switch (FIELD_GET(MT_RXWI_RATE_PHY, rate)) { 669 case MT_PHY_TYPE_OFDM: 670 if (idx >= 8) 671 idx = 0; 672 673 if (status->band == NL80211_BAND_2GHZ) 674 idx += 4; 675 676 status->rate_idx = idx; 677 return 0; 678 case MT_PHY_TYPE_CCK: 679 if (idx >= 8) { 680 idx -= 8; 681 status->enc_flags |= RX_ENC_FLAG_SHORTPRE; 682 } 683 684 if (idx >= 4) 685 idx = 0; 686 687 status->rate_idx = idx; 688 return 0; 689 case MT_PHY_TYPE_HT_GF: 690 status->enc_flags |= RX_ENC_FLAG_HT_GF; 691 fallthrough; 692 case MT_PHY_TYPE_HT: 693 status->encoding = RX_ENC_HT; 694 status->rate_idx = idx; 695 break; 696 case MT_PHY_TYPE_VHT: { 697 u8 n_rxstream = dev->mphy.chainmask & 0xf; 698 699 status->encoding = RX_ENC_VHT; 700 status->rate_idx = FIELD_GET(MT_RATE_INDEX_VHT_IDX, idx); 701 status->nss = min_t(u8, n_rxstream, 702 FIELD_GET(MT_RATE_INDEX_VHT_NSS, idx) + 1); 703 break; 704 } 705 default: 706 return -EINVAL; 707 } 708 709 if (rate & MT_RXWI_RATE_LDPC) 710 status->enc_flags |= RX_ENC_FLAG_LDPC; 711 712 if (rate & MT_RXWI_RATE_SGI) 713 status->enc_flags |= RX_ENC_FLAG_SHORT_GI; 714 715 if (rate & MT_RXWI_RATE_STBC) 716 status->enc_flags |= 1 << RX_ENC_FLAG_STBC_SHIFT; 717 718 switch (FIELD_GET(MT_RXWI_RATE_BW, rate)) { 719 case MT_PHY_BW_20: 720 break; 721 case MT_PHY_BW_40: 722 status->bw = RATE_INFO_BW_40; 723 break; 724 case MT_PHY_BW_80: 725 status->bw = RATE_INFO_BW_80; 726 break; 727 default: 728 break; 729 } 730 731 return 0; 732 } 733 734 void mt76x02_mac_setaddr(struct mt76x02_dev *dev, const u8 *addr) 735 { 736 static const u8 null_addr[ETH_ALEN] = {}; 737 int i; 738 739 ether_addr_copy(dev->mphy.macaddr, addr); 740 741 if (!is_valid_ether_addr(dev->mphy.macaddr)) { 742 eth_random_addr(dev->mphy.macaddr); 743 dev_info(dev->mt76.dev, 744 "Invalid MAC address, using random address %pM\n", 745 dev->mphy.macaddr); 746 } 747 748 mt76_wr(dev, MT_MAC_ADDR_DW0, get_unaligned_le32(dev->mphy.macaddr)); 749 mt76_wr(dev, MT_MAC_ADDR_DW1, 750 get_unaligned_le16(dev->mphy.macaddr + 4) | 751 FIELD_PREP(MT_MAC_ADDR_DW1_U2ME_MASK, 0xff)); 752 753 mt76_wr(dev, MT_MAC_BSSID_DW0, 754 get_unaligned_le32(dev->mphy.macaddr)); 755 mt76_wr(dev, MT_MAC_BSSID_DW1, 756 get_unaligned_le16(dev->mphy.macaddr + 4) | 757 FIELD_PREP(MT_MAC_BSSID_DW1_MBSS_MODE, 3) | /* 8 APs + 8 STAs */ 758 MT_MAC_BSSID_DW1_MBSS_LOCAL_BIT); 759 /* enable 7 additional beacon slots and control them with bypass mask */ 760 mt76_rmw_field(dev, MT_MAC_BSSID_DW1, MT_MAC_BSSID_DW1_MBEACON_N, 7); 761 762 for (i = 0; i < 16; i++) 763 mt76x02_mac_set_bssid(dev, i, null_addr); 764 } 765 EXPORT_SYMBOL_GPL(mt76x02_mac_setaddr); 766 767 static int 768 mt76x02_mac_get_rssi(struct mt76x02_dev *dev, s8 rssi, int chain) 769 { 770 struct mt76x02_rx_freq_cal *cal = &dev->cal.rx; 771 772 rssi += cal->rssi_offset[chain]; 773 rssi -= cal->lna_gain; 774 775 return rssi; 776 } 777 778 int mt76x02_mac_process_rx(struct mt76x02_dev *dev, struct sk_buff *skb, 779 void *rxi) 780 { 781 struct mt76_rx_status *status = (struct mt76_rx_status *)skb->cb; 782 struct ieee80211_hdr *hdr; 783 struct mt76x02_rxwi *rxwi = rxi; 784 struct mt76x02_sta *sta; 785 u32 rxinfo = le32_to_cpu(rxwi->rxinfo); 786 u32 ctl = le32_to_cpu(rxwi->ctl); 787 u16 rate = le16_to_cpu(rxwi->rate); 788 u16 tid_sn = le16_to_cpu(rxwi->tid_sn); 789 bool unicast = rxwi->rxinfo & cpu_to_le32(MT_RXINFO_UNICAST); 790 int pad_len = 0, nstreams = dev->mphy.chainmask & 0xf; 791 s8 signal; 792 u8 pn_len; 793 u8 wcid; 794 int len; 795 796 if (!test_bit(MT76_STATE_RUNNING, &dev->mphy.state)) 797 return -EINVAL; 798 799 if (rxinfo & MT_RXINFO_L2PAD) 800 pad_len += 2; 801 802 if (rxinfo & MT_RXINFO_CRCERR) 803 status->flag |= RX_FLAG_FAILED_FCS_CRC; 804 805 if (rxinfo & MT_RXINFO_DECRYPT) { 806 status->flag |= RX_FLAG_DECRYPTED; 807 status->flag |= RX_FLAG_MMIC_STRIPPED; 808 status->flag |= RX_FLAG_MIC_STRIPPED; 809 status->flag |= RX_FLAG_IV_STRIPPED; 810 } 811 812 wcid = FIELD_GET(MT_RXWI_CTL_WCID, ctl); 813 sta = mt76x02_rx_get_sta(&dev->mt76, wcid); 814 status->wcid = mt76x02_rx_get_sta_wcid(sta, unicast); 815 816 len = FIELD_GET(MT_RXWI_CTL_MPDU_LEN, ctl); 817 pn_len = FIELD_GET(MT_RXINFO_PN_LEN, rxinfo); 818 if (pn_len) { 819 unsigned int offset = ieee80211_get_hdrlen_from_skb(skb) + pad_len; 820 u8 *data; 821 822 if (offset + 8 > skb->len) 823 return -EINVAL; 824 825 data = skb->data + offset; 826 status->iv[0] = data[7]; 827 status->iv[1] = data[6]; 828 status->iv[2] = data[5]; 829 status->iv[3] = data[4]; 830 status->iv[4] = data[1]; 831 status->iv[5] = data[0]; 832 833 /* 834 * Driver CCMP validation can't deal with fragments. 835 * Let mac80211 take care of it. 836 */ 837 if (rxinfo & MT_RXINFO_FRAG) { 838 status->flag &= ~RX_FLAG_IV_STRIPPED; 839 } else { 840 pad_len += pn_len << 2; 841 len -= pn_len << 2; 842 } 843 } 844 845 if (pad_len + ieee80211_get_hdrlen_from_skb(skb) > skb->len) 846 return -EINVAL; 847 848 mt76x02_remove_hdr_pad(skb, pad_len); 849 850 if ((rxinfo & MT_RXINFO_BA) && !(rxinfo & MT_RXINFO_NULL)) 851 status->aggr = true; 852 853 if (rxinfo & MT_RXINFO_AMPDU) { 854 status->flag |= RX_FLAG_AMPDU_DETAILS; 855 status->ampdu_ref = dev->ampdu_ref; 856 857 /* 858 * When receiving an A-MPDU subframe and RSSI info is not valid, 859 * we can assume that more subframes belonging to the same A-MPDU 860 * are coming. The last one will have valid RSSI info 861 */ 862 if (rxinfo & MT_RXINFO_RSSI) { 863 if (!++dev->ampdu_ref) 864 dev->ampdu_ref++; 865 } 866 } 867 868 if (len > skb->len) 869 return -EINVAL; 870 871 if (pskb_trim(skb, len)) 872 return -EINVAL; 873 874 status->chains = BIT(0); 875 signal = mt76x02_mac_get_rssi(dev, rxwi->rssi[0], 0); 876 status->chain_signal[0] = signal; 877 if (nstreams > 1) { 878 status->chains |= BIT(1); 879 status->chain_signal[1] = mt76x02_mac_get_rssi(dev, 880 rxwi->rssi[1], 881 1); 882 } 883 status->freq = dev->mphy.chandef.chan->center_freq; 884 status->band = dev->mphy.chandef.chan->band; 885 886 hdr = (struct ieee80211_hdr *)skb->data; 887 status->qos_ctl = *ieee80211_get_qos_ctl(hdr); 888 status->seqno = FIELD_GET(MT_RXWI_SN, tid_sn); 889 890 return mt76x02_mac_process_rate(dev, status, rate); 891 } 892 893 void mt76x02_mac_poll_tx_status(struct mt76x02_dev *dev, bool irq) 894 { 895 struct mt76x02_tx_status stat = {}; 896 u8 update = 1; 897 bool ret; 898 899 if (!test_bit(MT76_STATE_RUNNING, &dev->mphy.state)) 900 return; 901 902 trace_mac_txstat_poll(dev); 903 904 while (!irq || !kfifo_is_full(&dev->txstatus_fifo)) { 905 if (!spin_trylock(&dev->txstatus_fifo_lock)) 906 break; 907 908 ret = mt76x02_mac_load_tx_status(dev, &stat); 909 spin_unlock(&dev->txstatus_fifo_lock); 910 911 if (!ret) 912 break; 913 914 if (!irq) { 915 mt76x02_send_tx_status(dev, &stat, &update); 916 continue; 917 } 918 919 kfifo_put(&dev->txstatus_fifo, stat); 920 } 921 } 922 923 void mt76x02_tx_complete_skb(struct mt76_dev *mdev, struct mt76_queue_entry *e) 924 { 925 struct mt76x02_dev *dev = container_of(mdev, struct mt76x02_dev, mt76); 926 struct mt76x02_txwi *txwi; 927 u8 *txwi_ptr; 928 929 if (!e->txwi) { 930 dev_kfree_skb_any(e->skb); 931 return; 932 } 933 934 mt76x02_mac_poll_tx_status(dev, false); 935 936 txwi_ptr = mt76_get_txwi_ptr(mdev, e->txwi); 937 txwi = (struct mt76x02_txwi *)txwi_ptr; 938 trace_mac_txdone(mdev, txwi->wcid, txwi->pktid); 939 940 mt76_tx_complete_skb(mdev, e->wcid, e->skb); 941 } 942 EXPORT_SYMBOL_GPL(mt76x02_tx_complete_skb); 943 944 void mt76x02_mac_set_rts_thresh(struct mt76x02_dev *dev, u32 val) 945 { 946 u32 data = 0; 947 948 if (val != ~0) 949 data = FIELD_PREP(MT_PROT_CFG_CTRL, 1) | 950 MT_PROT_CFG_RTS_THRESH; 951 952 mt76_rmw_field(dev, MT_TX_RTS_CFG, MT_TX_RTS_CFG_THRESH, val); 953 954 mt76_rmw(dev, MT_CCK_PROT_CFG, 955 MT_PROT_CFG_CTRL | MT_PROT_CFG_RTS_THRESH, data); 956 mt76_rmw(dev, MT_OFDM_PROT_CFG, 957 MT_PROT_CFG_CTRL | MT_PROT_CFG_RTS_THRESH, data); 958 } 959 960 void mt76x02_mac_set_tx_protection(struct mt76x02_dev *dev, bool legacy_prot, 961 int ht_mode) 962 { 963 int mode = ht_mode & IEEE80211_HT_OP_MODE_PROTECTION; 964 bool non_gf = !!(ht_mode & IEEE80211_HT_OP_MODE_NON_GF_STA_PRSNT); 965 u32 prot[6]; 966 u32 vht_prot[3]; 967 int i; 968 u16 rts_thr; 969 970 for (i = 0; i < ARRAY_SIZE(prot); i++) { 971 prot[i] = mt76_rr(dev, MT_CCK_PROT_CFG + i * 4); 972 prot[i] &= ~MT_PROT_CFG_CTRL; 973 if (i >= 2) 974 prot[i] &= ~MT_PROT_CFG_RATE; 975 } 976 977 for (i = 0; i < ARRAY_SIZE(vht_prot); i++) { 978 vht_prot[i] = mt76_rr(dev, MT_TX_PROT_CFG6 + i * 4); 979 vht_prot[i] &= ~(MT_PROT_CFG_CTRL | MT_PROT_CFG_RATE); 980 } 981 982 rts_thr = mt76_get_field(dev, MT_TX_RTS_CFG, MT_TX_RTS_CFG_THRESH); 983 984 if (rts_thr != 0xffff) 985 prot[0] |= MT_PROT_CTRL_RTS_CTS; 986 987 if (legacy_prot) { 988 prot[1] |= MT_PROT_CTRL_CTS2SELF; 989 990 prot[2] |= MT_PROT_RATE_CCK_11; 991 prot[3] |= MT_PROT_RATE_CCK_11; 992 prot[4] |= MT_PROT_RATE_CCK_11; 993 prot[5] |= MT_PROT_RATE_CCK_11; 994 995 vht_prot[0] |= MT_PROT_RATE_CCK_11; 996 vht_prot[1] |= MT_PROT_RATE_CCK_11; 997 vht_prot[2] |= MT_PROT_RATE_CCK_11; 998 } else { 999 if (rts_thr != 0xffff) 1000 prot[1] |= MT_PROT_CTRL_RTS_CTS; 1001 1002 prot[2] |= MT_PROT_RATE_OFDM_24; 1003 prot[3] |= MT_PROT_RATE_DUP_OFDM_24; 1004 prot[4] |= MT_PROT_RATE_OFDM_24; 1005 prot[5] |= MT_PROT_RATE_DUP_OFDM_24; 1006 1007 vht_prot[0] |= MT_PROT_RATE_OFDM_24; 1008 vht_prot[1] |= MT_PROT_RATE_DUP_OFDM_24; 1009 vht_prot[2] |= MT_PROT_RATE_SGI_OFDM_24; 1010 } 1011 1012 switch (mode) { 1013 case IEEE80211_HT_OP_MODE_PROTECTION_NONMEMBER: 1014 case IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED: 1015 prot[2] |= MT_PROT_CTRL_RTS_CTS; 1016 prot[3] |= MT_PROT_CTRL_RTS_CTS; 1017 prot[4] |= MT_PROT_CTRL_RTS_CTS; 1018 prot[5] |= MT_PROT_CTRL_RTS_CTS; 1019 vht_prot[0] |= MT_PROT_CTRL_RTS_CTS; 1020 vht_prot[1] |= MT_PROT_CTRL_RTS_CTS; 1021 vht_prot[2] |= MT_PROT_CTRL_RTS_CTS; 1022 break; 1023 case IEEE80211_HT_OP_MODE_PROTECTION_20MHZ: 1024 prot[3] |= MT_PROT_CTRL_RTS_CTS; 1025 prot[5] |= MT_PROT_CTRL_RTS_CTS; 1026 vht_prot[1] |= MT_PROT_CTRL_RTS_CTS; 1027 vht_prot[2] |= MT_PROT_CTRL_RTS_CTS; 1028 break; 1029 } 1030 1031 if (non_gf) { 1032 prot[4] |= MT_PROT_CTRL_RTS_CTS; 1033 prot[5] |= MT_PROT_CTRL_RTS_CTS; 1034 } 1035 1036 for (i = 0; i < ARRAY_SIZE(prot); i++) 1037 mt76_wr(dev, MT_CCK_PROT_CFG + i * 4, prot[i]); 1038 1039 for (i = 0; i < ARRAY_SIZE(vht_prot); i++) 1040 mt76_wr(dev, MT_TX_PROT_CFG6 + i * 4, vht_prot[i]); 1041 } 1042 1043 void mt76x02_update_channel(struct mt76_phy *mphy) 1044 { 1045 struct mt76x02_dev *dev = container_of(mphy->dev, struct mt76x02_dev, mt76); 1046 struct mt76_channel_state *state; 1047 1048 state = mphy->chan_state; 1049 state->cc_busy += mt76_rr(dev, MT_CH_BUSY); 1050 1051 spin_lock_bh(&dev->mt76.cc_lock); 1052 state->cc_tx += dev->tx_airtime; 1053 dev->tx_airtime = 0; 1054 spin_unlock_bh(&dev->mt76.cc_lock); 1055 } 1056 EXPORT_SYMBOL_GPL(mt76x02_update_channel); 1057 1058 static void mt76x02_check_mac_err(struct mt76x02_dev *dev) 1059 { 1060 if (dev->mt76.beacon_mask) { 1061 if (mt76_rr(dev, MT_TX_STA_0) & MT_TX_STA_0_BEACONS) { 1062 dev->beacon_hang_check = 0; 1063 return; 1064 } 1065 1066 if (dev->beacon_hang_check < 10) 1067 return; 1068 1069 } else { 1070 u32 val = mt76_rr(dev, 0x10f4); 1071 if (!(val & BIT(29)) || !(val & (BIT(7) | BIT(5)))) 1072 return; 1073 } 1074 1075 dev_err(dev->mt76.dev, "MAC error detected\n"); 1076 1077 mt76_wr(dev, MT_MAC_SYS_CTRL, 0); 1078 if (!mt76x02_wait_for_txrx_idle(&dev->mt76)) { 1079 dev_err(dev->mt76.dev, "MAC stop failed\n"); 1080 goto out; 1081 } 1082 1083 dev->beacon_hang_check = 0; 1084 mt76_set(dev, MT_MAC_SYS_CTRL, MT_MAC_SYS_CTRL_RESET_CSR); 1085 udelay(10); 1086 1087 out: 1088 mt76_wr(dev, MT_MAC_SYS_CTRL, 1089 MT_MAC_SYS_CTRL_ENABLE_TX | MT_MAC_SYS_CTRL_ENABLE_RX); 1090 } 1091 1092 static void 1093 mt76x02_edcca_tx_enable(struct mt76x02_dev *dev, bool enable) 1094 { 1095 if (enable) { 1096 u32 data; 1097 1098 mt76_set(dev, MT_MAC_SYS_CTRL, MT_MAC_SYS_CTRL_ENABLE_TX); 1099 mt76_set(dev, MT_AUTO_RSP_CFG, MT_AUTO_RSP_EN); 1100 /* enable pa-lna */ 1101 data = mt76_rr(dev, MT_TX_PIN_CFG); 1102 data |= MT_TX_PIN_CFG_TXANT | 1103 MT_TX_PIN_CFG_RXANT | 1104 MT_TX_PIN_RFTR_EN | 1105 MT_TX_PIN_TRSW_EN; 1106 mt76_wr(dev, MT_TX_PIN_CFG, data); 1107 } else { 1108 mt76_clear(dev, MT_MAC_SYS_CTRL, MT_MAC_SYS_CTRL_ENABLE_TX); 1109 mt76_clear(dev, MT_AUTO_RSP_CFG, MT_AUTO_RSP_EN); 1110 /* disable pa-lna */ 1111 mt76_clear(dev, MT_TX_PIN_CFG, MT_TX_PIN_CFG_TXANT); 1112 mt76_clear(dev, MT_TX_PIN_CFG, MT_TX_PIN_CFG_RXANT); 1113 } 1114 dev->ed_tx_blocked = !enable; 1115 } 1116 1117 void mt76x02_edcca_init(struct mt76x02_dev *dev) 1118 { 1119 dev->ed_trigger = 0; 1120 dev->ed_silent = 0; 1121 1122 if (dev->ed_monitor) { 1123 struct ieee80211_channel *chan = dev->mphy.chandef.chan; 1124 u8 ed_th = chan->band == NL80211_BAND_5GHZ ? 0x0e : 0x20; 1125 1126 mt76_clear(dev, MT_TX_LINK_CFG, MT_TX_CFACK_EN); 1127 mt76_set(dev, MT_TXOP_CTRL_CFG, MT_TXOP_ED_CCA_EN); 1128 mt76_rmw(dev, MT_BBP(AGC, 2), GENMASK(15, 0), 1129 ed_th << 8 | ed_th); 1130 mt76_set(dev, MT_TXOP_HLDR_ET, MT_TXOP_HLDR_TX40M_BLK_EN); 1131 } else { 1132 mt76_set(dev, MT_TX_LINK_CFG, MT_TX_CFACK_EN); 1133 mt76_clear(dev, MT_TXOP_CTRL_CFG, MT_TXOP_ED_CCA_EN); 1134 if (is_mt76x2(dev)) { 1135 mt76_wr(dev, MT_BBP(AGC, 2), 0x00007070); 1136 mt76_set(dev, MT_TXOP_HLDR_ET, 1137 MT_TXOP_HLDR_TX40M_BLK_EN); 1138 } else { 1139 mt76_wr(dev, MT_BBP(AGC, 2), 0x003a6464); 1140 mt76_clear(dev, MT_TXOP_HLDR_ET, 1141 MT_TXOP_HLDR_TX40M_BLK_EN); 1142 } 1143 } 1144 mt76x02_edcca_tx_enable(dev, true); 1145 dev->ed_monitor_learning = true; 1146 1147 /* clear previous CCA timer value */ 1148 mt76_rr(dev, MT_ED_CCA_TIMER); 1149 dev->ed_time = ktime_get_boottime(); 1150 } 1151 EXPORT_SYMBOL_GPL(mt76x02_edcca_init); 1152 1153 #define MT_EDCCA_TH 92 1154 #define MT_EDCCA_BLOCK_TH 2 1155 #define MT_EDCCA_LEARN_TH 50 1156 #define MT_EDCCA_LEARN_CCA 180 1157 #define MT_EDCCA_LEARN_TIMEOUT (20 * HZ) 1158 1159 static void mt76x02_edcca_check(struct mt76x02_dev *dev) 1160 { 1161 ktime_t cur_time; 1162 u32 active, val, busy; 1163 1164 cur_time = ktime_get_boottime(); 1165 val = mt76_rr(dev, MT_ED_CCA_TIMER); 1166 1167 active = ktime_to_us(ktime_sub(cur_time, dev->ed_time)); 1168 dev->ed_time = cur_time; 1169 1170 busy = (val * 100) / active; 1171 busy = min_t(u32, busy, 100); 1172 1173 if (busy > MT_EDCCA_TH) { 1174 dev->ed_trigger++; 1175 dev->ed_silent = 0; 1176 } else { 1177 dev->ed_silent++; 1178 dev->ed_trigger = 0; 1179 } 1180 1181 if (dev->cal.agc_lowest_gain && 1182 dev->cal.false_cca > MT_EDCCA_LEARN_CCA && 1183 dev->ed_trigger > MT_EDCCA_LEARN_TH) { 1184 dev->ed_monitor_learning = false; 1185 dev->ed_trigger_timeout = jiffies + 20 * HZ; 1186 } else if (!dev->ed_monitor_learning && 1187 time_is_after_jiffies(dev->ed_trigger_timeout)) { 1188 dev->ed_monitor_learning = true; 1189 mt76x02_edcca_tx_enable(dev, true); 1190 } 1191 1192 if (dev->ed_monitor_learning) 1193 return; 1194 1195 if (dev->ed_trigger > MT_EDCCA_BLOCK_TH && !dev->ed_tx_blocked) 1196 mt76x02_edcca_tx_enable(dev, false); 1197 else if (dev->ed_silent > MT_EDCCA_BLOCK_TH && dev->ed_tx_blocked) 1198 mt76x02_edcca_tx_enable(dev, true); 1199 } 1200 1201 void mt76x02_mac_work(struct work_struct *work) 1202 { 1203 struct mt76x02_dev *dev = container_of(work, struct mt76x02_dev, 1204 mphy.mac_work.work); 1205 int i, idx; 1206 1207 mutex_lock(&dev->mt76.mutex); 1208 1209 mt76_update_survey(&dev->mphy); 1210 for (i = 0, idx = 0; i < 16; i++) { 1211 u32 val = mt76_rr(dev, MT_TX_AGG_CNT(i)); 1212 1213 dev->mphy.aggr_stats[idx++] += val & 0xffff; 1214 dev->mphy.aggr_stats[idx++] += val >> 16; 1215 } 1216 1217 mt76x02_check_mac_err(dev); 1218 1219 if (dev->ed_monitor) 1220 mt76x02_edcca_check(dev); 1221 1222 mutex_unlock(&dev->mt76.mutex); 1223 1224 mt76_tx_status_check(&dev->mt76, false); 1225 1226 ieee80211_queue_delayed_work(mt76_hw(dev), &dev->mphy.mac_work, 1227 MT_MAC_WORK_INTERVAL); 1228 } 1229 1230 void mt76x02_mac_cc_reset(struct mt76x02_dev *dev) 1231 { 1232 dev->mphy.survey_time = ktime_get_boottime(); 1233 1234 mt76_wr(dev, MT_CH_TIME_CFG, 1235 MT_CH_TIME_CFG_TIMER_EN | 1236 MT_CH_TIME_CFG_TX_AS_BUSY | 1237 MT_CH_TIME_CFG_RX_AS_BUSY | 1238 MT_CH_TIME_CFG_NAV_AS_BUSY | 1239 MT_CH_TIME_CFG_EIFS_AS_BUSY | 1240 MT_CH_CCA_RC_EN | 1241 FIELD_PREP(MT_CH_TIME_CFG_CH_TIMER_CLR, 1)); 1242 1243 /* channel cycle counters read-and-clear */ 1244 mt76_rr(dev, MT_CH_BUSY); 1245 mt76_rr(dev, MT_CH_IDLE); 1246 } 1247 EXPORT_SYMBOL_GPL(mt76x02_mac_cc_reset); 1248 1249 void mt76x02_mac_set_bssid(struct mt76x02_dev *dev, u8 idx, const u8 *addr) 1250 { 1251 idx &= 7; 1252 mt76_wr(dev, MT_MAC_APC_BSSID_L(idx), get_unaligned_le32(addr)); 1253 mt76_rmw_field(dev, MT_MAC_APC_BSSID_H(idx), MT_MAC_APC_BSSID_H_ADDR, 1254 get_unaligned_le16(addr + 4)); 1255 } 1256