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