blob: 9e69b9d2012cca424abb5fffd4763670e09d90ce [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
Liad Kaufman4d339982017-03-21 17:13:16 +0200219 if (vif->type == NL80211_IFTYPE_AP ||
220 vif->type == NL80211_IFTYPE_ADHOC)
Johannes Berg110cf812014-08-01 23:14:24 +0200221 qmask |= BIT(vif->cab_queue);
222
223 return qmask;
224}
225
Arik Nemtsova74346d2014-09-21 19:07:30 +0300226static void iwl_mvm_iface_hw_queues_iter(void *_data, u8 *mac,
227 struct ieee80211_vif *vif)
228{
229 struct iwl_mvm_hw_queues_iface_iterator_data *data = _data;
230
231 /* exclude the given vif */
232 if (vif == data->exclude_vif)
233 return;
234
235 data->used_hw_queues |= iwl_mvm_mac_get_queues_mask(vif);
236}
237
238static void iwl_mvm_mac_sta_hw_queues_iter(void *_data,
239 struct ieee80211_sta *sta)
240{
241 struct iwl_mvm_hw_queues_iface_iterator_data *data = _data;
242 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
243
244 /* Mark the queues used by the sta */
245 data->used_hw_queues |= mvmsta->tfd_queue_msk;
246}
247
248unsigned long iwl_mvm_get_used_hw_queues(struct iwl_mvm *mvm,
249 struct ieee80211_vif *exclude_vif)
250{
Arik Nemtsov51ec09a2015-01-21 11:50:31 +0200251 u8 sta_id;
Arik Nemtsova74346d2014-09-21 19:07:30 +0300252 struct iwl_mvm_hw_queues_iface_iterator_data data = {
253 .exclude_vif = exclude_vif,
254 .used_hw_queues =
255 BIT(IWL_MVM_OFFCHANNEL_QUEUE) |
Liad Kaufman097129c2015-08-09 18:28:43 +0300256 BIT(mvm->aux_queue),
Arik Nemtsova74346d2014-09-21 19:07:30 +0300257 };
258
Liad Kaufman097129c2015-08-09 18:28:43 +0300259 if (iwl_mvm_is_dqa_supported(mvm))
260 data.used_hw_queues |= BIT(IWL_MVM_DQA_CMD_QUEUE);
261 else
262 data.used_hw_queues |= BIT(IWL_MVM_CMD_QUEUE);
263
Arik Nemtsova74346d2014-09-21 19:07:30 +0300264 lockdep_assert_held(&mvm->mutex);
265
266 /* mark all VIF used hw queues */
267 ieee80211_iterate_active_interfaces_atomic(
268 mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
269 iwl_mvm_iface_hw_queues_iter, &data);
270
271 /* don't assign the same hw queues as TDLS stations */
272 ieee80211_iterate_stations_atomic(mvm->hw,
273 iwl_mvm_mac_sta_hw_queues_iter,
274 &data);
275
Arik Nemtsov51ec09a2015-01-21 11:50:31 +0200276 /*
277 * Some TDLS stations may be removed but are in the process of being
278 * drained. Don't touch their queues.
279 */
280 for_each_set_bit(sta_id, mvm->sta_drained, IWL_MVM_STATION_COUNT)
281 data.used_hw_queues |= mvm->tfd_drained[sta_id];
282
Arik Nemtsova74346d2014-09-21 19:07:30 +0300283 return data.used_hw_queues;
284}
285
Ilan Peer6e97b0d2013-12-23 22:18:02 +0200286static void iwl_mvm_mac_iface_iterator(void *_data, u8 *mac,
287 struct ieee80211_vif *vif)
288{
289 struct iwl_mvm_mac_iface_iterator_data *data = _data;
290 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Ilan Peer6e97b0d2013-12-23 22:18:02 +0200291
292 /* Iterator may already find the interface being added -- skip it */
293 if (vif == data->vif) {
294 data->found_vif = true;
295 return;
296 }
297
Ilan Peer6e97b0d2013-12-23 22:18:02 +0200298 /* Mark MAC IDs as used by clearing the available bit, and
299 * (below) mark TSFs as used if their existing use is not
300 * compatible with the new interface type.
301 * No locking or atomic bit operations are needed since the
302 * data is on the stack of the caller function.
303 */
304 __clear_bit(mvmvif->id, data->available_mac_ids);
305
306 /* find a suitable tsf_id */
307 iwl_mvm_mac_tsf_id_iter(_data, mac, vif);
308}
309
Ilan Peer6e97b0d2013-12-23 22:18:02 +0200310void iwl_mvm_mac_ctxt_recalc_tsf_id(struct iwl_mvm *mvm,
311 struct ieee80211_vif *vif)
312{
313 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
314 struct iwl_mvm_mac_iface_iterator_data data = {
315 .mvm = mvm,
316 .vif = vif,
317 .available_tsf_ids = { (1 << NUM_TSF_IDS) - 1 },
318 /* no preference yet */
319 .preferred_tsf = NUM_TSF_IDS,
320 };
321
322 ieee80211_iterate_active_interfaces_atomic(
323 mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
324 iwl_mvm_mac_tsf_id_iter, &data);
325
326 if (data.preferred_tsf != NUM_TSF_IDS)
327 mvmvif->tsf_id = data.preferred_tsf;
328 else if (!test_bit(mvmvif->tsf_id, data.available_tsf_ids))
329 mvmvif->tsf_id = find_first_bit(data.available_tsf_ids,
330 NUM_TSF_IDS);
331}
332
Johannes Berg8ca151b2013-01-24 14:25:36 +0100333static int iwl_mvm_mac_ctxt_allocate_resources(struct iwl_mvm *mvm,
334 struct ieee80211_vif *vif)
335{
336 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
337 struct iwl_mvm_mac_iface_iterator_data data = {
338 .mvm = mvm,
339 .vif = vif,
340 .available_mac_ids = { (1 << NUM_MAC_INDEX_DRIVER) - 1 },
341 .available_tsf_ids = { (1 << NUM_TSF_IDS) - 1 },
342 /* no preference yet */
343 .preferred_tsf = NUM_TSF_IDS,
Johannes Berg8ca151b2013-01-24 14:25:36 +0100344 .found_vif = false,
345 };
346 u32 ac;
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +0300347 int ret, i;
Johannes Berg110cf812014-08-01 23:14:24 +0200348 unsigned long used_hw_queues;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100349
350 /*
351 * Allocate a MAC ID and a TSF for this MAC, along with the queues
352 * and other resources.
353 */
354
355 /*
356 * Before the iterator, we start with all MAC IDs and TSFs available.
357 *
358 * During iteration, all MAC IDs are cleared that are in use by other
359 * virtual interfaces, and all TSF IDs are cleared that can't be used
360 * by this new virtual interface because they're used by an interface
361 * that can't share it with the new one.
362 * At the same time, we check if there's a preferred TSF in the case
363 * that we should share it with another interface.
364 */
365
Johannes Berg5023d962013-07-31 14:07:43 +0200366 /* Currently, MAC ID 0 should be used only for the managed/IBSS vif */
367 switch (vif->type) {
368 case NL80211_IFTYPE_ADHOC:
369 break;
370 case NL80211_IFTYPE_STATION:
371 if (!vif->p2p)
372 break;
373 /* fall through */
374 default:
Ilan Peerec8b6882013-02-13 13:27:18 +0200375 __clear_bit(0, data.available_mac_ids);
Johannes Berg5023d962013-07-31 14:07:43 +0200376 }
Ilan Peerec8b6882013-02-13 13:27:18 +0200377
Johannes Berg8ca151b2013-01-24 14:25:36 +0100378 ieee80211_iterate_active_interfaces_atomic(
379 mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
380 iwl_mvm_mac_iface_iterator, &data);
381
Arik Nemtsova74346d2014-09-21 19:07:30 +0300382 used_hw_queues = iwl_mvm_get_used_hw_queues(mvm, vif);
383
Johannes Berg8ca151b2013-01-24 14:25:36 +0100384 /*
385 * In the case we're getting here during resume, it's similar to
386 * firmware restart, and with RESUME_ALL the iterator will find
387 * the vif being added already.
388 * We don't want to reassign any IDs in either case since doing
389 * so would probably assign different IDs (as interfaces aren't
390 * necessarily added in the same order), but the old IDs were
391 * preserved anyway, so skip ID assignment for both resume and
392 * recovery.
393 */
394 if (data.found_vif)
395 return 0;
396
397 /* Therefore, in recovery, we can't get here */
Ilan Peerfd11bd02013-06-16 12:18:11 +0300398 if (WARN_ON_ONCE(test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)))
399 return -EBUSY;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100400
401 mvmvif->id = find_first_bit(data.available_mac_ids,
402 NUM_MAC_INDEX_DRIVER);
403 if (mvmvif->id == NUM_MAC_INDEX_DRIVER) {
404 IWL_ERR(mvm, "Failed to init MAC context - no free ID!\n");
405 ret = -EIO;
406 goto exit_fail;
407 }
408
409 if (data.preferred_tsf != NUM_TSF_IDS)
410 mvmvif->tsf_id = data.preferred_tsf;
411 else
412 mvmvif->tsf_id = find_first_bit(data.available_tsf_ids,
413 NUM_TSF_IDS);
414 if (mvmvif->tsf_id == NUM_TSF_IDS) {
415 IWL_ERR(mvm, "Failed to init MAC context - no free TSF!\n");
416 ret = -EIO;
417 goto exit_fail;
418 }
419
420 mvmvif->color = 0;
421
Ilan Peer1e849c92013-02-13 12:26:39 +0200422 INIT_LIST_HEAD(&mvmvif->time_event_data.list);
423 mvmvif->time_event_data.id = TE_MAX;
424
Johannes Berg8ca151b2013-01-24 14:25:36 +0100425 /* No need to allocate data queues to P2P Device MAC.*/
426 if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
427 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
428 vif->hw_queue[ac] = IEEE80211_INVAL_HW_QUEUE;
429
430 return 0;
431 }
432
Liad Kaufman24afba72015-07-28 18:56:08 +0300433 /*
434 * Find available queues, and allocate them to the ACs. When in
435 * DQA-mode they aren't really used, and this is done only so the
436 * mac80211 ieee80211_check_queues() function won't fail
437 */
Johannes Berg8ca151b2013-01-24 14:25:36 +0100438 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
Johannes Berg110cf812014-08-01 23:14:24 +0200439 u8 queue = find_first_zero_bit(&used_hw_queues,
Eytan Lifshitz19e737c2013-09-09 13:30:15 +0200440 mvm->first_agg_queue);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100441
Liad Kaufman24afba72015-07-28 18:56:08 +0300442 if (!iwl_mvm_is_dqa_supported(mvm) &&
443 queue >= mvm->first_agg_queue) {
Johannes Berg8ca151b2013-01-24 14:25:36 +0100444 IWL_ERR(mvm, "Failed to allocate queue\n");
445 ret = -EIO;
446 goto exit_fail;
447 }
448
Johannes Berg110cf812014-08-01 23:14:24 +0200449 __set_bit(queue, &used_hw_queues);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100450 vif->hw_queue[ac] = queue;
451 }
452
453 /* Allocate the CAB queue for softAP and GO interfaces */
454 if (vif->type == NL80211_IFTYPE_AP) {
Liad Kaufman0e0e4422015-08-04 15:13:38 +0300455 u8 queue;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100456
Liad Kaufman0e0e4422015-08-04 15:13:38 +0300457 if (!iwl_mvm_is_dqa_supported(mvm)) {
458 queue = find_first_zero_bit(&used_hw_queues,
459 mvm->first_agg_queue);
460
461 if (queue >= mvm->first_agg_queue) {
462 IWL_ERR(mvm, "Failed to allocate cab queue\n");
463 ret = -EIO;
464 goto exit_fail;
465 }
466 } else {
467 queue = IWL_MVM_DQA_GCAST_QUEUE;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100468 }
469
470 vif->cab_queue = queue;
471 } else {
472 vif->cab_queue = IEEE80211_INVAL_HW_QUEUE;
473 }
474
Sara Sharon0ae98812017-01-04 14:53:58 +0200475 mvmvif->bcast_sta.sta_id = IWL_MVM_INVALID_STA;
476 mvmvif->mcast_sta.sta_id = IWL_MVM_INVALID_STA;
477 mvmvif->ap_sta_id = IWL_MVM_INVALID_STA;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100478
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +0300479 for (i = 0; i < NUM_IWL_MVM_SMPS_REQ; i++)
480 mvmvif->smps_requests[i] = IEEE80211_SMPS_AUTOMATIC;
481
Johannes Berg8ca151b2013-01-24 14:25:36 +0100482 return 0;
483
484exit_fail:
485 memset(mvmvif, 0, sizeof(struct iwl_mvm_vif));
486 memset(vif->hw_queue, IEEE80211_INVAL_HW_QUEUE, sizeof(vif->hw_queue));
487 vif->cab_queue = IEEE80211_INVAL_HW_QUEUE;
488 return ret;
489}
490
491int iwl_mvm_mac_ctxt_init(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
492{
Emmanuel Grumbach5d42e7b2015-03-19 20:04:51 +0200493 unsigned int wdg_timeout =
494 iwl_mvm_get_wd_timeout(mvm, vif, false, false);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100495 u32 ac;
496 int ret;
497
498 lockdep_assert_held(&mvm->mutex);
499
500 ret = iwl_mvm_mac_ctxt_allocate_resources(mvm, vif);
501 if (ret)
502 return ret;
503
Sara Sharon8236f7d2016-10-06 16:42:32 +0300504 /* If DQA is supported - queues will be enabled when needed */
505 if (iwl_mvm_is_dqa_supported(mvm))
506 return 0;
507
Johannes Berg8ca151b2013-01-24 14:25:36 +0100508 switch (vif->type) {
509 case NL80211_IFTYPE_P2P_DEVICE:
Sara Sharon8236f7d2016-10-06 16:42:32 +0300510 iwl_mvm_enable_ac_txq(mvm, IWL_MVM_OFFCHANNEL_QUEUE,
511 IWL_MVM_OFFCHANNEL_QUEUE,
512 IWL_MVM_TX_FIFO_VO, 0, wdg_timeout);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100513 break;
514 case NL80211_IFTYPE_AP:
Liad Kaufman4ecafae2015-07-14 13:36:18 +0300515 iwl_mvm_enable_ac_txq(mvm, vif->cab_queue, vif->cab_queue,
Liad Kaufman5c1156e2015-07-22 17:59:53 +0300516 IWL_MVM_TX_FIFO_MCAST, 0, wdg_timeout);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100517 /* fall through */
518 default:
519 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
Avri Altman3edf8ff2014-07-30 11:41:01 +0300520 iwl_mvm_enable_ac_txq(mvm, vif->hw_queue[ac],
Liad Kaufman4ecafae2015-07-14 13:36:18 +0300521 vif->hw_queue[ac],
Liad Kaufman5c1156e2015-07-22 17:59:53 +0300522 iwl_mvm_ac_to_tx_fifo[ac], 0,
Emmanuel Grumbach4cf677f2015-01-12 14:38:29 +0200523 wdg_timeout);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100524 break;
525 }
526
527 return 0;
528}
529
530void iwl_mvm_mac_ctxt_release(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
531{
532 int ac;
533
534 lockdep_assert_held(&mvm->mutex);
535
Liad Kaufmandf88c082016-11-24 15:31:00 +0200536 /*
537 * If DQA is supported - queues were already disabled, since in
538 * DQA-mode the queues are a property of the STA and not of the
539 * vif, and at this point the STA was already deleted
540 */
541 if (iwl_mvm_is_dqa_supported(mvm))
542 return;
543
Johannes Berg8ca151b2013-01-24 14:25:36 +0100544 switch (vif->type) {
545 case NL80211_IFTYPE_P2P_DEVICE:
Liad Kaufmandf88c082016-11-24 15:31:00 +0200546 iwl_mvm_disable_txq(mvm, IWL_MVM_OFFCHANNEL_QUEUE,
547 IWL_MVM_OFFCHANNEL_QUEUE,
548 IWL_MAX_TID_COUNT, 0);
Liad Kaufman4c965132015-08-09 19:26:56 +0300549
Johannes Berg8ca151b2013-01-24 14:25:36 +0100550 break;
551 case NL80211_IFTYPE_AP:
Liad Kaufman4ecafae2015-07-14 13:36:18 +0300552 iwl_mvm_disable_txq(mvm, vif->cab_queue, vif->cab_queue,
553 IWL_MAX_TID_COUNT, 0);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100554 /* fall through */
555 default:
556 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
Liad Kaufman4ecafae2015-07-14 13:36:18 +0300557 iwl_mvm_disable_txq(mvm, vif->hw_queue[ac],
558 vif->hw_queue[ac],
559 IWL_MAX_TID_COUNT, 0);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100560 }
561}
562
563static void iwl_mvm_ack_rates(struct iwl_mvm *mvm,
564 struct ieee80211_vif *vif,
Johannes Berg57fbcce2016-04-12 15:56:15 +0200565 enum nl80211_band band,
Johannes Berg8ca151b2013-01-24 14:25:36 +0100566 u8 *cck_rates, u8 *ofdm_rates)
567{
568 struct ieee80211_supported_band *sband;
569 unsigned long basic = vif->bss_conf.basic_rates;
570 int lowest_present_ofdm = 100;
571 int lowest_present_cck = 100;
572 u8 cck = 0;
573 u8 ofdm = 0;
574 int i;
575
576 sband = mvm->hw->wiphy->bands[band];
577
578 for_each_set_bit(i, &basic, BITS_PER_LONG) {
579 int hw = sband->bitrates[i].hw_value;
580 if (hw >= IWL_FIRST_OFDM_RATE) {
581 ofdm |= BIT(hw - IWL_FIRST_OFDM_RATE);
582 if (lowest_present_ofdm > hw)
583 lowest_present_ofdm = hw;
584 } else {
585 BUILD_BUG_ON(IWL_FIRST_CCK_RATE != 0);
586
587 cck |= BIT(hw);
588 if (lowest_present_cck > hw)
589 lowest_present_cck = hw;
590 }
591 }
592
593 /*
594 * Now we've got the basic rates as bitmaps in the ofdm and cck
595 * variables. This isn't sufficient though, as there might not
596 * be all the right rates in the bitmap. E.g. if the only basic
597 * rates are 5.5 Mbps and 11 Mbps, we still need to add 1 Mbps
598 * and 6 Mbps because the 802.11-2007 standard says in 9.6:
599 *
600 * [...] a STA responding to a received frame shall transmit
601 * its Control Response frame [...] at the highest rate in the
602 * BSSBasicRateSet parameter that is less than or equal to the
603 * rate of the immediately previous frame in the frame exchange
604 * sequence ([...]) and that is of the same modulation class
605 * ([...]) as the received frame. If no rate contained in the
606 * BSSBasicRateSet parameter meets these conditions, then the
607 * control frame sent in response to a received frame shall be
608 * transmitted at the highest mandatory rate of the PHY that is
609 * less than or equal to the rate of the received frame, and
610 * that is of the same modulation class as the received frame.
611 *
612 * As a consequence, we need to add all mandatory rates that are
613 * lower than all of the basic rates to these bitmaps.
614 */
615
616 if (IWL_RATE_24M_INDEX < lowest_present_ofdm)
617 ofdm |= IWL_RATE_BIT_MSK(24) >> IWL_FIRST_OFDM_RATE;
618 if (IWL_RATE_12M_INDEX < lowest_present_ofdm)
619 ofdm |= IWL_RATE_BIT_MSK(12) >> IWL_FIRST_OFDM_RATE;
620 /* 6M already there or needed so always add */
621 ofdm |= IWL_RATE_BIT_MSK(6) >> IWL_FIRST_OFDM_RATE;
622
623 /*
624 * CCK is a bit more complex with DSSS vs. HR/DSSS vs. ERP.
625 * Note, however:
626 * - if no CCK rates are basic, it must be ERP since there must
627 * be some basic rates at all, so they're OFDM => ERP PHY
628 * (or we're in 5 GHz, and the cck bitmap will never be used)
629 * - if 11M is a basic rate, it must be ERP as well, so add 5.5M
630 * - if 5.5M is basic, 1M and 2M are mandatory
631 * - if 2M is basic, 1M is mandatory
632 * - if 1M is basic, that's the only valid ACK rate.
633 * As a consequence, it's not as complicated as it sounds, just add
634 * any lower rates to the ACK rate bitmap.
635 */
636 if (IWL_RATE_11M_INDEX < lowest_present_cck)
637 cck |= IWL_RATE_BIT_MSK(11) >> IWL_FIRST_CCK_RATE;
638 if (IWL_RATE_5M_INDEX < lowest_present_cck)
639 cck |= IWL_RATE_BIT_MSK(5) >> IWL_FIRST_CCK_RATE;
640 if (IWL_RATE_2M_INDEX < lowest_present_cck)
641 cck |= IWL_RATE_BIT_MSK(2) >> IWL_FIRST_CCK_RATE;
642 /* 1M already there or needed so always add */
643 cck |= IWL_RATE_BIT_MSK(1) >> IWL_FIRST_CCK_RATE;
644
645 *cck_rates = cck;
646 *ofdm_rates = ofdm;
647}
648
Avri Altman8a5e3662013-11-12 19:16:03 +0200649static void iwl_mvm_mac_ctxt_set_ht_flags(struct iwl_mvm *mvm,
650 struct ieee80211_vif *vif,
651 struct iwl_mac_ctx_cmd *cmd)
652{
653 /* for both sta and ap, ht_operation_mode hold the protection_mode */
654 u8 protection_mode = vif->bss_conf.ht_operation_mode &
655 IEEE80211_HT_OP_MODE_PROTECTION;
656 /* The fw does not distinguish between ht and fat */
657 u32 ht_flag = MAC_PROT_FLG_HT_PROT | MAC_PROT_FLG_FAT_PROT;
658
659 IWL_DEBUG_RATE(mvm, "protection mode set to %d\n", protection_mode);
660 /*
661 * See section 9.23.3.1 of IEEE 80211-2012.
662 * Nongreenfield HT STAs Present is not supported.
663 */
664 switch (protection_mode) {
665 case IEEE80211_HT_OP_MODE_PROTECTION_NONE:
666 break;
667 case IEEE80211_HT_OP_MODE_PROTECTION_NONMEMBER:
668 case IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED:
669 cmd->protection_flags |= cpu_to_le32(ht_flag);
670 break;
671 case IEEE80211_HT_OP_MODE_PROTECTION_20MHZ:
672 /* Protect when channel wider than 20MHz */
673 if (vif->bss_conf.chandef.width > NL80211_CHAN_WIDTH_20)
674 cmd->protection_flags |= cpu_to_le32(ht_flag);
675 break;
676 default:
677 IWL_ERR(mvm, "Illegal protection mode %d\n",
678 protection_mode);
679 break;
680 }
681}
682
Johannes Berg8ca151b2013-01-24 14:25:36 +0100683static void iwl_mvm_mac_ctxt_cmd_common(struct iwl_mvm *mvm,
684 struct ieee80211_vif *vif,
685 struct iwl_mac_ctx_cmd *cmd,
Johannes Berg3dfd3a92014-08-11 21:37:30 +0200686 const u8 *bssid_override,
Johannes Berg8ca151b2013-01-24 14:25:36 +0100687 u32 action)
688{
689 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
690 struct ieee80211_chanctx_conf *chanctx;
Avri Altman8a5e3662013-11-12 19:16:03 +0200691 bool ht_enabled = !!(vif->bss_conf.ht_operation_mode &
692 IEEE80211_HT_OP_MODE_PROTECTION);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100693 u8 cck_ack_rates, ofdm_ack_rates;
Johannes Berg3dfd3a92014-08-11 21:37:30 +0200694 const u8 *bssid = bssid_override ?: vif->bss_conf.bssid;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100695 int i;
696
697 cmd->id_and_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
698 mvmvif->color));
699 cmd->action = cpu_to_le32(action);
700
701 switch (vif->type) {
702 case NL80211_IFTYPE_STATION:
703 if (vif->p2p)
704 cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_P2P_STA);
705 else
706 cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_BSS_STA);
707 break;
708 case NL80211_IFTYPE_AP:
709 cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_GO);
710 break;
711 case NL80211_IFTYPE_MONITOR:
712 cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_LISTENER);
713 break;
714 case NL80211_IFTYPE_P2P_DEVICE:
715 cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_P2P_DEVICE);
716 break;
717 case NL80211_IFTYPE_ADHOC:
718 cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_IBSS);
719 break;
720 default:
721 WARN_ON_ONCE(1);
722 }
723
724 cmd->tsf_id = cpu_to_le32(mvmvif->tsf_id);
725
726 memcpy(cmd->node_addr, vif->addr, ETH_ALEN);
Johannes Berg3dfd3a92014-08-11 21:37:30 +0200727
728 if (bssid)
729 memcpy(cmd->bssid_addr, bssid, ETH_ALEN);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100730 else
731 eth_broadcast_addr(cmd->bssid_addr);
732
733 rcu_read_lock();
734 chanctx = rcu_dereference(vif->chanctx_conf);
735 iwl_mvm_ack_rates(mvm, vif, chanctx ? chanctx->def.chan->band
Johannes Berg57fbcce2016-04-12 15:56:15 +0200736 : NL80211_BAND_2GHZ,
Johannes Berg8ca151b2013-01-24 14:25:36 +0100737 &cck_ack_rates, &ofdm_ack_rates);
738 rcu_read_unlock();
739
740 cmd->cck_rates = cpu_to_le32((u32)cck_ack_rates);
741 cmd->ofdm_rates = cpu_to_le32((u32)ofdm_ack_rates);
742
743 cmd->cck_short_preamble =
744 cpu_to_le32(vif->bss_conf.use_short_preamble ?
745 MAC_FLG_SHORT_PREAMBLE : 0);
746 cmd->short_slot =
747 cpu_to_le32(vif->bss_conf.use_short_slot ?
748 MAC_FLG_SHORT_SLOT : 0);
749
Ayala Bekerbd6f5bd2015-12-20 09:27:50 +0200750 cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP);
751
Emmanuel Grumbach3dd94792013-12-19 16:50:49 +0200752 for (i = 0; i < IEEE80211_NUM_ACS; i++) {
753 u8 txf = iwl_mvm_ac_to_tx_fifo[i];
754
755 cmd->ac[txf].cw_min =
756 cpu_to_le16(mvmvif->queue_params[i].cw_min);
757 cmd->ac[txf].cw_max =
758 cpu_to_le16(mvmvif->queue_params[i].cw_max);
759 cmd->ac[txf].edca_txop =
Johannes Berg8ca151b2013-01-24 14:25:36 +0100760 cpu_to_le16(mvmvif->queue_params[i].txop * 32);
Emmanuel Grumbach3dd94792013-12-19 16:50:49 +0200761 cmd->ac[txf].aifsn = mvmvif->queue_params[i].aifs;
762 cmd->ac[txf].fifos_mask = BIT(txf);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100763 }
764
765 if (vif->bss_conf.qos)
766 cmd->qos_flags |= cpu_to_le32(MAC_QOS_FLG_UPDATE_EDCA);
767
Emmanuel Grumbachdc271ee2014-07-03 20:46:35 +0300768 if (vif->bss_conf.use_cts_prot)
Dor Shaishcc7ee2b2013-02-18 13:51:36 +0200769 cmd->protection_flags |= cpu_to_le32(MAC_PROT_FLG_TGG_PROTECT);
Emmanuel Grumbachdc271ee2014-07-03 20:46:35 +0300770
Avri Altman8a5e3662013-11-12 19:16:03 +0200771 IWL_DEBUG_RATE(mvm, "use_cts_prot %d, ht_operation_mode %d\n",
772 vif->bss_conf.use_cts_prot,
773 vif->bss_conf.ht_operation_mode);
774 if (vif->bss_conf.chandef.width != NL80211_CHAN_WIDTH_20_NOHT)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100775 cmd->qos_flags |= cpu_to_le32(MAC_QOS_FLG_TGN);
Avri Altman8a5e3662013-11-12 19:16:03 +0200776 if (ht_enabled)
777 iwl_mvm_mac_ctxt_set_ht_flags(mvm, vif, cmd);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100778}
779
780static int iwl_mvm_mac_ctxt_send_cmd(struct iwl_mvm *mvm,
781 struct iwl_mac_ctx_cmd *cmd)
782{
Emmanuel Grumbacha1022922014-05-12 11:36:41 +0300783 int ret = iwl_mvm_send_cmd_pdu(mvm, MAC_CONTEXT_CMD, 0,
Johannes Berg8ca151b2013-01-24 14:25:36 +0100784 sizeof(*cmd), cmd);
785 if (ret)
786 IWL_ERR(mvm, "Failed to send MAC context (action:%d): %d\n",
787 le32_to_cpu(cmd->action), ret);
788 return ret;
789}
790
Luciano Coelhocf520232014-05-13 16:52:54 +0300791static int iwl_mvm_mac_ctxt_cmd_sta(struct iwl_mvm *mvm,
792 struct ieee80211_vif *vif,
Johannes Berg3dfd3a92014-08-11 21:37:30 +0200793 u32 action, bool force_assoc_off,
794 const u8 *bssid_override)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100795{
Luciano Coelhocf520232014-05-13 16:52:54 +0300796 struct iwl_mac_ctx_cmd cmd = {};
797 struct iwl_mac_data_sta *ctxt_sta;
798
799 WARN_ON(vif->type != NL80211_IFTYPE_STATION);
800
801 /* Fill the common data for all mac context types */
Johannes Berg3dfd3a92014-08-11 21:37:30 +0200802 iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, bssid_override, action);
Luciano Coelhocf520232014-05-13 16:52:54 +0300803
804 if (vif->p2p) {
805 struct ieee80211_p2p_noa_attr *noa =
806 &vif->bss_conf.p2p_noa_attr;
807
808 cmd.p2p_sta.ctwin = cpu_to_le32(noa->oppps_ctwindow &
809 IEEE80211_P2P_OPPPS_CTWINDOW_MASK);
810 ctxt_sta = &cmd.p2p_sta.sta;
811 } else {
Luciano Coelhocf520232014-05-13 16:52:54 +0300812 ctxt_sta = &cmd.sta;
813 }
814
Johannes Berg210a5442013-01-24 23:48:23 +0100815 /* We need the dtim_period to set the MAC as associated */
Alexander Bondarba283922013-05-02 16:34:48 +0300816 if (vif->bss_conf.assoc && vif->bss_conf.dtim_period &&
817 !force_assoc_off) {
Johannes Bergd2931bb2013-02-05 18:10:04 +0100818 u32 dtim_offs;
819
820 /*
821 * The DTIM count counts down, so when it is N that means N
822 * more beacon intervals happen until the DTIM TBTT. Therefore
823 * add this to the current time. If that ends up being in the
824 * future, the firmware will handle it.
825 *
826 * Also note that the system_timestamp (which we get here as
827 * "sync_device_ts") and TSF timestamp aren't at exactly the
828 * same offset in the frame -- the TSF is at the first symbol
829 * of the TSF, the system timestamp is at signal acquisition
830 * time. This means there's an offset between them of at most
831 * a few hundred microseconds (24 * 8 bits + PLCP time gives
832 * 384us in the longest case), this is currently not relevant
833 * as the firmware wakes up around 2ms before the TBTT.
834 */
835 dtim_offs = vif->bss_conf.sync_dtim_count *
836 vif->bss_conf.beacon_int;
837 /* convert TU to usecs */
838 dtim_offs *= 1024;
839
840 ctxt_sta->dtim_tsf =
841 cpu_to_le64(vif->bss_conf.sync_tsf + dtim_offs);
842 ctxt_sta->dtim_time =
843 cpu_to_le32(vif->bss_conf.sync_device_ts + dtim_offs);
844
845 IWL_DEBUG_INFO(mvm, "DTIM TBTT is 0x%llx/0x%x, offset %d\n",
846 le64_to_cpu(ctxt_sta->dtim_tsf),
847 le32_to_cpu(ctxt_sta->dtim_time),
848 dtim_offs);
849
Johannes Berg210a5442013-01-24 23:48:23 +0100850 ctxt_sta->is_assoc = cpu_to_le32(1);
Johannes Bergd2931bb2013-02-05 18:10:04 +0100851 } else {
Johannes Berg210a5442013-01-24 23:48:23 +0100852 ctxt_sta->is_assoc = cpu_to_le32(0);
Luciano Coelho7c8b3bc2014-07-30 15:04:08 +0300853
854 /* Allow beacons to pass through as long as we are not
855 * associated, or we do not have dtim period information.
856 */
857 cmd.filter_flags |= cpu_to_le32(MAC_FILTER_IN_BEACON);
Johannes Bergd2931bb2013-02-05 18:10:04 +0100858 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100859
860 ctxt_sta->bi = cpu_to_le32(vif->bss_conf.beacon_int);
861 ctxt_sta->bi_reciprocal =
862 cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int));
863 ctxt_sta->dtim_interval = cpu_to_le32(vif->bss_conf.beacon_int *
864 vif->bss_conf.dtim_period);
865 ctxt_sta->dtim_reciprocal =
866 cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int *
867 vif->bss_conf.dtim_period));
868
869 ctxt_sta->listen_interval = cpu_to_le32(mvm->hw->conf.listen_interval);
870 ctxt_sta->assoc_id = cpu_to_le32(vif->bss_conf.aid);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100871
Andrei Otcheretianskieffd1922015-06-30 12:08:28 +0300872 if (vif->probe_req_reg && vif->bss_conf.assoc && vif->p2p)
873 cmd.filter_flags |= cpu_to_le32(MAC_FILTER_IN_PROBE_REQUEST);
874
Johannes Berg8ca151b2013-01-24 14:25:36 +0100875 return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
876}
877
878static int iwl_mvm_mac_ctxt_cmd_listener(struct iwl_mvm *mvm,
879 struct ieee80211_vif *vif,
880 u32 action)
881{
882 struct iwl_mac_ctx_cmd cmd = {};
Chaya Rachel Ivgi0e39eb02015-12-03 15:51:46 +0200883 u32 tfd_queue_msk = 0;
884 int ret, i;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100885
886 WARN_ON(vif->type != NL80211_IFTYPE_MONITOR);
887
Johannes Berg3dfd3a92014-08-11 21:37:30 +0200888 iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
Johannes Berg53585492013-03-18 13:04:50 +0100889
Liad Kaufman737847a2016-09-27 15:17:29 +0300890 if (!iwl_mvm_is_dqa_supported(mvm)) {
891 for (i = 0; i < IEEE80211_NUM_ACS; i++)
892 if (vif->hw_queue[i] != IEEE80211_INVAL_HW_QUEUE)
893 tfd_queue_msk |= BIT(vif->hw_queue[i]);
894 }
Chaya Rachel Ivgi0e39eb02015-12-03 15:51:46 +0200895
Johannes Berg53585492013-03-18 13:04:50 +0100896 cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_PROMISC |
897 MAC_FILTER_IN_CONTROL_AND_MGMT |
898 MAC_FILTER_IN_BEACON |
Johannes Bergfb8b8ee2013-10-21 12:37:53 +0200899 MAC_FILTER_IN_PROBE_REQUEST |
900 MAC_FILTER_IN_CRC32);
Johannes Berg30686bf2015-06-02 21:39:54 +0200901 ieee80211_hw_set(mvm->hw, RX_INCLUDES_FCS);
Johannes Berg53585492013-03-18 13:04:50 +0100902
Chaya Rachel Ivgi0e39eb02015-12-03 15:51:46 +0200903 /* Allocate sniffer station */
904 ret = iwl_mvm_allocate_int_sta(mvm, &mvm->snif_sta, tfd_queue_msk,
905 vif->type);
906 if (ret)
907 return ret;
908
Johannes Berg8ca151b2013-01-24 14:25:36 +0100909 return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
910}
911
Johannes Berg5023d962013-07-31 14:07:43 +0200912static int iwl_mvm_mac_ctxt_cmd_ibss(struct iwl_mvm *mvm,
913 struct ieee80211_vif *vif,
914 u32 action)
915{
916 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
917 struct iwl_mac_ctx_cmd cmd = {};
918
919 WARN_ON(vif->type != NL80211_IFTYPE_ADHOC);
920
Johannes Berg3dfd3a92014-08-11 21:37:30 +0200921 iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
Johannes Berg5023d962013-07-31 14:07:43 +0200922
923 cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_BEACON |
924 MAC_FILTER_IN_PROBE_REQUEST);
925
926 /* cmd.ibss.beacon_time/cmd.ibss.beacon_tsf are curently ignored */
927 cmd.ibss.bi = cpu_to_le32(vif->bss_conf.beacon_int);
928 cmd.ibss.bi_reciprocal =
929 cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int));
930
931 /* TODO: Assumes that the beacon id == mac context id */
932 cmd.ibss.beacon_template = cpu_to_le32(mvmvif->id);
933
934 return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
935}
936
Johannes Berg8ca151b2013-01-24 14:25:36 +0100937struct iwl_mvm_go_iterator_data {
938 bool go_active;
939};
940
941static void iwl_mvm_go_iterator(void *_data, u8 *mac, struct ieee80211_vif *vif)
942{
943 struct iwl_mvm_go_iterator_data *data = _data;
944 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
945
Johannes Berg5023d962013-07-31 14:07:43 +0200946 if (vif->type == NL80211_IFTYPE_AP && vif->p2p &&
947 mvmvif->ap_ibss_active)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100948 data->go_active = true;
949}
950
951static int iwl_mvm_mac_ctxt_cmd_p2p_device(struct iwl_mvm *mvm,
952 struct ieee80211_vif *vif,
953 u32 action)
954{
955 struct iwl_mac_ctx_cmd cmd = {};
956 struct iwl_mvm_go_iterator_data data = {};
957
958 WARN_ON(vif->type != NL80211_IFTYPE_P2P_DEVICE);
959
Johannes Berg3dfd3a92014-08-11 21:37:30 +0200960 iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100961
962 cmd.protection_flags |= cpu_to_le32(MAC_PROT_FLG_TGG_PROTECT);
Ilan Peer1dcd15e2013-02-14 14:25:24 +0200963
964 /* Override the filter flags to accept only probe requests */
965 cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_PROBE_REQUEST);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100966
967 /*
968 * This flag should be set to true when the P2P Device is
969 * discoverable and there is at least another active P2P GO. Settings
970 * this flag will allow the P2P Device to be discoverable on other
971 * channels in addition to its listen channel.
972 * Note that this flag should not be set in other cases as it opens the
973 * Rx filters on all MAC and increases the number of interrupts.
974 */
975 ieee80211_iterate_active_interfaces_atomic(
976 mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
977 iwl_mvm_go_iterator, &data);
978
979 cmd.p2p_dev.is_disc_extended = cpu_to_le32(data.go_active ? 1 : 0);
980 return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
981}
982
983static void iwl_mvm_mac_ctxt_set_tim(struct iwl_mvm *mvm,
Sara Sharon8364fbb2016-12-08 11:44:20 +0200984 __le32 *tim_index, __le32 *tim_size,
Johannes Berg8ca151b2013-01-24 14:25:36 +0100985 u8 *beacon, u32 frame_size)
986{
987 u32 tim_idx;
988 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)beacon;
989
990 /* The index is relative to frame start but we start looking at the
991 * variable-length part of the beacon. */
992 tim_idx = mgmt->u.beacon.variable - beacon;
993
994 /* Parse variable-length elements of beacon to find WLAN_EID_TIM */
995 while ((tim_idx < (frame_size - 2)) &&
996 (beacon[tim_idx] != WLAN_EID_TIM))
997 tim_idx += beacon[tim_idx+1] + 2;
998
999 /* If TIM field was found, set variables */
1000 if ((tim_idx < (frame_size - 1)) && (beacon[tim_idx] == WLAN_EID_TIM)) {
Sara Sharon8364fbb2016-12-08 11:44:20 +02001001 *tim_index = cpu_to_le32(tim_idx);
1002 *tim_size = cpu_to_le32((u32)beacon[tim_idx + 1]);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001003 } else {
1004 IWL_WARN(mvm, "Unable to find TIM Element in beacon\n");
1005 }
1006}
1007
Andrei Otcheretianskid3a108a2016-02-28 17:12:21 +02001008static u32 iwl_mvm_find_ie_offset(u8 *beacon, u8 eid, u32 frame_size)
1009{
1010 struct ieee80211_mgmt *mgmt = (void *)beacon;
1011 const u8 *ie;
1012
1013 if (WARN_ON_ONCE(frame_size <= (mgmt->u.beacon.variable - beacon)))
1014 return 0;
1015
1016 frame_size -= mgmt->u.beacon.variable - beacon;
1017
1018 ie = cfg80211_find_ie(eid, mgmt->u.beacon.variable, frame_size);
1019 if (!ie)
1020 return 0;
1021
1022 return ie - beacon;
1023}
1024
Johannes Berg8ca151b2013-01-24 14:25:36 +01001025static int iwl_mvm_mac_ctxt_send_beacon(struct iwl_mvm *mvm,
1026 struct ieee80211_vif *vif,
1027 struct sk_buff *beacon)
1028{
1029 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1030 struct iwl_host_cmd cmd = {
1031 .id = BEACON_TEMPLATE_CMD,
1032 .flags = CMD_ASYNC,
1033 };
Andrei Otcheretianskid3a108a2016-02-28 17:12:21 +02001034 union {
1035 struct iwl_mac_beacon_cmd_v6 beacon_cmd_v6;
Sara Sharon8364fbb2016-12-08 11:44:20 +02001036 struct iwl_mac_beacon_cmd_v7 beacon_cmd;
Andrei Otcheretianskid3a108a2016-02-28 17:12:21 +02001037 } u = {};
Sara Sharon8364fbb2016-12-08 11:44:20 +02001038 struct iwl_mac_beacon_cmd beacon_cmd;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001039 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
Sara Sharon8364fbb2016-12-08 11:44:20 +02001048 if (fw_has_capa(&mvm->fw->ucode_capa,
1049 IWL_UCODE_TLV_CAPA_CSA_AND_TBTT_OFFLOAD)) {
1050 u32 csa_offset, ecsa_offset;
1051
1052 csa_offset = iwl_mvm_find_ie_offset(beacon->data,
1053 WLAN_EID_CHANNEL_SWITCH,
1054 beacon_skb_len);
1055 ecsa_offset =
1056 iwl_mvm_find_ie_offset(beacon->data,
1057 WLAN_EID_EXT_CHANSWITCH_ANN,
1058 beacon_skb_len);
1059
1060 if (iwl_mvm_has_new_tx_api(mvm)) {
1061 beacon_cmd.data.template_id =
1062 cpu_to_le32((u32)mvmvif->id);
1063 beacon_cmd.data.ecsa_offset = cpu_to_le32(ecsa_offset);
1064 beacon_cmd.data.csa_offset = cpu_to_le32(csa_offset);
1065 beacon_cmd.byte_cnt = cpu_to_le16((u16)beacon_skb_len);
1066 if (vif->type == NL80211_IFTYPE_AP)
1067 iwl_mvm_mac_ctxt_set_tim(mvm,
1068 &beacon_cmd.data.tim_idx,
1069 &beacon_cmd.data.tim_size,
1070 beacon->data,
1071 beacon_skb_len);
1072 cmd.len[0] = sizeof(beacon_cmd);
1073 cmd.data[0] = &beacon_cmd;
1074 goto send;
1075
1076 } else {
1077 u.beacon_cmd.data.ecsa_offset =
1078 cpu_to_le32(ecsa_offset);
1079 u.beacon_cmd.data.csa_offset = cpu_to_le32(csa_offset);
1080 cmd.len[0] = sizeof(u.beacon_cmd);
1081 cmd.data[0] = &u;
1082 }
1083 } else {
1084 cmd.len[0] = sizeof(u.beacon_cmd_v6);
1085 cmd.data[0] = &u;
1086 }
1087
Johannes Berg8ca151b2013-01-24 14:25:36 +01001088 /* TODO: for now the beacon template id is set to be the mac context id.
1089 * Might be better to handle it as another resource ... */
Andrei Otcheretianskid3a108a2016-02-28 17:12:21 +02001090 u.beacon_cmd_v6.template_id = cpu_to_le32((u32)mvmvif->id);
Emmanuel Grumbach75f6b9b2014-06-02 10:03:33 +03001091 info = IEEE80211_SKB_CB(beacon);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001092
1093 /* Set up TX command fields */
Andrei Otcheretianskid3a108a2016-02-28 17:12:21 +02001094 u.beacon_cmd_v6.tx.len = cpu_to_le16((u16)beacon_skb_len);
1095 u.beacon_cmd_v6.tx.sta_id = mvmvif->bcast_sta.sta_id;
1096 u.beacon_cmd_v6.tx.life_time = cpu_to_le32(TX_CMD_LIFE_TIME_INFINITE);
Emmanuel Grumbach75f6b9b2014-06-02 10:03:33 +03001097 tx_flags = TX_CMD_FLG_SEQ_CTL | TX_CMD_FLG_TSF;
1098 tx_flags |=
1099 iwl_mvm_bt_coex_tx_prio(mvm, (void *)beacon->data, info, 0) <<
1100 TX_CMD_FLG_BT_PRIO_POS;
Andrei Otcheretianskid3a108a2016-02-28 17:12:21 +02001101 u.beacon_cmd_v6.tx.tx_flags = cpu_to_le32(tx_flags);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001102
Emmanuel Grumbach1e3c3c32015-12-13 09:35:30 +02001103 if (!fw_has_capa(&mvm->fw->ucode_capa,
1104 IWL_UCODE_TLV_CAPA_BEACON_ANT_SELECTION)) {
1105 mvm->mgmt_last_antenna_idx =
1106 iwl_mvm_next_antenna(mvm, iwl_mvm_get_valid_tx_ant(mvm),
1107 mvm->mgmt_last_antenna_idx);
1108 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01001109
Andrei Otcheretianskid3a108a2016-02-28 17:12:21 +02001110 u.beacon_cmd_v6.tx.rate_n_flags =
Johannes Berg8ca151b2013-01-24 14:25:36 +01001111 cpu_to_le32(BIT(mvm->mgmt_last_antenna_idx) <<
1112 RATE_MCS_ANT_POS);
1113
Johannes Berg57fbcce2016-04-12 15:56:15 +02001114 if (info->band == NL80211_BAND_5GHZ || vif->p2p) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001115 rate = IWL_FIRST_OFDM_RATE;
1116 } else {
1117 rate = IWL_FIRST_CCK_RATE;
Andrei Otcheretianskid3a108a2016-02-28 17:12:21 +02001118 u.beacon_cmd_v6.tx.rate_n_flags |=
1119 cpu_to_le32(RATE_MCS_CCK_MSK);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001120 }
Andrei Otcheretianskid3a108a2016-02-28 17:12:21 +02001121 u.beacon_cmd_v6.tx.rate_n_flags |=
Johannes Berg8ca151b2013-01-24 14:25:36 +01001122 cpu_to_le32(iwl_mvm_mac80211_idx_to_hwrate(rate));
1123
1124 /* Set up TX beacon command fields */
Johannes Berg5023d962013-07-31 14:07:43 +02001125 if (vif->type == NL80211_IFTYPE_AP)
Sara Sharon8364fbb2016-12-08 11:44:20 +02001126 iwl_mvm_mac_ctxt_set_tim(mvm, &u.beacon_cmd_v6.tim_idx,
1127 &u.beacon_cmd_v6.tim_size,
Johannes Berg5023d962013-07-31 14:07:43 +02001128 beacon->data,
1129 beacon_skb_len);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001130
Sara Sharon8364fbb2016-12-08 11:44:20 +02001131send:
Johannes Berg8ca151b2013-01-24 14:25:36 +01001132 /* Submit command */
Johannes Berg8ca151b2013-01-24 14:25:36 +01001133 cmd.dataflags[0] = 0;
1134 cmd.len[1] = beacon_skb_len;
1135 cmd.data[1] = beacon->data;
1136 cmd.dataflags[1] = IWL_HCMD_DFL_DUP;
1137
1138 return iwl_mvm_send_cmd(mvm, &cmd);
1139}
1140
Johannes Berg5023d962013-07-31 14:07:43 +02001141/* The beacon template for the AP/GO/IBSS has changed and needs update */
Johannes Berg8ca151b2013-01-24 14:25:36 +01001142int iwl_mvm_mac_ctxt_beacon_changed(struct iwl_mvm *mvm,
1143 struct ieee80211_vif *vif)
1144{
1145 struct sk_buff *beacon;
1146 int ret;
1147
Johannes Berg5023d962013-07-31 14:07:43 +02001148 WARN_ON(vif->type != NL80211_IFTYPE_AP &&
1149 vif->type != NL80211_IFTYPE_ADHOC);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001150
Andrei Otcheretianskife887662014-04-10 21:46:16 +03001151 beacon = ieee80211_beacon_get_template(mvm->hw, vif, NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001152 if (!beacon)
1153 return -ENOMEM;
1154
1155 ret = iwl_mvm_mac_ctxt_send_beacon(mvm, vif, beacon);
1156 dev_kfree_skb(beacon);
1157 return ret;
1158}
1159
Ilan Peer3a3cb922013-06-13 08:30:12 +03001160struct iwl_mvm_mac_ap_iterator_data {
1161 struct iwl_mvm *mvm;
1162 struct ieee80211_vif *vif;
1163 u32 beacon_device_ts;
1164 u16 beacon_int;
1165};
1166
1167/* Find the beacon_device_ts and beacon_int for a managed interface */
1168static void iwl_mvm_mac_ap_iterator(void *_data, u8 *mac,
1169 struct ieee80211_vif *vif)
1170{
1171 struct iwl_mvm_mac_ap_iterator_data *data = _data;
1172
1173 if (vif->type != NL80211_IFTYPE_STATION || !vif->bss_conf.assoc)
1174 return;
1175
1176 /* Station client has higher priority over P2P client*/
1177 if (vif->p2p && data->beacon_device_ts)
1178 return;
1179
1180 data->beacon_device_ts = vif->bss_conf.sync_device_ts;
1181 data->beacon_int = vif->bss_conf.beacon_int;
1182}
1183
Johannes Berg8ca151b2013-01-24 14:25:36 +01001184/*
1185 * Fill the specific data for mac context of type AP of P2P GO
1186 */
1187static void iwl_mvm_mac_ctxt_cmd_fill_ap(struct iwl_mvm *mvm,
1188 struct ieee80211_vif *vif,
Johannes Berg186cd492016-08-24 10:05:40 +02001189 struct iwl_mac_ctx_cmd *cmd,
Johannes Berg506a81e2013-02-28 14:05:14 +01001190 struct iwl_mac_data_ap *ctxt_ap,
1191 bool add)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001192{
1193 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Ilan Peer3a3cb922013-06-13 08:30:12 +03001194 struct iwl_mvm_mac_ap_iterator_data data = {
1195 .mvm = mvm,
1196 .vif = vif,
1197 .beacon_device_ts = 0
1198 };
Johannes Berg8ca151b2013-01-24 14:25:36 +01001199
Johannes Berg186cd492016-08-24 10:05:40 +02001200 /* in AP mode, the MCAST FIFO takes the EDCA params from VO */
1201 cmd->ac[IWL_MVM_TX_FIFO_VO].fifos_mask |= BIT(IWL_MVM_TX_FIFO_MCAST);
1202
1203 /*
1204 * in AP mode, pass probe requests and beacons from other APs
1205 * (needed for ht protection); when there're no any associated
1206 * station don't ask FW to pass beacons to prevent unnecessary
1207 * wake-ups.
1208 */
1209 cmd->filter_flags |= cpu_to_le32(MAC_FILTER_IN_PROBE_REQUEST);
1210 if (mvmvif->ap_assoc_sta_count || !mvm->drop_bcn_ap_mode) {
1211 cmd->filter_flags |= cpu_to_le32(MAC_FILTER_IN_BEACON);
1212 IWL_DEBUG_HC(mvm, "Asking FW to pass beacons\n");
1213 } else {
1214 IWL_DEBUG_HC(mvm, "No need to receive beacons\n");
1215 }
1216
Johannes Berg8ca151b2013-01-24 14:25:36 +01001217 ctxt_ap->bi = cpu_to_le32(vif->bss_conf.beacon_int);
1218 ctxt_ap->bi_reciprocal =
1219 cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int));
1220 ctxt_ap->dtim_interval = cpu_to_le32(vif->bss_conf.beacon_int *
1221 vif->bss_conf.dtim_period);
1222 ctxt_ap->dtim_reciprocal =
1223 cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int *
1224 vif->bss_conf.dtim_period));
1225
1226 ctxt_ap->mcast_qid = cpu_to_le32(vif->cab_queue);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001227
Johannes Berg506a81e2013-02-28 14:05:14 +01001228 /*
Ilan Peer3a3cb922013-06-13 08:30:12 +03001229 * Only set the beacon time when the MAC is being added, when we
Johannes Berg506a81e2013-02-28 14:05:14 +01001230 * just modify the MAC then we should keep the time -- the firmware
1231 * can otherwise have a "jumping" TBTT.
1232 */
Ilan Peer3a3cb922013-06-13 08:30:12 +03001233 if (add) {
1234 /*
1235 * If there is a station/P2P client interface which is
1236 * associated, set the AP's TBTT far enough from the station's
1237 * TBTT. Otherwise, set it to the current system time
1238 */
1239 ieee80211_iterate_active_interfaces_atomic(
1240 mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
1241 iwl_mvm_mac_ap_iterator, &data);
1242
1243 if (data.beacon_device_ts) {
Ilan Peer9f8f8ca2013-12-24 17:17:05 +02001244 u32 rand = (prandom_u32() % (64 - 36)) + 36;
Ilan Peer3a3cb922013-06-13 08:30:12 +03001245 mvmvif->ap_beacon_time = data.beacon_device_ts +
1246 ieee80211_tu_to_usec(data.beacon_int * rand /
1247 100);
1248 } else {
1249 mvmvif->ap_beacon_time =
1250 iwl_read_prph(mvm->trans,
1251 DEVICE_SYSTEM_TIME_REG);
1252 }
1253 }
Johannes Berg506a81e2013-02-28 14:05:14 +01001254
1255 ctxt_ap->beacon_time = cpu_to_le32(mvmvif->ap_beacon_time);
Johannes Berg506a81e2013-02-28 14:05:14 +01001256 ctxt_ap->beacon_tsf = 0; /* unused */
Johannes Berg8ca151b2013-01-24 14:25:36 +01001257
1258 /* TODO: Assume that the beacon id == mac context id */
1259 ctxt_ap->beacon_template = cpu_to_le32(mvmvif->id);
1260}
1261
Ilan Peerf82c8332015-09-10 12:54:38 +03001262static int iwl_mvm_mac_ctxt_cmd_ap(struct iwl_mvm *mvm,
1263 struct ieee80211_vif *vif,
1264 u32 action)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001265{
1266 struct iwl_mac_ctx_cmd cmd = {};
1267
1268 WARN_ON(vif->type != NL80211_IFTYPE_AP || vif->p2p);
1269
1270 /* Fill the common data for all mac context types */
Johannes Berg3dfd3a92014-08-11 21:37:30 +02001271 iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001272
Johannes Berg8ca151b2013-01-24 14:25:36 +01001273 /* Fill the data specific for ap mode */
Johannes Berg186cd492016-08-24 10:05:40 +02001274 iwl_mvm_mac_ctxt_cmd_fill_ap(mvm, vif, &cmd, &cmd.ap,
Johannes Berg506a81e2013-02-28 14:05:14 +01001275 action == FW_CTXT_ACTION_ADD);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001276
1277 return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
1278}
1279
1280static int iwl_mvm_mac_ctxt_cmd_go(struct iwl_mvm *mvm,
1281 struct ieee80211_vif *vif,
1282 u32 action)
1283{
1284 struct iwl_mac_ctx_cmd cmd = {};
Janusz Dziedzic67baf662013-03-21 15:47:56 +01001285 struct ieee80211_p2p_noa_attr *noa = &vif->bss_conf.p2p_noa_attr;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001286
1287 WARN_ON(vif->type != NL80211_IFTYPE_AP || !vif->p2p);
1288
1289 /* Fill the common data for all mac context types */
Johannes Berg3dfd3a92014-08-11 21:37:30 +02001290 iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001291
1292 /* Fill the data specific for GO mode */
Johannes Berg186cd492016-08-24 10:05:40 +02001293 iwl_mvm_mac_ctxt_cmd_fill_ap(mvm, vif, &cmd, &cmd.go.ap,
Johannes Berg506a81e2013-02-28 14:05:14 +01001294 action == FW_CTXT_ACTION_ADD);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001295
Janusz Dziedzic67baf662013-03-21 15:47:56 +01001296 cmd.go.ctwin = cpu_to_le32(noa->oppps_ctwindow &
1297 IEEE80211_P2P_OPPPS_CTWINDOW_MASK);
1298 cmd.go.opp_ps_enabled =
1299 cpu_to_le32(!!(noa->oppps_ctwindow &
1300 IEEE80211_P2P_OPPPS_ENABLE_BIT));
Johannes Berg8ca151b2013-01-24 14:25:36 +01001301
1302 return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
1303}
1304
1305static int iwl_mvm_mac_ctx_send(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
Johannes Berg3dfd3a92014-08-11 21:37:30 +02001306 u32 action, bool force_assoc_off,
1307 const u8 *bssid_override)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001308{
1309 switch (vif->type) {
1310 case NL80211_IFTYPE_STATION:
Luciano Coelhocf520232014-05-13 16:52:54 +03001311 return iwl_mvm_mac_ctxt_cmd_sta(mvm, vif, action,
Johannes Berg3dfd3a92014-08-11 21:37:30 +02001312 force_assoc_off,
1313 bssid_override);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001314 break;
1315 case NL80211_IFTYPE_AP:
1316 if (!vif->p2p)
1317 return iwl_mvm_mac_ctxt_cmd_ap(mvm, vif, action);
1318 else
1319 return iwl_mvm_mac_ctxt_cmd_go(mvm, vif, action);
1320 break;
1321 case NL80211_IFTYPE_MONITOR:
1322 return iwl_mvm_mac_ctxt_cmd_listener(mvm, vif, action);
1323 case NL80211_IFTYPE_P2P_DEVICE:
1324 return iwl_mvm_mac_ctxt_cmd_p2p_device(mvm, vif, action);
Johannes Berg5023d962013-07-31 14:07:43 +02001325 case NL80211_IFTYPE_ADHOC:
1326 return iwl_mvm_mac_ctxt_cmd_ibss(mvm, vif, action);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001327 default:
1328 break;
1329 }
1330
1331 return -EOPNOTSUPP;
1332}
1333
1334int iwl_mvm_mac_ctxt_add(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1335{
1336 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1337 int ret;
1338
1339 if (WARN_ONCE(mvmvif->uploaded, "Adding active MAC %pM/%d\n",
1340 vif->addr, ieee80211_vif_type_p2p(vif)))
1341 return -EIO;
1342
Luciano Coelhobca49d92014-05-13 17:33:38 +03001343 ret = iwl_mvm_mac_ctx_send(mvm, vif, FW_CTXT_ACTION_ADD,
Johannes Berg3dfd3a92014-08-11 21:37:30 +02001344 true, NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001345 if (ret)
1346 return ret;
1347
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001348 /* will only do anything at resume from D3 time */
1349 iwl_mvm_set_last_nonqos_seq(mvm, vif);
1350
Johannes Berg8ca151b2013-01-24 14:25:36 +01001351 mvmvif->uploaded = true;
1352 return 0;
1353}
1354
Luciano Coelhobca49d92014-05-13 17:33:38 +03001355int iwl_mvm_mac_ctxt_changed(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
Johannes Berg3dfd3a92014-08-11 21:37:30 +02001356 bool force_assoc_off, const u8 *bssid_override)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001357{
1358 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1359
1360 if (WARN_ONCE(!mvmvif->uploaded, "Changing inactive MAC %pM/%d\n",
1361 vif->addr, ieee80211_vif_type_p2p(vif)))
1362 return -EIO;
1363
Luciano Coelhobca49d92014-05-13 17:33:38 +03001364 return iwl_mvm_mac_ctx_send(mvm, vif, FW_CTXT_ACTION_MODIFY,
Johannes Berg3dfd3a92014-08-11 21:37:30 +02001365 force_assoc_off, bssid_override);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001366}
1367
1368int iwl_mvm_mac_ctxt_remove(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1369{
1370 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1371 struct iwl_mac_ctx_cmd cmd;
1372 int ret;
1373
1374 if (WARN_ONCE(!mvmvif->uploaded, "Removing inactive MAC %pM/%d\n",
1375 vif->addr, ieee80211_vif_type_p2p(vif)))
1376 return -EIO;
1377
1378 memset(&cmd, 0, sizeof(cmd));
1379
1380 cmd.id_and_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
1381 mvmvif->color));
1382 cmd.action = cpu_to_le32(FW_CTXT_ACTION_REMOVE);
1383
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03001384 ret = iwl_mvm_send_cmd_pdu(mvm, MAC_CONTEXT_CMD, 0,
Johannes Berg8ca151b2013-01-24 14:25:36 +01001385 sizeof(cmd), &cmd);
1386 if (ret) {
1387 IWL_ERR(mvm, "Failed to remove MAC context: %d\n", ret);
1388 return ret;
1389 }
1390
1391 mvmvif->uploaded = false;
Johannes Bergfb8b8ee2013-10-21 12:37:53 +02001392
Chaya Rachel Ivgi0e39eb02015-12-03 15:51:46 +02001393 if (vif->type == NL80211_IFTYPE_MONITOR) {
Johannes Berg30686bf2015-06-02 21:39:54 +02001394 __clear_bit(IEEE80211_HW_RX_INCLUDES_FCS, mvm->hw->flags);
Chaya Rachel Ivgi0e39eb02015-12-03 15:51:46 +02001395 iwl_mvm_dealloc_snif_sta(mvm);
1396 }
Johannes Bergfb8b8ee2013-10-21 12:37:53 +02001397
Johannes Berg8ca151b2013-01-24 14:25:36 +01001398 return 0;
1399}
Ilan Peer571765c2013-03-05 15:26:03 +02001400
Andrei Otcheretianski7f0a7c62014-05-04 11:48:12 +03001401static void iwl_mvm_csa_count_down(struct iwl_mvm *mvm,
Andrei Otcheretianski7ef0aab2014-11-10 11:10:11 +02001402 struct ieee80211_vif *csa_vif, u32 gp2,
1403 bool tx_success)
Andrei Otcheretianski7f0a7c62014-05-04 11:48:12 +03001404{
1405 struct iwl_mvm_vif *mvmvif =
1406 iwl_mvm_vif_from_mac80211(csa_vif);
1407
Andrei Otcheretianski7ef0aab2014-11-10 11:10:11 +02001408 /* Don't start to countdown from a failed beacon */
1409 if (!tx_success && !mvmvif->csa_countdown)
1410 return;
1411
1412 mvmvif->csa_countdown = true;
1413
Andrei Otcheretianski7f0a7c62014-05-04 11:48:12 +03001414 if (!ieee80211_csa_is_complete(csa_vif)) {
1415 int c = ieee80211_csa_update_counter(csa_vif);
1416
1417 iwl_mvm_mac_ctxt_beacon_changed(mvm, csa_vif);
1418 if (csa_vif->p2p &&
Andrei Otcheretianski7ef0aab2014-11-10 11:10:11 +02001419 !iwl_mvm_te_scheduled(&mvmvif->time_event_data) && gp2 &&
1420 tx_success) {
Andrei Otcheretianski7f0a7c62014-05-04 11:48:12 +03001421 u32 rel_time = (c + 1) *
1422 csa_vif->bss_conf.beacon_int -
Luciano Coelhof991e172014-08-26 16:14:10 +03001423 IWL_MVM_CHANNEL_SWITCH_TIME_GO;
Andrei Otcheretianski7f0a7c62014-05-04 11:48:12 +03001424 u32 apply_time = gp2 + rel_time * 1024;
1425
Luciano Coelhof991e172014-08-26 16:14:10 +03001426 iwl_mvm_schedule_csa_period(mvm, csa_vif,
1427 IWL_MVM_CHANNEL_SWITCH_TIME_GO -
1428 IWL_MVM_CHANNEL_SWITCH_MARGIN,
1429 apply_time);
Andrei Otcheretianski7f0a7c62014-05-04 11:48:12 +03001430 }
1431 } else if (!iwl_mvm_te_scheduled(&mvmvif->time_event_data)) {
1432 /* we don't have CSA NoA scheduled yet, switch now */
1433 ieee80211_csa_finish(csa_vif);
1434 RCU_INIT_POINTER(mvm->csa_vif, NULL);
1435 }
1436}
1437
Johannes Berg04168412015-06-23 21:22:09 +02001438void iwl_mvm_rx_beacon_notif(struct iwl_mvm *mvm,
1439 struct iwl_rx_cmd_buffer *rxb)
Ilan Peer571765c2013-03-05 15:26:03 +02001440{
1441 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Emmanuel Grumbach2d2c0e92014-11-24 08:51:46 +02001442 struct iwl_extended_beacon_notif *beacon = (void *)pkt->data;
David Spinadel1c87bba2014-02-27 16:41:52 +02001443 struct iwl_mvm_tx_resp *beacon_notify_hdr;
Andrei Otcheretianski664322f2014-06-05 16:40:36 +03001444 struct ieee80211_vif *csa_vif;
Andrei Otcheretianski003e52362014-05-25 17:24:22 +03001445 struct ieee80211_vif *tx_blocked_vif;
Sara Sharon12db2942017-01-17 14:28:21 +02001446 struct agg_tx_status *agg_status;
Andrei Otcheretianski7ef0aab2014-11-10 11:10:11 +02001447 u16 status;
Ilan Peer571765c2013-03-05 15:26:03 +02001448
Andrei Otcheretianskibd3398e2013-10-22 05:01:12 +02001449 lockdep_assert_held(&mvm->mutex);
1450
Emmanuel Grumbach2d2c0e92014-11-24 08:51:46 +02001451 beacon_notify_hdr = &beacon->beacon_notify_hdr;
1452 mvm->ap_last_beacon_gp2 = le32_to_cpu(beacon->gp2);
David Spinadel1c87bba2014-02-27 16:41:52 +02001453
Sara Sharon12db2942017-01-17 14:28:21 +02001454 agg_status = iwl_mvm_get_agg_status(mvm, beacon_notify_hdr);
1455 status = le16_to_cpu(agg_status->status) & TX_STATUS_MSK;
David Spinadel1c87bba2014-02-27 16:41:52 +02001456 IWL_DEBUG_RX(mvm,
1457 "beacon status %#x retries:%d tsf:0x%16llX gp2:0x%X rate:%d\n",
Emmanuel Grumbach2d2c0e92014-11-24 08:51:46 +02001458 status, beacon_notify_hdr->failure_frame,
1459 le64_to_cpu(beacon->tsf),
David Spinadel1c87bba2014-02-27 16:41:52 +02001460 mvm->ap_last_beacon_gp2,
1461 le32_to_cpu(beacon_notify_hdr->initial_rate));
Andrei Otcheretianskibd3398e2013-10-22 05:01:12 +02001462
Andrei Otcheretianski664322f2014-06-05 16:40:36 +03001463 csa_vif = rcu_dereference_protected(mvm->csa_vif,
1464 lockdep_is_held(&mvm->mutex));
Andrei Otcheretianski7f0a7c62014-05-04 11:48:12 +03001465 if (unlikely(csa_vif && csa_vif->csa_active))
Andrei Otcheretianski7ef0aab2014-11-10 11:10:11 +02001466 iwl_mvm_csa_count_down(mvm, csa_vif, mvm->ap_last_beacon_gp2,
1467 (status == TX_STATUS_SUCCESS));
Andrei Otcheretianskibd3398e2013-10-22 05:01:12 +02001468
Andrei Otcheretianski003e52362014-05-25 17:24:22 +03001469 tx_blocked_vif = rcu_dereference_protected(mvm->csa_tx_blocked_vif,
1470 lockdep_is_held(&mvm->mutex));
1471 if (unlikely(tx_blocked_vif)) {
1472 struct iwl_mvm_vif *mvmvif =
1473 iwl_mvm_vif_from_mac80211(tx_blocked_vif);
1474
1475 /*
1476 * The channel switch is started and we have blocked the
1477 * stations. If this is the first beacon (the timeout wasn't
1478 * set), set the unblock timeout, otherwise countdown
1479 */
1480 if (!mvm->csa_tx_block_bcn_timeout)
1481 mvm->csa_tx_block_bcn_timeout =
1482 IWL_MVM_CS_UNBLOCK_TX_TIMEOUT;
1483 else
1484 mvm->csa_tx_block_bcn_timeout--;
1485
1486 /* Check if the timeout is expired, and unblock tx */
1487 if (mvm->csa_tx_block_bcn_timeout == 0) {
1488 iwl_mvm_modify_all_sta_disable_tx(mvm, mvmvif, false);
1489 RCU_INIT_POINTER(mvm->csa_tx_blocked_vif, NULL);
1490 }
1491 }
Ilan Peer571765c2013-03-05 15:26:03 +02001492}
Hila Gonend64048e2013-03-13 18:00:03 +02001493
1494static void iwl_mvm_beacon_loss_iterator(void *_data, u8 *mac,
1495 struct ieee80211_vif *vif)
1496{
Ilan Peer12d423e2013-12-24 22:08:14 +02001497 struct iwl_missed_beacons_notif *missed_beacons = _data;
Hila Gonend64048e2013-03-13 18:00:03 +02001498 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Emmanuel Grumbach9d761fd2015-02-02 12:44:23 +02001499 struct iwl_mvm *mvm = mvmvif->mvm;
1500 struct iwl_fw_dbg_trigger_missed_bcon *bcon_trig;
1501 struct iwl_fw_dbg_trigger_tlv *trigger;
1502 u32 stop_trig_missed_bcon, stop_trig_missed_bcon_since_rx;
1503 u32 rx_missed_bcon, rx_missed_bcon_since_rx;
Hila Gonend64048e2013-03-13 18:00:03 +02001504
Ilan Peer12d423e2013-12-24 22:08:14 +02001505 if (mvmvif->id != (u16)le32_to_cpu(missed_beacons->mac_id))
1506 return;
1507
Emmanuel Grumbach9d761fd2015-02-02 12:44:23 +02001508 rx_missed_bcon = le32_to_cpu(missed_beacons->consec_missed_beacons);
1509 rx_missed_bcon_since_rx =
1510 le32_to_cpu(missed_beacons->consec_missed_beacons_since_last_rx);
Ilan Peer12d423e2013-12-24 22:08:14 +02001511 /*
1512 * TODO: the threshold should be adjusted based on latency conditions,
1513 * and/or in case of a CS flow on one of the other AP vifs.
1514 */
1515 if (le32_to_cpu(missed_beacons->consec_missed_beacons_since_last_rx) >
1516 IWL_MVM_MISSED_BEACONS_THRESHOLD)
Hila Gonend64048e2013-03-13 18:00:03 +02001517 ieee80211_beacon_loss(vif);
Emmanuel Grumbach9d761fd2015-02-02 12:44:23 +02001518
1519 if (!iwl_fw_dbg_trigger_enabled(mvm->fw,
1520 FW_DBG_TRIGGER_MISSED_BEACONS))
1521 return;
1522
1523 trigger = iwl_fw_dbg_get_trigger(mvm->fw,
1524 FW_DBG_TRIGGER_MISSED_BEACONS);
1525 bcon_trig = (void *)trigger->data;
1526 stop_trig_missed_bcon = le32_to_cpu(bcon_trig->stop_consec_missed_bcon);
1527 stop_trig_missed_bcon_since_rx =
1528 le32_to_cpu(bcon_trig->stop_consec_missed_bcon_since_rx);
1529
1530 /* TODO: implement start trigger */
1531
1532 if (!iwl_fw_dbg_trigger_check_stop(mvm, vif, trigger))
1533 return;
1534
1535 if (rx_missed_bcon_since_rx >= stop_trig_missed_bcon_since_rx ||
1536 rx_missed_bcon >= stop_trig_missed_bcon)
Johannes Berg5d4f9292015-03-31 09:12:54 +02001537 iwl_mvm_fw_dbg_collect_trig(mvm, trigger, NULL);
Hila Gonend64048e2013-03-13 18:00:03 +02001538}
1539
Johannes Berg04168412015-06-23 21:22:09 +02001540void iwl_mvm_rx_missed_beacons_notif(struct iwl_mvm *mvm,
1541 struct iwl_rx_cmd_buffer *rxb)
Hila Gonend64048e2013-03-13 18:00:03 +02001542{
1543 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Ilan Peer12d423e2013-12-24 22:08:14 +02001544 struct iwl_missed_beacons_notif *mb = (void *)pkt->data;
1545
1546 IWL_DEBUG_INFO(mvm,
1547 "missed bcn mac_id=%u, consecutive=%u (%u, %u, %u)\n",
1548 le32_to_cpu(mb->mac_id),
1549 le32_to_cpu(mb->consec_missed_beacons),
1550 le32_to_cpu(mb->consec_missed_beacons_since_last_rx),
1551 le32_to_cpu(mb->num_recvd_beacons),
1552 le32_to_cpu(mb->num_expected_beacons));
Hila Gonend64048e2013-03-13 18:00:03 +02001553
1554 ieee80211_iterate_active_interfaces_atomic(mvm->hw,
1555 IEEE80211_IFACE_ITER_NORMAL,
1556 iwl_mvm_beacon_loss_iterator,
Ilan Peer12d423e2013-12-24 22:08:14 +02001557 mb);
Hila Gonend64048e2013-03-13 18:00:03 +02001558}
Sara Sharon0db056d2015-12-29 11:07:15 +02001559
1560void iwl_mvm_rx_stored_beacon_notif(struct iwl_mvm *mvm,
1561 struct iwl_rx_cmd_buffer *rxb)
1562{
1563 struct iwl_rx_packet *pkt = rxb_addr(rxb);
1564 struct iwl_stored_beacon_notif *sb = (void *)pkt->data;
1565 struct ieee80211_rx_status rx_status;
1566 struct sk_buff *skb;
1567 u32 size = le32_to_cpu(sb->byte_count);
1568
1569 if (size == 0)
1570 return;
1571
1572 skb = alloc_skb(size, GFP_ATOMIC);
1573 if (!skb) {
1574 IWL_ERR(mvm, "alloc_skb failed\n");
1575 return;
1576 }
1577
1578 /* update rx_status according to the notification's metadata */
1579 memset(&rx_status, 0, sizeof(rx_status));
1580 rx_status.mactime = le64_to_cpu(sb->tsf);
Sara Sharon77fe7392016-01-10 14:23:25 +02001581 /* TSF as indicated by the firmware is at INA time */
1582 rx_status.flag |= RX_FLAG_MACTIME_PLCP_START;
Sara Sharon0db056d2015-12-29 11:07:15 +02001583 rx_status.device_timestamp = le32_to_cpu(sb->system_time);
1584 rx_status.band =
Sara Sharon34118c22016-11-08 18:34:42 +02001585 (sb->band & cpu_to_le16(RX_RES_PHY_FLAGS_BAND_24)) ?
Johannes Berg57fbcce2016-04-12 15:56:15 +02001586 NL80211_BAND_2GHZ : NL80211_BAND_5GHZ;
Sara Sharon0db056d2015-12-29 11:07:15 +02001587 rx_status.freq =
1588 ieee80211_channel_to_frequency(le16_to_cpu(sb->channel),
1589 rx_status.band);
1590
1591 /* copy the data */
1592 memcpy(skb_put(skb, size), sb->data, size);
1593 memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
1594
1595 /* pass it as regular rx to mac80211 */
Johannes Bergd63b5482016-03-31 20:02:02 +03001596 ieee80211_rx_napi(mvm->hw, NULL, skb, NULL);
Sara Sharon0db056d2015-12-29 11:07:15 +02001597}
Andrei Otcheretianskid3a108a2016-02-28 17:12:21 +02001598
1599void iwl_mvm_channel_switch_noa_notif(struct iwl_mvm *mvm,
1600 struct iwl_rx_cmd_buffer *rxb)
1601{
1602 struct iwl_rx_packet *pkt = rxb_addr(rxb);
1603 struct iwl_channel_switch_noa_notif *notif = (void *)pkt->data;
1604 struct ieee80211_vif *csa_vif;
1605 struct iwl_mvm_vif *mvmvif;
1606 int len = iwl_rx_packet_payload_len(pkt);
1607 u32 id_n_color;
1608
1609 if (WARN_ON_ONCE(len < sizeof(*notif)))
1610 return;
1611
1612 rcu_read_lock();
1613
1614 csa_vif = rcu_dereference(mvm->csa_vif);
1615 if (WARN_ON(!csa_vif || !csa_vif->csa_active))
1616 goto out_unlock;
1617
1618 id_n_color = le32_to_cpu(notif->id_and_color);
1619
1620 mvmvif = iwl_mvm_vif_from_mac80211(csa_vif);
1621 if (WARN(FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color) != id_n_color,
1622 "channel switch noa notification on unexpected vif (csa_vif=%d, notif=%d)",
1623 FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color), id_n_color))
1624 goto out_unlock;
1625
1626 IWL_DEBUG_INFO(mvm, "Channel Switch Started Notification\n");
1627
1628 queue_delayed_work(system_wq, &mvm->cs_tx_unblock_dwork,
1629 msecs_to_jiffies(IWL_MVM_CS_UNBLOCK_TX_TIMEOUT *
1630 csa_vif->bss_conf.beacon_int));
1631
1632 ieee80211_csa_finish(csa_vif);
1633
1634 rcu_read_unlock();
1635
1636 RCU_INIT_POINTER(mvm->csa_vif, NULL);
1637
1638 return;
1639
1640out_unlock:
1641 rcu_read_unlock();
1642}