tx.c 65.5 KB
Newer Older
Johannes Berg's avatar
Johannes Berg committed
1 2
// SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
/*
3
 * Copyright (C) 2012-2014, 2018-2023 Intel Corporation
Johannes Berg's avatar
Johannes Berg committed
4 5 6
 * Copyright (C) 2013-2015 Intel Mobile Communications GmbH
 * Copyright (C) 2016-2017 Intel Deutschland GmbH
 */
Johannes Berg's avatar
Johannes Berg committed
7 8
#include <linux/ieee80211.h>
#include <linux/etherdevice.h>
9
#include <linux/tcp.h>
10
#include <net/ip.h>
11
#include <net/ipv6.h>
Johannes Berg's avatar
Johannes Berg committed
12 13 14 15 16

#include "iwl-trans.h"
#include "iwl-eeprom-parse.h"
#include "mvm.h"
#include "sta.h"
17
#include "time-sync.h"
Johannes Berg's avatar
Johannes Berg committed
18

19 20 21 22 23 24 25
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;

26 27
	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, NULL, FW_DBG_TRIGGER_BA);
	if (!trig)
28 29 30 31 32 33 34
		return;

	ba_trig = (void *)trig->data;

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

35 36 37
	iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
				"BAR sent to %pM, tid %d, ssn %d",
				addr, tid, ssn);
38 39
}

40 41 42
#define OPT_HDR(type, skb, off) \
	(type *)(skb_network_header(skb) + (off))

43 44 45
static u32 iwl_mvm_tx_csum(struct iwl_mvm *mvm, struct sk_buff *skb,
			   struct ieee80211_tx_info *info,
			   bool amsdu)
46
{
47
	struct ieee80211_hdr *hdr = (void *)skb->data;
48
	u16 mh_len = ieee80211_hdrlen(hdr->frame_control);
49
	u16 offload_assist = 0;
50 51 52
#if IS_ENABLED(CONFIG_INET)
	u8 protocol = 0;

53 54
	/* Do not compute checksum if already computed */
	if (skb->ip_summed != CHECKSUM_PARTIAL)
55
		goto out;
56 57 58 59 60 61 62

	/* 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);
63
		goto out;
64 65 66 67 68 69 70 71 72 73 74 75
	}

	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)) {
76 77
			struct ipv6_opt_hdr *hp;

78 79 80
			/* only supported extension headers */
			if (protocol != NEXTHDR_ROUTING &&
			    protocol != NEXTHDR_HOP &&
81
			    protocol != NEXTHDR_DEST) {
82
				skb_checksum_help(skb);
83
				goto out;
84 85
			}

86 87 88
			hp = OPT_HDR(struct ipv6_opt_hdr, skb, off);
			protocol = hp->nexthdr;
			off += ipv6_optlen(hp);
89 90 91 92 93 94 95 96
		}
		/* 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);
97
		goto out;
98 99 100 101 102 103 104 105 106 107 108 109 110
	}

	/* 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) */
111
	if (skb->protocol == htons(ETH_P_IP) && amsdu) {
112 113 114 115 116 117 118 119 120 121
		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;

122 123
out:
#endif
124 125 126 127 128 129 130 131
	/*
	 * 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)
132 133 134 135
		mh_len += info->control.hw_key->iv_len;
	mh_len /= 2;
	offload_assist |= mh_len << TX_CMD_OFFLD_MH_SIZE;

136 137 138 139 140 141
	if (amsdu)
		offload_assist |= BIT(TX_CMD_OFFLD_AMSDU);
	else if (ieee80211_hdrlen(hdr->frame_control) % 4)
		/* padding is inserted later in transport */
		offload_assist |= BIT(TX_CMD_OFFLD_PAD);

142
	return offload_assist;
143 144
}

Johannes Berg's avatar
Johannes Berg committed
145 146 147
/*
 * Sets most of the Tx cmd's fields
 */
148 149 150
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)
Johannes Berg's avatar
Johannes Berg committed
151 152 153 154 155
{
	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;
156
	bool amsdu = false;
157
	u8 ac;
Johannes Berg's avatar
Johannes Berg committed
158

159 160 161
	if (!(info->flags & IEEE80211_TX_CTL_NO_ACK) ||
	    (ieee80211_is_probe_resp(fc) &&
	     !is_multicast_ether_addr(hdr->addr1)))
Johannes Berg's avatar
Johannes Berg committed
162 163 164 165 166 167 168 169 170 171 172 173 174 175
		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;
176
		amsdu = *qc & IEEE80211_QOS_CTL_A_MSDU_PRESENT;
177 178 179
	} else if (ieee80211_is_back_req(fc)) {
		struct ieee80211_bar *bar = (void *)skb->data;
		u16 control = le16_to_cpu(bar->control);
180
		u16 ssn = le16_to_cpu(bar->start_seq_num);
181 182 183 184 185 186

		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);
187 188
		iwl_mvm_bar_check_trigger(mvm, bar->ra, tx_cmd->tid_tspec,
					  ssn);
Johannes Berg's avatar
Johannes Berg committed
189
	} else {
190 191 192 193 194
		if (ieee80211_is_data(fc))
			tx_cmd->tid_tspec = IWL_TID_NON_QOS;
		else
			tx_cmd->tid_tspec = IWL_MAX_TID_COUNT;

Johannes Berg's avatar
Johannes Berg committed
195 196 197 198 199 200
		if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ)
			tx_flags |= TX_CMD_FLG_SEQ_CTL;
		else
			tx_flags &= ~TX_CMD_FLG_SEQ_CTL;
	}

201
	/* Default to 0 (BE) when tid_spec is set to IWL_MAX_TID_COUNT */
202 203 204 205 206
	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];

207 208 209
	tx_flags |= iwl_mvm_bt_coex_tx_prio(mvm, hdr, info, ac) <<
			TX_CMD_FLG_BT_PRIO_POS;

Johannes Berg's avatar
Johannes Berg committed
210 211
	if (ieee80211_is_mgmt(fc)) {
		if (ieee80211_is_assoc_req(fc) || ieee80211_is_reassoc_req(fc))
212 213 214
			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);
Johannes Berg's avatar
Johannes Berg committed
215
		else
216
			tx_cmd->pm_frame_timeout = cpu_to_le16(PM_FRAME_MGMT);
Johannes Berg's avatar
Johannes Berg committed
217 218 219 220 221

		/* The spec allows Action frames in A-MPDU, we don't support
		 * it
		 */
		WARN_ON_ONCE(info->flags & IEEE80211_TX_CTL_AMPDU);
222
	} else if (info->control.flags & IEEE80211_TX_CTRL_PORT_CTRL_PROTO) {
223
		tx_cmd->pm_frame_timeout = cpu_to_le16(PM_FRAME_MGMT);
Johannes Berg's avatar
Johannes Berg committed
224
	} else {
225
		tx_cmd->pm_frame_timeout = cpu_to_le16(PM_FRAME_NONE);
Johannes Berg's avatar
Johannes Berg committed
226 227 228
	}

	if (ieee80211_is_data(fc) && len > mvm->rts_threshold &&
229
	    !is_multicast_ether_addr(hdr->addr1))
Johannes Berg's avatar
Johannes Berg committed
230 231
		tx_flags |= TX_CMD_FLG_PROT_REQUIRE;

232 233
	if (fw_has_capa(&mvm->fw->ucode_capa,
			IWL_UCODE_TLV_CAPA_TXPOWER_INSERTION_SUPPORT) &&
234 235 236
	    ieee80211_action_contains_tpc(skb))
		tx_flags |= TX_CMD_FLG_WRITE_TX_POWER;

Johannes Berg's avatar
Johannes Berg committed
237
	tx_cmd->tx_flags = cpu_to_le32(tx_flags);
238 239
	/* Total # bytes to be transmitted - PCIe code will adjust for A-MSDU */
	tx_cmd->len = cpu_to_le16((u16)skb->len);
Johannes Berg's avatar
Johannes Berg committed
240 241
	tx_cmd->life_time = cpu_to_le32(TX_CMD_LIFE_TIME_INFINITE);
	tx_cmd->sta_id = sta_id;
242

243
	tx_cmd->offload_assist =
244
		cpu_to_le16(iwl_mvm_tx_csum(mvm, skb, info, amsdu));
Johannes Berg's avatar
Johannes Berg committed
245 246
}

247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
static u32 iwl_mvm_get_tx_ant(struct iwl_mvm *mvm,
			      struct ieee80211_tx_info *info,
			      struct ieee80211_sta *sta, __le16 fc)
{
	if (info->band == NL80211_BAND_2GHZ &&
	    !iwl_mvm_bt_coex_is_shared_ant_avail(mvm))
		return mvm->cfg->non_shared_ant << RATE_MCS_ANT_POS;

	if (sta && ieee80211_is_data(fc)) {
		struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);

		return BIT(mvmsta->tx_ant) << RATE_MCS_ANT_POS;
	}

	return BIT(mvm->mgmt_last_antenna_idx) << RATE_MCS_ANT_POS;
}

264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
static u32 iwl_mvm_get_inject_tx_rate(struct iwl_mvm *mvm,
				      struct ieee80211_tx_info *info)
{
	struct ieee80211_tx_rate *rate = &info->control.rates[0];
	u32 result;

	/*
	 * we only care about legacy/HT/VHT so far, so we can
	 * build in v1 and use iwl_new_rate_from_v1()
	 */

