1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause 2 /* Copyright(c) 2018-2019 Realtek Corporation 3 */ 4 5 #include "main.h" 6 #include "tx.h" 7 #include "fw.h" 8 #include "ps.h" 9 #include "debug.h" 10 11 static 12 void rtw_tx_stats(struct rtw_dev *rtwdev, struct ieee80211_vif *vif, 13 struct sk_buff *skb) 14 { 15 struct ieee80211_hdr *hdr; 16 struct rtw_vif *rtwvif; 17 18 hdr = (struct ieee80211_hdr *)skb->data; 19 20 if (!ieee80211_is_data(hdr->frame_control)) 21 return; 22 23 if (!is_broadcast_ether_addr(hdr->addr1) && 24 !is_multicast_ether_addr(hdr->addr1)) { 25 rtwdev->stats.tx_unicast += skb->len; 26 rtwdev->stats.tx_cnt++; 27 if (vif) { 28 rtwvif = (struct rtw_vif *)vif->drv_priv; 29 rtwvif->stats.tx_unicast += skb->len; 30 rtwvif->stats.tx_cnt++; 31 } 32 } 33 } 34 35 void rtw_tx_fill_tx_desc(struct rtw_tx_pkt_info *pkt_info, struct sk_buff *skb) 36 { 37 __le32 *txdesc = (__le32 *)skb->data; 38 39 SET_TX_DESC_TXPKTSIZE(txdesc, pkt_info->tx_pkt_size); 40 SET_TX_DESC_OFFSET(txdesc, pkt_info->offset); 41 SET_TX_DESC_PKT_OFFSET(txdesc, pkt_info->pkt_offset); 42 SET_TX_DESC_QSEL(txdesc, pkt_info->qsel); 43 SET_TX_DESC_BMC(txdesc, pkt_info->bmc); 44 SET_TX_DESC_RATE_ID(txdesc, pkt_info->rate_id); 45 SET_TX_DESC_DATARATE(txdesc, pkt_info->rate); 46 SET_TX_DESC_DISDATAFB(txdesc, pkt_info->dis_rate_fallback); 47 SET_TX_DESC_USE_RATE(txdesc, pkt_info->use_rate); 48 SET_TX_DESC_SEC_TYPE(txdesc, pkt_info->sec_type); 49 SET_TX_DESC_DATA_BW(txdesc, pkt_info->bw); 50 SET_TX_DESC_SW_SEQ(txdesc, pkt_info->seq); 51 SET_TX_DESC_MAX_AGG_NUM(txdesc, pkt_info->ampdu_factor); 52 SET_TX_DESC_AMPDU_DENSITY(txdesc, pkt_info->ampdu_density); 53 SET_TX_DESC_DATA_STBC(txdesc, pkt_info->stbc); 54 SET_TX_DESC_DATA_LDPC(txdesc, pkt_info->ldpc); 55 SET_TX_DESC_AGG_EN(txdesc, pkt_info->ampdu_en); 56 SET_TX_DESC_LS(txdesc, pkt_info->ls); 57 SET_TX_DESC_DATA_SHORT(txdesc, pkt_info->short_gi); 58 SET_TX_DESC_SPE_RPT(txdesc, pkt_info->report); 59 SET_TX_DESC_SW_DEFINE(txdesc, pkt_info->sn); 60 SET_TX_DESC_USE_RTS(txdesc, pkt_info->rts); 61 SET_TX_DESC_DISQSELSEQ(txdesc, pkt_info->dis_qselseq); 62 SET_TX_DESC_EN_HWSEQ(txdesc, pkt_info->en_hwseq); 63 SET_TX_DESC_HW_SSN_SEL(txdesc, pkt_info->hw_ssn_sel); 64 } 65 EXPORT_SYMBOL(rtw_tx_fill_tx_desc); 66 67 static u8 get_tx_ampdu_factor(struct ieee80211_sta *sta) 68 { 69 u8 exp = sta->ht_cap.ampdu_factor; 70 71 /* the least ampdu factor is 8K, and the value in the tx desc is the 72 * max aggregation num, which represents val * 2 packets can be 73 * aggregated in an AMPDU, so here we should use 8/2=4 as the base 74 */ 75 return (BIT(2) << exp) - 1; 76 } 77 78 static u8 get_tx_ampdu_density(struct ieee80211_sta *sta) 79 { 80 return sta->ht_cap.ampdu_density; 81 } 82 83 static u8 get_highest_ht_tx_rate(struct rtw_dev *rtwdev, 84 struct ieee80211_sta *sta) 85 { 86 u8 rate; 87 88 if (rtwdev->hal.rf_type == RF_2T2R && sta->ht_cap.mcs.rx_mask[1] != 0) 89 rate = DESC_RATEMCS15; 90 else 91 rate = DESC_RATEMCS7; 92 93 return rate; 94 } 95 96 static u8 get_highest_vht_tx_rate(struct rtw_dev *rtwdev, 97 struct ieee80211_sta *sta) 98 { 99 struct rtw_efuse *efuse = &rtwdev->efuse; 100 u8 rate; 101 u16 tx_mcs_map; 102 103 tx_mcs_map = le16_to_cpu(sta->vht_cap.vht_mcs.tx_mcs_map); 104 if (efuse->hw_cap.nss == 1) { 105 switch (tx_mcs_map & 0x3) { 106 case IEEE80211_VHT_MCS_SUPPORT_0_7: 107 rate = DESC_RATEVHT1SS_MCS7; 108 break; 109 case IEEE80211_VHT_MCS_SUPPORT_0_8: 110 rate = DESC_RATEVHT1SS_MCS8; 111 break; 112 default: 113 case IEEE80211_VHT_MCS_SUPPORT_0_9: 114 rate = DESC_RATEVHT1SS_MCS9; 115 break; 116 } 117 } else if (efuse->hw_cap.nss >= 2) { 118 switch ((tx_mcs_map & 0xc) >> 2) { 119 case IEEE80211_VHT_MCS_SUPPORT_0_7: 120 rate = DESC_RATEVHT2SS_MCS7; 121 break; 122 case IEEE80211_VHT_MCS_SUPPORT_0_8: 123 rate = DESC_RATEVHT2SS_MCS8; 124 break; 125 default: 126 case IEEE80211_VHT_MCS_SUPPORT_0_9: 127 rate = DESC_RATEVHT2SS_MCS9; 128 break; 129 } 130 } else { 131 rate = DESC_RATEVHT1SS_MCS9; 132 } 133 134 return rate; 135 } 136 137 static void rtw_tx_report_enable(struct rtw_dev *rtwdev, 138 struct rtw_tx_pkt_info *pkt_info) 139 { 140 struct rtw_tx_report *tx_report = &rtwdev->tx_report; 141 142 /* [11:8], reserved, fills with zero 143 * [7:2], tx report sequence number 144 * [1:0], firmware use, fills with zero 145 */ 146 pkt_info->sn = (atomic_inc_return(&tx_report->sn) << 2) & 0xfc; 147 pkt_info->report = true; 148 } 149 150 void rtw_tx_report_purge_timer(struct timer_list *t) 151 { 152 struct rtw_dev *rtwdev = from_timer(rtwdev, t, tx_report.purge_timer); 153 struct rtw_tx_report *tx_report = &rtwdev->tx_report; 154 unsigned long flags; 155 156 if (skb_queue_len(&tx_report->queue) == 0) 157 return; 158 159 WARN(1, "purge skb(s) not reported by firmware\n"); 160 161 spin_lock_irqsave(&tx_report->q_lock, flags); 162 skb_queue_purge(&tx_report->queue); 163 spin_unlock_irqrestore(&tx_report->q_lock, flags); 164 } 165 166 void rtw_tx_report_enqueue(struct rtw_dev *rtwdev, struct sk_buff *skb, u8 sn) 167 { 168 struct rtw_tx_report *tx_report = &rtwdev->tx_report; 169 unsigned long flags; 170 u8 *drv_data; 171 172 /* pass sn to tx report handler through driver data */ 173 drv_data = (u8 *)IEEE80211_SKB_CB(skb)->status.status_driver_data; 174 *drv_data = sn; 175 176 spin_lock_irqsave(&tx_report->q_lock, flags); 177 __skb_queue_tail(&tx_report->queue, skb); 178 spin_unlock_irqrestore(&tx_report->q_lock, flags); 179 180 mod_timer(&tx_report->purge_timer, jiffies + RTW_TX_PROBE_TIMEOUT); 181 } 182 EXPORT_SYMBOL(rtw_tx_report_enqueue); 183 184 static void rtw_tx_report_tx_status(struct rtw_dev *rtwdev, 185 struct sk_buff *skb, bool acked) 186 { 187 struct ieee80211_tx_info *info; 188 189 info = IEEE80211_SKB_CB(skb); 190 ieee80211_tx_info_clear_status(info); 191 if (acked) 192 info->flags |= IEEE80211_TX_STAT_ACK; 193 else 194 info->flags &= ~IEEE80211_TX_STAT_ACK; 195 196 ieee80211_tx_status_irqsafe(rtwdev->hw, skb); 197 } 198 199 void rtw_tx_report_handle(struct rtw_dev *rtwdev, struct sk_buff *skb, int src) 200 { 201 struct rtw_tx_report *tx_report = &rtwdev->tx_report; 202 struct rtw_c2h_cmd *c2h; 203 struct sk_buff *cur, *tmp; 204 unsigned long flags; 205 u8 sn, st; 206 u8 *n; 207 208 c2h = get_c2h_from_skb(skb); 209 210 if (src == C2H_CCX_TX_RPT) { 211 sn = GET_CCX_REPORT_SEQNUM_V0(c2h->payload); 212 st = GET_CCX_REPORT_STATUS_V0(c2h->payload); 213 } else { 214 sn = GET_CCX_REPORT_SEQNUM_V1(c2h->payload); 215 st = GET_CCX_REPORT_STATUS_V1(c2h->payload); 216 } 217 218 spin_lock_irqsave(&tx_report->q_lock, flags); 219 skb_queue_walk_safe(&tx_report->queue, cur, tmp) { 220 n = (u8 *)IEEE80211_SKB_CB(cur)->status.status_driver_data; 221 if (*n == sn) { 222 __skb_unlink(cur, &tx_report->queue); 223 rtw_tx_report_tx_status(rtwdev, cur, st == 0); 224 break; 225 } 226 } 227 spin_unlock_irqrestore(&tx_report->q_lock, flags); 228 } 229 230 static void rtw_tx_mgmt_pkt_info_update(struct rtw_dev *rtwdev, 231 struct rtw_tx_pkt_info *pkt_info, 232 struct ieee80211_sta *sta, 233 struct sk_buff *skb) 234 { 235 pkt_info->use_rate = true; 236 pkt_info->rate_id = 6; 237 pkt_info->dis_rate_fallback = true; 238 pkt_info->dis_qselseq = true; 239 pkt_info->en_hwseq = true; 240 pkt_info->hw_ssn_sel = 0; 241 } 242 243 static void rtw_tx_data_pkt_info_update(struct rtw_dev *rtwdev, 244 struct rtw_tx_pkt_info *pkt_info, 245 struct ieee80211_sta *sta, 246 struct sk_buff *skb) 247 { 248 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data; 249 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); 250 struct rtw_sta_info *si; 251 u16 seq; 252 u8 ampdu_factor = 0; 253 u8 ampdu_density = 0; 254 bool ampdu_en = false; 255 u8 rate = DESC_RATE6M; 256 u8 rate_id = 6; 257 u8 bw = RTW_CHANNEL_WIDTH_20; 258 bool stbc = false; 259 bool ldpc = false; 260 261 seq = (le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_SEQ) >> 4; 262 263 /* for broadcast/multicast, use default values */ 264 if (!sta) 265 goto out; 266 267 if (info->flags & IEEE80211_TX_CTL_AMPDU) { 268 ampdu_en = true; 269 ampdu_factor = get_tx_ampdu_factor(sta); 270 ampdu_density = get_tx_ampdu_density(sta); 271 } 272 273 if (info->control.use_rts) 274 pkt_info->rts = true; 275 276 if (sta->vht_cap.vht_supported) 277 rate = get_highest_vht_tx_rate(rtwdev, sta); 278 else if (sta->ht_cap.ht_supported) 279 rate = get_highest_ht_tx_rate(rtwdev, sta); 280 else if (sta->supp_rates[0] <= 0xf) 281 rate = DESC_RATE11M; 282 else 283 rate = DESC_RATE54M; 284 285 si = (struct rtw_sta_info *)sta->drv_priv; 286 287 bw = si->bw_mode; 288 rate_id = si->rate_id; 289 stbc = si->stbc_en; 290 ldpc = si->ldpc_en; 291 292 out: 293 pkt_info->seq = seq; 294 pkt_info->ampdu_factor = ampdu_factor; 295 pkt_info->ampdu_density = ampdu_density; 296 pkt_info->ampdu_en = ampdu_en; 297 pkt_info->rate = rate; 298 pkt_info->rate_id = rate_id; 299 pkt_info->bw = bw; 300 pkt_info->stbc = stbc; 301 pkt_info->ldpc = ldpc; 302 } 303 304 void rtw_tx_pkt_info_update(struct rtw_dev *rtwdev, 305 struct rtw_tx_pkt_info *pkt_info, 306 struct ieee80211_sta *sta, 307 struct sk_buff *skb) 308 { 309 struct rtw_chip_info *chip = rtwdev->chip; 310 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); 311 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data; 312 struct rtw_sta_info *si; 313 struct ieee80211_vif *vif = NULL; 314 __le16 fc = hdr->frame_control; 315 u8 sec_type = 0; 316 bool bmc; 317 318 if (sta) { 319 si = (struct rtw_sta_info *)sta->drv_priv; 320 vif = si->vif; 321 } 322 323 if (ieee80211_is_mgmt(fc) || ieee80211_is_nullfunc(fc)) 324 rtw_tx_mgmt_pkt_info_update(rtwdev, pkt_info, sta, skb); 325 else if (ieee80211_is_data(fc)) 326 rtw_tx_data_pkt_info_update(rtwdev, pkt_info, sta, skb); 327 328 if (info->control.hw_key) { 329 struct ieee80211_key_conf *key = info->control.hw_key; 330 331 switch (key->cipher) { 332 case WLAN_CIPHER_SUITE_WEP40: 333 case WLAN_CIPHER_SUITE_WEP104: 334 case WLAN_CIPHER_SUITE_TKIP: 335 sec_type = 0x01; 336 break; 337 case WLAN_CIPHER_SUITE_CCMP: 338 sec_type = 0x03; 339 break; 340 default: 341 break; 342 } 343 } 344 345 bmc = is_broadcast_ether_addr(hdr->addr1) || 346 is_multicast_ether_addr(hdr->addr1); 347 348 if (info->flags & IEEE80211_TX_CTL_REQ_TX_STATUS) 349 rtw_tx_report_enable(rtwdev, pkt_info); 350 351 pkt_info->bmc = bmc; 352 pkt_info->sec_type = sec_type; 353 pkt_info->tx_pkt_size = skb->len; 354 pkt_info->offset = chip->tx_pkt_desc_sz; 355 pkt_info->qsel = skb->priority; 356 pkt_info->ls = true; 357 358 /* maybe merge with tx status ? */ 359 rtw_tx_stats(rtwdev, vif, skb); 360 } 361 362 void rtw_rsvd_page_pkt_info_update(struct rtw_dev *rtwdev, 363 struct rtw_tx_pkt_info *pkt_info, 364 struct sk_buff *skb) 365 { 366 struct rtw_chip_info *chip = rtwdev->chip; 367 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data; 368 bool bmc; 369 370 bmc = is_broadcast_ether_addr(hdr->addr1) || 371 is_multicast_ether_addr(hdr->addr1); 372 pkt_info->use_rate = true; 373 pkt_info->rate_id = 6; 374 pkt_info->dis_rate_fallback = true; 375 pkt_info->bmc = bmc; 376 pkt_info->tx_pkt_size = skb->len; 377 pkt_info->offset = chip->tx_pkt_desc_sz; 378 pkt_info->qsel = TX_DESC_QSEL_MGMT; 379 pkt_info->ls = true; 380 } 381 382 struct sk_buff * 383 rtw_tx_write_data_rsvd_page_get(struct rtw_dev *rtwdev, 384 struct rtw_tx_pkt_info *pkt_info, 385 u8 *buf, u32 size) 386 { 387 struct rtw_chip_info *chip = rtwdev->chip; 388 struct sk_buff *skb; 389 u32 tx_pkt_desc_sz; 390 u32 length; 391 392 tx_pkt_desc_sz = chip->tx_pkt_desc_sz; 393 length = size + tx_pkt_desc_sz; 394 skb = dev_alloc_skb(length); 395 if (!skb) { 396 rtw_err(rtwdev, "failed to alloc write data rsvd page skb\n"); 397 return NULL; 398 } 399 400 skb_reserve(skb, tx_pkt_desc_sz); 401 skb_put_data(skb, buf, size); 402 pkt_info->tx_pkt_size = size; 403 pkt_info->offset = tx_pkt_desc_sz; 404 405 return skb; 406 } 407 EXPORT_SYMBOL(rtw_tx_write_data_rsvd_page_get); 408 409 struct sk_buff * 410 rtw_tx_write_data_h2c_get(struct rtw_dev *rtwdev, 411 struct rtw_tx_pkt_info *pkt_info, 412 u8 *buf, u32 size) 413 { 414 struct rtw_chip_info *chip = rtwdev->chip; 415 struct sk_buff *skb; 416 u32 tx_pkt_desc_sz; 417 u32 length; 418 419 tx_pkt_desc_sz = chip->tx_pkt_desc_sz; 420 length = size + tx_pkt_desc_sz; 421 skb = dev_alloc_skb(length); 422 if (!skb) { 423 rtw_err(rtwdev, "failed to alloc write data h2c skb\n"); 424 return NULL; 425 } 426 427 skb_reserve(skb, tx_pkt_desc_sz); 428 skb_put_data(skb, buf, size); 429 pkt_info->tx_pkt_size = size; 430 431 return skb; 432 } 433 EXPORT_SYMBOL(rtw_tx_write_data_h2c_get); 434 435 void rtw_tx(struct rtw_dev *rtwdev, 436 struct ieee80211_tx_control *control, 437 struct sk_buff *skb) 438 { 439 struct rtw_tx_pkt_info pkt_info = {0}; 440 int ret; 441 442 rtw_tx_pkt_info_update(rtwdev, &pkt_info, control->sta, skb); 443 ret = rtw_hci_tx_write(rtwdev, &pkt_info, skb); 444 if (ret) { 445 rtw_err(rtwdev, "failed to write TX skb to HCI\n"); 446 goto out; 447 } 448 449 rtw_hci_tx_kick_off(rtwdev); 450 451 return; 452 453 out: 454 ieee80211_free_txskb(rtwdev->hw, skb); 455 } 456 457 static void rtw_txq_check_agg(struct rtw_dev *rtwdev, 458 struct rtw_txq *rtwtxq, 459 struct sk_buff *skb) 460 { 461 struct ieee80211_txq *txq = rtwtxq_to_txq(rtwtxq); 462 struct ieee80211_tx_info *info; 463 struct rtw_sta_info *si; 464 465 if (test_bit(RTW_TXQ_AMPDU, &rtwtxq->flags)) { 466 info = IEEE80211_SKB_CB(skb); 467 info->flags |= IEEE80211_TX_CTL_AMPDU; 468 return; 469 } 470 471 if (skb_get_queue_mapping(skb) == IEEE80211_AC_VO) 472 return; 473 474 if (test_bit(RTW_TXQ_BLOCK_BA, &rtwtxq->flags)) 475 return; 476 477 if (unlikely(skb->protocol == cpu_to_be16(ETH_P_PAE))) 478 return; 479 480 if (!txq->sta) 481 return; 482 483 si = (struct rtw_sta_info *)txq->sta->drv_priv; 484 set_bit(txq->tid, si->tid_ba); 485 486 ieee80211_queue_work(rtwdev->hw, &rtwdev->ba_work); 487 } 488 489 static int rtw_txq_push_skb(struct rtw_dev *rtwdev, 490 struct rtw_txq *rtwtxq, 491 struct sk_buff *skb) 492 { 493 struct ieee80211_txq *txq = rtwtxq_to_txq(rtwtxq); 494 struct rtw_tx_pkt_info pkt_info = {0}; 495 int ret; 496 497 rtw_txq_check_agg(rtwdev, rtwtxq, skb); 498 499 rtw_tx_pkt_info_update(rtwdev, &pkt_info, txq->sta, skb); 500 ret = rtw_hci_tx_write(rtwdev, &pkt_info, skb); 501 if (ret) { 502 rtw_err(rtwdev, "failed to write TX skb to HCI\n"); 503 return ret; 504 } 505 rtwtxq->last_push = jiffies; 506 507 return 0; 508 } 509 510 static struct sk_buff *rtw_txq_dequeue(struct rtw_dev *rtwdev, 511 struct rtw_txq *rtwtxq) 512 { 513 struct ieee80211_txq *txq = rtwtxq_to_txq(rtwtxq); 514 struct sk_buff *skb; 515 516 skb = ieee80211_tx_dequeue(rtwdev->hw, txq); 517 if (!skb) 518 return NULL; 519 520 return skb; 521 } 522 523 static void rtw_txq_push(struct rtw_dev *rtwdev, 524 struct rtw_txq *rtwtxq, 525 unsigned long frames) 526 { 527 struct sk_buff *skb; 528 int ret; 529 int i; 530 531 rcu_read_lock(); 532 533 for (i = 0; i < frames; i++) { 534 skb = rtw_txq_dequeue(rtwdev, rtwtxq); 535 if (!skb) 536 break; 537 538 ret = rtw_txq_push_skb(rtwdev, rtwtxq, skb); 539 if (ret) { 540 rtw_err(rtwdev, "failed to pusk skb, ret %d\n", ret); 541 break; 542 } 543 } 544 545 rcu_read_unlock(); 546 } 547 548 void rtw_tx_tasklet(unsigned long data) 549 { 550 struct rtw_dev *rtwdev = (void *)data; 551 struct rtw_txq *rtwtxq, *tmp; 552 553 spin_lock_bh(&rtwdev->txq_lock); 554 555 list_for_each_entry_safe(rtwtxq, tmp, &rtwdev->txqs, list) { 556 struct ieee80211_txq *txq = rtwtxq_to_txq(rtwtxq); 557 unsigned long frame_cnt; 558 unsigned long byte_cnt; 559 560 ieee80211_txq_get_depth(txq, &frame_cnt, &byte_cnt); 561 rtw_txq_push(rtwdev, rtwtxq, frame_cnt); 562 563 list_del_init(&rtwtxq->list); 564 } 565 566 rtw_hci_tx_kick_off(rtwdev); 567 568 spin_unlock_bh(&rtwdev->txq_lock); 569 } 570 571 void rtw_txq_init(struct rtw_dev *rtwdev, struct ieee80211_txq *txq) 572 { 573 struct rtw_txq *rtwtxq; 574 575 if (!txq) 576 return; 577 578 rtwtxq = (struct rtw_txq *)txq->drv_priv; 579 INIT_LIST_HEAD(&rtwtxq->list); 580 } 581 582 void rtw_txq_cleanup(struct rtw_dev *rtwdev, struct ieee80211_txq *txq) 583 { 584 struct rtw_txq *rtwtxq; 585 586 if (!txq) 587 return; 588 589 rtwtxq = (struct rtw_txq *)txq->drv_priv; 590 spin_lock_bh(&rtwdev->txq_lock); 591 if (!list_empty(&rtwtxq->list)) 592 list_del_init(&rtwtxq->list); 593 spin_unlock_bh(&rtwdev->txq_lock); 594 } 595