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