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