tx.c 59.4 KB
Newer Older
J
Johannes Berg 已提交
1 2 3 4 5 6 7
/******************************************************************************
 *
 * This file is provided under a dual BSD/GPLv2 license.  When using or
 * redistributing this file, you may do so under either license.
 *
 * GPL LICENSE SUMMARY
 *
8
 * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
9
 * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
10
 * Copyright(c) 2016 - 2017 Intel Deutschland GmbH
11
 * Copyright(c) 2018        Intel Corporation
J
Johannes Berg 已提交
12 13 14 15 16 17 18 19 20 21 22
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of version 2 of the GNU General Public License as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * The full GNU General Public License is included in this distribution
23
 * in the file called COPYING.
J
Johannes Berg 已提交
24 25
 *
 * Contact Information:
26
 *  Intel Linux Wireless <linuxwifi@intel.com>
J
Johannes Berg 已提交
27 28 29 30
 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
 *
 * BSD LICENSE
 *
31
 * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
32
 * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
33
 * Copyright(c) 2016 - 2017 Intel Deutschland GmbH
34
 * Copyright(c) 2018        Intel Corporation
J
Johannes Berg 已提交
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *  * Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *  * Neither the name Intel Corporation nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 *****************************************************************************/
#include <linux/ieee80211.h>
#include <linux/etherdevice.h>
66
#include <linux/tcp.h>
67
#include <net/ip.h>
68
#include <net/ipv6.h>
J
Johannes Berg 已提交
69 70 71 72 73 74

#include "iwl-trans.h"
#include "iwl-eeprom-parse.h"
#include "mvm.h"
#include "sta.h"

75 76 77 78 79 80 81
static void
iwl_mvm_bar_check_trigger(struct iwl_mvm *mvm, const u8 *addr,
			  u16 tid, u16 ssn)
{
	struct iwl_fw_dbg_trigger_tlv *trig;
	struct iwl_fw_dbg_trigger_ba *ba_trig;

82 83
	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, NULL, FW_DBG_TRIGGER_BA);
	if (!trig)
84 85 86 87 88 89 90
		return;

	ba_trig = (void *)trig->data;

	if (!(le16_to_cpu(ba_trig->tx_bar) & BIT(tid)))
		return;

91 92 93
	iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
				"BAR sent to %pM, tid %d, ssn %d",
				addr, tid, ssn);
94 95
}

96 97 98
#define OPT_HDR(type, skb, off) \
	(type *)(skb_network_header(skb) + (off))

99 100
static u16 iwl_mvm_tx_csum(struct iwl_mvm *mvm, struct sk_buff *skb,
			   struct ieee80211_hdr *hdr,
101 102
			   struct ieee80211_tx_info *info,
			   u16 offload_assist)
103 104 105 106 107 108 109 110 111 112
{
#if IS_ENABLED(CONFIG_INET)
	u16 mh_len = ieee80211_hdrlen(hdr->frame_control);
	u8 protocol = 0;

	/*
	 * Do not compute checksum if already computed or if transport will
	 * compute it
	 */
	if (skb->ip_summed != CHECKSUM_PARTIAL || IWL_MVM_SW_TX_CSUM_OFFLOAD)
113
		goto out;
114 115 116 117 118 119 120

	/* We do not expect to be requested to csum stuff we do not support */
	if (WARN_ONCE(!(mvm->hw->netdev_features & IWL_TX_CSUM_NETIF_FLAGS) ||
		      (skb->protocol != htons(ETH_P_IP) &&
		       skb->protocol != htons(ETH_P_IPV6)),
		      "No support for requested checksum\n")) {
		skb_checksum_help(skb);
121
		goto out;
122 123 124 125 126 127 128 129 130 131 132 133
	}

	if (skb->protocol == htons(ETH_P_IP)) {
		protocol = ip_hdr(skb)->protocol;
	} else {
#if IS_ENABLED(CONFIG_IPV6)
		struct ipv6hdr *ipv6h =
			(struct ipv6hdr *)skb_network_header(skb);
		unsigned int off = sizeof(*ipv6h);

		protocol = ipv6h->nexthdr;
		while (protocol != NEXTHDR_NONE && ipv6_ext_hdr(protocol)) {
134 135
			struct ipv6_opt_hdr *hp;

136 137 138
			/* only supported extension headers */
			if (protocol != NEXTHDR_ROUTING &&
			    protocol != NEXTHDR_HOP &&
139
			    protocol != NEXTHDR_DEST) {
140
				skb_checksum_help(skb);
141
				goto out;
142 143
			}

144 145 146
			hp = OPT_HDR(struct ipv6_opt_hdr, skb, off);
			protocol = hp->nexthdr;
			off += ipv6_optlen(hp);
147 148 149 150 151 152 153 154
		}
		/* if we get here - protocol now should be TCP/UDP */
#endif
	}

	if (protocol != IPPROTO_TCP && protocol != IPPROTO_UDP) {
		WARN_ON_ONCE(1);
		skb_checksum_help(skb);
155
		goto out;
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
	}

	/* enable L4 csum */
	offload_assist |= BIT(TX_CMD_OFFLD_L4_EN);

	/*
	 * Set offset to IP header (snap).
	 * We don't support tunneling so no need to take care of inner header.
	 * Size is in words.
	 */
	offload_assist |= (4 << TX_CMD_OFFLD_IP_HDR);

	/* Do IPv4 csum for AMSDU only (no IP csum for Ipv6) */
	if (skb->protocol == htons(ETH_P_IP) &&
	    (offload_assist & BIT(TX_CMD_OFFLD_AMSDU))) {
		ip_hdr(skb)->check = 0;
		offload_assist |= BIT(TX_CMD_OFFLD_L3_EN);
	}

	/* reset UDP/TCP header csum */
	if (protocol == IPPROTO_TCP)
		tcp_hdr(skb)->check = 0;
	else
		udp_hdr(skb)->check = 0;

181 182 183 184 185 186 187 188
	/*
	 * mac header len should include IV, size is in words unless
	 * the IV is added by the firmware like in WEP.
	 * In new Tx API, the IV is always added by the firmware.
	 */
	if (!iwl_mvm_has_new_tx_api(mvm) && info->control.hw_key &&
	    info->control.hw_key->cipher != WLAN_CIPHER_SUITE_WEP40 &&
	    info->control.hw_key->cipher != WLAN_CIPHER_SUITE_WEP104)
189 190 191 192
		mh_len += info->control.hw_key->iv_len;
	mh_len /= 2;
	offload_assist |= mh_len << TX_CMD_OFFLD_MH_SIZE;

193
out:
194
#endif
195
	return offload_assist;
196 197
}

J
Johannes Berg 已提交
198 199 200
/*
 * Sets most of the Tx cmd's fields
 */
201 202 203
void iwl_mvm_set_tx_cmd(struct iwl_mvm *mvm, struct sk_buff *skb,
			struct iwl_tx_cmd *tx_cmd,
			struct ieee80211_tx_info *info, u8 sta_id)
J
Johannes Berg 已提交
204 205 206 207 208
{
	struct ieee80211_hdr *hdr = (void *)skb->data;
	__le16 fc = hdr->frame_control;
	u32 tx_flags = le32_to_cpu(tx_cmd->tx_flags);
	u32 len = skb->len + FCS_LEN;
209
	u16 offload_assist = 0;
210
	u8 ac;
J
Johannes Berg 已提交
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226

	if (!(info->flags & IEEE80211_TX_CTL_NO_ACK))
		tx_flags |= TX_CMD_FLG_ACK;
	else
		tx_flags &= ~TX_CMD_FLG_ACK;

	if (ieee80211_is_probe_resp(fc))
		tx_flags |= TX_CMD_FLG_TSF;

	if (ieee80211_has_morefrags(fc))
		tx_flags |= TX_CMD_FLG_MORE_FRAG;

	if (ieee80211_is_data_qos(fc)) {
		u8 *qc = ieee80211_get_qos_ctl(hdr);
		tx_cmd->tid_tspec = qc[0] & 0xf;
		tx_flags &= ~TX_CMD_FLG_SEQ_CTL;
227
		if (*qc & IEEE80211_QOS_CTL_A_MSDU_PRESENT)
228
			offload_assist |= BIT(TX_CMD_OFFLD_AMSDU);
229 230 231
	} else if (ieee80211_is_back_req(fc)) {
		struct ieee80211_bar *bar = (void *)skb->data;
		u16 control = le16_to_cpu(bar->control);
232
		u16 ssn = le16_to_cpu(bar->start_seq_num);
233 234 235 236 237 238

		tx_flags |= TX_CMD_FLG_ACK | TX_CMD_FLG_BAR;
		tx_cmd->tid_tspec = (control &
				     IEEE80211_BAR_CTRL_TID_INFO_MASK) >>
			IEEE80211_BAR_CTRL_TID_INFO_SHIFT;
		WARN_ON_ONCE(tx_cmd->tid_tspec >= IWL_MAX_TID_COUNT);
239 240
		iwl_mvm_bar_check_trigger(mvm, bar->ra, tx_cmd->tid_tspec,
					  ssn);
J
Johannes Berg 已提交
241
	} else {
242 243 244 245 246
		if (ieee80211_is_data(fc))
			tx_cmd->tid_tspec = IWL_TID_NON_QOS;
		else
			tx_cmd->tid_tspec = IWL_MAX_TID_COUNT;

J
Johannes Berg 已提交
247 248 249 250 251 252
		if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ)
			tx_flags |= TX_CMD_FLG_SEQ_CTL;
		else
			tx_flags &= ~TX_CMD_FLG_SEQ_CTL;
	}

253
	/* Default to 0 (BE) when tid_spec is set to IWL_MAX_TID_COUNT */
254 255 256 257 258
	if (tx_cmd->tid_tspec < IWL_MAX_TID_COUNT)
		ac = tid_to_mac80211_ac[tx_cmd->tid_tspec];
	else
		ac = tid_to_mac80211_ac[0];

259 260 261
	tx_flags |= iwl_mvm_bt_coex_tx_prio(mvm, hdr, info, ac) <<
			TX_CMD_FLG_BT_PRIO_POS;

J
Johannes Berg 已提交
262 263
	if (ieee80211_is_mgmt(fc)) {
		if (ieee80211_is_assoc_req(fc) || ieee80211_is_reassoc_req(fc))
264 265 266
			tx_cmd->pm_frame_timeout = cpu_to_le16(PM_FRAME_ASSOC);
		else if (ieee80211_is_action(fc))
			tx_cmd->pm_frame_timeout = cpu_to_le16(PM_FRAME_NONE);
J
Johannes Berg 已提交
267
		else
268
			tx_cmd->pm_frame_timeout = cpu_to_le16(PM_FRAME_MGMT);
J
Johannes Berg 已提交
269 270 271 272 273

		/* The spec allows Action frames in A-MPDU, we don't support
		 * it
		 */
		WARN_ON_ONCE(info->flags & IEEE80211_TX_CTL_AMPDU);
274
	} else if (info->control.flags & IEEE80211_TX_CTRL_PORT_CTRL_PROTO) {
275
		tx_cmd->pm_frame_timeout = cpu_to_le16(PM_FRAME_MGMT);
J
Johannes Berg 已提交
276
	} else {
277
		tx_cmd->pm_frame_timeout = cpu_to_le16(PM_FRAME_NONE);
J
Johannes Berg 已提交
278 279 280 281 282 283
	}

	if (ieee80211_is_data(fc) && len > mvm->rts_threshold &&
	    !is_multicast_ether_addr(ieee80211_get_DA(hdr)))
		tx_flags |= TX_CMD_FLG_PROT_REQUIRE;

284 285
	if (fw_has_capa(&mvm->fw->ucode_capa,
			IWL_UCODE_TLV_CAPA_TXPOWER_INSERTION_SUPPORT) &&
286 287 288
	    ieee80211_action_contains_tpc(skb))
		tx_flags |= TX_CMD_FLG_WRITE_TX_POWER;

J
Johannes Berg 已提交
289
	tx_cmd->tx_flags = cpu_to_le32(tx_flags);
