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