	if (rate->flags & IEEE80211_TX_RC_VHT_MCS) {
		u8 mcs = ieee80211_rate_get_vht_mcs(rate);
		u8 nss = ieee80211_rate_get_vht_nss(rate);

		result = RATE_MCS_VHT_MSK_V1;
		result |= u32_encode_bits(mcs, RATE_VHT_MCS_RATE_CODE_MSK);
		result |= u32_encode_bits(nss, RATE_MCS_NSS_MSK);
		if (rate->flags & IEEE80211_TX_RC_SHORT_GI)
			result |= RATE_MCS_SGI_MSK_V1;
		if (rate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
			result |= u32_encode_bits(1, RATE_MCS_CHAN_WIDTH_MSK_V1);
		else if (rate->flags & IEEE80211_TX_RC_80_MHZ_WIDTH)
			result |= u32_encode_bits(2, RATE_MCS_CHAN_WIDTH_MSK_V1);
		else if (rate->flags & IEEE80211_TX_RC_160_MHZ_WIDTH)
			result |= u32_encode_bits(3, RATE_MCS_CHAN_WIDTH_MSK_V1);
	} else if (rate->flags & IEEE80211_TX_RC_MCS) {
		result = RATE_MCS_HT_MSK_V1;
		result |= u32_encode_bits(rate->idx,
					  RATE_HT_MCS_RATE_CODE_MSK_V1 |
					  RATE_HT_MCS_NSS_MSK_V1);
		if (rate->flags & IEEE80211_TX_RC_SHORT_GI)
			result |= RATE_MCS_SGI_MSK_V1;
		if (rate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
			result |= u32_encode_bits(1, RATE_MCS_CHAN_WIDTH_MSK_V1);
299
		if (info->flags & IEEE80211_TX_CTL_LDPC)
300
			result |= RATE_MCS_LDPC_MSK_V1;
301
		if (u32_get_bits(info->flags, IEEE80211_TX_CTL_STBC))
302 303 304 305 306 307 308 309 310 311
			result |= RATE_MCS_STBC_MSK;
	} else {
		return 0;
	}

	if (iwl_fw_lookup_notif_ver(mvm->fw, LONG_GROUP, TX_CMD, 0) > 6)
		return iwl_new_rate_from_v1(result);
	return result;
}

312 313
static u32 iwl_mvm_get_tx_rate(struct iwl_mvm *mvm,
			       struct ieee80211_tx_info *info,
314
			       struct ieee80211_sta *sta, __le16 fc)
315
{
316
	int rate_idx = -1;
317
	u8 rate_plcp;
318
	u32 rate_flags = 0;
319
	bool is_cck;
320

321 322 323 324 325 326 327 328 329
	if (unlikely(info->control.flags & IEEE80211_TX_CTRL_RATE_INJECT)) {
		u32 result = iwl_mvm_get_inject_tx_rate(mvm, info);

		if (result)
			return result;
		rate_idx = info->control.rates[0].idx;
	} else if (!ieee80211_hw_check(mvm->hw, HAS_RATE_CONTROL)) {
		/* info->control is only relevant for non HW rate control */

330 331 332 333 334 335
		/* HT rate doesn't make sense for a non data frame */
		WARN_ONCE(info->control.rates[0].flags & IEEE80211_TX_RC_MCS &&
			  !ieee80211_is_data(fc),
			  "Got a HT rate (flags:0x%x/mcs:%d/fc:0x%x/state:%d) for a non data frame\n",
			  info->control.rates[0].flags,
			  info->control.rates[0].idx,
336 337
			  le16_to_cpu(fc),
			  sta ? iwl_mvm_sta_from_mac80211(sta)->sta_state : -1);
338 339

		rate_idx = info->control.rates[0].idx;
340 341 342 343 344 345 346 347 348 349

		/* For non 2 GHZ band, remap mac80211 rate indices into driver
		 * indices.
		 */
		if (info->band != NL80211_BAND_2GHZ ||
		    (info->flags & IEEE80211_TX_CTL_NO_CCK_RATE))
			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);
350
	}
351 352 353

	/* if the rate isn't a well known legacy rate, take the lowest one */
	if (rate_idx < 0 || rate_idx >= IWL_RATE_COUNT_LEGACY)
354 355 356
		rate_idx = iwl_mvm_mac_ctxt_get_lowest_rate(mvm,
							    info,
							    info->control.vif);
357 358

	/* Get PLCP rate for tx_cmd->rate_n_flags */
359 360
	rate_plcp = iwl_mvm_mac80211_idx_to_hwrate(mvm->fw, rate_idx);
	is_cck = (rate_idx >= IWL_FIRST_CCK_RATE) && (rate_idx <= IWL_LAST_CCK_RATE);
361

362
	/* Set CCK or OFDM flag */
363
	if (iwl_fw_lookup_cmd_ver(mvm->fw, TX_CMD, 0) > 8) {
364 365 366 367 368
		if (!is_cck)
			rate_flags |= RATE_MCS_LEGACY_OFDM_MSK;
		else
			rate_flags |= RATE_MCS_CCK_MSK;
	} else if (is_cck) {
369
		rate_flags |= RATE_MCS_CCK_MSK_V1;
370
	}
371 372 373 374

	return (u32)rate_plcp | rate_flags;
}

375 376 377 378
static u32 iwl_mvm_get_tx_rate_n_flags(struct iwl_mvm *mvm,
				       struct ieee80211_tx_info *info,
				       struct ieee80211_sta *sta, __le16 fc)
{
379
	return iwl_mvm_get_tx_rate(mvm, info, sta, fc) |
380 381 382
		iwl_mvm_get_tx_ant(mvm, info, sta, fc);
}

Johannes Berg's avatar
Johannes Berg committed
383 384 385
/*
 * Sets the fields in the Tx cmd that are rate related
 */
386 387 388
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)
Johannes Berg's avatar
Johannes Berg committed
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
{
	/* 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;
	}

	/*
405
	 * for data packets, rate info comes from the table inside the fw. This
406
	 * table is controlled by LINK_QUALITY commands
Johannes Berg's avatar
Johannes Berg committed
407 408
	 */

409 410
	if (likely(ieee80211_is_data(fc) && sta &&
		   !(info->control.flags & IEEE80211_TX_CTRL_RATE_INJECT))) {
411 412 413 414 415 416 417
		struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);

		if (mvmsta->sta_state >= IEEE80211_STA_AUTHORIZED) {
			tx_cmd->initial_rate_index = 0;
			tx_cmd->tx_flags |= cpu_to_le32(TX_CMD_FLG_STA_RATE);
			return;
		}
Johannes Berg's avatar
Johannes Berg committed
418
	} else if (ieee80211_is_back_req(fc)) {
419 420
		tx_cmd->tx_flags |=
			cpu_to_le32(TX_CMD_FLG_ACK | TX_CMD_FLG_BAR);
Johannes Berg's avatar
Johannes Berg committed
421 422 423
	}

	/* Set the rate in the TX cmd */
424 425
	tx_cmd->rate_n_flags =
		cpu_to_le32(iwl_mvm_get_tx_rate_n_flags(mvm, info, sta, fc));
Johannes Berg's avatar
Johannes Berg committed
426 427
}

428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
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;
}

Johannes Berg's avatar
Johannes Berg committed
445 446 447
/*
 * Sets the fields in the Tx cmd that are crypto related
 */
448 449 450 451 452
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)
Johannes Berg's avatar
Johannes Berg committed
453 454
{
	struct ieee80211_key_conf *keyconf = info->control.hw_key;
455
	u8 *crypto_hdr = skb_frag->data + hdrlen;
456
	enum iwl_tx_cmd_sec_ctrl type = TX_CMD_SEC_CCM;
457
	u64 pn;
Johannes Berg's avatar
Johannes Berg committed
458 459 460

	switch (keyconf->cipher) {
	case WLAN_CIPHER_SUITE_CCMP:
461
		iwl_mvm_set_tx_cmd_ccmp(info, tx_cmd);
462
		iwl_mvm_set_tx_cmd_pn(info, crypto_hdr);
Johannes Berg's avatar
Johannes Berg committed
463 464 465 466
		break;

	case WLAN_CIPHER_SUITE_TKIP:
		tx_cmd->sec_ctl = TX_CMD_SEC_TKIP;
467 468
		pn = atomic64_inc_return(&keyconf->tx_pn);
		ieee80211_tkip_add_iv(crypto_hdr, keyconf, pn);
Johannes Berg's avatar
Johannes Berg committed
469 470 471 472 473
		ieee80211_get_tkip_p2k(keyconf, skb_frag, tx_cmd->key);
		break;

	case WLAN_CIPHER_SUITE_WEP104:
		tx_cmd->sec_ctl |= TX_CMD_SEC_KEY128;
474
		fallthrough;
Johannes Berg's avatar
Johannes Berg committed
475 476 477 478 479 480 481
	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;
482 483
	case WLAN_CIPHER_SUITE_GCMP:
	case WLAN_CIPHER_SUITE_GCMP_256:
484
		type = TX_CMD_SEC_GCMP;
485
		fallthrough;
486
	case WLAN_CIPHER_SUITE_CCMP_256:
487 488 489 490 491 492
		/* 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.
		 */
493
		tx_cmd->sec_ctl |= type | TX_CMD_SEC_KEY_FROM_TABLE;
494 495 496
		tx_cmd->key[0] = keyconf->hw_key_idx;
		iwl_mvm_set_tx_cmd_pn(info, crypto_hdr);
		break;
Johannes Berg's avatar
Johannes Berg committed
497
	default:
498
		tx_cmd->sec_ctl |= TX_CMD_SEC_EXT;
Johannes Berg's avatar
Johannes Berg committed
499 500 501 502 503 504
	}
}

/*
 * Allocates and sets the Tx cmd the driver data pointers in the skb
 */
505
static struct iwl_device_tx_cmd *
Johannes Berg's avatar
Johannes Berg committed
506
iwl_mvm_set_tx_params(struct iwl_mvm *mvm, struct sk_buff *skb,
507 508
		      struct ieee80211_tx_info *info, int hdrlen,
		      struct ieee80211_sta *sta, u8 sta_id)
