blob: 0bcd8c78beb8521f1507d9d1f42a58251f4c784a [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
Sara Sharon0db056d2015-12-29 11:07:15 +020010 * Copyright(c) 2015 - 2016 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
Sara Sharon0db056d2015-12-29 11:07:15 +020037 * Copyright(c) 2015 - 2016 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"
Golan Ben-Ami2f89a5d2015-10-27 19:17:14 +020075#include "fw-dbg.h"
Johannes Berg8ca151b2013-01-24 14:25:36 +010076
77const u8 iwl_mvm_ac_to_tx_fifo[] = {
Johannes Berg8ca151b2013-01-24 14:25:36 +010078 IWL_MVM_TX_FIFO_VO,
Emmanuel Grumbach3dd94792013-12-19 16:50:49 +020079 IWL_MVM_TX_FIFO_VI,
80 IWL_MVM_TX_FIFO_BE,
81 IWL_MVM_TX_FIFO_BK,
Johannes Berg8ca151b2013-01-24 14:25:36 +010082};
83
84struct iwl_mvm_mac_iface_iterator_data {
85 struct iwl_mvm *mvm;
86 struct ieee80211_vif *vif;
87 unsigned long available_mac_ids[BITS_TO_LONGS(NUM_MAC_INDEX_DRIVER)];
88 unsigned long available_tsf_ids[BITS_TO_LONGS(NUM_TSF_IDS)];
Johannes Berg8ca151b2013-01-24 14:25:36 +010089 enum iwl_tsf_id preferred_tsf;
90 bool found_vif;
91};
92
Arik Nemtsova74346d2014-09-21 19:07:30 +030093struct iwl_mvm_hw_queues_iface_iterator_data {
94 struct ieee80211_vif *exclude_vif;
95 unsigned long used_hw_queues;
96};
97
Ilan Peer6e97b0d2013-12-23 22:18:02 +020098static void iwl_mvm_mac_tsf_id_iter(void *_data, u8 *mac,
99 struct ieee80211_vif *vif)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100100{
101 struct iwl_mvm_mac_iface_iterator_data *data = _data;
102 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Ilan Peer2c3e62a2014-02-02 21:54:35 +0200103 u16 min_bi;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100104
Ilan Peer6e97b0d2013-12-23 22:18:02 +0200105 /* Skip the interface for which we are trying to assign a tsf_id */
106 if (vif == data->vif)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100107 return;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100108
109 /*
110 * The TSF is a hardware/firmware resource, there are 4 and
111 * the driver should assign and free them as needed. However,
112 * there are cases where 2 MACs should share the same TSF ID
113 * for the purpose of clock sync, an optimization to avoid
114 * clock drift causing overlapping TBTTs/DTIMs for a GO and
115 * client in the system.
116 *
117 * The firmware will decide according to the MAC type which
118 * will be the master and slave. Clients that need to sync
119 * with a remote station will be the master, and an AP or GO
120 * will be the slave.
121 *
122 * Depending on the new interface type it can be slaved to
123 * or become the master of an existing interface.
124 */
125 switch (data->vif->type) {
126 case NL80211_IFTYPE_STATION:
127 /*
Ilan Peer2c3e62a2014-02-02 21:54:35 +0200128 * The new interface is a client, so if the one we're iterating
129 * is an AP, and the beacon interval of the AP is a multiple or
130 * divisor of the beacon interval of the client, the same TSF
131 * should be used to avoid drift between the new client and
132 * existing AP. The existing AP will get drift updates from the
133 * new client context in this case.
Johannes Berg8ca151b2013-01-24 14:25:36 +0100134 */
Ilan Peer2c3e62a2014-02-02 21:54:35 +0200135 if (vif->type != NL80211_IFTYPE_AP ||
136 data->preferred_tsf != NUM_TSF_IDS ||
137 !test_bit(mvmvif->tsf_id, data->available_tsf_ids))
138 break;
139
140 min_bi = min(data->vif->bss_conf.beacon_int,
141 vif->bss_conf.beacon_int);
142
143 if (!min_bi)
144 break;
145
146 if ((data->vif->bss_conf.beacon_int -
147 vif->bss_conf.beacon_int) % min_bi == 0) {
148 data->preferred_tsf = mvmvif->tsf_id;
149 return;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100150 }
151 break;
Ilan Peer2c3e62a2014-02-02 21:54:35 +0200152
Johannes Berg8ca151b2013-01-24 14:25:36 +0100153 case NL80211_IFTYPE_AP:
154 /*
Ilan Peer2c3e62a2014-02-02 21:54:35 +0200155 * The new interface is AP/GO, so if its beacon interval is a
156 * multiple or a divisor of the beacon interval of an existing
157 * interface, it should get drift updates from an existing
158 * client or use the same TSF as an existing GO. There's no
159 * drift between TSFs internally but if they used different
160 * TSFs then a new client MAC could update one of them and
161 * cause drift that way.
Johannes Berg8ca151b2013-01-24 14:25:36 +0100162 */
Ilan Peer2c3e62a2014-02-02 21:54:35 +0200163 if ((vif->type != NL80211_IFTYPE_AP &&
164 vif->type != NL80211_IFTYPE_STATION) ||
165 data->preferred_tsf != NUM_TSF_IDS ||
166 !test_bit(mvmvif->tsf_id, data->available_tsf_ids))
167 break;
168
169 min_bi = min(data->vif->bss_conf.beacon_int,
170 vif->bss_conf.beacon_int);
171
172 if (!min_bi)
173 break;
174
175 if ((data->vif->bss_conf.beacon_int -
176 vif->bss_conf.beacon_int) % min_bi == 0) {
177 data->preferred_tsf = mvmvif->tsf_id;
178 return;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100179 }
180 break;
181 default:
182 /*
183 * For all other interface types there's no need to
184 * take drift into account. Either they're exclusive
185 * like IBSS and monitor, or we don't care much about
186 * their TSF (like P2P Device), but we won't be able
187 * to share the TSF resource.
188 */
189 break;
190 }
191
192 /*
193 * Unless we exited above, we can't share the TSF resource
194 * that the virtual interface we're iterating over is using
195 * with the new one, so clear the available bit and if this
196 * was the preferred one, reset that as well.
197 */
198 __clear_bit(mvmvif->tsf_id, data->available_tsf_ids);
199
200 if (data->preferred_tsf == mvmvif->tsf_id)
201 data->preferred_tsf = NUM_TSF_IDS;
202}
203
Johannes Berg110cf812014-08-01 23:14:24 +0200204/*
205 * Get the mask of the queues used by the vif
206 */
Arik Nemtsovd92b732e2014-09-21 19:00:42 +0300207u32 iwl_mvm_mac_get_queues_mask(struct ieee80211_vif *vif)
Johannes Berg110cf812014-08-01 23:14:24 +0200208{
209 u32 qmask = 0, ac;
210
211 if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
212 return BIT(IWL_MVM_OFFCHANNEL_QUEUE);
213
Ilan Peer1008e442015-01-17 22:35:00 -0500214 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
215 if (vif->hw_queue[ac] != IEEE80211_INVAL_HW_QUEUE)
216 qmask |= BIT(vif->hw_queue[ac]);
217 }
Johannes Berg110cf812014-08-01 23:14:24 +0200218
219 if (vif->type == NL80211_IFTYPE_AP)
220 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))
259 data.used_hw_queues |= BIT(IWL_MVM_DQA_CMD_QUEUE);
260 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
270 /* don't assign the same hw queues as TDLS stations */
271 ieee80211_iterate_stations_atomic(mvm->hw,
272 iwl_mvm_mac_sta_hw_queues_iter,
273 &data);
274
Arik Nemtsov51ec09a2015-01-21 11:50:31 +0200275 /*
276 * Some TDLS stations may be removed but are in the process of being
277 * drained. Don't touch their queues.
278 */
279 for_each_set_bit(sta_id, mvm->sta_drained, IWL_MVM_STATION_COUNT)
280 data.used_hw_queues |= mvm->tfd_drained[sta_id];
281
Arik Nemtsova74346d2014-09-21 19:07:30 +0300282 return data.used_hw_queues;
283}
284
Ilan Peer6e97b0d2013-12-23 22:18:02 +0200285static void iwl_mvm_mac_iface_iterator(void *_data, u8 *mac,
286 struct ieee80211_vif *vif)
287{
288 struct iwl_mvm_mac_iface_iterator_data *data = _data;
289 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Ilan Peer6e97b0d2013-12-23 22:18:02 +0200290
291 /* Iterator may already find the interface being added -- skip it */
292 if (vif == data->vif) {
293 data->found_vif = true;
294 return;
295 }
296
Ilan Peer6e97b0d2013-12-23 22:18:02 +0200297 /* Mark MAC IDs as used by clearing the available bit, and
298 * (below) mark TSFs as used if their existing use is not
299 * compatible with the new interface type.
300 * No locking or atomic bit operations are needed since the
301 * data is on the stack of the caller function.
302 */
303 __clear_bit(mvmvif->id, data->available_mac_ids);
304
305 /* find a suitable tsf_id */
306 iwl_mvm_mac_tsf_id_iter(_data, mac, vif);
307}
308
Ilan Peer6e97b0d2013-12-23 22:18:02 +0200309void iwl_mvm_mac_ctxt_recalc_tsf_id(struct iwl_mvm *mvm,
310 struct ieee80211_vif *vif)
311{
312 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
313 struct iwl_mvm_mac_iface_iterator_data data = {
314 .mvm = mvm,
315 .vif = vif,
316 .available_tsf_ids = { (1 << NUM_TSF_IDS) - 1 },
317 /* no preference yet */
318 .preferred_tsf = NUM_TSF_IDS,
319 };
320
321 ieee80211_iterate_active_interfaces_atomic(
322 mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
323 iwl_mvm_mac_tsf_id_iter, &data);
324
325 if (data.preferred_tsf != NUM_TSF_IDS)
326 mvmvif->tsf_id = data.preferred_tsf;
327 else if (!test_bit(mvmvif->tsf_id, data.available_tsf_ids))
328 mvmvif->tsf_id = find_first_bit(data.available_tsf_ids,
329 NUM_TSF_IDS);
330}
331
Johannes Berg8ca151b2013-01-24 14:25:36 +0100332static int iwl_mvm_mac_ctxt_allocate_resources(struct iwl_mvm *mvm,
333 struct ieee80211_vif *vif)
334{
335 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
336 struct iwl_mvm_mac_iface_iterator_data data = {
337 .mvm = mvm,
338 .vif = vif,
339 .available_mac_ids = { (1 << NUM_MAC_INDEX_DRIVER) - 1 },
340 .available_tsf_ids = { (1 << NUM_TSF_IDS) - 1 },
341 /* no preference yet */
342 .preferred_tsf = NUM_TSF_IDS,
Johannes Berg8ca151b2013-01-24 14:25:36 +0100343 .found_vif = false,
344 };
345 u32 ac;
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +0300346 int ret, i;
Johannes Berg110cf812014-08-01 23:14:24 +0200347 unsigned long used_hw_queues;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100348
349 /*
350 * Allocate a MAC ID and a TSF for this MAC, along with the queues
351 * and other resources.
352 */
353
354 /*
355 * Before the iterator, we start with all MAC IDs and TSFs available.
356 *
357 * During iteration, all MAC IDs are cleared that are in use by other
358 * virtual interfaces, and all TSF IDs are cleared that can't be used
359 * by this new virtual interface because they're used by an interface
360 * that can't share it with the new one.
361 * At the same time, we check if there's a preferred TSF in the case
362 * that we should share it with another interface.
363 */
364
Johannes Berg5023d962013-07-31 14:07:43 +0200365 /* Currently, MAC ID 0 should be used only for the managed/IBSS vif */
366 switch (vif->type) {
367 case NL80211_IFTYPE_ADHOC:
368 break;
369 case NL80211_IFTYPE_STATION:
370 if (!vif->p2p)
371 break;
372 /* fall through */
373 default:
Ilan Peerec8b6882013-02-13 13:27:18 +0200374 __clear_bit(0, data.available_mac_ids);
Johannes Berg5023d962013-07-31 14:07:43 +0200375 }
Ilan Peerec8b6882013-02-13 13:27:18 +0200376
Johannes Berg8ca151b2013-01-24 14:25:36 +0100377 ieee80211_iterate_active_interfaces_atomic(
378 mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
379 iwl_mvm_mac_iface_iterator, &data);
380
Arik Nemtsova74346d2014-09-21 19:07:30 +0300381 used_hw_queues = iwl_mvm_get_used_hw_queues(mvm, vif);
382
Johannes Berg8ca151b2013-01-24 14:25:36 +0100383 /*
384 * In the case we're getting here during resume, it's similar to
385 * firmware restart, and with RESUME_ALL the iterator will find
386 * the vif being added already.
387 * We don't want to reassign any IDs in either case since doing
388 * so would probably assign different IDs (as interfaces aren't
389 * necessarily added in the same order), but the old IDs were
390 * preserved anyway, so skip ID assignment for both resume and
391 * recovery.
392 */
393 if (data.found_vif)
394 return 0;
395
396 /* Therefore, in recovery, we can't get here */
Ilan Peerfd11bd02013-06-16 12:18:11 +0300397 if (WARN_ON_ONCE(test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)))
398 return -EBUSY;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100399
400 mvmvif->id = find_first_bit(data.available_mac_ids,
401 NUM_MAC_INDEX_DRIVER);
402 if (mvmvif->id == NUM_MAC_INDEX_DRIVER) {
403 IWL_ERR(mvm, "Failed to init MAC context - no free ID!\n");
404 ret = -EIO;
405 goto exit_fail;
406 }
407
408 if (data.preferred_tsf != NUM_TSF_IDS)
409 mvmvif->tsf_id = data.preferred_tsf;
410 else
411 mvmvif->tsf_id = find_first_bit(data.available_tsf_ids,
412 NUM_TSF_IDS);
413 if (mvmvif->tsf_id == NUM_TSF_IDS) {
414 IWL_ERR(mvm, "Failed to init MAC context - no free TSF!\n");
415 ret = -EIO;
416 goto exit_fail;
417 }
418
419 mvmvif->color = 0;
420
Ilan Peer1e849c92013-02-13 12:26:39 +0200421 INIT_LIST_HEAD(&mvmvif->time_event_data.list);
422 mvmvif->time_event_data.id = TE_MAX;
423
Johannes Berg8ca151b2013-01-24 14:25:36 +0100424 /* No need to allocate data queues to P2P Device MAC.*/
425 if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
426 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
427 vif->hw_queue[ac] = IEEE80211_INVAL_HW_QUEUE;
428
429 return 0;
430 }
431
Liad Kaufman24afba72015-07-28 18:56:08 +0300432 /*
433 * Find available queues, and allocate them to the ACs. When in
434 * DQA-mode they aren't really used, and this is done only so the
435 * mac80211 ieee80211_check_queues() function won't fail
436 */
Johannes Berg8ca151b2013-01-24 14:25:36 +0100437 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
Johannes Berg110cf812014-08-01 23:14:24 +0200438 u8 queue = find_first_zero_bit(&used_hw_queues,
Eytan Lifshitz19e737c2013-09-09 13:30:15 +0200439 mvm->first_agg_queue);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100440
Liad Kaufman24afba72015-07-28 18:56:08 +0300441 if (!iwl_mvm_is_dqa_supported(mvm) &&
442 queue >= mvm->first_agg_queue) {
Johannes Berg8ca151b2013-01-24 14:25:36 +0100443 IWL_ERR(mvm, "Failed to allocate queue\n");
444 ret = -EIO;
445 goto exit_fail;
446 }
447
Johannes Berg110cf812014-08-01 23:14:24 +0200448 __set_bit(queue, &used_hw_queues);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100449 vif->hw_queue[ac] = queue;
450 }
451
452 /* Allocate the CAB queue for softAP and GO interfaces */
453 if (vif->type == NL80211_IFTYPE_AP) {
Liad Kaufman0e0e4422015-08-04 15:13:38 +0300454 u8 queue;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100455
Liad Kaufman0e0e4422015-08-04 15:13:38 +0300456 if (!iwl_mvm_is_dqa_supported(mvm)) {
457 queue = find_first_zero_bit(&used_hw_queues,
458 mvm->first_agg_queue);
459
460 if (queue >= mvm->first_agg_queue) {
461 IWL_ERR(mvm, "Failed to allocate cab queue\n");
462 ret = -EIO;
463 goto exit_fail;
464 }
465 } else {
466 queue = IWL_MVM_DQA_GCAST_QUEUE;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100467 }
468
469 vif->cab_queue = queue;
470 } else {
471 vif->cab_queue = IEEE80211_INVAL_HW_QUEUE;
472 }
473
474 mvmvif->bcast_sta.sta_id = IWL_MVM_STATION_COUNT;
475 mvmvif->ap_sta_id = IWL_MVM_STATION_COUNT;
476
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +0300477 for (i = 0; i < NUM_IWL_MVM_SMPS_REQ; i++)
478 mvmvif->smps_requests[i] = IEEE80211_SMPS_AUTOMATIC;
479
Johannes Berg8ca151b2013-01-24 14:25:36 +0100480 return 0;
481
482exit_fail:
483 memset(mvmvif, 0, sizeof(struct iwl_mvm_vif));
484 memset(vif->hw_queue, IEEE80211_INVAL_HW_QUEUE, sizeof(vif->hw_queue));
485 vif->cab_queue = IEEE80211_INVAL_HW_QUEUE;
486 return ret;
487}
488
489int iwl_mvm_mac_ctxt_init(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
490{
Emmanuel Grumbach5d42e7b2015-03-19 20:04:51 +0200491 unsigned int wdg_timeout =
492 iwl_mvm_get_wd_timeout(mvm, vif, false, false);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100493 u32 ac;
494 int ret;
495
496 lockdep_assert_held(&mvm->mutex);
497
498 ret = iwl_mvm_mac_ctxt_allocate_resources(mvm, vif);
499 if (ret)
500 return ret;
501
502 switch (vif->type) {
503 case NL80211_IFTYPE_P2P_DEVICE:
Avri Altman3edf8ff2014-07-30 11:41:01 +0300504 iwl_mvm_enable_ac_txq(mvm, IWL_MVM_OFFCHANNEL_QUEUE,
Liad Kaufman4ecafae2015-07-14 13:36:18 +0300505 IWL_MVM_OFFCHANNEL_QUEUE,
Liad Kaufman5c1156e2015-07-22 17:59:53 +0300506 IWL_MVM_TX_FIFO_VO, 0, wdg_timeout);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100507 break;
508 case NL80211_IFTYPE_AP:
Liad Kaufman4ecafae2015-07-14 13:36:18 +0300509 iwl_mvm_enable_ac_txq(mvm, vif->cab_queue, vif->cab_queue,
Liad Kaufman5c1156e2015-07-22 17:59:53 +0300510 IWL_MVM_TX_FIFO_MCAST, 0, wdg_timeout);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100511 /* fall through */
512 default:
Liad Kaufman24afba72015-07-28 18:56:08 +0300513 /* If DQA is supported - queues will be enabled when needed */
514 if (iwl_mvm_is_dqa_supported(mvm))
515 break;
516
Johannes Berg8ca151b2013-01-24 14:25:36 +0100517 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
Avri Altman3edf8ff2014-07-30 11:41:01 +0300518 iwl_mvm_enable_ac_txq(mvm, vif->hw_queue[ac],
Liad Kaufman4ecafae2015-07-14 13:36:18 +0300519 vif->hw_queue[ac],
Liad Kaufman5c1156e2015-07-22 17:59:53 +0300520 iwl_mvm_ac_to_tx_fifo[ac], 0,
Emmanuel Grumbach4cf677f2015-01-12 14:38:29 +0200521 wdg_timeout);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100522 break;
523 }
524
525 return 0;
526}
527
528void iwl_mvm_mac_ctxt_release(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
529{
530 int ac;
531
532 lockdep_assert_held(&mvm->mutex);
533
534 switch (vif->type) {
535 case NL80211_IFTYPE_P2P_DEVICE:
Liad Kaufman4ecafae2015-07-14 13:36:18 +0300536 iwl_mvm_disable_txq(mvm, IWL_MVM_OFFCHANNEL_QUEUE,
537 IWL_MVM_OFFCHANNEL_QUEUE, IWL_MAX_TID_COUNT,
538 0);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100539 break;
540 case NL80211_IFTYPE_AP:
Liad Kaufman4ecafae2015-07-14 13:36:18 +0300541 iwl_mvm_disable_txq(mvm, vif->cab_queue, vif->cab_queue,
542 IWL_MAX_TID_COUNT, 0);
Liad Kaufmande24f632015-08-04 15:19:18 +0300543
544 if (iwl_mvm_is_dqa_supported(mvm))
545 iwl_mvm_disable_txq(mvm,
546 IWL_MVM_DQA_AP_PROBE_RESP_QUEUE,
547 vif->hw_queue[0], IWL_MAX_TID_COUNT,
548 0);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100549 /* fall through */
550 default:
Liad Kaufman24afba72015-07-28 18:56:08 +0300551 /*
552 * If DQA is supported - queues were already disabled, since in
553 * DQA-mode the queues are a property of the STA and not of the
554 * vif, and at this point the STA was already deleted
555 */
556 if (iwl_mvm_is_dqa_supported(mvm))
557 break;
558
Johannes Berg8ca151b2013-01-24 14:25:36 +0100559 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
Liad Kaufman4ecafae2015-07-14 13:36:18 +0300560 iwl_mvm_disable_txq(mvm, vif->hw_queue[ac],
561 vif->hw_queue[ac],
562 IWL_MAX_TID_COUNT, 0);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100563 }
564}
565
566static void iwl_mvm_ack_rates(struct iwl_mvm *mvm,
567 struct ieee80211_vif *vif,
Johannes Berg57fbcce2016-04-12 15:56:15 +0200568 enum nl80211_band band,
Johannes Berg8ca151b2013-01-24 14:25:36 +0100569 u8 *cck_rates, u8 *ofdm_rates)
570{
571 struct ieee80211_supported_band *sband;
572 unsigned long basic = vif->bss_conf.basic_rates;
573 int lowest_present_ofdm = 100;
574 int lowest_present_cck = 100;
575 u8 cck = 0;
576 u8 ofdm = 0;
577 int i;
578
579 sband = mvm->hw->wiphy->bands[band];
580
581 for_each_set_bit(i, &basic, BITS_PER_LONG) {
582 int hw = sband->bitrates[i].hw_value;
583 if (hw >= IWL_FIRST_OFDM_RATE) {
584 ofdm |= BIT(hw - IWL_FIRST_OFDM_RATE);
585 if (lowest_present_ofdm > hw)
586 lowest_present_ofdm = hw;
587 } else {
588 BUILD_BUG_ON(IWL_FIRST_CCK_RATE != 0);
589
590 cck |= BIT(hw);
591 if (lowest_present_cck > hw)
592 lowest_present_cck = hw;
593 }
594 }
595
596 /*
597 * Now we've got the basic rates as bitmaps in the ofdm and cck
598 * variables. This isn't sufficient though, as there might not
599 * be all the right rates in the bitmap. E.g. if the only basic
600 * rates are 5.5 Mbps and 11 Mbps, we still need to add 1 Mbps
601 * and 6 Mbps because the 802.11-2007 standard says in 9.6:
602 *
603 * [...] a STA responding to a received frame shall transmit
604 * its Control Response frame [...] at the highest rate in the
605 * BSSBasicRateSet parameter that is less than or equal to the
606 * rate of the immediately previous frame in the frame exchange
607 * sequence ([...]) and that is of the same modulation class
608 * ([...]) as the received frame. If no rate contained in the
609 * BSSBasicRateSet parameter meets these conditions, then the
610 * control frame sent in response to a received frame shall be
611 * transmitted at the highest mandatory rate of the PHY that is
612 * less than or equal to the rate of the received frame, and
613 * that is of the same modulation class as the received frame.
614 *
615 * As a consequence, we need to add all mandatory rates that are
616 * lower than all of the basic rates to these bitmaps.
617 */
618
619 if (IWL_RATE_24M_INDEX < lowest_present_ofdm)
620 ofdm |= IWL_RATE_BIT_MSK(24) >> IWL_FIRST_OFDM_RATE;
621 if (IWL_RATE_12M_INDEX < lowest_present_ofdm)
622 ofdm |= IWL_RATE_BIT_MSK(12) >> IWL_FIRST_OFDM_RATE;
623 /* 6M already there or needed so always add */
624 ofdm |= IWL_RATE_BIT_MSK(6) >> IWL_FIRST_OFDM_RATE;
625
626 /*
627 * CCK is a bit more complex with DSSS vs. HR/DSSS vs. ERP.
628 * Note, however:
629 * - if no CCK rates are basic, it must be ERP since there must
630 * be some basic rates at all, so they're OFDM => ERP PHY
631 * (or we're in 5 GHz, and the cck bitmap will never be used)
632 * - if 11M is a basic rate, it must be ERP as well, so add 5.5M
633 * - if 5.5M is basic, 1M and 2M are mandatory
634 * - if 2M is basic, 1M is mandatory
635 * - if 1M is basic, that's the only valid ACK rate.
636 * As a consequence, it's not as complicated as it sounds, just add
637 * any lower rates to the ACK rate bitmap.
638 */
639 if (IWL_RATE_11M_INDEX < lowest_present_cck)
640 cck |= IWL_RATE_BIT_MSK(11) >> IWL_FIRST_CCK_RATE;
641 if (IWL_RATE_5M_INDEX < lowest_present_cck)
642 cck |= IWL_RATE_BIT_MSK(5) >> IWL_FIRST_CCK_RATE;
643 if (IWL_RATE_2M_INDEX < lowest_present_cck)
644 cck |= IWL_RATE_BIT_MSK(2) >> IWL_FIRST_CCK_RATE;
645 /* 1M already there or needed so always add */
646 cck |= IWL_RATE_BIT_MSK(1) >> IWL_FIRST_CCK_RATE;
647
648 *cck_rates = cck;
649 *ofdm_rates = ofdm;
650}
651
Avri Altman8a5e3662013-11-12 19:16:03 +0200652static void iwl_mvm_mac_ctxt_set_ht_flags(struct iwl_mvm *mvm,
653 struct ieee80211_vif *vif,
654 struct iwl_mac_ctx_cmd *cmd)
655{
656 /* for both sta and ap, ht_operation_mode hold the protection_mode */
657 u8 protection_mode = vif->bss_conf.ht_operation_mode &
658 IEEE80211_HT_OP_MODE_PROTECTION;
659 /* The fw does not distinguish between ht and fat */
660 u32 ht_flag = MAC_PROT_FLG_HT_PROT | MAC_PROT_FLG_FAT_PROT;
661
662 IWL_DEBUG_RATE(mvm, "protection mode set to %d\n", protection_mode);
663 /*
664 * See section 9.23.3.1 of IEEE 80211-2012.
665 * Nongreenfield HT STAs Present is not supported.
666 */
667 switch (protection_mode) {
668 case IEEE80211_HT_OP_MODE_PROTECTION_NONE:
669 break;
670 case IEEE80211_HT_OP_MODE_PROTECTION_NONMEMBER:
671 case IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED:
672 cmd->protection_flags |= cpu_to_le32(ht_flag);
673 break;
674 case IEEE80211_HT_OP_MODE_PROTECTION_20MHZ:
675 /* Protect when channel wider than 20MHz */
676 if (vif->bss_conf.chandef.width > NL80211_CHAN_WIDTH_20)
677 cmd->protection_flags |= cpu_to_le32(ht_flag);
678 break;
679 default:
680 IWL_ERR(mvm, "Illegal protection mode %d\n",
681 protection_mode);
682 break;
683 }
684}
685
Johannes Berg8ca151b2013-01-24 14:25:36 +0100686static void iwl_mvm_mac_ctxt_cmd_common(struct iwl_mvm *mvm,
687 struct ieee80211_vif *vif,
688 struct iwl_mac_ctx_cmd *cmd,
Johannes Berg3dfd3a92014-08-11 21:37:30 +0200689 const u8 *bssid_override,
Johannes Berg8ca151b2013-01-24 14:25:36 +0100690 u32 action)
691{
692 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
693 struct ieee80211_chanctx_conf *chanctx;
Avri Altman8a5e3662013-11-12 19:16:03 +0200694 bool ht_enabled = !!(vif->bss_conf.ht_operation_mode &
695 IEEE80211_HT_OP_MODE_PROTECTION);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100696 u8 cck_ack_rates, ofdm_ack_rates;
Johannes Berg3dfd3a92014-08-11 21:37:30 +0200697 const u8 *bssid = bssid_override ?: vif->bss_conf.bssid;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100698 int i;
699
700 cmd->id_and_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
701 mvmvif->color));
702 cmd->action = cpu_to_le32(action);
703
704 switch (vif->type) {
705 case NL80211_IFTYPE_STATION:
706 if (vif->p2p)
707 cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_P2P_STA);
708 else
709 cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_BSS_STA);
710 break;
711 case NL80211_IFTYPE_AP:
712 cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_GO);
713 break;
714 case NL80211_IFTYPE_MONITOR:
715 cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_LISTENER);
716 break;
717 case NL80211_IFTYPE_P2P_DEVICE:
718 cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_P2P_DEVICE);
719 break;
720 case NL80211_IFTYPE_ADHOC:
721 cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_IBSS);
722 break;
723 default:
724 WARN_ON_ONCE(1);
725 }
726
727 cmd->tsf_id = cpu_to_le32(mvmvif->tsf_id);
728
729 memcpy(cmd->node_addr, vif->addr, ETH_ALEN);
Johannes Berg3dfd3a92014-08-11 21:37:30 +0200730
731 if (bssid)
732 memcpy(cmd->bssid_addr, bssid, ETH_ALEN);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100733 else
734 eth_broadcast_addr(cmd->bssid_addr);
735
736 rcu_read_lock();
737 chanctx = rcu_dereference(vif->chanctx_conf);
738 iwl_mvm_ack_rates(mvm, vif, chanctx ? chanctx->def.chan->band
Johannes Berg57fbcce2016-04-12 15:56:15 +0200739 : NL80211_BAND_2GHZ,
Johannes Berg8ca151b2013-01-24 14:25:36 +0100740 &cck_ack_rates, &ofdm_ack_rates);
741 rcu_read_unlock();
742
743 cmd->cck_rates = cpu_to_le32((u32)cck_ack_rates);
744 cmd->ofdm_rates = cpu_to_le32((u32)ofdm_ack_rates);
745
746 cmd->cck_short_preamble =
747 cpu_to_le32(vif->bss_conf.use_short_preamble ?
748 MAC_FLG_SHORT_PREAMBLE : 0);
749 cmd->short_slot =
750 cpu_to_le32(vif->bss_conf.use_short_slot ?
751 MAC_FLG_SHORT_SLOT : 0);
752
Ayala Bekerbd6f5bd2015-12-20 09:27:50 +0200753 cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP);
754
Emmanuel Grumbach3dd94792013-12-19 16:50:49 +0200755 for (i = 0; i < IEEE80211_NUM_ACS; i++) {
756 u8 txf = iwl_mvm_ac_to_tx_fifo[i];
757
758 cmd->ac[txf].cw_min =
759 cpu_to_le16(mvmvif->queue_params[i].cw_min);
760 cmd->ac[txf].cw_max =
761 cpu_to_le16(mvmvif->queue_params[i].cw_max);
762 cmd->ac[txf].edca_txop =
Johannes Berg8ca151b2013-01-24 14:25:36 +0100763 cpu_to_le16(mvmvif->queue_params[i].txop * 32);
Emmanuel Grumbach3dd94792013-12-19 16:50:49 +0200764 cmd->ac[txf].aifsn = mvmvif->queue_params[i].aifs;
765 cmd->ac[txf].fifos_mask = BIT(txf);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100766 }
767
Ayala Bekerbd6f5bd2015-12-20 09:27:50 +0200768 if (vif->type == NL80211_IFTYPE_AP) {
769 /* in AP mode, the MCAST FIFO takes the EDCA params from VO */
Emmanuel Grumbach3dd94792013-12-19 16:50:49 +0200770 cmd->ac[IWL_MVM_TX_FIFO_VO].fifos_mask |=
771 BIT(IWL_MVM_TX_FIFO_MCAST);
Emmanuel Grumbach86a91ec2013-05-26 20:47:53 +0300772
Ayala Bekerbd6f5bd2015-12-20 09:27:50 +0200773 /*
774 * in AP mode, pass probe requests and beacons from other APs
775 * (needed for ht protection); when there're no any associated
776 * station don't ask FW to pass beacons to prevent unnecessary
777 * wake-ups.
778 */
779 cmd->filter_flags |= cpu_to_le32(MAC_FILTER_IN_PROBE_REQUEST);
Andrei Otcheretianskic89e3332016-01-26 18:12:28 +0200780 if (mvmvif->ap_assoc_sta_count || !mvm->drop_bcn_ap_mode) {
Ayala Bekerbd6f5bd2015-12-20 09:27:50 +0200781 cmd->filter_flags |= cpu_to_le32(MAC_FILTER_IN_BEACON);
782 IWL_DEBUG_HC(mvm, "Asking FW to pass beacons\n");
783 } else {
784 IWL_DEBUG_HC(mvm, "No need to receive beacons\n");
785 }
786 }
787
Johannes Berg8ca151b2013-01-24 14:25:36 +0100788 if (vif->bss_conf.qos)
789 cmd->qos_flags |= cpu_to_le32(MAC_QOS_FLG_UPDATE_EDCA);
790
Emmanuel Grumbachdc271ee2014-07-03 20:46:35 +0300791 if (vif->bss_conf.use_cts_prot)
Dor Shaishcc7ee2b2013-02-18 13:51:36 +0200792 cmd->protection_flags |= cpu_to_le32(MAC_PROT_FLG_TGG_PROTECT);
Emmanuel Grumbachdc271ee2014-07-03 20:46:35 +0300793
Avri Altman8a5e3662013-11-12 19:16:03 +0200794 IWL_DEBUG_RATE(mvm, "use_cts_prot %d, ht_operation_mode %d\n",
795 vif->bss_conf.use_cts_prot,
796 vif->bss_conf.ht_operation_mode);
797 if (vif->bss_conf.chandef.width != NL80211_CHAN_WIDTH_20_NOHT)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100798 cmd->qos_flags |= cpu_to_le32(MAC_QOS_FLG_TGN);
Avri Altman8a5e3662013-11-12 19:16:03 +0200799 if (ht_enabled)
800 iwl_mvm_mac_ctxt_set_ht_flags(mvm, vif, cmd);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100801}
802
803static int iwl_mvm_mac_ctxt_send_cmd(struct iwl_mvm *mvm,
804 struct iwl_mac_ctx_cmd *cmd)
805{
Emmanuel Grumbacha1022922014-05-12 11:36:41 +0300806 int ret = iwl_mvm_send_cmd_pdu(mvm, MAC_CONTEXT_CMD, 0,
Johannes Berg8ca151b2013-01-24 14:25:36 +0100807 sizeof(*cmd), cmd);
808 if (ret)
809 IWL_ERR(mvm, "Failed to send MAC context (action:%d): %d\n",
810 le32_to_cpu(cmd->action), ret);
811 return ret;
812}
813
Luciano Coelhocf520232014-05-13 16:52:54 +0300814static int iwl_mvm_mac_ctxt_cmd_sta(struct iwl_mvm *mvm,
815 struct ieee80211_vif *vif,
Johannes Berg3dfd3a92014-08-11 21:37:30 +0200816 u32 action, bool force_assoc_off,
817 const u8 *bssid_override)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100818{
Luciano Coelhocf520232014-05-13 16:52:54 +0300819 struct iwl_mac_ctx_cmd cmd = {};
820 struct iwl_mac_data_sta *ctxt_sta;
821
822 WARN_ON(vif->type != NL80211_IFTYPE_STATION);
823
824 /* Fill the common data for all mac context types */
Johannes Berg3dfd3a92014-08-11 21:37:30 +0200825 iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, bssid_override, action);
Luciano Coelhocf520232014-05-13 16:52:54 +0300826
827 if (vif->p2p) {
828 struct ieee80211_p2p_noa_attr *noa =
829 &vif->bss_conf.p2p_noa_attr;
830
831 cmd.p2p_sta.ctwin = cpu_to_le32(noa->oppps_ctwindow &
832 IEEE80211_P2P_OPPPS_CTWINDOW_MASK);
833 ctxt_sta = &cmd.p2p_sta.sta;
834 } else {
Luciano Coelhocf520232014-05-13 16:52:54 +0300835 ctxt_sta = &cmd.sta;
836 }
837
Johannes Berg210a5442013-01-24 23:48:23 +0100838 /* We need the dtim_period to set the MAC as associated */
Alexander Bondarba283922013-05-02 16:34:48 +0300839 if (vif->bss_conf.assoc && vif->bss_conf.dtim_period &&
840 !force_assoc_off) {
Johannes Bergd2931bb2013-02-05 18:10:04 +0100841 u32 dtim_offs;
842
843 /*
844 * The DTIM count counts down, so when it is N that means N
845 * more beacon intervals happen until the DTIM TBTT. Therefore
846 * add this to the current time. If that ends up being in the
847 * future, the firmware will handle it.
848 *
849 * Also note that the system_timestamp (which we get here as
850 * "sync_device_ts") and TSF timestamp aren't at exactly the
851 * same offset in the frame -- the TSF is at the first symbol
852 * of the TSF, the system timestamp is at signal acquisition
853 * time. This means there's an offset between them of at most
854 * a few hundred microseconds (24 * 8 bits + PLCP time gives
855 * 384us in the longest case), this is currently not relevant
856 * as the firmware wakes up around 2ms before the TBTT.
857 */
858 dtim_offs = vif->bss_conf.sync_dtim_count *
859 vif->bss_conf.beacon_int;
860 /* convert TU to usecs */
861 dtim_offs *= 1024;
862
863 ctxt_sta->dtim_tsf =
864 cpu_to_le64(vif->bss_conf.sync_tsf + dtim_offs);
865 ctxt_sta->dtim_time =
866 cpu_to_le32(vif->bss_conf.sync_device_ts + dtim_offs);
867
868 IWL_DEBUG_INFO(mvm, "DTIM TBTT is 0x%llx/0x%x, offset %d\n",
869 le64_to_cpu(ctxt_sta->dtim_tsf),
870 le32_to_cpu(ctxt_sta->dtim_time),
871 dtim_offs);
872
Johannes Berg210a5442013-01-24 23:48:23 +0100873 ctxt_sta->is_assoc = cpu_to_le32(1);
Johannes Bergd2931bb2013-02-05 18:10:04 +0100874 } else {
Johannes Berg210a5442013-01-24 23:48:23 +0100875 ctxt_sta->is_assoc = cpu_to_le32(0);
Luciano Coelho7c8b3bc2014-07-30 15:04:08 +0300876
877 /* Allow beacons to pass through as long as we are not
878 * associated, or we do not have dtim period information.
879 */
880 cmd.filter_flags |= cpu_to_le32(MAC_FILTER_IN_BEACON);
Johannes Bergd2931bb2013-02-05 18:10:04 +0100881 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100882
883 ctxt_sta->bi = cpu_to_le32(vif->bss_conf.beacon_int);
884 ctxt_sta->bi_reciprocal =
885 cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int));
886 ctxt_sta->dtim_interval = cpu_to_le32(vif->bss_conf.beacon_int *
887 vif->bss_conf.dtim_period);
888 ctxt_sta->dtim_reciprocal =
889 cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int *
890 vif->bss_conf.dtim_period));
891
892 ctxt_sta->listen_interval = cpu_to_le32(mvm->hw->conf.listen_interval);
893 ctxt_sta->assoc_id = cpu_to_le32(vif->bss_conf.aid);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100894
Andrei Otcheretianskieffd1922015-06-30 12:08:28 +0300895 if (vif->probe_req_reg && vif->bss_conf.assoc && vif->p2p)
896 cmd.filter_flags |= cpu_to_le32(MAC_FILTER_IN_PROBE_REQUEST);
897
Johannes Berg8ca151b2013-01-24 14:25:36 +0100898 return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
899}
900
901static int iwl_mvm_mac_ctxt_cmd_listener(struct iwl_mvm *mvm,
902 struct ieee80211_vif *vif,
903 u32 action)
904{
905 struct iwl_mac_ctx_cmd cmd = {};
Chaya Rachel Ivgi0e39eb02015-12-03 15:51:46 +0200906 u32 tfd_queue_msk = 0;
907 int ret, i;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100908
909 WARN_ON(vif->type != NL80211_IFTYPE_MONITOR);
910
Johannes Berg3dfd3a92014-08-11 21:37:30 +0200911 iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
Johannes Berg53585492013-03-18 13:04:50 +0100912
Chaya Rachel Ivgi0e39eb02015-12-03 15:51:46 +0200913 for (i = 0; i < IEEE80211_NUM_ACS; i++)
914 if (vif->hw_queue[i] != IEEE80211_INVAL_HW_QUEUE)
915 tfd_queue_msk |= BIT(vif->hw_queue[i]);
916
Johannes Berg53585492013-03-18 13:04:50 +0100917 cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_PROMISC |
918 MAC_FILTER_IN_CONTROL_AND_MGMT |
919 MAC_FILTER_IN_BEACON |
Johannes Bergfb8b8ee2013-10-21 12:37:53 +0200920 MAC_FILTER_IN_PROBE_REQUEST |
921 MAC_FILTER_IN_CRC32);
Johannes Berg30686bf2015-06-02 21:39:54 +0200922 ieee80211_hw_set(mvm->hw, RX_INCLUDES_FCS);
Johannes Berg53585492013-03-18 13:04:50 +0100923
Chaya Rachel Ivgi0e39eb02015-12-03 15:51:46 +0200924 /* Allocate sniffer station */
925 ret = iwl_mvm_allocate_int_sta(mvm, &mvm->snif_sta, tfd_queue_msk,
926 vif->type);
927 if (ret)
928 return ret;
929
Johannes Berg8ca151b2013-01-24 14:25:36 +0100930 return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
931}
932
Johannes Berg5023d962013-07-31 14:07:43 +0200933static int iwl_mvm_mac_ctxt_cmd_ibss(struct iwl_mvm *mvm,
934 struct ieee80211_vif *vif,
935 u32 action)
936{
937 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
938 struct iwl_mac_ctx_cmd cmd = {};
939
940 WARN_ON(vif->type != NL80211_IFTYPE_ADHOC);
941
Johannes Berg3dfd3a92014-08-11 21:37:30 +0200942 iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
Johannes Berg5023d962013-07-31 14:07:43 +0200943
944 cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_BEACON |
945 MAC_FILTER_IN_PROBE_REQUEST);
946
947 /* cmd.ibss.beacon_time/cmd.ibss.beacon_tsf are curently ignored */
948 cmd.ibss.bi = cpu_to_le32(vif->bss_conf.beacon_int);
949 cmd.ibss.bi_reciprocal =
950 cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int));
951
952 /* TODO: Assumes that the beacon id == mac context id */
953 cmd.ibss.beacon_template = cpu_to_le32(mvmvif->id);
954
955 return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
956}
957
Johannes Berg8ca151b2013-01-24 14:25:36 +0100958struct iwl_mvm_go_iterator_data {
959 bool go_active;
960};
961
962static void iwl_mvm_go_iterator(void *_data, u8 *mac, struct ieee80211_vif *vif)
963{
964 struct iwl_mvm_go_iterator_data *data = _data;
965 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
966
Johannes Berg5023d962013-07-31 14:07:43 +0200967 if (vif->type == NL80211_IFTYPE_AP && vif->p2p &&
968 mvmvif->ap_ibss_active)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100969 data->go_active = true;
970}
971
972static int iwl_mvm_mac_ctxt_cmd_p2p_device(struct iwl_mvm *mvm,
973 struct ieee80211_vif *vif,
974 u32 action)
975{
976 struct iwl_mac_ctx_cmd cmd = {};
977 struct iwl_mvm_go_iterator_data data = {};
978
979 WARN_ON(vif->type != NL80211_IFTYPE_P2P_DEVICE);
980
Johannes Berg3dfd3a92014-08-11 21:37:30 +0200981 iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100982
983 cmd.protection_flags |= cpu_to_le32(MAC_PROT_FLG_TGG_PROTECT);
Ilan Peer1dcd15e2013-02-14 14:25:24 +0200984
985 /* Override the filter flags to accept only probe requests */
986 cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_PROBE_REQUEST);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100987
988 /*
989 * This flag should be set to true when the P2P Device is
990 * discoverable and there is at least another active P2P GO. Settings
991 * this flag will allow the P2P Device to be discoverable on other
992 * channels in addition to its listen channel.
993 * Note that this flag should not be set in other cases as it opens the
994 * Rx filters on all MAC and increases the number of interrupts.
995 */
996 ieee80211_iterate_active_interfaces_atomic(
997 mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
998 iwl_mvm_go_iterator, &data);
999
1000 cmd.p2p_dev.is_disc_extended = cpu_to_le32(data.go_active ? 1 : 0);
1001 return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
1002}
1003
1004static void iwl_mvm_mac_ctxt_set_tim(struct iwl_mvm *mvm,
1005 struct iwl_mac_beacon_cmd *beacon_cmd,
1006 u8 *beacon, u32 frame_size)
1007{
1008 u32 tim_idx;
1009 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)beacon;
1010
1011 /* The index is relative to frame start but we start looking at the
1012 * variable-length part of the beacon. */
1013 tim_idx = mgmt->u.beacon.variable - beacon;
1014
1015 /* Parse variable-length elements of beacon to find WLAN_EID_TIM */
1016 while ((tim_idx < (frame_size - 2)) &&
1017 (beacon[tim_idx] != WLAN_EID_TIM))
1018 tim_idx += beacon[tim_idx+1] + 2;
1019
1020 /* If TIM field was found, set variables */
1021 if ((tim_idx < (frame_size - 1)) && (beacon[tim_idx] == WLAN_EID_TIM)) {
1022 beacon_cmd->tim_idx = cpu_to_le32(tim_idx);
1023 beacon_cmd->tim_size = cpu_to_le32((u32)beacon[tim_idx+1]);
1024 } else {
1025 IWL_WARN(mvm, "Unable to find TIM Element in beacon\n");
1026 }
1027}
1028
1029static int iwl_mvm_mac_ctxt_send_beacon(struct iwl_mvm *mvm,
1030 struct ieee80211_vif *vif,
1031 struct sk_buff *beacon)
1032{
1033 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1034 struct iwl_host_cmd cmd = {
1035 .id = BEACON_TEMPLATE_CMD,
1036 .flags = CMD_ASYNC,
1037 };
1038 struct iwl_mac_beacon_cmd beacon_cmd = {};
1039 struct ieee80211_tx_info *info;
1040 u32 beacon_skb_len;
Emmanuel Grumbach75f6b9b2014-06-02 10:03:33 +03001041 u32 rate, tx_flags;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001042
1043 if (WARN_ON(!beacon))
1044 return -EINVAL;
1045
1046 beacon_skb_len = beacon->len;
1047
1048 /* TODO: for now the beacon template id is set to be the mac context id.
1049 * Might be better to handle it as another resource ... */
1050 beacon_cmd.template_id = cpu_to_le32((u32)mvmvif->id);
Emmanuel Grumbach75f6b9b2014-06-02 10:03:33 +03001051 info = IEEE80211_SKB_CB(beacon);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001052
1053 /* Set up TX command fields */
1054 beacon_cmd.tx.len = cpu_to_le16((u16)beacon_skb_len);
1055 beacon_cmd.tx.sta_id = mvmvif->bcast_sta.sta_id;
1056 beacon_cmd.tx.life_time = cpu_to_le32(TX_CMD_LIFE_TIME_INFINITE);
Emmanuel Grumbach75f6b9b2014-06-02 10:03:33 +03001057 tx_flags = TX_CMD_FLG_SEQ_CTL | TX_CMD_FLG_TSF;
1058 tx_flags |=
1059 iwl_mvm_bt_coex_tx_prio(mvm, (void *)beacon->data, info, 0) <<
1060 TX_CMD_FLG_BT_PRIO_POS;
1061 beacon_cmd.tx.tx_flags = cpu_to_le32(tx_flags);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001062
Emmanuel Grumbach1e3c3c32015-12-13 09:35:30 +02001063 if (!fw_has_capa(&mvm->fw->ucode_capa,
1064 IWL_UCODE_TLV_CAPA_BEACON_ANT_SELECTION)) {
1065 mvm->mgmt_last_antenna_idx =
1066 iwl_mvm_next_antenna(mvm, iwl_mvm_get_valid_tx_ant(mvm),
1067 mvm->mgmt_last_antenna_idx);
1068 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01001069
1070 beacon_cmd.tx.rate_n_flags =
1071 cpu_to_le32(BIT(mvm->mgmt_last_antenna_idx) <<
1072 RATE_MCS_ANT_POS);
1073
Johannes Berg57fbcce2016-04-12 15:56:15 +02001074 if (info->band == NL80211_BAND_5GHZ || vif->p2p) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001075 rate = IWL_FIRST_OFDM_RATE;
1076 } else {
1077 rate = IWL_FIRST_CCK_RATE;
1078 beacon_cmd.tx.rate_n_flags |= cpu_to_le32(RATE_MCS_CCK_MSK);
1079 }
1080 beacon_cmd.tx.rate_n_flags |=
1081 cpu_to_le32(iwl_mvm_mac80211_idx_to_hwrate(rate));
1082
1083 /* Set up TX beacon command fields */
Johannes Berg5023d962013-07-31 14:07:43 +02001084 if (vif->type == NL80211_IFTYPE_AP)
1085 iwl_mvm_mac_ctxt_set_tim(mvm, &beacon_cmd,
1086 beacon->data,
1087 beacon_skb_len);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001088
1089 /* Submit command */
1090 cmd.len[0] = sizeof(beacon_cmd);
1091 cmd.data[0] = &beacon_cmd;
1092 cmd.dataflags[0] = 0;
1093 cmd.len[1] = beacon_skb_len;
1094 cmd.data[1] = beacon->data;
1095 cmd.dataflags[1] = IWL_HCMD_DFL_DUP;
1096
1097 return iwl_mvm_send_cmd(mvm, &cmd);
1098}
1099
Johannes Berg5023d962013-07-31 14:07:43 +02001100/* The beacon template for the AP/GO/IBSS has changed and needs update */
Johannes Berg8ca151b2013-01-24 14:25:36 +01001101int iwl_mvm_mac_ctxt_beacon_changed(struct iwl_mvm *mvm,
1102 struct ieee80211_vif *vif)
1103{
1104 struct sk_buff *beacon;
1105 int ret;
1106
Johannes Berg5023d962013-07-31 14:07:43 +02001107 WARN_ON(vif->type != NL80211_IFTYPE_AP &&
1108 vif->type != NL80211_IFTYPE_ADHOC);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001109
Andrei Otcheretianskife887662014-04-10 21:46:16 +03001110 beacon = ieee80211_beacon_get_template(mvm->hw, vif, NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001111 if (!beacon)
1112 return -ENOMEM;
1113
1114 ret = iwl_mvm_mac_ctxt_send_beacon(mvm, vif, beacon);
1115 dev_kfree_skb(beacon);
1116 return ret;
1117}
1118
Ilan Peer3a3cb922013-06-13 08:30:12 +03001119struct iwl_mvm_mac_ap_iterator_data {
1120 struct iwl_mvm *mvm;
1121 struct ieee80211_vif *vif;
1122 u32 beacon_device_ts;
1123 u16 beacon_int;
1124};
1125
1126/* Find the beacon_device_ts and beacon_int for a managed interface */
1127static void iwl_mvm_mac_ap_iterator(void *_data, u8 *mac,
1128 struct ieee80211_vif *vif)
1129{
1130 struct iwl_mvm_mac_ap_iterator_data *data = _data;
1131
1132 if (vif->type != NL80211_IFTYPE_STATION || !vif->bss_conf.assoc)
1133 return;
1134
1135 /* Station client has higher priority over P2P client*/
1136 if (vif->p2p && data->beacon_device_ts)
1137 return;
1138
1139 data->beacon_device_ts = vif->bss_conf.sync_device_ts;
1140 data->beacon_int = vif->bss_conf.beacon_int;
1141}
1142
Johannes Berg8ca151b2013-01-24 14:25:36 +01001143/*
1144 * Fill the specific data for mac context of type AP of P2P GO
1145 */
1146static void iwl_mvm_mac_ctxt_cmd_fill_ap(struct iwl_mvm *mvm,
1147 struct ieee80211_vif *vif,
Johannes Berg506a81e2013-02-28 14:05:14 +01001148 struct iwl_mac_data_ap *ctxt_ap,
1149 bool add)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001150{
1151 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Ilan Peer3a3cb922013-06-13 08:30:12 +03001152 struct iwl_mvm_mac_ap_iterator_data data = {
1153 .mvm = mvm,
1154 .vif = vif,
1155 .beacon_device_ts = 0
1156 };
Johannes Berg8ca151b2013-01-24 14:25:36 +01001157
1158 ctxt_ap->bi = cpu_to_le32(vif->bss_conf.beacon_int);
1159 ctxt_ap->bi_reciprocal =
1160 cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int));
1161 ctxt_ap->dtim_interval = cpu_to_le32(vif->bss_conf.beacon_int *
1162 vif->bss_conf.dtim_period);
1163 ctxt_ap->dtim_reciprocal =
1164 cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int *
1165 vif->bss_conf.dtim_period));
1166
1167 ctxt_ap->mcast_qid = cpu_to_le32(vif->cab_queue);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001168
Johannes Berg506a81e2013-02-28 14:05:14 +01001169 /*
Ilan Peer3a3cb922013-06-13 08:30:12 +03001170 * Only set the beacon time when the MAC is being added, when we
Johannes Berg506a81e2013-02-28 14:05:14 +01001171 * just modify the MAC then we should keep the time -- the firmware
1172 * can otherwise have a "jumping" TBTT.
1173 */
Ilan Peer3a3cb922013-06-13 08:30:12 +03001174 if (add) {
1175 /*
1176 * If there is a station/P2P client interface which is
1177 * associated, set the AP's TBTT far enough from the station's
1178 * TBTT. Otherwise, set it to the current system time
1179 */
1180 ieee80211_iterate_active_interfaces_atomic(
1181 mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
1182 iwl_mvm_mac_ap_iterator, &data);
1183
1184 if (data.beacon_device_ts) {
Ilan Peer9f8f8ca2013-12-24 17:17:05 +02001185 u32 rand = (prandom_u32() % (64 - 36)) + 36;
Ilan Peer3a3cb922013-06-13 08:30:12 +03001186 mvmvif->ap_beacon_time = data.beacon_device_ts +
1187 ieee80211_tu_to_usec(data.beacon_int * rand /
1188 100);
1189 } else {
1190 mvmvif->ap_beacon_time =
1191 iwl_read_prph(mvm->trans,
1192 DEVICE_SYSTEM_TIME_REG);
1193 }
1194 }
Johannes Berg506a81e2013-02-28 14:05:14 +01001195
1196 ctxt_ap->beacon_time = cpu_to_le32(mvmvif->ap_beacon_time);
Johannes Berg506a81e2013-02-28 14:05:14 +01001197 ctxt_ap->beacon_tsf = 0; /* unused */
Johannes Berg8ca151b2013-01-24 14:25:36 +01001198
1199 /* TODO: Assume that the beacon id == mac context id */
1200 ctxt_ap->beacon_template = cpu_to_le32(mvmvif->id);
1201}
1202
Ilan Peerf82c8332015-09-10 12:54:38 +03001203static int iwl_mvm_mac_ctxt_cmd_ap(struct iwl_mvm *mvm,
1204 struct ieee80211_vif *vif,
1205 u32 action)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001206{
1207 struct iwl_mac_ctx_cmd cmd = {};
1208
1209 WARN_ON(vif->type != NL80211_IFTYPE_AP || vif->p2p);
1210
1211 /* Fill the common data for all mac context types */
Johannes Berg3dfd3a92014-08-11 21:37:30 +02001212 iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001213
Johannes Berg8ca151b2013-01-24 14:25:36 +01001214 /* Fill the data specific for ap mode */
Johannes Berg506a81e2013-02-28 14:05:14 +01001215 iwl_mvm_mac_ctxt_cmd_fill_ap(mvm, vif, &cmd.ap,
1216 action == FW_CTXT_ACTION_ADD);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001217
1218 return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
1219}
1220
1221static int iwl_mvm_mac_ctxt_cmd_go(struct iwl_mvm *mvm,
1222 struct ieee80211_vif *vif,
1223 u32 action)
1224{
1225 struct iwl_mac_ctx_cmd cmd = {};
Janusz Dziedzic67baf662013-03-21 15:47:56 +01001226 struct ieee80211_p2p_noa_attr *noa = &vif->bss_conf.p2p_noa_attr;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001227
1228 WARN_ON(vif->type != NL80211_IFTYPE_AP || !vif->p2p);
1229
1230 /* Fill the common data for all mac context types */
Johannes Berg3dfd3a92014-08-11 21:37:30 +02001231 iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001232
1233 /* Fill the data specific for GO mode */
Johannes Berg506a81e2013-02-28 14:05:14 +01001234 iwl_mvm_mac_ctxt_cmd_fill_ap(mvm, vif, &cmd.go.ap,
1235 action == FW_CTXT_ACTION_ADD);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001236
Janusz Dziedzic67baf662013-03-21 15:47:56 +01001237 cmd.go.ctwin = cpu_to_le32(noa->oppps_ctwindow &
1238 IEEE80211_P2P_OPPPS_CTWINDOW_MASK);
1239 cmd.go.opp_ps_enabled =
1240 cpu_to_le32(!!(noa->oppps_ctwindow &
1241 IEEE80211_P2P_OPPPS_ENABLE_BIT));
Johannes Berg8ca151b2013-01-24 14:25:36 +01001242
1243 return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
1244}
1245
1246static int iwl_mvm_mac_ctx_send(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
Johannes Berg3dfd3a92014-08-11 21:37:30 +02001247 u32 action, bool force_assoc_off,
1248 const u8 *bssid_override)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001249{
1250 switch (vif->type) {
1251 case NL80211_IFTYPE_STATION:
Luciano Coelhocf520232014-05-13 16:52:54 +03001252 return iwl_mvm_mac_ctxt_cmd_sta(mvm, vif, action,
Johannes Berg3dfd3a92014-08-11 21:37:30 +02001253 force_assoc_off,
1254 bssid_override);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001255 break;
1256 case NL80211_IFTYPE_AP:
1257 if (!vif->p2p)
1258 return iwl_mvm_mac_ctxt_cmd_ap(mvm, vif, action);
1259 else
1260 return iwl_mvm_mac_ctxt_cmd_go(mvm, vif, action);
1261 break;
1262 case NL80211_IFTYPE_MONITOR:
1263 return iwl_mvm_mac_ctxt_cmd_listener(mvm, vif, action);
1264 case NL80211_IFTYPE_P2P_DEVICE:
1265 return iwl_mvm_mac_ctxt_cmd_p2p_device(mvm, vif, action);
Johannes Berg5023d962013-07-31 14:07:43 +02001266 case NL80211_IFTYPE_ADHOC:
1267 return iwl_mvm_mac_ctxt_cmd_ibss(mvm, vif, action);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001268 default:
1269 break;
1270 }
1271
1272 return -EOPNOTSUPP;
1273}
1274
1275int iwl_mvm_mac_ctxt_add(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1276{
1277 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1278 int ret;
1279
1280 if (WARN_ONCE(mvmvif->uploaded, "Adding active MAC %pM/%d\n",
1281 vif->addr, ieee80211_vif_type_p2p(vif)))
1282 return -EIO;
1283
Luciano Coelhobca49d92014-05-13 17:33:38 +03001284 ret = iwl_mvm_mac_ctx_send(mvm, vif, FW_CTXT_ACTION_ADD,
Johannes Berg3dfd3a92014-08-11 21:37:30 +02001285 true, NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001286 if (ret)
1287 return ret;
1288
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001289 /* will only do anything at resume from D3 time */
1290 iwl_mvm_set_last_nonqos_seq(mvm, vif);
1291
Johannes Berg8ca151b2013-01-24 14:25:36 +01001292 mvmvif->uploaded = true;
1293 return 0;
1294}
1295
Luciano Coelhobca49d92014-05-13 17:33:38 +03001296int iwl_mvm_mac_ctxt_changed(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
Johannes Berg3dfd3a92014-08-11 21:37:30 +02001297 bool force_assoc_off, const u8 *bssid_override)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001298{
1299 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1300
1301 if (WARN_ONCE(!mvmvif->uploaded, "Changing inactive MAC %pM/%d\n",
1302 vif->addr, ieee80211_vif_type_p2p(vif)))
1303 return -EIO;
1304
Luciano Coelhobca49d92014-05-13 17:33:38 +03001305 return iwl_mvm_mac_ctx_send(mvm, vif, FW_CTXT_ACTION_MODIFY,
Johannes Berg3dfd3a92014-08-11 21:37:30 +02001306 force_assoc_off, bssid_override);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001307}
1308
1309int iwl_mvm_mac_ctxt_remove(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1310{
1311 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1312 struct iwl_mac_ctx_cmd cmd;
1313 int ret;
1314
1315 if (WARN_ONCE(!mvmvif->uploaded, "Removing inactive MAC %pM/%d\n",
1316 vif->addr, ieee80211_vif_type_p2p(vif)))
1317 return -EIO;
1318
1319 memset(&cmd, 0, sizeof(cmd));
1320
1321 cmd.id_and_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
1322 mvmvif->color));
1323 cmd.action = cpu_to_le32(FW_CTXT_ACTION_REMOVE);
1324
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03001325 ret = iwl_mvm_send_cmd_pdu(mvm, MAC_CONTEXT_CMD, 0,
Johannes Berg8ca151b2013-01-24 14:25:36 +01001326 sizeof(cmd), &cmd);
1327 if (ret) {
1328 IWL_ERR(mvm, "Failed to remove MAC context: %d\n", ret);
1329 return ret;
1330 }
1331
1332 mvmvif->uploaded = false;
Johannes Bergfb8b8ee2013-10-21 12:37:53 +02001333
Chaya Rachel Ivgi0e39eb02015-12-03 15:51:46 +02001334 if (vif->type == NL80211_IFTYPE_MONITOR) {
Johannes Berg30686bf2015-06-02 21:39:54 +02001335 __clear_bit(IEEE80211_HW_RX_INCLUDES_FCS, mvm->hw->flags);
Chaya Rachel Ivgi0e39eb02015-12-03 15:51:46 +02001336 iwl_mvm_dealloc_snif_sta(mvm);
1337 }
Johannes Bergfb8b8ee2013-10-21 12:37:53 +02001338
Johannes Berg8ca151b2013-01-24 14:25:36 +01001339 return 0;
1340}
Ilan Peer571765c2013-03-05 15:26:03 +02001341
Andrei Otcheretianski7f0a7c62014-05-04 11:48:12 +03001342static void iwl_mvm_csa_count_down(struct iwl_mvm *mvm,
Andrei Otcheretianski7ef0aab2014-11-10 11:10:11 +02001343 struct ieee80211_vif *csa_vif, u32 gp2,
1344 bool tx_success)
Andrei Otcheretianski7f0a7c62014-05-04 11:48:12 +03001345{
1346 struct iwl_mvm_vif *mvmvif =
1347 iwl_mvm_vif_from_mac80211(csa_vif);
1348
Andrei Otcheretianski7ef0aab2014-11-10 11:10:11 +02001349 /* Don't start to countdown from a failed beacon */
1350 if (!tx_success && !mvmvif->csa_countdown)
1351 return;
1352
1353 mvmvif->csa_countdown = true;
1354
Andrei Otcheretianski7f0a7c62014-05-04 11:48:12 +03001355 if (!ieee80211_csa_is_complete(csa_vif)) {
1356 int c = ieee80211_csa_update_counter(csa_vif);
1357
1358 iwl_mvm_mac_ctxt_beacon_changed(mvm, csa_vif);
1359 if (csa_vif->p2p &&
Andrei Otcheretianski7ef0aab2014-11-10 11:10:11 +02001360 !iwl_mvm_te_scheduled(&mvmvif->time_event_data) && gp2 &&
1361 tx_success) {
Andrei Otcheretianski7f0a7c62014-05-04 11:48:12 +03001362 u32 rel_time = (c + 1) *
1363 csa_vif->bss_conf.beacon_int -
Luciano Coelhof991e172014-08-26 16:14:10 +03001364 IWL_MVM_CHANNEL_SWITCH_TIME_GO;
Andrei Otcheretianski7f0a7c62014-05-04 11:48:12 +03001365 u32 apply_time = gp2 + rel_time * 1024;
1366
Luciano Coelhof991e172014-08-26 16:14:10 +03001367 iwl_mvm_schedule_csa_period(mvm, csa_vif,
1368 IWL_MVM_CHANNEL_SWITCH_TIME_GO -
1369 IWL_MVM_CHANNEL_SWITCH_MARGIN,
1370 apply_time);
Andrei Otcheretianski7f0a7c62014-05-04 11:48:12 +03001371 }
1372 } else if (!iwl_mvm_te_scheduled(&mvmvif->time_event_data)) {
1373 /* we don't have CSA NoA scheduled yet, switch now */
1374 ieee80211_csa_finish(csa_vif);
1375 RCU_INIT_POINTER(mvm->csa_vif, NULL);
1376 }
1377}
1378
Johannes Berg04168412015-06-23 21:22:09 +02001379void iwl_mvm_rx_beacon_notif(struct iwl_mvm *mvm,
1380 struct iwl_rx_cmd_buffer *rxb)
Ilan Peer571765c2013-03-05 15:26:03 +02001381{
1382 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Emmanuel Grumbach2d2c0e92014-11-24 08:51:46 +02001383 struct iwl_extended_beacon_notif *beacon = (void *)pkt->data;
David Spinadel1c87bba2014-02-27 16:41:52 +02001384 struct iwl_mvm_tx_resp *beacon_notify_hdr;
Andrei Otcheretianski664322f2014-06-05 16:40:36 +03001385 struct ieee80211_vif *csa_vif;
Andrei Otcheretianski003e52362014-05-25 17:24:22 +03001386 struct ieee80211_vif *tx_blocked_vif;
Andrei Otcheretianski7ef0aab2014-11-10 11:10:11 +02001387 u16 status;
Ilan Peer571765c2013-03-05 15:26:03 +02001388
Andrei Otcheretianskibd3398e2013-10-22 05:01:12 +02001389 lockdep_assert_held(&mvm->mutex);
1390
Emmanuel Grumbach2d2c0e92014-11-24 08:51:46 +02001391 beacon_notify_hdr = &beacon->beacon_notify_hdr;
1392 mvm->ap_last_beacon_gp2 = le32_to_cpu(beacon->gp2);
David Spinadel1c87bba2014-02-27 16:41:52 +02001393
Andrei Otcheretianski7ef0aab2014-11-10 11:10:11 +02001394 status = le16_to_cpu(beacon_notify_hdr->status.status) & TX_STATUS_MSK;
David Spinadel1c87bba2014-02-27 16:41:52 +02001395 IWL_DEBUG_RX(mvm,
1396 "beacon status %#x retries:%d tsf:0x%16llX gp2:0x%X rate:%d\n",
Emmanuel Grumbach2d2c0e92014-11-24 08:51:46 +02001397 status, beacon_notify_hdr->failure_frame,
1398 le64_to_cpu(beacon->tsf),
David Spinadel1c87bba2014-02-27 16:41:52 +02001399 mvm->ap_last_beacon_gp2,
1400 le32_to_cpu(beacon_notify_hdr->initial_rate));
Andrei Otcheretianskibd3398e2013-10-22 05:01:12 +02001401
Andrei Otcheretianski664322f2014-06-05 16:40:36 +03001402 csa_vif = rcu_dereference_protected(mvm->csa_vif,
1403 lockdep_is_held(&mvm->mutex));
Andrei Otcheretianski7f0a7c62014-05-04 11:48:12 +03001404 if (unlikely(csa_vif && csa_vif->csa_active))
Andrei Otcheretianski7ef0aab2014-11-10 11:10:11 +02001405 iwl_mvm_csa_count_down(mvm, csa_vif, mvm->ap_last_beacon_gp2,
1406 (status == TX_STATUS_SUCCESS));
Andrei Otcheretianskibd3398e2013-10-22 05:01:12 +02001407
Andrei Otcheretianski003e52362014-05-25 17:24:22 +03001408 tx_blocked_vif = rcu_dereference_protected(mvm->csa_tx_blocked_vif,
1409 lockdep_is_held(&mvm->mutex));
1410 if (unlikely(tx_blocked_vif)) {
1411 struct iwl_mvm_vif *mvmvif =
1412 iwl_mvm_vif_from_mac80211(tx_blocked_vif);
1413
1414 /*
1415 * The channel switch is started and we have blocked the
1416 * stations. If this is the first beacon (the timeout wasn't
1417 * set), set the unblock timeout, otherwise countdown
1418 */
1419 if (!mvm->csa_tx_block_bcn_timeout)
1420 mvm->csa_tx_block_bcn_timeout =
1421 IWL_MVM_CS_UNBLOCK_TX_TIMEOUT;
1422 else
1423 mvm->csa_tx_block_bcn_timeout--;
1424
1425 /* Check if the timeout is expired, and unblock tx */
1426 if (mvm->csa_tx_block_bcn_timeout == 0) {
1427 iwl_mvm_modify_all_sta_disable_tx(mvm, mvmvif, false);
1428 RCU_INIT_POINTER(mvm->csa_tx_blocked_vif, NULL);
1429 }
1430 }
Ilan Peer571765c2013-03-05 15:26:03 +02001431}
Hila Gonend64048e2013-03-13 18:00:03 +02001432
1433static void iwl_mvm_beacon_loss_iterator(void *_data, u8 *mac,
1434 struct ieee80211_vif *vif)
1435{
Ilan Peer12d423e2013-12-24 22:08:14 +02001436 struct iwl_missed_beacons_notif *missed_beacons = _data;
Hila Gonend64048e2013-03-13 18:00:03 +02001437 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Emmanuel Grumbach9d761fd2015-02-02 12:44:23 +02001438 struct iwl_mvm *mvm = mvmvif->mvm;
1439 struct iwl_fw_dbg_trigger_missed_bcon *bcon_trig;
1440 struct iwl_fw_dbg_trigger_tlv *trigger;
1441 u32 stop_trig_missed_bcon, stop_trig_missed_bcon_since_rx;
1442 u32 rx_missed_bcon, rx_missed_bcon_since_rx;
Hila Gonend64048e2013-03-13 18:00:03 +02001443
Ilan Peer12d423e2013-12-24 22:08:14 +02001444 if (mvmvif->id != (u16)le32_to_cpu(missed_beacons->mac_id))
1445 return;
1446
Emmanuel Grumbach9d761fd2015-02-02 12:44:23 +02001447 rx_missed_bcon = le32_to_cpu(missed_beacons->consec_missed_beacons);
1448 rx_missed_bcon_since_rx =
1449 le32_to_cpu(missed_beacons->consec_missed_beacons_since_last_rx);
Ilan Peer12d423e2013-12-24 22:08:14 +02001450 /*
1451 * TODO: the threshold should be adjusted based on latency conditions,
1452 * and/or in case of a CS flow on one of the other AP vifs.
1453 */
1454 if (le32_to_cpu(missed_beacons->consec_missed_beacons_since_last_rx) >
1455 IWL_MVM_MISSED_BEACONS_THRESHOLD)
Hila Gonend64048e2013-03-13 18:00:03 +02001456 ieee80211_beacon_loss(vif);
Emmanuel Grumbach9d761fd2015-02-02 12:44:23 +02001457
1458 if (!iwl_fw_dbg_trigger_enabled(mvm->fw,
1459 FW_DBG_TRIGGER_MISSED_BEACONS))
1460 return;
1461
1462 trigger = iwl_fw_dbg_get_trigger(mvm->fw,
1463 FW_DBG_TRIGGER_MISSED_BEACONS);
1464 bcon_trig = (void *)trigger->data;
1465 stop_trig_missed_bcon = le32_to_cpu(bcon_trig->stop_consec_missed_bcon);
1466 stop_trig_missed_bcon_since_rx =
1467 le32_to_cpu(bcon_trig->stop_consec_missed_bcon_since_rx);
1468
1469 /* TODO: implement start trigger */
1470
1471 if (!iwl_fw_dbg_trigger_check_stop(mvm, vif, trigger))
1472 return;
1473
1474 if (rx_missed_bcon_since_rx >= stop_trig_missed_bcon_since_rx ||
1475 rx_missed_bcon >= stop_trig_missed_bcon)
Johannes Berg5d4f9292015-03-31 09:12:54 +02001476 iwl_mvm_fw_dbg_collect_trig(mvm, trigger, NULL);
Hila Gonend64048e2013-03-13 18:00:03 +02001477}
1478
Johannes Berg04168412015-06-23 21:22:09 +02001479void iwl_mvm_rx_missed_beacons_notif(struct iwl_mvm *mvm,
1480 struct iwl_rx_cmd_buffer *rxb)
Hila Gonend64048e2013-03-13 18:00:03 +02001481{
1482 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Ilan Peer12d423e2013-12-24 22:08:14 +02001483 struct iwl_missed_beacons_notif *mb = (void *)pkt->data;
1484
1485 IWL_DEBUG_INFO(mvm,
1486 "missed bcn mac_id=%u, consecutive=%u (%u, %u, %u)\n",
1487 le32_to_cpu(mb->mac_id),
1488 le32_to_cpu(mb->consec_missed_beacons),
1489 le32_to_cpu(mb->consec_missed_beacons_since_last_rx),
1490 le32_to_cpu(mb->num_recvd_beacons),
1491 le32_to_cpu(mb->num_expected_beacons));
Hila Gonend64048e2013-03-13 18:00:03 +02001492
1493 ieee80211_iterate_active_interfaces_atomic(mvm->hw,
1494 IEEE80211_IFACE_ITER_NORMAL,
1495 iwl_mvm_beacon_loss_iterator,
Ilan Peer12d423e2013-12-24 22:08:14 +02001496 mb);
Hila Gonend64048e2013-03-13 18:00:03 +02001497}
Sara Sharon0db056d2015-12-29 11:07:15 +02001498
1499void iwl_mvm_rx_stored_beacon_notif(struct iwl_mvm *mvm,
1500 struct iwl_rx_cmd_buffer *rxb)
1501{
1502 struct iwl_rx_packet *pkt = rxb_addr(rxb);
1503 struct iwl_stored_beacon_notif *sb = (void *)pkt->data;
1504 struct ieee80211_rx_status rx_status;
1505 struct sk_buff *skb;
1506 u32 size = le32_to_cpu(sb->byte_count);
1507
1508 if (size == 0)
1509 return;
1510
1511 skb = alloc_skb(size, GFP_ATOMIC);
1512 if (!skb) {
1513 IWL_ERR(mvm, "alloc_skb failed\n");
1514 return;
1515 }
1516
1517 /* update rx_status according to the notification's metadata */
1518 memset(&rx_status, 0, sizeof(rx_status));
1519 rx_status.mactime = le64_to_cpu(sb->tsf);
Sara Sharon77fe7392016-01-10 14:23:25 +02001520 /* TSF as indicated by the firmware is at INA time */
1521 rx_status.flag |= RX_FLAG_MACTIME_PLCP_START;
Sara Sharon0db056d2015-12-29 11:07:15 +02001522 rx_status.device_timestamp = le32_to_cpu(sb->system_time);
1523 rx_status.band =
1524 (sb->phy_flags & cpu_to_le16(RX_RES_PHY_FLAGS_BAND_24)) ?
Johannes Berg57fbcce2016-04-12 15:56:15 +02001525 NL80211_BAND_2GHZ : NL80211_BAND_5GHZ;
Sara Sharon0db056d2015-12-29 11:07:15 +02001526 rx_status.freq =
1527 ieee80211_channel_to_frequency(le16_to_cpu(sb->channel),
1528 rx_status.band);
1529
1530 /* copy the data */
1531 memcpy(skb_put(skb, size), sb->data, size);
1532 memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
1533
1534 /* pass it as regular rx to mac80211 */
Johannes Bergd63b5482016-03-31 20:02:02 +03001535 ieee80211_rx_napi(mvm->hw, NULL, skb, NULL);
Sara Sharon0db056d2015-12-29 11:07:15 +02001536}