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