Linux Audio

Check our new training course

Loading...
v4.17
   1/******************************************************************************
   2 *
   3 * This file is provided under a dual BSD/GPLv2 license.  When using or
   4 * redistributing this file, you may do so under either license.
   5 *
   6 * GPL LICENSE SUMMARY
   7 *
   8 * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
   9 * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
  10 * Copyright(c) 2015 - 2017 Intel Deutschland GmbH
  11 *
  12 * This program is free software; you can redistribute it and/or modify
  13 * it under the terms of version 2 of the GNU General Public License as
  14 * published by the Free Software Foundation.
  15 *
  16 * This program is distributed in the hope that it will be useful, but
  17 * WITHOUT ANY WARRANTY; without even the implied warranty of
  18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  19 * General Public License for more details.
  20 *
  21 * You should have received a copy of the GNU General Public License
  22 * along with this program; if not, write to the Free Software
  23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
  24 * USA
  25 *
  26 * The full GNU General Public License is included in this distribution
  27 * in the file called COPYING.
  28 *
  29 * Contact Information:
  30 *  Intel Linux Wireless <linuxwifi@intel.com>
  31 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  32 *
  33 * BSD LICENSE
  34 *
  35 * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
  36 * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
  37 * Copyright(c) 2015 - 2017 Intel Deutschland GmbH
  38 * All rights reserved.
  39 *
  40 * Redistribution and use in source and binary forms, with or without
  41 * modification, are permitted provided that the following conditions
  42 * are met:
  43 *
  44 *  * Redistributions of source code must retain the above copyright
  45 *    notice, this list of conditions and the following disclaimer.
  46 *  * Redistributions in binary form must reproduce the above copyright
  47 *    notice, this list of conditions and the following disclaimer in
  48 *    the documentation and/or other materials provided with the
  49 *    distribution.
  50 *  * Neither the name Intel Corporation nor the names of its
  51 *    contributors may be used to endorse or promote products derived
  52 *    from this software without specific prior written permission.
  53 *
  54 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  55 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  56 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  57 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  58 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  59 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  60 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  61 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  62 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  63 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  64 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  65 *
  66 *****************************************************************************/
  67
  68#include <linux/etherdevice.h>
  69#include <net/mac80211.h>
  70#include "iwl-io.h"
  71#include "iwl-prph.h"
  72#include "fw-api.h"
  73#include "mvm.h"
  74#include "time-event.h"
 
  75
  76const u8 iwl_mvm_ac_to_tx_fifo[] = {
  77	IWL_MVM_TX_FIFO_VO,
  78	IWL_MVM_TX_FIFO_VI,
  79	IWL_MVM_TX_FIFO_BE,
  80	IWL_MVM_TX_FIFO_BK,
  81};
  82
  83const u8 iwl_mvm_ac_to_gen2_tx_fifo[] = {
  84	IWL_GEN2_EDCA_TX_FIFO_VO,
  85	IWL_GEN2_EDCA_TX_FIFO_VI,
  86	IWL_GEN2_EDCA_TX_FIFO_BE,
  87	IWL_GEN2_EDCA_TX_FIFO_BK,
  88};
  89
  90struct iwl_mvm_mac_iface_iterator_data {
  91	struct iwl_mvm *mvm;
  92	struct ieee80211_vif *vif;
  93	unsigned long available_mac_ids[BITS_TO_LONGS(NUM_MAC_INDEX_DRIVER)];
  94	unsigned long available_tsf_ids[BITS_TO_LONGS(NUM_TSF_IDS)];
  95	enum iwl_tsf_id preferred_tsf;
  96	bool found_vif;
  97};
  98
  99struct iwl_mvm_hw_queues_iface_iterator_data {
 100	struct ieee80211_vif *exclude_vif;
 101	unsigned long used_hw_queues;
 102};
 103
 104static void iwl_mvm_mac_tsf_id_iter(void *_data, u8 *mac,
 105				    struct ieee80211_vif *vif)
 106{
 107	struct iwl_mvm_mac_iface_iterator_data *data = _data;
 108	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
 109	u16 min_bi;
 110
 111	/* Skip the interface for which we are trying to assign a tsf_id  */
 112	if (vif == data->vif)
 113		return;
 114
 115	/*
 116	 * The TSF is a hardware/firmware resource, there are 4 and
 117	 * the driver should assign and free them as needed. However,
 118	 * there are cases where 2 MACs should share the same TSF ID
 119	 * for the purpose of clock sync, an optimization to avoid
 120	 * clock drift causing overlapping TBTTs/DTIMs for a GO and
 121	 * client in the system.
 122	 *
 123	 * The firmware will decide according to the MAC type which
 124	 * will be the master and slave. Clients that need to sync
 125	 * with a remote station will be the master, and an AP or GO
 126	 * will be the slave.
 127	 *
 128	 * Depending on the new interface type it can be slaved to
 129	 * or become the master of an existing interface.
 130	 */
 131	switch (data->vif->type) {
 132	case NL80211_IFTYPE_STATION:
 133		/*
 134		 * The new interface is a client, so if the one we're iterating
 135		 * is an AP, and the beacon interval of the AP is a multiple or
 136		 * divisor of the beacon interval of the client, the same TSF
 137		 * should be used to avoid drift between the new client and
 138		 * existing AP. The existing AP will get drift updates from the
 139		 * new client context in this case.
 140		 */
 141		if (vif->type != NL80211_IFTYPE_AP ||
 142		    data->preferred_tsf != NUM_TSF_IDS ||
 143		    !test_bit(mvmvif->tsf_id, data->available_tsf_ids))
 144			break;
 145
 146		min_bi = min(data->vif->bss_conf.beacon_int,
 147			     vif->bss_conf.beacon_int);
 148
 149		if (!min_bi)
 150			break;
 151
 152		if ((data->vif->bss_conf.beacon_int -
 153		     vif->bss_conf.beacon_int) % min_bi == 0) {
 154			data->preferred_tsf = mvmvif->tsf_id;
 155			return;
 156		}
 157		break;
 158
 159	case NL80211_IFTYPE_AP:
 160		/*
 161		 * The new interface is AP/GO, so if its beacon interval is a
 162		 * multiple or a divisor of the beacon interval of an existing
 163		 * interface, it should get drift updates from an existing
 164		 * client or use the same TSF as an existing GO. There's no
 165		 * drift between TSFs internally but if they used different
 166		 * TSFs then a new client MAC could update one of them and
 167		 * cause drift that way.
 168		 */
 169		if ((vif->type != NL80211_IFTYPE_AP &&
 170		     vif->type != NL80211_IFTYPE_STATION) ||
 171		    data->preferred_tsf != NUM_TSF_IDS ||
 172		    !test_bit(mvmvif->tsf_id, data->available_tsf_ids))
 173			break;
 174
 175		min_bi = min(data->vif->bss_conf.beacon_int,
 176			     vif->bss_conf.beacon_int);
 177
 178		if (!min_bi)
 179			break;
 180
 181		if ((data->vif->bss_conf.beacon_int -
 182		     vif->bss_conf.beacon_int) % min_bi == 0) {
 183			data->preferred_tsf = mvmvif->tsf_id;
 184			return;
 185		}
 186		break;
 187	default:
 188		/*
 189		 * For all other interface types there's no need to
 190		 * take drift into account. Either they're exclusive
 191		 * like IBSS and monitor, or we don't care much about
 192		 * their TSF (like P2P Device), but we won't be able
 193		 * to share the TSF resource.
 194		 */
 195		break;
 196	}
 197
 198	/*
 199	 * Unless we exited above, we can't share the TSF resource
 200	 * that the virtual interface we're iterating over is using
 201	 * with the new one, so clear the available bit and if this
 202	 * was the preferred one, reset that as well.
 203	 */
 204	__clear_bit(mvmvif->tsf_id, data->available_tsf_ids);
 205
 206	if (data->preferred_tsf == mvmvif->tsf_id)
 207		data->preferred_tsf = NUM_TSF_IDS;
 208}
 209
 210/*
 211 * Get the mask of the queues used by the vif
 212 */
 213u32 iwl_mvm_mac_get_queues_mask(struct ieee80211_vif *vif)
 214{
 215	u32 qmask = 0, ac;
 216
 217	if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
 218		return BIT(IWL_MVM_OFFCHANNEL_QUEUE);
 219
 220	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
 221		if (vif->hw_queue[ac] != IEEE80211_INVAL_HW_QUEUE)
 222			qmask |= BIT(vif->hw_queue[ac]);
 223	}
 224
 225	if (vif->type == NL80211_IFTYPE_AP ||
 226	    vif->type == NL80211_IFTYPE_ADHOC)
 227		qmask |= BIT(vif->cab_queue);
 228
 229	return qmask;
 230}
 231
 232static void iwl_mvm_iface_hw_queues_iter(void *_data, u8 *mac,
 233					 struct ieee80211_vif *vif)
 234{
 235	struct iwl_mvm_hw_queues_iface_iterator_data *data = _data;
 236
 237	/* exclude the given vif */
 238	if (vif == data->exclude_vif)
 239		return;
 240
 241	data->used_hw_queues |= iwl_mvm_mac_get_queues_mask(vif);
 242}
 243
 
 
 
 
 
 
 
 
 
 
 244unsigned long iwl_mvm_get_used_hw_queues(struct iwl_mvm *mvm,
 245					 struct ieee80211_vif *exclude_vif)
 246{
 
 247	struct iwl_mvm_hw_queues_iface_iterator_data data = {
 248		.exclude_vif = exclude_vif,
 249		.used_hw_queues =
 250			BIT(IWL_MVM_OFFCHANNEL_QUEUE) |
 251			BIT(mvm->aux_queue) |
 252			BIT(IWL_MVM_DQA_GCAST_QUEUE),
 253	};
 254
 
 
 
 
 
 255	lockdep_assert_held(&mvm->mutex);
 256
 257	/* mark all VIF used hw queues */
 258	ieee80211_iterate_active_interfaces_atomic(
 259		mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
 260		iwl_mvm_iface_hw_queues_iter, &data);
 261
 
 
 
 
 
 
 
 
 
 
 
 
 262	return data.used_hw_queues;
 263}
 264
 265static void iwl_mvm_mac_iface_iterator(void *_data, u8 *mac,
 266				       struct ieee80211_vif *vif)
 267{
 268	struct iwl_mvm_mac_iface_iterator_data *data = _data;
 269	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
 270
 271	/* Iterator may already find the interface being added -- skip it */
 272	if (vif == data->vif) {
 273		data->found_vif = true;
 274		return;
 275	}
 276
 277	/* Mark MAC IDs as used by clearing the available bit, and
 278	 * (below) mark TSFs as used if their existing use is not
 279	 * compatible with the new interface type.
 280	 * No locking or atomic bit operations are needed since the
 281	 * data is on the stack of the caller function.
 282	 */
 283	__clear_bit(mvmvif->id, data->available_mac_ids);
 284
 285	/* find a suitable tsf_id */
 286	iwl_mvm_mac_tsf_id_iter(_data, mac, vif);
 287}
 288
 289void iwl_mvm_mac_ctxt_recalc_tsf_id(struct iwl_mvm *mvm,
 290				    struct ieee80211_vif *vif)
 291{
 292	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
 293	struct iwl_mvm_mac_iface_iterator_data data = {
 294		.mvm = mvm,
 295		.vif = vif,
 296		.available_tsf_ids = { (1 << NUM_TSF_IDS) - 1 },
 297		/* no preference yet */
 298		.preferred_tsf = NUM_TSF_IDS,
 299	};
 300
 301	ieee80211_iterate_active_interfaces_atomic(
 302		mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
 303		iwl_mvm_mac_tsf_id_iter, &data);
 304
 305	if (data.preferred_tsf != NUM_TSF_IDS)
 306		mvmvif->tsf_id = data.preferred_tsf;
 307	else if (!test_bit(mvmvif->tsf_id, data.available_tsf_ids))
 308		mvmvif->tsf_id = find_first_bit(data.available_tsf_ids,
 309						NUM_TSF_IDS);
 310}
 311
 312int iwl_mvm_mac_ctxt_init(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
 
 313{
 314	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
 315	struct iwl_mvm_mac_iface_iterator_data data = {
 316		.mvm = mvm,
 317		.vif = vif,
 318		.available_mac_ids = { (1 << NUM_MAC_INDEX_DRIVER) - 1 },
 319		.available_tsf_ids = { (1 << NUM_TSF_IDS) - 1 },
 320		/* no preference yet */
 321		.preferred_tsf = NUM_TSF_IDS,
 322		.found_vif = false,
 323	};
 324	u32 ac;
 325	int ret, i, queue_limit;
 326	unsigned long used_hw_queues;
 327
 328	lockdep_assert_held(&mvm->mutex);
 329
 330	/*
 331	 * Allocate a MAC ID and a TSF for this MAC, along with the queues
 332	 * and other resources.
 333	 */
 334
 335	/*
 336	 * Before the iterator, we start with all MAC IDs and TSFs available.
 337	 *
 338	 * During iteration, all MAC IDs are cleared that are in use by other
 339	 * virtual interfaces, and all TSF IDs are cleared that can't be used
 340	 * by this new virtual interface because they're used by an interface
 341	 * that can't share it with the new one.
 342	 * At the same time, we check if there's a preferred TSF in the case
 343	 * that we should share it with another interface.
 344	 */
 345
 346	/* Currently, MAC ID 0 should be used only for the managed/IBSS vif */
 347	switch (vif->type) {
 348	case NL80211_IFTYPE_ADHOC:
 349		break;
 350	case NL80211_IFTYPE_STATION:
 351		if (!vif->p2p)
 352			break;
 353		/* fall through */
 354	default:
 355		__clear_bit(0, data.available_mac_ids);
 356	}
 357
 358	ieee80211_iterate_active_interfaces_atomic(
 359		mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
 360		iwl_mvm_mac_iface_iterator, &data);
 361
 362	used_hw_queues = iwl_mvm_get_used_hw_queues(mvm, vif);
 363
 364	/*
 365	 * In the case we're getting here during resume, it's similar to
 366	 * firmware restart, and with RESUME_ALL the iterator will find
 367	 * the vif being added already.
 368	 * We don't want to reassign any IDs in either case since doing
 369	 * so would probably assign different IDs (as interfaces aren't
 370	 * necessarily added in the same order), but the old IDs were
 371	 * preserved anyway, so skip ID assignment for both resume and
 372	 * recovery.
 373	 */
 374	if (data.found_vif)
 375		return 0;
 376
 377	/* Therefore, in recovery, we can't get here */
 378	if (WARN_ON_ONCE(test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)))
 379		return -EBUSY;
 380
 381	mvmvif->id = find_first_bit(data.available_mac_ids,
 382				    NUM_MAC_INDEX_DRIVER);
 383	if (mvmvif->id == NUM_MAC_INDEX_DRIVER) {
 384		IWL_ERR(mvm, "Failed to init MAC context - no free ID!\n");
 385		ret = -EIO;
 386		goto exit_fail;
 387	}
 388
 389	if (data.preferred_tsf != NUM_TSF_IDS)
 390		mvmvif->tsf_id = data.preferred_tsf;
 391	else
 392		mvmvif->tsf_id = find_first_bit(data.available_tsf_ids,
 393						NUM_TSF_IDS);
 394	if (mvmvif->tsf_id == NUM_TSF_IDS) {
 395		IWL_ERR(mvm, "Failed to init MAC context - no free TSF!\n");
 396		ret = -EIO;
 397		goto exit_fail;
 398	}
 399
 400	mvmvif->color = 0;
 401
 402	INIT_LIST_HEAD(&mvmvif->time_event_data.list);
 403	mvmvif->time_event_data.id = TE_MAX;
 404
 405	/* No need to allocate data queues to P2P Device MAC.*/
 406	if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
 407		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
 408			vif->hw_queue[ac] = IEEE80211_INVAL_HW_QUEUE;
 409
 410		return 0;
 411	}
 412
 413	/*
 414	 * queues in mac80211 almost entirely independent of
 415	 * the ones here - no real limit
 416	 */
 417	queue_limit = IEEE80211_MAX_QUEUES;
 418	BUILD_BUG_ON(IEEE80211_MAX_QUEUES >
 419		     BITS_PER_BYTE *
 420		     sizeof(mvm->hw_queue_to_mac80211[0]));
 421
 422	/*
 423	 * Find available queues, and allocate them to the ACs. When in
 424	 * DQA-mode they aren't really used, and this is done only so the
 425	 * mac80211 ieee80211_check_queues() function won't fail
 426	 */
 427	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
 428		u8 queue = find_first_zero_bit(&used_hw_queues, queue_limit);
 
 429
 430		if (queue >= queue_limit) {
 
 431			IWL_ERR(mvm, "Failed to allocate queue\n");
 432			ret = -EIO;
 433			goto exit_fail;
 434		}
 435
 436		__set_bit(queue, &used_hw_queues);
 437		vif->hw_queue[ac] = queue;
 438	}
 439
 440	/* Allocate the CAB queue for softAP and GO interfaces */
 441	if (vif->type == NL80211_IFTYPE_AP ||
 442	    vif->type == NL80211_IFTYPE_ADHOC) {
 443		/*
 444		 * For TVQM this will be overwritten later with the FW assigned
 445		 * queue value (when queue is enabled).
 446		 */
 447		mvmvif->cab_queue = IWL_MVM_DQA_GCAST_QUEUE;
 448		vif->cab_queue = IWL_MVM_DQA_GCAST_QUEUE;
 
 
 
 
 
 
 
 
 
 449	} else {
 450		vif->cab_queue = IEEE80211_INVAL_HW_QUEUE;
 451	}
 452
 453	mvmvif->bcast_sta.sta_id = IWL_MVM_INVALID_STA;
 454	mvmvif->mcast_sta.sta_id = IWL_MVM_INVALID_STA;
 455	mvmvif->ap_sta_id = IWL_MVM_INVALID_STA;
 456
 457	for (i = 0; i < NUM_IWL_MVM_SMPS_REQ; i++)
 458		mvmvif->smps_requests[i] = IEEE80211_SMPS_AUTOMATIC;
 459
 460	return 0;
 461
 462exit_fail:
 463	memset(mvmvif, 0, sizeof(struct iwl_mvm_vif));
 464	memset(vif->hw_queue, IEEE80211_INVAL_HW_QUEUE, sizeof(vif->hw_queue));
 465	vif->cab_queue = IEEE80211_INVAL_HW_QUEUE;
 466	return ret;
 467}
 468
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 469static void iwl_mvm_ack_rates(struct iwl_mvm *mvm,
 470			      struct ieee80211_vif *vif,
 471			      enum nl80211_band band,
 472			      u8 *cck_rates, u8 *ofdm_rates)
 473{
 474	struct ieee80211_supported_band *sband;
 475	unsigned long basic = vif->bss_conf.basic_rates;
 476	int lowest_present_ofdm = 100;
 477	int lowest_present_cck = 100;
 478	u8 cck = 0;
 479	u8 ofdm = 0;
 480	int i;
 481
 482	sband = mvm->hw->wiphy->bands[band];
 483
 484	for_each_set_bit(i, &basic, BITS_PER_LONG) {
 485		int hw = sband->bitrates[i].hw_value;
 486		if (hw >= IWL_FIRST_OFDM_RATE) {
 487			ofdm |= BIT(hw - IWL_FIRST_OFDM_RATE);
 488			if (lowest_present_ofdm > hw)
 489				lowest_present_ofdm = hw;
 490		} else {
 491			BUILD_BUG_ON(IWL_FIRST_CCK_RATE != 0);
 492
 493			cck |= BIT(hw);
 494			if (lowest_present_cck > hw)
 495				lowest_present_cck = hw;
 496		}
 497	}
 498
 499	/*
 500	 * Now we've got the basic rates as bitmaps in the ofdm and cck
 501	 * variables. This isn't sufficient though, as there might not
 502	 * be all the right rates in the bitmap. E.g. if the only basic
 503	 * rates are 5.5 Mbps and 11 Mbps, we still need to add 1 Mbps
 504	 * and 6 Mbps because the 802.11-2007 standard says in 9.6:
 505	 *
 506	 *    [...] a STA responding to a received frame shall transmit
 507	 *    its Control Response frame [...] at the highest rate in the
 508	 *    BSSBasicRateSet parameter that is less than or equal to the
 509	 *    rate of the immediately previous frame in the frame exchange
 510	 *    sequence ([...]) and that is of the same modulation class
 511	 *    ([...]) as the received frame. If no rate contained in the
 512	 *    BSSBasicRateSet parameter meets these conditions, then the
 513	 *    control frame sent in response to a received frame shall be
 514	 *    transmitted at the highest mandatory rate of the PHY that is
 515	 *    less than or equal to the rate of the received frame, and
 516	 *    that is of the same modulation class as the received frame.
 517	 *
 518	 * As a consequence, we need to add all mandatory rates that are
 519	 * lower than all of the basic rates to these bitmaps.
 520	 */
 521
 522	if (IWL_RATE_24M_INDEX < lowest_present_ofdm)
 523		ofdm |= IWL_RATE_BIT_MSK(24) >> IWL_FIRST_OFDM_RATE;
 524	if (IWL_RATE_12M_INDEX < lowest_present_ofdm)
 525		ofdm |= IWL_RATE_BIT_MSK(12) >> IWL_FIRST_OFDM_RATE;
 526	/* 6M already there or needed so always add */
 527	ofdm |= IWL_RATE_BIT_MSK(6) >> IWL_FIRST_OFDM_RATE;
 528
 529	/*
 530	 * CCK is a bit more complex with DSSS vs. HR/DSSS vs. ERP.
 531	 * Note, however:
 532	 *  - if no CCK rates are basic, it must be ERP since there must
 533	 *    be some basic rates at all, so they're OFDM => ERP PHY
 534	 *    (or we're in 5 GHz, and the cck bitmap will never be used)
 535	 *  - if 11M is a basic rate, it must be ERP as well, so add 5.5M
 536	 *  - if 5.5M is basic, 1M and 2M are mandatory
 537	 *  - if 2M is basic, 1M is mandatory
 538	 *  - if 1M is basic, that's the only valid ACK rate.
 539	 * As a consequence, it's not as complicated as it sounds, just add
 540	 * any lower rates to the ACK rate bitmap.
 541	 */
 542	if (IWL_RATE_11M_INDEX < lowest_present_cck)
 543		cck |= IWL_RATE_BIT_MSK(11) >> IWL_FIRST_CCK_RATE;
 544	if (IWL_RATE_5M_INDEX < lowest_present_cck)
 545		cck |= IWL_RATE_BIT_MSK(5) >> IWL_FIRST_CCK_RATE;
 546	if (IWL_RATE_2M_INDEX < lowest_present_cck)
 547		cck |= IWL_RATE_BIT_MSK(2) >> IWL_FIRST_CCK_RATE;
 548	/* 1M already there or needed so always add */
 549	cck |= IWL_RATE_BIT_MSK(1) >> IWL_FIRST_CCK_RATE;
 550
 551	*cck_rates = cck;
 552	*ofdm_rates = ofdm;
 553}
 554
 555static void iwl_mvm_mac_ctxt_set_ht_flags(struct iwl_mvm *mvm,
 556					 struct ieee80211_vif *vif,
 557					 struct iwl_mac_ctx_cmd *cmd)
 558{
 559	/* for both sta and ap, ht_operation_mode hold the protection_mode */
 560	u8 protection_mode = vif->bss_conf.ht_operation_mode &
 561				 IEEE80211_HT_OP_MODE_PROTECTION;
 562	/* The fw does not distinguish between ht and fat */
 563	u32 ht_flag = MAC_PROT_FLG_HT_PROT | MAC_PROT_FLG_FAT_PROT;
 564
 565	IWL_DEBUG_RATE(mvm, "protection mode set to %d\n", protection_mode);
 566	/*
 567	 * See section 9.23.3.1 of IEEE 80211-2012.
 568	 * Nongreenfield HT STAs Present is not supported.
 569	 */
 570	switch (protection_mode) {
 571	case IEEE80211_HT_OP_MODE_PROTECTION_NONE:
 572		break;
 573	case IEEE80211_HT_OP_MODE_PROTECTION_NONMEMBER:
 574	case IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED:
 575		cmd->protection_flags |= cpu_to_le32(ht_flag);
 576		break;
 577	case IEEE80211_HT_OP_MODE_PROTECTION_20MHZ:
 578		/* Protect when channel wider than 20MHz */
 579		if (vif->bss_conf.chandef.width > NL80211_CHAN_WIDTH_20)
 580			cmd->protection_flags |= cpu_to_le32(ht_flag);
 581		break;
 582	default:
 583		IWL_ERR(mvm, "Illegal protection mode %d\n",
 584			protection_mode);
 585		break;
 586	}
 587}
 588
 589static void iwl_mvm_mac_ctxt_cmd_common(struct iwl_mvm *mvm,
 590					struct ieee80211_vif *vif,
 591					struct iwl_mac_ctx_cmd *cmd,
 592					const u8 *bssid_override,
 593					u32 action)
 594{
 595	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
 596	struct ieee80211_chanctx_conf *chanctx;
 597	bool ht_enabled = !!(vif->bss_conf.ht_operation_mode &
 598			     IEEE80211_HT_OP_MODE_PROTECTION);
 599	u8 cck_ack_rates, ofdm_ack_rates;
 600	const u8 *bssid = bssid_override ?: vif->bss_conf.bssid;
 601	int i;
 602
 603	cmd->id_and_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
 604							    mvmvif->color));
 605	cmd->action = cpu_to_le32(action);
 606
 607	switch (vif->type) {
 608	case NL80211_IFTYPE_STATION:
 609		if (vif->p2p)
 610			cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_P2P_STA);
 611		else
 612			cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_BSS_STA);
 613		break;
 614	case NL80211_IFTYPE_AP:
 615		cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_GO);
 616		break;
 617	case NL80211_IFTYPE_MONITOR:
 618		cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_LISTENER);
 619		break;
 620	case NL80211_IFTYPE_P2P_DEVICE:
 621		cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_P2P_DEVICE);
 622		break;
 623	case NL80211_IFTYPE_ADHOC:
 624		cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_IBSS);
 625		break;
 626	default:
 627		WARN_ON_ONCE(1);
 628	}
 629
 630	cmd->tsf_id = cpu_to_le32(mvmvif->tsf_id);
 631
 632	memcpy(cmd->node_addr, vif->addr, ETH_ALEN);
 633
 634	if (bssid)
 635		memcpy(cmd->bssid_addr, bssid, ETH_ALEN);
 636	else
 637		eth_broadcast_addr(cmd->bssid_addr);
 638
 639	rcu_read_lock();
 640	chanctx = rcu_dereference(vif->chanctx_conf);
 641	iwl_mvm_ack_rates(mvm, vif, chanctx ? chanctx->def.chan->band
 642					    : NL80211_BAND_2GHZ,
 643			  &cck_ack_rates, &ofdm_ack_rates);
 644	rcu_read_unlock();
 645
 646	cmd->cck_rates = cpu_to_le32((u32)cck_ack_rates);
 647	cmd->ofdm_rates = cpu_to_le32((u32)ofdm_ack_rates);
 648
 649	cmd->cck_short_preamble =
 650		cpu_to_le32(vif->bss_conf.use_short_preamble ?
 651			    MAC_FLG_SHORT_PREAMBLE : 0);
 652	cmd->short_slot =
 653		cpu_to_le32(vif->bss_conf.use_short_slot ?
 654			    MAC_FLG_SHORT_SLOT : 0);
 655
 656	cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP);
 657
 658	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
 659		u8 txf = iwl_mvm_mac_ac_to_tx_fifo(mvm, i);
 660
 661		cmd->ac[txf].cw_min =
 662			cpu_to_le16(mvmvif->queue_params[i].cw_min);
 663		cmd->ac[txf].cw_max =
 664			cpu_to_le16(mvmvif->queue_params[i].cw_max);
 665		cmd->ac[txf].edca_txop =
 666			cpu_to_le16(mvmvif->queue_params[i].txop * 32);
 667		cmd->ac[txf].aifsn = mvmvif->queue_params[i].aifs;
 668		cmd->ac[txf].fifos_mask = BIT(txf);
 669	}
 670
 671	if (vif->bss_conf.qos)
 672		cmd->qos_flags |= cpu_to_le32(MAC_QOS_FLG_UPDATE_EDCA);
 673
 674	if (vif->bss_conf.use_cts_prot)
 675		cmd->protection_flags |= cpu_to_le32(MAC_PROT_FLG_TGG_PROTECT);
 676
 677	IWL_DEBUG_RATE(mvm, "use_cts_prot %d, ht_operation_mode %d\n",
 678		       vif->bss_conf.use_cts_prot,
 679		       vif->bss_conf.ht_operation_mode);
 680	if (vif->bss_conf.chandef.width != NL80211_CHAN_WIDTH_20_NOHT)
 681		cmd->qos_flags |= cpu_to_le32(MAC_QOS_FLG_TGN);
 682	if (ht_enabled)
 683		iwl_mvm_mac_ctxt_set_ht_flags(mvm, vif, cmd);
 684}
 685
 686static int iwl_mvm_mac_ctxt_send_cmd(struct iwl_mvm *mvm,
 687				     struct iwl_mac_ctx_cmd *cmd)
 688{
 689	int ret = iwl_mvm_send_cmd_pdu(mvm, MAC_CONTEXT_CMD, 0,
 690				       sizeof(*cmd), cmd);
 691	if (ret)
 692		IWL_ERR(mvm, "Failed to send MAC context (action:%d): %d\n",
 693			le32_to_cpu(cmd->action), ret);
 694	return ret;
 695}
 696
 697static int iwl_mvm_mac_ctxt_cmd_sta(struct iwl_mvm *mvm,
 698				    struct ieee80211_vif *vif,
 699				    u32 action, bool force_assoc_off,
 700				    const u8 *bssid_override)
 701{
 702	struct iwl_mac_ctx_cmd cmd = {};
 703	struct iwl_mac_data_sta *ctxt_sta;
 704
 705	WARN_ON(vif->type != NL80211_IFTYPE_STATION);
 706
 707	/* Fill the common data for all mac context types */
 708	iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, bssid_override, action);
 709
 710	if (vif->p2p) {
 711		struct ieee80211_p2p_noa_attr *noa =
 712			&vif->bss_conf.p2p_noa_attr;
 713
 714		cmd.p2p_sta.ctwin = cpu_to_le32(noa->oppps_ctwindow &
 715					IEEE80211_P2P_OPPPS_CTWINDOW_MASK);
 716		ctxt_sta = &cmd.p2p_sta.sta;
 717	} else {
 718		ctxt_sta = &cmd.sta;
 719	}
 720
 721	/* We need the dtim_period to set the MAC as associated */
 722	if (vif->bss_conf.assoc && vif->bss_conf.dtim_period &&
 723	    !force_assoc_off) {
 724		u32 dtim_offs;
 725
 726		/*
 727		 * The DTIM count counts down, so when it is N that means N
 728		 * more beacon intervals happen until the DTIM TBTT. Therefore
 729		 * add this to the current time. If that ends up being in the
 730		 * future, the firmware will handle it.
 731		 *
 732		 * Also note that the system_timestamp (which we get here as
 733		 * "sync_device_ts") and TSF timestamp aren't at exactly the
 734		 * same offset in the frame -- the TSF is at the first symbol
 735		 * of the TSF, the system timestamp is at signal acquisition
 736		 * time. This means there's an offset between them of at most
 737		 * a few hundred microseconds (24 * 8 bits + PLCP time gives
 738		 * 384us in the longest case), this is currently not relevant
 739		 * as the firmware wakes up around 2ms before the TBTT.
 740		 */
 741		dtim_offs = vif->bss_conf.sync_dtim_count *
 742				vif->bss_conf.beacon_int;
 743		/* convert TU to usecs */
 744		dtim_offs *= 1024;
 745
 746		ctxt_sta->dtim_tsf =
 747			cpu_to_le64(vif->bss_conf.sync_tsf + dtim_offs);
 748		ctxt_sta->dtim_time =
 749			cpu_to_le32(vif->bss_conf.sync_device_ts + dtim_offs);
 750		ctxt_sta->assoc_beacon_arrive_time =
 751			cpu_to_le32(vif->bss_conf.sync_device_ts);
 752
 753		IWL_DEBUG_INFO(mvm, "DTIM TBTT is 0x%llx/0x%x, offset %d\n",
 754			       le64_to_cpu(ctxt_sta->dtim_tsf),
 755			       le32_to_cpu(ctxt_sta->dtim_time),
 756			       dtim_offs);
 757
 758		ctxt_sta->is_assoc = cpu_to_le32(1);
 759	} else {
 760		ctxt_sta->is_assoc = cpu_to_le32(0);
 761
 762		/* Allow beacons to pass through as long as we are not
 763		 * associated, or we do not have dtim period information.
 764		 */
 765		cmd.filter_flags |= cpu_to_le32(MAC_FILTER_IN_BEACON);
 766	}
 767
 768	ctxt_sta->bi = cpu_to_le32(vif->bss_conf.beacon_int);
 769	ctxt_sta->bi_reciprocal =
 770		cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int));
 771	ctxt_sta->dtim_interval = cpu_to_le32(vif->bss_conf.beacon_int *
 772					      vif->bss_conf.dtim_period);
 773	ctxt_sta->dtim_reciprocal =
 774		cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int *
 775					       vif->bss_conf.dtim_period));
 776
 777	ctxt_sta->listen_interval = cpu_to_le32(mvm->hw->conf.listen_interval);
 778	ctxt_sta->assoc_id = cpu_to_le32(vif->bss_conf.aid);
 779
 780	if (vif->probe_req_reg && vif->bss_conf.assoc && vif->p2p)
 781		cmd.filter_flags |= cpu_to_le32(MAC_FILTER_IN_PROBE_REQUEST);
 782
 783	return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
 784}
 785
 786static int iwl_mvm_mac_ctxt_cmd_listener(struct iwl_mvm *mvm,
 787					 struct ieee80211_vif *vif,
 788					 u32 action)
 789{
 790	struct iwl_mac_ctx_cmd cmd = {};
 791	u32 tfd_queue_msk = BIT(mvm->snif_queue);
 792	int ret;
 793
 794	WARN_ON(vif->type != NL80211_IFTYPE_MONITOR);
 795
 796	iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
 797
 
 
 
 
 
 
 798	cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_PROMISC |
 799				       MAC_FILTER_IN_CONTROL_AND_MGMT |
 800				       MAC_FILTER_IN_BEACON |
 801				       MAC_FILTER_IN_PROBE_REQUEST |
 802				       MAC_FILTER_IN_CRC32);
 803	ieee80211_hw_set(mvm->hw, RX_INCLUDES_FCS);
 804
 805	/* Allocate sniffer station */
 806	ret = iwl_mvm_allocate_int_sta(mvm, &mvm->snif_sta, tfd_queue_msk,
 807				       vif->type, IWL_STA_GENERAL_PURPOSE);
 808	if (ret)
 809		return ret;
 810
 811	return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
 812}
 813
 814static int iwl_mvm_mac_ctxt_cmd_ibss(struct iwl_mvm *mvm,
 815				     struct ieee80211_vif *vif,
 816				     u32 action)
 817{
 818	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
 819	struct iwl_mac_ctx_cmd cmd = {};
 820
 821	WARN_ON(vif->type != NL80211_IFTYPE_ADHOC);
 822
 823	iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
 824
 825	cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_BEACON |
 826				       MAC_FILTER_IN_PROBE_REQUEST);
 827
 828	/* cmd.ibss.beacon_time/cmd.ibss.beacon_tsf are curently ignored */
 829	cmd.ibss.bi = cpu_to_le32(vif->bss_conf.beacon_int);
 830	cmd.ibss.bi_reciprocal =
 831		cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int));
 832
 833	/* TODO: Assumes that the beacon id == mac context id */
 834	cmd.ibss.beacon_template = cpu_to_le32(mvmvif->id);
 835
 836	return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
 837}
 838
 839struct iwl_mvm_go_iterator_data {
 840	bool go_active;
 841};
 842
 843static void iwl_mvm_go_iterator(void *_data, u8 *mac, struct ieee80211_vif *vif)
 844{
 845	struct iwl_mvm_go_iterator_data *data = _data;
 846	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
 847
 848	if (vif->type == NL80211_IFTYPE_AP && vif->p2p &&
 849	    mvmvif->ap_ibss_active)
 850		data->go_active = true;
 851}
 852
 853static int iwl_mvm_mac_ctxt_cmd_p2p_device(struct iwl_mvm *mvm,
 854					   struct ieee80211_vif *vif,
 855					   u32 action)
 856{
 857	struct iwl_mac_ctx_cmd cmd = {};
 858	struct iwl_mvm_go_iterator_data data = {};
 859
 860	WARN_ON(vif->type != NL80211_IFTYPE_P2P_DEVICE);
 861
 862	iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
 863
 864	cmd.protection_flags |= cpu_to_le32(MAC_PROT_FLG_TGG_PROTECT);
 865
 866	/* Override the filter flags to accept only probe requests */
 867	cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_PROBE_REQUEST);
 868
 869	/*
 870	 * This flag should be set to true when the P2P Device is
 871	 * discoverable and there is at least another active P2P GO. Settings
 872	 * this flag will allow the P2P Device to be discoverable on other
 873	 * channels in addition to its listen channel.
 874	 * Note that this flag should not be set in other cases as it opens the
 875	 * Rx filters on all MAC and increases the number of interrupts.
 876	 */
 877	ieee80211_iterate_active_interfaces_atomic(
 878		mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
 879		iwl_mvm_go_iterator, &data);
 880
 881	cmd.p2p_dev.is_disc_extended = cpu_to_le32(data.go_active ? 1 : 0);
 882	return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
 883}
 884
 885static void iwl_mvm_mac_ctxt_set_tim(struct iwl_mvm *mvm,
 886				     __le32 *tim_index, __le32 *tim_size,
 887				     u8 *beacon, u32 frame_size)
 888{
 889	u32 tim_idx;
 890	struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)beacon;
 891
 892	/* The index is relative to frame start but we start looking at the
 893	 * variable-length part of the beacon. */
 894	tim_idx = mgmt->u.beacon.variable - beacon;
 895
 896	/* Parse variable-length elements of beacon to find WLAN_EID_TIM */
 897	while ((tim_idx < (frame_size - 2)) &&
 898			(beacon[tim_idx] != WLAN_EID_TIM))
 899		tim_idx += beacon[tim_idx+1] + 2;
 900
 901	/* If TIM field was found, set variables */
 902	if ((tim_idx < (frame_size - 1)) && (beacon[tim_idx] == WLAN_EID_TIM)) {
 903		*tim_index = cpu_to_le32(tim_idx);
 904		*tim_size = cpu_to_le32((u32)beacon[tim_idx + 1]);
 905	} else {
 906		IWL_WARN(mvm, "Unable to find TIM Element in beacon\n");
 907	}
 908}
 909
 910static u32 iwl_mvm_find_ie_offset(u8 *beacon, u8 eid, u32 frame_size)
 911{
 912	struct ieee80211_mgmt *mgmt = (void *)beacon;
 913	const u8 *ie;
 914
 915	if (WARN_ON_ONCE(frame_size <= (mgmt->u.beacon.variable - beacon)))
 916		return 0;
 917
 918	frame_size -= mgmt->u.beacon.variable - beacon;
 919
 920	ie = cfg80211_find_ie(eid, mgmt->u.beacon.variable, frame_size);
 921	if (!ie)
 922		return 0;
 923
 924	return ie - beacon;
 925}
 926
 927static u8 iwl_mvm_mac_ctxt_get_lowest_rate(struct ieee80211_tx_info *info,
 928					   struct ieee80211_vif *vif)
 929{
 930	u8 rate;
 931
 932	if (info->band == NL80211_BAND_5GHZ || vif->p2p)
 933		rate = IWL_FIRST_OFDM_RATE;
 934	else
 935		rate = IWL_FIRST_CCK_RATE;
 936
 937	return rate;
 938}
 939
 940static void iwl_mvm_mac_ctxt_set_tx(struct iwl_mvm *mvm,
 941				    struct ieee80211_vif *vif,
 942				    struct sk_buff *beacon,
 943				    struct iwl_tx_cmd *tx)
 944{
 945	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
 
 
 
 
 
 
 
 
 946	struct ieee80211_tx_info *info;
 947	u8 rate;
 948	u32 tx_flags;
 
 
 
 949
 
 
 
 
 
 950	info = IEEE80211_SKB_CB(beacon);
 951
 952	/* Set up TX command fields */
 953	tx->len = cpu_to_le16((u16)beacon->len);
 954	tx->sta_id = mvmvif->bcast_sta.sta_id;
 955	tx->life_time = cpu_to_le32(TX_CMD_LIFE_TIME_INFINITE);
 956	tx_flags = TX_CMD_FLG_SEQ_CTL | TX_CMD_FLG_TSF;
 957	tx_flags |=
 958		iwl_mvm_bt_coex_tx_prio(mvm, (void *)beacon->data, info, 0) <<
 959						TX_CMD_FLG_BT_PRIO_POS;
 960	tx->tx_flags = cpu_to_le32(tx_flags);
 961
 962	if (!fw_has_capa(&mvm->fw->ucode_capa,
 963			 IWL_UCODE_TLV_CAPA_BEACON_ANT_SELECTION)) {
 964		mvm->mgmt_last_antenna_idx =
 965			iwl_mvm_next_antenna(mvm, iwl_mvm_get_valid_tx_ant(mvm),
 966					     mvm->mgmt_last_antenna_idx);
 967	}
 968
 969	tx->rate_n_flags =
 970		cpu_to_le32(BIT(mvm->mgmt_last_antenna_idx) <<
 971			    RATE_MCS_ANT_POS);
 972
 973	rate = iwl_mvm_mac_ctxt_get_lowest_rate(info, vif);
 974
 975	tx->rate_n_flags |= cpu_to_le32(iwl_mvm_mac80211_idx_to_hwrate(rate));
 976	if (rate == IWL_FIRST_CCK_RATE)
 977		tx->rate_n_flags |= cpu_to_le32(RATE_MCS_CCK_MSK);
 
 
 
 
 978
 979}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 980
 981static int iwl_mvm_mac_ctxt_send_beacon_cmd(struct iwl_mvm *mvm,
 982					    struct sk_buff *beacon,
 983					    void *data, int len)
 984{
 985	struct iwl_host_cmd cmd = {
 986		.id = BEACON_TEMPLATE_CMD,
 987		.flags = CMD_ASYNC,
 988	};
 989
 990	cmd.len[0] = len;
 991	cmd.data[0] = data;
 992	cmd.dataflags[0] = 0;
 993	cmd.len[1] = beacon->len;
 994	cmd.data[1] = beacon->data;
 995	cmd.dataflags[1] = IWL_HCMD_DFL_DUP;
 996
 997	return iwl_mvm_send_cmd(mvm, &cmd);
 998}
 999