Johannes Berg's avatar
Johannes Berg committed
509 510
{
	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
511
	struct iwl_device_tx_cmd *dev_cmd;
Johannes Berg's avatar
Johannes Berg committed
512 513 514 515 516 517 518
	struct iwl_tx_cmd *tx_cmd;

	dev_cmd = iwl_trans_alloc_tx_cmd(mvm->trans);

	if (unlikely(!dev_cmd))
		return NULL;

519
	dev_cmd->hdr.cmd = TX_CMD;
520 521

	if (iwl_mvm_has_new_tx_api(mvm)) {
522 523
		u32 rate_n_flags = 0;
		u16 flags = 0;
524 525
		struct iwl_mvm_sta *mvmsta = sta ?
			iwl_mvm_sta_from_mac80211(sta) : NULL;
526
		bool amsdu = false;
527

528 529 530
		if (ieee80211_is_data_qos(hdr->frame_control)) {
			u8 *qc = ieee80211_get_qos_ctl(hdr);

531
			amsdu = *qc & IEEE80211_QOS_CTL_A_MSDU_PRESENT;
532 533
		}

534 535
		if (!info->control.hw_key)
			flags |= IWL_TX_FLAGS_ENCRYPT_DIS;
536

537 538
		/*
		 * For data packets rate info comes from the fw. Only
539 540
		 * set rate/antenna during connection establishment or in case
		 * no station is given.
541
		 */
542 543
		if (!sta || !ieee80211_is_data(hdr->frame_control) ||
		    mvmsta->sta_state < IEEE80211_STA_AUTHORIZED) {
544
			flags |= IWL_TX_FLAGS_CMD_RATE;
545 546 547
			rate_n_flags =
				iwl_mvm_get_tx_rate_n_flags(mvm, info, sta,
							    hdr->frame_control);
548
		}
549

550
		if (mvm->trans->trans_cfg->device_family >=
551
		    IWL_DEVICE_FAMILY_AX210) {
552
			struct iwl_tx_cmd_gen3 *cmd = (void *)dev_cmd->payload;
553 554
			u32 offload_assist = iwl_mvm_tx_csum(mvm, skb,
							     info, amsdu);
555

556
			cmd->offload_assist = cpu_to_le32(offload_assist);
557

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

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

564 565 566 567
			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;
568 569
			u16 offload_assist = iwl_mvm_tx_csum(mvm, skb,
							     info, amsdu);
570

571
			cmd->offload_assist = cpu_to_le16(offload_assist);
572 573 574 575 576 577 578 579 580 581

			/* 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);
		}
582 583 584
		goto out;
	}

Johannes Berg's avatar
Johannes Berg committed
585 586 587
	tx_cmd = (struct iwl_tx_cmd *)dev_cmd->payload;

	if (info->control.hw_key)
588
		iwl_mvm_set_tx_cmd_crypto(mvm, info, tx_cmd, skb, hdrlen);
Johannes Berg's avatar
Johannes Berg committed
589 590 591 592 593

	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);

594 595 596 597
	/* Copy MAC header from skb into command buffer */
	memcpy(tx_cmd->hdr, hdr, hdrlen);

out:
598 599 600 601
	return dev_cmd;
}

static void iwl_mvm_skb_prepare_status(struct sk_buff *skb,
602
				       struct iwl_device_tx_cmd *cmd)
603 604 605
{
	struct ieee80211_tx_info *skb_info = IEEE80211_SKB_CB(skb);

606 607
	memset(&skb_info->status, 0, sizeof(skb_info->status));
	memset(skb_info->driver_data, 0, sizeof(skb_info->driver_data));
Johannes Berg's avatar
Johannes Berg committed
608

609
	skb_info->driver_data[1] = cmd;
Johannes Berg's avatar
Johannes Berg committed
610 611
}

612
static int iwl_mvm_get_ctrl_vif_queue(struct iwl_mvm *mvm,
613
				      struct iwl_mvm_vif_link_info *link,
614
				      struct ieee80211_tx_info *info,
615
				      struct sk_buff *skb)
616
{
617
	struct ieee80211_hdr *hdr = (void *)skb->data;
618
	__le16 fc = hdr->frame_control;
619

620 621
	switch (info->control.vif->type) {
	case NL80211_IFTYPE_AP:
622
	case NL80211_IFTYPE_ADHOC:
623
		/*
624 625
		 * Non-bufferable frames use the broadcast station, thus they
		 * use the probe queue.
626 627 628 629
		 * 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.
630 631
		 * Also, disassociate frames might happen, particular with
		 * reason 7 ("Class 3 frame received from nonassociated STA").
632
		 */
633
		if (ieee80211_is_mgmt(fc) &&
634
		    (!ieee80211_is_bufferable_mmpdu(skb) ||
635
		     ieee80211_is_deauth(fc) || ieee80211_is_disassoc(fc)))
636
			return link->mgmt_queue;
637 638 639

		if (!ieee80211_has_order(fc) && !ieee80211_is_probe_req(fc) &&
		    is_multicast_ether_addr(hdr->addr1))
640
			return link->cab_queue;
641

642 643
		WARN_ONCE(info->control.vif->type != NL80211_IFTYPE_ADHOC,
			  "fc=0x%02x", le16_to_cpu(fc));
644
		return link->mgmt_queue;
645 646
	case NL80211_IFTYPE_P2P_DEVICE:
		if (ieee80211_is_mgmt(fc))
647
			return mvm->p2p_dev_queue;
648

649
		WARN_ON_ONCE(1);
650
		return mvm->p2p_dev_queue;
651 652 653 654
	default:
		WARN_ONCE(1, "Not a ctrl vif, no available queue\n");
		return -1;
	}
655 656
}

657 658 659 660 661 662 663 664 665
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;
666 667
	const u8 *ie;
	u8 *pos;
668 669 670 671 672 673 674 675 676
	u8 match[] = {
		(WLAN_OUI_WFA >> 16) & 0xff,
		(WLAN_OUI_WFA >> 8) & 0xff,
		WLAN_OUI_WFA & 0xff,
		WLAN_OUI_TYPE_WFA_P2P,
	};

	rcu_read_lock();

677
	resp_data = rcu_dereference(mvmvif->deflink.probe_resp_data);
678 679 680 681 682 683
	if (!resp_data)
		goto out;

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

684 685 686 687
	ie = cfg80211_find_ie_match(WLAN_EID_VENDOR_SPECIFIC,
				    mgmt->u.probe_resp.variable,
				    skb->len - base_len,
				    match, 4, 2);
688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717
	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();
}

Johannes Berg's avatar
Johannes Berg committed
718 719 720
int iwl_mvm_tx_skb_non_sta(struct iwl_mvm *mvm, struct sk_buff *skb)
{
	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
721
	struct ieee80211_tx_info info;
722
	struct iwl_device_tx_cmd *dev_cmd;
Johannes Berg's avatar
Johannes Berg committed
723
	u8 sta_id;
724
	int hdrlen = ieee80211_hdrlen(hdr->frame_control);
725
	__le16 fc = hdr->frame_control;
726 727
	bool offchannel = IEEE80211_SKB_CB(skb)->flags &
		IEEE80211_TX_CTL_TX_OFFCHAN;
728
	int queue = -1;
729

730 731 732
	if (IWL_MVM_NON_TRANSMITTING_AP && ieee80211_is_probe_resp(fc))
		return -1;

733 734
	memcpy(&info, skb->cb, sizeof(info));

735 736 737
	if (WARN_ON_ONCE(skb->len > IEEE80211_MAX_DATA_LEN + hdrlen))
		return -1;

738
	if (WARN_ON_ONCE(info.flags & IEEE80211_TX_CTL_AMPDU))
Johannes Berg's avatar
Johannes Berg committed
739 740
		return -1;

741
	if (info.control.vif) {
Johannes Berg's avatar
Johannes Berg committed
742
		struct iwl_mvm_vif *mvmvif =
743
			iwl_mvm_vif_from_mac80211(info.control.vif);
744

745
		if (info.control.vif->type == NL80211_IFTYPE_P2P_DEVICE ||
746 747
		    info.control.vif->type == NL80211_IFTYPE_AP ||
		    info.control.vif->type == NL80211_IFTYPE_ADHOC) {
748 749 750 751 752 753 754 755 756 757 758 759
			u32 link_id = u32_get_bits(info.control.flags,
						   IEEE80211_TX_CTRL_MLO_LINK);
			struct iwl_mvm_vif_link_info *link;

			if (link_id == IEEE80211_LINK_UNSPECIFIED) {
				if (info.control.vif->active_links)
					link_id = ffs(info.control.vif->active_links) - 1;
				else
					link_id = 0;
			}

			link = mvmvif->link[link_id];
760 761
			if (WARN_ON(!link))
				return -1;
762

763
			if (!ieee80211_is_data(hdr->frame_control))
764
				sta_id = link->bcast_sta.sta_id;
765
			else
766
				sta_id = link->mcast_sta.sta_id;
767

768
			queue = iwl_mvm_get_ctrl_vif_queue(mvm, link, &info,
769
							   skb);
770
		} else if (info.control.vif->type == NL80211_IFTYPE_MONITOR) {
771 772
			queue = mvm->snif_queue;
			sta_id = mvm->snif_sta.sta_id;
773
		} else if (info.control.vif->type == NL80211_IFTYPE_STATION &&
774
			   offchannel) {
775 776 777 778 779 780 781 782 783 784
			/*
			 * 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.
			 */
			sta_id = mvm->aux_sta.sta_id;
			queue = mvm->aux_queue;
785
		}
Johannes Berg's avatar
Johannes Berg committed
786 787
	}

788 789
	if (queue < 0) {
		IWL_ERR(mvm, "No queue was found. Dropping TX\n");
790
		return -1;
791
	}
792

793 794 795
	if (unlikely(ieee80211_is_probe_resp(fc)))
		iwl_mvm_probe_resp_set_noa(mvm, skb);

796
	IWL_DEBUG_TX(mvm, "station Id %d, queue=%d\n", sta_id, queue);
Johannes Berg's avatar
Johannes Berg committed
797

798
	dev_cmd = iwl_mvm_set_tx_params(mvm, skb, &info, hdrlen, NULL, sta_id);
Johannes Berg's avatar
Johannes Berg committed
799 800 801
	if (!dev_cmd)
		return -1;

802 803 804
	/* From now on, we cannot access info->control */
	iwl_mvm_skb_prepare_status(skb, dev_cmd);

805
	if (iwl_trans_tx(mvm->trans, skb, dev_cmd, queue)) {
Johannes Berg's avatar
Johannes Berg committed
806 807 808 809 810 811 812
		iwl_trans_free_tx_cmd(mvm->trans, dev_cmd);
		return -1;
	}

	return 0;
}

813 814
unsigned int iwl_mvm_max_amsdu_size(struct iwl_mvm *mvm,
				    struct ieee80211_sta *sta, unsigned int tid)
815 816 817
{
	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
	u8 ac = tid_to_mac80211_ac[tid];
818
	enum nl80211_band band;
819
	unsigned int txf;
820 821
	unsigned int val;
	int lmac;
822 823

	/* For HE redirect to trigger based fifos */
824
	if (sta->deflink.he_cap.has_he && !WARN_ON(!iwl_mvm_has_new_tx_api(mvm)))
825 826 827 828 829 830 831 832 833 834
		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.
	 */
835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852
	val = mvmsta->max_amsdu_len;

	if (hweight16(sta->valid_links) <= 1) {
		if (sta->valid_links) {
			struct ieee80211_bss_conf *link_conf;
			unsigned int link = ffs(sta->valid_links) - 1;

			rcu_read_lock();
			link_conf = rcu_dereference(mvmsta->vif->link_conf[link]);
			if (WARN_ON(!link_conf))
				band = NL80211_BAND_2GHZ;
			else
				band = link_conf->chandef.chan->band;
			rcu_read_unlock();
		} else {
			band = mvmsta->vif->bss_conf.chandef.chan->band;
		}

853
		lmac = iwl_mvm_get_lmac_id(mvm, band);
854 855 856 857 858 859 860 861 862 863 864 865
	} else if (fw_has_capa(&mvm->fw->ucode_capa,
			       IWL_UCODE_TLV_CAPA_CDB_SUPPORT)) {
		/* for real MLO restrict to both LMACs if they exist */
		lmac = IWL_LMAC_5G_INDEX;
		val = min_t(unsigned int, val,
			    mvm->fwrt.smem_cfg.lmac[lmac].txfifo_size[txf] - 256);
		lmac = IWL_LMAC_24G_INDEX;
	} else {
		lmac = IWL_LMAC_24G_INDEX;
	}

	return min_t(unsigned int, val,
866 867 868
		     mvm->fwrt.smem_cfg.lmac[lmac].txfifo_size[txf] - 256);
}

869 870 871 872 873 874 875 876 877 878 879 880 881 882
#ifdef CONFIG_INET

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));
883
	bool qos = ieee80211_is_data_qos(hdr->frame_control);