290 291
	/* Total # bytes to be transmitted - PCIe code will adjust for A-MSDU */
	tx_cmd->len = cpu_to_le16((u16)skb->len);
J
Johannes Berg 已提交
292 293
	tx_cmd->life_time = cpu_to_le32(TX_CMD_LIFE_TIME_INFINITE);
	tx_cmd->sta_id = sta_id;
294 295 296

	/* padding is inserted later in transport */
	if (ieee80211_hdrlen(fc) % 4 &&
297 298
	    !(offload_assist & BIT(TX_CMD_OFFLD_AMSDU)))
		offload_assist |= BIT(TX_CMD_OFFLD_PAD);
299

300
	tx_cmd->offload_assist |=
301 302
		cpu_to_le16(iwl_mvm_tx_csum(mvm, skb, hdr, info,
					    offload_assist));
J
Johannes Berg 已提交
303 304
}

305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
static u32 iwl_mvm_get_tx_rate(struct iwl_mvm *mvm,
			       struct ieee80211_tx_info *info,
			       struct ieee80211_sta *sta)
{
	int rate_idx;
	u8 rate_plcp;
	u32 rate_flags;

	/* HT rate doesn't make sense for a non data frame */
	WARN_ONCE(info->control.rates[0].flags & IEEE80211_TX_RC_MCS,
		  "Got an HT rate (flags:0x%x/mcs:%d) for a non data frame\n",
		  info->control.rates[0].flags,
		  info->control.rates[0].idx);

	rate_idx = info->control.rates[0].idx;
	/* if the rate isn't a well known legacy rate, take the lowest one */
	if (rate_idx < 0 || rate_idx >= IWL_RATE_COUNT_LEGACY)
		rate_idx = rate_lowest_index(
				&mvm->nvm_data->bands[info->band], sta);

	/* For 5 GHZ band, remap mac80211 rate indices into driver indices */
	if (info->band == NL80211_BAND_5GHZ)
		rate_idx += IWL_FIRST_OFDM_RATE;

	/* For 2.4 GHZ band, check that there is no need to remap */
	BUILD_BUG_ON(IWL_FIRST_CCK_RATE != 0);

	/* Get PLCP rate for tx_cmd->rate_n_flags */
	rate_plcp = iwl_mvm_mac80211_idx_to_hwrate(rate_idx);

	if (info->band == NL80211_BAND_2GHZ &&
	    !iwl_mvm_bt_coex_is_shared_ant_avail(mvm))
		rate_flags = mvm->cfg->non_shared_ant << RATE_MCS_ANT_POS;
	else
		rate_flags =
			BIT(mvm->mgmt_last_antenna_idx) << RATE_MCS_ANT_POS;

	/* Set CCK flag as needed */
	if ((rate_idx >= IWL_FIRST_CCK_RATE) && (rate_idx <= IWL_LAST_CCK_RATE))
		rate_flags |= RATE_MCS_CCK_MSK;

	return (u32)rate_plcp | rate_flags;
}

J
Johannes Berg 已提交
349 350 351
/*
 * Sets the fields in the Tx cmd that are rate related
 */
352 353 354
void iwl_mvm_set_tx_cmd_rate(struct iwl_mvm *mvm, struct iwl_tx_cmd *tx_cmd,
			    struct ieee80211_tx_info *info,
			    struct ieee80211_sta *sta, __le16 fc)
J
Johannes Berg 已提交
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
{
	/* Set retry limit on RTS packets */
	tx_cmd->rts_retry_limit = IWL_RTS_DFAULT_RETRY_LIMIT;

	/* Set retry limit on DATA packets and Probe Responses*/
	if (ieee80211_is_probe_resp(fc)) {
		tx_cmd->data_retry_limit = IWL_MGMT_DFAULT_RETRY_LIMIT;
		tx_cmd->rts_retry_limit =
			min(tx_cmd->data_retry_limit, tx_cmd->rts_retry_limit);
	} else if (ieee80211_is_back_req(fc)) {
		tx_cmd->data_retry_limit = IWL_BAR_DFAULT_RETRY_LIMIT;
	} else {
		tx_cmd->data_retry_limit = IWL_DEFAULT_TX_RETRY;
	}

	/*
371
	 * for data packets, rate info comes from the table inside the fw. This
372
	 * table is controlled by LINK_QUALITY commands
J
Johannes Berg 已提交
373 374
	 */

375
	if (ieee80211_is_data(fc) && sta) {
J
Johannes Berg 已提交
376 377 378 379
		tx_cmd->initial_rate_index = 0;
		tx_cmd->tx_flags |= cpu_to_le32(TX_CMD_FLG_STA_RATE);
		return;
	} else if (ieee80211_is_back_req(fc)) {
380 381
		tx_cmd->tx_flags |=
			cpu_to_le32(TX_CMD_FLG_ACK | TX_CMD_FLG_BAR);
J
Johannes Berg 已提交
382 383 384
	}

	mvm->mgmt_last_antenna_idx =
385
		iwl_mvm_next_antenna(mvm, iwl_mvm_get_valid_tx_ant(mvm),
J
Johannes Berg 已提交
386
				     mvm->mgmt_last_antenna_idx);
387

J
Johannes Berg 已提交
388
	/* Set the rate in the TX cmd */
389
	tx_cmd->rate_n_flags = cpu_to_le32(iwl_mvm_get_tx_rate(mvm, info, sta));
J
Johannes Berg 已提交
390 391
}

392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
static inline void iwl_mvm_set_tx_cmd_pn(struct ieee80211_tx_info *info,
					 u8 *crypto_hdr)
{
	struct ieee80211_key_conf *keyconf = info->control.hw_key;
	u64 pn;

	pn = atomic64_inc_return(&keyconf->tx_pn);
	crypto_hdr[0] = pn;
	crypto_hdr[2] = 0;
	crypto_hdr[3] = 0x20 | (keyconf->keyidx << 6);
	crypto_hdr[1] = pn >> 8;
	crypto_hdr[4] = pn >> 16;
	crypto_hdr[5] = pn >> 24;
	crypto_hdr[6] = pn >> 32;
	crypto_hdr[7] = pn >> 40;
}

J
Johannes Berg 已提交
409 410 411
/*
 * Sets the fields in the Tx cmd that are crypto related
 */
412 413 414 415 416
static void iwl_mvm_set_tx_cmd_crypto(struct iwl_mvm *mvm,
				      struct ieee80211_tx_info *info,
				      struct iwl_tx_cmd *tx_cmd,
				      struct sk_buff *skb_frag,
				      int hdrlen)
J
Johannes Berg 已提交
417 418
{
	struct ieee80211_key_conf *keyconf = info->control.hw_key;
419
	u8 *crypto_hdr = skb_frag->data + hdrlen;
S
Sara Sharon 已提交
420
	enum iwl_tx_cmd_sec_ctrl type = TX_CMD_SEC_CCM;
421
	u64 pn;
J
Johannes Berg 已提交
422 423 424

	switch (keyconf->cipher) {
	case WLAN_CIPHER_SUITE_CCMP:
425
		iwl_mvm_set_tx_cmd_ccmp(info, tx_cmd);
426
		iwl_mvm_set_tx_cmd_pn(info, crypto_hdr);
J
Johannes Berg 已提交
427 428 429 430
		break;

	case WLAN_CIPHER_SUITE_TKIP:
		tx_cmd->sec_ctl = TX_CMD_SEC_TKIP;
431 432
		pn = atomic64_inc_return(&keyconf->tx_pn);
		ieee80211_tkip_add_iv(crypto_hdr, keyconf, pn);
J
Johannes Berg 已提交
433 434 435 436 437 438 439 440 441 442 443 444 445
		ieee80211_get_tkip_p2k(keyconf, skb_frag, tx_cmd->key);
		break;

	case WLAN_CIPHER_SUITE_WEP104:
		tx_cmd->sec_ctl |= TX_CMD_SEC_KEY128;
		/* fall through */
	case WLAN_CIPHER_SUITE_WEP40:
		tx_cmd->sec_ctl |= TX_CMD_SEC_WEP |
			((keyconf->keyidx << TX_CMD_SEC_WEP_KEY_IDX_POS) &
			  TX_CMD_SEC_WEP_KEY_IDX_MSK);

		memcpy(&tx_cmd->key[3], keyconf->key, keyconf->keylen);
		break;
446 447
	case WLAN_CIPHER_SUITE_GCMP:
	case WLAN_CIPHER_SUITE_GCMP_256:
S
Sara Sharon 已提交
448 449 450
		type = TX_CMD_SEC_GCMP;
		/* Fall through */
	case WLAN_CIPHER_SUITE_CCMP_256:
451 452 453 454 455 456
		/* TODO: Taking the key from the table might introduce a race
		 * when PTK rekeying is done, having an old packets with a PN
		 * based on the old key but the message encrypted with a new
		 * one.
		 * Need to handle this.
		 */
S
Sara Sharon 已提交
457
		tx_cmd->sec_ctl |= type | TX_CMD_SEC_KEY_FROM_TABLE;
458 459 460
		tx_cmd->key[0] = keyconf->hw_key_idx;
		iwl_mvm_set_tx_cmd_pn(info, crypto_hdr);
		break;
J
Johannes Berg 已提交
461
	default:
462
		tx_cmd->sec_ctl |= TX_CMD_SEC_EXT;
J
Johannes Berg 已提交
463 464 465 466 467 468 469 470
	}
}

/*
 * Allocates and sets the Tx cmd the driver data pointers in the skb
 */
static struct iwl_device_cmd *
iwl_mvm_set_tx_params(struct iwl_mvm *mvm, struct sk_buff *skb,
471 472
		      struct ieee80211_tx_info *info, int hdrlen,
		      struct ieee80211_sta *sta, u8 sta_id)
