]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/net/wireless/iwlwifi/mvm/mac-ctxt.c
Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-next
[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         if (vif->probe_req_reg && vif->bss_conf.assoc && vif->p2p)
847                 cmd.filter_flags |= cpu_to_le32(MAC_FILTER_IN_PROBE_REQUEST);
848
849         return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
850 }
851
852 static int iwl_mvm_mac_ctxt_cmd_listener(struct iwl_mvm *mvm,
853                                          struct ieee80211_vif *vif,
854                                          u32 action)
855 {
856         struct iwl_mac_ctx_cmd cmd = {};
857
858         WARN_ON(vif->type != NL80211_IFTYPE_MONITOR);
859
860         iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
861
862         cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_PROMISC |
863                                        MAC_FILTER_IN_CONTROL_AND_MGMT |
864                                        MAC_FILTER_IN_BEACON |
865                                        MAC_FILTER_IN_PROBE_REQUEST |
866                                        MAC_FILTER_IN_CRC32);
867         ieee80211_hw_set(mvm->hw, RX_INCLUDES_FCS);
868
869         return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
870 }
871
872 static int iwl_mvm_mac_ctxt_cmd_ibss(struct iwl_mvm *mvm,
873                                      struct ieee80211_vif *vif,
874                                      u32 action)
875 {
876         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
877         struct iwl_mac_ctx_cmd cmd = {};
878
879         WARN_ON(vif->type != NL80211_IFTYPE_ADHOC);
880
881         iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
882
883         cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_BEACON |
884                                        MAC_FILTER_IN_PROBE_REQUEST);
885
886         /* cmd.ibss.beacon_time/cmd.ibss.beacon_tsf are curently ignored */
887         cmd.ibss.bi = cpu_to_le32(vif->bss_conf.beacon_int);
888         cmd.ibss.bi_reciprocal =
889                 cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int));
890
891         /* TODO: Assumes that the beacon id == mac context id */
892         cmd.ibss.beacon_template = cpu_to_le32(mvmvif->id);
893
894         return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
895 }
896
897 struct iwl_mvm_go_iterator_data {
898         bool go_active;
899 };
900
901 static void iwl_mvm_go_iterator(void *_data, u8 *mac, struct ieee80211_vif *vif)
902 {
903         struct iwl_mvm_go_iterator_data *data = _data;
904         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
905
906         if (vif->type == NL80211_IFTYPE_AP && vif->p2p &&
907             mvmvif->ap_ibss_active)
908                 data->go_active = true;
909 }
910
911 static int iwl_mvm_mac_ctxt_cmd_p2p_device(struct iwl_mvm *mvm,
912                                            struct ieee80211_vif *vif,
913                                            u32 action)
914 {
915         struct iwl_mac_ctx_cmd cmd = {};
916         struct iwl_mvm_go_iterator_data data = {};
917
918         WARN_ON(vif->type != NL80211_IFTYPE_P2P_DEVICE);
919
920         iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
921
922         cmd.protection_flags |= cpu_to_le32(MAC_PROT_FLG_TGG_PROTECT);
923
924         /* Override the filter flags to accept only probe requests */
925         cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_PROBE_REQUEST);
926
927         /*
928          * This flag should be set to true when the P2P Device is
929          * discoverable and there is at least another active P2P GO. Settings
930          * this flag will allow the P2P Device to be discoverable on other
931          * channels in addition to its listen channel.
932          * Note that this flag should not be set in other cases as it opens the
933          * Rx filters on all MAC and increases the number of interrupts.
934          */
935         ieee80211_iterate_active_interfaces_atomic(
936                 mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
937                 iwl_mvm_go_iterator, &data);
938
939         cmd.p2p_dev.is_disc_extended = cpu_to_le32(data.go_active ? 1 : 0);
940         return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
941 }
942
943 static void iwl_mvm_mac_ctxt_set_tim(struct iwl_mvm *mvm,
944                                      struct iwl_mac_beacon_cmd *beacon_cmd,
945                                      u8 *beacon, u32 frame_size)
946 {
947         u32 tim_idx;
948         struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)beacon;
949
950         /* The index is relative to frame start but we start looking at the
951          * variable-length part of the beacon. */
952         tim_idx = mgmt->u.beacon.variable - beacon;
953
954         /* Parse variable-length elements of beacon to find WLAN_EID_TIM */
955         while ((tim_idx < (frame_size - 2)) &&
956                         (beacon[tim_idx] != WLAN_EID_TIM))
957                 tim_idx += beacon[tim_idx+1] + 2;
958
959         /* If TIM field was found, set variables */
960         if ((tim_idx < (frame_size - 1)) && (beacon[tim_idx] == WLAN_EID_TIM)) {
961                 beacon_cmd->tim_idx = cpu_to_le32(tim_idx);
962                 beacon_cmd->tim_size = cpu_to_le32((u32)beacon[tim_idx+1]);
963         } else {
964                 IWL_WARN(mvm, "Unable to find TIM Element in beacon\n");
965         }
966 }
967
968 static int iwl_mvm_mac_ctxt_send_beacon(struct iwl_mvm *mvm,
969                                         struct ieee80211_vif *vif,
970                                         struct sk_buff *beacon)
971 {
972         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
973         struct iwl_host_cmd cmd = {
974                 .id = BEACON_TEMPLATE_CMD,
975                 .flags = CMD_ASYNC,
976         };
977         struct iwl_mac_beacon_cmd beacon_cmd = {};
978         struct ieee80211_tx_info *info;
979         u32 beacon_skb_len;
980         u32 rate, tx_flags;
981
982         if (WARN_ON(!beacon))
983                 return -EINVAL;
984
985         beacon_skb_len = beacon->len;
986
987         /* TODO: for now the beacon template id is set to be the mac context id.
988          * Might be better to handle it as another resource ... */
989         beacon_cmd.template_id = cpu_to_le32((u32)mvmvif->id);
990         info = IEEE80211_SKB_CB(beacon);
991
992         /* Set up TX command fields */
993         beacon_cmd.tx.len = cpu_to_le16((u16)beacon_skb_len);
994         beacon_cmd.tx.sta_id = mvmvif->bcast_sta.sta_id;
995         beacon_cmd.tx.life_time = cpu_to_le32(TX_CMD_LIFE_TIME_INFINITE);
996         tx_flags = TX_CMD_FLG_SEQ_CTL | TX_CMD_FLG_TSF;
997         tx_flags |=
998                 iwl_mvm_bt_coex_tx_prio(mvm, (void *)beacon->data, info, 0) <<
999                                                 TX_CMD_FLG_BT_PRIO_POS;
1000         beacon_cmd.tx.tx_flags = cpu_to_le32(tx_flags);
1001
1002         mvm->mgmt_last_antenna_idx =
1003                 iwl_mvm_next_antenna(mvm, iwl_mvm_get_valid_tx_ant(mvm),
1004                                      mvm->mgmt_last_antenna_idx);
1005
1006         beacon_cmd.tx.rate_n_flags =
1007                 cpu_to_le32(BIT(mvm->mgmt_last_antenna_idx) <<
1008                             RATE_MCS_ANT_POS);
1009
1010         if (info->band == IEEE80211_BAND_5GHZ || vif->p2p) {
1011                 rate = IWL_FIRST_OFDM_RATE;
1012         } else {
1013                 rate = IWL_FIRST_CCK_RATE;
1014                 beacon_cmd.tx.rate_n_flags |= cpu_to_le32(RATE_MCS_CCK_MSK);
1015         }
1016         beacon_cmd.tx.rate_n_flags |=
1017                 cpu_to_le32(iwl_mvm_mac80211_idx_to_hwrate(rate));
1018
1019         /* Set up TX beacon command fields */
1020         if (vif->type == NL80211_IFTYPE_AP)
1021                 iwl_mvm_mac_ctxt_set_tim(mvm, &beacon_cmd,
1022                                          beacon->data,
1023                                          beacon_skb_len);
1024
1025         /* Submit command */
1026         cmd.len[0] = sizeof(beacon_cmd);
1027         cmd.data[0] = &beacon_cmd;
1028         cmd.dataflags[0] = 0;
1029         cmd.len[1] = beacon_skb_len;
1030         cmd.data[1] = beacon->data;
1031         cmd.dataflags[1] = IWL_HCMD_DFL_DUP;
1032
1033         return iwl_mvm_send_cmd(mvm, &cmd);
1034 }
1035
1036 /* The beacon template for the AP/GO/IBSS has changed and needs update */
1037 int iwl_mvm_mac_ctxt_beacon_changed(struct iwl_mvm *mvm,
1038                                     struct ieee80211_vif *vif)
1039 {
1040         struct sk_buff *beacon;
1041         int ret;
1042
1043         WARN_ON(vif->type != NL80211_IFTYPE_AP &&
1044                 vif->type != NL80211_IFTYPE_ADHOC);
1045
1046         beacon = ieee80211_beacon_get_template(mvm->hw, vif, NULL);
1047         if (!beacon)
1048                 return -ENOMEM;
1049
1050         ret = iwl_mvm_mac_ctxt_send_beacon(mvm, vif, beacon);
1051         dev_kfree_skb(beacon);
1052         return ret;
1053 }
1054
1055 struct iwl_mvm_mac_ap_iterator_data {
1056         struct iwl_mvm *mvm;
1057         struct ieee80211_vif *vif;
1058         u32 beacon_device_ts;
1059         u16 beacon_int;
1060 };
1061
1062 /* Find the beacon_device_ts and beacon_int for a managed interface */
1063 static void iwl_mvm_mac_ap_iterator(void *_data, u8 *mac,
1064                                     struct ieee80211_vif *vif)
1065 {
1066         struct iwl_mvm_mac_ap_iterator_data *data = _data;
1067
1068         if (vif->type != NL80211_IFTYPE_STATION || !vif->bss_conf.assoc)
1069                 return;
1070
1071         /* Station client has higher priority over P2P client*/
1072         if (vif->p2p && data->beacon_device_ts)
1073                 return;
1074
1075         data->beacon_device_ts = vif->bss_conf.sync_device_ts;
1076         data->beacon_int = vif->bss_conf.beacon_int;
1077 }
1078
1079 /*
1080  * Fill the specific data for mac context of type AP of P2P GO
1081  */
1082 static void iwl_mvm_mac_ctxt_cmd_fill_ap(struct iwl_mvm *mvm,
1083                                          struct ieee80211_vif *vif,
1084                                          struct iwl_mac_data_ap *ctxt_ap,
1085                                          bool add)
1086 {
1087         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1088         struct iwl_mvm_mac_ap_iterator_data data = {
1089                 .mvm = mvm,
1090                 .vif = vif,
1091                 .beacon_device_ts = 0
1092         };
1093
1094         ctxt_ap->bi = cpu_to_le32(vif->bss_conf.beacon_int);
1095         ctxt_ap->bi_reciprocal =
1096                 cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int));
1097         ctxt_ap->dtim_interval = cpu_to_le32(vif->bss_conf.beacon_int *
1098                                              vif->bss_conf.dtim_period);
1099         ctxt_ap->dtim_reciprocal =
1100                 cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int *
1101                                                vif->bss_conf.dtim_period));
1102
1103         ctxt_ap->mcast_qid = cpu_to_le32(vif->cab_queue);
1104
1105         /*
1106          * Only set the beacon time when the MAC is being added, when we
1107          * just modify the MAC then we should keep the time -- the firmware
1108          * can otherwise have a "jumping" TBTT.
1109          */
1110         if (add) {
1111                 /*
1112                  * If there is a station/P2P client interface which is
1113                  * associated, set the AP's TBTT far enough from the station's
1114                  * TBTT. Otherwise, set it to the current system time
1115                  */
1116                 ieee80211_iterate_active_interfaces_atomic(
1117                         mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
1118                         iwl_mvm_mac_ap_iterator, &data);
1119
1120                 if (data.beacon_device_ts) {
1121                         u32 rand = (prandom_u32() % (64 - 36)) + 36;
1122                         mvmvif->ap_beacon_time = data.beacon_device_ts +
1123                                 ieee80211_tu_to_usec(data.beacon_int * rand /
1124                                                      100);
1125                 } else {
1126                         mvmvif->ap_beacon_time =
1127                                 iwl_read_prph(mvm->trans,
1128                                               DEVICE_SYSTEM_TIME_REG);
1129                 }
1130         }
1131
1132         ctxt_ap->beacon_time = cpu_to_le32(mvmvif->ap_beacon_time);
1133         ctxt_ap->beacon_tsf = 0; /* unused */
1134
1135         /* TODO: Assume that the beacon id == mac context id */
1136         ctxt_ap->beacon_template = cpu_to_le32(mvmvif->id);
1137 }
1138
1139 static int iwl_mvm_mac_ctxt_cmd_ap(struct iwl_mvm *mvm,
1140                                    struct ieee80211_vif *vif,
1141                                    u32 action)
1142 {
1143         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1144         struct iwl_mac_ctx_cmd cmd = {};
1145
1146         WARN_ON(vif->type != NL80211_IFTYPE_AP || vif->p2p);
1147
1148         /* Fill the common data for all mac context types */
1149         iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
1150
1151         /*
1152          * pass probe requests and beacons from other APs (needed
1153          * for ht protection); when there're no any associated station
1154          * don't ask FW to pass beacons to prevent unnecessary wake-ups.
1155          */
1156         cmd.filter_flags |= cpu_to_le32(MAC_FILTER_IN_PROBE_REQUEST);
1157         if (mvmvif->ap_assoc_sta_count) {
1158                 cmd.filter_flags |= cpu_to_le32(MAC_FILTER_IN_BEACON);
1159                 IWL_DEBUG_HC(mvm, "Asking FW to pass beacons\n");
1160         } else {
1161                 IWL_DEBUG_HC(mvm, "No need to receive beacons\n");
1162         }
1163
1164         /* Fill the data specific for ap mode */
1165         iwl_mvm_mac_ctxt_cmd_fill_ap(mvm, vif, &cmd.ap,
1166                                      action == FW_CTXT_ACTION_ADD);
1167
1168         return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
1169 }
1170
1171 static int iwl_mvm_mac_ctxt_cmd_go(struct iwl_mvm *mvm,
1172                                    struct ieee80211_vif *vif,
1173                                    u32 action)
1174 {
1175         struct iwl_mac_ctx_cmd cmd = {};
1176         struct ieee80211_p2p_noa_attr *noa = &vif->bss_conf.p2p_noa_attr;
1177
1178         WARN_ON(vif->type != NL80211_IFTYPE_AP || !vif->p2p);
1179
1180         /* Fill the common data for all mac context types */
1181         iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
1182
1183         /*
1184          * pass probe requests and beacons from other APs (needed
1185          * for ht protection)
1186          */
1187         cmd.filter_flags |= cpu_to_le32(MAC_FILTER_IN_PROBE_REQUEST |
1188                                         MAC_FILTER_IN_BEACON);
1189
1190         /* Fill the data specific for GO mode */
1191         iwl_mvm_mac_ctxt_cmd_fill_ap(mvm, vif, &cmd.go.ap,
1192                                      action == FW_CTXT_ACTION_ADD);
1193
1194         cmd.go.ctwin = cpu_to_le32(noa->oppps_ctwindow &
1195                                         IEEE80211_P2P_OPPPS_CTWINDOW_MASK);
1196         cmd.go.opp_ps_enabled =
1197                         cpu_to_le32(!!(noa->oppps_ctwindow &
1198                                         IEEE80211_P2P_OPPPS_ENABLE_BIT));
1199
1200         return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
1201 }
1202
1203 static int iwl_mvm_mac_ctx_send(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1204                                 u32 action, bool force_assoc_off,
1205                                 const u8 *bssid_override)
1206 {
1207         switch (vif->type) {
1208         case NL80211_IFTYPE_STATION:
1209                 return iwl_mvm_mac_ctxt_cmd_sta(mvm, vif, action,
1210                                                 force_assoc_off,
1211                                                 bssid_override);
1212                 break;
1213         case NL80211_IFTYPE_AP:
1214                 if (!vif->p2p)
1215                         return iwl_mvm_mac_ctxt_cmd_ap(mvm, vif, action);
1216                 else
1217                         return iwl_mvm_mac_ctxt_cmd_go(mvm, vif, action);
1218                 break;
1219         case NL80211_IFTYPE_MONITOR:
1220                 return iwl_mvm_mac_ctxt_cmd_listener(mvm, vif, action);
1221         case NL80211_IFTYPE_P2P_DEVICE:
1222                 return iwl_mvm_mac_ctxt_cmd_p2p_device(mvm, vif, action);
1223         case NL80211_IFTYPE_ADHOC:
1224                 return iwl_mvm_mac_ctxt_cmd_ibss(mvm, vif, action);
1225         default:
1226                 break;
1227         }
1228
1229         return -EOPNOTSUPP;
1230 }
1231
1232 int iwl_mvm_mac_ctxt_add(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1233 {
1234         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1235         int ret;
1236
1237         if (WARN_ONCE(mvmvif->uploaded, "Adding active MAC %pM/%d\n",
1238                       vif->addr, ieee80211_vif_type_p2p(vif)))
1239                 return -EIO;
1240
1241         ret = iwl_mvm_mac_ctx_send(mvm, vif, FW_CTXT_ACTION_ADD,
1242                                    true, NULL);
1243         if (ret)
1244                 return ret;
1245
1246         /* will only do anything at resume from D3 time */
1247         iwl_mvm_set_last_nonqos_seq(mvm, vif);
1248
1249         mvmvif->uploaded = true;
1250         return 0;
1251 }
1252
1253 int iwl_mvm_mac_ctxt_changed(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1254                              bool force_assoc_off, const u8 *bssid_override)
1255 {
1256         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1257
1258         if (WARN_ONCE(!mvmvif->uploaded, "Changing inactive MAC %pM/%d\n",
1259                       vif->addr, ieee80211_vif_type_p2p(vif)))
1260                 return -EIO;
1261
1262         return iwl_mvm_mac_ctx_send(mvm, vif, FW_CTXT_ACTION_MODIFY,
1263                                     force_assoc_off, bssid_override);
1264 }
1265
1266 int iwl_mvm_mac_ctxt_remove(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1267 {
1268         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1269         struct iwl_mac_ctx_cmd cmd;
1270         int ret;
1271
1272         if (WARN_ONCE(!mvmvif->uploaded, "Removing inactive MAC %pM/%d\n",
1273                       vif->addr, ieee80211_vif_type_p2p(vif)))
1274                 return -EIO;
1275
1276         memset(&cmd, 0, sizeof(cmd));
1277
1278         cmd.id_and_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
1279                                                            mvmvif->color));
1280         cmd.action = cpu_to_le32(FW_CTXT_ACTION_REMOVE);
1281
1282         ret = iwl_mvm_send_cmd_pdu(mvm, MAC_CONTEXT_CMD, 0,
1283                                    sizeof(cmd), &cmd);
1284         if (ret) {
1285                 IWL_ERR(mvm, "Failed to remove MAC context: %d\n", ret);
1286                 return ret;
1287         }
1288
1289         mvmvif->uploaded = false;
1290
1291         if (vif->type == NL80211_IFTYPE_MONITOR)
1292                 __clear_bit(IEEE80211_HW_RX_INCLUDES_FCS, mvm->hw->flags);
1293
1294         return 0;
1295 }
1296
1297 static void iwl_mvm_csa_count_down(struct iwl_mvm *mvm,
1298                                    struct ieee80211_vif *csa_vif, u32 gp2,
1299                                    bool tx_success)
1300 {
1301         struct iwl_mvm_vif *mvmvif =
1302                         iwl_mvm_vif_from_mac80211(csa_vif);
1303
1304         /* Don't start to countdown from a failed beacon */
1305         if (!tx_success && !mvmvif->csa_countdown)
1306                 return;
1307
1308         mvmvif->csa_countdown = true;
1309
1310         if (!ieee80211_csa_is_complete(csa_vif)) {
1311                 int c = ieee80211_csa_update_counter(csa_vif);
1312
1313                 iwl_mvm_mac_ctxt_beacon_changed(mvm, csa_vif);
1314                 if (csa_vif->p2p &&
1315                     !iwl_mvm_te_scheduled(&mvmvif->time_event_data) && gp2 &&
1316                     tx_success) {
1317                         u32 rel_time = (c + 1) *
1318                                        csa_vif->bss_conf.beacon_int -
1319                                        IWL_MVM_CHANNEL_SWITCH_TIME_GO;
1320                         u32 apply_time = gp2 + rel_time * 1024;
1321
1322                         iwl_mvm_schedule_csa_period(mvm, csa_vif,
1323                                          IWL_MVM_CHANNEL_SWITCH_TIME_GO -
1324                                          IWL_MVM_CHANNEL_SWITCH_MARGIN,
1325                                          apply_time);
1326                 }
1327         } else if (!iwl_mvm_te_scheduled(&mvmvif->time_event_data)) {
1328                 /* we don't have CSA NoA scheduled yet, switch now */
1329                 ieee80211_csa_finish(csa_vif);
1330                 RCU_INIT_POINTER(mvm->csa_vif, NULL);
1331         }
1332 }
1333
1334 void iwl_mvm_rx_beacon_notif(struct iwl_mvm *mvm,
1335                              struct iwl_rx_cmd_buffer *rxb)
1336 {
1337         struct iwl_rx_packet *pkt = rxb_addr(rxb);
1338         struct iwl_extended_beacon_notif *beacon = (void *)pkt->data;
1339         struct iwl_mvm_tx_resp *beacon_notify_hdr;
1340         struct ieee80211_vif *csa_vif;
1341         struct ieee80211_vif *tx_blocked_vif;
1342         u16 status;
1343
1344         lockdep_assert_held(&mvm->mutex);
1345
1346         beacon_notify_hdr = &beacon->beacon_notify_hdr;
1347         mvm->ap_last_beacon_gp2 = le32_to_cpu(beacon->gp2);
1348
1349         status = le16_to_cpu(beacon_notify_hdr->status.status) & TX_STATUS_MSK;
1350         IWL_DEBUG_RX(mvm,
1351                      "beacon status %#x retries:%d tsf:0x%16llX gp2:0x%X rate:%d\n",
1352                      status, beacon_notify_hdr->failure_frame,
1353                      le64_to_cpu(beacon->tsf),
1354                      mvm->ap_last_beacon_gp2,
1355                      le32_to_cpu(beacon_notify_hdr->initial_rate));
1356
1357         csa_vif = rcu_dereference_protected(mvm->csa_vif,
1358                                             lockdep_is_held(&mvm->mutex));
1359         if (unlikely(csa_vif && csa_vif->csa_active))
1360                 iwl_mvm_csa_count_down(mvm, csa_vif, mvm->ap_last_beacon_gp2,
1361                                        (status == TX_STATUS_SUCCESS));
1362
1363         tx_blocked_vif = rcu_dereference_protected(mvm->csa_tx_blocked_vif,
1364                                                 lockdep_is_held(&mvm->mutex));
1365         if (unlikely(tx_blocked_vif)) {
1366                 struct iwl_mvm_vif *mvmvif =
1367                         iwl_mvm_vif_from_mac80211(tx_blocked_vif);
1368
1369                 /*
1370                  * The channel switch is started and we have blocked the
1371                  * stations. If this is the first beacon (the timeout wasn't
1372                  * set), set the unblock timeout, otherwise countdown
1373                  */
1374                 if (!mvm->csa_tx_block_bcn_timeout)
1375                         mvm->csa_tx_block_bcn_timeout =
1376                                 IWL_MVM_CS_UNBLOCK_TX_TIMEOUT;
1377                 else
1378                         mvm->csa_tx_block_bcn_timeout--;
1379
1380                 /* Check if the timeout is expired, and unblock tx */
1381                 if (mvm->csa_tx_block_bcn_timeout == 0) {
1382                         iwl_mvm_modify_all_sta_disable_tx(mvm, mvmvif, false);
1383                         RCU_INIT_POINTER(mvm->csa_tx_blocked_vif, NULL);
1384                 }
1385         }
1386 }
1387
1388 static void iwl_mvm_beacon_loss_iterator(void *_data, u8 *mac,
1389                                          struct ieee80211_vif *vif)
1390 {
1391         struct iwl_missed_beacons_notif *missed_beacons = _data;
1392         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1393         struct iwl_mvm *mvm = mvmvif->mvm;
1394         struct iwl_fw_dbg_trigger_missed_bcon *bcon_trig;
1395         struct iwl_fw_dbg_trigger_tlv *trigger;
1396         u32 stop_trig_missed_bcon, stop_trig_missed_bcon_since_rx;
1397         u32 rx_missed_bcon, rx_missed_bcon_since_rx;
1398
1399         if (mvmvif->id != (u16)le32_to_cpu(missed_beacons->mac_id))
1400                 return;
1401
1402         rx_missed_bcon = le32_to_cpu(missed_beacons->consec_missed_beacons);
1403         rx_missed_bcon_since_rx =
1404                 le32_to_cpu(missed_beacons->consec_missed_beacons_since_last_rx);
1405         /*
1406          * TODO: the threshold should be adjusted based on latency conditions,
1407          * and/or in case of a CS flow on one of the other AP vifs.
1408          */
1409         if (le32_to_cpu(missed_beacons->consec_missed_beacons_since_last_rx) >
1410              IWL_MVM_MISSED_BEACONS_THRESHOLD)
1411                 ieee80211_beacon_loss(vif);
1412
1413         if (!iwl_fw_dbg_trigger_enabled(mvm->fw,
1414                                         FW_DBG_TRIGGER_MISSED_BEACONS))
1415                 return;
1416
1417         trigger = iwl_fw_dbg_get_trigger(mvm->fw,
1418                                          FW_DBG_TRIGGER_MISSED_BEACONS);
1419         bcon_trig = (void *)trigger->data;
1420         stop_trig_missed_bcon = le32_to_cpu(bcon_trig->stop_consec_missed_bcon);
1421         stop_trig_missed_bcon_since_rx =
1422                 le32_to_cpu(bcon_trig->stop_consec_missed_bcon_since_rx);
1423
1424         /* TODO: implement start trigger */
1425
1426         if (!iwl_fw_dbg_trigger_check_stop(mvm, vif, trigger))
1427                 return;
1428
1429         if (rx_missed_bcon_since_rx >= stop_trig_missed_bcon_since_rx ||
1430             rx_missed_bcon >= stop_trig_missed_bcon)
1431                 iwl_mvm_fw_dbg_collect_trig(mvm, trigger, NULL);
1432 }
1433
1434 void iwl_mvm_rx_missed_beacons_notif(struct iwl_mvm *mvm,
1435                                      struct iwl_rx_cmd_buffer *rxb)
1436 {
1437         struct iwl_rx_packet *pkt = rxb_addr(rxb);
1438         struct iwl_missed_beacons_notif *mb = (void *)pkt->data;
1439
1440         IWL_DEBUG_INFO(mvm,
1441                        "missed bcn mac_id=%u, consecutive=%u (%u, %u, %u)\n",
1442                        le32_to_cpu(mb->mac_id),
1443                        le32_to_cpu(mb->consec_missed_beacons),
1444                        le32_to_cpu(mb->consec_missed_beacons_since_last_rx),
1445                        le32_to_cpu(mb->num_recvd_beacons),
1446                        le32_to_cpu(mb->num_expected_beacons));
1447
1448         ieee80211_iterate_active_interfaces_atomic(mvm->hw,
1449                                                    IEEE80211_IFACE_ITER_NORMAL,
1450                                                    iwl_mvm_beacon_loss_iterator,
1451                                                    mb);
1452 }