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