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