]> rtime.felk.cvut.cz Git - linux-imx.git/blob - drivers/net/wireless/iwlwifi/mvm/mac-ctxt.c
af_unix: use freezable blocking calls in read
[linux-imx.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 - 2013 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 - 2013 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
71 const u8 iwl_mvm_ac_to_tx_fifo[] = {
72         IWL_MVM_TX_FIFO_BK,
73         IWL_MVM_TX_FIFO_BE,
74         IWL_MVM_TX_FIFO_VI,
75         IWL_MVM_TX_FIFO_VO,
76 };
77
78 struct iwl_mvm_mac_iface_iterator_data {
79         struct iwl_mvm *mvm;
80         struct ieee80211_vif *vif;
81         unsigned long available_mac_ids[BITS_TO_LONGS(NUM_MAC_INDEX_DRIVER)];
82         unsigned long available_tsf_ids[BITS_TO_LONGS(NUM_TSF_IDS)];
83         unsigned long used_hw_queues[BITS_TO_LONGS(IWL_MVM_FIRST_AGG_QUEUE)];
84         enum iwl_tsf_id preferred_tsf;
85         bool found_vif;
86 };
87
88 static void iwl_mvm_mac_iface_iterator(void *_data, u8 *mac,
89                                        struct ieee80211_vif *vif)
90 {
91         struct iwl_mvm_mac_iface_iterator_data *data = _data;
92         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
93         u32 ac;
94
95         /* Iterator may already find the interface being added -- skip it */
96         if (vif == data->vif) {
97                 data->found_vif = true;
98                 return;
99         }
100
101         /* Mark the queues used by the vif */
102         for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
103                 if (vif->hw_queue[ac] != IEEE80211_INVAL_HW_QUEUE)
104                         __set_bit(vif->hw_queue[ac], data->used_hw_queues);
105
106         if (vif->cab_queue != IEEE80211_INVAL_HW_QUEUE)
107                 __set_bit(vif->cab_queue, data->used_hw_queues);
108
109         /*
110          * Mark MAC IDs as used by clearing the available bit, and
111          * (below) mark TSFs as used if their existing use is not
112          * compatible with the new interface type.
113          * No locking or atomic bit operations are needed since the
114          * data is on the stack of the caller function.
115          */
116         __clear_bit(mvmvif->id, data->available_mac_ids);
117
118         /*
119          * The TSF is a hardware/firmware resource, there are 4 and
120          * the driver should assign and free them as needed. However,
121          * there are cases where 2 MACs should share the same TSF ID
122          * for the purpose of clock sync, an optimization to avoid
123          * clock drift causing overlapping TBTTs/DTIMs for a GO and
124          * client in the system.
125          *
126          * The firmware will decide according to the MAC type which
127          * will be the master and slave. Clients that need to sync
128          * with a remote station will be the master, and an AP or GO
129          * will be the slave.
130          *
131          * Depending on the new interface type it can be slaved to
132          * or become the master of an existing interface.
133          */
134         switch (data->vif->type) {
135         case NL80211_IFTYPE_STATION:
136                 /*
137                  * The new interface is client, so if the existing one
138                  * we're iterating is an AP, the TSF should be used to
139                  * avoid drift between the new client and existing AP,
140                  * the existing AP will get drift updates from the new
141                  * client context in this case
142                  */
143                 if (vif->type == NL80211_IFTYPE_AP) {
144                         if (data->preferred_tsf == NUM_TSF_IDS &&
145                             test_bit(mvmvif->tsf_id, data->available_tsf_ids))
146                                 data->preferred_tsf = mvmvif->tsf_id;
147                         return;
148                 }
149                 break;
150         case NL80211_IFTYPE_AP:
151                 /*
152                  * The new interface is AP/GO, so should get drift
153                  * updates from an existing client or use the same
154                  * TSF as an existing GO. There's no drift between
155                  * TSFs internally but if they used different TSFs
156                  * then a new client MAC could update one of them
157                  * and cause drift that way.
158                  */
159                 if (vif->type == NL80211_IFTYPE_STATION ||
160                     vif->type == NL80211_IFTYPE_AP) {
161                         if (data->preferred_tsf == NUM_TSF_IDS &&
162                             test_bit(mvmvif->tsf_id, data->available_tsf_ids))
163                                 data->preferred_tsf = mvmvif->tsf_id;
164                         return;
165                 }
166                 break;
167         default:
168                 /*
169                  * For all other interface types there's no need to
170                  * take drift into account. Either they're exclusive
171                  * like IBSS and monitor, or we don't care much about
172                  * their TSF (like P2P Device), but we won't be able
173                  * to share the TSF resource.
174                  */
175                 break;
176         }
177
178         /*
179          * Unless we exited above, we can't share the TSF resource
180          * that the virtual interface we're iterating over is using
181          * with the new one, so clear the available bit and if this
182          * was the preferred one, reset that as well.
183          */
184         __clear_bit(mvmvif->tsf_id, data->available_tsf_ids);
185
186         if (data->preferred_tsf == mvmvif->tsf_id)
187                 data->preferred_tsf = NUM_TSF_IDS;
188 }
189
190 /*
191  * Get the mask of the queus used by the vif
192  */
193 u32 iwl_mvm_mac_get_queues_mask(struct iwl_mvm *mvm,
194                                 struct ieee80211_vif *vif)
195 {
196         u32 qmask, ac;
197
198         if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
199                 return BIT(IWL_MVM_OFFCHANNEL_QUEUE);
200
201         qmask = (vif->cab_queue != IEEE80211_INVAL_HW_QUEUE) ?
202                 BIT(vif->cab_queue) : 0;
203
204         for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
205                 if (vif->hw_queue[ac] != IEEE80211_INVAL_HW_QUEUE)
206                         qmask |= BIT(vif->hw_queue[ac]);
207
208         return qmask;
209 }
210
211 static int iwl_mvm_mac_ctxt_allocate_resources(struct iwl_mvm *mvm,
212                                                struct ieee80211_vif *vif)
213 {
214         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
215         struct iwl_mvm_mac_iface_iterator_data data = {
216                 .mvm = mvm,
217                 .vif = vif,
218                 .available_mac_ids = { (1 << NUM_MAC_INDEX_DRIVER) - 1 },
219                 .available_tsf_ids = { (1 << NUM_TSF_IDS) - 1 },
220                 /* no preference yet */
221                 .preferred_tsf = NUM_TSF_IDS,
222                 .used_hw_queues = {
223                         BIT(IWL_MVM_OFFCHANNEL_QUEUE) |
224                         BIT(IWL_MVM_AUX_QUEUE) |
225                         BIT(IWL_MVM_CMD_QUEUE)
226                 },
227                 .found_vif = false,
228         };
229         u32 ac;
230         int ret;
231
232         /*
233          * Allocate a MAC ID and a TSF for this MAC, along with the queues
234          * and other resources.
235          */
236
237         /*
238          * Before the iterator, we start with all MAC IDs and TSFs available.
239          *
240          * During iteration, all MAC IDs are cleared that are in use by other
241          * virtual interfaces, and all TSF IDs are cleared that can't be used
242          * by this new virtual interface because they're used by an interface
243          * that can't share it with the new one.
244          * At the same time, we check if there's a preferred TSF in the case
245          * that we should share it with another interface.
246          */
247
248         /* Currently, MAC ID 0 should be used only for the managed vif */
249         if (vif->type != NL80211_IFTYPE_STATION || vif->p2p)
250                 __clear_bit(0, data.available_mac_ids);
251
252         ieee80211_iterate_active_interfaces_atomic(
253                 mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
254                 iwl_mvm_mac_iface_iterator, &data);
255
256         /*
257          * In the case we're getting here during resume, it's similar to
258          * firmware restart, and with RESUME_ALL the iterator will find
259          * the vif being added already.
260          * We don't want to reassign any IDs in either case since doing
261          * so would probably assign different IDs (as interfaces aren't
262          * necessarily added in the same order), but the old IDs were
263          * preserved anyway, so skip ID assignment for both resume and
264          * recovery.
265          */
266         if (data.found_vif)
267                 return 0;
268
269         /* Therefore, in recovery, we can't get here */
270         WARN_ON_ONCE(test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status));
271
272         mvmvif->id = find_first_bit(data.available_mac_ids,
273                                     NUM_MAC_INDEX_DRIVER);
274         if (mvmvif->id == NUM_MAC_INDEX_DRIVER) {
275                 IWL_ERR(mvm, "Failed to init MAC context - no free ID!\n");
276                 ret = -EIO;
277                 goto exit_fail;
278         }
279
280         if (data.preferred_tsf != NUM_TSF_IDS)
281                 mvmvif->tsf_id = data.preferred_tsf;
282         else
283                 mvmvif->tsf_id = find_first_bit(data.available_tsf_ids,
284                                                 NUM_TSF_IDS);
285         if (mvmvif->tsf_id == NUM_TSF_IDS) {
286                 IWL_ERR(mvm, "Failed to init MAC context - no free TSF!\n");
287                 ret = -EIO;
288                 goto exit_fail;
289         }
290
291         mvmvif->color = 0;
292
293         INIT_LIST_HEAD(&mvmvif->time_event_data.list);
294         mvmvif->time_event_data.id = TE_MAX;
295
296         /* No need to allocate data queues to P2P Device MAC.*/
297         if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
298                 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
299                         vif->hw_queue[ac] = IEEE80211_INVAL_HW_QUEUE;
300
301                 return 0;
302         }
303
304         /* Find available queues, and allocate them to the ACs */
305         for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
306                 u8 queue = find_first_zero_bit(data.used_hw_queues,
307                                                IWL_MVM_FIRST_AGG_QUEUE);
308
309                 if (queue >= IWL_MVM_FIRST_AGG_QUEUE) {
310                         IWL_ERR(mvm, "Failed to allocate queue\n");
311                         ret = -EIO;
312                         goto exit_fail;
313                 }
314
315                 __set_bit(queue, data.used_hw_queues);
316                 vif->hw_queue[ac] = queue;
317         }
318
319         /* Allocate the CAB queue for softAP and GO interfaces */
320         if (vif->type == NL80211_IFTYPE_AP) {
321                 u8 queue = find_first_zero_bit(data.used_hw_queues,
322                                                IWL_MVM_FIRST_AGG_QUEUE);
323
324                 if (queue >= IWL_MVM_FIRST_AGG_QUEUE) {
325                         IWL_ERR(mvm, "Failed to allocate cab queue\n");
326                         ret = -EIO;
327                         goto exit_fail;
328                 }
329
330                 vif->cab_queue = queue;
331         } else {
332                 vif->cab_queue = IEEE80211_INVAL_HW_QUEUE;
333         }
334
335         mvmvif->bcast_sta.sta_id = IWL_MVM_STATION_COUNT;
336         mvmvif->ap_sta_id = IWL_MVM_STATION_COUNT;
337
338         return 0;
339
340 exit_fail:
341         memset(mvmvif, 0, sizeof(struct iwl_mvm_vif));
342         memset(vif->hw_queue, IEEE80211_INVAL_HW_QUEUE, sizeof(vif->hw_queue));
343         vif->cab_queue = IEEE80211_INVAL_HW_QUEUE;
344         return ret;
345 }
346
347 int iwl_mvm_mac_ctxt_init(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
348 {
349         u32 ac;
350         int ret;
351
352         lockdep_assert_held(&mvm->mutex);
353
354         ret = iwl_mvm_mac_ctxt_allocate_resources(mvm, vif);
355         if (ret)
356                 return ret;
357
358         switch (vif->type) {
359         case NL80211_IFTYPE_P2P_DEVICE:
360                 iwl_trans_ac_txq_enable(mvm->trans, IWL_MVM_OFFCHANNEL_QUEUE,
361                                         IWL_MVM_TX_FIFO_VO);
362                 break;
363         case NL80211_IFTYPE_AP:
364                 iwl_trans_ac_txq_enable(mvm->trans, vif->cab_queue,
365                                         IWL_MVM_TX_FIFO_VO);
366                 /* fall through */
367         default:
368                 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
369                         iwl_trans_ac_txq_enable(mvm->trans, vif->hw_queue[ac],
370                                                 iwl_mvm_ac_to_tx_fifo[ac]);
371                 break;
372         }
373
374         return 0;
375 }
376
377 void iwl_mvm_mac_ctxt_release(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
378 {
379         int ac;
380
381         lockdep_assert_held(&mvm->mutex);
382
383         switch (vif->type) {
384         case NL80211_IFTYPE_P2P_DEVICE:
385                 iwl_trans_txq_disable(mvm->trans, IWL_MVM_OFFCHANNEL_QUEUE);
386                 break;
387         case NL80211_IFTYPE_AP:
388                 iwl_trans_txq_disable(mvm->trans, vif->cab_queue);
389                 /* fall through */
390         default:
391                 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
392                         iwl_trans_txq_disable(mvm->trans, vif->hw_queue[ac]);
393         }
394 }
395
396 static void iwl_mvm_ack_rates(struct iwl_mvm *mvm,
397                               struct ieee80211_vif *vif,
398                               enum ieee80211_band band,
399                               u8 *cck_rates, u8 *ofdm_rates)
400 {
401         struct ieee80211_supported_band *sband;
402         unsigned long basic = vif->bss_conf.basic_rates;
403         int lowest_present_ofdm = 100;
404         int lowest_present_cck = 100;
405         u8 cck = 0;
406         u8 ofdm = 0;
407         int i;
408
409         sband = mvm->hw->wiphy->bands[band];
410
411         for_each_set_bit(i, &basic, BITS_PER_LONG) {
412                 int hw = sband->bitrates[i].hw_value;
413                 if (hw >= IWL_FIRST_OFDM_RATE) {
414                         ofdm |= BIT(hw - IWL_FIRST_OFDM_RATE);
415                         if (lowest_present_ofdm > hw)
416                                 lowest_present_ofdm = hw;
417                 } else {
418                         BUILD_BUG_ON(IWL_FIRST_CCK_RATE != 0);
419
420                         cck |= BIT(hw);
421                         if (lowest_present_cck > hw)
422                                 lowest_present_cck = hw;
423                 }
424         }
425
426         /*
427          * Now we've got the basic rates as bitmaps in the ofdm and cck
428          * variables. This isn't sufficient though, as there might not
429          * be all the right rates in the bitmap. E.g. if the only basic
430          * rates are 5.5 Mbps and 11 Mbps, we still need to add 1 Mbps
431          * and 6 Mbps because the 802.11-2007 standard says in 9.6:
432          *
433          *    [...] a STA responding to a received frame shall transmit
434          *    its Control Response frame [...] at the highest rate in the
435          *    BSSBasicRateSet parameter that is less than or equal to the
436          *    rate of the immediately previous frame in the frame exchange
437          *    sequence ([...]) and that is of the same modulation class
438          *    ([...]) as the received frame. If no rate contained in the
439          *    BSSBasicRateSet parameter meets these conditions, then the
440          *    control frame sent in response to a received frame shall be
441          *    transmitted at the highest mandatory rate of the PHY that is
442          *    less than or equal to the rate of the received frame, and
443          *    that is of the same modulation class as the received frame.
444          *
445          * As a consequence, we need to add all mandatory rates that are
446          * lower than all of the basic rates to these bitmaps.
447          */
448
449         if (IWL_RATE_24M_INDEX < lowest_present_ofdm)
450                 ofdm |= IWL_RATE_BIT_MSK(24) >> IWL_FIRST_OFDM_RATE;
451         if (IWL_RATE_12M_INDEX < lowest_present_ofdm)
452                 ofdm |= IWL_RATE_BIT_MSK(12) >> IWL_FIRST_OFDM_RATE;
453         /* 6M already there or needed so always add */
454         ofdm |= IWL_RATE_BIT_MSK(6) >> IWL_FIRST_OFDM_RATE;
455
456         /*
457          * CCK is a bit more complex with DSSS vs. HR/DSSS vs. ERP.
458          * Note, however:
459          *  - if no CCK rates are basic, it must be ERP since there must
460          *    be some basic rates at all, so they're OFDM => ERP PHY
461          *    (or we're in 5 GHz, and the cck bitmap will never be used)
462          *  - if 11M is a basic rate, it must be ERP as well, so add 5.5M
463          *  - if 5.5M is basic, 1M and 2M are mandatory
464          *  - if 2M is basic, 1M is mandatory
465          *  - if 1M is basic, that's the only valid ACK rate.
466          * As a consequence, it's not as complicated as it sounds, just add
467          * any lower rates to the ACK rate bitmap.
468          */
469         if (IWL_RATE_11M_INDEX < lowest_present_cck)
470                 cck |= IWL_RATE_BIT_MSK(11) >> IWL_FIRST_CCK_RATE;
471         if (IWL_RATE_5M_INDEX < lowest_present_cck)
472                 cck |= IWL_RATE_BIT_MSK(5) >> IWL_FIRST_CCK_RATE;
473         if (IWL_RATE_2M_INDEX < lowest_present_cck)
474                 cck |= IWL_RATE_BIT_MSK(2) >> IWL_FIRST_CCK_RATE;
475         /* 1M already there or needed so always add */
476         cck |= IWL_RATE_BIT_MSK(1) >> IWL_FIRST_CCK_RATE;
477
478         *cck_rates = cck;
479         *ofdm_rates = ofdm;
480 }
481
482 static void iwl_mvm_mac_ctxt_cmd_common(struct iwl_mvm *mvm,
483                                         struct ieee80211_vif *vif,
484                                         struct iwl_mac_ctx_cmd *cmd,
485                                         u32 action)
486 {
487         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
488         struct ieee80211_chanctx_conf *chanctx;
489         u8 cck_ack_rates, ofdm_ack_rates;
490         int i;
491
492         cmd->id_and_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
493                                                             mvmvif->color));
494         cmd->action = cpu_to_le32(action);
495
496         switch (vif->type) {
497         case NL80211_IFTYPE_STATION:
498                 if (vif->p2p)
499                         cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_P2P_STA);
500                 else
501                         cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_BSS_STA);
502                 break;
503         case NL80211_IFTYPE_AP:
504                 cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_GO);
505                 break;
506         case NL80211_IFTYPE_MONITOR:
507                 cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_LISTENER);
508                 break;
509         case NL80211_IFTYPE_P2P_DEVICE:
510                 cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_P2P_DEVICE);
511                 break;
512         case NL80211_IFTYPE_ADHOC:
513                 cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_IBSS);
514                 break;
515         default:
516                 WARN_ON_ONCE(1);
517         }
518
519         cmd->tsf_id = cpu_to_le32(mvmvif->tsf_id);
520
521         memcpy(cmd->node_addr, vif->addr, ETH_ALEN);
522         if (vif->bss_conf.bssid)
523                 memcpy(cmd->bssid_addr, vif->bss_conf.bssid, ETH_ALEN);
524         else
525                 eth_broadcast_addr(cmd->bssid_addr);
526
527         rcu_read_lock();
528         chanctx = rcu_dereference(vif->chanctx_conf);
529         iwl_mvm_ack_rates(mvm, vif, chanctx ? chanctx->def.chan->band
530                                             : IEEE80211_BAND_2GHZ,
531                           &cck_ack_rates, &ofdm_ack_rates);
532         rcu_read_unlock();
533
534         cmd->cck_rates = cpu_to_le32((u32)cck_ack_rates);
535         cmd->ofdm_rates = cpu_to_le32((u32)ofdm_ack_rates);
536
537         cmd->cck_short_preamble =
538                 cpu_to_le32(vif->bss_conf.use_short_preamble ?
539                             MAC_FLG_SHORT_PREAMBLE : 0);
540         cmd->short_slot =
541                 cpu_to_le32(vif->bss_conf.use_short_slot ?
542                             MAC_FLG_SHORT_SLOT : 0);
543
544         for (i = 0; i < AC_NUM; i++) {
545                 cmd->ac[i].cw_min = cpu_to_le16(mvmvif->queue_params[i].cw_min);
546                 cmd->ac[i].cw_max = cpu_to_le16(mvmvif->queue_params[i].cw_max);
547                 cmd->ac[i].aifsn = mvmvif->queue_params[i].aifs;
548                 cmd->ac[i].edca_txop =
549                         cpu_to_le16(mvmvif->queue_params[i].txop * 32);
550                 cmd->ac[i].fifos_mask = BIT(iwl_mvm_ac_to_tx_fifo[i]);
551         }
552
553         if (vif->bss_conf.qos)
554                 cmd->qos_flags |= cpu_to_le32(MAC_QOS_FLG_UPDATE_EDCA);
555
556         /* Don't use cts to self as the fw doesn't support it currently. */
557         if (vif->bss_conf.use_cts_prot)
558                 cmd->protection_flags |= cpu_to_le32(MAC_PROT_FLG_TGG_PROTECT);
559
560         /*
561          * I think that we should enable these 2 flags regardless the HT PROT
562          * fields in the HT IE, but I am not sure. Someone knows whom to ask?...
563          */
564         if (vif->bss_conf.chandef.width != NL80211_CHAN_WIDTH_20_NOHT) {
565                 cmd->qos_flags |= cpu_to_le32(MAC_QOS_FLG_TGN);
566                 cmd->protection_flags |= cpu_to_le32(MAC_PROT_FLG_HT_PROT |
567                                                      MAC_PROT_FLG_FAT_PROT);
568         }
569
570         cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP);
571 }
572
573 static int iwl_mvm_mac_ctxt_send_cmd(struct iwl_mvm *mvm,
574                                      struct iwl_mac_ctx_cmd *cmd)
575 {
576         int ret = iwl_mvm_send_cmd_pdu(mvm, MAC_CONTEXT_CMD, CMD_SYNC,
577                                        sizeof(*cmd), cmd);
578         if (ret)
579                 IWL_ERR(mvm, "Failed to send MAC context (action:%d): %d\n",
580                         le32_to_cpu(cmd->action), ret);
581         return ret;
582 }
583
584 /*
585  * Fill the specific data for mac context of type station or p2p client
586  */
587 static void iwl_mvm_mac_ctxt_cmd_fill_sta(struct iwl_mvm *mvm,
588                                           struct ieee80211_vif *vif,
589                                           struct iwl_mac_data_sta *ctxt_sta)
590 {
591         /* We need the dtim_period to set the MAC as associated */
592         if (vif->bss_conf.assoc && vif->bss_conf.dtim_period) {
593                 u32 dtim_offs;
594
595                 /*
596                  * The DTIM count counts down, so when it is N that means N
597                  * more beacon intervals happen until the DTIM TBTT. Therefore
598                  * add this to the current time. If that ends up being in the
599                  * future, the firmware will handle it.
600                  *
601                  * Also note that the system_timestamp (which we get here as
602                  * "sync_device_ts") and TSF timestamp aren't at exactly the
603                  * same offset in the frame -- the TSF is at the first symbol
604                  * of the TSF, the system timestamp is at signal acquisition
605                  * time. This means there's an offset between them of at most
606                  * a few hundred microseconds (24 * 8 bits + PLCP time gives
607                  * 384us in the longest case), this is currently not relevant
608                  * as the firmware wakes up around 2ms before the TBTT.
609                  */
610                 dtim_offs = vif->bss_conf.sync_dtim_count *
611                                 vif->bss_conf.beacon_int;
612                 /* convert TU to usecs */
613                 dtim_offs *= 1024;
614
615                 ctxt_sta->dtim_tsf =
616                         cpu_to_le64(vif->bss_conf.sync_tsf + dtim_offs);
617                 ctxt_sta->dtim_time =
618                         cpu_to_le32(vif->bss_conf.sync_device_ts + dtim_offs);
619
620                 IWL_DEBUG_INFO(mvm, "DTIM TBTT is 0x%llx/0x%x, offset %d\n",
621                                le64_to_cpu(ctxt_sta->dtim_tsf),
622                                le32_to_cpu(ctxt_sta->dtim_time),
623                                dtim_offs);
624
625                 ctxt_sta->is_assoc = cpu_to_le32(1);
626         } else {
627                 ctxt_sta->is_assoc = cpu_to_le32(0);
628         }
629
630         ctxt_sta->bi = cpu_to_le32(vif->bss_conf.beacon_int);
631         ctxt_sta->bi_reciprocal =
632                 cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int));
633         ctxt_sta->dtim_interval = cpu_to_le32(vif->bss_conf.beacon_int *
634                                               vif->bss_conf.dtim_period);
635         ctxt_sta->dtim_reciprocal =
636                 cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int *
637                                                vif->bss_conf.dtim_period));
638
639         ctxt_sta->listen_interval = cpu_to_le32(mvm->hw->conf.listen_interval);
640         ctxt_sta->assoc_id = cpu_to_le32(vif->bss_conf.aid);
641 }
642
643 static int iwl_mvm_mac_ctxt_cmd_station(struct iwl_mvm *mvm,
644                                         struct ieee80211_vif *vif,
645                                         u32 action)
646 {
647         struct iwl_mac_ctx_cmd cmd = {};
648
649         WARN_ON(vif->type != NL80211_IFTYPE_STATION || vif->p2p);
650
651         /* Fill the common data for all mac context types */
652         iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, action);
653
654         /* Allow beacons to pass through as long as we are not associated,or we
655          * do not have dtim period information */
656         if (!vif->bss_conf.assoc || !vif->bss_conf.dtim_period)
657                 cmd.filter_flags |= cpu_to_le32(MAC_FILTER_IN_BEACON);
658         else
659                 cmd.filter_flags &= ~cpu_to_le32(MAC_FILTER_IN_BEACON);
660
661         /* Fill the data specific for station mode */
662         iwl_mvm_mac_ctxt_cmd_fill_sta(mvm, vif, &cmd.sta);
663
664         return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
665 }
666
667 static int iwl_mvm_mac_ctxt_cmd_p2p_client(struct iwl_mvm *mvm,
668                                            struct ieee80211_vif *vif,
669                                            u32 action)
670 {
671         struct iwl_mac_ctx_cmd cmd = {};
672         struct ieee80211_p2p_noa_attr *noa = &vif->bss_conf.p2p_noa_attr;
673
674         WARN_ON(vif->type != NL80211_IFTYPE_STATION || !vif->p2p);
675
676         /* Fill the common data for all mac context types */
677         iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, action);
678
679         /* Fill the data specific for station mode */
680         iwl_mvm_mac_ctxt_cmd_fill_sta(mvm, vif, &cmd.p2p_sta.sta);
681
682         cmd.p2p_sta.ctwin = cpu_to_le32(noa->oppps_ctwindow &
683                                         IEEE80211_P2P_OPPPS_CTWINDOW_MASK);
684
685         return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
686 }
687
688 static int iwl_mvm_mac_ctxt_cmd_listener(struct iwl_mvm *mvm,
689                                          struct ieee80211_vif *vif,
690                                          u32 action)
691 {
692         struct iwl_mac_ctx_cmd cmd = {};
693
694         WARN_ON(vif->type != NL80211_IFTYPE_MONITOR);
695
696         iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, action);
697
698         cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_PROMISC |
699                                        MAC_FILTER_IN_CONTROL_AND_MGMT |
700                                        MAC_FILTER_IN_BEACON |
701                                        MAC_FILTER_IN_PROBE_REQUEST);
702
703         return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
704 }
705
706 struct iwl_mvm_go_iterator_data {
707         bool go_active;
708 };
709
710 static void iwl_mvm_go_iterator(void *_data, u8 *mac, struct ieee80211_vif *vif)
711 {
712         struct iwl_mvm_go_iterator_data *data = _data;
713         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
714
715         if (vif->type == NL80211_IFTYPE_AP && vif->p2p && mvmvif->ap_active)
716                 data->go_active = true;
717 }
718
719 static int iwl_mvm_mac_ctxt_cmd_p2p_device(struct iwl_mvm *mvm,
720                                            struct ieee80211_vif *vif,
721                                            u32 action)
722 {
723         struct iwl_mac_ctx_cmd cmd = {};
724         struct iwl_mvm_go_iterator_data data = {};
725
726         WARN_ON(vif->type != NL80211_IFTYPE_P2P_DEVICE);
727
728         iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, action);
729
730         cmd.protection_flags |= cpu_to_le32(MAC_PROT_FLG_TGG_PROTECT);
731
732         /* Override the filter flags to accept only probe requests */
733         cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_PROBE_REQUEST);
734
735         /*
736          * This flag should be set to true when the P2P Device is
737          * discoverable and there is at least another active P2P GO. Settings
738          * this flag will allow the P2P Device to be discoverable on other
739          * channels in addition to its listen channel.
740          * Note that this flag should not be set in other cases as it opens the
741          * Rx filters on all MAC and increases the number of interrupts.
742          */
743         ieee80211_iterate_active_interfaces_atomic(
744                 mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
745                 iwl_mvm_go_iterator, &data);
746
747         cmd.p2p_dev.is_disc_extended = cpu_to_le32(data.go_active ? 1 : 0);
748         return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
749 }
750
751 static void iwl_mvm_mac_ctxt_set_tim(struct iwl_mvm *mvm,
752                                      struct iwl_mac_beacon_cmd *beacon_cmd,
753                                      u8 *beacon, u32 frame_size)
754 {
755         u32 tim_idx;
756         struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)beacon;
757
758         /* The index is relative to frame start but we start looking at the
759          * variable-length part of the beacon. */
760         tim_idx = mgmt->u.beacon.variable - beacon;
761
762         /* Parse variable-length elements of beacon to find WLAN_EID_TIM */
763         while ((tim_idx < (frame_size - 2)) &&
764                         (beacon[tim_idx] != WLAN_EID_TIM))
765                 tim_idx += beacon[tim_idx+1] + 2;
766
767         /* If TIM field was found, set variables */
768         if ((tim_idx < (frame_size - 1)) && (beacon[tim_idx] == WLAN_EID_TIM)) {
769                 beacon_cmd->tim_idx = cpu_to_le32(tim_idx);
770                 beacon_cmd->tim_size = cpu_to_le32((u32)beacon[tim_idx+1]);
771         } else {
772                 IWL_WARN(mvm, "Unable to find TIM Element in beacon\n");
773         }
774 }
775
776 static int iwl_mvm_mac_ctxt_send_beacon(struct iwl_mvm *mvm,
777                                         struct ieee80211_vif *vif,
778                                         struct sk_buff *beacon)
779 {
780         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
781         struct iwl_host_cmd cmd = {
782                 .id = BEACON_TEMPLATE_CMD,
783                 .flags = CMD_ASYNC,
784         };
785         struct iwl_mac_beacon_cmd beacon_cmd = {};
786         struct ieee80211_tx_info *info;
787         u32 beacon_skb_len;
788         u32 rate;
789
790         if (WARN_ON(!beacon))
791                 return -EINVAL;
792
793         beacon_skb_len = beacon->len;
794
795         /* TODO: for now the beacon template id is set to be the mac context id.
796          * Might be better to handle it as another resource ... */
797         beacon_cmd.template_id = cpu_to_le32((u32)mvmvif->id);
798
799         /* Set up TX command fields */
800         beacon_cmd.tx.len = cpu_to_le16((u16)beacon_skb_len);
801         beacon_cmd.tx.sta_id = mvmvif->bcast_sta.sta_id;
802         beacon_cmd.tx.life_time = cpu_to_le32(TX_CMD_LIFE_TIME_INFINITE);
803         beacon_cmd.tx.tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL |
804                                              TX_CMD_FLG_BT_DIS  |
805                                              TX_CMD_FLG_TSF);
806
807         mvm->mgmt_last_antenna_idx =
808                 iwl_mvm_next_antenna(mvm, iwl_fw_valid_tx_ant(mvm->fw),
809                                      mvm->mgmt_last_antenna_idx);
810
811         beacon_cmd.tx.rate_n_flags =
812                 cpu_to_le32(BIT(mvm->mgmt_last_antenna_idx) <<
813                             RATE_MCS_ANT_POS);
814
815         info = IEEE80211_SKB_CB(beacon);
816
817         if (info->band == IEEE80211_BAND_5GHZ || vif->p2p) {
818                 rate = IWL_FIRST_OFDM_RATE;
819         } else {
820                 rate = IWL_FIRST_CCK_RATE;
821                 beacon_cmd.tx.rate_n_flags |= cpu_to_le32(RATE_MCS_CCK_MSK);
822         }
823         beacon_cmd.tx.rate_n_flags |=
824                 cpu_to_le32(iwl_mvm_mac80211_idx_to_hwrate(rate));
825
826         /* Set up TX beacon command fields */
827         iwl_mvm_mac_ctxt_set_tim(mvm, &beacon_cmd,
828                                  beacon->data,
829                                  beacon_skb_len);
830
831         /* Submit command */
832         cmd.len[0] = sizeof(beacon_cmd);
833         cmd.data[0] = &beacon_cmd;
834         cmd.dataflags[0] = 0;
835         cmd.len[1] = beacon_skb_len;
836         cmd.data[1] = beacon->data;
837         cmd.dataflags[1] = IWL_HCMD_DFL_DUP;
838
839         return iwl_mvm_send_cmd(mvm, &cmd);
840 }
841
842 /* The beacon template for the AP/GO context has changed and needs update */
843 int iwl_mvm_mac_ctxt_beacon_changed(struct iwl_mvm *mvm,
844                                     struct ieee80211_vif *vif)
845 {
846         struct sk_buff *beacon;
847         int ret;
848
849         WARN_ON(vif->type != NL80211_IFTYPE_AP);
850
851         beacon = ieee80211_beacon_get(mvm->hw, vif);
852         if (!beacon)
853                 return -ENOMEM;
854
855         ret = iwl_mvm_mac_ctxt_send_beacon(mvm, vif, beacon);
856         dev_kfree_skb(beacon);
857         return ret;
858 }
859
860 /*
861  * Fill the specific data for mac context of type AP of P2P GO
862  */
863 static void iwl_mvm_mac_ctxt_cmd_fill_ap(struct iwl_mvm *mvm,
864                                          struct ieee80211_vif *vif,
865                                          struct iwl_mac_data_ap *ctxt_ap,
866                                          bool add)
867 {
868         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
869
870         ctxt_ap->bi = cpu_to_le32(vif->bss_conf.beacon_int);
871         ctxt_ap->bi_reciprocal =
872                 cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int));
873         ctxt_ap->dtim_interval = cpu_to_le32(vif->bss_conf.beacon_int *
874                                              vif->bss_conf.dtim_period);
875         ctxt_ap->dtim_reciprocal =
876                 cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int *
877                                                vif->bss_conf.dtim_period));
878
879         ctxt_ap->mcast_qid = cpu_to_le32(vif->cab_queue);
880
881         /*
882          * Only read the system time when the MAC is being added, when we
883          * just modify the MAC then we should keep the time -- the firmware
884          * can otherwise have a "jumping" TBTT.
885          */
886         if (add)
887                 mvmvif->ap_beacon_time =
888                         iwl_read_prph(mvm->trans, DEVICE_SYSTEM_TIME_REG);
889
890         ctxt_ap->beacon_time = cpu_to_le32(mvmvif->ap_beacon_time);
891
892         ctxt_ap->beacon_tsf = 0; /* unused */
893
894         /* TODO: Assume that the beacon id == mac context id */
895         ctxt_ap->beacon_template = cpu_to_le32(mvmvif->id);
896 }
897
898 static int iwl_mvm_mac_ctxt_cmd_ap(struct iwl_mvm *mvm,
899                                    struct ieee80211_vif *vif,
900                                    u32 action)
901 {
902         struct iwl_mac_ctx_cmd cmd = {};
903
904         WARN_ON(vif->type != NL80211_IFTYPE_AP || vif->p2p);
905
906         /* Fill the common data for all mac context types */
907         iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, action);
908
909         /* Also enable probe requests to pass */
910         cmd.filter_flags |= cpu_to_le32(MAC_FILTER_IN_PROBE_REQUEST);
911
912         /* Fill the data specific for ap mode */
913         iwl_mvm_mac_ctxt_cmd_fill_ap(mvm, vif, &cmd.ap,
914                                      action == FW_CTXT_ACTION_ADD);
915
916         return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
917 }
918
919 static int iwl_mvm_mac_ctxt_cmd_go(struct iwl_mvm *mvm,
920                                    struct ieee80211_vif *vif,
921                                    u32 action)
922 {
923         struct iwl_mac_ctx_cmd cmd = {};
924         struct ieee80211_p2p_noa_attr *noa = &vif->bss_conf.p2p_noa_attr;
925
926         WARN_ON(vif->type != NL80211_IFTYPE_AP || !vif->p2p);
927
928         /* Fill the common data for all mac context types */
929         iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, action);
930
931         /* Fill the data specific for GO mode */
932         iwl_mvm_mac_ctxt_cmd_fill_ap(mvm, vif, &cmd.go.ap,
933                                      action == FW_CTXT_ACTION_ADD);
934
935         cmd.go.ctwin = cpu_to_le32(noa->oppps_ctwindow &
936                                         IEEE80211_P2P_OPPPS_CTWINDOW_MASK);
937         cmd.go.opp_ps_enabled =
938                         cpu_to_le32(!!(noa->oppps_ctwindow &
939                                         IEEE80211_P2P_OPPPS_ENABLE_BIT));
940
941         return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
942 }
943
944 static int iwl_mvm_mac_ctx_send(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
945                                 u32 action)
946 {
947         switch (vif->type) {
948         case NL80211_IFTYPE_STATION:
949                 if (!vif->p2p)
950                         return iwl_mvm_mac_ctxt_cmd_station(mvm, vif,
951                                                             action);
952                 else
953                         return iwl_mvm_mac_ctxt_cmd_p2p_client(mvm, vif,
954                                                                action);
955                 break;
956         case NL80211_IFTYPE_AP:
957                 if (!vif->p2p)
958                         return iwl_mvm_mac_ctxt_cmd_ap(mvm, vif, action);
959                 else
960                         return iwl_mvm_mac_ctxt_cmd_go(mvm, vif, action);
961                 break;
962         case NL80211_IFTYPE_MONITOR:
963                 return iwl_mvm_mac_ctxt_cmd_listener(mvm, vif, action);
964         case NL80211_IFTYPE_P2P_DEVICE:
965                 return iwl_mvm_mac_ctxt_cmd_p2p_device(mvm, vif, action);
966         default:
967                 break;
968         }
969
970         return -EOPNOTSUPP;
971 }
972
973 int iwl_mvm_mac_ctxt_add(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
974 {
975         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
976         int ret;
977
978         if (WARN_ONCE(mvmvif->uploaded, "Adding active MAC %pM/%d\n",
979                       vif->addr, ieee80211_vif_type_p2p(vif)))
980                 return -EIO;
981
982         ret = iwl_mvm_mac_ctx_send(mvm, vif, FW_CTXT_ACTION_ADD);
983         if (ret)
984                 return ret;
985
986         mvmvif->uploaded = true;
987         return 0;
988 }
989
990 int iwl_mvm_mac_ctxt_changed(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
991 {
992         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
993
994         if (WARN_ONCE(!mvmvif->uploaded, "Changing inactive MAC %pM/%d\n",
995                       vif->addr, ieee80211_vif_type_p2p(vif)))
996                 return -EIO;
997
998         return iwl_mvm_mac_ctx_send(mvm, vif, FW_CTXT_ACTION_MODIFY);
999 }
1000
1001 int iwl_mvm_mac_ctxt_remove(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1002 {
1003         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1004         struct iwl_mac_ctx_cmd cmd;
1005         int ret;
1006
1007         if (WARN_ONCE(!mvmvif->uploaded, "Removing inactive MAC %pM/%d\n",
1008                       vif->addr, ieee80211_vif_type_p2p(vif)))
1009                 return -EIO;
1010
1011         memset(&cmd, 0, sizeof(cmd));
1012
1013         cmd.id_and_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
1014                                                            mvmvif->color));
1015         cmd.action = cpu_to_le32(FW_CTXT_ACTION_REMOVE);
1016
1017         ret = iwl_mvm_send_cmd_pdu(mvm, MAC_CONTEXT_CMD, CMD_SYNC,
1018                                    sizeof(cmd), &cmd);
1019         if (ret) {
1020                 IWL_ERR(mvm, "Failed to remove MAC context: %d\n", ret);
1021                 return ret;
1022         }
1023
1024         mvmvif->uploaded = false;
1025         return 0;
1026 }
1027
1028 int iwl_mvm_rx_beacon_notif(struct iwl_mvm *mvm,
1029                             struct iwl_rx_cmd_buffer *rxb,
1030                             struct iwl_device_cmd *cmd)
1031 {
1032         struct iwl_rx_packet *pkt = rxb_addr(rxb);
1033         struct iwl_beacon_notif *beacon = (void *)pkt->data;
1034         u16 status __maybe_unused =
1035                 le16_to_cpu(beacon->beacon_notify_hdr.status.status);
1036         u32 rate __maybe_unused =
1037                 le32_to_cpu(beacon->beacon_notify_hdr.initial_rate);
1038
1039         IWL_DEBUG_RX(mvm, "beacon status %#x retries:%d tsf:0x%16llX rate:%d\n",
1040                      status & TX_STATUS_MSK,
1041                      beacon->beacon_notify_hdr.failure_frame,
1042                      le64_to_cpu(beacon->tsf),
1043                      rate);
1044         return 0;
1045 }