1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /* Copyright(c) 2018-2019 Realtek Corporation
12 void rtw_tx_stats(struct rtw_dev
*rtwdev
, struct ieee80211_vif
*vif
,
15 struct ieee80211_hdr
*hdr
;
16 struct rtw_vif
*rtwvif
;
18 hdr
= (struct ieee80211_hdr
*)skb
->data
;
20 if (!ieee80211_is_data(hdr
->frame_control
))
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
++;
28 rtwvif
= (struct rtw_vif
*)vif
->drv_priv
;
29 rtwvif
->stats
.tx_unicast
+= skb
->len
;
30 rtwvif
->stats
.tx_cnt
++;
35 void rtw_tx_fill_tx_desc(struct rtw_tx_pkt_info
*pkt_info
, struct sk_buff
*skb
)
37 __le32
*txdesc
= (__le32
*)skb
->data
;
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 SET_TX_DESC_NAVUSEHDR(txdesc
, pkt_info
->nav_use_hdr
);
65 SET_TX_DESC_BT_NULL(txdesc
, pkt_info
->bt_null
);
67 EXPORT_SYMBOL(rtw_tx_fill_tx_desc
);
69 static u8
get_tx_ampdu_factor(struct ieee80211_sta
*sta
)
71 u8 exp
= sta
->ht_cap
.ampdu_factor
;
73 /* the least ampdu factor is 8K, and the value in the tx desc is the
74 * max aggregation num, which represents val * 2 packets can be
75 * aggregated in an AMPDU, so here we should use 8/2=4 as the base
77 return (BIT(2) << exp
) - 1;
80 static u8
get_tx_ampdu_density(struct ieee80211_sta
*sta
)
82 return sta
->ht_cap
.ampdu_density
;
85 static u8
get_highest_ht_tx_rate(struct rtw_dev
*rtwdev
,
86 struct ieee80211_sta
*sta
)
90 if (rtwdev
->hal
.rf_type
== RF_2T2R
&& sta
->ht_cap
.mcs
.rx_mask
[1] != 0)
91 rate
= DESC_RATEMCS15
;
98 static u8
get_highest_vht_tx_rate(struct rtw_dev
*rtwdev
,
99 struct ieee80211_sta
*sta
)
101 struct rtw_efuse
*efuse
= &rtwdev
->efuse
;
105 tx_mcs_map
= le16_to_cpu(sta
->vht_cap
.vht_mcs
.tx_mcs_map
);
106 if (efuse
->hw_cap
.nss
== 1) {
107 switch (tx_mcs_map
& 0x3) {
108 case IEEE80211_VHT_MCS_SUPPORT_0_7
:
109 rate
= DESC_RATEVHT1SS_MCS7
;
111 case IEEE80211_VHT_MCS_SUPPORT_0_8
:
112 rate
= DESC_RATEVHT1SS_MCS8
;
115 case IEEE80211_VHT_MCS_SUPPORT_0_9
:
116 rate
= DESC_RATEVHT1SS_MCS9
;
119 } else if (efuse
->hw_cap
.nss
>= 2) {
120 switch ((tx_mcs_map
& 0xc) >> 2) {
121 case IEEE80211_VHT_MCS_SUPPORT_0_7
:
122 rate
= DESC_RATEVHT2SS_MCS7
;
124 case IEEE80211_VHT_MCS_SUPPORT_0_8
:
125 rate
= DESC_RATEVHT2SS_MCS8
;
128 case IEEE80211_VHT_MCS_SUPPORT_0_9
:
129 rate
= DESC_RATEVHT2SS_MCS9
;
133 rate
= DESC_RATEVHT1SS_MCS9
;
139 static void rtw_tx_report_enable(struct rtw_dev
*rtwdev
,
140 struct rtw_tx_pkt_info
*pkt_info
)
142 struct rtw_tx_report
*tx_report
= &rtwdev
->tx_report
;
144 /* [11:8], reserved, fills with zero
145 * [7:2], tx report sequence number
146 * [1:0], firmware use, fills with zero
148 pkt_info
->sn
= (atomic_inc_return(&tx_report
->sn
) << 2) & 0xfc;
149 pkt_info
->report
= true;
152 void rtw_tx_report_purge_timer(struct timer_list
*t
)
154 struct rtw_dev
*rtwdev
= from_timer(rtwdev
, t
, tx_report
.purge_timer
);
155 struct rtw_tx_report
*tx_report
= &rtwdev
->tx_report
;
158 if (skb_queue_len(&tx_report
->queue
) == 0)
161 WARN(1, "purge skb(s) not reported by firmware\n");
163 spin_lock_irqsave(&tx_report
->q_lock
, flags
);
164 skb_queue_purge(&tx_report
->queue
);
165 spin_unlock_irqrestore(&tx_report
->q_lock
, flags
);
168 void rtw_tx_report_enqueue(struct rtw_dev
*rtwdev
, struct sk_buff
*skb
, u8 sn
)
170 struct rtw_tx_report
*tx_report
= &rtwdev
->tx_report
;
174 /* pass sn to tx report handler through driver data */
175 drv_data
= (u8
*)IEEE80211_SKB_CB(skb
)->status
.status_driver_data
;
178 spin_lock_irqsave(&tx_report
->q_lock
, flags
);
179 __skb_queue_tail(&tx_report
->queue
, skb
);
180 spin_unlock_irqrestore(&tx_report
->q_lock
, flags
);
182 mod_timer(&tx_report
->purge_timer
, jiffies
+ RTW_TX_PROBE_TIMEOUT
);
184 EXPORT_SYMBOL(rtw_tx_report_enqueue
);
186 static void rtw_tx_report_tx_status(struct rtw_dev
*rtwdev
,
187 struct sk_buff
*skb
, bool acked
)
189 struct ieee80211_tx_info
*info
;
191 info
= IEEE80211_SKB_CB(skb
);
192 ieee80211_tx_info_clear_status(info
);
194 info
->flags
|= IEEE80211_TX_STAT_ACK
;
196 info
->flags
&= ~IEEE80211_TX_STAT_ACK
;
198 ieee80211_tx_status_irqsafe(rtwdev
->hw
, skb
);
201 void rtw_tx_report_handle(struct rtw_dev
*rtwdev
, struct sk_buff
*skb
, int src
)
203 struct rtw_tx_report
*tx_report
= &rtwdev
->tx_report
;
204 struct rtw_c2h_cmd
*c2h
;
205 struct sk_buff
*cur
, *tmp
;
210 c2h
= get_c2h_from_skb(skb
);
212 if (src
== C2H_CCX_TX_RPT
) {
213 sn
= GET_CCX_REPORT_SEQNUM_V0(c2h
->payload
);
214 st
= GET_CCX_REPORT_STATUS_V0(c2h
->payload
);
216 sn
= GET_CCX_REPORT_SEQNUM_V1(c2h
->payload
);
217 st
= GET_CCX_REPORT_STATUS_V1(c2h
->payload
);
220 spin_lock_irqsave(&tx_report
->q_lock
, flags
);
221 skb_queue_walk_safe(&tx_report
->queue
, cur
, tmp
) {
222 n
= (u8
*)IEEE80211_SKB_CB(cur
)->status
.status_driver_data
;
224 __skb_unlink(cur
, &tx_report
->queue
);
225 rtw_tx_report_tx_status(rtwdev
, cur
, st
== 0);
229 spin_unlock_irqrestore(&tx_report
->q_lock
, flags
);
232 static void rtw_tx_pkt_info_update_rate(struct rtw_dev
*rtwdev
,
233 struct rtw_tx_pkt_info
*pkt_info
,
236 if (rtwdev
->hal
.current_band_type
== RTW_BAND_2G
) {
237 pkt_info
->rate_id
= RTW_RATEID_B_20M
;
238 pkt_info
->rate
= DESC_RATE1M
;
240 pkt_info
->rate_id
= RTW_RATEID_G
;
241 pkt_info
->rate
= DESC_RATE6M
;
243 pkt_info
->use_rate
= true;
244 pkt_info
->dis_rate_fallback
= true;
247 static void rtw_tx_pkt_info_update_sec(struct rtw_dev
*rtwdev
,
248 struct rtw_tx_pkt_info
*pkt_info
,
251 struct ieee80211_tx_info
*info
= IEEE80211_SKB_CB(skb
);
254 if (info
&& info
->control
.hw_key
) {
255 struct ieee80211_key_conf
*key
= info
->control
.hw_key
;
257 switch (key
->cipher
) {
258 case WLAN_CIPHER_SUITE_WEP40
:
259 case WLAN_CIPHER_SUITE_WEP104
:
260 case WLAN_CIPHER_SUITE_TKIP
:
263 case WLAN_CIPHER_SUITE_CCMP
:
271 pkt_info
->sec_type
= sec_type
;
274 static void rtw_tx_mgmt_pkt_info_update(struct rtw_dev
*rtwdev
,
275 struct rtw_tx_pkt_info
*pkt_info
,
276 struct ieee80211_sta
*sta
,
279 rtw_tx_pkt_info_update_rate(rtwdev
, pkt_info
, skb
);
280 pkt_info
->dis_qselseq
= true;
281 pkt_info
->en_hwseq
= true;
282 pkt_info
->hw_ssn_sel
= 0;
283 /* TODO: need to change hw port and hw ssn sel for multiple vifs */
286 static void rtw_tx_data_pkt_info_update(struct rtw_dev
*rtwdev
,
287 struct rtw_tx_pkt_info
*pkt_info
,
288 struct ieee80211_sta
*sta
,
291 struct ieee80211_hdr
*hdr
= (struct ieee80211_hdr
*)skb
->data
;
292 struct ieee80211_tx_info
*info
= IEEE80211_SKB_CB(skb
);
293 struct rtw_sta_info
*si
;
296 u8 ampdu_density
= 0;
297 bool ampdu_en
= false;
298 u8 rate
= DESC_RATE6M
;
300 u8 bw
= RTW_CHANNEL_WIDTH_20
;
304 seq
= (le16_to_cpu(hdr
->seq_ctrl
) & IEEE80211_SCTL_SEQ
) >> 4;
306 /* for broadcast/multicast, use default values */
310 if (info
->flags
& IEEE80211_TX_CTL_AMPDU
) {
312 ampdu_factor
= get_tx_ampdu_factor(sta
);
313 ampdu_density
= get_tx_ampdu_density(sta
);
316 if (info
->control
.use_rts
)
317 pkt_info
->rts
= true;
319 if (sta
->vht_cap
.vht_supported
)
320 rate
= get_highest_vht_tx_rate(rtwdev
, sta
);
321 else if (sta
->ht_cap
.ht_supported
)
322 rate
= get_highest_ht_tx_rate(rtwdev
, sta
);
323 else if (sta
->supp_rates
[0] <= 0xf)
328 si
= (struct rtw_sta_info
*)sta
->drv_priv
;
331 rate_id
= si
->rate_id
;
337 pkt_info
->ampdu_factor
= ampdu_factor
;
338 pkt_info
->ampdu_density
= ampdu_density
;
339 pkt_info
->ampdu_en
= ampdu_en
;
340 pkt_info
->rate
= rate
;
341 pkt_info
->rate_id
= rate_id
;
343 pkt_info
->stbc
= stbc
;
344 pkt_info
->ldpc
= ldpc
;
347 void rtw_tx_pkt_info_update(struct rtw_dev
*rtwdev
,
348 struct rtw_tx_pkt_info
*pkt_info
,
349 struct ieee80211_sta
*sta
,
352 struct rtw_chip_info
*chip
= rtwdev
->chip
;
353 struct ieee80211_tx_info
*info
= IEEE80211_SKB_CB(skb
);
354 struct ieee80211_hdr
*hdr
= (struct ieee80211_hdr
*)skb
->data
;
355 struct rtw_sta_info
*si
;
356 struct ieee80211_vif
*vif
= NULL
;
357 __le16 fc
= hdr
->frame_control
;
361 si
= (struct rtw_sta_info
*)sta
->drv_priv
;
365 if (ieee80211_is_mgmt(fc
) || ieee80211_is_nullfunc(fc
))
366 rtw_tx_mgmt_pkt_info_update(rtwdev
, pkt_info
, sta
, skb
);
367 else if (ieee80211_is_data(fc
))
368 rtw_tx_data_pkt_info_update(rtwdev
, pkt_info
, sta
, skb
);
370 bmc
= is_broadcast_ether_addr(hdr
->addr1
) ||
371 is_multicast_ether_addr(hdr
->addr1
);
373 if (info
->flags
& IEEE80211_TX_CTL_REQ_TX_STATUS
)
374 rtw_tx_report_enable(rtwdev
, pkt_info
);
377 rtw_tx_pkt_info_update_sec(rtwdev
, pkt_info
, skb
);
378 pkt_info
->tx_pkt_size
= skb
->len
;
379 pkt_info
->offset
= chip
->tx_pkt_desc_sz
;
380 pkt_info
->qsel
= skb
->priority
;
383 /* maybe merge with tx status ? */
384 rtw_tx_stats(rtwdev
, vif
, skb
);
387 void rtw_tx_rsvd_page_pkt_info_update(struct rtw_dev
*rtwdev
,
388 struct rtw_tx_pkt_info
*pkt_info
,
390 enum rtw_rsvd_packet_type type
)
392 struct rtw_chip_info
*chip
= rtwdev
->chip
;
393 struct ieee80211_hdr
*hdr
= (struct ieee80211_hdr
*)skb
->data
;
396 /* A beacon or dummy reserved page packet indicates that it is the first
397 * reserved page, and the qsel of it will be set in each hci.
399 if (type
!= RSVD_BEACON
&& type
!= RSVD_DUMMY
)
400 pkt_info
->qsel
= TX_DESC_QSEL_MGMT
;
402 rtw_tx_pkt_info_update_rate(rtwdev
, pkt_info
, skb
);
404 bmc
= is_broadcast_ether_addr(hdr
->addr1
) ||
405 is_multicast_ether_addr(hdr
->addr1
);
407 pkt_info
->tx_pkt_size
= skb
->len
;
408 pkt_info
->offset
= chip
->tx_pkt_desc_sz
;
410 if (type
== RSVD_PS_POLL
) {
411 pkt_info
->nav_use_hdr
= true;
413 pkt_info
->dis_qselseq
= true;
414 pkt_info
->en_hwseq
= true;
415 pkt_info
->hw_ssn_sel
= 0;
417 if (type
== RSVD_QOS_NULL
)
418 pkt_info
->bt_null
= true;
420 rtw_tx_pkt_info_update_sec(rtwdev
, pkt_info
, skb
);
422 /* TODO: need to change hw port and hw ssn sel for multiple vifs */
426 rtw_tx_write_data_rsvd_page_get(struct rtw_dev
*rtwdev
,
427 struct rtw_tx_pkt_info
*pkt_info
,
430 struct rtw_chip_info
*chip
= rtwdev
->chip
;
435 tx_pkt_desc_sz
= chip
->tx_pkt_desc_sz
;
436 length
= size
+ tx_pkt_desc_sz
;
437 skb
= dev_alloc_skb(length
);
439 rtw_err(rtwdev
, "failed to alloc write data rsvd page skb\n");
443 skb_reserve(skb
, tx_pkt_desc_sz
);
444 skb_put_data(skb
, buf
, size
);
445 rtw_tx_rsvd_page_pkt_info_update(rtwdev
, pkt_info
, skb
, RSVD_BEACON
);
449 EXPORT_SYMBOL(rtw_tx_write_data_rsvd_page_get
);
452 rtw_tx_write_data_h2c_get(struct rtw_dev
*rtwdev
,
453 struct rtw_tx_pkt_info
*pkt_info
,
456 struct rtw_chip_info
*chip
= rtwdev
->chip
;
461 tx_pkt_desc_sz
= chip
->tx_pkt_desc_sz
;
462 length
= size
+ tx_pkt_desc_sz
;
463 skb
= dev_alloc_skb(length
);
465 rtw_err(rtwdev
, "failed to alloc write data h2c skb\n");
469 skb_reserve(skb
, tx_pkt_desc_sz
);
470 skb_put_data(skb
, buf
, size
);
471 pkt_info
->tx_pkt_size
= size
;
475 EXPORT_SYMBOL(rtw_tx_write_data_h2c_get
);
477 void rtw_tx(struct rtw_dev
*rtwdev
,
478 struct ieee80211_tx_control
*control
,
481 struct rtw_tx_pkt_info pkt_info
= {0};
484 rtw_tx_pkt_info_update(rtwdev
, &pkt_info
, control
->sta
, skb
);
485 ret
= rtw_hci_tx_write(rtwdev
, &pkt_info
, skb
);
487 rtw_err(rtwdev
, "failed to write TX skb to HCI\n");
491 rtw_hci_tx_kick_off(rtwdev
);
496 ieee80211_free_txskb(rtwdev
->hw
, skb
);
499 static void rtw_txq_check_agg(struct rtw_dev
*rtwdev
,
500 struct rtw_txq
*rtwtxq
,
503 struct ieee80211_txq
*txq
= rtwtxq_to_txq(rtwtxq
);
504 struct ieee80211_tx_info
*info
;
505 struct rtw_sta_info
*si
;
507 if (test_bit(RTW_TXQ_AMPDU
, &rtwtxq
->flags
)) {
508 info
= IEEE80211_SKB_CB(skb
);
509 info
->flags
|= IEEE80211_TX_CTL_AMPDU
;
513 if (skb_get_queue_mapping(skb
) == IEEE80211_AC_VO
)
516 if (test_bit(RTW_TXQ_BLOCK_BA
, &rtwtxq
->flags
))
519 if (unlikely(skb
->protocol
== cpu_to_be16(ETH_P_PAE
)))
525 si
= (struct rtw_sta_info
*)txq
->sta
->drv_priv
;
526 set_bit(txq
->tid
, si
->tid_ba
);
528 ieee80211_queue_work(rtwdev
->hw
, &rtwdev
->ba_work
);
531 static int rtw_txq_push_skb(struct rtw_dev
*rtwdev
,
532 struct rtw_txq
*rtwtxq
,
535 struct ieee80211_txq
*txq
= rtwtxq_to_txq(rtwtxq
);
536 struct rtw_tx_pkt_info pkt_info
= {0};
539 rtw_txq_check_agg(rtwdev
, rtwtxq
, skb
);
541 rtw_tx_pkt_info_update(rtwdev
, &pkt_info
, txq
->sta
, skb
);
542 ret
= rtw_hci_tx_write(rtwdev
, &pkt_info
, skb
);
544 rtw_err(rtwdev
, "failed to write TX skb to HCI\n");
547 rtwtxq
->last_push
= jiffies
;
552 static struct sk_buff
*rtw_txq_dequeue(struct rtw_dev
*rtwdev
,
553 struct rtw_txq
*rtwtxq
)
555 struct ieee80211_txq
*txq
= rtwtxq_to_txq(rtwtxq
);
558 skb
= ieee80211_tx_dequeue(rtwdev
->hw
, txq
);
565 static void rtw_txq_push(struct rtw_dev
*rtwdev
,
566 struct rtw_txq
*rtwtxq
,
567 unsigned long frames
)
575 for (i
= 0; i
< frames
; i
++) {
576 skb
= rtw_txq_dequeue(rtwdev
, rtwtxq
);
580 ret
= rtw_txq_push_skb(rtwdev
, rtwtxq
, skb
);
582 rtw_err(rtwdev
, "failed to pusk skb, ret %d\n", ret
);
590 void rtw_tx_tasklet(struct tasklet_struct
*t
)
592 struct rtw_dev
*rtwdev
= from_tasklet(rtwdev
, t
, tx_tasklet
);
593 struct rtw_txq
*rtwtxq
, *tmp
;
595 spin_lock_bh(&rtwdev
->txq_lock
);
597 list_for_each_entry_safe(rtwtxq
, tmp
, &rtwdev
->txqs
, list
) {
598 struct ieee80211_txq
*txq
= rtwtxq_to_txq(rtwtxq
);
599 unsigned long frame_cnt
;
600 unsigned long byte_cnt
;
602 ieee80211_txq_get_depth(txq
, &frame_cnt
, &byte_cnt
);
603 rtw_txq_push(rtwdev
, rtwtxq
, frame_cnt
);
605 list_del_init(&rtwtxq
->list
);
608 rtw_hci_tx_kick_off(rtwdev
);
610 spin_unlock_bh(&rtwdev
->txq_lock
);
613 void rtw_txq_init(struct rtw_dev
*rtwdev
, struct ieee80211_txq
*txq
)
615 struct rtw_txq
*rtwtxq
;
620 rtwtxq
= (struct rtw_txq
*)txq
->drv_priv
;
621 INIT_LIST_HEAD(&rtwtxq
->list
);
624 void rtw_txq_cleanup(struct rtw_dev
*rtwdev
, struct ieee80211_txq
*txq
)
626 struct rtw_txq
*rtwtxq
;
631 rtwtxq
= (struct rtw_txq
*)txq
->drv_priv
;
632 spin_lock_bh(&rtwdev
->txq_lock
);
633 if (!list_empty(&rtwtxq
->list
))
634 list_del_init(&rtwtxq
->list
);
635 spin_unlock_bh(&rtwdev
->txq_lock
);