J
Johannes Berg 已提交
473 474 475 476 477 478 479 480 481 482
{
	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
	struct iwl_device_cmd *dev_cmd;
	struct iwl_tx_cmd *tx_cmd;

	dev_cmd = iwl_trans_alloc_tx_cmd(mvm->trans);

	if (unlikely(!dev_cmd))
		return NULL;

483 484
	/* Make sure we zero enough of dev_cmd */
	BUILD_BUG_ON(sizeof(struct iwl_tx_cmd_gen2) > sizeof(*tx_cmd));
G
Golan Ben Ami 已提交
485
	BUILD_BUG_ON(sizeof(struct iwl_tx_cmd_gen3) > sizeof(*tx_cmd));
486 487

	memset(dev_cmd, 0, sizeof(dev_cmd->hdr) + sizeof(*tx_cmd));
488
	dev_cmd->hdr.cmd = TX_CMD;
S
Sara Sharon 已提交
489 490

	if (iwl_mvm_has_new_tx_api(mvm)) {
491
		u16 offload_assist = 0;
G
Golan Ben Ami 已提交
492 493
		u32 rate_n_flags = 0;
		u16 flags = 0;
S
Sara Sharon 已提交
494

495 496 497 498 499 500 501
		if (ieee80211_is_data_qos(hdr->frame_control)) {
			u8 *qc = ieee80211_get_qos_ctl(hdr);

			if (*qc & IEEE80211_QOS_CTL_A_MSDU_PRESENT)
				offload_assist |= BIT(TX_CMD_OFFLD_AMSDU);
		}

502 503 504
		offload_assist = iwl_mvm_tx_csum(mvm, skb, hdr, info,
						 offload_assist);

S
Sara Sharon 已提交
505 506 507 508 509
		/* padding is inserted later in transport */
		if (ieee80211_hdrlen(hdr->frame_control) % 4 &&
		    !(offload_assist & BIT(TX_CMD_OFFLD_AMSDU)))
			offload_assist |= BIT(TX_CMD_OFFLD_PAD);

G
Golan Ben Ami 已提交
510 511
		if (!info->control.hw_key)
			flags |= IWL_TX_FLAGS_ENCRYPT_DIS;
S
Sara Sharon 已提交
512

G
Golan Ben Ami 已提交
513 514 515 516 517
		/* For data packets rate info comes from the fw */
		if (!(ieee80211_is_data(hdr->frame_control) && sta)) {
			flags |= IWL_TX_FLAGS_CMD_RATE;
			rate_n_flags = iwl_mvm_get_tx_rate(mvm, info, sta);
		}
S
Sara Sharon 已提交
518

G
Golan Ben Ami 已提交
519 520 521
		if (mvm->trans->cfg->device_family >=
		    IWL_DEVICE_FAMILY_22560) {
			struct iwl_tx_cmd_gen3 *cmd = (void *)dev_cmd->payload;
S
Sara Sharon 已提交
522

G
Golan Ben Ami 已提交
523
			cmd->offload_assist |= cpu_to_le32(offload_assist);
S
Sara Sharon 已提交
524

G
Golan Ben Ami 已提交
525 526
			/* Total # bytes to be transmitted */
			cmd->len = cpu_to_le16((u16)skb->len);
S
Sara Sharon 已提交
527

G
Golan Ben Ami 已提交
528 529
			/* Copy MAC header from skb into command buffer */
			memcpy(cmd->hdr, hdr, hdrlen);
S
Sara Sharon 已提交
530

G
Golan Ben Ami 已提交
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546
			cmd->flags = cpu_to_le16(flags);
			cmd->rate_n_flags = cpu_to_le32(rate_n_flags);
		} else {
			struct iwl_tx_cmd_gen2 *cmd = (void *)dev_cmd->payload;

			cmd->offload_assist |= cpu_to_le16(offload_assist);

			/* Total # bytes to be transmitted */
			cmd->len = cpu_to_le16((u16)skb->len);

			/* Copy MAC header from skb into command buffer */
			memcpy(cmd->hdr, hdr, hdrlen);

			cmd->flags = cpu_to_le32(flags);
			cmd->rate_n_flags = cpu_to_le32(rate_n_flags);
		}
S
Sara Sharon 已提交
547 548 549
		goto out;
	}

J
Johannes Berg 已提交
550 551 552
	tx_cmd = (struct iwl_tx_cmd *)dev_cmd->payload;

	if (info->control.hw_key)
553
		iwl_mvm_set_tx_cmd_crypto(mvm, info, tx_cmd, skb, hdrlen);
J
Johannes Berg 已提交
554 555 556 557 558

	iwl_mvm_set_tx_cmd(mvm, skb, tx_cmd, info, sta_id);

	iwl_mvm_set_tx_cmd_rate(mvm, tx_cmd, info, sta, hdr->frame_control);

S
Sara Sharon 已提交
559 560 561 562
	/* Copy MAC header from skb into command buffer */
	memcpy(tx_cmd->hdr, hdr, hdrlen);

out:
563 564 565 566 567 568 569 570
	return dev_cmd;
}

static void iwl_mvm_skb_prepare_status(struct sk_buff *skb,
				       struct iwl_device_cmd *cmd)
{
	struct ieee80211_tx_info *skb_info = IEEE80211_SKB_CB(skb);

571 572
	memset(&skb_info->status, 0, sizeof(skb_info->status));
	memset(skb_info->driver_data, 0, sizeof(skb_info->driver_data));
J
Johannes Berg 已提交
573

574
	skb_info->driver_data[1] = cmd;
J
Johannes Berg 已提交
575 576
}

577 578 579
static int iwl_mvm_get_ctrl_vif_queue(struct iwl_mvm *mvm,
				      struct ieee80211_tx_info *info, __le16 fc)
{
580 581 582 583
	struct iwl_mvm_vif *mvmvif;

	mvmvif = iwl_mvm_vif_from_mac80211(info->control.vif);

584 585
	switch (info->control.vif->type) {
	case NL80211_IFTYPE_AP:
586
	case NL80211_IFTYPE_ADHOC:
587
		/*
588 589
		 * Non-bufferable frames use the broadcast station, thus they
		 * use the probe queue.
590 591 592 593
		 * Also take care of the case where we send a deauth to a
		 * station that we don't have, or similarly an association
		 * response (with non-success status) for a station we can't
		 * accept.
594 595
		 * Also, disassociate frames might happen, particular with
		 * reason 7 ("Class 3 frame received from nonassociated STA").
596
		 */
597 598 599
		if (ieee80211_is_mgmt(fc) &&
		    (!ieee80211_is_bufferable_mmpdu(fc) ||
		     ieee80211_is_deauth(fc) || ieee80211_is_disassoc(fc)))
600
			return mvm->probe_queue;
601
		if (info->hw_queue == info->control.vif->cab_queue)
602
			return mvmvif->cab_queue;
603

604 605
		WARN_ONCE(info->control.vif->type != NL80211_IFTYPE_ADHOC,
			  "fc=0x%02x", le16_to_cpu(fc));
606
		return mvm->probe_queue;
607 608
	case NL80211_IFTYPE_P2P_DEVICE:
		if (ieee80211_is_mgmt(fc))
609
			return mvm->p2p_dev_queue;
610
		if (info->hw_queue == info->control.vif->cab_queue)
611
			return mvmvif->cab_queue;
612

613
		WARN_ON_ONCE(1);
614
		return mvm->p2p_dev_queue;
615 616 617 618
	default:
		WARN_ONCE(1, "Not a ctrl vif, no available queue\n");
		return -1;
	}
619 620
}

621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680
static void iwl_mvm_probe_resp_set_noa(struct iwl_mvm *mvm,
				       struct sk_buff *skb)
{
	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
	struct iwl_mvm_vif *mvmvif =
		iwl_mvm_vif_from_mac80211(info->control.vif);
	struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)skb->data;
	int base_len = (u8 *)mgmt->u.probe_resp.variable - (u8 *)mgmt;
	struct iwl_probe_resp_data *resp_data;
	u8 *ie, *pos;
	u8 match[] = {
		(WLAN_OUI_WFA >> 16) & 0xff,
		(WLAN_OUI_WFA >> 8) & 0xff,
		WLAN_OUI_WFA & 0xff,
		WLAN_OUI_TYPE_WFA_P2P,
	};

	rcu_read_lock();

	resp_data = rcu_dereference(mvmvif->probe_resp_data);
	if (!resp_data)
		goto out;

	if (!resp_data->notif.noa_active)
		goto out;

	ie = (u8 *)cfg80211_find_ie_match(WLAN_EID_VENDOR_SPECIFIC,
					  mgmt->u.probe_resp.variable,
					  skb->len - base_len,
					  match, 4, 2);
	if (!ie) {
		IWL_DEBUG_TX(mvm, "probe resp doesn't have P2P IE\n");
		goto out;
	}

	if (skb_tailroom(skb) < resp_data->noa_len) {
		if (pskb_expand_head(skb, 0, resp_data->noa_len, GFP_ATOMIC)) {
			IWL_ERR(mvm,
				"Failed to reallocate probe resp\n");
			goto out;
		}
	}

	pos = skb_put(skb, resp_data->noa_len);

	*pos++ = WLAN_EID_VENDOR_SPECIFIC;
	/* Set length of IE body (not including ID and length itself) */
	*pos++ = resp_data->noa_len - 2;
	*pos++ = (WLAN_OUI_WFA >> 16) & 0xff;
	*pos++ = (WLAN_OUI_WFA >> 8) & 0xff;
	*pos++ = WLAN_OUI_WFA & 0xff;
	*pos++ = WLAN_OUI_TYPE_WFA_P2P;

	memcpy(pos, &resp_data->notif.noa_attr,
	       resp_data->noa_len - sizeof(struct ieee80211_vendor_ie));

out:
	rcu_read_unlock();
}

J
Johannes Berg 已提交
681 682 683
int iwl_mvm_tx_skb_non_sta(struct iwl_mvm *mvm, struct sk_buff *skb)
{
	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
684 685
	struct ieee80211_tx_info *skb_info = IEEE80211_SKB_CB(skb);
	struct ieee80211_tx_info info;
J
Johannes Berg 已提交
686 687
	struct iwl_device_cmd *dev_cmd;
	u8 sta_id;
688
	int hdrlen = ieee80211_hdrlen(hdr->frame_control);
689
	__le16 fc = hdr->frame_control;
690
	int queue;
J
Johannes Berg 已提交
691

692 693 694 695 696
	/* IWL_MVM_OFFCHANNEL_QUEUE is used for ROC packets that can be used
	 * in 2 different types of vifs, P2P & STATION. P2P uses the offchannel
	 * queue. STATION (HS2.0) uses the auxiliary context of the FW,
	 * and hence needs to be sent on the aux queue
	 */
697
	if (skb_info->hw_queue == IWL_MVM_OFFCHANNEL_QUEUE &&
698
	    skb_info->control.vif->type == NL80211_IFTYPE_STATION)
699
		skb_info->hw_queue = mvm->aux_queue;
700

701 702 703
	memcpy(&info, skb->cb, sizeof(info));

	if (WARN_ON_ONCE(info.flags & IEEE80211_TX_CTL_AMPDU))
J
Johannes Berg 已提交
704 705
		return -1;

706 707 708
	if (WARN_ON_ONCE(info.flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM &&
			 (!info.control.vif ||
			  info.hw_queue != info.control.vif->cab_queue)))
J
Johannes Berg 已提交
709 710
		return -1;

711 712
	queue = info.hw_queue;

J
Johannes Berg 已提交
713
	/*
714
	 * If the interface on which the frame is sent is the P2P_DEVICE
J
Johannes Berg 已提交
715
	 * or an AP/GO interface use the broadcast station associated
716 717 718 719 720
	 * with it; otherwise if the interface is a managed interface
	 * use the AP station associated with it for multicast traffic
	 * (this is not possible for unicast packets as a TLDS discovery
	 * response are sent without a station entry); otherwise use the
	 * AUX station.
J
Johannes Berg 已提交
721
	 */
722
	sta_id = mvm->aux_sta.sta_id;
723
	if (info.control.vif) {
J
Johannes Berg 已提交
724
		struct iwl_mvm_vif *mvmvif =
725
			iwl_mvm_vif_from_mac80211(info.control.vif);
726

727
		if (info.control.vif->type == NL80211_IFTYPE_P2P_DEVICE ||
728 729
		    info.control.vif->type == NL80211_IFTYPE_AP ||
		    info.control.vif->type == NL80211_IFTYPE_ADHOC) {
730
			if (!ieee80211_is_data(hdr->frame_control))
731 732 733 734
				sta_id = mvmvif->bcast_sta.sta_id;
			else
				sta_id = mvmvif->mcast_sta.sta_id;

735 736
			queue = iwl_mvm_get_ctrl_vif_queue(mvm, &info,
							   hdr->frame_control);
737 738
			if (queue < 0)
				return -1;
739 740
		} else if (info.control.vif->type == NL80211_IFTYPE_STATION &&
			   is_multicast_ether_addr(hdr->addr1)) {
741
			u8 ap_sta_id = READ_ONCE(mvmvif->ap_sta_id);
742

743
			if (ap_sta_id != IWL_MVM_INVALID_STA)
744
				sta_id = ap_sta_id;
745
		} else if (info.control.vif->type == NL80211_IFTYPE_MONITOR) {
746 747
			queue = mvm->snif_queue;
			sta_id = mvm->snif_sta.sta_id;
748
		}
J
Johannes Berg 已提交
749 750
	}

751 752 753
	if (unlikely(ieee80211_is_probe_resp(fc)))
		iwl_mvm_probe_resp_set_noa(mvm, skb);

754
	IWL_DEBUG_TX(mvm, "station Id %d, queue=%d\n", sta_id, queue);
J
Johannes Berg 已提交
755

756
	dev_cmd = iwl_mvm_set_tx_params(mvm, skb, &info, hdrlen, NULL, sta_id);
J
Johannes Berg 已提交
757 758 759
	if (!dev_cmd)
		return -1;

760 761 762
	/* From now on, we cannot access info->control */
	iwl_mvm_skb_prepare_status(skb, dev_cmd);

763
	if (iwl_trans_tx(mvm->trans, skb, dev_cmd, queue)) {
J
Johannes Berg 已提交
764 765 766 767 768 769 770
		iwl_trans_free_tx_cmd(mvm->trans, dev_cmd);
		return -1;
	}

	return 0;
}