1000static int iwl_mvm_mac_ctxt_send_beacon_v6(struct iwl_mvm *mvm,
1001					   struct ieee80211_vif *vif,
1002					   struct sk_buff *beacon)
1003{
1004	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1005	struct iwl_mac_beacon_cmd_v6 beacon_cmd = {};
1006
1007	iwl_mvm_mac_ctxt_set_tx(mvm, vif, beacon, &beacon_cmd.tx);
1008
1009	beacon_cmd.template_id = cpu_to_le32((u32)mvmvif->id);
1010
1011	if (vif->type == NL80211_IFTYPE_AP)
1012		iwl_mvm_mac_ctxt_set_tim(mvm, &beacon_cmd.tim_idx,
1013					 &beacon_cmd.tim_size,
1014					 beacon->data, beacon->len);
1015
1016	return iwl_mvm_mac_ctxt_send_beacon_cmd(mvm, beacon, &beacon_cmd,
1017						sizeof(beacon_cmd));
1018}
1019
1020static int iwl_mvm_mac_ctxt_send_beacon_v7(struct iwl_mvm *mvm,
1021					   struct ieee80211_vif *vif,
1022					   struct sk_buff *beacon)
1023{
1024	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1025	struct iwl_mac_beacon_cmd_v7 beacon_cmd = {};
1026
1027	iwl_mvm_mac_ctxt_set_tx(mvm, vif, beacon, &beacon_cmd.tx);
1028
1029	beacon_cmd.template_id = cpu_to_le32((u32)mvmvif->id);
1030
1031	if (vif->type == NL80211_IFTYPE_AP)
1032		iwl_mvm_mac_ctxt_set_tim(mvm, &beacon_cmd.tim_idx,
1033					 &beacon_cmd.tim_size,
1034					 beacon->data, beacon->len);
1035
1036	beacon_cmd.csa_offset =
1037		cpu_to_le32(iwl_mvm_find_ie_offset(beacon->data,
1038						   WLAN_EID_CHANNEL_SWITCH,
1039						   beacon->len));
1040	beacon_cmd.ecsa_offset =
1041		cpu_to_le32(iwl_mvm_find_ie_offset(beacon->data,
1042						   WLAN_EID_EXT_CHANSWITCH_ANN,
1043						   beacon->len));
1044
1045	return iwl_mvm_mac_ctxt_send_beacon_cmd(mvm, beacon, &beacon_cmd,
1046						sizeof(beacon_cmd));
1047}
1048
1049static int iwl_mvm_mac_ctxt_send_beacon_v9(struct iwl_mvm *mvm,
1050					   struct ieee80211_vif *vif,
1051					   struct sk_buff *beacon)
1052{
1053	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1054	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(beacon);
1055	struct iwl_mac_beacon_cmd beacon_cmd = {};
1056	u8 rate = iwl_mvm_mac_ctxt_get_lowest_rate(info, vif);
1057	u16 flags;
1058
1059	flags = iwl_mvm_mac80211_idx_to_hwrate(rate);
1060
1061	if (rate == IWL_FIRST_CCK_RATE)
1062		flags |= IWL_MAC_BEACON_CCK;
1063
1064	beacon_cmd.flags = cpu_to_le16(flags);
1065	beacon_cmd.byte_cnt = cpu_to_le16((u16)beacon->len);
1066	beacon_cmd.template_id = cpu_to_le32((u32)mvmvif->id);
1067
1068	if (vif->type == NL80211_IFTYPE_AP)
1069		iwl_mvm_mac_ctxt_set_tim(mvm, &beacon_cmd.tim_idx,
1070					 &beacon_cmd.tim_size,
1071					 beacon->data, beacon->len);
1072
1073	beacon_cmd.csa_offset =
1074		cpu_to_le32(iwl_mvm_find_ie_offset(beacon->data,
1075						   WLAN_EID_CHANNEL_SWITCH,
1076						   beacon->len));
1077	beacon_cmd.ecsa_offset =
1078		cpu_to_le32(iwl_mvm_find_ie_offset(beacon->data,
1079						   WLAN_EID_EXT_CHANSWITCH_ANN,
1080						   beacon->len));
1081
1082	return iwl_mvm_mac_ctxt_send_beacon_cmd(mvm, beacon, &beacon_cmd,
1083						sizeof(beacon_cmd));
1084}
1085
1086static int iwl_mvm_mac_ctxt_send_beacon(struct iwl_mvm *mvm,
1087					struct ieee80211_vif *vif,
1088					struct sk_buff *beacon)
1089{
1090	if (WARN_ON(!beacon))
1091		return -EINVAL;
1092
1093	if (!fw_has_capa(&mvm->fw->ucode_capa,
1094			 IWL_UCODE_TLV_CAPA_CSA_AND_TBTT_OFFLOAD))
1095		return iwl_mvm_mac_ctxt_send_beacon_v6(mvm, vif, beacon);
1096
1097	if (fw_has_api(&mvm->fw->ucode_capa,
1098		       IWL_UCODE_TLV_API_NEW_BEACON_TEMPLATE))
1099		return iwl_mvm_mac_ctxt_send_beacon_v9(mvm, vif, beacon);
1100
1101	return iwl_mvm_mac_ctxt_send_beacon_v7(mvm, vif, beacon);
1102}
1103
1104/* The beacon template for the AP/GO/IBSS has changed and needs update */
1105int iwl_mvm_mac_ctxt_beacon_changed(struct iwl_mvm *mvm,
1106				    struct ieee80211_vif *vif)
1107{
1108	struct sk_buff *beacon;
1109	int ret;
1110
1111	WARN_ON(vif->type != NL80211_IFTYPE_AP &&
1112		vif->type != NL80211_IFTYPE_ADHOC);
1113
1114	beacon = ieee80211_beacon_get_template(mvm->hw, vif, NULL);
1115	if (!beacon)
1116		return -ENOMEM;
1117
1118	ret = iwl_mvm_mac_ctxt_send_beacon(mvm, vif, beacon);
1119	dev_kfree_skb(beacon);
1120	return ret;
1121}
1122
1123struct iwl_mvm_mac_ap_iterator_data {
1124	struct iwl_mvm *mvm;
1125	struct ieee80211_vif *vif;
1126	u32 beacon_device_ts;
1127	u16 beacon_int;
1128};
1129
1130/* Find the beacon_device_ts and beacon_int for a managed interface */
1131static void iwl_mvm_mac_ap_iterator(void *_data, u8 *mac,
1132				    struct ieee80211_vif *vif)
1133{
1134	struct iwl_mvm_mac_ap_iterator_data *data = _data;
1135
1136	if (vif->type != NL80211_IFTYPE_STATION || !vif->bss_conf.assoc)
1137		return;
1138
1139	/* Station client has higher priority over P2P client*/
1140	if (vif->p2p && data->beacon_device_ts)
1141		return;
1142
1143	data->beacon_device_ts = vif->bss_conf.sync_device_ts;
1144	data->beacon_int = vif->bss_conf.beacon_int;
1145}
1146
1147/*
1148 * Fill the specific data for mac context of type AP of P2P GO
1149 */
1150static void iwl_mvm_mac_ctxt_cmd_fill_ap(struct iwl_mvm *mvm,
1151					 struct ieee80211_vif *vif,
1152					 struct iwl_mac_ctx_cmd *cmd,
1153					 struct iwl_mac_data_ap *ctxt_ap,
1154					 bool add)
1155{
1156	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1157	struct iwl_mvm_mac_ap_iterator_data data = {
1158		.mvm = mvm,
1159		.vif = vif,
1160		.beacon_device_ts = 0
1161	};
1162
1163	/* in AP mode, the MCAST FIFO takes the EDCA params from VO */
1164	cmd->ac[IWL_MVM_TX_FIFO_VO].fifos_mask |= BIT(IWL_MVM_TX_FIFO_MCAST);
1165
1166	/*
1167	 * in AP mode, pass probe requests and beacons from other APs
1168	 * (needed for ht protection); when there're no any associated
1169	 * station don't ask FW to pass beacons to prevent unnecessary
1170	 * wake-ups.
1171	 */
1172	cmd->filter_flags |= cpu_to_le32(MAC_FILTER_IN_PROBE_REQUEST);
1173	if (mvmvif->ap_assoc_sta_count || !mvm->drop_bcn_ap_mode) {
1174		cmd->filter_flags |= cpu_to_le32(MAC_FILTER_IN_BEACON);
1175		IWL_DEBUG_HC(mvm, "Asking FW to pass beacons\n");
1176	} else {
1177		IWL_DEBUG_HC(mvm, "No need to receive beacons\n");
1178	}
1179
1180	ctxt_ap->bi = cpu_to_le32(vif->bss_conf.beacon_int);
1181	ctxt_ap->bi_reciprocal =
1182		cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int));
1183	ctxt_ap->dtim_interval = cpu_to_le32(vif->bss_conf.beacon_int *
1184					     vif->bss_conf.dtim_period);
1185	ctxt_ap->dtim_reciprocal =
1186		cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int *
1187					       vif->bss_conf.dtim_period));
1188
1189	if (!fw_has_api(&mvm->fw->ucode_capa,
1190			IWL_UCODE_TLV_API_STA_TYPE))
1191		ctxt_ap->mcast_qid = cpu_to_le32(vif->cab_queue);
1192
1193	/*
1194	 * Only set the beacon time when the MAC is being added, when we
1195	 * just modify the MAC then we should keep the time -- the firmware
1196	 * can otherwise have a "jumping" TBTT.
1197	 */
1198	if (add) {
1199		/*
1200		 * If there is a station/P2P client interface which is
1201		 * associated, set the AP's TBTT far enough from the station's
1202		 * TBTT. Otherwise, set it to the current system time
1203		 */
1204		ieee80211_iterate_active_interfaces_atomic(
1205			mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
1206			iwl_mvm_mac_ap_iterator, &data);
1207
1208		if (data.beacon_device_ts) {
1209			u32 rand = (prandom_u32() % (64 - 36)) + 36;
1210			mvmvif->ap_beacon_time = data.beacon_device_ts +
1211				ieee80211_tu_to_usec(data.beacon_int * rand /
1212						     100);
1213		} else {
1214			mvmvif->ap_beacon_time =
1215				iwl_read_prph(mvm->trans,
1216					      DEVICE_SYSTEM_TIME_REG);
1217		}
1218	}
1219
1220	ctxt_ap->beacon_time = cpu_to_le32(mvmvif->ap_beacon_time);
1221	ctxt_ap->beacon_tsf = 0; /* unused */
1222
1223	/* TODO: Assume that the beacon id == mac context id */
1224	ctxt_ap->beacon_template = cpu_to_le32(mvmvif->id);
1225}
1226
1227static int iwl_mvm_mac_ctxt_cmd_ap(struct iwl_mvm *mvm,
1228				   struct ieee80211_vif *vif,
1229				   u32 action)
1230{
1231	struct iwl_mac_ctx_cmd cmd = {};
1232
1233	WARN_ON(vif->type != NL80211_IFTYPE_AP || vif->p2p);
1234
1235	/* Fill the common data for all mac context types */
1236	iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
1237
1238	/* Fill the data specific for ap mode */
1239	iwl_mvm_mac_ctxt_cmd_fill_ap(mvm, vif, &cmd, &cmd.ap,
1240				     action == FW_CTXT_ACTION_ADD);
1241
1242	return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
1243}
1244
1245static int iwl_mvm_mac_ctxt_cmd_go(struct iwl_mvm *mvm,
1246				   struct ieee80211_vif *vif,
1247				   u32 action)
1248{
1249	struct iwl_mac_ctx_cmd cmd = {};
1250	struct ieee80211_p2p_noa_attr *noa = &vif->bss_conf.p2p_noa_attr;
1251
1252	WARN_ON(vif->type != NL80211_IFTYPE_AP || !vif->p2p);
1253
1254	/* Fill the common data for all mac context types */
1255	iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
1256
1257	/* Fill the data specific for GO mode */
1258	iwl_mvm_mac_ctxt_cmd_fill_ap(mvm, vif, &cmd, &cmd.go.ap,
1259				     action == FW_CTXT_ACTION_ADD);
1260
1261	cmd.go.ctwin = cpu_to_le32(noa->oppps_ctwindow &
1262					IEEE80211_P2P_OPPPS_CTWINDOW_MASK);
1263	cmd.go.opp_ps_enabled =
1264			cpu_to_le32(!!(noa->oppps_ctwindow &
1265					IEEE80211_P2P_OPPPS_ENABLE_BIT));
1266
1267	return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
1268}
1269
1270static int iwl_mvm_mac_ctx_send(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1271				u32 action, bool force_assoc_off,
1272				const u8 *bssid_override)
1273{
1274	switch (vif->type) {
1275	case NL80211_IFTYPE_STATION:
1276		return iwl_mvm_mac_ctxt_cmd_sta(mvm, vif, action,
1277						force_assoc_off,
1278						bssid_override);
1279		break;
1280	case NL80211_IFTYPE_AP:
1281		if (!vif->p2p)
1282			return iwl_mvm_mac_ctxt_cmd_ap(mvm, vif, action);
1283		else
1284			return iwl_mvm_mac_ctxt_cmd_go(mvm, vif, action);
1285		break;
1286	case NL80211_IFTYPE_MONITOR:
1287		return iwl_mvm_mac_ctxt_cmd_listener(mvm, vif, action);
1288	case NL80211_IFTYPE_P2P_DEVICE:
1289		return iwl_mvm_mac_ctxt_cmd_p2p_device(mvm, vif, action);
1290	case NL80211_IFTYPE_ADHOC:
1291		return iwl_mvm_mac_ctxt_cmd_ibss(mvm, vif, action);
1292	default:
1293		break;
1294	}
1295
1296	return -EOPNOTSUPP;
1297}
1298
1299int iwl_mvm_mac_ctxt_add(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1300{
1301	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1302	int ret;
1303
1304	if (WARN_ONCE(mvmvif->uploaded, "Adding active MAC %pM/%d\n",
1305		      vif->addr, ieee80211_vif_type_p2p(vif)))
1306		return -EIO;
1307
1308	ret = iwl_mvm_mac_ctx_send(mvm, vif, FW_CTXT_ACTION_ADD,
1309				   true, NULL);
1310	if (ret)
1311		return ret;
1312
1313	/* will only do anything at resume from D3 time */
1314	iwl_mvm_set_last_nonqos_seq(mvm, vif);
1315
1316	mvmvif->uploaded = true;
1317	return 0;
1318}
1319
1320int iwl_mvm_mac_ctxt_changed(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1321			     bool force_assoc_off, const u8 *bssid_override)
1322{
1323	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1324
1325	if (WARN_ONCE(!mvmvif->uploaded, "Changing inactive MAC %pM/%d\n",
1326		      vif->addr, ieee80211_vif_type_p2p(vif)))
1327		return -EIO;
1328
1329	return iwl_mvm_mac_ctx_send(mvm, vif, FW_CTXT_ACTION_MODIFY,
1330				    force_assoc_off, bssid_override);
1331}
1332
1333int iwl_mvm_mac_ctxt_remove(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1334{
1335	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1336	struct iwl_mac_ctx_cmd cmd;
1337	int ret;
1338
1339	if (WARN_ONCE(!mvmvif->uploaded, "Removing inactive MAC %pM/%d\n",
1340		      vif->addr, ieee80211_vif_type_p2p(vif)))
1341		return -EIO;
1342
1343	memset(&cmd, 0, sizeof(cmd));
1344
1345	cmd.id_and_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
1346							   mvmvif->color));
1347	cmd.action = cpu_to_le32(FW_CTXT_ACTION_REMOVE);
1348
1349	ret = iwl_mvm_send_cmd_pdu(mvm, MAC_CONTEXT_CMD, 0,
1350				   sizeof(cmd), &cmd);
1351	if (ret) {
1352		IWL_ERR(mvm, "Failed to remove MAC context: %d\n", ret);
1353		return ret;
1354	}
1355
1356	mvmvif->uploaded = false;
1357
1358	if (vif->type == NL80211_IFTYPE_MONITOR) {
1359		__clear_bit(IEEE80211_HW_RX_INCLUDES_FCS, mvm->hw->flags);
1360		iwl_mvm_dealloc_snif_sta(mvm);
1361	}
1362
1363	return 0;
1364}
1365
1366static void iwl_mvm_csa_count_down(struct iwl_mvm *mvm,
1367				   struct ieee80211_vif *csa_vif, u32 gp2,
1368				   bool tx_success)
1369{
1370	struct iwl_mvm_vif *mvmvif =
1371			iwl_mvm_vif_from_mac80211(csa_vif);
1372
1373	/* Don't start to countdown from a failed beacon */
1374	if (!tx_success && !mvmvif->csa_countdown)
1375		return;
1376
1377	mvmvif->csa_countdown = true;
1378
1379	if (!ieee80211_csa_is_complete(csa_vif)) {
1380		int c = ieee80211_csa_update_counter(csa_vif);
1381
1382		iwl_mvm_mac_ctxt_beacon_changed(mvm, csa_vif);
1383		if (csa_vif->p2p &&
1384		    !iwl_mvm_te_scheduled(&mvmvif->time_event_data) && gp2 &&
1385		    tx_success) {
1386			u32 rel_time = (c + 1) *
1387				       csa_vif->bss_conf.beacon_int -
1388				       IWL_MVM_CHANNEL_SWITCH_TIME_GO;
1389			u32 apply_time = gp2 + rel_time * 1024;
1390
1391			iwl_mvm_schedule_csa_period(mvm, csa_vif,
1392					 IWL_MVM_CHANNEL_SWITCH_TIME_GO -
1393					 IWL_MVM_CHANNEL_SWITCH_MARGIN,
1394					 apply_time);
1395		}
1396	} else if (!iwl_mvm_te_scheduled(&mvmvif->time_event_data)) {
1397		/* we don't have CSA NoA scheduled yet, switch now */
1398		ieee80211_csa_finish(csa_vif);
1399		RCU_INIT_POINTER(mvm->csa_vif, NULL);
1400	}
1401}
1402
1403void iwl_mvm_rx_beacon_notif(struct iwl_mvm *mvm,
1404			     struct iwl_rx_cmd_buffer *rxb)
1405{
1406	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1407	struct iwl_extended_beacon_notif *beacon = (void *)pkt->data;
1408	struct iwl_mvm_tx_resp *beacon_notify_hdr;
1409	struct ieee80211_vif *csa_vif;
1410	struct ieee80211_vif *tx_blocked_vif;
1411	struct agg_tx_status *agg_status;
1412	u16 status;
1413
1414	lockdep_assert_held(&mvm->mutex);
1415
1416	beacon_notify_hdr = &beacon->beacon_notify_hdr;
1417	mvm->ap_last_beacon_gp2 = le32_to_cpu(beacon->gp2);
1418	mvm->ibss_manager = beacon->ibss_mgr_status != 0;
1419
1420	agg_status = iwl_mvm_get_agg_status(mvm, beacon_notify_hdr);
1421	status = le16_to_cpu(agg_status->status) & TX_STATUS_MSK;
1422	IWL_DEBUG_RX(mvm,
1423		     "beacon status %#x retries:%d tsf:0x%16llX gp2:0x%X rate:%d\n",
1424		     status, beacon_notify_hdr->failure_frame,
1425		     le64_to_cpu(beacon->tsf),
1426		     mvm->ap_last_beacon_gp2,
1427		     le32_to_cpu(beacon_notify_hdr->initial_rate));
1428
1429	csa_vif = rcu_dereference_protected(mvm->csa_vif,
1430					    lockdep_is_held(&mvm->mutex));
1431	if (unlikely(csa_vif && csa_vif->csa_active))
1432		iwl_mvm_csa_count_down(mvm, csa_vif, mvm->ap_last_beacon_gp2,
1433				       (status == TX_STATUS_SUCCESS));
1434
1435	tx_blocked_vif = rcu_dereference_protected(mvm->csa_tx_blocked_vif,
1436						lockdep_is_held(&mvm->mutex));
1437	if (unlikely(tx_blocked_vif)) {
1438		struct iwl_mvm_vif *mvmvif =
1439			iwl_mvm_vif_from_mac80211(tx_blocked_vif);
1440
1441		/*
1442		 * The channel switch is started and we have blocked the
1443		 * stations. If this is the first beacon (the timeout wasn't
1444		 * set), set the unblock timeout, otherwise countdown
1445		 */
1446		if (!mvm->csa_tx_block_bcn_timeout)
1447			mvm->csa_tx_block_bcn_timeout =
1448				IWL_MVM_CS_UNBLOCK_TX_TIMEOUT;
1449		else
1450			mvm->csa_tx_block_bcn_timeout--;
1451
1452		/* Check if the timeout is expired, and unblock tx */
1453		if (mvm->csa_tx_block_bcn_timeout == 0) {
1454			iwl_mvm_modify_all_sta_disable_tx(mvm, mvmvif, false);
1455			RCU_INIT_POINTER(mvm->csa_tx_blocked_vif, NULL);
1456		}
1457	}
1458}
1459
1460static void iwl_mvm_beacon_loss_iterator(void *_data, u8 *mac,
1461					 struct ieee80211_vif *vif)
1462{
1463	struct iwl_missed_beacons_notif *missed_beacons = _data;
1464	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1465	struct iwl_mvm *mvm = mvmvif->mvm;
1466	struct iwl_fw_dbg_trigger_missed_bcon *bcon_trig;
1467	struct iwl_fw_dbg_trigger_tlv *trigger;
1468	u32 stop_trig_missed_bcon, stop_trig_missed_bcon_since_rx;
1469	u32 rx_missed_bcon, rx_missed_bcon_since_rx;
1470
1471	if (mvmvif->id != (u16)le32_to_cpu(missed_beacons->mac_id))
1472		return;
1473
1474	rx_missed_bcon = le32_to_cpu(missed_beacons->consec_missed_beacons);
1475	rx_missed_bcon_since_rx =
1476		le32_to_cpu(missed_beacons->consec_missed_beacons_since_last_rx);
1477	/*
1478	 * TODO: the threshold should be adjusted based on latency conditions,
1479	 * and/or in case of a CS flow on one of the other AP vifs.
1480	 */
1481	if (le32_to_cpu(missed_beacons->consec_missed_beacons_since_last_rx) >
1482	     IWL_MVM_MISSED_BEACONS_THRESHOLD)
1483		ieee80211_beacon_loss(vif);
1484
1485	if (!iwl_fw_dbg_trigger_enabled(mvm->fw,
1486					FW_DBG_TRIGGER_MISSED_BEACONS))
1487		return;
1488
1489	trigger = iwl_fw_dbg_get_trigger(mvm->fw,
1490					 FW_DBG_TRIGGER_MISSED_BEACONS);
1491	bcon_trig = (void *)trigger->data;
1492	stop_trig_missed_bcon = le32_to_cpu(bcon_trig->stop_consec_missed_bcon);
1493	stop_trig_missed_bcon_since_rx =
1494		le32_to_cpu(bcon_trig->stop_consec_missed_bcon_since_rx);
1495
1496	/* TODO: implement start trigger */
1497
1498	if (!iwl_fw_dbg_trigger_check_stop(&mvm->fwrt,
1499					   ieee80211_vif_to_wdev(vif),
1500					   trigger))
1501		return;
1502
1503	if (rx_missed_bcon_since_rx >= stop_trig_missed_bcon_since_rx ||
1504	    rx_missed_bcon >= stop_trig_missed_bcon)
1505		iwl_fw_dbg_collect_trig(&mvm->fwrt, trigger, NULL);
1506}
1507
1508void iwl_mvm_rx_missed_beacons_notif(struct iwl_mvm *mvm,
1509				     struct iwl_rx_cmd_buffer *rxb)
1510{
1511	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1512	struct iwl_missed_beacons_notif *mb = (void *)pkt->data;
1513
1514	IWL_DEBUG_INFO(mvm,
1515		       "missed bcn mac_id=%u, consecutive=%u (%u, %u, %u)\n",
1516		       le32_to_cpu(mb->mac_id),
1517		       le32_to_cpu(mb->consec_missed_beacons),
1518		       le32_to_cpu(mb->consec_missed_beacons_since_last_rx),
1519		       le32_to_cpu(mb->num_recvd_beacons),
1520		       le32_to_cpu(mb->num_expected_beacons));
1521
1522	ieee80211_iterate_active_interfaces_atomic(mvm->hw,
1523						   IEEE80211_IFACE_ITER_NORMAL,
1524						   iwl_mvm_beacon_loss_iterator,
1525						   mb);
1526}
1527
1528void iwl_mvm_rx_stored_beacon_notif(struct iwl_mvm *mvm,
1529				    struct iwl_rx_cmd_buffer *rxb)
1530{
1531	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1532	struct iwl_stored_beacon_notif *sb = (void *)pkt->data;
1533	struct ieee80211_rx_status rx_status;
1534	struct sk_buff *skb;
1535	u32 size = le32_to_cpu(sb->byte_count);
1536
1537	if (size == 0)
1538		return;
1539
1540	skb = alloc_skb(size, GFP_ATOMIC);
1541	if (!skb) {
1542		IWL_ERR(mvm, "alloc_skb failed\n");
1543		return;
1544	}
1545
1546	/* update rx_status according to the notification's metadata */
1547	memset(&rx_status, 0, sizeof(rx_status));
1548	rx_status.mactime = le64_to_cpu(sb->tsf);
1549	/* TSF as indicated by the firmware  is at INA time */
1550	rx_status.flag |= RX_FLAG_MACTIME_PLCP_START;
1551	rx_status.device_timestamp = le32_to_cpu(sb->system_time);
1552	rx_status.band =
1553		(sb->band & cpu_to_le16(RX_RES_PHY_FLAGS_BAND_24)) ?
1554				NL80211_BAND_2GHZ : NL80211_BAND_5GHZ;
1555	rx_status.freq =
1556		ieee80211_channel_to_frequency(le16_to_cpu(sb->channel),
1557					       rx_status.band);
1558
1559	/* copy the data */
1560	skb_put_data(skb, sb->data, size);
1561	memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
1562
1563	/* pass it as regular rx to mac80211 */
1564	ieee80211_rx_napi(mvm->hw, NULL, skb, NULL);
1565}
1566
1567void iwl_mvm_channel_switch_noa_notif(struct iwl_mvm *mvm,
1568				      struct iwl_rx_cmd_buffer *rxb)
1569{
1570	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1571	struct iwl_channel_switch_noa_notif *notif = (void *)pkt->data;
1572	struct ieee80211_vif *csa_vif;
1573	struct iwl_mvm_vif *mvmvif;
1574	int len = iwl_rx_packet_payload_len(pkt);
1575	u32 id_n_color;
1576
1577	if (WARN_ON_ONCE(len < sizeof(*notif)))
1578		return;
1579
1580	rcu_read_lock();
1581
1582	csa_vif = rcu_dereference(mvm->csa_vif);
1583	if (WARN_ON(!csa_vif || !csa_vif->csa_active))
1584		goto out_unlock;
1585
1586	id_n_color = le32_to_cpu(notif->id_and_color);
1587
1588	mvmvif = iwl_mvm_vif_from_mac80211(csa_vif);
1589	if (WARN(FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color) != id_n_color,
1590		 "channel switch noa notification on unexpected vif (csa_vif=%d, notif=%d)",
1591		 FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color), id_n_color))
1592		goto out_unlock;
1593
1594	IWL_DEBUG_INFO(mvm, "Channel Switch Started Notification\n");
1595
1596	schedule_delayed_work(&mvm->cs_tx_unblock_dwork,
1597			      msecs_to_jiffies(IWL_MVM_CS_UNBLOCK_TX_TIMEOUT *
1598					       csa_vif->bss_conf.beacon_int));
1599
1600	ieee80211_csa_finish(csa_vif);
1601
1602	rcu_read_unlock();
1603
1604	RCU_INIT_POINTER(mvm->csa_vif, NULL);
1605
1606	return;
1607
1608out_unlock:
1609	rcu_read_unlock();
1610}
v4.10.11
   1/******************************************************************************
   2 *
   3 * This file is provided under a dual BSD/GPLv2 license.  When using or
   4 * redistributing this file, you may do so under either license.
   5 *
   6 * GPL LICENSE SUMMARY
   7 *
   8 * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
   9 * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
  10 * Copyright(c) 2015 - 2016 Intel Deutschland GmbH
  11 *
  12 * This program is free software; you can redistribute it and/or modify
  13 * it under the terms of version 2 of the GNU General Public License as
  14 * published by the Free Software Foundation.
  15 *
  16 * This program is distributed in the hope that it will be useful, but
  17 * WITHOUT ANY WARRANTY; without even the implied warranty of
  18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  19 * General Public License for more details.
  20 *
  21 * You should have received a copy of the GNU General Public License
  22 * along with this program; if not, write to the Free Software
  23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
  24 * USA
  25 *
  26 * The full GNU General Public License is included in this distribution
  27 * in the file called COPYING.
  28 *
  29 * Contact Information:
  30 *  Intel Linux Wireless <linuxwifi@intel.com>
  31 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  32 *
  33 * BSD LICENSE
  34 *
  35 * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
  36 * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
  37 * Copyright(c) 2015 - 2016 Intel Deutschland GmbH
  38 * All rights reserved.
  39 *
  40 * Redistribution and use in source and binary forms, with or without
  41 * modification, are permitted provided that the following conditions
  42 * are met:
  43 *
  44 *  * Redistributions of source code must retain the above copyright
  45 *    notice, this list of conditions and the following disclaimer.
  46 *  * Redistributions in binary form must reproduce the above copyright
  47 *    notice, this list of conditions and the following disclaimer in
  48 *    the documentation and/or other materials provided with the
  49 *    distribution.
  50 *  * Neither the name Intel Corporation nor the names of its
  51 *    contributors may be used to endorse or promote products derived
  52 *    from this software without specific prior written permission.
  53 *
  54 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  55 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  56 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  57 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  58 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  59 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  60 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  61 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  62 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  63 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  64 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  65 *
  66 *****************************************************************************/
  67
  68#include <linux/etherdevice.h>
  69#include <net/mac80211.h>
  70#include "iwl-io.h"
  71#include "iwl-prph.h"
  72#include "fw-api.h"
  73#include "mvm.h"
  74#include "time-event.h"
  75#include "fw-dbg.h"
  76
  77const u8 iwl_mvm_ac_to_tx_fifo[] = {
  78	IWL_MVM_TX_FIFO_VO,
  79	IWL_MVM_TX_FIFO_VI,
  80	IWL_MVM_TX_FIFO_BE,
  81	IWL_MVM_TX_FIFO_BK,
  82};
  83
 
 
 
 
 
 
 
  84struct iwl_mvm_mac_iface_iterator_data {
  85	struct iwl_mvm *mvm;
  86	struct ieee80211_vif *vif;
  87	unsigned long available_mac_ids[BITS_TO_LONGS(NUM_MAC_INDEX_DRIVER)];
  88	unsigned long available_tsf_ids[BITS_TO_LONGS(NUM_TSF_IDS)];
  89	enum iwl_tsf_id preferred_tsf;
  90	bool found_vif;
  91};
  92
  93struct iwl_mvm_hw_queues_iface_iterator_data {
  94	struct ieee80211_vif *exclude_vif;
  95	unsigned long used_hw_queues;
  96};
  97
  98static void iwl_mvm_mac_tsf_id_iter(void *_data, u8 *mac,
  99				    struct ieee80211_vif *vif)
 100{
 101	struct iwl_mvm_mac_iface_iterator_data *data = _data;
 102	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
 103	u16 min_bi;
 104
 105	/* Skip the interface for which we are trying to assign a tsf_id  */
 106	if (vif == data->vif)
 107		return;
 108
 109	/*
 110	 * The TSF is a hardware/firmware resource, there are 4 and
 111	 * the driver should assign and free them as needed. However,
 112	 * there are cases where 2 MACs should share the same TSF ID
 113	 * for the purpose of clock sync, an optimization to avoid
 114	 * clock drift causing overlapping TBTTs/DTIMs for a GO and
 115	 * client in the system.
 116	 *
 117	 * The firmware will decide according to the MAC type which
 118	 * will be the master and slave. Clients that need to sync
 119	 * with a remote station will be the master, and an AP or GO
 120	 * will be the slave.
 121	 *
 122	 * Depending on the new interface type it can be slaved to
 123	 * or become the master of an existing interface.
 124	 */
 125	switch (data->vif->type) {
 126	case NL80211_IFTYPE_STATION:
 127		/*
 128		 * The new interface is a client, so if the one we're iterating
 129		 * is an AP, and the beacon interval of the AP is a multiple or
 130		 * divisor of the beacon interval of the client, the same TSF
 131		 * should be used to avoid drift between the new client and
 132		 * existing AP. The existing AP will get drift updates from the
 133		 * new client context in this case.
 134		 */
 135		if (vif->type != NL80211_IFTYPE_AP ||
 136		    data->preferred_tsf != NUM_TSF_IDS ||
 137		    !test_bit(mvmvif->tsf_id, data->available_tsf_ids))
 138			break;
 139
 140		min_bi = min(data->vif->bss_conf.beacon_int,
 141			     vif->bss_conf.beacon_int);
 142
 143		if (!min_bi)
 144			break;
 145
 146		if ((data->vif->bss_conf.beacon_int -
 147		     vif->bss_conf.beacon_int) % min_bi == 0) {
 148			data->preferred_tsf = mvmvif->tsf_id;
 149			return;
 150		}
 151		break;
 152
 153	case NL80211_IFTYPE_AP:
 154		/*
 155		 * The new interface is AP/GO, so if its beacon interval is a
 156		 * multiple or a divisor of the beacon interval of an existing
 157		 * interface, it should get drift updates from an existing
 158		 * client or use the same TSF as an existing GO. There's no
 159		 * drift between TSFs internally but if they used different
 160		 * TSFs then a new client MAC could update one of them and
 161		 * cause drift that way.
 162		 */
 163		if ((vif->type != NL80211_IFTYPE_AP &&
 164		     vif->type != NL80211_IFTYPE_STATION) ||
 165		    data->preferred_tsf != NUM_TSF_IDS ||
 166		    !test_bit(mvmvif->tsf_id, data->available_tsf_ids))
 167			break;
 168
 169		min_bi = min(data->vif->bss_conf.beacon_int,
 170			     vif->bss_conf.beacon_int);
 171
 172		if (!min_bi)
 173			break;
 174
 175		if ((data->vif->bss_conf.beacon_int -
 176		     vif->bss_conf.beacon_int) % min_bi == 0) {
 177			data->preferred_tsf = mvmvif->tsf_id;
 178			return;
 179		}
 180		break;
 181	default:
 182		/*
 183		 * For all other interface types there's no need to
 184		 * take drift into account. Either they're exclusive
 185		 * like IBSS and monitor, or we don't care much about
 186		 * their TSF (like P2P Device), but we won't be able
 187		 * to share the TSF resource.
 188		 */
 189		break;
 190	}
 191
 192	/*
 193	 * Unless we exited above, we can't share the TSF resource
 194	 * that the virtual interface we're iterating over is using
 195	 * with the new one, so clear the available bit and if this
 196	 * was the preferred one, reset that as well.
 197	 */
 198	__clear_bit(mvmvif->tsf_id, data->available_tsf_ids);
 199
 200	if (data->preferred_tsf == mvmvif->tsf_id)
 201		data->preferred_tsf = NUM_TSF_IDS;
 202}
 203
 204/*
 205 * Get the mask of the queues used by the vif
 206 */
 207u32 iwl_mvm_mac_get_queues_mask(struct ieee80211_vif *vif)
 208{
 209	u32 qmask = 0, ac;
 210
 211	if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
 212		return BIT(IWL_MVM_OFFCHANNEL_QUEUE);
 213
 214	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
 215		if (vif->hw_queue[ac] != IEEE80211_INVAL_HW_QUEUE)
 216			qmask |= BIT(vif->hw_queue[ac]);
 217	}
 218
 219	if (vif->type == NL80211_IFTYPE_AP)
 
 220		qmask |= BIT(vif->cab_queue);
 221
 222	return qmask;
 223}
 224
 225static void iwl_mvm_iface_hw_queues_iter(void *_data, u8 *mac,
 226					 struct ieee80211_vif *vif)
 227{
 228	struct iwl_mvm_hw_queues_iface_iterator_data *data = _data;
 229
 230	/* exclude the given vif */
 231	if (vif == data->exclude_vif)
 232		return;
 233
 234	data->used_hw_queues |= iwl_mvm_mac_get_queues_mask(vif);
 235}
 236
 237static void iwl_mvm_mac_sta_hw_queues_iter(void *_data,
 238					   struct ieee80211_sta *sta)
 239{
 240	struct iwl_mvm_hw_queues_iface_iterator_data *data = _data;
 241	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
 242
 243	/* Mark the queues used by the sta */
 244	data->used_hw_queues |= mvmsta->tfd_queue_msk;
 245}
 246
 247unsigned long iwl_mvm_get_used_hw_queues(struct iwl_mvm *mvm,
 248					 struct ieee80211_vif *exclude_vif)
 249{
 250	u8 sta_id;
 251	struct iwl_mvm_hw_queues_iface_iterator_data data = {
 252		.exclude_vif = exclude_vif,
 253		.used_hw_queues =
 254			BIT(IWL_MVM_OFFCHANNEL_QUEUE) |
 255			BIT(mvm->aux_queue),
 
 256	};
 257
 258	if (iwl_mvm_is_dqa_supported(mvm))
 259		data.used_hw_queues |= BIT(IWL_MVM_DQA_CMD_QUEUE);
 260	else
 261		data.used_hw_queues |= BIT(IWL_MVM_CMD_QUEUE);
 262
 263	lockdep_assert_held(&mvm->mutex);
 264
 265	/* mark all VIF used hw queues */
 266	ieee80211_iterate_active_interfaces_atomic(
 267		mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
 268		iwl_mvm_iface_hw_queues_iter, &data);
 269
 270	/* don't assign the same hw queues as TDLS stations */
 271	ieee80211_iterate_stations_atomic(mvm->hw,
 272					  iwl_mvm_mac_sta_hw_queues_iter,
 273					  &data);
 274
 275	/*
 276	 * Some TDLS stations may be removed but are in the process of being
 277	 * drained. Don't touch their queues.
 278	 */
 279	for_each_set_bit(sta_id, mvm->sta_drained, IWL_MVM_STATION_COUNT)
 280		data.used_hw_queues |= mvm->tfd_drained[sta_id];
 281
 282	return data.used_hw_queues;
 283}
 284
 285static void iwl_mvm_mac_iface_iterator(void *_data, u8 *mac,
 286				       struct ieee80211_vif *vif)
 287{
 288	struct iwl_mvm_mac_iface_iterator_data *data = _data;
 289	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
 290
 291	/* Iterator may already find the interface being added -- skip it */
 292	if (vif == data->vif) {
 293		data->found_vif = true;
 294		return;
 295	}
 296
 297	/* Mark MAC IDs as used by clearing the available bit, and
 298	 * (below) mark TSFs as used if their existing use is not
 299	 * compatible with the new interface type.
 300	 * No locking or atomic bit operations are needed since the
 301	 * data is on the stack of the caller function.
 302	 */
 303	__clear_bit(mvmvif->id, data->available_mac_ids);
 304
 305	/* find a suitable tsf_id */
 306	iwl_mvm_mac_tsf_id_iter(_data, mac, vif);
 307}
 308
 309void iwl_mvm_mac_ctxt_recalc_tsf_id(struct iwl_mvm *mvm,
 310				    struct ieee80211_vif *vif)
 311{
 312	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
 313	struct iwl_mvm_mac_iface_iterator_data data = {
 314		.mvm = mvm,
 315		.vif = vif,
 316		.available_tsf_ids = { (1 << NUM_TSF_IDS) - 1 },
 317		/* no preference yet */
 318		.preferred_tsf = NUM_TSF_IDS,
 319	};
 320
 321	ieee80211_iterate_active_interfaces_atomic(
 322		mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
 323		iwl_mvm_mac_tsf_id_iter, &data);
 324
 325	if (data.preferred_tsf != NUM_TSF_IDS)
 326		mvmvif->tsf_id = data.preferred_tsf;
 327	else if (!test_bit(mvmvif->tsf_id, data.available_tsf_ids))
 328		mvmvif->tsf_id = find_first_bit(data.available_tsf_ids,
 329						NUM_TSF_IDS);
 330}
 331
 332static int iwl_mvm_mac_ctxt_allocate_resources(struct iwl_mvm *mvm,
 333					       struct ieee80211_vif *vif)
 334{
 335	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
 336	struct iwl_mvm_mac_iface_iterator_data data = {
 337		.mvm = mvm,
 338		.vif = vif,
 339		.available_mac_ids = { (1 << NUM_MAC_INDEX_DRIVER) - 1 },
 340		.available_tsf_ids = { (1 << NUM_TSF_IDS) - 1 },
 341		/* no preference yet */
 342		.preferred_tsf = NUM_TSF_IDS,
 343		.found_vif = false,
 344	};
 345	u32 ac;
 346	int ret, i;
 347	unsigned long used_hw_queues;
 348
 
 
 349	/*
 350	 * Allocate a MAC ID and a TSF for this MAC, along with the queues
 351	 * and other resources.
 352	 */
 353
 354	/*
 355	 * Before the iterator, we start with all MAC IDs and TSFs available.
 356	 *
 357	 * During iteration, all MAC IDs are cleared that are in use by other
 358	 * virtual interfaces, and all TSF IDs are cleared that can't be used
 359	 * by this new virtual interface because they're used by an interface
 360	 * that can't share it with the new one.
 361	 * At the same time, we check if there's a preferred TSF in the case
 362	 * that we should share it with another interface.
 363	 */
 364
 365	/* Currently, MAC ID 0 should be used only for the managed/IBSS vif */
 366	switch (vif->type) {
 367	case NL80211_IFTYPE_ADHOC:
 368		break;
 369	case NL80211_IFTYPE_STATION:
 370		if (!vif->p2p)
 371			break;
 372		/* fall through */
 373	default:
 374		__clear_bit(0, data.available_mac_ids);
 375	}
 376
 377	ieee80211_iterate_active_interfaces_atomic(
 378		mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
 379		iwl_mvm_mac_iface_iterator, &data);
 380
 381	used_hw_queues = iwl_mvm_get_used_hw_queues(mvm, vif);
 382
 383	/*
 384	 * In the case we're getting here during resume, it's similar to
 385	 * firmware restart, and with RESUME_ALL the iterator will find
 386	 * the vif being added already.
 387	 * We don't want to reassign any IDs in either case since doing
 388	 * so would probably assign different IDs (as interfaces aren't
 389	 * necessarily added in the same order), but the old IDs were
 390	 * preserved anyway, so skip ID assignment for both resume and
 391	 * recovery.
 392	 */
 393	if (data.found_vif)
 394		return 0;
 395
 396	/* Therefore, in recovery, we can't get here */
 397	if (WARN_ON_ONCE(test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)))
 398		return -EBUSY;
 399
 400	mvmvif->id = find_first_bit(data.available_mac_ids,
 401				    NUM_MAC_INDEX_DRIVER);
 402	if (mvmvif->id == NUM_MAC_INDEX_DRIVER) {
 403		IWL_ERR(mvm, "Failed to init MAC context - no free ID!\n");
 404		ret = -EIO;
 405		goto exit_fail;
 406	}
 407
 408	if (data.preferred_tsf != NUM_TSF_IDS)
 409		mvmvif->tsf_id = data.preferred_tsf;
 410	else
 411		mvmvif->tsf_id = find_first_bit(data.available_tsf_ids,
 412						NUM_TSF_IDS);
 413	if (mvmvif->tsf_id == NUM_TSF_IDS) {
 414		IWL_ERR(mvm, "Failed to init MAC context - no free TSF!\n");
 415		ret = -EIO;
 416		goto exit_fail;
 417	}
 418
 419	mvmvif->color = 0;
 420
 421	INIT_LIST_HEAD(&mvmvif->time_event_data.list);
 422	mvmvif->time_event_data.id = TE_MAX;
 423
 424	/* No need to allocate data queues to P2P Device MAC.*/
 425	if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
 426		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
 427			vif->hw_queue[ac] = IEEE80211_INVAL_HW_QUEUE;
 428
 429		return 0;
 430	}
 431
 432	/*
 
 
 
 
 
 
 
 
 
 433	 * Find available queues, and allocate them to the ACs. When in
 434	 * DQA-mode they aren't really used, and this is done only so the
 435	 * mac80211 ieee80211_check_queues() function won't fail
 436	 */
 437	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
 438		u8 queue = find_first_zero_bit(&used_hw_queues,
 439					       mvm->first_agg_queue);
 440
 441		if (!iwl_mvm_is_dqa_supported(mvm) &&
 442		    queue >= mvm->first_agg_queue) {
 443			IWL_ERR(mvm, "Failed to allocate queue\n");
 444			ret = -EIO;
 445			goto exit_fail;
 446		}
 447
 448		__set_bit(queue, &used_hw_queues);
 449		vif->hw_queue[ac] = queue;
 450	}
 451
 452	/* Allocate the CAB queue for softAP and GO interfaces */
 453	if (vif->type == NL80211_IFTYPE_AP) {
 454		u8 queue;
 455
 456		if (!iwl_mvm_is_dqa_supported(mvm)) {
 457			queue = find_first_zero_bit(&used_hw_queues,
 458						    mvm->first_agg_queue);
 459
 460			if (queue >= mvm->first_agg_queue) {
 461				IWL_ERR(mvm, "Failed to allocate cab queue\n");
 462				ret = -EIO;
 463				goto exit_fail;
 464			}
 465		} else {
 466			queue = IWL_MVM_DQA_GCAST_QUEUE;
 467		}
 468
 469		vif->cab_queue = queue;
 470	} else {
 471		vif->cab_queue = IEEE80211_INVAL_HW_QUEUE;
 472	}
 473
 474	mvmvif->bcast_sta.sta_id = IWL_MVM_STATION_COUNT;
 475	mvmvif->ap_sta_id = IWL_MVM_STATION_COUNT;
 
 476
 477	for (i = 0; i < NUM_IWL_MVM_SMPS_REQ; i++)
 478		mvmvif->smps_requests[i] = IEEE80211_SMPS_AUTOMATIC;
 479
 480	return 0;
 481
 482exit_fail:
 483	memset(mvmvif, 0, sizeof(struct iwl_mvm_vif));
 484	memset(vif->hw_queue, IEEE80211_INVAL_HW_QUEUE, sizeof(vif->hw_queue));
 485	vif->cab_queue = IEEE80211_INVAL_HW_QUEUE;
 486	return ret;
 487}
 488
 489int iwl_mvm_mac_ctxt_init(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
 490{
 491	unsigned int wdg_timeout =
 492		iwl_mvm_get_wd_timeout(mvm, vif, false, false);
 493	u32 ac;
 494	int ret;
 495
 496	lockdep_assert_held(&mvm->mutex);
 497
 498	ret = iwl_mvm_mac_ctxt_allocate_resources(mvm, vif);
 499	if (ret)
 500		return ret;
 501
 502	/* If DQA is supported - queues will be enabled when needed */
 503	if (iwl_mvm_is_dqa_supported(mvm))
 504		return 0;
 505
 506	switch (vif->type) {
 507	case NL80211_IFTYPE_P2P_DEVICE:
 508		iwl_mvm_enable_ac_txq(mvm, IWL_MVM_OFFCHANNEL_QUEUE,
 509				      IWL_MVM_OFFCHANNEL_QUEUE,
 510				      IWL_MVM_TX_FIFO_VO, 0, wdg_timeout);
 511		break;
 512	case NL80211_IFTYPE_AP:
 513		iwl_mvm_enable_ac_txq(mvm, vif->cab_queue, vif->cab_queue,
 514				      IWL_MVM_TX_FIFO_MCAST, 0, wdg_timeout);
 515		/* fall through */
 516	default:
 517		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
 518			iwl_mvm_enable_ac_txq(mvm, vif->hw_queue[ac],
 519					      vif->hw_queue[ac],
 520					      iwl_mvm_ac_to_tx_fifo[ac], 0,
 521					      wdg_timeout);
 522		break;
 523	}
 524
 525	return 0;
 526}
 527
 528void iwl_mvm_mac_ctxt_release(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
 529{
 530	int ac;
 531
 532	lockdep_assert_held(&mvm->mutex);
 533
 534	switch (vif->type) {
 535	case NL80211_IFTYPE_P2P_DEVICE:
 536		if (!iwl_mvm_is_dqa_supported(mvm))
 537			iwl_mvm_disable_txq(mvm, IWL_MVM_OFFCHANNEL_QUEUE,
 538					    IWL_MVM_OFFCHANNEL_QUEUE,
 539					    IWL_MAX_TID_COUNT, 0);
 540		else
 541			iwl_mvm_disable_txq(mvm,
 542					    IWL_MVM_DQA_P2P_DEVICE_QUEUE,
 543					    vif->hw_queue[0], IWL_MAX_TID_COUNT,
 544					    0);
 545
 546		break;
 547	case NL80211_IFTYPE_AP:
 548		iwl_mvm_disable_txq(mvm, vif->cab_queue, vif->cab_queue,
 549				    IWL_MAX_TID_COUNT, 0);
 550
 551		if (iwl_mvm_is_dqa_supported(mvm))
 552			iwl_mvm_disable_txq(mvm,
 553					    IWL_MVM_DQA_AP_PROBE_RESP_QUEUE,
 554					    vif->hw_queue[0], IWL_MAX_TID_COUNT,
 555					    0);
 556		/* fall through */
 557	default:
 558		/*
 559		 * If DQA is supported - queues were already disabled, since in
 560		 * DQA-mode the queues are a property of the STA and not of the
 561		 * vif, and at this point the STA was already deleted
 562		 */
 563		if (iwl_mvm_is_dqa_supported(mvm))
 564			break;
 565
 566		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
 567			iwl_mvm_disable_txq(mvm, vif->hw_queue[ac],
 568					    vif->hw_queue[ac],
 569					    IWL_MAX_TID_COUNT, 0);
 570	}
 571}
 572
 573static void iwl_mvm_ack_rates(struct iwl_mvm *mvm,
 574			      struct ieee80211_vif *vif,
 575			      enum nl80211_band band,
 576			      u8 *cck_rates, u8 *ofdm_rates)
 577{
 578	struct ieee80211_supported_band *sband;
 579	unsigned long basic = vif->bss_conf.basic_rates;
 580	int lowest_present_ofdm = 100;
 581	int lowest_present_cck = 100;
 582	u8 cck = 0;
 583	u8 ofdm = 0;
 584	int i;
 585
 586	sband = mvm->hw->wiphy->bands[band];
 587
 588	for_each_set_bit(i, &basic, BITS_PER_LONG) {
 589		int hw = sband->bitrates[i].hw_value;
 590		if (hw >= IWL_FIRST_OFDM_RATE) {
 591			ofdm |= BIT(hw - IWL_FIRST_OFDM_RATE);
 592			if (lowest_present_ofdm > hw)
 593				lowest_present_ofdm = hw;
 594		} else {
 595			BUILD_BUG_ON(IWL_FIRST_CCK_RATE != 0);
 596
 597			cck |= BIT(hw);
 598			if (lowest_present_cck > hw)
 599				lowest_present_cck = hw;
 600		}
 601	}
 602
 603	/*
 604	 * Now we've got the basic rates as bitmaps in the ofdm and cck
 605	 * variables. This isn't sufficient though, as there might not
 606	 * be all the right rates in the bitmap. E.g. if the only basic
 607	 * rates are 5.5 Mbps and 11 Mbps, we still need to add 1 Mbps
 608	 * and 6 Mbps because the 802.11-2007 standard says in 9.6:
 609	 *
 610	 *    [...] a STA responding to a received frame shall transmit
 611	 *    its Control Response frame [...] at the highest rate in the
 612	 *    BSSBasicRateSet parameter that is less than or equal to the
 613	 *    rate of the immediately previous frame in the frame exchange
 614	 *    sequence ([...]) and that is of the same modulation class
 615	 *    ([...]) as the received frame. If no rate contained in the
 616	 *    BSSBasicRateSet parameter meets these conditions, then the
 617	 *    control frame sent in response to a received frame shall be
 618	 *    transmitted at the highest mandatory rate of the PHY that is
 619	 *    less than or equal to the rate of the received frame, and
 620	 *    that is of the same modulation class as the received frame.
 621	 *
 622	 * As a consequence, we need to add all mandatory rates that are
 623	 * lower than all of the basic rates to these bitmaps.
 624	 */
 625
 626	if (IWL_RATE_24M_INDEX < lowest_present_ofdm)
 627		ofdm |= IWL_RATE_BIT_MSK(24) >> IWL_FIRST_OFDM_RATE;
 628	if (IWL_RATE_12M_INDEX < lowest_present_ofdm)
 629		ofdm |= IWL_RATE_BIT_MSK(12) >> IWL_FIRST_OFDM_RATE;
 630	/* 6M already there or needed so always add */
 631	ofdm |= IWL_RATE_BIT_MSK(6) >> IWL_FIRST_OFDM_RATE;
 632
 633	/*
 634	 * CCK is a bit more complex with DSSS vs. HR/DSSS vs. ERP.
 635	 * Note, however:
 636	 *  - if no CCK rates are basic, it must be ERP since there must
 637	 *    be some basic rates at all, so they're OFDM => ERP PHY
 638	 *    (or we're in 5 GHz, and the cck bitmap will never be used)
 639	 *  - if 11M is a basic rate, it must be ERP as well, so add 5.5M
 640	 *  - if 5.5M is basic, 1M and 2M are mandatory
 641	 *  - if 2M is basic, 1M is mandatory
 642	 *  - if 1M is basic, that's the only valid ACK rate.
 643	 * As a consequence, it's not as complicated as it sounds, just add
 644	 * any lower rates to the ACK rate bitmap.
 645	 */
 646	if (IWL_RATE_11M_INDEX < lowest_present_cck)
 647		cck |= IWL_RATE_BIT_MSK(11) >> IWL_FIRST_CCK_RATE;
 648	if (IWL_RATE_5M_INDEX < lowest_present_cck)
 649		cck |= IWL_RATE_BIT_MSK(5) >> IWL_FIRST_CCK_RATE;
 650	if (IWL_RATE_2M_INDEX < lowest_present_cck)
 651		cck |= IWL_RATE_BIT_MSK(2) >> IWL_FIRST_CCK_RATE;
 652	/* 1M already there or needed so always add */
 653	cck |= IWL_RATE_BIT_MSK(1) >> IWL_FIRST_CCK_RATE;
 654
 655	*cck_rates = cck;
 656	*ofdm_rates = ofdm;
 657}
 658
 659static void iwl_mvm_mac_ctxt_set_ht_flags(struct iwl_mvm *mvm,
 660					 struct ieee80211_vif *vif,
 661					 struct iwl_mac_ctx_cmd *cmd)
 662{
 663	/* for both sta and ap, ht_operation_mode hold the protection_mode */
 664	u8 protection_mode = vif->bss_conf.ht_operation_mode &
 665				 IEEE80211_HT_OP_MODE_PROTECTION;
 666	/* The fw does not distinguish between ht and fat */
 667	u32 ht_flag = MAC_PROT_FLG_HT_PROT | MAC_PROT_FLG_FAT_PROT;
 668
 669	IWL_DEBUG_RATE(mvm, "protection mode set to %d\n", protection_mode);
 670	/*
 671	 * See section 9.23.3.1 of IEEE 80211-2012.
 672	 * Nongreenfield HT STAs Present is not supported.
 673	 */
 674	switch (protection_mode) {
 675	case IEEE80211_HT_OP_MODE_PROTECTION_NONE:
 676		break;
 677	case IEEE80211_HT_OP_MODE_PROTECTION_NONMEMBER:
 678	case IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED:
 679		cmd->protection_flags |= cpu_to_le32(ht_flag);
 680		break;
 681	case IEEE80211_HT_OP_MODE_PROTECTION_20MHZ:
 682		/* Protect when channel wider than 20MHz */
 683		if (vif->bss_conf.chandef.width > NL80211_CHAN_WIDTH_20)
 684			cmd->protection_flags |= cpu_to_le32(ht_flag);
 685		break;
 686	default:
 687		IWL_ERR(mvm, "Illegal protection mode %d\n",
 688			protection_mode);
 689		break;
 690	}
 691}
 692
 693static void iwl_mvm_mac_ctxt_cmd_common(struct iwl_mvm *mvm,
 694					struct ieee80211_vif *vif,
 695					struct iwl_mac_ctx_cmd *cmd,
 696					const u8 *bssid_override,
 697					u32 action)
 698{
 699	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
 700	struct ieee80211_chanctx_conf *chanctx;
 701	bool ht_enabled = !!(vif->bss_conf.ht_operation_mode &
 702			     IEEE80211_HT_OP_MODE_PROTECTION);
 703	u8 cck_ack_rates, ofdm_ack_rates;
 704	const u8 *bssid = bssid_override ?: vif->bss_conf.bssid;
 705	int i;
 706
 707	cmd->id_and_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
 708							    mvmvif->color));
 709	cmd->action = cpu_to_le32(action);
 710
 711	switch (vif->type) {
 712	case NL80211_IFTYPE_STATION:
 713		if (vif->p2p)
 714			cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_P2P_STA);
 715		else
 716			cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_BSS_STA);
 717		break;
 718	case NL80211_IFTYPE_AP:
 719		cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_GO);
 720		break;
 721	case NL80211_IFTYPE_MONITOR:
 722		cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_LISTENER);
 723		break;
 724	case NL80211_IFTYPE_P2P_DEVICE:
 725		cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_P2P_DEVICE);
 726		break;
 727	case NL80211_IFTYPE_ADHOC:
 728		cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_IBSS);
 729		break;
 730	default:
 731		WARN_ON_ONCE(1);
 732	}
 733
 734	cmd->tsf_id = cpu_to_le32(mvmvif->tsf_id);
 735
 736	memcpy(cmd->node_addr, vif->addr, ETH_ALEN);
 737
 738	if (bssid)
 739		memcpy(cmd->bssid_addr, bssid, ETH_ALEN);
 740	else
 741		eth_broadcast_addr(cmd->bssid_addr);
 742
 743	rcu_read_lock();
 744	chanctx = rcu_dereference(vif->chanctx_conf);
 745	iwl_mvm_ack_rates(mvm, vif, chanctx ? chanctx->def.chan->band
 746					    : NL80211_BAND_2GHZ,
 747			  &cck_ack_rates, &ofdm_ack_rates);
 748	rcu_read_unlock();
 749
 750	cmd->cck_rates = cpu_to_le32((u32)cck_ack_rates);
 751	cmd->ofdm_rates = cpu_to_le32((u32)ofdm_ack_rates);
 752
 753	cmd->cck_short_preamble =
 754		cpu_to_le32(vif->bss_conf.use_short_preamble ?
 755			    MAC_FLG_SHORT_PREAMBLE : 0);
 756	cmd->short_slot =
 757		cpu_to_le32(vif->bss_conf.use_short_slot ?
 758			    MAC_FLG_SHORT_SLOT : 0);
 759
 760	cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP);
 761
 762	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
 763		u8 txf = iwl_mvm_ac_to_tx_fifo[i];
 764
 765		cmd->ac[txf].cw_min =
 766			cpu_to_le16(mvmvif->queue_params[i].cw_min);
 767		cmd->ac[txf].cw_max =
 768			cpu_to_le16(mvmvif->queue_params[i].cw_max);
 769		cmd->ac[txf].edca_txop =
 770			cpu_to_le16(mvmvif->queue_params[i].txop * 32);
 771		cmd->ac[txf].aifsn = mvmvif->queue_params[i].aifs;
 772		cmd->ac[txf].fifos_mask = BIT(txf);
 773	}
 774
 775	if (vif->bss_conf.qos)
 776		cmd->qos_flags |= cpu_to_le32(MAC_QOS_FLG_UPDATE_EDCA);
 777
 778	if (vif->bss_conf.use_cts_prot)
 779		cmd->protection_flags |= cpu_to_le32(MAC_PROT_FLG_TGG_PROTECT);
 780
 781	IWL_DEBUG_RATE(mvm, "use_cts_prot %d, ht_operation_mode %d\n",
 782		       vif->bss_conf.use_cts_prot,
 783		       vif->bss_conf.ht_operation_mode);
 784	if (vif->bss_conf.chandef.width != NL80211_CHAN_WIDTH_20_NOHT)
 785		cmd->qos_flags |= cpu_to_le32(MAC_QOS_FLG_TGN);
 786	if (ht_enabled)
 787		iwl_mvm_mac_ctxt_set_ht_flags(mvm, vif, cmd);
 788}
 789
 790static int iwl_mvm_mac_ctxt_send_cmd(struct iwl_mvm *mvm,
 791				     struct iwl_mac_ctx_cmd *cmd)
 792{
 793	int ret = iwl_mvm_send_cmd_pdu(mvm, MAC_CONTEXT_CMD, 0,
 794				       sizeof(*cmd), cmd);
 795	if (ret)
 796		IWL_ERR(mvm, "Failed to send MAC context (action:%d): %d\n",
 797			le32_to_cpu(cmd->action), ret);
 798	return ret;
 799}
 800
 801static int iwl_mvm_mac_ctxt_cmd_sta(struct iwl_mvm *mvm,
 802				    struct ieee80211_vif *vif,
 803				    u32 action, bool force_assoc_off,
 804				    const u8 *bssid_override)
 805{
 806	struct iwl_mac_ctx_cmd cmd = {};
 807	struct iwl_mac_data_sta *ctxt_sta;
 808
 809	WARN_ON(vif->type != NL80211_IFTYPE_STATION);
 810
 811	/* Fill the common data for all mac context types */
 812	iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, bssid_override, action);
 813
 814	if (vif->p2p) {
 815		struct ieee80211_p2p_noa_attr *noa =
 816			&vif->bss_conf.p2p_noa_attr;
 817
 818		cmd.p2p_sta.ctwin = cpu_to_le32(noa->oppps_ctwindow &
 819					IEEE80211_P2P_OPPPS_CTWINDOW_MASK);
 820		ctxt_sta = &cmd.p2p_sta.sta;
 821	} else {
 822		ctxt_sta = &cmd.sta;
 823	}
 824
 825	/* We need the dtim_period to set the MAC as associated */
 826	if (vif->bss_conf.assoc && vif->bss_conf.dtim_period &&
 827	    !force_assoc_off) {
 828		u32 dtim_offs;
 829
 830		/*
 831		 * The DTIM count counts down, so when it is N that means N
 832		 * more beacon intervals happen until the DTIM TBTT. Therefore
 833		 * add this to the current time. If that ends up being in the
 834		 * future, the firmware will handle it.
 835		 *
 836		 * Also note that the system_timestamp (which we get here as
 837		 * "sync_device_ts") and TSF timestamp aren't at exactly the
 838		 * same offset in the frame -- the TSF is at the first symbol
 839		 * of the TSF, the system timestamp is at signal acquisition
 840		 * time. This means there's an offset between them of at most
 841		 * a few hundred microseconds (24 * 8 bits + PLCP time gives
 842		 * 384us in the longest case), this is currently not relevant
 843		 * as the firmware wakes up around 2ms before the TBTT.
 844		 */
 845		dtim_offs = vif->bss_conf.sync_dtim_count *
 846				vif->bss_conf.beacon_int;
 847		/* convert TU to usecs */
 848		dtim_offs *= 1024;
 849
 850		ctxt_sta->dtim_tsf =
 851			cpu_to_le64(vif->bss_conf.sync_tsf + dtim_offs);
 852		ctxt_sta->dtim_time =
 853			cpu_to_le32(vif->bss_conf.sync_device_ts + dtim_offs);
 
 
 854
 855		IWL_DEBUG_INFO(mvm, "DTIM TBTT is 0x%llx/0x%x, offset %d\n",
 856			       le64_to_cpu(ctxt_sta->dtim_tsf),
 857			       le32_to_cpu(ctxt_sta->dtim_time),
 858			       dtim_offs);
 859
 860		ctxt_sta->is_assoc = cpu_to_le32(1);
 861	} else {
 862		ctxt_sta->is_assoc = cpu_to_le32(0);
 863
 864		/* Allow beacons to pass through as long as we are not
 865		 * associated, or we do not have dtim period information.
 866		 */
 867		cmd.filter_flags |= cpu_to_le32(MAC_FILTER_IN_BEACON);
 868	}
 869
 870	ctxt_sta->bi = cpu_to_le32(vif->bss_conf.beacon_int);
 871	ctxt_sta->bi_reciprocal =
 872		cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int));
 873	ctxt_sta->dtim_interval = cpu_to_le32(vif->bss_conf.beacon_int *
 874					      vif->bss_conf.dtim_period);
 875	ctxt_sta->dtim_reciprocal =
 876		cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int *
 877					       vif->bss_conf.dtim_period));
 878
 879	ctxt_sta->listen_interval = cpu_to_le32(mvm->hw->conf.listen_interval);
 880	ctxt_sta->assoc_id = cpu_to_le32(vif->bss_conf.aid);
 881
 882	if (vif->probe_req_reg && vif->bss_conf.assoc && vif->p2p)
 883		cmd.filter_flags |= cpu_to_le32(MAC_FILTER_IN_PROBE_REQUEST);
 884
 885	return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
 886}
 887
 888static int iwl_mvm_mac_ctxt_cmd_listener(struct iwl_mvm *mvm,
 889					 struct ieee80211_vif *vif,
 890					 u32 action)
 891{
 892	struct iwl_mac_ctx_cmd cmd = {};
 893	u32 tfd_queue_msk = 0;
 894	int ret, i;
 895
 896	WARN_ON(vif->type != NL80211_IFTYPE_MONITOR);
 897
 898	iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
 899
 900	if (!iwl_mvm_is_dqa_supported(mvm)) {
 901		for (i = 0; i < IEEE80211_NUM_ACS; i++)
 902			if (vif->hw_queue[i] != IEEE80211_INVAL_HW_QUEUE)
 903				tfd_queue_msk |= BIT(vif->hw_queue[i]);
 904	}
 905
 906	cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_PROMISC |
 907				       MAC_FILTER_IN_CONTROL_AND_MGMT |
 908				       MAC_FILTER_IN_BEACON |
 909				       MAC_FILTER_IN_PROBE_REQUEST |
 910				       MAC_FILTER_IN_CRC32);
 911	ieee80211_hw_set(mvm->hw, RX_INCLUDES_FCS);
 912
 913	/* Allocate sniffer station */
 914	ret = iwl_mvm_allocate_int_sta(mvm, &mvm->snif_sta, tfd_queue_msk,
 915				       vif->type);
 916	if (ret)
 917		return ret;
 918
 919	return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
 920}
 921
 922static int iwl_mvm_mac_ctxt_cmd_ibss(struct iwl_mvm *mvm,
 923				     struct ieee80211_vif *vif,
 924				     u32 action)
 925{
 926	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
 927	struct iwl_mac_ctx_cmd cmd = {};
 928
 929	WARN_ON(vif->type != NL80211_IFTYPE_ADHOC);
 930
 931	iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
 932
 933	cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_BEACON |
 934				       MAC_FILTER_IN_PROBE_REQUEST);
 935
 936	/* cmd.ibss.beacon_time/cmd.ibss.beacon_tsf are curently ignored */
 937	cmd.ibss.bi = cpu_to_le32(vif->bss_conf.beacon_int);
 938	cmd.ibss.bi_reciprocal =
 939		cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int));
 940
 941	/* TODO: Assumes that the beacon id == mac context id */
 942	cmd.ibss.beacon_template = cpu_to_le32(mvmvif->id);
 943
 944	return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
 945}
 946
 947struct iwl_mvm_go_iterator_data {
 948	bool go_active;
 949};
 950
 951static void iwl_mvm_go_iterator(void *_data, u8 *mac, struct ieee80211_vif *vif)
 952{
 953	struct iwl_mvm_go_iterator_data *data = _data;
 954	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
 955
 956	if (vif->type == NL80211_IFTYPE_AP && vif->p2p &&
 957	    mvmvif->ap_ibss_active)
 958		data->go_active = true;
 959}
 960
 961static int iwl_mvm_mac_ctxt_cmd_p2p_device(struct iwl_mvm *mvm,
 962					   struct ieee80211_vif *vif,
 963					   u32 action)
 964{
 965	struct iwl_mac_ctx_cmd cmd = {};
 966	struct iwl_mvm_go_iterator_data data = {};
 967
 968	WARN_ON(vif->type != NL80211_IFTYPE_P2P_DEVICE);
 969
 970	iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
 971
 972	cmd.protection_flags |= cpu_to_le32(MAC_PROT_FLG_TGG_PROTECT);
 973
 974	/* Override the filter flags to accept only probe requests */
 975	cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_PROBE_REQUEST);
 976
 977	/*
 978	 * This flag should be set to true when the P2P Device is
 979	 * discoverable and there is at least another active P2P GO. Settings
 980	 * this flag will allow the P2P Device to be discoverable on other
 981	 * channels in addition to its listen channel.
 982	 * Note that this flag should not be set in other cases as it opens the
 983	 * Rx filters on all MAC and increases the number of interrupts.
 984	 */
 985	ieee80211_iterate_active_interfaces_atomic(
 986		mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
 987		iwl_mvm_go_iterator, &data);
 988
 989	cmd.p2p_dev.is_disc_extended = cpu_to_le32(data.go_active ? 1 : 0);
 990	return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
 991}
 992
 993static void iwl_mvm_mac_ctxt_set_tim(struct iwl_mvm *mvm,
 994				     struct iwl_mac_beacon_cmd_v6 *beacon_cmd,
 995				     u8 *beacon, u32 frame_size)
 996{
 997	u32 tim_idx;
 998	struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)beacon;
 999
