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
);
65 EXPORT_SYMBOL(rtw_tx_fill_tx_desc
);
67 static u8
get_tx_ampdu_factor(struct ieee80211_sta
*sta
)
69 u8 exp
= sta
->ht_cap
.ampdu_factor
;
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
75 return (BIT(2) << exp
) - 1;
78 static u8
get_tx_ampdu_density(struct ieee80211_sta
*sta
)
80 return sta
->ht_cap
.ampdu_density
;
83 static u8
get_highest_ht_tx_rate(struct rtw_dev
*rtwdev
,
84 struct ieee80211_sta
*sta
)
88 if (rtwdev
->hal
.rf_type
== RF_2T2R
&& sta
->ht_cap
.mcs
.rx_mask
[1] != 0)
89 rate
= DESC_RATEMCS15
;
96 static u8
get_highest_vht_tx_rate(struct rtw_dev
*rtwdev
,
97 struct ieee80211_sta
*sta
)
99 struct rtw_efuse
*efuse
= &rtwdev
->efuse
;
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
;
109 case IEEE80211_VHT_MCS_SUPPORT_0_8
:
110 rate
= DESC_RATEVHT1SS_MCS8
;
113 case IEEE80211_VHT_MCS_SUPPORT_0_9
:
114 rate
= DESC_RATEVHT1SS_MCS9
;
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
;
122 case IEEE80211_VHT_MCS_SUPPORT_0_8
:
123 rate
= DESC_RATEVHT2SS_MCS8
;
126 case IEEE80211_VHT_MCS_SUPPORT_0_9
:
127 rate
= DESC_RATEVHT2SS_MCS9
;
131 rate
= DESC_RATEVHT1SS_MCS9
;
137 static void rtw_tx_report_enable(struct rtw_dev
*rtwdev
,
138 struct rtw_tx_pkt_info
*pkt_info
)
140 struct rtw_tx_report
*tx_report
= &rtwdev
->tx_report
;
142 /* [11:8], reserved, fills with zero
143 * [7:2], tx report sequence number
144 * [1:0], firmware use, fills with zero
146 pkt_info
->sn
= (atomic_inc_return(&tx_report
->sn
) << 2) & 0xfc;
147 pkt_info
->report
= true;
150 void rtw_tx_report_purge_timer(struct timer_list
*t
)
152 struct rtw_dev
*rtwdev
= from_timer(rtwdev
, t
, tx_report
.purge_timer
);
153 struct rtw_tx_report
*tx_report
= &rtwdev
->tx_report
;
156 if (skb_queue_len(&tx_report
->queue
) == 0)
159 WARN(1, "purge skb(s) not reported by firmware\n");
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
);
166 void rtw_tx_report_enqueue(struct rtw_dev
*rtwdev
, struct sk_buff
*skb
, u8 sn
)
168 struct rtw_tx_report
*tx_report
= &rtwdev
->tx_report
;
172 /* pass sn to tx report handler through driver data */
173 drv_data
= (u8
*)IEEE80211_SKB_CB(skb
)->status
.status_driver_data
;
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
);
180 mod_timer(&tx_report
->purge_timer
, jiffies
+ RTW_TX_PROBE_TIMEOUT
);
182 EXPORT_SYMBOL(rtw_tx_report_enqueue
);
184 static void rtw_tx_report_tx_status(struct rtw_dev
*rtwdev
,
185 struct sk_buff
*skb
, bool acked
)
187 struct ieee80211_tx_info
*info
;
189 info
= IEEE80211_SKB_CB(skb
);
190 ieee80211_tx_info_clear_status(info
);
192 info
->flags
|= IEEE80211_TX_STAT_ACK
;
194 info
->flags
&= ~IEEE80211_TX_STAT_ACK
;
196 ieee80211_tx_status_irqsafe(rtwdev
->hw
, skb
);
199 void rtw_tx_report_handle(struct rtw_dev
*rtwdev
, struct sk_buff
*skb
)
201 struct rtw_tx_report
*tx_report
= &rtwdev
->tx_report
;
202 struct rtw_c2h_cmd
*c2h
;
203 struct sk_buff
*cur
, *tmp
;
208 c2h
= get_c2h_from_skb(skb
);
210 sn
= GET_CCX_REPORT_SEQNUM(c2h
->payload
);
211 st
= GET_CCX_REPORT_STATUS(c2h
->payload
);
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
;
217 __skb_unlink(cur
, &tx_report
->queue
);
218 rtw_tx_report_tx_status(rtwdev
, cur
, st
== 0);
222 spin_unlock_irqrestore(&tx_report
->q_lock
, flags
);
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
,
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;
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
,
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
;
248 u8 ampdu_density
= 0;
249 bool ampdu_en
= false;
250 u8 rate
= DESC_RATE6M
;
252 u8 bw
= RTW_CHANNEL_WIDTH_20
;
256 seq
= (le16_to_cpu(hdr
->seq_ctrl
) & IEEE80211_SCTL_SEQ
) >> 4;
258 /* for broadcast/multicast, use default values */
262 if (info
->flags
& IEEE80211_TX_CTL_AMPDU
) {
264 ampdu_factor
= get_tx_ampdu_factor(sta
);
265 ampdu_density
= get_tx_ampdu_density(sta
);
268 if (info
->control
.use_rts
)
269 pkt_info
->rts
= true;
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)
280 si
= (struct rtw_sta_info
*)sta
->drv_priv
;
283 rate_id
= si
->rate_id
;
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
;
295 pkt_info
->stbc
= stbc
;
296 pkt_info
->ldpc
= ldpc
;
299 void rtw_tx_pkt_info_update(struct rtw_dev
*rtwdev
,
300 struct rtw_tx_pkt_info
*pkt_info
,
301 struct ieee80211_sta
*sta
,
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
;
314 si
= (struct rtw_sta_info
*)sta
->drv_priv
;
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
);
323 if (info
->control
.hw_key
) {
324 struct ieee80211_key_conf
*key
= info
->control
.hw_key
;
326 switch (key
->cipher
) {
327 case WLAN_CIPHER_SUITE_WEP40
:
328 case WLAN_CIPHER_SUITE_WEP104
:
329 case WLAN_CIPHER_SUITE_TKIP
:
332 case WLAN_CIPHER_SUITE_CCMP
:
340 bmc
= is_broadcast_ether_addr(hdr
->addr1
) ||
341 is_multicast_ether_addr(hdr
->addr1
);
343 if (info
->flags
& IEEE80211_TX_CTL_REQ_TX_STATUS
)
344 rtw_tx_report_enable(rtwdev
, pkt_info
);
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
;
353 /* maybe merge with tx status ? */
354 rtw_tx_stats(rtwdev
, vif
, skb
);
357 void rtw_rsvd_page_pkt_info_update(struct rtw_dev
*rtwdev
,
358 struct rtw_tx_pkt_info
*pkt_info
,
361 struct rtw_chip_info
*chip
= rtwdev
->chip
;
362 struct ieee80211_hdr
*hdr
= (struct ieee80211_hdr
*)skb
->data
;
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;
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
;
378 rtw_tx_write_data_rsvd_page_get(struct rtw_dev
*rtwdev
,
379 struct rtw_tx_pkt_info
*pkt_info
,
382 struct rtw_chip_info
*chip
= rtwdev
->chip
;
387 tx_pkt_desc_sz
= chip
->tx_pkt_desc_sz
;
388 length
= size
+ tx_pkt_desc_sz
;
389 skb
= dev_alloc_skb(length
);
391 rtw_err(rtwdev
, "failed to alloc write data rsvd page skb\n");
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
;
402 EXPORT_SYMBOL(rtw_tx_write_data_rsvd_page_get
);
405 rtw_tx_write_data_h2c_get(struct rtw_dev
*rtwdev
,
406 struct rtw_tx_pkt_info
*pkt_info
,
409 struct rtw_chip_info
*chip
= rtwdev
->chip
;
414 tx_pkt_desc_sz
= chip
->tx_pkt_desc_sz
;
415 length
= size
+ tx_pkt_desc_sz
;
416 skb
= dev_alloc_skb(length
);
418 rtw_err(rtwdev
, "failed to alloc write data h2c skb\n");
422 skb_reserve(skb
, tx_pkt_desc_sz
);
423 skb_put_data(skb
, buf
, size
);
424 pkt_info
->tx_pkt_size
= size
;
428 EXPORT_SYMBOL(rtw_tx_write_data_h2c_get
);
430 void rtw_tx(struct rtw_dev
*rtwdev
,
431 struct ieee80211_tx_control
*control
,
434 struct rtw_tx_pkt_info pkt_info
= {0};
437 rtw_tx_pkt_info_update(rtwdev
, &pkt_info
, control
->sta
, skb
);
438 ret
= rtw_hci_tx_write(rtwdev
, &pkt_info
, skb
);
440 rtw_err(rtwdev
, "failed to write TX skb to HCI\n");
444 rtw_hci_tx_kick_off(rtwdev
);
449 ieee80211_free_txskb(rtwdev
->hw
, skb
);
452 static void rtw_txq_check_agg(struct rtw_dev
*rtwdev
,
453 struct rtw_txq
*rtwtxq
,
456 struct ieee80211_txq
*txq
= rtwtxq_to_txq(rtwtxq
);
457 struct ieee80211_tx_info
*info
;
458 struct rtw_sta_info
*si
;
460 if (test_bit(RTW_TXQ_AMPDU
, &rtwtxq
->flags
)) {
461 info
= IEEE80211_SKB_CB(skb
);
462 info
->flags
|= IEEE80211_TX_CTL_AMPDU
;
466 if (skb_get_queue_mapping(skb
) == IEEE80211_AC_VO
)
469 if (test_bit(RTW_TXQ_BLOCK_BA
, &rtwtxq
->flags
))
472 if (unlikely(skb
->protocol
== cpu_to_be16(ETH_P_PAE
)))
478 si
= (struct rtw_sta_info
*)txq
->sta
->drv_priv
;
479 set_bit(txq
->tid
, si
->tid_ba
);
481 ieee80211_queue_work(rtwdev
->hw
, &rtwdev
->ba_work
);
484 static int rtw_txq_push_skb(struct rtw_dev
*rtwdev
,
485 struct rtw_txq
*rtwtxq
,
488 struct ieee80211_txq
*txq
= rtwtxq_to_txq(rtwtxq
);
489 struct rtw_tx_pkt_info pkt_info
= {0};
492 rtw_txq_check_agg(rtwdev
, rtwtxq
, skb
);
494 rtw_tx_pkt_info_update(rtwdev
, &pkt_info
, txq
->sta
, skb
);
495 ret
= rtw_hci_tx_write(rtwdev
, &pkt_info
, skb
);
497 rtw_err(rtwdev
, "failed to write TX skb to HCI\n");
500 rtwtxq
->last_push
= jiffies
;
505 static struct sk_buff
*rtw_txq_dequeue(struct rtw_dev
*rtwdev
,
506 struct rtw_txq
*rtwtxq
)
508 struct ieee80211_txq
*txq
= rtwtxq_to_txq(rtwtxq
);
511 skb
= ieee80211_tx_dequeue(rtwdev
->hw
, txq
);
518 static void rtw_txq_push(struct rtw_dev
*rtwdev
,
519 struct rtw_txq
*rtwtxq
,
520 unsigned long frames
)
528 for (i
= 0; i
< frames
; i
++) {
529 skb
= rtw_txq_dequeue(rtwdev
, rtwtxq
);
533 ret
= rtw_txq_push_skb(rtwdev
, rtwtxq
, skb
);
535 rtw_err(rtwdev
, "failed to pusk skb, ret %d\n", ret
);
543 void rtw_tx_tasklet(unsigned long data
)
545 struct rtw_dev
*rtwdev
= (void *)data
;
546 struct rtw_txq
*rtwtxq
, *tmp
;
548 spin_lock_bh(&rtwdev
->txq_lock
);
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
;
555 ieee80211_txq_get_depth(txq
, &frame_cnt
, &byte_cnt
);
556 rtw_txq_push(rtwdev
, rtwtxq
, frame_cnt
);
558 list_del_init(&rtwtxq
->list
);
561 rtw_hci_tx_kick_off(rtwdev
);
563 spin_unlock_bh(&rtwdev
->txq_lock
);
566 void rtw_txq_init(struct rtw_dev
*rtwdev
, struct ieee80211_txq
*txq
)
568 struct rtw_txq
*rtwtxq
;
573 rtwtxq
= (struct rtw_txq
*)txq
->drv_priv
;
574 INIT_LIST_HEAD(&rtwtxq
->list
);
577 void rtw_txq_cleanup(struct rtw_dev
*rtwdev
, struct ieee80211_txq
*txq
)
579 struct rtw_txq
*rtwtxq
;
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
);