771
#ifdef CONFIG_INET
772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839

static int
iwl_mvm_tx_tso_segment(struct sk_buff *skb, unsigned int num_subframes,
		       netdev_features_t netdev_flags,
		       struct sk_buff_head *mpdus_skb)
{
	struct sk_buff *tmp, *next;
	struct ieee80211_hdr *hdr = (void *)skb->data;
	char cb[sizeof(skb->cb)];
	u16 i = 0;
	unsigned int tcp_payload_len;
	unsigned int mss = skb_shinfo(skb)->gso_size;
	bool ipv4 = (skb->protocol == htons(ETH_P_IP));
	u16 ip_base_id = ipv4 ? ntohs(ip_hdr(skb)->id) : 0;

	skb_shinfo(skb)->gso_size = num_subframes * mss;
	memcpy(cb, skb->cb, sizeof(cb));

	next = skb_gso_segment(skb, netdev_flags);
	skb_shinfo(skb)->gso_size = mss;
	if (WARN_ON_ONCE(IS_ERR(next)))
		return -EINVAL;
	else if (next)
		consume_skb(skb);

	while (next) {
		tmp = next;
		next = tmp->next;

		memcpy(tmp->cb, cb, sizeof(tmp->cb));
		/*
		 * Compute the length of all the data added for the A-MSDU.
		 * This will be used to compute the length to write in the TX
		 * command. We have: SNAP + IP + TCP for n -1 subframes and
		 * ETH header for n subframes.
		 */
		tcp_payload_len = skb_tail_pointer(tmp) -
			skb_transport_header(tmp) -
			tcp_hdrlen(tmp) + tmp->data_len;

		if (ipv4)
			ip_hdr(tmp)->id = htons(ip_base_id + i * num_subframes);

		if (tcp_payload_len > mss) {
			skb_shinfo(tmp)->gso_size = mss;
		} else {
			if (ieee80211_is_data_qos(hdr->frame_control)) {
				u8 *qc;

				if (ipv4)
					ip_send_check(ip_hdr(tmp));

				qc = ieee80211_get_qos_ctl((void *)tmp->data);
				*qc &= ~IEEE80211_QOS_CTL_A_MSDU_PRESENT;
			}
			skb_shinfo(tmp)->gso_size = 0;
		}

		tmp->prev = NULL;
		tmp->next = NULL;

		__skb_queue_tail(mpdus_skb, tmp);
		i++;
	}

	return 0;
}

840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869
static unsigned int iwl_mvm_max_amsdu_size(struct iwl_mvm *mvm,
					   struct ieee80211_sta *sta,
					   unsigned int tid)
{
	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
	enum nl80211_band band = mvmsta->vif->bss_conf.chandef.chan->band;
	u8 ac = tid_to_mac80211_ac[tid];
	unsigned int txf;
	int lmac = IWL_LMAC_24G_INDEX;

	if (iwl_mvm_is_cdb_supported(mvm) &&
	    band == NL80211_BAND_5GHZ)
		lmac = IWL_LMAC_5G_INDEX;

	/* For HE redirect to trigger based fifos */
	if (sta->he_cap.has_he && !WARN_ON(!iwl_mvm_has_new_tx_api(mvm)))
		ac += 4;

	txf = iwl_mvm_mac_ac_to_tx_fifo(mvm, ac);

	/*
	 * Don't send an AMSDU that will be longer than the TXF.
	 * Add a security margin of 256 for the TX command + headers.
	 * We also want to have the start of the next packet inside the
	 * fifo to be able to send bursts.
	 */
	return min_t(unsigned int, mvmsta->max_amsdu_len,
		     mvm->fwrt.smem_cfg.lmac[lmac].txfifo_size[txf] - 256);
}

870
static int iwl_mvm_tx_tso(struct iwl_mvm *mvm, struct sk_buff *skb,
871
			  struct ieee80211_tx_info *info,
872 873 874
			  struct ieee80211_sta *sta,
			  struct sk_buff_head *mpdus_skb)
{
875
	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
876 877
	struct ieee80211_hdr *hdr = (void *)skb->data;
	unsigned int mss = skb_shinfo(skb)->gso_size;
878
	unsigned int num_subframes, tcp_payload_len, subf_len, max_amsdu_len;
879
	u16 snap_ip_tcp, pad;
880
	unsigned int dbg_max_amsdu_len;
881
	netdev_features_t netdev_flags = NETIF_F_CSUM_MASK | NETIF_F_SG;
882
	u8 tid;
883 884 885 886

	snap_ip_tcp = 8 + skb_transport_header(skb) - skb_network_header(skb) +
		tcp_hdrlen(skb);

887
	dbg_max_amsdu_len = READ_ONCE(mvm->max_amsdu_len);
888

889
	if (!mvmsta->max_amsdu_len ||
890
	    !ieee80211_is_data_qos(hdr->frame_control) ||
891
	    (!mvmsta->amsdu_enabled && !dbg_max_amsdu_len))
892
		return iwl_mvm_tx_tso_segment(skb, 1, netdev_flags, mpdus_skb);
893

894 895 896 897 898 899 900
	/*
	 * Do not build AMSDU for IPv6 with extension headers.
	 * ask stack to segment and checkum the generated MPDUs for us.
	 */
	if (skb->protocol == htons(ETH_P_IPV6) &&
	    ((struct ipv6hdr *)skb_network_header(skb))->nexthdr !=
	    IPPROTO_TCP) {
901 902
		netdev_flags &= ~NETIF_F_CSUM_MASK;
		return iwl_mvm_tx_tso_segment(skb, 1, netdev_flags, mpdus_skb);
903 904
	}

905
	tid = ieee80211_get_tid(hdr);
906 907 908
	if (WARN_ON_ONCE(tid >= IWL_MAX_TID_COUNT))
		return -EINVAL;

909 910 911 912 913
	/*
	 * No need to lock amsdu_in_ampdu_allowed since it can't be modified
	 * during an BA session.
	 */
	if (info->flags & IEEE80211_TX_CTL_AMPDU &&
914 915
	    !mvmsta->tid_data[tid].amsdu_in_ampdu_allowed)
		return iwl_mvm_tx_tso_segment(skb, 1, netdev_flags, mpdus_skb);
916

917 918 919 920
	if (iwl_mvm_vif_low_latency(iwl_mvm_vif_from_mac80211(mvmsta->vif)) ||
	    !(mvmsta->amsdu_enabled & BIT(tid)))
		return iwl_mvm_tx_tso_segment(skb, 1, netdev_flags, mpdus_skb);

921
	max_amsdu_len = iwl_mvm_max_amsdu_size(mvm, sta, tid);
922

923
	if (unlikely(dbg_max_amsdu_len))
924 925
		max_amsdu_len = min_t(unsigned int, max_amsdu_len,
				      dbg_max_amsdu_len);
926 927 928 929 930 931 932 933 934 935

	/*
	 * Limit A-MSDU in A-MPDU to 4095 bytes when VHT is not
	 * supported. This is a spec requirement (IEEE 802.11-2015
	 * section 8.7.3 NOTE 3).
	 */
	if (info->flags & IEEE80211_TX_CTL_AMPDU &&
	    !sta->vht_cap.vht_supported)
		max_amsdu_len = min_t(unsigned int, max_amsdu_len, 4095);

936 937 938 939 940 941 942 943
	/* Sub frame header + SNAP + IP header + TCP header + MSS */
	subf_len = sizeof(struct ethhdr) + snap_ip_tcp + mss;
	pad = (4 - subf_len) & 0x3;

	/*
	 * If we have N subframes in the A-MSDU, then the A-MSDU's size is
	 * N * subf_len + (N - 1) * pad.
	 */
944
	num_subframes = (max_amsdu_len + pad) / (subf_len + pad);
945

946 947 948 949
	if (sta->max_amsdu_subframes &&
	    num_subframes > sta->max_amsdu_subframes)
		num_subframes = sta->max_amsdu_subframes;

950 951 952 953 954 955 956 957 958
	tcp_payload_len = skb_tail_pointer(skb) - skb_transport_header(skb) -
		tcp_hdrlen(skb) + skb->data_len;

	/*
	 * Make sure we have enough TBs for the A-MSDU:
	 *	2 for each subframe
	 *	1 more for each fragment
	 *	1 more for the potential data in the header
	 */
959 960 961 962 963 964
	if ((num_subframes * 2 + skb_shinfo(skb)->nr_frags + 1) >
	    mvm->trans->max_skb_frags)
		num_subframes = 1;

	if (num_subframes > 1)
		*ieee80211_get_qos_ctl(hdr) |= IEEE80211_QOS_CTL_A_MSDU_PRESENT;
965 966 967 968 969 970

	/* This skb fits in one single A-MSDU */
	if (num_subframes * mss >= tcp_payload_len) {
		__skb_queue_tail(mpdus_skb, skb);
		return 0;
	}
971

972 973 974 975
	/*
	 * Trick the segmentation function to make it
	 * create SKBs that can fit into one A-MSDU.
	 */
976 977
	return iwl_mvm_tx_tso_segment(skb, num_subframes, netdev_flags,
				      mpdus_skb);
978
}
979 980
#else /* CONFIG_INET */
static int iwl_mvm_tx_tso(struct iwl_mvm *mvm, struct sk_buff *skb,
981
			  struct ieee80211_tx_info *info,
982 983 984 985 986 987 988 989 990
			  struct ieee80211_sta *sta,
			  struct sk_buff_head *mpdus_skb)
{
	/* Impossible to get TSO with CONFIG_INET */
	WARN_ON(1);

	return -1;
}
#endif
991

992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011
static void iwl_mvm_tx_add_stream(struct iwl_mvm *mvm,
				  struct iwl_mvm_sta *mvm_sta, u8 tid,
				  struct sk_buff *skb)
{
	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
	u8 mac_queue = info->hw_queue;
	struct sk_buff_head *deferred_tx_frames;

	lockdep_assert_held(&mvm_sta->lock);

	mvm_sta->deferred_traffic_tid_map |= BIT(tid);
	set_bit(mvm_sta->sta_id, mvm->sta_deferred_frames);

	deferred_tx_frames = &mvm_sta->tid_data[tid].deferred_tx_frames;

	skb_queue_tail(deferred_tx_frames, skb);

	/*
	 * The first deferred frame should've stopped the MAC queues, so we
	 * should never get a second deferred frame for the RA/TID.
1012
	 * In case of GSO the first packet may have been split, so don't warn.
1013
	 */
1014
	if (skb_queue_len(deferred_tx_frames) == 1) {
1015 1016 1017 1018 1019
		iwl_mvm_stop_mac_queues(mvm, BIT(mac_queue));
		schedule_work(&mvm->add_stream_wk);
	}
}

1020 1021 1022 1023 1024 1025 1026
/* Check if there are any timed-out TIDs on a given shared TXQ */
static bool iwl_mvm_txq_should_update(struct iwl_mvm *mvm, int txq_id)
{
	unsigned long queue_tid_bitmap = mvm->queue_info[txq_id].tid_bitmap;
	unsigned long now = jiffies;
	int tid;

1027 1028 1029
	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
		return false;

1030 1031 1032 1033 1034 1035 1036 1037 1038
	for_each_set_bit(tid, &queue_tid_bitmap, IWL_MAX_TID_COUNT + 1) {
		if (time_before(mvm->queue_info[txq_id].last_frame_time[tid] +
				IWL_MVM_DQA_QUEUE_TIMEOUT, now))
			return true;
	}

	return false;
}

1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064
static void iwl_mvm_tx_airtime(struct iwl_mvm *mvm,
			       struct iwl_mvm_sta *mvmsta,
			       int airtime)
{
	int mac = mvmsta->mac_id_n_color & FW_CTXT_ID_MSK;
	struct iwl_mvm_tcm_mac *mdata = &mvm->tcm.data[mac];

	if (mvm->tcm.paused)
		return;

	if (time_after(jiffies, mvm->tcm.ts + MVM_TCM_PERIOD))
		schedule_delayed_work(&mvm->tcm.work, 0);

	mdata->tx.airtime += airtime;
}