884 885 886 887 888 889 890
	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;
891
	skb_shinfo(skb)->gso_type = ipv4 ? SKB_GSO_TCPV4 : SKB_GSO_TCPV6;
892 893 894 895 896
	if (WARN_ON_ONCE(IS_ERR(next)))
		return -EINVAL;
	else if (next)
		consume_skb(skb);

897
	skb_list_walk_safe(next, tmp, next) {
898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913
		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;
914 915
			skb_shinfo(tmp)->gso_type = ipv4 ? SKB_GSO_TCPV4 :
							   SKB_GSO_TCPV6;
916
		} else {
917
			if (qos) {
918 919 920 921 922 923 924 925 926 927 928
				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;
		}

929
		skb_mark_not_on_list(tmp);
930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954
		__skb_queue_tail(mpdus_skb, tmp);
		i++;
	}

	return 0;
}

static int iwl_mvm_tx_tso(struct iwl_mvm *mvm, struct sk_buff *skb,
			  struct ieee80211_tx_info *info,
			  struct ieee80211_sta *sta,
			  struct sk_buff_head *mpdus_skb)
{
	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
	struct ieee80211_hdr *hdr = (void *)skb->data;
	unsigned int mss = skb_shinfo(skb)->gso_size;
	unsigned int num_subframes, tcp_payload_len, subf_len, max_amsdu_len;
	u16 snap_ip_tcp, pad;
	netdev_features_t netdev_flags = NETIF_F_CSUM_MASK | NETIF_F_SG;
	u8 tid;

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

	if (!mvmsta->max_amsdu_len ||
	    !ieee80211_is_data_qos(hdr->frame_control) ||
955
	    !mvmsta->amsdu_enabled)
956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976
		return iwl_mvm_tx_tso_segment(skb, 1, netdev_flags, mpdus_skb);

	/*
	 * 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) {
		netdev_flags &= ~NETIF_F_CSUM_MASK;
		return iwl_mvm_tx_tso_segment(skb, 1, netdev_flags, mpdus_skb);
	}

	tid = ieee80211_get_tid(hdr);
	if (WARN_ON_ONCE(tid >= IWL_MAX_TID_COUNT))
		return -EINVAL;

	/*
	 * No need to lock amsdu_in_ampdu_allowed since it can't be modified
	 * during an BA session.
	 */
977 978
	if ((info->flags & IEEE80211_TX_CTL_AMPDU &&
	     !mvmsta->tid_data[tid].amsdu_in_ampdu_allowed) ||
979 980 981
	    !(mvmsta->amsdu_enabled & BIT(tid)))
		return iwl_mvm_tx_tso_segment(skb, 1, netdev_flags, mpdus_skb);

982 983 984 985
	/*
	 * Take the min of ieee80211 station and mvm station
	 */
	max_amsdu_len =
986
		min_t(unsigned int, sta->cur->max_amsdu_len,
987
		      iwl_mvm_max_amsdu_size(mvm, sta, tid));
988 989 990 991 992 993 994

	/*
	 * 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 &&
995
	    !sta->deflink.vht_cap.vht_supported)
996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053
		max_amsdu_len = min_t(unsigned int, max_amsdu_len, 4095);

	/* 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.
	 */
	num_subframes = (max_amsdu_len + pad) / (subf_len + pad);

	if (sta->max_amsdu_subframes &&
	    num_subframes > sta->max_amsdu_subframes)
		num_subframes = sta->max_amsdu_subframes;

	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
	 */
	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;

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

	/*
	 * Trick the segmentation function to make it
	 * create SKBs that can fit into one A-MSDU.
	 */
	return iwl_mvm_tx_tso_segment(skb, num_subframes, netdev_flags,
				      mpdus_skb);
}
#else /* CONFIG_INET */
static int iwl_mvm_tx_tso(struct iwl_mvm *mvm, struct sk_buff *skb,
			  struct ieee80211_tx_info *info,
			  struct ieee80211_sta *sta,
			  struct sk_buff_head *mpdus_skb)
{
	/* Impossible to get TSO with CONFIG_INET */
	WARN_ON(1);

	return -1;
}
#endif

1054 1055 1056 1057 1058 1059 1060
/* 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;

1061 1062 1063
	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
		return false;

1064 1065 1066 1067 1068 1069 1070 1071 1072
	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;
}

1073 1074 1075 1076 1077
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;
1078 1079 1080 1081 1082 1083
	struct iwl_mvm_tcm_mac *mdata;

	if (mac >= NUM_MAC_INDEX_DRIVER)
		return;

	mdata = &mvm->tcm.data[mac];
1084 1085 1086 1087 1088 1089 1090 1091 1092 1093

	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;
}

1094 1095
static int iwl_mvm_tx_pkt_queued(struct iwl_mvm *mvm,
				 struct iwl_mvm_sta *mvmsta, int tid)
1096 1097 1098
{
	u32 ac = tid_to_mac80211_ac[tid];
	int mac = mvmsta->mac_id_n_color & FW_CTXT_ID_MSK;
1099 1100 1101 1102 1103 1104
	struct iwl_mvm_tcm_mac *mdata;

	if (mac >= NUM_MAC_INDEX_DRIVER)
		return -EINVAL;

	mdata = &mvm->tcm.data[mac];
1105 1106

	mdata->tx.pkts[ac]++;
1107 1108

	return 0;
1109 1110
}

Johannes Berg's avatar
Johannes Berg committed
1111
/*
1112 1113 1114
 * Sets the fields in the Tx cmd that are crypto related.
 *
 * This function must be called with BHs disabled.
Johannes Berg's avatar
Johannes Berg committed
1115
 */
1116
static int iwl_mvm_tx_mpdu(struct iwl_mvm *mvm, struct sk_buff *skb,
1117
			   struct ieee80211_tx_info *info,
1118
			   struct ieee80211_sta *sta)
Johannes Berg's avatar
Johannes Berg committed
1119 1120 1121
{
	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
	struct iwl_mvm_sta *mvmsta;
1122
	struct iwl_device_tx_cmd *dev_cmd;
Johannes Berg's avatar
Johannes Berg committed
1123 1124 1125
	__le16 fc;
	u16 seq_number = 0;
	u8 tid = IWL_MAX_TID_COUNT;
1126
	u16 txq_id;
1127
	bool is_ampdu = false;
1128
	int hdrlen;
Johannes Berg's avatar
Johannes Berg committed
1129

1130
	mvmsta = iwl_mvm_sta_from_mac80211(sta);
Johannes Berg's avatar
Johannes Berg committed
1131
	fc = hdr->frame_control;
1132
	hdrlen = ieee80211_hdrlen(fc);
Johannes Berg's avatar
Johannes Berg committed
1133

1134 1135 1136
	if (IWL_MVM_NON_TRANSMITTING_AP && ieee80211_is_probe_resp(fc))
		return -1;

Johannes Berg's avatar
Johannes Berg committed
1137 1138 1139
	if (WARN_ON_ONCE(!mvmsta))
		return -1;

1140
	if (WARN_ON_ONCE(mvmsta->deflink.sta_id == IWL_MVM_INVALID_STA))
Johannes Berg's avatar
Johannes Berg committed
1141 1142
		return -1;

1143
	if (unlikely(ieee80211_is_any_nullfunc(fc)) && sta->deflink.he_cap.has_he)
1144 1145
		return -1;

1146 1147 1148
	if (unlikely(ieee80211_is_probe_resp(fc)))
		iwl_mvm_probe_resp_set_noa(mvm, skb);

1149
	dev_cmd = iwl_mvm_set_tx_params(mvm, skb, info, hdrlen,
1150
					sta, mvmsta->deflink.sta_id);
Johannes Berg's avatar
Johannes Berg committed
1151 1152 1153
	if (!dev_cmd)
		goto drop;

1154 1155 1156 1157 1158 1159 1160
	/*
	 * 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;

Johannes Berg's avatar
Johannes Berg committed
1161 1162
	spin_lock(&mvmsta->lock);

1163
	/* nullfunc frames should go to the MGMT queue regardless of QOS,
1164 1165
	 * the conditions of !ieee80211_is_qos_nullfunc(fc) and
	 * !ieee80211_is_data_qos(fc) keep the default assignment of MGMT TID
1166
	 */
Johannes Berg's avatar
Johannes Berg committed
1167
	if (ieee80211_is_data_qos(fc) && !ieee80211_is_qos_nullfunc(fc)) {
1168
		tid = ieee80211_get_tid(hdr);
1169
		if (WARN_ONCE(tid >= IWL_MAX_TID_COUNT, "Invalid TID %d", tid))
Johannes Berg's avatar
Johannes Berg committed
1170 1171 1172
			goto drop_unlock_sta;

		is_ampdu = info->flags & IEEE80211_TX_CTL_AMPDU;
1173 1174 1175 1176
		if (WARN_ONCE(is_ampdu &&
			      mvmsta->tid_data[tid].state != IWL_AGG_ON,
			      "Invalid internal agg state %d for TID %d",
			       mvmsta->tid_data[tid].state, tid))
1177
			goto drop_unlock_sta;
1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189

		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;
		}
1190 1191
	} else if (ieee80211_is_data(fc) && !ieee80211_is_data_qos(fc) &&
		   !ieee80211_is_nullfunc(fc)) {
1192
		tid = IWL_TID_NON_QOS;
Johannes Berg's avatar
Johannes Berg committed
1193 1194
	}