1000	/* The index is relative to frame start but we start looking at the
1001	 * variable-length part of the beacon. */
1002	tim_idx = mgmt->u.beacon.variable - beacon;
1003
1004	/* Parse variable-length elements of beacon to find WLAN_EID_TIM */
1005	while ((tim_idx < (frame_size - 2)) &&
1006			(beacon[tim_idx] != WLAN_EID_TIM))
1007		tim_idx += beacon[tim_idx+1] + 2;
1008
1009	/* If TIM field was found, set variables */
1010	if ((tim_idx < (frame_size - 1)) && (beacon[tim_idx] == WLAN_EID_TIM)) {
1011		beacon_cmd->tim_idx = cpu_to_le32(tim_idx);
1012		beacon_cmd->tim_size = cpu_to_le32((u32)beacon[tim_idx+1]);
1013	} else {
1014		IWL_WARN(mvm, "Unable to find TIM Element in beacon\n");
1015	}
1016}
1017
1018static u32 iwl_mvm_find_ie_offset(u8 *beacon, u8 eid, u32 frame_size)
1019{
1020	struct ieee80211_mgmt *mgmt = (void *)beacon;
1021	const u8 *ie;
1022
1023	if (WARN_ON_ONCE(frame_size <= (mgmt->u.beacon.variable - beacon)))
1024		return 0;
1025
1026	frame_size -= mgmt->u.beacon.variable - beacon;
1027
1028	ie = cfg80211_find_ie(eid, mgmt->u.beacon.variable, frame_size);
1029	if (!ie)
1030		return 0;
1031
1032	return ie - beacon;
1033}
1034
1035static int iwl_mvm_mac_ctxt_send_beacon(struct iwl_mvm *mvm,
1036					struct ieee80211_vif *vif,
1037					struct sk_buff *beacon)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1038{
1039	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1040	struct iwl_host_cmd cmd = {
1041		.id = BEACON_TEMPLATE_CMD,
1042		.flags = CMD_ASYNC,
1043	};
1044	union {
1045		struct iwl_mac_beacon_cmd_v6 beacon_cmd_v6;
1046		struct iwl_mac_beacon_cmd beacon_cmd;
1047	} u = {};
1048	struct ieee80211_tx_info *info;
1049	u32 beacon_skb_len;
1050	u32 rate, tx_flags;
1051
1052	if (WARN_ON(!beacon))
1053		return -EINVAL;
1054
1055	beacon_skb_len = beacon->len;
1056
1057	/* TODO: for now the beacon template id is set to be the mac context id.
1058	 * Might be better to handle it as another resource ... */
1059	u.beacon_cmd_v6.template_id = cpu_to_le32((u32)mvmvif->id);
1060	info = IEEE80211_SKB_CB(beacon);
1061
1062	/* Set up TX command fields */
1063	u.beacon_cmd_v6.tx.len = cpu_to_le16((u16)beacon_skb_len);
1064	u.beacon_cmd_v6.tx.sta_id = mvmvif->bcast_sta.sta_id;
1065	u.beacon_cmd_v6.tx.life_time = cpu_to_le32(TX_CMD_LIFE_TIME_INFINITE);
1066	tx_flags = TX_CMD_FLG_SEQ_CTL | TX_CMD_FLG_TSF;
1067	tx_flags |=
1068		iwl_mvm_bt_coex_tx_prio(mvm, (void *)beacon->data, info, 0) <<
1069						TX_CMD_FLG_BT_PRIO_POS;
1070	u.beacon_cmd_v6.tx.tx_flags = cpu_to_le32(tx_flags);
1071
1072	if (!fw_has_capa(&mvm->fw->ucode_capa,
1073			 IWL_UCODE_TLV_CAPA_BEACON_ANT_SELECTION)) {
1074		mvm->mgmt_last_antenna_idx =
1075			iwl_mvm_next_antenna(mvm, iwl_mvm_get_valid_tx_ant(mvm),
1076					     mvm->mgmt_last_antenna_idx);
1077	}
1078
1079	u.beacon_cmd_v6.tx.rate_n_flags =
1080		cpu_to_le32(BIT(mvm->mgmt_last_antenna_idx) <<
1081			    RATE_MCS_ANT_POS);
1082
1083	if (info->band == NL80211_BAND_5GHZ || vif->p2p) {
1084		rate = IWL_FIRST_OFDM_RATE;
1085	} else {
1086		rate = IWL_FIRST_CCK_RATE;
1087		u.beacon_cmd_v6.tx.rate_n_flags |=
1088					cpu_to_le32(RATE_MCS_CCK_MSK);
1089	}
1090	u.beacon_cmd_v6.tx.rate_n_flags |=
1091		cpu_to_le32(iwl_mvm_mac80211_idx_to_hwrate(rate));
1092
1093	/* Set up TX beacon command fields */
1094	if (vif->type == NL80211_IFTYPE_AP)
1095		iwl_mvm_mac_ctxt_set_tim(mvm, &u.beacon_cmd_v6,
1096					 beacon->data,
1097					 beacon_skb_len);
1098
1099	/* Submit command */
1100
1101	if (fw_has_capa(&mvm->fw->ucode_capa,
1102			IWL_UCODE_TLV_CAPA_CSA_AND_TBTT_OFFLOAD)) {
1103		u.beacon_cmd.csa_offset =
1104			cpu_to_le32(iwl_mvm_find_ie_offset(beacon->data,
1105						    WLAN_EID_CHANNEL_SWITCH,
1106						    beacon_skb_len));
1107		u.beacon_cmd.ecsa_offset =
1108			cpu_to_le32(iwl_mvm_find_ie_offset(beacon->data,
1109						    WLAN_EID_EXT_CHANSWITCH_ANN,
1110						    beacon_skb_len));
1111
1112		cmd.len[0] = sizeof(u.beacon_cmd);
1113	} else {
1114		cmd.len[0] = sizeof(u.beacon_cmd_v6);
1115	}
 
 
 
 
1116
1117	cmd.data[0] = &u;
 
1118	cmd.dataflags[0] = 0;
1119	cmd.len[1] = beacon_skb_len;
1120	cmd.data[1] = beacon->data;
1121	cmd.dataflags[1] = IWL_HCMD_DFL_DUP;
1122
1123	return iwl_mvm_send_cmd(mvm, &cmd);
1124}
1125
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1126/* The beacon template for the AP/GO/IBSS has changed and needs update */
1127int iwl_mvm_mac_ctxt_beacon_changed(struct iwl_mvm *mvm,
1128				    struct ieee80211_vif *vif)
1129{
1130	struct sk_buff *beacon;
1131	int ret;
1132
1133	WARN_ON(vif->type != NL80211_IFTYPE_AP &&
1134		vif->type != NL80211_IFTYPE_ADHOC);
1135
1136	beacon = ieee80211_beacon_get_template(mvm->hw, vif, NULL);
1137	if (!beacon)
1138		return -ENOMEM;
1139
1140	ret = iwl_mvm_mac_ctxt_send_beacon(mvm, vif, beacon);
1141	dev_kfree_skb(beacon);
1142	return ret;
1143}
1144
1145struct iwl_mvm_mac_ap_iterator_data {
1146	struct iwl_mvm *mvm;
1147	struct ieee80211_vif *vif;
1148	u32 beacon_device_ts;
1149	u16 beacon_int;
1150};
1151
1152/* Find the beacon_device_ts and beacon_int for a managed interface */
1153static void iwl_mvm_mac_ap_iterator(void *_data, u8 *mac,
1154				    struct ieee80211_vif *vif)
1155{
1156	struct iwl_mvm_mac_ap_iterator_data *data = _data;
1157
1158	if (vif->type != NL80211_IFTYPE_STATION || !vif->bss_conf.assoc)
1159		return;
1160
1161	/* Station client has higher priority over P2P client*/
1162	if (vif->p2p && data->beacon_device_ts)
1163		return;
1164
1165	data->beacon_device_ts = vif->bss_conf.sync_device_ts;
1166	data->beacon_int = vif->bss_conf.beacon_int;
1167}
1168
1169/*
1170 * Fill the specific data for mac context of type AP of P2P GO
1171 */
1172static void iwl_mvm_mac_ctxt_cmd_fill_ap(struct iwl_mvm *mvm,
1173					 struct ieee80211_vif *vif,
1174					 struct iwl_mac_ctx_cmd *cmd,
1175					 struct iwl_mac_data_ap *ctxt_ap,
1176					 bool add)
1177{
1178	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1179	struct iwl_mvm_mac_ap_iterator_data data = {
1180		.mvm = mvm,
1181		.vif = vif,
1182		.beacon_device_ts = 0
1183	};
1184
1185	/* in AP mode, the MCAST FIFO takes the EDCA params from VO */
1186	cmd->ac[IWL_MVM_TX_FIFO_VO].fifos_mask |= BIT(IWL_MVM_TX_FIFO_MCAST);
1187
1188	/*
1189	 * in AP mode, pass probe requests and beacons from other APs
1190	 * (needed for ht protection); when there're no any associated
1191	 * station don't ask FW to pass beacons to prevent unnecessary
1192	 * wake-ups.
1193	 */
1194	cmd->filter_flags |= cpu_to_le32(MAC_FILTER_IN_PROBE_REQUEST);
1195	if (mvmvif->ap_assoc_sta_count || !mvm->drop_bcn_ap_mode) {
1196		cmd->filter_flags |= cpu_to_le32(MAC_FILTER_IN_BEACON);
1197		IWL_DEBUG_HC(mvm, "Asking FW to pass beacons\n");
1198	} else {
1199		IWL_DEBUG_HC(mvm, "No need to receive beacons\n");
1200	}
1201
1202	ctxt_ap->bi = cpu_to_le32(vif->bss_conf.beacon_int);
1203	ctxt_ap->bi_reciprocal =
1204		cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int));
1205	ctxt_ap->dtim_interval = cpu_to_le32(vif->bss_conf.beacon_int *
1206					     vif->bss_conf.dtim_period);
1207	ctxt_ap->dtim_reciprocal =
1208		cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int *
1209					       vif->bss_conf.dtim_period));
1210
1211	ctxt_ap->mcast_qid = cpu_to_le32(vif->cab_queue);
 
 
1212
1213	/*
1214	 * Only set the beacon time when the MAC is being added, when we
1215	 * just modify the MAC then we should keep the time -- the firmware
1216	 * can otherwise have a "jumping" TBTT.
1217	 */
1218	if (add) {
1219		/*
1220		 * If there is a station/P2P client interface which is
1221		 * associated, set the AP's TBTT far enough from the station's
1222		 * TBTT. Otherwise, set it to the current system time
1223		 */
1224		ieee80211_iterate_active_interfaces_atomic(
1225			mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
1226			iwl_mvm_mac_ap_iterator, &data);
1227
1228		if (data.beacon_device_ts) {
1229			u32 rand = (prandom_u32() % (64 - 36)) + 36;
1230			mvmvif->ap_beacon_time = data.beacon_device_ts +
1231				ieee80211_tu_to_usec(data.beacon_int * rand /
1232						     100);
1233		} else {
1234			mvmvif->ap_beacon_time =
1235				iwl_read_prph(mvm->trans,
1236					      DEVICE_SYSTEM_TIME_REG);
1237		}
1238	}
1239
1240	ctxt_ap->beacon_time = cpu_to_le32(mvmvif->ap_beacon_time);
1241	ctxt_ap->beacon_tsf = 0; /* unused */
1242
1243	/* TODO: Assume that the beacon id == mac context id */
1244	ctxt_ap->beacon_template = cpu_to_le32(mvmvif->id);
1245}
1246
1247static int iwl_mvm_mac_ctxt_cmd_ap(struct iwl_mvm *mvm,
1248				   struct ieee80211_vif *vif,
1249				   u32 action)
1250{
1251	struct iwl_mac_ctx_cmd cmd = {};
1252
1253	WARN_ON(vif->type != NL80211_IFTYPE_AP || vif->p2p);
1254
1255	/* Fill the common data for all mac context types */
1256	iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
1257
1258	/* Fill the data specific for ap mode */
1259	iwl_mvm_mac_ctxt_cmd_fill_ap(mvm, vif, &cmd, &cmd.ap,
1260				     action == FW_CTXT_ACTION_ADD);
1261
1262	return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
1263}
1264
1265static int iwl_mvm_mac_ctxt_cmd_go(struct iwl_mvm *mvm,
1266				   struct ieee80211_vif *vif,
1267				   u32 action)
1268{
1269	struct iwl_mac_ctx_cmd cmd = {};
1270	struct ieee80211_p2p_noa_attr *noa = &vif->bss_conf.p2p_noa_attr;
1271
1272	WARN_ON(vif->type != NL80211_IFTYPE_AP || !vif->p2p);
1273
1274	/* Fill the common data for all mac context types */
1275	iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
1276
1277	/* Fill the data specific for GO mode */
1278	iwl_mvm_mac_ctxt_cmd_fill_ap(mvm, vif, &cmd, &cmd.go.ap,
1279				     action == FW_CTXT_ACTION_ADD);
1280
1281	cmd.go.ctwin = cpu_to_le32(noa->oppps_ctwindow &
1282					IEEE80211_P2P_OPPPS_CTWINDOW_MASK);
1283	cmd.go.opp_ps_enabled =
1284			cpu_to_le32(!!(noa->oppps_ctwindow &
1285					IEEE80211_P2P_OPPPS_ENABLE_BIT));
1286
1287	return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
1288}
1289
1290static int iwl_mvm_mac_ctx_send(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1291				u32 action, bool force_assoc_off,
1292				const u8 *bssid_override)
1293{
1294	switch (vif->type) {
1295	case NL80211_IFTYPE_STATION:
1296		return iwl_mvm_mac_ctxt_cmd_sta(mvm, vif, action,
1297						force_assoc_off,
1298						bssid_override);
1299		break;
1300	case NL80211_IFTYPE_AP:
1301		if (!vif->p2p)
1302			return iwl_mvm_mac_ctxt_cmd_ap(mvm, vif, action);
1303		else
1304			return iwl_mvm_mac_ctxt_cmd_go(mvm, vif, action);
1305		break;
1306	case NL80211_IFTYPE_MONITOR:
1307		return iwl_mvm_mac_ctxt_cmd_listener(mvm, vif, action);
1308	case NL80211_IFTYPE_P2P_DEVICE:
1309		return iwl_mvm_mac_ctxt_cmd_p2p_device(mvm, vif, action);
1310	case NL80211_IFTYPE_ADHOC:
1311		return iwl_mvm_mac_ctxt_cmd_ibss(mvm, vif, action);
1312	default:
1313		break;
1314	}
1315
1316	return -EOPNOTSUPP;
1317}
1318
1319int iwl_mvm_mac_ctxt_add(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1320{
1321	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1322	int ret;
1323
1324	if (WARN_ONCE(mvmvif->uploaded, "Adding active MAC %pM/%d\n",
1325		      vif->addr, ieee80211_vif_type_p2p(vif)))
1326		return -EIO;
1327
1328	ret = iwl_mvm_mac_ctx_send(mvm, vif, FW_CTXT_ACTION_ADD,
1329				   true, NULL);
1330	if (ret)
1331		return ret;
1332
1333	/* will only do anything at resume from D3 time */
1334	iwl_mvm_set_last_nonqos_seq(mvm, vif);
1335
1336	mvmvif->uploaded = true;
1337	return 0;
1338}
1339
1340int iwl_mvm_mac_ctxt_changed(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1341			     bool force_assoc_off, const u8 *bssid_override)
1342{
1343	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1344
1345	if (WARN_ONCE(!mvmvif->uploaded, "Changing inactive MAC %pM/%d\n",
1346		      vif->addr, ieee80211_vif_type_p2p(vif)))
1347		return -EIO;
1348
1349	return iwl_mvm_mac_ctx_send(mvm, vif, FW_CTXT_ACTION_MODIFY,
1350				    force_assoc_off, bssid_override);
1351}
1352
1353int iwl_mvm_mac_ctxt_remove(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1354{
1355	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1356	struct iwl_mac_ctx_cmd cmd;
1357	int ret;
1358
1359	if (WARN_ONCE(!mvmvif->uploaded, "Removing inactive MAC %pM/%d\n",
1360		      vif->addr, ieee80211_vif_type_p2p(vif)))
1361		return -EIO;
1362
1363	memset(&cmd, 0, sizeof(cmd));
1364
1365	cmd.id_and_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
1366							   mvmvif->color));
1367	cmd.action = cpu_to_le32(FW_CTXT_ACTION_REMOVE);
1368
1369	ret = iwl_mvm_send_cmd_pdu(mvm, MAC_CONTEXT_CMD, 0,
1370				   sizeof(cmd), &cmd);
1371	if (ret) {
1372		IWL_ERR(mvm, "Failed to remove MAC context: %d\n", ret);
1373		return ret;
1374	}
1375
1376	mvmvif->uploaded = false;
1377
1378	if (vif->type == NL80211_IFTYPE_MONITOR) {
1379		__clear_bit(IEEE80211_HW_RX_INCLUDES_FCS, mvm->hw->flags);
1380		iwl_mvm_dealloc_snif_sta(mvm);
1381	}
1382
1383	return 0;
1384}
1385
1386static void iwl_mvm_csa_count_down(struct iwl_mvm *mvm,
1387				   struct ieee80211_vif *csa_vif, u32 gp2,
1388				   bool tx_success)
1389{
1390	struct iwl_mvm_vif *mvmvif =
1391			iwl_mvm_vif_from_mac80211(csa_vif);
1392
1393	/* Don't start to countdown from a failed beacon */
1394	if (!tx_success && !mvmvif->csa_countdown)
1395		return;
1396
1397	mvmvif->csa_countdown = true;
1398
1399	if (!ieee80211_csa_is_complete(csa_vif)) {
1400		int c = ieee80211_csa_update_counter(csa_vif);
1401
1402		iwl_mvm_mac_ctxt_beacon_changed(mvm, csa_vif);
1403		if (csa_vif->p2p &&
1404		    !iwl_mvm_te_scheduled(&mvmvif->time_event_data) && gp2 &&
1405		    tx_success) {
1406			u32 rel_time = (c + 1) *
1407				       csa_vif->bss_conf.beacon_int -
1408				       IWL_MVM_CHANNEL_SWITCH_TIME_GO;
1409			u32 apply_time = gp2 + rel_time * 1024;
1410
1411			iwl_mvm_schedule_csa_period(mvm, csa_vif,
1412					 IWL_MVM_CHANNEL_SWITCH_TIME_GO -
1413					 IWL_MVM_CHANNEL_SWITCH_MARGIN,
1414					 apply_time);
1415		}
1416	} else if (!iwl_mvm_te_scheduled(&mvmvif->time_event_data)) {
1417		/* we don't have CSA NoA scheduled yet, switch now */
1418		ieee80211_csa_finish(csa_vif);
1419		RCU_INIT_POINTER(mvm->csa_vif, NULL);
1420	}
1421}
1422
1423void iwl_mvm_rx_beacon_notif(struct iwl_mvm *mvm,
1424			     struct iwl_rx_cmd_buffer *rxb)
1425{
1426	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1427	struct iwl_extended_beacon_notif *beacon = (void *)pkt->data;
1428	struct iwl_mvm_tx_resp *beacon_notify_hdr;
1429	struct ieee80211_vif *csa_vif;
1430	struct ieee80211_vif *tx_blocked_vif;
 
1431	u16 status;
1432
1433	lockdep_assert_held(&mvm->mutex);
1434
1435	beacon_notify_hdr = &beacon->beacon_notify_hdr;
1436	mvm->ap_last_beacon_gp2 = le32_to_cpu(beacon->gp2);
 
1437
1438	status = le16_to_cpu(beacon_notify_hdr->status.status) & TX_STATUS_MSK;
 
1439	IWL_DEBUG_RX(mvm,
1440		     "beacon status %#x retries:%d tsf:0x%16llX gp2:0x%X rate:%d\n",
1441		     status, beacon_notify_hdr->failure_frame,
1442		     le64_to_cpu(beacon->tsf),
1443		     mvm->ap_last_beacon_gp2,
1444		     le32_to_cpu(beacon_notify_hdr->initial_rate));
1445
1446	csa_vif = rcu_dereference_protected(mvm->csa_vif,
1447					    lockdep_is_held(&mvm->mutex));
1448	if (unlikely(csa_vif && csa_vif->csa_active))
1449		iwl_mvm_csa_count_down(mvm, csa_vif, mvm->ap_last_beacon_gp2,
1450				       (status == TX_STATUS_SUCCESS));
1451
1452	tx_blocked_vif = rcu_dereference_protected(mvm->csa_tx_blocked_vif,
1453						lockdep_is_held(&mvm->mutex));
1454	if (unlikely(tx_blocked_vif)) {
1455		struct iwl_mvm_vif *mvmvif =
1456			iwl_mvm_vif_from_mac80211(tx_blocked_vif);
1457
1458		/*
1459		 * The channel switch is started and we have blocked the
1460		 * stations. If this is the first beacon (the timeout wasn't
1461		 * set), set the unblock timeout, otherwise countdown
1462		 */
1463		if (!mvm->csa_tx_block_bcn_timeout)
1464			mvm->csa_tx_block_bcn_timeout =
1465				IWL_MVM_CS_UNBLOCK_TX_TIMEOUT;
1466		else
1467			mvm->csa_tx_block_bcn_timeout--;
1468
1469		/* Check if the timeout is expired, and unblock tx */
1470		if (mvm->csa_tx_block_bcn_timeout == 0) {
1471			iwl_mvm_modify_all_sta_disable_tx(mvm, mvmvif, false);
1472			RCU_INIT_POINTER(mvm->csa_tx_blocked_vif, NULL);
1473		}
1474	}
1475}
1476
1477static void iwl_mvm_beacon_loss_iterator(void *_data, u8 *mac,
1478					 struct ieee80211_vif *vif)
1479{
1480	struct iwl_missed_beacons_notif *missed_beacons = _data;
1481	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1482	struct iwl_mvm *mvm = mvmvif->mvm;
1483	struct iwl_fw_dbg_trigger_missed_bcon *bcon_trig;
1484	struct iwl_fw_dbg_trigger_tlv *trigger;
1485	u32 stop_trig_missed_bcon, stop_trig_missed_bcon_since_rx;
1486	u32 rx_missed_bcon, rx_missed_bcon_since_rx;
1487
1488	if (mvmvif->id != (u16)le32_to_cpu(missed_beacons->mac_id))
1489		return;
1490
1491	rx_missed_bcon = le32_to_cpu(missed_beacons->consec_missed_beacons);
1492	rx_missed_bcon_since_rx =
1493		le32_to_cpu(missed_beacons->consec_missed_beacons_since_last_rx);
1494	/*
1495	 * TODO: the threshold should be adjusted based on latency conditions,
1496	 * and/or in case of a CS flow on one of the other AP vifs.
1497	 */
1498	if (le32_to_cpu(missed_beacons->consec_missed_beacons_since_last_rx) >
1499	     IWL_MVM_MISSED_BEACONS_THRESHOLD)
1500		ieee80211_beacon_loss(vif);
1501
1502	if (!iwl_fw_dbg_trigger_enabled(mvm->fw,
1503					FW_DBG_TRIGGER_MISSED_BEACONS))
1504		return;
1505
1506	trigger = iwl_fw_dbg_get_trigger(mvm->fw,
1507					 FW_DBG_TRIGGER_MISSED_BEACONS);
1508	bcon_trig = (void *)trigger->data;
1509	stop_trig_missed_bcon = le32_to_cpu(bcon_trig->stop_consec_missed_bcon);
1510	stop_trig_missed_bcon_since_rx =
1511		le32_to_cpu(bcon_trig->stop_consec_missed_bcon_since_rx);
1512
1513	/* TODO: implement start trigger */
1514
1515	if (!iwl_fw_dbg_trigger_check_stop(mvm, vif, trigger))
 
 
1516		return;
1517
1518	if (rx_missed_bcon_since_rx >= stop_trig_missed_bcon_since_rx ||
1519	    rx_missed_bcon >= stop_trig_missed_bcon)
1520		iwl_mvm_fw_dbg_collect_trig(mvm, trigger, NULL);
1521}
1522
1523void iwl_mvm_rx_missed_beacons_notif(struct iwl_mvm *mvm,
1524				     struct iwl_rx_cmd_buffer *rxb)
1525{
1526	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1527	struct iwl_missed_beacons_notif *mb = (void *)pkt->data;
1528
1529	IWL_DEBUG_INFO(mvm,
1530		       "missed bcn mac_id=%u, consecutive=%u (%u, %u, %u)\n",
1531		       le32_to_cpu(mb->mac_id),
1532		       le32_to_cpu(mb->consec_missed_beacons),
1533		       le32_to_cpu(mb->consec_missed_beacons_since_last_rx),
1534		       le32_to_cpu(mb->num_recvd_beacons),
1535		       le32_to_cpu(mb->num_expected_beacons));
1536
1537	ieee80211_iterate_active_interfaces_atomic(mvm->hw,
1538						   IEEE80211_IFACE_ITER_NORMAL,
1539						   iwl_mvm_beacon_loss_iterator,
1540						   mb);
1541}
1542
1543void iwl_mvm_rx_stored_beacon_notif(struct iwl_mvm *mvm,
1544				    struct iwl_rx_cmd_buffer *rxb)
1545{
1546	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1547	struct iwl_stored_beacon_notif *sb = (void *)pkt->data;
1548	struct ieee80211_rx_status rx_status;
1549	struct sk_buff *skb;
1550	u32 size = le32_to_cpu(sb->byte_count);
1551
1552	if (size == 0)
1553		return;
1554
1555	skb = alloc_skb(size, GFP_ATOMIC);
1556	if (!skb) {
1557		IWL_ERR(mvm, "alloc_skb failed\n");
1558		return;
1559	}
1560
1561	/* update rx_status according to the notification's metadata */
1562	memset(&rx_status, 0, sizeof(rx_status));
1563	rx_status.mactime = le64_to_cpu(sb->tsf);
1564	/* TSF as indicated by the firmware  is at INA time */
1565	rx_status.flag |= RX_FLAG_MACTIME_PLCP_START;
1566	rx_status.device_timestamp = le32_to_cpu(sb->system_time);
1567	rx_status.band =
1568		(sb->phy_flags & cpu_to_le16(RX_RES_PHY_FLAGS_BAND_24)) ?
1569				NL80211_BAND_2GHZ : NL80211_BAND_5GHZ;
1570	rx_status.freq =
1571		ieee80211_channel_to_frequency(le16_to_cpu(sb->channel),
1572					       rx_status.band);
1573
1574	/* copy the data */
1575	memcpy(skb_put(skb, size), sb->data, size);
1576	memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
1577
1578	/* pass it as regular rx to mac80211 */
1579	ieee80211_rx_napi(mvm->hw, NULL, skb, NULL);
1580}
1581
1582void iwl_mvm_channel_switch_noa_notif(struct iwl_mvm *mvm,
1583				      struct iwl_rx_cmd_buffer *rxb)
1584{
1585	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1586	struct iwl_channel_switch_noa_notif *notif = (void *)pkt->data;
1587	struct ieee80211_vif *csa_vif;
1588	struct iwl_mvm_vif *mvmvif;
1589	int len = iwl_rx_packet_payload_len(pkt);
1590	u32 id_n_color;
1591
1592	if (WARN_ON_ONCE(len < sizeof(*notif)))
1593		return;
1594
1595	rcu_read_lock();
1596
1597	csa_vif = rcu_dereference(mvm->csa_vif);
1598	if (WARN_ON(!csa_vif || !csa_vif->csa_active))
1599		goto out_unlock;
1600
1601	id_n_color = le32_to_cpu(notif->id_and_color);
1602
1603	mvmvif = iwl_mvm_vif_from_mac80211(csa_vif);
1604	if (WARN(FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color) != id_n_color,
1605		 "channel switch noa notification on unexpected vif (csa_vif=%d, notif=%d)",
1606		 FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color), id_n_color))
1607		goto out_unlock;
1608
1609	IWL_DEBUG_INFO(mvm, "Channel Switch Started Notification\n");
1610
1611	queue_delayed_work(system_wq, &mvm->cs_tx_unblock_dwork,
1612			   msecs_to_jiffies(IWL_MVM_CS_UNBLOCK_TX_TIMEOUT *
1613					    csa_vif->bss_conf.beacon_int));
1614
1615	ieee80211_csa_finish(csa_vif);
1616
1617	rcu_read_unlock();
1618
1619	RCU_INIT_POINTER(mvm->csa_vif, NULL);
1620
1621	return;
1622
1623out_unlock:
1624	rcu_read_unlock();
1625}