iwlwifi: mvm: set the TX disable bit when doing a chanctx switch
[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,
593 u32 action)
594{
595 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
596 struct ieee80211_chanctx_conf *chanctx;
8a5e3660
AA
597 bool ht_enabled = !!(vif->bss_conf.ht_operation_mode &
598 IEEE80211_HT_OP_MODE_PROTECTION);
8ca151b5
JB
599 u8 cck_ack_rates, ofdm_ack_rates;
600 int i;
601
602 cmd->id_and_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
603 mvmvif->color));
604 cmd->action = cpu_to_le32(action);
605
606 switch (vif->type) {
607 case NL80211_IFTYPE_STATION:
608 if (vif->p2p)
609 cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_P2P_STA);
610 else
611 cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_BSS_STA);
612 break;
613 case NL80211_IFTYPE_AP:
614 cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_GO);
615 break;
616 case NL80211_IFTYPE_MONITOR:
617 cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_LISTENER);
618 break;
619 case NL80211_IFTYPE_P2P_DEVICE:
620 cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_P2P_DEVICE);
621 break;
622 case NL80211_IFTYPE_ADHOC:
623 cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_IBSS);
624 break;
625 default:
626 WARN_ON_ONCE(1);
627 }
628
629 cmd->tsf_id = cpu_to_le32(mvmvif->tsf_id);
630
631 memcpy(cmd->node_addr, vif->addr, ETH_ALEN);
632 if (vif->bss_conf.bssid)
633 memcpy(cmd->bssid_addr, vif->bss_conf.bssid, ETH_ALEN);
634 else
635 eth_broadcast_addr(cmd->bssid_addr);
636
637 rcu_read_lock();
638 chanctx = rcu_dereference(vif->chanctx_conf);
639 iwl_mvm_ack_rates(mvm, vif, chanctx ? chanctx->def.chan->band
640 : IEEE80211_BAND_2GHZ,
641 &cck_ack_rates, &ofdm_ack_rates);
642 rcu_read_unlock();
643
644 cmd->cck_rates = cpu_to_le32((u32)cck_ack_rates);
645 cmd->ofdm_rates = cpu_to_le32((u32)ofdm_ack_rates);
646
647 cmd->cck_short_preamble =
648 cpu_to_le32(vif->bss_conf.use_short_preamble ?
649 MAC_FLG_SHORT_PREAMBLE : 0);
650 cmd->short_slot =
651 cpu_to_le32(vif->bss_conf.use_short_slot ?
652 MAC_FLG_SHORT_SLOT : 0);
653
3dd94794
EG
654 for (i = 0; i < IEEE80211_NUM_ACS; i++) {
655 u8 txf = iwl_mvm_ac_to_tx_fifo[i];
656
657 cmd->ac[txf].cw_min =
658 cpu_to_le16(mvmvif->queue_params[i].cw_min);
659 cmd->ac[txf].cw_max =
660 cpu_to_le16(mvmvif->queue_params[i].cw_max);
661 cmd->ac[txf].edca_txop =
8ca151b5 662 cpu_to_le16(mvmvif->queue_params[i].txop * 32);
3dd94794
EG
663 cmd->ac[txf].aifsn = mvmvif->queue_params[i].aifs;
664 cmd->ac[txf].fifos_mask = BIT(txf);
8ca151b5
JB
665 }
666
86a91ec7
EG
667 /* in AP mode, the MCAST FIFO takes the EDCA params from VO */
668 if (vif->type == NL80211_IFTYPE_AP)
3dd94794
EG
669 cmd->ac[IWL_MVM_TX_FIFO_VO].fifos_mask |=
670 BIT(IWL_MVM_TX_FIFO_MCAST);
86a91ec7 671
8ca151b5
JB
672 if (vif->bss_conf.qos)
673 cmd->qos_flags |= cpu_to_le32(MAC_QOS_FLG_UPDATE_EDCA);
674
dc271ee0 675 if (vif->bss_conf.use_cts_prot)
cc7ee2ba 676 cmd->protection_flags |= cpu_to_le32(MAC_PROT_FLG_TGG_PROTECT);
dc271ee0 677
8a5e3660
AA
678 IWL_DEBUG_RATE(mvm, "use_cts_prot %d, ht_operation_mode %d\n",
679 vif->bss_conf.use_cts_prot,
680 vif->bss_conf.ht_operation_mode);
681 if (vif->bss_conf.chandef.width != NL80211_CHAN_WIDTH_20_NOHT)
8ca151b5 682 cmd->qos_flags |= cpu_to_le32(MAC_QOS_FLG_TGN);
8a5e3660
AA
683 if (ht_enabled)
684 iwl_mvm_mac_ctxt_set_ht_flags(mvm, vif, cmd);
8ca151b5
JB
685
686 cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP);
687}
688
689static int iwl_mvm_mac_ctxt_send_cmd(struct iwl_mvm *mvm,
690 struct iwl_mac_ctx_cmd *cmd)
691{
a1022927 692 int ret = iwl_mvm_send_cmd_pdu(mvm, MAC_CONTEXT_CMD, 0,
8ca151b5
JB
693 sizeof(*cmd), cmd);
694 if (ret)
695 IWL_ERR(mvm, "Failed to send MAC context (action:%d): %d\n",
696 le32_to_cpu(cmd->action), ret);
697 return ret;
698}
699
cf52023c
LC
700static int iwl_mvm_mac_ctxt_cmd_sta(struct iwl_mvm *mvm,
701 struct ieee80211_vif *vif,
702 u32 action, bool force_assoc_off)
8ca151b5 703{
cf52023c
LC
704 struct iwl_mac_ctx_cmd cmd = {};
705 struct iwl_mac_data_sta *ctxt_sta;
706
707 WARN_ON(vif->type != NL80211_IFTYPE_STATION);
708
709 /* Fill the common data for all mac context types */
710 iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, action);
711
712 if (vif->p2p) {
713 struct ieee80211_p2p_noa_attr *noa =
714 &vif->bss_conf.p2p_noa_attr;
715
716 cmd.p2p_sta.ctwin = cpu_to_le32(noa->oppps_ctwindow &
717 IEEE80211_P2P_OPPPS_CTWINDOW_MASK);
718 ctxt_sta = &cmd.p2p_sta.sta;
719 } else {
cf52023c
LC
720 ctxt_sta = &cmd.sta;
721 }
722
210a544e 723 /* We need the dtim_period to set the MAC as associated */
ba283927
AB
724 if (vif->bss_conf.assoc && vif->bss_conf.dtim_period &&
725 !force_assoc_off) {
d2931bbd
JB
726 u32 dtim_offs;
727
cad3f08c
LC
728 /* Allow beacons to pass through as long as we are not
729 * associated, or we do not have dtim period information.
730 */
53446699 731 cmd.filter_flags |= cpu_to_le32(MAC_FILTER_IN_BEACON);
cad3f08c 732
d2931bbd
JB
733 /*
734 * The DTIM count counts down, so when it is N that means N
735 * more beacon intervals happen until the DTIM TBTT. Therefore
736 * add this to the current time. If that ends up being in the
737 * future, the firmware will handle it.
738 *
739 * Also note that the system_timestamp (which we get here as
740 * "sync_device_ts") and TSF timestamp aren't at exactly the
741 * same offset in the frame -- the TSF is at the first symbol
742 * of the TSF, the system timestamp is at signal acquisition
743 * time. This means there's an offset between them of at most
744 * a few hundred microseconds (24 * 8 bits + PLCP time gives
745 * 384us in the longest case), this is currently not relevant
746 * as the firmware wakes up around 2ms before the TBTT.
747 */
748 dtim_offs = vif->bss_conf.sync_dtim_count *
749 vif->bss_conf.beacon_int;
750 /* convert TU to usecs */
751 dtim_offs *= 1024;
752
753 ctxt_sta->dtim_tsf =
754 cpu_to_le64(vif->bss_conf.sync_tsf + dtim_offs);
755 ctxt_sta->dtim_time =
756 cpu_to_le32(vif->bss_conf.sync_device_ts + dtim_offs);
757
758 IWL_DEBUG_INFO(mvm, "DTIM TBTT is 0x%llx/0x%x, offset %d\n",
759 le64_to_cpu(ctxt_sta->dtim_tsf),
760 le32_to_cpu(ctxt_sta->dtim_time),
761 dtim_offs);
762
210a544e 763 ctxt_sta->is_assoc = cpu_to_le32(1);
d2931bbd 764 } else {
210a544e 765 ctxt_sta->is_assoc = cpu_to_le32(0);
d2931bbd 766 }
8ca151b5
JB
767
768 ctxt_sta->bi = cpu_to_le32(vif->bss_conf.beacon_int);
769 ctxt_sta->bi_reciprocal =
770 cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int));
771 ctxt_sta->dtim_interval = cpu_to_le32(vif->bss_conf.beacon_int *
772 vif->bss_conf.dtim_period);
773 ctxt_sta->dtim_reciprocal =
774 cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int *
775 vif->bss_conf.dtim_period));
776
777 ctxt_sta->listen_interval = cpu_to_le32(mvm->hw->conf.listen_interval);
778 ctxt_sta->assoc_id = cpu_to_le32(vif->bss_conf.aid);
8ca151b5
JB
779
780 return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
781}
782
783static int iwl_mvm_mac_ctxt_cmd_listener(struct iwl_mvm *mvm,
784 struct ieee80211_vif *vif,
785 u32 action)
786{
787 struct iwl_mac_ctx_cmd cmd = {};
788
789 WARN_ON(vif->type != NL80211_IFTYPE_MONITOR);
790
791 iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, action);
53585495
JB
792
793 cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_PROMISC |
794 MAC_FILTER_IN_CONTROL_AND_MGMT |
795 MAC_FILTER_IN_BEACON |
fb8b8ee1
JB
796 MAC_FILTER_IN_PROBE_REQUEST |
797 MAC_FILTER_IN_CRC32);
798 mvm->hw->flags |= IEEE80211_HW_RX_INCLUDES_FCS;
53585495 799
8ca151b5
JB
800 return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
801}
802
5023d966
JB
803static int iwl_mvm_mac_ctxt_cmd_ibss(struct iwl_mvm *mvm,
804 struct ieee80211_vif *vif,
805 u32 action)
806{
807 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
808 struct iwl_mac_ctx_cmd cmd = {};
809
810 WARN_ON(vif->type != NL80211_IFTYPE_ADHOC);
811
812 iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, action);
813
814 cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_BEACON |
815 MAC_FILTER_IN_PROBE_REQUEST);
816
817 /* cmd.ibss.beacon_time/cmd.ibss.beacon_tsf are curently ignored */
818 cmd.ibss.bi = cpu_to_le32(vif->bss_conf.beacon_int);
819 cmd.ibss.bi_reciprocal =
820 cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int));
821
822 /* TODO: Assumes that the beacon id == mac context id */
823 cmd.ibss.beacon_template = cpu_to_le32(mvmvif->id);
824
825 return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
826}
827
8ca151b5
JB
828struct iwl_mvm_go_iterator_data {
829 bool go_active;
830};
831
832static void iwl_mvm_go_iterator(void *_data, u8 *mac, struct ieee80211_vif *vif)
833{
834 struct iwl_mvm_go_iterator_data *data = _data;
835 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
836
5023d966
JB
837 if (vif->type == NL80211_IFTYPE_AP && vif->p2p &&
838 mvmvif->ap_ibss_active)
8ca151b5
JB
839 data->go_active = true;
840}
841
842static int iwl_mvm_mac_ctxt_cmd_p2p_device(struct iwl_mvm *mvm,
843 struct ieee80211_vif *vif,
844 u32 action)
845{
846 struct iwl_mac_ctx_cmd cmd = {};
847 struct iwl_mvm_go_iterator_data data = {};
848
849 WARN_ON(vif->type != NL80211_IFTYPE_P2P_DEVICE);
850
851 iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, action);
852
853 cmd.protection_flags |= cpu_to_le32(MAC_PROT_FLG_TGG_PROTECT);
1dcd15ee
IP
854
855 /* Override the filter flags to accept only probe requests */
856 cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_PROBE_REQUEST);
8ca151b5
JB
857
858 /*
859 * This flag should be set to true when the P2P Device is
860 * discoverable and there is at least another active P2P GO. Settings
861 * this flag will allow the P2P Device to be discoverable on other
862 * channels in addition to its listen channel.
863 * Note that this flag should not be set in other cases as it opens the
864 * Rx filters on all MAC and increases the number of interrupts.
865 */
866 ieee80211_iterate_active_interfaces_atomic(
867 mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
868 iwl_mvm_go_iterator, &data);
869
870 cmd.p2p_dev.is_disc_extended = cpu_to_le32(data.go_active ? 1 : 0);
871 return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
872}
873
874static void iwl_mvm_mac_ctxt_set_tim(struct iwl_mvm *mvm,
875 struct iwl_mac_beacon_cmd *beacon_cmd,
876 u8 *beacon, u32 frame_size)
877{
878 u32 tim_idx;
879 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)beacon;
880
881 /* The index is relative to frame start but we start looking at the
882 * variable-length part of the beacon. */
883 tim_idx = mgmt->u.beacon.variable - beacon;
884
885 /* Parse variable-length elements of beacon to find WLAN_EID_TIM */
886 while ((tim_idx < (frame_size - 2)) &&
887 (beacon[tim_idx] != WLAN_EID_TIM))
888 tim_idx += beacon[tim_idx+1] + 2;
889
890 /* If TIM field was found, set variables */
891 if ((tim_idx < (frame_size - 1)) && (beacon[tim_idx] == WLAN_EID_TIM)) {
892 beacon_cmd->tim_idx = cpu_to_le32(tim_idx);
893 beacon_cmd->tim_size = cpu_to_le32((u32)beacon[tim_idx+1]);
894 } else {
895 IWL_WARN(mvm, "Unable to find TIM Element in beacon\n");
896 }
897}
898
899static int iwl_mvm_mac_ctxt_send_beacon(struct iwl_mvm *mvm,
900 struct ieee80211_vif *vif,
901 struct sk_buff *beacon)
902{
903 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
904 struct iwl_host_cmd cmd = {
905 .id = BEACON_TEMPLATE_CMD,
906 .flags = CMD_ASYNC,
907 };
908 struct iwl_mac_beacon_cmd beacon_cmd = {};
909 struct ieee80211_tx_info *info;
910 u32 beacon_skb_len;
75f6b9b6 911 u32 rate, tx_flags;
8ca151b5
JB
912
913 if (WARN_ON(!beacon))
914 return -EINVAL;
915
916 beacon_skb_len = beacon->len;
917
918 /* TODO: for now the beacon template id is set to be the mac context id.
919 * Might be better to handle it as another resource ... */
920 beacon_cmd.template_id = cpu_to_le32((u32)mvmvif->id);
75f6b9b6 921 info = IEEE80211_SKB_CB(beacon);
8ca151b5
JB
922
923 /* Set up TX command fields */
924 beacon_cmd.tx.len = cpu_to_le16((u16)beacon_skb_len);
925 beacon_cmd.tx.sta_id = mvmvif->bcast_sta.sta_id;
926 beacon_cmd.tx.life_time = cpu_to_le32(TX_CMD_LIFE_TIME_INFINITE);
75f6b9b6
EG
927 tx_flags = TX_CMD_FLG_SEQ_CTL | TX_CMD_FLG_TSF;
928 tx_flags |=
929 iwl_mvm_bt_coex_tx_prio(mvm, (void *)beacon->data, info, 0) <<
930 TX_CMD_FLG_BT_PRIO_POS;
931 beacon_cmd.tx.tx_flags = cpu_to_le32(tx_flags);
8ca151b5
JB
932
933 mvm->mgmt_last_antenna_idx =
4ed735e7 934 iwl_mvm_next_antenna(mvm, mvm->fw->valid_tx_ant,
8ca151b5
JB
935 mvm->mgmt_last_antenna_idx);
936
937 beacon_cmd.tx.rate_n_flags =
938 cpu_to_le32(BIT(mvm->mgmt_last_antenna_idx) <<
939 RATE_MCS_ANT_POS);
940
8ca151b5
JB
941 if (info->band == IEEE80211_BAND_5GHZ || vif->p2p) {
942 rate = IWL_FIRST_OFDM_RATE;
943 } else {
944 rate = IWL_FIRST_CCK_RATE;
945 beacon_cmd.tx.rate_n_flags |= cpu_to_le32(RATE_MCS_CCK_MSK);
946 }
947 beacon_cmd.tx.rate_n_flags |=
948 cpu_to_le32(iwl_mvm_mac80211_idx_to_hwrate(rate));
949
950 /* Set up TX beacon command fields */
5023d966
JB
951 if (vif->type == NL80211_IFTYPE_AP)
952 iwl_mvm_mac_ctxt_set_tim(mvm, &beacon_cmd,
953 beacon->data,
954 beacon_skb_len);
8ca151b5
JB
955
956 /* Submit command */
957 cmd.len[0] = sizeof(beacon_cmd);
958 cmd.data[0] = &beacon_cmd;
959 cmd.dataflags[0] = 0;
960 cmd.len[1] = beacon_skb_len;
961 cmd.data[1] = beacon->data;
962 cmd.dataflags[1] = IWL_HCMD_DFL_DUP;
963
964 return iwl_mvm_send_cmd(mvm, &cmd);
965}
966
5023d966 967/* The beacon template for the AP/GO/IBSS has changed and needs update */
8ca151b5
JB
968int iwl_mvm_mac_ctxt_beacon_changed(struct iwl_mvm *mvm,
969 struct ieee80211_vif *vif)
970{
971 struct sk_buff *beacon;
972 int ret;
973
5023d966
JB
974 WARN_ON(vif->type != NL80211_IFTYPE_AP &&
975 vif->type != NL80211_IFTYPE_ADHOC);
8ca151b5 976
fe887665 977 beacon = ieee80211_beacon_get_template(mvm->hw, vif, NULL);
8ca151b5
JB
978 if (!beacon)
979 return -ENOMEM;
980
981 ret = iwl_mvm_mac_ctxt_send_beacon(mvm, vif, beacon);
982 dev_kfree_skb(beacon);
983 return ret;
984}
985
3a3cb92e
IP
986struct iwl_mvm_mac_ap_iterator_data {
987 struct iwl_mvm *mvm;
988 struct ieee80211_vif *vif;
989 u32 beacon_device_ts;
990 u16 beacon_int;
991};
992
993/* Find the beacon_device_ts and beacon_int for a managed interface */
994static void iwl_mvm_mac_ap_iterator(void *_data, u8 *mac,
995 struct ieee80211_vif *vif)
996{
997 struct iwl_mvm_mac_ap_iterator_data *data = _data;
998
999 if (vif->type != NL80211_IFTYPE_STATION || !vif->bss_conf.assoc)
1000 return;
1001
1002 /* Station client has higher priority over P2P client*/
1003 if (vif->p2p && data->beacon_device_ts)
1004 return;
1005
1006 data->beacon_device_ts = vif->bss_conf.sync_device_ts;
1007 data->beacon_int = vif->bss_conf.beacon_int;
1008}
1009
8ca151b5
JB
1010/*
1011 * Fill the specific data for mac context of type AP of P2P GO
1012 */
1013static void iwl_mvm_mac_ctxt_cmd_fill_ap(struct iwl_mvm *mvm,
1014 struct ieee80211_vif *vif,
506a81e6
JB
1015 struct iwl_mac_data_ap *ctxt_ap,
1016 bool add)
8ca151b5
JB
1017{
1018 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3a3cb92e
IP
1019 struct iwl_mvm_mac_ap_iterator_data data = {
1020 .mvm = mvm,
1021 .vif = vif,
1022 .beacon_device_ts = 0
1023 };
8ca151b5
JB
1024
1025 ctxt_ap->bi = cpu_to_le32(vif->bss_conf.beacon_int);
1026 ctxt_ap->bi_reciprocal =
1027 cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int));
1028 ctxt_ap->dtim_interval = cpu_to_le32(vif->bss_conf.beacon_int *
1029 vif->bss_conf.dtim_period);
1030 ctxt_ap->dtim_reciprocal =
1031 cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int *
1032 vif->bss_conf.dtim_period));
1033
1034 ctxt_ap->mcast_qid = cpu_to_le32(vif->cab_queue);
8ca151b5 1035
506a81e6 1036 /*
3a3cb92e 1037 * Only set the beacon time when the MAC is being added, when we
506a81e6
JB
1038 * just modify the MAC then we should keep the time -- the firmware
1039 * can otherwise have a "jumping" TBTT.
1040 */
3a3cb92e
IP
1041 if (add) {
1042 /*
1043 * If there is a station/P2P client interface which is
1044 * associated, set the AP's TBTT far enough from the station's
1045 * TBTT. Otherwise, set it to the current system time
1046 */
1047 ieee80211_iterate_active_interfaces_atomic(
1048 mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
1049 iwl_mvm_mac_ap_iterator, &data);
1050
1051 if (data.beacon_device_ts) {
9f8f8ca5 1052 u32 rand = (prandom_u32() % (64 - 36)) + 36;
3a3cb92e
IP
1053 mvmvif->ap_beacon_time = data.beacon_device_ts +
1054 ieee80211_tu_to_usec(data.beacon_int * rand /
1055 100);
1056 } else {
1057 mvmvif->ap_beacon_time =
1058 iwl_read_prph(mvm->trans,
1059 DEVICE_SYSTEM_TIME_REG);
1060 }
1061 }
506a81e6
JB
1062
1063 ctxt_ap->beacon_time = cpu_to_le32(mvmvif->ap_beacon_time);
506a81e6 1064 ctxt_ap->beacon_tsf = 0; /* unused */
8ca151b5
JB
1065
1066 /* TODO: Assume that the beacon id == mac context id */
1067 ctxt_ap->beacon_template = cpu_to_le32(mvmvif->id);
1068}
1069
1070static int iwl_mvm_mac_ctxt_cmd_ap(struct iwl_mvm *mvm,
1071 struct ieee80211_vif *vif,
1072 u32 action)
1073{
1074 struct iwl_mac_ctx_cmd cmd = {};
1075
1076 WARN_ON(vif->type != NL80211_IFTYPE_AP || vif->p2p);
1077
1078 /* Fill the common data for all mac context types */
1079 iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, action);
1080
46018794
EP
1081 /*
1082 * pass probe requests and beacons from other APs (needed
1083 * for ht protection)
1084 */
1085 cmd.filter_flags |= cpu_to_le32(MAC_FILTER_IN_PROBE_REQUEST |
1086 MAC_FILTER_IN_BEACON);
1dcd15ee 1087
8ca151b5 1088 /* Fill the data specific for ap mode */
506a81e6
JB
1089 iwl_mvm_mac_ctxt_cmd_fill_ap(mvm, vif, &cmd.ap,
1090 action == FW_CTXT_ACTION_ADD);
8ca151b5
JB
1091
1092 return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
1093}
1094
1095static int iwl_mvm_mac_ctxt_cmd_go(struct iwl_mvm *mvm,
1096 struct ieee80211_vif *vif,
1097 u32 action)
1098{
1099 struct iwl_mac_ctx_cmd cmd = {};
67baf663 1100 struct ieee80211_p2p_noa_attr *noa = &vif->bss_conf.p2p_noa_attr;
8ca151b5
JB
1101
1102 WARN_ON(vif->type != NL80211_IFTYPE_AP || !vif->p2p);
1103
1104 /* Fill the common data for all mac context types */
1105 iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, action);
1106
46018794
EP
1107 /*
1108 * pass probe requests and beacons from other APs (needed
1109 * for ht protection)
1110 */
1111 cmd.filter_flags |= cpu_to_le32(MAC_FILTER_IN_PROBE_REQUEST |
1112 MAC_FILTER_IN_BEACON);
1113
8ca151b5 1114 /* Fill the data specific for GO mode */
506a81e6
JB
1115 iwl_mvm_mac_ctxt_cmd_fill_ap(mvm, vif, &cmd.go.ap,
1116 action == FW_CTXT_ACTION_ADD);
8ca151b5 1117
67baf663
JD
1118 cmd.go.ctwin = cpu_to_le32(noa->oppps_ctwindow &
1119 IEEE80211_P2P_OPPPS_CTWINDOW_MASK);
1120 cmd.go.opp_ps_enabled =
1121 cpu_to_le32(!!(noa->oppps_ctwindow &
1122 IEEE80211_P2P_OPPPS_ENABLE_BIT));
8ca151b5
JB
1123
1124 return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
1125}
1126
1127static int iwl_mvm_mac_ctx_send(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
bca49d9a 1128 u32 action, bool force_assoc_off)
8ca151b5
JB
1129{
1130 switch (vif->type) {
1131 case NL80211_IFTYPE_STATION:
cf52023c 1132 return iwl_mvm_mac_ctxt_cmd_sta(mvm, vif, action,
bca49d9a 1133 force_assoc_off);
8ca151b5
JB
1134 break;
1135 case NL80211_IFTYPE_AP:
1136 if (!vif->p2p)
1137 return iwl_mvm_mac_ctxt_cmd_ap(mvm, vif, action);
1138 else
1139 return iwl_mvm_mac_ctxt_cmd_go(mvm, vif, action);
1140 break;
1141 case NL80211_IFTYPE_MONITOR:
1142 return iwl_mvm_mac_ctxt_cmd_listener(mvm, vif, action);
1143 case NL80211_IFTYPE_P2P_DEVICE:
1144 return iwl_mvm_mac_ctxt_cmd_p2p_device(mvm, vif, action);
5023d966
JB
1145 case NL80211_IFTYPE_ADHOC:
1146 return iwl_mvm_mac_ctxt_cmd_ibss(mvm, vif, action);
8ca151b5
JB
1147 default:
1148 break;
1149 }
1150
1151 return -EOPNOTSUPP;
1152}
1153
1154int iwl_mvm_mac_ctxt_add(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1155{
1156 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1157 int ret;
1158
1159 if (WARN_ONCE(mvmvif->uploaded, "Adding active MAC %pM/%d\n",
1160 vif->addr, ieee80211_vif_type_p2p(vif)))
1161 return -EIO;
1162
bca49d9a
LC
1163 ret = iwl_mvm_mac_ctx_send(mvm, vif, FW_CTXT_ACTION_ADD,
1164 true);
8ca151b5
JB
1165 if (ret)
1166 return ret;
1167
6d9d32b8
JB
1168 /* will only do anything at resume from D3 time */
1169 iwl_mvm_set_last_nonqos_seq(mvm, vif);
1170
8ca151b5
JB
1171 mvmvif->uploaded = true;
1172 return 0;
1173}
1174
bca49d9a
LC
1175int iwl_mvm_mac_ctxt_changed(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1176 bool force_assoc_off)
8ca151b5
JB
1177{
1178 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1179
1180 if (WARN_ONCE(!mvmvif->uploaded, "Changing inactive MAC %pM/%d\n",
1181 vif->addr, ieee80211_vif_type_p2p(vif)))
1182 return -EIO;
1183
bca49d9a
LC
1184 return iwl_mvm_mac_ctx_send(mvm, vif, FW_CTXT_ACTION_MODIFY,
1185 force_assoc_off);
8ca151b5
JB
1186}
1187
1188int iwl_mvm_mac_ctxt_remove(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1189{
1190 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1191 struct iwl_mac_ctx_cmd cmd;
1192 int ret;
1193
1194 if (WARN_ONCE(!mvmvif->uploaded, "Removing inactive MAC %pM/%d\n",
1195 vif->addr, ieee80211_vif_type_p2p(vif)))
1196 return -EIO;
1197
1198 memset(&cmd, 0, sizeof(cmd));
1199
1200 cmd.id_and_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
1201 mvmvif->color));
1202 cmd.action = cpu_to_le32(FW_CTXT_ACTION_REMOVE);
1203
a1022927 1204 ret = iwl_mvm_send_cmd_pdu(mvm, MAC_CONTEXT_CMD, 0,
8ca151b5
JB
1205 sizeof(cmd), &cmd);
1206 if (ret) {
1207 IWL_ERR(mvm, "Failed to remove MAC context: %d\n", ret);
1208 return ret;
1209 }
1210
1211 mvmvif->uploaded = false;
fb8b8ee1
JB
1212
1213 if (vif->type == NL80211_IFTYPE_MONITOR)
1214 mvm->hw->flags &= ~IEEE80211_HW_RX_INCLUDES_FCS;
1215
8ca151b5
JB
1216 return 0;
1217}
571765c8 1218
7f0a7c67
AO
1219static void iwl_mvm_csa_count_down(struct iwl_mvm *mvm,
1220 struct ieee80211_vif *csa_vif, u32 gp2)
1221{
1222 struct iwl_mvm_vif *mvmvif =
1223 iwl_mvm_vif_from_mac80211(csa_vif);
1224
1225 if (!ieee80211_csa_is_complete(csa_vif)) {
1226 int c = ieee80211_csa_update_counter(csa_vif);
1227
1228 iwl_mvm_mac_ctxt_beacon_changed(mvm, csa_vif);
1229 if (csa_vif->p2p &&
1230 !iwl_mvm_te_scheduled(&mvmvif->time_event_data) && gp2) {
1231 u32 rel_time = (c + 1) *
1232 csa_vif->bss_conf.beacon_int -
1233 IWL_MVM_CHANNEL_SWITCH_TIME;
1234 u32 apply_time = gp2 + rel_time * 1024;
1235
1236 iwl_mvm_schedule_csa_noa(mvm, csa_vif,
1237 IWL_MVM_CHANNEL_SWITCH_TIME -
1238 IWL_MVM_CHANNEL_SWITCH_MARGIN,
1239 apply_time);
1240 }
1241 } else if (!iwl_mvm_te_scheduled(&mvmvif->time_event_data)) {
1242 /* we don't have CSA NoA scheduled yet, switch now */
1243 ieee80211_csa_finish(csa_vif);
1244 RCU_INIT_POINTER(mvm->csa_vif, NULL);
1245 }
1246}
1247
571765c8
IP
1248int iwl_mvm_rx_beacon_notif(struct iwl_mvm *mvm,
1249 struct iwl_rx_cmd_buffer *rxb,
1250 struct iwl_device_cmd *cmd)
1251{
1252 struct iwl_rx_packet *pkt = rxb_addr(rxb);
1c87bbad 1253 struct iwl_mvm_tx_resp *beacon_notify_hdr;
664322fa 1254 struct ieee80211_vif *csa_vif;
003e5236 1255 struct ieee80211_vif *tx_blocked_vif;
1c87bbad 1256 u64 tsf;
571765c8 1257
bd3398e2
AO
1258 lockdep_assert_held(&mvm->mutex);
1259
1c87bbad
DS
1260 if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_CAPA_EXTENDED_BEACON) {
1261 struct iwl_extended_beacon_notif *beacon = (void *)pkt->data;
1262
1263 beacon_notify_hdr = &beacon->beacon_notify_hdr;
1264 tsf = le64_to_cpu(beacon->tsf);
1265 mvm->ap_last_beacon_gp2 = le32_to_cpu(beacon->gp2);
1266 } else {
1267 struct iwl_beacon_notif *beacon = (void *)pkt->data;
1268
1269 beacon_notify_hdr = &beacon->beacon_notify_hdr;
1270 tsf = le64_to_cpu(beacon->tsf);
1271 }
1272
1273 IWL_DEBUG_RX(mvm,
1274 "beacon status %#x retries:%d tsf:0x%16llX gp2:0x%X rate:%d\n",
1275 le16_to_cpu(beacon_notify_hdr->status.status) &
1276 TX_STATUS_MSK,
1277 beacon_notify_hdr->failure_frame, tsf,
1278 mvm->ap_last_beacon_gp2,
1279 le32_to_cpu(beacon_notify_hdr->initial_rate));
bd3398e2 1280
664322fa
AO
1281 csa_vif = rcu_dereference_protected(mvm->csa_vif,
1282 lockdep_is_held(&mvm->mutex));
7f0a7c67
AO
1283 if (unlikely(csa_vif && csa_vif->csa_active))
1284 iwl_mvm_csa_count_down(mvm, csa_vif, mvm->ap_last_beacon_gp2);
bd3398e2 1285
003e5236
AO
1286 tx_blocked_vif = rcu_dereference_protected(mvm->csa_tx_blocked_vif,
1287 lockdep_is_held(&mvm->mutex));
1288 if (unlikely(tx_blocked_vif)) {
1289 struct iwl_mvm_vif *mvmvif =
1290 iwl_mvm_vif_from_mac80211(tx_blocked_vif);
1291
1292 /*
1293 * The channel switch is started and we have blocked the
1294 * stations. If this is the first beacon (the timeout wasn't
1295 * set), set the unblock timeout, otherwise countdown
1296 */
1297 if (!mvm->csa_tx_block_bcn_timeout)
1298 mvm->csa_tx_block_bcn_timeout =
1299 IWL_MVM_CS_UNBLOCK_TX_TIMEOUT;
1300 else
1301 mvm->csa_tx_block_bcn_timeout--;
1302
1303 /* Check if the timeout is expired, and unblock tx */
1304 if (mvm->csa_tx_block_bcn_timeout == 0) {
1305 iwl_mvm_modify_all_sta_disable_tx(mvm, mvmvif, false);
1306 RCU_INIT_POINTER(mvm->csa_tx_blocked_vif, NULL);
1307 }
1308 }
1309
571765c8
IP
1310 return 0;
1311}
d64048ed
HG
1312
1313static void iwl_mvm_beacon_loss_iterator(void *_data, u8 *mac,
1314 struct ieee80211_vif *vif)
1315{
12d423e8 1316 struct iwl_missed_beacons_notif *missed_beacons = _data;
d64048ed
HG
1317 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1318
12d423e8
IP
1319 if (mvmvif->id != (u16)le32_to_cpu(missed_beacons->mac_id))
1320 return;
1321
1322 /*
1323 * TODO: the threshold should be adjusted based on latency conditions,
1324 * and/or in case of a CS flow on one of the other AP vifs.
1325 */
1326 if (le32_to_cpu(missed_beacons->consec_missed_beacons_since_last_rx) >
1327 IWL_MVM_MISSED_BEACONS_THRESHOLD)
d64048ed
HG
1328 ieee80211_beacon_loss(vif);
1329}
1330
1331int iwl_mvm_rx_missed_beacons_notif(struct iwl_mvm *mvm,
1332 struct iwl_rx_cmd_buffer *rxb,
1333 struct iwl_device_cmd *cmd)
1334{
1335 struct iwl_rx_packet *pkt = rxb_addr(rxb);
12d423e8
IP
1336 struct iwl_missed_beacons_notif *mb = (void *)pkt->data;
1337
1338 IWL_DEBUG_INFO(mvm,
1339 "missed bcn mac_id=%u, consecutive=%u (%u, %u, %u)\n",
1340 le32_to_cpu(mb->mac_id),
1341 le32_to_cpu(mb->consec_missed_beacons),
1342 le32_to_cpu(mb->consec_missed_beacons_since_last_rx),
1343 le32_to_cpu(mb->num_recvd_beacons),
1344 le32_to_cpu(mb->num_expected_beacons));
d64048ed
HG
1345
1346 ieee80211_iterate_active_interfaces_atomic(mvm->hw,
1347 IEEE80211_IFACE_ITER_NORMAL,
1348 iwl_mvm_beacon_loss_iterator,
12d423e8 1349 mb);
d64048ed
HG
1350 return 0;
1351}