1195
	txq_id = mvmsta->tid_data[tid].txq_id;
1196

1197
	WARN_ON_ONCE(info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM);
Johannes Berg's avatar
Johannes Berg committed
1198

1199
	if (WARN_ONCE(txq_id == IWL_MVM_INVALID_QUEUE, "Invalid TXQ id")) {
1200 1201
		iwl_trans_free_tx_cmd(mvm->trans, dev_cmd);
		spin_unlock(&mvmsta->lock);
1202
		return -1;
1203 1204
	}

1205
	if (!iwl_mvm_has_new_tx_api(mvm)) {
1206 1207 1208 1209 1210 1211 1212
		/* 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
		 *
1213 1214 1215 1216 1217
		 * 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.
1218 1219 1220 1221 1222 1223
		 */
		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);
	}
1224

1225
	IWL_DEBUG_TX(mvm, "TX to [%d|%d] Q:%d - seq: 0x%x len %d\n",
1226
		     mvmsta->deflink.sta_id, tid, txq_id,
1227
		     IEEE80211_SEQ_TO_SN(seq_number), skb->len);
Johannes Berg's avatar
Johannes Berg committed
1228

1229 1230 1231
	/* From now on, we cannot access info->control */
	iwl_mvm_skb_prepare_status(skb, dev_cmd);

1232 1233 1234 1235 1236
	/*
	 * The IV is introduced by the HW for new tx api, and it is not present
	 * in the skb, hence, don't tell iwl_mvm_mei_tx_copy_to_csme about the
	 * IV for those devices.
	 */
1237 1238
	if (ieee80211_is_data(fc))
		iwl_mvm_mei_tx_copy_to_csme(mvm, skb,
1239 1240
					    info->control.hw_key &&
					    !iwl_mvm_has_new_tx_api(mvm) ?
1241 1242
					    info->control.hw_key->iv_len : 0);

Johannes Berg's avatar
Johannes Berg committed
1243 1244 1245
	if (iwl_trans_tx(mvm->trans, skb, dev_cmd, txq_id))
		goto drop_unlock_sta;

1246
	if (tid < IWL_MAX_TID_COUNT && !ieee80211_has_morefrags(fc))
1247
		mvmsta->tid_data[tid].seq_number = seq_number + 0x10;
Johannes Berg's avatar
Johannes Berg committed
1248 1249 1250

	spin_unlock(&mvmsta->lock);

1251 1252 1253
	if (iwl_mvm_tx_pkt_queued(mvm, mvmsta,
				  tid == IWL_MAX_TID_COUNT ? 0 : tid))
		goto drop;
1254

Johannes Berg's avatar
Johannes Berg committed
1255 1256 1257 1258 1259 1260
	return 0;

drop_unlock_sta:
	iwl_trans_free_tx_cmd(mvm->trans, dev_cmd);
	spin_unlock(&mvmsta->lock);
drop:
1261 1262
	IWL_DEBUG_TX(mvm, "TX to [%d|%d] dropped\n", mvmsta->deflink.sta_id,
		     tid);
Johannes Berg's avatar
Johannes Berg committed
1263 1264 1265
	return -1;
}

1266 1267
int iwl_mvm_tx_skb_sta(struct iwl_mvm *mvm, struct sk_buff *skb,
		       struct ieee80211_sta *sta)
1268 1269
{
	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1270
	struct ieee80211_tx_info info;
1271 1272 1273
	struct sk_buff_head mpdus_skbs;
	unsigned int payload_len;
	int ret;
1274
	struct sk_buff *orig_skb = skb;
1275 1276 1277 1278

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

1279
	if (WARN_ON_ONCE(mvmsta->deflink.sta_id == IWL_MVM_INVALID_STA))
1280 1281
		return -1;

1282 1283
	memcpy(&info, skb->cb, sizeof(info));

1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298
	if (!skb_is_gso(skb))
		return iwl_mvm_tx_mpdu(mvm, skb, &info, sta);

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

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

	__skb_queue_head_init(&mpdus_skbs);

	ret = iwl_mvm_tx_tso(mvm, skb, &info, sta, &mpdus_skbs);
	if (ret)
		return ret;

1299
	WARN_ON(skb_queue_empty(&mpdus_skbs));
1300 1301 1302 1303 1304 1305

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

		ret = iwl_mvm_tx_mpdu(mvm, skb, &info, sta);
		if (ret) {
1306
			/* Free skbs created as part of TSO logic that have not yet been dequeued */
1307
			__skb_queue_purge(&mpdus_skbs);
1308 1309 1310 1311 1312 1313 1314 1315 1316
			/* skb here is not necessarily same as skb that entered this method,
			 * so free it explicitly.
			 */
			if (skb == orig_skb)
				ieee80211_free_txskb(mvm->hw, skb);
			else
				kfree_skb(skb);
			/* there was error, but we consumed skb one way or another, so return 0 */
			return 0;
1317 1318 1319 1320
		}
	}

	return 0;
1321 1322
}

Johannes Berg's avatar
Johannes Berg committed
1323 1324 1325
static void iwl_mvm_check_ratid_empty(struct iwl_mvm *mvm,
				      struct ieee80211_sta *sta, u8 tid)
{
1326
	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Johannes Berg's avatar
Johannes Berg committed
1327 1328
	struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
	struct ieee80211_vif *vif = mvmsta->vif;
1329
	u16 normalized_ssn;
Johannes Berg's avatar
Johannes Berg committed
1330 1331 1332

	lockdep_assert_held(&mvmsta->lock);

1333
	if ((tid_data->state == IWL_AGG_ON ||
1334
	     tid_data->state == IWL_EMPTYING_HW_QUEUE_DELBA) &&
1335
	    iwl_mvm_tid_queued(mvm, tid_data) == 0) {
1336
		/*
1337 1338 1339
		 * 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.)
1340 1341 1342 1343
		 */
		ieee80211_sta_set_buffered(sta, tid, false);
	}

1344
	/*
1345
	 * In 22000 HW, the next_reclaimed index is only 8 bit, so we'll need
1346 1347 1348
	 * to align the wrap around of ssn so we compare relevant values.
	 */
	normalized_ssn = tid_data->ssn;
1349
	if (mvm->trans->trans_cfg->gen2)
1350 1351 1352
		normalized_ssn &= 0xff;

	if (normalized_ssn != tid_data->next_reclaimed)
Johannes Berg's avatar
Johannes Berg committed
1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416
		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 */

1417
static int iwl_mvm_get_hwrate_chan_width(u32 chan_width)
Johannes Berg's avatar
Johannes Berg committed
1418
{
1419
	switch (chan_width) {
Johannes Berg's avatar
Johannes Berg committed
1420
	case RATE_MCS_CHAN_WIDTH_20:
1421
		return 0;
Johannes Berg's avatar
Johannes Berg committed
1422
	case RATE_MCS_CHAN_WIDTH_40:
1423
		return IEEE80211_TX_RC_40_MHZ_WIDTH;
Johannes Berg's avatar
Johannes Berg committed
1424
	case RATE_MCS_CHAN_WIDTH_80:
1425
		return IEEE80211_TX_RC_80_MHZ_WIDTH;
Johannes Berg's avatar
Johannes Berg committed
1426
	case RATE_MCS_CHAN_WIDTH_160:
1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452
		return IEEE80211_TX_RC_160_MHZ_WIDTH;
	default:
		return 0;
	}
}

void iwl_mvm_hwrate_to_tx_rate(u32 rate_n_flags,
			       enum nl80211_band band,
			       struct ieee80211_tx_rate *r)
{
	u32 format = rate_n_flags & RATE_MCS_MOD_TYPE_MSK;
	u32 rate = format == RATE_MCS_HT_MSK ?
		RATE_HT_MCS_INDEX(rate_n_flags) :
		rate_n_flags & RATE_MCS_CODE_MSK;

	r->flags |=
		iwl_mvm_get_hwrate_chan_width(rate_n_flags &
					      RATE_MCS_CHAN_WIDTH_MSK);

