xref: /linux/drivers/net/wireless/realtek/rtw88/tx.c (revision 902cb7b1)
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
rtw_tx_stats(struct rtw_dev * rtwdev,struct ieee80211_vif * vif,struct sk_buff * skb)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 
rtw_tx_fill_tx_desc(struct rtw_tx_pkt_info * pkt_info,struct sk_buff * skb)35 void rtw_tx_fill_tx_desc(struct rtw_tx_pkt_info *pkt_info, struct sk_buff *skb)
36 {
37 	struct rtw_tx_desc *tx_desc = (struct rtw_tx_desc *)skb->data;
38 	bool more_data = false;
39 
40 	if (pkt_info->qsel == TX_DESC_QSEL_HIGH)
41 		more_data = true;
42 
43 	tx_desc->w0 = le32_encode_bits(pkt_info->tx_pkt_size, RTW_TX_DESC_W0_TXPKTSIZE) |
44 		      le32_encode_bits(pkt_info->offset, RTW_TX_DESC_W0_OFFSET) |
45 		      le32_encode_bits(pkt_info->bmc, RTW_TX_DESC_W0_BMC) |
46 		      le32_encode_bits(pkt_info->ls, RTW_TX_DESC_W0_LS) |
47 		      le32_encode_bits(pkt_info->dis_qselseq, RTW_TX_DESC_W0_DISQSELSEQ);
48 
49 	tx_desc->w1 = le32_encode_bits(pkt_info->mac_id, RTW_TX_DESC_W1_MACID) |
50 		      le32_encode_bits(pkt_info->qsel, RTW_TX_DESC_W1_QSEL) |
51 		      le32_encode_bits(pkt_info->rate_id, RTW_TX_DESC_W1_RATE_ID) |
52 		      le32_encode_bits(pkt_info->sec_type, RTW_TX_DESC_W1_SEC_TYPE) |
53 		      le32_encode_bits(pkt_info->pkt_offset, RTW_TX_DESC_W1_PKT_OFFSET) |
54 		      le32_encode_bits(more_data, RTW_TX_DESC_W1_MORE_DATA);
55 
56 	tx_desc->w2 = le32_encode_bits(pkt_info->ampdu_en, RTW_TX_DESC_W2_AGG_EN) |
57 		      le32_encode_bits(pkt_info->report, RTW_TX_DESC_W2_SPE_RPT) |
58 		      le32_encode_bits(pkt_info->ampdu_density, RTW_TX_DESC_W2_AMPDU_DEN) |
59 		      le32_encode_bits(pkt_info->bt_null, RTW_TX_DESC_W2_BT_NULL);
60 
61 	tx_desc->w3 = le32_encode_bits(pkt_info->hw_ssn_sel, RTW_TX_DESC_W3_HW_SSN_SEL) |
62 		      le32_encode_bits(pkt_info->use_rate, RTW_TX_DESC_W3_USE_RATE) |
63 		      le32_encode_bits(pkt_info->dis_rate_fallback, RTW_TX_DESC_W3_DISDATAFB) |
64 		      le32_encode_bits(pkt_info->rts, RTW_TX_DESC_W3_USE_RTS) |
65 		      le32_encode_bits(pkt_info->nav_use_hdr, RTW_TX_DESC_W3_NAVUSEHDR) |
66 		      le32_encode_bits(pkt_info->ampdu_factor, RTW_TX_DESC_W3_MAX_AGG_NUM);
67 
68 	tx_desc->w4 = le32_encode_bits(pkt_info->rate, RTW_TX_DESC_W4_DATARATE);
69 
70 	tx_desc->w5 = le32_encode_bits(pkt_info->short_gi, RTW_TX_DESC_W5_DATA_SHORT) |
71 		      le32_encode_bits(pkt_info->bw, RTW_TX_DESC_W5_DATA_BW) |
72 		      le32_encode_bits(pkt_info->ldpc, RTW_TX_DESC_W5_DATA_LDPC) |
73 		      le32_encode_bits(pkt_info->stbc, RTW_TX_DESC_W5_DATA_STBC);
74 
75 	tx_desc->w6 = le32_encode_bits(pkt_info->sn, RTW_TX_DESC_W6_SW_DEFINE);
76 
77 	tx_desc->w8 = le32_encode_bits(pkt_info->en_hwseq, RTW_TX_DESC_W8_EN_HWSEQ);
78 
79 	tx_desc->w9 = le32_encode_bits(pkt_info->seq, RTW_TX_DESC_W9_SW_SEQ);
80 
81 	if (pkt_info->rts) {
82 		tx_desc->w4 |= le32_encode_bits(DESC_RATE24M, RTW_TX_DESC_W4_RTSRATE);
83 		tx_desc->w5 |= le32_encode_bits(1, RTW_TX_DESC_W5_DATA_RTS_SHORT);
84 	}
85 
86 	if (pkt_info->tim_offset)
87 		tx_desc->w9 |= le32_encode_bits(1, RTW_TX_DESC_W9_TIM_EN) |
88 			       le32_encode_bits(pkt_info->tim_offset, RTW_TX_DESC_W9_TIM_OFFSET);
89 }
90 EXPORT_SYMBOL(rtw_tx_fill_tx_desc);
91 
get_tx_ampdu_factor(struct ieee80211_sta * sta)92 static u8 get_tx_ampdu_factor(struct ieee80211_sta *sta)
93 {
94 	u8 exp = sta->deflink.ht_cap.ampdu_factor;
95 
96 	/* the least ampdu factor is 8K, and the value in the tx desc is the
97 	 * max aggregation num, which represents val * 2 packets can be
98 	 * aggregated in an AMPDU, so here we should use 8/2=4 as the base
99 	 */
100 	return (BIT(2) << exp) - 1;
101 }
102 
get_tx_ampdu_density(struct ieee80211_sta * sta)103 static u8 get_tx_ampdu_density(struct ieee80211_sta *sta)
104 {
105 	return sta->deflink.ht_cap.ampdu_density;
106 }
107 
get_highest_ht_tx_rate(struct rtw_dev * rtwdev,struct ieee80211_sta * sta)108 static u8 get_highest_ht_tx_rate(struct rtw_dev *rtwdev,
109 				 struct ieee80211_sta *sta)
110 {
111 	u8 rate;
112 
113 	if (rtwdev->hal.rf_type == RF_2T2R && sta->deflink.ht_cap.mcs.rx_mask[1] != 0)
114 		rate = DESC_RATEMCS15;
115 	else
116 		rate = DESC_RATEMCS7;
117 
118 	return rate;
119 }
120 
get_highest_vht_tx_rate(struct rtw_dev * rtwdev,struct ieee80211_sta * sta)121 static u8 get_highest_vht_tx_rate(struct rtw_dev *rtwdev,
122 				  struct ieee80211_sta *sta)
123 {
124 	struct rtw_efuse *efuse = &rtwdev->efuse;
125 	u8 rate;
126 	u16 tx_mcs_map;
127 
128 	tx_mcs_map = le16_to_cpu(sta->deflink.vht_cap.vht_mcs.tx_mcs_map);
129 	if (efuse->hw_cap.nss == 1) {
130 		switch (tx_mcs_map & 0x3) {
131 		case IEEE80211_VHT_MCS_SUPPORT_0_7:
132 			rate = DESC_RATEVHT1SS_MCS7;
133 			break;
134 		case IEEE80211_VHT_MCS_SUPPORT_0_8:
135 			rate = DESC_RATEVHT1SS_MCS8;
136 			break;
137 		default:
138 		case IEEE80211_VHT_MCS_SUPPORT_0_9:
139 			rate = DESC_RATEVHT1SS_MCS9;
140 			break;
141 		}
142 	} else if (efuse->hw_cap.nss >= 2) {
143 		switch ((tx_mcs_map & 0xc) >> 2) {
144 		case IEEE80211_VHT_MCS_SUPPORT_0_7:
145 			rate = DESC_RATEVHT2SS_MCS7;
146 			break;
147 		case IEEE80211_VHT_MCS_SUPPORT_0_8:
148 			rate = DESC_RATEVHT2SS_MCS8;
149 			break;
150 		default:
151 		case IEEE80211_VHT_MCS_SUPPORT_0_9:
152 			rate = DESC_RATEVHT2SS_MCS9;
153 			break;
154 		}
155 	} else {
156 		rate = DESC_RATEVHT1SS_MCS9;
157 	}
158 
159 	return rate;
160 }
161 
rtw_tx_report_enable(struct rtw_dev * rtwdev,struct rtw_tx_pkt_info * pkt_info)162 static void rtw_tx_report_enable(struct rtw_dev *rtwdev,
163 				 struct rtw_tx_pkt_info *pkt_info)
164 {
165 	struct rtw_tx_report *tx_report = &rtwdev->tx_report;
166 
167 	/* [11:8], reserved, fills with zero
168 	 * [7:2],  tx report sequence number
169 	 * [1:0],  firmware use, fills with zero
170 	 */
171 	pkt_info->sn = (atomic_inc_return(&tx_report->sn) << 2) & 0xfc;
172 	pkt_info->report = true;
173 }
174 
rtw_tx_report_purge_timer(struct timer_list * t)175 void rtw_tx_report_purge_timer(struct timer_list *t)
176 {
177 	struct rtw_dev *rtwdev = from_timer(rtwdev, t, tx_report.purge_timer);
178 	struct rtw_tx_report *tx_report = &rtwdev->tx_report;
179 	unsigned long flags;
180 
181 	if (skb_queue_len(&tx_report->queue) == 0)
182 		return;
183 
184 	rtw_warn(rtwdev, "failed to get tx report from firmware\n");
185 
186 	spin_lock_irqsave(&tx_report->q_lock, flags);
187 	skb_queue_purge(&tx_report->queue);
188 	spin_unlock_irqrestore(&tx_report->q_lock, flags);
189 }
190 
rtw_tx_report_enqueue(struct rtw_dev * rtwdev,struct sk_buff * skb,u8 sn)191 void rtw_tx_report_enqueue(struct rtw_dev *rtwdev, struct sk_buff *skb, u8 sn)
192 {
193 	struct rtw_tx_report *tx_report = &rtwdev->tx_report;
194 	unsigned long flags;
195 	u8 *drv_data;
196 
197 	/* pass sn to tx report handler through driver data */
198 	drv_data = (u8 *)IEEE80211_SKB_CB(skb)->status.status_driver_data;
199 	*drv_data = sn;
200 
201 	spin_lock_irqsave(&tx_report->q_lock, flags);
202 	__skb_queue_tail(&tx_report->queue, skb);
203 	spin_unlock_irqrestore(&tx_report->q_lock, flags);
204 
205 	mod_timer(&tx_report->purge_timer, jiffies + RTW_TX_PROBE_TIMEOUT);
206 }
207 EXPORT_SYMBOL(rtw_tx_report_enqueue);
208 
rtw_tx_report_tx_status(struct rtw_dev * rtwdev,struct sk_buff * skb,bool acked)209 static void rtw_tx_report_tx_status(struct rtw_dev *rtwdev,
210 				    struct sk_buff *skb, bool acked)
211 {
212 	struct ieee80211_tx_info *info;
213 
214 	info = IEEE80211_SKB_CB(skb);
215 	ieee80211_tx_info_clear_status(info);
216 	if (acked)
217 		info->flags |= IEEE80211_TX_STAT_ACK;
218 	else
219 		info->flags &= ~IEEE80211_TX_STAT_ACK;
220 
221 	ieee80211_tx_status_irqsafe(rtwdev->hw, skb);
222 }
223 
rtw_tx_report_handle(struct rtw_dev * rtwdev,struct sk_buff * skb,int src)224 void rtw_tx_report_handle(struct rtw_dev *rtwdev, struct sk_buff *skb, int src)
225 {
226 	struct rtw_tx_report *tx_report = &rtwdev->tx_report;
227 	struct rtw_c2h_cmd *c2h;
228 	struct sk_buff *cur, *tmp;
229 	unsigned long flags;
230 	u8 sn, st;
231 	u8 *n;
232 
233 	c2h = get_c2h_from_skb(skb);
234 
235 	if (src == C2H_CCX_TX_RPT) {
236 		sn = GET_CCX_REPORT_SEQNUM_V0(c2h->payload);
237 		st = GET_CCX_REPORT_STATUS_V0(c2h->payload);
238 	} else {
239 		sn = GET_CCX_REPORT_SEQNUM_V1(c2h->payload);
240 		st = GET_CCX_REPORT_STATUS_V1(c2h->payload);
241 	}
242 
243 	spin_lock_irqsave(&tx_report->q_lock, flags);
244 	skb_queue_walk_safe(&tx_report->queue, cur, tmp) {
245 		n = (u8 *)IEEE80211_SKB_CB(cur)->status.status_driver_data;
246 		if (*n == sn) {
247 			__skb_unlink(cur, &tx_report->queue);
248 			rtw_tx_report_tx_status(rtwdev, cur, st == 0);
249 			break;
250 		}
251 	}
252 	spin_unlock_irqrestore(&tx_report->q_lock, flags);
253 }
254 
rtw_get_mgmt_rate(struct rtw_dev * rtwdev,struct sk_buff * skb,u8 lowest_rate,bool ignore_rate)255 static u8 rtw_get_mgmt_rate(struct rtw_dev *rtwdev, struct sk_buff *skb,
256 			    u8 lowest_rate, bool ignore_rate)
257 {
258 	struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
259 	struct ieee80211_vif *vif = tx_info->control.vif;
260 	bool force_lowest = test_bit(RTW_FLAG_FORCE_LOWEST_RATE, rtwdev->flags);
261 
262 	if (!vif || !vif->bss_conf.basic_rates || ignore_rate || force_lowest)
263 		return lowest_rate;
264 
265 	return __ffs(vif->bss_conf.basic_rates) + lowest_rate;
266 }
267 
rtw_tx_pkt_info_update_rate(struct rtw_dev * rtwdev,struct rtw_tx_pkt_info * pkt_info,struct sk_buff * skb,bool ignore_rate)268 static void rtw_tx_pkt_info_update_rate(struct rtw_dev *rtwdev,
269 					struct rtw_tx_pkt_info *pkt_info,
270 					struct sk_buff *skb,
271 					bool ignore_rate)
272 {
273 	if (rtwdev->hal.current_band_type == RTW_BAND_2G) {
274 		pkt_info->rate_id = RTW_RATEID_B_20M;
275 		pkt_info->rate = rtw_get_mgmt_rate(rtwdev, skb, DESC_RATE1M,
276 						   ignore_rate);
277 	} else {
278 		pkt_info->rate_id = RTW_RATEID_G;
279 		pkt_info->rate = rtw_get_mgmt_rate(rtwdev, skb, DESC_RATE6M,
280 						   ignore_rate);
281 	}
282 
283 	pkt_info->use_rate = true;
284 	pkt_info->dis_rate_fallback = true;
285 }
286 
rtw_tx_pkt_info_update_sec(struct rtw_dev * rtwdev,struct rtw_tx_pkt_info * pkt_info,struct sk_buff * skb)287 static void rtw_tx_pkt_info_update_sec(struct rtw_dev *rtwdev,
288 				       struct rtw_tx_pkt_info *pkt_info,
289 				       struct sk_buff *skb)
290 {
291 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
292 	u8 sec_type = 0;
293 
294 	if (info && info->control.hw_key) {
295 		struct ieee80211_key_conf *key = info->control.hw_key;
296 
297 		switch (key->cipher) {
298 		case WLAN_CIPHER_SUITE_WEP40:
299 		case WLAN_CIPHER_SUITE_WEP104:
300 		case WLAN_CIPHER_SUITE_TKIP:
301 			sec_type = 0x01;
302 			break;
303 		case WLAN_CIPHER_SUITE_CCMP:
304 			sec_type = 0x03;
305 			break;
306 		default:
307 			break;
308 		}
309 	}
310 
311 	pkt_info->sec_type = sec_type;
312 }
313 
rtw_tx_mgmt_pkt_info_update(struct rtw_dev * rtwdev,struct rtw_tx_pkt_info * pkt_info,struct ieee80211_sta * sta,struct sk_buff * skb)314 static void rtw_tx_mgmt_pkt_info_update(struct rtw_dev *rtwdev,
315 					struct rtw_tx_pkt_info *pkt_info,
316 					struct ieee80211_sta *sta,
317 					struct sk_buff *skb)
318 {
319 	rtw_tx_pkt_info_update_rate(rtwdev, pkt_info, skb, false);
320 	pkt_info->dis_qselseq = true;
321 	pkt_info->en_hwseq = true;
322 	pkt_info->hw_ssn_sel = 0;
323 	/* TODO: need to change hw port and hw ssn sel for multiple vifs */
324 }
325 
rtw_tx_data_pkt_info_update(struct rtw_dev * rtwdev,struct rtw_tx_pkt_info * pkt_info,struct ieee80211_sta * sta,struct sk_buff * skb)326 static void rtw_tx_data_pkt_info_update(struct rtw_dev *rtwdev,
327 					struct rtw_tx_pkt_info *pkt_info,
328 					struct ieee80211_sta *sta,
329 					struct sk_buff *skb)
330 {
331 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
332 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
333 	struct ieee80211_hw *hw = rtwdev->hw;
334 	struct rtw_dm_info *dm_info = &rtwdev->dm_info;
335 	struct rtw_sta_info *si;
336 	u8 fix_rate;
337 	u16 seq;
338 	u8 ampdu_factor = 0;
339 	u8 ampdu_density = 0;
340 	bool ampdu_en = false;
341 	u8 rate = DESC_RATE6M;
342 	u8 rate_id = 6;
343 	u8 bw = RTW_CHANNEL_WIDTH_20;
344 	bool stbc = false;
345 	bool ldpc = false;
346 
347 	seq = (le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_SEQ) >> 4;
348 
349 	/* for broadcast/multicast, use default values */
350 	if (!sta)
351 		goto out;
352 
353 	if (info->flags & IEEE80211_TX_CTL_AMPDU) {
354 		ampdu_en = true;
355 		ampdu_factor = get_tx_ampdu_factor(sta);
356 		ampdu_density = get_tx_ampdu_density(sta);
357 	}
358 
359 	if (info->control.use_rts || skb->len > hw->wiphy->rts_threshold)
360 		pkt_info->rts = true;
361 
362 	if (sta->deflink.vht_cap.vht_supported)
363 		rate = get_highest_vht_tx_rate(rtwdev, sta);
364 	else if (sta->deflink.ht_cap.ht_supported)
365 		rate = get_highest_ht_tx_rate(rtwdev, sta);
366 	else if (sta->deflink.supp_rates[0] <= 0xf)
367 		rate = DESC_RATE11M;
368 	else
369 		rate = DESC_RATE54M;
370 
371 	si = (struct rtw_sta_info *)sta->drv_priv;
372 
373 	bw = si->bw_mode;
374 	rate_id = si->rate_id;
375 	stbc = rtwdev->hal.txrx_1ss ? false : si->stbc_en;
376 	ldpc = si->ldpc_en;
377 
378 out:
379 	pkt_info->seq = seq;
380 	pkt_info->ampdu_factor = ampdu_factor;
381 	pkt_info->ampdu_density = ampdu_density;
382 	pkt_info->ampdu_en = ampdu_en;
383 	pkt_info->rate = rate;
384 	pkt_info->rate_id = rate_id;
385 	pkt_info->bw = bw;
386 	pkt_info->stbc = stbc;
387 	pkt_info->ldpc = ldpc;
388 
389 	fix_rate = dm_info->fix_rate;
390 	if (fix_rate < DESC_RATE_MAX) {
391 		pkt_info->rate = fix_rate;
392 		pkt_info->dis_rate_fallback = true;
393 		pkt_info->use_rate = true;
394 	}
395 }
396 
rtw_tx_pkt_info_update(struct rtw_dev * rtwdev,struct rtw_tx_pkt_info * pkt_info,struct ieee80211_sta * sta,struct sk_buff * skb)397 void rtw_tx_pkt_info_update(struct rtw_dev *rtwdev,
398 			    struct rtw_tx_pkt_info *pkt_info,
399 			    struct ieee80211_sta *sta,
400 			    struct sk_buff *skb)
401 {
402 	const struct rtw_chip_info *chip = rtwdev->chip;
403 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
404 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
405 	struct ieee80211_vif *vif = info->control.vif;
406 	struct rtw_sta_info *si;
407 	struct rtw_vif *rtwvif;
408 	__le16 fc = hdr->frame_control;
409 	bool bmc;
410 
411 	if (sta) {
412 		si = (struct rtw_sta_info *)sta->drv_priv;
413 		pkt_info->mac_id = si->mac_id;
414 	} else if (vif) {
415 		rtwvif = (struct rtw_vif *)vif->drv_priv;
416 		pkt_info->mac_id = rtwvif->mac_id;
417 	}
418 
419 	if (ieee80211_is_mgmt(fc) || ieee80211_is_nullfunc(fc))
420 		rtw_tx_mgmt_pkt_info_update(rtwdev, pkt_info, sta, skb);
421 	else if (ieee80211_is_data(fc))
422 		rtw_tx_data_pkt_info_update(rtwdev, pkt_info, sta, skb);
423 
424 	bmc = is_broadcast_ether_addr(hdr->addr1) ||
425 	      is_multicast_ether_addr(hdr->addr1);
426 
427 	if (info->flags & IEEE80211_TX_CTL_REQ_TX_STATUS)
428 		rtw_tx_report_enable(rtwdev, pkt_info);
429 
430 	pkt_info->bmc = bmc;
431 	rtw_tx_pkt_info_update_sec(rtwdev, pkt_info, skb);
432 	pkt_info->tx_pkt_size = skb->len;
433 	pkt_info->offset = chip->tx_pkt_desc_sz;
434 	pkt_info->qsel = skb->priority;
435 	pkt_info->ls = true;
436 
437 	/* maybe merge with tx status ? */
438 	rtw_tx_stats(rtwdev, vif, skb);
439 }
440 
rtw_tx_rsvd_page_pkt_info_update(struct rtw_dev * rtwdev,struct rtw_tx_pkt_info * pkt_info,struct sk_buff * skb,enum rtw_rsvd_packet_type type)441 void rtw_tx_rsvd_page_pkt_info_update(struct rtw_dev *rtwdev,
442 				      struct rtw_tx_pkt_info *pkt_info,
443 				      struct sk_buff *skb,
444 				      enum rtw_rsvd_packet_type type)
445 {
446 	const struct rtw_chip_info *chip = rtwdev->chip;
447 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
448 	bool bmc;
449 
450 	/* A beacon or dummy reserved page packet indicates that it is the first
451 	 * reserved page, and the qsel of it will be set in each hci.
452 	 */
453 	if (type != RSVD_BEACON && type != RSVD_DUMMY)
454 		pkt_info->qsel = TX_DESC_QSEL_MGMT;
455 
456 	rtw_tx_pkt_info_update_rate(rtwdev, pkt_info, skb, true);
457 
458 	bmc = is_broadcast_ether_addr(hdr->addr1) ||
459 	      is_multicast_ether_addr(hdr->addr1);
460 	pkt_info->bmc = bmc;
461 	pkt_info->tx_pkt_size = skb->len;
462 	pkt_info->offset = chip->tx_pkt_desc_sz;
463 	pkt_info->ls = true;
464 	if (type == RSVD_PS_POLL) {
465 		pkt_info->nav_use_hdr = true;
466 	} else {
467 		pkt_info->dis_qselseq = true;
468 		pkt_info->en_hwseq = true;
469 		pkt_info->hw_ssn_sel = 0;
470 	}
471 	if (type == RSVD_QOS_NULL)
472 		pkt_info->bt_null = true;
473 
474 	if (type == RSVD_BEACON) {
475 		struct rtw_rsvd_page *rsvd_pkt;
476 		int hdr_len;
477 
478 		rsvd_pkt = list_first_entry_or_null(&rtwdev->rsvd_page_list,
479 						    struct rtw_rsvd_page,
480 						    build_list);
481 		if (rsvd_pkt && rsvd_pkt->tim_offset != 0) {
482 			hdr_len = sizeof(struct ieee80211_hdr_3addr);
483 			pkt_info->tim_offset = rsvd_pkt->tim_offset - hdr_len;
484 		}
485 	}
486 
487 	rtw_tx_pkt_info_update_sec(rtwdev, pkt_info, skb);
488 
489 	/* TODO: need to change hw port and hw ssn sel for multiple vifs */
490 }
491 
492 struct sk_buff *
rtw_tx_write_data_rsvd_page_get(struct rtw_dev * rtwdev,struct rtw_tx_pkt_info * pkt_info,u8 * buf,u32 size)493 rtw_tx_write_data_rsvd_page_get(struct rtw_dev *rtwdev,
494 				struct rtw_tx_pkt_info *pkt_info,
495 				u8 *buf, u32 size)
496 {
497 	const struct rtw_chip_info *chip = rtwdev->chip;
498 	struct sk_buff *skb;
499 	u32 tx_pkt_desc_sz;
500 	u32 length;
501 
502 	tx_pkt_desc_sz = chip->tx_pkt_desc_sz;
503 	length = size + tx_pkt_desc_sz;
504 	skb = dev_alloc_skb(length);
505 	if (!skb) {
506 		rtw_err(rtwdev, "failed to alloc write data rsvd page skb\n");
507 		return NULL;
508 	}
509 
510 	skb_reserve(skb, tx_pkt_desc_sz);
511 	skb_put_data(skb, buf, size);
512 	rtw_tx_rsvd_page_pkt_info_update(rtwdev, pkt_info, skb, RSVD_BEACON);
513 
514 	return skb;
515 }
516 EXPORT_SYMBOL(rtw_tx_write_data_rsvd_page_get);
517 
518 struct sk_buff *
rtw_tx_write_data_h2c_get(struct rtw_dev * rtwdev,struct rtw_tx_pkt_info * pkt_info,u8 * buf,u32 size)519 rtw_tx_write_data_h2c_get(struct rtw_dev *rtwdev,
520 			  struct rtw_tx_pkt_info *pkt_info,
521 			  u8 *buf, u32 size)
522 {
523 	const struct rtw_chip_info *chip = rtwdev->chip;
524 	struct sk_buff *skb;
525 	u32 tx_pkt_desc_sz;
526 	u32 length;
527 
528 	tx_pkt_desc_sz = chip->tx_pkt_desc_sz;
529 	length = size + tx_pkt_desc_sz;
530 	skb = dev_alloc_skb(length);
531 	if (!skb) {
532 		rtw_err(rtwdev, "failed to alloc write data h2c skb\n");
533 		return NULL;
534 	}
535 
536 	skb_reserve(skb, tx_pkt_desc_sz);
537 	skb_put_data(skb, buf, size);
538 	pkt_info->tx_pkt_size = size;
539 
540 	return skb;
541 }
542 EXPORT_SYMBOL(rtw_tx_write_data_h2c_get);
543 
rtw_tx(struct rtw_dev * rtwdev,struct ieee80211_tx_control * control,struct sk_buff * skb)544 void rtw_tx(struct rtw_dev *rtwdev,
545 	    struct ieee80211_tx_control *control,
546 	    struct sk_buff *skb)
547 {
548 	struct rtw_tx_pkt_info pkt_info = {0};
549 	int ret;
550 
551 	rtw_tx_pkt_info_update(rtwdev, &pkt_info, control->sta, skb);
552 	ret = rtw_hci_tx_write(rtwdev, &pkt_info, skb);
553 	if (ret) {
554 		rtw_err(rtwdev, "failed to write TX skb to HCI\n");
555 		goto out;
556 	}
557 
558 	rtw_hci_tx_kick_off(rtwdev);
559 
560 	return;
561 
562 out:
563 	ieee80211_free_txskb(rtwdev->hw, skb);
564 }
565 
rtw_txq_check_agg(struct rtw_dev * rtwdev,struct rtw_txq * rtwtxq,struct sk_buff * skb)566 static void rtw_txq_check_agg(struct rtw_dev *rtwdev,
567 			      struct rtw_txq *rtwtxq,
568 			      struct sk_buff *skb)
569 {
570 	struct ieee80211_txq *txq = rtwtxq_to_txq(rtwtxq);
571 	struct ieee80211_tx_info *info;
572 	struct rtw_sta_info *si;
573 
574 	if (test_bit(RTW_TXQ_AMPDU, &rtwtxq->flags)) {
575 		info = IEEE80211_SKB_CB(skb);
576 		info->flags |= IEEE80211_TX_CTL_AMPDU;
577 		return;
578 	}
579 
580 	if (skb_get_queue_mapping(skb) == IEEE80211_AC_VO)
581 		return;
582 
583 	if (test_bit(RTW_TXQ_BLOCK_BA, &rtwtxq->flags))
584 		return;
585 
586 	if (unlikely(skb->protocol == cpu_to_be16(ETH_P_PAE)))
587 		return;
588 
589 	if (!txq->sta)
590 		return;
591 
592 	si = (struct rtw_sta_info *)txq->sta->drv_priv;
593 	set_bit(txq->tid, si->tid_ba);
594 
595 	ieee80211_queue_work(rtwdev->hw, &rtwdev->ba_work);
596 }
597 
rtw_txq_push_skb(struct rtw_dev * rtwdev,struct rtw_txq * rtwtxq,struct sk_buff * skb)598 static int rtw_txq_push_skb(struct rtw_dev *rtwdev,
599 			    struct rtw_txq *rtwtxq,
600 			    struct sk_buff *skb)
601 {
602 	struct ieee80211_txq *txq = rtwtxq_to_txq(rtwtxq);
603 	struct rtw_tx_pkt_info pkt_info = {0};
604 	int ret;
605 
606 	rtw_txq_check_agg(rtwdev, rtwtxq, skb);
607 
608 	rtw_tx_pkt_info_update(rtwdev, &pkt_info, txq->sta, skb);
609 	ret = rtw_hci_tx_write(rtwdev, &pkt_info, skb);
610 	if (ret) {
611 		rtw_err(rtwdev, "failed to write TX skb to HCI\n");
612 		return ret;
613 	}
614 	return 0;
615 }
616 
rtw_txq_dequeue(struct rtw_dev * rtwdev,struct rtw_txq * rtwtxq)617 static struct sk_buff *rtw_txq_dequeue(struct rtw_dev *rtwdev,
618 				       struct rtw_txq *rtwtxq)
619 {
620 	struct ieee80211_txq *txq = rtwtxq_to_txq(rtwtxq);
621 	struct sk_buff *skb;
622 
623 	skb = ieee80211_tx_dequeue(rtwdev->hw, txq);
624 	if (!skb)
625 		return NULL;
626 
627 	return skb;
628 }
629 
rtw_txq_push(struct rtw_dev * rtwdev,struct rtw_txq * rtwtxq,unsigned long frames)630 static void rtw_txq_push(struct rtw_dev *rtwdev,
631 			 struct rtw_txq *rtwtxq,
632 			 unsigned long frames)
633 {
634 	struct sk_buff *skb;
635 	int ret;
636 	int i;
637 
638 	rcu_read_lock();
639 
640 	for (i = 0; i < frames; i++) {
641 		skb = rtw_txq_dequeue(rtwdev, rtwtxq);
642 		if (!skb)
643 			break;
644 
645 		ret = rtw_txq_push_skb(rtwdev, rtwtxq, skb);
646 		if (ret) {
647 			rtw_err(rtwdev, "failed to pusk skb, ret %d\n", ret);
648 			break;
649 		}
650 	}
651 
652 	rcu_read_unlock();
653 }
654 
__rtw_tx_work(struct rtw_dev * rtwdev)655 void __rtw_tx_work(struct rtw_dev *rtwdev)
656 {
657 	struct rtw_txq *rtwtxq, *tmp;
658 
659 	spin_lock_bh(&rtwdev->txq_lock);
660 
661 	list_for_each_entry_safe(rtwtxq, tmp, &rtwdev->txqs, list) {
662 		struct ieee80211_txq *txq = rtwtxq_to_txq(rtwtxq);
663 		unsigned long frame_cnt;
664 
665 		ieee80211_txq_get_depth(txq, &frame_cnt, NULL);
666 		rtw_txq_push(rtwdev, rtwtxq, frame_cnt);
667 
668 		list_del_init(&rtwtxq->list);
669 	}
670 
671 	rtw_hci_tx_kick_off(rtwdev);
672 
673 	spin_unlock_bh(&rtwdev->txq_lock);
674 }
675 
rtw_tx_work(struct work_struct * w)676 void rtw_tx_work(struct work_struct *w)
677 {
678 	struct rtw_dev *rtwdev = container_of(w, struct rtw_dev, tx_work);
679 
680 	__rtw_tx_work(rtwdev);
681 }
682 
rtw_txq_init(struct rtw_dev * rtwdev,struct ieee80211_txq * txq)683 void rtw_txq_init(struct rtw_dev *rtwdev, struct ieee80211_txq *txq)
684 {
685 	struct rtw_txq *rtwtxq;
686 
687 	if (!txq)
688 		return;
689 
690 	rtwtxq = (struct rtw_txq *)txq->drv_priv;
691 	INIT_LIST_HEAD(&rtwtxq->list);
692 }
693 
rtw_txq_cleanup(struct rtw_dev * rtwdev,struct ieee80211_txq * txq)694 void rtw_txq_cleanup(struct rtw_dev *rtwdev, struct ieee80211_txq *txq)
695 {
696 	struct rtw_txq *rtwtxq;
697 
698 	if (!txq)
699 		return;
700 
701 	rtwtxq = (struct rtw_txq *)txq->drv_priv;
702 	spin_lock_bh(&rtwdev->txq_lock);
703 	if (!list_empty(&rtwtxq->list))
704 		list_del_init(&rtwtxq->list);
705 	spin_unlock_bh(&rtwdev->txq_lock);
706 }
707 
708 static const enum rtw_tx_queue_type ac_to_hwq[] = {
709 	[IEEE80211_AC_VO] = RTW_TX_QUEUE_VO,
710 	[IEEE80211_AC_VI] = RTW_TX_QUEUE_VI,
711 	[IEEE80211_AC_BE] = RTW_TX_QUEUE_BE,
712 	[IEEE80211_AC_BK] = RTW_TX_QUEUE_BK,
713 };
714 
715 static_assert(ARRAY_SIZE(ac_to_hwq) == IEEE80211_NUM_ACS);
716 
rtw_tx_ac_to_hwq(enum ieee80211_ac_numbers ac)717 enum rtw_tx_queue_type rtw_tx_ac_to_hwq(enum ieee80211_ac_numbers ac)
718 {
719 	if (WARN_ON(unlikely(ac >= IEEE80211_NUM_ACS)))
720 		return RTW_TX_QUEUE_BE;
721 
722 	return ac_to_hwq[ac];
723 }
724 EXPORT_SYMBOL(rtw_tx_ac_to_hwq);
725 
rtw_tx_queue_mapping(struct sk_buff * skb)726 enum rtw_tx_queue_type rtw_tx_queue_mapping(struct sk_buff *skb)
727 {
728 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
729 	__le16 fc = hdr->frame_control;
730 	u8 q_mapping = skb_get_queue_mapping(skb);
731 	enum rtw_tx_queue_type queue;
732 
733 	if (unlikely(ieee80211_is_beacon(fc)))
734 		queue = RTW_TX_QUEUE_BCN;
735 	else if (unlikely(ieee80211_is_mgmt(fc) || ieee80211_is_ctl(fc)))
736 		queue = RTW_TX_QUEUE_MGMT;
737 	else if (is_broadcast_ether_addr(hdr->addr1) ||
738 		 is_multicast_ether_addr(hdr->addr1))
739 		queue = RTW_TX_QUEUE_HI0;
740 	else if (WARN_ON_ONCE(q_mapping >= ARRAY_SIZE(ac_to_hwq)))
741 		queue = ac_to_hwq[IEEE80211_AC_BE];
742 	else
743 		queue = ac_to_hwq[q_mapping];
744 
745 	return queue;
746 }
747 EXPORT_SYMBOL(rtw_tx_queue_mapping);
748