blob: 1546d54e2ebd8d41c30ab547fce210611726a883 [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
83struct iwl_mvm_mac_iface_iterator_data {
84 struct iwl_mvm *mvm;
85 struct ieee80211_vif *vif;
86 unsigned long available_mac_ids[BITS_TO_LONGS(NUM_MAC_INDEX_DRIVER)];
87 unsigned long available_tsf_ids[BITS_TO_LONGS(NUM_TSF_IDS)];
Johannes Berg8ca151b2013-01-24 14:25:36 +010088 enum iwl_tsf_id preferred_tsf;
89 bool found_vif;
90};
91
Arik Nemtsova74346d2014-09-21 19:07:30 +030092struct iwl_mvm_hw_queues_iface_iterator_data {
93 struct ieee80211_vif *exclude_vif;
94 unsigned long used_hw_queues;
95};
96
Ilan Peer6e97b0d2013-12-23 22:18:02 +020097static void iwl_mvm_mac_tsf_id_iter(void *_data, u8 *mac,
98 struct ieee80211_vif *vif)
Johannes Berg8ca151b2013-01-24 14:25:36 +010099{
100 struct iwl_mvm_mac_iface_iterator_data *data = _data;
101 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Ilan Peer2c3e62a2014-02-02 21:54:35 +0200102 u16 min_bi;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100103
Ilan Peer6e97b0d2013-12-23 22:18:02 +0200104 /* Skip the interface for which we are trying to assign a tsf_id */
105 if (vif == data->vif)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100106 return;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100107
108 /*
109 * The TSF is a hardware/firmware resource, there are 4 and
110 * the driver should assign and free them as needed. However,
111 * there are cases where 2 MACs should share the same TSF ID
112 * for the purpose of clock sync, an optimization to avoid
113 * clock drift causing overlapping TBTTs/DTIMs for a GO and
114 * client in the system.
115 *
116 * The firmware will decide according to the MAC type which
117 * will be the master and slave. Clients that need to sync
118 * with a remote station will be the master, and an AP or GO
119 * will be the slave.
120 *
121 * Depending on the new interface type it can be slaved to
122 * or become the master of an existing interface.
123 */
124 switch (data->vif->type) {
125 case NL80211_IFTYPE_STATION:
126 /*
Ilan Peer2c3e62a2014-02-02 21:54:35 +0200127 * The new interface is a client, so if the one we're iterating
128 * is an AP, and the beacon interval of the AP is a multiple or
129 * divisor of the beacon interval of the client, the same TSF
130 * should be used to avoid drift between the new client and
131 * existing AP. The existing AP will get drift updates from the
132 * new client context in this case.
Johannes Berg8ca151b2013-01-24 14:25:36 +0100133 */
Ilan Peer2c3e62a2014-02-02 21:54:35 +0200134 if (vif->type != NL80211_IFTYPE_AP ||
135 data->preferred_tsf != NUM_TSF_IDS ||
136 !test_bit(mvmvif->tsf_id, data->available_tsf_ids))
137 break;
138
139 min_bi = min(data->vif->bss_conf.beacon_int,
140 vif->bss_conf.beacon_int);
141
142 if (!min_bi)
143 break;
144
145 if ((data->vif->bss_conf.beacon_int -
146 vif->bss_conf.beacon_int) % min_bi == 0) {
147 data->preferred_tsf = mvmvif->tsf_id;
148 return;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100149 }
150 break;
Ilan Peer2c3e62a2014-02-02 21:54:35 +0200151
Johannes Berg8ca151b2013-01-24 14:25:36 +0100152 case NL80211_IFTYPE_AP:
153 /*
Ilan Peer2c3e62a2014-02-02 21:54:35 +0200154 * The new interface is AP/GO, so if its beacon interval is a
155 * multiple or a divisor of the beacon interval of an existing
156 * interface, it should get drift updates from an existing
157 * client or use the same TSF as an existing GO. There's no
158 * drift between TSFs internally but if they used different
159 * TSFs then a new client MAC could update one of them and
160 * cause drift that way.
Johannes Berg8ca151b2013-01-24 14:25:36 +0100161 */
Ilan Peer2c3e62a2014-02-02 21:54:35 +0200162 if ((vif->type != NL80211_IFTYPE_AP &&
163 vif->type != NL80211_IFTYPE_STATION) ||
164 data->preferred_tsf != NUM_TSF_IDS ||
165 !test_bit(mvmvif->tsf_id, data->available_tsf_ids))
166 break;
167
168 min_bi = min(data->vif->bss_conf.beacon_int,
169 vif->bss_conf.beacon_int);
170
171 if (!min_bi)
172 break;
173
174 if ((data->vif->bss_conf.beacon_int -
175 vif->bss_conf.beacon_int) % min_bi == 0) {
176 data->preferred_tsf = mvmvif->tsf_id;
177 return;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100178 }
179 break;
180 default:
181 /*
182 * For all other interface types there's no need to
183 * take drift into account. Either they're exclusive
184 * like IBSS and monitor, or we don't care much about
185 * their TSF (like P2P Device), but we won't be able
186 * to share the TSF resource.
187 */
188 break;
189 }
190
191 /*
192 * Unless we exited above, we can't share the TSF resource
193 * that the virtual interface we're iterating over is using
194 * with the new one, so clear the available bit and if this
195 * was the preferred one, reset that as well.
196 */
197 __clear_bit(mvmvif->tsf_id, data->available_tsf_ids);
198
199 if (data->preferred_tsf == mvmvif->tsf_id)
200 data->preferred_tsf = NUM_TSF_IDS;
201}
202
Johannes Berg110cf812014-08-01 23:14:24 +0200203/*
204 * Get the mask of the queues used by the vif
205 */
Arik Nemtsovd92b732e2014-09-21 19:00:42 +0300206u32 iwl_mvm_mac_get_queues_mask(struct ieee80211_vif *vif)
Johannes Berg110cf812014-08-01 23:14:24 +0200207{
208 u32 qmask = 0, ac;
209
210 if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
211 return BIT(IWL_MVM_OFFCHANNEL_QUEUE);
212
Ilan Peer1008e442015-01-17 22:35:00 -0500213 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
214 if (vif->hw_queue[ac] != IEEE80211_INVAL_HW_QUEUE)
215 qmask |= BIT(vif->hw_queue[ac]);
216 }
Johannes Berg110cf812014-08-01 23:14:24 +0200217
Liad Kaufman4d339982017-03-21 17:13:16 +0200218 if (vif->type == NL80211_IFTYPE_AP ||
219 vif->type == NL80211_IFTYPE_ADHOC)
Johannes Berg110cf812014-08-01 23:14:24 +0200220 qmask |= BIT(vif->cab_queue);
221
222 return qmask;
223}
224
Arik Nemtsova74346d2014-09-21 19:07:30 +0300225static void iwl_mvm_iface_hw_queues_iter(void *_data, u8 *mac,
226 struct ieee80211_vif *vif)
227{
228 struct iwl_mvm_hw_queues_iface_iterator_data *data = _data;
229
230 /* exclude the given vif */
231 if (vif == data->exclude_vif)
232 return;
233
234 data->used_hw_queues |= iwl_mvm_mac_get_queues_mask(vif);
235}
236
237static void iwl_mvm_mac_sta_hw_queues_iter(void *_data,
238 struct ieee80211_sta *sta)
239{
240 struct iwl_mvm_hw_queues_iface_iterator_data *data = _data;
241 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
242
243 /* Mark the queues used by the sta */
244 data->used_hw_queues |= mvmsta->tfd_queue_msk;
245}
246
247unsigned long iwl_mvm_get_used_hw_queues(struct iwl_mvm *mvm,
248 struct ieee80211_vif *exclude_vif)
249{
Arik Nemtsov51ec09a2015-01-21 11:50:31 +0200250 u8 sta_id;
Arik Nemtsova74346d2014-09-21 19:07:30 +0300251 struct iwl_mvm_hw_queues_iface_iterator_data data = {
252 .exclude_vif = exclude_vif,
253 .used_hw_queues =
254 BIT(IWL_MVM_OFFCHANNEL_QUEUE) |
Liad Kaufman097129c2015-08-09 18:28:43 +0300255 BIT(mvm->aux_queue),
Arik Nemtsova74346d2014-09-21 19:07:30 +0300256 };
257
Liad Kaufman097129c2015-08-09 18:28:43 +0300258 if (iwl_mvm_is_dqa_supported(mvm))
Johannes Berg32026e82017-06-19 23:26:55 +0200259 data.used_hw_queues |= BIT(IWL_MVM_DQA_GCAST_QUEUE);
Liad Kaufman097129c2015-08-09 18:28:43 +0300260 else
261 data.used_hw_queues |= BIT(IWL_MVM_CMD_QUEUE);
262
Arik Nemtsova74346d2014-09-21 19:07:30 +0300263 lockdep_assert_held(&mvm->mutex);
264
265 /* mark all VIF used hw queues */
266 ieee80211_iterate_active_interfaces_atomic(
267 mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
268 iwl_mvm_iface_hw_queues_iter, &data);
269
Johannes Berg32026e82017-06-19 23:26:55 +0200270 /*
271 * for DQA, the hw_queue in mac80211 is never really used for
272 * real traffic (only the few queue IDs covered above), so
273 * we can reuse the real HW queue IDs the stations use
274 */
275 if (iwl_mvm_is_dqa_supported(mvm))
276 return data.used_hw_queues;
277
Arik Nemtsova74346d2014-09-21 19:07:30 +0300278 /* don't assign the same hw queues as TDLS stations */
279 ieee80211_iterate_stations_atomic(mvm->hw,
280 iwl_mvm_mac_sta_hw_queues_iter,
281 &data);
282
Arik Nemtsov51ec09a2015-01-21 11:50:31 +0200283 /*
284 * Some TDLS stations may be removed but are in the process of being
285 * drained. Don't touch their queues.
286 */
287 for_each_set_bit(sta_id, mvm->sta_drained, IWL_MVM_STATION_COUNT)
288 data.used_hw_queues |= mvm->tfd_drained[sta_id];
289
Arik Nemtsova74346d2014-09-21 19:07:30 +0300290 return data.used_hw_queues;
291}
292
Ilan Peer6e97b0d2013-12-23 22:18:02 +0200293static void iwl_mvm_mac_iface_iterator(void *_data, u8 *mac,
294 struct ieee80211_vif *vif)
295{
296 struct iwl_mvm_mac_iface_iterator_data *data = _data;
297 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Ilan Peer6e97b0d2013-12-23 22:18:02 +0200298
299 /* Iterator may already find the interface being added -- skip it */
300 if (vif == data->vif) {
301 data->found_vif = true;
302 return;
303 }
304
Ilan Peer6e97b0d2013-12-23 22:18:02 +0200305 /* Mark MAC IDs as used by clearing the available bit, and
306 * (below) mark TSFs as used if their existing use is not
307 * compatible with the new interface type.
308 * No locking or atomic bit operations are needed since the
309 * data is on the stack of the caller function.
310 */
311 __clear_bit(mvmvif->id, data->available_mac_ids);
312
313 /* find a suitable tsf_id */
314 iwl_mvm_mac_tsf_id_iter(_data, mac, vif);
315}
316
Ilan Peer6e97b0d2013-12-23 22:18:02 +0200317void iwl_mvm_mac_ctxt_recalc_tsf_id(struct iwl_mvm *mvm,
318 struct ieee80211_vif *vif)
319{
320 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
321 struct iwl_mvm_mac_iface_iterator_data data = {
322 .mvm = mvm,
323 .vif = vif,
324 .available_tsf_ids = { (1 << NUM_TSF_IDS) - 1 },
325 /* no preference yet */
326 .preferred_tsf = NUM_TSF_IDS,
327 };
328
329 ieee80211_iterate_active_interfaces_atomic(
330 mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
331 iwl_mvm_mac_tsf_id_iter, &data);
332
333 if (data.preferred_tsf != NUM_TSF_IDS)
334 mvmvif->tsf_id = data.preferred_tsf;
335 else if (!test_bit(mvmvif->tsf_id, data.available_tsf_ids))
336 mvmvif->tsf_id = find_first_bit(data.available_tsf_ids,
337 NUM_TSF_IDS);
338}
339
Johannes Berg8ca151b2013-01-24 14:25:36 +0100340static int iwl_mvm_mac_ctxt_allocate_resources(struct iwl_mvm *mvm,
341 struct ieee80211_vif *vif)
342{
343 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
344 struct iwl_mvm_mac_iface_iterator_data data = {
345 .mvm = mvm,
346 .vif = vif,
347 .available_mac_ids = { (1 << NUM_MAC_INDEX_DRIVER) - 1 },
348 .available_tsf_ids = { (1 << NUM_TSF_IDS) - 1 },
349 /* no preference yet */
350 .preferred_tsf = NUM_TSF_IDS,
Johannes Berg8ca151b2013-01-24 14:25:36 +0100351 .found_vif = false,
352 };
353 u32 ac;
Johannes Berg32026e82017-06-19 23:26:55 +0200354 int ret, i, queue_limit;
Johannes Berg110cf812014-08-01 23:14:24 +0200355 unsigned long used_hw_queues;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100356
357 /*
358 * Allocate a MAC ID and a TSF for this MAC, along with the queues
359 * and other resources.
360 */
361
362 /*
363 * Before the iterator, we start with all MAC IDs and TSFs available.
364 *
365 * During iteration, all MAC IDs are cleared that are in use by other
366 * virtual interfaces, and all TSF IDs are cleared that can't be used
367 * by this new virtual interface because they're used by an interface
368 * that can't share it with the new one.
369 * At the same time, we check if there's a preferred TSF in the case
370 * that we should share it with another interface.
371 */
372
Johannes Berg5023d962013-07-31 14:07:43 +0200373 /* Currently, MAC ID 0 should be used only for the managed/IBSS vif */
374 switch (vif->type) {
375 case NL80211_IFTYPE_ADHOC:
376 break;
377 case NL80211_IFTYPE_STATION:
378 if (!vif->p2p)
379 break;
380 /* fall through */
381 default:
Ilan Peerec8b6882013-02-13 13:27:18 +0200382 __clear_bit(0, data.available_mac_ids);
Johannes Berg5023d962013-07-31 14:07:43 +0200383 }
Ilan Peerec8b6882013-02-13 13:27:18 +0200384
Johannes Berg8ca151b2013-01-24 14:25:36 +0100385 ieee80211_iterate_active_interfaces_atomic(
386 mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
387 iwl_mvm_mac_iface_iterator, &data);
388
Arik Nemtsova74346d2014-09-21 19:07:30 +0300389 used_hw_queues = iwl_mvm_get_used_hw_queues(mvm, vif);
390
Johannes Berg8ca151b2013-01-24 14:25:36 +0100391 /*
392 * In the case we're getting here during resume, it's similar to
393 * firmware restart, and with RESUME_ALL the iterator will find
394 * the vif being added already.
395 * We don't want to reassign any IDs in either case since doing
396 * so would probably assign different IDs (as interfaces aren't
397 * necessarily added in the same order), but the old IDs were
398 * preserved anyway, so skip ID assignment for both resume and
399 * recovery.
400 */
401 if (data.found_vif)
402 return 0;
403
404 /* Therefore, in recovery, we can't get here */
Ilan Peerfd11bd02013-06-16 12:18:11 +0300405 if (WARN_ON_ONCE(test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)))
406 return -EBUSY;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100407
408 mvmvif->id = find_first_bit(data.available_mac_ids,
409 NUM_MAC_INDEX_DRIVER);
410 if (mvmvif->id == NUM_MAC_INDEX_DRIVER) {
411 IWL_ERR(mvm, "Failed to init MAC context - no free ID!\n");
412 ret = -EIO;
413 goto exit_fail;
414 }
415
416 if (data.preferred_tsf != NUM_TSF_IDS)
417 mvmvif->tsf_id = data.preferred_tsf;
418 else
419 mvmvif->tsf_id = find_first_bit(data.available_tsf_ids,
420 NUM_TSF_IDS);
421 if (mvmvif->tsf_id == NUM_TSF_IDS) {
422 IWL_ERR(mvm, "Failed to init MAC context - no free TSF!\n");
423 ret = -EIO;
424 goto exit_fail;
425 }
426
427 mvmvif->color = 0;
428
Ilan Peer1e849c92013-02-13 12:26:39 +0200429 INIT_LIST_HEAD(&mvmvif->time_event_data.list);
430 mvmvif->time_event_data.id = TE_MAX;
431
Johannes Berg8ca151b2013-01-24 14:25:36 +0100432 /* No need to allocate data queues to P2P Device MAC.*/
433 if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
434 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
435 vif->hw_queue[ac] = IEEE80211_INVAL_HW_QUEUE;
436
437 return 0;
438 }
439
Johannes Berg32026e82017-06-19 23:26:55 +0200440 if (iwl_mvm_is_dqa_supported(mvm)) {
441 /*
442 * queues in mac80211 almost entirely independent of
443 * the ones here - no real limit
444 */
445 queue_limit = IEEE80211_MAX_QUEUES;
446 BUILD_BUG_ON(IEEE80211_MAX_QUEUES >
447 BITS_PER_BYTE *
448 sizeof(mvm->hw_queue_to_mac80211[0]));
449 } else {
450 /* need to not use too many in this case */
451 queue_limit = mvm->first_agg_queue;
452 }
453
Liad Kaufman24afba72015-07-28 18:56:08 +0300454 /*
455 * Find available queues, and allocate them to the ACs. When in
456 * DQA-mode they aren't really used, and this is done only so the
457 * mac80211 ieee80211_check_queues() function won't fail
458 */
Johannes Berg8ca151b2013-01-24 14:25:36 +0100459 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
Johannes Berg32026e82017-06-19 23:26:55 +0200460 u8 queue = find_first_zero_bit(&used_hw_queues, queue_limit);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100461
Johannes Berg32026e82017-06-19 23:26:55 +0200462 if (queue >= queue_limit) {
Johannes Berg8ca151b2013-01-24 14:25:36 +0100463 IWL_ERR(mvm, "Failed to allocate queue\n");
464 ret = -EIO;
465 goto exit_fail;
466 }
467
Johannes Berg110cf812014-08-01 23:14:24 +0200468 __set_bit(queue, &used_hw_queues);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100469 vif->hw_queue[ac] = queue;
470 }
471
472 /* Allocate the CAB queue for softAP and GO interfaces */
473 if (vif->type == NL80211_IFTYPE_AP) {
Liad Kaufman0e0e4422015-08-04 15:13:38 +0300474 u8 queue;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100475
Liad Kaufman0e0e4422015-08-04 15:13:38 +0300476 if (!iwl_mvm_is_dqa_supported(mvm)) {
477 queue = find_first_zero_bit(&used_hw_queues,
478 mvm->first_agg_queue);
479
480 if (queue >= mvm->first_agg_queue) {
481 IWL_ERR(mvm, "Failed to allocate cab queue\n");
482 ret = -EIO;
483 goto exit_fail;
484 }
485 } else {
486 queue = IWL_MVM_DQA_GCAST_QUEUE;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100487 }
488
Sara Sharone2af3fa2017-02-22 19:35:10 +0200489 /*
490 * For TVQM this will be overwritten later with the FW assigned
491 * queue value (when queue is enabled).
492 */
493 mvmvif->cab_queue = queue;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100494 vif->cab_queue = queue;
495 } else {
496 vif->cab_queue = IEEE80211_INVAL_HW_QUEUE;
497 }
498
Sara Sharon0ae98812017-01-04 14:53:58 +0200499 mvmvif->bcast_sta.sta_id = IWL_MVM_INVALID_STA;
500 mvmvif->mcast_sta.sta_id = IWL_MVM_INVALID_STA;
501 mvmvif->ap_sta_id = IWL_MVM_INVALID_STA;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100502
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +0300503 for (i = 0; i < NUM_IWL_MVM_SMPS_REQ; i++)
504 mvmvif->smps_requests[i] = IEEE80211_SMPS_AUTOMATIC;
505
Johannes Berg8ca151b2013-01-24 14:25:36 +0100506 return 0;
507
508exit_fail:
509 memset(mvmvif, 0, sizeof(struct iwl_mvm_vif));
510 memset(vif->hw_queue, IEEE80211_INVAL_HW_QUEUE, sizeof(vif->hw_queue));
511 vif->cab_queue = IEEE80211_INVAL_HW_QUEUE;
512 return ret;
513}
514
515int iwl_mvm_mac_ctxt_init(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
516{
Emmanuel Grumbach5d42e7b2015-03-19 20:04:51 +0200517 unsigned int wdg_timeout =
518 iwl_mvm_get_wd_timeout(mvm, vif, false, false);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100519 u32 ac;
520 int ret;
521
522 lockdep_assert_held(&mvm->mutex);
523
524 ret = iwl_mvm_mac_ctxt_allocate_resources(mvm, vif);
525 if (ret)
526 return ret;
527
Sara Sharon8236f7d2016-10-06 16:42:32 +0300528 /* If DQA is supported - queues will be enabled when needed */
529 if (iwl_mvm_is_dqa_supported(mvm))
530 return 0;
531
Johannes Berg8ca151b2013-01-24 14:25:36 +0100532 switch (vif->type) {
533 case NL80211_IFTYPE_P2P_DEVICE:
Sara Sharon8236f7d2016-10-06 16:42:32 +0300534 iwl_mvm_enable_ac_txq(mvm, IWL_MVM_OFFCHANNEL_QUEUE,
535 IWL_MVM_OFFCHANNEL_QUEUE,
536 IWL_MVM_TX_FIFO_VO, 0, wdg_timeout);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100537 break;
538 case NL80211_IFTYPE_AP:
Liad Kaufman4ecafae2015-07-14 13:36:18 +0300539 iwl_mvm_enable_ac_txq(mvm, vif->cab_queue, vif->cab_queue,
Liad Kaufman5c1156e2015-07-22 17:59:53 +0300540 IWL_MVM_TX_FIFO_MCAST, 0, wdg_timeout);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100541 /* fall through */
542 default:
543 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
Avri Altman3edf8ff2014-07-30 11:41:01 +0300544 iwl_mvm_enable_ac_txq(mvm, vif->hw_queue[ac],
Liad Kaufman4ecafae2015-07-14 13:36:18 +0300545 vif->hw_queue[ac],
Liad Kaufman5c1156e2015-07-22 17:59:53 +0300546 iwl_mvm_ac_to_tx_fifo[ac], 0,
Emmanuel Grumbach4cf677f2015-01-12 14:38:29 +0200547 wdg_timeout);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100548 break;
549 }
550
551 return 0;
552}
553
554void iwl_mvm_mac_ctxt_release(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
555{
556 int ac;
557
558 lockdep_assert_held(&mvm->mutex);
559
Liad Kaufmandf88c082016-11-24 15:31:00 +0200560 /*
561 * If DQA is supported - queues were already disabled, since in
562 * DQA-mode the queues are a property of the STA and not of the
563 * vif, and at this point the STA was already deleted
564 */
565 if (iwl_mvm_is_dqa_supported(mvm))
566 return;
567
Johannes Berg8ca151b2013-01-24 14:25:36 +0100568 switch (vif->type) {
569 case NL80211_IFTYPE_P2P_DEVICE:
Liad Kaufmandf88c082016-11-24 15:31:00 +0200570 iwl_mvm_disable_txq(mvm, IWL_MVM_OFFCHANNEL_QUEUE,
571 IWL_MVM_OFFCHANNEL_QUEUE,
572 IWL_MAX_TID_COUNT, 0);
Liad Kaufman4c965132015-08-09 19:26:56 +0300573
Johannes Berg8ca151b2013-01-24 14:25:36 +0100574 break;
575 case NL80211_IFTYPE_AP:
Liad Kaufman4ecafae2015-07-14 13:36:18 +0300576 iwl_mvm_disable_txq(mvm, vif->cab_queue, vif->cab_queue,
577 IWL_MAX_TID_COUNT, 0);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100578 /* fall through */
579 default:
580 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
Liad Kaufman4ecafae2015-07-14 13:36:18 +0300581 iwl_mvm_disable_txq(mvm, vif->hw_queue[ac],
582 vif->hw_queue[ac],
583 IWL_MAX_TID_COUNT, 0);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100584 }
585}
586
587static void iwl_mvm_ack_rates(struct iwl_mvm *mvm,
588 struct ieee80211_vif *vif,
Johannes Berg57fbcce2016-04-12 15:56:15 +0200589 enum nl80211_band band,
Johannes Berg8ca151b2013-01-24 14:25:36 +0100590 u8 *cck_rates, u8 *ofdm_rates)
591{
592 struct ieee80211_supported_band *sband;
593 unsigned long basic = vif->bss_conf.basic_rates;
594 int lowest_present_ofdm = 100;
595 int lowest_present_cck = 100;
596 u8 cck = 0;
597 u8 ofdm = 0;
598 int i;
599
600 sband = mvm->hw->wiphy->bands[band];
601
602 for_each_set_bit(i, &basic, BITS_PER_LONG) {
603 int hw = sband->bitrates[i].hw_value;
604 if (hw >= IWL_FIRST_OFDM_RATE) {
605 ofdm |= BIT(hw - IWL_FIRST_OFDM_RATE);
606 if (lowest_present_ofdm > hw)
607 lowest_present_ofdm = hw;
608 } else {
609 BUILD_BUG_ON(IWL_FIRST_CCK_RATE != 0);
610
611 cck |= BIT(hw);
612 if (lowest_present_cck > hw)
613 lowest_present_cck = hw;
614 }
615 }
616
617 /*
618 * Now we've got the basic rates as bitmaps in the ofdm and cck
619 * variables. This isn't sufficient though, as there might not
620 * be all the right rates in the bitmap. E.g. if the only basic
621 * rates are 5.5 Mbps and 11 Mbps, we still need to add 1 Mbps
622 * and 6 Mbps because the 802.11-2007 standard says in 9.6:
623 *
624 * [...] a STA responding to a received frame shall transmit
625 * its Control Response frame [...] at the highest rate in the
626 * BSSBasicRateSet parameter that is less than or equal to the
627 * rate of the immediately previous frame in the frame exchange
628 * sequence ([...]) and that is of the same modulation class
629 * ([...]) as the received frame. If no rate contained in the
630 * BSSBasicRateSet parameter meets these conditions, then the
631 * control frame sent in response to a received frame shall be
632 * transmitted at the highest mandatory rate of the PHY that is
633 * less than or equal to the rate of the received frame, and
634 * that is of the same modulation class as the received frame.
635 *
636 * As a consequence, we need to add all mandatory rates that are
637 * lower than all of the basic rates to these bitmaps.
638 */
639
640 if (IWL_RATE_24M_INDEX < lowest_present_ofdm)
641 ofdm |= IWL_RATE_BIT_MSK(24) >> IWL_FIRST_OFDM_RATE;
642 if (IWL_RATE_12M_INDEX < lowest_present_ofdm)
643 ofdm |= IWL_RATE_BIT_MSK(12) >> IWL_FIRST_OFDM_RATE;
644 /* 6M already there or needed so always add */
645 ofdm |= IWL_RATE_BIT_MSK(6) >> IWL_FIRST_OFDM_RATE;
646
647 /*
648 * CCK is a bit more complex with DSSS vs. HR/DSSS vs. ERP.
649 * Note, however:
650 * - if no CCK rates are basic, it must be ERP since there must
651 * be some basic rates at all, so they're OFDM => ERP PHY
652 * (or we're in 5 GHz, and the cck bitmap will never be used)
653 * - if 11M is a basic rate, it must be ERP as well, so add 5.5M
654 * - if 5.5M is basic, 1M and 2M are mandatory
655 * - if 2M is basic, 1M is mandatory
656 * - if 1M is basic, that's the only valid ACK rate.
657 * As a consequence, it's not as complicated as it sounds, just add
658 * any lower rates to the ACK rate bitmap.
659 */
660 if (IWL_RATE_11M_INDEX < lowest_present_cck)
661 cck |= IWL_RATE_BIT_MSK(11) >> IWL_FIRST_CCK_RATE;
662 if (IWL_RATE_5M_INDEX < lowest_present_cck)
663 cck |= IWL_RATE_BIT_MSK(5) >> IWL_FIRST_CCK_RATE;
664 if (IWL_RATE_2M_INDEX < lowest_present_cck)
665 cck |= IWL_RATE_BIT_MSK(2) >> IWL_FIRST_CCK_RATE;
666 /* 1M already there or needed so always add */
667 cck |= IWL_RATE_BIT_MSK(1) >> IWL_FIRST_CCK_RATE;
668
669 *cck_rates = cck;
670 *ofdm_rates = ofdm;
671}
672
Avri Altman8a5e3662013-11-12 19:16:03 +0200673static void iwl_mvm_mac_ctxt_set_ht_flags(struct iwl_mvm *mvm,
674 struct ieee80211_vif *vif,
675 struct iwl_mac_ctx_cmd *cmd)
676{
677 /* for both sta and ap, ht_operation_mode hold the protection_mode */
678 u8 protection_mode = vif->bss_conf.ht_operation_mode &
679 IEEE80211_HT_OP_MODE_PROTECTION;
680 /* The fw does not distinguish between ht and fat */
681 u32 ht_flag = MAC_PROT_FLG_HT_PROT | MAC_PROT_FLG_FAT_PROT;
682
683 IWL_DEBUG_RATE(mvm, "protection mode set to %d\n", protection_mode);
684 /*
685 * See section 9.23.3.1 of IEEE 80211-2012.
686 * Nongreenfield HT STAs Present is not supported.
687 */
688 switch (protection_mode) {
689 case IEEE80211_HT_OP_MODE_PROTECTION_NONE:
690 break;
691 case IEEE80211_HT_OP_MODE_PROTECTION_NONMEMBER:
692 case IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED:
693 cmd->protection_flags |= cpu_to_le32(ht_flag);
694 break;
695 case IEEE80211_HT_OP_MODE_PROTECTION_20MHZ:
696 /* Protect when channel wider than 20MHz */
697 if (vif->bss_conf.chandef.width > NL80211_CHAN_WIDTH_20)
698 cmd->protection_flags |= cpu_to_le32(ht_flag);
699 break;
700 default:
701 IWL_ERR(mvm, "Illegal protection mode %d\n",
702 protection_mode);
703 break;
704 }
705}
706
Johannes Berg8ca151b2013-01-24 14:25:36 +0100707static void iwl_mvm_mac_ctxt_cmd_common(struct iwl_mvm *mvm,
708 struct ieee80211_vif *vif,
709 struct iwl_mac_ctx_cmd *cmd,
Johannes Berg3dfd3a92014-08-11 21:37:30 +0200710 const u8 *bssid_override,
Johannes Berg8ca151b2013-01-24 14:25:36 +0100711 u32 action)
712{
713 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
714 struct ieee80211_chanctx_conf *chanctx;
Avri Altman8a5e3662013-11-12 19:16:03 +0200715 bool ht_enabled = !!(vif->bss_conf.ht_operation_mode &
716 IEEE80211_HT_OP_MODE_PROTECTION);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100717 u8 cck_ack_rates, ofdm_ack_rates;
Johannes Berg3dfd3a92014-08-11 21:37:30 +0200718 const u8 *bssid = bssid_override ?: vif->bss_conf.bssid;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100719 int i;
720
721 cmd->id_and_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
722 mvmvif->color));
723 cmd->action = cpu_to_le32(action);
724
725 switch (vif->type) {
726 case NL80211_IFTYPE_STATION:
727 if (vif->p2p)
728 cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_P2P_STA);
729 else
730 cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_BSS_STA);
731 break;
732 case NL80211_IFTYPE_AP:
733 cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_GO);
734 break;
735 case NL80211_IFTYPE_MONITOR:
736 cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_LISTENER);
737 break;
738 case NL80211_IFTYPE_P2P_DEVICE:
739 cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_P2P_DEVICE);
740 break;
741 case NL80211_IFTYPE_ADHOC:
742 cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_IBSS);
743 break;
744 default:
745 WARN_ON_ONCE(1);
746 }
747
748 cmd->tsf_id = cpu_to_le32(mvmvif->tsf_id);
749
750 memcpy(cmd->node_addr, vif->addr, ETH_ALEN);
Johannes Berg3dfd3a92014-08-11 21:37:30 +0200751
752 if (bssid)
753 memcpy(cmd->bssid_addr, bssid, ETH_ALEN);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100754 else
755 eth_broadcast_addr(cmd->bssid_addr);
756
757 rcu_read_lock();
758 chanctx = rcu_dereference(vif->chanctx_conf);
759 iwl_mvm_ack_rates(mvm, vif, chanctx ? chanctx->def.chan->band
Johannes Berg57fbcce2016-04-12 15:56:15 +0200760 : NL80211_BAND_2GHZ,
Johannes Berg8ca151b2013-01-24 14:25:36 +0100761 &cck_ack_rates, &ofdm_ack_rates);
762 rcu_read_unlock();
763
764 cmd->cck_rates = cpu_to_le32((u32)cck_ack_rates);
765 cmd->ofdm_rates = cpu_to_le32((u32)ofdm_ack_rates);
766
767 cmd->cck_short_preamble =
768 cpu_to_le32(vif->bss_conf.use_short_preamble ?
769 MAC_FLG_SHORT_PREAMBLE : 0);
770 cmd->short_slot =
771 cpu_to_le32(vif->bss_conf.use_short_slot ?
772 MAC_FLG_SHORT_SLOT : 0);
773
Ayala Bekerbd6f5bd2015-12-20 09:27:50 +0200774 cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP);
775
Emmanuel Grumbach3dd94792013-12-19 16:50:49 +0200776 for (i = 0; i < IEEE80211_NUM_ACS; i++) {
777 u8 txf = iwl_mvm_ac_to_tx_fifo[i];
778
779 cmd->ac[txf].cw_min =
780 cpu_to_le16(mvmvif->queue_params[i].cw_min);
781 cmd->ac[txf].cw_max =
782 cpu_to_le16(mvmvif->queue_params[i].cw_max);
783 cmd->ac[txf].edca_txop =
Johannes Berg8ca151b2013-01-24 14:25:36 +0100784 cpu_to_le16(mvmvif->queue_params[i].txop * 32);
Emmanuel Grumbach3dd94792013-12-19 16:50:49 +0200785 cmd->ac[txf].aifsn = mvmvif->queue_params[i].aifs;
786 cmd->ac[txf].fifos_mask = BIT(txf);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100787 }
788
789 if (vif->bss_conf.qos)
790 cmd->qos_flags |= cpu_to_le32(MAC_QOS_FLG_UPDATE_EDCA);
791
Emmanuel Grumbachdc271ee2014-07-03 20:46:35 +0300792 if (vif->bss_conf.use_cts_prot)
Dor Shaishcc7ee2b2013-02-18 13:51:36 +0200793 cmd->protection_flags |= cpu_to_le32(MAC_PROT_FLG_TGG_PROTECT);
Emmanuel Grumbachdc271ee2014-07-03 20:46:35 +0300794
Avri Altman8a5e3662013-11-12 19:16:03 +0200795 IWL_DEBUG_RATE(mvm, "use_cts_prot %d, ht_operation_mode %d\n",
796 vif->bss_conf.use_cts_prot,
797 vif->bss_conf.ht_operation_mode);
798 if (vif->bss_conf.chandef.width != NL80211_CHAN_WIDTH_20_NOHT)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100799 cmd->qos_flags |= cpu_to_le32(MAC_QOS_FLG_TGN);
Avri Altman8a5e3662013-11-12 19:16:03 +0200800 if (ht_enabled)
801 iwl_mvm_mac_ctxt_set_ht_flags(mvm, vif, cmd);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100802}
803
804static int iwl_mvm_mac_ctxt_send_cmd(struct iwl_mvm *mvm,
805 struct iwl_mac_ctx_cmd *cmd)
806{
Emmanuel Grumbacha1022922014-05-12 11:36:41 +0300807 int ret = iwl_mvm_send_cmd_pdu(mvm, MAC_CONTEXT_CMD, 0,
Johannes Berg8ca151b2013-01-24 14:25:36 +0100808 sizeof(*cmd), cmd);
809 if (ret)
810 IWL_ERR(mvm, "Failed to send MAC context (action:%d): %d\n",
811 le32_to_cpu(cmd->action), ret);
812 return ret;
813}
814
Luciano Coelhocf520232014-05-13 16:52:54 +0300815static int iwl_mvm_mac_ctxt_cmd_sta(struct iwl_mvm *mvm,
816 struct ieee80211_vif *vif,
Johannes Berg3dfd3a92014-08-11 21:37:30 +0200817 u32 action, bool force_assoc_off,
818 const u8 *bssid_override)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100819{
Luciano Coelhocf520232014-05-13 16:52:54 +0300820 struct iwl_mac_ctx_cmd cmd = {};
821 struct iwl_mac_data_sta *ctxt_sta;
822
823 WARN_ON(vif->type != NL80211_IFTYPE_STATION);
824
825 /* Fill the common data for all mac context types */
Johannes Berg3dfd3a92014-08-11 21:37:30 +0200826 iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, bssid_override, action);
Luciano Coelhocf520232014-05-13 16:52:54 +0300827
828 if (vif->p2p) {
829 struct ieee80211_p2p_noa_attr *noa =
830 &vif->bss_conf.p2p_noa_attr;
831
832 cmd.p2p_sta.ctwin = cpu_to_le32(noa->oppps_ctwindow &
833 IEEE80211_P2P_OPPPS_CTWINDOW_MASK);
834 ctxt_sta = &cmd.p2p_sta.sta;
835 } else {
Luciano Coelhocf520232014-05-13 16:52:54 +0300836 ctxt_sta = &cmd.sta;
837 }
838
Johannes Berg210a5442013-01-24 23:48:23 +0100839 /* We need the dtim_period to set the MAC as associated */
Alexander Bondarba283922013-05-02 16:34:48 +0300840 if (vif->bss_conf.assoc && vif->bss_conf.dtim_period &&
841 !force_assoc_off) {
Johannes Bergd2931bb2013-02-05 18:10:04 +0100842 u32 dtim_offs;
843
844 /*
845 * The DTIM count counts down, so when it is N that means N
846 * more beacon intervals happen until the DTIM TBTT. Therefore
847 * add this to the current time. If that ends up being in the
848 * future, the firmware will handle it.
849 *
850 * Also note that the system_timestamp (which we get here as
851 * "sync_device_ts") and TSF timestamp aren't at exactly the
852 * same offset in the frame -- the TSF is at the first symbol
853 * of the TSF, the system timestamp is at signal acquisition
854 * time. This means there's an offset between them of at most
855 * a few hundred microseconds (24 * 8 bits + PLCP time gives
856 * 384us in the longest case), this is currently not relevant
857 * as the firmware wakes up around 2ms before the TBTT.
858 */
859 dtim_offs = vif->bss_conf.sync_dtim_count *
860 vif->bss_conf.beacon_int;
861 /* convert TU to usecs */
862 dtim_offs *= 1024;
863
864 ctxt_sta->dtim_tsf =
865 cpu_to_le64(vif->bss_conf.sync_tsf + dtim_offs);
866 ctxt_sta->dtim_time =
867 cpu_to_le32(vif->bss_conf.sync_device_ts + dtim_offs);
Avraham Sternb5669722017-05-09 16:40:01 +0300868 ctxt_sta->assoc_beacon_arrive_time =
869 cpu_to_le32(vif->bss_conf.sync_device_ts);
Johannes Bergd2931bb2013-02-05 18:10:04 +0100870
871 IWL_DEBUG_INFO(mvm, "DTIM TBTT is 0x%llx/0x%x, offset %d\n",
872 le64_to_cpu(ctxt_sta->dtim_tsf),
873 le32_to_cpu(ctxt_sta->dtim_time),
874 dtim_offs);
875
Johannes Berg210a5442013-01-24 23:48:23 +0100876 ctxt_sta->is_assoc = cpu_to_le32(1);
Johannes Bergd2931bb2013-02-05 18:10:04 +0100877 } else {
Johannes Berg210a5442013-01-24 23:48:23 +0100878 ctxt_sta->is_assoc = cpu_to_le32(0);
Luciano Coelho7c8b3bc2014-07-30 15:04:08 +0300879
880 /* Allow beacons to pass through as long as we are not
881 * associated, or we do not have dtim period information.
882 */
883 cmd.filter_flags |= cpu_to_le32(MAC_FILTER_IN_BEACON);
Johannes Bergd2931bb2013-02-05 18:10:04 +0100884 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100885
886 ctxt_sta->bi = cpu_to_le32(vif->bss_conf.beacon_int);
887 ctxt_sta->bi_reciprocal =
888 cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int));
889 ctxt_sta->dtim_interval = cpu_to_le32(vif->bss_conf.beacon_int *
890 vif->bss_conf.dtim_period);
891 ctxt_sta->dtim_reciprocal =
892 cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int *
893 vif->bss_conf.dtim_period));
894
895 ctxt_sta->listen_interval = cpu_to_le32(mvm->hw->conf.listen_interval);
896 ctxt_sta->assoc_id = cpu_to_le32(vif->bss_conf.aid);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100897
Andrei Otcheretianskieffd1922015-06-30 12:08:28 +0300898 if (vif->probe_req_reg && vif->bss_conf.assoc && vif->p2p)
899 cmd.filter_flags |= cpu_to_le32(MAC_FILTER_IN_PROBE_REQUEST);
900
Johannes Berg8ca151b2013-01-24 14:25:36 +0100901 return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
902}
903
904static int iwl_mvm_mac_ctxt_cmd_listener(struct iwl_mvm *mvm,
905 struct ieee80211_vif *vif,
906 u32 action)
907{
908 struct iwl_mac_ctx_cmd cmd = {};
Chaya Rachel Ivgi0e39eb02015-12-03 15:51:46 +0200909 u32 tfd_queue_msk = 0;
910 int ret, i;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100911
912 WARN_ON(vif->type != NL80211_IFTYPE_MONITOR);
913
Johannes Berg3dfd3a92014-08-11 21:37:30 +0200914 iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
Johannes Berg53585492013-03-18 13:04:50 +0100915
Liad Kaufman737847a2016-09-27 15:17:29 +0300916 if (!iwl_mvm_is_dqa_supported(mvm)) {
917 for (i = 0; i < IEEE80211_NUM_ACS; i++)
918 if (vif->hw_queue[i] != IEEE80211_INVAL_HW_QUEUE)
919 tfd_queue_msk |= BIT(vif->hw_queue[i]);
920 }
Chaya Rachel Ivgi0e39eb02015-12-03 15:51:46 +0200921
Johannes Berg53585492013-03-18 13:04:50 +0100922 cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_PROMISC |
923 MAC_FILTER_IN_CONTROL_AND_MGMT |
924 MAC_FILTER_IN_BEACON |
Johannes Bergfb8b8ee2013-10-21 12:37:53 +0200925 MAC_FILTER_IN_PROBE_REQUEST |
926 MAC_FILTER_IN_CRC32);
Johannes Berg30686bf2015-06-02 21:39:54 +0200927 ieee80211_hw_set(mvm->hw, RX_INCLUDES_FCS);
Johannes Berg53585492013-03-18 13:04:50 +0100928
Chaya Rachel Ivgi0e39eb02015-12-03 15:51:46 +0200929 /* Allocate sniffer station */
930 ret = iwl_mvm_allocate_int_sta(mvm, &mvm->snif_sta, tfd_queue_msk,
Sara Sharonced19f22017-02-06 19:09:32 +0200931 vif->type, IWL_STA_GENERAL_PURPOSE);
Chaya Rachel Ivgi0e39eb02015-12-03 15:51:46 +0200932 if (ret)
933 return ret;
934
Johannes Berg8ca151b2013-01-24 14:25:36 +0100935 return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
936}
937
Johannes Berg5023d962013-07-31 14:07:43 +0200938static int iwl_mvm_mac_ctxt_cmd_ibss(struct iwl_mvm *mvm,
939 struct ieee80211_vif *vif,
940 u32 action)
941{
942 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
943 struct iwl_mac_ctx_cmd cmd = {};
944
945 WARN_ON(vif->type != NL80211_IFTYPE_ADHOC);
946
Johannes Berg3dfd3a92014-08-11 21:37:30 +0200947 iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
Johannes Berg5023d962013-07-31 14:07:43 +0200948
949 cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_BEACON |
950 MAC_FILTER_IN_PROBE_REQUEST);
951
952 /* cmd.ibss.beacon_time/cmd.ibss.beacon_tsf are curently ignored */
953 cmd.ibss.bi = cpu_to_le32(vif->bss_conf.beacon_int);
954 cmd.ibss.bi_reciprocal =
955 cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int));
956
957 /* TODO: Assumes that the beacon id == mac context id */
958 cmd.ibss.beacon_template = cpu_to_le32(mvmvif->id);
959
960 return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
961}
962
Johannes Berg8ca151b2013-01-24 14:25:36 +0100963struct iwl_mvm_go_iterator_data {
964 bool go_active;
965};
966
967static void iwl_mvm_go_iterator(void *_data, u8 *mac, struct ieee80211_vif *vif)
968{
969 struct iwl_mvm_go_iterator_data *data = _data;
970 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
971
Johannes Berg5023d962013-07-31 14:07:43 +0200972 if (vif->type == NL80211_IFTYPE_AP && vif->p2p &&
973 mvmvif->ap_ibss_active)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100974 data->go_active = true;
975}
976
977static int iwl_mvm_mac_ctxt_cmd_p2p_device(struct iwl_mvm *mvm,
978 struct ieee80211_vif *vif,
979 u32 action)
980{
981 struct iwl_mac_ctx_cmd cmd = {};
982 struct iwl_mvm_go_iterator_data data = {};
983
984 WARN_ON(vif->type != NL80211_IFTYPE_P2P_DEVICE);
985
Johannes Berg3dfd3a92014-08-11 21:37:30 +0200986 iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100987
988 cmd.protection_flags |= cpu_to_le32(MAC_PROT_FLG_TGG_PROTECT);
Ilan Peer1dcd15e2013-02-14 14:25:24 +0200989
990 /* Override the filter flags to accept only probe requests */
991 cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_PROBE_REQUEST);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100992
993 /*
994 * This flag should be set to true when the P2P Device is
995 * discoverable and there is at least another active P2P GO. Settings
996 * this flag will allow the P2P Device to be discoverable on other
997 * channels in addition to its listen channel.
998 * Note that this flag should not be set in other cases as it opens the
999 * Rx filters on all MAC and increases the number of interrupts.
1000 */
1001 ieee80211_iterate_active_interfaces_atomic(
1002 mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
1003 iwl_mvm_go_iterator, &data);
1004
1005 cmd.p2p_dev.is_disc_extended = cpu_to_le32(data.go_active ? 1 : 0);
1006 return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
1007}
1008
1009static void iwl_mvm_mac_ctxt_set_tim(struct iwl_mvm *mvm,
Sara Sharon8364fbb2016-12-08 11:44:20 +02001010 __le32 *tim_index, __le32 *tim_size,
Johannes Berg8ca151b2013-01-24 14:25:36 +01001011 u8 *beacon, u32 frame_size)
1012{
1013 u32 tim_idx;
1014 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)beacon;
1015
1016 /* The index is relative to frame start but we start looking at the
1017 * variable-length part of the beacon. */
1018 tim_idx = mgmt->u.beacon.variable - beacon;
1019
1020 /* Parse variable-length elements of beacon to find WLAN_EID_TIM */
1021 while ((tim_idx < (frame_size - 2)) &&
1022 (beacon[tim_idx] != WLAN_EID_TIM))
1023 tim_idx += beacon[tim_idx+1] + 2;
1024
1025 /* If TIM field was found, set variables */
1026 if ((tim_idx < (frame_size - 1)) && (beacon[tim_idx] == WLAN_EID_TIM)) {
Sara Sharon8364fbb2016-12-08 11:44:20 +02001027 *tim_index = cpu_to_le32(tim_idx);
1028 *tim_size = cpu_to_le32((u32)beacon[tim_idx + 1]);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001029 } else {
1030 IWL_WARN(mvm, "Unable to find TIM Element in beacon\n");
1031 }
1032}
1033
Andrei Otcheretianskid3a108a2016-02-28 17:12:21 +02001034static u32 iwl_mvm_find_ie_offset(u8 *beacon, u8 eid, u32 frame_size)
1035{
1036 struct ieee80211_mgmt *mgmt = (void *)beacon;
1037 const u8 *ie;
1038
1039 if (WARN_ON_ONCE(frame_size <= (mgmt->u.beacon.variable - beacon)))
1040 return 0;
1041
1042 frame_size -= mgmt->u.beacon.variable - beacon;
1043
1044 ie = cfg80211_find_ie(eid, mgmt->u.beacon.variable, frame_size);
1045 if (!ie)
1046 return 0;
1047
1048 return ie - beacon;
1049}
1050
Luca Coelho09856582017-06-01 15:19:45 +03001051static void iwl_mvm_mac_ctxt_set_tx(struct iwl_mvm *mvm,
1052 struct ieee80211_vif *vif,
1053 struct sk_buff *beacon,
1054 struct iwl_tx_cmd *tx)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001055{
1056 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001057 struct ieee80211_tx_info *info;
Emmanuel Grumbach75f6b9b2014-06-02 10:03:33 +03001058 u32 rate, tx_flags;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001059
Emmanuel Grumbach75f6b9b2014-06-02 10:03:33 +03001060 info = IEEE80211_SKB_CB(beacon);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001061
1062 /* Set up TX command fields */
Luca Coelho09856582017-06-01 15:19:45 +03001063 tx->len = cpu_to_le16((u16)beacon->len);
1064 tx->sta_id = mvmvif->bcast_sta.sta_id;
1065 tx->life_time = cpu_to_le32(TX_CMD_LIFE_TIME_INFINITE);
Emmanuel Grumbach75f6b9b2014-06-02 10:03:33 +03001066 tx_flags = TX_CMD_FLG_SEQ_CTL | TX_CMD_FLG_TSF;
1067 tx_flags |=
1068 iwl_mvm_bt_coex_tx_prio(mvm, (void *)beacon->data, info, 0) <<
1069 TX_CMD_FLG_BT_PRIO_POS;
Luca Coelho09856582017-06-01 15:19:45 +03001070 tx->tx_flags = cpu_to_le32(tx_flags);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001071
Emmanuel Grumbach1e3c3c32015-12-13 09:35:30 +02001072 if (!fw_has_capa(&mvm->fw->ucode_capa,
1073 IWL_UCODE_TLV_CAPA_BEACON_ANT_SELECTION)) {
1074 mvm->mgmt_last_antenna_idx =
1075 iwl_mvm_next_antenna(mvm, iwl_mvm_get_valid_tx_ant(mvm),
1076 mvm->mgmt_last_antenna_idx);
1077 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01001078
Luca Coelho09856582017-06-01 15:19:45 +03001079 tx->rate_n_flags =
Johannes Berg8ca151b2013-01-24 14:25:36 +01001080 cpu_to_le32(BIT(mvm->mgmt_last_antenna_idx) <<
1081 RATE_MCS_ANT_POS);
1082
Johannes Berg57fbcce2016-04-12 15:56:15 +02001083 if (info->band == NL80211_BAND_5GHZ || vif->p2p) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001084 rate = IWL_FIRST_OFDM_RATE;
1085 } else {
1086 rate = IWL_FIRST_CCK_RATE;
Luca Coelho09856582017-06-01 15:19:45 +03001087 tx->rate_n_flags |= cpu_to_le32(RATE_MCS_CCK_MSK);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001088 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01001089
Luca Coelho09856582017-06-01 15:19:45 +03001090 tx->rate_n_flags |= cpu_to_le32(iwl_mvm_mac80211_idx_to_hwrate(rate));
1091}
Johannes Berg8ca151b2013-01-24 14:25:36 +01001092
Luca Coelho09856582017-06-01 15:19:45 +03001093static int iwl_mvm_mac_ctxt_send_beacon_cmd(struct iwl_mvm *mvm,
1094 struct sk_buff *beacon,
1095 void *data, int len)
1096{
1097 struct iwl_host_cmd cmd = {
1098 .id = BEACON_TEMPLATE_CMD,
1099 .flags = CMD_ASYNC,
1100 };
1101
1102 cmd.len[0] = len;
1103 cmd.data[0] = data;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001104 cmd.dataflags[0] = 0;
Luca Coelho09856582017-06-01 15:19:45 +03001105 cmd.len[1] = beacon->len;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001106 cmd.data[1] = beacon->data;
1107 cmd.dataflags[1] = IWL_HCMD_DFL_DUP;
1108
1109 return iwl_mvm_send_cmd(mvm, &cmd);
1110}
1111
Luca Coelho09856582017-06-01 15:19:45 +03001112static int iwl_mvm_mac_ctxt_send_beacon_v6(struct iwl_mvm *mvm,
1113 struct ieee80211_vif *vif,
1114 struct sk_buff *beacon)
1115{
1116 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1117 struct iwl_mac_beacon_cmd_v6 beacon_cmd = {};
1118
1119 iwl_mvm_mac_ctxt_set_tx(mvm, vif, beacon, &beacon_cmd.tx);
1120
1121 beacon_cmd.template_id = cpu_to_le32((u32)mvmvif->id);
1122
1123 if (vif->type == NL80211_IFTYPE_AP)
1124 iwl_mvm_mac_ctxt_set_tim(mvm, &beacon_cmd.tim_idx,
1125 &beacon_cmd.tim_size,
1126 beacon->data, beacon->len);
1127
1128 return iwl_mvm_mac_ctxt_send_beacon_cmd(mvm, beacon, &beacon_cmd,
1129 sizeof(beacon_cmd));
1130}
1131
1132static int iwl_mvm_mac_ctxt_send_beacon_v7(struct iwl_mvm *mvm,
1133 struct ieee80211_vif *vif,
1134 struct sk_buff *beacon)
1135{
1136 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1137 struct iwl_mac_beacon_cmd_v7 beacon_cmd = {};
1138
1139 iwl_mvm_mac_ctxt_set_tx(mvm, vif, beacon, &beacon_cmd.tx);
1140
1141 beacon_cmd.template_id = cpu_to_le32((u32)mvmvif->id);
1142
1143 if (vif->type == NL80211_IFTYPE_AP)
1144 iwl_mvm_mac_ctxt_set_tim(mvm, &beacon_cmd.tim_idx,
1145 &beacon_cmd.tim_size,
1146 beacon->data, beacon->len);
1147
1148 beacon_cmd.csa_offset =
1149 cpu_to_le32(iwl_mvm_find_ie_offset(beacon->data,
1150 WLAN_EID_CHANNEL_SWITCH,
1151 beacon->len));
1152 beacon_cmd.ecsa_offset =
1153 cpu_to_le32(iwl_mvm_find_ie_offset(beacon->data,
1154 WLAN_EID_EXT_CHANSWITCH_ANN,
1155 beacon->len));
1156
1157 return iwl_mvm_mac_ctxt_send_beacon_cmd(mvm, beacon, &beacon_cmd,
1158 sizeof(beacon_cmd));
1159}
1160
1161static int iwl_mvm_mac_ctxt_send_beacon_v8(struct iwl_mvm *mvm,
1162 struct ieee80211_vif *vif,
1163 struct sk_buff *beacon)
1164{
1165 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1166 struct iwl_mac_beacon_cmd beacon_cmd = {};
1167
1168 beacon_cmd.byte_cnt = cpu_to_le16((u16)beacon->len);
1169 beacon_cmd.template_id = cpu_to_le32((u32)mvmvif->id);
1170
1171 if (vif->type == NL80211_IFTYPE_AP)
1172 iwl_mvm_mac_ctxt_set_tim(mvm,
1173 &beacon_cmd.tim_idx,
1174 &beacon_cmd.tim_size,
1175 beacon->data, beacon->len);
1176
1177 beacon_cmd.csa_offset =
1178 cpu_to_le32(iwl_mvm_find_ie_offset(beacon->data,
1179 WLAN_EID_CHANNEL_SWITCH,
1180 beacon->len));
1181 beacon_cmd.ecsa_offset =
1182 cpu_to_le32(iwl_mvm_find_ie_offset(beacon->data,
1183 WLAN_EID_EXT_CHANSWITCH_ANN,
1184 beacon->len));
1185
1186 return iwl_mvm_mac_ctxt_send_beacon_cmd(mvm, beacon, &beacon_cmd,
1187 sizeof(beacon_cmd));
1188}
1189
1190static int iwl_mvm_mac_ctxt_send_beacon(struct iwl_mvm *mvm,
1191 struct ieee80211_vif *vif,
1192 struct sk_buff *beacon)
1193{
1194 if (WARN_ON(!beacon))
1195 return -EINVAL;
1196
1197 if (!fw_has_capa(&mvm->fw->ucode_capa,
1198 IWL_UCODE_TLV_CAPA_CSA_AND_TBTT_OFFLOAD))
1199 return iwl_mvm_mac_ctxt_send_beacon_v6(mvm, vif, beacon);
1200
1201 if (!iwl_mvm_has_new_tx_api(mvm))
1202 return iwl_mvm_mac_ctxt_send_beacon_v7(mvm, vif, beacon);
1203
1204 return iwl_mvm_mac_ctxt_send_beacon_v8(mvm, vif, beacon);
1205}
1206
Johannes Berg5023d962013-07-31 14:07:43 +02001207/* The beacon template for the AP/GO/IBSS has changed and needs update */
Johannes Berg8ca151b2013-01-24 14:25:36 +01001208int iwl_mvm_mac_ctxt_beacon_changed(struct iwl_mvm *mvm,
1209 struct ieee80211_vif *vif)
1210{
1211 struct sk_buff *beacon;
1212 int ret;
1213
Johannes Berg5023d962013-07-31 14:07:43 +02001214 WARN_ON(vif->type != NL80211_IFTYPE_AP &&
1215 vif->type != NL80211_IFTYPE_ADHOC);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001216
Andrei Otcheretianskife887662014-04-10 21:46:16 +03001217 beacon = ieee80211_beacon_get_template(mvm->hw, vif, NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001218 if (!beacon)
1219 return -ENOMEM;
1220
1221 ret = iwl_mvm_mac_ctxt_send_beacon(mvm, vif, beacon);
1222 dev_kfree_skb(beacon);
1223 return ret;
1224}
1225
Ilan Peer3a3cb922013-06-13 08:30:12 +03001226struct iwl_mvm_mac_ap_iterator_data {
1227 struct iwl_mvm *mvm;
1228 struct ieee80211_vif *vif;
1229 u32 beacon_device_ts;
1230 u16 beacon_int;
1231};
1232
1233/* Find the beacon_device_ts and beacon_int for a managed interface */
1234static void iwl_mvm_mac_ap_iterator(void *_data, u8 *mac,
1235 struct ieee80211_vif *vif)
1236{
1237 struct iwl_mvm_mac_ap_iterator_data *data = _data;
1238
1239 if (vif->type != NL80211_IFTYPE_STATION || !vif->bss_conf.assoc)
1240 return;
1241
1242 /* Station client has higher priority over P2P client*/
1243 if (vif->p2p && data->beacon_device_ts)
1244 return;
1245
1246 data->beacon_device_ts = vif->bss_conf.sync_device_ts;
1247 data->beacon_int = vif->bss_conf.beacon_int;
1248}
1249
Johannes Berg8ca151b2013-01-24 14:25:36 +01001250/*
1251 * Fill the specific data for mac context of type AP of P2P GO
1252 */
1253static void iwl_mvm_mac_ctxt_cmd_fill_ap(struct iwl_mvm *mvm,
1254 struct ieee80211_vif *vif,
Johannes Berg186cd492016-08-24 10:05:40 +02001255 struct iwl_mac_ctx_cmd *cmd,
Johannes Berg506a81e2013-02-28 14:05:14 +01001256 struct iwl_mac_data_ap *ctxt_ap,
1257 bool add)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001258{
1259 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Ilan Peer3a3cb922013-06-13 08:30:12 +03001260 struct iwl_mvm_mac_ap_iterator_data data = {
1261 .mvm = mvm,
1262 .vif = vif,
1263 .beacon_device_ts = 0
1264 };
Johannes Berg8ca151b2013-01-24 14:25:36 +01001265
Johannes Berg186cd492016-08-24 10:05:40 +02001266 /* in AP mode, the MCAST FIFO takes the EDCA params from VO */
1267 cmd->ac[IWL_MVM_TX_FIFO_VO].fifos_mask |= BIT(IWL_MVM_TX_FIFO_MCAST);
1268
1269 /*
1270 * in AP mode, pass probe requests and beacons from other APs
1271 * (needed for ht protection); when there're no any associated
1272 * station don't ask FW to pass beacons to prevent unnecessary
1273 * wake-ups.
1274 */
1275 cmd->filter_flags |= cpu_to_le32(MAC_FILTER_IN_PROBE_REQUEST);
1276 if (mvmvif->ap_assoc_sta_count || !mvm->drop_bcn_ap_mode) {
1277 cmd->filter_flags |= cpu_to_le32(MAC_FILTER_IN_BEACON);
1278 IWL_DEBUG_HC(mvm, "Asking FW to pass beacons\n");
1279 } else {
1280 IWL_DEBUG_HC(mvm, "No need to receive beacons\n");
1281 }
1282
Johannes Berg8ca151b2013-01-24 14:25:36 +01001283 ctxt_ap->bi = cpu_to_le32(vif->bss_conf.beacon_int);
1284 ctxt_ap->bi_reciprocal =
1285 cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int));
1286 ctxt_ap->dtim_interval = cpu_to_le32(vif->bss_conf.beacon_int *
1287 vif->bss_conf.dtim_period);
1288 ctxt_ap->dtim_reciprocal =
1289 cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int *
1290 vif->bss_conf.dtim_period));
1291
Sara Sharonced19f22017-02-06 19:09:32 +02001292 if (!fw_has_api(&mvm->fw->ucode_capa,
1293 IWL_UCODE_TLV_API_STA_TYPE))
1294 ctxt_ap->mcast_qid = cpu_to_le32(vif->cab_queue);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001295
Johannes Berg506a81e2013-02-28 14:05:14 +01001296 /*
Ilan Peer3a3cb922013-06-13 08:30:12 +03001297 * Only set the beacon time when the MAC is being added, when we
Johannes Berg506a81e2013-02-28 14:05:14 +01001298 * just modify the MAC then we should keep the time -- the firmware
1299 * can otherwise have a "jumping" TBTT.
1300 */
Ilan Peer3a3cb922013-06-13 08:30:12 +03001301 if (add) {
1302 /*
1303 * If there is a station/P2P client interface which is
1304 * associated, set the AP's TBTT far enough from the station's
1305 * TBTT. Otherwise, set it to the current system time
1306 */
1307 ieee80211_iterate_active_interfaces_atomic(
1308 mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
1309 iwl_mvm_mac_ap_iterator, &data);
1310
1311 if (data.beacon_device_ts) {
Ilan Peer9f8f8ca2013-12-24 17:17:05 +02001312 u32 rand = (prandom_u32() % (64 - 36)) + 36;
Ilan Peer3a3cb922013-06-13 08:30:12 +03001313 mvmvif->ap_beacon_time = data.beacon_device_ts +
1314 ieee80211_tu_to_usec(data.beacon_int * rand /
1315 100);
1316 } else {
1317 mvmvif->ap_beacon_time =
1318 iwl_read_prph(mvm->trans,
1319 DEVICE_SYSTEM_TIME_REG);
1320 }
1321 }
Johannes Berg506a81e2013-02-28 14:05:14 +01001322
1323 ctxt_ap->beacon_time = cpu_to_le32(mvmvif->ap_beacon_time);
Johannes Berg506a81e2013-02-28 14:05:14 +01001324 ctxt_ap->beacon_tsf = 0; /* unused */
Johannes Berg8ca151b2013-01-24 14:25:36 +01001325
1326 /* TODO: Assume that the beacon id == mac context id */
1327 ctxt_ap->beacon_template = cpu_to_le32(mvmvif->id);
1328}
1329
Ilan Peerf82c8332015-09-10 12:54:38 +03001330static int iwl_mvm_mac_ctxt_cmd_ap(struct iwl_mvm *mvm,
1331 struct ieee80211_vif *vif,
1332 u32 action)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001333{
1334 struct iwl_mac_ctx_cmd cmd = {};
1335
1336 WARN_ON(vif->type != NL80211_IFTYPE_AP || vif->p2p);
1337
1338 /* Fill the common data for all mac context types */
Johannes Berg3dfd3a92014-08-11 21:37:30 +02001339 iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001340
Johannes Berg8ca151b2013-01-24 14:25:36 +01001341 /* Fill the data specific for ap mode */
Johannes Berg186cd492016-08-24 10:05:40 +02001342 iwl_mvm_mac_ctxt_cmd_fill_ap(mvm, vif, &cmd, &cmd.ap,
Johannes Berg506a81e2013-02-28 14:05:14 +01001343 action == FW_CTXT_ACTION_ADD);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001344
1345 return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
1346}
1347
1348static int iwl_mvm_mac_ctxt_cmd_go(struct iwl_mvm *mvm,
1349 struct ieee80211_vif *vif,
1350 u32 action)
1351{
1352 struct iwl_mac_ctx_cmd cmd = {};
Janusz Dziedzic67baf662013-03-21 15:47:56 +01001353 struct ieee80211_p2p_noa_attr *noa = &vif->bss_conf.p2p_noa_attr;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001354
1355 WARN_ON(vif->type != NL80211_IFTYPE_AP || !vif->p2p);
1356
1357 /* Fill the common data for all mac context types */
Johannes Berg3dfd3a92014-08-11 21:37:30 +02001358 iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001359
1360 /* Fill the data specific for GO mode */
Johannes Berg186cd492016-08-24 10:05:40 +02001361 iwl_mvm_mac_ctxt_cmd_fill_ap(mvm, vif, &cmd, &cmd.go.ap,
Johannes Berg506a81e2013-02-28 14:05:14 +01001362 action == FW_CTXT_ACTION_ADD);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001363
Janusz Dziedzic67baf662013-03-21 15:47:56 +01001364 cmd.go.ctwin = cpu_to_le32(noa->oppps_ctwindow &
1365 IEEE80211_P2P_OPPPS_CTWINDOW_MASK);
1366 cmd.go.opp_ps_enabled =
1367 cpu_to_le32(!!(noa->oppps_ctwindow &
1368 IEEE80211_P2P_OPPPS_ENABLE_BIT));
Johannes Berg8ca151b2013-01-24 14:25:36 +01001369
1370 return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
1371}
1372
1373static int iwl_mvm_mac_ctx_send(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
Johannes Berg3dfd3a92014-08-11 21:37:30 +02001374 u32 action, bool force_assoc_off,
1375 const u8 *bssid_override)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001376{
1377 switch (vif->type) {
1378 case NL80211_IFTYPE_STATION:
Luciano Coelhocf520232014-05-13 16:52:54 +03001379 return iwl_mvm_mac_ctxt_cmd_sta(mvm, vif, action,
Johannes Berg3dfd3a92014-08-11 21:37:30 +02001380 force_assoc_off,
1381 bssid_override);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001382 break;
1383 case NL80211_IFTYPE_AP:
1384 if (!vif->p2p)
1385 return iwl_mvm_mac_ctxt_cmd_ap(mvm, vif, action);
1386 else
1387 return iwl_mvm_mac_ctxt_cmd_go(mvm, vif, action);
1388 break;
1389 case NL80211_IFTYPE_MONITOR:
1390 return iwl_mvm_mac_ctxt_cmd_listener(mvm, vif, action);
1391 case NL80211_IFTYPE_P2P_DEVICE:
1392 return iwl_mvm_mac_ctxt_cmd_p2p_device(mvm, vif, action);
Johannes Berg5023d962013-07-31 14:07:43 +02001393 case NL80211_IFTYPE_ADHOC:
1394 return iwl_mvm_mac_ctxt_cmd_ibss(mvm, vif, action);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001395 default:
1396 break;
1397 }
1398
1399 return -EOPNOTSUPP;
1400}
1401
1402int iwl_mvm_mac_ctxt_add(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1403{
1404 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1405 int ret;
1406
1407 if (WARN_ONCE(mvmvif->uploaded, "Adding active MAC %pM/%d\n",
1408 vif->addr, ieee80211_vif_type_p2p(vif)))
1409 return -EIO;
1410
Luciano Coelhobca49d92014-05-13 17:33:38 +03001411 ret = iwl_mvm_mac_ctx_send(mvm, vif, FW_CTXT_ACTION_ADD,
Johannes Berg3dfd3a92014-08-11 21:37:30 +02001412 true, NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001413 if (ret)
1414 return ret;
1415
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001416 /* will only do anything at resume from D3 time */
1417 iwl_mvm_set_last_nonqos_seq(mvm, vif);
1418
Johannes Berg8ca151b2013-01-24 14:25:36 +01001419 mvmvif->uploaded = true;
1420 return 0;
1421}
1422
Luciano Coelhobca49d92014-05-13 17:33:38 +03001423int iwl_mvm_mac_ctxt_changed(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
Johannes Berg3dfd3a92014-08-11 21:37:30 +02001424 bool force_assoc_off, const u8 *bssid_override)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001425{
1426 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1427
1428 if (WARN_ONCE(!mvmvif->uploaded, "Changing inactive MAC %pM/%d\n",
1429 vif->addr, ieee80211_vif_type_p2p(vif)))
1430 return -EIO;
1431
Luciano Coelhobca49d92014-05-13 17:33:38 +03001432 return iwl_mvm_mac_ctx_send(mvm, vif, FW_CTXT_ACTION_MODIFY,
Johannes Berg3dfd3a92014-08-11 21:37:30 +02001433 force_assoc_off, bssid_override);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001434}
1435
1436int iwl_mvm_mac_ctxt_remove(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1437{
1438 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1439 struct iwl_mac_ctx_cmd cmd;
1440 int ret;
1441
1442 if (WARN_ONCE(!mvmvif->uploaded, "Removing inactive MAC %pM/%d\n",
1443 vif->addr, ieee80211_vif_type_p2p(vif)))
1444 return -EIO;
1445
1446 memset(&cmd, 0, sizeof(cmd));
1447
1448 cmd.id_and_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
1449 mvmvif->color));
1450 cmd.action = cpu_to_le32(FW_CTXT_ACTION_REMOVE);
1451
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03001452 ret = iwl_mvm_send_cmd_pdu(mvm, MAC_CONTEXT_CMD, 0,
Johannes Berg8ca151b2013-01-24 14:25:36 +01001453 sizeof(cmd), &cmd);
1454 if (ret) {
1455 IWL_ERR(mvm, "Failed to remove MAC context: %d\n", ret);
1456 return ret;
1457 }
1458
1459 mvmvif->uploaded = false;
Johannes Bergfb8b8ee2013-10-21 12:37:53 +02001460
Chaya Rachel Ivgi0e39eb02015-12-03 15:51:46 +02001461 if (vif->type == NL80211_IFTYPE_MONITOR) {
Johannes Berg30686bf2015-06-02 21:39:54 +02001462 __clear_bit(IEEE80211_HW_RX_INCLUDES_FCS, mvm->hw->flags);
Chaya Rachel Ivgi0e39eb02015-12-03 15:51:46 +02001463 iwl_mvm_dealloc_snif_sta(mvm);
1464 }
Johannes Bergfb8b8ee2013-10-21 12:37:53 +02001465
Johannes Berg8ca151b2013-01-24 14:25:36 +01001466 return 0;
1467}
Ilan Peer571765c2013-03-05 15:26:03 +02001468
Andrei Otcheretianski7f0a7c62014-05-04 11:48:12 +03001469static void iwl_mvm_csa_count_down(struct iwl_mvm *mvm,
Andrei Otcheretianski7ef0aab2014-11-10 11:10:11 +02001470 struct ieee80211_vif *csa_vif, u32 gp2,
1471 bool tx_success)
Andrei Otcheretianski7f0a7c62014-05-04 11:48:12 +03001472{
1473 struct iwl_mvm_vif *mvmvif =
1474 iwl_mvm_vif_from_mac80211(csa_vif);
1475
Andrei Otcheretianski7ef0aab2014-11-10 11:10:11 +02001476 /* Don't start to countdown from a failed beacon */
1477 if (!tx_success && !mvmvif->csa_countdown)
1478 return;
1479
1480 mvmvif->csa_countdown = true;
1481
Andrei Otcheretianski7f0a7c62014-05-04 11:48:12 +03001482 if (!ieee80211_csa_is_complete(csa_vif)) {
1483 int c = ieee80211_csa_update_counter(csa_vif);
1484
1485 iwl_mvm_mac_ctxt_beacon_changed(mvm, csa_vif);
1486 if (csa_vif->p2p &&
Andrei Otcheretianski7ef0aab2014-11-10 11:10:11 +02001487 !iwl_mvm_te_scheduled(&mvmvif->time_event_data) && gp2 &&
1488 tx_success) {
Andrei Otcheretianski7f0a7c62014-05-04 11:48:12 +03001489 u32 rel_time = (c + 1) *
1490 csa_vif->bss_conf.beacon_int -
Luciano Coelhof991e172014-08-26 16:14:10 +03001491 IWL_MVM_CHANNEL_SWITCH_TIME_GO;
Andrei Otcheretianski7f0a7c62014-05-04 11:48:12 +03001492 u32 apply_time = gp2 + rel_time * 1024;
1493
Luciano Coelhof991e172014-08-26 16:14:10 +03001494 iwl_mvm_schedule_csa_period(mvm, csa_vif,
1495 IWL_MVM_CHANNEL_SWITCH_TIME_GO -
1496 IWL_MVM_CHANNEL_SWITCH_MARGIN,
1497 apply_time);
Andrei Otcheretianski7f0a7c62014-05-04 11:48:12 +03001498 }
1499 } else if (!iwl_mvm_te_scheduled(&mvmvif->time_event_data)) {
1500 /* we don't have CSA NoA scheduled yet, switch now */
1501 ieee80211_csa_finish(csa_vif);
1502 RCU_INIT_POINTER(mvm->csa_vif, NULL);
1503 }
1504}
1505
Johannes Berg04168412015-06-23 21:22:09 +02001506void iwl_mvm_rx_beacon_notif(struct iwl_mvm *mvm,
1507 struct iwl_rx_cmd_buffer *rxb)
Ilan Peer571765c2013-03-05 15:26:03 +02001508{
1509 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Emmanuel Grumbach2d2c0e92014-11-24 08:51:46 +02001510 struct iwl_extended_beacon_notif *beacon = (void *)pkt->data;
David Spinadel1c87bba2014-02-27 16:41:52 +02001511 struct iwl_mvm_tx_resp *beacon_notify_hdr;
Andrei Otcheretianski664322f2014-06-05 16:40:36 +03001512 struct ieee80211_vif *csa_vif;
Andrei Otcheretianski003e52362014-05-25 17:24:22 +03001513 struct ieee80211_vif *tx_blocked_vif;
Sara Sharon12db2942017-01-17 14:28:21 +02001514 struct agg_tx_status *agg_status;
Andrei Otcheretianski7ef0aab2014-11-10 11:10:11 +02001515 u16 status;
Ilan Peer571765c2013-03-05 15:26:03 +02001516
Andrei Otcheretianskibd3398e2013-10-22 05:01:12 +02001517 lockdep_assert_held(&mvm->mutex);
1518
Emmanuel Grumbach2d2c0e92014-11-24 08:51:46 +02001519 beacon_notify_hdr = &beacon->beacon_notify_hdr;
1520 mvm->ap_last_beacon_gp2 = le32_to_cpu(beacon->gp2);
Johannes Berg2f0282d2017-04-20 10:29:03 +02001521 mvm->ibss_manager = beacon->ibss_mgr_status != 0;
David Spinadel1c87bba2014-02-27 16:41:52 +02001522
Sara Sharon12db2942017-01-17 14:28:21 +02001523 agg_status = iwl_mvm_get_agg_status(mvm, beacon_notify_hdr);
1524 status = le16_to_cpu(agg_status->status) & TX_STATUS_MSK;
David Spinadel1c87bba2014-02-27 16:41:52 +02001525 IWL_DEBUG_RX(mvm,
1526 "beacon status %#x retries:%d tsf:0x%16llX gp2:0x%X rate:%d\n",
Emmanuel Grumbach2d2c0e92014-11-24 08:51:46 +02001527 status, beacon_notify_hdr->failure_frame,
1528 le64_to_cpu(beacon->tsf),
David Spinadel1c87bba2014-02-27 16:41:52 +02001529 mvm->ap_last_beacon_gp2,
1530 le32_to_cpu(beacon_notify_hdr->initial_rate));
Andrei Otcheretianskibd3398e2013-10-22 05:01:12 +02001531
Andrei Otcheretianski664322f2014-06-05 16:40:36 +03001532 csa_vif = rcu_dereference_protected(mvm->csa_vif,
1533 lockdep_is_held(&mvm->mutex));
Andrei Otcheretianski7f0a7c62014-05-04 11:48:12 +03001534 if (unlikely(csa_vif && csa_vif->csa_active))
Andrei Otcheretianski7ef0aab2014-11-10 11:10:11 +02001535 iwl_mvm_csa_count_down(mvm, csa_vif, mvm->ap_last_beacon_gp2,
1536 (status == TX_STATUS_SUCCESS));
Andrei Otcheretianskibd3398e2013-10-22 05:01:12 +02001537
Andrei Otcheretianski003e52362014-05-25 17:24:22 +03001538 tx_blocked_vif = rcu_dereference_protected(mvm->csa_tx_blocked_vif,
1539 lockdep_is_held(&mvm->mutex));
1540 if (unlikely(tx_blocked_vif)) {
1541 struct iwl_mvm_vif *mvmvif =
1542 iwl_mvm_vif_from_mac80211(tx_blocked_vif);
1543
1544 /*
1545 * The channel switch is started and we have blocked the
1546 * stations. If this is the first beacon (the timeout wasn't
1547 * set), set the unblock timeout, otherwise countdown
1548 */
1549 if (!mvm->csa_tx_block_bcn_timeout)
1550 mvm->csa_tx_block_bcn_timeout =
1551 IWL_MVM_CS_UNBLOCK_TX_TIMEOUT;
1552 else
1553 mvm->csa_tx_block_bcn_timeout--;
1554
1555 /* Check if the timeout is expired, and unblock tx */
1556 if (mvm->csa_tx_block_bcn_timeout == 0) {
1557 iwl_mvm_modify_all_sta_disable_tx(mvm, mvmvif, false);
1558 RCU_INIT_POINTER(mvm->csa_tx_blocked_vif, NULL);
1559 }
1560 }
Ilan Peer571765c2013-03-05 15:26:03 +02001561}
Hila Gonend64048e2013-03-13 18:00:03 +02001562
1563static void iwl_mvm_beacon_loss_iterator(void *_data, u8 *mac,
1564 struct ieee80211_vif *vif)
1565{
Ilan Peer12d423e2013-12-24 22:08:14 +02001566 struct iwl_missed_beacons_notif *missed_beacons = _data;
Hila Gonend64048e2013-03-13 18:00:03 +02001567 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Emmanuel Grumbach9d761fd2015-02-02 12:44:23 +02001568 struct iwl_mvm *mvm = mvmvif->mvm;
1569 struct iwl_fw_dbg_trigger_missed_bcon *bcon_trig;
1570 struct iwl_fw_dbg_trigger_tlv *trigger;
1571 u32 stop_trig_missed_bcon, stop_trig_missed_bcon_since_rx;
1572 u32 rx_missed_bcon, rx_missed_bcon_since_rx;
Hila Gonend64048e2013-03-13 18:00:03 +02001573
Ilan Peer12d423e2013-12-24 22:08:14 +02001574 if (mvmvif->id != (u16)le32_to_cpu(missed_beacons->mac_id))
1575 return;
1576
Emmanuel Grumbach9d761fd2015-02-02 12:44:23 +02001577 rx_missed_bcon = le32_to_cpu(missed_beacons->consec_missed_beacons);
1578 rx_missed_bcon_since_rx =
1579 le32_to_cpu(missed_beacons->consec_missed_beacons_since_last_rx);
Ilan Peer12d423e2013-12-24 22:08:14 +02001580 /*
1581 * TODO: the threshold should be adjusted based on latency conditions,
1582 * and/or in case of a CS flow on one of the other AP vifs.
1583 */
1584 if (le32_to_cpu(missed_beacons->consec_missed_beacons_since_last_rx) >
1585 IWL_MVM_MISSED_BEACONS_THRESHOLD)
Hila Gonend64048e2013-03-13 18:00:03 +02001586 ieee80211_beacon_loss(vif);
Emmanuel Grumbach9d761fd2015-02-02 12:44:23 +02001587
1588 if (!iwl_fw_dbg_trigger_enabled(mvm->fw,
1589 FW_DBG_TRIGGER_MISSED_BEACONS))
1590 return;
1591
1592 trigger = iwl_fw_dbg_get_trigger(mvm->fw,
1593 FW_DBG_TRIGGER_MISSED_BEACONS);
1594 bcon_trig = (void *)trigger->data;
1595 stop_trig_missed_bcon = le32_to_cpu(bcon_trig->stop_consec_missed_bcon);
1596 stop_trig_missed_bcon_since_rx =
1597 le32_to_cpu(bcon_trig->stop_consec_missed_bcon_since_rx);
1598
1599 /* TODO: implement start trigger */
1600
Johannes Berg7174beb2017-06-01 16:03:19 +02001601 if (!iwl_fw_dbg_trigger_check_stop(&mvm->fwrt,
1602 ieee80211_vif_to_wdev(vif),
1603 trigger))
Emmanuel Grumbach9d761fd2015-02-02 12:44:23 +02001604 return;
1605
1606 if (rx_missed_bcon_since_rx >= stop_trig_missed_bcon_since_rx ||
1607 rx_missed_bcon >= stop_trig_missed_bcon)
Johannes Berg7174beb2017-06-01 16:03:19 +02001608 iwl_fw_dbg_collect_trig(&mvm->fwrt, trigger, NULL);
Hila Gonend64048e2013-03-13 18:00:03 +02001609}
1610
Johannes Berg04168412015-06-23 21:22:09 +02001611void iwl_mvm_rx_missed_beacons_notif(struct iwl_mvm *mvm,
1612 struct iwl_rx_cmd_buffer *rxb)
Hila Gonend64048e2013-03-13 18:00:03 +02001613{
1614 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Ilan Peer12d423e2013-12-24 22:08:14 +02001615 struct iwl_missed_beacons_notif *mb = (void *)pkt->data;
1616
1617 IWL_DEBUG_INFO(mvm,
1618 "missed bcn mac_id=%u, consecutive=%u (%u, %u, %u)\n",
1619 le32_to_cpu(mb->mac_id),
1620 le32_to_cpu(mb->consec_missed_beacons),
1621 le32_to_cpu(mb->consec_missed_beacons_since_last_rx),
1622 le32_to_cpu(mb->num_recvd_beacons),
1623 le32_to_cpu(mb->num_expected_beacons));
Hila Gonend64048e2013-03-13 18:00:03 +02001624
1625 ieee80211_iterate_active_interfaces_atomic(mvm->hw,
1626 IEEE80211_IFACE_ITER_NORMAL,
1627 iwl_mvm_beacon_loss_iterator,
Ilan Peer12d423e2013-12-24 22:08:14 +02001628 mb);
Hila Gonend64048e2013-03-13 18:00:03 +02001629}
Sara Sharon0db056d2015-12-29 11:07:15 +02001630
1631void iwl_mvm_rx_stored_beacon_notif(struct iwl_mvm *mvm,
1632 struct iwl_rx_cmd_buffer *rxb)
1633{
1634 struct iwl_rx_packet *pkt = rxb_addr(rxb);
1635 struct iwl_stored_beacon_notif *sb = (void *)pkt->data;
1636 struct ieee80211_rx_status rx_status;
1637 struct sk_buff *skb;
1638 u32 size = le32_to_cpu(sb->byte_count);
1639
1640 if (size == 0)
1641 return;
1642
1643 skb = alloc_skb(size, GFP_ATOMIC);
1644 if (!skb) {
1645 IWL_ERR(mvm, "alloc_skb failed\n");
1646 return;
1647 }
1648
1649 /* update rx_status according to the notification's metadata */
1650 memset(&rx_status, 0, sizeof(rx_status));
1651 rx_status.mactime = le64_to_cpu(sb->tsf);
Sara Sharon77fe7392016-01-10 14:23:25 +02001652 /* TSF as indicated by the firmware is at INA time */
1653 rx_status.flag |= RX_FLAG_MACTIME_PLCP_START;
Sara Sharon0db056d2015-12-29 11:07:15 +02001654 rx_status.device_timestamp = le32_to_cpu(sb->system_time);
1655 rx_status.band =
Sara Sharon34118c22016-11-08 18:34:42 +02001656 (sb->band & cpu_to_le16(RX_RES_PHY_FLAGS_BAND_24)) ?
Johannes Berg57fbcce2016-04-12 15:56:15 +02001657 NL80211_BAND_2GHZ : NL80211_BAND_5GHZ;
Sara Sharon0db056d2015-12-29 11:07:15 +02001658 rx_status.freq =
1659 ieee80211_channel_to_frequency(le16_to_cpu(sb->channel),
1660 rx_status.band);
1661
1662 /* copy the data */
Johannes Berg59ae1d12017-06-16 14:29:20 +02001663 skb_put_data(skb, sb->data, size);
Sara Sharon0db056d2015-12-29 11:07:15 +02001664 memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
1665
1666 /* pass it as regular rx to mac80211 */
Johannes Bergd63b5482016-03-31 20:02:02 +03001667 ieee80211_rx_napi(mvm->hw, NULL, skb, NULL);
Sara Sharon0db056d2015-12-29 11:07:15 +02001668}
Andrei Otcheretianskid3a108a2016-02-28 17:12:21 +02001669
1670void iwl_mvm_channel_switch_noa_notif(struct iwl_mvm *mvm,
1671 struct iwl_rx_cmd_buffer *rxb)
1672{
1673 struct iwl_rx_packet *pkt = rxb_addr(rxb);
1674 struct iwl_channel_switch_noa_notif *notif = (void *)pkt->data;
1675 struct ieee80211_vif *csa_vif;
1676 struct iwl_mvm_vif *mvmvif;
1677 int len = iwl_rx_packet_payload_len(pkt);
1678 u32 id_n_color;
1679
1680 if (WARN_ON_ONCE(len < sizeof(*notif)))
1681 return;
1682
1683 rcu_read_lock();
1684
1685 csa_vif = rcu_dereference(mvm->csa_vif);
1686 if (WARN_ON(!csa_vif || !csa_vif->csa_active))
1687 goto out_unlock;
1688
1689 id_n_color = le32_to_cpu(notif->id_and_color);
1690
1691 mvmvif = iwl_mvm_vif_from_mac80211(csa_vif);
1692 if (WARN(FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color) != id_n_color,
1693 "channel switch noa notification on unexpected vif (csa_vif=%d, notif=%d)",
1694 FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color), id_n_color))
1695 goto out_unlock;
1696
1697 IWL_DEBUG_INFO(mvm, "Channel Switch Started Notification\n");
1698
Johannes Berg86bbb1e2017-03-22 21:44:59 +01001699 schedule_delayed_work(&mvm->cs_tx_unblock_dwork,
1700 msecs_to_jiffies(IWL_MVM_CS_UNBLOCK_TX_TIMEOUT *
1701 csa_vif->bss_conf.beacon_int));
Andrei Otcheretianskid3a108a2016-02-28 17:12:21 +02001702
1703 ieee80211_csa_finish(csa_vif);
1704
1705 rcu_read_unlock();
1706
1707 RCU_INIT_POINTER(mvm->csa_vif, NULL);
1708
1709 return;
1710
1711out_unlock:
1712 rcu_read_unlock();
1713}