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