static void iwl_mvm_tx_pkt_queued(struct iwl_mvm *mvm,
				  struct iwl_mvm_sta *mvmsta, int tid)
{
	u32 ac = tid_to_mac80211_ac[tid];
	int mac = mvmsta->mac_id_n_color & FW_CTXT_ID_MSK;
	struct iwl_mvm_tcm_mac *mdata = &mvm->tcm.data[mac];

	mdata->tx.pkts[ac]++;
}

J
Johannes Berg 已提交
1065 1066 1067
/*
 * Sets the fields in the Tx cmd that are crypto related
 */
1068
static int iwl_mvm_tx_mpdu(struct iwl_mvm *mvm, struct sk_buff *skb,
1069
			   struct ieee80211_tx_info *info,
1070
			   struct ieee80211_sta *sta)
J
Johannes Berg 已提交
1071 1072 1073 1074 1075 1076 1077
{
	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
	struct iwl_mvm_sta *mvmsta;
	struct iwl_device_cmd *dev_cmd;
	__le16 fc;
	u16 seq_number = 0;
	u8 tid = IWL_MAX_TID_COUNT;
1078
	u16 txq_id = info->hw_queue;
1079
	bool is_ampdu = false;
1080
	int hdrlen;
J
Johannes Berg 已提交
1081

1082
	mvmsta = iwl_mvm_sta_from_mac80211(sta);
J
Johannes Berg 已提交
1083
	fc = hdr->frame_control;
1084
	hdrlen = ieee80211_hdrlen(fc);
J
Johannes Berg 已提交
1085 1086 1087 1088

	if (WARN_ON_ONCE(!mvmsta))
		return -1;

1089
	if (WARN_ON_ONCE(mvmsta->sta_id == IWL_MVM_INVALID_STA))
J
Johannes Berg 已提交
1090 1091
		return -1;

1092 1093 1094
	if (unlikely(ieee80211_is_probe_resp(fc)))
		iwl_mvm_probe_resp_set_noa(mvm, skb);

1095 1096
	dev_cmd = iwl_mvm_set_tx_params(mvm, skb, info, hdrlen,
					sta, mvmsta->sta_id);
J
Johannes Berg 已提交
1097 1098 1099
	if (!dev_cmd)
		goto drop;

1100 1101 1102 1103 1104 1105 1106
	/*
	 * we handle that entirely ourselves -- for uAPSD the firmware
	 * will always send a notification, and for PS-Poll responses
	 * we'll notify mac80211 when getting frame status
	 */
	info->flags &= ~IEEE80211_TX_STATUS_EOSP;

J
Johannes Berg 已提交
1107 1108
	spin_lock(&mvmsta->lock);

1109 1110 1111 1112
	/* nullfunc frames should go to the MGMT queue regardless of QOS,
	 * the condition of !ieee80211_is_qos_nullfunc(fc) keeps the default
	 * assignment of MGMT TID
	 */
J
Johannes Berg 已提交
1113
	if (ieee80211_is_data_qos(fc) && !ieee80211_is_qos_nullfunc(fc)) {
1114
		tid = ieee80211_get_tid(hdr);
J
Johannes Berg 已提交
1115 1116 1117 1118
		if (WARN_ON_ONCE(tid >= IWL_MAX_TID_COUNT))
			goto drop_unlock_sta;

		is_ampdu = info->flags & IEEE80211_TX_CTL_AMPDU;
1119 1120 1121
		if (WARN_ON_ONCE(is_ampdu &&
				 mvmsta->tid_data[tid].state != IWL_AGG_ON))
			goto drop_unlock_sta;
S
Sara Sharon 已提交
1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133

		seq_number = mvmsta->tid_data[tid].seq_number;
		seq_number &= IEEE80211_SCTL_SEQ;

		if (!iwl_mvm_has_new_tx_api(mvm)) {
			struct iwl_tx_cmd *tx_cmd = (void *)dev_cmd->payload;

			hdr->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
			hdr->seq_ctrl |= cpu_to_le16(seq_number);
			/* update the tx_cmd hdr as it was already copied */
			tx_cmd->hdr->seq_ctrl = hdr->seq_ctrl;
		}
1134 1135
	} else if (ieee80211_is_data(fc) && !ieee80211_is_data_qos(fc)) {
		tid = IWL_TID_NON_QOS;
J
Johannes Berg 已提交
1136 1137
	}

1138
	txq_id = mvmsta->tid_data[tid].txq_id;
1139

1140
	WARN_ON_ONCE(info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM);
J
Johannes Berg 已提交
1141

1142
	/* Check if TXQ needs to be allocated or re-activated */
1143 1144
	if (unlikely(txq_id == IWL_MVM_INVALID_QUEUE)) {
		iwl_mvm_tx_add_stream(mvm, mvmsta, tid, skb);
1145

1146 1147 1148 1149 1150 1151 1152
		/*
		 * The frame is now deferred, and the worker scheduled
		 * will re-allocate it, so we can free it for now.
		 */
		iwl_trans_free_tx_cmd(mvm->trans, dev_cmd);
		spin_unlock(&mvmsta->lock);
		return 0;
1153 1154
	}

1155
	if (!iwl_mvm_has_new_tx_api(mvm)) {
1156 1157 1158 1159 1160 1161 1162
		/* Keep track of the time of the last frame for this RA/TID */
		mvm->queue_info[txq_id].last_frame_time[tid] = jiffies;

		/*
		 * If we have timed-out TIDs - schedule the worker that will
		 * reconfig the queues and update them
		 *
1163 1164 1165 1166 1167
		 * Note that the no lock is taken here in order to not serialize
		 * the TX flow. This isn't dangerous because scheduling
		 * mvm->add_stream_wk can't ruin the state, and if we DON'T
		 * schedule it due to some race condition then next TX we get
		 * here we will.
1168 1169 1170 1171 1172 1173
		 */
		if (unlikely(mvm->queue_info[txq_id].status ==
			     IWL_MVM_QUEUE_SHARED &&
			     iwl_mvm_txq_should_update(mvm, txq_id)))
			schedule_work(&mvm->add_stream_wk);
	}
1174

J
Johannes Berg 已提交
1175
	IWL_DEBUG_TX(mvm, "TX to [%d|%d] Q:%d - seq: 0x%x\n", mvmsta->sta_id,
1176
		     tid, txq_id, IEEE80211_SEQ_TO_SN(seq_number));
J
Johannes Berg 已提交
1177

1178 1179 1180
	/* From now on, we cannot access info->control */
	iwl_mvm_skb_prepare_status(skb, dev_cmd);

J
Johannes Berg 已提交
1181 1182 1183
	if (iwl_trans_tx(mvm->trans, skb, dev_cmd, txq_id))
		goto drop_unlock_sta;

1184
	if (tid < IWL_MAX_TID_COUNT && !ieee80211_has_morefrags(fc))
1185
		mvmsta->tid_data[tid].seq_number = seq_number + 0x10;
J
Johannes Berg 已提交
1186 1187 1188

	spin_unlock(&mvmsta->lock);

1189 1190
	iwl_mvm_tx_pkt_queued(mvm, mvmsta, tid == IWL_MAX_TID_COUNT ? 0 : tid);

J
Johannes Berg 已提交
1191 1192 1193 1194 1195 1196 1197 1198 1199
	return 0;

drop_unlock_sta:
	iwl_trans_free_tx_cmd(mvm->trans, dev_cmd);
	spin_unlock(&mvmsta->lock);
drop:
	return -1;
}

1200 1201 1202 1203
int iwl_mvm_tx_skb(struct iwl_mvm *mvm, struct sk_buff *skb,
		   struct ieee80211_sta *sta)
{
	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1204
	struct ieee80211_tx_info info;
1205 1206 1207 1208 1209 1210 1211
	struct sk_buff_head mpdus_skbs;
	unsigned int payload_len;
	int ret;

	if (WARN_ON_ONCE(!mvmsta))
		return -1;

1212
	if (WARN_ON_ONCE(mvmsta->sta_id == IWL_MVM_INVALID_STA))
1213 1214
		return -1;

1215 1216
	memcpy(&info, skb->cb, sizeof(info));

1217
	if (!skb_is_gso(skb))
1218
		return iwl_mvm_tx_mpdu(mvm, skb, &info, sta);
1219 1220 1221 1222 1223

	payload_len = skb_tail_pointer(skb) - skb_transport_header(skb) -
		tcp_hdrlen(skb) + skb->data_len;

	if (payload_len <= skb_shinfo(skb)->gso_size)
1224
		return iwl_mvm_tx_mpdu(mvm, skb, &info, sta);
1225 1226 1227

	__skb_queue_head_init(&mpdus_skbs);

1228
	ret = iwl_mvm_tx_tso(mvm, skb, &info, sta, &mpdus_skbs);
1229 1230 1231 1232 1233 1234 1235
	if (ret)
		return ret;

	if (WARN_ON(skb_queue_empty(&mpdus_skbs)))
		return ret;

	while (!skb_queue_empty(&mpdus_skbs)) {
1236
		skb = __skb_dequeue(&mpdus_skbs);
1237

1238
		ret = iwl_mvm_tx_mpdu(mvm, skb, &info, sta);
1239 1240 1241 1242 1243 1244 1245 1246 1247
		if (ret) {
			__skb_queue_purge(&mpdus_skbs);
			return ret;
		}
	}

	return 0;
}

J
Johannes Berg 已提交
1248 1249 1250
static void iwl_mvm_check_ratid_empty(struct iwl_mvm *mvm,
				      struct ieee80211_sta *sta, u8 tid)
{
1251
	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
J
Johannes Berg 已提交
1252 1253
	struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
	struct ieee80211_vif *vif = mvmsta->vif;
1254
	u16 normalized_ssn;
J
Johannes Berg 已提交
1255 1256 1257

	lockdep_assert_held(&mvmsta->lock);

1258
	if ((tid_data->state == IWL_AGG_ON ||
1259
	     tid_data->state == IWL_EMPTYING_HW_QUEUE_DELBA) &&
1260
	    iwl_mvm_tid_queued(mvm, tid_data) == 0) {
1261
		/*
1262 1263 1264
		 * Now that this aggregation or DQA queue is empty tell
		 * mac80211 so it knows we no longer have frames buffered for
		 * the station on this TID (for the TIM bitmap calculation.)
1265 1266 1267 1268
		 */
		ieee80211_sta_set_buffered(sta, tid, false);
	}

1269
	/*
1270
	 * In 22000 HW, the next_reclaimed index is only 8 bit, so we'll need
1271 1272 1273 1274 1275 1276 1277
	 * to align the wrap around of ssn so we compare relevant values.
	 */
	normalized_ssn = tid_data->ssn;
	if (mvm->trans->cfg->gen2)
		normalized_ssn &= 0xff;

	if (normalized_ssn != tid_data->next_reclaimed)
J
Johannes Berg 已提交
1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341
		return;

	switch (tid_data->state) {
	case IWL_EMPTYING_HW_QUEUE_ADDBA:
		IWL_DEBUG_TX_QUEUES(mvm,
				    "Can continue addBA flow ssn = next_recl = %d\n",
				    tid_data->next_reclaimed);
		tid_data->state = IWL_AGG_STARTING;
		ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
		break;

	case IWL_EMPTYING_HW_QUEUE_DELBA:
		IWL_DEBUG_TX_QUEUES(mvm,
				    "Can continue DELBA flow ssn = next_recl = %d\n",
				    tid_data->next_reclaimed);
		tid_data->state = IWL_AGG_OFF;
		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
		break;

	default:
		break;
	}
}

