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) 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 sn = GET_CCX_REPORT_SEQNUM(c2h->payload); 211 st = GET_CCX_REPORT_STATUS(c2h->payload); 212 213 spin_lock_irqsave(&tx_report->q_lock, flags); 214 skb_queue_walk_safe(&tx_report->queue, cur, tmp) { 215 n = (u8 *)IEEE80211_SKB_CB(cur)->status.status_driver_data; 216 if (*n == sn) { 217 __skb_unlink(cur, &tx_report->queue); 218 rtw_tx_report_tx_status(rtwdev, cur, st == 0); 219 break; 220 } 221 } 222 spin_unlock_irqrestore(&tx_report->q_lock, flags); 223 } 224 225 static void rtw_tx_mgmt_pkt_info_update(struct rtw_dev *rtwdev, 226 struct rtw_tx_pkt_info *pkt_info, 227 struct ieee80211_sta *sta, 228 struct sk_buff *skb) 229 { 230 pkt_info->use_rate = true; 231 pkt_info->rate_id = 6; 232 pkt_info->dis_rate_fallback = true; 233 pkt_info->dis_qselseq = true; 234 pkt_info->en_hwseq = true; 235 pkt_info->hw_ssn_sel = 0; 236 } 237 238 static void rtw_tx_data_pkt_info_update(struct rtw_dev *rtwdev, 239 struct rtw_tx_pkt_info *pkt_info, 240 struct ieee80211_sta *sta, 241 struct sk_buff *skb) 242 { 243 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data; 244 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); 245 struct rtw_sta_info *si; 246 u16 seq; 247 u8 ampdu_factor = 0; 248 u8 ampdu_density = 0; 249 bool ampdu_en = false; 250 u8 rate = DESC_RATE6M; 251 u8 rate_id = 6; 252 u8 bw = RTW_CHANNEL_WIDTH_20; 253 bool stbc = false; 254 bool ldpc = false; 255 256 seq = (le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_SEQ) >> 4; 257 258 /* for broadcast/multicast, use default values */ 259 if (!sta) 260 goto out; 261 262 if (info->flags & IEEE80211_TX_CTL_AMPDU) { 263 ampdu_en = true; 264 ampdu_factor = get_tx_ampdu_factor(sta); 265 ampdu_density = get_tx_ampdu_density(sta); 266 } 267 268 if (info->control.use_rts) 269 pkt_info->rts = true; 270 271 if (sta->vht_cap.vht_supported) 272 rate = get_highest_vht_tx_rate(rtwdev, sta); 273 else if (sta->ht_cap.ht_supported) 274 rate = get_highest_ht_tx_rate(rtwdev, sta); 275 else if (sta->supp_rates[0] <= 0xf) 276 rate = DESC_RATE11M; 277 else 278 rate = DESC_RATE54M; 279 280 si = (struct rtw_sta_info *)sta->drv_priv; 281 282 bw = si->bw_mode; 283 rate_id = si->rate_id; 284 stbc = si->stbc_en; 285 ldpc = si->ldpc_en; 286 287 out: 288 pkt_info->seq = seq; 289 pkt_info->ampdu_factor = ampdu_factor; 290 pkt_info->ampdu_density = ampdu_density; 291 pkt_info->ampdu_en = ampdu_en; 292 pkt_info->rate = rate; 293 pkt_info->rate_id = rate_id; 294 pkt_info->bw = bw; 295 pkt_info->stbc = stbc; 296 pkt_info->ldpc = ldpc; 297 } 298 299 void rtw_tx_pkt_info_update(struct rtw_dev *rtwdev, 300 struct rtw_tx_pkt_info *pkt_info, 301 struct ieee80211_sta *sta, 302 struct sk_buff *skb) 303 { 304 struct rtw_chip_info *chip = rtwdev->chip; 305 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); 306 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data; 307 struct rtw_sta_info *si; 308 struct ieee80211_vif *vif = NULL; 309 __le16 fc = hdr->frame_control; 310 u8 sec_type = 0; 311 bool bmc; 312 313 if (sta) { 314 si = (struct rtw_sta_info *)sta->drv_priv; 315 vif = si->vif; 316 } 317 318 if (ieee80211_is_mgmt(fc) || ieee80211_is_nullfunc(fc)) 319 rtw_tx_mgmt_pkt_info_update(rtwdev, pkt_info, sta, skb); 320 else if (ieee80211_is_data(fc)) 321 rtw_tx_data_pkt_info_update(rtwdev, pkt_info, sta, skb); 322 323 if (info->control.hw_key) { 324 struct ieee80211_key_conf *key = info->control.hw_key; 325 326 switch (key->cipher) { 327 case WLAN_CIPHER_SUITE_WEP40: 328 case WLAN_CIPHER_SUITE_WEP104: 329 case WLAN_CIPHER_SUITE_TKIP: 330 sec_type = 0x01; 331 break; 332 case WLAN_CIPHER_SUITE_CCMP: 333 sec_type = 0x03; 334 break; 335 default: 336 break; 337 } 338 } 339 340 bmc = is_broadcast_ether_addr(hdr->addr1) || 341 is_multicast_ether_addr(hdr->addr1); 342 343 if (info->flags & IEEE80211_TX_CTL_REQ_TX_STATUS) 344 rtw_tx_report_enable(rtwdev, pkt_info); 345 346 pkt_info->bmc = bmc; 347 pkt_info->sec_type = sec_type; 348 pkt_info->tx_pkt_size = skb->len; 349 pkt_info->offset = chip->tx_pkt_desc_sz; 350 pkt_info->qsel = skb->priority; 351 pkt_info->ls = true; 352 353 /* maybe merge with tx status ? */ 354 rtw_tx_stats(rtwdev, vif, skb); 355 } 356 357 void rtw_rsvd_page_pkt_info_update(struct rtw_dev *rtwdev, 358 struct rtw_tx_pkt_info *pkt_info, 359 struct sk_buff *skb) 360 { 361 struct rtw_chip_info *chip = rtwdev->chip; 362 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data; 363 bool bmc; 364 365 bmc = is_broadcast_ether_addr(hdr->addr1) || 366 is_multicast_ether_addr(hdr->addr1); 367 pkt_info->use_rate = true; 368 pkt_info->rate_id = 6; 369 pkt_info->dis_rate_fallback = true; 370 pkt_info->bmc = bmc; 371 pkt_info->tx_pkt_size = skb->len; 372 pkt_info->offset = chip->tx_pkt_desc_sz; 373 pkt_info->qsel = TX_DESC_QSEL_MGMT; 374 pkt_info->ls = true; 375 } 376 377 struct sk_buff * 378 rtw_tx_write_data_rsvd_page_get(struct rtw_dev *rtwdev, 379 struct rtw_tx_pkt_info *pkt_info, 380 u8 *buf, u32 size) 381 { 382 struct rtw_chip_info *chip = rtwdev->chip; 383 struct sk_buff *skb; 384 u32 tx_pkt_desc_sz; 385 u32 length; 386 387 tx_pkt_desc_sz = chip->tx_pkt_desc_sz; 388 length = size + tx_pkt_desc_sz; 389 skb = dev_alloc_skb(length); 390 if (!skb) { 391 rtw_err(rtwdev, "failed to alloc write data rsvd page skb\n"); 392 return NULL; 393 } 394 395 skb_reserve(skb, tx_pkt_desc_sz); 396 skb_put_data(skb, buf, size); 397 pkt_info->tx_pkt_size = size; 398 pkt_info->offset = tx_pkt_desc_sz; 399 400 return skb; 401 } 402 EXPORT_SYMBOL(rtw_tx_write_data_rsvd_page_get); 403 404 struct sk_buff * 405 rtw_tx_write_data_h2c_get(struct rtw_dev *rtwdev, 406 struct rtw_tx_pkt_info *pkt_info, 407 u8 *buf, u32 size) 408 { 409 struct rtw_chip_info *chip = rtwdev->chip; 410 struct sk_buff *skb; 411 u32 tx_pkt_desc_sz; 412 u32 length; 413 414 tx_pkt_desc_sz = chip->tx_pkt_desc_sz; 415 length = size + tx_pkt_desc_sz; 416 skb = dev_alloc_skb(length); 417 if (!skb) { 418 rtw_err(rtwdev, "failed to alloc write data h2c skb\n"); 419 return NULL; 420 } 421 422 skb_reserve(skb, tx_pkt_desc_sz); 423 skb_put_data(skb, buf, size); 424 pkt_info->tx_pkt_size = size; 425 426 return skb; 427 } 428 EXPORT_SYMBOL(rtw_tx_write_data_h2c_get); 429 430 void rtw_tx(struct rtw_dev *rtwdev, 431 struct ieee80211_tx_control *control, 432 struct sk_buff *skb) 433 { 434 struct rtw_tx_pkt_info pkt_info = {0}; 435 int ret; 436 437 rtw_tx_pkt_info_update(rtwdev, &pkt_info, control->sta, skb); 438 ret = rtw_hci_tx_write(rtwdev, &pkt_info, skb); 439 if (ret) { 440 rtw_err(rtwdev, "failed to write TX skb to HCI\n"); 441 goto out; 442 } 443 444 rtw_hci_tx_kick_off(rtwdev); 445 446 return; 447 448 out: 449 ieee80211_free_txskb(rtwdev->hw, skb); 450 } 451 452 static void rtw_txq_check_agg(struct rtw_dev *rtwdev, 453 struct rtw_txq *rtwtxq, 454 struct sk_buff *skb) 455 { 456 struct ieee80211_txq *txq = rtwtxq_to_txq(rtwtxq); 457 struct ieee80211_tx_info *info; 458 struct rtw_sta_info *si; 459 460 if (test_bit(RTW_TXQ_AMPDU, &rtwtxq->flags)) { 461 info = IEEE80211_SKB_CB(skb); 462 info->flags |= IEEE80211_TX_CTL_AMPDU; 463 return; 464 } 465 466 if (skb_get_queue_mapping(skb) == IEEE80211_AC_VO) 467 return; 468 469 if (test_bit(RTW_TXQ_BLOCK_BA, &rtwtxq->flags)) 470 return; 471 472 if (unlikely(skb->protocol == cpu_to_be16(ETH_P_PAE))) 473 return; 474 475 if (!txq->sta) 476 return; 477 478 si = (struct rtw_sta_info *)txq->sta->drv_priv; 479 set_bit(txq->tid, si->tid_ba); 480 481 ieee80211_queue_work(rtwdev->hw, &rtwdev->ba_work); 482 } 483 484 static int rtw_txq_push_skb(struct rtw_dev *rtwdev, 485 struct rtw_txq *rtwtxq, 486 struct sk_buff *skb) 487 { 488 struct ieee80211_txq *txq = rtwtxq_to_txq(rtwtxq); 489 struct rtw_tx_pkt_info pkt_info = {0}; 490 int ret; 491 492 rtw_txq_check_agg(rtwdev, rtwtxq, skb); 493 494 rtw_tx_pkt_info_update(rtwdev, &pkt_info, txq->sta, skb); 495 ret = rtw_hci_tx_write(rtwdev, &pkt_info, skb); 496 if (ret) { 497 rtw_err(rtwdev, "failed to write TX skb to HCI\n"); 498 return ret; 499 } 500 rtwtxq->last_push = jiffies; 501 502 return 0; 503 } 504 505 static struct sk_buff *rtw_txq_dequeue(struct rtw_dev *rtwdev, 506 struct rtw_txq *rtwtxq) 507 { 508 struct ieee80211_txq *txq = rtwtxq_to_txq(rtwtxq); 509 struct sk_buff *skb; 510 511 skb = ieee80211_tx_dequeue(rtwdev->hw, txq); 512 if (!skb) 513 return NULL; 514 515 return skb; 516 } 517 518 static void rtw_txq_push(struct rtw_dev *rtwdev, 519 struct rtw_txq *rtwtxq, 520 unsigned long frames) 521 { 522 struct sk_buff *skb; 523 int ret; 524 int i; 525 526 rcu_read_lock(); 527 528 for (i = 0; i < frames; i++) { 529 skb = rtw_txq_dequeue(rtwdev, rtwtxq); 530 if (!skb) 531 break; 532 533 ret = rtw_txq_push_skb(rtwdev, rtwtxq, skb); 534 if (ret) { 535 rtw_err(rtwdev, "failed to pusk skb, ret %d\n", ret); 536 break; 537 } 538 } 539 540 rcu_read_unlock(); 541 } 542 543 void rtw_tx_tasklet(unsigned long data) 544 { 545 struct rtw_dev *rtwdev = (void *)data; 546 struct rtw_txq *rtwtxq, *tmp; 547 548 spin_lock_bh(&rtwdev->txq_lock); 549 550 list_for_each_entry_safe(rtwtxq, tmp, &rtwdev->txqs, list) { 551 struct ieee80211_txq *txq = rtwtxq_to_txq(rtwtxq); 552 unsigned long frame_cnt; 553 unsigned long byte_cnt; 554 555 ieee80211_txq_get_depth(txq, &frame_cnt, &byte_cnt); 556 rtw_txq_push(rtwdev, rtwtxq, frame_cnt); 557 558 list_del_init(&rtwtxq->list); 559 } 560 561 rtw_hci_tx_kick_off(rtwdev); 562 563 spin_unlock_bh(&rtwdev->txq_lock); 564 } 565 566 void rtw_txq_init(struct rtw_dev *rtwdev, struct ieee80211_txq *txq) 567 { 568 struct rtw_txq *rtwtxq; 569 570 if (!txq) 571 return; 572 573 rtwtxq = (struct rtw_txq *)txq->drv_priv; 574 INIT_LIST_HEAD(&rtwtxq->list); 575 } 576 577 void rtw_txq_cleanup(struct rtw_dev *rtwdev, struct ieee80211_txq *txq) 578 { 579 struct rtw_txq *rtwtxq; 580 581 if (!txq) 582 return; 583 584 rtwtxq = (struct rtw_txq *)txq->drv_priv; 585 spin_lock_bh(&rtwdev->txq_lock); 586 if (!list_empty(&rtwtxq->list)) 587 list_del_init(&rtwtxq->list); 588 spin_unlock_bh(&rtwdev->txq_lock); 589 } 590