xref: /OK3568_Linux_fs/kernel/drivers/net/wireless/intel/iwlwifi/dvm/tx.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /******************************************************************************
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * Copyright(c) 2008 - 2014 Intel Corporation. All rights reserved.
5*4882a593Smuzhiyun  * Copyright (C) 2019 Intel Corporation
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * Contact Information:
8*4882a593Smuzhiyun  *  Intel Linux Wireless <linuxwifi@intel.com>
9*4882a593Smuzhiyun  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
10*4882a593Smuzhiyun  *
11*4882a593Smuzhiyun  *****************************************************************************/
12*4882a593Smuzhiyun 
13*4882a593Smuzhiyun #include <linux/kernel.h>
14*4882a593Smuzhiyun #include <linux/module.h>
15*4882a593Smuzhiyun #include <linux/sched.h>
16*4882a593Smuzhiyun #include <linux/ieee80211.h>
17*4882a593Smuzhiyun #include "iwl-io.h"
18*4882a593Smuzhiyun #include "iwl-trans.h"
19*4882a593Smuzhiyun #include "iwl-agn-hw.h"
20*4882a593Smuzhiyun #include "dev.h"
21*4882a593Smuzhiyun #include "agn.h"
22*4882a593Smuzhiyun 
23*4882a593Smuzhiyun static const u8 tid_to_ac[] = {
24*4882a593Smuzhiyun 	IEEE80211_AC_BE,
25*4882a593Smuzhiyun 	IEEE80211_AC_BK,
26*4882a593Smuzhiyun 	IEEE80211_AC_BK,
27*4882a593Smuzhiyun 	IEEE80211_AC_BE,
28*4882a593Smuzhiyun 	IEEE80211_AC_VI,
29*4882a593Smuzhiyun 	IEEE80211_AC_VI,
30*4882a593Smuzhiyun 	IEEE80211_AC_VO,
31*4882a593Smuzhiyun 	IEEE80211_AC_VO,
32*4882a593Smuzhiyun };
33*4882a593Smuzhiyun 
iwlagn_tx_cmd_protection(struct iwl_priv * priv,struct ieee80211_tx_info * info,__le16 fc,__le32 * tx_flags)34*4882a593Smuzhiyun static void iwlagn_tx_cmd_protection(struct iwl_priv *priv,
35*4882a593Smuzhiyun 				     struct ieee80211_tx_info *info,
36*4882a593Smuzhiyun 				     __le16 fc, __le32 *tx_flags)
37*4882a593Smuzhiyun {
38*4882a593Smuzhiyun 	if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_RTS_CTS ||
39*4882a593Smuzhiyun 	    info->control.rates[0].flags & IEEE80211_TX_RC_USE_CTS_PROTECT ||
40*4882a593Smuzhiyun 	    info->flags & IEEE80211_TX_CTL_AMPDU)
41*4882a593Smuzhiyun 		*tx_flags |= TX_CMD_FLG_PROT_REQUIRE_MSK;
42*4882a593Smuzhiyun }
43*4882a593Smuzhiyun 
44*4882a593Smuzhiyun /*
45*4882a593Smuzhiyun  * handle build REPLY_TX command notification.
46*4882a593Smuzhiyun  */
iwlagn_tx_cmd_build_basic(struct iwl_priv * priv,struct sk_buff * skb,struct iwl_tx_cmd * tx_cmd,struct ieee80211_tx_info * info,struct ieee80211_hdr * hdr,u8 sta_id)47*4882a593Smuzhiyun static void iwlagn_tx_cmd_build_basic(struct iwl_priv *priv,
48*4882a593Smuzhiyun 				      struct sk_buff *skb,
49*4882a593Smuzhiyun 				      struct iwl_tx_cmd *tx_cmd,
50*4882a593Smuzhiyun 				      struct ieee80211_tx_info *info,
51*4882a593Smuzhiyun 				      struct ieee80211_hdr *hdr, u8 sta_id)
52*4882a593Smuzhiyun {
53*4882a593Smuzhiyun 	__le16 fc = hdr->frame_control;
54*4882a593Smuzhiyun 	__le32 tx_flags = tx_cmd->tx_flags;
55*4882a593Smuzhiyun 
56*4882a593Smuzhiyun 	tx_cmd->stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
57*4882a593Smuzhiyun 
58*4882a593Smuzhiyun 	if (!(info->flags & IEEE80211_TX_CTL_NO_ACK))
59*4882a593Smuzhiyun 		tx_flags |= TX_CMD_FLG_ACK_MSK;
60*4882a593Smuzhiyun 	else
61*4882a593Smuzhiyun 		tx_flags &= ~TX_CMD_FLG_ACK_MSK;
62*4882a593Smuzhiyun 
63*4882a593Smuzhiyun 	if (ieee80211_is_probe_resp(fc))
64*4882a593Smuzhiyun 		tx_flags |= TX_CMD_FLG_TSF_MSK;
65*4882a593Smuzhiyun 	else if (ieee80211_is_back_req(fc))
66*4882a593Smuzhiyun 		tx_flags |= TX_CMD_FLG_ACK_MSK | TX_CMD_FLG_IMM_BA_RSP_MASK;
67*4882a593Smuzhiyun 	else if (info->band == NL80211_BAND_2GHZ &&
68*4882a593Smuzhiyun 		 priv->lib->bt_params &&
69*4882a593Smuzhiyun 		 priv->lib->bt_params->advanced_bt_coexist &&
70*4882a593Smuzhiyun 		 (ieee80211_is_auth(fc) || ieee80211_is_assoc_req(fc) ||
71*4882a593Smuzhiyun 		 ieee80211_is_reassoc_req(fc) ||
72*4882a593Smuzhiyun 		 info->control.flags & IEEE80211_TX_CTRL_PORT_CTRL_PROTO))
73*4882a593Smuzhiyun 		tx_flags |= TX_CMD_FLG_IGNORE_BT;
74*4882a593Smuzhiyun 
75*4882a593Smuzhiyun 
76*4882a593Smuzhiyun 	tx_cmd->sta_id = sta_id;
77*4882a593Smuzhiyun 	if (ieee80211_has_morefrags(fc))
78*4882a593Smuzhiyun 		tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
79*4882a593Smuzhiyun 
80*4882a593Smuzhiyun 	if (ieee80211_is_data_qos(fc)) {
81*4882a593Smuzhiyun 		u8 *qc = ieee80211_get_qos_ctl(hdr);
82*4882a593Smuzhiyun 		tx_cmd->tid_tspec = qc[0] & 0xf;
83*4882a593Smuzhiyun 		tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
84*4882a593Smuzhiyun 	} else {
85*4882a593Smuzhiyun 		tx_cmd->tid_tspec = IWL_TID_NON_QOS;
86*4882a593Smuzhiyun 		if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ)
87*4882a593Smuzhiyun 			tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
88*4882a593Smuzhiyun 		else
89*4882a593Smuzhiyun 			tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
90*4882a593Smuzhiyun 	}
91*4882a593Smuzhiyun 
92*4882a593Smuzhiyun 	iwlagn_tx_cmd_protection(priv, info, fc, &tx_flags);
93*4882a593Smuzhiyun 
94*4882a593Smuzhiyun 	tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
95*4882a593Smuzhiyun 	if (ieee80211_is_mgmt(fc)) {
96*4882a593Smuzhiyun 		if (ieee80211_is_assoc_req(fc) || ieee80211_is_reassoc_req(fc))
97*4882a593Smuzhiyun 			tx_cmd->timeout.pm_frame_timeout = cpu_to_le16(3);
98*4882a593Smuzhiyun 		else
99*4882a593Smuzhiyun 			tx_cmd->timeout.pm_frame_timeout = cpu_to_le16(2);
100*4882a593Smuzhiyun 	} else {
101*4882a593Smuzhiyun 		tx_cmd->timeout.pm_frame_timeout = 0;
102*4882a593Smuzhiyun 	}
103*4882a593Smuzhiyun 
104*4882a593Smuzhiyun 	tx_cmd->driver_txop = 0;
105*4882a593Smuzhiyun 	tx_cmd->tx_flags = tx_flags;
106*4882a593Smuzhiyun 	tx_cmd->next_frame_len = 0;
107*4882a593Smuzhiyun }
108*4882a593Smuzhiyun 
iwlagn_tx_cmd_build_rate(struct iwl_priv * priv,struct iwl_tx_cmd * tx_cmd,struct ieee80211_tx_info * info,struct ieee80211_sta * sta,__le16 fc)109*4882a593Smuzhiyun static void iwlagn_tx_cmd_build_rate(struct iwl_priv *priv,
110*4882a593Smuzhiyun 				     struct iwl_tx_cmd *tx_cmd,
111*4882a593Smuzhiyun 				     struct ieee80211_tx_info *info,
112*4882a593Smuzhiyun 				     struct ieee80211_sta *sta,
113*4882a593Smuzhiyun 				     __le16 fc)
114*4882a593Smuzhiyun {
115*4882a593Smuzhiyun 	u32 rate_flags;
116*4882a593Smuzhiyun 	int rate_idx;
117*4882a593Smuzhiyun 	u8 rts_retry_limit;
118*4882a593Smuzhiyun 	u8 data_retry_limit;
119*4882a593Smuzhiyun 	u8 rate_plcp;
120*4882a593Smuzhiyun 
121*4882a593Smuzhiyun 	if (priv->wowlan) {
122*4882a593Smuzhiyun 		rts_retry_limit = IWLAGN_LOW_RETRY_LIMIT;
123*4882a593Smuzhiyun 		data_retry_limit = IWLAGN_LOW_RETRY_LIMIT;
124*4882a593Smuzhiyun 	} else {
125*4882a593Smuzhiyun 		/* Set retry limit on RTS packets */
126*4882a593Smuzhiyun 		rts_retry_limit = IWLAGN_RTS_DFAULT_RETRY_LIMIT;
127*4882a593Smuzhiyun 
128*4882a593Smuzhiyun 		/* Set retry limit on DATA packets and Probe Responses*/
129*4882a593Smuzhiyun 		if (ieee80211_is_probe_resp(fc)) {
130*4882a593Smuzhiyun 			data_retry_limit = IWLAGN_MGMT_DFAULT_RETRY_LIMIT;
131*4882a593Smuzhiyun 			rts_retry_limit =
132*4882a593Smuzhiyun 				min(data_retry_limit, rts_retry_limit);
133*4882a593Smuzhiyun 		} else if (ieee80211_is_back_req(fc))
134*4882a593Smuzhiyun 			data_retry_limit = IWLAGN_BAR_DFAULT_RETRY_LIMIT;
135*4882a593Smuzhiyun 		else
136*4882a593Smuzhiyun 			data_retry_limit = IWLAGN_DEFAULT_TX_RETRY;
137*4882a593Smuzhiyun 	}
138*4882a593Smuzhiyun 
139*4882a593Smuzhiyun 	tx_cmd->data_retry_limit = data_retry_limit;
140*4882a593Smuzhiyun 	tx_cmd->rts_retry_limit = rts_retry_limit;
141*4882a593Smuzhiyun 
142*4882a593Smuzhiyun 	/* DATA packets will use the uCode station table for rate/antenna
143*4882a593Smuzhiyun 	 * selection */
144*4882a593Smuzhiyun 	if (ieee80211_is_data(fc)) {
145*4882a593Smuzhiyun 		tx_cmd->initial_rate_index = 0;
146*4882a593Smuzhiyun 		tx_cmd->tx_flags |= TX_CMD_FLG_STA_RATE_MSK;
147*4882a593Smuzhiyun 		return;
148*4882a593Smuzhiyun 	} else if (ieee80211_is_back_req(fc))
149*4882a593Smuzhiyun 		tx_cmd->tx_flags |= TX_CMD_FLG_STA_RATE_MSK;
150*4882a593Smuzhiyun 
151*4882a593Smuzhiyun 	/**
152*4882a593Smuzhiyun 	 * If the current TX rate stored in mac80211 has the MCS bit set, it's
153*4882a593Smuzhiyun 	 * not really a TX rate.  Thus, we use the lowest supported rate for
154*4882a593Smuzhiyun 	 * this band.  Also use the lowest supported rate if the stored rate
155*4882a593Smuzhiyun 	 * index is invalid.
156*4882a593Smuzhiyun 	 */
157*4882a593Smuzhiyun 	rate_idx = info->control.rates[0].idx;
158*4882a593Smuzhiyun 	if (info->control.rates[0].flags & IEEE80211_TX_RC_MCS ||
159*4882a593Smuzhiyun 			(rate_idx < 0) || (rate_idx > IWL_RATE_COUNT_LEGACY))
160*4882a593Smuzhiyun 		rate_idx = rate_lowest_index(
161*4882a593Smuzhiyun 				&priv->nvm_data->bands[info->band], sta);
162*4882a593Smuzhiyun 	/* For 5 GHZ band, remap mac80211 rate indices into driver indices */
163*4882a593Smuzhiyun 	if (info->band == NL80211_BAND_5GHZ)
164*4882a593Smuzhiyun 		rate_idx += IWL_FIRST_OFDM_RATE;
165*4882a593Smuzhiyun 	/* Get PLCP rate for tx_cmd->rate_n_flags */
166*4882a593Smuzhiyun 	rate_plcp = iwl_rates[rate_idx].plcp;
167*4882a593Smuzhiyun 	/* Zero out flags for this packet */
168*4882a593Smuzhiyun 	rate_flags = 0;
169*4882a593Smuzhiyun 
170*4882a593Smuzhiyun 	/* Set CCK flag as needed */
171*4882a593Smuzhiyun 	if ((rate_idx >= IWL_FIRST_CCK_RATE) && (rate_idx <= IWL_LAST_CCK_RATE))
172*4882a593Smuzhiyun 		rate_flags |= RATE_MCS_CCK_MSK;
173*4882a593Smuzhiyun 
174*4882a593Smuzhiyun 	/* Set up antennas */
175*4882a593Smuzhiyun 	if (priv->lib->bt_params &&
176*4882a593Smuzhiyun 	    priv->lib->bt_params->advanced_bt_coexist &&
177*4882a593Smuzhiyun 	    priv->bt_full_concurrent) {
178*4882a593Smuzhiyun 		/* operated as 1x1 in full concurrency mode */
179*4882a593Smuzhiyun 		priv->mgmt_tx_ant = iwl_toggle_tx_ant(priv, priv->mgmt_tx_ant,
180*4882a593Smuzhiyun 				first_antenna(priv->nvm_data->valid_tx_ant));
181*4882a593Smuzhiyun 	} else
182*4882a593Smuzhiyun 		priv->mgmt_tx_ant = iwl_toggle_tx_ant(
183*4882a593Smuzhiyun 					priv, priv->mgmt_tx_ant,
184*4882a593Smuzhiyun 					priv->nvm_data->valid_tx_ant);
185*4882a593Smuzhiyun 	rate_flags |= iwl_ant_idx_to_flags(priv->mgmt_tx_ant);
186*4882a593Smuzhiyun 
187*4882a593Smuzhiyun 	/* Set the rate in the TX cmd */
188*4882a593Smuzhiyun 	tx_cmd->rate_n_flags = iwl_hw_set_rate_n_flags(rate_plcp, rate_flags);
189*4882a593Smuzhiyun }
190*4882a593Smuzhiyun 
iwlagn_tx_cmd_build_hwcrypto(struct iwl_priv * priv,struct ieee80211_tx_info * info,struct iwl_tx_cmd * tx_cmd,struct sk_buff * skb_frag)191*4882a593Smuzhiyun static void iwlagn_tx_cmd_build_hwcrypto(struct iwl_priv *priv,
192*4882a593Smuzhiyun 					 struct ieee80211_tx_info *info,
193*4882a593Smuzhiyun 					 struct iwl_tx_cmd *tx_cmd,
194*4882a593Smuzhiyun 					 struct sk_buff *skb_frag)
195*4882a593Smuzhiyun {
196*4882a593Smuzhiyun 	struct ieee80211_key_conf *keyconf = info->control.hw_key;
197*4882a593Smuzhiyun 
198*4882a593Smuzhiyun 	switch (keyconf->cipher) {
199*4882a593Smuzhiyun 	case WLAN_CIPHER_SUITE_CCMP:
200*4882a593Smuzhiyun 		tx_cmd->sec_ctl = TX_CMD_SEC_CCM;
201*4882a593Smuzhiyun 		memcpy(tx_cmd->key, keyconf->key, keyconf->keylen);
202*4882a593Smuzhiyun 		if (info->flags & IEEE80211_TX_CTL_AMPDU)
203*4882a593Smuzhiyun 			tx_cmd->tx_flags |= TX_CMD_FLG_AGG_CCMP_MSK;
204*4882a593Smuzhiyun 		break;
205*4882a593Smuzhiyun 
206*4882a593Smuzhiyun 	case WLAN_CIPHER_SUITE_TKIP:
207*4882a593Smuzhiyun 		tx_cmd->sec_ctl = TX_CMD_SEC_TKIP;
208*4882a593Smuzhiyun 		ieee80211_get_tkip_p2k(keyconf, skb_frag, tx_cmd->key);
209*4882a593Smuzhiyun 		break;
210*4882a593Smuzhiyun 
211*4882a593Smuzhiyun 	case WLAN_CIPHER_SUITE_WEP104:
212*4882a593Smuzhiyun 		tx_cmd->sec_ctl |= TX_CMD_SEC_KEY128;
213*4882a593Smuzhiyun 		/* fall through */
214*4882a593Smuzhiyun 	case WLAN_CIPHER_SUITE_WEP40:
215*4882a593Smuzhiyun 		tx_cmd->sec_ctl |= (TX_CMD_SEC_WEP |
216*4882a593Smuzhiyun 			(keyconf->keyidx & TX_CMD_SEC_MSK) << TX_CMD_SEC_SHIFT);
217*4882a593Smuzhiyun 
218*4882a593Smuzhiyun 		memcpy(&tx_cmd->key[3], keyconf->key, keyconf->keylen);
219*4882a593Smuzhiyun 
220*4882a593Smuzhiyun 		IWL_DEBUG_TX(priv, "Configuring packet for WEP encryption "
221*4882a593Smuzhiyun 			     "with key %d\n", keyconf->keyidx);
222*4882a593Smuzhiyun 		break;
223*4882a593Smuzhiyun 
224*4882a593Smuzhiyun 	default:
225*4882a593Smuzhiyun 		IWL_ERR(priv, "Unknown encode cipher %x\n", keyconf->cipher);
226*4882a593Smuzhiyun 		break;
227*4882a593Smuzhiyun 	}
228*4882a593Smuzhiyun }
229*4882a593Smuzhiyun 
230*4882a593Smuzhiyun /**
231*4882a593Smuzhiyun  * iwl_sta_id_or_broadcast - return sta_id or broadcast sta
232*4882a593Smuzhiyun  * @context: the current context
233*4882a593Smuzhiyun  * @sta: mac80211 station
234*4882a593Smuzhiyun  *
235*4882a593Smuzhiyun  * In certain circumstances mac80211 passes a station pointer
236*4882a593Smuzhiyun  * that may be %NULL, for example during TX or key setup. In
237*4882a593Smuzhiyun  * that case, we need to use the broadcast station, so this
238*4882a593Smuzhiyun  * inline wraps that pattern.
239*4882a593Smuzhiyun  */
iwl_sta_id_or_broadcast(struct iwl_rxon_context * context,struct ieee80211_sta * sta)240*4882a593Smuzhiyun static int iwl_sta_id_or_broadcast(struct iwl_rxon_context *context,
241*4882a593Smuzhiyun 				   struct ieee80211_sta *sta)
242*4882a593Smuzhiyun {
243*4882a593Smuzhiyun 	int sta_id;
244*4882a593Smuzhiyun 
245*4882a593Smuzhiyun 	if (!sta)
246*4882a593Smuzhiyun 		return context->bcast_sta_id;
247*4882a593Smuzhiyun 
248*4882a593Smuzhiyun 	sta_id = iwl_sta_id(sta);
249*4882a593Smuzhiyun 
250*4882a593Smuzhiyun 	/*
251*4882a593Smuzhiyun 	 * mac80211 should not be passing a partially
252*4882a593Smuzhiyun 	 * initialised station!
253*4882a593Smuzhiyun 	 */
254*4882a593Smuzhiyun 	WARN_ON(sta_id == IWL_INVALID_STATION);
255*4882a593Smuzhiyun 
256*4882a593Smuzhiyun 	return sta_id;
257*4882a593Smuzhiyun }
258*4882a593Smuzhiyun 
259*4882a593Smuzhiyun /*
260*4882a593Smuzhiyun  * start REPLY_TX command process
261*4882a593Smuzhiyun  */
iwlagn_tx_skb(struct iwl_priv * priv,struct ieee80211_sta * sta,struct sk_buff * skb)262*4882a593Smuzhiyun int iwlagn_tx_skb(struct iwl_priv *priv,
263*4882a593Smuzhiyun 		  struct ieee80211_sta *sta,
264*4882a593Smuzhiyun 		  struct sk_buff *skb)
265*4882a593Smuzhiyun {
266*4882a593Smuzhiyun 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
267*4882a593Smuzhiyun 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
268*4882a593Smuzhiyun 	struct iwl_station_priv *sta_priv = NULL;
269*4882a593Smuzhiyun 	struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
270*4882a593Smuzhiyun 	struct iwl_device_tx_cmd *dev_cmd;
271*4882a593Smuzhiyun 	struct iwl_tx_cmd *tx_cmd;
272*4882a593Smuzhiyun 	__le16 fc;
273*4882a593Smuzhiyun 	u8 hdr_len;
274*4882a593Smuzhiyun 	u16 len, seq_number = 0;
275*4882a593Smuzhiyun 	u8 sta_id, tid = IWL_MAX_TID_COUNT;
276*4882a593Smuzhiyun 	bool is_agg = false, is_data_qos = false;
277*4882a593Smuzhiyun 	int txq_id;
278*4882a593Smuzhiyun 
279*4882a593Smuzhiyun 	if (info->control.vif)
280*4882a593Smuzhiyun 		ctx = iwl_rxon_ctx_from_vif(info->control.vif);
281*4882a593Smuzhiyun 
282*4882a593Smuzhiyun 	if (iwl_is_rfkill(priv)) {
283*4882a593Smuzhiyun 		IWL_DEBUG_DROP(priv, "Dropping - RF KILL\n");
284*4882a593Smuzhiyun 		goto drop_unlock_priv;
285*4882a593Smuzhiyun 	}
286*4882a593Smuzhiyun 
287*4882a593Smuzhiyun 	fc = hdr->frame_control;
288*4882a593Smuzhiyun 
289*4882a593Smuzhiyun #ifdef CONFIG_IWLWIFI_DEBUG
290*4882a593Smuzhiyun 	if (ieee80211_is_auth(fc))
291*4882a593Smuzhiyun 		IWL_DEBUG_TX(priv, "Sending AUTH frame\n");
292*4882a593Smuzhiyun 	else if (ieee80211_is_assoc_req(fc))
293*4882a593Smuzhiyun 		IWL_DEBUG_TX(priv, "Sending ASSOC frame\n");
294*4882a593Smuzhiyun 	else if (ieee80211_is_reassoc_req(fc))
295*4882a593Smuzhiyun 		IWL_DEBUG_TX(priv, "Sending REASSOC frame\n");
296*4882a593Smuzhiyun #endif
297*4882a593Smuzhiyun 
298*4882a593Smuzhiyun 	if (unlikely(ieee80211_is_probe_resp(fc))) {
299*4882a593Smuzhiyun 		struct iwl_wipan_noa_data *noa_data =
300*4882a593Smuzhiyun 			rcu_dereference(priv->noa_data);
301*4882a593Smuzhiyun 
302*4882a593Smuzhiyun 		if (noa_data &&
303*4882a593Smuzhiyun 		    pskb_expand_head(skb, 0, noa_data->length,
304*4882a593Smuzhiyun 				     GFP_ATOMIC) == 0) {
305*4882a593Smuzhiyun 			skb_put_data(skb, noa_data->data, noa_data->length);
306*4882a593Smuzhiyun 			hdr = (struct ieee80211_hdr *)skb->data;
307*4882a593Smuzhiyun 		}
308*4882a593Smuzhiyun 	}
309*4882a593Smuzhiyun 
310*4882a593Smuzhiyun 	hdr_len = ieee80211_hdrlen(fc);
311*4882a593Smuzhiyun 
312*4882a593Smuzhiyun 	/* For management frames use broadcast id to do not break aggregation */
313*4882a593Smuzhiyun 	if (!ieee80211_is_data(fc))
314*4882a593Smuzhiyun 		sta_id = ctx->bcast_sta_id;
315*4882a593Smuzhiyun 	else {
316*4882a593Smuzhiyun 		/* Find index into station table for destination station */
317*4882a593Smuzhiyun 		sta_id = iwl_sta_id_or_broadcast(ctx, sta);
318*4882a593Smuzhiyun 		if (sta_id == IWL_INVALID_STATION) {
319*4882a593Smuzhiyun 			IWL_DEBUG_DROP(priv, "Dropping - INVALID STATION: %pM\n",
320*4882a593Smuzhiyun 				       hdr->addr1);
321*4882a593Smuzhiyun 			goto drop_unlock_priv;
322*4882a593Smuzhiyun 		}
323*4882a593Smuzhiyun 	}
324*4882a593Smuzhiyun 
325*4882a593Smuzhiyun 	if (sta)
326*4882a593Smuzhiyun 		sta_priv = (void *)sta->drv_priv;
327*4882a593Smuzhiyun 
328*4882a593Smuzhiyun 	if (sta_priv && sta_priv->asleep &&
329*4882a593Smuzhiyun 	    (info->flags & IEEE80211_TX_CTL_NO_PS_BUFFER)) {
330*4882a593Smuzhiyun 		/*
331*4882a593Smuzhiyun 		 * This sends an asynchronous command to the device,
332*4882a593Smuzhiyun 		 * but we can rely on it being processed before the
333*4882a593Smuzhiyun 		 * next frame is processed -- and the next frame to
334*4882a593Smuzhiyun 		 * this station is the one that will consume this
335*4882a593Smuzhiyun 		 * counter.
336*4882a593Smuzhiyun 		 * For now set the counter to just 1 since we do not
337*4882a593Smuzhiyun 		 * support uAPSD yet.
338*4882a593Smuzhiyun 		 *
339*4882a593Smuzhiyun 		 * FIXME: If we get two non-bufferable frames one
340*4882a593Smuzhiyun 		 * after the other, we might only send out one of
341*4882a593Smuzhiyun 		 * them because this is racy.
342*4882a593Smuzhiyun 		 */
343*4882a593Smuzhiyun 		iwl_sta_modify_sleep_tx_count(priv, sta_id, 1);
344*4882a593Smuzhiyun 	}
345*4882a593Smuzhiyun 
346*4882a593Smuzhiyun 	dev_cmd = iwl_trans_alloc_tx_cmd(priv->trans);
347*4882a593Smuzhiyun 
348*4882a593Smuzhiyun 	if (unlikely(!dev_cmd))
349*4882a593Smuzhiyun 		goto drop_unlock_priv;
350*4882a593Smuzhiyun 
351*4882a593Smuzhiyun 	dev_cmd->hdr.cmd = REPLY_TX;
352*4882a593Smuzhiyun 	tx_cmd = (struct iwl_tx_cmd *) dev_cmd->payload;
353*4882a593Smuzhiyun 
354*4882a593Smuzhiyun 	/* Total # bytes to be transmitted */
355*4882a593Smuzhiyun 	len = (u16)skb->len;
356*4882a593Smuzhiyun 	tx_cmd->len = cpu_to_le16(len);
357*4882a593Smuzhiyun 
358*4882a593Smuzhiyun 	if (info->control.hw_key)
359*4882a593Smuzhiyun 		iwlagn_tx_cmd_build_hwcrypto(priv, info, tx_cmd, skb);
360*4882a593Smuzhiyun 
361*4882a593Smuzhiyun 	/* TODO need this for burst mode later on */
362*4882a593Smuzhiyun 	iwlagn_tx_cmd_build_basic(priv, skb, tx_cmd, info, hdr, sta_id);
363*4882a593Smuzhiyun 
364*4882a593Smuzhiyun 	iwlagn_tx_cmd_build_rate(priv, tx_cmd, info, sta, fc);
365*4882a593Smuzhiyun 
366*4882a593Smuzhiyun 	memset(&info->status, 0, sizeof(info->status));
367*4882a593Smuzhiyun 	memset(info->driver_data, 0, sizeof(info->driver_data));
368*4882a593Smuzhiyun 
369*4882a593Smuzhiyun 	info->driver_data[0] = ctx;
370*4882a593Smuzhiyun 	info->driver_data[1] = dev_cmd;
371*4882a593Smuzhiyun 	/* From now on, we cannot access info->control */
372*4882a593Smuzhiyun 
373*4882a593Smuzhiyun 	spin_lock(&priv->sta_lock);
374*4882a593Smuzhiyun 
375*4882a593Smuzhiyun 	if (ieee80211_is_data_qos(fc) && !ieee80211_is_qos_nullfunc(fc)) {
376*4882a593Smuzhiyun 		u8 *qc = NULL;
377*4882a593Smuzhiyun 		struct iwl_tid_data *tid_data;
378*4882a593Smuzhiyun 		qc = ieee80211_get_qos_ctl(hdr);
379*4882a593Smuzhiyun 		tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
380*4882a593Smuzhiyun 		if (WARN_ON_ONCE(tid >= IWL_MAX_TID_COUNT))
381*4882a593Smuzhiyun 			goto drop_unlock_sta;
382*4882a593Smuzhiyun 		tid_data = &priv->tid_data[sta_id][tid];
383*4882a593Smuzhiyun 
384*4882a593Smuzhiyun 		/* aggregation is on for this <sta,tid> */
385*4882a593Smuzhiyun 		if (info->flags & IEEE80211_TX_CTL_AMPDU &&
386*4882a593Smuzhiyun 		    tid_data->agg.state != IWL_AGG_ON) {
387*4882a593Smuzhiyun 			IWL_ERR(priv,
388*4882a593Smuzhiyun 				"TX_CTL_AMPDU while not in AGG: Tx flags = 0x%08x, agg.state = %d\n",
389*4882a593Smuzhiyun 				info->flags, tid_data->agg.state);
390*4882a593Smuzhiyun 			IWL_ERR(priv, "sta_id = %d, tid = %d seq_num = %d\n",
391*4882a593Smuzhiyun 				sta_id, tid,
392*4882a593Smuzhiyun 				IEEE80211_SEQ_TO_SN(tid_data->seq_number));
393*4882a593Smuzhiyun 			goto drop_unlock_sta;
394*4882a593Smuzhiyun 		}
395*4882a593Smuzhiyun 
396*4882a593Smuzhiyun 		/* We can receive packets from the stack in IWL_AGG_{ON,OFF}
397*4882a593Smuzhiyun 		 * only. Check this here.
398*4882a593Smuzhiyun 		 */
399*4882a593Smuzhiyun 		if (WARN_ONCE(tid_data->agg.state != IWL_AGG_ON &&
400*4882a593Smuzhiyun 			      tid_data->agg.state != IWL_AGG_OFF,
401*4882a593Smuzhiyun 			      "Tx while agg.state = %d\n", tid_data->agg.state))
402*4882a593Smuzhiyun 			goto drop_unlock_sta;
403*4882a593Smuzhiyun 
404*4882a593Smuzhiyun 		seq_number = tid_data->seq_number;
405*4882a593Smuzhiyun 		seq_number &= IEEE80211_SCTL_SEQ;
406*4882a593Smuzhiyun 		hdr->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
407*4882a593Smuzhiyun 		hdr->seq_ctrl |= cpu_to_le16(seq_number);
408*4882a593Smuzhiyun 		seq_number += 0x10;
409*4882a593Smuzhiyun 
410*4882a593Smuzhiyun 		if (info->flags & IEEE80211_TX_CTL_AMPDU)
411*4882a593Smuzhiyun 			is_agg = true;
412*4882a593Smuzhiyun 		is_data_qos = true;
413*4882a593Smuzhiyun 	}
414*4882a593Smuzhiyun 
415*4882a593Smuzhiyun 	/* Copy MAC header from skb into command buffer */
416*4882a593Smuzhiyun 	memcpy(tx_cmd->hdr, hdr, hdr_len);
417*4882a593Smuzhiyun 
418*4882a593Smuzhiyun 	txq_id = info->hw_queue;
419*4882a593Smuzhiyun 
420*4882a593Smuzhiyun 	if (is_agg)
421*4882a593Smuzhiyun 		txq_id = priv->tid_data[sta_id][tid].agg.txq_id;
422*4882a593Smuzhiyun 	else if (info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM) {
423*4882a593Smuzhiyun 		/*
424*4882a593Smuzhiyun 		 * The microcode will clear the more data
425*4882a593Smuzhiyun 		 * bit in the last frame it transmits.
426*4882a593Smuzhiyun 		 */
427*4882a593Smuzhiyun 		hdr->frame_control |=
428*4882a593Smuzhiyun 			cpu_to_le16(IEEE80211_FCTL_MOREDATA);
429*4882a593Smuzhiyun 	}
430*4882a593Smuzhiyun 
431*4882a593Smuzhiyun 	WARN_ON_ONCE(is_agg &&
432*4882a593Smuzhiyun 		     priv->queue_to_mac80211[txq_id] != info->hw_queue);
433*4882a593Smuzhiyun 
434*4882a593Smuzhiyun 	IWL_DEBUG_TX(priv, "TX to [%d|%d] Q:%d - seq: 0x%x\n", sta_id, tid,
435*4882a593Smuzhiyun 		     txq_id, seq_number);
436*4882a593Smuzhiyun 
437*4882a593Smuzhiyun 	if (iwl_trans_tx(priv->trans, skb, dev_cmd, txq_id))
438*4882a593Smuzhiyun 		goto drop_unlock_sta;
439*4882a593Smuzhiyun 
440*4882a593Smuzhiyun 	if (is_data_qos && !ieee80211_has_morefrags(fc))
441*4882a593Smuzhiyun 		priv->tid_data[sta_id][tid].seq_number = seq_number;
442*4882a593Smuzhiyun 
443*4882a593Smuzhiyun 	spin_unlock(&priv->sta_lock);
444*4882a593Smuzhiyun 
445*4882a593Smuzhiyun 	/*
446*4882a593Smuzhiyun 	 * Avoid atomic ops if it isn't an associated client.
447*4882a593Smuzhiyun 	 * Also, if this is a packet for aggregation, don't
448*4882a593Smuzhiyun 	 * increase the counter because the ucode will stop
449*4882a593Smuzhiyun 	 * aggregation queues when their respective station
450*4882a593Smuzhiyun 	 * goes to sleep.
451*4882a593Smuzhiyun 	 */
452*4882a593Smuzhiyun 	if (sta_priv && sta_priv->client && !is_agg)
453*4882a593Smuzhiyun 		atomic_inc(&sta_priv->pending_frames);
454*4882a593Smuzhiyun 
455*4882a593Smuzhiyun 	return 0;
456*4882a593Smuzhiyun 
457*4882a593Smuzhiyun drop_unlock_sta:
458*4882a593Smuzhiyun 	if (dev_cmd)
459*4882a593Smuzhiyun 		iwl_trans_free_tx_cmd(priv->trans, dev_cmd);
460*4882a593Smuzhiyun 	spin_unlock(&priv->sta_lock);
461*4882a593Smuzhiyun drop_unlock_priv:
462*4882a593Smuzhiyun 	return -1;
463*4882a593Smuzhiyun }
464*4882a593Smuzhiyun 
iwlagn_alloc_agg_txq(struct iwl_priv * priv,int mq)465*4882a593Smuzhiyun static int iwlagn_alloc_agg_txq(struct iwl_priv *priv, int mq)
466*4882a593Smuzhiyun {
467*4882a593Smuzhiyun 	int q;
468*4882a593Smuzhiyun 
469*4882a593Smuzhiyun 	for (q = IWLAGN_FIRST_AMPDU_QUEUE;
470*4882a593Smuzhiyun 	     q < priv->trans->trans_cfg->base_params->num_of_queues; q++) {
471*4882a593Smuzhiyun 		if (!test_and_set_bit(q, priv->agg_q_alloc)) {
472*4882a593Smuzhiyun 			priv->queue_to_mac80211[q] = mq;
473*4882a593Smuzhiyun 			return q;
474*4882a593Smuzhiyun 		}
475*4882a593Smuzhiyun 	}
476*4882a593Smuzhiyun 
477*4882a593Smuzhiyun 	return -ENOSPC;
478*4882a593Smuzhiyun }
479*4882a593Smuzhiyun 
iwlagn_dealloc_agg_txq(struct iwl_priv * priv,int q)480*4882a593Smuzhiyun static void iwlagn_dealloc_agg_txq(struct iwl_priv *priv, int q)
481*4882a593Smuzhiyun {
482*4882a593Smuzhiyun 	clear_bit(q, priv->agg_q_alloc);
483*4882a593Smuzhiyun 	priv->queue_to_mac80211[q] = IWL_INVALID_MAC80211_QUEUE;
484*4882a593Smuzhiyun }
485*4882a593Smuzhiyun 
iwlagn_tx_agg_stop(struct iwl_priv * priv,struct ieee80211_vif * vif,struct ieee80211_sta * sta,u16 tid)486*4882a593Smuzhiyun int iwlagn_tx_agg_stop(struct iwl_priv *priv, struct ieee80211_vif *vif,
487*4882a593Smuzhiyun 			struct ieee80211_sta *sta, u16 tid)
488*4882a593Smuzhiyun {
489*4882a593Smuzhiyun 	struct iwl_tid_data *tid_data;
490*4882a593Smuzhiyun 	int sta_id, txq_id;
491*4882a593Smuzhiyun 	enum iwl_agg_state agg_state;
492*4882a593Smuzhiyun 
493*4882a593Smuzhiyun 	sta_id = iwl_sta_id(sta);
494*4882a593Smuzhiyun 
495*4882a593Smuzhiyun 	if (sta_id == IWL_INVALID_STATION) {
496*4882a593Smuzhiyun 		IWL_ERR(priv, "Invalid station for AGG tid %d\n", tid);
497*4882a593Smuzhiyun 		return -ENXIO;
498*4882a593Smuzhiyun 	}
499*4882a593Smuzhiyun 
500*4882a593Smuzhiyun 	spin_lock_bh(&priv->sta_lock);
501*4882a593Smuzhiyun 
502*4882a593Smuzhiyun 	tid_data = &priv->tid_data[sta_id][tid];
503*4882a593Smuzhiyun 	txq_id = tid_data->agg.txq_id;
504*4882a593Smuzhiyun 
505*4882a593Smuzhiyun 	switch (tid_data->agg.state) {
506*4882a593Smuzhiyun 	case IWL_EMPTYING_HW_QUEUE_ADDBA:
507*4882a593Smuzhiyun 		/*
508*4882a593Smuzhiyun 		* This can happen if the peer stops aggregation
509*4882a593Smuzhiyun 		* again before we've had a chance to drain the
510*4882a593Smuzhiyun 		* queue we selected previously, i.e. before the
511*4882a593Smuzhiyun 		* session was really started completely.
512*4882a593Smuzhiyun 		*/
513*4882a593Smuzhiyun 		IWL_DEBUG_HT(priv, "AGG stop before setup done\n");
514*4882a593Smuzhiyun 		goto turn_off;
515*4882a593Smuzhiyun 	case IWL_AGG_STARTING:
516*4882a593Smuzhiyun 		/*
517*4882a593Smuzhiyun 		 * This can happen when the session is stopped before
518*4882a593Smuzhiyun 		 * we receive ADDBA response
519*4882a593Smuzhiyun 		 */
520*4882a593Smuzhiyun 		IWL_DEBUG_HT(priv, "AGG stop before AGG became operational\n");
521*4882a593Smuzhiyun 		goto turn_off;
522*4882a593Smuzhiyun 	case IWL_AGG_ON:
523*4882a593Smuzhiyun 		break;
524*4882a593Smuzhiyun 	default:
525*4882a593Smuzhiyun 		IWL_WARN(priv,
526*4882a593Smuzhiyun 			 "Stopping AGG while state not ON or starting for %d on %d (%d)\n",
527*4882a593Smuzhiyun 			 sta_id, tid, tid_data->agg.state);
528*4882a593Smuzhiyun 		spin_unlock_bh(&priv->sta_lock);
529*4882a593Smuzhiyun 		return 0;
530*4882a593Smuzhiyun 	}
531*4882a593Smuzhiyun 
532*4882a593Smuzhiyun 	tid_data->agg.ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
533*4882a593Smuzhiyun 
534*4882a593Smuzhiyun 	/* There are still packets for this RA / TID in the HW */
535*4882a593Smuzhiyun 	if (!test_bit(txq_id, priv->agg_q_alloc)) {
536*4882a593Smuzhiyun 		IWL_DEBUG_TX_QUEUES(priv,
537*4882a593Smuzhiyun 			"stopping AGG on STA/TID %d/%d but hwq %d not used\n",
538*4882a593Smuzhiyun 			sta_id, tid, txq_id);
539*4882a593Smuzhiyun 	} else if (tid_data->agg.ssn != tid_data->next_reclaimed) {
540*4882a593Smuzhiyun 		IWL_DEBUG_TX_QUEUES(priv,
541*4882a593Smuzhiyun 				    "Can't proceed: ssn %d, next_recl = %d\n",
542*4882a593Smuzhiyun 				    tid_data->agg.ssn,
543*4882a593Smuzhiyun 				    tid_data->next_reclaimed);
544*4882a593Smuzhiyun 		tid_data->agg.state = IWL_EMPTYING_HW_QUEUE_DELBA;
545*4882a593Smuzhiyun 		spin_unlock_bh(&priv->sta_lock);
546*4882a593Smuzhiyun 		return 0;
547*4882a593Smuzhiyun 	}
548*4882a593Smuzhiyun 
549*4882a593Smuzhiyun 	IWL_DEBUG_TX_QUEUES(priv, "Can proceed: ssn = next_recl = %d\n",
550*4882a593Smuzhiyun 			    tid_data->agg.ssn);
551*4882a593Smuzhiyun turn_off:
552*4882a593Smuzhiyun 	agg_state = tid_data->agg.state;
553*4882a593Smuzhiyun 	tid_data->agg.state = IWL_AGG_OFF;
554*4882a593Smuzhiyun 
555*4882a593Smuzhiyun 	spin_unlock_bh(&priv->sta_lock);
556*4882a593Smuzhiyun 
557*4882a593Smuzhiyun 	if (test_bit(txq_id, priv->agg_q_alloc)) {
558*4882a593Smuzhiyun 		/*
559*4882a593Smuzhiyun 		 * If the transport didn't know that we wanted to start
560*4882a593Smuzhiyun 		 * agreggation, don't tell it that we want to stop them.
561*4882a593Smuzhiyun 		 * This can happen when we don't get the addBA response on
562*4882a593Smuzhiyun 		 * time, or we hadn't time to drain the AC queues.
563*4882a593Smuzhiyun 		 */
564*4882a593Smuzhiyun 		if (agg_state == IWL_AGG_ON)
565*4882a593Smuzhiyun 			iwl_trans_txq_disable(priv->trans, txq_id, true);
566*4882a593Smuzhiyun 		else
567*4882a593Smuzhiyun 			IWL_DEBUG_TX_QUEUES(priv, "Don't disable tx agg: %d\n",
568*4882a593Smuzhiyun 					    agg_state);
569*4882a593Smuzhiyun 		iwlagn_dealloc_agg_txq(priv, txq_id);
570*4882a593Smuzhiyun 	}
571*4882a593Smuzhiyun 
572*4882a593Smuzhiyun 	ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
573*4882a593Smuzhiyun 
574*4882a593Smuzhiyun 	return 0;
575*4882a593Smuzhiyun }
576*4882a593Smuzhiyun 
iwlagn_tx_agg_start(struct iwl_priv * priv,struct ieee80211_vif * vif,struct ieee80211_sta * sta,u16 tid,u16 * ssn)577*4882a593Smuzhiyun int iwlagn_tx_agg_start(struct iwl_priv *priv, struct ieee80211_vif *vif,
578*4882a593Smuzhiyun 			struct ieee80211_sta *sta, u16 tid, u16 *ssn)
579*4882a593Smuzhiyun {
580*4882a593Smuzhiyun 	struct iwl_rxon_context *ctx = iwl_rxon_ctx_from_vif(vif);
581*4882a593Smuzhiyun 	struct iwl_tid_data *tid_data;
582*4882a593Smuzhiyun 	int sta_id, txq_id, ret;
583*4882a593Smuzhiyun 
584*4882a593Smuzhiyun 	IWL_DEBUG_HT(priv, "TX AGG request on ra = %pM tid = %d\n",
585*4882a593Smuzhiyun 		     sta->addr, tid);
586*4882a593Smuzhiyun 
587*4882a593Smuzhiyun 	sta_id = iwl_sta_id(sta);
588*4882a593Smuzhiyun 	if (sta_id == IWL_INVALID_STATION) {
589*4882a593Smuzhiyun 		IWL_ERR(priv, "Start AGG on invalid station\n");
590*4882a593Smuzhiyun 		return -ENXIO;
591*4882a593Smuzhiyun 	}
592*4882a593Smuzhiyun 	if (unlikely(tid >= IWL_MAX_TID_COUNT))
593*4882a593Smuzhiyun 		return -EINVAL;
594*4882a593Smuzhiyun 
595*4882a593Smuzhiyun 	if (priv->tid_data[sta_id][tid].agg.state != IWL_AGG_OFF) {
596*4882a593Smuzhiyun 		IWL_ERR(priv, "Start AGG when state is not IWL_AGG_OFF !\n");
597*4882a593Smuzhiyun 		return -ENXIO;
598*4882a593Smuzhiyun 	}
599*4882a593Smuzhiyun 
600*4882a593Smuzhiyun 	txq_id = iwlagn_alloc_agg_txq(priv, ctx->ac_to_queue[tid_to_ac[tid]]);
601*4882a593Smuzhiyun 	if (txq_id < 0) {
602*4882a593Smuzhiyun 		IWL_DEBUG_TX_QUEUES(priv,
603*4882a593Smuzhiyun 			"No free aggregation queue for %pM/%d\n",
604*4882a593Smuzhiyun 			sta->addr, tid);
605*4882a593Smuzhiyun 		return txq_id;
606*4882a593Smuzhiyun 	}
607*4882a593Smuzhiyun 
608*4882a593Smuzhiyun 	ret = iwl_sta_tx_modify_enable_tid(priv, sta_id, tid);
609*4882a593Smuzhiyun 	if (ret)
610*4882a593Smuzhiyun 		return ret;
611*4882a593Smuzhiyun 
612*4882a593Smuzhiyun 	spin_lock_bh(&priv->sta_lock);
613*4882a593Smuzhiyun 	tid_data = &priv->tid_data[sta_id][tid];
614*4882a593Smuzhiyun 	tid_data->agg.ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
615*4882a593Smuzhiyun 	tid_data->agg.txq_id = txq_id;
616*4882a593Smuzhiyun 
617*4882a593Smuzhiyun 	*ssn = tid_data->agg.ssn;
618*4882a593Smuzhiyun 
619*4882a593Smuzhiyun 	if (*ssn == tid_data->next_reclaimed) {
620*4882a593Smuzhiyun 		IWL_DEBUG_TX_QUEUES(priv, "Can proceed: ssn = next_recl = %d\n",
621*4882a593Smuzhiyun 				    tid_data->agg.ssn);
622*4882a593Smuzhiyun 		tid_data->agg.state = IWL_AGG_STARTING;
623*4882a593Smuzhiyun 		ret = IEEE80211_AMPDU_TX_START_IMMEDIATE;
624*4882a593Smuzhiyun 	} else {
625*4882a593Smuzhiyun 		IWL_DEBUG_TX_QUEUES(priv, "Can't proceed: ssn %d, "
626*4882a593Smuzhiyun 				    "next_reclaimed = %d\n",
627*4882a593Smuzhiyun 				    tid_data->agg.ssn,
628*4882a593Smuzhiyun 				    tid_data->next_reclaimed);
629*4882a593Smuzhiyun 		tid_data->agg.state = IWL_EMPTYING_HW_QUEUE_ADDBA;
630*4882a593Smuzhiyun 	}
631*4882a593Smuzhiyun 	spin_unlock_bh(&priv->sta_lock);
632*4882a593Smuzhiyun 
633*4882a593Smuzhiyun 	return ret;
634*4882a593Smuzhiyun }
635*4882a593Smuzhiyun 
iwlagn_tx_agg_flush(struct iwl_priv * priv,struct ieee80211_vif * vif,struct ieee80211_sta * sta,u16 tid)636*4882a593Smuzhiyun int iwlagn_tx_agg_flush(struct iwl_priv *priv, struct ieee80211_vif *vif,
637*4882a593Smuzhiyun 			struct ieee80211_sta *sta, u16 tid)
638*4882a593Smuzhiyun {
639*4882a593Smuzhiyun 	struct iwl_tid_data *tid_data;
640*4882a593Smuzhiyun 	enum iwl_agg_state agg_state;
641*4882a593Smuzhiyun 	int sta_id, txq_id;
642*4882a593Smuzhiyun 	sta_id = iwl_sta_id(sta);
643*4882a593Smuzhiyun 
644*4882a593Smuzhiyun 	/*
645*4882a593Smuzhiyun 	 * First set the agg state to OFF to avoid calling
646*4882a593Smuzhiyun 	 * ieee80211_stop_tx_ba_cb in iwlagn_check_ratid_empty.
647*4882a593Smuzhiyun 	 */
648*4882a593Smuzhiyun 	spin_lock_bh(&priv->sta_lock);
649*4882a593Smuzhiyun 
650*4882a593Smuzhiyun 	tid_data = &priv->tid_data[sta_id][tid];
651*4882a593Smuzhiyun 	txq_id = tid_data->agg.txq_id;
652*4882a593Smuzhiyun 	agg_state = tid_data->agg.state;
653*4882a593Smuzhiyun 	IWL_DEBUG_TX_QUEUES(priv, "Flush AGG: sta %d tid %d q %d state %d\n",
654*4882a593Smuzhiyun 			    sta_id, tid, txq_id, tid_data->agg.state);
655*4882a593Smuzhiyun 
656*4882a593Smuzhiyun 	tid_data->agg.state = IWL_AGG_OFF;
657*4882a593Smuzhiyun 
658*4882a593Smuzhiyun 	spin_unlock_bh(&priv->sta_lock);
659*4882a593Smuzhiyun 
660*4882a593Smuzhiyun 	if (iwlagn_txfifo_flush(priv, BIT(txq_id)))
661*4882a593Smuzhiyun 		IWL_ERR(priv, "Couldn't flush the AGG queue\n");
662*4882a593Smuzhiyun 
663*4882a593Smuzhiyun 	if (test_bit(txq_id, priv->agg_q_alloc)) {
664*4882a593Smuzhiyun 		/*
665*4882a593Smuzhiyun 		 * If the transport didn't know that we wanted to start
666*4882a593Smuzhiyun 		 * agreggation, don't tell it that we want to stop them.
667*4882a593Smuzhiyun 		 * This can happen when we don't get the addBA response on
668*4882a593Smuzhiyun 		 * time, or we hadn't time to drain the AC queues.
669*4882a593Smuzhiyun 		 */
670*4882a593Smuzhiyun 		if (agg_state == IWL_AGG_ON)
671*4882a593Smuzhiyun 			iwl_trans_txq_disable(priv->trans, txq_id, true);
672*4882a593Smuzhiyun 		else
673*4882a593Smuzhiyun 			IWL_DEBUG_TX_QUEUES(priv, "Don't disable tx agg: %d\n",
674*4882a593Smuzhiyun 					    agg_state);
675*4882a593Smuzhiyun 		iwlagn_dealloc_agg_txq(priv, txq_id);
676*4882a593Smuzhiyun 	}
677*4882a593Smuzhiyun 
678*4882a593Smuzhiyun 	return 0;
679*4882a593Smuzhiyun }
680*4882a593Smuzhiyun 
iwlagn_tx_agg_oper(struct iwl_priv * priv,struct ieee80211_vif * vif,struct ieee80211_sta * sta,u16 tid,u8 buf_size)681*4882a593Smuzhiyun int iwlagn_tx_agg_oper(struct iwl_priv *priv, struct ieee80211_vif *vif,
682*4882a593Smuzhiyun 			struct ieee80211_sta *sta, u16 tid, u8 buf_size)
683*4882a593Smuzhiyun {
684*4882a593Smuzhiyun 	struct iwl_station_priv *sta_priv = (void *) sta->drv_priv;
685*4882a593Smuzhiyun 	struct iwl_rxon_context *ctx = iwl_rxon_ctx_from_vif(vif);
686*4882a593Smuzhiyun 	int q, fifo;
687*4882a593Smuzhiyun 	u16 ssn;
688*4882a593Smuzhiyun 
689*4882a593Smuzhiyun 	buf_size = min_t(int, buf_size, LINK_QUAL_AGG_FRAME_LIMIT_DEF);
690*4882a593Smuzhiyun 
691*4882a593Smuzhiyun 	spin_lock_bh(&priv->sta_lock);
692*4882a593Smuzhiyun 	ssn = priv->tid_data[sta_priv->sta_id][tid].agg.ssn;
693*4882a593Smuzhiyun 	q = priv->tid_data[sta_priv->sta_id][tid].agg.txq_id;
694*4882a593Smuzhiyun 	priv->tid_data[sta_priv->sta_id][tid].agg.state = IWL_AGG_ON;
695*4882a593Smuzhiyun 	spin_unlock_bh(&priv->sta_lock);
696*4882a593Smuzhiyun 
697*4882a593Smuzhiyun 	fifo = ctx->ac_to_fifo[tid_to_ac[tid]];
698*4882a593Smuzhiyun 
699*4882a593Smuzhiyun 	iwl_trans_txq_enable(priv->trans, q, fifo, sta_priv->sta_id, tid,
700*4882a593Smuzhiyun 			     buf_size, ssn, 0);
701*4882a593Smuzhiyun 
702*4882a593Smuzhiyun 	/*
703*4882a593Smuzhiyun 	 * If the limit is 0, then it wasn't initialised yet,
704*4882a593Smuzhiyun 	 * use the default. We can do that since we take the
705*4882a593Smuzhiyun 	 * minimum below, and we don't want to go above our
706*4882a593Smuzhiyun 	 * default due to hardware restrictions.
707*4882a593Smuzhiyun 	 */
708*4882a593Smuzhiyun 	if (sta_priv->max_agg_bufsize == 0)
709*4882a593Smuzhiyun 		sta_priv->max_agg_bufsize =
710*4882a593Smuzhiyun 			LINK_QUAL_AGG_FRAME_LIMIT_DEF;
711*4882a593Smuzhiyun 
712*4882a593Smuzhiyun 	/*
713*4882a593Smuzhiyun 	 * Even though in theory the peer could have different
714*4882a593Smuzhiyun 	 * aggregation reorder buffer sizes for different sessions,
715*4882a593Smuzhiyun 	 * our ucode doesn't allow for that and has a global limit
716*4882a593Smuzhiyun 	 * for each station. Therefore, use the minimum of all the
717*4882a593Smuzhiyun 	 * aggregation sessions and our default value.
718*4882a593Smuzhiyun 	 */
719*4882a593Smuzhiyun 	sta_priv->max_agg_bufsize =
720*4882a593Smuzhiyun 		min(sta_priv->max_agg_bufsize, buf_size);
721*4882a593Smuzhiyun 
722*4882a593Smuzhiyun 	if (priv->hw_params.use_rts_for_aggregation) {
723*4882a593Smuzhiyun 		/*
724*4882a593Smuzhiyun 		 * switch to RTS/CTS if it is the prefer protection
725*4882a593Smuzhiyun 		 * method for HT traffic
726*4882a593Smuzhiyun 		 */
727*4882a593Smuzhiyun 
728*4882a593Smuzhiyun 		sta_priv->lq_sta.lq.general_params.flags |=
729*4882a593Smuzhiyun 			LINK_QUAL_FLAGS_SET_STA_TLC_RTS_MSK;
730*4882a593Smuzhiyun 	}
731*4882a593Smuzhiyun 	priv->agg_tids_count++;
732*4882a593Smuzhiyun 	IWL_DEBUG_HT(priv, "priv->agg_tids_count = %u\n",
733*4882a593Smuzhiyun 		     priv->agg_tids_count);
734*4882a593Smuzhiyun 
735*4882a593Smuzhiyun 	sta_priv->lq_sta.lq.agg_params.agg_frame_cnt_limit =
736*4882a593Smuzhiyun 		sta_priv->max_agg_bufsize;
737*4882a593Smuzhiyun 
738*4882a593Smuzhiyun 	IWL_DEBUG_HT(priv, "Tx aggregation enabled on ra = %pM tid = %d\n",
739*4882a593Smuzhiyun 		 sta->addr, tid);
740*4882a593Smuzhiyun 
741*4882a593Smuzhiyun 	return iwl_send_lq_cmd(priv, ctx,
742*4882a593Smuzhiyun 			&sta_priv->lq_sta.lq, CMD_ASYNC, false);
743*4882a593Smuzhiyun }
744*4882a593Smuzhiyun 
iwlagn_check_ratid_empty(struct iwl_priv * priv,int sta_id,u8 tid)745*4882a593Smuzhiyun static void iwlagn_check_ratid_empty(struct iwl_priv *priv, int sta_id, u8 tid)
746*4882a593Smuzhiyun {
747*4882a593Smuzhiyun 	struct iwl_tid_data *tid_data = &priv->tid_data[sta_id][tid];
748*4882a593Smuzhiyun 	enum iwl_rxon_context_id ctx;
749*4882a593Smuzhiyun 	struct ieee80211_vif *vif;
750*4882a593Smuzhiyun 	u8 *addr;
751*4882a593Smuzhiyun 
752*4882a593Smuzhiyun 	lockdep_assert_held(&priv->sta_lock);
753*4882a593Smuzhiyun 
754*4882a593Smuzhiyun 	addr = priv->stations[sta_id].sta.sta.addr;
755*4882a593Smuzhiyun 	ctx = priv->stations[sta_id].ctxid;
756*4882a593Smuzhiyun 	vif = priv->contexts[ctx].vif;
757*4882a593Smuzhiyun 
758*4882a593Smuzhiyun 	switch (priv->tid_data[sta_id][tid].agg.state) {
759*4882a593Smuzhiyun 	case IWL_EMPTYING_HW_QUEUE_DELBA:
760*4882a593Smuzhiyun 		/* There are no packets for this RA / TID in the HW any more */
761*4882a593Smuzhiyun 		if (tid_data->agg.ssn == tid_data->next_reclaimed) {
762*4882a593Smuzhiyun 			IWL_DEBUG_TX_QUEUES(priv,
763*4882a593Smuzhiyun 				"Can continue DELBA flow ssn = next_recl = %d\n",
764*4882a593Smuzhiyun 				tid_data->next_reclaimed);
765*4882a593Smuzhiyun 			iwl_trans_txq_disable(priv->trans,
766*4882a593Smuzhiyun 					      tid_data->agg.txq_id, true);
767*4882a593Smuzhiyun 			iwlagn_dealloc_agg_txq(priv, tid_data->agg.txq_id);
768*4882a593Smuzhiyun 			tid_data->agg.state = IWL_AGG_OFF;
769*4882a593Smuzhiyun 			ieee80211_stop_tx_ba_cb_irqsafe(vif, addr, tid);
770*4882a593Smuzhiyun 		}
771*4882a593Smuzhiyun 		break;
772*4882a593Smuzhiyun 	case IWL_EMPTYING_HW_QUEUE_ADDBA:
773*4882a593Smuzhiyun 		/* There are no packets for this RA / TID in the HW any more */
774*4882a593Smuzhiyun 		if (tid_data->agg.ssn == tid_data->next_reclaimed) {
775*4882a593Smuzhiyun 			IWL_DEBUG_TX_QUEUES(priv,
776*4882a593Smuzhiyun 				"Can continue ADDBA flow ssn = next_recl = %d\n",
777*4882a593Smuzhiyun 				tid_data->next_reclaimed);
778*4882a593Smuzhiyun 			tid_data->agg.state = IWL_AGG_STARTING;
779*4882a593Smuzhiyun 			ieee80211_start_tx_ba_cb_irqsafe(vif, addr, tid);
780*4882a593Smuzhiyun 		}
781*4882a593Smuzhiyun 		break;
782*4882a593Smuzhiyun 	default:
783*4882a593Smuzhiyun 		break;
784*4882a593Smuzhiyun 	}
785*4882a593Smuzhiyun }
786*4882a593Smuzhiyun 
iwlagn_non_agg_tx_status(struct iwl_priv * priv,struct iwl_rxon_context * ctx,const u8 * addr1)787*4882a593Smuzhiyun static void iwlagn_non_agg_tx_status(struct iwl_priv *priv,
788*4882a593Smuzhiyun 				     struct iwl_rxon_context *ctx,
789*4882a593Smuzhiyun 				     const u8 *addr1)
790*4882a593Smuzhiyun {
791*4882a593Smuzhiyun 	struct ieee80211_sta *sta;
792*4882a593Smuzhiyun 	struct iwl_station_priv *sta_priv;
793*4882a593Smuzhiyun 
794*4882a593Smuzhiyun 	rcu_read_lock();
795*4882a593Smuzhiyun 	sta = ieee80211_find_sta(ctx->vif, addr1);
796*4882a593Smuzhiyun 	if (sta) {
797*4882a593Smuzhiyun 		sta_priv = (void *)sta->drv_priv;
798*4882a593Smuzhiyun 		/* avoid atomic ops if this isn't a client */
799*4882a593Smuzhiyun 		if (sta_priv->client &&
800*4882a593Smuzhiyun 		    atomic_dec_return(&sta_priv->pending_frames) == 0)
801*4882a593Smuzhiyun 			ieee80211_sta_block_awake(priv->hw, sta, false);
802*4882a593Smuzhiyun 	}
803*4882a593Smuzhiyun 	rcu_read_unlock();
804*4882a593Smuzhiyun }
805*4882a593Smuzhiyun 
806*4882a593Smuzhiyun /*
807*4882a593Smuzhiyun  * translate ucode response to mac80211 tx status control values
808*4882a593Smuzhiyun  */
iwlagn_hwrate_to_tx_control(struct iwl_priv * priv,u32 rate_n_flags,struct ieee80211_tx_info * info)809*4882a593Smuzhiyun static void iwlagn_hwrate_to_tx_control(struct iwl_priv *priv, u32 rate_n_flags,
810*4882a593Smuzhiyun 				  struct ieee80211_tx_info *info)
811*4882a593Smuzhiyun {
812*4882a593Smuzhiyun 	struct ieee80211_tx_rate *r = &info->status.rates[0];
813*4882a593Smuzhiyun 
814*4882a593Smuzhiyun 	info->status.antenna =
815*4882a593Smuzhiyun 		((rate_n_flags & RATE_MCS_ANT_ABC_MSK) >> RATE_MCS_ANT_POS);
816*4882a593Smuzhiyun 	if (rate_n_flags & RATE_MCS_HT_MSK)
817*4882a593Smuzhiyun 		r->flags |= IEEE80211_TX_RC_MCS;
818*4882a593Smuzhiyun 	if (rate_n_flags & RATE_MCS_GF_MSK)
819*4882a593Smuzhiyun 		r->flags |= IEEE80211_TX_RC_GREEN_FIELD;
820*4882a593Smuzhiyun 	if (rate_n_flags & RATE_MCS_HT40_MSK)
821*4882a593Smuzhiyun 		r->flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
822*4882a593Smuzhiyun 	if (rate_n_flags & RATE_MCS_DUP_MSK)
823*4882a593Smuzhiyun 		r->flags |= IEEE80211_TX_RC_DUP_DATA;
824*4882a593Smuzhiyun 	if (rate_n_flags & RATE_MCS_SGI_MSK)
825*4882a593Smuzhiyun 		r->flags |= IEEE80211_TX_RC_SHORT_GI;
826*4882a593Smuzhiyun 	r->idx = iwlagn_hwrate_to_mac80211_idx(rate_n_flags, info->band);
827*4882a593Smuzhiyun }
828*4882a593Smuzhiyun 
829*4882a593Smuzhiyun #ifdef CONFIG_IWLWIFI_DEBUG
iwl_get_tx_fail_reason(u32 status)830*4882a593Smuzhiyun const char *iwl_get_tx_fail_reason(u32 status)
831*4882a593Smuzhiyun {
832*4882a593Smuzhiyun #define TX_STATUS_FAIL(x) case TX_STATUS_FAIL_ ## x: return #x
833*4882a593Smuzhiyun #define TX_STATUS_POSTPONE(x) case TX_STATUS_POSTPONE_ ## x: return #x
834*4882a593Smuzhiyun 
835*4882a593Smuzhiyun 	switch (status & TX_STATUS_MSK) {
836*4882a593Smuzhiyun 	case TX_STATUS_SUCCESS:
837*4882a593Smuzhiyun 		return "SUCCESS";
838*4882a593Smuzhiyun 	TX_STATUS_POSTPONE(DELAY);
839*4882a593Smuzhiyun 	TX_STATUS_POSTPONE(FEW_BYTES);
840*4882a593Smuzhiyun 	TX_STATUS_POSTPONE(BT_PRIO);
841*4882a593Smuzhiyun 	TX_STATUS_POSTPONE(QUIET_PERIOD);
842*4882a593Smuzhiyun 	TX_STATUS_POSTPONE(CALC_TTAK);
843*4882a593Smuzhiyun 	TX_STATUS_FAIL(INTERNAL_CROSSED_RETRY);
844*4882a593Smuzhiyun 	TX_STATUS_FAIL(SHORT_LIMIT);
845*4882a593Smuzhiyun 	TX_STATUS_FAIL(LONG_LIMIT);
846*4882a593Smuzhiyun 	TX_STATUS_FAIL(FIFO_UNDERRUN);
847*4882a593Smuzhiyun 	TX_STATUS_FAIL(DRAIN_FLOW);
848*4882a593Smuzhiyun 	TX_STATUS_FAIL(RFKILL_FLUSH);
849*4882a593Smuzhiyun 	TX_STATUS_FAIL(LIFE_EXPIRE);
850*4882a593Smuzhiyun 	TX_STATUS_FAIL(DEST_PS);
851*4882a593Smuzhiyun 	TX_STATUS_FAIL(HOST_ABORTED);
852*4882a593Smuzhiyun 	TX_STATUS_FAIL(BT_RETRY);
853*4882a593Smuzhiyun 	TX_STATUS_FAIL(STA_INVALID);
854*4882a593Smuzhiyun 	TX_STATUS_FAIL(FRAG_DROPPED);
855*4882a593Smuzhiyun 	TX_STATUS_FAIL(TID_DISABLE);
856*4882a593Smuzhiyun 	TX_STATUS_FAIL(FIFO_FLUSHED);
857*4882a593Smuzhiyun 	TX_STATUS_FAIL(INSUFFICIENT_CF_POLL);
858*4882a593Smuzhiyun 	TX_STATUS_FAIL(PASSIVE_NO_RX);
859*4882a593Smuzhiyun 	TX_STATUS_FAIL(NO_BEACON_ON_RADAR);
860*4882a593Smuzhiyun 	}
861*4882a593Smuzhiyun 
862*4882a593Smuzhiyun 	return "UNKNOWN";
863*4882a593Smuzhiyun 
864*4882a593Smuzhiyun #undef TX_STATUS_FAIL
865*4882a593Smuzhiyun #undef TX_STATUS_POSTPONE
866*4882a593Smuzhiyun }
867*4882a593Smuzhiyun #endif /* CONFIG_IWLWIFI_DEBUG */
868*4882a593Smuzhiyun 
iwlagn_count_agg_tx_err_status(struct iwl_priv * priv,u16 status)869*4882a593Smuzhiyun static void iwlagn_count_agg_tx_err_status(struct iwl_priv *priv, u16 status)
870*4882a593Smuzhiyun {
871*4882a593Smuzhiyun 	status &= AGG_TX_STATUS_MSK;
872*4882a593Smuzhiyun 
873*4882a593Smuzhiyun 	switch (status) {
874*4882a593Smuzhiyun 	case AGG_TX_STATE_UNDERRUN_MSK:
875*4882a593Smuzhiyun 		priv->reply_agg_tx_stats.underrun++;
876*4882a593Smuzhiyun 		break;
877*4882a593Smuzhiyun 	case AGG_TX_STATE_BT_PRIO_MSK:
878*4882a593Smuzhiyun 		priv->reply_agg_tx_stats.bt_prio++;
879*4882a593Smuzhiyun 		break;
880*4882a593Smuzhiyun 	case AGG_TX_STATE_FEW_BYTES_MSK:
881*4882a593Smuzhiyun 		priv->reply_agg_tx_stats.few_bytes++;
882*4882a593Smuzhiyun 		break;
883*4882a593Smuzhiyun 	case AGG_TX_STATE_ABORT_MSK:
884*4882a593Smuzhiyun 		priv->reply_agg_tx_stats.abort++;
885*4882a593Smuzhiyun 		break;
886*4882a593Smuzhiyun 	case AGG_TX_STATE_LAST_SENT_TTL_MSK:
887*4882a593Smuzhiyun 		priv->reply_agg_tx_stats.last_sent_ttl++;
888*4882a593Smuzhiyun 		break;
889*4882a593Smuzhiyun 	case AGG_TX_STATE_LAST_SENT_TRY_CNT_MSK:
890*4882a593Smuzhiyun 		priv->reply_agg_tx_stats.last_sent_try++;
891*4882a593Smuzhiyun 		break;
892*4882a593Smuzhiyun 	case AGG_TX_STATE_LAST_SENT_BT_KILL_MSK:
893*4882a593Smuzhiyun 		priv->reply_agg_tx_stats.last_sent_bt_kill++;
894*4882a593Smuzhiyun 		break;
895*4882a593Smuzhiyun 	case AGG_TX_STATE_SCD_QUERY_MSK:
896*4882a593Smuzhiyun 		priv->reply_agg_tx_stats.scd_query++;
897*4882a593Smuzhiyun 		break;
898*4882a593Smuzhiyun 	case AGG_TX_STATE_TEST_BAD_CRC32_MSK:
899*4882a593Smuzhiyun 		priv->reply_agg_tx_stats.bad_crc32++;
900*4882a593Smuzhiyun 		break;
901*4882a593Smuzhiyun 	case AGG_TX_STATE_RESPONSE_MSK:
902*4882a593Smuzhiyun 		priv->reply_agg_tx_stats.response++;
903*4882a593Smuzhiyun 		break;
904*4882a593Smuzhiyun 	case AGG_TX_STATE_DUMP_TX_MSK:
905*4882a593Smuzhiyun 		priv->reply_agg_tx_stats.dump_tx++;
906*4882a593Smuzhiyun 		break;
907*4882a593Smuzhiyun 	case AGG_TX_STATE_DELAY_TX_MSK:
908*4882a593Smuzhiyun 		priv->reply_agg_tx_stats.delay_tx++;
909*4882a593Smuzhiyun 		break;
910*4882a593Smuzhiyun 	default:
911*4882a593Smuzhiyun 		priv->reply_agg_tx_stats.unknown++;
912*4882a593Smuzhiyun 		break;
913*4882a593Smuzhiyun 	}
914*4882a593Smuzhiyun }
915*4882a593Smuzhiyun 
iwlagn_get_scd_ssn(struct iwlagn_tx_resp * tx_resp)916*4882a593Smuzhiyun static inline u32 iwlagn_get_scd_ssn(struct iwlagn_tx_resp *tx_resp)
917*4882a593Smuzhiyun {
918*4882a593Smuzhiyun 	return le32_to_cpup((__le32 *)&tx_resp->status +
919*4882a593Smuzhiyun 			    tx_resp->frame_count) & IEEE80211_MAX_SN;
920*4882a593Smuzhiyun }
921*4882a593Smuzhiyun 
iwl_rx_reply_tx_agg(struct iwl_priv * priv,struct iwlagn_tx_resp * tx_resp)922*4882a593Smuzhiyun static void iwl_rx_reply_tx_agg(struct iwl_priv *priv,
923*4882a593Smuzhiyun 				struct iwlagn_tx_resp *tx_resp)
924*4882a593Smuzhiyun {
925*4882a593Smuzhiyun 	struct agg_tx_status *frame_status = &tx_resp->status;
926*4882a593Smuzhiyun 	int tid = (tx_resp->ra_tid & IWLAGN_TX_RES_TID_MSK) >>
927*4882a593Smuzhiyun 		IWLAGN_TX_RES_TID_POS;
928*4882a593Smuzhiyun 	int sta_id = (tx_resp->ra_tid & IWLAGN_TX_RES_RA_MSK) >>
929*4882a593Smuzhiyun 		IWLAGN_TX_RES_RA_POS;
930*4882a593Smuzhiyun 	struct iwl_ht_agg *agg = &priv->tid_data[sta_id][tid].agg;
931*4882a593Smuzhiyun 	u32 status = le16_to_cpu(tx_resp->status.status);
932*4882a593Smuzhiyun 	int i;
933*4882a593Smuzhiyun 
934*4882a593Smuzhiyun 	WARN_ON(tid == IWL_TID_NON_QOS);
935*4882a593Smuzhiyun 
936*4882a593Smuzhiyun 	if (agg->wait_for_ba)
937*4882a593Smuzhiyun 		IWL_DEBUG_TX_REPLY(priv,
938*4882a593Smuzhiyun 			"got tx response w/o block-ack\n");
939*4882a593Smuzhiyun 
940*4882a593Smuzhiyun 	agg->rate_n_flags = le32_to_cpu(tx_resp->rate_n_flags);
941*4882a593Smuzhiyun 	agg->wait_for_ba = (tx_resp->frame_count > 1);
942*4882a593Smuzhiyun 
943*4882a593Smuzhiyun 	/*
944*4882a593Smuzhiyun 	 * If the BT kill count is non-zero, we'll get this
945*4882a593Smuzhiyun 	 * notification again.
946*4882a593Smuzhiyun 	 */
947*4882a593Smuzhiyun 	if (tx_resp->bt_kill_count && tx_resp->frame_count == 1 &&
948*4882a593Smuzhiyun 	    priv->lib->bt_params &&
949*4882a593Smuzhiyun 	    priv->lib->bt_params->advanced_bt_coexist) {
950*4882a593Smuzhiyun 		IWL_DEBUG_COEX(priv, "receive reply tx w/ bt_kill\n");
951*4882a593Smuzhiyun 	}
952*4882a593Smuzhiyun 
953*4882a593Smuzhiyun 	if (tx_resp->frame_count == 1)
954*4882a593Smuzhiyun 		return;
955*4882a593Smuzhiyun 
956*4882a593Smuzhiyun 	IWL_DEBUG_TX_REPLY(priv, "TXQ %d initial_rate 0x%x ssn %d frm_cnt %d\n",
957*4882a593Smuzhiyun 			   agg->txq_id,
958*4882a593Smuzhiyun 			   le32_to_cpu(tx_resp->rate_n_flags),
959*4882a593Smuzhiyun 			   iwlagn_get_scd_ssn(tx_resp), tx_resp->frame_count);
960*4882a593Smuzhiyun 
961*4882a593Smuzhiyun 	/* Construct bit-map of pending frames within Tx window */
962*4882a593Smuzhiyun 	for (i = 0; i < tx_resp->frame_count; i++) {
963*4882a593Smuzhiyun 		u16 fstatus = le16_to_cpu(frame_status[i].status);
964*4882a593Smuzhiyun 		u8 retry_cnt = (fstatus & AGG_TX_TRY_MSK) >> AGG_TX_TRY_POS;
965*4882a593Smuzhiyun 
966*4882a593Smuzhiyun 		if (status & AGG_TX_STATUS_MSK)
967*4882a593Smuzhiyun 			iwlagn_count_agg_tx_err_status(priv, fstatus);
968*4882a593Smuzhiyun 
969*4882a593Smuzhiyun 		if (status & (AGG_TX_STATE_FEW_BYTES_MSK |
970*4882a593Smuzhiyun 			      AGG_TX_STATE_ABORT_MSK))
971*4882a593Smuzhiyun 			continue;
972*4882a593Smuzhiyun 
973*4882a593Smuzhiyun 		if (status & AGG_TX_STATUS_MSK || retry_cnt > 1)
974*4882a593Smuzhiyun 			IWL_DEBUG_TX_REPLY(priv,
975*4882a593Smuzhiyun 					   "%d: status %s (0x%04x), try-count (0x%01x)\n",
976*4882a593Smuzhiyun 					   i,
977*4882a593Smuzhiyun 					   iwl_get_agg_tx_fail_reason(fstatus),
978*4882a593Smuzhiyun 					   fstatus & AGG_TX_STATUS_MSK,
979*4882a593Smuzhiyun 					   retry_cnt);
980*4882a593Smuzhiyun 	}
981*4882a593Smuzhiyun }
982*4882a593Smuzhiyun 
983*4882a593Smuzhiyun #ifdef CONFIG_IWLWIFI_DEBUG
984*4882a593Smuzhiyun #define AGG_TX_STATE_FAIL(x) case AGG_TX_STATE_ ## x: return #x
985*4882a593Smuzhiyun 
iwl_get_agg_tx_fail_reason(u16 status)986*4882a593Smuzhiyun const char *iwl_get_agg_tx_fail_reason(u16 status)
987*4882a593Smuzhiyun {
988*4882a593Smuzhiyun 	status &= AGG_TX_STATUS_MSK;
989*4882a593Smuzhiyun 	switch (status) {
990*4882a593Smuzhiyun 	case AGG_TX_STATE_TRANSMITTED:
991*4882a593Smuzhiyun 		return "SUCCESS";
992*4882a593Smuzhiyun 		AGG_TX_STATE_FAIL(UNDERRUN_MSK);
993*4882a593Smuzhiyun 		AGG_TX_STATE_FAIL(BT_PRIO_MSK);
994*4882a593Smuzhiyun 		AGG_TX_STATE_FAIL(FEW_BYTES_MSK);
995*4882a593Smuzhiyun 		AGG_TX_STATE_FAIL(ABORT_MSK);
996*4882a593Smuzhiyun 		AGG_TX_STATE_FAIL(LAST_SENT_TTL_MSK);
997*4882a593Smuzhiyun 		AGG_TX_STATE_FAIL(LAST_SENT_TRY_CNT_MSK);
998*4882a593Smuzhiyun 		AGG_TX_STATE_FAIL(LAST_SENT_BT_KILL_MSK);
999*4882a593Smuzhiyun 		AGG_TX_STATE_FAIL(SCD_QUERY_MSK);
1000*4882a593Smuzhiyun 		AGG_TX_STATE_FAIL(TEST_BAD_CRC32_MSK);
1001*4882a593Smuzhiyun 		AGG_TX_STATE_FAIL(RESPONSE_MSK);
1002*4882a593Smuzhiyun 		AGG_TX_STATE_FAIL(DUMP_TX_MSK);
1003*4882a593Smuzhiyun 		AGG_TX_STATE_FAIL(DELAY_TX_MSK);
1004*4882a593Smuzhiyun 	}
1005*4882a593Smuzhiyun 
1006*4882a593Smuzhiyun 	return "UNKNOWN";
1007*4882a593Smuzhiyun }
1008*4882a593Smuzhiyun #endif /* CONFIG_IWLWIFI_DEBUG */
1009*4882a593Smuzhiyun 
iwlagn_count_tx_err_status(struct iwl_priv * priv,u16 status)1010*4882a593Smuzhiyun static void iwlagn_count_tx_err_status(struct iwl_priv *priv, u16 status)
1011*4882a593Smuzhiyun {
1012*4882a593Smuzhiyun 	status &= TX_STATUS_MSK;
1013*4882a593Smuzhiyun 
1014*4882a593Smuzhiyun 	switch (status) {
1015*4882a593Smuzhiyun 	case TX_STATUS_POSTPONE_DELAY:
1016*4882a593Smuzhiyun 		priv->reply_tx_stats.pp_delay++;
1017*4882a593Smuzhiyun 		break;
1018*4882a593Smuzhiyun 	case TX_STATUS_POSTPONE_FEW_BYTES:
1019*4882a593Smuzhiyun 		priv->reply_tx_stats.pp_few_bytes++;
1020*4882a593Smuzhiyun 		break;
1021*4882a593Smuzhiyun 	case TX_STATUS_POSTPONE_BT_PRIO:
1022*4882a593Smuzhiyun 		priv->reply_tx_stats.pp_bt_prio++;
1023*4882a593Smuzhiyun 		break;
1024*4882a593Smuzhiyun 	case TX_STATUS_POSTPONE_QUIET_PERIOD:
1025*4882a593Smuzhiyun 		priv->reply_tx_stats.pp_quiet_period++;
1026*4882a593Smuzhiyun 		break;
1027*4882a593Smuzhiyun 	case TX_STATUS_POSTPONE_CALC_TTAK:
1028*4882a593Smuzhiyun 		priv->reply_tx_stats.pp_calc_ttak++;
1029*4882a593Smuzhiyun 		break;
1030*4882a593Smuzhiyun 	case TX_STATUS_FAIL_INTERNAL_CROSSED_RETRY:
1031*4882a593Smuzhiyun 		priv->reply_tx_stats.int_crossed_retry++;
1032*4882a593Smuzhiyun 		break;
1033*4882a593Smuzhiyun 	case TX_STATUS_FAIL_SHORT_LIMIT:
1034*4882a593Smuzhiyun 		priv->reply_tx_stats.short_limit++;
1035*4882a593Smuzhiyun 		break;
1036*4882a593Smuzhiyun 	case TX_STATUS_FAIL_LONG_LIMIT:
1037*4882a593Smuzhiyun 		priv->reply_tx_stats.long_limit++;
1038*4882a593Smuzhiyun 		break;
1039*4882a593Smuzhiyun 	case TX_STATUS_FAIL_FIFO_UNDERRUN:
1040*4882a593Smuzhiyun 		priv->reply_tx_stats.fifo_underrun++;
1041*4882a593Smuzhiyun 		break;
1042*4882a593Smuzhiyun 	case TX_STATUS_FAIL_DRAIN_FLOW:
1043*4882a593Smuzhiyun 		priv->reply_tx_stats.drain_flow++;
1044*4882a593Smuzhiyun 		break;
1045*4882a593Smuzhiyun 	case TX_STATUS_FAIL_RFKILL_FLUSH:
1046*4882a593Smuzhiyun 		priv->reply_tx_stats.rfkill_flush++;
1047*4882a593Smuzhiyun 		break;
1048*4882a593Smuzhiyun 	case TX_STATUS_FAIL_LIFE_EXPIRE:
1049*4882a593Smuzhiyun 		priv->reply_tx_stats.life_expire++;
1050*4882a593Smuzhiyun 		break;
1051*4882a593Smuzhiyun 	case TX_STATUS_FAIL_DEST_PS:
1052*4882a593Smuzhiyun 		priv->reply_tx_stats.dest_ps++;
1053*4882a593Smuzhiyun 		break;
1054*4882a593Smuzhiyun 	case TX_STATUS_FAIL_HOST_ABORTED:
1055*4882a593Smuzhiyun 		priv->reply_tx_stats.host_abort++;
1056*4882a593Smuzhiyun 		break;
1057*4882a593Smuzhiyun 	case TX_STATUS_FAIL_BT_RETRY:
1058*4882a593Smuzhiyun 		priv->reply_tx_stats.bt_retry++;
1059*4882a593Smuzhiyun 		break;
1060*4882a593Smuzhiyun 	case TX_STATUS_FAIL_STA_INVALID:
1061*4882a593Smuzhiyun 		priv->reply_tx_stats.sta_invalid++;
1062*4882a593Smuzhiyun 		break;
1063*4882a593Smuzhiyun 	case TX_STATUS_FAIL_FRAG_DROPPED:
1064*4882a593Smuzhiyun 		priv->reply_tx_stats.frag_drop++;
1065*4882a593Smuzhiyun 		break;
1066*4882a593Smuzhiyun 	case TX_STATUS_FAIL_TID_DISABLE:
1067*4882a593Smuzhiyun 		priv->reply_tx_stats.tid_disable++;
1068*4882a593Smuzhiyun 		break;
1069*4882a593Smuzhiyun 	case TX_STATUS_FAIL_FIFO_FLUSHED:
1070*4882a593Smuzhiyun 		priv->reply_tx_stats.fifo_flush++;
1071*4882a593Smuzhiyun 		break;
1072*4882a593Smuzhiyun 	case TX_STATUS_FAIL_INSUFFICIENT_CF_POLL:
1073*4882a593Smuzhiyun 		priv->reply_tx_stats.insuff_cf_poll++;
1074*4882a593Smuzhiyun 		break;
1075*4882a593Smuzhiyun 	case TX_STATUS_FAIL_PASSIVE_NO_RX:
1076*4882a593Smuzhiyun 		priv->reply_tx_stats.fail_hw_drop++;
1077*4882a593Smuzhiyun 		break;
1078*4882a593Smuzhiyun 	case TX_STATUS_FAIL_NO_BEACON_ON_RADAR:
1079*4882a593Smuzhiyun 		priv->reply_tx_stats.sta_color_mismatch++;
1080*4882a593Smuzhiyun 		break;
1081*4882a593Smuzhiyun 	default:
1082*4882a593Smuzhiyun 		priv->reply_tx_stats.unknown++;
1083*4882a593Smuzhiyun 		break;
1084*4882a593Smuzhiyun 	}
1085*4882a593Smuzhiyun }
1086*4882a593Smuzhiyun 
iwlagn_set_tx_status(struct iwl_priv * priv,struct ieee80211_tx_info * info,struct iwlagn_tx_resp * tx_resp)1087*4882a593Smuzhiyun static void iwlagn_set_tx_status(struct iwl_priv *priv,
1088*4882a593Smuzhiyun 				 struct ieee80211_tx_info *info,
1089*4882a593Smuzhiyun 				 struct iwlagn_tx_resp *tx_resp)
1090*4882a593Smuzhiyun {
1091*4882a593Smuzhiyun 	u16 status = le16_to_cpu(tx_resp->status.status);
1092*4882a593Smuzhiyun 
1093*4882a593Smuzhiyun 	info->flags &= ~IEEE80211_TX_CTL_AMPDU;
1094*4882a593Smuzhiyun 
1095*4882a593Smuzhiyun 	info->status.rates[0].count = tx_resp->failure_frame + 1;
1096*4882a593Smuzhiyun 	info->flags |= iwl_tx_status_to_mac80211(status);
1097*4882a593Smuzhiyun 	iwlagn_hwrate_to_tx_control(priv, le32_to_cpu(tx_resp->rate_n_flags),
1098*4882a593Smuzhiyun 				    info);
1099*4882a593Smuzhiyun 	if (!iwl_is_tx_success(status))
1100*4882a593Smuzhiyun 		iwlagn_count_tx_err_status(priv, status);
1101*4882a593Smuzhiyun }
1102*4882a593Smuzhiyun 
iwl_check_abort_status(struct iwl_priv * priv,u8 frame_count,u32 status)1103*4882a593Smuzhiyun static void iwl_check_abort_status(struct iwl_priv *priv,
1104*4882a593Smuzhiyun 			    u8 frame_count, u32 status)
1105*4882a593Smuzhiyun {
1106*4882a593Smuzhiyun 	if (frame_count == 1 && status == TX_STATUS_FAIL_RFKILL_FLUSH) {
1107*4882a593Smuzhiyun 		IWL_ERR(priv, "Tx flush command to flush out all frames\n");
1108*4882a593Smuzhiyun 		if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
1109*4882a593Smuzhiyun 			queue_work(priv->workqueue, &priv->tx_flush);
1110*4882a593Smuzhiyun 	}
1111*4882a593Smuzhiyun }
1112*4882a593Smuzhiyun 
iwlagn_rx_reply_tx(struct iwl_priv * priv,struct iwl_rx_cmd_buffer * rxb)1113*4882a593Smuzhiyun void iwlagn_rx_reply_tx(struct iwl_priv *priv, struct iwl_rx_cmd_buffer *rxb)
1114*4882a593Smuzhiyun {
1115*4882a593Smuzhiyun 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1116*4882a593Smuzhiyun 	u16 sequence = le16_to_cpu(pkt->hdr.sequence);
1117*4882a593Smuzhiyun 	int txq_id = SEQ_TO_QUEUE(sequence);
1118*4882a593Smuzhiyun 	int cmd_index __maybe_unused = SEQ_TO_INDEX(sequence);
1119*4882a593Smuzhiyun 	struct iwlagn_tx_resp *tx_resp = (void *)pkt->data;
1120*4882a593Smuzhiyun 	struct ieee80211_hdr *hdr;
1121*4882a593Smuzhiyun 	u32 status = le16_to_cpu(tx_resp->status.status);
1122*4882a593Smuzhiyun 	u16 ssn = iwlagn_get_scd_ssn(tx_resp);
1123*4882a593Smuzhiyun 	int tid;
1124*4882a593Smuzhiyun 	int sta_id;
1125*4882a593Smuzhiyun 	int freed;
1126*4882a593Smuzhiyun 	struct ieee80211_tx_info *info;
1127*4882a593Smuzhiyun 	struct sk_buff_head skbs;
1128*4882a593Smuzhiyun 	struct sk_buff *skb;
1129*4882a593Smuzhiyun 	struct iwl_rxon_context *ctx;
1130*4882a593Smuzhiyun 	bool is_agg = (txq_id >= IWLAGN_FIRST_AMPDU_QUEUE);
1131*4882a593Smuzhiyun 
1132*4882a593Smuzhiyun 	tid = (tx_resp->ra_tid & IWLAGN_TX_RES_TID_MSK) >>
1133*4882a593Smuzhiyun 		IWLAGN_TX_RES_TID_POS;
1134*4882a593Smuzhiyun 	sta_id = (tx_resp->ra_tid & IWLAGN_TX_RES_RA_MSK) >>
1135*4882a593Smuzhiyun 		IWLAGN_TX_RES_RA_POS;
1136*4882a593Smuzhiyun 
1137*4882a593Smuzhiyun 	spin_lock_bh(&priv->sta_lock);
1138*4882a593Smuzhiyun 
1139*4882a593Smuzhiyun 	if (is_agg) {
1140*4882a593Smuzhiyun 		WARN_ON_ONCE(sta_id >= IWLAGN_STATION_COUNT ||
1141*4882a593Smuzhiyun 			     tid >= IWL_MAX_TID_COUNT);
1142*4882a593Smuzhiyun 		if (txq_id != priv->tid_data[sta_id][tid].agg.txq_id)
1143*4882a593Smuzhiyun 			IWL_ERR(priv, "txq_id mismatch: %d %d\n", txq_id,
1144*4882a593Smuzhiyun 				priv->tid_data[sta_id][tid].agg.txq_id);
1145*4882a593Smuzhiyun 		iwl_rx_reply_tx_agg(priv, tx_resp);
1146*4882a593Smuzhiyun 	}
1147*4882a593Smuzhiyun 
1148*4882a593Smuzhiyun 	__skb_queue_head_init(&skbs);
1149*4882a593Smuzhiyun 
1150*4882a593Smuzhiyun 	if (tx_resp->frame_count == 1) {
1151*4882a593Smuzhiyun 		u16 next_reclaimed = le16_to_cpu(tx_resp->seq_ctl);
1152*4882a593Smuzhiyun 		next_reclaimed = IEEE80211_SEQ_TO_SN(next_reclaimed + 0x10);
1153*4882a593Smuzhiyun 
1154*4882a593Smuzhiyun 		if (is_agg) {
1155*4882a593Smuzhiyun 			/* If this is an aggregation queue, we can rely on the
1156*4882a593Smuzhiyun 			 * ssn since the wifi sequence number corresponds to
1157*4882a593Smuzhiyun 			 * the index in the TFD ring (%256).
1158*4882a593Smuzhiyun 			 * The seq_ctl is the sequence control of the packet
1159*4882a593Smuzhiyun 			 * to which this Tx response relates. But if there is a
1160*4882a593Smuzhiyun 			 * hole in the bitmap of the BA we received, this Tx
1161*4882a593Smuzhiyun 			 * response may allow to reclaim the hole and all the
1162*4882a593Smuzhiyun 			 * subsequent packets that were already acked.
1163*4882a593Smuzhiyun 			 * In that case, seq_ctl != ssn, and the next packet
1164*4882a593Smuzhiyun 			 * to be reclaimed will be ssn and not seq_ctl.
1165*4882a593Smuzhiyun 			 */
1166*4882a593Smuzhiyun 			next_reclaimed = ssn;
1167*4882a593Smuzhiyun 		}
1168*4882a593Smuzhiyun 
1169*4882a593Smuzhiyun 		if (tid != IWL_TID_NON_QOS) {
1170*4882a593Smuzhiyun 			priv->tid_data[sta_id][tid].next_reclaimed =
1171*4882a593Smuzhiyun 				next_reclaimed;
1172*4882a593Smuzhiyun 			IWL_DEBUG_TX_REPLY(priv, "Next reclaimed packet:%d\n",
1173*4882a593Smuzhiyun 						  next_reclaimed);
1174*4882a593Smuzhiyun 			iwlagn_check_ratid_empty(priv, sta_id, tid);
1175*4882a593Smuzhiyun 		}
1176*4882a593Smuzhiyun 
1177*4882a593Smuzhiyun 		iwl_trans_reclaim(priv->trans, txq_id, ssn, &skbs);
1178*4882a593Smuzhiyun 
1179*4882a593Smuzhiyun 		freed = 0;
1180*4882a593Smuzhiyun 
1181*4882a593Smuzhiyun 		/* process frames */
1182*4882a593Smuzhiyun 		skb_queue_walk(&skbs, skb) {
1183*4882a593Smuzhiyun 			hdr = (struct ieee80211_hdr *)skb->data;
1184*4882a593Smuzhiyun 
1185*4882a593Smuzhiyun 			if (!ieee80211_is_data_qos(hdr->frame_control))
1186*4882a593Smuzhiyun 				priv->last_seq_ctl = tx_resp->seq_ctl;
1187*4882a593Smuzhiyun 
1188*4882a593Smuzhiyun 			info = IEEE80211_SKB_CB(skb);
1189*4882a593Smuzhiyun 			ctx = info->driver_data[0];
1190*4882a593Smuzhiyun 			iwl_trans_free_tx_cmd(priv->trans,
1191*4882a593Smuzhiyun 					      info->driver_data[1]);
1192*4882a593Smuzhiyun 
1193*4882a593Smuzhiyun 			memset(&info->status, 0, sizeof(info->status));
1194*4882a593Smuzhiyun 
1195*4882a593Smuzhiyun 			if (status == TX_STATUS_FAIL_PASSIVE_NO_RX &&
1196*4882a593Smuzhiyun 			    ctx->vif &&
1197*4882a593Smuzhiyun 			    ctx->vif->type == NL80211_IFTYPE_STATION) {
1198*4882a593Smuzhiyun 				/* block and stop all queues */
1199*4882a593Smuzhiyun 				priv->passive_no_rx = true;
1200*4882a593Smuzhiyun 				IWL_DEBUG_TX_QUEUES(priv,
1201*4882a593Smuzhiyun 					"stop all queues: passive channel\n");
1202*4882a593Smuzhiyun 				ieee80211_stop_queues(priv->hw);
1203*4882a593Smuzhiyun 
1204*4882a593Smuzhiyun 				IWL_DEBUG_TX_REPLY(priv,
1205*4882a593Smuzhiyun 					   "TXQ %d status %s (0x%08x) "
1206*4882a593Smuzhiyun 					   "rate_n_flags 0x%x retries %d\n",
1207*4882a593Smuzhiyun 					   txq_id,
1208*4882a593Smuzhiyun 					   iwl_get_tx_fail_reason(status),
1209*4882a593Smuzhiyun 					   status,
1210*4882a593Smuzhiyun 					   le32_to_cpu(tx_resp->rate_n_flags),
1211*4882a593Smuzhiyun 					   tx_resp->failure_frame);
1212*4882a593Smuzhiyun 
1213*4882a593Smuzhiyun 				IWL_DEBUG_TX_REPLY(priv,
1214*4882a593Smuzhiyun 					   "FrameCnt = %d, idx=%d\n",
1215*4882a593Smuzhiyun 					   tx_resp->frame_count, cmd_index);
1216*4882a593Smuzhiyun 			}
1217*4882a593Smuzhiyun 
1218*4882a593Smuzhiyun 			/* check if BAR is needed */
1219*4882a593Smuzhiyun 			if (is_agg && !iwl_is_tx_success(status))
1220*4882a593Smuzhiyun 				info->flags |= IEEE80211_TX_STAT_AMPDU_NO_BACK;
1221*4882a593Smuzhiyun 			iwlagn_set_tx_status(priv, IEEE80211_SKB_CB(skb),
1222*4882a593Smuzhiyun 				     tx_resp);
1223*4882a593Smuzhiyun 			if (!is_agg)
1224*4882a593Smuzhiyun 				iwlagn_non_agg_tx_status(priv, ctx, hdr->addr1);
1225*4882a593Smuzhiyun 
1226*4882a593Smuzhiyun 			freed++;
1227*4882a593Smuzhiyun 		}
1228*4882a593Smuzhiyun 
1229*4882a593Smuzhiyun 		if (tid != IWL_TID_NON_QOS) {
1230*4882a593Smuzhiyun 			priv->tid_data[sta_id][tid].next_reclaimed =
1231*4882a593Smuzhiyun 				next_reclaimed;
1232*4882a593Smuzhiyun 			IWL_DEBUG_TX_REPLY(priv, "Next reclaimed packet:%d\n",
1233*4882a593Smuzhiyun 					   next_reclaimed);
1234*4882a593Smuzhiyun 		}
1235*4882a593Smuzhiyun 
1236*4882a593Smuzhiyun 		if (!is_agg && freed != 1)
1237*4882a593Smuzhiyun 			IWL_ERR(priv, "Q: %d, freed %d\n", txq_id, freed);
1238*4882a593Smuzhiyun 
1239*4882a593Smuzhiyun 		IWL_DEBUG_TX_REPLY(priv, "TXQ %d status %s (0x%08x)\n", txq_id,
1240*4882a593Smuzhiyun 				   iwl_get_tx_fail_reason(status), status);
1241*4882a593Smuzhiyun 
1242*4882a593Smuzhiyun 		IWL_DEBUG_TX_REPLY(priv,
1243*4882a593Smuzhiyun 				   "\t\t\t\tinitial_rate 0x%x retries %d, idx=%d ssn=%d seq_ctl=0x%x\n",
1244*4882a593Smuzhiyun 				   le32_to_cpu(tx_resp->rate_n_flags),
1245*4882a593Smuzhiyun 				   tx_resp->failure_frame,
1246*4882a593Smuzhiyun 				   SEQ_TO_INDEX(sequence), ssn,
1247*4882a593Smuzhiyun 				   le16_to_cpu(tx_resp->seq_ctl));
1248*4882a593Smuzhiyun 	}
1249*4882a593Smuzhiyun 
1250*4882a593Smuzhiyun 	iwl_check_abort_status(priv, tx_resp->frame_count, status);
1251*4882a593Smuzhiyun 	spin_unlock_bh(&priv->sta_lock);
1252*4882a593Smuzhiyun 
1253*4882a593Smuzhiyun 	while (!skb_queue_empty(&skbs)) {
1254*4882a593Smuzhiyun 		skb = __skb_dequeue(&skbs);
1255*4882a593Smuzhiyun 		ieee80211_tx_status(priv->hw, skb);
1256*4882a593Smuzhiyun 	}
1257*4882a593Smuzhiyun }
1258*4882a593Smuzhiyun 
1259*4882a593Smuzhiyun /*
1260*4882a593Smuzhiyun  * iwlagn_rx_reply_compressed_ba - Handler for REPLY_COMPRESSED_BA
1261*4882a593Smuzhiyun  *
1262*4882a593Smuzhiyun  * Handles block-acknowledge notification from device, which reports success
1263*4882a593Smuzhiyun  * of frames sent via aggregation.
1264*4882a593Smuzhiyun  */
iwlagn_rx_reply_compressed_ba(struct iwl_priv * priv,struct iwl_rx_cmd_buffer * rxb)1265*4882a593Smuzhiyun void iwlagn_rx_reply_compressed_ba(struct iwl_priv *priv,
1266*4882a593Smuzhiyun 				   struct iwl_rx_cmd_buffer *rxb)
1267*4882a593Smuzhiyun {
1268*4882a593Smuzhiyun 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1269*4882a593Smuzhiyun 	struct iwl_compressed_ba_resp *ba_resp = (void *)pkt->data;
1270*4882a593Smuzhiyun 	struct iwl_ht_agg *agg;
1271*4882a593Smuzhiyun 	struct sk_buff_head reclaimed_skbs;
1272*4882a593Smuzhiyun 	struct sk_buff *skb;
1273*4882a593Smuzhiyun 	int sta_id;
1274*4882a593Smuzhiyun 	int tid;
1275*4882a593Smuzhiyun 	int freed;
1276*4882a593Smuzhiyun 
1277*4882a593Smuzhiyun 	/* "flow" corresponds to Tx queue */
1278*4882a593Smuzhiyun 	u16 scd_flow = le16_to_cpu(ba_resp->scd_flow);
1279*4882a593Smuzhiyun 
1280*4882a593Smuzhiyun 	/* "ssn" is start of block-ack Tx window, corresponds to index
1281*4882a593Smuzhiyun 	 * (in Tx queue's circular buffer) of first TFD/frame in window */
1282*4882a593Smuzhiyun 	u16 ba_resp_scd_ssn = le16_to_cpu(ba_resp->scd_ssn);
1283*4882a593Smuzhiyun 
1284*4882a593Smuzhiyun 	if (scd_flow >= priv->trans->trans_cfg->base_params->num_of_queues) {
1285*4882a593Smuzhiyun 		IWL_ERR(priv,
1286*4882a593Smuzhiyun 			"BUG_ON scd_flow is bigger than number of queues\n");
1287*4882a593Smuzhiyun 		return;
1288*4882a593Smuzhiyun 	}
1289*4882a593Smuzhiyun 
1290*4882a593Smuzhiyun 	sta_id = ba_resp->sta_id;
1291*4882a593Smuzhiyun 	tid = ba_resp->tid;
1292*4882a593Smuzhiyun 	agg = &priv->tid_data[sta_id][tid].agg;
1293*4882a593Smuzhiyun 
1294*4882a593Smuzhiyun 	spin_lock_bh(&priv->sta_lock);
1295*4882a593Smuzhiyun 
1296*4882a593Smuzhiyun 	if (unlikely(!agg->wait_for_ba)) {
1297*4882a593Smuzhiyun 		if (unlikely(ba_resp->bitmap))
1298*4882a593Smuzhiyun 			IWL_ERR(priv, "Received BA when not expected\n");
1299*4882a593Smuzhiyun 		spin_unlock_bh(&priv->sta_lock);
1300*4882a593Smuzhiyun 		return;
1301*4882a593Smuzhiyun 	}
1302*4882a593Smuzhiyun 
1303*4882a593Smuzhiyun 	if (unlikely(scd_flow != agg->txq_id)) {
1304*4882a593Smuzhiyun 		/*
1305*4882a593Smuzhiyun 		 * FIXME: this is a uCode bug which need to be addressed,
1306*4882a593Smuzhiyun 		 * log the information and return for now.
1307*4882a593Smuzhiyun 		 * Since it is can possibly happen very often and in order
1308*4882a593Smuzhiyun 		 * not to fill the syslog, don't use IWL_ERR or IWL_WARN
1309*4882a593Smuzhiyun 		 */
1310*4882a593Smuzhiyun 		IWL_DEBUG_TX_QUEUES(priv,
1311*4882a593Smuzhiyun 				    "Bad queue mapping txq_id=%d, agg_txq[sta:%d,tid:%d]=%d\n",
1312*4882a593Smuzhiyun 				    scd_flow, sta_id, tid, agg->txq_id);
1313*4882a593Smuzhiyun 		spin_unlock_bh(&priv->sta_lock);
1314*4882a593Smuzhiyun 		return;
1315*4882a593Smuzhiyun 	}
1316*4882a593Smuzhiyun 
1317*4882a593Smuzhiyun 	__skb_queue_head_init(&reclaimed_skbs);
1318*4882a593Smuzhiyun 
1319*4882a593Smuzhiyun 	/* Release all TFDs before the SSN, i.e. all TFDs in front of
1320*4882a593Smuzhiyun 	 * block-ack window (we assume that they've been successfully
1321*4882a593Smuzhiyun 	 * transmitted ... if not, it's too late anyway). */
1322*4882a593Smuzhiyun 	iwl_trans_reclaim(priv->trans, scd_flow, ba_resp_scd_ssn,
1323*4882a593Smuzhiyun 			  &reclaimed_skbs);
1324*4882a593Smuzhiyun 
1325*4882a593Smuzhiyun 	IWL_DEBUG_TX_REPLY(priv, "REPLY_COMPRESSED_BA [%d] Received from %pM, "
1326*4882a593Smuzhiyun 			   "sta_id = %d\n",
1327*4882a593Smuzhiyun 			   agg->wait_for_ba,
1328*4882a593Smuzhiyun 			   (u8 *) &ba_resp->sta_addr_lo32,
1329*4882a593Smuzhiyun 			   ba_resp->sta_id);
1330*4882a593Smuzhiyun 	IWL_DEBUG_TX_REPLY(priv, "TID = %d, SeqCtl = %d, bitmap = 0x%llx, "
1331*4882a593Smuzhiyun 			   "scd_flow = %d, scd_ssn = %d sent:%d, acked:%d\n",
1332*4882a593Smuzhiyun 			   ba_resp->tid, le16_to_cpu(ba_resp->seq_ctl),
1333*4882a593Smuzhiyun 			   (unsigned long long)le64_to_cpu(ba_resp->bitmap),
1334*4882a593Smuzhiyun 			   scd_flow, ba_resp_scd_ssn, ba_resp->txed,
1335*4882a593Smuzhiyun 			   ba_resp->txed_2_done);
1336*4882a593Smuzhiyun 
1337*4882a593Smuzhiyun 	/* Mark that the expected block-ack response arrived */
1338*4882a593Smuzhiyun 	agg->wait_for_ba = false;
1339*4882a593Smuzhiyun 
1340*4882a593Smuzhiyun 	/* Sanity check values reported by uCode */
1341*4882a593Smuzhiyun 	if (ba_resp->txed_2_done > ba_resp->txed) {
1342*4882a593Smuzhiyun 		IWL_DEBUG_TX_REPLY(priv,
1343*4882a593Smuzhiyun 			"bogus sent(%d) and ack(%d) count\n",
1344*4882a593Smuzhiyun 			ba_resp->txed, ba_resp->txed_2_done);
1345*4882a593Smuzhiyun 		/*
1346*4882a593Smuzhiyun 		 * set txed_2_done = txed,
1347*4882a593Smuzhiyun 		 * so it won't impact rate scale
1348*4882a593Smuzhiyun 		 */
1349*4882a593Smuzhiyun 		ba_resp->txed = ba_resp->txed_2_done;
1350*4882a593Smuzhiyun 	}
1351*4882a593Smuzhiyun 
1352*4882a593Smuzhiyun 	priv->tid_data[sta_id][tid].next_reclaimed = ba_resp_scd_ssn;
1353*4882a593Smuzhiyun 
1354*4882a593Smuzhiyun 	iwlagn_check_ratid_empty(priv, sta_id, tid);
1355*4882a593Smuzhiyun 	freed = 0;
1356*4882a593Smuzhiyun 
1357*4882a593Smuzhiyun 	skb_queue_walk(&reclaimed_skbs, skb) {
1358*4882a593Smuzhiyun 		struct ieee80211_hdr *hdr = (void *)skb->data;
1359*4882a593Smuzhiyun 		struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1360*4882a593Smuzhiyun 
1361*4882a593Smuzhiyun 		if (ieee80211_is_data_qos(hdr->frame_control))
1362*4882a593Smuzhiyun 			freed++;
1363*4882a593Smuzhiyun 		else
1364*4882a593Smuzhiyun 			WARN_ON_ONCE(1);
1365*4882a593Smuzhiyun 
1366*4882a593Smuzhiyun 		iwl_trans_free_tx_cmd(priv->trans, info->driver_data[1]);
1367*4882a593Smuzhiyun 
1368*4882a593Smuzhiyun 		memset(&info->status, 0, sizeof(info->status));
1369*4882a593Smuzhiyun 		/* Packet was transmitted successfully, failures come as single
1370*4882a593Smuzhiyun 		 * frames because before failing a frame the firmware transmits
1371*4882a593Smuzhiyun 		 * it without aggregation at least once.
1372*4882a593Smuzhiyun 		 */
1373*4882a593Smuzhiyun 		info->flags |= IEEE80211_TX_STAT_ACK;
1374*4882a593Smuzhiyun 
1375*4882a593Smuzhiyun 		if (freed == 1) {
1376*4882a593Smuzhiyun 			/* this is the first skb we deliver in this batch */
1377*4882a593Smuzhiyun 			/* put the rate scaling data there */
1378*4882a593Smuzhiyun 			info = IEEE80211_SKB_CB(skb);
1379*4882a593Smuzhiyun 			memset(&info->status, 0, sizeof(info->status));
1380*4882a593Smuzhiyun 			info->flags |= IEEE80211_TX_STAT_AMPDU;
1381*4882a593Smuzhiyun 			info->status.ampdu_ack_len = ba_resp->txed_2_done;
1382*4882a593Smuzhiyun 			info->status.ampdu_len = ba_resp->txed;
1383*4882a593Smuzhiyun 			iwlagn_hwrate_to_tx_control(priv, agg->rate_n_flags,
1384*4882a593Smuzhiyun 						    info);
1385*4882a593Smuzhiyun 		}
1386*4882a593Smuzhiyun 	}
1387*4882a593Smuzhiyun 
1388*4882a593Smuzhiyun 	spin_unlock_bh(&priv->sta_lock);
1389*4882a593Smuzhiyun 
1390*4882a593Smuzhiyun 	while (!skb_queue_empty(&reclaimed_skbs)) {
1391*4882a593Smuzhiyun 		skb = __skb_dequeue(&reclaimed_skbs);
1392*4882a593Smuzhiyun 		ieee80211_tx_status(priv->hw, skb);
1393*4882a593Smuzhiyun 	}
1394*4882a593Smuzhiyun }
1395