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