	if (rate_n_flags & RATE_MCS_SGI_MSK)
		r->flags |= IEEE80211_TX_RC_SHORT_GI;
	if (format ==  RATE_MCS_HT_MSK) {
		r->flags |= IEEE80211_TX_RC_MCS;
		r->idx = rate;
	} else if (format ==  RATE_MCS_VHT_MSK) {
		ieee80211_rate_set_vht(r, rate,
1453 1454
				       FIELD_GET(RATE_MCS_NSS_MSK,
						 rate_n_flags) + 1);
1455 1456 1457 1458 1459 1460 1461 1462
		r->flags |= IEEE80211_TX_RC_VHT_MCS;
	} else if (format == RATE_MCS_HE_MSK) {
		/* mac80211 cannot do this without ieee80211_tx_status_ext()
		 * but it only matters for radiotap */
		r->idx = 0;
	} else {
		r->idx = iwl_mvm_legacy_hw_idx_to_mac80211_idx(rate_n_flags,
							       band);
Johannes Berg's avatar
Johannes Berg committed
1463
	}
1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476
}

void iwl_mvm_hwrate_to_tx_rate_v1(u32 rate_n_flags,
				  enum nl80211_band band,
				  struct ieee80211_tx_rate *r)
{
	if (rate_n_flags & RATE_HT_MCS_GF_MSK)
		r->flags |= IEEE80211_TX_RC_GREEN_FIELD;

	r->flags |=
		iwl_mvm_get_hwrate_chan_width(rate_n_flags &
					      RATE_MCS_CHAN_WIDTH_MSK_V1);

1477
	if (rate_n_flags & RATE_MCS_SGI_MSK_V1)
Johannes Berg's avatar
Johannes Berg committed
1478
		r->flags |= IEEE80211_TX_RC_SHORT_GI;
1479
	if (rate_n_flags & RATE_MCS_HT_MSK_V1) {
Johannes Berg's avatar
Johannes Berg committed
1480
		r->flags |= IEEE80211_TX_RC_MCS;
1481 1482
		r->idx = rate_n_flags & RATE_HT_MCS_INDEX_MSK_V1;
	} else if (rate_n_flags & RATE_MCS_VHT_MSK_V1) {
Johannes Berg's avatar
Johannes Berg committed
1483 1484
		ieee80211_rate_set_vht(
			r, rate_n_flags & RATE_VHT_MCS_RATE_CODE_MSK,
1485
			FIELD_GET(RATE_MCS_NSS_MSK, rate_n_flags) + 1);
Johannes Berg's avatar
Johannes Berg committed
1486 1487 1488
		r->flags |= IEEE80211_TX_RC_VHT_MCS;
	} else {
		r->idx = iwl_mvm_legacy_rate_to_mac80211_idx(rate_n_flags,
1489
							     band);
Johannes Berg's avatar
Johannes Berg committed
1490 1491 1492
	}
}

1493
/*
1494 1495
 * translate ucode response to mac80211 tx status control values
 */
1496 1497
static void iwl_mvm_hwrate_to_tx_status(const struct iwl_fw *fw,
					u32 rate_n_flags,
1498 1499 1500 1501
					struct ieee80211_tx_info *info)
{
	struct ieee80211_tx_rate *r = &info->status.rates[0];

1502
	if (iwl_fw_lookup_notif_ver(fw, LONG_GROUP,
1503
				    TX_CMD, 0) <= 6)
1504 1505
		rate_n_flags = iwl_new_rate_from_v1(rate_n_flags);

1506
	info->status.antenna =
1507
		((rate_n_flags & RATE_MCS_ANT_AB_MSK) >> RATE_MCS_ANT_POS);
1508 1509
	iwl_mvm_hwrate_to_tx_rate(rate_n_flags,
				  info->band, r);
1510 1511
}

1512
static void iwl_mvm_tx_status_check_trigger(struct iwl_mvm *mvm,
1513
					    u32 status, __le16 frame_control)
1514 1515 1516 1517 1518
{
	struct iwl_fw_dbg_trigger_tlv *trig;
	struct iwl_fw_dbg_trigger_tx_status *status_trig;
	int i;

1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530
	if ((status & TX_STATUS_MSK) != TX_STATUS_SUCCESS) {
		enum iwl_fw_ini_time_point tp =
			IWL_FW_INI_TIME_POINT_TX_FAILED;

		if (ieee80211_is_action(frame_control))
			tp = IWL_FW_INI_TIME_POINT_TX_WFD_ACTION_FRAME_FAILED;

		iwl_dbg_tlv_time_point(&mvm->fwrt,
				       tp, NULL);
		return;
	}

1531 1532 1533
	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, NULL,
				     FW_DBG_TRIGGER_TX_STATUS);
	if (!trig)
1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545
		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;

1546 1547 1548
		iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
					"Tx status %d was received",
					status & TX_STATUS_MSK);
1549 1550 1551 1552
		break;
	}
}

1553
/*
1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572
 * 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;
}

Johannes Berg's avatar
Johannes Berg committed
1573 1574 1575 1576 1577 1578
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);
1579
	/* struct iwl_mvm_tx_resp_v3 is almost the same */
Johannes Berg's avatar
Johannes Berg committed
1580 1581 1582
	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);
1583 1584 1585 1586
	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);
Johannes Berg's avatar
Johannes Berg committed
1587 1588
	struct sk_buff_head skbs;
	u8 skb_freed = 0;
1589
	u8 lq_color;
Johannes Berg's avatar
Johannes Berg committed
1590
	u16 next_reclaimed, seq_ctl;
1591
	bool is_ndp = false;
Johannes Berg's avatar
Johannes Berg committed
1592 1593 1594

	__skb_queue_head_init(&skbs);

1595
	if (iwl_mvm_has_new_tx_api(mvm))
1596
		txq_id = le16_to_cpu(tx_resp->tx_queue);
1597

Johannes Berg's avatar
Johannes Berg committed
1598 1599 1600 1601 1602 1603 1604 1605
	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);
1606
		struct ieee80211_hdr *hdr = (void *)skb->data;
1607
		bool flushed = false;
Johannes Berg's avatar
Johannes Berg committed
1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620

		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;
1621 1622 1623 1624
		case TX_STATUS_FAIL_FIFO_FLUSHED:
		case TX_STATUS_FAIL_DRAIN_FLOW:
			flushed = true;
			break;
Johannes Berg's avatar
Johannes Berg committed
1625
		case TX_STATUS_FAIL_DEST_PS:
1626
			/* the FW should have stopped the queue and not
1627 1628
			 * return this status
			 */
1629 1630 1631
			IWL_ERR_LIMIT(mvm,
				      "FW reported TX filtered, status=0x%x, FC=0x%x\n",
				      status, le16_to_cpu(hdr->frame_control));
Johannes Berg's avatar
Johannes Berg committed
1632 1633 1634 1635 1636 1637
			info->flags |= IEEE80211_TX_STAT_TX_FILTERED;
			break;
		default:
			break;
		}

1638 1639 1640 1641
		if ((status & TX_STATUS_MSK) != TX_STATUS_SUCCESS &&
		    ieee80211_is_mgmt(hdr->frame_control))
			iwl_mvm_toggle_tx_ant(mvm, &mvm->mgmt_last_antenna_idx);

1642 1643 1644 1645 1646 1647 1648 1649
		/*
		 * 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;

1650
		iwl_mvm_tx_status_check_trigger(mvm, status, hdr->frame_control);
1651

Johannes Berg's avatar
Johannes Berg committed
1652
		info->status.rates[0].count = tx_resp->failure_frame + 1;
1653 1654 1655

		iwl_mvm_hwrate_to_tx_status(mvm->fw,
					    le32_to_cpu(tx_resp->initial_rate),
1656
					    info);
1657 1658 1659 1660

		/* Don't assign the converted initial_rate, because driver
		 * TLC uses this and doesn't support the new FW rate
		 */
1661 1662
		info->status.status_driver_data[1] =
			(void *)(uintptr_t)le32_to_cpu(tx_resp->initial_rate);
Johannes Berg's avatar
Johannes Berg committed
1663 1664

		/* Single frame failure in an AMPDU queue => send BAR */
1665
		if (info->flags & IEEE80211_TX_CTL_AMPDU &&
1666
		    !(info->flags & IEEE80211_TX_STAT_ACK) &&
1667
		    !(info->flags & IEEE80211_TX_STAT_TX_FILTERED) && !flushed)
Johannes Berg's avatar
Johannes Berg committed
1668
			info->flags |= IEEE80211_TX_STAT_AMPDU_NO_BACK;
1669
		info->flags &= ~IEEE80211_TX_CTL_AMPDU;
Johannes Berg's avatar
Johannes Berg committed
1670