#ifdef CONFIG_IWLWIFI_DEBUG
const char *iwl_mvm_get_tx_fail_reason(u32 status)
{
#define TX_STATUS_FAIL(x) case TX_STATUS_FAIL_ ## x: return #x
#define TX_STATUS_POSTPONE(x) case TX_STATUS_POSTPONE_ ## x: return #x

	switch (status & TX_STATUS_MSK) {
	case TX_STATUS_SUCCESS:
		return "SUCCESS";
	TX_STATUS_POSTPONE(DELAY);
	TX_STATUS_POSTPONE(FEW_BYTES);
	TX_STATUS_POSTPONE(BT_PRIO);
	TX_STATUS_POSTPONE(QUIET_PERIOD);
	TX_STATUS_POSTPONE(CALC_TTAK);
	TX_STATUS_FAIL(INTERNAL_CROSSED_RETRY);
	TX_STATUS_FAIL(SHORT_LIMIT);
	TX_STATUS_FAIL(LONG_LIMIT);
	TX_STATUS_FAIL(UNDERRUN);
	TX_STATUS_FAIL(DRAIN_FLOW);
	TX_STATUS_FAIL(RFKILL_FLUSH);
	TX_STATUS_FAIL(LIFE_EXPIRE);
	TX_STATUS_FAIL(DEST_PS);
	TX_STATUS_FAIL(HOST_ABORTED);
	TX_STATUS_FAIL(BT_RETRY);
	TX_STATUS_FAIL(STA_INVALID);
	TX_STATUS_FAIL(FRAG_DROPPED);
	TX_STATUS_FAIL(TID_DISABLE);
	TX_STATUS_FAIL(FIFO_FLUSHED);
	TX_STATUS_FAIL(SMALL_CF_POLL);
	TX_STATUS_FAIL(FW_DROP);
	TX_STATUS_FAIL(STA_COLOR_MISMATCH);
	}

	return "UNKNOWN";

#undef TX_STATUS_FAIL
#undef TX_STATUS_POSTPONE
}
#endif /* CONFIG_IWLWIFI_DEBUG */

E
Eyal Shapira 已提交
1342
void iwl_mvm_hwrate_to_tx_rate(u32 rate_n_flags,
1343
			       enum nl80211_band band,
E
Eyal Shapira 已提交
1344
			       struct ieee80211_tx_rate *r)
J
Johannes Berg 已提交
1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373
{
	if (rate_n_flags & RATE_HT_MCS_GF_MSK)
		r->flags |= IEEE80211_TX_RC_GREEN_FIELD;
	switch (rate_n_flags & RATE_MCS_CHAN_WIDTH_MSK) {
	case RATE_MCS_CHAN_WIDTH_20:
		break;
	case RATE_MCS_CHAN_WIDTH_40:
		r->flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
		break;
	case RATE_MCS_CHAN_WIDTH_80:
		r->flags |= IEEE80211_TX_RC_80_MHZ_WIDTH;
		break;
	case RATE_MCS_CHAN_WIDTH_160:
		r->flags |= IEEE80211_TX_RC_160_MHZ_WIDTH;
		break;
	}
	if (rate_n_flags & RATE_MCS_SGI_MSK)
		r->flags |= IEEE80211_TX_RC_SHORT_GI;
	if (rate_n_flags & RATE_MCS_HT_MSK) {
		r->flags |= IEEE80211_TX_RC_MCS;
		r->idx = rate_n_flags & RATE_HT_MCS_INDEX_MSK;
	} else if (rate_n_flags & RATE_MCS_VHT_MSK) {
		ieee80211_rate_set_vht(
			r, rate_n_flags & RATE_VHT_MCS_RATE_CODE_MSK,
			((rate_n_flags & RATE_VHT_MCS_NSS_MSK) >>
						RATE_VHT_MCS_NSS_POS) + 1);
		r->flags |= IEEE80211_TX_RC_VHT_MCS;
	} else {
		r->idx = iwl_mvm_legacy_rate_to_mac80211_idx(rate_n_flags,
E
Eyal Shapira 已提交
1374
							     band);
J
Johannes Berg 已提交
1375 1376 1377
	}
}

E
Eyal Shapira 已提交
1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390
/**
 * translate ucode response to mac80211 tx status control values
 */
static void iwl_mvm_hwrate_to_tx_status(u32 rate_n_flags,
					struct ieee80211_tx_info *info)
{
	struct ieee80211_tx_rate *r = &info->status.rates[0];

	info->status.antenna =
		((rate_n_flags & RATE_MCS_ANT_ABC_MSK) >> RATE_MCS_ANT_POS);
	iwl_mvm_hwrate_to_tx_rate(rate_n_flags, info->band, r);
}

1391 1392 1393 1394 1395 1396 1397
static void iwl_mvm_tx_status_check_trigger(struct iwl_mvm *mvm,
					    u32 status)
{
	struct iwl_fw_dbg_trigger_tlv *trig;
	struct iwl_fw_dbg_trigger_tx_status *status_trig;
	int i;

1398 1399 1400
	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, NULL,
				     FW_DBG_TRIGGER_TX_STATUS);
	if (!trig)
1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412
		return;

	status_trig = (void *)trig->data;

	for (i = 0; i < ARRAY_SIZE(status_trig->statuses); i++) {
		/* don't collect on status 0 */
		if (!status_trig->statuses[i].status)
			break;

		if (status_trig->statuses[i].status != (status & TX_STATUS_MSK))
			continue;

1413 1414 1415
		iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
					"Tx status %d was received",
					status & TX_STATUS_MSK);
1416 1417 1418 1419
		break;
	}
}

1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439
/**
 * iwl_mvm_get_scd_ssn - returns the SSN of the SCD
 * @tx_resp: the Tx response from the fw (agg or non-agg)
 *
 * When the fw sends an AMPDU, it fetches the MPDUs one after the other. Since
 * it can't know that everything will go well until the end of the AMPDU, it
 * can't know in advance the number of MPDUs that will be sent in the current
 * batch. This is why it writes the agg Tx response while it fetches the MPDUs.
 * Hence, it can't know in advance what the SSN of the SCD will be at the end
 * of the batch. This is why the SSN of the SCD is written at the end of the
 * whole struct at a variable offset. This function knows how to cope with the
 * variable offset and returns the SSN of the SCD.
 */
static inline u32 iwl_mvm_get_scd_ssn(struct iwl_mvm *mvm,
				      struct iwl_mvm_tx_resp *tx_resp)
{
	return le32_to_cpup((__le32 *)iwl_mvm_get_agg_status(mvm, tx_resp) +
			    tx_resp->frame_count) & 0xfff;
}

J
Johannes Berg 已提交
1440 1441 1442 1443 1444 1445
static void iwl_mvm_rx_tx_cmd_single(struct iwl_mvm *mvm,
				     struct iwl_rx_packet *pkt)
{
	struct ieee80211_sta *sta;
	u16 sequence = le16_to_cpu(pkt->hdr.sequence);
	int txq_id = SEQ_TO_QUEUE(sequence);
1446
	/* struct iwl_mvm_tx_resp_v3 is almost the same */
J
Johannes Berg 已提交
1447 1448 1449
	struct iwl_mvm_tx_resp *tx_resp = (void *)pkt->data;
	int sta_id = IWL_MVM_TX_RES_GET_RA(tx_resp->ra_tid);
	int tid = IWL_MVM_TX_RES_GET_TID(tx_resp->ra_tid);
1450 1451 1452 1453
	struct agg_tx_status *agg_status =
		iwl_mvm_get_agg_status(mvm, tx_resp);
	u32 status = le16_to_cpu(agg_status->status);
	u16 ssn = iwl_mvm_get_scd_ssn(mvm, tx_resp);
J
Johannes Berg 已提交
1454 1455 1456
	struct iwl_mvm_sta *mvmsta;
	struct sk_buff_head skbs;
	u8 skb_freed = 0;
1457
	u8 lq_color;
J
Johannes Berg 已提交
1458
	u16 next_reclaimed, seq_ctl;
1459
	bool is_ndp = false;
J
Johannes Berg 已提交
1460 1461 1462

	__skb_queue_head_init(&skbs);

1463
	if (iwl_mvm_has_new_tx_api(mvm))
1464
		txq_id = le16_to_cpu(tx_resp->tx_queue);
1465

J
Johannes Berg 已提交
1466 1467 1468 1469 1470 1471 1472 1473
	seq_ctl = le16_to_cpu(tx_resp->seq_ctl);

	/* we can free until ssn % q.n_bd not inclusive */
	iwl_trans_reclaim(mvm->trans, txq_id, ssn, &skbs);

	while (!skb_queue_empty(&skbs)) {
		struct sk_buff *skb = __skb_dequeue(&skbs);
		struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1474
		struct ieee80211_hdr *hdr = (void *)skb->data;
1475
		bool flushed = false;
J
Johannes Berg 已提交
1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488

		skb_freed++;

		iwl_trans_free_tx_cmd(mvm->trans, info->driver_data[1]);

		memset(&info->status, 0, sizeof(info->status));

		/* inform mac80211 about what happened with the frame */
		switch (status & TX_STATUS_MSK) {
		case TX_STATUS_SUCCESS:
		case TX_STATUS_DIRECT_DONE:
			info->flags |= IEEE80211_TX_STAT_ACK;
			break;
1489 1490 1491 1492
		case TX_STATUS_FAIL_FIFO_FLUSHED:
		case TX_STATUS_FAIL_DRAIN_FLOW:
			flushed = true;
			break;
J
Johannes Berg 已提交
1493
		case TX_STATUS_FAIL_DEST_PS:
1494
			/* the FW should have stopped the queue and not
1495 1496
			 * return this status
			 */
1497
			WARN_ON(1);
J
Johannes Berg 已提交
1498 1499 1500 1501 1502 1503
			info->flags |= IEEE80211_TX_STAT_TX_FILTERED;
			break;
		default:
			break;
		}

1504 1505 1506 1507 1508 1509 1510 1511
		/*
		 * If we are freeing multiple frames, mark all the frames
		 * but the first one as acked, since they were acknowledged
		 * before
		 * */
		if (skb_freed > 1)
			info->flags |= IEEE80211_TX_STAT_ACK;

1512 1513
		iwl_mvm_tx_status_check_trigger(mvm, status);

J
Johannes Berg 已提交
1514
		info->status.rates[0].count = tx_resp->failure_frame + 1;
E
Eyal Shapira 已提交
1515 1516
		iwl_mvm_hwrate_to_tx_status(le32_to_cpu(tx_resp->initial_rate),
					    info);
1517 1518
		info->status.status_driver_data[1] =
			(void *)(uintptr_t)le32_to_cpu(tx_resp->initial_rate);
J
Johannes Berg 已提交
1519 1520

		/* Single frame failure in an AMPDU queue => send BAR */
1521
		if (info->flags & IEEE80211_TX_CTL_AMPDU &&
1522
		    !(info->flags & IEEE80211_TX_STAT_ACK) &&
1523
		    !(info->flags & IEEE80211_TX_STAT_TX_FILTERED) && !flushed)
J
Johannes Berg 已提交
1524
			info->flags |= IEEE80211_TX_STAT_AMPDU_NO_BACK;
1525
		info->flags &= ~IEEE80211_TX_CTL_AMPDU;
J
Johannes Berg 已提交
1526

1527 1528 1529 1530
		/* W/A FW bug: seq_ctl is wrong upon failure / BAR frame */
		if (ieee80211_is_back_req(hdr->frame_control))
			seq_ctl = 0;
		else if (status != TX_STATUS_SUCCESS)
J
Johannes Berg 已提交
1531 1532
			seq_ctl = le16_to_cpu(hdr->seq_ctrl);

1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546
		if (unlikely(!seq_ctl)) {
			struct ieee80211_hdr *hdr = (void *)skb->data;

			/*
			 * If it is an NDP, we can't update next_reclaim since
			 * its sequence control is 0. Note that for that same
			 * reason, NDPs are never sent to A-MPDU'able queues
			 * so that we can never have more than one freed frame
			 * for a single Tx resonse (see WARN_ON below).
			 */
			if (ieee80211_is_qos_nullfunc(hdr->frame_control))
				is_ndp = true;
		}

1547 1548 1549 1550 1551 1552
		/*
		 * TODO: this is not accurate if we are freeing more than one
		 * packet.
		 */
		info->status.tx_time =
			le16_to_cpu(tx_resp->wireless_media_time);
1553
		BUILD_BUG_ON(ARRAY_SIZE(info->status.status_driver_data) < 1);
1554
		lq_color = TX_RES_RATE_TABLE_COL_GET(tx_resp->tlc_info);
1555
		info->status.status_driver_data[0] =
1556
			RS_DRV_DATA_PACK(lq_color, tx_resp->reduced_tpc);
1557

1558
		ieee80211_tx_status(mvm->hw, skb);
J
Johannes Berg 已提交
1559 1560
	}

1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575
	/* This is an aggregation queue or might become one, so we use
	 * the ssn since: ssn = wifi seq_num % 256.
	 * The seq_ctl is the sequence control of the packet to which
	 * this Tx response relates. But if there is a hole in the
	 * bitmap of the BA we received, this Tx response may allow to
	 * reclaim the hole and all the subsequent packets that were
	 * already acked. In that case, seq_ctl != ssn, and the next
	 * packet to be reclaimed will be ssn and not seq_ctl. In that
	 * case, several packets will be reclaimed even if
	 * frame_count = 1.
	 *
	 * The ssn is the index (% 256) of the latest packet that has
	 * treated (acked / dropped) + 1.
	 */
	next_reclaimed = ssn;
J
Johannes Berg 已提交
1576 1577

