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