mac-ctxt.c revision 6e97b0d2984a7f80b1d6ba4f62d84d2ce8106069
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 - 2013 Intel Corporation. All rights reserved.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of version 2 of the GNU General Public License as
12 * published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
22 * USA
23 *
24 * The full GNU General Public License is included in this distribution
25 * in the file called COPYING.
26 *
27 * Contact Information:
28 *  Intel Linux Wireless <ilw@linux.intel.com>
29 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
30 *
31 * BSD LICENSE
32 *
33 * Copyright(c) 2012 - 2013 Intel Corporation. All rights reserved.
34 * All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 *
40 *  * Redistributions of source code must retain the above copyright
41 *    notice, this list of conditions and the following disclaimer.
42 *  * Redistributions in binary form must reproduce the above copyright
43 *    notice, this list of conditions and the following disclaimer in
44 *    the documentation and/or other materials provided with the
45 *    distribution.
46 *  * Neither the name Intel Corporation nor the names of its
47 *    contributors may be used to endorse or promote products derived
48 *    from this software without specific prior written permission.
49 *
50 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
51 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
52 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
53 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
54 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
55 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
56 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
57 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
58 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
59 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
60 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
61 *
62 *****************************************************************************/
63
64#include <linux/etherdevice.h>
65#include <net/mac80211.h>
66#include "iwl-io.h"
67#include "iwl-prph.h"
68#include "fw-api.h"
69#include "mvm.h"
70
71const u8 iwl_mvm_ac_to_tx_fifo[] = {
72	IWL_MVM_TX_FIFO_VO,
73	IWL_MVM_TX_FIFO_VI,
74	IWL_MVM_TX_FIFO_BE,
75	IWL_MVM_TX_FIFO_BK,
76};
77
78struct iwl_mvm_mac_iface_iterator_data {
79	struct iwl_mvm *mvm;
80	struct ieee80211_vif *vif;
81	unsigned long available_mac_ids[BITS_TO_LONGS(NUM_MAC_INDEX_DRIVER)];
82	unsigned long available_tsf_ids[BITS_TO_LONGS(NUM_TSF_IDS)];
83	unsigned long used_hw_queues[BITS_TO_LONGS(IWL_MVM_MAX_QUEUES)];
84	enum iwl_tsf_id preferred_tsf;
85	bool found_vif;
86};
87
88static void iwl_mvm_mac_tsf_id_iter(void *_data, u8 *mac,
89				    struct ieee80211_vif *vif)
90{
91	struct iwl_mvm_mac_iface_iterator_data *data = _data;
92	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
93
94	/* Skip the interface for which we are trying to assign a tsf_id  */
95	if (vif == data->vif)
96		return;
97
98	/*
99	 * The TSF is a hardware/firmware resource, there are 4 and
100	 * the driver should assign and free them as needed. However,
101	 * there are cases where 2 MACs should share the same TSF ID
102	 * for the purpose of clock sync, an optimization to avoid
103	 * clock drift causing overlapping TBTTs/DTIMs for a GO and
104	 * client in the system.
105	 *
106	 * The firmware will decide according to the MAC type which
107	 * will be the master and slave. Clients that need to sync
108	 * with a remote station will be the master, and an AP or GO
109	 * will be the slave.
110	 *
111	 * Depending on the new interface type it can be slaved to
112	 * or become the master of an existing interface.
113	 */
114	switch (data->vif->type) {
115	case NL80211_IFTYPE_STATION:
116		/*
117		 * The new interface is client, so if the existing one
118		 * we're iterating is an AP, and both interfaces have the
119		 * same beacon interval, the same TSF should be used to
120		 * avoid drift between the new client and existing AP,
121		 * the existing AP will get drift updates from the new
122		 * client context in this case
123		 */
124		if (vif->type == NL80211_IFTYPE_AP) {
125			if (data->preferred_tsf == NUM_TSF_IDS &&
126			    test_bit(mvmvif->tsf_id, data->available_tsf_ids) &&
127			    (vif->bss_conf.beacon_int ==
128			     data->vif->bss_conf.beacon_int)) {
129				data->preferred_tsf = mvmvif->tsf_id;
130				return;
131			}
132		}
133		break;
134	case NL80211_IFTYPE_AP:
135		/*
136		 * The new interface is AP/GO, so in case both interfaces
137		 * have the same beacon interval, it should get drift
138		 * updates from an existing client or use the same
139		 * TSF as an existing GO. There's no drift between
140		 * TSFs internally but if they used different TSFs
141		 * then a new client MAC could update one of them
142		 * and cause drift that way.
143		 */
144		if (vif->type == NL80211_IFTYPE_STATION ||
145		    vif->type == NL80211_IFTYPE_AP) {
146			if (data->preferred_tsf == NUM_TSF_IDS &&
147			    test_bit(mvmvif->tsf_id, data->available_tsf_ids) &&
148			    (vif->bss_conf.beacon_int ==
149			     data->vif->bss_conf.beacon_int)) {
150				data->preferred_tsf = mvmvif->tsf_id;
151				return;
152			}
153		}
154		break;
155	default:
156		/*
157		 * For all other interface types there's no need to
158		 * take drift into account. Either they're exclusive
159		 * like IBSS and monitor, or we don't care much about
160		 * their TSF (like P2P Device), but we won't be able
161		 * to share the TSF resource.
162		 */
163		break;
164	}
165
166	/*
167	 * Unless we exited above, we can't share the TSF resource
168	 * that the virtual interface we're iterating over is using
169	 * with the new one, so clear the available bit and if this
170	 * was the preferred one, reset that as well.
171	 */
172	__clear_bit(mvmvif->tsf_id, data->available_tsf_ids);
173
174	if (data->preferred_tsf == mvmvif->tsf_id)
175		data->preferred_tsf = NUM_TSF_IDS;
176}
177
178static void iwl_mvm_mac_iface_iterator(void *_data, u8 *mac,
179				       struct ieee80211_vif *vif)
180{
181	struct iwl_mvm_mac_iface_iterator_data *data = _data;
182	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
183	u32 ac;
184
185	/* Iterator may already find the interface being added -- skip it */
186	if (vif == data->vif) {
187		data->found_vif = true;
188		return;
189	}
190
191	/* Mark the queues used by the vif */
192	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
193		if (vif->hw_queue[ac] != IEEE80211_INVAL_HW_QUEUE)
194			__set_bit(vif->hw_queue[ac], data->used_hw_queues);
195
196	if (vif->cab_queue != IEEE80211_INVAL_HW_QUEUE)
197		__set_bit(vif->cab_queue, data->used_hw_queues);
198
199	/* Mark MAC IDs as used by clearing the available bit, and
200	 * (below) mark TSFs as used if their existing use is not
201	 * compatible with the new interface type.
202	 * No locking or atomic bit operations are needed since the
203	 * data is on the stack of the caller function.
204	 */
205	__clear_bit(mvmvif->id, data->available_mac_ids);
206
207	/* find a suitable tsf_id */
208	iwl_mvm_mac_tsf_id_iter(_data, mac, vif);
209}
210
211/*
212 * Get the mask of the queus used by the vif
213 */
214u32 iwl_mvm_mac_get_queues_mask(struct iwl_mvm *mvm,
215				struct ieee80211_vif *vif)
216{
217	u32 qmask = 0, ac;
218
219	if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
220		return BIT(IWL_MVM_OFFCHANNEL_QUEUE);
221
222	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
223		if (vif->hw_queue[ac] != IEEE80211_INVAL_HW_QUEUE)
224			qmask |= BIT(vif->hw_queue[ac]);
225
226	return qmask;
227}
228
229void iwl_mvm_mac_ctxt_recalc_tsf_id(struct iwl_mvm *mvm,
230				    struct ieee80211_vif *vif)
231{
232	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
233	struct iwl_mvm_mac_iface_iterator_data data = {
234		.mvm = mvm,
235		.vif = vif,
236		.available_tsf_ids = { (1 << NUM_TSF_IDS) - 1 },
237		/* no preference yet */
238		.preferred_tsf = NUM_TSF_IDS,
239	};
240
241	ieee80211_iterate_active_interfaces_atomic(
242		mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
243		iwl_mvm_mac_tsf_id_iter, &data);
244
245	if (data.preferred_tsf != NUM_TSF_IDS)
246		mvmvif->tsf_id = data.preferred_tsf;
247	else if (!test_bit(mvmvif->tsf_id, data.available_tsf_ids))
248		mvmvif->tsf_id = find_first_bit(data.available_tsf_ids,
249						NUM_TSF_IDS);
250}
251
252static int iwl_mvm_mac_ctxt_allocate_resources(struct iwl_mvm *mvm,
253					       struct ieee80211_vif *vif)
254{
255	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
256	struct iwl_mvm_mac_iface_iterator_data data = {
257		.mvm = mvm,
258		.vif = vif,
259		.available_mac_ids = { (1 << NUM_MAC_INDEX_DRIVER) - 1 },
260		.available_tsf_ids = { (1 << NUM_TSF_IDS) - 1 },
261		/* no preference yet */
262		.preferred_tsf = NUM_TSF_IDS,
263		.used_hw_queues = {
264			BIT(IWL_MVM_OFFCHANNEL_QUEUE) |
265			BIT(mvm->aux_queue) |
266			BIT(IWL_MVM_CMD_QUEUE)
267		},
268		.found_vif = false,
269	};
270	u32 ac;
271	int ret, i;
272
273	/*
274	 * Allocate a MAC ID and a TSF for this MAC, along with the queues
275	 * and other resources.
276	 */
277
278	/*
279	 * Before the iterator, we start with all MAC IDs and TSFs available.
280	 *
281	 * During iteration, all MAC IDs are cleared that are in use by other
282	 * virtual interfaces, and all TSF IDs are cleared that can't be used
283	 * by this new virtual interface because they're used by an interface
284	 * that can't share it with the new one.
285	 * At the same time, we check if there's a preferred TSF in the case
286	 * that we should share it with another interface.
287	 */
288
289	/* Currently, MAC ID 0 should be used only for the managed/IBSS vif */
290	switch (vif->type) {
291	case NL80211_IFTYPE_ADHOC:
292		break;
293	case NL80211_IFTYPE_STATION:
294		if (!vif->p2p)
295			break;
296		/* fall through */
297	default:
298		__clear_bit(0, data.available_mac_ids);
299	}
300
301	ieee80211_iterate_active_interfaces_atomic(
302		mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
303		iwl_mvm_mac_iface_iterator, &data);
304
305	/*
306	 * In the case we're getting here during resume, it's similar to
307	 * firmware restart, and with RESUME_ALL the iterator will find
308	 * the vif being added already.
309	 * We don't want to reassign any IDs in either case since doing
310	 * so would probably assign different IDs (as interfaces aren't
311	 * necessarily added in the same order), but the old IDs were
312	 * preserved anyway, so skip ID assignment for both resume and
313	 * recovery.
314	 */
315	if (data.found_vif)
316		return 0;
317
318	/* Therefore, in recovery, we can't get here */
319	if (WARN_ON_ONCE(test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)))
320		return -EBUSY;
321
322	mvmvif->id = find_first_bit(data.available_mac_ids,
323				    NUM_MAC_INDEX_DRIVER);
324	if (mvmvif->id == NUM_MAC_INDEX_DRIVER) {
325		IWL_ERR(mvm, "Failed to init MAC context - no free ID!\n");
326		ret = -EIO;
327		goto exit_fail;
328	}
329
330	if (data.preferred_tsf != NUM_TSF_IDS)
331		mvmvif->tsf_id = data.preferred_tsf;
332	else
333		mvmvif->tsf_id = find_first_bit(data.available_tsf_ids,
334						NUM_TSF_IDS);
335	if (mvmvif->tsf_id == NUM_TSF_IDS) {
336		IWL_ERR(mvm, "Failed to init MAC context - no free TSF!\n");
337		ret = -EIO;
338		goto exit_fail;
339	}
340
341	mvmvif->color = 0;
342
343	INIT_LIST_HEAD(&mvmvif->time_event_data.list);
344	mvmvif->time_event_data.id = TE_MAX;
345
346	/* No need to allocate data queues to P2P Device MAC.*/
347	if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
348		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
349			vif->hw_queue[ac] = IEEE80211_INVAL_HW_QUEUE;
350
351		return 0;
352	}
353
354	/* Find available queues, and allocate them to the ACs */
355	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
356		u8 queue = find_first_zero_bit(data.used_hw_queues,
357					       mvm->first_agg_queue);
358
359		if (queue >= mvm->first_agg_queue) {
360			IWL_ERR(mvm, "Failed to allocate queue\n");
361			ret = -EIO;
362			goto exit_fail;
363		}
364
365		__set_bit(queue, data.used_hw_queues);
366		vif->hw_queue[ac] = queue;
367	}
368
369	/* Allocate the CAB queue for softAP and GO interfaces */
370	if (vif->type == NL80211_IFTYPE_AP) {
371		u8 queue = find_first_zero_bit(data.used_hw_queues,
372					       mvm->first_agg_queue);
373
374		if (queue >= mvm->first_agg_queue) {
375			IWL_ERR(mvm, "Failed to allocate cab queue\n");
376			ret = -EIO;
377			goto exit_fail;
378		}
379
380		vif->cab_queue = queue;
381	} else {
382		vif->cab_queue = IEEE80211_INVAL_HW_QUEUE;
383	}
384
385	mvmvif->bcast_sta.sta_id = IWL_MVM_STATION_COUNT;
386	mvmvif->ap_sta_id = IWL_MVM_STATION_COUNT;
387
388	for (i = 0; i < NUM_IWL_MVM_SMPS_REQ; i++)
389		mvmvif->smps_requests[i] = IEEE80211_SMPS_AUTOMATIC;
390
391	return 0;
392
393exit_fail:
394	memset(mvmvif, 0, sizeof(struct iwl_mvm_vif));
395	memset(vif->hw_queue, IEEE80211_INVAL_HW_QUEUE, sizeof(vif->hw_queue));
396	vif->cab_queue = IEEE80211_INVAL_HW_QUEUE;
397	return ret;
398}
399
400int iwl_mvm_mac_ctxt_init(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
401{
402	u32 ac;
403	int ret;
404
405	lockdep_assert_held(&mvm->mutex);
406
407	ret = iwl_mvm_mac_ctxt_allocate_resources(mvm, vif);
408	if (ret)
409		return ret;
410
411	switch (vif->type) {
412	case NL80211_IFTYPE_P2P_DEVICE:
413		iwl_trans_ac_txq_enable(mvm->trans, IWL_MVM_OFFCHANNEL_QUEUE,
414					IWL_MVM_TX_FIFO_VO);
415		break;
416	case NL80211_IFTYPE_AP:
417		iwl_trans_ac_txq_enable(mvm->trans, vif->cab_queue,
418					IWL_MVM_TX_FIFO_MCAST);
419		/* fall through */
420	default:
421		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
422			iwl_trans_ac_txq_enable(mvm->trans, vif->hw_queue[ac],
423						iwl_mvm_ac_to_tx_fifo[ac]);
424		break;
425	}
426
427	return 0;
428}
429
430void iwl_mvm_mac_ctxt_release(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
431{
432	int ac;
433
434	lockdep_assert_held(&mvm->mutex);
435
436	switch (vif->type) {
437	case NL80211_IFTYPE_P2P_DEVICE:
438		iwl_trans_txq_disable(mvm->trans, IWL_MVM_OFFCHANNEL_QUEUE);
439		break;
440	case NL80211_IFTYPE_AP:
441		iwl_trans_txq_disable(mvm->trans, vif->cab_queue);
442		/* fall through */
443	default:
444		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
445			iwl_trans_txq_disable(mvm->trans, vif->hw_queue[ac]);
446	}
447}
448
449static void iwl_mvm_ack_rates(struct iwl_mvm *mvm,
450			      struct ieee80211_vif *vif,
451			      enum ieee80211_band band,
452			      u8 *cck_rates, u8 *ofdm_rates)
453{
454	struct ieee80211_supported_band *sband;
455	unsigned long basic = vif->bss_conf.basic_rates;
456	int lowest_present_ofdm = 100;
457	int lowest_present_cck = 100;
458	u8 cck = 0;
459	u8 ofdm = 0;
460	int i;
461
462	sband = mvm->hw->wiphy->bands[band];
463
464	for_each_set_bit(i, &basic, BITS_PER_LONG) {
465		int hw = sband->bitrates[i].hw_value;
466		if (hw >= IWL_FIRST_OFDM_RATE) {
467			ofdm |= BIT(hw - IWL_FIRST_OFDM_RATE);
468			if (lowest_present_ofdm > hw)
469				lowest_present_ofdm = hw;
470		} else {
471			BUILD_BUG_ON(IWL_FIRST_CCK_RATE != 0);
472
473			cck |= BIT(hw);
474			if (lowest_present_cck > hw)
475				lowest_present_cck = hw;
476		}
477	}
478
479	/*
480	 * Now we've got the basic rates as bitmaps in the ofdm and cck
481	 * variables. This isn't sufficient though, as there might not
482	 * be all the right rates in the bitmap. E.g. if the only basic
483	 * rates are 5.5 Mbps and 11 Mbps, we still need to add 1 Mbps
484	 * and 6 Mbps because the 802.11-2007 standard says in 9.6:
485	 *
486	 *    [...] a STA responding to a received frame shall transmit
487	 *    its Control Response frame [...] at the highest rate in the
488	 *    BSSBasicRateSet parameter that is less than or equal to the
489	 *    rate of the immediately previous frame in the frame exchange
490	 *    sequence ([...]) and that is of the same modulation class
491	 *    ([...]) as the received frame. If no rate contained in the
492	 *    BSSBasicRateSet parameter meets these conditions, then the
493	 *    control frame sent in response to a received frame shall be
494	 *    transmitted at the highest mandatory rate of the PHY that is
495	 *    less than or equal to the rate of the received frame, and
496	 *    that is of the same modulation class as the received frame.
497	 *
498	 * As a consequence, we need to add all mandatory rates that are
499	 * lower than all of the basic rates to these bitmaps.
500	 */
501
502	if (IWL_RATE_24M_INDEX < lowest_present_ofdm)
503		ofdm |= IWL_RATE_BIT_MSK(24) >> IWL_FIRST_OFDM_RATE;
504	if (IWL_RATE_12M_INDEX < lowest_present_ofdm)
505		ofdm |= IWL_RATE_BIT_MSK(12) >> IWL_FIRST_OFDM_RATE;
506	/* 6M already there or needed so always add */
507	ofdm |= IWL_RATE_BIT_MSK(6) >> IWL_FIRST_OFDM_RATE;
508
509	/*
510	 * CCK is a bit more complex with DSSS vs. HR/DSSS vs. ERP.
511	 * Note, however:
512	 *  - if no CCK rates are basic, it must be ERP since there must
513	 *    be some basic rates at all, so they're OFDM => ERP PHY
514	 *    (or we're in 5 GHz, and the cck bitmap will never be used)
515	 *  - if 11M is a basic rate, it must be ERP as well, so add 5.5M
516	 *  - if 5.5M is basic, 1M and 2M are mandatory
517	 *  - if 2M is basic, 1M is mandatory
518	 *  - if 1M is basic, that's the only valid ACK rate.
519	 * As a consequence, it's not as complicated as it sounds, just add
520	 * any lower rates to the ACK rate bitmap.
521	 */
522	if (IWL_RATE_11M_INDEX < lowest_present_cck)
523		cck |= IWL_RATE_BIT_MSK(11) >> IWL_FIRST_CCK_RATE;
524	if (IWL_RATE_5M_INDEX < lowest_present_cck)
525		cck |= IWL_RATE_BIT_MSK(5) >> IWL_FIRST_CCK_RATE;
526	if (IWL_RATE_2M_INDEX < lowest_present_cck)
527		cck |= IWL_RATE_BIT_MSK(2) >> IWL_FIRST_CCK_RATE;
528	/* 1M already there or needed so always add */
529	cck |= IWL_RATE_BIT_MSK(1) >> IWL_FIRST_CCK_RATE;
530
531	*cck_rates = cck;
532	*ofdm_rates = ofdm;
533}
534
535static void iwl_mvm_mac_ctxt_set_ht_flags(struct iwl_mvm *mvm,
536					 struct ieee80211_vif *vif,
537					 struct iwl_mac_ctx_cmd *cmd)
538{
539	/* for both sta and ap, ht_operation_mode hold the protection_mode */
540	u8 protection_mode = vif->bss_conf.ht_operation_mode &
541				 IEEE80211_HT_OP_MODE_PROTECTION;
542	/* The fw does not distinguish between ht and fat */
543	u32 ht_flag = MAC_PROT_FLG_HT_PROT | MAC_PROT_FLG_FAT_PROT;
544
545	IWL_DEBUG_RATE(mvm, "protection mode set to %d\n", protection_mode);
546	/*
547	 * See section 9.23.3.1 of IEEE 80211-2012.
548	 * Nongreenfield HT STAs Present is not supported.
549	 */
550	switch (protection_mode) {
551	case IEEE80211_HT_OP_MODE_PROTECTION_NONE:
552		break;
553	case IEEE80211_HT_OP_MODE_PROTECTION_NONMEMBER:
554	case IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED:
555		cmd->protection_flags |= cpu_to_le32(ht_flag);
556		break;
557	case IEEE80211_HT_OP_MODE_PROTECTION_20MHZ:
558		/* Protect when channel wider than 20MHz */
559		if (vif->bss_conf.chandef.width > NL80211_CHAN_WIDTH_20)
560			cmd->protection_flags |= cpu_to_le32(ht_flag);
561		break;
562	default:
563		IWL_ERR(mvm, "Illegal protection mode %d\n",
564			protection_mode);
565		break;
566	}
567}
568
569static void iwl_mvm_mac_ctxt_cmd_common(struct iwl_mvm *mvm,
570					struct ieee80211_vif *vif,
571					struct iwl_mac_ctx_cmd *cmd,
572					u32 action)
573{
574	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
575	struct ieee80211_chanctx_conf *chanctx;
576	bool ht_enabled = !!(vif->bss_conf.ht_operation_mode &
577			     IEEE80211_HT_OP_MODE_PROTECTION);
578	u8 cck_ack_rates, ofdm_ack_rates;
579	int i;
580
581	cmd->id_and_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
582							    mvmvif->color));
583	cmd->action = cpu_to_le32(action);
584
585	switch (vif->type) {
586	case NL80211_IFTYPE_STATION:
587		if (vif->p2p)
588			cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_P2P_STA);
589		else
590			cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_BSS_STA);
591		break;
592	case NL80211_IFTYPE_AP:
593		cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_GO);
594		break;
595	case NL80211_IFTYPE_MONITOR:
596		cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_LISTENER);
597		break;
598	case NL80211_IFTYPE_P2P_DEVICE:
599		cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_P2P_DEVICE);
600		break;
601	case NL80211_IFTYPE_ADHOC:
602		cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_IBSS);
603		break;
604	default:
605		WARN_ON_ONCE(1);
606	}
607
608	cmd->tsf_id = cpu_to_le32(mvmvif->tsf_id);
609
610	memcpy(cmd->node_addr, vif->addr, ETH_ALEN);
611	if (vif->bss_conf.bssid)
612		memcpy(cmd->bssid_addr, vif->bss_conf.bssid, ETH_ALEN);
613	else
614		eth_broadcast_addr(cmd->bssid_addr);
615
616	rcu_read_lock();
617	chanctx = rcu_dereference(vif->chanctx_conf);
618	iwl_mvm_ack_rates(mvm, vif, chanctx ? chanctx->def.chan->band
619					    : IEEE80211_BAND_2GHZ,
620			  &cck_ack_rates, &ofdm_ack_rates);
621	rcu_read_unlock();
622
623	cmd->cck_rates = cpu_to_le32((u32)cck_ack_rates);
624	cmd->ofdm_rates = cpu_to_le32((u32)ofdm_ack_rates);
625
626	cmd->cck_short_preamble =
627		cpu_to_le32(vif->bss_conf.use_short_preamble ?
628			    MAC_FLG_SHORT_PREAMBLE : 0);
629	cmd->short_slot =
630		cpu_to_le32(vif->bss_conf.use_short_slot ?
631			    MAC_FLG_SHORT_SLOT : 0);
632
633	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
634		u8 txf = iwl_mvm_ac_to_tx_fifo[i];
635
636		cmd->ac[txf].cw_min =
637			cpu_to_le16(mvmvif->queue_params[i].cw_min);
638		cmd->ac[txf].cw_max =
639			cpu_to_le16(mvmvif->queue_params[i].cw_max);
640		cmd->ac[txf].edca_txop =
641			cpu_to_le16(mvmvif->queue_params[i].txop * 32);
642		cmd->ac[txf].aifsn = mvmvif->queue_params[i].aifs;
643		cmd->ac[txf].fifos_mask = BIT(txf);
644	}
645
646	/* in AP mode, the MCAST FIFO takes the EDCA params from VO */
647	if (vif->type == NL80211_IFTYPE_AP)
648		cmd->ac[IWL_MVM_TX_FIFO_VO].fifos_mask |=
649			BIT(IWL_MVM_TX_FIFO_MCAST);
650
651	if (vif->bss_conf.qos)
652		cmd->qos_flags |= cpu_to_le32(MAC_QOS_FLG_UPDATE_EDCA);
653
654	/* Don't use cts to self as the fw doesn't support it currently. */
655	if (vif->bss_conf.use_cts_prot) {
656		cmd->protection_flags |= cpu_to_le32(MAC_PROT_FLG_TGG_PROTECT);
657		if (IWL_UCODE_API(mvm->fw->ucode_ver) >= 8)
658			cmd->protection_flags |=
659				cpu_to_le32(MAC_PROT_FLG_SELF_CTS_EN);
660	}
661	IWL_DEBUG_RATE(mvm, "use_cts_prot %d, ht_operation_mode %d\n",
662		       vif->bss_conf.use_cts_prot,
663		       vif->bss_conf.ht_operation_mode);
664	if (vif->bss_conf.chandef.width != NL80211_CHAN_WIDTH_20_NOHT)
665		cmd->qos_flags |= cpu_to_le32(MAC_QOS_FLG_TGN);
666	if (ht_enabled)
667		iwl_mvm_mac_ctxt_set_ht_flags(mvm, vif, cmd);
668
669	cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP);
670}
671
672static int iwl_mvm_mac_ctxt_send_cmd(struct iwl_mvm *mvm,
673				     struct iwl_mac_ctx_cmd *cmd)
674{
675	int ret = iwl_mvm_send_cmd_pdu(mvm, MAC_CONTEXT_CMD, CMD_SYNC,
676				       sizeof(*cmd), cmd);
677	if (ret)
678		IWL_ERR(mvm, "Failed to send MAC context (action:%d): %d\n",
679			le32_to_cpu(cmd->action), ret);
680	return ret;
681}
682
683/*
684 * Fill the specific data for mac context of type station or p2p client
685 */
686static void iwl_mvm_mac_ctxt_cmd_fill_sta(struct iwl_mvm *mvm,
687					  struct ieee80211_vif *vif,
688					  struct iwl_mac_data_sta *ctxt_sta,
689					  bool force_assoc_off)
690{
691	/* We need the dtim_period to set the MAC as associated */
692	if (vif->bss_conf.assoc && vif->bss_conf.dtim_period &&
693	    !force_assoc_off) {
694		u32 dtim_offs;
695
696		/*
697		 * The DTIM count counts down, so when it is N that means N
698		 * more beacon intervals happen until the DTIM TBTT. Therefore
699		 * add this to the current time. If that ends up being in the
700		 * future, the firmware will handle it.
701		 *
702		 * Also note that the system_timestamp (which we get here as
703		 * "sync_device_ts") and TSF timestamp aren't at exactly the
704		 * same offset in the frame -- the TSF is at the first symbol
705		 * of the TSF, the system timestamp is at signal acquisition
706		 * time. This means there's an offset between them of at most
707		 * a few hundred microseconds (24 * 8 bits + PLCP time gives
708		 * 384us in the longest case), this is currently not relevant
709		 * as the firmware wakes up around 2ms before the TBTT.
710		 */
711		dtim_offs = vif->bss_conf.sync_dtim_count *
712				vif->bss_conf.beacon_int;
713		/* convert TU to usecs */
714		dtim_offs *= 1024;
715
716		ctxt_sta->dtim_tsf =
717			cpu_to_le64(vif->bss_conf.sync_tsf + dtim_offs);
718		ctxt_sta->dtim_time =
719			cpu_to_le32(vif->bss_conf.sync_device_ts + dtim_offs);
720
721		IWL_DEBUG_INFO(mvm, "DTIM TBTT is 0x%llx/0x%x, offset %d\n",
722			       le64_to_cpu(ctxt_sta->dtim_tsf),
723			       le32_to_cpu(ctxt_sta->dtim_time),
724			       dtim_offs);
725
726		ctxt_sta->is_assoc = cpu_to_le32(1);
727	} else {
728		ctxt_sta->is_assoc = cpu_to_le32(0);
729	}
730
731	ctxt_sta->bi = cpu_to_le32(vif->bss_conf.beacon_int);
732	ctxt_sta->bi_reciprocal =
733		cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int));
734	ctxt_sta->dtim_interval = cpu_to_le32(vif->bss_conf.beacon_int *
735					      vif->bss_conf.dtim_period);
736	ctxt_sta->dtim_reciprocal =
737		cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int *
738					       vif->bss_conf.dtim_period));
739
740	ctxt_sta->listen_interval = cpu_to_le32(mvm->hw->conf.listen_interval);
741	ctxt_sta->assoc_id = cpu_to_le32(vif->bss_conf.aid);
742}
743
744static int iwl_mvm_mac_ctxt_cmd_station(struct iwl_mvm *mvm,
745					struct ieee80211_vif *vif,
746					u32 action)
747{
748	struct iwl_mac_ctx_cmd cmd = {};
749
750	WARN_ON(vif->type != NL80211_IFTYPE_STATION || vif->p2p);
751
752	/* Fill the common data for all mac context types */
753	iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, action);
754
755	/* Allow beacons to pass through as long as we are not associated,or we
756	 * do not have dtim period information */
757	if (!vif->bss_conf.assoc || !vif->bss_conf.dtim_period)
758		cmd.filter_flags |= cpu_to_le32(MAC_FILTER_IN_BEACON);
759	else
760		cmd.filter_flags &= ~cpu_to_le32(MAC_FILTER_IN_BEACON);
761
762	/* Fill the data specific for station mode */
763	iwl_mvm_mac_ctxt_cmd_fill_sta(mvm, vif, &cmd.sta,
764				      action == FW_CTXT_ACTION_ADD);
765
766	return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
767}
768
769static int iwl_mvm_mac_ctxt_cmd_p2p_client(struct iwl_mvm *mvm,
770					   struct ieee80211_vif *vif,
771					   u32 action)
772{
773	struct iwl_mac_ctx_cmd cmd = {};
774	struct ieee80211_p2p_noa_attr *noa = &vif->bss_conf.p2p_noa_attr;
775
776	WARN_ON(vif->type != NL80211_IFTYPE_STATION || !vif->p2p);
777
778	/* Fill the common data for all mac context types */
779	iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, action);
780
781	/* Fill the data specific for station mode */
782	iwl_mvm_mac_ctxt_cmd_fill_sta(mvm, vif, &cmd.p2p_sta.sta,
783				      action == FW_CTXT_ACTION_ADD);
784
785	cmd.p2p_sta.ctwin = cpu_to_le32(noa->oppps_ctwindow &
786					IEEE80211_P2P_OPPPS_CTWINDOW_MASK);
787
788	return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
789}
790
791static int iwl_mvm_mac_ctxt_cmd_listener(struct iwl_mvm *mvm,
792					 struct ieee80211_vif *vif,
793					 u32 action)
794{
795	struct iwl_mac_ctx_cmd cmd = {};
796
797	WARN_ON(vif->type != NL80211_IFTYPE_MONITOR);
798
799	iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, action);
800
801	cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_PROMISC |
802				       MAC_FILTER_IN_CONTROL_AND_MGMT |
803				       MAC_FILTER_IN_BEACON |
804				       MAC_FILTER_IN_PROBE_REQUEST |
805				       MAC_FILTER_IN_CRC32);
806	mvm->hw->flags |= IEEE80211_HW_RX_INCLUDES_FCS;
807
808	return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
809}
810
811static int iwl_mvm_mac_ctxt_cmd_ibss(struct iwl_mvm *mvm,
812				     struct ieee80211_vif *vif,
813				     u32 action)
814{
815	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
816	struct iwl_mac_ctx_cmd cmd = {};
817
818	WARN_ON(vif->type != NL80211_IFTYPE_ADHOC);
819
820	iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, action);
821
822	cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_BEACON |
823				       MAC_FILTER_IN_PROBE_REQUEST);
824
825	/* cmd.ibss.beacon_time/cmd.ibss.beacon_tsf are curently ignored */
826	cmd.ibss.bi = cpu_to_le32(vif->bss_conf.beacon_int);
827	cmd.ibss.bi_reciprocal =
828		cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int));
829
830	/* TODO: Assumes that the beacon id == mac context id */
831	cmd.ibss.beacon_template = cpu_to_le32(mvmvif->id);
832
833	return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
834}
835
836struct iwl_mvm_go_iterator_data {
837	bool go_active;
838};
839
840static void iwl_mvm_go_iterator(void *_data, u8 *mac, struct ieee80211_vif *vif)
841{
842	struct iwl_mvm_go_iterator_data *data = _data;
843	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
844
845	if (vif->type == NL80211_IFTYPE_AP && vif->p2p &&
846	    mvmvif->ap_ibss_active)
847		data->go_active = true;
848}
849
850static int iwl_mvm_mac_ctxt_cmd_p2p_device(struct iwl_mvm *mvm,
851					   struct ieee80211_vif *vif,
852					   u32 action)
853{
854	struct iwl_mac_ctx_cmd cmd = {};
855	struct iwl_mvm_go_iterator_data data = {};
856
857	WARN_ON(vif->type != NL80211_IFTYPE_P2P_DEVICE);
858
859	iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, action);
860
861	cmd.protection_flags |= cpu_to_le32(MAC_PROT_FLG_TGG_PROTECT);
862
863	/* Override the filter flags to accept only probe requests */
864	cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_PROBE_REQUEST);
865
866	/*
867	 * This flag should be set to true when the P2P Device is
868	 * discoverable and there is at least another active P2P GO. Settings
869	 * this flag will allow the P2P Device to be discoverable on other
870	 * channels in addition to its listen channel.
871	 * Note that this flag should not be set in other cases as it opens the
872	 * Rx filters on all MAC and increases the number of interrupts.
873	 */
874	ieee80211_iterate_active_interfaces_atomic(
875		mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
876		iwl_mvm_go_iterator, &data);
877
878	cmd.p2p_dev.is_disc_extended = cpu_to_le32(data.go_active ? 1 : 0);
879	return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
880}
881
882static void iwl_mvm_mac_ctxt_set_tim(struct iwl_mvm *mvm,
883				     struct iwl_mac_beacon_cmd *beacon_cmd,
884				     u8 *beacon, u32 frame_size)
885{
886	u32 tim_idx;
887	struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)beacon;
888
889	/* The index is relative to frame start but we start looking at the
890	 * variable-length part of the beacon. */
891	tim_idx = mgmt->u.beacon.variable - beacon;
892
893	/* Parse variable-length elements of beacon to find WLAN_EID_TIM */
894	while ((tim_idx < (frame_size - 2)) &&
895			(beacon[tim_idx] != WLAN_EID_TIM))
896		tim_idx += beacon[tim_idx+1] + 2;
897
898	/* If TIM field was found, set variables */
899	if ((tim_idx < (frame_size - 1)) && (beacon[tim_idx] == WLAN_EID_TIM)) {
900		beacon_cmd->tim_idx = cpu_to_le32(tim_idx);
901		beacon_cmd->tim_size = cpu_to_le32((u32)beacon[tim_idx+1]);
902	} else {
903		IWL_WARN(mvm, "Unable to find TIM Element in beacon\n");
904	}
905}
906
907static int iwl_mvm_mac_ctxt_send_beacon(struct iwl_mvm *mvm,
908					struct ieee80211_vif *vif,
909					struct sk_buff *beacon)
910{
911	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
912	struct iwl_host_cmd cmd = {
913		.id = BEACON_TEMPLATE_CMD,
914		.flags = CMD_ASYNC,
915	};
916	struct iwl_mac_beacon_cmd beacon_cmd = {};
917	struct ieee80211_tx_info *info;
918	u32 beacon_skb_len;
919	u32 rate;
920
921	if (WARN_ON(!beacon))
922		return -EINVAL;
923
924	beacon_skb_len = beacon->len;
925
926	/* TODO: for now the beacon template id is set to be the mac context id.
927	 * Might be better to handle it as another resource ... */
928	beacon_cmd.template_id = cpu_to_le32((u32)mvmvif->id);
929
930	/* Set up TX command fields */
931	beacon_cmd.tx.len = cpu_to_le16((u16)beacon_skb_len);
932	beacon_cmd.tx.sta_id = mvmvif->bcast_sta.sta_id;
933	beacon_cmd.tx.life_time = cpu_to_le32(TX_CMD_LIFE_TIME_INFINITE);
934	beacon_cmd.tx.tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL |
935					     TX_CMD_FLG_BT_DIS  |
936					     TX_CMD_FLG_TSF);
937
938	mvm->mgmt_last_antenna_idx =
939		iwl_mvm_next_antenna(mvm, iwl_fw_valid_tx_ant(mvm->fw),
940				     mvm->mgmt_last_antenna_idx);
941
942	beacon_cmd.tx.rate_n_flags =
943		cpu_to_le32(BIT(mvm->mgmt_last_antenna_idx) <<
944			    RATE_MCS_ANT_POS);
945
946	info = IEEE80211_SKB_CB(beacon);
947
948	if (info->band == IEEE80211_BAND_5GHZ || vif->p2p) {
949		rate = IWL_FIRST_OFDM_RATE;
950	} else {
951		rate = IWL_FIRST_CCK_RATE;
952		beacon_cmd.tx.rate_n_flags |= cpu_to_le32(RATE_MCS_CCK_MSK);
953	}
954	beacon_cmd.tx.rate_n_flags |=
955		cpu_to_le32(iwl_mvm_mac80211_idx_to_hwrate(rate));
956
957	/* Set up TX beacon command fields */
958	if (vif->type == NL80211_IFTYPE_AP)
959		iwl_mvm_mac_ctxt_set_tim(mvm, &beacon_cmd,
960					 beacon->data,
961					 beacon_skb_len);
962
963	/* Submit command */
964	cmd.len[0] = sizeof(beacon_cmd);
965	cmd.data[0] = &beacon_cmd;
966	cmd.dataflags[0] = 0;
967	cmd.len[1] = beacon_skb_len;
968	cmd.data[1] = beacon->data;
969	cmd.dataflags[1] = IWL_HCMD_DFL_DUP;
970
971	return iwl_mvm_send_cmd(mvm, &cmd);
972}
973
974/* The beacon template for the AP/GO/IBSS has changed and needs update */
975int iwl_mvm_mac_ctxt_beacon_changed(struct iwl_mvm *mvm,
976				    struct ieee80211_vif *vif)
977{
978	struct sk_buff *beacon;
979	int ret;
980
981	WARN_ON(vif->type != NL80211_IFTYPE_AP &&
982		vif->type != NL80211_IFTYPE_ADHOC);
983
984	beacon = ieee80211_beacon_get(mvm->hw, vif);
985	if (!beacon)
986		return -ENOMEM;
987
988	ret = iwl_mvm_mac_ctxt_send_beacon(mvm, vif, beacon);
989	dev_kfree_skb(beacon);
990	return ret;
991}
992
993struct iwl_mvm_mac_ap_iterator_data {
994	struct iwl_mvm *mvm;
995	struct ieee80211_vif *vif;
996	u32 beacon_device_ts;
997	u16 beacon_int;
998};
999
1000/* Find the beacon_device_ts and beacon_int for a managed interface */
1001static void iwl_mvm_mac_ap_iterator(void *_data, u8 *mac,
1002				    struct ieee80211_vif *vif)
1003{
1004	struct iwl_mvm_mac_ap_iterator_data *data = _data;
1005
1006	if (vif->type != NL80211_IFTYPE_STATION || !vif->bss_conf.assoc)
1007		return;
1008
1009	/* Station client has higher priority over P2P client*/
1010	if (vif->p2p && data->beacon_device_ts)
1011		return;
1012
1013	data->beacon_device_ts = vif->bss_conf.sync_device_ts;
1014	data->beacon_int = vif->bss_conf.beacon_int;
1015}
1016
1017/*
1018 * Fill the specific data for mac context of type AP of P2P GO
1019 */
1020static void iwl_mvm_mac_ctxt_cmd_fill_ap(struct iwl_mvm *mvm,
1021					 struct ieee80211_vif *vif,
1022					 struct iwl_mac_data_ap *ctxt_ap,
1023					 bool add)
1024{
1025	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1026	struct iwl_mvm_mac_ap_iterator_data data = {
1027		.mvm = mvm,
1028		.vif = vif,
1029		.beacon_device_ts = 0
1030	};
1031
1032	ctxt_ap->bi = cpu_to_le32(vif->bss_conf.beacon_int);
1033	ctxt_ap->bi_reciprocal =
1034		cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int));
1035	ctxt_ap->dtim_interval = cpu_to_le32(vif->bss_conf.beacon_int *
1036					     vif->bss_conf.dtim_period);
1037	ctxt_ap->dtim_reciprocal =
1038		cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int *
1039					       vif->bss_conf.dtim_period));
1040
1041	ctxt_ap->mcast_qid = cpu_to_le32(vif->cab_queue);
1042
1043	/*
1044	 * Only set the beacon time when the MAC is being added, when we
1045	 * just modify the MAC then we should keep the time -- the firmware
1046	 * can otherwise have a "jumping" TBTT.
1047	 */
1048	if (add) {
1049		/*
1050		 * If there is a station/P2P client interface which is
1051		 * associated, set the AP's TBTT far enough from the station's
1052		 * TBTT. Otherwise, set it to the current system time
1053		 */
1054		ieee80211_iterate_active_interfaces_atomic(
1055			mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
1056			iwl_mvm_mac_ap_iterator, &data);
1057
1058		if (data.beacon_device_ts) {
1059			u32 rand = (prandom_u32() % (64 - 36)) + 36;
1060			mvmvif->ap_beacon_time = data.beacon_device_ts +
1061				ieee80211_tu_to_usec(data.beacon_int * rand /
1062						     100);
1063		} else {
1064			mvmvif->ap_beacon_time =
1065				iwl_read_prph(mvm->trans,
1066					      DEVICE_SYSTEM_TIME_REG);
1067		}
1068	}
1069
1070	ctxt_ap->beacon_time = cpu_to_le32(mvmvif->ap_beacon_time);
1071	ctxt_ap->beacon_tsf = 0; /* unused */
1072
1073	/* TODO: Assume that the beacon id == mac context id */
1074	ctxt_ap->beacon_template = cpu_to_le32(mvmvif->id);
1075}
1076
1077static int iwl_mvm_mac_ctxt_cmd_ap(struct iwl_mvm *mvm,
1078				   struct ieee80211_vif *vif,
1079				   u32 action)
1080{
1081	struct iwl_mac_ctx_cmd cmd = {};
1082
1083	WARN_ON(vif->type != NL80211_IFTYPE_AP || vif->p2p);
1084
1085	/* Fill the common data for all mac context types */
1086	iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, action);
1087
1088	/* Also enable probe requests to pass */
1089	cmd.filter_flags |= cpu_to_le32(MAC_FILTER_IN_PROBE_REQUEST);
1090
1091	/* Fill the data specific for ap mode */
1092	iwl_mvm_mac_ctxt_cmd_fill_ap(mvm, vif, &cmd.ap,
1093				     action == FW_CTXT_ACTION_ADD);
1094
1095	return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
1096}
1097
1098static int iwl_mvm_mac_ctxt_cmd_go(struct iwl_mvm *mvm,
1099				   struct ieee80211_vif *vif,
1100				   u32 action)
1101{
1102	struct iwl_mac_ctx_cmd cmd = {};
1103	struct ieee80211_p2p_noa_attr *noa = &vif->bss_conf.p2p_noa_attr;
1104
1105	WARN_ON(vif->type != NL80211_IFTYPE_AP || !vif->p2p);
1106
1107	/* Fill the common data for all mac context types */
1108	iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, action);
1109
1110	/* Fill the data specific for GO mode */
1111	iwl_mvm_mac_ctxt_cmd_fill_ap(mvm, vif, &cmd.go.ap,
1112				     action == FW_CTXT_ACTION_ADD);
1113
1114	cmd.go.ctwin = cpu_to_le32(noa->oppps_ctwindow &
1115					IEEE80211_P2P_OPPPS_CTWINDOW_MASK);
1116	cmd.go.opp_ps_enabled =
1117			cpu_to_le32(!!(noa->oppps_ctwindow &
1118					IEEE80211_P2P_OPPPS_ENABLE_BIT));
1119
1120	return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
1121}
1122
1123static int iwl_mvm_mac_ctx_send(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1124				u32 action)
1125{
1126	switch (vif->type) {
1127	case NL80211_IFTYPE_STATION:
1128		if (!vif->p2p)
1129			return iwl_mvm_mac_ctxt_cmd_station(mvm, vif,
1130							    action);
1131		else
1132			return iwl_mvm_mac_ctxt_cmd_p2p_client(mvm, vif,
1133							       action);
1134		break;
1135	case NL80211_IFTYPE_AP:
1136		if (!vif->p2p)
1137			return iwl_mvm_mac_ctxt_cmd_ap(mvm, vif, action);
1138		else
1139			return iwl_mvm_mac_ctxt_cmd_go(mvm, vif, action);
1140		break;
1141	case NL80211_IFTYPE_MONITOR:
1142		return iwl_mvm_mac_ctxt_cmd_listener(mvm, vif, action);
1143	case NL80211_IFTYPE_P2P_DEVICE:
1144		return iwl_mvm_mac_ctxt_cmd_p2p_device(mvm, vif, action);
1145	case NL80211_IFTYPE_ADHOC:
1146		return iwl_mvm_mac_ctxt_cmd_ibss(mvm, vif, action);
1147	default:
1148		break;
1149	}
1150
1151	return -EOPNOTSUPP;
1152}
1153
1154int iwl_mvm_mac_ctxt_add(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1155{
1156	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1157	int ret;
1158
1159	if (WARN_ONCE(mvmvif->uploaded, "Adding active MAC %pM/%d\n",
1160		      vif->addr, ieee80211_vif_type_p2p(vif)))
1161		return -EIO;
1162
1163	ret = iwl_mvm_mac_ctx_send(mvm, vif, FW_CTXT_ACTION_ADD);
1164	if (ret)
1165		return ret;
1166
1167	/* will only do anything at resume from D3 time */
1168	iwl_mvm_set_last_nonqos_seq(mvm, vif);
1169
1170	mvmvif->uploaded = true;
1171	return 0;
1172}
1173
1174int iwl_mvm_mac_ctxt_changed(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1175{
1176	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1177
1178	if (WARN_ONCE(!mvmvif->uploaded, "Changing inactive MAC %pM/%d\n",
1179		      vif->addr, ieee80211_vif_type_p2p(vif)))
1180		return -EIO;
1181
1182	return iwl_mvm_mac_ctx_send(mvm, vif, FW_CTXT_ACTION_MODIFY);
1183}
1184
1185int iwl_mvm_mac_ctxt_remove(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1186{
1187	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1188	struct iwl_mac_ctx_cmd cmd;
1189	int ret;
1190
1191	if (WARN_ONCE(!mvmvif->uploaded, "Removing inactive MAC %pM/%d\n",
1192		      vif->addr, ieee80211_vif_type_p2p(vif)))
1193		return -EIO;
1194
1195	memset(&cmd, 0, sizeof(cmd));
1196
1197	cmd.id_and_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
1198							   mvmvif->color));
1199	cmd.action = cpu_to_le32(FW_CTXT_ACTION_REMOVE);
1200
1201	ret = iwl_mvm_send_cmd_pdu(mvm, MAC_CONTEXT_CMD, CMD_SYNC,
1202				   sizeof(cmd), &cmd);
1203	if (ret) {
1204		IWL_ERR(mvm, "Failed to remove MAC context: %d\n", ret);
1205		return ret;
1206	}
1207
1208	mvmvif->uploaded = false;
1209
1210	if (vif->type == NL80211_IFTYPE_MONITOR)
1211		mvm->hw->flags &= ~IEEE80211_HW_RX_INCLUDES_FCS;
1212
1213	return 0;
1214}
1215
1216int iwl_mvm_rx_beacon_notif(struct iwl_mvm *mvm,
1217			    struct iwl_rx_cmd_buffer *rxb,
1218			    struct iwl_device_cmd *cmd)
1219{
1220	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1221	struct iwl_beacon_notif *beacon = (void *)pkt->data;
1222	u16 status __maybe_unused =
1223		le16_to_cpu(beacon->beacon_notify_hdr.status.status);
1224	u32 rate __maybe_unused =
1225		le32_to_cpu(beacon->beacon_notify_hdr.initial_rate);
1226
1227	IWL_DEBUG_RX(mvm, "beacon status %#x retries:%d tsf:0x%16llX rate:%d\n",
1228		     status & TX_STATUS_MSK,
1229		     beacon->beacon_notify_hdr.failure_frame,
1230		     le64_to_cpu(beacon->tsf),
1231		     rate);
1232	return 0;
1233}
1234
1235static void iwl_mvm_beacon_loss_iterator(void *_data, u8 *mac,
1236					 struct ieee80211_vif *vif)
1237{
1238	struct iwl_missed_beacons_notif *missed_beacons = _data;
1239	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1240
1241	if (mvmvif->id != (u16)le32_to_cpu(missed_beacons->mac_id))
1242		return;
1243
1244	/*
1245	 * TODO: the threshold should be adjusted based on latency conditions,
1246	 * and/or in case of a CS flow on one of the other AP vifs.
1247	 */
1248	if (le32_to_cpu(missed_beacons->consec_missed_beacons_since_last_rx) >
1249	     IWL_MVM_MISSED_BEACONS_THRESHOLD)
1250		ieee80211_beacon_loss(vif);
1251}
1252
1253int iwl_mvm_rx_missed_beacons_notif(struct iwl_mvm *mvm,
1254				    struct iwl_rx_cmd_buffer *rxb,
1255				    struct iwl_device_cmd *cmd)
1256{
1257	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1258	struct iwl_missed_beacons_notif *mb = (void *)pkt->data;
1259
1260	IWL_DEBUG_INFO(mvm,
1261		       "missed bcn mac_id=%u, consecutive=%u (%u, %u, %u)\n",
1262		       le32_to_cpu(mb->mac_id),
1263		       le32_to_cpu(mb->consec_missed_beacons),
1264		       le32_to_cpu(mb->consec_missed_beacons_since_last_rx),
1265		       le32_to_cpu(mb->num_recvd_beacons),
1266		       le32_to_cpu(mb->num_expected_beacons));
1267
1268	ieee80211_iterate_active_interfaces_atomic(mvm->hw,
1269						   IEEE80211_IFACE_ITER_NORMAL,
1270						   iwl_mvm_beacon_loss_iterator,
1271						   mb);
1272	return 0;
1273}
1274