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