1671 1672 1673 1674
		/* 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)
Johannes Berg's avatar
Johannes Berg committed
1675 1676
			seq_ctl = le16_to_cpu(hdr->seq_ctrl);

1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688
		if (unlikely(!seq_ctl)) {
			/*
			 * 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;
		}

1689 1690 1691 1692 1693 1694
		/*
		 * 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);
1695
		BUILD_BUG_ON(ARRAY_SIZE(info->status.status_driver_data) < 1);
1696
		lq_color = TX_RES_RATE_TABLE_COL_GET(tx_resp->tlc_info);
1697
		info->status.status_driver_data[0] =
1698
			RS_DRV_DATA_PACK(lq_color, tx_resp->reduced_tpc);
1699

1700 1701
		if (likely(!iwl_mvm_time_sync_frame(mvm, skb, hdr->addr1)))
			ieee80211_tx_status(mvm->hw, skb);
Johannes Berg's avatar
Johannes Berg committed
1702 1703
	}

1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718
	/* 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;
Johannes Berg's avatar
Johannes Berg committed
1719 1720

	IWL_DEBUG_TX_REPLY(mvm,
1721 1722 1723 1724 1725 1726
			   "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),
Johannes Berg's avatar
Johannes Berg committed
1727 1728 1729 1730 1731 1732
			   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]);
1733 1734 1735 1736 1737 1738
	/*
	 * 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;
Johannes Berg's avatar
Johannes Berg committed
1739

1740
	if (!IS_ERR(sta)) {
1741
		struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Johannes Berg's avatar
Johannes Berg committed
1742

1743 1744 1745
		iwl_mvm_tx_airtime(mvm, mvmsta,
				   le16_to_cpu(tx_resp->wireless_media_time));

1746 1747 1748 1749
		if ((status & TX_STATUS_MSK) != TX_STATUS_SUCCESS &&
		    mvmsta->sta_state < IEEE80211_STA_AUTHORIZED)
			iwl_mvm_toggle_tx_ant(mvm, &mvmsta->tx_ant);

1750
		if (sta->wme && tid != IWL_MGMT_TID) {
Johannes Berg's avatar
Johannes Berg committed
1751 1752
			struct iwl_mvm_tid_data *tid_data =
				&mvmsta->tid_data[tid];
1753
			bool send_eosp_ndp = false;
Johannes Berg's avatar
Johannes Berg committed
1754

1755
			spin_lock_bh(&mvmsta->lock);
1756

1757 1758 1759 1760 1761 1762 1763 1764 1765 1766
			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");
			}

Johannes Berg's avatar
Johannes Berg committed
1767
			iwl_mvm_check_ratid_empty(mvm, sta, tid);
1768 1769 1770 1771

			if (mvmsta->sleep_tx_count) {
				mvmsta->sleep_tx_count--;
				if (mvmsta->sleep_tx_count &&
1772
				    !iwl_mvm_tid_queued(mvm, tid_data)) {
1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789
					/*
					 * 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;
				}
			}

1790
			spin_unlock_bh(&mvmsta->lock);
1791 1792 1793 1794 1795 1796 1797
			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);
			}
Johannes Berg's avatar
Johannes Berg committed
1798
		}
1799 1800 1801 1802 1803

		if (mvmsta->next_status_eosp) {
			mvmsta->next_status_eosp = false;
			ieee80211_sta_eosp(sta);
		}
Johannes Berg's avatar
Johannes Berg committed
1804
	}
1805
out:
Johannes Berg's avatar
Johannes Berg committed
1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818
	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);
1819
	AGG_TX_STATE_(TX_ON_AIR_DROP);
Johannes Berg's avatar
Johannes Berg committed
1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835
	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;
1836 1837
	struct agg_tx_status *frame_status =
		iwl_mvm_get_agg_status(mvm, tx_resp);
Johannes Berg's avatar
Johannes Berg committed
1838
	int i;
1839
	bool tirgger_timepoint = false;
Johannes Berg's avatar
Johannes Berg committed
1840 1841 1842

	for (i = 0; i < tx_resp->frame_count; i++) {
		u16 fstatus = le16_to_cpu(frame_status[i].status);
1843 1844 1845
		/* In case one frame wasn't transmitted trigger time point */
		tirgger_timepoint |= ((fstatus & AGG_TX_STATE_STATUS_MSK) !=
				      AGG_TX_STATE_TRANSMITTED);
Johannes Berg's avatar
Johannes Berg committed
1846 1847 1848 1849 1850 1851 1852 1853
		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));
	}
1854 1855 1856 1857 1858

	if (tirgger_timepoint)
		iwl_dbg_tlv_time_point(&mvm->fwrt,
				       IWL_FW_INI_TIME_POINT_TX_FAILED, NULL);

Johannes Berg's avatar
Johannes Berg committed
1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872
}
#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);
1873
	struct iwl_mvm_sta *mvmsta;
1874
	int queue = SEQ_TO_QUEUE(sequence);
1875
	struct ieee80211_sta *sta;
Johannes Berg's avatar
Johannes Berg committed
1876

1877 1878
	if (WARN_ON_ONCE(queue < IWL_MVM_DQA_MIN_DATA_QUEUE &&
			 (queue != IWL_MVM_DQA_BSS_CLIENT_QUEUE)))
Johannes Berg's avatar
Johannes Berg committed
1879 1880 1881 1882 1883 1884
		return;

	iwl_mvm_rx_tx_cmd_agg_dbg(mvm, pkt);

	rcu_read_lock();

1885
	mvmsta = iwl_mvm_sta_from_staid_rcu(mvm, sta_id);
Johannes Berg's avatar
Johannes Berg committed
1886

1887
	sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
1888
	if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta) || !sta->wme)) {
1889 1890 1891 1892
		rcu_read_unlock();
		return;
	}

1893
	if (!WARN_ON_ONCE(!mvmsta)) {
Johannes Berg's avatar
Johannes Berg committed
1894 1895
		mvmsta->tid_data[tid].rate_n_flags =
			le32_to_cpu(tx_resp->initial_rate);
1896 1897
		mvmsta->tid_data[tid].tx_time =
			le16_to_cpu(tx_resp->wireless_media_time);
1898
		mvmsta->tid_data[tid].lq_color =
1899
			TX_RES_RATE_TABLE_COL_GET(tx_resp->tlc_info);
1900 1901
		iwl_mvm_tx_airtime(mvm, mvmsta,
				   le16_to_cpu(tx_resp->wireless_media_time));
Johannes Berg's avatar
Johannes Berg committed
1902 1903 1904 1905 1906
	}

	rcu_read_unlock();
}

1907
void iwl_mvm_rx_tx_cmd(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb)
Johannes Berg's avatar
Johannes Berg committed
1908 1909 1910 1911 1912 1913 1914 1915 1916 1917
{
	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);
}

1918 1919
static void iwl_mvm_tx_reclaim(struct iwl_mvm *mvm, int sta_id, int tid,
			       int txq, int index,
1920 1921
			       struct ieee80211_tx_info *tx_info, u32 rate,
			       bool is_flush)
Johannes Berg's avatar
Johannes Berg committed
1922 1923
{
	struct sk_buff_head reclaimed_skbs;
1924
	struct iwl_mvm_tid_data *tid_data = NULL;
Johannes Berg's avatar
Johannes Berg committed
1925
	struct ieee80211_sta *sta;
1926
	struct iwl_mvm_sta *mvmsta = NULL;
Johannes Berg's avatar
Johannes Berg committed
1927
	struct sk_buff *skb;
1928
	int freed;
Johannes Berg's avatar
Johannes Berg committed
1929

1930
	if (WARN_ONCE(sta_id >= mvm->fw->ucode_capa.num_stations ||
1931
		      tid > IWL_MAX_TID_COUNT,
1932
		      "sta_id %d tid %d", sta_id, tid))
1933
		return;
1934

Johannes Berg's avatar
Johannes Berg committed
1935 1936 1937 1938 1939
	rcu_read_lock();

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

	/* Reclaiming frames for a station that has been deleted ? */
1940
	if (WARN_ON_ONCE(!sta)) {
Johannes Berg's avatar
Johannes Berg committed
1941
		rcu_read_unlock();
1942
		return;
Johannes Berg's avatar
Johannes Berg committed
1943 1944
	}

1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963
	__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).
	 */
	iwl_trans_reclaim(mvm->trans, txq, index, &reclaimed_skbs);

	skb_queue_walk(&reclaimed_skbs, skb) {
		struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);

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

		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.
		 */
1964 1965
		if (!is_flush)
			info->flags |= IEEE80211_TX_STAT_ACK;
1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978
	}

	/*
	 * It's possible to get a BA response after invalidating the rcu (rcu is
	 * invalidated in order to prevent new Tx from being sent, but there may
	 * be some frames already in-flight).
	 * In this case we just want to reclaim, and could skip all the
	 * sta-dependent stuff since it's in the middle of being removed
	 * anyways.
	 */
	if (IS_ERR(sta))
		goto out;

1979
	mvmsta = iwl_mvm_sta_from_mac80211(sta);
Johannes Berg's avatar
Johannes Berg committed
1980 1981
	tid_data = &mvmsta->tid_data[tid];

1982
	if (tid_data->txq_id != txq) {
1983
		IWL_ERR(mvm,
1984
			"invalid reclaim request: Q %d, tid %d\n",
1985
			tid_data->txq_id, tid);
Johannes Berg's avatar
Johannes Berg committed
1986
		rcu_read_unlock();
1987
		return;
Johannes Berg's avatar
Johannes Berg committed
1988 1989
	}

1990 1991
	spin_lock_bh(&mvmsta->lock);

1992
	tid_data->next_reclaimed = index;
Johannes Berg's avatar
Johannes Berg committed
1993 1994 1995 1996

	iwl_mvm_check_ratid_empty(mvm, sta, tid);

	freed = 0;
1997 1998

	/* pack lq color from tid_data along the reduced txp */
1999
	tx_info->status.status_driver_data[0] =
2000
		RS_DRV_DATA_PACK(tid_data->lq_color,
2001 2002
				 tx_info->status.status_driver_data[0]);
	tx_info->status.status_driver_data[1] = (void *)(uintptr_t)rate;
Johannes Berg's avatar
Johannes Berg committed
2003 2004

	skb_queue_walk(&reclaimed_skbs, skb) {
2005 2006
		struct ieee80211_hdr *hdr = (void *)skb->data;
		struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Johannes Berg's avatar
Johannes Berg committed
2007

2008 2009 2010 2011 2012 2013
		if (!is_flush) {
			if (ieee80211_is_data_qos(hdr->frame_control))
				freed++;
			else
				WARN_ON_ONCE(tid != IWL_MAX_TID_COUNT);
		}
Johannes Berg's avatar
Johannes Berg committed
2014

2015 2016
		/* this is the first skb we deliver in this batch */
		/* put the rate scaling data there */
2017 2018
		if (freed == 1) {
			info->flags |= IEEE80211_TX_STAT_AMPDU;
2019 2020
			memcpy(&info->status, &tx_info->status,
			       sizeof(tx_info->status));
2021
			iwl_mvm_hwrate_to_tx_status(mvm->fw, rate, info);
2022
		}
Johannes Berg's avatar
Johannes Berg committed
2023 2024
	}

2025
	spin_unlock_bh(&mvmsta->lock);
Johannes Berg's avatar
Johannes Berg committed
2026

2027 2028 2029 2030
	/* 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.
	 */
