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