iwlwifi: mvm: use tdls indication from mac80211
[linux-2.6-block.git] / drivers / net / wireless / iwlwifi / mvm / mac-ctxt.c
CommitLineData
8ca151b5
JB
1/******************************************************************************
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 *
51368bf7 8 * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
8b4139dc 9 * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
8ca151b5
JB
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of version 2 of the GNU General Public License as
13 * published by the Free Software Foundation.
14 *
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
23 * USA
24 *
25 * The full GNU General Public License is included in this distribution
410dc5aa 26 * in the file called COPYING.
8ca151b5
JB
27 *
28 * Contact Information:
29 * Intel Linux Wireless <ilw@linux.intel.com>
30 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
31 *
32 * BSD LICENSE
33 *
51368bf7 34 * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
8b4139dc 35 * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
8ca151b5
JB
36 * All rights reserved.
37 *
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
40 * are met:
41 *
42 * * Redistributions of source code must retain the above copyright
43 * notice, this list of conditions and the following disclaimer.
44 * * Redistributions in binary form must reproduce the above copyright
45 * notice, this list of conditions and the following disclaimer in
46 * the documentation and/or other materials provided with the
47 * distribution.
48 * * Neither the name Intel Corporation nor the names of its
49 * contributors may be used to endorse or promote products derived
50 * from this software without specific prior written permission.
51 *
52 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
53 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
54 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
55 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
56 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
57 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
58 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
59 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
60 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
61 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
62 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
63 *
64 *****************************************************************************/
65
66#include <linux/etherdevice.h>
67#include <net/mac80211.h>
68#include "iwl-io.h"
69#include "iwl-prph.h"
70#include "fw-api.h"
71#include "mvm.h"
7f0a7c67 72#include "time-event.h"
8ca151b5
JB
73
74const u8 iwl_mvm_ac_to_tx_fifo[] = {
8ca151b5 75 IWL_MVM_TX_FIFO_VO,
3dd94794
EG
76 IWL_MVM_TX_FIFO_VI,
77 IWL_MVM_TX_FIFO_BE,
78 IWL_MVM_TX_FIFO_BK,
8ca151b5
JB
79};
80
81struct iwl_mvm_mac_iface_iterator_data {
82 struct iwl_mvm *mvm;
83 struct ieee80211_vif *vif;
84 unsigned long available_mac_ids[BITS_TO_LONGS(NUM_MAC_INDEX_DRIVER)];
85 unsigned long available_tsf_ids[BITS_TO_LONGS(NUM_TSF_IDS)];
19e737c9 86 unsigned long used_hw_queues[BITS_TO_LONGS(IWL_MVM_MAX_QUEUES)];
8ca151b5
JB
87 enum iwl_tsf_id preferred_tsf;
88 bool found_vif;
89};
90
6e97b0d2
IP
91static void iwl_mvm_mac_tsf_id_iter(void *_data, u8 *mac,
92 struct ieee80211_vif *vif)
8ca151b5
JB
93{
94 struct iwl_mvm_mac_iface_iterator_data *data = _data;
95 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2c3e62a1 96 u16 min_bi;
8ca151b5 97
6e97b0d2
IP
98 /* Skip the interface for which we are trying to assign a tsf_id */
99 if (vif == data->vif)
8ca151b5 100 return;
8ca151b5
JB
101
102 /*
103 * The TSF is a hardware/firmware resource, there are 4 and
104 * the driver should assign and free them as needed. However,
105 * there are cases where 2 MACs should share the same TSF ID
106 * for the purpose of clock sync, an optimization to avoid
107 * clock drift causing overlapping TBTTs/DTIMs for a GO and
108 * client in the system.
109 *
110 * The firmware will decide according to the MAC type which
111 * will be the master and slave. Clients that need to sync
112 * with a remote station will be the master, and an AP or GO
113 * will be the slave.
114 *
115 * Depending on the new interface type it can be slaved to
116 * or become the master of an existing interface.
117 */
118 switch (data->vif->type) {
119 case NL80211_IFTYPE_STATION:
120 /*
2c3e62a1
IP
121 * The new interface is a client, so if the one we're iterating
122 * is an AP, and the beacon interval of the AP is a multiple or
123 * divisor of the beacon interval of the client, the same TSF
124 * should be used to avoid drift between the new client and
125 * existing AP. The existing AP will get drift updates from the
126 * new client context in this case.
8ca151b5 127 */
2c3e62a1
IP
128 if (vif->type != NL80211_IFTYPE_AP ||
129 data->preferred_tsf != NUM_TSF_IDS ||
130 !test_bit(mvmvif->tsf_id, data->available_tsf_ids))
131 break;
132
133 min_bi = min(data->vif->bss_conf.beacon_int,
134 vif->bss_conf.beacon_int);
135
136 if (!min_bi)
137 break;
138
139 if ((data->vif->bss_conf.beacon_int -
140 vif->bss_conf.beacon_int) % min_bi == 0) {
141 data->preferred_tsf = mvmvif->tsf_id;
142 return;
8ca151b5
JB
143 }
144 break;
2c3e62a1 145
8ca151b5
JB
146 case NL80211_IFTYPE_AP:
147 /*
2c3e62a1
IP
148 * The new interface is AP/GO, so if its beacon interval is a
149 * multiple or a divisor of the beacon interval of an existing
150 * interface, it should get drift updates from an existing
151 * client or use the same TSF as an existing GO. There's no
152 * drift between TSFs internally but if they used different
153 * TSFs then a new client MAC could update one of them and
154 * cause drift that way.
8ca151b5 155 */
2c3e62a1
IP
156 if ((vif->type != NL80211_IFTYPE_AP &&
157 vif->type != NL80211_IFTYPE_STATION) ||
158 data->preferred_tsf != NUM_TSF_IDS ||
159 !test_bit(mvmvif->tsf_id, data->available_tsf_ids))
160 break;
161
162 min_bi = min(data->vif->bss_conf.beacon_int,
163 vif->bss_conf.beacon_int);
164
165 if (!min_bi)
166 break;
167
168 if ((data->vif->bss_conf.beacon_int -
169 vif->bss_conf.beacon_int) % min_bi == 0) {
170 data->preferred_tsf = mvmvif->tsf_id;
171 return;
8ca151b5
JB
172 }
173 break;
174 default:
175 /*
176 * For all other interface types there's no need to
177 * take drift into account. Either they're exclusive
178 * like IBSS and monitor, or we don't care much about
179 * their TSF (like P2P Device), but we won't be able
180 * to share the TSF resource.
181 */
182 break;
183 }
184
185 /*
186 * Unless we exited above, we can't share the TSF resource
187 * that the virtual interface we're iterating over is using
188 * with the new one, so clear the available bit and if this
189 * was the preferred one, reset that as well.
190 */
191 __clear_bit(mvmvif->tsf_id, data->available_tsf_ids);
192
193 if (data->preferred_tsf == mvmvif->tsf_id)
194 data->preferred_tsf = NUM_TSF_IDS;
195}
196
6e97b0d2
IP
197static void iwl_mvm_mac_iface_iterator(void *_data, u8 *mac,
198 struct ieee80211_vif *vif)
199{
200 struct iwl_mvm_mac_iface_iterator_data *data = _data;
201 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
202 u32 ac;
203
204 /* Iterator may already find the interface being added -- skip it */
205 if (vif == data->vif) {
206 data->found_vif = true;
207 return;
208 }
209
210 /* Mark the queues used by the vif */
211 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
212 if (vif->hw_queue[ac] != IEEE80211_INVAL_HW_QUEUE)
213 __set_bit(vif->hw_queue[ac], data->used_hw_queues);
214
215 if (vif->cab_queue != IEEE80211_INVAL_HW_QUEUE)
216 __set_bit(vif->cab_queue, data->used_hw_queues);
217
218 /* Mark MAC IDs as used by clearing the available bit, and
219 * (below) mark TSFs as used if their existing use is not
220 * compatible with the new interface type.
221 * No locking or atomic bit operations are needed since the
222 * data is on the stack of the caller function.
223 */
224 __clear_bit(mvmvif->id, data->available_mac_ids);
225
226 /* find a suitable tsf_id */
227 iwl_mvm_mac_tsf_id_iter(_data, mac, vif);
228}
229
8ca151b5
JB
230/*
231 * Get the mask of the queus used by the vif
232 */
233u32 iwl_mvm_mac_get_queues_mask(struct iwl_mvm *mvm,
234 struct ieee80211_vif *vif)
235{
837fb69f 236 u32 qmask = 0, ac;
8ca151b5
JB
237
238 if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
398e8c6c 239 return BIT(IWL_MVM_OFFCHANNEL_QUEUE);
8ca151b5 240
8ca151b5
JB
241 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
242 if (vif->hw_queue[ac] != IEEE80211_INVAL_HW_QUEUE)
243 qmask |= BIT(vif->hw_queue[ac]);
244
245 return qmask;
246}
247
6e97b0d2
IP
248void iwl_mvm_mac_ctxt_recalc_tsf_id(struct iwl_mvm *mvm,
249 struct ieee80211_vif *vif)
250{
251 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
252 struct iwl_mvm_mac_iface_iterator_data data = {
253 .mvm = mvm,
254 .vif = vif,
255 .available_tsf_ids = { (1 << NUM_TSF_IDS) - 1 },
256 /* no preference yet */
257 .preferred_tsf = NUM_TSF_IDS,
258 };
259
260 ieee80211_iterate_active_interfaces_atomic(
261 mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
262 iwl_mvm_mac_tsf_id_iter, &data);
263
264 if (data.preferred_tsf != NUM_TSF_IDS)
265 mvmvif->tsf_id = data.preferred_tsf;
266 else if (!test_bit(mvmvif->tsf_id, data.available_tsf_ids))
267 mvmvif->tsf_id = find_first_bit(data.available_tsf_ids,
268 NUM_TSF_IDS);
269}
270
8ca151b5
JB
271static int iwl_mvm_mac_ctxt_allocate_resources(struct iwl_mvm *mvm,
272 struct ieee80211_vif *vif)
273{
274 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
275 struct iwl_mvm_mac_iface_iterator_data data = {
276 .mvm = mvm,
277 .vif = vif,
278 .available_mac_ids = { (1 << NUM_MAC_INDEX_DRIVER) - 1 },
279 .available_tsf_ids = { (1 << NUM_TSF_IDS) - 1 },
280 /* no preference yet */
281 .preferred_tsf = NUM_TSF_IDS,
282 .used_hw_queues = {
283 BIT(IWL_MVM_OFFCHANNEL_QUEUE) |
19e737c9 284 BIT(mvm->aux_queue) |
8ca151b5
JB
285 BIT(IWL_MVM_CMD_QUEUE)
286 },
287 .found_vif = false,
288 };
289 u32 ac;
9ee718aa 290 int ret, i;
8ca151b5
JB
291
292 /*
293 * Allocate a MAC ID and a TSF for this MAC, along with the queues
294 * and other resources.
295 */
296
297 /*
298 * Before the iterator, we start with all MAC IDs and TSFs available.
299 *
300 * During iteration, all MAC IDs are cleared that are in use by other
301 * virtual interfaces, and all TSF IDs are cleared that can't be used
302 * by this new virtual interface because they're used by an interface
303 * that can't share it with the new one.
304 * At the same time, we check if there's a preferred TSF in the case
305 * that we should share it with another interface.
306 */
307
5023d966
JB
308 /* Currently, MAC ID 0 should be used only for the managed/IBSS vif */
309 switch (vif->type) {
310 case NL80211_IFTYPE_ADHOC:
311 break;
312 case NL80211_IFTYPE_STATION:
313 if (!vif->p2p)
314 break;
315 /* fall through */
316 default:
ec8b6885 317 __clear_bit(0, data.available_mac_ids);
5023d966 318 }
ec8b6885 319
8ca151b5
JB
320 ieee80211_iterate_active_interfaces_atomic(
321 mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
322 iwl_mvm_mac_iface_iterator, &data);
323
324 /*
325 * In the case we're getting here during resume, it's similar to
326 * firmware restart, and with RESUME_ALL the iterator will find
327 * the vif being added already.
328 * We don't want to reassign any IDs in either case since doing
329 * so would probably assign different IDs (as interfaces aren't
330 * necessarily added in the same order), but the old IDs were
331 * preserved anyway, so skip ID assignment for both resume and
332 * recovery.
333 */
334 if (data.found_vif)
335 return 0;
336
337 /* Therefore, in recovery, we can't get here */
fd11bd05
IP
338 if (WARN_ON_ONCE(test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)))
339 return -EBUSY;
8ca151b5
JB
340
341 mvmvif->id = find_first_bit(data.available_mac_ids,
342 NUM_MAC_INDEX_DRIVER);
343 if (mvmvif->id == NUM_MAC_INDEX_DRIVER) {
344 IWL_ERR(mvm, "Failed to init MAC context - no free ID!\n");
345 ret = -EIO;
346 goto exit_fail;
347 }
348
349 if (data.preferred_tsf != NUM_TSF_IDS)
350 mvmvif->tsf_id = data.preferred_tsf;
351 else
352 mvmvif->tsf_id = find_first_bit(data.available_tsf_ids,
353 NUM_TSF_IDS);
354 if (mvmvif->tsf_id == NUM_TSF_IDS) {
355 IWL_ERR(mvm, "Failed to init MAC context - no free TSF!\n");
356 ret = -EIO;
357 goto exit_fail;
358 }
359
360 mvmvif->color = 0;
361
1e849c93
IP
362 INIT_LIST_HEAD(&mvmvif->time_event_data.list);
363 mvmvif->time_event_data.id = TE_MAX;
364
8ca151b5
JB
365 /* No need to allocate data queues to P2P Device MAC.*/
366 if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
367 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
368 vif->hw_queue[ac] = IEEE80211_INVAL_HW_QUEUE;
369
370 return 0;
371 }
372
373 /* Find available queues, and allocate them to the ACs */
374 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
375 u8 queue = find_first_zero_bit(data.used_hw_queues,
19e737c9 376 mvm->first_agg_queue);
8ca151b5 377
19e737c9 378 if (queue >= mvm->first_agg_queue) {
8ca151b5
JB
379 IWL_ERR(mvm, "Failed to allocate queue\n");
380 ret = -EIO;
381 goto exit_fail;
382 }
383
384 __set_bit(queue, data.used_hw_queues);
385 vif->hw_queue[ac] = queue;
386 }
387
388 /* Allocate the CAB queue for softAP and GO interfaces */
389 if (vif->type == NL80211_IFTYPE_AP) {
390 u8 queue = find_first_zero_bit(data.used_hw_queues,
19e737c9 391 mvm->first_agg_queue);
8ca151b5 392
19e737c9 393 if (queue >= mvm->first_agg_queue) {
8ca151b5
JB
394 IWL_ERR(mvm, "Failed to allocate cab queue\n");
395 ret = -EIO;
396 goto exit_fail;
397 }
398
399 vif->cab_queue = queue;
400 } else {
401 vif->cab_queue = IEEE80211_INVAL_HW_QUEUE;
402 }
403
404 mvmvif->bcast_sta.sta_id = IWL_MVM_STATION_COUNT;
405 mvmvif->ap_sta_id = IWL_MVM_STATION_COUNT;
406
9ee718aa
EL
407 for (i = 0; i < NUM_IWL_MVM_SMPS_REQ; i++)
408 mvmvif->smps_requests[i] = IEEE80211_SMPS_AUTOMATIC;
409
8ca151b5
JB
410 return 0;
411
412exit_fail:
413 memset(mvmvif, 0, sizeof(struct iwl_mvm_vif));
414 memset(vif->hw_queue, IEEE80211_INVAL_HW_QUEUE, sizeof(vif->hw_queue));
415 vif->cab_queue = IEEE80211_INVAL_HW_QUEUE;
416 return ret;
417}
418
419int iwl_mvm_mac_ctxt_init(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
420{
421 u32 ac;
422 int ret;
423
424 lockdep_assert_held(&mvm->mutex);
425
426 ret = iwl_mvm_mac_ctxt_allocate_resources(mvm, vif);
427 if (ret)
428 return ret;
429
430 switch (vif->type) {
431 case NL80211_IFTYPE_P2P_DEVICE:
432 iwl_trans_ac_txq_enable(mvm->trans, IWL_MVM_OFFCHANNEL_QUEUE,
433 IWL_MVM_TX_FIFO_VO);
434 break;
435 case NL80211_IFTYPE_AP:
436 iwl_trans_ac_txq_enable(mvm->trans, vif->cab_queue,
86a91ec7 437 IWL_MVM_TX_FIFO_MCAST);
8ca151b5
JB
438 /* fall through */
439 default:
440 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
441 iwl_trans_ac_txq_enable(mvm->trans, vif->hw_queue[ac],
442 iwl_mvm_ac_to_tx_fifo[ac]);
443 break;
444 }
445
446 return 0;
447}
448
449void iwl_mvm_mac_ctxt_release(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
450{
451 int ac;
452
453 lockdep_assert_held(&mvm->mutex);
454
455 switch (vif->type) {
456 case NL80211_IFTYPE_P2P_DEVICE:
d4578ea8
JB
457 iwl_trans_txq_disable(mvm->trans, IWL_MVM_OFFCHANNEL_QUEUE,
458 true);
8ca151b5
JB
459 break;
460 case NL80211_IFTYPE_AP:
d4578ea8 461 iwl_trans_txq_disable(mvm->trans, vif->cab_queue, true);
8ca151b5
JB
462 /* fall through */
463 default:
464 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
d4578ea8
JB
465 iwl_trans_txq_disable(mvm->trans, vif->hw_queue[ac],
466 true);
8ca151b5
JB
467 }
468}
469
470static void iwl_mvm_ack_rates(struct iwl_mvm *mvm,
471 struct ieee80211_vif *vif,
472 enum ieee80211_band band,
473 u8 *cck_rates, u8 *ofdm_rates)
474{
475 struct ieee80211_supported_band *sband;
476 unsigned long basic = vif->bss_conf.basic_rates;
477 int lowest_present_ofdm = 100;
478 int lowest_present_cck = 100;
479 u8 cck = 0;
480 u8 ofdm = 0;
481 int i;
482
483 sband = mvm->hw->wiphy->bands[band];
484
485 for_each_set_bit(i, &basic, BITS_PER_LONG) {
486 int hw = sband->bitrates[i].hw_value;
487 if (hw >= IWL_FIRST_OFDM_RATE) {
488 ofdm |= BIT(hw - IWL_FIRST_OFDM_RATE);
489 if (lowest_present_ofdm > hw)
490 lowest_present_ofdm = hw;
491 } else {
492 BUILD_BUG_ON(IWL_FIRST_CCK_RATE != 0);
493
494 cck |= BIT(hw);
495 if (lowest_present_cck > hw)
496 lowest_present_cck = hw;
497 }
498 }
499
500 /*
501 * Now we've got the basic rates as bitmaps in the ofdm and cck
502 * variables. This isn't sufficient though, as there might not
503 * be all the right rates in the bitmap. E.g. if the only basic
504 * rates are 5.5 Mbps and 11 Mbps, we still need to add 1 Mbps
505 * and 6 Mbps because the 802.11-2007 standard says in 9.6:
506 *
507 * [...] a STA responding to a received frame shall transmit
508 * its Control Response frame [...] at the highest rate in the
509 * BSSBasicRateSet parameter that is less than or equal to the
510 * rate of the immediately previous frame in the frame exchange
511 * sequence ([...]) and that is of the same modulation class
512 * ([...]) as the received frame. If no rate contained in the
513 * BSSBasicRateSet parameter meets these conditions, then the
514 * control frame sent in response to a received frame shall be
515 * transmitted at the highest mandatory rate of the PHY that is
516 * less than or equal to the rate of the received frame, and
517 * that is of the same modulation class as the received frame.
518 *
519 * As a consequence, we need to add all mandatory rates that are
520 * lower than all of the basic rates to these bitmaps.
521 */
522
523 if (IWL_RATE_24M_INDEX < lowest_present_ofdm)
524 ofdm |= IWL_RATE_BIT_MSK(24) >> IWL_FIRST_OFDM_RATE;
525 if (IWL_RATE_12M_INDEX < lowest_present_ofdm)
526 ofdm |= IWL_RATE_BIT_MSK(12) >> IWL_FIRST_OFDM_RATE;
527 /* 6M already there or needed so always add */
528 ofdm |= IWL_RATE_BIT_MSK(6) >> IWL_FIRST_OFDM_RATE;
529
530 /*
531 * CCK is a bit more complex with DSSS vs. HR/DSSS vs. ERP.
532 * Note, however:
533 * - if no CCK rates are basic, it must be ERP since there must
534 * be some basic rates at all, so they're OFDM => ERP PHY
535 * (or we're in 5 GHz, and the cck bitmap will never be used)
536 * - if 11M is a basic rate, it must be ERP as well, so add 5.5M
537 * - if 5.5M is basic, 1M and 2M are mandatory
538 * - if 2M is basic, 1M is mandatory
539 * - if 1M is basic, that's the only valid ACK rate.
540 * As a consequence, it's not as complicated as it sounds, just add
541 * any lower rates to the ACK rate bitmap.
542 */
543 if (IWL_RATE_11M_INDEX < lowest_present_cck)
544 cck |= IWL_RATE_BIT_MSK(11) >> IWL_FIRST_CCK_RATE;
545 if (IWL_RATE_5M_INDEX < lowest_present_cck)
546 cck |= IWL_RATE_BIT_MSK(5) >> IWL_FIRST_CCK_RATE;
547 if (IWL_RATE_2M_INDEX < lowest_present_cck)
548 cck |= IWL_RATE_BIT_MSK(2) >> IWL_FIRST_CCK_RATE;
549 /* 1M already there or needed so always add */
550 cck |= IWL_RATE_BIT_MSK(1) >> IWL_FIRST_CCK_RATE;
551
552 *cck_rates = cck;
553 *ofdm_rates = ofdm;
554}
555
8a5e3660
AA
556static void iwl_mvm_mac_ctxt_set_ht_flags(struct iwl_mvm *mvm,
557 struct ieee80211_vif *vif,
558 struct iwl_mac_ctx_cmd *cmd)
559{
560 /* for both sta and ap, ht_operation_mode hold the protection_mode */
561 u8 protection_mode = vif->bss_conf.ht_operation_mode &
562 IEEE80211_HT_OP_MODE_PROTECTION;
563 /* The fw does not distinguish between ht and fat */
564 u32 ht_flag = MAC_PROT_FLG_HT_PROT | MAC_PROT_FLG_FAT_PROT;
565
566 IWL_DEBUG_RATE(mvm, "protection mode set to %d\n", protection_mode);
567 /*
568 * See section 9.23.3.1 of IEEE 80211-2012.
569 * Nongreenfield HT STAs Present is not supported.
570 */
571 switch (protection_mode) {
572 case IEEE80211_HT_OP_MODE_PROTECTION_NONE:
573 break;
574 case IEEE80211_HT_OP_MODE_PROTECTION_NONMEMBER:
575 case IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED:
576 cmd->protection_flags |= cpu_to_le32(ht_flag);
577 break;
578 case IEEE80211_HT_OP_MODE_PROTECTION_20MHZ:
579 /* Protect when channel wider than 20MHz */
580 if (vif->bss_conf.chandef.width > NL80211_CHAN_WIDTH_20)
581 cmd->protection_flags |= cpu_to_le32(ht_flag);
582 break;
583 default:
584 IWL_ERR(mvm, "Illegal protection mode %d\n",
585 protection_mode);
586 break;
587 }
588}
589
8ca151b5
JB
590static void iwl_mvm_mac_ctxt_cmd_common(struct iwl_mvm *mvm,
591 struct ieee80211_vif *vif,
592 struct iwl_mac_ctx_cmd *cmd,
3dfd3a97 593 const u8 *bssid_override,
8ca151b5
JB
594 u32 action)
595{
596 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
597 struct ieee80211_chanctx_conf *chanctx;
8a5e3660
AA
598 bool ht_enabled = !!(vif->bss_conf.ht_operation_mode &
599 IEEE80211_HT_OP_MODE_PROTECTION);
8ca151b5 600 u8 cck_ack_rates, ofdm_ack_rates;
3dfd3a97 601 const u8 *bssid = bssid_override ?: vif->bss_conf.bssid;
8ca151b5
JB
602 int i;
603
604 cmd->id_and_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
605 mvmvif->color));
606 cmd->action = cpu_to_le32(action);
607
608 switch (vif->type) {
609 case NL80211_IFTYPE_STATION:
610 if (vif->p2p)
611 cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_P2P_STA);
612 else
613 cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_BSS_STA);
614 break;
615 case NL80211_IFTYPE_AP:
616 cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_GO);
617 break;
618 case NL80211_IFTYPE_MONITOR:
619 cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_LISTENER);
620 break;
621 case NL80211_IFTYPE_P2P_DEVICE:
622 cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_P2P_DEVICE);
623 break;
624 case NL80211_IFTYPE_ADHOC:
625 cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_IBSS);
626 break;
627 default:
628 WARN_ON_ONCE(1);
629 }
630
631 cmd->tsf_id = cpu_to_le32(mvmvif->tsf_id);
632
633 memcpy(cmd->node_addr, vif->addr, ETH_ALEN);
3dfd3a97
JB
634
635 if (bssid)
636 memcpy(cmd->bssid_addr, bssid, ETH_ALEN);
8ca151b5
JB
637 else
638 eth_broadcast_addr(cmd->bssid_addr);
639
640 rcu_read_lock();
641 chanctx = rcu_dereference(vif->chanctx_conf);
642 iwl_mvm_ack_rates(mvm, vif, chanctx ? chanctx->def.chan->band
643 : IEEE80211_BAND_2GHZ,
644 &cck_ack_rates, &ofdm_ack_rates);
645 rcu_read_unlock();
646
647 cmd->cck_rates = cpu_to_le32((u32)cck_ack_rates);
648 cmd->ofdm_rates = cpu_to_le32((u32)ofdm_ack_rates);
649
650 cmd->cck_short_preamble =
651 cpu_to_le32(vif->bss_conf.use_short_preamble ?
652 MAC_FLG_SHORT_PREAMBLE : 0);
653 cmd->short_slot =
654 cpu_to_le32(vif->bss_conf.use_short_slot ?
655 MAC_FLG_SHORT_SLOT : 0);
656
3dd94794
EG
657 for (i = 0; i < IEEE80211_NUM_ACS; i++) {
658 u8 txf = iwl_mvm_ac_to_tx_fifo[i];
659
660 cmd->ac[txf].cw_min =
661 cpu_to_le16(mvmvif->queue_params[i].cw_min);
662 cmd->ac[txf].cw_max =
663 cpu_to_le16(mvmvif->queue_params[i].cw_max);
664 cmd->ac[txf].edca_txop =
8ca151b5 665 cpu_to_le16(mvmvif->queue_params[i].txop * 32);
3dd94794
EG
666 cmd->ac[txf].aifsn = mvmvif->queue_params[i].aifs;
667 cmd->ac[txf].fifos_mask = BIT(txf);
8ca151b5
JB
668 }
669
86a91ec7
EG
670 /* in AP mode, the MCAST FIFO takes the EDCA params from VO */
671 if (vif->type == NL80211_IFTYPE_AP)
3dd94794
EG
672 cmd->ac[IWL_MVM_TX_FIFO_VO].fifos_mask |=
673 BIT(IWL_MVM_TX_FIFO_MCAST);
86a91ec7 674
8ca151b5
JB
675 if (vif->bss_conf.qos)
676 cmd->qos_flags |= cpu_to_le32(MAC_QOS_FLG_UPDATE_EDCA);
677
dc271ee0 678 if (vif->bss_conf.use_cts_prot)
cc7ee2ba 679 cmd->protection_flags |= cpu_to_le32(MAC_PROT_FLG_TGG_PROTECT);
dc271ee0 680
8a5e3660
AA
681 IWL_DEBUG_RATE(mvm, "use_cts_prot %d, ht_operation_mode %d\n",
682 vif->bss_conf.use_cts_prot,
683 vif->bss_conf.ht_operation_mode);
684 if (vif->bss_conf.chandef.width != NL80211_CHAN_WIDTH_20_NOHT)
8ca151b5 685 cmd->qos_flags |= cpu_to_le32(MAC_QOS_FLG_TGN);
8a5e3660
AA
686 if (ht_enabled)
687 iwl_mvm_mac_ctxt_set_ht_flags(mvm, vif, cmd);
8ca151b5
JB
688
689 cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP);
690}
691
692static int iwl_mvm_mac_ctxt_send_cmd(struct iwl_mvm *mvm,
693 struct iwl_mac_ctx_cmd *cmd)
694{
a1022927 695 int ret = iwl_mvm_send_cmd_pdu(mvm, MAC_CONTEXT_CMD, 0,
8ca151b5
JB
696 sizeof(*cmd), cmd);
697 if (ret)
698 IWL_ERR(mvm, "Failed to send MAC context (action:%d): %d\n",
699 le32_to_cpu(cmd->action), ret);
700 return ret;
701}
702
cf52023c
LC
703static int iwl_mvm_mac_ctxt_cmd_sta(struct iwl_mvm *mvm,
704 struct ieee80211_vif *vif,
3dfd3a97
JB
705 u32 action, bool force_assoc_off,
706 const u8 *bssid_override)
8ca151b5 707{
cf52023c
LC
708 struct iwl_mac_ctx_cmd cmd = {};
709 struct iwl_mac_data_sta *ctxt_sta;
710
711 WARN_ON(vif->type != NL80211_IFTYPE_STATION);
712
713 /* Fill the common data for all mac context types */
3dfd3a97 714 iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, bssid_override, action);
cf52023c
LC
715
716 if (vif->p2p) {
717 struct ieee80211_p2p_noa_attr *noa =
718 &vif->bss_conf.p2p_noa_attr;
719
720 cmd.p2p_sta.ctwin = cpu_to_le32(noa->oppps_ctwindow &
721 IEEE80211_P2P_OPPPS_CTWINDOW_MASK);
722 ctxt_sta = &cmd.p2p_sta.sta;
723 } else {
cf52023c
LC
724 ctxt_sta = &cmd.sta;
725 }
726
210a544e 727 /* We need the dtim_period to set the MAC as associated */
ba283927
AB
728 if (vif->bss_conf.assoc && vif->bss_conf.dtim_period &&
729 !force_assoc_off) {
d2931bbd
JB
730 u32 dtim_offs;
731
cad3f08c
LC
732 /* Allow beacons to pass through as long as we are not
733 * associated, or we do not have dtim period information.
734 */
53446699 735 cmd.filter_flags |= cpu_to_le32(MAC_FILTER_IN_BEACON);
cad3f08c 736
d2931bbd
JB
737 /*
738 * The DTIM count counts down, so when it is N that means N
739 * more beacon intervals happen until the DTIM TBTT. Therefore
740 * add this to the current time. If that ends up being in the
741 * future, the firmware will handle it.
742 *
743 * Also note that the system_timestamp (which we get here as
744 * "sync_device_ts") and TSF timestamp aren't at exactly the
745 * same offset in the frame -- the TSF is at the first symbol
746 * of the TSF, the system timestamp is at signal acquisition
747 * time. This means there's an offset between them of at most
748 * a few hundred microseconds (24 * 8 bits + PLCP time gives
749 * 384us in the longest case), this is currently not relevant
750 * as the firmware wakes up around 2ms before the TBTT.
751 */
752 dtim_offs = vif->bss_conf.sync_dtim_count *
753 vif->bss_conf.beacon_int;
754 /* convert TU to usecs */
755 dtim_offs *= 1024;
756
757 ctxt_sta->dtim_tsf =
758 cpu_to_le64(vif->bss_conf.sync_tsf + dtim_offs);
759 ctxt_sta->dtim_time =
760 cpu_to_le32(vif->bss_conf.sync_device_ts + dtim_offs);
761
762 IWL_DEBUG_INFO(mvm, "DTIM TBTT is 0x%llx/0x%x, offset %d\n",
763 le64_to_cpu(ctxt_sta->dtim_tsf),
764 le32_to_cpu(ctxt_sta->dtim_time),
765 dtim_offs);
766
210a544e 767 ctxt_sta->is_assoc = cpu_to_le32(1);
d2931bbd 768 } else {
210a544e 769 ctxt_sta->is_assoc = cpu_to_le32(0);
d2931bbd 770 }
8ca151b5
JB
771
772 ctxt_sta->bi = cpu_to_le32(vif->bss_conf.beacon_int);
773 ctxt_sta->bi_reciprocal =
774 cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int));
775 ctxt_sta->dtim_interval = cpu_to_le32(vif->bss_conf.beacon_int *
776 vif->bss_conf.dtim_period);
777 ctxt_sta->dtim_reciprocal =
778 cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int *
779 vif->bss_conf.dtim_period));
780
781 ctxt_sta->listen_interval = cpu_to_le32(mvm->hw->conf.listen_interval);
782 ctxt_sta->assoc_id = cpu_to_le32(vif->bss_conf.aid);
8ca151b5
JB
783
784 return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
785}
786
787static int iwl_mvm_mac_ctxt_cmd_listener(struct iwl_mvm *mvm,
788 struct ieee80211_vif *vif,
789 u32 action)
790{
791 struct iwl_mac_ctx_cmd cmd = {};
792
793 WARN_ON(vif->type != NL80211_IFTYPE_MONITOR);
794
3dfd3a97 795 iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
53585495
JB
796
797 cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_PROMISC |
798 MAC_FILTER_IN_CONTROL_AND_MGMT |
799 MAC_FILTER_IN_BEACON |
fb8b8ee1
JB
800 MAC_FILTER_IN_PROBE_REQUEST |
801 MAC_FILTER_IN_CRC32);
802 mvm->hw->flags |= IEEE80211_HW_RX_INCLUDES_FCS;
53585495 803
8ca151b5
JB
804 return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
805}
806
5023d966
JB
807static int iwl_mvm_mac_ctxt_cmd_ibss(struct iwl_mvm *mvm,
808 struct ieee80211_vif *vif,
809 u32 action)
810{
811 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
812 struct iwl_mac_ctx_cmd cmd = {};
813
814 WARN_ON(vif->type != NL80211_IFTYPE_ADHOC);
815
3dfd3a97 816 iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
5023d966
JB
817
818 cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_BEACON |
819 MAC_FILTER_IN_PROBE_REQUEST);
820
821 /* cmd.ibss.beacon_time/cmd.ibss.beacon_tsf are curently ignored */
822 cmd.ibss.bi = cpu_to_le32(vif->bss_conf.beacon_int);
823 cmd.ibss.bi_reciprocal =
824 cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int));
825
826 /* TODO: Assumes that the beacon id == mac context id */
827 cmd.ibss.beacon_template = cpu_to_le32(mvmvif->id);
828
829 return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
830}
831
8ca151b5
JB
832struct iwl_mvm_go_iterator_data {
833 bool go_active;
834};
835
836static void iwl_mvm_go_iterator(void *_data, u8 *mac, struct ieee80211_vif *vif)
837{
838 struct iwl_mvm_go_iterator_data *data = _data;
839 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
840
5023d966
JB
841 if (vif->type == NL80211_IFTYPE_AP && vif->p2p &&
842 mvmvif->ap_ibss_active)
8ca151b5
JB
843 data->go_active = true;
844}
845
846static int iwl_mvm_mac_ctxt_cmd_p2p_device(struct iwl_mvm *mvm,
847 struct ieee80211_vif *vif,
848 u32 action)
849{
850 struct iwl_mac_ctx_cmd cmd = {};
851 struct iwl_mvm_go_iterator_data data = {};
852
853 WARN_ON(vif->type != NL80211_IFTYPE_P2P_DEVICE);
854
3dfd3a97 855 iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
8ca151b5
JB
856
857 cmd.protection_flags |= cpu_to_le32(MAC_PROT_FLG_TGG_PROTECT);
1dcd15ee
IP
858
859 /* Override the filter flags to accept only probe requests */
860 cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_PROBE_REQUEST);
8ca151b5
JB
861
862 /*
863 * This flag should be set to true when the P2P Device is
864 * discoverable and there is at least another active P2P GO. Settings
865 * this flag will allow the P2P Device to be discoverable on other
866 * channels in addition to its listen channel.
867 * Note that this flag should not be set in other cases as it opens the
868 * Rx filters on all MAC and increases the number of interrupts.
869 */
870 ieee80211_iterate_active_interfaces_atomic(
871 mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
872 iwl_mvm_go_iterator, &data);
873
874 cmd.p2p_dev.is_disc_extended = cpu_to_le32(data.go_active ? 1 : 0);
875 return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
876}
877
878static void iwl_mvm_mac_ctxt_set_tim(struct iwl_mvm *mvm,
879 struct iwl_mac_beacon_cmd *beacon_cmd,
880 u8 *beacon, u32 frame_size)
881{
882 u32 tim_idx;
883 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)beacon;
884
885 /* The index is relative to frame start but we start looking at the
886 * variable-length part of the beacon. */
887 tim_idx = mgmt->u.beacon.variable - beacon;
888
889 /* Parse variable-length elements of beacon to find WLAN_EID_TIM */
890 while ((tim_idx < (frame_size - 2)) &&
891 (beacon[tim_idx] != WLAN_EID_TIM))
892 tim_idx += beacon[tim_idx+1] + 2;
893
894 /* If TIM field was found, set variables */
895 if ((tim_idx < (frame_size - 1)) && (beacon[tim_idx] == WLAN_EID_TIM)) {
896 beacon_cmd->tim_idx = cpu_to_le32(tim_idx);
897 beacon_cmd->tim_size = cpu_to_le32((u32)beacon[tim_idx+1]);
898 } else {
899 IWL_WARN(mvm, "Unable to find TIM Element in beacon\n");
900 }
901}
902
903static int iwl_mvm_mac_ctxt_send_beacon(struct iwl_mvm *mvm,
904 struct ieee80211_vif *vif,
905 struct sk_buff *beacon)
906{
907 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
908 struct iwl_host_cmd cmd = {
909 .id = BEACON_TEMPLATE_CMD,
910 .flags = CMD_ASYNC,
911 };
912 struct iwl_mac_beacon_cmd beacon_cmd = {};
913 struct ieee80211_tx_info *info;
914 u32 beacon_skb_len;
75f6b9b6 915 u32 rate, tx_flags;
8ca151b5
JB
916
917 if (WARN_ON(!beacon))
918 return -EINVAL;
919
920 beacon_skb_len = beacon->len;
921
922 /* TODO: for now the beacon template id is set to be the mac context id.
923 * Might be better to handle it as another resource ... */
924 beacon_cmd.template_id = cpu_to_le32((u32)mvmvif->id);
75f6b9b6 925 info = IEEE80211_SKB_CB(beacon);
8ca151b5
JB
926
927 /* Set up TX command fields */
928 beacon_cmd.tx.len = cpu_to_le16((u16)beacon_skb_len);
929 beacon_cmd.tx.sta_id = mvmvif->bcast_sta.sta_id;
930 beacon_cmd.tx.life_time = cpu_to_le32(TX_CMD_LIFE_TIME_INFINITE);
75f6b9b6
EG
931 tx_flags = TX_CMD_FLG_SEQ_CTL | TX_CMD_FLG_TSF;
932 tx_flags |=
933 iwl_mvm_bt_coex_tx_prio(mvm, (void *)beacon->data, info, 0) <<
934 TX_CMD_FLG_BT_PRIO_POS;
935 beacon_cmd.tx.tx_flags = cpu_to_le32(tx_flags);
8ca151b5
JB
936
937 mvm->mgmt_last_antenna_idx =
4ed735e7 938 iwl_mvm_next_antenna(mvm, mvm->fw->valid_tx_ant,
8ca151b5
JB
939 mvm->mgmt_last_antenna_idx);
940
941 beacon_cmd.tx.rate_n_flags =
942 cpu_to_le32(BIT(mvm->mgmt_last_antenna_idx) <<
943 RATE_MCS_ANT_POS);
944
8ca151b5
JB
945 if (info->band == IEEE80211_BAND_5GHZ || vif->p2p) {
946 rate = IWL_FIRST_OFDM_RATE;
947 } else {
948 rate = IWL_FIRST_CCK_RATE;
949 beacon_cmd.tx.rate_n_flags |= cpu_to_le32(RATE_MCS_CCK_MSK);
950 }
951 beacon_cmd.tx.rate_n_flags |=
952 cpu_to_le32(iwl_mvm_mac80211_idx_to_hwrate(rate));
953
954 /* Set up TX beacon command fields */
5023d966
JB
955 if (vif->type == NL80211_IFTYPE_AP)
956 iwl_mvm_mac_ctxt_set_tim(mvm, &beacon_cmd,
957 beacon->data,
958 beacon_skb_len);
8ca151b5
JB
959
960 /* Submit command */
961 cmd.len[0] = sizeof(beacon_cmd);
962 cmd.data[0] = &beacon_cmd;
963 cmd.dataflags[0] = 0;
964 cmd.len[1] = beacon_skb_len;
965 cmd.data[1] = beacon->data;
966 cmd.dataflags[1] = IWL_HCMD_DFL_DUP;
967
968 return iwl_mvm_send_cmd(mvm, &cmd);
969}
970
5023d966 971/* The beacon template for the AP/GO/IBSS has changed and needs update */
8ca151b5
JB
972int iwl_mvm_mac_ctxt_beacon_changed(struct iwl_mvm *mvm,
973 struct ieee80211_vif *vif)
974{
975 struct sk_buff *beacon;
976 int ret;
977
5023d966
JB
978 WARN_ON(vif->type != NL80211_IFTYPE_AP &&
979 vif->type != NL80211_IFTYPE_ADHOC);
8ca151b5 980
fe887665 981 beacon = ieee80211_beacon_get_template(mvm->hw, vif, NULL);
8ca151b5
JB
982 if (!beacon)
983 return -ENOMEM;
984
985 ret = iwl_mvm_mac_ctxt_send_beacon(mvm, vif, beacon);
986 dev_kfree_skb(beacon);
987 return ret;
988}
989
3a3cb92e
IP
990struct iwl_mvm_mac_ap_iterator_data {
991 struct iwl_mvm *mvm;
992 struct ieee80211_vif *vif;
993 u32 beacon_device_ts;
994 u16 beacon_int;
995};
996
997/* Find the beacon_device_ts and beacon_int for a managed interface */
998static void iwl_mvm_mac_ap_iterator(void *_data, u8 *mac,
999 struct ieee80211_vif *vif)
1000{
1001 struct iwl_mvm_mac_ap_iterator_data *data = _data;
1002
1003 if (vif->type != NL80211_IFTYPE_STATION || !vif->bss_conf.assoc)
1004 return;
1005
1006 /* Station client has higher priority over P2P client*/
1007 if (vif->p2p && data->beacon_device_ts)
1008 return;
1009
1010 data->beacon_device_ts = vif->bss_conf.sync_device_ts;
1011 data->beacon_int = vif->bss_conf.beacon_int;
1012}
1013
8ca151b5
JB
1014/*
1015 * Fill the specific data for mac context of type AP of P2P GO
1016 */
1017static void iwl_mvm_mac_ctxt_cmd_fill_ap(struct iwl_mvm *mvm,
1018 struct ieee80211_vif *vif,
506a81e6
JB
1019 struct iwl_mac_data_ap *ctxt_ap,
1020 bool add)
8ca151b5
JB
1021{
1022 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3a3cb92e
IP
1023 struct iwl_mvm_mac_ap_iterator_data data = {
1024 .mvm = mvm,
1025 .vif = vif,
1026 .beacon_device_ts = 0
1027 };
8ca151b5
JB
1028
1029 ctxt_ap->bi = cpu_to_le32(vif->bss_conf.beacon_int);
1030 ctxt_ap->bi_reciprocal =
1031 cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int));
1032 ctxt_ap->dtim_interval = cpu_to_le32(vif->bss_conf.beacon_int *
1033 vif->bss_conf.dtim_period);
1034 ctxt_ap->dtim_reciprocal =
1035 cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int *
1036 vif->bss_conf.dtim_period));
1037
1038 ctxt_ap->mcast_qid = cpu_to_le32(vif->cab_queue);
8ca151b5 1039
506a81e6 1040 /*
3a3cb92e 1041 * Only set the beacon time when the MAC is being added, when we
506a81e6
JB
1042 * just modify the MAC then we should keep the time -- the firmware
1043 * can otherwise have a "jumping" TBTT.
1044 */
3a3cb92e
IP
1045 if (add) {
1046 /*
1047 * If there is a station/P2P client interface which is
1048 * associated, set the AP's TBTT far enough from the station's
1049 * TBTT. Otherwise, set it to the current system time
1050 */
1051 ieee80211_iterate_active_interfaces_atomic(
1052 mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
1053 iwl_mvm_mac_ap_iterator, &data);
1054
1055 if (data.beacon_device_ts) {
9f8f8ca5 1056 u32 rand = (prandom_u32() % (64 - 36)) + 36;
3a3cb92e
IP
1057 mvmvif->ap_beacon_time = data.beacon_device_ts +
1058 ieee80211_tu_to_usec(data.beacon_int * rand /
1059 100);
1060 } else {
1061 mvmvif->ap_beacon_time =
1062 iwl_read_prph(mvm->trans,
1063 DEVICE_SYSTEM_TIME_REG);
1064 }
1065 }
506a81e6
JB
1066
1067 ctxt_ap->beacon_time = cpu_to_le32(mvmvif->ap_beacon_time);
506a81e6 1068 ctxt_ap->beacon_tsf = 0; /* unused */
8ca151b5
JB
1069
1070 /* TODO: Assume that the beacon id == mac context id */
1071 ctxt_ap->beacon_template = cpu_to_le32(mvmvif->id);
1072}
1073
1074static int iwl_mvm_mac_ctxt_cmd_ap(struct iwl_mvm *mvm,
1075 struct ieee80211_vif *vif,
1076 u32 action)
1077{
1078 struct iwl_mac_ctx_cmd cmd = {};
1079
1080 WARN_ON(vif->type != NL80211_IFTYPE_AP || vif->p2p);
1081
1082 /* Fill the common data for all mac context types */
3dfd3a97 1083 iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
8ca151b5 1084
46018794
EP
1085 /*
1086 * pass probe requests and beacons from other APs (needed
1087 * for ht protection)
1088 */
1089 cmd.filter_flags |= cpu_to_le32(MAC_FILTER_IN_PROBE_REQUEST |
1090 MAC_FILTER_IN_BEACON);
1dcd15ee 1091
8ca151b5 1092 /* Fill the data specific for ap mode */
506a81e6
JB
1093 iwl_mvm_mac_ctxt_cmd_fill_ap(mvm, vif, &cmd.ap,
1094 action == FW_CTXT_ACTION_ADD);
8ca151b5
JB
1095
1096 return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
1097}
1098
1099static int iwl_mvm_mac_ctxt_cmd_go(struct iwl_mvm *mvm,
1100 struct ieee80211_vif *vif,
1101 u32 action)
1102{
1103 struct iwl_mac_ctx_cmd cmd = {};
67baf663 1104 struct ieee80211_p2p_noa_attr *noa = &vif->bss_conf.p2p_noa_attr;
8ca151b5
JB
1105
1106 WARN_ON(vif->type != NL80211_IFTYPE_AP || !vif->p2p);
1107
1108 /* Fill the common data for all mac context types */
3dfd3a97 1109 iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
8ca151b5 1110
46018794
EP
1111 /*
1112 * pass probe requests and beacons from other APs (needed
1113 * for ht protection)
1114 */
1115 cmd.filter_flags |= cpu_to_le32(MAC_FILTER_IN_PROBE_REQUEST |
1116 MAC_FILTER_IN_BEACON);
1117
8ca151b5 1118 /* Fill the data specific for GO mode */
506a81e6
JB
1119 iwl_mvm_mac_ctxt_cmd_fill_ap(mvm, vif, &cmd.go.ap,
1120 action == FW_CTXT_ACTION_ADD);
8ca151b5 1121
67baf663
JD
1122 cmd.go.ctwin = cpu_to_le32(noa->oppps_ctwindow &
1123 IEEE80211_P2P_OPPPS_CTWINDOW_MASK);
1124 cmd.go.opp_ps_enabled =
1125 cpu_to_le32(!!(noa->oppps_ctwindow &
1126 IEEE80211_P2P_OPPPS_ENABLE_BIT));
8ca151b5
JB
1127
1128 return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
1129}
1130
1131static int iwl_mvm_mac_ctx_send(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
3dfd3a97
JB
1132 u32 action, bool force_assoc_off,
1133 const u8 *bssid_override)
8ca151b5
JB
1134{
1135 switch (vif->type) {
1136 case NL80211_IFTYPE_STATION:
cf52023c 1137 return iwl_mvm_mac_ctxt_cmd_sta(mvm, vif, action,
3dfd3a97
JB
1138 force_assoc_off,
1139 bssid_override);
8ca151b5
JB
1140 break;
1141 case NL80211_IFTYPE_AP:
1142 if (!vif->p2p)
1143 return iwl_mvm_mac_ctxt_cmd_ap(mvm, vif, action);
1144 else
1145 return iwl_mvm_mac_ctxt_cmd_go(mvm, vif, action);
1146 break;
1147 case NL80211_IFTYPE_MONITOR:
1148 return iwl_mvm_mac_ctxt_cmd_listener(mvm, vif, action);
1149 case NL80211_IFTYPE_P2P_DEVICE:
1150 return iwl_mvm_mac_ctxt_cmd_p2p_device(mvm, vif, action);
5023d966
JB
1151 case NL80211_IFTYPE_ADHOC:
1152 return iwl_mvm_mac_ctxt_cmd_ibss(mvm, vif, action);
8ca151b5
JB
1153 default:
1154 break;
1155 }
1156
1157 return -EOPNOTSUPP;
1158}
1159
1160int iwl_mvm_mac_ctxt_add(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1161{
1162 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1163 int ret;
1164
1165 if (WARN_ONCE(mvmvif->uploaded, "Adding active MAC %pM/%d\n",
1166 vif->addr, ieee80211_vif_type_p2p(vif)))
1167 return -EIO;
1168
bca49d9a 1169 ret = iwl_mvm_mac_ctx_send(mvm, vif, FW_CTXT_ACTION_ADD,
3dfd3a97 1170 true, NULL);
8ca151b5
JB
1171 if (ret)
1172 return ret;
1173
6d9d32b8
JB
1174 /* will only do anything at resume from D3 time */
1175 iwl_mvm_set_last_nonqos_seq(mvm, vif);
1176
8ca151b5
JB
1177 mvmvif->uploaded = true;
1178 return 0;
1179}
1180
bca49d9a 1181int iwl_mvm_mac_ctxt_changed(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
3dfd3a97 1182 bool force_assoc_off, const u8 *bssid_override)
8ca151b5
JB
1183{
1184 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1185
1186 if (WARN_ONCE(!mvmvif->uploaded, "Changing inactive MAC %pM/%d\n",
1187 vif->addr, ieee80211_vif_type_p2p(vif)))
1188 return -EIO;
1189
bca49d9a 1190 return iwl_mvm_mac_ctx_send(mvm, vif, FW_CTXT_ACTION_MODIFY,
3dfd3a97 1191 force_assoc_off, bssid_override);
8ca151b5
JB
1192}
1193
1194int iwl_mvm_mac_ctxt_remove(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1195{
1196 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1197 struct iwl_mac_ctx_cmd cmd;
1198 int ret;
1199
1200 if (WARN_ONCE(!mvmvif->uploaded, "Removing inactive MAC %pM/%d\n",
1201 vif->addr, ieee80211_vif_type_p2p(vif)))
1202 return -EIO;
1203
1204 memset(&cmd, 0, sizeof(cmd));
1205
1206 cmd.id_and_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
1207 mvmvif->color));
1208 cmd.action = cpu_to_le32(FW_CTXT_ACTION_REMOVE);
1209
a1022927 1210 ret = iwl_mvm_send_cmd_pdu(mvm, MAC_CONTEXT_CMD, 0,
8ca151b5
JB
1211 sizeof(cmd), &cmd);
1212 if (ret) {
1213 IWL_ERR(mvm, "Failed to remove MAC context: %d\n", ret);
1214 return ret;
1215 }
1216
1217 mvmvif->uploaded = false;
fb8b8ee1
JB
1218
1219 if (vif->type == NL80211_IFTYPE_MONITOR)
1220 mvm->hw->flags &= ~IEEE80211_HW_RX_INCLUDES_FCS;
1221
8ca151b5
JB
1222 return 0;
1223}
571765c8 1224
7f0a7c67
AO
1225static void iwl_mvm_csa_count_down(struct iwl_mvm *mvm,
1226 struct ieee80211_vif *csa_vif, u32 gp2)
1227{
1228 struct iwl_mvm_vif *mvmvif =
1229 iwl_mvm_vif_from_mac80211(csa_vif);
1230
1231 if (!ieee80211_csa_is_complete(csa_vif)) {
1232 int c = ieee80211_csa_update_counter(csa_vif);
1233
1234 iwl_mvm_mac_ctxt_beacon_changed(mvm, csa_vif);
1235 if (csa_vif->p2p &&
1236 !iwl_mvm_te_scheduled(&mvmvif->time_event_data) && gp2) {
1237 u32 rel_time = (c + 1) *
1238 csa_vif->bss_conf.beacon_int -
1239 IWL_MVM_CHANNEL_SWITCH_TIME;
1240 u32 apply_time = gp2 + rel_time * 1024;
1241
1242 iwl_mvm_schedule_csa_noa(mvm, csa_vif,
1243 IWL_MVM_CHANNEL_SWITCH_TIME -
1244 IWL_MVM_CHANNEL_SWITCH_MARGIN,
1245 apply_time);
1246 }
1247 } else if (!iwl_mvm_te_scheduled(&mvmvif->time_event_data)) {
1248 /* we don't have CSA NoA scheduled yet, switch now */
1249 ieee80211_csa_finish(csa_vif);
1250 RCU_INIT_POINTER(mvm->csa_vif, NULL);
1251 }
1252}
1253
571765c8
IP
1254int iwl_mvm_rx_beacon_notif(struct iwl_mvm *mvm,
1255 struct iwl_rx_cmd_buffer *rxb,
1256 struct iwl_device_cmd *cmd)
1257{
1258 struct iwl_rx_packet *pkt = rxb_addr(rxb);
1c87bbad 1259 struct iwl_mvm_tx_resp *beacon_notify_hdr;
664322fa 1260 struct ieee80211_vif *csa_vif;
003e5236 1261 struct ieee80211_vif *tx_blocked_vif;
1c87bbad 1262 u64 tsf;
571765c8 1263
bd3398e2
AO
1264 lockdep_assert_held(&mvm->mutex);
1265
1c87bbad
DS
1266 if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_CAPA_EXTENDED_BEACON) {
1267 struct iwl_extended_beacon_notif *beacon = (void *)pkt->data;
1268
1269 beacon_notify_hdr = &beacon->beacon_notify_hdr;
1270 tsf = le64_to_cpu(beacon->tsf);
1271 mvm->ap_last_beacon_gp2 = le32_to_cpu(beacon->gp2);
1272 } else {
1273 struct iwl_beacon_notif *beacon = (void *)pkt->data;
1274
1275 beacon_notify_hdr = &beacon->beacon_notify_hdr;
1276 tsf = le64_to_cpu(beacon->tsf);
1277 }
1278
1279 IWL_DEBUG_RX(mvm,
1280 "beacon status %#x retries:%d tsf:0x%16llX gp2:0x%X rate:%d\n",
1281 le16_to_cpu(beacon_notify_hdr->status.status) &
1282 TX_STATUS_MSK,
1283 beacon_notify_hdr->failure_frame, tsf,
1284 mvm->ap_last_beacon_gp2,
1285 le32_to_cpu(beacon_notify_hdr->initial_rate));
bd3398e2 1286
664322fa
AO
1287 csa_vif = rcu_dereference_protected(mvm->csa_vif,
1288 lockdep_is_held(&mvm->mutex));
7f0a7c67
AO
1289 if (unlikely(csa_vif && csa_vif->csa_active))
1290 iwl_mvm_csa_count_down(mvm, csa_vif, mvm->ap_last_beacon_gp2);
bd3398e2 1291
003e5236
AO
1292 tx_blocked_vif = rcu_dereference_protected(mvm->csa_tx_blocked_vif,
1293 lockdep_is_held(&mvm->mutex));
1294 if (unlikely(tx_blocked_vif)) {
1295 struct iwl_mvm_vif *mvmvif =
1296 iwl_mvm_vif_from_mac80211(tx_blocked_vif);
1297
1298 /*
1299 * The channel switch is started and we have blocked the
1300 * stations. If this is the first beacon (the timeout wasn't
1301 * set), set the unblock timeout, otherwise countdown
1302 */
1303 if (!mvm->csa_tx_block_bcn_timeout)
1304 mvm->csa_tx_block_bcn_timeout =
1305 IWL_MVM_CS_UNBLOCK_TX_TIMEOUT;
1306 else
1307 mvm->csa_tx_block_bcn_timeout--;
1308
1309 /* Check if the timeout is expired, and unblock tx */
1310 if (mvm->csa_tx_block_bcn_timeout == 0) {
1311 iwl_mvm_modify_all_sta_disable_tx(mvm, mvmvif, false);
1312 RCU_INIT_POINTER(mvm->csa_tx_blocked_vif, NULL);
1313 }
1314 }
1315
571765c8
IP
1316 return 0;
1317}
d64048ed
HG
1318
1319static void iwl_mvm_beacon_loss_iterator(void *_data, u8 *mac,
1320 struct ieee80211_vif *vif)
1321{
12d423e8 1322 struct iwl_missed_beacons_notif *missed_beacons = _data;
d64048ed
HG
1323 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1324
12d423e8
IP
1325 if (mvmvif->id != (u16)le32_to_cpu(missed_beacons->mac_id))
1326 return;
1327
1328 /*
1329 * TODO: the threshold should be adjusted based on latency conditions,
1330 * and/or in case of a CS flow on one of the other AP vifs.
1331 */
1332 if (le32_to_cpu(missed_beacons->consec_missed_beacons_since_last_rx) >
1333 IWL_MVM_MISSED_BEACONS_THRESHOLD)
d64048ed
HG
1334 ieee80211_beacon_loss(vif);
1335}
1336
1337int iwl_mvm_rx_missed_beacons_notif(struct iwl_mvm *mvm,
1338 struct iwl_rx_cmd_buffer *rxb,
1339 struct iwl_device_cmd *cmd)
1340{
1341 struct iwl_rx_packet *pkt = rxb_addr(rxb);
12d423e8
IP
1342 struct iwl_missed_beacons_notif *mb = (void *)pkt->data;
1343
1344 IWL_DEBUG_INFO(mvm,
1345 "missed bcn mac_id=%u, consecutive=%u (%u, %u, %u)\n",
1346 le32_to_cpu(mb->mac_id),
1347 le32_to_cpu(mb->consec_missed_beacons),
1348 le32_to_cpu(mb->consec_missed_beacons_since_last_rx),
1349 le32_to_cpu(mb->num_recvd_beacons),
1350 le32_to_cpu(mb->num_expected_beacons));
d64048ed
HG
1351
1352 ieee80211_iterate_active_interfaces_atomic(mvm->hw,
1353 IEEE80211_IFACE_ITER_NORMAL,
1354 iwl_mvm_beacon_loss_iterator,
12d423e8 1355 mb);
d64048ed
HG
1356 return 0;
1357}