2031 2032
	if (!is_flush && skb_queue_empty(&reclaimed_skbs) &&
	    !iwl_mvm_has_tlc_offload(mvm)) {
2033 2034
		struct ieee80211_chanctx_conf *chanctx_conf = NULL;

2035
		/* no TLC offload, so non-MLD mode */
2036 2037
		if (mvmsta->vif)
			chanctx_conf =
2038
				rcu_dereference(mvmsta->vif->bss_conf.chanctx_conf);
2039 2040 2041 2042

		if (WARN_ON_ONCE(!chanctx_conf))
			goto out;

2043
		tx_info->band = chanctx_conf->def.chan->band;
2044
		iwl_mvm_hwrate_to_tx_status(mvm->fw, rate, tx_info);
2045

2046 2047
		IWL_DEBUG_TX_REPLY(mvm, "No reclaim. Update rs directly\n");
		iwl_mvm_rs_tx_status(mvm, sta, tid, tx_info, false);
2048 2049 2050
	}

out:
Johannes Berg's avatar
Johannes Berg committed
2051 2052 2053 2054
	rcu_read_unlock();

	while (!skb_queue_empty(&reclaimed_skbs)) {
		skb = __skb_dequeue(&reclaimed_skbs);
2055
		ieee80211_tx_status(mvm->hw, skb);
Johannes Berg's avatar
Johannes Berg committed
2056 2057 2058
	}
}

2059 2060 2061
void iwl_mvm_rx_ba_notif(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb)
{
	struct iwl_rx_packet *pkt = rxb_addr(rxb);
2062
	unsigned int pkt_len = iwl_rx_packet_payload_len(pkt);
2063 2064 2065 2066 2067 2068
	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;

2069 2070
	ba_info.flags = IEEE80211_TX_STAT_AMPDU;

2071 2072 2073
	if (iwl_mvm_has_new_tx_api(mvm)) {
		struct iwl_mvm_compressed_ba_notif *ba_res =
			(void *)pkt->data;
2074
		u8 lq_color = TX_RES_RATE_TABLE_COL_GET(ba_res->tlc_rate_info);
2075
		u16 tfd_cnt;
2076
		int i;
2077

2078 2079
		if (IWL_FW_CHECK(mvm, sizeof(*ba_res) > pkt_len,
				 "short BA notification (%d)\n", pkt_len))
2080 2081
			return;

2082 2083 2084 2085 2086 2087 2088 2089
		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;

2090
		tfd_cnt = le16_to_cpu(ba_res->tfd_cnt);
2091 2092 2093 2094 2095 2096 2097
		if (!tfd_cnt)
			return;

		if (IWL_FW_CHECK(mvm,
				 struct_size(ba_res, tfd, tfd_cnt) > pkt_len,
				 "short BA notification (tfds:%d, size:%d)\n",
				 tfd_cnt, pkt_len))
2098
			return;
2099

2100 2101 2102
		rcu_read_lock();

		mvmsta = iwl_mvm_sta_from_staid_rcu(mvm, sta_id);
2103 2104 2105 2106 2107 2108 2109 2110
		/*
		 * It's possible to get a BA response after invalidating the rcu
		 * (rcu is invalidated in order to prevent new Tx from being
		 * sent, but there may be some frames already in-flight).
		 * In this case we just want to reclaim, and could skip all the
		 * sta-dependent stuff since it's in the middle of being removed
		 * anyways.
		 */
2111

2112
		/* Free per TID */
2113
		for (i = 0; i < tfd_cnt; i++) {
2114 2115 2116
			struct iwl_mvm_compressed_ba_tfd *ba_tfd =
				&ba_res->tfd[i];

2117 2118 2119 2120
			tid = ba_tfd->tid;
			if (tid == IWL_MGMT_TID)
				tid = IWL_MAX_TID_COUNT;

2121 2122 2123
			if (mvmsta)
				mvmsta->tid_data[i].lq_color = lq_color;

2124
			iwl_mvm_tx_reclaim(mvm, sta_id, tid,
2125 2126 2127
					   (int)(le16_to_cpu(ba_tfd->q_num)),
					   le16_to_cpu(ba_tfd->tfd_index),
					   &ba_info,
2128
					   le32_to_cpu(ba_res->tx_rate), false);
2129
		}
2130

2131 2132 2133
		if (mvmsta)
			iwl_mvm_tx_airtime(mvm, mvmsta,
					   le32_to_cpu(ba_res->wireless_time));
2134
		rcu_read_unlock();
2135

2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154
		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);
2155 2156 2157
	if (IWL_FW_CHECK(mvm, !mvmsta,
			 "invalid STA ID %d in BA notif\n",
			 sta_id)) {
2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172
		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,
2173
			   tid_data->rate_n_flags, false);
2174 2175 2176

	IWL_DEBUG_TX_REPLY(mvm,
			   "BA_NOTIFICATION Received from %pM, sta_id = %d\n",
2177
			   ba_notif->sta_addr, ba_notif->sta_id);
2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188

	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);
}

2189 2190 2191 2192 2193 2194 2195 2196
/*
 * 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
 */
2197
int iwl_mvm_flush_tx_path(struct iwl_mvm *mvm, u32 tfd_msk)
Johannes Berg's avatar
Johannes Berg committed
2198 2199
{
	int ret;
2200
	struct iwl_tx_path_flush_cmd_v1 flush_cmd = {
Johannes Berg's avatar
Johannes Berg committed
2201 2202 2203 2204
		.queues_ctl = cpu_to_le32(tfd_msk),
		.flush_ctl = cpu_to_le16(DUMP_TX_FIFO_FLUSH),
	};

2205
	WARN_ON(iwl_mvm_has_new_tx_api(mvm));
2206
	ret = iwl_mvm_send_cmd_pdu(mvm, TXPATH_FLUSH, 0,
Johannes Berg's avatar
Johannes Berg committed
2207 2208 2209 2210 2211
				   sizeof(flush_cmd), &flush_cmd);
	if (ret)
		IWL_ERR(mvm, "Failed to send flush command (%d)\n", ret);
	return ret;
}
2212

2213
int iwl_mvm_flush_sta_tids(struct iwl_mvm *mvm, u32 sta_id, u16 tids)
2214
{
2215
	int ret;
2216
	struct iwl_tx_path_flush_cmd_rsp *rsp;
2217 2218 2219 2220
	struct iwl_tx_path_flush_cmd flush_cmd = {
		.sta_id = cpu_to_le32(sta_id),
		.tid_mask = cpu_to_le16(tids),
	};
2221

2222 2223 2224 2225 2226 2227
	struct iwl_host_cmd cmd = {
		.id = TXPATH_FLUSH,
		.len = { sizeof(flush_cmd), },
		.data = { &flush_cmd, },
	};

2228
	WARN_ON(!iwl_mvm_has_new_tx_api(mvm));
2229

2230 2231 2232 2233 2234 2235 2236 2237 2238
	if (iwl_fw_lookup_notif_ver(mvm->fw, LONG_GROUP, TXPATH_FLUSH, 0) > 0)
		cmd.flags |= CMD_WANT_SKB;

	IWL_DEBUG_TX_QUEUES(mvm, "flush for sta id %d tid mask 0x%x\n",
			    sta_id, tids);

	ret = iwl_mvm_send_cmd(mvm, &cmd);

	if (ret) {
2239
		IWL_ERR(mvm, "Failed to send flush command (%d)\n", ret);
2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288
		return ret;
	}

	if (cmd.flags & CMD_WANT_SKB) {
		int i;
		int num_flushed_queues;

		if (WARN_ON_ONCE(iwl_rx_packet_payload_len(cmd.resp_pkt) != sizeof(*rsp))) {
			ret = -EIO;
			goto free_rsp;
		}

		rsp = (void *)cmd.resp_pkt->data;

		if (WARN_ONCE(le16_to_cpu(rsp->sta_id) != sta_id,
			      "sta_id %d != rsp_sta_id %d",
			      sta_id, le16_to_cpu(rsp->sta_id))) {
			ret = -EIO;
			goto free_rsp;
		}

		num_flushed_queues = le16_to_cpu(rsp->num_flushed_queues);
		if (WARN_ONCE(num_flushed_queues > IWL_TX_FLUSH_QUEUE_RSP,
			      "num_flushed_queues %d", num_flushed_queues)) {
			ret = -EIO;
			goto free_rsp;
		}

		for (i = 0; i < num_flushed_queues; i++) {
			struct ieee80211_tx_info tx_info = {};
			struct iwl_flush_queue_info *queue_info = &rsp->queues[i];
			int tid = le16_to_cpu(queue_info->tid);
			int read_before = le16_to_cpu(queue_info->read_before_flush);
			int read_after = le16_to_cpu(queue_info->read_after_flush);
			int queue_num = le16_to_cpu(queue_info->queue_num);

			if (tid == IWL_MGMT_TID)
				tid = IWL_MAX_TID_COUNT;

			IWL_DEBUG_TX_QUEUES(mvm,
					    "tid %d queue_id %d read-before %d read-after %d\n",
					    tid, queue_num, read_before, read_after);

			iwl_mvm_tx_reclaim(mvm, sta_id, tid, queue_num, read_after,
					   &tx_info, 0, true);
		}
free_rsp:
		iwl_free_resp(&cmd);
	}
2289 2290
	return ret;
}
2291

2292
int iwl_mvm_flush_sta(struct iwl_mvm *mvm, void *sta, bool internal)
2293
{
2294
	u32 sta_id, tfd_queue_msk;
2295

2296 2297
	if (internal) {
		struct iwl_mvm_int_sta *int_sta = sta;
2298

2299 2300 2301 2302
		sta_id = int_sta->sta_id;
		tfd_queue_msk = int_sta->tfd_queue_msk;
	} else {
		struct iwl_mvm_sta *mvm_sta = sta;
2303

2304 2305 2306 2307 2308 2309
		sta_id = mvm_sta->deflink.sta_id;
		tfd_queue_msk = mvm_sta->tfd_queue_msk;
	}

	if (iwl_mvm_has_new_tx_api(mvm))
		return iwl_mvm_flush_sta_tids(mvm, sta_id, 0xffff);
2310

2311
	return iwl_mvm_flush_tx_path(mvm, tfd_queue_msk);
2312
}