	IWL_DEBUG_TX_REPLY(mvm,
1578 1579 1580 1581 1582 1583
			   "TXQ %d status %s (0x%08x)\n",
			   txq_id, iwl_mvm_get_tx_fail_reason(status), status);

	IWL_DEBUG_TX_REPLY(mvm,
			   "\t\t\t\tinitial_rate 0x%x retries %d, idx=%d ssn=%d next_reclaimed=0x%x seq_ctl=0x%x\n",
			   le32_to_cpu(tx_resp->initial_rate),
J
Johannes Berg 已提交
1584 1585 1586 1587 1588 1589
			   tx_resp->failure_frame, SEQ_TO_INDEX(sequence),
			   ssn, next_reclaimed, seq_ctl);

	rcu_read_lock();

	sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
1590 1591 1592 1593 1594 1595
	/*
	 * sta can't be NULL otherwise it'd mean that the sta has been freed in
	 * the firmware while we still have packets for it in the Tx queues.
	 */
	if (WARN_ON_ONCE(!sta))
		goto out;
J
Johannes Berg 已提交
1596

1597
	if (!IS_ERR(sta)) {
1598
		mvmsta = iwl_mvm_sta_from_mac80211(sta);
J
Johannes Berg 已提交
1599

1600 1601 1602
		iwl_mvm_tx_airtime(mvm, mvmsta,
				   le16_to_cpu(tx_resp->wireless_media_time));

1603
		if (sta->wme && tid != IWL_MGMT_TID) {
J
Johannes Berg 已提交
1604 1605
			struct iwl_mvm_tid_data *tid_data =
				&mvmsta->tid_data[tid];
1606
			bool send_eosp_ndp = false;
J
Johannes Berg 已提交
1607

1608
			spin_lock_bh(&mvmsta->lock);
1609

1610 1611 1612 1613 1614 1615 1616 1617 1618 1619
			if (!is_ndp) {
				tid_data->next_reclaimed = next_reclaimed;
				IWL_DEBUG_TX_REPLY(mvm,
						   "Next reclaimed packet:%d\n",
						   next_reclaimed);
			} else {
				IWL_DEBUG_TX_REPLY(mvm,
						   "NDP - don't update next_reclaimed\n");
			}

J
Johannes Berg 已提交
1620
			iwl_mvm_check_ratid_empty(mvm, sta, tid);
1621 1622 1623 1624

			if (mvmsta->sleep_tx_count) {
				mvmsta->sleep_tx_count--;
				if (mvmsta->sleep_tx_count &&
1625
				    !iwl_mvm_tid_queued(mvm, tid_data)) {
1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642
					/*
					 * The number of frames in the queue
					 * dropped to 0 even if we sent less
					 * frames than we thought we had on the
					 * Tx queue.
					 * This means we had holes in the BA
					 * window that we just filled, ask
					 * mac80211 to send EOSP since the
					 * firmware won't know how to do that.
					 * Send NDP and the firmware will send
					 * EOSP notification that will trigger
					 * a call to ieee80211_sta_eosp().
					 */
					send_eosp_ndp = true;
				}
			}

1643
			spin_unlock_bh(&mvmsta->lock);
1644 1645 1646 1647 1648 1649 1650
			if (send_eosp_ndp) {
				iwl_mvm_sta_modify_sleep_tx_count(mvm, sta,
					IEEE80211_FRAME_RELEASE_UAPSD,
					1, tid, false, false);
				mvmsta->sleep_tx_count = 0;
				ieee80211_send_eosp_nullfunc(sta, tid);
			}
J
Johannes Berg 已提交
1651
		}
1652 1653 1654 1655 1656

		if (mvmsta->next_status_eosp) {
			mvmsta->next_status_eosp = false;
			ieee80211_sta_eosp(sta);
		}
J
Johannes Berg 已提交
1657 1658 1659 1660
	} else {
		mvmsta = NULL;
	}

1661
out:
J
Johannes Berg 已提交
1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674
	rcu_read_unlock();
}

#ifdef CONFIG_IWLWIFI_DEBUG
#define AGG_TX_STATE_(x) case AGG_TX_STATE_ ## x: return #x
static const char *iwl_get_agg_tx_status(u16 status)
{
	switch (status & AGG_TX_STATE_STATUS_MSK) {
	AGG_TX_STATE_(TRANSMITTED);
	AGG_TX_STATE_(UNDERRUN);
	AGG_TX_STATE_(BT_PRIO);
	AGG_TX_STATE_(FEW_BYTES);
	AGG_TX_STATE_(ABORT);
1675
	AGG_TX_STATE_(TX_ON_AIR_DROP);
J
Johannes Berg 已提交
1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691
	AGG_TX_STATE_(LAST_SENT_TRY_CNT);
	AGG_TX_STATE_(LAST_SENT_BT_KILL);
	AGG_TX_STATE_(SCD_QUERY);
	AGG_TX_STATE_(TEST_BAD_CRC32);
	AGG_TX_STATE_(RESPONSE);
	AGG_TX_STATE_(DUMP_TX);
	AGG_TX_STATE_(DELAY_TX);
	}

	return "UNKNOWN";
}

static void iwl_mvm_rx_tx_cmd_agg_dbg(struct iwl_mvm *mvm,
				      struct iwl_rx_packet *pkt)
{
	struct iwl_mvm_tx_resp *tx_resp = (void *)pkt->data;
1692 1693
	struct agg_tx_status *frame_status =
		iwl_mvm_get_agg_status(mvm, tx_resp);
J
Johannes Berg 已提交
1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720
	int i;

	for (i = 0; i < tx_resp->frame_count; i++) {
		u16 fstatus = le16_to_cpu(frame_status[i].status);

		IWL_DEBUG_TX_REPLY(mvm,
				   "status %s (0x%04x), try-count (%d) seq (0x%x)\n",
				   iwl_get_agg_tx_status(fstatus),
				   fstatus & AGG_TX_STATE_STATUS_MSK,
				   (fstatus & AGG_TX_STATE_TRY_CNT_MSK) >>
					AGG_TX_STATE_TRY_CNT_POS,
				   le16_to_cpu(frame_status[i].sequence));
	}
}
#else
static void iwl_mvm_rx_tx_cmd_agg_dbg(struct iwl_mvm *mvm,
				      struct iwl_rx_packet *pkt)
{}
#endif /* CONFIG_IWLWIFI_DEBUG */

static void iwl_mvm_rx_tx_cmd_agg(struct iwl_mvm *mvm,
				  struct iwl_rx_packet *pkt)
{
	struct iwl_mvm_tx_resp *tx_resp = (void *)pkt->data;
	int sta_id = IWL_MVM_TX_RES_GET_RA(tx_resp->ra_tid);
	int tid = IWL_MVM_TX_RES_GET_TID(tx_resp->ra_tid);
	u16 sequence = le16_to_cpu(pkt->hdr.sequence);
1721
	struct iwl_mvm_sta *mvmsta;
1722
	int queue = SEQ_TO_QUEUE(sequence);
1723
	struct ieee80211_sta *sta;
J
Johannes Berg 已提交
1724

1725 1726
	if (WARN_ON_ONCE(queue < IWL_MVM_DQA_MIN_DATA_QUEUE &&
			 (queue != IWL_MVM_DQA_BSS_CLIENT_QUEUE)))
J
Johannes Berg 已提交
1727 1728 1729 1730 1731 1732
		return;

	iwl_mvm_rx_tx_cmd_agg_dbg(mvm, pkt);

	rcu_read_lock();

1733
	mvmsta = iwl_mvm_sta_from_staid_rcu(mvm, sta_id);
J
Johannes Berg 已提交
1734

1735 1736 1737 1738 1739 1740
	sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
	if (WARN_ON_ONCE(!sta || !sta->wme)) {
		rcu_read_unlock();
		return;
	}

1741
	if (!WARN_ON_ONCE(!mvmsta)) {
J
Johannes Berg 已提交
1742 1743
		mvmsta->tid_data[tid].rate_n_flags =
			le32_to_cpu(tx_resp->initial_rate);
1744 1745
		mvmsta->tid_data[tid].tx_time =
			le16_to_cpu(tx_resp->wireless_media_time);
1746
		mvmsta->tid_data[tid].lq_color =
1747
			TX_RES_RATE_TABLE_COL_GET(tx_resp->tlc_info);
1748 1749
		iwl_mvm_tx_airtime(mvm, mvmsta,
				   le16_to_cpu(tx_resp->wireless_media_time));
J
Johannes Berg 已提交
1750 1751 1752 1753 1754
	}

	rcu_read_unlock();
}

1755
void iwl_mvm_rx_tx_cmd(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb)
J
Johannes Berg 已提交
1756 1757 1758 1759 1760 1761 1762 1763 1764 1765
{
	struct iwl_rx_packet *pkt = rxb_addr(rxb);
	struct iwl_mvm_tx_resp *tx_resp = (void *)pkt->data;

	if (tx_resp->frame_count == 1)
		iwl_mvm_rx_tx_cmd_single(mvm, pkt);
	else
		iwl_mvm_rx_tx_cmd_agg(mvm, pkt);
}

1766 1767 1768
static void iwl_mvm_tx_reclaim(struct iwl_mvm *mvm, int sta_id, int tid,
			       int txq, int index,
			       struct ieee80211_tx_info *ba_info, u32 rate)
J
Johannes Berg 已提交
1769 1770 1771 1772 1773 1774
{
	struct sk_buff_head reclaimed_skbs;
	struct iwl_mvm_tid_data *tid_data;
	struct ieee80211_sta *sta;
	struct iwl_mvm_sta *mvmsta;
	struct sk_buff *skb;
1775
	int freed;
J
Johannes Berg 已提交
1776

1777
	if (WARN_ONCE(sta_id >= IWL_MVM_STATION_COUNT ||
1778
		      tid > IWL_MAX_TID_COUNT,
1779
		      "sta_id %d tid %d", sta_id, tid))
1780
		return;
1781

J
Johannes Berg 已提交
1782 1783 1784 1785 1786 1787 1788
	rcu_read_lock();

	sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);

	/* Reclaiming frames for a station that has been deleted ? */
	if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta))) {
		rcu_read_unlock();
1789
		return;
J
Johannes Berg 已提交
1790 1791
	}

1792
	mvmsta = iwl_mvm_sta_from_mac80211(sta);
J
Johannes Berg 已提交
1793 1794
	tid_data = &mvmsta->tid_data[tid];

1795
	if (tid_data->txq_id != txq) {
1796
		IWL_ERR(mvm,
1797 1798
			"invalid BA notification: Q %d, tid %d\n",
			tid_data->txq_id, tid);
J
Johannes Berg 已提交
1799
		rcu_read_unlock();
1800
		return;
J
Johannes Berg 已提交
1801 1802
	}

1803
	spin_lock_bh(&mvmsta->lock);
J
Johannes Berg 已提交
1804 1805 1806 1807 1808 1809 1810 1811

	__skb_queue_head_init(&reclaimed_skbs);

	/*
	 * Release all TFDs before the SSN, i.e. all TFDs in front of
	 * block-ack window (we assume that they've been successfully
	 * transmitted ... if not, it's too late anyway).
	 */
