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