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