1812
	iwl_trans_reclaim(mvm->trans, txq, index, &reclaimed_skbs);
J
Johannes Berg 已提交
1813

1814
	tid_data->next_reclaimed = index;
J
Johannes Berg 已提交
1815 1816 1817 1818

	iwl_mvm_check_ratid_empty(mvm, sta, tid);

	freed = 0;
1819 1820 1821 1822 1823

	/* pack lq color from tid_data along the reduced txp */
	ba_info->status.status_driver_data[0] =
		RS_DRV_DATA_PACK(tid_data->lq_color,
				 ba_info->status.status_driver_data[0]);
1824
	ba_info->status.status_driver_data[1] = (void *)(uintptr_t)rate;
J
Johannes Berg 已提交
1825 1826

	skb_queue_walk(&reclaimed_skbs, skb) {
1827 1828
		struct ieee80211_hdr *hdr = (void *)skb->data;
		struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
J
Johannes Berg 已提交
1829 1830 1831 1832

		if (ieee80211_is_data_qos(hdr->frame_control))
			freed++;
		else
1833
			WARN_ON_ONCE(tid != IWL_MAX_TID_COUNT);
J
Johannes Berg 已提交
1834 1835 1836

		iwl_trans_free_tx_cmd(mvm->trans, info->driver_data[1]);

1837 1838 1839 1840 1841 1842 1843
		memset(&info->status, 0, sizeof(info->status));
		/* Packet was transmitted successfully, failures come as single
		 * frames because before failing a frame the firmware transmits
		 * it without aggregation at least once.
		 */
		info->flags |= IEEE80211_TX_STAT_ACK;

1844 1845
		/* this is the first skb we deliver in this batch */
		/* put the rate scaling data there */
1846 1847 1848 1849 1850 1851
		if (freed == 1) {
			info->flags |= IEEE80211_TX_STAT_AMPDU;
			memcpy(&info->status, &ba_info->status,
			       sizeof(ba_info->status));
			iwl_mvm_hwrate_to_tx_status(rate, info);
		}
J
Johannes Berg 已提交
1852 1853
	}

1854
	spin_unlock_bh(&mvmsta->lock);
J
Johannes Berg 已提交
1855

1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869
	/* We got a BA notif with 0 acked or scd_ssn didn't progress which is
	 * possible (i.e. first MPDU in the aggregation wasn't acked)
	 * Still it's important to update RS about sent vs. acked.
	 */
	if (skb_queue_empty(&reclaimed_skbs)) {
		struct ieee80211_chanctx_conf *chanctx_conf = NULL;

		if (mvmsta->vif)
			chanctx_conf =
				rcu_dereference(mvmsta->vif->chanctx_conf);

		if (WARN_ON_ONCE(!chanctx_conf))
			goto out;

1870 1871
		ba_info->band = chanctx_conf->def.chan->band;
		iwl_mvm_hwrate_to_tx_status(rate, ba_info);
1872

1873
		if (!iwl_mvm_has_tlc_offload(mvm)) {
1874 1875 1876 1877
			IWL_DEBUG_TX_REPLY(mvm,
					   "No reclaim. Update rs directly\n");
			iwl_mvm_rs_tx_status(mvm, sta, tid, ba_info, false);
		}
1878 1879 1880
	}

out:
J
Johannes Berg 已提交
1881 1882 1883 1884
	rcu_read_unlock();

	while (!skb_queue_empty(&reclaimed_skbs)) {
		skb = __skb_dequeue(&reclaimed_skbs);
1885
		ieee80211_tx_status(mvm->hw, skb);
J
Johannes Berg 已提交
1886 1887 1888
	}
}

1889 1890 1891 1892 1893 1894 1895 1896 1897
void iwl_mvm_rx_ba_notif(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb)
{
	struct iwl_rx_packet *pkt = rxb_addr(rxb);
	int sta_id, tid, txq, index;
	struct ieee80211_tx_info ba_info = {};
	struct iwl_mvm_ba_notif *ba_notif;
	struct iwl_mvm_tid_data *tid_data;
	struct iwl_mvm_sta *mvmsta;

1898 1899
	ba_info.flags = IEEE80211_TX_STAT_AMPDU;

1900 1901 1902
	if (iwl_mvm_has_new_tx_api(mvm)) {
		struct iwl_mvm_compressed_ba_notif *ba_res =
			(void *)pkt->data;
L
Liad Kaufman 已提交
1903
		u8 lq_color = TX_RES_RATE_TABLE_COL_GET(ba_res->tlc_rate_info);
1904
		int i;
1905 1906 1907 1908 1909 1910 1911 1912 1913

		sta_id = ba_res->sta_id;
		ba_info.status.ampdu_ack_len = (u8)le16_to_cpu(ba_res->done);
		ba_info.status.ampdu_len = (u8)le16_to_cpu(ba_res->txed);
		ba_info.status.tx_time =
			(u16)le32_to_cpu(ba_res->wireless_time);
		ba_info.status.status_driver_data[0] =
			(void *)(uintptr_t)ba_res->reduced_txp;

1914 1915 1916
		if (!le16_to_cpu(ba_res->tfd_cnt))
			goto out;

L
Liad Kaufman 已提交
1917 1918 1919 1920 1921 1922
		rcu_read_lock();

		mvmsta = iwl_mvm_sta_from_staid_rcu(mvm, sta_id);
		if (!mvmsta)
			goto out_unlock;

1923 1924 1925 1926 1927
		/* Free per TID */
		for (i = 0; i < le16_to_cpu(ba_res->tfd_cnt); i++) {
			struct iwl_mvm_compressed_ba_tfd *ba_tfd =
				&ba_res->tfd[i];

1928 1929 1930 1931
			tid = ba_tfd->tid;
			if (tid == IWL_MGMT_TID)
				tid = IWL_MAX_TID_COUNT;

L
Liad Kaufman 已提交
1932
			mvmsta->tid_data[i].lq_color = lq_color;
1933
			iwl_mvm_tx_reclaim(mvm, sta_id, tid,
1934 1935 1936 1937 1938
					   (int)(le16_to_cpu(ba_tfd->q_num)),
					   le16_to_cpu(ba_tfd->tfd_index),
					   &ba_info,
					   le32_to_cpu(ba_res->tx_rate));
		}
1939

1940 1941
		iwl_mvm_tx_airtime(mvm, mvmsta,
				   le32_to_cpu(ba_res->wireless_time));
L
Liad Kaufman 已提交
1942 1943
out_unlock:
		rcu_read_unlock();
1944
out:
1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983
		IWL_DEBUG_TX_REPLY(mvm,
				   "BA_NOTIFICATION Received from sta_id = %d, flags %x, sent:%d, acked:%d\n",
				   sta_id, le32_to_cpu(ba_res->flags),
				   le16_to_cpu(ba_res->txed),
				   le16_to_cpu(ba_res->done));
		return;
	}

	ba_notif = (void *)pkt->data;
	sta_id = ba_notif->sta_id;
	tid = ba_notif->tid;
	/* "flow" corresponds to Tx queue */
	txq = le16_to_cpu(ba_notif->scd_flow);
	/* "ssn" is start of block-ack Tx window, corresponds to index
	 * (in Tx queue's circular buffer) of first TFD/frame in window */
	index = le16_to_cpu(ba_notif->scd_ssn);

	rcu_read_lock();
	mvmsta = iwl_mvm_sta_from_staid_rcu(mvm, sta_id);
	if (WARN_ON_ONCE(!mvmsta)) {
		rcu_read_unlock();
		return;
	}

	tid_data = &mvmsta->tid_data[tid];

	ba_info.status.ampdu_ack_len = ba_notif->txed_2_done;
	ba_info.status.ampdu_len = ba_notif->txed;
	ba_info.status.tx_time = tid_data->tx_time;
	ba_info.status.status_driver_data[0] =
		(void *)(uintptr_t)ba_notif->reduced_txp;

	rcu_read_unlock();

	iwl_mvm_tx_reclaim(mvm, sta_id, tid, txq, index, &ba_info,
			   tid_data->rate_n_flags);

	IWL_DEBUG_TX_REPLY(mvm,
			   "BA_NOTIFICATION Received from %pM, sta_id = %d\n",
1984
			   ba_notif->sta_addr, ba_notif->sta_id);
1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995

	IWL_DEBUG_TX_REPLY(mvm,
			   "TID = %d, SeqCtl = %d, bitmap = 0x%llx, scd_flow = %d, scd_ssn = %d sent:%d, acked:%d\n",
			   ba_notif->tid, le16_to_cpu(ba_notif->seq_ctl),
			   le64_to_cpu(ba_notif->bitmap), txq, index,
			   ba_notif->txed, ba_notif->txed_2_done);

	IWL_DEBUG_TX_REPLY(mvm, "reduced txp from ba notif %d\n",
			   ba_notif->reduced_txp);
}

1996 1997 1998 1999 2000 2001 2002 2003
/*
 * Note that there are transports that buffer frames before they reach
 * the firmware. This means that after flush_tx_path is called, the
 * queue might not be empty. The race-free way to handle this is to:
 * 1) set the station as draining
 * 2) flush the Tx path
 * 3) wait for the transport queues to be empty
 */
2004
int iwl_mvm_flush_tx_path(struct iwl_mvm *mvm, u32 tfd_msk, u32 flags)
J
Johannes Berg 已提交
2005 2006
{
	int ret;
2007
	struct iwl_tx_path_flush_cmd_v1 flush_cmd = {
J
Johannes Berg 已提交
2008 2009 2010 2011
		.queues_ctl = cpu_to_le32(tfd_msk),
		.flush_ctl = cpu_to_le16(DUMP_TX_FIFO_FLUSH),
	};

2012 2013
	WARN_ON(iwl_mvm_has_new_tx_api(mvm));

J
Johannes Berg 已提交
2014 2015 2016 2017 2018 2019
	ret = iwl_mvm_send_cmd_pdu(mvm, TXPATH_FLUSH, flags,
				   sizeof(flush_cmd), &flush_cmd);
	if (ret)
		IWL_ERR(mvm, "Failed to send flush command (%d)\n", ret);
	return ret;
}
2020

2021 2022
int iwl_mvm_flush_sta_tids(struct iwl_mvm *mvm, u32 sta_id,
			   u16 tids, u32 flags)
2023
{
2024 2025 2026 2027 2028
	int ret;
	struct iwl_tx_path_flush_cmd flush_cmd = {
		.sta_id = cpu_to_le32(sta_id),
		.tid_mask = cpu_to_le16(tids),
	};
2029

2030
	WARN_ON(!iwl_mvm_has_new_tx_api(mvm));
2031

2032 2033 2034 2035 2036 2037
	ret = iwl_mvm_send_cmd_pdu(mvm, TXPATH_FLUSH, flags,
				   sizeof(flush_cmd), &flush_cmd);
	if (ret)
		IWL_ERR(mvm, "Failed to send flush command (%d)\n", ret);
	return ret;
}
2038

2039 2040 2041 2042 2043
int iwl_mvm_flush_sta(struct iwl_mvm *mvm, void *sta, bool internal, u32 flags)
{
	struct iwl_mvm_int_sta *int_sta = sta;
	struct iwl_mvm_sta *mvm_sta = sta;

2044 2045
	BUILD_BUG_ON(offsetof(struct iwl_mvm_int_sta, sta_id) !=
		     offsetof(struct iwl_mvm_sta, sta_id));
2046

2047
	if (iwl_mvm_has_new_tx_api(mvm))
2048
		return iwl_mvm_flush_sta_tids(mvm, mvm_sta->sta_id,
2049
					      0xff | BIT(IWL_MGMT_TID), flags);
2050

2051 2052 2053 2054 2055
	if (internal)
		return iwl_mvm_flush_tx_path(mvm, int_sta->tfd_queue_msk,
					     flags);

	return iwl_mvm_flush_tx_path(mvm, mvm_sta->tfd_queue_msk, flags);
2056
}