blob: 8fe955d58c6e9a53913fcaa18b934c001a9bceaf [file] [log] [blame]
Johannes Berg8ca151b2013-01-24 14:25:36 +01001/******************************************************************************
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 *
Emmanuel Grumbach51368bf2013-12-30 13:15:54 +02008 * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
Johannes Berg8b4139d2014-07-24 14:05:26 +02009 * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
Luca Coelho09856582017-06-01 15:19:45 +030010 * Copyright(c) 2015 - 2017 Intel Deutschland GmbH
Johannes Berg8ca151b2013-01-24 14:25:36 +010011 *
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
Emmanuel Grumbach410dc5a2013-02-18 09:22:28 +020027 * in the file called COPYING.
Johannes Berg8ca151b2013-01-24 14:25:36 +010028 *
29 * Contact Information:
Emmanuel Grumbachcb2f8272015-11-17 15:39:56 +020030 * Intel Linux Wireless <linuxwifi@intel.com>
Johannes Berg8ca151b2013-01-24 14:25:36 +010031 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
32 *
33 * BSD LICENSE
34 *
Emmanuel Grumbach51368bf2013-12-30 13:15:54 +020035 * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
Johannes Berg8b4139d2014-07-24 14:05:26 +020036 * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
Luca Coelho09856582017-06-01 15:19:45 +030037 * Copyright(c) 2015 - 2017 Intel Deutschland GmbH
Johannes Berg8ca151b2013-01-24 14:25:36 +010038 * 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"
Andrei Otcheretianski7f0a7c62014-05-04 11:48:12 +030074#include "time-event.h"
Johannes Berg8ca151b2013-01-24 14:25:36 +010075
76const u8 iwl_mvm_ac_to_tx_fifo[] = {
Johannes Berg8ca151b2013-01-24 14:25:36 +010077 IWL_MVM_TX_FIFO_VO,
Emmanuel Grumbach3dd94792013-12-19 16:50:49 +020078 IWL_MVM_TX_FIFO_VI,
79 IWL_MVM_TX_FIFO_BE,
80 IWL_MVM_TX_FIFO_BK,
Johannes Berg8ca151b2013-01-24 14:25:36 +010081};
82
Emmanuel Grumbachcf6c6ea2017-06-13 13:18:48 +030083const 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
Johannes Berg8ca151b2013-01-24 14:25:36 +010090struct 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)];
Johannes Berg8ca151b2013-01-24 14:25:36 +010095 enum iwl_tsf_id preferred_tsf;
96 bool found_vif;
97};
98
Arik Nemtsova74346d2014-09-21 19:07:30 +030099struct iwl_mvm_hw_queues_iface_iterator_data {
100 struct ieee80211_vif *exclude_vif;
101 unsigned long used_hw_queues;
102};
103
Ilan Peer6e97b0d2013-12-23 22:18:02 +0200104static void iwl_mvm_mac_tsf_id_iter(void *_data, u8 *mac,
105 struct ieee80211_vif *vif)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100106{
107 struct iwl_mvm_mac_iface_iterator_data *data = _data;
108 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Ilan Peer2c3e62a2014-02-02 21:54:35 +0200109 u16 min_bi;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100110
Ilan Peer6e97b0d2013-12-23 22:18:02 +0200111 /* Skip the interface for which we are trying to assign a tsf_id */
112 if (vif == data->vif)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100113 return;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100114
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 /*
Ilan Peer2c3e62a2014-02-02 21:54:35 +0200134 * 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.
Johannes Berg8ca151b2013-01-24 14:25:36 +0100140 */
Ilan Peer2c3e62a2014-02-02 21:54:35 +0200141 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;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100156 }
157 break;
Ilan Peer2c3e62a2014-02-02 21:54:35 +0200158
Johannes Berg8ca151b2013-01-24 14:25:36 +0100159 case NL80211_IFTYPE_AP:
160 /*
Ilan Peer2c3e62a2014-02-02 21:54:35 +0200161 * 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.
Johannes Berg8ca151b2013-01-24 14:25:36 +0100168 */
Ilan Peer2c3e62a2014-02-02 21:54:35 +0200169 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;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100185 }
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
Johannes Berg110cf812014-08-01 23:14:24 +0200210/*
211 * Get the mask of the queues used by the vif
212 */
Arik Nemtsovd92b732e2014-09-21 19:00:42 +0300213u32 iwl_mvm_mac_get_queues_mask(struct ieee80211_vif *vif)
Johannes Berg110cf812014-08-01 23:14:24 +0200214{
215 u32 qmask = 0, ac;
216
217 if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
218 return BIT(IWL_MVM_OFFCHANNEL_QUEUE);
219
Ilan Peer1008e442015-01-17 22:35:00 -0500220 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 }
Johannes Berg110cf812014-08-01 23:14:24 +0200224
Liad Kaufman4d339982017-03-21 17:13:16 +0200225 if (vif->type == NL80211_IFTYPE_AP ||
226 vif->type == NL80211_IFTYPE_ADHOC)
Johannes Berg110cf812014-08-01 23:14:24 +0200227 qmask |= BIT(vif->cab_queue);
228
229 return qmask;
230}
231
Arik Nemtsova74346d2014-09-21 19:07:30 +0300232static 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
Arik Nemtsova74346d2014-09-21 19:07:30 +0300244unsigned 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) |
Johannes Bergc8f54702017-06-19 23:50:31 +0200251 BIT(mvm->aux_queue) |
252 BIT(IWL_MVM_DQA_GCAST_QUEUE),
Arik Nemtsova74346d2014-09-21 19:07:30 +0300253 };
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
Arik Nemtsova74346d2014-09-21 19:07:30 +0300262 return data.used_hw_queues;
263}
264
Ilan Peer6e97b0d2013-12-23 22:18:02 +0200265static 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);
Ilan Peer6e97b0d2013-12-23 22:18:02 +0200270
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
Ilan Peer6e97b0d2013-12-23 22:18:02 +0200277 /* 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
Ilan Peer6e97b0d2013-12-23 22:18:02 +0200289void 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
Johannes Bergc8f54702017-06-19 23:50:31 +0200312int iwl_mvm_mac_ctxt_init(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100313{
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,
Johannes Berg8ca151b2013-01-24 14:25:36 +0100322 .found_vif = false,
323 };
324 u32 ac;
Johannes Berg32026e82017-06-19 23:26:55 +0200325 int ret, i, queue_limit;
Johannes Berg110cf812014-08-01 23:14:24 +0200326 unsigned long used_hw_queues;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100327
Johannes Bergc8f54702017-06-19 23:50:31 +0200328 lockdep_assert_held(&mvm->mutex);
329
Johannes Berg8ca151b2013-01-24 14:25:36 +0100330 /*
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
Johannes Berg5023d962013-07-31 14:07:43 +0200346 /* 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:
Ilan Peerec8b6882013-02-13 13:27:18 +0200355 __clear_bit(0, data.available_mac_ids);
Johannes Berg5023d962013-07-31 14:07:43 +0200356 }
Ilan Peerec8b6882013-02-13 13:27:18 +0200357
Johannes Berg8ca151b2013-01-24 14:25:36 +0100358 ieee80211_iterate_active_interfaces_atomic(
359 mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
360 iwl_mvm_mac_iface_iterator, &data);
361
Arik Nemtsova74346d2014-09-21 19:07:30 +0300362 used_hw_queues = iwl_mvm_get_used_hw_queues(mvm, vif);
363
Johannes Berg8ca151b2013-01-24 14:25:36 +0100364 /*
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 */
Ilan Peerfd11bd02013-06-16 12:18:11 +0300378 if (WARN_ON_ONCE(test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)))
379 return -EBUSY;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100380
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
Ilan Peer1e849c92013-02-13 12:26:39 +0200402 INIT_LIST_HEAD(&mvmvif->time_event_data.list);
403 mvmvif->time_event_data.id = TE_MAX;
404
Johannes Berg8ca151b2013-01-24 14:25:36 +0100405 /* 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
Johannes Bergc8f54702017-06-19 23:50:31 +0200413 /*
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]));
Johannes Berg32026e82017-06-19 23:26:55 +0200421
Liad Kaufman24afba72015-07-28 18:56:08 +0300422 /*
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 */
Johannes Berg8ca151b2013-01-24 14:25:36 +0100427 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
Johannes Berg32026e82017-06-19 23:26:55 +0200428 u8 queue = find_first_zero_bit(&used_hw_queues, queue_limit);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100429
Johannes Berg32026e82017-06-19 23:26:55 +0200430 if (queue >= queue_limit) {
Johannes Berg8ca151b2013-01-24 14:25:36 +0100431 IWL_ERR(mvm, "Failed to allocate queue\n");
432 ret = -EIO;
433 goto exit_fail;
434 }
435
Johannes Berg110cf812014-08-01 23:14:24 +0200436 __set_bit(queue, &used_hw_queues);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100437 vif->hw_queue[ac] = queue;
438 }
439
440 /* Allocate the CAB queue for softAP and GO interfaces */
441 if (vif->type == NL80211_IFTYPE_AP) {
Sara Sharone2af3fa2017-02-22 19:35:10 +0200442 /*
443 * For TVQM this will be overwritten later with the FW assigned
444 * queue value (when queue is enabled).
445 */
Johannes Bergc8f54702017-06-19 23:50:31 +0200446 mvmvif->cab_queue = IWL_MVM_DQA_GCAST_QUEUE;
447 vif->cab_queue = IWL_MVM_DQA_GCAST_QUEUE;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100448 } else {
449 vif->cab_queue = IEEE80211_INVAL_HW_QUEUE;
450 }
451
Sara Sharon0ae98812017-01-04 14:53:58 +0200452 mvmvif->bcast_sta.sta_id = IWL_MVM_INVALID_STA;
453 mvmvif->mcast_sta.sta_id = IWL_MVM_INVALID_STA;
454 mvmvif->ap_sta_id = IWL_MVM_INVALID_STA;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100455
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +0300456 for (i = 0; i < NUM_IWL_MVM_SMPS_REQ; i++)
457 mvmvif->smps_requests[i] = IEEE80211_SMPS_AUTOMATIC;
458
Johannes Berg8ca151b2013-01-24 14:25:36 +0100459 return 0;
460
461exit_fail:
462 memset(mvmvif, 0, sizeof(struct iwl_mvm_vif));
463 memset(vif->hw_queue, IEEE80211_INVAL_HW_QUEUE, sizeof(vif->hw_queue));
464 vif->cab_queue = IEEE80211_INVAL_HW_QUEUE;
465 return ret;
466}
467
Johannes Berg8ca151b2013-01-24 14:25:36 +0100468static void iwl_mvm_ack_rates(struct iwl_mvm *mvm,
469 struct ieee80211_vif *vif,
Johannes Berg57fbcce2016-04-12 15:56:15 +0200470 enum nl80211_band band,
Johannes Berg8ca151b2013-01-24 14:25:36 +0100471 u8 *cck_rates, u8 *ofdm_rates)
472{
473 struct ieee80211_supported_band *sband;
474 unsigned long basic = vif->bss_conf.basic_rates;
475 int lowest_present_ofdm = 100;
476 int lowest_present_cck = 100;
477 u8 cck = 0;
478 u8 ofdm = 0;
479 int i;
480
481 sband = mvm->hw->wiphy->bands[band];
482
483 for_each_set_bit(i, &basic, BITS_PER_LONG) {
484 int hw = sband->bitrates[i].hw_value;
485 if (hw >= IWL_FIRST_OFDM_RATE) {
486 ofdm |= BIT(hw - IWL_FIRST_OFDM_RATE);
487 if (lowest_present_ofdm > hw)
488 lowest_present_ofdm = hw;
489 } else {
490 BUILD_BUG_ON(IWL_FIRST_CCK_RATE != 0);
491
492 cck |= BIT(hw);
493 if (lowest_present_cck > hw)
494 lowest_present_cck = hw;
495 }
496 }
497
498 /*
499 * Now we've got the basic rates as bitmaps in the ofdm and cck
500 * variables. This isn't sufficient though, as there might not
501 * be all the right rates in the bitmap. E.g. if the only basic
502 * rates are 5.5 Mbps and 11 Mbps, we still need to add 1 Mbps
503 * and 6 Mbps because the 802.11-2007 standard says in 9.6:
504 *
505 * [...] a STA responding to a received frame shall transmit
506 * its Control Response frame [...] at the highest rate in the
507 * BSSBasicRateSet parameter that is less than or equal to the
508 * rate of the immediately previous frame in the frame exchange
509 * sequence ([...]) and that is of the same modulation class
510 * ([...]) as the received frame. If no rate contained in the
511 * BSSBasicRateSet parameter meets these conditions, then the
512 * control frame sent in response to a received frame shall be
513 * transmitted at the highest mandatory rate of the PHY that is
514 * less than or equal to the rate of the received frame, and
515 * that is of the same modulation class as the received frame.
516 *
517 * As a consequence, we need to add all mandatory rates that are
518 * lower than all of the basic rates to these bitmaps.
519 */
520
521 if (IWL_RATE_24M_INDEX < lowest_present_ofdm)
522 ofdm |= IWL_RATE_BIT_MSK(24) >> IWL_FIRST_OFDM_RATE;
523 if (IWL_RATE_12M_INDEX < lowest_present_ofdm)
524 ofdm |= IWL_RATE_BIT_MSK(12) >> IWL_FIRST_OFDM_RATE;
525 /* 6M already there or needed so always add */
526 ofdm |= IWL_RATE_BIT_MSK(6) >> IWL_FIRST_OFDM_RATE;
527
528 /*
529 * CCK is a bit more complex with DSSS vs. HR/DSSS vs. ERP.
530 * Note, however:
531 * - if no CCK rates are basic, it must be ERP since there must
532 * be some basic rates at all, so they're OFDM => ERP PHY
533 * (or we're in 5 GHz, and the cck bitmap will never be used)
534 * - if 11M is a basic rate, it must be ERP as well, so add 5.5M
535 * - if 5.5M is basic, 1M and 2M are mandatory
536 * - if 2M is basic, 1M is mandatory
537 * - if 1M is basic, that's the only valid ACK rate.
538 * As a consequence, it's not as complicated as it sounds, just add
539 * any lower rates to the ACK rate bitmap.
540 */
541 if (IWL_RATE_11M_INDEX < lowest_present_cck)
542 cck |= IWL_RATE_BIT_MSK(11) >> IWL_FIRST_CCK_RATE;
543 if (IWL_RATE_5M_INDEX < lowest_present_cck)
544 cck |= IWL_RATE_BIT_MSK(5) >> IWL_FIRST_CCK_RATE;
545 if (IWL_RATE_2M_INDEX < lowest_present_cck)
546 cck |= IWL_RATE_BIT_MSK(2) >> IWL_FIRST_CCK_RATE;
547 /* 1M already there or needed so always add */
548 cck |= IWL_RATE_BIT_MSK(1) >> IWL_FIRST_CCK_RATE;
549
550 *cck_rates = cck;
551 *ofdm_rates = ofdm;
552}
553
Avri Altman8a5e3662013-11-12 19:16:03 +0200554static void iwl_mvm_mac_ctxt_set_ht_flags(struct iwl_mvm *mvm,
555 struct ieee80211_vif *vif,
556 struct iwl_mac_ctx_cmd *cmd)
557{
558 /* for both sta and ap, ht_operation_mode hold the protection_mode */
559 u8 protection_mode = vif->bss_conf.ht_operation_mode &
560 IEEE80211_HT_OP_MODE_PROTECTION;
561 /* The fw does not distinguish between ht and fat */
562 u32 ht_flag = MAC_PROT_FLG_HT_PROT | MAC_PROT_FLG_FAT_PROT;
563
564 IWL_DEBUG_RATE(mvm, "protection mode set to %d\n", protection_mode);
565 /*
566 * See section 9.23.3.1 of IEEE 80211-2012.
567 * Nongreenfield HT STAs Present is not supported.
568 */
569 switch (protection_mode) {
570 case IEEE80211_HT_OP_MODE_PROTECTION_NONE:
571 break;
572 case IEEE80211_HT_OP_MODE_PROTECTION_NONMEMBER:
573 case IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED:
574 cmd->protection_flags |= cpu_to_le32(ht_flag);
575 break;
576 case IEEE80211_HT_OP_MODE_PROTECTION_20MHZ:
577 /* Protect when channel wider than 20MHz */
578 if (vif->bss_conf.chandef.width > NL80211_CHAN_WIDTH_20)
579 cmd->protection_flags |= cpu_to_le32(ht_flag);
580 break;
581 default:
582 IWL_ERR(mvm, "Illegal protection mode %d\n",
583 protection_mode);
584 break;
585 }
586}
587
Johannes Berg8ca151b2013-01-24 14:25:36 +0100588static void iwl_mvm_mac_ctxt_cmd_common(struct iwl_mvm *mvm,
589 struct ieee80211_vif *vif,
590 struct iwl_mac_ctx_cmd *cmd,
Johannes Berg3dfd3a92014-08-11 21:37:30 +0200591 const u8 *bssid_override,
Johannes Berg8ca151b2013-01-24 14:25:36 +0100592 u32 action)
593{
594 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
595 struct ieee80211_chanctx_conf *chanctx;
Avri Altman8a5e3662013-11-12 19:16:03 +0200596 bool ht_enabled = !!(vif->bss_conf.ht_operation_mode &
597 IEEE80211_HT_OP_MODE_PROTECTION);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100598 u8 cck_ack_rates, ofdm_ack_rates;
Johannes Berg3dfd3a92014-08-11 21:37:30 +0200599 const u8 *bssid = bssid_override ?: vif->bss_conf.bssid;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100600 int i;
601
602 cmd->id_and_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
603 mvmvif->color));
604 cmd->action = cpu_to_le32(action);
605
606 switch (vif->type) {
607 case NL80211_IFTYPE_STATION:
608 if (vif->p2p)
609 cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_P2P_STA);
610 else
611 cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_BSS_STA);
612 break;
613 case NL80211_IFTYPE_AP:
614 cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_GO);
615 break;
616 case NL80211_IFTYPE_MONITOR:
617 cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_LISTENER);
618 break;
619 case NL80211_IFTYPE_P2P_DEVICE:
620 cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_P2P_DEVICE);
621 break;
622 case NL80211_IFTYPE_ADHOC:
623 cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_IBSS);
624 break;
625 default:
626 WARN_ON_ONCE(1);
627 }
628
629 cmd->tsf_id = cpu_to_le32(mvmvif->tsf_id);
630
631 memcpy(cmd->node_addr, vif->addr, ETH_ALEN);
Johannes Berg3dfd3a92014-08-11 21:37:30 +0200632
633 if (bssid)
634 memcpy(cmd->bssid_addr, bssid, ETH_ALEN);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100635 else
636 eth_broadcast_addr(cmd->bssid_addr);
637
638 rcu_read_lock();
639 chanctx = rcu_dereference(vif->chanctx_conf);
640 iwl_mvm_ack_rates(mvm, vif, chanctx ? chanctx->def.chan->band
Johannes Berg57fbcce2016-04-12 15:56:15 +0200641 : NL80211_BAND_2GHZ,
Johannes Berg8ca151b2013-01-24 14:25:36 +0100642 &cck_ack_rates, &ofdm_ack_rates);
643 rcu_read_unlock();
644
645 cmd->cck_rates = cpu_to_le32((u32)cck_ack_rates);
646 cmd->ofdm_rates = cpu_to_le32((u32)ofdm_ack_rates);
647
648 cmd->cck_short_preamble =
649 cpu_to_le32(vif->bss_conf.use_short_preamble ?
650 MAC_FLG_SHORT_PREAMBLE : 0);
651 cmd->short_slot =
652 cpu_to_le32(vif->bss_conf.use_short_slot ?
653 MAC_FLG_SHORT_SLOT : 0);
654
Ayala Bekerbd6f5bd2015-12-20 09:27:50 +0200655 cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP);
656
Emmanuel Grumbach3dd94792013-12-19 16:50:49 +0200657 for (i = 0; i < IEEE80211_NUM_ACS; i++) {
Emmanuel Grumbachcf6c6ea2017-06-13 13:18:48 +0300658 u8 txf = iwl_mvm_mac_ac_to_tx_fifo(mvm, i);
Emmanuel Grumbach3dd94792013-12-19 16:50:49 +0200659
660 cmd->ac[txf].cw_min =
661 cpu_to_le16(mvmvif->queue_params[i].cw_min);
662 cmd->ac[txf].cw_max =
663 cpu_to_le16(mvmvif->queue_params[i].cw_max);
664 cmd->ac[txf].edca_txop =
Johannes Berg8ca151b2013-01-24 14:25:36 +0100665 cpu_to_le16(mvmvif->queue_params[i].txop * 32);
Emmanuel Grumbach3dd94792013-12-19 16:50:49 +0200666 cmd->ac[txf].aifsn = mvmvif->queue_params[i].aifs;
667 cmd->ac[txf].fifos_mask = BIT(txf);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100668 }
669
670 if (vif->bss_conf.qos)
671 cmd->qos_flags |= cpu_to_le32(MAC_QOS_FLG_UPDATE_EDCA);
672
Emmanuel Grumbachdc271ee2014-07-03 20:46:35 +0300673 if (vif->bss_conf.use_cts_prot)
Dor Shaishcc7ee2b2013-02-18 13:51:36 +0200674 cmd->protection_flags |= cpu_to_le32(MAC_PROT_FLG_TGG_PROTECT);
Emmanuel Grumbachdc271ee2014-07-03 20:46:35 +0300675
Avri Altman8a5e3662013-11-12 19:16:03 +0200676 IWL_DEBUG_RATE(mvm, "use_cts_prot %d, ht_operation_mode %d\n",
677 vif->bss_conf.use_cts_prot,
678 vif->bss_conf.ht_operation_mode);
679 if (vif->bss_conf.chandef.width != NL80211_CHAN_WIDTH_20_NOHT)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100680 cmd->qos_flags |= cpu_to_le32(MAC_QOS_FLG_TGN);
Avri Altman8a5e3662013-11-12 19:16:03 +0200681 if (ht_enabled)
682 iwl_mvm_mac_ctxt_set_ht_flags(mvm, vif, cmd);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100683}
684
685static int iwl_mvm_mac_ctxt_send_cmd(struct iwl_mvm *mvm,
686 struct iwl_mac_ctx_cmd *cmd)
687{
Emmanuel Grumbacha1022922014-05-12 11:36:41 +0300688 int ret = iwl_mvm_send_cmd_pdu(mvm, MAC_CONTEXT_CMD, 0,
Johannes Berg8ca151b2013-01-24 14:25:36 +0100689 sizeof(*cmd), cmd);
690 if (ret)
691 IWL_ERR(mvm, "Failed to send MAC context (action:%d): %d\n",
692 le32_to_cpu(cmd->action), ret);
693 return ret;
694}
695
Luciano Coelhocf520232014-05-13 16:52:54 +0300696static int iwl_mvm_mac_ctxt_cmd_sta(struct iwl_mvm *mvm,
697 struct ieee80211_vif *vif,
Johannes Berg3dfd3a92014-08-11 21:37:30 +0200698 u32 action, bool force_assoc_off,
699 const u8 *bssid_override)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100700{
Luciano Coelhocf520232014-05-13 16:52:54 +0300701 struct iwl_mac_ctx_cmd cmd = {};
702 struct iwl_mac_data_sta *ctxt_sta;
703
704 WARN_ON(vif->type != NL80211_IFTYPE_STATION);
705
706 /* Fill the common data for all mac context types */
Johannes Berg3dfd3a92014-08-11 21:37:30 +0200707 iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, bssid_override, action);
Luciano Coelhocf520232014-05-13 16:52:54 +0300708
709 if (vif->p2p) {
710 struct ieee80211_p2p_noa_attr *noa =
711 &vif->bss_conf.p2p_noa_attr;
712
713 cmd.p2p_sta.ctwin = cpu_to_le32(noa->oppps_ctwindow &
714 IEEE80211_P2P_OPPPS_CTWINDOW_MASK);
715 ctxt_sta = &cmd.p2p_sta.sta;
716 } else {
Luciano Coelhocf520232014-05-13 16:52:54 +0300717 ctxt_sta = &cmd.sta;
718 }
719
Johannes Berg210a5442013-01-24 23:48:23 +0100720 /* We need the dtim_period to set the MAC as associated */
Alexander Bondarba283922013-05-02 16:34:48 +0300721 if (vif->bss_conf.assoc && vif->bss_conf.dtim_period &&
722 !force_assoc_off) {
Johannes Bergd2931bb2013-02-05 18:10:04 +0100723 u32 dtim_offs;
724
725 /*
726 * The DTIM count counts down, so when it is N that means N
727 * more beacon intervals happen until the DTIM TBTT. Therefore
728 * add this to the current time. If that ends up being in the
729 * future, the firmware will handle it.
730 *
731 * Also note that the system_timestamp (which we get here as
732 * "sync_device_ts") and TSF timestamp aren't at exactly the
733 * same offset in the frame -- the TSF is at the first symbol
734 * of the TSF, the system timestamp is at signal acquisition
735 * time. This means there's an offset between them of at most
736 * a few hundred microseconds (24 * 8 bits + PLCP time gives
737 * 384us in the longest case), this is currently not relevant
738 * as the firmware wakes up around 2ms before the TBTT.
739 */
740 dtim_offs = vif->bss_conf.sync_dtim_count *
741 vif->bss_conf.beacon_int;
742 /* convert TU to usecs */
743 dtim_offs *= 1024;
744
745 ctxt_sta->dtim_tsf =
746 cpu_to_le64(vif->bss_conf.sync_tsf + dtim_offs);
747 ctxt_sta->dtim_time =
748 cpu_to_le32(vif->bss_conf.sync_device_ts + dtim_offs);
Avraham Sternb5669722017-05-09 16:40:01 +0300749 ctxt_sta->assoc_beacon_arrive_time =
750 cpu_to_le32(vif->bss_conf.sync_device_ts);
Johannes Bergd2931bb2013-02-05 18:10:04 +0100751
752 IWL_DEBUG_INFO(mvm, "DTIM TBTT is 0x%llx/0x%x, offset %d\n",
753 le64_to_cpu(ctxt_sta->dtim_tsf),
754 le32_to_cpu(ctxt_sta->dtim_time),
755 dtim_offs);
756
Johannes Berg210a5442013-01-24 23:48:23 +0100757 ctxt_sta->is_assoc = cpu_to_le32(1);
Johannes Bergd2931bb2013-02-05 18:10:04 +0100758 } else {
Johannes Berg210a5442013-01-24 23:48:23 +0100759 ctxt_sta->is_assoc = cpu_to_le32(0);
Luciano Coelho7c8b3bc2014-07-30 15:04:08 +0300760
761 /* Allow beacons to pass through as long as we are not
762 * associated, or we do not have dtim period information.
763 */
764 cmd.filter_flags |= cpu_to_le32(MAC_FILTER_IN_BEACON);
Johannes Bergd2931bb2013-02-05 18:10:04 +0100765 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100766
767 ctxt_sta->bi = cpu_to_le32(vif->bss_conf.beacon_int);
768 ctxt_sta->bi_reciprocal =
769 cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int));
770 ctxt_sta->dtim_interval = cpu_to_le32(vif->bss_conf.beacon_int *
771 vif->bss_conf.dtim_period);
772 ctxt_sta->dtim_reciprocal =
773 cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int *
774 vif->bss_conf.dtim_period));
775
776 ctxt_sta->listen_interval = cpu_to_le32(mvm->hw->conf.listen_interval);
777 ctxt_sta->assoc_id = cpu_to_le32(vif->bss_conf.aid);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100778
Andrei Otcheretianskieffd1922015-06-30 12:08:28 +0300779 if (vif->probe_req_reg && vif->bss_conf.assoc && vif->p2p)
780 cmd.filter_flags |= cpu_to_le32(MAC_FILTER_IN_PROBE_REQUEST);
781
Johannes Berg8ca151b2013-01-24 14:25:36 +0100782 return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
783}
784
785static int iwl_mvm_mac_ctxt_cmd_listener(struct iwl_mvm *mvm,
786 struct ieee80211_vif *vif,
787 u32 action)
788{
789 struct iwl_mac_ctx_cmd cmd = {};
Chaya Rachel Ivgi0e39eb02015-12-03 15:51:46 +0200790 u32 tfd_queue_msk = 0;
Johannes Bergc8f54702017-06-19 23:50:31 +0200791 int ret;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100792
793 WARN_ON(vif->type != NL80211_IFTYPE_MONITOR);
794
Johannes Berg3dfd3a92014-08-11 21:37:30 +0200795 iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
Johannes Berg53585492013-03-18 13:04:50 +0100796
797 cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_PROMISC |
798 MAC_FILTER_IN_CONTROL_AND_MGMT |
799 MAC_FILTER_IN_BEACON |
Johannes Bergfb8b8ee2013-10-21 12:37:53 +0200800 MAC_FILTER_IN_PROBE_REQUEST |
801 MAC_FILTER_IN_CRC32);
Johannes Berg30686bf2015-06-02 21:39:54 +0200802 ieee80211_hw_set(mvm->hw, RX_INCLUDES_FCS);
Johannes Berg53585492013-03-18 13:04:50 +0100803
Chaya Rachel Ivgi0e39eb02015-12-03 15:51:46 +0200804 /* Allocate sniffer station */
805 ret = iwl_mvm_allocate_int_sta(mvm, &mvm->snif_sta, tfd_queue_msk,
Sara Sharonced19f22017-02-06 19:09:32 +0200806 vif->type, IWL_STA_GENERAL_PURPOSE);
Chaya Rachel Ivgi0e39eb02015-12-03 15:51:46 +0200807 if (ret)
808 return ret;
809
Johannes Berg8ca151b2013-01-24 14:25:36 +0100810 return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
811}
812
Johannes Berg5023d962013-07-31 14:07:43 +0200813static int iwl_mvm_mac_ctxt_cmd_ibss(struct iwl_mvm *mvm,
814 struct ieee80211_vif *vif,
815 u32 action)
816{
817 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
818 struct iwl_mac_ctx_cmd cmd = {};
819
820 WARN_ON(vif->type != NL80211_IFTYPE_ADHOC);
821
Johannes Berg3dfd3a92014-08-11 21:37:30 +0200822 iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
Johannes Berg5023d962013-07-31 14:07:43 +0200823
824 cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_BEACON |
825 MAC_FILTER_IN_PROBE_REQUEST);
826
827 /* cmd.ibss.beacon_time/cmd.ibss.beacon_tsf are curently ignored */
828 cmd.ibss.bi = cpu_to_le32(vif->bss_conf.beacon_int);
829 cmd.ibss.bi_reciprocal =
830 cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int));
831
832 /* TODO: Assumes that the beacon id == mac context id */
833 cmd.ibss.beacon_template = cpu_to_le32(mvmvif->id);
834
835 return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
836}
837
Johannes Berg8ca151b2013-01-24 14:25:36 +0100838struct iwl_mvm_go_iterator_data {
839 bool go_active;
840};
841
842static void iwl_mvm_go_iterator(void *_data, u8 *mac, struct ieee80211_vif *vif)
843{
844 struct iwl_mvm_go_iterator_data *data = _data;
845 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
846
Johannes Berg5023d962013-07-31 14:07:43 +0200847 if (vif->type == NL80211_IFTYPE_AP && vif->p2p &&
848 mvmvif->ap_ibss_active)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100849 data->go_active = true;
850}
851
852static int iwl_mvm_mac_ctxt_cmd_p2p_device(struct iwl_mvm *mvm,
853 struct ieee80211_vif *vif,
854 u32 action)
855{
856 struct iwl_mac_ctx_cmd cmd = {};
857 struct iwl_mvm_go_iterator_data data = {};
858
859 WARN_ON(vif->type != NL80211_IFTYPE_P2P_DEVICE);
860
Johannes Berg3dfd3a92014-08-11 21:37:30 +0200861 iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100862
863 cmd.protection_flags |= cpu_to_le32(MAC_PROT_FLG_TGG_PROTECT);
Ilan Peer1dcd15e2013-02-14 14:25:24 +0200864
865 /* Override the filter flags to accept only probe requests */
866 cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_PROBE_REQUEST);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100867
868 /*
869 * This flag should be set to true when the P2P Device is
870 * discoverable and there is at least another active P2P GO. Settings
871 * this flag will allow the P2P Device to be discoverable on other
872 * channels in addition to its listen channel.
873 * Note that this flag should not be set in other cases as it opens the
874 * Rx filters on all MAC and increases the number of interrupts.
875 */
876 ieee80211_iterate_active_interfaces_atomic(
877 mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
878 iwl_mvm_go_iterator, &data);
879
880 cmd.p2p_dev.is_disc_extended = cpu_to_le32(data.go_active ? 1 : 0);
881 return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
882}
883
884static void iwl_mvm_mac_ctxt_set_tim(struct iwl_mvm *mvm,
Sara Sharon8364fbb2016-12-08 11:44:20 +0200885 __le32 *tim_index, __le32 *tim_size,
Johannes Berg8ca151b2013-01-24 14:25:36 +0100886 u8 *beacon, u32 frame_size)
887{
888 u32 tim_idx;
889 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)beacon;
890
891 /* The index is relative to frame start but we start looking at the
892 * variable-length part of the beacon. */
893 tim_idx = mgmt->u.beacon.variable - beacon;
894
895 /* Parse variable-length elements of beacon to find WLAN_EID_TIM */
896 while ((tim_idx < (frame_size - 2)) &&
897 (beacon[tim_idx] != WLAN_EID_TIM))
898 tim_idx += beacon[tim_idx+1] + 2;
899
900 /* If TIM field was found, set variables */
901 if ((tim_idx < (frame_size - 1)) && (beacon[tim_idx] == WLAN_EID_TIM)) {
Sara Sharon8364fbb2016-12-08 11:44:20 +0200902 *tim_index = cpu_to_le32(tim_idx);
903 *tim_size = cpu_to_le32((u32)beacon[tim_idx + 1]);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100904 } else {
905 IWL_WARN(mvm, "Unable to find TIM Element in beacon\n");
906 }
907}
908
Andrei Otcheretianskid3a108a2016-02-28 17:12:21 +0200909static u32 iwl_mvm_find_ie_offset(u8 *beacon, u8 eid, u32 frame_size)
910{
911 struct ieee80211_mgmt *mgmt = (void *)beacon;
912 const u8 *ie;
913
914 if (WARN_ON_ONCE(frame_size <= (mgmt->u.beacon.variable - beacon)))
915 return 0;
916
917 frame_size -= mgmt->u.beacon.variable - beacon;
918
919 ie = cfg80211_find_ie(eid, mgmt->u.beacon.variable, frame_size);
920 if (!ie)
921 return 0;
922
923 return ie - beacon;
924}
925
Luca Coelho09856582017-06-01 15:19:45 +0300926static void iwl_mvm_mac_ctxt_set_tx(struct iwl_mvm *mvm,
927 struct ieee80211_vif *vif,
928 struct sk_buff *beacon,
929 struct iwl_tx_cmd *tx)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100930{
931 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100932 struct ieee80211_tx_info *info;
Emmanuel Grumbach75f6b9b2014-06-02 10:03:33 +0300933 u32 rate, tx_flags;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100934
Emmanuel Grumbach75f6b9b2014-06-02 10:03:33 +0300935 info = IEEE80211_SKB_CB(beacon);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100936
937 /* Set up TX command fields */
Luca Coelho09856582017-06-01 15:19:45 +0300938 tx->len = cpu_to_le16((u16)beacon->len);
939 tx->sta_id = mvmvif->bcast_sta.sta_id;
940 tx->life_time = cpu_to_le32(TX_CMD_LIFE_TIME_INFINITE);
Emmanuel Grumbach75f6b9b2014-06-02 10:03:33 +0300941 tx_flags = TX_CMD_FLG_SEQ_CTL | TX_CMD_FLG_TSF;
942 tx_flags |=
943 iwl_mvm_bt_coex_tx_prio(mvm, (void *)beacon->data, info, 0) <<
944 TX_CMD_FLG_BT_PRIO_POS;
Luca Coelho09856582017-06-01 15:19:45 +0300945 tx->tx_flags = cpu_to_le32(tx_flags);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100946
Emmanuel Grumbach1e3c3c32015-12-13 09:35:30 +0200947 if (!fw_has_capa(&mvm->fw->ucode_capa,
948 IWL_UCODE_TLV_CAPA_BEACON_ANT_SELECTION)) {
949 mvm->mgmt_last_antenna_idx =
950 iwl_mvm_next_antenna(mvm, iwl_mvm_get_valid_tx_ant(mvm),
951 mvm->mgmt_last_antenna_idx);
952 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100953
Luca Coelho09856582017-06-01 15:19:45 +0300954 tx->rate_n_flags =
Johannes Berg8ca151b2013-01-24 14:25:36 +0100955 cpu_to_le32(BIT(mvm->mgmt_last_antenna_idx) <<
956 RATE_MCS_ANT_POS);
957
Johannes Berg57fbcce2016-04-12 15:56:15 +0200958 if (info->band == NL80211_BAND_5GHZ || vif->p2p) {
Johannes Berg8ca151b2013-01-24 14:25:36 +0100959 rate = IWL_FIRST_OFDM_RATE;
960 } else {
961 rate = IWL_FIRST_CCK_RATE;
Luca Coelho09856582017-06-01 15:19:45 +0300962 tx->rate_n_flags |= cpu_to_le32(RATE_MCS_CCK_MSK);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100963 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100964
Luca Coelho09856582017-06-01 15:19:45 +0300965 tx->rate_n_flags |= cpu_to_le32(iwl_mvm_mac80211_idx_to_hwrate(rate));
966}
Johannes Berg8ca151b2013-01-24 14:25:36 +0100967
Luca Coelho09856582017-06-01 15:19:45 +0300968static int iwl_mvm_mac_ctxt_send_beacon_cmd(struct iwl_mvm *mvm,
969 struct sk_buff *beacon,
970 void *data, int len)
971{
972 struct iwl_host_cmd cmd = {
973 .id = BEACON_TEMPLATE_CMD,
974 .flags = CMD_ASYNC,
975 };
976
977 cmd.len[0] = len;
978 cmd.data[0] = data;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100979 cmd.dataflags[0] = 0;
Luca Coelho09856582017-06-01 15:19:45 +0300980 cmd.len[1] = beacon->len;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100981 cmd.data[1] = beacon->data;
982 cmd.dataflags[1] = IWL_HCMD_DFL_DUP;
983
984 return iwl_mvm_send_cmd(mvm, &cmd);
985}
986
Luca Coelho09856582017-06-01 15:19:45 +0300987static int iwl_mvm_mac_ctxt_send_beacon_v6(struct iwl_mvm *mvm,
988 struct ieee80211_vif *vif,
989 struct sk_buff *beacon)
990{
991 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
992 struct iwl_mac_beacon_cmd_v6 beacon_cmd = {};
993
994 iwl_mvm_mac_ctxt_set_tx(mvm, vif, beacon, &beacon_cmd.tx);
995
996 beacon_cmd.template_id = cpu_to_le32((u32)mvmvif->id);
997
998 if (vif->type == NL80211_IFTYPE_AP)
999 iwl_mvm_mac_ctxt_set_tim(mvm, &beacon_cmd.tim_idx,
1000 &beacon_cmd.tim_size,
1001 beacon->data, beacon->len);
1002
1003 return iwl_mvm_mac_ctxt_send_beacon_cmd(mvm, beacon, &beacon_cmd,
1004 sizeof(beacon_cmd));
1005}
1006
1007static int iwl_mvm_mac_ctxt_send_beacon_v7(struct iwl_mvm *mvm,
1008 struct ieee80211_vif *vif,
1009 struct sk_buff *beacon)
1010{
1011 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1012 struct iwl_mac_beacon_cmd_v7 beacon_cmd = {};
1013
1014 iwl_mvm_mac_ctxt_set_tx(mvm, vif, beacon, &beacon_cmd.tx);
1015
1016 beacon_cmd.template_id = cpu_to_le32((u32)mvmvif->id);
1017
1018 if (vif->type == NL80211_IFTYPE_AP)
1019 iwl_mvm_mac_ctxt_set_tim(mvm, &beacon_cmd.tim_idx,
1020 &beacon_cmd.tim_size,
1021 beacon->data, beacon->len);
1022
1023 beacon_cmd.csa_offset =
1024 cpu_to_le32(iwl_mvm_find_ie_offset(beacon->data,
1025 WLAN_EID_CHANNEL_SWITCH,
1026 beacon->len));
1027 beacon_cmd.ecsa_offset =
1028 cpu_to_le32(iwl_mvm_find_ie_offset(beacon->data,
1029 WLAN_EID_EXT_CHANSWITCH_ANN,
1030 beacon->len));
1031
1032 return iwl_mvm_mac_ctxt_send_beacon_cmd(mvm, beacon, &beacon_cmd,
1033 sizeof(beacon_cmd));
1034}
1035
1036static int iwl_mvm_mac_ctxt_send_beacon_v8(struct iwl_mvm *mvm,
1037 struct ieee80211_vif *vif,
1038 struct sk_buff *beacon)
1039{
1040 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1041 struct iwl_mac_beacon_cmd beacon_cmd = {};
1042
1043 beacon_cmd.byte_cnt = cpu_to_le16((u16)beacon->len);
1044 beacon_cmd.template_id = cpu_to_le32((u32)mvmvif->id);
1045
1046 if (vif->type == NL80211_IFTYPE_AP)
1047 iwl_mvm_mac_ctxt_set_tim(mvm,
1048 &beacon_cmd.tim_idx,
1049 &beacon_cmd.tim_size,
1050 beacon->data, beacon->len);
1051
1052 beacon_cmd.csa_offset =
1053 cpu_to_le32(iwl_mvm_find_ie_offset(beacon->data,
1054 WLAN_EID_CHANNEL_SWITCH,
1055 beacon->len));
1056 beacon_cmd.ecsa_offset =
1057 cpu_to_le32(iwl_mvm_find_ie_offset(beacon->data,
1058 WLAN_EID_EXT_CHANSWITCH_ANN,
1059 beacon->len));
1060
1061 return iwl_mvm_mac_ctxt_send_beacon_cmd(mvm, beacon, &beacon_cmd,
1062 sizeof(beacon_cmd));
1063}
1064
1065static int iwl_mvm_mac_ctxt_send_beacon(struct iwl_mvm *mvm,
1066 struct ieee80211_vif *vif,
1067 struct sk_buff *beacon)
1068{
1069 if (WARN_ON(!beacon))
1070 return -EINVAL;
1071
1072 if (!fw_has_capa(&mvm->fw->ucode_capa,
1073 IWL_UCODE_TLV_CAPA_CSA_AND_TBTT_OFFLOAD))
1074 return iwl_mvm_mac_ctxt_send_beacon_v6(mvm, vif, beacon);
1075
1076 if (!iwl_mvm_has_new_tx_api(mvm))
1077 return iwl_mvm_mac_ctxt_send_beacon_v7(mvm, vif, beacon);
1078
1079 return iwl_mvm_mac_ctxt_send_beacon_v8(mvm, vif, beacon);
1080}
1081
Johannes Berg5023d962013-07-31 14:07:43 +02001082/* The beacon template for the AP/GO/IBSS has changed and needs update */
Johannes Berg8ca151b2013-01-24 14:25:36 +01001083int iwl_mvm_mac_ctxt_beacon_changed(struct iwl_mvm *mvm,
1084 struct ieee80211_vif *vif)
1085{
1086 struct sk_buff *beacon;
1087 int ret;
1088
Johannes Berg5023d962013-07-31 14:07:43 +02001089 WARN_ON(vif->type != NL80211_IFTYPE_AP &&
1090 vif->type != NL80211_IFTYPE_ADHOC);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001091
Andrei Otcheretianskife887662014-04-10 21:46:16 +03001092 beacon = ieee80211_beacon_get_template(mvm->hw, vif, NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001093 if (!beacon)
1094 return -ENOMEM;
1095
1096 ret = iwl_mvm_mac_ctxt_send_beacon(mvm, vif, beacon);
1097 dev_kfree_skb(beacon);
1098 return ret;
1099}
1100
Ilan Peer3a3cb922013-06-13 08:30:12 +03001101struct iwl_mvm_mac_ap_iterator_data {
1102 struct iwl_mvm *mvm;
1103 struct ieee80211_vif *vif;
1104 u32 beacon_device_ts;
1105 u16 beacon_int;
1106};
1107
1108/* Find the beacon_device_ts and beacon_int for a managed interface */
1109static void iwl_mvm_mac_ap_iterator(void *_data, u8 *mac,
1110 struct ieee80211_vif *vif)
1111{
1112 struct iwl_mvm_mac_ap_iterator_data *data = _data;
1113
1114 if (vif->type != NL80211_IFTYPE_STATION || !vif->bss_conf.assoc)
1115 return;
1116
1117 /* Station client has higher priority over P2P client*/
1118 if (vif->p2p && data->beacon_device_ts)
1119 return;
1120
1121 data->beacon_device_ts = vif->bss_conf.sync_device_ts;
1122 data->beacon_int = vif->bss_conf.beacon_int;
1123}
1124
Johannes Berg8ca151b2013-01-24 14:25:36 +01001125/*
1126 * Fill the specific data for mac context of type AP of P2P GO
1127 */
1128static void iwl_mvm_mac_ctxt_cmd_fill_ap(struct iwl_mvm *mvm,
1129 struct ieee80211_vif *vif,
Johannes Berg186cd492016-08-24 10:05:40 +02001130 struct iwl_mac_ctx_cmd *cmd,
Johannes Berg506a81e2013-02-28 14:05:14 +01001131 struct iwl_mac_data_ap *ctxt_ap,
1132 bool add)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001133{
1134 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Ilan Peer3a3cb922013-06-13 08:30:12 +03001135 struct iwl_mvm_mac_ap_iterator_data data = {
1136 .mvm = mvm,
1137 .vif = vif,
1138 .beacon_device_ts = 0
1139 };
Johannes Berg8ca151b2013-01-24 14:25:36 +01001140
Johannes Berg186cd492016-08-24 10:05:40 +02001141 /* in AP mode, the MCAST FIFO takes the EDCA params from VO */
1142 cmd->ac[IWL_MVM_TX_FIFO_VO].fifos_mask |= BIT(IWL_MVM_TX_FIFO_MCAST);
1143
1144 /*
1145 * in AP mode, pass probe requests and beacons from other APs
1146 * (needed for ht protection); when there're no any associated
1147 * station don't ask FW to pass beacons to prevent unnecessary
1148 * wake-ups.
1149 */
1150 cmd->filter_flags |= cpu_to_le32(MAC_FILTER_IN_PROBE_REQUEST);
1151 if (mvmvif->ap_assoc_sta_count || !mvm->drop_bcn_ap_mode) {
1152 cmd->filter_flags |= cpu_to_le32(MAC_FILTER_IN_BEACON);
1153 IWL_DEBUG_HC(mvm, "Asking FW to pass beacons\n");
1154 } else {
1155 IWL_DEBUG_HC(mvm, "No need to receive beacons\n");
1156 }
1157
Johannes Berg8ca151b2013-01-24 14:25:36 +01001158 ctxt_ap->bi = cpu_to_le32(vif->bss_conf.beacon_int);
1159 ctxt_ap->bi_reciprocal =
1160 cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int));
1161 ctxt_ap->dtim_interval = cpu_to_le32(vif->bss_conf.beacon_int *
1162 vif->bss_conf.dtim_period);
1163 ctxt_ap->dtim_reciprocal =
1164 cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int *
1165 vif->bss_conf.dtim_period));
1166
Sara Sharonced19f22017-02-06 19:09:32 +02001167 if (!fw_has_api(&mvm->fw->ucode_capa,
1168 IWL_UCODE_TLV_API_STA_TYPE))
1169 ctxt_ap->mcast_qid = cpu_to_le32(vif->cab_queue);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001170
Johannes Berg506a81e2013-02-28 14:05:14 +01001171 /*
Ilan Peer3a3cb922013-06-13 08:30:12 +03001172 * Only set the beacon time when the MAC is being added, when we
Johannes Berg506a81e2013-02-28 14:05:14 +01001173 * just modify the MAC then we should keep the time -- the firmware
1174 * can otherwise have a "jumping" TBTT.
1175 */
Ilan Peer3a3cb922013-06-13 08:30:12 +03001176 if (add) {
1177 /*
1178 * If there is a station/P2P client interface which is
1179 * associated, set the AP's TBTT far enough from the station's
1180 * TBTT. Otherwise, set it to the current system time
1181 */
1182 ieee80211_iterate_active_interfaces_atomic(
1183 mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
1184 iwl_mvm_mac_ap_iterator, &data);
1185
1186 if (data.beacon_device_ts) {
Ilan Peer9f8f8ca2013-12-24 17:17:05 +02001187 u32 rand = (prandom_u32() % (64 - 36)) + 36;
Ilan Peer3a3cb922013-06-13 08:30:12 +03001188 mvmvif->ap_beacon_time = data.beacon_device_ts +
1189 ieee80211_tu_to_usec(data.beacon_int * rand /
1190 100);
1191 } else {
1192 mvmvif->ap_beacon_time =
1193 iwl_read_prph(mvm->trans,
1194 DEVICE_SYSTEM_TIME_REG);
1195 }
1196 }
Johannes Berg506a81e2013-02-28 14:05:14 +01001197
1198 ctxt_ap->beacon_time = cpu_to_le32(mvmvif->ap_beacon_time);
Johannes Berg506a81e2013-02-28 14:05:14 +01001199 ctxt_ap->beacon_tsf = 0; /* unused */
Johannes Berg8ca151b2013-01-24 14:25:36 +01001200
1201 /* TODO: Assume that the beacon id == mac context id */
1202 ctxt_ap->beacon_template = cpu_to_le32(mvmvif->id);
1203}
1204
Ilan Peerf82c8332015-09-10 12:54:38 +03001205static int iwl_mvm_mac_ctxt_cmd_ap(struct iwl_mvm *mvm,
1206 struct ieee80211_vif *vif,
1207 u32 action)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001208{
1209 struct iwl_mac_ctx_cmd cmd = {};
1210
1211 WARN_ON(vif->type != NL80211_IFTYPE_AP || vif->p2p);
1212
1213 /* Fill the common data for all mac context types */
Johannes Berg3dfd3a92014-08-11 21:37:30 +02001214 iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001215
Johannes Berg8ca151b2013-01-24 14:25:36 +01001216 /* Fill the data specific for ap mode */
Johannes Berg186cd492016-08-24 10:05:40 +02001217 iwl_mvm_mac_ctxt_cmd_fill_ap(mvm, vif, &cmd, &cmd.ap,
Johannes Berg506a81e2013-02-28 14:05:14 +01001218 action == FW_CTXT_ACTION_ADD);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001219
1220 return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
1221}
1222
1223static int iwl_mvm_mac_ctxt_cmd_go(struct iwl_mvm *mvm,
1224 struct ieee80211_vif *vif,
1225 u32 action)
1226{
1227 struct iwl_mac_ctx_cmd cmd = {};
Janusz Dziedzic67baf662013-03-21 15:47:56 +01001228 struct ieee80211_p2p_noa_attr *noa = &vif->bss_conf.p2p_noa_attr;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001229
1230 WARN_ON(vif->type != NL80211_IFTYPE_AP || !vif->p2p);
1231
1232 /* Fill the common data for all mac context types */
Johannes Berg3dfd3a92014-08-11 21:37:30 +02001233 iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001234
1235 /* Fill the data specific for GO mode */
Johannes Berg186cd492016-08-24 10:05:40 +02001236 iwl_mvm_mac_ctxt_cmd_fill_ap(mvm, vif, &cmd, &cmd.go.ap,
Johannes Berg506a81e2013-02-28 14:05:14 +01001237 action == FW_CTXT_ACTION_ADD);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001238
Janusz Dziedzic67baf662013-03-21 15:47:56 +01001239 cmd.go.ctwin = cpu_to_le32(noa->oppps_ctwindow &
1240 IEEE80211_P2P_OPPPS_CTWINDOW_MASK);
1241 cmd.go.opp_ps_enabled =
1242 cpu_to_le32(!!(noa->oppps_ctwindow &
1243 IEEE80211_P2P_OPPPS_ENABLE_BIT));
Johannes Berg8ca151b2013-01-24 14:25:36 +01001244
1245 return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
1246}
1247
1248static int iwl_mvm_mac_ctx_send(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
Johannes Berg3dfd3a92014-08-11 21:37:30 +02001249 u32 action, bool force_assoc_off,
1250 const u8 *bssid_override)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001251{
1252 switch (vif->type) {
1253 case NL80211_IFTYPE_STATION:
Luciano Coelhocf520232014-05-13 16:52:54 +03001254 return iwl_mvm_mac_ctxt_cmd_sta(mvm, vif, action,
Johannes Berg3dfd3a92014-08-11 21:37:30 +02001255 force_assoc_off,
1256 bssid_override);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001257 break;
1258 case NL80211_IFTYPE_AP:
1259 if (!vif->p2p)
1260 return iwl_mvm_mac_ctxt_cmd_ap(mvm, vif, action);
1261 else
1262 return iwl_mvm_mac_ctxt_cmd_go(mvm, vif, action);
1263 break;
1264 case NL80211_IFTYPE_MONITOR:
1265 return iwl_mvm_mac_ctxt_cmd_listener(mvm, vif, action);
1266 case NL80211_IFTYPE_P2P_DEVICE:
1267 return iwl_mvm_mac_ctxt_cmd_p2p_device(mvm, vif, action);
Johannes Berg5023d962013-07-31 14:07:43 +02001268 case NL80211_IFTYPE_ADHOC:
1269 return iwl_mvm_mac_ctxt_cmd_ibss(mvm, vif, action);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001270 default:
1271 break;
1272 }
1273
1274 return -EOPNOTSUPP;
1275}
1276
1277int iwl_mvm_mac_ctxt_add(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1278{
1279 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1280 int ret;
1281
1282 if (WARN_ONCE(mvmvif->uploaded, "Adding active MAC %pM/%d\n",
1283 vif->addr, ieee80211_vif_type_p2p(vif)))
1284 return -EIO;
1285
Luciano Coelhobca49d92014-05-13 17:33:38 +03001286 ret = iwl_mvm_mac_ctx_send(mvm, vif, FW_CTXT_ACTION_ADD,
Johannes Berg3dfd3a92014-08-11 21:37:30 +02001287 true, NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001288 if (ret)
1289 return ret;
1290
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001291 /* will only do anything at resume from D3 time */
1292 iwl_mvm_set_last_nonqos_seq(mvm, vif);
1293
Johannes Berg8ca151b2013-01-24 14:25:36 +01001294 mvmvif->uploaded = true;
1295 return 0;
1296}
1297
Luciano Coelhobca49d92014-05-13 17:33:38 +03001298int iwl_mvm_mac_ctxt_changed(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
Johannes Berg3dfd3a92014-08-11 21:37:30 +02001299 bool force_assoc_off, const u8 *bssid_override)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001300{
1301 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1302
1303 if (WARN_ONCE(!mvmvif->uploaded, "Changing inactive MAC %pM/%d\n",
1304 vif->addr, ieee80211_vif_type_p2p(vif)))
1305 return -EIO;
1306
Luciano Coelhobca49d92014-05-13 17:33:38 +03001307 return iwl_mvm_mac_ctx_send(mvm, vif, FW_CTXT_ACTION_MODIFY,
Johannes Berg3dfd3a92014-08-11 21:37:30 +02001308 force_assoc_off, bssid_override);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001309}
1310
1311int iwl_mvm_mac_ctxt_remove(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1312{
1313 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1314 struct iwl_mac_ctx_cmd cmd;
1315 int ret;
1316
1317 if (WARN_ONCE(!mvmvif->uploaded, "Removing inactive MAC %pM/%d\n",
1318 vif->addr, ieee80211_vif_type_p2p(vif)))
1319 return -EIO;
1320
1321 memset(&cmd, 0, sizeof(cmd));
1322
1323 cmd.id_and_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
1324 mvmvif->color));
1325 cmd.action = cpu_to_le32(FW_CTXT_ACTION_REMOVE);
1326
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03001327 ret = iwl_mvm_send_cmd_pdu(mvm, MAC_CONTEXT_CMD, 0,
Johannes Berg8ca151b2013-01-24 14:25:36 +01001328 sizeof(cmd), &cmd);
1329 if (ret) {
1330 IWL_ERR(mvm, "Failed to remove MAC context: %d\n", ret);
1331 return ret;
1332 }
1333
1334 mvmvif->uploaded = false;
Johannes Bergfb8b8ee2013-10-21 12:37:53 +02001335
Chaya Rachel Ivgi0e39eb02015-12-03 15:51:46 +02001336 if (vif->type == NL80211_IFTYPE_MONITOR) {
Johannes Berg30686bf2015-06-02 21:39:54 +02001337 __clear_bit(IEEE80211_HW_RX_INCLUDES_FCS, mvm->hw->flags);
Chaya Rachel Ivgi0e39eb02015-12-03 15:51:46 +02001338 iwl_mvm_dealloc_snif_sta(mvm);
1339 }
Johannes Bergfb8b8ee2013-10-21 12:37:53 +02001340
Johannes Berg8ca151b2013-01-24 14:25:36 +01001341 return 0;
1342}
Ilan Peer571765c2013-03-05 15:26:03 +02001343
Andrei Otcheretianski7f0a7c62014-05-04 11:48:12 +03001344static void iwl_mvm_csa_count_down(struct iwl_mvm *mvm,
Andrei Otcheretianski7ef0aab2014-11-10 11:10:11 +02001345 struct ieee80211_vif *csa_vif, u32 gp2,
1346 bool tx_success)
Andrei Otcheretianski7f0a7c62014-05-04 11:48:12 +03001347{
1348 struct iwl_mvm_vif *mvmvif =
1349 iwl_mvm_vif_from_mac80211(csa_vif);
1350
Andrei Otcheretianski7ef0aab2014-11-10 11:10:11 +02001351 /* Don't start to countdown from a failed beacon */
1352 if (!tx_success && !mvmvif->csa_countdown)
1353 return;
1354
1355 mvmvif->csa_countdown = true;
1356
Andrei Otcheretianski7f0a7c62014-05-04 11:48:12 +03001357 if (!ieee80211_csa_is_complete(csa_vif)) {
1358 int c = ieee80211_csa_update_counter(csa_vif);
1359
1360 iwl_mvm_mac_ctxt_beacon_changed(mvm, csa_vif);
1361 if (csa_vif->p2p &&
Andrei Otcheretianski7ef0aab2014-11-10 11:10:11 +02001362 !iwl_mvm_te_scheduled(&mvmvif->time_event_data) && gp2 &&
1363 tx_success) {
Andrei Otcheretianski7f0a7c62014-05-04 11:48:12 +03001364 u32 rel_time = (c + 1) *
1365 csa_vif->bss_conf.beacon_int -
Luciano Coelhof991e172014-08-26 16:14:10 +03001366 IWL_MVM_CHANNEL_SWITCH_TIME_GO;
Andrei Otcheretianski7f0a7c62014-05-04 11:48:12 +03001367 u32 apply_time = gp2 + rel_time * 1024;
1368
Luciano Coelhof991e172014-08-26 16:14:10 +03001369 iwl_mvm_schedule_csa_period(mvm, csa_vif,
1370 IWL_MVM_CHANNEL_SWITCH_TIME_GO -
1371 IWL_MVM_CHANNEL_SWITCH_MARGIN,
1372 apply_time);
Andrei Otcheretianski7f0a7c62014-05-04 11:48:12 +03001373 }
1374 } else if (!iwl_mvm_te_scheduled(&mvmvif->time_event_data)) {
1375 /* we don't have CSA NoA scheduled yet, switch now */
1376 ieee80211_csa_finish(csa_vif);
1377 RCU_INIT_POINTER(mvm->csa_vif, NULL);
1378 }
1379}
1380
Johannes Berg04168412015-06-23 21:22:09 +02001381void iwl_mvm_rx_beacon_notif(struct iwl_mvm *mvm,
1382 struct iwl_rx_cmd_buffer *rxb)
Ilan Peer571765c2013-03-05 15:26:03 +02001383{
1384 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Emmanuel Grumbach2d2c0e92014-11-24 08:51:46 +02001385 struct iwl_extended_beacon_notif *beacon = (void *)pkt->data;
David Spinadel1c87bba2014-02-27 16:41:52 +02001386 struct iwl_mvm_tx_resp *beacon_notify_hdr;
Andrei Otcheretianski664322f2014-06-05 16:40:36 +03001387 struct ieee80211_vif *csa_vif;
Andrei Otcheretianski003e52362014-05-25 17:24:22 +03001388 struct ieee80211_vif *tx_blocked_vif;
Sara Sharon12db2942017-01-17 14:28:21 +02001389 struct agg_tx_status *agg_status;
Andrei Otcheretianski7ef0aab2014-11-10 11:10:11 +02001390 u16 status;
Ilan Peer571765c2013-03-05 15:26:03 +02001391
Andrei Otcheretianskibd3398e2013-10-22 05:01:12 +02001392 lockdep_assert_held(&mvm->mutex);
1393
Emmanuel Grumbach2d2c0e92014-11-24 08:51:46 +02001394 beacon_notify_hdr = &beacon->beacon_notify_hdr;
1395 mvm->ap_last_beacon_gp2 = le32_to_cpu(beacon->gp2);
Johannes Berg2f0282d2017-04-20 10:29:03 +02001396 mvm->ibss_manager = beacon->ibss_mgr_status != 0;
David Spinadel1c87bba2014-02-27 16:41:52 +02001397
Sara Sharon12db2942017-01-17 14:28:21 +02001398 agg_status = iwl_mvm_get_agg_status(mvm, beacon_notify_hdr);
1399 status = le16_to_cpu(agg_status->status) & TX_STATUS_MSK;
David Spinadel1c87bba2014-02-27 16:41:52 +02001400 IWL_DEBUG_RX(mvm,
1401 "beacon status %#x retries:%d tsf:0x%16llX gp2:0x%X rate:%d\n",
Emmanuel Grumbach2d2c0e92014-11-24 08:51:46 +02001402 status, beacon_notify_hdr->failure_frame,
1403 le64_to_cpu(beacon->tsf),
David Spinadel1c87bba2014-02-27 16:41:52 +02001404 mvm->ap_last_beacon_gp2,
1405 le32_to_cpu(beacon_notify_hdr->initial_rate));
Andrei Otcheretianskibd3398e2013-10-22 05:01:12 +02001406
Andrei Otcheretianski664322f2014-06-05 16:40:36 +03001407 csa_vif = rcu_dereference_protected(mvm->csa_vif,
1408 lockdep_is_held(&mvm->mutex));
Andrei Otcheretianski7f0a7c62014-05-04 11:48:12 +03001409 if (unlikely(csa_vif && csa_vif->csa_active))
Andrei Otcheretianski7ef0aab2014-11-10 11:10:11 +02001410 iwl_mvm_csa_count_down(mvm, csa_vif, mvm->ap_last_beacon_gp2,
1411 (status == TX_STATUS_SUCCESS));
Andrei Otcheretianskibd3398e2013-10-22 05:01:12 +02001412
Andrei Otcheretianski003e52362014-05-25 17:24:22 +03001413 tx_blocked_vif = rcu_dereference_protected(mvm->csa_tx_blocked_vif,
1414 lockdep_is_held(&mvm->mutex));
1415 if (unlikely(tx_blocked_vif)) {
1416 struct iwl_mvm_vif *mvmvif =
1417 iwl_mvm_vif_from_mac80211(tx_blocked_vif);
1418
1419 /*
1420 * The channel switch is started and we have blocked the
1421 * stations. If this is the first beacon (the timeout wasn't
1422 * set), set the unblock timeout, otherwise countdown
1423 */
1424 if (!mvm->csa_tx_block_bcn_timeout)
1425 mvm->csa_tx_block_bcn_timeout =
1426 IWL_MVM_CS_UNBLOCK_TX_TIMEOUT;
1427 else
1428 mvm->csa_tx_block_bcn_timeout--;
1429
1430 /* Check if the timeout is expired, and unblock tx */
1431 if (mvm->csa_tx_block_bcn_timeout == 0) {
1432 iwl_mvm_modify_all_sta_disable_tx(mvm, mvmvif, false);
1433 RCU_INIT_POINTER(mvm->csa_tx_blocked_vif, NULL);
1434 }
1435 }
Ilan Peer571765c2013-03-05 15:26:03 +02001436}
Hila Gonend64048e2013-03-13 18:00:03 +02001437
1438static void iwl_mvm_beacon_loss_iterator(void *_data, u8 *mac,
1439 struct ieee80211_vif *vif)
1440{
Ilan Peer12d423e2013-12-24 22:08:14 +02001441 struct iwl_missed_beacons_notif *missed_beacons = _data;
Hila Gonend64048e2013-03-13 18:00:03 +02001442 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Emmanuel Grumbach9d761fd2015-02-02 12:44:23 +02001443 struct iwl_mvm *mvm = mvmvif->mvm;
1444 struct iwl_fw_dbg_trigger_missed_bcon *bcon_trig;
1445 struct iwl_fw_dbg_trigger_tlv *trigger;
1446 u32 stop_trig_missed_bcon, stop_trig_missed_bcon_since_rx;
1447 u32 rx_missed_bcon, rx_missed_bcon_since_rx;
Hila Gonend64048e2013-03-13 18:00:03 +02001448
Ilan Peer12d423e2013-12-24 22:08:14 +02001449 if (mvmvif->id != (u16)le32_to_cpu(missed_beacons->mac_id))
1450 return;
1451
Emmanuel Grumbach9d761fd2015-02-02 12:44:23 +02001452 rx_missed_bcon = le32_to_cpu(missed_beacons->consec_missed_beacons);
1453 rx_missed_bcon_since_rx =
1454 le32_to_cpu(missed_beacons->consec_missed_beacons_since_last_rx);
Ilan Peer12d423e2013-12-24 22:08:14 +02001455 /*
1456 * TODO: the threshold should be adjusted based on latency conditions,
1457 * and/or in case of a CS flow on one of the other AP vifs.
1458 */
1459 if (le32_to_cpu(missed_beacons->consec_missed_beacons_since_last_rx) >
1460 IWL_MVM_MISSED_BEACONS_THRESHOLD)
Hila Gonend64048e2013-03-13 18:00:03 +02001461 ieee80211_beacon_loss(vif);
Emmanuel Grumbach9d761fd2015-02-02 12:44:23 +02001462
1463 if (!iwl_fw_dbg_trigger_enabled(mvm->fw,
1464 FW_DBG_TRIGGER_MISSED_BEACONS))
1465 return;
1466
1467 trigger = iwl_fw_dbg_get_trigger(mvm->fw,
1468 FW_DBG_TRIGGER_MISSED_BEACONS);
1469 bcon_trig = (void *)trigger->data;
1470 stop_trig_missed_bcon = le32_to_cpu(bcon_trig->stop_consec_missed_bcon);
1471 stop_trig_missed_bcon_since_rx =
1472 le32_to_cpu(bcon_trig->stop_consec_missed_bcon_since_rx);
1473
1474 /* TODO: implement start trigger */
1475
Johannes Berg7174beb2017-06-01 16:03:19 +02001476 if (!iwl_fw_dbg_trigger_check_stop(&mvm->fwrt,
1477 ieee80211_vif_to_wdev(vif),
1478 trigger))
Emmanuel Grumbach9d761fd2015-02-02 12:44:23 +02001479 return;
1480
1481 if (rx_missed_bcon_since_rx >= stop_trig_missed_bcon_since_rx ||
1482 rx_missed_bcon >= stop_trig_missed_bcon)
Johannes Berg7174beb2017-06-01 16:03:19 +02001483 iwl_fw_dbg_collect_trig(&mvm->fwrt, trigger, NULL);
Hila Gonend64048e2013-03-13 18:00:03 +02001484}
1485
Johannes Berg04168412015-06-23 21:22:09 +02001486void iwl_mvm_rx_missed_beacons_notif(struct iwl_mvm *mvm,
1487 struct iwl_rx_cmd_buffer *rxb)
Hila Gonend64048e2013-03-13 18:00:03 +02001488{
1489 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Ilan Peer12d423e2013-12-24 22:08:14 +02001490 struct iwl_missed_beacons_notif *mb = (void *)pkt->data;
1491
1492 IWL_DEBUG_INFO(mvm,
1493 "missed bcn mac_id=%u, consecutive=%u (%u, %u, %u)\n",
1494 le32_to_cpu(mb->mac_id),
1495 le32_to_cpu(mb->consec_missed_beacons),
1496 le32_to_cpu(mb->consec_missed_beacons_since_last_rx),
1497 le32_to_cpu(mb->num_recvd_beacons),
1498 le32_to_cpu(mb->num_expected_beacons));
Hila Gonend64048e2013-03-13 18:00:03 +02001499
1500 ieee80211_iterate_active_interfaces_atomic(mvm->hw,
1501 IEEE80211_IFACE_ITER_NORMAL,
1502 iwl_mvm_beacon_loss_iterator,
Ilan Peer12d423e2013-12-24 22:08:14 +02001503 mb);
Hila Gonend64048e2013-03-13 18:00:03 +02001504}
Sara Sharon0db056d2015-12-29 11:07:15 +02001505
1506void iwl_mvm_rx_stored_beacon_notif(struct iwl_mvm *mvm,
1507 struct iwl_rx_cmd_buffer *rxb)
1508{
1509 struct iwl_rx_packet *pkt = rxb_addr(rxb);
1510 struct iwl_stored_beacon_notif *sb = (void *)pkt->data;
1511 struct ieee80211_rx_status rx_status;
1512 struct sk_buff *skb;
1513 u32 size = le32_to_cpu(sb->byte_count);
1514
1515 if (size == 0)
1516 return;
1517
1518 skb = alloc_skb(size, GFP_ATOMIC);
1519 if (!skb) {
1520 IWL_ERR(mvm, "alloc_skb failed\n");
1521 return;
1522 }
1523
1524 /* update rx_status according to the notification's metadata */
1525 memset(&rx_status, 0, sizeof(rx_status));
1526 rx_status.mactime = le64_to_cpu(sb->tsf);
Sara Sharon77fe7392016-01-10 14:23:25 +02001527 /* TSF as indicated by the firmware is at INA time */
1528 rx_status.flag |= RX_FLAG_MACTIME_PLCP_START;
Sara Sharon0db056d2015-12-29 11:07:15 +02001529 rx_status.device_timestamp = le32_to_cpu(sb->system_time);
1530 rx_status.band =
Sara Sharon34118c22016-11-08 18:34:42 +02001531 (sb->band & cpu_to_le16(RX_RES_PHY_FLAGS_BAND_24)) ?
Johannes Berg57fbcce2016-04-12 15:56:15 +02001532 NL80211_BAND_2GHZ : NL80211_BAND_5GHZ;
Sara Sharon0db056d2015-12-29 11:07:15 +02001533 rx_status.freq =
1534 ieee80211_channel_to_frequency(le16_to_cpu(sb->channel),
1535 rx_status.band);
1536
1537 /* copy the data */
Johannes Berg59ae1d12017-06-16 14:29:20 +02001538 skb_put_data(skb, sb->data, size);
Sara Sharon0db056d2015-12-29 11:07:15 +02001539 memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
1540
1541 /* pass it as regular rx to mac80211 */
Johannes Bergd63b5482016-03-31 20:02:02 +03001542 ieee80211_rx_napi(mvm->hw, NULL, skb, NULL);
Sara Sharon0db056d2015-12-29 11:07:15 +02001543}
Andrei Otcheretianskid3a108a2016-02-28 17:12:21 +02001544
1545void iwl_mvm_channel_switch_noa_notif(struct iwl_mvm *mvm,
1546 struct iwl_rx_cmd_buffer *rxb)
1547{
1548 struct iwl_rx_packet *pkt = rxb_addr(rxb);
1549 struct iwl_channel_switch_noa_notif *notif = (void *)pkt->data;
1550 struct ieee80211_vif *csa_vif;
1551 struct iwl_mvm_vif *mvmvif;
1552 int len = iwl_rx_packet_payload_len(pkt);
1553 u32 id_n_color;
1554
1555 if (WARN_ON_ONCE(len < sizeof(*notif)))
1556 return;
1557
1558 rcu_read_lock();
1559
1560 csa_vif = rcu_dereference(mvm->csa_vif);
1561 if (WARN_ON(!csa_vif || !csa_vif->csa_active))
1562 goto out_unlock;
1563
1564 id_n_color = le32_to_cpu(notif->id_and_color);
1565
1566 mvmvif = iwl_mvm_vif_from_mac80211(csa_vif);
1567 if (WARN(FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color) != id_n_color,
1568 "channel switch noa notification on unexpected vif (csa_vif=%d, notif=%d)",
1569 FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color), id_n_color))
1570 goto out_unlock;
1571
1572 IWL_DEBUG_INFO(mvm, "Channel Switch Started Notification\n");
1573
Johannes Berg86bbb1e2017-03-22 21:44:59 +01001574 schedule_delayed_work(&mvm->cs_tx_unblock_dwork,
1575 msecs_to_jiffies(IWL_MVM_CS_UNBLOCK_TX_TIMEOUT *
1576 csa_vif->bss_conf.beacon_int));
Andrei Otcheretianskid3a108a2016-02-28 17:12:21 +02001577
1578 ieee80211_csa_finish(csa_vif);
1579
1580 rcu_read_unlock();
1581
1582 RCU_INIT_POINTER(mvm->csa_vif, NULL);
1583
1584 return;
1585
1586out_unlock:
1587 rcu_read_unlock();
1588}