iwlwifi: do not re-configure HT40 after associated
[firefly-linux-kernel-4.4.55.git] / drivers / net / wireless / iwlwifi / iwl-mac80211.c
1 /******************************************************************************
2  *
3  * Copyright(c) 2003 - 2011 Intel Corporation. All rights reserved.
4  *
5  * Portions of this file are derived from the ipw3945 project, as well
6  * as portions of the ieee80211 subsystem header files.
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms of version 2 of the GNU General Public License as
10  * published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
15  * more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
20  *
21  * The full GNU General Public License is included in this distribution in the
22  * file called LICENSE.
23  *
24  * Contact Information:
25  *  Intel Linux Wireless <ilw@linux.intel.com>
26  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
27  *
28  *****************************************************************************/
29 #include <linux/kernel.h>
30 #include <linux/module.h>
31 #include <linux/init.h>
32 #include <linux/slab.h>
33 #include <linux/dma-mapping.h>
34 #include <linux/delay.h>
35 #include <linux/sched.h>
36 #include <linux/skbuff.h>
37 #include <linux/netdevice.h>
38 #include <linux/firmware.h>
39 #include <linux/etherdevice.h>
40 #include <linux/if_arp.h>
41
42 #include <net/mac80211.h>
43
44 #include <asm/div64.h>
45
46 #include "iwl-eeprom.h"
47 #include "iwl-dev.h"
48 #include "iwl-core.h"
49 #include "iwl-io.h"
50 #include "iwl-agn-calib.h"
51 #include "iwl-agn.h"
52 #include "iwl-shared.h"
53 #include "iwl-bus.h"
54 #include "iwl-trans.h"
55
56 /*****************************************************************************
57  *
58  * mac80211 entry point functions
59  *
60  *****************************************************************************/
61
62 static const struct ieee80211_iface_limit iwlagn_sta_ap_limits[] = {
63         {
64                 .max = 1,
65                 .types = BIT(NL80211_IFTYPE_STATION),
66         },
67         {
68                 .max = 1,
69                 .types = BIT(NL80211_IFTYPE_AP),
70         },
71 };
72
73 static const struct ieee80211_iface_limit iwlagn_2sta_limits[] = {
74         {
75                 .max = 2,
76                 .types = BIT(NL80211_IFTYPE_STATION),
77         },
78 };
79
80 static const struct ieee80211_iface_limit iwlagn_p2p_sta_go_limits[] = {
81         {
82                 .max = 1,
83                 .types = BIT(NL80211_IFTYPE_STATION),
84         },
85         {
86                 .max = 1,
87                 .types = BIT(NL80211_IFTYPE_P2P_GO) |
88                          BIT(NL80211_IFTYPE_AP),
89         },
90 };
91
92 static const struct ieee80211_iface_limit iwlagn_p2p_2sta_limits[] = {
93         {
94                 .max = 2,
95                 .types = BIT(NL80211_IFTYPE_STATION),
96         },
97         {
98                 .max = 1,
99                 .types = BIT(NL80211_IFTYPE_P2P_CLIENT),
100         },
101 };
102
103 static const struct ieee80211_iface_combination
104 iwlagn_iface_combinations_dualmode[] = {
105         { .num_different_channels = 1,
106           .max_interfaces = 2,
107           .beacon_int_infra_match = true,
108           .limits = iwlagn_sta_ap_limits,
109           .n_limits = ARRAY_SIZE(iwlagn_sta_ap_limits),
110         },
111         { .num_different_channels = 1,
112           .max_interfaces = 2,
113           .limits = iwlagn_2sta_limits,
114           .n_limits = ARRAY_SIZE(iwlagn_2sta_limits),
115         },
116 };
117
118 static const struct ieee80211_iface_combination
119 iwlagn_iface_combinations_p2p[] = {
120         { .num_different_channels = 1,
121           .max_interfaces = 2,
122           .beacon_int_infra_match = true,
123           .limits = iwlagn_p2p_sta_go_limits,
124           .n_limits = ARRAY_SIZE(iwlagn_p2p_sta_go_limits),
125         },
126         { .num_different_channels = 1,
127           .max_interfaces = 2,
128           .limits = iwlagn_p2p_2sta_limits,
129           .n_limits = ARRAY_SIZE(iwlagn_p2p_2sta_limits),
130         },
131 };
132
133 /*
134  * Not a mac80211 entry point function, but it fits in with all the
135  * other mac80211 functions grouped here.
136  */
137 int iwlagn_mac_setup_register(struct iwl_priv *priv,
138                                   struct iwlagn_ucode_capabilities *capa)
139 {
140         int ret;
141         struct ieee80211_hw *hw = priv->hw;
142         struct iwl_rxon_context *ctx;
143
144         hw->rate_control_algorithm = "iwl-agn-rs";
145
146         /* Tell mac80211 our characteristics */
147         hw->flags = IEEE80211_HW_SIGNAL_DBM |
148                     IEEE80211_HW_AMPDU_AGGREGATION |
149                     IEEE80211_HW_NEED_DTIM_PERIOD |
150                     IEEE80211_HW_SPECTRUM_MGMT |
151                     IEEE80211_HW_REPORTS_TX_ACK_STATUS;
152
153         /*
154          * Including the following line will crash some AP's.  This
155          * workaround removes the stimulus which causes the crash until
156          * the AP software can be fixed.
157         hw->max_tx_aggregation_subframes = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
158          */
159
160         hw->flags |= IEEE80211_HW_SUPPORTS_PS |
161                      IEEE80211_HW_SUPPORTS_DYNAMIC_PS;
162
163         if (priv->cfg->sku & EEPROM_SKU_CAP_11N_ENABLE)
164                 hw->flags |= IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS |
165                              IEEE80211_HW_SUPPORTS_STATIC_SMPS;
166
167         if (capa->flags & IWL_UCODE_TLV_FLAGS_MFP)
168                 hw->flags |= IEEE80211_HW_MFP_CAPABLE;
169
170         hw->sta_data_size = sizeof(struct iwl_station_priv);
171         hw->vif_data_size = sizeof(struct iwl_vif_priv);
172
173         for_each_context(priv, ctx) {
174                 hw->wiphy->interface_modes |= ctx->interface_modes;
175                 hw->wiphy->interface_modes |= ctx->exclusive_interface_modes;
176         }
177
178         BUILD_BUG_ON(NUM_IWL_RXON_CTX != 2);
179
180         if (hw->wiphy->interface_modes & BIT(NL80211_IFTYPE_P2P_CLIENT)) {
181                 hw->wiphy->iface_combinations = iwlagn_iface_combinations_p2p;
182                 hw->wiphy->n_iface_combinations =
183                         ARRAY_SIZE(iwlagn_iface_combinations_p2p);
184         } else if (hw->wiphy->interface_modes & BIT(NL80211_IFTYPE_AP)) {
185                 hw->wiphy->iface_combinations =
186                         iwlagn_iface_combinations_dualmode;
187                 hw->wiphy->n_iface_combinations =
188                         ARRAY_SIZE(iwlagn_iface_combinations_dualmode);
189         }
190
191         hw->wiphy->max_remain_on_channel_duration = 1000;
192
193         hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY |
194                             WIPHY_FLAG_DISABLE_BEACON_HINTS |
195                             WIPHY_FLAG_IBSS_RSN;
196
197         if (trans(priv)->ucode_wowlan.code.len &&
198             device_can_wakeup(bus(priv)->dev)) {
199                 hw->wiphy->wowlan.flags = WIPHY_WOWLAN_MAGIC_PKT |
200                                           WIPHY_WOWLAN_DISCONNECT |
201                                           WIPHY_WOWLAN_EAP_IDENTITY_REQ |
202                                           WIPHY_WOWLAN_RFKILL_RELEASE;
203                 if (!iwlagn_mod_params.sw_crypto)
204                         hw->wiphy->wowlan.flags |=
205                                 WIPHY_WOWLAN_SUPPORTS_GTK_REKEY |
206                                 WIPHY_WOWLAN_GTK_REKEY_FAILURE;
207
208                 hw->wiphy->wowlan.n_patterns = IWLAGN_WOWLAN_MAX_PATTERNS;
209                 hw->wiphy->wowlan.pattern_min_len =
210                                         IWLAGN_WOWLAN_MIN_PATTERN_LEN;
211                 hw->wiphy->wowlan.pattern_max_len =
212                                         IWLAGN_WOWLAN_MAX_PATTERN_LEN;
213         }
214
215         if (iwlagn_mod_params.power_save)
216                 hw->wiphy->flags |= WIPHY_FLAG_PS_ON_BY_DEFAULT;
217         else
218                 hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
219
220         hw->wiphy->max_scan_ssids = PROBE_OPTION_MAX;
221         /* we create the 802.11 header and a zero-length SSID element */
222         hw->wiphy->max_scan_ie_len = capa->max_probe_length - 24 - 2;
223
224         /* Default value; 4 EDCA QOS priorities */
225         hw->queues = 4;
226
227         hw->max_listen_interval = IWL_CONN_MAX_LISTEN_INTERVAL;
228
229         if (priv->bands[IEEE80211_BAND_2GHZ].n_channels)
230                 priv->hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
231                         &priv->bands[IEEE80211_BAND_2GHZ];
232         if (priv->bands[IEEE80211_BAND_5GHZ].n_channels)
233                 priv->hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
234                         &priv->bands[IEEE80211_BAND_5GHZ];
235
236         iwl_leds_init(priv);
237
238         ret = ieee80211_register_hw(priv->hw);
239         if (ret) {
240                 IWL_ERR(priv, "Failed to register hw (error %d)\n", ret);
241                 return ret;
242         }
243         priv->mac80211_registered = 1;
244
245         return 0;
246 }
247
248 static int __iwl_up(struct iwl_priv *priv)
249 {
250         struct iwl_rxon_context *ctx;
251         int ret;
252
253         lockdep_assert_held(&priv->shrd->mutex);
254
255         if (test_bit(STATUS_EXIT_PENDING, &priv->shrd->status)) {
256                 IWL_WARN(priv, "Exit pending; will not bring the NIC up\n");
257                 return -EIO;
258         }
259
260         for_each_context(priv, ctx) {
261                 ret = iwlagn_alloc_bcast_station(priv, ctx);
262                 if (ret) {
263                         iwl_dealloc_bcast_stations(priv);
264                         return ret;
265                 }
266         }
267
268         ret = iwlagn_run_init_ucode(priv);
269         if (ret) {
270                 IWL_ERR(priv, "Failed to run INIT ucode: %d\n", ret);
271                 goto error;
272         }
273
274         ret = iwlagn_load_ucode_wait_alive(priv, IWL_UCODE_REGULAR);
275         if (ret) {
276                 IWL_ERR(priv, "Failed to start RT ucode: %d\n", ret);
277                 goto error;
278         }
279
280         ret = iwl_alive_start(priv);
281         if (ret)
282                 goto error;
283         return 0;
284
285  error:
286         set_bit(STATUS_EXIT_PENDING, &priv->shrd->status);
287         __iwl_down(priv);
288         clear_bit(STATUS_EXIT_PENDING, &priv->shrd->status);
289
290         IWL_ERR(priv, "Unable to initialize device.\n");
291         return ret;
292 }
293
294 static int iwlagn_mac_start(struct ieee80211_hw *hw)
295 {
296         struct iwl_priv *priv = hw->priv;
297         int ret;
298
299         IWL_DEBUG_MAC80211(priv, "enter\n");
300
301         /* we should be verifying the device is ready to be opened */
302         mutex_lock(&priv->shrd->mutex);
303         ret = __iwl_up(priv);
304         mutex_unlock(&priv->shrd->mutex);
305         if (ret)
306                 return ret;
307
308         IWL_DEBUG_INFO(priv, "Start UP work done.\n");
309
310         /* Now we should be done, and the READY bit should be set. */
311         if (WARN_ON(!test_bit(STATUS_READY, &priv->shrd->status)))
312                 ret = -EIO;
313
314         iwlagn_led_enable(priv);
315
316         priv->is_open = 1;
317         IWL_DEBUG_MAC80211(priv, "leave\n");
318         return 0;
319 }
320
321 static void iwlagn_mac_stop(struct ieee80211_hw *hw)
322 {
323         struct iwl_priv *priv = hw->priv;
324
325         IWL_DEBUG_MAC80211(priv, "enter\n");
326
327         if (!priv->is_open)
328                 return;
329
330         priv->is_open = 0;
331
332         iwl_down(priv);
333
334         flush_workqueue(priv->shrd->workqueue);
335
336         /* User space software may expect getting rfkill changes
337          * even if interface is down */
338         iwl_write32(bus(priv), CSR_INT, 0xFFFFFFFF);
339         iwl_enable_rfkill_int(priv);
340
341         IWL_DEBUG_MAC80211(priv, "leave\n");
342 }
343
344 static void iwlagn_mac_set_rekey_data(struct ieee80211_hw *hw,
345                                       struct ieee80211_vif *vif,
346                                       struct cfg80211_gtk_rekey_data *data)
347 {
348         struct iwl_priv *priv = hw->priv;
349
350         if (iwlagn_mod_params.sw_crypto)
351                 return;
352
353         IWL_DEBUG_MAC80211(priv, "enter\n");
354         mutex_lock(&priv->shrd->mutex);
355
356         if (priv->contexts[IWL_RXON_CTX_BSS].vif != vif)
357                 goto out;
358
359         memcpy(priv->kek, data->kek, NL80211_KEK_LEN);
360         memcpy(priv->kck, data->kck, NL80211_KCK_LEN);
361         priv->replay_ctr =
362                 cpu_to_le64(be64_to_cpup((__be64 *)&data->replay_ctr));
363         priv->have_rekey_data = true;
364
365  out:
366         mutex_unlock(&priv->shrd->mutex);
367         IWL_DEBUG_MAC80211(priv, "leave\n");
368 }
369
370 #ifdef CONFIG_PM_SLEEP
371
372 static int iwlagn_mac_suspend(struct ieee80211_hw *hw,
373                               struct cfg80211_wowlan *wowlan)
374 {
375         struct iwl_priv *priv = hw->priv;
376         struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
377         int ret;
378
379         if (WARN_ON(!wowlan))
380                 return -EINVAL;
381
382         IWL_DEBUG_MAC80211(priv, "enter\n");
383         mutex_lock(&priv->shrd->mutex);
384
385         /* Don't attempt WoWLAN when not associated, tear down instead. */
386         if (!ctx->vif || ctx->vif->type != NL80211_IFTYPE_STATION ||
387             !iwl_is_associated_ctx(ctx)) {
388                 ret = 1;
389                 goto out;
390         }
391
392         ret = iwlagn_suspend(priv, hw, wowlan);
393         if (ret)
394                 goto error;
395
396         device_set_wakeup_enable(bus(priv)->dev, true);
397
398         /* Now let the ucode operate on its own */
399         iwl_write32(bus(priv), CSR_UCODE_DRV_GP1_SET,
400                           CSR_UCODE_DRV_GP1_BIT_D3_CFG_COMPLETE);
401
402         goto out;
403
404  error:
405         priv->shrd->wowlan = false;
406         iwlagn_prepare_restart(priv);
407         ieee80211_restart_hw(priv->hw);
408  out:
409         mutex_unlock(&priv->shrd->mutex);
410         IWL_DEBUG_MAC80211(priv, "leave\n");
411
412         return ret;
413 }
414
415 static int iwlagn_mac_resume(struct ieee80211_hw *hw)
416 {
417         struct iwl_priv *priv = hw->priv;
418         struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
419         struct ieee80211_vif *vif;
420         unsigned long flags;
421         u32 base, status = 0xffffffff;
422         int ret = -EIO;
423
424         IWL_DEBUG_MAC80211(priv, "enter\n");
425         mutex_lock(&priv->shrd->mutex);
426
427         iwl_write32(bus(priv), CSR_UCODE_DRV_GP1_CLR,
428                           CSR_UCODE_DRV_GP1_BIT_D3_CFG_COMPLETE);
429
430         base = priv->device_pointers.error_event_table;
431         if (iwlagn_hw_valid_rtc_data_addr(base)) {
432                 spin_lock_irqsave(&bus(priv)->reg_lock, flags);
433                 ret = iwl_grab_nic_access_silent(bus(priv));
434                 if (ret == 0) {
435                         iwl_write32(bus(priv), HBUS_TARG_MEM_RADDR, base);
436                         status = iwl_read32(bus(priv), HBUS_TARG_MEM_RDAT);
437                         iwl_release_nic_access(bus(priv));
438                 }
439                 spin_unlock_irqrestore(&bus(priv)->reg_lock, flags);
440
441 #ifdef CONFIG_IWLWIFI_DEBUGFS
442                 if (ret == 0) {
443                         struct iwl_trans *trans = trans(priv);
444                         if (!priv->wowlan_sram)
445                                 priv->wowlan_sram =
446                                         kzalloc(trans->ucode_wowlan.data.len,
447                                                 GFP_KERNEL);
448
449                         if (priv->wowlan_sram)
450                                 _iwl_read_targ_mem_words(
451                                         bus(priv), 0x800000, priv->wowlan_sram,
452                                         trans->ucode_wowlan.data.len / 4);
453                 }
454 #endif
455         }
456
457         /* we'll clear ctx->vif during iwlagn_prepare_restart() */
458         vif = ctx->vif;
459
460         priv->shrd->wowlan = false;
461
462         device_set_wakeup_enable(bus(priv)->dev, false);
463
464         iwlagn_prepare_restart(priv);
465
466         memset((void *)&ctx->active, 0, sizeof(ctx->active));
467         iwl_connection_init_rx_config(priv, ctx);
468         iwlagn_set_rxon_chain(priv, ctx);
469
470         mutex_unlock(&priv->shrd->mutex);
471         IWL_DEBUG_MAC80211(priv, "leave\n");
472
473         ieee80211_resume_disconnect(vif);
474
475         return 1;
476 }
477
478 #endif
479
480 static void iwlagn_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
481 {
482         struct iwl_priv *priv = hw->priv;
483
484         IWL_DEBUG_MACDUMP(priv, "enter\n");
485
486         IWL_DEBUG_TX(priv, "dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
487                      ieee80211_get_tx_rate(hw, IEEE80211_SKB_CB(skb))->bitrate);
488
489         if (iwlagn_tx_skb(priv, skb))
490                 dev_kfree_skb_any(skb);
491
492         IWL_DEBUG_MACDUMP(priv, "leave\n");
493 }
494
495 static void iwlagn_mac_update_tkip_key(struct ieee80211_hw *hw,
496                                        struct ieee80211_vif *vif,
497                                        struct ieee80211_key_conf *keyconf,
498                                        struct ieee80211_sta *sta,
499                                        u32 iv32, u16 *phase1key)
500 {
501         struct iwl_priv *priv = hw->priv;
502
503         iwl_update_tkip_key(priv, vif, keyconf, sta, iv32, phase1key);
504 }
505
506 static int iwlagn_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
507                               struct ieee80211_vif *vif,
508                               struct ieee80211_sta *sta,
509                               struct ieee80211_key_conf *key)
510 {
511         struct iwl_priv *priv = hw->priv;
512         struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
513         struct iwl_rxon_context *ctx = vif_priv->ctx;
514         int ret;
515         bool is_default_wep_key = false;
516
517         IWL_DEBUG_MAC80211(priv, "enter\n");
518
519         if (iwlagn_mod_params.sw_crypto) {
520                 IWL_DEBUG_MAC80211(priv, "leave - hwcrypto disabled\n");
521                 return -EOPNOTSUPP;
522         }
523
524         /*
525          * We could program these keys into the hardware as well, but we
526          * don't expect much multicast traffic in IBSS and having keys
527          * for more stations is probably more useful.
528          *
529          * Mark key TX-only and return 0.
530          */
531         if (vif->type == NL80211_IFTYPE_ADHOC &&
532             !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
533                 key->hw_key_idx = WEP_INVALID_OFFSET;
534                 return 0;
535         }
536
537         /* If they key was TX-only, accept deletion */
538         if (cmd == DISABLE_KEY && key->hw_key_idx == WEP_INVALID_OFFSET)
539                 return 0;
540
541         mutex_lock(&priv->shrd->mutex);
542         iwl_scan_cancel_timeout(priv, 100);
543
544         BUILD_BUG_ON(WEP_INVALID_OFFSET == IWLAGN_HW_KEY_DEFAULT);
545
546         /*
547          * If we are getting WEP group key and we didn't receive any key mapping
548          * so far, we are in legacy wep mode (group key only), otherwise we are
549          * in 1X mode.
550          * In legacy wep mode, we use another host command to the uCode.
551          */
552         if ((key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
553              key->cipher == WLAN_CIPHER_SUITE_WEP104) && !sta) {
554                 if (cmd == SET_KEY)
555                         is_default_wep_key = !ctx->key_mapping_keys;
556                 else
557                         is_default_wep_key =
558                                 key->hw_key_idx == IWLAGN_HW_KEY_DEFAULT;
559         }
560
561
562         switch (cmd) {
563         case SET_KEY:
564                 if (is_default_wep_key) {
565                         ret = iwl_set_default_wep_key(priv, vif_priv->ctx, key);
566                         break;
567                 }
568                 ret = iwl_set_dynamic_key(priv, vif_priv->ctx, key, sta);
569                 if (ret) {
570                         /*
571                          * can't add key for RX, but we don't need it
572                          * in the device for TX so still return 0
573                          */
574                         ret = 0;
575                         key->hw_key_idx = WEP_INVALID_OFFSET;
576                 }
577
578                 IWL_DEBUG_MAC80211(priv, "enable hwcrypto key\n");
579                 break;
580         case DISABLE_KEY:
581                 if (is_default_wep_key)
582                         ret = iwl_remove_default_wep_key(priv, ctx, key);
583                 else
584                         ret = iwl_remove_dynamic_key(priv, ctx, key, sta);
585
586                 IWL_DEBUG_MAC80211(priv, "disable hwcrypto key\n");
587                 break;
588         default:
589                 ret = -EINVAL;
590         }
591
592         mutex_unlock(&priv->shrd->mutex);
593         IWL_DEBUG_MAC80211(priv, "leave\n");
594
595         return ret;
596 }
597
598 static int iwlagn_mac_ampdu_action(struct ieee80211_hw *hw,
599                                    struct ieee80211_vif *vif,
600                                    enum ieee80211_ampdu_mlme_action action,
601                                    struct ieee80211_sta *sta, u16 tid, u16 *ssn,
602                                    u8 buf_size)
603 {
604         struct iwl_priv *priv = hw->priv;
605         int ret = -EINVAL;
606         struct iwl_station_priv *sta_priv = (void *) sta->drv_priv;
607         struct iwl_rxon_context *ctx =  iwl_rxon_ctx_from_vif(vif);
608
609         IWL_DEBUG_HT(priv, "A-MPDU action on addr %pM tid %d\n",
610                      sta->addr, tid);
611
612         if (!(priv->cfg->sku & EEPROM_SKU_CAP_11N_ENABLE))
613                 return -EACCES;
614
615         IWL_DEBUG_MAC80211(priv, "enter\n");
616         mutex_lock(&priv->shrd->mutex);
617
618         switch (action) {
619         case IEEE80211_AMPDU_RX_START:
620                 IWL_DEBUG_HT(priv, "start Rx\n");
621                 ret = iwl_sta_rx_agg_start(priv, sta, tid, *ssn);
622                 break;
623         case IEEE80211_AMPDU_RX_STOP:
624                 IWL_DEBUG_HT(priv, "stop Rx\n");
625                 ret = iwl_sta_rx_agg_stop(priv, sta, tid);
626                 if (test_bit(STATUS_EXIT_PENDING, &priv->shrd->status))
627                         ret = 0;
628                 break;
629         case IEEE80211_AMPDU_TX_START:
630                 IWL_DEBUG_HT(priv, "start Tx\n");
631                 ret = iwlagn_tx_agg_start(priv, vif, sta, tid, ssn);
632                 break;
633         case IEEE80211_AMPDU_TX_STOP:
634                 IWL_DEBUG_HT(priv, "stop Tx\n");
635                 ret = iwlagn_tx_agg_stop(priv, vif, sta, tid);
636                 if ((ret == 0) && (priv->agg_tids_count > 0)) {
637                         priv->agg_tids_count--;
638                         IWL_DEBUG_HT(priv, "priv->agg_tids_count = %u\n",
639                                      priv->agg_tids_count);
640                 }
641                 if (test_bit(STATUS_EXIT_PENDING, &priv->shrd->status))
642                         ret = 0;
643                 if (!priv->agg_tids_count && priv->cfg->ht_params &&
644                     priv->cfg->ht_params->use_rts_for_aggregation) {
645                         /*
646                          * switch off RTS/CTS if it was previously enabled
647                          */
648                         sta_priv->lq_sta.lq.general_params.flags &=
649                                 ~LINK_QUAL_FLAGS_SET_STA_TLC_RTS_MSK;
650                         iwl_send_lq_cmd(priv, iwl_rxon_ctx_from_vif(vif),
651                                         &sta_priv->lq_sta.lq, CMD_ASYNC, false);
652                 }
653                 break;
654         case IEEE80211_AMPDU_TX_OPERATIONAL:
655                 buf_size = min_t(int, buf_size, LINK_QUAL_AGG_FRAME_LIMIT_DEF);
656
657                 iwl_trans_tx_agg_setup(trans(priv), ctx->ctxid, iwl_sta_id(sta),
658                                 tid, buf_size);
659
660                 /*
661                  * If the limit is 0, then it wasn't initialised yet,
662                  * use the default. We can do that since we take the
663                  * minimum below, and we don't want to go above our
664                  * default due to hardware restrictions.
665                  */
666                 if (sta_priv->max_agg_bufsize == 0)
667                         sta_priv->max_agg_bufsize =
668                                 LINK_QUAL_AGG_FRAME_LIMIT_DEF;
669
670                 /*
671                  * Even though in theory the peer could have different
672                  * aggregation reorder buffer sizes for different sessions,
673                  * our ucode doesn't allow for that and has a global limit
674                  * for each station. Therefore, use the minimum of all the
675                  * aggregation sessions and our default value.
676                  */
677                 sta_priv->max_agg_bufsize =
678                         min(sta_priv->max_agg_bufsize, buf_size);
679
680                 if (priv->cfg->ht_params &&
681                     priv->cfg->ht_params->use_rts_for_aggregation) {
682                         /*
683                          * switch to RTS/CTS if it is the prefer protection
684                          * method for HT traffic
685                          */
686
687                         sta_priv->lq_sta.lq.general_params.flags |=
688                                 LINK_QUAL_FLAGS_SET_STA_TLC_RTS_MSK;
689                 }
690                 priv->agg_tids_count++;
691                 IWL_DEBUG_HT(priv, "priv->agg_tids_count = %u\n",
692                              priv->agg_tids_count);
693
694                 sta_priv->lq_sta.lq.agg_params.agg_frame_cnt_limit =
695                         sta_priv->max_agg_bufsize;
696
697                 iwl_send_lq_cmd(priv, iwl_rxon_ctx_from_vif(vif),
698                                 &sta_priv->lq_sta.lq, CMD_ASYNC, false);
699
700                 IWL_INFO(priv, "Tx aggregation enabled on ra = %pM tid = %d\n",
701                          sta->addr, tid);
702                 ret = 0;
703                 break;
704         }
705         mutex_unlock(&priv->shrd->mutex);
706         IWL_DEBUG_MAC80211(priv, "leave\n");
707         return ret;
708 }
709
710 static int iwlagn_mac_sta_add(struct ieee80211_hw *hw,
711                               struct ieee80211_vif *vif,
712                               struct ieee80211_sta *sta)
713 {
714         struct iwl_priv *priv = hw->priv;
715         struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
716         struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
717         bool is_ap = vif->type == NL80211_IFTYPE_STATION;
718         int ret = 0;
719         u8 sta_id;
720
721         IWL_DEBUG_MAC80211(priv, "received request to add station %pM\n",
722                         sta->addr);
723         mutex_lock(&priv->shrd->mutex);
724         IWL_DEBUG_INFO(priv, "proceeding to add station %pM\n",
725                         sta->addr);
726         sta_priv->sta_id = IWL_INVALID_STATION;
727
728         atomic_set(&sta_priv->pending_frames, 0);
729         if (vif->type == NL80211_IFTYPE_AP)
730                 sta_priv->client = true;
731
732         ret = iwl_add_station_common(priv, vif_priv->ctx, sta->addr,
733                                      is_ap, sta, &sta_id);
734         if (ret) {
735                 IWL_ERR(priv, "Unable to add station %pM (%d)\n",
736                         sta->addr, ret);
737                 /* Should we return success if return code is EEXIST ? */
738                 goto out;
739         }
740
741         sta_priv->sta_id = sta_id;
742
743         /* Initialize rate scaling */
744         IWL_DEBUG_INFO(priv, "Initializing rate scaling for station %pM\n",
745                        sta->addr);
746         iwl_rs_rate_init(priv, sta, sta_id);
747  out:
748         mutex_unlock(&priv->shrd->mutex);
749         IWL_DEBUG_MAC80211(priv, "leave\n");
750
751         return ret;
752 }
753
754 static void iwlagn_mac_channel_switch(struct ieee80211_hw *hw,
755                                 struct ieee80211_channel_switch *ch_switch)
756 {
757         struct iwl_priv *priv = hw->priv;
758         const struct iwl_channel_info *ch_info;
759         struct ieee80211_conf *conf = &hw->conf;
760         struct ieee80211_channel *channel = ch_switch->channel;
761         struct iwl_ht_config *ht_conf = &priv->current_ht_config;
762         /*
763          * MULTI-FIXME
764          * When we add support for multiple interfaces, we need to
765          * revisit this. The channel switch command in the device
766          * only affects the BSS context, but what does that really
767          * mean? And what if we get a CSA on the second interface?
768          * This needs a lot of work.
769          */
770         struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
771         u16 ch;
772
773         IWL_DEBUG_MAC80211(priv, "enter\n");
774
775         mutex_lock(&priv->shrd->mutex);
776
777         if (iwl_is_rfkill(priv->shrd))
778                 goto out;
779
780         if (test_bit(STATUS_EXIT_PENDING, &priv->shrd->status) ||
781             test_bit(STATUS_SCANNING, &priv->shrd->status) ||
782             test_bit(STATUS_CHANNEL_SWITCH_PENDING, &priv->shrd->status))
783                 goto out;
784
785         if (!iwl_is_associated_ctx(ctx))
786                 goto out;
787
788         if (!priv->cfg->lib->set_channel_switch)
789                 goto out;
790
791         ch = channel->hw_value;
792         if (le16_to_cpu(ctx->active.channel) == ch)
793                 goto out;
794
795         ch_info = iwl_get_channel_info(priv, channel->band, ch);
796         if (!is_channel_valid(ch_info)) {
797                 IWL_DEBUG_MAC80211(priv, "invalid channel\n");
798                 goto out;
799         }
800
801         spin_lock_irq(&priv->shrd->lock);
802
803         priv->current_ht_config.smps = conf->smps_mode;
804
805         /* Configure HT40 channels */
806         ctx->ht.enabled = conf_is_ht(conf);
807         if (ctx->ht.enabled)
808                 iwlagn_config_ht40(conf, ctx);
809         else
810                 ctx->ht.is_40mhz = false;
811
812         if ((le16_to_cpu(ctx->staging.channel) != ch))
813                 ctx->staging.flags = 0;
814
815         iwl_set_rxon_channel(priv, channel, ctx);
816         iwl_set_rxon_ht(priv, ht_conf);
817         iwl_set_flags_for_band(priv, ctx, channel->band, ctx->vif);
818
819         spin_unlock_irq(&priv->shrd->lock);
820
821         iwl_set_rate(priv);
822         /*
823          * at this point, staging_rxon has the
824          * configuration for channel switch
825          */
826         set_bit(STATUS_CHANNEL_SWITCH_PENDING, &priv->shrd->status);
827         priv->switch_channel = cpu_to_le16(ch);
828         if (priv->cfg->lib->set_channel_switch(priv, ch_switch)) {
829                 clear_bit(STATUS_CHANNEL_SWITCH_PENDING, &priv->shrd->status);
830                 priv->switch_channel = 0;
831                 ieee80211_chswitch_done(ctx->vif, false);
832         }
833
834 out:
835         mutex_unlock(&priv->shrd->mutex);
836         IWL_DEBUG_MAC80211(priv, "leave\n");
837 }
838
839 static void iwlagn_configure_filter(struct ieee80211_hw *hw,
840                                     unsigned int changed_flags,
841                                     unsigned int *total_flags,
842                                     u64 multicast)
843 {
844         struct iwl_priv *priv = hw->priv;
845         __le32 filter_or = 0, filter_nand = 0;
846         struct iwl_rxon_context *ctx;
847
848 #define CHK(test, flag) do { \
849         if (*total_flags & (test))              \
850                 filter_or |= (flag);            \
851         else                                    \
852                 filter_nand |= (flag);          \
853         } while (0)
854
855         IWL_DEBUG_MAC80211(priv, "Enter: changed: 0x%x, total: 0x%x\n",
856                         changed_flags, *total_flags);
857
858         CHK(FIF_OTHER_BSS | FIF_PROMISC_IN_BSS, RXON_FILTER_PROMISC_MSK);
859         /* Setting _just_ RXON_FILTER_CTL2HOST_MSK causes FH errors */
860         CHK(FIF_CONTROL, RXON_FILTER_CTL2HOST_MSK | RXON_FILTER_PROMISC_MSK);
861         CHK(FIF_BCN_PRBRESP_PROMISC, RXON_FILTER_BCON_AWARE_MSK);
862
863 #undef CHK
864
865         mutex_lock(&priv->shrd->mutex);
866
867         for_each_context(priv, ctx) {
868                 ctx->staging.filter_flags &= ~filter_nand;
869                 ctx->staging.filter_flags |= filter_or;
870
871                 /*
872                  * Not committing directly because hardware can perform a scan,
873                  * but we'll eventually commit the filter flags change anyway.
874                  */
875         }
876
877         mutex_unlock(&priv->shrd->mutex);
878
879         /*
880          * Receiving all multicast frames is always enabled by the
881          * default flags setup in iwl_connection_init_rx_config()
882          * since we currently do not support programming multicast
883          * filters into the device.
884          */
885         *total_flags &= FIF_OTHER_BSS | FIF_ALLMULTI | FIF_PROMISC_IN_BSS |
886                         FIF_BCN_PRBRESP_PROMISC | FIF_CONTROL;
887 }
888
889 static void iwlagn_mac_flush(struct ieee80211_hw *hw, bool drop)
890 {
891         struct iwl_priv *priv = hw->priv;
892
893         mutex_lock(&priv->shrd->mutex);
894         IWL_DEBUG_MAC80211(priv, "enter\n");
895
896         if (test_bit(STATUS_EXIT_PENDING, &priv->shrd->status)) {
897                 IWL_DEBUG_TX(priv, "Aborting flush due to device shutdown\n");
898                 goto done;
899         }
900         if (iwl_is_rfkill(priv->shrd)) {
901                 IWL_DEBUG_TX(priv, "Aborting flush due to RF Kill\n");
902                 goto done;
903         }
904
905         /*
906          * mac80211 will not push any more frames for transmit
907          * until the flush is completed
908          */
909         if (drop) {
910                 IWL_DEBUG_MAC80211(priv, "send flush command\n");
911                 if (iwlagn_txfifo_flush(priv, IWL_DROP_ALL)) {
912                         IWL_ERR(priv, "flush request fail\n");
913                         goto done;
914                 }
915         }
916         IWL_DEBUG_MAC80211(priv, "wait transmit/flush all frames\n");
917         iwl_trans_wait_tx_queue_empty(trans(priv));
918 done:
919         mutex_unlock(&priv->shrd->mutex);
920         IWL_DEBUG_MAC80211(priv, "leave\n");
921 }
922
923 static int iwlagn_mac_remain_on_channel(struct ieee80211_hw *hw,
924                                      struct ieee80211_channel *channel,
925                                      enum nl80211_channel_type channel_type,
926                                      int duration)
927 {
928         struct iwl_priv *priv = hw->priv;
929         struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_PAN];
930         int err = 0;
931
932         if (!(priv->shrd->valid_contexts & BIT(IWL_RXON_CTX_PAN)))
933                 return -EOPNOTSUPP;
934
935         if (!(ctx->interface_modes & BIT(NL80211_IFTYPE_P2P_CLIENT)))
936                 return -EOPNOTSUPP;
937
938         IWL_DEBUG_MAC80211(priv, "enter\n");
939         mutex_lock(&priv->shrd->mutex);
940
941         if (test_bit(STATUS_SCAN_HW, &priv->shrd->status)) {
942                 err = -EBUSY;
943                 goto out;
944         }
945
946         priv->hw_roc_channel = channel;
947         priv->hw_roc_chantype = channel_type;
948         /* convert from ms to TU */
949         priv->hw_roc_duration = DIV_ROUND_UP(1000 * duration, 1024);
950         priv->hw_roc_start_notified = false;
951         cancel_delayed_work(&priv->hw_roc_disable_work);
952
953         if (!ctx->is_active) {
954                 static const struct iwl_qos_info default_qos_data = {
955                         .def_qos_parm = {
956                                 .ac[0] = {
957                                         .cw_min = cpu_to_le16(3),
958                                         .cw_max = cpu_to_le16(7),
959                                         .aifsn = 2,
960                                         .edca_txop = cpu_to_le16(1504),
961                                 },
962                                 .ac[1] = {
963                                         .cw_min = cpu_to_le16(7),
964                                         .cw_max = cpu_to_le16(15),
965                                         .aifsn = 2,
966                                         .edca_txop = cpu_to_le16(3008),
967                                 },
968                                 .ac[2] = {
969                                         .cw_min = cpu_to_le16(15),
970                                         .cw_max = cpu_to_le16(1023),
971                                         .aifsn = 3,
972                                 },
973                                 .ac[3] = {
974                                         .cw_min = cpu_to_le16(15),
975                                         .cw_max = cpu_to_le16(1023),
976                                         .aifsn = 7,
977                                 },
978                         },
979                 };
980
981                 ctx->is_active = true;
982                 ctx->qos_data = default_qos_data;
983                 ctx->staging.dev_type = RXON_DEV_TYPE_P2P;
984                 memcpy(ctx->staging.node_addr,
985                        priv->contexts[IWL_RXON_CTX_BSS].staging.node_addr,
986                        ETH_ALEN);
987                 memcpy(ctx->staging.bssid_addr,
988                        priv->contexts[IWL_RXON_CTX_BSS].staging.node_addr,
989                        ETH_ALEN);
990                 err = iwlagn_commit_rxon(priv, ctx);
991                 if (err)
992                         goto out;
993                 ctx->staging.filter_flags |= RXON_FILTER_ASSOC_MSK |
994                                              RXON_FILTER_PROMISC_MSK |
995                                              RXON_FILTER_CTL2HOST_MSK;
996
997                 err = iwlagn_commit_rxon(priv, ctx);
998                 if (err) {
999                         iwlagn_disable_roc(priv);
1000                         goto out;
1001                 }
1002                 priv->hw_roc_setup = true;
1003         }
1004
1005         err = iwl_scan_initiate(priv, ctx->vif, IWL_SCAN_ROC, channel->band);
1006         if (err)
1007                 iwlagn_disable_roc(priv);
1008
1009  out:
1010         mutex_unlock(&priv->shrd->mutex);
1011         IWL_DEBUG_MAC80211(priv, "leave\n");
1012
1013         return err;
1014 }
1015
1016 static int iwlagn_mac_cancel_remain_on_channel(struct ieee80211_hw *hw)
1017 {
1018         struct iwl_priv *priv = hw->priv;
1019
1020         if (!(priv->shrd->valid_contexts & BIT(IWL_RXON_CTX_PAN)))
1021                 return -EOPNOTSUPP;
1022
1023         IWL_DEBUG_MAC80211(priv, "enter\n");
1024         mutex_lock(&priv->shrd->mutex);
1025         iwl_scan_cancel_timeout(priv, priv->hw_roc_duration);
1026         iwlagn_disable_roc(priv);
1027         mutex_unlock(&priv->shrd->mutex);
1028         IWL_DEBUG_MAC80211(priv, "leave\n");
1029
1030         return 0;
1031 }
1032
1033 static int iwlagn_mac_tx_sync(struct ieee80211_hw *hw,
1034                               struct ieee80211_vif *vif,
1035                               const u8 *bssid,
1036                               enum ieee80211_tx_sync_type type)
1037 {
1038         struct iwl_priv *priv = hw->priv;
1039         struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
1040         struct iwl_rxon_context *ctx = vif_priv->ctx;
1041         int ret;
1042         u8 sta_id;
1043
1044         IWL_DEBUG_MAC80211(priv, "enter\n");
1045         mutex_lock(&priv->shrd->mutex);
1046
1047         if (iwl_is_associated_ctx(ctx)) {
1048                 ret = 0;
1049                 goto out;
1050         }
1051
1052         if (ctx->preauth_bssid || test_bit(STATUS_SCAN_HW,
1053             &priv->shrd->status)) {
1054                 ret = -EBUSY;
1055                 goto out;
1056         }
1057
1058         ret = iwl_add_station_common(priv, ctx, bssid, true, NULL, &sta_id);
1059         if (ret)
1060                 goto out;
1061
1062         if (WARN_ON(sta_id != ctx->ap_sta_id)) {
1063                 ret = -EIO;
1064                 goto out_remove_sta;
1065         }
1066
1067         memcpy(ctx->bssid, bssid, ETH_ALEN);
1068         ctx->preauth_bssid = true;
1069
1070         ret = iwlagn_commit_rxon(priv, ctx);
1071
1072         if (ret == 0)
1073                 goto out;
1074
1075  out_remove_sta:
1076         iwl_remove_station(priv, sta_id, bssid);
1077  out:
1078         mutex_unlock(&priv->shrd->mutex);
1079         IWL_DEBUG_MAC80211(priv, "leave\n");
1080
1081         return ret;
1082 }
1083
1084 static void iwlagn_mac_finish_tx_sync(struct ieee80211_hw *hw,
1085                                    struct ieee80211_vif *vif,
1086                                    const u8 *bssid,
1087                                    enum ieee80211_tx_sync_type type)
1088 {
1089         struct iwl_priv *priv = hw->priv;
1090         struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
1091         struct iwl_rxon_context *ctx = vif_priv->ctx;
1092
1093         IWL_DEBUG_MAC80211(priv, "enter\n");
1094         mutex_lock(&priv->shrd->mutex);
1095
1096         if (iwl_is_associated_ctx(ctx))
1097                 goto out;
1098
1099         iwl_remove_station(priv, ctx->ap_sta_id, bssid);
1100         ctx->preauth_bssid = false;
1101         /* no need to commit */
1102  out:
1103         mutex_unlock(&priv->shrd->mutex);
1104         IWL_DEBUG_MAC80211(priv, "leave\n");
1105 }
1106
1107 static void iwlagn_mac_rssi_callback(struct ieee80211_hw *hw,
1108                            enum ieee80211_rssi_event rssi_event)
1109 {
1110         struct iwl_priv *priv = hw->priv;
1111
1112         IWL_DEBUG_MAC80211(priv, "enter\n");
1113         mutex_lock(&priv->shrd->mutex);
1114
1115         if (priv->cfg->bt_params &&
1116                         priv->cfg->bt_params->advanced_bt_coexist) {
1117                 if (rssi_event == RSSI_EVENT_LOW)
1118                         priv->bt_enable_pspoll = true;
1119                 else if (rssi_event == RSSI_EVENT_HIGH)
1120                         priv->bt_enable_pspoll = false;
1121
1122                 iwlagn_send_advance_bt_config(priv);
1123         } else {
1124                 IWL_DEBUG_MAC80211(priv, "Advanced BT coex disabled,"
1125                                 "ignoring RSSI callback\n");
1126         }
1127
1128         mutex_unlock(&priv->shrd->mutex);
1129         IWL_DEBUG_MAC80211(priv, "leave\n");
1130 }
1131
1132 static int iwlagn_mac_set_tim(struct ieee80211_hw *hw,
1133                            struct ieee80211_sta *sta, bool set)
1134 {
1135         struct iwl_priv *priv = hw->priv;
1136
1137         queue_work(priv->shrd->workqueue, &priv->beacon_update);
1138
1139         return 0;
1140 }
1141
1142 static int iwlagn_mac_conf_tx(struct ieee80211_hw *hw,
1143                     struct ieee80211_vif *vif, u16 queue,
1144                     const struct ieee80211_tx_queue_params *params)
1145 {
1146         struct iwl_priv *priv = hw->priv;
1147         struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
1148         struct iwl_rxon_context *ctx = vif_priv->ctx;
1149         unsigned long flags;
1150         int q;
1151
1152         if (WARN_ON(!ctx))
1153                 return -EINVAL;
1154
1155         IWL_DEBUG_MAC80211(priv, "enter\n");
1156
1157         if (!iwl_is_ready_rf(priv->shrd)) {
1158                 IWL_DEBUG_MAC80211(priv, "leave - RF not ready\n");
1159                 return -EIO;
1160         }
1161
1162         if (queue >= AC_NUM) {
1163                 IWL_DEBUG_MAC80211(priv, "leave - queue >= AC_NUM %d\n", queue);
1164                 return 0;
1165         }
1166
1167         q = AC_NUM - 1 - queue;
1168
1169         spin_lock_irqsave(&priv->shrd->lock, flags);
1170
1171         ctx->qos_data.def_qos_parm.ac[q].cw_min =
1172                 cpu_to_le16(params->cw_min);
1173         ctx->qos_data.def_qos_parm.ac[q].cw_max =
1174                 cpu_to_le16(params->cw_max);
1175         ctx->qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
1176         ctx->qos_data.def_qos_parm.ac[q].edca_txop =
1177                         cpu_to_le16((params->txop * 32));
1178
1179         ctx->qos_data.def_qos_parm.ac[q].reserved1 = 0;
1180
1181         spin_unlock_irqrestore(&priv->shrd->lock, flags);
1182
1183         IWL_DEBUG_MAC80211(priv, "leave\n");
1184         return 0;
1185 }
1186
1187 static int iwlagn_mac_tx_last_beacon(struct ieee80211_hw *hw)
1188 {
1189         struct iwl_priv *priv = hw->priv;
1190
1191         return priv->ibss_manager == IWL_IBSS_MANAGER;
1192 }
1193
1194 static int iwl_set_mode(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
1195 {
1196         iwl_connection_init_rx_config(priv, ctx);
1197
1198         iwlagn_set_rxon_chain(priv, ctx);
1199
1200         return iwlagn_commit_rxon(priv, ctx);
1201 }
1202
1203 static int iwl_setup_interface(struct iwl_priv *priv,
1204                                struct iwl_rxon_context *ctx)
1205 {
1206         struct ieee80211_vif *vif = ctx->vif;
1207         int err;
1208
1209         lockdep_assert_held(&priv->shrd->mutex);
1210
1211         /*
1212          * This variable will be correct only when there's just
1213          * a single context, but all code using it is for hardware
1214          * that supports only one context.
1215          */
1216         priv->iw_mode = vif->type;
1217
1218         ctx->is_active = true;
1219
1220         err = iwl_set_mode(priv, ctx);
1221         if (err) {
1222                 if (!ctx->always_active)
1223                         ctx->is_active = false;
1224                 return err;
1225         }
1226
1227         if (priv->cfg->bt_params && priv->cfg->bt_params->advanced_bt_coexist &&
1228             vif->type == NL80211_IFTYPE_ADHOC) {
1229                 /*
1230                  * pretend to have high BT traffic as long as we
1231                  * are operating in IBSS mode, as this will cause
1232                  * the rate scaling etc. to behave as intended.
1233                  */
1234                 priv->bt_traffic_load = IWL_BT_COEX_TRAFFIC_LOAD_HIGH;
1235         }
1236
1237         return 0;
1238 }
1239
1240 static int iwlagn_mac_add_interface(struct ieee80211_hw *hw,
1241                              struct ieee80211_vif *vif)
1242 {
1243         struct iwl_priv *priv = hw->priv;
1244         struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
1245         struct iwl_rxon_context *tmp, *ctx = NULL;
1246         int err;
1247         enum nl80211_iftype viftype = ieee80211_vif_type_p2p(vif);
1248
1249         IWL_DEBUG_MAC80211(priv, "enter: type %d, addr %pM\n",
1250                            viftype, vif->addr);
1251
1252         cancel_delayed_work_sync(&priv->hw_roc_disable_work);
1253
1254         mutex_lock(&priv->shrd->mutex);
1255
1256         iwlagn_disable_roc(priv);
1257
1258         if (!iwl_is_ready_rf(priv->shrd)) {
1259                 IWL_WARN(priv, "Try to add interface when device not ready\n");
1260                 err = -EINVAL;
1261                 goto out;
1262         }
1263
1264         for_each_context(priv, tmp) {
1265                 u32 possible_modes =
1266                         tmp->interface_modes | tmp->exclusive_interface_modes;
1267
1268                 if (tmp->vif) {
1269                         /* check if this busy context is exclusive */
1270                         if (tmp->exclusive_interface_modes &
1271                                                 BIT(tmp->vif->type)) {
1272                                 err = -EINVAL;
1273                                 goto out;
1274                         }
1275                         continue;
1276                 }
1277
1278                 if (!(possible_modes & BIT(viftype)))
1279                         continue;
1280
1281                 /* have maybe usable context w/o interface */
1282                 ctx = tmp;
1283                 break;
1284         }
1285
1286         if (!ctx) {
1287                 err = -EOPNOTSUPP;
1288                 goto out;
1289         }
1290
1291         vif_priv->ctx = ctx;
1292         ctx->vif = vif;
1293
1294         err = iwl_setup_interface(priv, ctx);
1295         if (!err)
1296                 goto out;
1297
1298         ctx->vif = NULL;
1299         priv->iw_mode = NL80211_IFTYPE_STATION;
1300  out:
1301         mutex_unlock(&priv->shrd->mutex);
1302
1303         IWL_DEBUG_MAC80211(priv, "leave\n");
1304         return err;
1305 }
1306
1307 static void iwl_teardown_interface(struct iwl_priv *priv,
1308                                    struct ieee80211_vif *vif,
1309                                    bool mode_change)
1310 {
1311         struct iwl_rxon_context *ctx = iwl_rxon_ctx_from_vif(vif);
1312
1313         lockdep_assert_held(&priv->shrd->mutex);
1314
1315         if (priv->scan_vif == vif) {
1316                 iwl_scan_cancel_timeout(priv, 200);
1317                 iwl_force_scan_end(priv);
1318         }
1319
1320         if (!mode_change) {
1321                 iwl_set_mode(priv, ctx);
1322                 if (!ctx->always_active)
1323                         ctx->is_active = false;
1324         }
1325
1326         /*
1327          * When removing the IBSS interface, overwrite the
1328          * BT traffic load with the stored one from the last
1329          * notification, if any. If this is a device that
1330          * doesn't implement this, this has no effect since
1331          * both values are the same and zero.
1332          */
1333         if (vif->type == NL80211_IFTYPE_ADHOC)
1334                 priv->bt_traffic_load = priv->last_bt_traffic_load;
1335 }
1336
1337 static void iwlagn_mac_remove_interface(struct ieee80211_hw *hw,
1338                               struct ieee80211_vif *vif)
1339 {
1340         struct iwl_priv *priv = hw->priv;
1341         struct iwl_rxon_context *ctx = iwl_rxon_ctx_from_vif(vif);
1342
1343         IWL_DEBUG_MAC80211(priv, "enter\n");
1344
1345         mutex_lock(&priv->shrd->mutex);
1346
1347         if (WARN_ON(ctx->vif != vif)) {
1348                 struct iwl_rxon_context *tmp;
1349                 IWL_ERR(priv, "ctx->vif = %p, vif = %p\n", ctx->vif, vif);
1350                 for_each_context(priv, tmp)
1351                         IWL_ERR(priv, "\tID = %d:\tctx = %p\tctx->vif = %p\n",
1352                                 tmp->ctxid, tmp, tmp->vif);
1353         }
1354         ctx->vif = NULL;
1355
1356         iwl_teardown_interface(priv, vif, false);
1357
1358         mutex_unlock(&priv->shrd->mutex);
1359
1360         IWL_DEBUG_MAC80211(priv, "leave\n");
1361
1362 }
1363
1364 static int iwlagn_mac_change_interface(struct ieee80211_hw *hw,
1365                                 struct ieee80211_vif *vif,
1366                                 enum nl80211_iftype newtype, bool newp2p)
1367 {
1368         struct iwl_priv *priv = hw->priv;
1369         struct iwl_rxon_context *ctx = iwl_rxon_ctx_from_vif(vif);
1370         struct iwl_rxon_context *bss_ctx = &priv->contexts[IWL_RXON_CTX_BSS];
1371         struct iwl_rxon_context *tmp;
1372         enum nl80211_iftype newviftype = newtype;
1373         u32 interface_modes;
1374         int err;
1375
1376         IWL_DEBUG_MAC80211(priv, "enter\n");
1377
1378         newtype = ieee80211_iftype_p2p(newtype, newp2p);
1379
1380         mutex_lock(&priv->shrd->mutex);
1381
1382         if (!ctx->vif || !iwl_is_ready_rf(priv->shrd)) {
1383                 /*
1384                  * Huh? But wait ... this can maybe happen when
1385                  * we're in the middle of a firmware restart!
1386                  */
1387                 err = -EBUSY;
1388                 goto out;
1389         }
1390
1391         interface_modes = ctx->interface_modes | ctx->exclusive_interface_modes;
1392
1393         if (!(interface_modes & BIT(newtype))) {
1394                 err = -EBUSY;
1395                 goto out;
1396         }
1397
1398         /*
1399          * Refuse a change that should be done by moving from the PAN
1400          * context to the BSS context instead, if the BSS context is
1401          * available and can support the new interface type.
1402          */
1403         if (ctx->ctxid == IWL_RXON_CTX_PAN && !bss_ctx->vif &&
1404             (bss_ctx->interface_modes & BIT(newtype) ||
1405              bss_ctx->exclusive_interface_modes & BIT(newtype))) {
1406                 BUILD_BUG_ON(NUM_IWL_RXON_CTX != 2);
1407                 err = -EBUSY;
1408                 goto out;
1409         }
1410
1411         if (ctx->exclusive_interface_modes & BIT(newtype)) {
1412                 for_each_context(priv, tmp) {
1413                         if (ctx == tmp)
1414                                 continue;
1415
1416                         if (!tmp->vif)
1417                                 continue;
1418
1419                         /*
1420                          * The current mode switch would be exclusive, but
1421                          * another context is active ... refuse the switch.
1422                          */
1423                         err = -EBUSY;
1424                         goto out;
1425                 }
1426         }
1427
1428         /* success */
1429         iwl_teardown_interface(priv, vif, true);
1430         vif->type = newviftype;
1431         vif->p2p = newp2p;
1432         err = iwl_setup_interface(priv, ctx);
1433         WARN_ON(err);
1434         /*
1435          * We've switched internally, but submitting to the
1436          * device may have failed for some reason. Mask this
1437          * error, because otherwise mac80211 will not switch
1438          * (and set the interface type back) and we'll be
1439          * out of sync with it.
1440          */
1441         err = 0;
1442
1443  out:
1444         mutex_unlock(&priv->shrd->mutex);
1445         IWL_DEBUG_MAC80211(priv, "leave\n");
1446
1447         return err;
1448 }
1449
1450 static int iwlagn_mac_hw_scan(struct ieee80211_hw *hw,
1451                     struct ieee80211_vif *vif,
1452                     struct cfg80211_scan_request *req)
1453 {
1454         struct iwl_priv *priv = hw->priv;
1455         int ret;
1456
1457         IWL_DEBUG_MAC80211(priv, "enter\n");
1458
1459         if (req->n_channels == 0)
1460                 return -EINVAL;
1461
1462         mutex_lock(&priv->shrd->mutex);
1463
1464         /*
1465          * If an internal scan is in progress, just set
1466          * up the scan_request as per above.
1467          */
1468         if (priv->scan_type != IWL_SCAN_NORMAL) {
1469                 IWL_DEBUG_SCAN(priv,
1470                                "SCAN request during internal scan - defer\n");
1471                 priv->scan_request = req;
1472                 priv->scan_vif = vif;
1473                 ret = 0;
1474         } else {
1475                 priv->scan_request = req;
1476                 priv->scan_vif = vif;
1477                 /*
1478                  * mac80211 will only ask for one band at a time
1479                  * so using channels[0] here is ok
1480                  */
1481                 ret = iwl_scan_initiate(priv, vif, IWL_SCAN_NORMAL,
1482                                         req->channels[0]->band);
1483                 if (ret) {
1484                         priv->scan_request = NULL;
1485                         priv->scan_vif = NULL;
1486                 }
1487         }
1488
1489         IWL_DEBUG_MAC80211(priv, "leave\n");
1490
1491         mutex_unlock(&priv->shrd->mutex);
1492
1493         return ret;
1494 }
1495
1496 static int iwlagn_mac_sta_remove(struct ieee80211_hw *hw,
1497                        struct ieee80211_vif *vif,
1498                        struct ieee80211_sta *sta)
1499 {
1500         struct iwl_priv *priv = hw->priv;
1501         struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
1502         int ret;
1503
1504         IWL_DEBUG_MAC80211(priv, "enter: received request to remove "
1505                            "station %pM\n", sta->addr);
1506         mutex_lock(&priv->shrd->mutex);
1507         IWL_DEBUG_INFO(priv, "proceeding to remove station %pM\n",
1508                         sta->addr);
1509         ret = iwl_remove_station(priv, sta_priv->sta_id, sta->addr);
1510         if (ret)
1511                 IWL_DEBUG_QUIET_RFKILL(priv, "Error removing station %pM\n",
1512                         sta->addr);
1513         mutex_unlock(&priv->shrd->mutex);
1514         IWL_DEBUG_MAC80211(priv, "leave\n");
1515
1516         return ret;
1517 }
1518
1519 static void iwl_sta_modify_ps_wake(struct iwl_priv *priv, int sta_id)
1520 {
1521         unsigned long flags;
1522
1523         spin_lock_irqsave(&priv->shrd->sta_lock, flags);
1524         priv->stations[sta_id].sta.station_flags &= ~STA_FLG_PWR_SAVE_MSK;
1525         priv->stations[sta_id].sta.station_flags_msk = STA_FLG_PWR_SAVE_MSK;
1526         priv->stations[sta_id].sta.sta.modify_mask = 0;
1527         priv->stations[sta_id].sta.sleep_tx_count = 0;
1528         priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1529         iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
1530         spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
1531
1532 }
1533
1534 static void iwlagn_mac_sta_notify(struct ieee80211_hw *hw,
1535                            struct ieee80211_vif *vif,
1536                            enum sta_notify_cmd cmd,
1537                            struct ieee80211_sta *sta)
1538 {
1539         struct iwl_priv *priv = hw->priv;
1540         struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
1541         int sta_id;
1542
1543         IWL_DEBUG_MAC80211(priv, "enter\n");
1544
1545         switch (cmd) {
1546         case STA_NOTIFY_SLEEP:
1547                 WARN_ON(!sta_priv->client);
1548                 sta_priv->asleep = true;
1549                 if (atomic_read(&sta_priv->pending_frames) > 0)
1550                         ieee80211_sta_block_awake(hw, sta, true);
1551                 break;
1552         case STA_NOTIFY_AWAKE:
1553                 WARN_ON(!sta_priv->client);
1554                 if (!sta_priv->asleep)
1555                         break;
1556                 sta_priv->asleep = false;
1557                 sta_id = iwl_sta_id(sta);
1558                 if (sta_id != IWL_INVALID_STATION)
1559                         iwl_sta_modify_ps_wake(priv, sta_id);
1560                 break;
1561         default:
1562                 break;
1563         }
1564         IWL_DEBUG_MAC80211(priv, "leave\n");
1565 }
1566
1567 struct ieee80211_ops iwlagn_hw_ops = {
1568         .tx = iwlagn_mac_tx,
1569         .start = iwlagn_mac_start,
1570         .stop = iwlagn_mac_stop,
1571 #ifdef CONFIG_PM_SLEEP
1572         .suspend = iwlagn_mac_suspend,
1573         .resume = iwlagn_mac_resume,
1574 #endif
1575         .add_interface = iwlagn_mac_add_interface,
1576         .remove_interface = iwlagn_mac_remove_interface,
1577         .change_interface = iwlagn_mac_change_interface,
1578         .config = iwlagn_mac_config,
1579         .configure_filter = iwlagn_configure_filter,
1580         .set_key = iwlagn_mac_set_key,
1581         .update_tkip_key = iwlagn_mac_update_tkip_key,
1582         .set_rekey_data = iwlagn_mac_set_rekey_data,
1583         .conf_tx = iwlagn_mac_conf_tx,
1584         .bss_info_changed = iwlagn_bss_info_changed,
1585         .ampdu_action = iwlagn_mac_ampdu_action,
1586         .hw_scan = iwlagn_mac_hw_scan,
1587         .sta_notify = iwlagn_mac_sta_notify,
1588         .sta_add = iwlagn_mac_sta_add,
1589         .sta_remove = iwlagn_mac_sta_remove,
1590         .channel_switch = iwlagn_mac_channel_switch,
1591         .flush = iwlagn_mac_flush,
1592         .tx_last_beacon = iwlagn_mac_tx_last_beacon,
1593         .remain_on_channel = iwlagn_mac_remain_on_channel,
1594         .cancel_remain_on_channel = iwlagn_mac_cancel_remain_on_channel,
1595         .rssi_callback = iwlagn_mac_rssi_callback,
1596         CFG80211_TESTMODE_CMD(iwlagn_mac_testmode_cmd)
1597         CFG80211_TESTMODE_DUMP(iwlagn_mac_testmode_dump)
1598         .tx_sync = iwlagn_mac_tx_sync,
1599         .finish_tx_sync = iwlagn_mac_finish_tx_sync,
1600         .set_tim = iwlagn_mac_set_tim,
1601 };
1602
1603 /* This function both allocates and initializes hw and priv. */
1604 struct ieee80211_hw *iwl_alloc_all(void)
1605 {
1606         struct iwl_priv *priv;
1607         /* mac80211 allocates memory for this device instance, including
1608          *   space for this driver's private structure */
1609         struct ieee80211_hw *hw;
1610
1611         hw = ieee80211_alloc_hw(sizeof(struct iwl_priv), &iwlagn_hw_ops);
1612         if (!hw)
1613                 goto out;
1614
1615         priv = hw->priv;
1616         priv->hw = hw;
1617
1618 out:
1619         return hw;
1620 }