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