blob: 604db47c8c21ae5eeabca92ae8c1292ee597ce27 [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
244static void iwl_mvm_mac_sta_hw_queues_iter(void *_data,
245 struct ieee80211_sta *sta)
246{
247 struct iwl_mvm_hw_queues_iface_iterator_data *data = _data;
248 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
249
250 /* Mark the queues used by the sta */
251 data->used_hw_queues |= mvmsta->tfd_queue_msk;
252}
253
254unsigned long iwl_mvm_get_used_hw_queues(struct iwl_mvm *mvm,
255 struct ieee80211_vif *exclude_vif)
256{
Arik Nemtsov51ec09a2015-01-21 11:50:31 +0200257 u8 sta_id;
Arik Nemtsova74346d2014-09-21 19:07:30 +0300258 struct iwl_mvm_hw_queues_iface_iterator_data data = {
259 .exclude_vif = exclude_vif,
260 .used_hw_queues =
261 BIT(IWL_MVM_OFFCHANNEL_QUEUE) |
Liad Kaufman097129c2015-08-09 18:28:43 +0300262 BIT(mvm->aux_queue),
Arik Nemtsova74346d2014-09-21 19:07:30 +0300263 };
264
Liad Kaufman097129c2015-08-09 18:28:43 +0300265 if (iwl_mvm_is_dqa_supported(mvm))
Johannes Berg32026e82017-06-19 23:26:55 +0200266 data.used_hw_queues |= BIT(IWL_MVM_DQA_GCAST_QUEUE);
Liad Kaufman097129c2015-08-09 18:28:43 +0300267 else
268 data.used_hw_queues |= BIT(IWL_MVM_CMD_QUEUE);
269
Arik Nemtsova74346d2014-09-21 19:07:30 +0300270 lockdep_assert_held(&mvm->mutex);
271
272 /* mark all VIF used hw queues */
273 ieee80211_iterate_active_interfaces_atomic(
274 mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
275 iwl_mvm_iface_hw_queues_iter, &data);
276
Johannes Berg32026e82017-06-19 23:26:55 +0200277 /*
278 * for DQA, the hw_queue in mac80211 is never really used for
279 * real traffic (only the few queue IDs covered above), so
280 * we can reuse the real HW queue IDs the stations use
281 */
282 if (iwl_mvm_is_dqa_supported(mvm))
283 return data.used_hw_queues;
284
Arik Nemtsova74346d2014-09-21 19:07:30 +0300285 /* don't assign the same hw queues as TDLS stations */
286 ieee80211_iterate_stations_atomic(mvm->hw,
287 iwl_mvm_mac_sta_hw_queues_iter,
288 &data);
289
Arik Nemtsov51ec09a2015-01-21 11:50:31 +0200290 /*
291 * Some TDLS stations may be removed but are in the process of being
292 * drained. Don't touch their queues.
293 */
294 for_each_set_bit(sta_id, mvm->sta_drained, IWL_MVM_STATION_COUNT)
295 data.used_hw_queues |= mvm->tfd_drained[sta_id];
296
Arik Nemtsova74346d2014-09-21 19:07:30 +0300297 return data.used_hw_queues;
298}
299
Ilan Peer6e97b0d2013-12-23 22:18:02 +0200300static void iwl_mvm_mac_iface_iterator(void *_data, u8 *mac,
301 struct ieee80211_vif *vif)
302{
303 struct iwl_mvm_mac_iface_iterator_data *data = _data;
304 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Ilan Peer6e97b0d2013-12-23 22:18:02 +0200305
306 /* Iterator may already find the interface being added -- skip it */
307 if (vif == data->vif) {
308 data->found_vif = true;
309 return;
310 }
311
Ilan Peer6e97b0d2013-12-23 22:18:02 +0200312 /* Mark MAC IDs as used by clearing the available bit, and
313 * (below) mark TSFs as used if their existing use is not
314 * compatible with the new interface type.
315 * No locking or atomic bit operations are needed since the
316 * data is on the stack of the caller function.
317 */
318 __clear_bit(mvmvif->id, data->available_mac_ids);
319
320 /* find a suitable tsf_id */
321 iwl_mvm_mac_tsf_id_iter(_data, mac, vif);
322}
323
Ilan Peer6e97b0d2013-12-23 22:18:02 +0200324void iwl_mvm_mac_ctxt_recalc_tsf_id(struct iwl_mvm *mvm,
325 struct ieee80211_vif *vif)
326{
327 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
328 struct iwl_mvm_mac_iface_iterator_data data = {
329 .mvm = mvm,
330 .vif = vif,
331 .available_tsf_ids = { (1 << NUM_TSF_IDS) - 1 },
332 /* no preference yet */
333 .preferred_tsf = NUM_TSF_IDS,
334 };
335
336 ieee80211_iterate_active_interfaces_atomic(
337 mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
338 iwl_mvm_mac_tsf_id_iter, &data);
339
340 if (data.preferred_tsf != NUM_TSF_IDS)
341 mvmvif->tsf_id = data.preferred_tsf;
342 else if (!test_bit(mvmvif->tsf_id, data.available_tsf_ids))
343 mvmvif->tsf_id = find_first_bit(data.available_tsf_ids,
344 NUM_TSF_IDS);
345}
346
Johannes Berg8ca151b2013-01-24 14:25:36 +0100347static int iwl_mvm_mac_ctxt_allocate_resources(struct iwl_mvm *mvm,
348 struct ieee80211_vif *vif)
349{
350 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
351 struct iwl_mvm_mac_iface_iterator_data data = {
352 .mvm = mvm,
353 .vif = vif,
354 .available_mac_ids = { (1 << NUM_MAC_INDEX_DRIVER) - 1 },
355 .available_tsf_ids = { (1 << NUM_TSF_IDS) - 1 },
356 /* no preference yet */
357 .preferred_tsf = NUM_TSF_IDS,
Johannes Berg8ca151b2013-01-24 14:25:36 +0100358 .found_vif = false,
359 };
360 u32 ac;
Johannes Berg32026e82017-06-19 23:26:55 +0200361 int ret, i, queue_limit;
Johannes Berg110cf812014-08-01 23:14:24 +0200362 unsigned long used_hw_queues;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100363
364 /*
365 * Allocate a MAC ID and a TSF for this MAC, along with the queues
366 * and other resources.
367 */
368
369 /*
370 * Before the iterator, we start with all MAC IDs and TSFs available.
371 *
372 * During iteration, all MAC IDs are cleared that are in use by other
373 * virtual interfaces, and all TSF IDs are cleared that can't be used
374 * by this new virtual interface because they're used by an interface
375 * that can't share it with the new one.
376 * At the same time, we check if there's a preferred TSF in the case
377 * that we should share it with another interface.
378 */
379
Johannes Berg5023d962013-07-31 14:07:43 +0200380 /* Currently, MAC ID 0 should be used only for the managed/IBSS vif */
381 switch (vif->type) {
382 case NL80211_IFTYPE_ADHOC:
383 break;
384 case NL80211_IFTYPE_STATION:
385 if (!vif->p2p)
386 break;
387 /* fall through */
388 default:
Ilan Peerec8b6882013-02-13 13:27:18 +0200389 __clear_bit(0, data.available_mac_ids);
Johannes Berg5023d962013-07-31 14:07:43 +0200390 }
Ilan Peerec8b6882013-02-13 13:27:18 +0200391
Johannes Berg8ca151b2013-01-24 14:25:36 +0100392 ieee80211_iterate_active_interfaces_atomic(
393 mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
394 iwl_mvm_mac_iface_iterator, &data);
395
Arik Nemtsova74346d2014-09-21 19:07:30 +0300396 used_hw_queues = iwl_mvm_get_used_hw_queues(mvm, vif);
397
Johannes Berg8ca151b2013-01-24 14:25:36 +0100398 /*
399 * In the case we're getting here during resume, it's similar to
400 * firmware restart, and with RESUME_ALL the iterator will find
401 * the vif being added already.
402 * We don't want to reassign any IDs in either case since doing
403 * so would probably assign different IDs (as interfaces aren't
404 * necessarily added in the same order), but the old IDs were
405 * preserved anyway, so skip ID assignment for both resume and
406 * recovery.
407 */
408 if (data.found_vif)
409 return 0;
410
411 /* Therefore, in recovery, we can't get here */
Ilan Peerfd11bd02013-06-16 12:18:11 +0300412 if (WARN_ON_ONCE(test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)))
413 return -EBUSY;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100414
415 mvmvif->id = find_first_bit(data.available_mac_ids,
416 NUM_MAC_INDEX_DRIVER);
417 if (mvmvif->id == NUM_MAC_INDEX_DRIVER) {
418 IWL_ERR(mvm, "Failed to init MAC context - no free ID!\n");
419 ret = -EIO;
420 goto exit_fail;
421 }
422
423 if (data.preferred_tsf != NUM_TSF_IDS)
424 mvmvif->tsf_id = data.preferred_tsf;
425 else
426 mvmvif->tsf_id = find_first_bit(data.available_tsf_ids,
427 NUM_TSF_IDS);
428 if (mvmvif->tsf_id == NUM_TSF_IDS) {
429 IWL_ERR(mvm, "Failed to init MAC context - no free TSF!\n");
430 ret = -EIO;
431 goto exit_fail;
432 }
433
434 mvmvif->color = 0;
435
Ilan Peer1e849c92013-02-13 12:26:39 +0200436 INIT_LIST_HEAD(&mvmvif->time_event_data.list);
437 mvmvif->time_event_data.id = TE_MAX;
438
Johannes Berg8ca151b2013-01-24 14:25:36 +0100439 /* No need to allocate data queues to P2P Device MAC.*/
440 if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
441 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
442 vif->hw_queue[ac] = IEEE80211_INVAL_HW_QUEUE;
443
444 return 0;
445 }
446
Johannes Berg32026e82017-06-19 23:26:55 +0200447 if (iwl_mvm_is_dqa_supported(mvm)) {
448 /*
449 * queues in mac80211 almost entirely independent of
450 * the ones here - no real limit
451 */
452 queue_limit = IEEE80211_MAX_QUEUES;
453 BUILD_BUG_ON(IEEE80211_MAX_QUEUES >
454 BITS_PER_BYTE *
455 sizeof(mvm->hw_queue_to_mac80211[0]));
456 } else {
457 /* need to not use too many in this case */
458 queue_limit = mvm->first_agg_queue;
459 }
460
Liad Kaufman24afba72015-07-28 18:56:08 +0300461 /*
462 * Find available queues, and allocate them to the ACs. When in
463 * DQA-mode they aren't really used, and this is done only so the
464 * mac80211 ieee80211_check_queues() function won't fail
465 */
Johannes Berg8ca151b2013-01-24 14:25:36 +0100466 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
Johannes Berg32026e82017-06-19 23:26:55 +0200467 u8 queue = find_first_zero_bit(&used_hw_queues, queue_limit);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100468
Johannes Berg32026e82017-06-19 23:26:55 +0200469 if (queue >= queue_limit) {
Johannes Berg8ca151b2013-01-24 14:25:36 +0100470 IWL_ERR(mvm, "Failed to allocate queue\n");
471 ret = -EIO;
472 goto exit_fail;
473 }
474
Johannes Berg110cf812014-08-01 23:14:24 +0200475 __set_bit(queue, &used_hw_queues);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100476 vif->hw_queue[ac] = queue;
477 }
478
479 /* Allocate the CAB queue for softAP and GO interfaces */
480 if (vif->type == NL80211_IFTYPE_AP) {
Liad Kaufman0e0e4422015-08-04 15:13:38 +0300481 u8 queue;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100482
Liad Kaufman0e0e4422015-08-04 15:13:38 +0300483 if (!iwl_mvm_is_dqa_supported(mvm)) {
484 queue = find_first_zero_bit(&used_hw_queues,
485 mvm->first_agg_queue);
486
487 if (queue >= mvm->first_agg_queue) {
488 IWL_ERR(mvm, "Failed to allocate cab queue\n");
489 ret = -EIO;
490 goto exit_fail;
491 }
492 } else {
493 queue = IWL_MVM_DQA_GCAST_QUEUE;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100494 }
495
Sara Sharone2af3fa2017-02-22 19:35:10 +0200496 /*
497 * For TVQM this will be overwritten later with the FW assigned
498 * queue value (when queue is enabled).
499 */
500 mvmvif->cab_queue = queue;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100501 vif->cab_queue = queue;
502 } else {
503 vif->cab_queue = IEEE80211_INVAL_HW_QUEUE;
504 }
505
Sara Sharon0ae98812017-01-04 14:53:58 +0200506 mvmvif->bcast_sta.sta_id = IWL_MVM_INVALID_STA;
507 mvmvif->mcast_sta.sta_id = IWL_MVM_INVALID_STA;
508 mvmvif->ap_sta_id = IWL_MVM_INVALID_STA;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100509
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +0300510 for (i = 0; i < NUM_IWL_MVM_SMPS_REQ; i++)
511 mvmvif->smps_requests[i] = IEEE80211_SMPS_AUTOMATIC;
512
Johannes Berg8ca151b2013-01-24 14:25:36 +0100513 return 0;
514
515exit_fail:
516 memset(mvmvif, 0, sizeof(struct iwl_mvm_vif));
517 memset(vif->hw_queue, IEEE80211_INVAL_HW_QUEUE, sizeof(vif->hw_queue));
518 vif->cab_queue = IEEE80211_INVAL_HW_QUEUE;
519 return ret;
520}
521
522int iwl_mvm_mac_ctxt_init(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
523{
Emmanuel Grumbach5d42e7b2015-03-19 20:04:51 +0200524 unsigned int wdg_timeout =
525 iwl_mvm_get_wd_timeout(mvm, vif, false, false);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100526 u32 ac;
527 int ret;
528
529 lockdep_assert_held(&mvm->mutex);
530
531 ret = iwl_mvm_mac_ctxt_allocate_resources(mvm, vif);
532 if (ret)
533 return ret;
534
Sara Sharon8236f7d2016-10-06 16:42:32 +0300535 /* If DQA is supported - queues will be enabled when needed */
536 if (iwl_mvm_is_dqa_supported(mvm))
537 return 0;
538
Johannes Berg8ca151b2013-01-24 14:25:36 +0100539 switch (vif->type) {
540 case NL80211_IFTYPE_P2P_DEVICE:
Sara Sharon8236f7d2016-10-06 16:42:32 +0300541 iwl_mvm_enable_ac_txq(mvm, IWL_MVM_OFFCHANNEL_QUEUE,
542 IWL_MVM_OFFCHANNEL_QUEUE,
543 IWL_MVM_TX_FIFO_VO, 0, wdg_timeout);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100544 break;
545 case NL80211_IFTYPE_AP:
Liad Kaufman4ecafae2015-07-14 13:36:18 +0300546 iwl_mvm_enable_ac_txq(mvm, vif->cab_queue, vif->cab_queue,
Liad Kaufman5c1156e2015-07-22 17:59:53 +0300547 IWL_MVM_TX_FIFO_MCAST, 0, wdg_timeout);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100548 /* fall through */
549 default:
550 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
Avri Altman3edf8ff2014-07-30 11:41:01 +0300551 iwl_mvm_enable_ac_txq(mvm, vif->hw_queue[ac],
Liad Kaufman4ecafae2015-07-14 13:36:18 +0300552 vif->hw_queue[ac],
Liad Kaufman5c1156e2015-07-22 17:59:53 +0300553 iwl_mvm_ac_to_tx_fifo[ac], 0,
Emmanuel Grumbach4cf677f2015-01-12 14:38:29 +0200554 wdg_timeout);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100555 break;
556 }
557
558 return 0;
559}
560
561void iwl_mvm_mac_ctxt_release(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
562{
563 int ac;
564
565 lockdep_assert_held(&mvm->mutex);
566
Liad Kaufmandf88c082016-11-24 15:31:00 +0200567 /*
568 * If DQA is supported - queues were already disabled, since in
569 * DQA-mode the queues are a property of the STA and not of the
570 * vif, and at this point the STA was already deleted
571 */
572 if (iwl_mvm_is_dqa_supported(mvm))
573 return;
574
Johannes Berg8ca151b2013-01-24 14:25:36 +0100575 switch (vif->type) {
576 case NL80211_IFTYPE_P2P_DEVICE:
Liad Kaufmandf88c082016-11-24 15:31:00 +0200577 iwl_mvm_disable_txq(mvm, IWL_MVM_OFFCHANNEL_QUEUE,
578 IWL_MVM_OFFCHANNEL_QUEUE,
579 IWL_MAX_TID_COUNT, 0);
Liad Kaufman4c965132015-08-09 19:26:56 +0300580
Johannes Berg8ca151b2013-01-24 14:25:36 +0100581 break;
582 case NL80211_IFTYPE_AP:
Liad Kaufman4ecafae2015-07-14 13:36:18 +0300583 iwl_mvm_disable_txq(mvm, vif->cab_queue, vif->cab_queue,
584 IWL_MAX_TID_COUNT, 0);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100585 /* fall through */
586 default:
587 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
Liad Kaufman4ecafae2015-07-14 13:36:18 +0300588 iwl_mvm_disable_txq(mvm, vif->hw_queue[ac],
589 vif->hw_queue[ac],
590 IWL_MAX_TID_COUNT, 0);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100591 }
592}
593
594static void iwl_mvm_ack_rates(struct iwl_mvm *mvm,
595 struct ieee80211_vif *vif,
Johannes Berg57fbcce2016-04-12 15:56:15 +0200596 enum nl80211_band band,
Johannes Berg8ca151b2013-01-24 14:25:36 +0100597 u8 *cck_rates, u8 *ofdm_rates)
598{
599 struct ieee80211_supported_band *sband;
600 unsigned long basic = vif->bss_conf.basic_rates;
601 int lowest_present_ofdm = 100;
602 int lowest_present_cck = 100;
603 u8 cck = 0;
604 u8 ofdm = 0;
605 int i;
606
607 sband = mvm->hw->wiphy->bands[band];
608
609 for_each_set_bit(i, &basic, BITS_PER_LONG) {
610 int hw = sband->bitrates[i].hw_value;
611 if (hw >= IWL_FIRST_OFDM_RATE) {
612 ofdm |= BIT(hw - IWL_FIRST_OFDM_RATE);
613 if (lowest_present_ofdm > hw)
614 lowest_present_ofdm = hw;
615 } else {
616 BUILD_BUG_ON(IWL_FIRST_CCK_RATE != 0);
617
618 cck |= BIT(hw);
619 if (lowest_present_cck > hw)
620 lowest_present_cck = hw;
621 }
622 }
623
624 /*
625 * Now we've got the basic rates as bitmaps in the ofdm and cck
626 * variables. This isn't sufficient though, as there might not
627 * be all the right rates in the bitmap. E.g. if the only basic
628 * rates are 5.5 Mbps and 11 Mbps, we still need to add 1 Mbps
629 * and 6 Mbps because the 802.11-2007 standard says in 9.6:
630 *
631 * [...] a STA responding to a received frame shall transmit
632 * its Control Response frame [...] at the highest rate in the
633 * BSSBasicRateSet parameter that is less than or equal to the
634 * rate of the immediately previous frame in the frame exchange
635 * sequence ([...]) and that is of the same modulation class
636 * ([...]) as the received frame. If no rate contained in the
637 * BSSBasicRateSet parameter meets these conditions, then the
638 * control frame sent in response to a received frame shall be
639 * transmitted at the highest mandatory rate of the PHY that is
640 * less than or equal to the rate of the received frame, and
641 * that is of the same modulation class as the received frame.
642 *
643 * As a consequence, we need to add all mandatory rates that are
644 * lower than all of the basic rates to these bitmaps.
645 */
646
647 if (IWL_RATE_24M_INDEX < lowest_present_ofdm)
648 ofdm |= IWL_RATE_BIT_MSK(24) >> IWL_FIRST_OFDM_RATE;
649 if (IWL_RATE_12M_INDEX < lowest_present_ofdm)
650 ofdm |= IWL_RATE_BIT_MSK(12) >> IWL_FIRST_OFDM_RATE;
651 /* 6M already there or needed so always add */
652 ofdm |= IWL_RATE_BIT_MSK(6) >> IWL_FIRST_OFDM_RATE;
653
654 /*
655 * CCK is a bit more complex with DSSS vs. HR/DSSS vs. ERP.
656 * Note, however:
657 * - if no CCK rates are basic, it must be ERP since there must
658 * be some basic rates at all, so they're OFDM => ERP PHY
659 * (or we're in 5 GHz, and the cck bitmap will never be used)
660 * - if 11M is a basic rate, it must be ERP as well, so add 5.5M
661 * - if 5.5M is basic, 1M and 2M are mandatory
662 * - if 2M is basic, 1M is mandatory
663 * - if 1M is basic, that's the only valid ACK rate.
664 * As a consequence, it's not as complicated as it sounds, just add
665 * any lower rates to the ACK rate bitmap.
666 */
667 if (IWL_RATE_11M_INDEX < lowest_present_cck)
668 cck |= IWL_RATE_BIT_MSK(11) >> IWL_FIRST_CCK_RATE;
669 if (IWL_RATE_5M_INDEX < lowest_present_cck)
670 cck |= IWL_RATE_BIT_MSK(5) >> IWL_FIRST_CCK_RATE;
671 if (IWL_RATE_2M_INDEX < lowest_present_cck)
672 cck |= IWL_RATE_BIT_MSK(2) >> IWL_FIRST_CCK_RATE;
673 /* 1M already there or needed so always add */
674 cck |= IWL_RATE_BIT_MSK(1) >> IWL_FIRST_CCK_RATE;
675
676 *cck_rates = cck;
677 *ofdm_rates = ofdm;
678}
679
Avri Altman8a5e3662013-11-12 19:16:03 +0200680static void iwl_mvm_mac_ctxt_set_ht_flags(struct iwl_mvm *mvm,
681 struct ieee80211_vif *vif,
682 struct iwl_mac_ctx_cmd *cmd)
683{
684 /* for both sta and ap, ht_operation_mode hold the protection_mode */
685 u8 protection_mode = vif->bss_conf.ht_operation_mode &
686 IEEE80211_HT_OP_MODE_PROTECTION;
687 /* The fw does not distinguish between ht and fat */
688 u32 ht_flag = MAC_PROT_FLG_HT_PROT | MAC_PROT_FLG_FAT_PROT;
689
690 IWL_DEBUG_RATE(mvm, "protection mode set to %d\n", protection_mode);
691 /*
692 * See section 9.23.3.1 of IEEE 80211-2012.
693 * Nongreenfield HT STAs Present is not supported.
694 */
695 switch (protection_mode) {
696 case IEEE80211_HT_OP_MODE_PROTECTION_NONE:
697 break;
698 case IEEE80211_HT_OP_MODE_PROTECTION_NONMEMBER:
699 case IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED:
700 cmd->protection_flags |= cpu_to_le32(ht_flag);
701 break;
702 case IEEE80211_HT_OP_MODE_PROTECTION_20MHZ:
703 /* Protect when channel wider than 20MHz */
704 if (vif->bss_conf.chandef.width > NL80211_CHAN_WIDTH_20)
705 cmd->protection_flags |= cpu_to_le32(ht_flag);
706 break;
707 default:
708 IWL_ERR(mvm, "Illegal protection mode %d\n",
709 protection_mode);
710 break;
711 }
712}
713
Johannes Berg8ca151b2013-01-24 14:25:36 +0100714static void iwl_mvm_mac_ctxt_cmd_common(struct iwl_mvm *mvm,
715 struct ieee80211_vif *vif,
716 struct iwl_mac_ctx_cmd *cmd,
Johannes Berg3dfd3a92014-08-11 21:37:30 +0200717 const u8 *bssid_override,
Johannes Berg8ca151b2013-01-24 14:25:36 +0100718 u32 action)
719{
720 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
721 struct ieee80211_chanctx_conf *chanctx;
Avri Altman8a5e3662013-11-12 19:16:03 +0200722 bool ht_enabled = !!(vif->bss_conf.ht_operation_mode &
723 IEEE80211_HT_OP_MODE_PROTECTION);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100724 u8 cck_ack_rates, ofdm_ack_rates;
Johannes Berg3dfd3a92014-08-11 21:37:30 +0200725 const u8 *bssid = bssid_override ?: vif->bss_conf.bssid;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100726 int i;
727
728 cmd->id_and_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
729 mvmvif->color));
730 cmd->action = cpu_to_le32(action);
731
732 switch (vif->type) {
733 case NL80211_IFTYPE_STATION:
734 if (vif->p2p)
735 cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_P2P_STA);
736 else
737 cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_BSS_STA);
738 break;
739 case NL80211_IFTYPE_AP:
740 cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_GO);
741 break;
742 case NL80211_IFTYPE_MONITOR:
743 cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_LISTENER);
744 break;
745 case NL80211_IFTYPE_P2P_DEVICE:
746 cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_P2P_DEVICE);
747 break;
748 case NL80211_IFTYPE_ADHOC:
749 cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_IBSS);
750 break;
751 default:
752 WARN_ON_ONCE(1);
753 }
754
755 cmd->tsf_id = cpu_to_le32(mvmvif->tsf_id);
756
757 memcpy(cmd->node_addr, vif->addr, ETH_ALEN);
Johannes Berg3dfd3a92014-08-11 21:37:30 +0200758
759 if (bssid)
760 memcpy(cmd->bssid_addr, bssid, ETH_ALEN);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100761 else
762 eth_broadcast_addr(cmd->bssid_addr);
763
764 rcu_read_lock();
765 chanctx = rcu_dereference(vif->chanctx_conf);
766 iwl_mvm_ack_rates(mvm, vif, chanctx ? chanctx->def.chan->band
Johannes Berg57fbcce2016-04-12 15:56:15 +0200767 : NL80211_BAND_2GHZ,
Johannes Berg8ca151b2013-01-24 14:25:36 +0100768 &cck_ack_rates, &ofdm_ack_rates);
769 rcu_read_unlock();
770
771 cmd->cck_rates = cpu_to_le32((u32)cck_ack_rates);
772 cmd->ofdm_rates = cpu_to_le32((u32)ofdm_ack_rates);
773
774 cmd->cck_short_preamble =
775 cpu_to_le32(vif->bss_conf.use_short_preamble ?
776 MAC_FLG_SHORT_PREAMBLE : 0);
777 cmd->short_slot =
778 cpu_to_le32(vif->bss_conf.use_short_slot ?
779 MAC_FLG_SHORT_SLOT : 0);
780
Ayala Bekerbd6f5bd2015-12-20 09:27:50 +0200781 cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP);
782
Emmanuel Grumbach3dd94792013-12-19 16:50:49 +0200783 for (i = 0; i < IEEE80211_NUM_ACS; i++) {
Emmanuel Grumbachcf6c6ea2017-06-13 13:18:48 +0300784 u8 txf = iwl_mvm_mac_ac_to_tx_fifo(mvm, i);
Emmanuel Grumbach3dd94792013-12-19 16:50:49 +0200785
786 cmd->ac[txf].cw_min =
787 cpu_to_le16(mvmvif->queue_params[i].cw_min);
788 cmd->ac[txf].cw_max =
789 cpu_to_le16(mvmvif->queue_params[i].cw_max);
790 cmd->ac[txf].edca_txop =
Johannes Berg8ca151b2013-01-24 14:25:36 +0100791 cpu_to_le16(mvmvif->queue_params[i].txop * 32);
Emmanuel Grumbach3dd94792013-12-19 16:50:49 +0200792 cmd->ac[txf].aifsn = mvmvif->queue_params[i].aifs;
793 cmd->ac[txf].fifos_mask = BIT(txf);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100794 }
795
796 if (vif->bss_conf.qos)
797 cmd->qos_flags |= cpu_to_le32(MAC_QOS_FLG_UPDATE_EDCA);
798
Emmanuel Grumbachdc271ee2014-07-03 20:46:35 +0300799 if (vif->bss_conf.use_cts_prot)
Dor Shaishcc7ee2b2013-02-18 13:51:36 +0200800 cmd->protection_flags |= cpu_to_le32(MAC_PROT_FLG_TGG_PROTECT);
Emmanuel Grumbachdc271ee2014-07-03 20:46:35 +0300801
Avri Altman8a5e3662013-11-12 19:16:03 +0200802 IWL_DEBUG_RATE(mvm, "use_cts_prot %d, ht_operation_mode %d\n",
803 vif->bss_conf.use_cts_prot,
804 vif->bss_conf.ht_operation_mode);
805 if (vif->bss_conf.chandef.width != NL80211_CHAN_WIDTH_20_NOHT)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100806 cmd->qos_flags |= cpu_to_le32(MAC_QOS_FLG_TGN);
Avri Altman8a5e3662013-11-12 19:16:03 +0200807 if (ht_enabled)
808 iwl_mvm_mac_ctxt_set_ht_flags(mvm, vif, cmd);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100809}
810
811static int iwl_mvm_mac_ctxt_send_cmd(struct iwl_mvm *mvm,
812 struct iwl_mac_ctx_cmd *cmd)
813{
Emmanuel Grumbacha1022922014-05-12 11:36:41 +0300814 int ret = iwl_mvm_send_cmd_pdu(mvm, MAC_CONTEXT_CMD, 0,
Johannes Berg8ca151b2013-01-24 14:25:36 +0100815 sizeof(*cmd), cmd);
816 if (ret)
817 IWL_ERR(mvm, "Failed to send MAC context (action:%d): %d\n",
818 le32_to_cpu(cmd->action), ret);
819 return ret;
820}
821
Luciano Coelhocf520232014-05-13 16:52:54 +0300822static int iwl_mvm_mac_ctxt_cmd_sta(struct iwl_mvm *mvm,
823 struct ieee80211_vif *vif,
Johannes Berg3dfd3a92014-08-11 21:37:30 +0200824 u32 action, bool force_assoc_off,
825 const u8 *bssid_override)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100826{
Luciano Coelhocf520232014-05-13 16:52:54 +0300827 struct iwl_mac_ctx_cmd cmd = {};
828 struct iwl_mac_data_sta *ctxt_sta;
829
830 WARN_ON(vif->type != NL80211_IFTYPE_STATION);
831
832 /* Fill the common data for all mac context types */
Johannes Berg3dfd3a92014-08-11 21:37:30 +0200833 iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, bssid_override, action);
Luciano Coelhocf520232014-05-13 16:52:54 +0300834
835 if (vif->p2p) {
836 struct ieee80211_p2p_noa_attr *noa =
837 &vif->bss_conf.p2p_noa_attr;
838
839 cmd.p2p_sta.ctwin = cpu_to_le32(noa->oppps_ctwindow &
840 IEEE80211_P2P_OPPPS_CTWINDOW_MASK);
841 ctxt_sta = &cmd.p2p_sta.sta;
842 } else {
Luciano Coelhocf520232014-05-13 16:52:54 +0300843 ctxt_sta = &cmd.sta;
844 }
845
Johannes Berg210a5442013-01-24 23:48:23 +0100846 /* We need the dtim_period to set the MAC as associated */
Alexander Bondarba283922013-05-02 16:34:48 +0300847 if (vif->bss_conf.assoc && vif->bss_conf.dtim_period &&
848 !force_assoc_off) {
Johannes Bergd2931bb2013-02-05 18:10:04 +0100849 u32 dtim_offs;
850
851 /*
852 * The DTIM count counts down, so when it is N that means N
853 * more beacon intervals happen until the DTIM TBTT. Therefore
854 * add this to the current time. If that ends up being in the
855 * future, the firmware will handle it.
856 *
857 * Also note that the system_timestamp (which we get here as
858 * "sync_device_ts") and TSF timestamp aren't at exactly the
859 * same offset in the frame -- the TSF is at the first symbol
860 * of the TSF, the system timestamp is at signal acquisition
861 * time. This means there's an offset between them of at most
862 * a few hundred microseconds (24 * 8 bits + PLCP time gives
863 * 384us in the longest case), this is currently not relevant
864 * as the firmware wakes up around 2ms before the TBTT.
865 */
866 dtim_offs = vif->bss_conf.sync_dtim_count *
867 vif->bss_conf.beacon_int;
868 /* convert TU to usecs */
869 dtim_offs *= 1024;
870
871 ctxt_sta->dtim_tsf =
872 cpu_to_le64(vif->bss_conf.sync_tsf + dtim_offs);
873 ctxt_sta->dtim_time =
874 cpu_to_le32(vif->bss_conf.sync_device_ts + dtim_offs);
Avraham Sternb5669722017-05-09 16:40:01 +0300875 ctxt_sta->assoc_beacon_arrive_time =
876 cpu_to_le32(vif->bss_conf.sync_device_ts);
Johannes Bergd2931bb2013-02-05 18:10:04 +0100877
878 IWL_DEBUG_INFO(mvm, "DTIM TBTT is 0x%llx/0x%x, offset %d\n",
879 le64_to_cpu(ctxt_sta->dtim_tsf),
880 le32_to_cpu(ctxt_sta->dtim_time),
881 dtim_offs);
882
Johannes Berg210a5442013-01-24 23:48:23 +0100883 ctxt_sta->is_assoc = cpu_to_le32(1);
Johannes Bergd2931bb2013-02-05 18:10:04 +0100884 } else {
Johannes Berg210a5442013-01-24 23:48:23 +0100885 ctxt_sta->is_assoc = cpu_to_le32(0);
Luciano Coelho7c8b3bc2014-07-30 15:04:08 +0300886
887 /* Allow beacons to pass through as long as we are not
888 * associated, or we do not have dtim period information.
889 */
890 cmd.filter_flags |= cpu_to_le32(MAC_FILTER_IN_BEACON);
Johannes Bergd2931bb2013-02-05 18:10:04 +0100891 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100892
893 ctxt_sta->bi = cpu_to_le32(vif->bss_conf.beacon_int);
894 ctxt_sta->bi_reciprocal =
895 cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int));
896 ctxt_sta->dtim_interval = cpu_to_le32(vif->bss_conf.beacon_int *
897 vif->bss_conf.dtim_period);
898 ctxt_sta->dtim_reciprocal =
899 cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int *
900 vif->bss_conf.dtim_period));
901
902 ctxt_sta->listen_interval = cpu_to_le32(mvm->hw->conf.listen_interval);
903 ctxt_sta->assoc_id = cpu_to_le32(vif->bss_conf.aid);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100904
Andrei Otcheretianskieffd1922015-06-30 12:08:28 +0300905 if (vif->probe_req_reg && vif->bss_conf.assoc && vif->p2p)
906 cmd.filter_flags |= cpu_to_le32(MAC_FILTER_IN_PROBE_REQUEST);
907
Johannes Berg8ca151b2013-01-24 14:25:36 +0100908 return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
909}
910
911static int iwl_mvm_mac_ctxt_cmd_listener(struct iwl_mvm *mvm,
912 struct ieee80211_vif *vif,
913 u32 action)
914{
915 struct iwl_mac_ctx_cmd cmd = {};
Chaya Rachel Ivgi0e39eb02015-12-03 15:51:46 +0200916 u32 tfd_queue_msk = 0;
917 int ret, i;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100918
919 WARN_ON(vif->type != NL80211_IFTYPE_MONITOR);
920
Johannes Berg3dfd3a92014-08-11 21:37:30 +0200921 iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
Johannes Berg53585492013-03-18 13:04:50 +0100922
Liad Kaufman737847a2016-09-27 15:17:29 +0300923 if (!iwl_mvm_is_dqa_supported(mvm)) {
924 for (i = 0; i < IEEE80211_NUM_ACS; i++)
925 if (vif->hw_queue[i] != IEEE80211_INVAL_HW_QUEUE)
926 tfd_queue_msk |= BIT(vif->hw_queue[i]);
927 }
Chaya Rachel Ivgi0e39eb02015-12-03 15:51:46 +0200928
Johannes Berg53585492013-03-18 13:04:50 +0100929 cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_PROMISC |
930 MAC_FILTER_IN_CONTROL_AND_MGMT |
931 MAC_FILTER_IN_BEACON |
Johannes Bergfb8b8ee2013-10-21 12:37:53 +0200932 MAC_FILTER_IN_PROBE_REQUEST |
933 MAC_FILTER_IN_CRC32);
Johannes Berg30686bf2015-06-02 21:39:54 +0200934 ieee80211_hw_set(mvm->hw, RX_INCLUDES_FCS);
Johannes Berg53585492013-03-18 13:04:50 +0100935
Chaya Rachel Ivgi0e39eb02015-12-03 15:51:46 +0200936 /* Allocate sniffer station */
937 ret = iwl_mvm_allocate_int_sta(mvm, &mvm->snif_sta, tfd_queue_msk,
Sara Sharonced19f22017-02-06 19:09:32 +0200938 vif->type, IWL_STA_GENERAL_PURPOSE);
Chaya Rachel Ivgi0e39eb02015-12-03 15:51:46 +0200939 if (ret)
940 return ret;
941
Johannes Berg8ca151b2013-01-24 14:25:36 +0100942 return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
943}
944
Johannes Berg5023d962013-07-31 14:07:43 +0200945static int iwl_mvm_mac_ctxt_cmd_ibss(struct iwl_mvm *mvm,
946 struct ieee80211_vif *vif,
947 u32 action)
948{
949 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
950 struct iwl_mac_ctx_cmd cmd = {};
951
952 WARN_ON(vif->type != NL80211_IFTYPE_ADHOC);
953
Johannes Berg3dfd3a92014-08-11 21:37:30 +0200954 iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
Johannes Berg5023d962013-07-31 14:07:43 +0200955
956 cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_BEACON |
957 MAC_FILTER_IN_PROBE_REQUEST);
958
959 /* cmd.ibss.beacon_time/cmd.ibss.beacon_tsf are curently ignored */
960 cmd.ibss.bi = cpu_to_le32(vif->bss_conf.beacon_int);
961 cmd.ibss.bi_reciprocal =
962 cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int));
963
964 /* TODO: Assumes that the beacon id == mac context id */
965 cmd.ibss.beacon_template = cpu_to_le32(mvmvif->id);
966
967 return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
968}
969
Johannes Berg8ca151b2013-01-24 14:25:36 +0100970struct iwl_mvm_go_iterator_data {
971 bool go_active;
972};
973
974static void iwl_mvm_go_iterator(void *_data, u8 *mac, struct ieee80211_vif *vif)
975{
976 struct iwl_mvm_go_iterator_data *data = _data;
977 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
978
Johannes Berg5023d962013-07-31 14:07:43 +0200979 if (vif->type == NL80211_IFTYPE_AP && vif->p2p &&
980 mvmvif->ap_ibss_active)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100981 data->go_active = true;
982}
983
984static int iwl_mvm_mac_ctxt_cmd_p2p_device(struct iwl_mvm *mvm,
985 struct ieee80211_vif *vif,
986 u32 action)
987{
988 struct iwl_mac_ctx_cmd cmd = {};
989 struct iwl_mvm_go_iterator_data data = {};
990
991 WARN_ON(vif->type != NL80211_IFTYPE_P2P_DEVICE);
992
Johannes Berg3dfd3a92014-08-11 21:37:30 +0200993 iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100994
995 cmd.protection_flags |= cpu_to_le32(MAC_PROT_FLG_TGG_PROTECT);
Ilan Peer1dcd15e2013-02-14 14:25:24 +0200996
997 /* Override the filter flags to accept only probe requests */
998 cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_PROBE_REQUEST);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100999
1000 /*
1001 * This flag should be set to true when the P2P Device is
1002 * discoverable and there is at least another active P2P GO. Settings
1003 * this flag will allow the P2P Device to be discoverable on other
1004 * channels in addition to its listen channel.
1005 * Note that this flag should not be set in other cases as it opens the
1006 * Rx filters on all MAC and increases the number of interrupts.
1007 */
1008 ieee80211_iterate_active_interfaces_atomic(
1009 mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
1010 iwl_mvm_go_iterator, &data);
1011
1012 cmd.p2p_dev.is_disc_extended = cpu_to_le32(data.go_active ? 1 : 0);
1013 return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
1014}
1015
1016static void iwl_mvm_mac_ctxt_set_tim(struct iwl_mvm *mvm,
Sara Sharon8364fbb2016-12-08 11:44:20 +02001017 __le32 *tim_index, __le32 *tim_size,
Johannes Berg8ca151b2013-01-24 14:25:36 +01001018 u8 *beacon, u32 frame_size)
1019{
1020 u32 tim_idx;
1021 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)beacon;
1022
1023 /* The index is relative to frame start but we start looking at the
1024 * variable-length part of the beacon. */
1025 tim_idx = mgmt->u.beacon.variable - beacon;
1026
1027 /* Parse variable-length elements of beacon to find WLAN_EID_TIM */
1028 while ((tim_idx < (frame_size - 2)) &&
1029 (beacon[tim_idx] != WLAN_EID_TIM))
1030 tim_idx += beacon[tim_idx+1] + 2;
1031
1032 /* If TIM field was found, set variables */
1033 if ((tim_idx < (frame_size - 1)) && (beacon[tim_idx] == WLAN_EID_TIM)) {
Sara Sharon8364fbb2016-12-08 11:44:20 +02001034 *tim_index = cpu_to_le32(tim_idx);
1035 *tim_size = cpu_to_le32((u32)beacon[tim_idx + 1]);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001036 } else {
1037 IWL_WARN(mvm, "Unable to find TIM Element in beacon\n");
1038 }
1039}
1040
Andrei Otcheretianskid3a108a2016-02-28 17:12:21 +02001041static u32 iwl_mvm_find_ie_offset(u8 *beacon, u8 eid, u32 frame_size)
1042{
1043 struct ieee80211_mgmt *mgmt = (void *)beacon;
1044 const u8 *ie;
1045
1046 if (WARN_ON_ONCE(frame_size <= (mgmt->u.beacon.variable - beacon)))
1047 return 0;
1048
1049 frame_size -= mgmt->u.beacon.variable - beacon;
1050
1051 ie = cfg80211_find_ie(eid, mgmt->u.beacon.variable, frame_size);
1052 if (!ie)
1053 return 0;
1054
1055 return ie - beacon;
1056}
1057
Luca Coelho09856582017-06-01 15:19:45 +03001058static void iwl_mvm_mac_ctxt_set_tx(struct iwl_mvm *mvm,
1059 struct ieee80211_vif *vif,
1060 struct sk_buff *beacon,
1061 struct iwl_tx_cmd *tx)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001062{
1063 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001064 struct ieee80211_tx_info *info;
Emmanuel Grumbach75f6b9b2014-06-02 10:03:33 +03001065 u32 rate, tx_flags;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001066
Emmanuel Grumbach75f6b9b2014-06-02 10:03:33 +03001067 info = IEEE80211_SKB_CB(beacon);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001068
1069 /* Set up TX command fields */
Luca Coelho09856582017-06-01 15:19:45 +03001070 tx->len = cpu_to_le16((u16)beacon->len);
1071 tx->sta_id = mvmvif->bcast_sta.sta_id;
1072 tx->life_time = cpu_to_le32(TX_CMD_LIFE_TIME_INFINITE);
Emmanuel Grumbach75f6b9b2014-06-02 10:03:33 +03001073 tx_flags = TX_CMD_FLG_SEQ_CTL | TX_CMD_FLG_TSF;
1074 tx_flags |=
1075 iwl_mvm_bt_coex_tx_prio(mvm, (void *)beacon->data, info, 0) <<
1076 TX_CMD_FLG_BT_PRIO_POS;
Luca Coelho09856582017-06-01 15:19:45 +03001077 tx->tx_flags = cpu_to_le32(tx_flags);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001078
Emmanuel Grumbach1e3c3c32015-12-13 09:35:30 +02001079 if (!fw_has_capa(&mvm->fw->ucode_capa,
1080 IWL_UCODE_TLV_CAPA_BEACON_ANT_SELECTION)) {
1081 mvm->mgmt_last_antenna_idx =
1082 iwl_mvm_next_antenna(mvm, iwl_mvm_get_valid_tx_ant(mvm),
1083 mvm->mgmt_last_antenna_idx);
1084 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01001085
Luca Coelho09856582017-06-01 15:19:45 +03001086 tx->rate_n_flags =
Johannes Berg8ca151b2013-01-24 14:25:36 +01001087 cpu_to_le32(BIT(mvm->mgmt_last_antenna_idx) <<
1088 RATE_MCS_ANT_POS);
1089
Johannes Berg57fbcce2016-04-12 15:56:15 +02001090 if (info->band == NL80211_BAND_5GHZ || vif->p2p) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001091 rate = IWL_FIRST_OFDM_RATE;
1092 } else {
1093 rate = IWL_FIRST_CCK_RATE;
Luca Coelho09856582017-06-01 15:19:45 +03001094 tx->rate_n_flags |= cpu_to_le32(RATE_MCS_CCK_MSK);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001095 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01001096
Luca Coelho09856582017-06-01 15:19:45 +03001097 tx->rate_n_flags |= cpu_to_le32(iwl_mvm_mac80211_idx_to_hwrate(rate));
1098}
Johannes Berg8ca151b2013-01-24 14:25:36 +01001099
Luca Coelho09856582017-06-01 15:19:45 +03001100static int iwl_mvm_mac_ctxt_send_beacon_cmd(struct iwl_mvm *mvm,
1101 struct sk_buff *beacon,
1102 void *data, int len)
1103{
1104 struct iwl_host_cmd cmd = {
1105 .id = BEACON_TEMPLATE_CMD,
1106 .flags = CMD_ASYNC,
1107 };
1108
1109 cmd.len[0] = len;
1110 cmd.data[0] = data;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001111 cmd.dataflags[0] = 0;
Luca Coelho09856582017-06-01 15:19:45 +03001112 cmd.len[1] = beacon->len;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001113 cmd.data[1] = beacon->data;
1114 cmd.dataflags[1] = IWL_HCMD_DFL_DUP;
1115
1116 return iwl_mvm_send_cmd(mvm, &cmd);
1117}
1118
Luca Coelho09856582017-06-01 15:19:45 +03001119static int iwl_mvm_mac_ctxt_send_beacon_v6(struct iwl_mvm *mvm,
1120 struct ieee80211_vif *vif,
1121 struct sk_buff *beacon)
1122{
1123 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1124 struct iwl_mac_beacon_cmd_v6 beacon_cmd = {};
1125
1126 iwl_mvm_mac_ctxt_set_tx(mvm, vif, beacon, &beacon_cmd.tx);
1127
1128 beacon_cmd.template_id = cpu_to_le32((u32)mvmvif->id);
1129
1130 if (vif->type == NL80211_IFTYPE_AP)
1131 iwl_mvm_mac_ctxt_set_tim(mvm, &beacon_cmd.tim_idx,
1132 &beacon_cmd.tim_size,
1133 beacon->data, beacon->len);
1134
1135 return iwl_mvm_mac_ctxt_send_beacon_cmd(mvm, beacon, &beacon_cmd,
1136 sizeof(beacon_cmd));
1137}
1138
1139static int iwl_mvm_mac_ctxt_send_beacon_v7(struct iwl_mvm *mvm,
1140 struct ieee80211_vif *vif,
1141 struct sk_buff *beacon)
1142{
1143 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1144 struct iwl_mac_beacon_cmd_v7 beacon_cmd = {};
1145
1146 iwl_mvm_mac_ctxt_set_tx(mvm, vif, beacon, &beacon_cmd.tx);
1147
1148 beacon_cmd.template_id = cpu_to_le32((u32)mvmvif->id);
1149
1150 if (vif->type == NL80211_IFTYPE_AP)
1151 iwl_mvm_mac_ctxt_set_tim(mvm, &beacon_cmd.tim_idx,
1152 &beacon_cmd.tim_size,
1153 beacon->data, beacon->len);
1154
1155 beacon_cmd.csa_offset =
1156 cpu_to_le32(iwl_mvm_find_ie_offset(beacon->data,
1157 WLAN_EID_CHANNEL_SWITCH,
1158 beacon->len));
1159 beacon_cmd.ecsa_offset =
1160 cpu_to_le32(iwl_mvm_find_ie_offset(beacon->data,
1161 WLAN_EID_EXT_CHANSWITCH_ANN,
1162 beacon->len));
1163
1164 return iwl_mvm_mac_ctxt_send_beacon_cmd(mvm, beacon, &beacon_cmd,
1165 sizeof(beacon_cmd));
1166}
1167
1168static int iwl_mvm_mac_ctxt_send_beacon_v8(struct iwl_mvm *mvm,
1169 struct ieee80211_vif *vif,
1170 struct sk_buff *beacon)
1171{
1172 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1173 struct iwl_mac_beacon_cmd beacon_cmd = {};
1174
1175 beacon_cmd.byte_cnt = cpu_to_le16((u16)beacon->len);
1176 beacon_cmd.template_id = cpu_to_le32((u32)mvmvif->id);
1177
1178 if (vif->type == NL80211_IFTYPE_AP)
1179 iwl_mvm_mac_ctxt_set_tim(mvm,
1180 &beacon_cmd.tim_idx,
1181 &beacon_cmd.tim_size,
1182 beacon->data, beacon->len);
1183
1184 beacon_cmd.csa_offset =
1185 cpu_to_le32(iwl_mvm_find_ie_offset(beacon->data,
1186 WLAN_EID_CHANNEL_SWITCH,
1187 beacon->len));
1188 beacon_cmd.ecsa_offset =
1189 cpu_to_le32(iwl_mvm_find_ie_offset(beacon->data,
1190 WLAN_EID_EXT_CHANSWITCH_ANN,
1191 beacon->len));
1192
1193 return iwl_mvm_mac_ctxt_send_beacon_cmd(mvm, beacon, &beacon_cmd,
1194 sizeof(beacon_cmd));
1195}
1196
1197static int iwl_mvm_mac_ctxt_send_beacon(struct iwl_mvm *mvm,
1198 struct ieee80211_vif *vif,
1199 struct sk_buff *beacon)
1200{
1201 if (WARN_ON(!beacon))
1202 return -EINVAL;
1203
1204 if (!fw_has_capa(&mvm->fw->ucode_capa,
1205 IWL_UCODE_TLV_CAPA_CSA_AND_TBTT_OFFLOAD))
1206 return iwl_mvm_mac_ctxt_send_beacon_v6(mvm, vif, beacon);
1207
1208 if (!iwl_mvm_has_new_tx_api(mvm))
1209 return iwl_mvm_mac_ctxt_send_beacon_v7(mvm, vif, beacon);
1210
1211 return iwl_mvm_mac_ctxt_send_beacon_v8(mvm, vif, beacon);
1212}
1213
Johannes Berg5023d962013-07-31 14:07:43 +02001214/* The beacon template for the AP/GO/IBSS has changed and needs update */
Johannes Berg8ca151b2013-01-24 14:25:36 +01001215int iwl_mvm_mac_ctxt_beacon_changed(struct iwl_mvm *mvm,
1216 struct ieee80211_vif *vif)
1217{
1218 struct sk_buff *beacon;
1219 int ret;
1220
Johannes Berg5023d962013-07-31 14:07:43 +02001221 WARN_ON(vif->type != NL80211_IFTYPE_AP &&
1222 vif->type != NL80211_IFTYPE_ADHOC);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001223
Andrei Otcheretianskife887662014-04-10 21:46:16 +03001224 beacon = ieee80211_beacon_get_template(mvm->hw, vif, NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001225 if (!beacon)
1226 return -ENOMEM;
1227
1228 ret = iwl_mvm_mac_ctxt_send_beacon(mvm, vif, beacon);
1229 dev_kfree_skb(beacon);
1230 return ret;
1231}
1232
Ilan Peer3a3cb922013-06-13 08:30:12 +03001233struct iwl_mvm_mac_ap_iterator_data {
1234 struct iwl_mvm *mvm;
1235 struct ieee80211_vif *vif;
1236 u32 beacon_device_ts;
1237 u16 beacon_int;
1238};
1239
1240/* Find the beacon_device_ts and beacon_int for a managed interface */
1241static void iwl_mvm_mac_ap_iterator(void *_data, u8 *mac,
1242 struct ieee80211_vif *vif)
1243{
1244 struct iwl_mvm_mac_ap_iterator_data *data = _data;
1245
1246 if (vif->type != NL80211_IFTYPE_STATION || !vif->bss_conf.assoc)
1247 return;
1248
1249 /* Station client has higher priority over P2P client*/
1250 if (vif->p2p && data->beacon_device_ts)
1251 return;
1252
1253 data->beacon_device_ts = vif->bss_conf.sync_device_ts;
1254 data->beacon_int = vif->bss_conf.beacon_int;
1255}
1256
Johannes Berg8ca151b2013-01-24 14:25:36 +01001257/*
1258 * Fill the specific data for mac context of type AP of P2P GO
1259 */
1260static void iwl_mvm_mac_ctxt_cmd_fill_ap(struct iwl_mvm *mvm,
1261 struct ieee80211_vif *vif,
Johannes Berg186cd492016-08-24 10:05:40 +02001262 struct iwl_mac_ctx_cmd *cmd,
Johannes Berg506a81e2013-02-28 14:05:14 +01001263 struct iwl_mac_data_ap *ctxt_ap,
1264 bool add)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001265{
1266 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Ilan Peer3a3cb922013-06-13 08:30:12 +03001267 struct iwl_mvm_mac_ap_iterator_data data = {
1268 .mvm = mvm,
1269 .vif = vif,
1270 .beacon_device_ts = 0
1271 };
Johannes Berg8ca151b2013-01-24 14:25:36 +01001272
Johannes Berg186cd492016-08-24 10:05:40 +02001273 /* in AP mode, the MCAST FIFO takes the EDCA params from VO */
1274 cmd->ac[IWL_MVM_TX_FIFO_VO].fifos_mask |= BIT(IWL_MVM_TX_FIFO_MCAST);
1275
1276 /*
1277 * in AP mode, pass probe requests and beacons from other APs
1278 * (needed for ht protection); when there're no any associated
1279 * station don't ask FW to pass beacons to prevent unnecessary
1280 * wake-ups.
1281 */
1282 cmd->filter_flags |= cpu_to_le32(MAC_FILTER_IN_PROBE_REQUEST);
1283 if (mvmvif->ap_assoc_sta_count || !mvm->drop_bcn_ap_mode) {
1284 cmd->filter_flags |= cpu_to_le32(MAC_FILTER_IN_BEACON);
1285 IWL_DEBUG_HC(mvm, "Asking FW to pass beacons\n");
1286 } else {
1287 IWL_DEBUG_HC(mvm, "No need to receive beacons\n");
1288 }
1289
Johannes Berg8ca151b2013-01-24 14:25:36 +01001290 ctxt_ap->bi = cpu_to_le32(vif->bss_conf.beacon_int);
1291 ctxt_ap->bi_reciprocal =
1292 cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int));
1293 ctxt_ap->dtim_interval = cpu_to_le32(vif->bss_conf.beacon_int *
1294 vif->bss_conf.dtim_period);
1295 ctxt_ap->dtim_reciprocal =
1296 cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int *
1297 vif->bss_conf.dtim_period));
1298
Sara Sharonced19f22017-02-06 19:09:32 +02001299 if (!fw_has_api(&mvm->fw->ucode_capa,
1300 IWL_UCODE_TLV_API_STA_TYPE))
1301 ctxt_ap->mcast_qid = cpu_to_le32(vif->cab_queue);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001302
Johannes Berg506a81e2013-02-28 14:05:14 +01001303 /*
Ilan Peer3a3cb922013-06-13 08:30:12 +03001304 * Only set the beacon time when the MAC is being added, when we
Johannes Berg506a81e2013-02-28 14:05:14 +01001305 * just modify the MAC then we should keep the time -- the firmware
1306 * can otherwise have a "jumping" TBTT.
1307 */
Ilan Peer3a3cb922013-06-13 08:30:12 +03001308 if (add) {
1309 /*
1310 * If there is a station/P2P client interface which is
1311 * associated, set the AP's TBTT far enough from the station's
1312 * TBTT. Otherwise, set it to the current system time
1313 */
1314 ieee80211_iterate_active_interfaces_atomic(
1315 mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
1316 iwl_mvm_mac_ap_iterator, &data);
1317
1318 if (data.beacon_device_ts) {
Ilan Peer9f8f8ca2013-12-24 17:17:05 +02001319 u32 rand = (prandom_u32() % (64 - 36)) + 36;
Ilan Peer3a3cb922013-06-13 08:30:12 +03001320 mvmvif->ap_beacon_time = data.beacon_device_ts +
1321 ieee80211_tu_to_usec(data.beacon_int * rand /
1322 100);
1323 } else {
1324 mvmvif->ap_beacon_time =
1325 iwl_read_prph(mvm->trans,
1326 DEVICE_SYSTEM_TIME_REG);
1327 }
1328 }
Johannes Berg506a81e2013-02-28 14:05:14 +01001329
1330 ctxt_ap->beacon_time = cpu_to_le32(mvmvif->ap_beacon_time);
Johannes Berg506a81e2013-02-28 14:05:14 +01001331 ctxt_ap->beacon_tsf = 0; /* unused */
Johannes Berg8ca151b2013-01-24 14:25:36 +01001332
1333 /* TODO: Assume that the beacon id == mac context id */
1334 ctxt_ap->beacon_template = cpu_to_le32(mvmvif->id);
1335}
1336
Ilan Peerf82c8332015-09-10 12:54:38 +03001337static int iwl_mvm_mac_ctxt_cmd_ap(struct iwl_mvm *mvm,
1338 struct ieee80211_vif *vif,
1339 u32 action)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001340{
1341 struct iwl_mac_ctx_cmd cmd = {};
1342
1343 WARN_ON(vif->type != NL80211_IFTYPE_AP || vif->p2p);
1344
1345 /* Fill the common data for all mac context types */
Johannes Berg3dfd3a92014-08-11 21:37:30 +02001346 iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001347
Johannes Berg8ca151b2013-01-24 14:25:36 +01001348 /* Fill the data specific for ap mode */
Johannes Berg186cd492016-08-24 10:05:40 +02001349 iwl_mvm_mac_ctxt_cmd_fill_ap(mvm, vif, &cmd, &cmd.ap,
Johannes Berg506a81e2013-02-28 14:05:14 +01001350 action == FW_CTXT_ACTION_ADD);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001351
1352 return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
1353}
1354
1355static int iwl_mvm_mac_ctxt_cmd_go(struct iwl_mvm *mvm,
1356 struct ieee80211_vif *vif,
1357 u32 action)
1358{
1359 struct iwl_mac_ctx_cmd cmd = {};
Janusz Dziedzic67baf662013-03-21 15:47:56 +01001360 struct ieee80211_p2p_noa_attr *noa = &vif->bss_conf.p2p_noa_attr;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001361
1362 WARN_ON(vif->type != NL80211_IFTYPE_AP || !vif->p2p);
1363
1364 /* Fill the common data for all mac context types */
Johannes Berg3dfd3a92014-08-11 21:37:30 +02001365 iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001366
1367 /* Fill the data specific for GO mode */
Johannes Berg186cd492016-08-24 10:05:40 +02001368 iwl_mvm_mac_ctxt_cmd_fill_ap(mvm, vif, &cmd, &cmd.go.ap,
Johannes Berg506a81e2013-02-28 14:05:14 +01001369 action == FW_CTXT_ACTION_ADD);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001370
Janusz Dziedzic67baf662013-03-21 15:47:56 +01001371 cmd.go.ctwin = cpu_to_le32(noa->oppps_ctwindow &
1372 IEEE80211_P2P_OPPPS_CTWINDOW_MASK);
1373 cmd.go.opp_ps_enabled =
1374 cpu_to_le32(!!(noa->oppps_ctwindow &
1375 IEEE80211_P2P_OPPPS_ENABLE_BIT));
Johannes Berg8ca151b2013-01-24 14:25:36 +01001376
1377 return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
1378}
1379
1380static int iwl_mvm_mac_ctx_send(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
Johannes Berg3dfd3a92014-08-11 21:37:30 +02001381 u32 action, bool force_assoc_off,
1382 const u8 *bssid_override)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001383{
1384 switch (vif->type) {
1385 case NL80211_IFTYPE_STATION:
Luciano Coelhocf520232014-05-13 16:52:54 +03001386 return iwl_mvm_mac_ctxt_cmd_sta(mvm, vif, action,
Johannes Berg3dfd3a92014-08-11 21:37:30 +02001387 force_assoc_off,
1388 bssid_override);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001389 break;
1390 case NL80211_IFTYPE_AP:
1391 if (!vif->p2p)
1392 return iwl_mvm_mac_ctxt_cmd_ap(mvm, vif, action);
1393 else
1394 return iwl_mvm_mac_ctxt_cmd_go(mvm, vif, action);
1395 break;
1396 case NL80211_IFTYPE_MONITOR:
1397 return iwl_mvm_mac_ctxt_cmd_listener(mvm, vif, action);
1398 case NL80211_IFTYPE_P2P_DEVICE:
1399 return iwl_mvm_mac_ctxt_cmd_p2p_device(mvm, vif, action);
Johannes Berg5023d962013-07-31 14:07:43 +02001400 case NL80211_IFTYPE_ADHOC:
1401 return iwl_mvm_mac_ctxt_cmd_ibss(mvm, vif, action);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001402 default:
1403 break;
1404 }
1405
1406 return -EOPNOTSUPP;
1407}
1408
1409int iwl_mvm_mac_ctxt_add(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1410{
1411 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1412 int ret;
1413
1414 if (WARN_ONCE(mvmvif->uploaded, "Adding active MAC %pM/%d\n",
1415 vif->addr, ieee80211_vif_type_p2p(vif)))
1416 return -EIO;
1417
Luciano Coelhobca49d92014-05-13 17:33:38 +03001418 ret = iwl_mvm_mac_ctx_send(mvm, vif, FW_CTXT_ACTION_ADD,
Johannes Berg3dfd3a92014-08-11 21:37:30 +02001419 true, NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001420 if (ret)
1421 return ret;
1422
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001423 /* will only do anything at resume from D3 time */
1424 iwl_mvm_set_last_nonqos_seq(mvm, vif);
1425
Johannes Berg8ca151b2013-01-24 14:25:36 +01001426 mvmvif->uploaded = true;
1427 return 0;
1428}
1429
Luciano Coelhobca49d92014-05-13 17:33:38 +03001430int iwl_mvm_mac_ctxt_changed(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
Johannes Berg3dfd3a92014-08-11 21:37:30 +02001431 bool force_assoc_off, const u8 *bssid_override)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001432{
1433 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1434
1435 if (WARN_ONCE(!mvmvif->uploaded, "Changing inactive MAC %pM/%d\n",
1436 vif->addr, ieee80211_vif_type_p2p(vif)))
1437 return -EIO;
1438
Luciano Coelhobca49d92014-05-13 17:33:38 +03001439 return iwl_mvm_mac_ctx_send(mvm, vif, FW_CTXT_ACTION_MODIFY,
Johannes Berg3dfd3a92014-08-11 21:37:30 +02001440 force_assoc_off, bssid_override);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001441}
1442
1443int iwl_mvm_mac_ctxt_remove(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1444{
1445 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1446 struct iwl_mac_ctx_cmd cmd;
1447 int ret;
1448
1449 if (WARN_ONCE(!mvmvif->uploaded, "Removing inactive MAC %pM/%d\n",
1450 vif->addr, ieee80211_vif_type_p2p(vif)))
1451 return -EIO;
1452
1453 memset(&cmd, 0, sizeof(cmd));
1454
1455 cmd.id_and_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
1456 mvmvif->color));
1457 cmd.action = cpu_to_le32(FW_CTXT_ACTION_REMOVE);
1458
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03001459 ret = iwl_mvm_send_cmd_pdu(mvm, MAC_CONTEXT_CMD, 0,
Johannes Berg8ca151b2013-01-24 14:25:36 +01001460 sizeof(cmd), &cmd);
1461 if (ret) {
1462 IWL_ERR(mvm, "Failed to remove MAC context: %d\n", ret);
1463 return ret;
1464 }
1465
1466 mvmvif->uploaded = false;
Johannes Bergfb8b8ee2013-10-21 12:37:53 +02001467
Chaya Rachel Ivgi0e39eb02015-12-03 15:51:46 +02001468 if (vif->type == NL80211_IFTYPE_MONITOR) {
Johannes Berg30686bf2015-06-02 21:39:54 +02001469 __clear_bit(IEEE80211_HW_RX_INCLUDES_FCS, mvm->hw->flags);
Chaya Rachel Ivgi0e39eb02015-12-03 15:51:46 +02001470 iwl_mvm_dealloc_snif_sta(mvm);
1471 }
Johannes Bergfb8b8ee2013-10-21 12:37:53 +02001472
Johannes Berg8ca151b2013-01-24 14:25:36 +01001473 return 0;
1474}
Ilan Peer571765c2013-03-05 15:26:03 +02001475
Andrei Otcheretianski7f0a7c62014-05-04 11:48:12 +03001476static void iwl_mvm_csa_count_down(struct iwl_mvm *mvm,
Andrei Otcheretianski7ef0aab2014-11-10 11:10:11 +02001477 struct ieee80211_vif *csa_vif, u32 gp2,
1478 bool tx_success)
Andrei Otcheretianski7f0a7c62014-05-04 11:48:12 +03001479{
1480 struct iwl_mvm_vif *mvmvif =
1481 iwl_mvm_vif_from_mac80211(csa_vif);
1482
Andrei Otcheretianski7ef0aab2014-11-10 11:10:11 +02001483 /* Don't start to countdown from a failed beacon */
1484 if (!tx_success && !mvmvif->csa_countdown)
1485 return;
1486
1487 mvmvif->csa_countdown = true;
1488
Andrei Otcheretianski7f0a7c62014-05-04 11:48:12 +03001489 if (!ieee80211_csa_is_complete(csa_vif)) {
1490 int c = ieee80211_csa_update_counter(csa_vif);
1491
1492 iwl_mvm_mac_ctxt_beacon_changed(mvm, csa_vif);
1493 if (csa_vif->p2p &&
Andrei Otcheretianski7ef0aab2014-11-10 11:10:11 +02001494 !iwl_mvm_te_scheduled(&mvmvif->time_event_data) && gp2 &&
1495 tx_success) {
Andrei Otcheretianski7f0a7c62014-05-04 11:48:12 +03001496 u32 rel_time = (c + 1) *
1497 csa_vif->bss_conf.beacon_int -
Luciano Coelhof991e172014-08-26 16:14:10 +03001498 IWL_MVM_CHANNEL_SWITCH_TIME_GO;
Andrei Otcheretianski7f0a7c62014-05-04 11:48:12 +03001499 u32 apply_time = gp2 + rel_time * 1024;
1500
Luciano Coelhof991e172014-08-26 16:14:10 +03001501 iwl_mvm_schedule_csa_period(mvm, csa_vif,
1502 IWL_MVM_CHANNEL_SWITCH_TIME_GO -
1503 IWL_MVM_CHANNEL_SWITCH_MARGIN,
1504 apply_time);
Andrei Otcheretianski7f0a7c62014-05-04 11:48:12 +03001505 }
1506 } else if (!iwl_mvm_te_scheduled(&mvmvif->time_event_data)) {
1507 /* we don't have CSA NoA scheduled yet, switch now */
1508 ieee80211_csa_finish(csa_vif);
1509 RCU_INIT_POINTER(mvm->csa_vif, NULL);
1510 }
1511}
1512
Johannes Berg04168412015-06-23 21:22:09 +02001513void iwl_mvm_rx_beacon_notif(struct iwl_mvm *mvm,
1514 struct iwl_rx_cmd_buffer *rxb)
Ilan Peer571765c2013-03-05 15:26:03 +02001515{
1516 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Emmanuel Grumbach2d2c0e92014-11-24 08:51:46 +02001517 struct iwl_extended_beacon_notif *beacon = (void *)pkt->data;
David Spinadel1c87bba2014-02-27 16:41:52 +02001518 struct iwl_mvm_tx_resp *beacon_notify_hdr;
Andrei Otcheretianski664322f2014-06-05 16:40:36 +03001519 struct ieee80211_vif *csa_vif;
Andrei Otcheretianski003e52362014-05-25 17:24:22 +03001520 struct ieee80211_vif *tx_blocked_vif;
Sara Sharon12db2942017-01-17 14:28:21 +02001521 struct agg_tx_status *agg_status;
Andrei Otcheretianski7ef0aab2014-11-10 11:10:11 +02001522 u16 status;
Ilan Peer571765c2013-03-05 15:26:03 +02001523
Andrei Otcheretianskibd3398e2013-10-22 05:01:12 +02001524 lockdep_assert_held(&mvm->mutex);
1525
Emmanuel Grumbach2d2c0e92014-11-24 08:51:46 +02001526 beacon_notify_hdr = &beacon->beacon_notify_hdr;
1527 mvm->ap_last_beacon_gp2 = le32_to_cpu(beacon->gp2);
Johannes Berg2f0282d2017-04-20 10:29:03 +02001528 mvm->ibss_manager = beacon->ibss_mgr_status != 0;
David Spinadel1c87bba2014-02-27 16:41:52 +02001529
Sara Sharon12db2942017-01-17 14:28:21 +02001530 agg_status = iwl_mvm_get_agg_status(mvm, beacon_notify_hdr);
1531 status = le16_to_cpu(agg_status->status) & TX_STATUS_MSK;
David Spinadel1c87bba2014-02-27 16:41:52 +02001532 IWL_DEBUG_RX(mvm,
1533 "beacon status %#x retries:%d tsf:0x%16llX gp2:0x%X rate:%d\n",
Emmanuel Grumbach2d2c0e92014-11-24 08:51:46 +02001534 status, beacon_notify_hdr->failure_frame,
1535 le64_to_cpu(beacon->tsf),
David Spinadel1c87bba2014-02-27 16:41:52 +02001536 mvm->ap_last_beacon_gp2,
1537 le32_to_cpu(beacon_notify_hdr->initial_rate));
Andrei Otcheretianskibd3398e2013-10-22 05:01:12 +02001538
Andrei Otcheretianski664322f2014-06-05 16:40:36 +03001539 csa_vif = rcu_dereference_protected(mvm->csa_vif,
1540 lockdep_is_held(&mvm->mutex));
Andrei Otcheretianski7f0a7c62014-05-04 11:48:12 +03001541 if (unlikely(csa_vif && csa_vif->csa_active))
Andrei Otcheretianski7ef0aab2014-11-10 11:10:11 +02001542 iwl_mvm_csa_count_down(mvm, csa_vif, mvm->ap_last_beacon_gp2,
1543 (status == TX_STATUS_SUCCESS));
Andrei Otcheretianskibd3398e2013-10-22 05:01:12 +02001544
Andrei Otcheretianski003e52362014-05-25 17:24:22 +03001545 tx_blocked_vif = rcu_dereference_protected(mvm->csa_tx_blocked_vif,
1546 lockdep_is_held(&mvm->mutex));
1547 if (unlikely(tx_blocked_vif)) {
1548 struct iwl_mvm_vif *mvmvif =
1549 iwl_mvm_vif_from_mac80211(tx_blocked_vif);
1550
1551 /*
1552 * The channel switch is started and we have blocked the
1553 * stations. If this is the first beacon (the timeout wasn't
1554 * set), set the unblock timeout, otherwise countdown
1555 */
1556 if (!mvm->csa_tx_block_bcn_timeout)
1557 mvm->csa_tx_block_bcn_timeout =
1558 IWL_MVM_CS_UNBLOCK_TX_TIMEOUT;
1559 else
1560 mvm->csa_tx_block_bcn_timeout--;
1561
1562 /* Check if the timeout is expired, and unblock tx */
1563 if (mvm->csa_tx_block_bcn_timeout == 0) {
1564 iwl_mvm_modify_all_sta_disable_tx(mvm, mvmvif, false);
1565 RCU_INIT_POINTER(mvm->csa_tx_blocked_vif, NULL);
1566 }
1567 }
Ilan Peer571765c2013-03-05 15:26:03 +02001568}
Hila Gonend64048e2013-03-13 18:00:03 +02001569
1570static void iwl_mvm_beacon_loss_iterator(void *_data, u8 *mac,
1571 struct ieee80211_vif *vif)
1572{
Ilan Peer12d423e2013-12-24 22:08:14 +02001573 struct iwl_missed_beacons_notif *missed_beacons = _data;
Hila Gonend64048e2013-03-13 18:00:03 +02001574 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Emmanuel Grumbach9d761fd2015-02-02 12:44:23 +02001575 struct iwl_mvm *mvm = mvmvif->mvm;
1576 struct iwl_fw_dbg_trigger_missed_bcon *bcon_trig;
1577 struct iwl_fw_dbg_trigger_tlv *trigger;
1578 u32 stop_trig_missed_bcon, stop_trig_missed_bcon_since_rx;
1579 u32 rx_missed_bcon, rx_missed_bcon_since_rx;
Hila Gonend64048e2013-03-13 18:00:03 +02001580
Ilan Peer12d423e2013-12-24 22:08:14 +02001581 if (mvmvif->id != (u16)le32_to_cpu(missed_beacons->mac_id))
1582 return;
1583
Emmanuel Grumbach9d761fd2015-02-02 12:44:23 +02001584 rx_missed_bcon = le32_to_cpu(missed_beacons->consec_missed_beacons);
1585 rx_missed_bcon_since_rx =
1586 le32_to_cpu(missed_beacons->consec_missed_beacons_since_last_rx);
Ilan Peer12d423e2013-12-24 22:08:14 +02001587 /*
1588 * TODO: the threshold should be adjusted based on latency conditions,
1589 * and/or in case of a CS flow on one of the other AP vifs.
1590 */
1591 if (le32_to_cpu(missed_beacons->consec_missed_beacons_since_last_rx) >
1592 IWL_MVM_MISSED_BEACONS_THRESHOLD)
Hila Gonend64048e2013-03-13 18:00:03 +02001593 ieee80211_beacon_loss(vif);
Emmanuel Grumbach9d761fd2015-02-02 12:44:23 +02001594
1595 if (!iwl_fw_dbg_trigger_enabled(mvm->fw,
1596 FW_DBG_TRIGGER_MISSED_BEACONS))
1597 return;
1598
1599 trigger = iwl_fw_dbg_get_trigger(mvm->fw,
1600 FW_DBG_TRIGGER_MISSED_BEACONS);
1601 bcon_trig = (void *)trigger->data;
1602 stop_trig_missed_bcon = le32_to_cpu(bcon_trig->stop_consec_missed_bcon);
1603 stop_trig_missed_bcon_since_rx =
1604 le32_to_cpu(bcon_trig->stop_consec_missed_bcon_since_rx);
1605
1606 /* TODO: implement start trigger */
1607
Johannes Berg7174beb2017-06-01 16:03:19 +02001608 if (!iwl_fw_dbg_trigger_check_stop(&mvm->fwrt,
1609 ieee80211_vif_to_wdev(vif),
1610 trigger))
Emmanuel Grumbach9d761fd2015-02-02 12:44:23 +02001611 return;
1612
1613 if (rx_missed_bcon_since_rx >= stop_trig_missed_bcon_since_rx ||
1614 rx_missed_bcon >= stop_trig_missed_bcon)
Johannes Berg7174beb2017-06-01 16:03:19 +02001615 iwl_fw_dbg_collect_trig(&mvm->fwrt, trigger, NULL);
Hila Gonend64048e2013-03-13 18:00:03 +02001616}
1617
Johannes Berg04168412015-06-23 21:22:09 +02001618void iwl_mvm_rx_missed_beacons_notif(struct iwl_mvm *mvm,
1619 struct iwl_rx_cmd_buffer *rxb)
Hila Gonend64048e2013-03-13 18:00:03 +02001620{
1621 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Ilan Peer12d423e2013-12-24 22:08:14 +02001622 struct iwl_missed_beacons_notif *mb = (void *)pkt->data;
1623
1624 IWL_DEBUG_INFO(mvm,
1625 "missed bcn mac_id=%u, consecutive=%u (%u, %u, %u)\n",
1626 le32_to_cpu(mb->mac_id),
1627 le32_to_cpu(mb->consec_missed_beacons),
1628 le32_to_cpu(mb->consec_missed_beacons_since_last_rx),
1629 le32_to_cpu(mb->num_recvd_beacons),
1630 le32_to_cpu(mb->num_expected_beacons));
Hila Gonend64048e2013-03-13 18:00:03 +02001631
1632 ieee80211_iterate_active_interfaces_atomic(mvm->hw,
1633 IEEE80211_IFACE_ITER_NORMAL,
1634 iwl_mvm_beacon_loss_iterator,
Ilan Peer12d423e2013-12-24 22:08:14 +02001635 mb);
Hila Gonend64048e2013-03-13 18:00:03 +02001636}
Sara Sharon0db056d2015-12-29 11:07:15 +02001637
1638void iwl_mvm_rx_stored_beacon_notif(struct iwl_mvm *mvm,
1639 struct iwl_rx_cmd_buffer *rxb)
1640{
1641 struct iwl_rx_packet *pkt = rxb_addr(rxb);
1642 struct iwl_stored_beacon_notif *sb = (void *)pkt->data;
1643 struct ieee80211_rx_status rx_status;
1644 struct sk_buff *skb;
1645 u32 size = le32_to_cpu(sb->byte_count);
1646
1647 if (size == 0)
1648 return;
1649
1650 skb = alloc_skb(size, GFP_ATOMIC);
1651 if (!skb) {
1652 IWL_ERR(mvm, "alloc_skb failed\n");
1653 return;
1654 }
1655
1656 /* update rx_status according to the notification's metadata */
1657 memset(&rx_status, 0, sizeof(rx_status));
1658 rx_status.mactime = le64_to_cpu(sb->tsf);
Sara Sharon77fe7392016-01-10 14:23:25 +02001659 /* TSF as indicated by the firmware is at INA time */
1660 rx_status.flag |= RX_FLAG_MACTIME_PLCP_START;
Sara Sharon0db056d2015-12-29 11:07:15 +02001661 rx_status.device_timestamp = le32_to_cpu(sb->system_time);
1662 rx_status.band =
Sara Sharon34118c22016-11-08 18:34:42 +02001663 (sb->band & cpu_to_le16(RX_RES_PHY_FLAGS_BAND_24)) ?
Johannes Berg57fbcce2016-04-12 15:56:15 +02001664 NL80211_BAND_2GHZ : NL80211_BAND_5GHZ;
Sara Sharon0db056d2015-12-29 11:07:15 +02001665 rx_status.freq =
1666 ieee80211_channel_to_frequency(le16_to_cpu(sb->channel),
1667 rx_status.band);
1668
1669 /* copy the data */
Johannes Berg59ae1d12017-06-16 14:29:20 +02001670 skb_put_data(skb, sb->data, size);
Sara Sharon0db056d2015-12-29 11:07:15 +02001671 memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
1672
1673 /* pass it as regular rx to mac80211 */
Johannes Bergd63b5482016-03-31 20:02:02 +03001674 ieee80211_rx_napi(mvm->hw, NULL, skb, NULL);
Sara Sharon0db056d2015-12-29 11:07:15 +02001675}
Andrei Otcheretianskid3a108a2016-02-28 17:12:21 +02001676
1677void iwl_mvm_channel_switch_noa_notif(struct iwl_mvm *mvm,
1678 struct iwl_rx_cmd_buffer *rxb)
1679{
1680 struct iwl_rx_packet *pkt = rxb_addr(rxb);
1681 struct iwl_channel_switch_noa_notif *notif = (void *)pkt->data;
1682 struct ieee80211_vif *csa_vif;
1683 struct iwl_mvm_vif *mvmvif;
1684 int len = iwl_rx_packet_payload_len(pkt);
1685 u32 id_n_color;
1686
1687 if (WARN_ON_ONCE(len < sizeof(*notif)))
1688 return;
1689
1690 rcu_read_lock();
1691
1692 csa_vif = rcu_dereference(mvm->csa_vif);
1693 if (WARN_ON(!csa_vif || !csa_vif->csa_active))
1694 goto out_unlock;
1695
1696 id_n_color = le32_to_cpu(notif->id_and_color);
1697
1698 mvmvif = iwl_mvm_vif_from_mac80211(csa_vif);
1699 if (WARN(FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color) != id_n_color,
1700 "channel switch noa notification on unexpected vif (csa_vif=%d, notif=%d)",
1701 FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color), id_n_color))
1702 goto out_unlock;
1703
1704 IWL_DEBUG_INFO(mvm, "Channel Switch Started Notification\n");
1705
Johannes Berg86bbb1e2017-03-22 21:44:59 +01001706 schedule_delayed_work(&mvm->cs_tx_unblock_dwork,
1707 msecs_to_jiffies(IWL_MVM_CS_UNBLOCK_TX_TIMEOUT *
1708 csa_vif->bss_conf.beacon_int));
Andrei Otcheretianskid3a108a2016-02-28 17:12:21 +02001709
1710 ieee80211_csa_finish(csa_vif);
1711
1712 rcu_read_unlock();
1713
1714 RCU_INIT_POINTER(mvm->csa_vif, NULL);
1715
1716 return;
1717
1718out_unlock:
1719 rcu_read_unlock();
1720}