1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /*
3  * Copyright (C) 2012-2014, 2018-2022 Intel Corporation
4  * Copyright (C) 2013-2015 Intel Mobile Communications GmbH
5  * Copyright (C) 2016-2017 Intel Deutschland GmbH
6  */
7 #include <linux/ieee80211.h>
8 #include <linux/etherdevice.h>
9 #include <linux/tcp.h>
10 #include <net/gso.h>
11 #include <net/ip.h>
12 #include <net/ipv6.h>
13 
14 #include "iwl-trans.h"
15 #include "iwl-eeprom-parse.h"
16 #include "mvm.h"
17 #include "sta.h"
18 #include "time-sync.h"
19 
20 static void
21 iwl_mvm_bar_check_trigger(struct iwl_mvm *mvm, const u8 *addr,
22 			  u16 tid, u16 ssn)
23 {
24 	struct iwl_fw_dbg_trigger_tlv *trig;
25 	struct iwl_fw_dbg_trigger_ba *ba_trig;
26 
27 	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, NULL, FW_DBG_TRIGGER_BA);
28 	if (!trig)
29 		return;
30 
31 	ba_trig = (void *)trig->data;
32 
33 	if (!(le16_to_cpu(ba_trig->tx_bar) & BIT(tid)))
34 		return;
35 
36 	iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
37 				"BAR sent to %pM, tid %d, ssn %d",
38 				addr, tid, ssn);
39 }
40 
41 #define OPT_HDR(type, skb, off) \
42 	(type *)(skb_network_header(skb) + (off))
43 
44 static u16 iwl_mvm_tx_csum_pre_bz(struct iwl_mvm *mvm, struct sk_buff *skb,
45 				  struct ieee80211_tx_info *info, bool amsdu)
46 {
47 	struct ieee80211_hdr *hdr = (void *)skb->data;
48 	u16 offload_assist = 0;
49 #if IS_ENABLED(CONFIG_INET)
50 	u16 mh_len = ieee80211_hdrlen(hdr->frame_control);
51 	u8 protocol = 0;
52 
53 	/* Do not compute checksum if already computed */
54 	if (skb->ip_summed != CHECKSUM_PARTIAL)
55 		goto out;
56 
57 	/* We do not expect to be requested to csum stuff we do not support */
58 	if (WARN_ONCE(!(mvm->hw->netdev_features & IWL_TX_CSUM_NETIF_FLAGS) ||
59 		      (skb->protocol != htons(ETH_P_IP) &&
60 		       skb->protocol != htons(ETH_P_IPV6)),
61 		      "No support for requested checksum\n")) {
62 		skb_checksum_help(skb);
63 		goto out;
64 	}
65 
66 	if (skb->protocol == htons(ETH_P_IP)) {
67 		protocol = ip_hdr(skb)->protocol;
68 	} else {
69 #if IS_ENABLED(CONFIG_IPV6)
70 		struct ipv6hdr *ipv6h =
71 			(struct ipv6hdr *)skb_network_header(skb);
72 		unsigned int off = sizeof(*ipv6h);
73 
74 		protocol = ipv6h->nexthdr;
75 		while (protocol != NEXTHDR_NONE && ipv6_ext_hdr(protocol)) {
76 			struct ipv6_opt_hdr *hp;
77 
78 			/* only supported extension headers */
79 			if (protocol != NEXTHDR_ROUTING &&
80 			    protocol != NEXTHDR_HOP &&
81 			    protocol != NEXTHDR_DEST) {
82 				skb_checksum_help(skb);
83 				goto out;
84 			}
85 
86 			hp = OPT_HDR(struct ipv6_opt_hdr, skb, off);
87 			protocol = hp->nexthdr;
88 			off += ipv6_optlen(hp);
89 		}
90 		/* if we get here - protocol now should be TCP/UDP */
91 #endif
92 	}
93 
94 	if (protocol != IPPROTO_TCP && protocol != IPPROTO_UDP) {
95 		WARN_ON_ONCE(1);
96 		skb_checksum_help(skb);
97 		goto out;
98 	}
99 
100 	/* enable L4 csum */
101 	offload_assist |= BIT(TX_CMD_OFFLD_L4_EN);
102 
103 	/*
104 	 * Set offset to IP header (snap).
105 	 * We don't support tunneling so no need to take care of inner header.
106 	 * Size is in words.
107 	 */
108 	offload_assist |= (4 << TX_CMD_OFFLD_IP_HDR);
109 
110 	/* Do IPv4 csum for AMSDU only (no IP csum for Ipv6) */
111 	if (skb->protocol == htons(ETH_P_IP) && amsdu) {
112 		ip_hdr(skb)->check = 0;
113 		offload_assist |= BIT(TX_CMD_OFFLD_L3_EN);
114 	}
115 
116 	/* reset UDP/TCP header csum */
117 	if (protocol == IPPROTO_TCP)
118 		tcp_hdr(skb)->check = 0;
119 	else
120 		udp_hdr(skb)->check = 0;
121 
122 	/*
123 	 * mac header len should include IV, size is in words unless
124 	 * the IV is added by the firmware like in WEP.
125 	 * In new Tx API, the IV is always added by the firmware.
126 	 */
127 	if (!iwl_mvm_has_new_tx_api(mvm) && info->control.hw_key &&
128 	    info->control.hw_key->cipher != WLAN_CIPHER_SUITE_WEP40 &&
129 	    info->control.hw_key->cipher != WLAN_CIPHER_SUITE_WEP104)
130 		mh_len += info->control.hw_key->iv_len;
131 	mh_len /= 2;
132 	offload_assist |= mh_len << TX_CMD_OFFLD_MH_SIZE;
133 
134 out:
135 #endif
136 	if (amsdu)
137 		offload_assist |= BIT(TX_CMD_OFFLD_AMSDU);
138 	else if (ieee80211_hdrlen(hdr->frame_control) % 4)
139 		/* padding is inserted later in transport */
140 		offload_assist |= BIT(TX_CMD_OFFLD_PAD);
141 
142 	return offload_assist;
143 }
144 
145 u32 iwl_mvm_tx_csum_bz(struct iwl_mvm *mvm, struct sk_buff *skb, bool amsdu)
146 {
147 	struct ieee80211_hdr *hdr = (void *)skb->data;
148 	u32 offload_assist = IWL_TX_CMD_OFFLD_BZ_PARTIAL_CSUM;
149 	unsigned int hdrlen = ieee80211_hdrlen(hdr->frame_control);
150 	unsigned int csum_start = skb_checksum_start_offset(skb);
151 
152 	offload_assist |= u32_encode_bits(hdrlen / 2,
153 					  IWL_TX_CMD_OFFLD_BZ_MH_LEN);
154 	if (amsdu)
155 		offload_assist |= IWL_TX_CMD_OFFLD_BZ_AMSDU;
156 	else if (hdrlen % 4)
157 		/* padding is inserted later in transport */
158 		offload_assist |= IWL_TX_CMD_OFFLD_BZ_MH_PAD;
159 
160 	if (skb->ip_summed != CHECKSUM_PARTIAL)
161 		return offload_assist;
162 
163 	offload_assist |= IWL_TX_CMD_OFFLD_BZ_ENABLE_CSUM |
164 			  IWL_TX_CMD_OFFLD_BZ_ZERO2ONES;
165 
166 	/*
167 	 * mac80211 will always calculate checksum in software for
168 	 * non-fast-xmit, and so we can only do offloaded checksum
169 	 * for fast-xmit frames. In this case, we always have the
170 	 * RFC 1042 header present. skb_checksum_start_offset()
171 	 * returns the offset from the beginning, but the hardware
172 	 * needs it from after the header & SNAP header.
173 	 */
174 	csum_start -= hdrlen + 8;
175 
176 	offload_assist |= u32_encode_bits(csum_start,
177 					  IWL_TX_CMD_OFFLD_BZ_START_OFFS);
178 	offload_assist |= u32_encode_bits(csum_start + skb->csum_offset,
179 					  IWL_TX_CMD_OFFLD_BZ_RESULT_OFFS);
180 
181 	return offload_assist;
182 }
183 
184 static u32 iwl_mvm_tx_csum(struct iwl_mvm *mvm, struct sk_buff *skb,
185 			   struct ieee80211_tx_info *info,
186 			   bool amsdu)
187 {
188 	if (!iwl_mvm_has_new_tx_csum(mvm))
189 		return iwl_mvm_tx_csum_pre_bz(mvm, skb, info, amsdu);
190 	return iwl_mvm_tx_csum_bz(mvm, skb, amsdu);
191 }
192 
193 /*
194  * Sets most of the Tx cmd's fields
195  */
196 void iwl_mvm_set_tx_cmd(struct iwl_mvm *mvm, struct sk_buff *skb,
197 			struct iwl_tx_cmd *tx_cmd,
198 			struct ieee80211_tx_info *info, u8 sta_id)
199 {
200 	struct ieee80211_hdr *hdr = (void *)skb->data;
201 	__le16 fc = hdr->frame_control;
202 	u32 tx_flags = le32_to_cpu(tx_cmd->tx_flags);
203 	u32 len = skb->len + FCS_LEN;
204 	bool amsdu = false;
205 	u8 ac;
206 
207 	if (!(info->flags & IEEE80211_TX_CTL_NO_ACK) ||
208 	    (ieee80211_is_probe_resp(fc) &&
209 	     !is_multicast_ether_addr(hdr->addr1)))
210 		tx_flags |= TX_CMD_FLG_ACK;
211 	else
212 		tx_flags &= ~TX_CMD_FLG_ACK;
213 
214 	if (ieee80211_is_probe_resp(fc))
215 		tx_flags |= TX_CMD_FLG_TSF;
216 
217 	if (ieee80211_has_morefrags(fc))
218 		tx_flags |= TX_CMD_FLG_MORE_FRAG;
219 
220 	if (ieee80211_is_data_qos(fc)) {
221 		u8 *qc = ieee80211_get_qos_ctl(hdr);
222 		tx_cmd->tid_tspec = qc[0] & 0xf;
223 		tx_flags &= ~TX_CMD_FLG_SEQ_CTL;
224 		amsdu = *qc & IEEE80211_QOS_CTL_A_MSDU_PRESENT;
225 	} else if (ieee80211_is_back_req(fc)) {
226 		struct ieee80211_bar *bar = (void *)skb->data;
227 		u16 control = le16_to_cpu(bar->control);
228 		u16 ssn = le16_to_cpu(bar->start_seq_num);
229 
230 		tx_flags |= TX_CMD_FLG_ACK | TX_CMD_FLG_BAR;
231 		tx_cmd->tid_tspec = (control &
232 				     IEEE80211_BAR_CTRL_TID_INFO_MASK) >>
233 			IEEE80211_BAR_CTRL_TID_INFO_SHIFT;
234 		WARN_ON_ONCE(tx_cmd->tid_tspec >= IWL_MAX_TID_COUNT);
235 		iwl_mvm_bar_check_trigger(mvm, bar->ra, tx_cmd->tid_tspec,
236 					  ssn);
237 	} else {
238 		if (ieee80211_is_data(fc))
239 			tx_cmd->tid_tspec = IWL_TID_NON_QOS;
240 		else
241 			tx_cmd->tid_tspec = IWL_MAX_TID_COUNT;
242 
243 		if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ)
244 			tx_flags |= TX_CMD_FLG_SEQ_CTL;
245 		else
246 			tx_flags &= ~TX_CMD_FLG_SEQ_CTL;
247 	}
248 
249 	/* Default to 0 (BE) when tid_spec is set to IWL_MAX_TID_COUNT */
250 	if (tx_cmd->tid_tspec < IWL_MAX_TID_COUNT)
251 		ac = tid_to_mac80211_ac[tx_cmd->tid_tspec];
252 	else
253 		ac = tid_to_mac80211_ac[0];
254 
255 	tx_flags |= iwl_mvm_bt_coex_tx_prio(mvm, hdr, info, ac) <<
256 			TX_CMD_FLG_BT_PRIO_POS;
257 
258 	if (ieee80211_is_mgmt(fc)) {
259 		if (ieee80211_is_assoc_req(fc) || ieee80211_is_reassoc_req(fc))
260 			tx_cmd->pm_frame_timeout = cpu_to_le16(PM_FRAME_ASSOC);
261 		else if (ieee80211_is_action(fc))
262 			tx_cmd->pm_frame_timeout = cpu_to_le16(PM_FRAME_NONE);
263 		else
264 			tx_cmd->pm_frame_timeout = cpu_to_le16(PM_FRAME_MGMT);
265 
266 		/* The spec allows Action frames in A-MPDU, we don't support
267 		 * it
268 		 */
269 		WARN_ON_ONCE(info->flags & IEEE80211_TX_CTL_AMPDU);
270 	} else if (info->control.flags & IEEE80211_TX_CTRL_PORT_CTRL_PROTO) {
271 		tx_cmd->pm_frame_timeout = cpu_to_le16(PM_FRAME_MGMT);
272 	} else {
273 		tx_cmd->pm_frame_timeout = cpu_to_le16(PM_FRAME_NONE);
274 	}
275 
276 	if (ieee80211_is_data(fc) && len > mvm->rts_threshold &&
277 	    !is_multicast_ether_addr(hdr->addr1))
278 		tx_flags |= TX_CMD_FLG_PROT_REQUIRE;
279 
280 	if (fw_has_capa(&mvm->fw->ucode_capa,
281 			IWL_UCODE_TLV_CAPA_TXPOWER_INSERTION_SUPPORT) &&
282 	    ieee80211_action_contains_tpc(skb))
283 		tx_flags |= TX_CMD_FLG_WRITE_TX_POWER;
284 
285 	tx_cmd->tx_flags = cpu_to_le32(tx_flags);
286 	/* Total # bytes to be transmitted - PCIe code will adjust for A-MSDU */
287 	tx_cmd->len = cpu_to_le16((u16)skb->len);
288 	tx_cmd->life_time = cpu_to_le32(TX_CMD_LIFE_TIME_INFINITE);
289 	tx_cmd->sta_id = sta_id;
290 
291 	tx_cmd->offload_assist =
292 		cpu_to_le16(iwl_mvm_tx_csum_pre_bz(mvm, skb, info, amsdu));
293 }
294 
295 static u32 iwl_mvm_get_tx_ant(struct iwl_mvm *mvm,
296 			      struct ieee80211_tx_info *info,
297 			      struct ieee80211_sta *sta, __le16 fc)
298 {
299 	if (info->band == NL80211_BAND_2GHZ &&
300 	    !iwl_mvm_bt_coex_is_shared_ant_avail(mvm))
301 		return mvm->cfg->non_shared_ant << RATE_MCS_ANT_POS;
302 
303 	if (sta && ieee80211_is_data(fc)) {
304 		struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
305 
306 		return BIT(mvmsta->tx_ant) << RATE_MCS_ANT_POS;
307 	}
308 
309 	return BIT(mvm->mgmt_last_antenna_idx) << RATE_MCS_ANT_POS;
310 }
311 
312 static u32 iwl_mvm_get_inject_tx_rate(struct iwl_mvm *mvm,
313 				      struct ieee80211_tx_info *info)
314 {
315 	struct ieee80211_tx_rate *rate = &info->control.rates[0];
316 	u32 result;
317 
318 	/*
319 	 * we only care about legacy/HT/VHT so far, so we can
320 	 * build in v1 and use iwl_new_rate_from_v1()
321 	 */
322 
323 	if (rate->flags & IEEE80211_TX_RC_VHT_MCS) {
324 		u8 mcs = ieee80211_rate_get_vht_mcs(rate);
325 		u8 nss = ieee80211_rate_get_vht_nss(rate);
326 
327 		result = RATE_MCS_VHT_MSK_V1;
328 		result |= u32_encode_bits(mcs, RATE_VHT_MCS_RATE_CODE_MSK);
329 		result |= u32_encode_bits(nss, RATE_MCS_NSS_MSK);
330 		if (rate->flags & IEEE80211_TX_RC_SHORT_GI)
331 			result |= RATE_MCS_SGI_MSK_V1;
332 		if (rate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
333 			result |= u32_encode_bits(1, RATE_MCS_CHAN_WIDTH_MSK_V1);
334 		else if (rate->flags & IEEE80211_TX_RC_80_MHZ_WIDTH)
335 			result |= u32_encode_bits(2, RATE_MCS_CHAN_WIDTH_MSK_V1);
336 		else if (rate->flags & IEEE80211_TX_RC_160_MHZ_WIDTH)
337 			result |= u32_encode_bits(3, RATE_MCS_CHAN_WIDTH_MSK_V1);
338 	} else if (rate->flags & IEEE80211_TX_RC_MCS) {
339 		result = RATE_MCS_HT_MSK_V1;
340 		result |= u32_encode_bits(rate->idx,
341 					  RATE_HT_MCS_RATE_CODE_MSK_V1 |
342 					  RATE_HT_MCS_NSS_MSK_V1);
343 		if (rate->flags & IEEE80211_TX_RC_SHORT_GI)
344 			result |= RATE_MCS_SGI_MSK_V1;
345 		if (rate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
346 			result |= u32_encode_bits(1, RATE_MCS_CHAN_WIDTH_MSK_V1);
347 		if (rate->flags & IEEE80211_TX_CTL_LDPC)
348 			result |= RATE_MCS_LDPC_MSK_V1;
349 		if (u32_get_bits(rate->flags, IEEE80211_TX_CTL_STBC))
350 			result |= RATE_MCS_STBC_MSK;
351 	} else {
352 		return 0;
353 	}
354 
355 	if (iwl_fw_lookup_notif_ver(mvm->fw, LONG_GROUP, TX_CMD, 0) > 6)
356 		return iwl_new_rate_from_v1(result);
357 	return result;
358 }
359 
360 static u32 iwl_mvm_get_tx_rate(struct iwl_mvm *mvm,
361 			       struct ieee80211_tx_info *info,
362 			       struct ieee80211_sta *sta, __le16 fc)
363 {
364 	int rate_idx = -1;
365 	u8 rate_plcp;
366 	u32 rate_flags = 0;
367 	bool is_cck;
368 
369 	if (unlikely(info->control.flags & IEEE80211_TX_CTRL_RATE_INJECT)) {
370 		u32 result = iwl_mvm_get_inject_tx_rate(mvm, info);
371 
372 		if (result)
373 			return result;
374 		rate_idx = info->control.rates[0].idx;
375 	} else if (!ieee80211_hw_check(mvm->hw, HAS_RATE_CONTROL)) {
376 		/* info->control is only relevant for non HW rate control */
377 
378 		/* HT rate doesn't make sense for a non data frame */
379 		WARN_ONCE(info->control.rates[0].flags & IEEE80211_TX_RC_MCS &&
380 			  !ieee80211_is_data(fc),
381 			  "Got a HT rate (flags:0x%x/mcs:%d/fc:0x%x/state:%d) for a non data frame\n",
382 			  info->control.rates[0].flags,
383 			  info->control.rates[0].idx,
384 			  le16_to_cpu(fc),
385 			  sta ? iwl_mvm_sta_from_mac80211(sta)->sta_state : -1);
386 
387 		rate_idx = info->control.rates[0].idx;
388 
389 		/* For non 2 GHZ band, remap mac80211 rate indices into driver
390 		 * indices.
391 		 */
392 		if (info->band != NL80211_BAND_2GHZ ||
393 		    (info->flags & IEEE80211_TX_CTL_NO_CCK_RATE))
394 			rate_idx += IWL_FIRST_OFDM_RATE;
395 
396 		/* For 2.4 GHZ band, check that there is no need to remap */
397 		BUILD_BUG_ON(IWL_FIRST_CCK_RATE != 0);
398 	}
399 
400 	/* if the rate isn't a well known legacy rate, take the lowest one */
401 	if (rate_idx < 0 || rate_idx >= IWL_RATE_COUNT_LEGACY)
402 		rate_idx = iwl_mvm_mac_ctxt_get_lowest_rate(mvm,
403 							    info,
404 							    info->control.vif);
405 
406 	/* Get PLCP rate for tx_cmd->rate_n_flags */
407 	rate_plcp = iwl_mvm_mac80211_idx_to_hwrate(mvm->fw, rate_idx);
408 	is_cck = (rate_idx >= IWL_FIRST_CCK_RATE) && (rate_idx <= IWL_LAST_CCK_RATE);
409 
410 	/* Set CCK or OFDM flag */
411 	if (iwl_fw_lookup_cmd_ver(mvm->fw, TX_CMD, 0) > 8) {
412 		if (!is_cck)
413 			rate_flags |= RATE_MCS_LEGACY_OFDM_MSK;
414 		else
415 			rate_flags |= RATE_MCS_CCK_MSK;
416 	} else if (is_cck) {
417 		rate_flags |= RATE_MCS_CCK_MSK_V1;
418 	}
419 
420 	return (u32)rate_plcp | rate_flags;
421 }
422 
423 static u32 iwl_mvm_get_tx_rate_n_flags(struct iwl_mvm *mvm,
424 				       struct ieee80211_tx_info *info,
425 				       struct ieee80211_sta *sta, __le16 fc)
426 {
427 	return iwl_mvm_get_tx_rate(mvm, info, sta, fc) |
428 		iwl_mvm_get_tx_ant(mvm, info, sta, fc);
429 }
430 
431 /*
432  * Sets the fields in the Tx cmd that are rate related
433  */
434 void iwl_mvm_set_tx_cmd_rate(struct iwl_mvm *mvm, struct iwl_tx_cmd *tx_cmd,
435 			    struct ieee80211_tx_info *info,
436 			    struct ieee80211_sta *sta, __le16 fc)
437 {
438 	/* Set retry limit on RTS packets */
439 	tx_cmd->rts_retry_limit = IWL_RTS_DFAULT_RETRY_LIMIT;
440 
441 	/* Set retry limit on DATA packets and Probe Responses*/
442 	if (ieee80211_is_probe_resp(fc)) {
443 		tx_cmd->data_retry_limit = IWL_MGMT_DFAULT_RETRY_LIMIT;
444 		tx_cmd->rts_retry_limit =
445 			min(tx_cmd->data_retry_limit, tx_cmd->rts_retry_limit);
446 	} else if (ieee80211_is_back_req(fc)) {
447 		tx_cmd->data_retry_limit = IWL_BAR_DFAULT_RETRY_LIMIT;
448 	} else {
449 		tx_cmd->data_retry_limit = IWL_DEFAULT_TX_RETRY;
450 	}
451 
452 	/*
453 	 * for data packets, rate info comes from the table inside the fw. This
454 	 * table is controlled by LINK_QUALITY commands
455 	 */
456 
457 	if (likely(ieee80211_is_data(fc) && sta &&
458 		   !(info->control.flags & IEEE80211_TX_CTRL_RATE_INJECT))) {
459 		struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
460 
461 		if (mvmsta->sta_state >= IEEE80211_STA_AUTHORIZED) {
462 			tx_cmd->initial_rate_index = 0;
463 			tx_cmd->tx_flags |= cpu_to_le32(TX_CMD_FLG_STA_RATE);
464 			return;
465 		}
466 	} else if (ieee80211_is_back_req(fc)) {
467 		tx_cmd->tx_flags |=
468 			cpu_to_le32(TX_CMD_FLG_ACK | TX_CMD_FLG_BAR);
469 	}
470 
471 	/* Set the rate in the TX cmd */
472 	tx_cmd->rate_n_flags =
473 		cpu_to_le32(iwl_mvm_get_tx_rate_n_flags(mvm, info, sta, fc));
474 }
475 
476 static inline void iwl_mvm_set_tx_cmd_pn(struct ieee80211_tx_info *info,
477 					 u8 *crypto_hdr)
478 {
479 	struct ieee80211_key_conf *keyconf = info->control.hw_key;
480 	u64 pn;
481 
482 	pn = atomic64_inc_return(&keyconf->tx_pn);
483 	crypto_hdr[0] = pn;
484 	crypto_hdr[2] = 0;
485 	crypto_hdr[3] = 0x20 | (keyconf->keyidx << 6);
486 	crypto_hdr[1] = pn >> 8;
487 	crypto_hdr[4] = pn >> 16;
488 	crypto_hdr[5] = pn >> 24;
489 	crypto_hdr[6] = pn >> 32;
490 	crypto_hdr[7] = pn >> 40;
491 }
492 
493 /*
494  * Sets the fields in the Tx cmd that are crypto related
495  */
496 static void iwl_mvm_set_tx_cmd_crypto(struct iwl_mvm *mvm,
497 				      struct ieee80211_tx_info *info,
498 				      struct iwl_tx_cmd *tx_cmd,
499 				      struct sk_buff *skb_frag,
500 				      int hdrlen)
501 {
502 	struct ieee80211_key_conf *keyconf = info->control.hw_key;
503 	u8 *crypto_hdr = skb_frag->data + hdrlen;
504 	enum iwl_tx_cmd_sec_ctrl type = TX_CMD_SEC_CCM;
505 	u64 pn;
506 
507 	switch (keyconf->cipher) {
508 	case WLAN_CIPHER_SUITE_CCMP:
509 		iwl_mvm_set_tx_cmd_ccmp(info, tx_cmd);
510 		iwl_mvm_set_tx_cmd_pn(info, crypto_hdr);
511 		break;
512 
513 	case WLAN_CIPHER_SUITE_TKIP:
514 		tx_cmd->sec_ctl = TX_CMD_SEC_TKIP;
515 		pn = atomic64_inc_return(&keyconf->tx_pn);
516 		ieee80211_tkip_add_iv(crypto_hdr, keyconf, pn);
517 		ieee80211_get_tkip_p2k(keyconf, skb_frag, tx_cmd->key);
518 		break;
519 
520 	case WLAN_CIPHER_SUITE_WEP104:
521 		tx_cmd->sec_ctl |= TX_CMD_SEC_KEY128;
522 		fallthrough;
523 	case WLAN_CIPHER_SUITE_WEP40:
524 		tx_cmd->sec_ctl |= TX_CMD_SEC_WEP |
525 			((keyconf->keyidx << TX_CMD_SEC_WEP_KEY_IDX_POS) &
526 			  TX_CMD_SEC_WEP_KEY_IDX_MSK);
527 
528 		memcpy(&tx_cmd->key[3], keyconf->key, keyconf->keylen);
529 		break;
530 	case WLAN_CIPHER_SUITE_GCMP:
531 	case WLAN_CIPHER_SUITE_GCMP_256:
532 		type = TX_CMD_SEC_GCMP;
533 		fallthrough;
534 	case WLAN_CIPHER_SUITE_CCMP_256:
535 		/* TODO: Taking the key from the table might introduce a race
536 		 * when PTK rekeying is done, having an old packets with a PN
537 		 * based on the old key but the message encrypted with a new
538 		 * one.
539 		 * Need to handle this.
540 		 */
541 		tx_cmd->sec_ctl |= type | TX_CMD_SEC_KEY_FROM_TABLE;
542 		tx_cmd->key[0] = keyconf->hw_key_idx;
543 		iwl_mvm_set_tx_cmd_pn(info, crypto_hdr);
544 		break;
545 	default:
546 		tx_cmd->sec_ctl |= TX_CMD_SEC_EXT;
547 	}
548 }
549 
550 /*
551  * Allocates and sets the Tx cmd the driver data pointers in the skb
552  */
553 static struct iwl_device_tx_cmd *
554 iwl_mvm_set_tx_params(struct iwl_mvm *mvm, struct sk_buff *skb,
555 		      struct ieee80211_tx_info *info, int hdrlen,
556 		      struct ieee80211_sta *sta, u8 sta_id)
557 {
558 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
559 	struct iwl_device_tx_cmd *dev_cmd;
560 	struct iwl_tx_cmd *tx_cmd;
561 
562 	dev_cmd = iwl_trans_alloc_tx_cmd(mvm->trans);
563 
564 	if (unlikely(!dev_cmd))
565 		return NULL;
566 
567 	dev_cmd->hdr.cmd = TX_CMD;
568 
569 	if (iwl_mvm_has_new_tx_api(mvm)) {
570 		u32 rate_n_flags = 0;
571 		u16 flags = 0;
572 		struct iwl_mvm_sta *mvmsta = sta ?
573 			iwl_mvm_sta_from_mac80211(sta) : NULL;
574 		bool amsdu = false;
575 
576 		if (ieee80211_is_data_qos(hdr->frame_control)) {
577 			u8 *qc = ieee80211_get_qos_ctl(hdr);
578 
579 			amsdu = *qc & IEEE80211_QOS_CTL_A_MSDU_PRESENT;
580 		}
581 
582 		if (!info->control.hw_key)
583 			flags |= IWL_TX_FLAGS_ENCRYPT_DIS;
584 
585 		/*
586 		 * For data packets rate info comes from the fw. Only
587 		 * set rate/antenna during connection establishment or in case
588 		 * no station is given.
589 		 */
590 		if (!sta || !ieee80211_is_data(hdr->frame_control) ||
591 		    mvmsta->sta_state < IEEE80211_STA_AUTHORIZED) {
592 			flags |= IWL_TX_FLAGS_CMD_RATE;
593 			rate_n_flags =
594 				iwl_mvm_get_tx_rate_n_flags(mvm, info, sta,
595 							    hdr->frame_control);
596 		}
597 
598 		if (mvm->trans->trans_cfg->device_family >=
599 		    IWL_DEVICE_FAMILY_AX210) {
600 			struct iwl_tx_cmd_gen3 *cmd = (void *)dev_cmd->payload;
601 			u32 offload_assist = iwl_mvm_tx_csum(mvm, skb,
602 							     info, amsdu);
603 
604 			cmd->offload_assist = cpu_to_le32(offload_assist);
605 
606 			/* Total # bytes to be transmitted */
607 			cmd->len = cpu_to_le16((u16)skb->len);
608 
609 			/* Copy MAC header from skb into command buffer */
610 			memcpy(cmd->hdr, hdr, hdrlen);
611 
612 			cmd->flags = cpu_to_le16(flags);
613 			cmd->rate_n_flags = cpu_to_le32(rate_n_flags);
614 		} else {
615 			struct iwl_tx_cmd_gen2 *cmd = (void *)dev_cmd->payload;
616 			u16 offload_assist = iwl_mvm_tx_csum_pre_bz(mvm, skb,
617 								    info,
618 								    amsdu);
619 
620 			cmd->offload_assist = cpu_to_le16(offload_assist);
621 
622 			/* Total # bytes to be transmitted */
623 			cmd->len = cpu_to_le16((u16)skb->len);
624 
625 			/* Copy MAC header from skb into command buffer */
626 			memcpy(cmd->hdr, hdr, hdrlen);
627 
628 			cmd->flags = cpu_to_le32(flags);
629 			cmd->rate_n_flags = cpu_to_le32(rate_n_flags);
630 		}
631 		goto out;
632 	}
633 
634 	tx_cmd = (struct iwl_tx_cmd *)dev_cmd->payload;
635 
636 	if (info->control.hw_key)
637 		iwl_mvm_set_tx_cmd_crypto(mvm, info, tx_cmd, skb, hdrlen);
638 
639 	iwl_mvm_set_tx_cmd(mvm, skb, tx_cmd, info, sta_id);
640 
641 	iwl_mvm_set_tx_cmd_rate(mvm, tx_cmd, info, sta, hdr->frame_control);
642 
643 	/* Copy MAC header from skb into command buffer */
644 	memcpy(tx_cmd->hdr, hdr, hdrlen);
645 
646 out:
647 	return dev_cmd;
648 }
649 
650 static void iwl_mvm_skb_prepare_status(struct sk_buff *skb,
651 				       struct iwl_device_tx_cmd *cmd)
652 {
653 	struct ieee80211_tx_info *skb_info = IEEE80211_SKB_CB(skb);
654 
655 	memset(&skb_info->status, 0, sizeof(skb_info->status));
656 	memset(skb_info->driver_data, 0, sizeof(skb_info->driver_data));
657 
658 	skb_info->driver_data[1] = cmd;
659 }
660 
661 static int iwl_mvm_get_ctrl_vif_queue(struct iwl_mvm *mvm,
662 				      struct iwl_mvm_vif_link_info *link,
663 				      struct ieee80211_tx_info *info,
664 				      struct sk_buff *skb)
665 {
666 	struct ieee80211_hdr *hdr = (void *)skb->data;
667 	__le16 fc = hdr->frame_control;
668 
669 	switch (info->control.vif->type) {
670 	case NL80211_IFTYPE_AP:
671 	case NL80211_IFTYPE_ADHOC:
672 		/*
673 		 * Non-bufferable frames use the broadcast station, thus they
674 		 * use the probe queue.
675 		 * Also take care of the case where we send a deauth to a
676 		 * station that we don't have, or similarly an association
677 		 * response (with non-success status) for a station we can't
678 		 * accept.
679 		 * Also, disassociate frames might happen, particular with
680 		 * reason 7 ("Class 3 frame received from nonassociated STA").
681 		 */
682 		if (ieee80211_is_mgmt(fc) &&
683 		    (!ieee80211_is_bufferable_mmpdu(skb) ||
684 		     ieee80211_is_deauth(fc) || ieee80211_is_disassoc(fc)))
685 			return link->mgmt_queue;
686 
687 		if (!ieee80211_has_order(fc) && !ieee80211_is_probe_req(fc) &&
688 		    is_multicast_ether_addr(hdr->addr1))
689 			return link->cab_queue;
690 
691 		WARN_ONCE(info->control.vif->type != NL80211_IFTYPE_ADHOC,
692 			  "fc=0x%02x", le16_to_cpu(fc));
693 		return link->mgmt_queue;
694 	case NL80211_IFTYPE_P2P_DEVICE:
695 		if (ieee80211_is_mgmt(fc))
696 			return mvm->p2p_dev_queue;
697 
698 		WARN_ON_ONCE(1);
699 		return mvm->p2p_dev_queue;
700 	default:
701 		WARN_ONCE(1, "Not a ctrl vif, no available queue\n");
702 		return -1;
703 	}
704 }
705 
706 static void iwl_mvm_probe_resp_set_noa(struct iwl_mvm *mvm,
707 				       struct sk_buff *skb)
708 {
709 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
710 	struct iwl_mvm_vif *mvmvif =
711 		iwl_mvm_vif_from_mac80211(info->control.vif);
712 	struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)skb->data;
713 	int base_len = (u8 *)mgmt->u.probe_resp.variable - (u8 *)mgmt;
714 	struct iwl_probe_resp_data *resp_data;
715 	const u8 *ie;
716 	u8 *pos;
717 	u8 match[] = {
718 		(WLAN_OUI_WFA >> 16) & 0xff,
719 		(WLAN_OUI_WFA >> 8) & 0xff,
720 		WLAN_OUI_WFA & 0xff,
721 		WLAN_OUI_TYPE_WFA_P2P,
722 	};
723 
724 	rcu_read_lock();
725 
726 	resp_data = rcu_dereference(mvmvif->deflink.probe_resp_data);
727 	if (!resp_data)
728 		goto out;
729 
730 	if (!resp_data->notif.noa_active)
731 		goto out;
732 
733 	ie = cfg80211_find_ie_match(WLAN_EID_VENDOR_SPECIFIC,
734 				    mgmt->u.probe_resp.variable,
735 				    skb->len - base_len,
736 				    match, 4, 2);
737 	if (!ie) {
738 		IWL_DEBUG_TX(mvm, "probe resp doesn't have P2P IE\n");
739 		goto out;
740 	}
741 
742 	if (skb_tailroom(skb) < resp_data->noa_len) {
743 		if (pskb_expand_head(skb, 0, resp_data->noa_len, GFP_ATOMIC)) {
744 			IWL_ERR(mvm,
745 				"Failed to reallocate probe resp\n");
746 			goto out;
747 		}
748 	}
749 
750 	pos = skb_put(skb, resp_data->noa_len);
751 
752 	*pos++ = WLAN_EID_VENDOR_SPECIFIC;
753 	/* Set length of IE body (not including ID and length itself) */
754 	*pos++ = resp_data->noa_len - 2;
755 	*pos++ = (WLAN_OUI_WFA >> 16) & 0xff;
756 	*pos++ = (WLAN_OUI_WFA >> 8) & 0xff;
757 	*pos++ = WLAN_OUI_WFA & 0xff;
758 	*pos++ = WLAN_OUI_TYPE_WFA_P2P;
759 
760 	memcpy(pos, &resp_data->notif.noa_attr,
761 	       resp_data->noa_len - sizeof(struct ieee80211_vendor_ie));
762 
763 out:
764 	rcu_read_unlock();
765 }
766 
767 int iwl_mvm_tx_skb_non_sta(struct iwl_mvm *mvm, struct sk_buff *skb)
768 {
769 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
770 	struct ieee80211_tx_info info;
771 	struct iwl_device_tx_cmd *dev_cmd;
772 	u8 sta_id;
773 	int hdrlen = ieee80211_hdrlen(hdr->frame_control);
774 	__le16 fc = hdr->frame_control;
775 	bool offchannel = IEEE80211_SKB_CB(skb)->flags &
776 		IEEE80211_TX_CTL_TX_OFFCHAN;
777 	int queue = -1;
778 
779 	if (IWL_MVM_NON_TRANSMITTING_AP && ieee80211_is_probe_resp(fc))
780 		return -1;
781 
782 	memcpy(&info, skb->cb, sizeof(info));
783 
784 	if (WARN_ON_ONCE(skb->len > IEEE80211_MAX_DATA_LEN + hdrlen))
785 		return -1;
786 
787 	if (WARN_ON_ONCE(info.flags & IEEE80211_TX_CTL_AMPDU))
788 		return -1;
789 
790 	if (info.control.vif) {
791 		struct iwl_mvm_vif *mvmvif =
792 			iwl_mvm_vif_from_mac80211(info.control.vif);
793 
794 		if (info.control.vif->type == NL80211_IFTYPE_P2P_DEVICE ||
795 		    info.control.vif->type == NL80211_IFTYPE_AP ||
796 		    info.control.vif->type == NL80211_IFTYPE_ADHOC) {
797 			u32 link_id = u32_get_bits(info.control.flags,
798 						   IEEE80211_TX_CTRL_MLO_LINK);
799 			struct iwl_mvm_vif_link_info *link;
800 
801 			if (link_id == IEEE80211_LINK_UNSPECIFIED) {
802 				if (info.control.vif->active_links)
803 					link_id = ffs(info.control.vif->active_links) - 1;
804 				else
805 					link_id = 0;
806 			}
807 
808 			link = mvmvif->link[link_id];
809 
810 			if (!ieee80211_is_data(hdr->frame_control))
811 				sta_id = link->bcast_sta.sta_id;
812 			else
813 				sta_id = link->mcast_sta.sta_id;
814 
815 			queue = iwl_mvm_get_ctrl_vif_queue(mvm, link, &info,
816 							   skb);
817 		} else if (info.control.vif->type == NL80211_IFTYPE_MONITOR) {
818 			queue = mvm->snif_queue;
819 			sta_id = mvm->snif_sta.sta_id;
820 		} else if (info.control.vif->type == NL80211_IFTYPE_STATION &&
821 			   offchannel) {
822 			/*
823 			 * IWL_MVM_OFFCHANNEL_QUEUE is used for ROC packets
824 			 * that can be used in 2 different types of vifs, P2P &
825 			 * STATION.
826 			 * P2P uses the offchannel queue.
827 			 * STATION (HS2.0) uses the auxiliary context of the FW,
828 			 * and hence needs to be sent on the aux queue.
829 			 */
830 			sta_id = mvm->aux_sta.sta_id;
831 			queue = mvm->aux_queue;
832 		}
833 	}
834 
835 	if (queue < 0) {
836 		IWL_ERR(mvm, "No queue was found. Dropping TX\n");
837 		return -1;
838 	}
839 
840 	if (unlikely(ieee80211_is_probe_resp(fc)))
841 		iwl_mvm_probe_resp_set_noa(mvm, skb);
842 
843 	IWL_DEBUG_TX(mvm, "station Id %d, queue=%d\n", sta_id, queue);
844 
845 	dev_cmd = iwl_mvm_set_tx_params(mvm, skb, &info, hdrlen, NULL, sta_id);
846 	if (!dev_cmd)
847 		return -1;
848 
849 	/* From now on, we cannot access info->control */
850 	iwl_mvm_skb_prepare_status(skb, dev_cmd);
851 
852 	if (iwl_trans_tx(mvm->trans, skb, dev_cmd, queue)) {
853 		iwl_trans_free_tx_cmd(mvm->trans, dev_cmd);
854 		return -1;
855 	}
856 
857 	return 0;
858 }
859 
860 unsigned int iwl_mvm_max_amsdu_size(struct iwl_mvm *mvm,
861 				    struct ieee80211_sta *sta, unsigned int tid)
862 {
863 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
864 	u8 ac = tid_to_mac80211_ac[tid];
865 	enum nl80211_band band;
866 	unsigned int txf;
867 	unsigned int val;
868 	int lmac;
869 
870 	/* For HE redirect to trigger based fifos */
871 	if (sta->deflink.he_cap.has_he && !WARN_ON(!iwl_mvm_has_new_tx_api(mvm)))
872 		ac += 4;
873 
874 	txf = iwl_mvm_mac_ac_to_tx_fifo(mvm, ac);
875 
876 	/*
877 	 * Don't send an AMSDU that will be longer than the TXF.
878 	 * Add a security margin of 256 for the TX command + headers.
879 	 * We also want to have the start of the next packet inside the
880 	 * fifo to be able to send bursts.
881 	 */
882 	val = mvmsta->max_amsdu_len;
883 
884 	if (hweight16(sta->valid_links) <= 1) {
885 		if (sta->valid_links) {
886 			struct ieee80211_bss_conf *link_conf;
887 			unsigned int link = ffs(sta->valid_links) - 1;
888 
889 			rcu_read_lock();
890 			link_conf = rcu_dereference(mvmsta->vif->link_conf[link]);
891 			if (WARN_ON(!link_conf))
892 				band = NL80211_BAND_2GHZ;
893 			else
894 				band = link_conf->chandef.chan->band;
895 			rcu_read_unlock();
896 		} else {
897 			band = mvmsta->vif->bss_conf.chandef.chan->band;
898 		}
899 
900 		lmac = iwl_mvm_get_lmac_id(mvm->fw, band);
901 	} else if (fw_has_capa(&mvm->fw->ucode_capa,
902 			       IWL_UCODE_TLV_CAPA_CDB_SUPPORT)) {
903 		/* for real MLO restrict to both LMACs if they exist */
904 		lmac = IWL_LMAC_5G_INDEX;
905 		val = min_t(unsigned int, val,
906 			    mvm->fwrt.smem_cfg.lmac[lmac].txfifo_size[txf] - 256);
907 		lmac = IWL_LMAC_24G_INDEX;
908 	} else {
909 		lmac = IWL_LMAC_24G_INDEX;
910 	}
911 
912 	return min_t(unsigned int, val,
913 		     mvm->fwrt.smem_cfg.lmac[lmac].txfifo_size[txf] - 256);
914 }
915 
916 #ifdef CONFIG_INET
917 
918 static int
919 iwl_mvm_tx_tso_segment(struct sk_buff *skb, unsigned int num_subframes,
920 		       netdev_features_t netdev_flags,
921 		       struct sk_buff_head *mpdus_skb)
922 {
923 	struct sk_buff *tmp, *next;
924 	struct ieee80211_hdr *hdr = (void *)skb->data;
925 	char cb[sizeof(skb->cb)];
926 	u16 i = 0;
927 	unsigned int tcp_payload_len;
928 	unsigned int mss = skb_shinfo(skb)->gso_size;
929 	bool ipv4 = (skb->protocol == htons(ETH_P_IP));
930 	bool qos = ieee80211_is_data_qos(hdr->frame_control);
931 	u16 ip_base_id = ipv4 ? ntohs(ip_hdr(skb)->id) : 0;
932 
933 	skb_shinfo(skb)->gso_size = num_subframes * mss;
934 	memcpy(cb, skb->cb, sizeof(cb));
935 
936 	next = skb_gso_segment(skb, netdev_flags);
937 	skb_shinfo(skb)->gso_size = mss;
938 	skb_shinfo(skb)->gso_type = ipv4 ? SKB_GSO_TCPV4 : SKB_GSO_TCPV6;
939 	if (WARN_ON_ONCE(IS_ERR(next)))
940 		return -EINVAL;
941 	else if (next)
942 		consume_skb(skb);
943 
944 	skb_list_walk_safe(next, tmp, next) {
945 		memcpy(tmp->cb, cb, sizeof(tmp->cb));
946 		/*
947 		 * Compute the length of all the data added for the A-MSDU.
948 		 * This will be used to compute the length to write in the TX
949 		 * command. We have: SNAP + IP + TCP for n -1 subframes and
950 		 * ETH header for n subframes.
951 		 */
952 		tcp_payload_len = skb_tail_pointer(tmp) -
953 			skb_transport_header(tmp) -
954 			tcp_hdrlen(tmp) + tmp->data_len;
955 
956 		if (ipv4)
957 			ip_hdr(tmp)->id = htons(ip_base_id + i * num_subframes);
958 
959 		if (tcp_payload_len > mss) {
960 			skb_shinfo(tmp)->gso_size = mss;
961 			skb_shinfo(tmp)->gso_type = ipv4 ? SKB_GSO_TCPV4 :
962 							   SKB_GSO_TCPV6;
963 		} else {
964 			if (qos) {
965 				u8 *qc;
966 
967 				if (ipv4)
968 					ip_send_check(ip_hdr(tmp));
969 
970 				qc = ieee80211_get_qos_ctl((void *)tmp->data);
971 				*qc &= ~IEEE80211_QOS_CTL_A_MSDU_PRESENT;
972 			}
973 			skb_shinfo(tmp)->gso_size = 0;
974 		}
975 
976 		skb_mark_not_on_list(tmp);
977 		__skb_queue_tail(mpdus_skb, tmp);
978 		i++;
979 	}
980 
981 	return 0;
982 }
983 
984 static int iwl_mvm_tx_tso(struct iwl_mvm *mvm, struct sk_buff *skb,
985 			  struct ieee80211_tx_info *info,
986 			  struct ieee80211_sta *sta,
987 			  struct sk_buff_head *mpdus_skb)
988 {
989 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
990 	struct ieee80211_hdr *hdr = (void *)skb->data;
991 	unsigned int mss = skb_shinfo(skb)->gso_size;
992 	unsigned int num_subframes, tcp_payload_len, subf_len, max_amsdu_len;
993 	u16 snap_ip_tcp, pad;
994 	netdev_features_t netdev_flags = NETIF_F_CSUM_MASK | NETIF_F_SG;
995 	u8 tid;
996 
997 	snap_ip_tcp = 8 + skb_transport_header(skb) - skb_network_header(skb) +
998 		tcp_hdrlen(skb);
999 
1000 	if (!mvmsta->max_amsdu_len ||
1001 	    !ieee80211_is_data_qos(hdr->frame_control) ||
1002 	    !mvmsta->amsdu_enabled)
1003 		return iwl_mvm_tx_tso_segment(skb, 1, netdev_flags, mpdus_skb);
1004 
1005 	/*
1006 	 * Do not build AMSDU for IPv6 with extension headers.
1007 	 * ask stack to segment and checkum the generated MPDUs for us.
1008 	 */
1009 	if (skb->protocol == htons(ETH_P_IPV6) &&
1010 	    ((struct ipv6hdr *)skb_network_header(skb))->nexthdr !=
1011 	    IPPROTO_TCP) {
1012 		netdev_flags &= ~NETIF_F_CSUM_MASK;
1013 		return iwl_mvm_tx_tso_segment(skb, 1, netdev_flags, mpdus_skb);
1014 	}
1015 
1016 	tid = ieee80211_get_tid(hdr);
1017 	if (WARN_ON_ONCE(tid >= IWL_MAX_TID_COUNT))
1018 		return -EINVAL;
1019 
1020 	/*
1021 	 * No need to lock amsdu_in_ampdu_allowed since it can't be modified
1022 	 * during an BA session.
1023 	 */
1024 	if ((info->flags & IEEE80211_TX_CTL_AMPDU &&
1025 	     !mvmsta->tid_data[tid].amsdu_in_ampdu_allowed) ||
1026 	    !(mvmsta->amsdu_enabled & BIT(tid)))
1027 		return iwl_mvm_tx_tso_segment(skb, 1, netdev_flags, mpdus_skb);
1028 
1029 	/*
1030 	 * Take the min of ieee80211 station and mvm station
1031 	 */
1032 	max_amsdu_len =
1033 		min_t(unsigned int, sta->cur->max_amsdu_len,
1034 		      iwl_mvm_max_amsdu_size(mvm, sta, tid));
1035 
1036 	/*
1037 	 * Limit A-MSDU in A-MPDU to 4095 bytes when VHT is not
1038 	 * supported. This is a spec requirement (IEEE 802.11-2015
1039 	 * section 8.7.3 NOTE 3).
1040 	 */
1041 	if (info->flags & IEEE80211_TX_CTL_AMPDU &&
1042 	    !sta->deflink.vht_cap.vht_supported)
1043 		max_amsdu_len = min_t(unsigned int, max_amsdu_len, 4095);
1044 
1045 	/* Sub frame header + SNAP + IP header + TCP header + MSS */
1046 	subf_len = sizeof(struct ethhdr) + snap_ip_tcp + mss;
1047 	pad = (4 - subf_len) & 0x3;
1048 
1049 	/*
1050 	 * If we have N subframes in the A-MSDU, then the A-MSDU's size is
1051 	 * N * subf_len + (N - 1) * pad.
1052 	 */
1053 	num_subframes = (max_amsdu_len + pad) / (subf_len + pad);
1054 
1055 	if (sta->max_amsdu_subframes &&
1056 	    num_subframes > sta->max_amsdu_subframes)
1057 		num_subframes = sta->max_amsdu_subframes;
1058 
1059 	tcp_payload_len = skb_tail_pointer(skb) - skb_transport_header(skb) -
1060 		tcp_hdrlen(skb) + skb->data_len;
1061 
1062 	/*
1063 	 * Make sure we have enough TBs for the A-MSDU:
1064 	 *	2 for each subframe
1065 	 *	1 more for each fragment
1066 	 *	1 more for the potential data in the header
1067 	 */
1068 	if ((num_subframes * 2 + skb_shinfo(skb)->nr_frags + 1) >
1069 	    mvm->trans->max_skb_frags)
1070 		num_subframes = 1;
1071 
1072 	if (num_subframes > 1)
1073 		*ieee80211_get_qos_ctl(hdr) |= IEEE80211_QOS_CTL_A_MSDU_PRESENT;
1074 
1075 	/* This skb fits in one single A-MSDU */
1076 	if (num_subframes * mss >= tcp_payload_len) {
1077 		__skb_queue_tail(mpdus_skb, skb);
1078 		return 0;
1079 	}
1080 
1081 	/*
1082 	 * Trick the segmentation function to make it
1083 	 * create SKBs that can fit into one A-MSDU.
1084 	 */
1085 	return iwl_mvm_tx_tso_segment(skb, num_subframes, netdev_flags,
1086 				      mpdus_skb);
1087 }
1088 #else /* CONFIG_INET */
1089 static int iwl_mvm_tx_tso(struct iwl_mvm *mvm, struct sk_buff *skb,
1090 			  struct ieee80211_tx_info *info,
1091 			  struct ieee80211_sta *sta,
1092 			  struct sk_buff_head *mpdus_skb)
1093 {
1094 	/* Impossible to get TSO with CONFIG_INET */
1095 	WARN_ON(1);
1096 
1097 	return -1;
1098 }
1099 #endif
1100 
1101 /* Check if there are any timed-out TIDs on a given shared TXQ */
1102 static bool iwl_mvm_txq_should_update(struct iwl_mvm *mvm, int txq_id)
1103 {
1104 	unsigned long queue_tid_bitmap = mvm->queue_info[txq_id].tid_bitmap;
1105 	unsigned long now = jiffies;
1106 	int tid;
1107 
1108 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
1109 		return false;
1110 
1111 	for_each_set_bit(tid, &queue_tid_bitmap, IWL_MAX_TID_COUNT + 1) {
1112 		if (time_before(mvm->queue_info[txq_id].last_frame_time[tid] +
1113 				IWL_MVM_DQA_QUEUE_TIMEOUT, now))
1114 			return true;
1115 	}
1116 
1117 	return false;
1118 }
1119 
1120 static void iwl_mvm_tx_airtime(struct iwl_mvm *mvm,
1121 			       struct iwl_mvm_sta *mvmsta,
1122 			       int airtime)
1123 {
1124 	int mac = mvmsta->mac_id_n_color & FW_CTXT_ID_MSK;
1125 	struct iwl_mvm_tcm_mac *mdata;
1126 
1127 	if (mac >= NUM_MAC_INDEX_DRIVER)
1128 		return;
1129 
1130 	mdata = &mvm->tcm.data[mac];
1131 
1132 	if (mvm->tcm.paused)
1133 		return;
1134 
1135 	if (time_after(jiffies, mvm->tcm.ts + MVM_TCM_PERIOD))
1136 		schedule_delayed_work(&mvm->tcm.work, 0);
1137 
1138 	mdata->tx.airtime += airtime;
1139 }
1140 
1141 static int iwl_mvm_tx_pkt_queued(struct iwl_mvm *mvm,
1142 				 struct iwl_mvm_sta *mvmsta, int tid)
1143 {
1144 	u32 ac = tid_to_mac80211_ac[tid];
1145 	int mac = mvmsta->mac_id_n_color & FW_CTXT_ID_MSK;
1146 	struct iwl_mvm_tcm_mac *mdata;
1147 
1148 	if (mac >= NUM_MAC_INDEX_DRIVER)
1149 		return -EINVAL;
1150 
1151 	mdata = &mvm->tcm.data[mac];
1152 
1153 	mdata->tx.pkts[ac]++;
1154 
1155 	return 0;
1156 }
1157 
1158 /*
1159  * Sets the fields in the Tx cmd that are crypto related.
1160  *
1161  * This function must be called with BHs disabled.
1162  */
1163 static int iwl_mvm_tx_mpdu(struct iwl_mvm *mvm, struct sk_buff *skb,
1164 			   struct ieee80211_tx_info *info,
1165 			   struct ieee80211_sta *sta)
1166 {
1167 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1168 	struct iwl_mvm_sta *mvmsta;
1169 	struct iwl_device_tx_cmd *dev_cmd;
1170 	__le16 fc;
1171 	u16 seq_number = 0;
1172 	u8 tid = IWL_MAX_TID_COUNT;
1173 	u16 txq_id;
1174 	bool is_ampdu = false;
1175 	int hdrlen;
1176 
1177 	mvmsta = iwl_mvm_sta_from_mac80211(sta);
1178 	fc = hdr->frame_control;
1179 	hdrlen = ieee80211_hdrlen(fc);
1180 
1181 	if (IWL_MVM_NON_TRANSMITTING_AP && ieee80211_is_probe_resp(fc))
1182 		return -1;
1183 
1184 	if (WARN_ON_ONCE(!mvmsta))
1185 		return -1;
1186 
1187 	if (WARN_ON_ONCE(mvmsta->deflink.sta_id == IWL_MVM_INVALID_STA))
1188 		return -1;
1189 
1190 	if (unlikely(ieee80211_is_any_nullfunc(fc)) && sta->deflink.he_cap.has_he)
1191 		return -1;
1192 
1193 	if (unlikely(ieee80211_is_probe_resp(fc)))
1194 		iwl_mvm_probe_resp_set_noa(mvm, skb);
1195 
1196 	dev_cmd = iwl_mvm_set_tx_params(mvm, skb, info, hdrlen,
1197 					sta, mvmsta->deflink.sta_id);
1198 	if (!dev_cmd)
1199 		goto drop;
1200 
1201 	/*
1202 	 * we handle that entirely ourselves -- for uAPSD the firmware
1203 	 * will always send a notification, and for PS-Poll responses
1204 	 * we'll notify mac80211 when getting frame status
1205 	 */
1206 	info->flags &= ~IEEE80211_TX_STATUS_EOSP;
1207 
1208 	spin_lock(&mvmsta->lock);
1209 
1210 	/* nullfunc frames should go to the MGMT queue regardless of QOS,
1211 	 * the conditions of !ieee80211_is_qos_nullfunc(fc) and
1212 	 * !ieee80211_is_data_qos(fc) keep the default assignment of MGMT TID
1213 	 */
1214 	if (ieee80211_is_data_qos(fc) && !ieee80211_is_qos_nullfunc(fc)) {
1215 		tid = ieee80211_get_tid(hdr);
1216 		if (WARN_ONCE(tid >= IWL_MAX_TID_COUNT, "Invalid TID %d", tid))
1217 			goto drop_unlock_sta;
1218 
1219 		is_ampdu = info->flags & IEEE80211_TX_CTL_AMPDU;
1220 		if (WARN_ONCE(is_ampdu &&
1221 			      mvmsta->tid_data[tid].state != IWL_AGG_ON,
1222 			      "Invalid internal agg state %d for TID %d",
1223 			       mvmsta->tid_data[tid].state, tid))
1224 			goto drop_unlock_sta;
1225 
1226 		seq_number = mvmsta->tid_data[tid].seq_number;
1227 		seq_number &= IEEE80211_SCTL_SEQ;
1228 
1229 		if (!iwl_mvm_has_new_tx_api(mvm)) {
1230 			struct iwl_tx_cmd *tx_cmd = (void *)dev_cmd->payload;
1231 
1232 			hdr->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
1233 			hdr->seq_ctrl |= cpu_to_le16(seq_number);
1234 			/* update the tx_cmd hdr as it was already copied */
1235 			tx_cmd->hdr->seq_ctrl = hdr->seq_ctrl;
1236 		}
1237 	} else if (ieee80211_is_data(fc) && !ieee80211_is_data_qos(fc) &&
1238 		   !ieee80211_is_nullfunc(fc)) {
1239 		tid = IWL_TID_NON_QOS;
1240 	}
1241 
1242 	txq_id = mvmsta->tid_data[tid].txq_id;
1243 
1244 	WARN_ON_ONCE(info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM);
1245 
1246 	if (WARN_ONCE(txq_id == IWL_MVM_INVALID_QUEUE, "Invalid TXQ id")) {
1247 		iwl_trans_free_tx_cmd(mvm->trans, dev_cmd);
1248 		spin_unlock(&mvmsta->lock);
1249 		return -1;
1250 	}
1251 
1252 	if (!iwl_mvm_has_new_tx_api(mvm)) {
1253 		/* Keep track of the time of the last frame for this RA/TID */
1254 		mvm->queue_info[txq_id].last_frame_time[tid] = jiffies;
1255 
1256 		/*
1257 		 * If we have timed-out TIDs - schedule the worker that will
1258 		 * reconfig the queues and update them
1259 		 *
1260 		 * Note that the no lock is taken here in order to not serialize
1261 		 * the TX flow. This isn't dangerous because scheduling
1262 		 * mvm->add_stream_wk can't ruin the state, and if we DON'T
1263 		 * schedule it due to some race condition then next TX we get
1264 		 * here we will.
1265 		 */
1266 		if (unlikely(mvm->queue_info[txq_id].status ==
1267 			     IWL_MVM_QUEUE_SHARED &&
1268 			     iwl_mvm_txq_should_update(mvm, txq_id)))
1269 			schedule_work(&mvm->add_stream_wk);
1270 	}
1271 
1272 	IWL_DEBUG_TX(mvm, "TX to [%d|%d] Q:%d - seq: 0x%x len %d\n",
1273 		     mvmsta->deflink.sta_id, tid, txq_id,
1274 		     IEEE80211_SEQ_TO_SN(seq_number), skb->len);
1275 
1276 	/* From now on, we cannot access info->control */
1277 	iwl_mvm_skb_prepare_status(skb, dev_cmd);
1278 
1279 	/*
1280 	 * The IV is introduced by the HW for new tx api, and it is not present
1281 	 * in the skb, hence, don't tell iwl_mvm_mei_tx_copy_to_csme about the
1282 	 * IV for those devices.
1283 	 */
1284 	if (ieee80211_is_data(fc))
1285 		iwl_mvm_mei_tx_copy_to_csme(mvm, skb,
1286 					    info->control.hw_key &&
1287 					    !iwl_mvm_has_new_tx_api(mvm) ?
1288 					    info->control.hw_key->iv_len : 0);
1289 
1290 	if (iwl_trans_tx(mvm->trans, skb, dev_cmd, txq_id))
1291 		goto drop_unlock_sta;
1292 
1293 	if (tid < IWL_MAX_TID_COUNT && !ieee80211_has_morefrags(fc))
1294 		mvmsta->tid_data[tid].seq_number = seq_number + 0x10;
1295 
1296 	spin_unlock(&mvmsta->lock);
1297 
1298 	if (iwl_mvm_tx_pkt_queued(mvm, mvmsta,
1299 				  tid == IWL_MAX_TID_COUNT ? 0 : tid))
1300 		goto drop;
1301 
1302 	return 0;
1303 
1304 drop_unlock_sta:
1305 	iwl_trans_free_tx_cmd(mvm->trans, dev_cmd);
1306 	spin_unlock(&mvmsta->lock);
1307 drop:
1308 	IWL_DEBUG_TX(mvm, "TX to [%d|%d] dropped\n", mvmsta->deflink.sta_id,
1309 		     tid);
1310 	return -1;
1311 }
1312 
1313 int iwl_mvm_tx_skb_sta(struct iwl_mvm *mvm, struct sk_buff *skb,
1314 		       struct ieee80211_sta *sta)
1315 {
1316 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1317 	struct ieee80211_tx_info info;
1318 	struct sk_buff_head mpdus_skbs;
1319 	unsigned int payload_len;
1320 	int ret;
1321 	struct sk_buff *orig_skb = skb;
1322 
1323 	if (WARN_ON_ONCE(!mvmsta))
1324 		return -1;
1325 
1326 	if (WARN_ON_ONCE(mvmsta->deflink.sta_id == IWL_MVM_INVALID_STA))
1327 		return -1;
1328 
1329 	memcpy(&info, skb->cb, sizeof(info));
1330 
1331 	if (!skb_is_gso(skb))
1332 		return iwl_mvm_tx_mpdu(mvm, skb, &info, sta);
1333 
1334 	payload_len = skb_tail_pointer(skb) - skb_transport_header(skb) -
1335 		tcp_hdrlen(skb) + skb->data_len;
1336 
1337 	if (payload_len <= skb_shinfo(skb)->gso_size)
1338 		return iwl_mvm_tx_mpdu(mvm, skb, &info, sta);
1339 
1340 	__skb_queue_head_init(&mpdus_skbs);
1341 
1342 	ret = iwl_mvm_tx_tso(mvm, skb, &info, sta, &mpdus_skbs);
1343 	if (ret)
1344 		return ret;
1345 
1346 	WARN_ON(skb_queue_empty(&mpdus_skbs));
1347 
1348 	while (!skb_queue_empty(&mpdus_skbs)) {
1349 		skb = __skb_dequeue(&mpdus_skbs);
1350 
1351 		ret = iwl_mvm_tx_mpdu(mvm, skb, &info, sta);
1352 		if (ret) {
1353 			/* Free skbs created as part of TSO logic that have not yet been dequeued */
1354 			__skb_queue_purge(&mpdus_skbs);
1355 			/* skb here is not necessarily same as skb that entered this method,
1356 			 * so free it explicitly.
1357 			 */
1358 			if (skb == orig_skb)
1359 				ieee80211_free_txskb(mvm->hw, skb);
1360 			else
1361 				kfree_skb(skb);
1362 			/* there was error, but we consumed skb one way or another, so return 0 */
1363 			return 0;
1364 		}
1365 	}
1366 
1367 	return 0;
1368 }
1369 
1370 static void iwl_mvm_check_ratid_empty(struct iwl_mvm *mvm,
1371 				      struct ieee80211_sta *sta, u8 tid)
1372 {
1373 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1374 	struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
1375 	struct ieee80211_vif *vif = mvmsta->vif;
1376 	u16 normalized_ssn;
1377 
1378 	lockdep_assert_held(&mvmsta->lock);
1379 
1380 	if ((tid_data->state == IWL_AGG_ON ||
1381 	     tid_data->state == IWL_EMPTYING_HW_QUEUE_DELBA) &&
1382 	    iwl_mvm_tid_queued(mvm, tid_data) == 0) {
1383 		/*
1384 		 * Now that this aggregation or DQA queue is empty tell
1385 		 * mac80211 so it knows we no longer have frames buffered for
1386 		 * the station on this TID (for the TIM bitmap calculation.)
1387 		 */
1388 		ieee80211_sta_set_buffered(sta, tid, false);
1389 	}
1390 
1391 	/*
1392 	 * In 22000 HW, the next_reclaimed index is only 8 bit, so we'll need
1393 	 * to align the wrap around of ssn so we compare relevant values.
1394 	 */
1395 	normalized_ssn = tid_data->ssn;
1396 	if (mvm->trans->trans_cfg->gen2)
1397 		normalized_ssn &= 0xff;
1398 
1399 	if (normalized_ssn != tid_data->next_reclaimed)
1400 		return;
1401 
1402 	switch (tid_data->state) {
1403 	case IWL_EMPTYING_HW_QUEUE_ADDBA:
1404 		IWL_DEBUG_TX_QUEUES(mvm,
1405 				    "Can continue addBA flow ssn = next_recl = %d\n",
1406 				    tid_data->next_reclaimed);
1407 		tid_data->state = IWL_AGG_STARTING;
1408 		ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1409 		break;
1410 
1411 	case IWL_EMPTYING_HW_QUEUE_DELBA:
1412 		IWL_DEBUG_TX_QUEUES(mvm,
1413 				    "Can continue DELBA flow ssn = next_recl = %d\n",
1414 				    tid_data->next_reclaimed);
1415 		tid_data->state = IWL_AGG_OFF;
1416 		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1417 		break;
1418 
1419 	default:
1420 		break;
1421 	}
1422 }
1423 
1424 #ifdef CONFIG_IWLWIFI_DEBUG
1425 const char *iwl_mvm_get_tx_fail_reason(u32 status)
1426 {
1427 #define TX_STATUS_FAIL(x) case TX_STATUS_FAIL_ ## x: return #x
1428 #define TX_STATUS_POSTPONE(x) case TX_STATUS_POSTPONE_ ## x: return #x
1429 
1430 	switch (status & TX_STATUS_MSK) {
1431 	case TX_STATUS_SUCCESS:
1432 		return "SUCCESS";
1433 	TX_STATUS_POSTPONE(DELAY);
1434 	TX_STATUS_POSTPONE(FEW_BYTES);
1435 	TX_STATUS_POSTPONE(BT_PRIO);
1436 	TX_STATUS_POSTPONE(QUIET_PERIOD);
1437 	TX_STATUS_POSTPONE(CALC_TTAK);
1438 	TX_STATUS_FAIL(INTERNAL_CROSSED_RETRY);
1439 	TX_STATUS_FAIL(SHORT_LIMIT);
1440 	TX_STATUS_FAIL(LONG_LIMIT);
1441 	TX_STATUS_FAIL(UNDERRUN);
1442 	TX_STATUS_FAIL(DRAIN_FLOW);
1443 	TX_STATUS_FAIL(RFKILL_FLUSH);
1444 	TX_STATUS_FAIL(LIFE_EXPIRE);
1445 	TX_STATUS_FAIL(DEST_PS);
1446 	TX_STATUS_FAIL(HOST_ABORTED);
1447 	TX_STATUS_FAIL(BT_RETRY);
1448 	TX_STATUS_FAIL(STA_INVALID);
1449 	TX_STATUS_FAIL(FRAG_DROPPED);
1450 	TX_STATUS_FAIL(TID_DISABLE);
1451 	TX_STATUS_FAIL(FIFO_FLUSHED);
1452 	TX_STATUS_FAIL(SMALL_CF_POLL);
1453 	TX_STATUS_FAIL(FW_DROP);
1454 	TX_STATUS_FAIL(STA_COLOR_MISMATCH);
1455 	}
1456 
1457 	return "UNKNOWN";
1458 
1459 #undef TX_STATUS_FAIL
1460 #undef TX_STATUS_POSTPONE
1461 }
1462 #endif /* CONFIG_IWLWIFI_DEBUG */
1463 
1464 static int iwl_mvm_get_hwrate_chan_width(u32 chan_width)
1465 {
1466 	switch (chan_width) {
1467 	case RATE_MCS_CHAN_WIDTH_20:
1468 		return 0;
1469 	case RATE_MCS_CHAN_WIDTH_40:
1470 		return IEEE80211_TX_RC_40_MHZ_WIDTH;
1471 	case RATE_MCS_CHAN_WIDTH_80:
1472 		return IEEE80211_TX_RC_80_MHZ_WIDTH;
1473 	case RATE_MCS_CHAN_WIDTH_160:
1474 		return IEEE80211_TX_RC_160_MHZ_WIDTH;
1475 	default:
1476 		return 0;
1477 	}
1478 }
1479 
1480 void iwl_mvm_hwrate_to_tx_rate(u32 rate_n_flags,
1481 			       enum nl80211_band band,
1482 			       struct ieee80211_tx_rate *r)
1483 {
1484 	u32 format = rate_n_flags & RATE_MCS_MOD_TYPE_MSK;
1485 	u32 rate = format == RATE_MCS_HT_MSK ?
1486 		RATE_HT_MCS_INDEX(rate_n_flags) :
1487 		rate_n_flags & RATE_MCS_CODE_MSK;
1488 
1489 	r->flags |=
1490 		iwl_mvm_get_hwrate_chan_width(rate_n_flags &
1491 					      RATE_MCS_CHAN_WIDTH_MSK);
1492 
1493 	if (rate_n_flags & RATE_MCS_SGI_MSK)
1494 		r->flags |= IEEE80211_TX_RC_SHORT_GI;
1495 	if (format ==  RATE_MCS_HT_MSK) {
1496 		r->flags |= IEEE80211_TX_RC_MCS;
1497 		r->idx = rate;
1498 	} else if (format ==  RATE_MCS_VHT_MSK) {
1499 		ieee80211_rate_set_vht(r, rate,
1500 				       FIELD_GET(RATE_MCS_NSS_MSK,
1501 						 rate_n_flags) + 1);
1502 		r->flags |= IEEE80211_TX_RC_VHT_MCS;
1503 	} else if (format == RATE_MCS_HE_MSK) {
1504 		/* mac80211 cannot do this without ieee80211_tx_status_ext()
1505 		 * but it only matters for radiotap */
1506 		r->idx = 0;
1507 	} else {
1508 		r->idx = iwl_mvm_legacy_hw_idx_to_mac80211_idx(rate_n_flags,
1509 							       band);
1510 	}
1511 }
1512 
1513 void iwl_mvm_hwrate_to_tx_rate_v1(u32 rate_n_flags,
1514 				  enum nl80211_band band,
1515 				  struct ieee80211_tx_rate *r)
1516 {
1517 	if (rate_n_flags & RATE_HT_MCS_GF_MSK)
1518 		r->flags |= IEEE80211_TX_RC_GREEN_FIELD;
1519 
1520 	r->flags |=
1521 		iwl_mvm_get_hwrate_chan_width(rate_n_flags &
1522 					      RATE_MCS_CHAN_WIDTH_MSK_V1);
1523 
1524 	if (rate_n_flags & RATE_MCS_SGI_MSK_V1)
1525 		r->flags |= IEEE80211_TX_RC_SHORT_GI;
1526 	if (rate_n_flags & RATE_MCS_HT_MSK_V1) {
1527 		r->flags |= IEEE80211_TX_RC_MCS;
1528 		r->idx = rate_n_flags & RATE_HT_MCS_INDEX_MSK_V1;
1529 	} else if (rate_n_flags & RATE_MCS_VHT_MSK_V1) {
1530 		ieee80211_rate_set_vht(
1531 			r, rate_n_flags & RATE_VHT_MCS_RATE_CODE_MSK,
1532 			FIELD_GET(RATE_MCS_NSS_MSK, rate_n_flags) + 1);
1533 		r->flags |= IEEE80211_TX_RC_VHT_MCS;
1534 	} else {
1535 		r->idx = iwl_mvm_legacy_rate_to_mac80211_idx(rate_n_flags,
1536 							     band);
1537 	}
1538 }
1539 
1540 /*
1541  * translate ucode response to mac80211 tx status control values
1542  */
1543 static void iwl_mvm_hwrate_to_tx_status(const struct iwl_fw *fw,
1544 					u32 rate_n_flags,
1545 					struct ieee80211_tx_info *info)
1546 {
1547 	struct ieee80211_tx_rate *r = &info->status.rates[0];
1548 
1549 	if (iwl_fw_lookup_notif_ver(fw, LONG_GROUP,
1550 				    TX_CMD, 0) <= 6)
1551 		rate_n_flags = iwl_new_rate_from_v1(rate_n_flags);
1552 
1553 	info->status.antenna =
1554 		((rate_n_flags & RATE_MCS_ANT_AB_MSK) >> RATE_MCS_ANT_POS);
1555 	iwl_mvm_hwrate_to_tx_rate(rate_n_flags,
1556 				  info->band, r);
1557 }
1558 
1559 static void iwl_mvm_tx_status_check_trigger(struct iwl_mvm *mvm,
1560 					    u32 status, __le16 frame_control)
1561 {
1562 	struct iwl_fw_dbg_trigger_tlv *trig;
1563 	struct iwl_fw_dbg_trigger_tx_status *status_trig;
1564 	int i;
1565 
1566 	if ((status & TX_STATUS_MSK) != TX_STATUS_SUCCESS) {
1567 		enum iwl_fw_ini_time_point tp =
1568 			IWL_FW_INI_TIME_POINT_TX_FAILED;
1569 
1570 		if (ieee80211_is_action(frame_control))
1571 			tp = IWL_FW_INI_TIME_POINT_TX_WFD_ACTION_FRAME_FAILED;
1572 
1573 		iwl_dbg_tlv_time_point(&mvm->fwrt,
1574 				       tp, NULL);
1575 		return;
1576 	}
1577 
1578 	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, NULL,
1579 				     FW_DBG_TRIGGER_TX_STATUS);
1580 	if (!trig)
1581 		return;
1582 
1583 	status_trig = (void *)trig->data;
1584 
1585 	for (i = 0; i < ARRAY_SIZE(status_trig->statuses); i++) {
1586 		/* don't collect on status 0 */
1587 		if (!status_trig->statuses[i].status)
1588 			break;
1589 
1590 		if (status_trig->statuses[i].status != (status & TX_STATUS_MSK))
1591 			continue;
1592 
1593 		iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
1594 					"Tx status %d was received",
1595 					status & TX_STATUS_MSK);
1596 		break;
1597 	}
1598 }
1599 
1600 /*
1601  * iwl_mvm_get_scd_ssn - returns the SSN of the SCD
1602  * @tx_resp: the Tx response from the fw (agg or non-agg)
1603  *
1604  * When the fw sends an AMPDU, it fetches the MPDUs one after the other. Since
1605  * it can't know that everything will go well until the end of the AMPDU, it
1606  * can't know in advance the number of MPDUs that will be sent in the current
1607  * batch. This is why it writes the agg Tx response while it fetches the MPDUs.
1608  * Hence, it can't know in advance what the SSN of the SCD will be at the end
1609  * of the batch. This is why the SSN of the SCD is written at the end of the
1610  * whole struct at a variable offset. This function knows how to cope with the
1611  * variable offset and returns the SSN of the SCD.
1612  */
1613 static inline u32 iwl_mvm_get_scd_ssn(struct iwl_mvm *mvm,
1614 				      struct iwl_mvm_tx_resp *tx_resp)
1615 {
1616 	return le32_to_cpup((__le32 *)iwl_mvm_get_agg_status(mvm, tx_resp) +
1617 			    tx_resp->frame_count) & 0xfff;
1618 }
1619 
1620 static void iwl_mvm_rx_tx_cmd_single(struct iwl_mvm *mvm,
1621 				     struct iwl_rx_packet *pkt)
1622 {
1623 	struct ieee80211_sta *sta;
1624 	u16 sequence = le16_to_cpu(pkt->hdr.sequence);
1625 	int txq_id = SEQ_TO_QUEUE(sequence);
1626 	/* struct iwl_mvm_tx_resp_v3 is almost the same */
1627 	struct iwl_mvm_tx_resp *tx_resp = (void *)pkt->data;
1628 	int sta_id = IWL_MVM_TX_RES_GET_RA(tx_resp->ra_tid);
1629 	int tid = IWL_MVM_TX_RES_GET_TID(tx_resp->ra_tid);
1630 	struct agg_tx_status *agg_status =
1631 		iwl_mvm_get_agg_status(mvm, tx_resp);
1632 	u32 status = le16_to_cpu(agg_status->status);
1633 	u16 ssn = iwl_mvm_get_scd_ssn(mvm, tx_resp);
1634 	struct sk_buff_head skbs;
1635 	u8 skb_freed = 0;
1636 	u8 lq_color;
1637 	u16 next_reclaimed, seq_ctl;
1638 	bool is_ndp = false;
1639 
1640 	__skb_queue_head_init(&skbs);
1641 
1642 	if (iwl_mvm_has_new_tx_api(mvm))
1643 		txq_id = le16_to_cpu(tx_resp->tx_queue);
1644 
1645 	seq_ctl = le16_to_cpu(tx_resp->seq_ctl);
1646 
1647 	/* we can free until ssn % q.n_bd not inclusive */
1648 	iwl_trans_reclaim(mvm->trans, txq_id, ssn, &skbs);
1649 
1650 	while (!skb_queue_empty(&skbs)) {
1651 		struct sk_buff *skb = __skb_dequeue(&skbs);
1652 		struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1653 		struct ieee80211_hdr *hdr = (void *)skb->data;
1654 		bool flushed = false;
1655 
1656 		skb_freed++;
1657 
1658 		iwl_trans_free_tx_cmd(mvm->trans, info->driver_data[1]);
1659 
1660 		memset(&info->status, 0, sizeof(info->status));
1661 
1662 		/* inform mac80211 about what happened with the frame */
1663 		switch (status & TX_STATUS_MSK) {
1664 		case TX_STATUS_SUCCESS:
1665 		case TX_STATUS_DIRECT_DONE:
1666 			info->flags |= IEEE80211_TX_STAT_ACK;
1667 			break;
1668 		case TX_STATUS_FAIL_FIFO_FLUSHED:
1669 		case TX_STATUS_FAIL_DRAIN_FLOW:
1670 			flushed = true;
1671 			break;
1672 		case TX_STATUS_FAIL_DEST_PS:
1673 			/* the FW should have stopped the queue and not
1674 			 * return this status
1675 			 */
1676 			IWL_ERR_LIMIT(mvm,
1677 				      "FW reported TX filtered, status=0x%x, FC=0x%x\n",
1678 				      status, le16_to_cpu(hdr->frame_control));
1679 			info->flags |= IEEE80211_TX_STAT_TX_FILTERED;
1680 			break;
1681 		default:
1682 			break;
1683 		}
1684 
1685 		if ((status & TX_STATUS_MSK) != TX_STATUS_SUCCESS &&
1686 		    ieee80211_is_mgmt(hdr->frame_control))
1687 			iwl_mvm_toggle_tx_ant(mvm, &mvm->mgmt_last_antenna_idx);
1688 
1689 		/*
1690 		 * If we are freeing multiple frames, mark all the frames
1691 		 * but the first one as acked, since they were acknowledged
1692 		 * before
1693 		 * */
1694 		if (skb_freed > 1)
1695 			info->flags |= IEEE80211_TX_STAT_ACK;
1696 
1697 		iwl_mvm_tx_status_check_trigger(mvm, status, hdr->frame_control);
1698 
1699 		info->status.rates[0].count = tx_resp->failure_frame + 1;
1700 
1701 		iwl_mvm_hwrate_to_tx_status(mvm->fw,
1702 					    le32_to_cpu(tx_resp->initial_rate),
1703 					    info);
1704 
1705 		/* Don't assign the converted initial_rate, because driver
1706 		 * TLC uses this and doesn't support the new FW rate
1707 		 */
1708 		info->status.status_driver_data[1] =
1709 			(void *)(uintptr_t)le32_to_cpu(tx_resp->initial_rate);
1710 
1711 		/* Single frame failure in an AMPDU queue => send BAR */
1712 		if (info->flags & IEEE80211_TX_CTL_AMPDU &&
1713 		    !(info->flags & IEEE80211_TX_STAT_ACK) &&
1714 		    !(info->flags & IEEE80211_TX_STAT_TX_FILTERED) && !flushed)
1715 			info->flags |= IEEE80211_TX_STAT_AMPDU_NO_BACK;
1716 		info->flags &= ~IEEE80211_TX_CTL_AMPDU;
1717 
1718 		/* W/A FW bug: seq_ctl is wrong upon failure / BAR frame */
1719 		if (ieee80211_is_back_req(hdr->frame_control))
1720 			seq_ctl = 0;
1721 		else if (status != TX_STATUS_SUCCESS)
1722 			seq_ctl = le16_to_cpu(hdr->seq_ctrl);
1723 
1724 		if (unlikely(!seq_ctl)) {
1725 			/*
1726 			 * If it is an NDP, we can't update next_reclaim since
1727 			 * its sequence control is 0. Note that for that same
1728 			 * reason, NDPs are never sent to A-MPDU'able queues
1729 			 * so that we can never have more than one freed frame
1730 			 * for a single Tx resonse (see WARN_ON below).
1731 			 */
1732 			if (ieee80211_is_qos_nullfunc(hdr->frame_control))
1733 				is_ndp = true;
1734 		}
1735 
1736 		/*
1737 		 * TODO: this is not accurate if we are freeing more than one
1738 		 * packet.
1739 		 */
1740 		info->status.tx_time =
1741 			le16_to_cpu(tx_resp->wireless_media_time);
1742 		BUILD_BUG_ON(ARRAY_SIZE(info->status.status_driver_data) < 1);
1743 		lq_color = TX_RES_RATE_TABLE_COL_GET(tx_resp->tlc_info);
1744 		info->status.status_driver_data[0] =
1745 			RS_DRV_DATA_PACK(lq_color, tx_resp->reduced_tpc);
1746 
1747 		if (likely(!iwl_mvm_time_sync_frame(mvm, skb, hdr->addr1)))
1748 			ieee80211_tx_status(mvm->hw, skb);
1749 	}
1750 
1751 	/* This is an aggregation queue or might become one, so we use
1752 	 * the ssn since: ssn = wifi seq_num % 256.
1753 	 * The seq_ctl is the sequence control of the packet to which
1754 	 * this Tx response relates. But if there is a hole in the
1755 	 * bitmap of the BA we received, this Tx response may allow to
1756 	 * reclaim the hole and all the subsequent packets that were
1757 	 * already acked. In that case, seq_ctl != ssn, and the next
1758 	 * packet to be reclaimed will be ssn and not seq_ctl. In that
1759 	 * case, several packets will be reclaimed even if
1760 	 * frame_count = 1.
1761 	 *
1762 	 * The ssn is the index (% 256) of the latest packet that has
1763 	 * treated (acked / dropped) + 1.
1764 	 */
1765 	next_reclaimed = ssn;
1766 
1767 	IWL_DEBUG_TX_REPLY(mvm,
1768 			   "TXQ %d status %s (0x%08x)\n",
1769 			   txq_id, iwl_mvm_get_tx_fail_reason(status), status);
1770 
1771 	IWL_DEBUG_TX_REPLY(mvm,
1772 			   "\t\t\t\tinitial_rate 0x%x retries %d, idx=%d ssn=%d next_reclaimed=0x%x seq_ctl=0x%x\n",
1773 			   le32_to_cpu(tx_resp->initial_rate),
1774 			   tx_resp->failure_frame, SEQ_TO_INDEX(sequence),
1775 			   ssn, next_reclaimed, seq_ctl);
1776 
1777 	rcu_read_lock();
1778 
1779 	sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
1780 	/*
1781 	 * sta can't be NULL otherwise it'd mean that the sta has been freed in
1782 	 * the firmware while we still have packets for it in the Tx queues.
1783 	 */
1784 	if (WARN_ON_ONCE(!sta))
1785 		goto out;
1786 
1787 	if (!IS_ERR(sta)) {
1788 		struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1789 
1790 		iwl_mvm_tx_airtime(mvm, mvmsta,
1791 				   le16_to_cpu(tx_resp->wireless_media_time));
1792 
1793 		if ((status & TX_STATUS_MSK) != TX_STATUS_SUCCESS &&
1794 		    mvmsta->sta_state < IEEE80211_STA_AUTHORIZED)
1795 			iwl_mvm_toggle_tx_ant(mvm, &mvmsta->tx_ant);
1796 
1797 		if (sta->wme && tid != IWL_MGMT_TID) {
1798 			struct iwl_mvm_tid_data *tid_data =
1799 				&mvmsta->tid_data[tid];
1800 			bool send_eosp_ndp = false;
1801 
1802 			spin_lock_bh(&mvmsta->lock);
1803 
1804 			if (!is_ndp) {
1805 				tid_data->next_reclaimed = next_reclaimed;
1806 				IWL_DEBUG_TX_REPLY(mvm,
1807 						   "Next reclaimed packet:%d\n",
1808 						   next_reclaimed);
1809 			} else {
1810 				IWL_DEBUG_TX_REPLY(mvm,
1811 						   "NDP - don't update next_reclaimed\n");
1812 			}
1813 
1814 			iwl_mvm_check_ratid_empty(mvm, sta, tid);
1815 
1816 			if (mvmsta->sleep_tx_count) {
1817 				mvmsta->sleep_tx_count--;
1818 				if (mvmsta->sleep_tx_count &&
1819 				    !iwl_mvm_tid_queued(mvm, tid_data)) {
1820 					/*
1821 					 * The number of frames in the queue
1822 					 * dropped to 0 even if we sent less
1823 					 * frames than we thought we had on the
1824 					 * Tx queue.
1825 					 * This means we had holes in the BA
1826 					 * window that we just filled, ask
1827 					 * mac80211 to send EOSP since the
1828 					 * firmware won't know how to do that.
1829 					 * Send NDP and the firmware will send
1830 					 * EOSP notification that will trigger
1831 					 * a call to ieee80211_sta_eosp().
1832 					 */
1833 					send_eosp_ndp = true;
1834 				}
1835 			}
1836 
1837 			spin_unlock_bh(&mvmsta->lock);
1838 			if (send_eosp_ndp) {
1839 				iwl_mvm_sta_modify_sleep_tx_count(mvm, sta,
1840 					IEEE80211_FRAME_RELEASE_UAPSD,
1841 					1, tid, false, false);
1842 				mvmsta->sleep_tx_count = 0;
1843 				ieee80211_send_eosp_nullfunc(sta, tid);
1844 			}
1845 		}
1846 
1847 		if (mvmsta->next_status_eosp) {
1848 			mvmsta->next_status_eosp = false;
1849 			ieee80211_sta_eosp(sta);
1850 		}
1851 	}
1852 out:
1853 	rcu_read_unlock();
1854 }
1855 
1856 #ifdef CONFIG_IWLWIFI_DEBUG
1857 #define AGG_TX_STATE_(x) case AGG_TX_STATE_ ## x: return #x
1858 static const char *iwl_get_agg_tx_status(u16 status)
1859 {
1860 	switch (status & AGG_TX_STATE_STATUS_MSK) {
1861 	AGG_TX_STATE_(TRANSMITTED);
1862 	AGG_TX_STATE_(UNDERRUN);
1863 	AGG_TX_STATE_(BT_PRIO);
1864 	AGG_TX_STATE_(FEW_BYTES);
1865 	AGG_TX_STATE_(ABORT);
1866 	AGG_TX_STATE_(TX_ON_AIR_DROP);
1867 	AGG_TX_STATE_(LAST_SENT_TRY_CNT);
1868 	AGG_TX_STATE_(LAST_SENT_BT_KILL);
1869 	AGG_TX_STATE_(SCD_QUERY);
1870 	AGG_TX_STATE_(TEST_BAD_CRC32);
1871 	AGG_TX_STATE_(RESPONSE);
1872 	AGG_TX_STATE_(DUMP_TX);
1873 	AGG_TX_STATE_(DELAY_TX);
1874 	}
1875 
1876 	return "UNKNOWN";
1877 }
1878 
1879 static void iwl_mvm_rx_tx_cmd_agg_dbg(struct iwl_mvm *mvm,
1880 				      struct iwl_rx_packet *pkt)
1881 {
1882 	struct iwl_mvm_tx_resp *tx_resp = (void *)pkt->data;
1883 	struct agg_tx_status *frame_status =
1884 		iwl_mvm_get_agg_status(mvm, tx_resp);
1885 	int i;
1886 	bool tirgger_timepoint = false;
1887 
1888 	for (i = 0; i < tx_resp->frame_count; i++) {
1889 		u16 fstatus = le16_to_cpu(frame_status[i].status);
1890 		/* In case one frame wasn't transmitted trigger time point */
1891 		tirgger_timepoint |= ((fstatus & AGG_TX_STATE_STATUS_MSK) !=
1892 				      AGG_TX_STATE_TRANSMITTED);
1893 		IWL_DEBUG_TX_REPLY(mvm,
1894 				   "status %s (0x%04x), try-count (%d) seq (0x%x)\n",
1895 				   iwl_get_agg_tx_status(fstatus),
1896 				   fstatus & AGG_TX_STATE_STATUS_MSK,
1897 				   (fstatus & AGG_TX_STATE_TRY_CNT_MSK) >>
1898 					AGG_TX_STATE_TRY_CNT_POS,
1899 				   le16_to_cpu(frame_status[i].sequence));
1900 	}
1901 
1902 	if (tirgger_timepoint)
1903 		iwl_dbg_tlv_time_point(&mvm->fwrt,
1904 				       IWL_FW_INI_TIME_POINT_TX_FAILED, NULL);
1905 
1906 }
1907 #else
1908 static void iwl_mvm_rx_tx_cmd_agg_dbg(struct iwl_mvm *mvm,
1909 				      struct iwl_rx_packet *pkt)
1910 {}
1911 #endif /* CONFIG_IWLWIFI_DEBUG */
1912 
1913 static void iwl_mvm_rx_tx_cmd_agg(struct iwl_mvm *mvm,
1914 				  struct iwl_rx_packet *pkt)
1915 {
1916 	struct iwl_mvm_tx_resp *tx_resp = (void *)pkt->data;
1917 	int sta_id = IWL_MVM_TX_RES_GET_RA(tx_resp->ra_tid);
1918 	int tid = IWL_MVM_TX_RES_GET_TID(tx_resp->ra_tid);
1919 	u16 sequence = le16_to_cpu(pkt->hdr.sequence);
1920 	struct iwl_mvm_sta *mvmsta;
1921 	int queue = SEQ_TO_QUEUE(sequence);
1922 	struct ieee80211_sta *sta;
1923 
1924 	if (WARN_ON_ONCE(queue < IWL_MVM_DQA_MIN_DATA_QUEUE &&
1925 			 (queue != IWL_MVM_DQA_BSS_CLIENT_QUEUE)))
1926 		return;
1927 
1928 	iwl_mvm_rx_tx_cmd_agg_dbg(mvm, pkt);
1929 
1930 	rcu_read_lock();
1931 
1932 	mvmsta = iwl_mvm_sta_from_staid_rcu(mvm, sta_id);
1933 
1934 	sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
1935 	if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta) || !sta->wme)) {
1936 		rcu_read_unlock();
1937 		return;
1938 	}
1939 
1940 	if (!WARN_ON_ONCE(!mvmsta)) {
1941 		mvmsta->tid_data[tid].rate_n_flags =
1942 			le32_to_cpu(tx_resp->initial_rate);
1943 		mvmsta->tid_data[tid].tx_time =
1944 			le16_to_cpu(tx_resp->wireless_media_time);
1945 		mvmsta->tid_data[tid].lq_color =
1946 			TX_RES_RATE_TABLE_COL_GET(tx_resp->tlc_info);
1947 		iwl_mvm_tx_airtime(mvm, mvmsta,
1948 				   le16_to_cpu(tx_resp->wireless_media_time));
1949 	}
1950 
1951 	rcu_read_unlock();
1952 }
1953 
1954 void iwl_mvm_rx_tx_cmd(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb)
1955 {
1956 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1957 	struct iwl_mvm_tx_resp *tx_resp = (void *)pkt->data;
1958 
1959 	if (tx_resp->frame_count == 1)
1960 		iwl_mvm_rx_tx_cmd_single(mvm, pkt);
1961 	else
1962 		iwl_mvm_rx_tx_cmd_agg(mvm, pkt);
1963 }
1964 
1965 static void iwl_mvm_tx_reclaim(struct iwl_mvm *mvm, int sta_id, int tid,
1966 			       int txq, int index,
1967 			       struct ieee80211_tx_info *tx_info, u32 rate,
1968 			       bool is_flush)
1969 {
1970 	struct sk_buff_head reclaimed_skbs;
1971 	struct iwl_mvm_tid_data *tid_data = NULL;
1972 	struct ieee80211_sta *sta;
1973 	struct iwl_mvm_sta *mvmsta = NULL;
1974 	struct sk_buff *skb;
1975 	int freed;
1976 
1977 	if (WARN_ONCE(sta_id >= mvm->fw->ucode_capa.num_stations ||
1978 		      tid > IWL_MAX_TID_COUNT,
1979 		      "sta_id %d tid %d", sta_id, tid))
1980 		return;
1981 
1982 	rcu_read_lock();
1983 
1984 	sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
1985 
1986 	/* Reclaiming frames for a station that has been deleted ? */
1987 	if (WARN_ON_ONCE(!sta)) {
1988 		rcu_read_unlock();
1989 		return;
1990 	}
1991 
1992 	__skb_queue_head_init(&reclaimed_skbs);
1993 
1994 	/*
1995 	 * Release all TFDs before the SSN, i.e. all TFDs in front of
1996 	 * block-ack window (we assume that they've been successfully
1997 	 * transmitted ... if not, it's too late anyway).
1998 	 */
1999 	iwl_trans_reclaim(mvm->trans, txq, index, &reclaimed_skbs);
2000 
2001 	skb_queue_walk(&reclaimed_skbs, skb) {
2002 		struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2003 
2004 		iwl_trans_free_tx_cmd(mvm->trans, info->driver_data[1]);
2005 
2006 		memset(&info->status, 0, sizeof(info->status));
2007 		/* Packet was transmitted successfully, failures come as single
2008 		 * frames because before failing a frame the firmware transmits
2009 		 * it without aggregation at least once.
2010 		 */
2011 		if (!is_flush)
2012 			info->flags |= IEEE80211_TX_STAT_ACK;
2013 	}
2014 
2015 	/*
2016 	 * It's possible to get a BA response after invalidating the rcu (rcu is
2017 	 * invalidated in order to prevent new Tx from being sent, but there may
2018 	 * be some frames already in-flight).
2019 	 * In this case we just want to reclaim, and could skip all the
2020 	 * sta-dependent stuff since it's in the middle of being removed
2021 	 * anyways.
2022 	 */
2023 	if (IS_ERR(sta))
2024 		goto out;
2025 
2026 	mvmsta = iwl_mvm_sta_from_mac80211(sta);
2027 	tid_data = &mvmsta->tid_data[tid];
2028 
2029 	if (tid_data->txq_id != txq) {
2030 		IWL_ERR(mvm,
2031 			"invalid reclaim request: Q %d, tid %d\n",
2032 			tid_data->txq_id, tid);
2033 		rcu_read_unlock();
2034 		return;
2035 	}
2036 
2037 	spin_lock_bh(&mvmsta->lock);
2038 
2039 	tid_data->next_reclaimed = index;
2040 
2041 	iwl_mvm_check_ratid_empty(mvm, sta, tid);
2042 
2043 	freed = 0;
2044 
2045 	/* pack lq color from tid_data along the reduced txp */
2046 	tx_info->status.status_driver_data[0] =
2047 		RS_DRV_DATA_PACK(tid_data->lq_color,
2048 				 tx_info->status.status_driver_data[0]);
2049 	tx_info->status.status_driver_data[1] = (void *)(uintptr_t)rate;
2050 
2051 	skb_queue_walk(&reclaimed_skbs, skb) {
2052 		struct ieee80211_hdr *hdr = (void *)skb->data;
2053 		struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2054 
2055 		if (!is_flush) {
2056 			if (ieee80211_is_data_qos(hdr->frame_control))
2057 				freed++;
2058 			else
2059 				WARN_ON_ONCE(tid != IWL_MAX_TID_COUNT);
2060 		}
2061 
2062 		/* this is the first skb we deliver in this batch */
2063 		/* put the rate scaling data there */
2064 		if (freed == 1) {
2065 			info->flags |= IEEE80211_TX_STAT_AMPDU;
2066 			memcpy(&info->status, &tx_info->status,
2067 			       sizeof(tx_info->status));
2068 			iwl_mvm_hwrate_to_tx_status(mvm->fw, rate, info);
2069 		}
2070 	}
2071 
2072 	spin_unlock_bh(&mvmsta->lock);
2073 
2074 	/* We got a BA notif with 0 acked or scd_ssn didn't progress which is
2075 	 * possible (i.e. first MPDU in the aggregation wasn't acked)
2076 	 * Still it's important to update RS about sent vs. acked.
2077 	 */
2078 	if (!is_flush && skb_queue_empty(&reclaimed_skbs) &&
2079 	    !iwl_mvm_has_tlc_offload(mvm)) {
2080 		struct ieee80211_chanctx_conf *chanctx_conf = NULL;
2081 
2082 		/* no TLC offload, so non-MLD mode */
2083 		if (mvmsta->vif)
2084 			chanctx_conf =
2085 				rcu_dereference(mvmsta->vif->bss_conf.chanctx_conf);
2086 
2087 		if (WARN_ON_ONCE(!chanctx_conf))
2088 			goto out;
2089 
2090 		tx_info->band = chanctx_conf->def.chan->band;
2091 		iwl_mvm_hwrate_to_tx_status(mvm->fw, rate, tx_info);
2092 
2093 		IWL_DEBUG_TX_REPLY(mvm, "No reclaim. Update rs directly\n");
2094 		iwl_mvm_rs_tx_status(mvm, sta, tid, tx_info, false);
2095 	}
2096 
2097 out:
2098 	rcu_read_unlock();
2099 
2100 	while (!skb_queue_empty(&reclaimed_skbs)) {
2101 		skb = __skb_dequeue(&reclaimed_skbs);
2102 		ieee80211_tx_status(mvm->hw, skb);
2103 	}
2104 }
2105 
2106 void iwl_mvm_rx_ba_notif(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb)
2107 {
2108 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
2109 	unsigned int pkt_len = iwl_rx_packet_payload_len(pkt);
2110 	int sta_id, tid, txq, index;
2111 	struct ieee80211_tx_info ba_info = {};
2112 	struct iwl_mvm_ba_notif *ba_notif;
2113 	struct iwl_mvm_tid_data *tid_data;
2114 	struct iwl_mvm_sta *mvmsta;
2115 
2116 	ba_info.flags = IEEE80211_TX_STAT_AMPDU;
2117 
2118 	if (iwl_mvm_has_new_tx_api(mvm)) {
2119 		struct iwl_mvm_compressed_ba_notif *ba_res =
2120 			(void *)pkt->data;
2121 		u8 lq_color = TX_RES_RATE_TABLE_COL_GET(ba_res->tlc_rate_info);
2122 		u16 tfd_cnt;
2123 		int i;
2124 
2125 		if (unlikely(sizeof(*ba_res) > pkt_len))
2126 			return;
2127 
2128 		sta_id = ba_res->sta_id;
2129 		ba_info.status.ampdu_ack_len = (u8)le16_to_cpu(ba_res->done);
2130 		ba_info.status.ampdu_len = (u8)le16_to_cpu(ba_res->txed);
2131 		ba_info.status.tx_time =
2132 			(u16)le32_to_cpu(ba_res->wireless_time);
2133 		ba_info.status.status_driver_data[0] =
2134 			(void *)(uintptr_t)ba_res->reduced_txp;
2135 
2136 		tfd_cnt = le16_to_cpu(ba_res->tfd_cnt);
2137 		if (!tfd_cnt || struct_size(ba_res, tfd, tfd_cnt) > pkt_len)
2138 			return;
2139 
2140 		rcu_read_lock();
2141 
2142 		mvmsta = iwl_mvm_sta_from_staid_rcu(mvm, sta_id);
2143 		/*
2144 		 * It's possible to get a BA response after invalidating the rcu
2145 		 * (rcu is invalidated in order to prevent new Tx from being
2146 		 * sent, but there may be some frames already in-flight).
2147 		 * In this case we just want to reclaim, and could skip all the
2148 		 * sta-dependent stuff since it's in the middle of being removed
2149 		 * anyways.
2150 		 */
2151 
2152 		/* Free per TID */
2153 		for (i = 0; i < tfd_cnt; i++) {
2154 			struct iwl_mvm_compressed_ba_tfd *ba_tfd =
2155 				&ba_res->tfd[i];
2156 
2157 			tid = ba_tfd->tid;
2158 			if (tid == IWL_MGMT_TID)
2159 				tid = IWL_MAX_TID_COUNT;
2160 
2161 			if (mvmsta)
2162 				mvmsta->tid_data[i].lq_color = lq_color;
2163 
2164 			iwl_mvm_tx_reclaim(mvm, sta_id, tid,
2165 					   (int)(le16_to_cpu(ba_tfd->q_num)),
2166 					   le16_to_cpu(ba_tfd->tfd_index),
2167 					   &ba_info,
2168 					   le32_to_cpu(ba_res->tx_rate), false);
2169 		}
2170 
2171 		if (mvmsta)
2172 			iwl_mvm_tx_airtime(mvm, mvmsta,
2173 					   le32_to_cpu(ba_res->wireless_time));
2174 		rcu_read_unlock();
2175 
2176 		IWL_DEBUG_TX_REPLY(mvm,
2177 				   "BA_NOTIFICATION Received from sta_id = %d, flags %x, sent:%d, acked:%d\n",
2178 				   sta_id, le32_to_cpu(ba_res->flags),
2179 				   le16_to_cpu(ba_res->txed),
2180 				   le16_to_cpu(ba_res->done));
2181 		return;
2182 	}
2183 
2184 	ba_notif = (void *)pkt->data;
2185 	sta_id = ba_notif->sta_id;
2186 	tid = ba_notif->tid;
2187 	/* "flow" corresponds to Tx queue */
2188 	txq = le16_to_cpu(ba_notif->scd_flow);
2189 	/* "ssn" is start of block-ack Tx window, corresponds to index
2190 	 * (in Tx queue's circular buffer) of first TFD/frame in window */
2191 	index = le16_to_cpu(ba_notif->scd_ssn);
2192 
2193 	rcu_read_lock();
2194 	mvmsta = iwl_mvm_sta_from_staid_rcu(mvm, sta_id);
2195 	if (WARN_ON_ONCE(!mvmsta)) {
2196 		rcu_read_unlock();
2197 		return;
2198 	}
2199 
2200 	tid_data = &mvmsta->tid_data[tid];
2201 
2202 	ba_info.status.ampdu_ack_len = ba_notif->txed_2_done;
2203 	ba_info.status.ampdu_len = ba_notif->txed;
2204 	ba_info.status.tx_time = tid_data->tx_time;
2205 	ba_info.status.status_driver_data[0] =
2206 		(void *)(uintptr_t)ba_notif->reduced_txp;
2207 
2208 	rcu_read_unlock();
2209 
2210 	iwl_mvm_tx_reclaim(mvm, sta_id, tid, txq, index, &ba_info,
2211 			   tid_data->rate_n_flags, false);
2212 
2213 	IWL_DEBUG_TX_REPLY(mvm,
2214 			   "BA_NOTIFICATION Received from %pM, sta_id = %d\n",
2215 			   ba_notif->sta_addr, ba_notif->sta_id);
2216 
2217 	IWL_DEBUG_TX_REPLY(mvm,
2218 			   "TID = %d, SeqCtl = %d, bitmap = 0x%llx, scd_flow = %d, scd_ssn = %d sent:%d, acked:%d\n",
2219 			   ba_notif->tid, le16_to_cpu(ba_notif->seq_ctl),
2220 			   le64_to_cpu(ba_notif->bitmap), txq, index,
2221 			   ba_notif->txed, ba_notif->txed_2_done);
2222 
2223 	IWL_DEBUG_TX_REPLY(mvm, "reduced txp from ba notif %d\n",
2224 			   ba_notif->reduced_txp);
2225 }
2226 
2227 /*
2228  * Note that there are transports that buffer frames before they reach
2229  * the firmware. This means that after flush_tx_path is called, the
2230  * queue might not be empty. The race-free way to handle this is to:
2231  * 1) set the station as draining
2232  * 2) flush the Tx path
2233  * 3) wait for the transport queues to be empty
2234  */
2235 int iwl_mvm_flush_tx_path(struct iwl_mvm *mvm, u32 tfd_msk)
2236 {
2237 	int ret;
2238 	struct iwl_tx_path_flush_cmd_v1 flush_cmd = {
2239 		.queues_ctl = cpu_to_le32(tfd_msk),
2240 		.flush_ctl = cpu_to_le16(DUMP_TX_FIFO_FLUSH),
2241 	};
2242 
2243 	WARN_ON(iwl_mvm_has_new_tx_api(mvm));
2244 	ret = iwl_mvm_send_cmd_pdu(mvm, TXPATH_FLUSH, 0,
2245 				   sizeof(flush_cmd), &flush_cmd);
2246 	if (ret)
2247 		IWL_ERR(mvm, "Failed to send flush command (%d)\n", ret);
2248 	return ret;
2249 }
2250 
2251 int iwl_mvm_flush_sta_tids(struct iwl_mvm *mvm, u32 sta_id, u16 tids)
2252 {
2253 	int ret;
2254 	struct iwl_tx_path_flush_cmd_rsp *rsp;
2255 	struct iwl_tx_path_flush_cmd flush_cmd = {
2256 		.sta_id = cpu_to_le32(sta_id),
2257 		.tid_mask = cpu_to_le16(tids),
2258 	};
2259 
2260 	struct iwl_host_cmd cmd = {
2261 		.id = TXPATH_FLUSH,
2262 		.len = { sizeof(flush_cmd), },
2263 		.data = { &flush_cmd, },
2264 	};
2265 
2266 	WARN_ON(!iwl_mvm_has_new_tx_api(mvm));
2267 
2268 	if (iwl_fw_lookup_notif_ver(mvm->fw, LONG_GROUP, TXPATH_FLUSH, 0) > 0)
2269 		cmd.flags |= CMD_WANT_SKB;
2270 
2271 	IWL_DEBUG_TX_QUEUES(mvm, "flush for sta id %d tid mask 0x%x\n",
2272 			    sta_id, tids);
2273 
2274 	ret = iwl_mvm_send_cmd(mvm, &cmd);
2275 
2276 	if (ret) {
2277 		IWL_ERR(mvm, "Failed to send flush command (%d)\n", ret);
2278 		return ret;
2279 	}
2280 
2281 	if (cmd.flags & CMD_WANT_SKB) {
2282 		int i;
2283 		int num_flushed_queues;
2284 
2285 		if (WARN_ON_ONCE(iwl_rx_packet_payload_len(cmd.resp_pkt) != sizeof(*rsp))) {
2286 			ret = -EIO;
2287 			goto free_rsp;
2288 		}
2289 
2290 		rsp = (void *)cmd.resp_pkt->data;
2291 
2292 		if (WARN_ONCE(le16_to_cpu(rsp->sta_id) != sta_id,
2293 			      "sta_id %d != rsp_sta_id %d",
2294 			      sta_id, le16_to_cpu(rsp->sta_id))) {
2295 			ret = -EIO;
2296 			goto free_rsp;
2297 		}
2298 
2299 		num_flushed_queues = le16_to_cpu(rsp->num_flushed_queues);
2300 		if (WARN_ONCE(num_flushed_queues > IWL_TX_FLUSH_QUEUE_RSP,
2301 			      "num_flushed_queues %d", num_flushed_queues)) {
2302 			ret = -EIO;
2303 			goto free_rsp;
2304 		}
2305 
2306 		for (i = 0; i < num_flushed_queues; i++) {
2307 			struct ieee80211_tx_info tx_info = {};
2308 			struct iwl_flush_queue_info *queue_info = &rsp->queues[i];
2309 			int tid = le16_to_cpu(queue_info->tid);
2310 			int read_before = le16_to_cpu(queue_info->read_before_flush);
2311 			int read_after = le16_to_cpu(queue_info->read_after_flush);
2312 			int queue_num = le16_to_cpu(queue_info->queue_num);
2313 
2314 			if (tid == IWL_MGMT_TID)
2315 				tid = IWL_MAX_TID_COUNT;
2316 
2317 			IWL_DEBUG_TX_QUEUES(mvm,
2318 					    "tid %d queue_id %d read-before %d read-after %d\n",
2319 					    tid, queue_num, read_before, read_after);
2320 
2321 			iwl_mvm_tx_reclaim(mvm, sta_id, tid, queue_num, read_after,
2322 					   &tx_info, 0, true);
2323 		}
2324 free_rsp:
2325 		iwl_free_resp(&cmd);
2326 	}
2327 	return ret;
2328 }
2329 
2330 int iwl_mvm_flush_sta(struct iwl_mvm *mvm, void *sta, bool internal)
2331 {
2332 	u32 sta_id, tfd_queue_msk;
2333 
2334 	if (internal) {
2335 		struct iwl_mvm_int_sta *int_sta = sta;
2336 
2337 		sta_id = int_sta->sta_id;
2338 		tfd_queue_msk = int_sta->tfd_queue_msk;
2339 	} else {
2340 		struct iwl_mvm_sta *mvm_sta = sta;
2341 
2342 		sta_id = mvm_sta->deflink.sta_id;
2343 		tfd_queue_msk = mvm_sta->tfd_queue_msk;
2344 	}
2345 
2346 	if (iwl_mvm_has_new_tx_api(mvm))
2347 		return iwl_mvm_flush_sta_tids(mvm, sta_id, 0xffff);
2348 
2349 	return iwl_mvm_flush_tx_path(mvm, tfd_queue_msk);
2350 }
2351