File Index
Files and directories
Sign in
FILE

tx.c

File preview with syntax highlighting.

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