blob: f0a2c957d503b6f89881f11b0ce210ef0eaa6767 [file] [log] [blame]
Wey-Yi Guy73356132011-11-10 06:55:11 -08001/******************************************************************************
2 *
Johannes Berg128e63e2013-01-21 21:39:26 +01003 * Copyright(c) 2003 - 2013 Intel Corporation. All rights reserved.
Wey-Yi Guy73356132011-11-10 06:55:11 -08004 *
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>
Wey-Yi Guy73356132011-11-10 06:55:11 -080038#include <linux/etherdevice.h>
39#include <linux/if_arp.h>
40
Johannes Berg53e11162012-05-16 23:59:03 +020041#include <net/ieee80211_radiotap.h>
Wey-Yi Guy73356132011-11-10 06:55:11 -080042#include <net/mac80211.h>
43
44#include <asm/div64.h>
45
Wey-Yi Guy73356132011-11-10 06:55:11 -080046#include "iwl-io.h"
Wey-Yi Guy73356132011-11-10 06:55:11 -080047#include "iwl-trans.h"
Emmanuel Grumbachd0f76d62012-02-09 16:08:15 +020048#include "iwl-op-mode.h"
Johannes Berg65de7e82012-04-17 07:36:30 -070049#include "iwl-modparams.h"
Wey-Yi Guy73356132011-11-10 06:55:11 -080050
Johannes Berg1023fdc2012-05-15 12:16:34 +020051#include "dev.h"
52#include "calib.h"
53#include "agn.h"
54
Wey-Yi Guy73356132011-11-10 06:55:11 -080055/*****************************************************************************
56 *
57 * mac80211 entry point functions
58 *
59 *****************************************************************************/
60
61static 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
72static const struct ieee80211_iface_limit iwlagn_2sta_limits[] = {
73 {
74 .max = 2,
75 .types = BIT(NL80211_IFTYPE_STATION),
76 },
77};
78
Wey-Yi Guy73356132011-11-10 06:55:11 -080079static const struct ieee80211_iface_combination
80iwlagn_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
Wey-Yi Guy73356132011-11-10 06:55:11 -080094/*
95 * Not a mac80211 entry point function, but it fits in with all the
96 * other mac80211 functions grouped here.
97 */
98int iwlagn_mac_setup_register(struct iwl_priv *priv,
Johannes Berg0692fe42012-03-06 13:30:37 -080099 const struct iwl_ucode_capabilities *capa)
Wey-Yi Guy73356132011-11-10 06:55:11 -0800100{
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 |
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +0200110 IEEE80211_HW_NEED_DTIM_BEFORE_ASSOC |
Wey-Yi Guy73356132011-11-10 06:55:11 -0800111 IEEE80211_HW_SPECTRUM_MGMT |
Johannes Berg14791772012-04-17 07:39:03 -0700112 IEEE80211_HW_REPORTS_TX_ACK_STATUS |
113 IEEE80211_HW_QUEUE_CONTROL |
114 IEEE80211_HW_SUPPORTS_PS |
115 IEEE80211_HW_SUPPORTS_DYNAMIC_PS |
Johannes Bergf1e3e052013-02-06 23:57:57 +0100116 IEEE80211_HW_WANT_MONITOR_VIF;
Johannes Berg14791772012-04-17 07:39:03 -0700117
118 hw->offchannel_tx_hw_queue = IWL_AUX_QUEUE;
Johannes Berg53e11162012-05-16 23:59:03 +0200119 hw->radiotap_mcs_details |= IEEE80211_RADIOTAP_MCS_HAVE_FMT;
Wey-Yi Guy73356132011-11-10 06:55:11 -0800120
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
Eytan Lifshitzb7998c82012-12-01 20:59:49 +0200128 if (priv->nvm_data->sku_cap_11n_enable)
Wey-Yi Guy73356132011-11-10 06:55:11 -0800129 hw->flags |= IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS |
130 IEEE80211_HW_SUPPORTS_STATIC_SMPS;
131
Johannes Berg686681b2012-11-15 11:35:57 +0100132 /*
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)
Wey-Yi Guy73356132011-11-10 06:55:11 -0800139 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
Johannes Bergda4f87f2013-07-01 13:48:56 +0200151 if (hw->wiphy->interface_modes & BIT(NL80211_IFTYPE_AP)) {
Wey-Yi Guy73356132011-11-10 06:55:11 -0800152 hw->wiphy->iface_combinations =
153 iwlagn_iface_combinations_dualmode;
154 hw->wiphy->n_iface_combinations =
155 ARRAY_SIZE(iwlagn_iface_combinations_dualmode);
156 }
157
Wey-Yi Guy73356132011-11-10 06:55:11 -0800158 hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY |
159 WIPHY_FLAG_DISABLE_BEACON_HINTS |
160 WIPHY_FLAG_IBSS_RSN;
161
Johannes Bergfcb6ff52012-06-04 13:43:11 +0200162#ifdef CONFIG_PM_SLEEP
David Spinadel6dfa8d02012-03-10 13:00:14 -0800163 if (priv->fw->img[IWL_UCODE_WOWLAN].sec[0].len &&
Johannes Bergddaf5a52013-01-08 11:25:44 +0100164 priv->trans->ops->d3_suspend &&
165 priv->trans->ops->d3_resume &&
Emmanuel Grumbach68e8dfd2012-04-18 07:28:17 -0700166 device_can_wakeup(priv->trans->dev)) {
Johannes Berg964dc9e2013-06-03 17:25:34 +0200167 priv->wowlan_support.flags = WIPHY_WOWLAN_MAGIC_PKT |
168 WIPHY_WOWLAN_DISCONNECT |
169 WIPHY_WOWLAN_EAP_IDENTITY_REQ |
170 WIPHY_WOWLAN_RFKILL_RELEASE;
Johannes Berg65de7e82012-04-17 07:36:30 -0700171 if (!iwlwifi_mod_params.sw_crypto)
Johannes Berg964dc9e2013-06-03 17:25:34 +0200172 priv->wowlan_support.flags |=
Wey-Yi Guy73356132011-11-10 06:55:11 -0800173 WIPHY_WOWLAN_SUPPORTS_GTK_REKEY |
174 WIPHY_WOWLAN_GTK_REKEY_FAILURE;
175
Johannes Berg964dc9e2013-06-03 17:25:34 +0200176 priv->wowlan_support.n_patterns = IWLAGN_WOWLAN_MAX_PATTERNS;
177 priv->wowlan_support.pattern_min_len =
Wey-Yi Guy73356132011-11-10 06:55:11 -0800178 IWLAGN_WOWLAN_MIN_PATTERN_LEN;
Johannes Berg964dc9e2013-06-03 17:25:34 +0200179 priv->wowlan_support.pattern_max_len =
Wey-Yi Guy73356132011-11-10 06:55:11 -0800180 IWLAGN_WOWLAN_MAX_PATTERN_LEN;
Johannes Berg964dc9e2013-06-03 17:25:34 +0200181 hw->wiphy->wowlan = &priv->wowlan_support;
Wey-Yi Guy73356132011-11-10 06:55:11 -0800182 }
Johannes Bergfcb6ff52012-06-04 13:43:11 +0200183#endif
Wey-Yi Guy73356132011-11-10 06:55:11 -0800184
Johannes Berg65de7e82012-04-17 07:36:30 -0700185 if (iwlwifi_mod_params.power_save)
Wey-Yi Guy73356132011-11-10 06:55:11 -0800186 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;
David Spinadel4f9bfbb2012-05-11 10:53:16 +0200191 /* 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;
Wey-Yi Guy73356132011-11-10 06:55:11 -0800193
Johannes Berg14791772012-04-17 07:39:03 -0700194 /*
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;
Wey-Yi Guy73356132011-11-10 06:55:11 -0800199
200 hw->max_listen_interval = IWL_CONN_MAX_LISTEN_INTERVAL;
201
Eytan Lifshitzb7998c82012-12-01 20:59:49 +0200202 if (priv->nvm_data->bands[IEEE80211_BAND_2GHZ].n_channels)
Wey-Yi Guy73356132011-11-10 06:55:11 -0800203 priv->hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
Eytan Lifshitzb7998c82012-12-01 20:59:49 +0200204 &priv->nvm_data->bands[IEEE80211_BAND_2GHZ];
205 if (priv->nvm_data->bands[IEEE80211_BAND_5GHZ].n_channels)
Wey-Yi Guy73356132011-11-10 06:55:11 -0800206 priv->hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
Eytan Lifshitzb7998c82012-12-01 20:59:49 +0200207 &priv->nvm_data->bands[IEEE80211_BAND_5GHZ];
Wey-Yi Guy73356132011-11-10 06:55:11 -0800208
Emmanuel Grumbach68e8dfd2012-04-18 07:28:17 -0700209 hw->wiphy->hw_version = priv->trans->hw_id;
Wey-Yi Guy0ba958e2011-12-10 11:33:52 -0800210
Wey-Yi Guy73356132011-11-10 06:55:11 -0800211 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);
Johannes Berg0e1fa7e2012-06-05 09:38:35 +0200216 iwl_leds_exit(priv);
Wey-Yi Guy73356132011-11-10 06:55:11 -0800217 return ret;
218 }
219 priv->mac80211_registered = 1;
220
221 return 0;
222}
223
Don Fry09af1402011-12-09 10:07:38 -0800224void 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
Wey-Yi Guy73356132011-11-10 06:55:11 -0800233static int __iwl_up(struct iwl_priv *priv)
234{
235 struct iwl_rxon_context *ctx;
236 int ret;
237
Johannes Bergb1eea292012-03-06 13:30:42 -0800238 lockdep_assert_held(&priv->mutex);
Wey-Yi Guy73356132011-11-10 06:55:11 -0800239
Don Fry83626402012-03-07 09:52:37 -0800240 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
Wey-Yi Guy73356132011-11-10 06:55:11 -0800241 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
Johannes Berge1991882012-03-06 13:30:36 -0800253 ret = iwl_run_init_ucode(priv);
Wey-Yi Guy73356132011-11-10 06:55:11 -0800254 if (ret) {
255 IWL_ERR(priv, "Failed to run INIT ucode: %d\n", ret);
256 goto error;
257 }
258
Johannes Berge1991882012-03-06 13:30:36 -0800259 ret = iwl_load_ucode_wait_alive(priv, IWL_UCODE_REGULAR);
Wey-Yi Guy73356132011-11-10 06:55:11 -0800260 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:
Don Fry83626402012-03-07 09:52:37 -0800271 set_bit(STATUS_EXIT_PENDING, &priv->status);
Emmanuel Grumbach78e5a462012-02-17 10:34:53 -0800272 iwl_down(priv);
Don Fry83626402012-03-07 09:52:37 -0800273 clear_bit(STATUS_EXIT_PENDING, &priv->status);
Wey-Yi Guy73356132011-11-10 06:55:11 -0800274
275 IWL_ERR(priv, "Unable to initialize device.\n");
276 return ret;
277}
278
279static int iwlagn_mac_start(struct ieee80211_hw *hw)
280{
Emmanuel Grumbachd0f76d62012-02-09 16:08:15 +0200281 struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
Wey-Yi Guy73356132011-11-10 06:55:11 -0800282 int ret;
283
284 IWL_DEBUG_MAC80211(priv, "enter\n");
285
286 /* we should be verifying the device is ready to be opened */
Johannes Bergb1eea292012-03-06 13:30:42 -0800287 mutex_lock(&priv->mutex);
Wey-Yi Guy73356132011-11-10 06:55:11 -0800288 ret = __iwl_up(priv);
Johannes Bergb1eea292012-03-06 13:30:42 -0800289 mutex_unlock(&priv->mutex);
Wey-Yi Guy73356132011-11-10 06:55:11 -0800290 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. */
Don Fry83626402012-03-07 09:52:37 -0800296 if (WARN_ON(!test_bit(STATUS_READY, &priv->status)))
Wey-Yi Guy73356132011-11-10 06:55:11 -0800297 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
Johannes Bergaca86262012-05-14 09:08:50 +0200306static void iwlagn_mac_stop(struct ieee80211_hw *hw)
Wey-Yi Guy73356132011-11-10 06:55:11 -0800307{
Emmanuel Grumbachd0f76d62012-02-09 16:08:15 +0200308 struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
Wey-Yi Guy73356132011-11-10 06:55:11 -0800309
310 IWL_DEBUG_MAC80211(priv, "enter\n");
311
312 if (!priv->is_open)
313 return;
314
315 priv->is_open = 0;
316
Johannes Bergb1eea292012-03-06 13:30:42 -0800317 mutex_lock(&priv->mutex);
Wey-Yi Guy73356132011-11-10 06:55:11 -0800318 iwl_down(priv);
Johannes Bergb1eea292012-03-06 13:30:42 -0800319 mutex_unlock(&priv->mutex);
Emmanuel Grumbach78e5a462012-02-17 10:34:53 -0800320
321 iwl_cancel_deferred_work(priv);
Wey-Yi Guy73356132011-11-10 06:55:11 -0800322
Johannes Berg1ee158d2012-02-17 10:07:44 -0800323 flush_workqueue(priv->workqueue);
Wey-Yi Guy73356132011-11-10 06:55:11 -0800324
325 /* User space software may expect getting rfkill changes
Emmanuel Grumbach1df06bd2012-01-09 16:35:08 +0200326 * even if interface is down, trans->down will leave the RF
327 * kill interrupt enabled
328 */
Emmanuel Grumbach218733c2012-03-31 08:28:38 -0700329 iwl_trans_stop_hw(priv->trans, false);
Wey-Yi Guy73356132011-11-10 06:55:11 -0800330
331 IWL_DEBUG_MAC80211(priv, "leave\n");
332}
333
Johannes Bergaca86262012-05-14 09:08:50 +0200334static void iwlagn_mac_set_rekey_data(struct ieee80211_hw *hw,
335 struct ieee80211_vif *vif,
336 struct cfg80211_gtk_rekey_data *data)
Wey-Yi Guy73356132011-11-10 06:55:11 -0800337{
Emmanuel Grumbachd0f76d62012-02-09 16:08:15 +0200338 struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
Wey-Yi Guy73356132011-11-10 06:55:11 -0800339
Johannes Berg65de7e82012-04-17 07:36:30 -0700340 if (iwlwifi_mod_params.sw_crypto)
Wey-Yi Guy73356132011-11-10 06:55:11 -0800341 return;
342
343 IWL_DEBUG_MAC80211(priv, "enter\n");
Johannes Bergb1eea292012-03-06 13:30:42 -0800344 mutex_lock(&priv->mutex);
Wey-Yi Guy73356132011-11-10 06:55:11 -0800345
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:
Johannes Bergb1eea292012-03-06 13:30:42 -0800356 mutex_unlock(&priv->mutex);
Wey-Yi Guy73356132011-11-10 06:55:11 -0800357 IWL_DEBUG_MAC80211(priv, "leave\n");
358}
359
360#ifdef CONFIG_PM_SLEEP
Wey-Yi Guy73356132011-11-10 06:55:11 -0800361
Johannes Bergaca86262012-05-14 09:08:50 +0200362static int iwlagn_mac_suspend(struct ieee80211_hw *hw,
363 struct cfg80211_wowlan *wowlan)
Wey-Yi Guy73356132011-11-10 06:55:11 -0800364{
Emmanuel Grumbachd0f76d62012-02-09 16:08:15 +0200365 struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
Wey-Yi Guy73356132011-11-10 06:55:11 -0800366 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
Wey-Yi Guy023ca582011-11-10 06:55:17 -0800367 int ret;
Wey-Yi Guy73356132011-11-10 06:55:11 -0800368
369 if (WARN_ON(!wowlan))
370 return -EINVAL;
371
372 IWL_DEBUG_MAC80211(priv, "enter\n");
Johannes Bergb1eea292012-03-06 13:30:42 -0800373 mutex_lock(&priv->mutex);
Wey-Yi Guy73356132011-11-10 06:55:11 -0800374
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
Johannes Bergea886a62012-03-07 09:52:15 -0800382 ret = iwlagn_suspend(priv, wowlan);
Wey-Yi Guy73356132011-11-10 06:55:11 -0800383 if (ret)
384 goto error;
385
Johannes Berg8ade6282013-05-17 12:01:26 +0200386 /* 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
Johannes Bergdebff612013-05-14 13:53:45 +0200390 iwl_trans_d3_suspend(priv->trans, false);
Wey-Yi Guy73356132011-11-10 06:55:11 -0800391
392 goto out;
393
394 error:
Johannes Berg15b86bf2012-03-05 11:24:36 -0800395 priv->wowlan = false;
Wey-Yi Guy73356132011-11-10 06:55:11 -0800396 iwlagn_prepare_restart(priv);
397 ieee80211_restart_hw(priv->hw);
398 out:
Johannes Bergb1eea292012-03-06 13:30:42 -0800399 mutex_unlock(&priv->mutex);
Wey-Yi Guy73356132011-11-10 06:55:11 -0800400 IWL_DEBUG_MAC80211(priv, "leave\n");
401
402 return ret;
403}
404
Johannes Berg5718d272013-01-08 11:29:12 +0100405struct iwl_resume_data {
406 struct iwl_priv *priv;
407 struct iwlagn_wowlan_status *cmd;
408 bool valid;
409};
410
411static 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
Wey-Yi Guy73356132011-11-10 06:55:11 -0800428static int iwlagn_mac_resume(struct ieee80211_hw *hw)
429{
Emmanuel Grumbachd0f76d62012-02-09 16:08:15 +0200430 struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
Wey-Yi Guy73356132011-11-10 06:55:11 -0800431 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
432 struct ieee80211_vif *vif;
Johannes Berg5718d272013-01-08 11:29:12 +0100433 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
Wey-Yi Guy73356132011-11-10 06:55:11 -0800457
458 IWL_DEBUG_MAC80211(priv, "enter\n");
Johannes Bergb1eea292012-03-06 13:30:42 -0800459 mutex_lock(&priv->mutex);
Wey-Yi Guy73356132011-11-10 06:55:11 -0800460
Wey-Yi Guy73356132011-11-10 06:55:11 -0800461 /* we'll clear ctx->vif during iwlagn_prepare_restart() */
462 vif = ctx->vif;
463
Johannes Bergdebff612013-05-14 13:53:45 +0200464 ret = iwl_trans_d3_resume(priv->trans, &d3_status, false);
Johannes Berg5718d272013-01-08 11:29:12 +0100465 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
Johannes Berg8ade6282013-05-17 12:01:26 +0200473 /* 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
Johannes Berg5718d272013-01-08 11:29:12 +0100477 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
Johannes Berg15b86bf2012-03-05 11:24:36 -0800558 priv->wowlan = false;
Wey-Yi Guy73356132011-11-10 06:55:11 -0800559
Wey-Yi Guy73356132011-11-10 06:55:11 -0800560 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
Johannes Berg5718d272013-01-08 11:29:12 +0100566 out_unlock:
Johannes Bergb1eea292012-03-06 13:30:42 -0800567 mutex_unlock(&priv->mutex);
Wey-Yi Guy73356132011-11-10 06:55:11 -0800568 IWL_DEBUG_MAC80211(priv, "leave\n");
569
570 ieee80211_resume_disconnect(vif);
571
572 return 1;
573}
574
Johannes Berg76ed2ed2012-05-16 22:54:25 +0200575static 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}
Wey-Yi Guy73356132011-11-10 06:55:11 -0800581#endif
582
Thomas Huehn36323f82012-07-23 21:33:42 +0200583static void iwlagn_mac_tx(struct ieee80211_hw *hw,
584 struct ieee80211_tx_control *control,
585 struct sk_buff *skb)
Wey-Yi Guy73356132011-11-10 06:55:11 -0800586{
Emmanuel Grumbachd0f76d62012-02-09 16:08:15 +0200587 struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
Wey-Yi Guy73356132011-11-10 06:55:11 -0800588
Thomas Huehn36323f82012-07-23 21:33:42 +0200589 if (iwlagn_tx_skb(priv, control->sta, skb))
Johannes Berg31df3bb2012-10-31 22:07:39 +0100590 ieee80211_free_txskb(hw, skb);
Wey-Yi Guy73356132011-11-10 06:55:11 -0800591}
592
Johannes Bergaca86262012-05-14 09:08:50 +0200593static 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)
Wey-Yi Guy73356132011-11-10 06:55:11 -0800598{
Emmanuel Grumbachd0f76d62012-02-09 16:08:15 +0200599 struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
Wey-Yi Guy73356132011-11-10 06:55:11 -0800600
601 iwl_update_tkip_key(priv, vif, keyconf, sta, iv32, phase1key);
602}
603
Johannes Bergaca86262012-05-14 09:08:50 +0200604static 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)
Wey-Yi Guy73356132011-11-10 06:55:11 -0800608{
Emmanuel Grumbachd0f76d62012-02-09 16:08:15 +0200609 struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
Wey-Yi Guy73356132011-11-10 06:55:11 -0800610 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
Johannes Berg65de7e82012-04-17 07:36:30 -0700617 if (iwlwifi_mod_params.sw_crypto) {
Wey-Yi Guy73356132011-11-10 06:55:11 -0800618 IWL_DEBUG_MAC80211(priv, "leave - hwcrypto disabled\n");
619 return -EOPNOTSUPP;
620 }
621
Johannes Berga7e12c82011-11-25 03:20:09 -0800622 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
Wey-Yi Guy73356132011-11-10 06:55:11 -0800633 /*
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
Johannes Bergb1eea292012-03-06 13:30:42 -0800650 mutex_lock(&priv->mutex);
Wey-Yi Guy73356132011-11-10 06:55:11 -0800651 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
Johannes Bergb1eea292012-03-06 13:30:42 -0800701 mutex_unlock(&priv->mutex);
Wey-Yi Guy73356132011-11-10 06:55:11 -0800702 IWL_DEBUG_MAC80211(priv, "leave\n");
703
704 return ret;
705}
706
Johannes Bergaca86262012-05-14 09:08:50 +0200707static 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)
Wey-Yi Guy73356132011-11-10 06:55:11 -0800712{
Emmanuel Grumbachd0f76d62012-02-09 16:08:15 +0200713 struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
Wey-Yi Guy73356132011-11-10 06:55:11 -0800714 int ret = -EINVAL;
715 struct iwl_station_priv *sta_priv = (void *) sta->drv_priv;
Wey-Yi Guy73356132011-11-10 06:55:11 -0800716
717 IWL_DEBUG_HT(priv, "A-MPDU action on addr %pM tid %d\n",
718 sta->addr, tid);
719
Eytan Lifshitzb7998c82012-12-01 20:59:49 +0200720 if (!(priv->nvm_data->sku_cap_11n_enable))
Wey-Yi Guy73356132011-11-10 06:55:11 -0800721 return -EACCES;
722
723 IWL_DEBUG_MAC80211(priv, "enter\n");
Johannes Bergb1eea292012-03-06 13:30:42 -0800724 mutex_lock(&priv->mutex);
Wey-Yi Guy73356132011-11-10 06:55:11 -0800725
726 switch (action) {
727 case IEEE80211_AMPDU_RX_START:
Johannes Berg65de7e82012-04-17 07:36:30 -0700728 if (iwlwifi_mod_params.disable_11n & IWL_DISABLE_HT_RXAGG)
Johannes Berg74289942011-12-13 00:07:40 -0800729 break;
Wey-Yi Guy73356132011-11-10 06:55:11 -0800730 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);
Wey-Yi Guy73356132011-11-10 06:55:11 -0800736 break;
737 case IEEE80211_AMPDU_TX_START:
Emmanuel Grumbach4beaf6c2012-05-29 11:29:10 +0300738 if (!priv->trans->ops->txq_enable)
Johannes Berg9eae88f2012-03-15 13:26:52 -0700739 break;
Johannes Berg65de7e82012-04-17 07:36:30 -0700740 if (iwlwifi_mod_params.disable_11n & IWL_DISABLE_HT_TXAGG)
Johannes Berg74289942011-12-13 00:07:40 -0800741 break;
Wey-Yi Guy73356132011-11-10 06:55:11 -0800742 IWL_DEBUG_HT(priv, "start Tx\n");
743 ret = iwlagn_tx_agg_start(priv, vif, sta, tid, ssn);
744 break;
Johannes Berg18b559d2012-07-18 13:51:25 +0200745 case IEEE80211_AMPDU_TX_STOP_FLUSH:
746 case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
Emmanuel Grumbach2d055af2013-04-07 10:13:44 +0300747 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:
Wey-Yi Guy73356132011-11-10 06:55:11 -0800751 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 }
Johannes Bergb9ad70d2012-03-06 13:30:55 -0800758 if (!priv->agg_tids_count &&
Johannes Berg9e295112012-04-09 17:46:55 -0700759 priv->hw_params.use_rts_for_aggregation) {
Wey-Yi Guy73356132011-11-10 06:55:11 -0800760 /*
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:
Emmanuel Grumbach822e8b22011-11-21 13:25:31 +0200770 ret = iwlagn_tx_agg_oper(priv, vif, sta, tid, buf_size);
Wey-Yi Guy73356132011-11-10 06:55:11 -0800771 break;
772 }
Johannes Bergb1eea292012-03-06 13:30:42 -0800773 mutex_unlock(&priv->mutex);
Wey-Yi Guy73356132011-11-10 06:55:11 -0800774 IWL_DEBUG_MAC80211(priv, "leave\n");
775 return ret;
776}
777
778static int iwlagn_mac_sta_add(struct ieee80211_hw *hw,
779 struct ieee80211_vif *vif,
780 struct ieee80211_sta *sta)
781{
Emmanuel Grumbachd0f76d62012-02-09 16:08:15 +0200782 struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
Wey-Yi Guy73356132011-11-10 06:55:11 -0800783 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;
Johannes Bergfb5fe5b2012-03-05 11:24:29 -0800786 int ret;
Wey-Yi Guy73356132011-11-10 06:55:11 -0800787 u8 sta_id;
788
Wey-Yi Guy73356132011-11-10 06:55:11 -0800789 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 ? */
Johannes Bergfb5fe5b2012-03-05 11:24:29 -0800803 return ret;
Wey-Yi Guy73356132011-11-10 06:55:11 -0800804 }
805
806 sta_priv->sta_id = sta_id;
807
Johannes Bergfb5fe5b2012-03-05 11:24:29 -0800808 return 0;
809}
810
811static 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
Johannes Berg97420512012-03-07 09:52:42 -0800819 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 }
Johannes Bergfb5fe5b2012-03-05 11:24:29 -0800835 return ret;
836}
837
Johannes Bergaca86262012-05-14 09:08:50 +0200838static 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)
Johannes Bergfb5fe5b2012-03-05 11:24:29 -0800843{
844 struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
Johannes Bergab0bd5b2012-03-05 11:24:47 -0800845 struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
Johannes Bergfb5fe5b2012-03-05 11:24:29 -0800846 enum {
Johannes Bergab0bd5b2012-03-05 11:24:47 -0800847 NONE, ADD, REMOVE, HT_RATE_INIT, ADD_RATE_INIT,
Johannes Bergfb5fe5b2012-03-05 11:24:29 -0800848 } 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
Johannes Bergb1eea292012-03-06 13:30:42 -0800854 mutex_lock(&priv->mutex);
Johannes Bergfb5fe5b2012-03-05 11:24:29 -0800855 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)
Johannes Bergab0bd5b2012-03-05 11:24:47 -0800864 op = HT_RATE_INIT;
Johannes Bergfb5fe5b2012-03-05 11:24:29 -0800865 } 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);
Johannes Bergeac9ac62012-06-25 09:36:41 +0200877 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);
Johannes Bergfb5fe5b2012-03-05 11:24:29 -0800889 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;
Johannes Bergfb5fe5b2012-03-05 11:24:29 -0800897 /* 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;
Johannes Bergab0bd5b2012-03-05 11:24:47 -0800904 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;
Johannes Bergfb5fe5b2012-03-05 11:24:29 -0800915 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 */
Don Fry83626402012-03-07 09:52:37 -0800924 if (iwl_is_rfkill(priv))
Johannes Bergfb5fe5b2012-03-05 11:24:29 -0800925 ret = 0;
926
Johannes Bergb1eea292012-03-06 13:30:42 -0800927 mutex_unlock(&priv->mutex);
Wey-Yi Guy73356132011-11-10 06:55:11 -0800928 IWL_DEBUG_MAC80211(priv, "leave\n");
929
930 return ret;
931}
932
Johannes Bergaca86262012-05-14 09:08:50 +0200933static void iwlagn_mac_channel_switch(struct ieee80211_hw *hw,
934 struct ieee80211_channel_switch *ch_switch)
Wey-Yi Guy73356132011-11-10 06:55:11 -0800935{
Emmanuel Grumbachd0f76d62012-02-09 16:08:15 +0200936 struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
Wey-Yi Guy73356132011-11-10 06:55:11 -0800937 struct ieee80211_conf *conf = &hw->conf;
Johannes Berg85220d72013-03-25 18:29:27 +0100938 struct ieee80211_channel *channel = ch_switch->chandef.chan;
Wey-Yi Guy73356132011-11-10 06:55:11 -0800939 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
Johannes Bergb1eea292012-03-06 13:30:42 -0800953 mutex_lock(&priv->mutex);
Wey-Yi Guy73356132011-11-10 06:55:11 -0800954
Don Fry83626402012-03-07 09:52:37 -0800955 if (iwl_is_rfkill(priv))
Wey-Yi Guy73356132011-11-10 06:55:11 -0800956 goto out;
957
Don Fry83626402012-03-07 09:52:37 -0800958 if (test_bit(STATUS_EXIT_PENDING, &priv->status) ||
959 test_bit(STATUS_SCANNING, &priv->status) ||
960 test_bit(STATUS_CHANNEL_SWITCH_PENDING, &priv->status))
Wey-Yi Guy73356132011-11-10 06:55:11 -0800961 goto out;
962
963 if (!iwl_is_associated_ctx(ctx))
964 goto out;
965
Johannes Berge9676692012-04-10 14:10:28 -0700966 if (!priv->lib->set_channel_switch)
Wey-Yi Guy73356132011-11-10 06:55:11 -0800967 goto out;
968
969 ch = channel->hw_value;
970 if (le16_to_cpu(ctx->active.channel) == ch)
971 goto out;
972
Wey-Yi Guy73356132011-11-10 06:55:11 -0800973 priv->current_ht_config.smps = conf->smps_mode;
974
975 /* Configure HT40 channels */
Johannes Berg85220d72013-03-25 18:29:27 +0100976 switch (cfg80211_get_chandef_type(&ch_switch->chandef)) {
977 case NL80211_CHAN_NO_HT:
978 case NL80211_CHAN_HT20:
Wey-Yi Guy73356132011-11-10 06:55:11 -0800979 ctx->ht.is_40mhz = false;
Johannes Berg85220d72013-03-25 18:29:27 +0100980 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 }
Wey-Yi Guy73356132011-11-10 06:55:11 -0800991
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
Wey-Yi Guy73356132011-11-10 06:55:11 -0800999 /*
1000 * at this point, staging_rxon has the
1001 * configuration for channel switch
1002 */
Don Fry83626402012-03-07 09:52:37 -08001003 set_bit(STATUS_CHANNEL_SWITCH_PENDING, &priv->status);
Wey-Yi Guy73356132011-11-10 06:55:11 -08001004 priv->switch_channel = cpu_to_le16(ch);
Johannes Berge9676692012-04-10 14:10:28 -07001005 if (priv->lib->set_channel_switch(priv, ch_switch)) {
Don Fry83626402012-03-07 09:52:37 -08001006 clear_bit(STATUS_CHANNEL_SWITCH_PENDING, &priv->status);
Wey-Yi Guy73356132011-11-10 06:55:11 -08001007 priv->switch_channel = 0;
1008 ieee80211_chswitch_done(ctx->vif, false);
1009 }
1010
1011out:
Johannes Bergb1eea292012-03-06 13:30:42 -08001012 mutex_unlock(&priv->mutex);
Wey-Yi Guy73356132011-11-10 06:55:11 -08001013 IWL_DEBUG_MAC80211(priv, "leave\n");
1014}
1015
Meenakshi Venkataramanbedec3a2012-03-13 17:47:23 -07001016void 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 ieee80211_chswitch_done(ctx->vif, is_success);
1029}
1030
Johannes Bergaca86262012-05-14 09:08:50 +02001031static void iwlagn_configure_filter(struct ieee80211_hw *hw,
1032 unsigned int changed_flags,
1033 unsigned int *total_flags,
1034 u64 multicast)
Wey-Yi Guy73356132011-11-10 06:55:11 -08001035{
Emmanuel Grumbachd0f76d62012-02-09 16:08:15 +02001036 struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
Wey-Yi Guy73356132011-11-10 06:55:11 -08001037 __le32 filter_or = 0, filter_nand = 0;
1038 struct iwl_rxon_context *ctx;
1039
1040#define CHK(test, flag) do { \
1041 if (*total_flags & (test)) \
1042 filter_or |= (flag); \
1043 else \
1044 filter_nand |= (flag); \
1045 } while (0)
1046
1047 IWL_DEBUG_MAC80211(priv, "Enter: changed: 0x%x, total: 0x%x\n",
1048 changed_flags, *total_flags);
1049
1050 CHK(FIF_OTHER_BSS | FIF_PROMISC_IN_BSS, RXON_FILTER_PROMISC_MSK);
1051 /* Setting _just_ RXON_FILTER_CTL2HOST_MSK causes FH errors */
1052 CHK(FIF_CONTROL, RXON_FILTER_CTL2HOST_MSK | RXON_FILTER_PROMISC_MSK);
1053 CHK(FIF_BCN_PRBRESP_PROMISC, RXON_FILTER_BCON_AWARE_MSK);
1054
1055#undef CHK
1056
Johannes Bergb1eea292012-03-06 13:30:42 -08001057 mutex_lock(&priv->mutex);
Wey-Yi Guy73356132011-11-10 06:55:11 -08001058
1059 for_each_context(priv, ctx) {
1060 ctx->staging.filter_flags &= ~filter_nand;
1061 ctx->staging.filter_flags |= filter_or;
1062
1063 /*
1064 * Not committing directly because hardware can perform a scan,
1065 * but we'll eventually commit the filter flags change anyway.
1066 */
1067 }
1068
Johannes Bergb1eea292012-03-06 13:30:42 -08001069 mutex_unlock(&priv->mutex);
Wey-Yi Guy73356132011-11-10 06:55:11 -08001070
1071 /*
1072 * Receiving all multicast frames is always enabled by the
1073 * default flags setup in iwl_connection_init_rx_config()
1074 * since we currently do not support programming multicast
1075 * filters into the device.
1076 */
1077 *total_flags &= FIF_OTHER_BSS | FIF_ALLMULTI | FIF_PROMISC_IN_BSS |
1078 FIF_BCN_PRBRESP_PROMISC | FIF_CONTROL;
1079}
1080
Johannes Berg39ecc012013-02-13 12:11:00 +01001081static void iwlagn_mac_flush(struct ieee80211_hw *hw, u32 queues, bool drop)
Wey-Yi Guy73356132011-11-10 06:55:11 -08001082{
Emmanuel Grumbachd0f76d62012-02-09 16:08:15 +02001083 struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
Wey-Yi Guy73356132011-11-10 06:55:11 -08001084
Johannes Bergb1eea292012-03-06 13:30:42 -08001085 mutex_lock(&priv->mutex);
Wey-Yi Guy73356132011-11-10 06:55:11 -08001086 IWL_DEBUG_MAC80211(priv, "enter\n");
1087
Don Fry83626402012-03-07 09:52:37 -08001088 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
Wey-Yi Guy73356132011-11-10 06:55:11 -08001089 IWL_DEBUG_TX(priv, "Aborting flush due to device shutdown\n");
1090 goto done;
1091 }
Don Fry83626402012-03-07 09:52:37 -08001092 if (iwl_is_rfkill(priv)) {
Wey-Yi Guy73356132011-11-10 06:55:11 -08001093 IWL_DEBUG_TX(priv, "Aborting flush due to RF Kill\n");
1094 goto done;
1095 }
1096
1097 /*
1098 * mac80211 will not push any more frames for transmit
1099 * until the flush is completed
1100 */
1101 if (drop) {
1102 IWL_DEBUG_MAC80211(priv, "send flush command\n");
Emmanuel Grumbach2d055af2013-04-07 10:13:44 +03001103 if (iwlagn_txfifo_flush(priv, 0)) {
Wey-Yi Guy73356132011-11-10 06:55:11 -08001104 IWL_ERR(priv, "flush request fail\n");
1105 goto done;
1106 }
1107 }
1108 IWL_DEBUG_MAC80211(priv, "wait transmit/flush all frames\n");
Emmanuel Grumbach68e8dfd2012-04-18 07:28:17 -07001109 iwl_trans_wait_tx_queue_empty(priv->trans);
Wey-Yi Guy73356132011-11-10 06:55:11 -08001110done:
Johannes Bergb1eea292012-03-06 13:30:42 -08001111 mutex_unlock(&priv->mutex);
Wey-Yi Guy73356132011-11-10 06:55:11 -08001112 IWL_DEBUG_MAC80211(priv, "leave\n");
1113}
1114
Johannes Bergaca86262012-05-14 09:08:50 +02001115static void iwlagn_mac_rssi_callback(struct ieee80211_hw *hw,
Emmanuel Grumbach887da912013-01-20 17:32:41 +02001116 struct ieee80211_vif *vif,
Johannes Bergaca86262012-05-14 09:08:50 +02001117 enum ieee80211_rssi_event rssi_event)
Wey-Yi Guy73356132011-11-10 06:55:11 -08001118{
Emmanuel Grumbachd0f76d62012-02-09 16:08:15 +02001119 struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
Wey-Yi Guy73356132011-11-10 06:55:11 -08001120
1121 IWL_DEBUG_MAC80211(priv, "enter\n");
Johannes Bergb1eea292012-03-06 13:30:42 -08001122 mutex_lock(&priv->mutex);
Wey-Yi Guy73356132011-11-10 06:55:11 -08001123
Johannes Berg0d8877a2013-05-17 10:36:29 +02001124 if (priv->lib->bt_params &&
1125 priv->lib->bt_params->advanced_bt_coexist) {
Wey-Yi Guy73356132011-11-10 06:55:11 -08001126 if (rssi_event == RSSI_EVENT_LOW)
1127 priv->bt_enable_pspoll = true;
1128 else if (rssi_event == RSSI_EVENT_HIGH)
1129 priv->bt_enable_pspoll = false;
1130
1131 iwlagn_send_advance_bt_config(priv);
1132 } else {
1133 IWL_DEBUG_MAC80211(priv, "Advanced BT coex disabled,"
1134 "ignoring RSSI callback\n");
1135 }
1136
Johannes Bergb1eea292012-03-06 13:30:42 -08001137 mutex_unlock(&priv->mutex);
Wey-Yi Guy73356132011-11-10 06:55:11 -08001138 IWL_DEBUG_MAC80211(priv, "leave\n");
1139}
1140
Johannes Bergaca86262012-05-14 09:08:50 +02001141static int iwlagn_mac_set_tim(struct ieee80211_hw *hw,
1142 struct ieee80211_sta *sta, bool set)
Wey-Yi Guy73356132011-11-10 06:55:11 -08001143{
Emmanuel Grumbachd0f76d62012-02-09 16:08:15 +02001144 struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
Wey-Yi Guy73356132011-11-10 06:55:11 -08001145
Johannes Berg1ee158d2012-02-17 10:07:44 -08001146 queue_work(priv->workqueue, &priv->beacon_update);
Wey-Yi Guy73356132011-11-10 06:55:11 -08001147
1148 return 0;
1149}
1150
Johannes Bergaca86262012-05-14 09:08:50 +02001151static int iwlagn_mac_conf_tx(struct ieee80211_hw *hw,
1152 struct ieee80211_vif *vif, u16 queue,
1153 const struct ieee80211_tx_queue_params *params)
Wey-Yi Guy0b7a4c72011-11-10 06:55:14 -08001154{
Emmanuel Grumbachd0f76d62012-02-09 16:08:15 +02001155 struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
Wey-Yi Guy0b7a4c72011-11-10 06:55:14 -08001156 struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
1157 struct iwl_rxon_context *ctx = vif_priv->ctx;
Wey-Yi Guy0b7a4c72011-11-10 06:55:14 -08001158 int q;
1159
1160 if (WARN_ON(!ctx))
1161 return -EINVAL;
1162
1163 IWL_DEBUG_MAC80211(priv, "enter\n");
1164
Don Fry83626402012-03-07 09:52:37 -08001165 if (!iwl_is_ready_rf(priv)) {
Wey-Yi Guy0b7a4c72011-11-10 06:55:14 -08001166 IWL_DEBUG_MAC80211(priv, "leave - RF not ready\n");
1167 return -EIO;
1168 }
1169
1170 if (queue >= AC_NUM) {
1171 IWL_DEBUG_MAC80211(priv, "leave - queue >= AC_NUM %d\n", queue);
1172 return 0;
1173 }
1174
1175 q = AC_NUM - 1 - queue;
1176
Johannes Bergb1eea292012-03-06 13:30:42 -08001177 mutex_lock(&priv->mutex);
Wey-Yi Guy0b7a4c72011-11-10 06:55:14 -08001178
1179 ctx->qos_data.def_qos_parm.ac[q].cw_min =
1180 cpu_to_le16(params->cw_min);
1181 ctx->qos_data.def_qos_parm.ac[q].cw_max =
1182 cpu_to_le16(params->cw_max);
1183 ctx->qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
1184 ctx->qos_data.def_qos_parm.ac[q].edca_txop =
1185 cpu_to_le16((params->txop * 32));
1186
1187 ctx->qos_data.def_qos_parm.ac[q].reserved1 = 0;
1188
Johannes Bergb1eea292012-03-06 13:30:42 -08001189 mutex_unlock(&priv->mutex);
Wey-Yi Guy0b7a4c72011-11-10 06:55:14 -08001190
1191 IWL_DEBUG_MAC80211(priv, "leave\n");
1192 return 0;
1193}
1194
Johannes Bergaca86262012-05-14 09:08:50 +02001195static int iwlagn_mac_tx_last_beacon(struct ieee80211_hw *hw)
Wey-Yi Guy0b7a4c72011-11-10 06:55:14 -08001196{
Emmanuel Grumbachd0f76d62012-02-09 16:08:15 +02001197 struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
Wey-Yi Guy0b7a4c72011-11-10 06:55:14 -08001198
1199 return priv->ibss_manager == IWL_IBSS_MANAGER;
1200}
1201
1202static int iwl_set_mode(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
1203{
1204 iwl_connection_init_rx_config(priv, ctx);
1205
1206 iwlagn_set_rxon_chain(priv, ctx);
1207
1208 return iwlagn_commit_rxon(priv, ctx);
1209}
1210
Johannes Bergaca86262012-05-14 09:08:50 +02001211static int iwl_setup_interface(struct iwl_priv *priv,
1212 struct iwl_rxon_context *ctx)
Wey-Yi Guy0b7a4c72011-11-10 06:55:14 -08001213{
1214 struct ieee80211_vif *vif = ctx->vif;
Johannes Berg14791772012-04-17 07:39:03 -07001215 int err, ac;
Wey-Yi Guy0b7a4c72011-11-10 06:55:14 -08001216
Johannes Bergb1eea292012-03-06 13:30:42 -08001217 lockdep_assert_held(&priv->mutex);
Wey-Yi Guy0b7a4c72011-11-10 06:55:14 -08001218
1219 /*
1220 * This variable will be correct only when there's just
1221 * a single context, but all code using it is for hardware
1222 * that supports only one context.
1223 */
1224 priv->iw_mode = vif->type;
1225
1226 ctx->is_active = true;
1227
1228 err = iwl_set_mode(priv, ctx);
1229 if (err) {
1230 if (!ctx->always_active)
1231 ctx->is_active = false;
1232 return err;
1233 }
1234
Johannes Berg0d8877a2013-05-17 10:36:29 +02001235 if (priv->lib->bt_params && priv->lib->bt_params->advanced_bt_coexist &&
Wey-Yi Guy0b7a4c72011-11-10 06:55:14 -08001236 vif->type == NL80211_IFTYPE_ADHOC) {
1237 /*
1238 * pretend to have high BT traffic as long as we
1239 * are operating in IBSS mode, as this will cause
1240 * the rate scaling etc. to behave as intended.
1241 */
1242 priv->bt_traffic_load = IWL_BT_COEX_TRAFFIC_LOAD_HIGH;
1243 }
1244
Johannes Berg14791772012-04-17 07:39:03 -07001245 /* set up queue mappings */
1246 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
1247 vif->hw_queue[ac] = ctx->ac_to_queue[ac];
1248
1249 if (vif->type == NL80211_IFTYPE_AP)
1250 vif->cab_queue = ctx->mcast_queue;
1251 else
1252 vif->cab_queue = IEEE80211_INVAL_HW_QUEUE;
1253
Wey-Yi Guy0b7a4c72011-11-10 06:55:14 -08001254 return 0;
1255}
1256
1257static int iwlagn_mac_add_interface(struct ieee80211_hw *hw,
Johannes Berg14791772012-04-17 07:39:03 -07001258 struct ieee80211_vif *vif)
Wey-Yi Guy0b7a4c72011-11-10 06:55:14 -08001259{
Emmanuel Grumbachd0f76d62012-02-09 16:08:15 +02001260 struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
Wey-Yi Guy0b7a4c72011-11-10 06:55:14 -08001261 struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
1262 struct iwl_rxon_context *tmp, *ctx = NULL;
1263 int err;
1264 enum nl80211_iftype viftype = ieee80211_vif_type_p2p(vif);
Stanislaw Gruszka8db4c7e2012-04-18 08:01:15 -07001265 bool reset = false;
Wey-Yi Guy0b7a4c72011-11-10 06:55:14 -08001266
1267 IWL_DEBUG_MAC80211(priv, "enter: type %d, addr %pM\n",
1268 viftype, vif->addr);
1269
Johannes Bergb1eea292012-03-06 13:30:42 -08001270 mutex_lock(&priv->mutex);
Wey-Yi Guy0b7a4c72011-11-10 06:55:14 -08001271
Don Fry83626402012-03-07 09:52:37 -08001272 if (!iwl_is_ready_rf(priv)) {
Wey-Yi Guy0b7a4c72011-11-10 06:55:14 -08001273 IWL_WARN(priv, "Try to add interface when device not ready\n");
1274 err = -EINVAL;
1275 goto out;
1276 }
1277
1278 for_each_context(priv, tmp) {
1279 u32 possible_modes =
1280 tmp->interface_modes | tmp->exclusive_interface_modes;
1281
1282 if (tmp->vif) {
Stanislaw Gruszka8db4c7e2012-04-18 08:01:15 -07001283 /* On reset we need to add the same interface again */
1284 if (tmp->vif == vif) {
1285 reset = true;
1286 ctx = tmp;
1287 break;
1288 }
1289
Wey-Yi Guy0b7a4c72011-11-10 06:55:14 -08001290 /* check if this busy context is exclusive */
1291 if (tmp->exclusive_interface_modes &
1292 BIT(tmp->vif->type)) {
1293 err = -EINVAL;
1294 goto out;
1295 }
1296 continue;
1297 }
1298
1299 if (!(possible_modes & BIT(viftype)))
1300 continue;
1301
1302 /* have maybe usable context w/o interface */
1303 ctx = tmp;
1304 break;
1305 }
1306
1307 if (!ctx) {
1308 err = -EOPNOTSUPP;
1309 goto out;
1310 }
1311
1312 vif_priv->ctx = ctx;
1313 ctx->vif = vif;
1314
Johannes Berg04baaa22012-11-13 21:28:44 +01001315 /*
1316 * In SNIFFER device type, the firmware reports the FCS to
1317 * the host, rather than snipping it off. Unfortunately,
1318 * mac80211 doesn't (yet) provide a per-packet flag for
1319 * this, so that we have to set the hardware flag based
1320 * on the interfaces added. As the monitor interface can
1321 * only be present by itself, and will be removed before
1322 * other interfaces are added, this is safe.
1323 */
1324 if (vif->type == NL80211_IFTYPE_MONITOR)
1325 priv->hw->flags |= IEEE80211_HW_RX_INCLUDES_FCS;
1326 else
1327 priv->hw->flags &= ~IEEE80211_HW_RX_INCLUDES_FCS;
1328
Wey-Yi Guy0b7a4c72011-11-10 06:55:14 -08001329 err = iwl_setup_interface(priv, ctx);
Stanislaw Gruszka8db4c7e2012-04-18 08:01:15 -07001330 if (!err || reset)
Wey-Yi Guy0b7a4c72011-11-10 06:55:14 -08001331 goto out;
1332
1333 ctx->vif = NULL;
1334 priv->iw_mode = NL80211_IFTYPE_STATION;
1335 out:
Johannes Bergb1eea292012-03-06 13:30:42 -08001336 mutex_unlock(&priv->mutex);
Wey-Yi Guy0b7a4c72011-11-10 06:55:14 -08001337
1338 IWL_DEBUG_MAC80211(priv, "leave\n");
1339 return err;
1340}
1341
Johannes Bergaca86262012-05-14 09:08:50 +02001342static void iwl_teardown_interface(struct iwl_priv *priv,
1343 struct ieee80211_vif *vif,
1344 bool mode_change)
Wey-Yi Guy0b7a4c72011-11-10 06:55:14 -08001345{
1346 struct iwl_rxon_context *ctx = iwl_rxon_ctx_from_vif(vif);
1347
Johannes Bergb1eea292012-03-06 13:30:42 -08001348 lockdep_assert_held(&priv->mutex);
Wey-Yi Guy0b7a4c72011-11-10 06:55:14 -08001349
1350 if (priv->scan_vif == vif) {
1351 iwl_scan_cancel_timeout(priv, 200);
1352 iwl_force_scan_end(priv);
1353 }
1354
1355 if (!mode_change) {
1356 iwl_set_mode(priv, ctx);
1357 if (!ctx->always_active)
1358 ctx->is_active = false;
1359 }
1360
1361 /*
1362 * When removing the IBSS interface, overwrite the
1363 * BT traffic load with the stored one from the last
1364 * notification, if any. If this is a device that
1365 * doesn't implement this, this has no effect since
1366 * both values are the same and zero.
1367 */
1368 if (vif->type == NL80211_IFTYPE_ADHOC)
1369 priv->bt_traffic_load = priv->last_bt_traffic_load;
1370}
1371
1372static void iwlagn_mac_remove_interface(struct ieee80211_hw *hw,
1373 struct ieee80211_vif *vif)
1374{
Emmanuel Grumbachd0f76d62012-02-09 16:08:15 +02001375 struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
Wey-Yi Guy0b7a4c72011-11-10 06:55:14 -08001376 struct iwl_rxon_context *ctx = iwl_rxon_ctx_from_vif(vif);
1377
1378 IWL_DEBUG_MAC80211(priv, "enter\n");
1379
Johannes Bergb1eea292012-03-06 13:30:42 -08001380 mutex_lock(&priv->mutex);
Wey-Yi Guy0b7a4c72011-11-10 06:55:14 -08001381
1382 if (WARN_ON(ctx->vif != vif)) {
1383 struct iwl_rxon_context *tmp;
1384 IWL_ERR(priv, "ctx->vif = %p, vif = %p\n", ctx->vif, vif);
1385 for_each_context(priv, tmp)
1386 IWL_ERR(priv, "\tID = %d:\tctx = %p\tctx->vif = %p\n",
1387 tmp->ctxid, tmp, tmp->vif);
1388 }
1389 ctx->vif = NULL;
1390
1391 iwl_teardown_interface(priv, vif, false);
1392
Johannes Bergb1eea292012-03-06 13:30:42 -08001393 mutex_unlock(&priv->mutex);
Wey-Yi Guy0b7a4c72011-11-10 06:55:14 -08001394
1395 IWL_DEBUG_MAC80211(priv, "leave\n");
1396
1397}
1398
1399static int iwlagn_mac_change_interface(struct ieee80211_hw *hw,
Johannes Berge75dac92012-06-20 11:47:42 +02001400 struct ieee80211_vif *vif,
1401 enum nl80211_iftype newtype, bool newp2p)
Wey-Yi Guy0b7a4c72011-11-10 06:55:14 -08001402{
Emmanuel Grumbachd0f76d62012-02-09 16:08:15 +02001403 struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
Johannes Berge75dac92012-06-20 11:47:42 +02001404 struct iwl_rxon_context *ctx, *tmp;
Wey-Yi Guy0b7a4c72011-11-10 06:55:14 -08001405 enum nl80211_iftype newviftype = newtype;
1406 u32 interface_modes;
1407 int err;
1408
1409 IWL_DEBUG_MAC80211(priv, "enter\n");
1410
1411 newtype = ieee80211_iftype_p2p(newtype, newp2p);
1412
Johannes Bergb1eea292012-03-06 13:30:42 -08001413 mutex_lock(&priv->mutex);
Wey-Yi Guy0b7a4c72011-11-10 06:55:14 -08001414
Johannes Berge75dac92012-06-20 11:47:42 +02001415 ctx = iwl_rxon_ctx_from_vif(vif);
1416
1417 /*
1418 * To simplify this code, only support changes on the
1419 * BSS context. The PAN context is usually reassigned
1420 * by creating/removing P2P interfaces anyway.
1421 */
1422 if (ctx->ctxid != IWL_RXON_CTX_BSS) {
1423 err = -EBUSY;
1424 goto out;
1425 }
1426
Don Fry83626402012-03-07 09:52:37 -08001427 if (!ctx->vif || !iwl_is_ready_rf(priv)) {
Wey-Yi Guy0b7a4c72011-11-10 06:55:14 -08001428 /*
1429 * Huh? But wait ... this can maybe happen when
1430 * we're in the middle of a firmware restart!
1431 */
1432 err = -EBUSY;
1433 goto out;
1434 }
1435
Johannes Berge75dac92012-06-20 11:47:42 +02001436 /* Check if the switch is supported in the same context */
Wey-Yi Guy0b7a4c72011-11-10 06:55:14 -08001437 interface_modes = ctx->interface_modes | ctx->exclusive_interface_modes;
Wey-Yi Guy0b7a4c72011-11-10 06:55:14 -08001438 if (!(interface_modes & BIT(newtype))) {
1439 err = -EBUSY;
1440 goto out;
1441 }
1442
Wey-Yi Guy0b7a4c72011-11-10 06:55:14 -08001443 if (ctx->exclusive_interface_modes & BIT(newtype)) {
1444 for_each_context(priv, tmp) {
1445 if (ctx == tmp)
1446 continue;
1447
Johannes Berge75dac92012-06-20 11:47:42 +02001448 if (!tmp->is_active)
Wey-Yi Guy0b7a4c72011-11-10 06:55:14 -08001449 continue;
1450
1451 /*
1452 * The current mode switch would be exclusive, but
1453 * another context is active ... refuse the switch.
1454 */
1455 err = -EBUSY;
1456 goto out;
1457 }
1458 }
1459
1460 /* success */
1461 iwl_teardown_interface(priv, vif, true);
1462 vif->type = newviftype;
1463 vif->p2p = newp2p;
1464 err = iwl_setup_interface(priv, ctx);
1465 WARN_ON(err);
1466 /*
1467 * We've switched internally, but submitting to the
1468 * device may have failed for some reason. Mask this
1469 * error, because otherwise mac80211 will not switch
1470 * (and set the interface type back) and we'll be
1471 * out of sync with it.
1472 */
1473 err = 0;
1474
1475 out:
Johannes Bergb1eea292012-03-06 13:30:42 -08001476 mutex_unlock(&priv->mutex);
Wey-Yi Guy0b7a4c72011-11-10 06:55:14 -08001477 IWL_DEBUG_MAC80211(priv, "leave\n");
1478
1479 return err;
1480}
1481
Johannes Bergaca86262012-05-14 09:08:50 +02001482static int iwlagn_mac_hw_scan(struct ieee80211_hw *hw,
1483 struct ieee80211_vif *vif,
1484 struct cfg80211_scan_request *req)
Wey-Yi Guyba4c5312011-11-10 06:55:15 -08001485{
Emmanuel Grumbachd0f76d62012-02-09 16:08:15 +02001486 struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
Wey-Yi Guyba4c5312011-11-10 06:55:15 -08001487 int ret;
1488
1489 IWL_DEBUG_MAC80211(priv, "enter\n");
1490
1491 if (req->n_channels == 0)
1492 return -EINVAL;
1493
Johannes Bergb1eea292012-03-06 13:30:42 -08001494 mutex_lock(&priv->mutex);
Wey-Yi Guyba4c5312011-11-10 06:55:15 -08001495
1496 /*
1497 * If an internal scan is in progress, just set
1498 * up the scan_request as per above.
1499 */
1500 if (priv->scan_type != IWL_SCAN_NORMAL) {
1501 IWL_DEBUG_SCAN(priv,
1502 "SCAN request during internal scan - defer\n");
1503 priv->scan_request = req;
1504 priv->scan_vif = vif;
1505 ret = 0;
1506 } else {
1507 priv->scan_request = req;
1508 priv->scan_vif = vif;
1509 /*
1510 * mac80211 will only ask for one band at a time
1511 * so using channels[0] here is ok
1512 */
1513 ret = iwl_scan_initiate(priv, vif, IWL_SCAN_NORMAL,
1514 req->channels[0]->band);
1515 if (ret) {
1516 priv->scan_request = NULL;
1517 priv->scan_vif = NULL;
1518 }
1519 }
1520
1521 IWL_DEBUG_MAC80211(priv, "leave\n");
1522
Johannes Bergb1eea292012-03-06 13:30:42 -08001523 mutex_unlock(&priv->mutex);
Wey-Yi Guyba4c5312011-11-10 06:55:15 -08001524
1525 return ret;
1526}
1527
Wey-Yi Guy76b29332011-11-10 06:55:16 -08001528static void iwl_sta_modify_ps_wake(struct iwl_priv *priv, int sta_id)
1529{
Johannes Berg9451ca12012-03-05 11:24:23 -08001530 struct iwl_addsta_cmd cmd = {
1531 .mode = STA_CONTROL_MODIFY_MSK,
1532 .station_flags_msk = STA_FLG_PWR_SAVE_MSK,
1533 .sta.sta_id = sta_id,
1534 };
Wey-Yi Guy76b29332011-11-10 06:55:16 -08001535
Johannes Berg9451ca12012-03-05 11:24:23 -08001536 iwl_send_add_sta(priv, &cmd, CMD_ASYNC);
Wey-Yi Guy76b29332011-11-10 06:55:16 -08001537}
1538
Johannes Bergaca86262012-05-14 09:08:50 +02001539static void iwlagn_mac_sta_notify(struct ieee80211_hw *hw,
1540 struct ieee80211_vif *vif,
1541 enum sta_notify_cmd cmd,
1542 struct ieee80211_sta *sta)
Wey-Yi Guy76b29332011-11-10 06:55:16 -08001543{
Emmanuel Grumbachd0f76d62012-02-09 16:08:15 +02001544 struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
Wey-Yi Guy76b29332011-11-10 06:55:16 -08001545 struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
1546 int sta_id;
1547
1548 IWL_DEBUG_MAC80211(priv, "enter\n");
1549
1550 switch (cmd) {
1551 case STA_NOTIFY_SLEEP:
1552 WARN_ON(!sta_priv->client);
1553 sta_priv->asleep = true;
1554 if (atomic_read(&sta_priv->pending_frames) > 0)
1555 ieee80211_sta_block_awake(hw, sta, true);
1556 break;
1557 case STA_NOTIFY_AWAKE:
1558 WARN_ON(!sta_priv->client);
1559 if (!sta_priv->asleep)
1560 break;
1561 sta_priv->asleep = false;
1562 sta_id = iwl_sta_id(sta);
1563 if (sta_id != IWL_INVALID_STATION)
1564 iwl_sta_modify_ps_wake(priv, sta_id);
1565 break;
1566 default:
1567 break;
1568 }
1569 IWL_DEBUG_MAC80211(priv, "leave\n");
1570}
1571
Wey-Yi Guy73356132011-11-10 06:55:11 -08001572struct ieee80211_ops iwlagn_hw_ops = {
1573 .tx = iwlagn_mac_tx,
1574 .start = iwlagn_mac_start,
1575 .stop = iwlagn_mac_stop,
1576#ifdef CONFIG_PM_SLEEP
1577 .suspend = iwlagn_mac_suspend,
1578 .resume = iwlagn_mac_resume,
Johannes Berg76ed2ed2012-05-16 22:54:25 +02001579 .set_wakeup = iwlagn_mac_set_wakeup,
Wey-Yi Guy73356132011-11-10 06:55:11 -08001580#endif
1581 .add_interface = iwlagn_mac_add_interface,
1582 .remove_interface = iwlagn_mac_remove_interface,
1583 .change_interface = iwlagn_mac_change_interface,
1584 .config = iwlagn_mac_config,
1585 .configure_filter = iwlagn_configure_filter,
1586 .set_key = iwlagn_mac_set_key,
1587 .update_tkip_key = iwlagn_mac_update_tkip_key,
1588 .set_rekey_data = iwlagn_mac_set_rekey_data,
1589 .conf_tx = iwlagn_mac_conf_tx,
1590 .bss_info_changed = iwlagn_bss_info_changed,
1591 .ampdu_action = iwlagn_mac_ampdu_action,
1592 .hw_scan = iwlagn_mac_hw_scan,
1593 .sta_notify = iwlagn_mac_sta_notify,
Johannes Bergfb5fe5b2012-03-05 11:24:29 -08001594 .sta_state = iwlagn_mac_sta_state,
Wey-Yi Guy73356132011-11-10 06:55:11 -08001595 .channel_switch = iwlagn_mac_channel_switch,
1596 .flush = iwlagn_mac_flush,
1597 .tx_last_beacon = iwlagn_mac_tx_last_beacon,
Wey-Yi Guy73356132011-11-10 06:55:11 -08001598 .rssi_callback = iwlagn_mac_rssi_callback,
Wey-Yi Guy73356132011-11-10 06:55:11 -08001599 .set_tim = iwlagn_mac_set_tim,
1600};
1601
1602/* This function both allocates and initializes hw and priv. */
1603struct ieee80211_hw *iwl_alloc_all(void)
1604{
1605 struct iwl_priv *priv;
Emmanuel Grumbachd0f76d62012-02-09 16:08:15 +02001606 struct iwl_op_mode *op_mode;
Wey-Yi Guy73356132011-11-10 06:55:11 -08001607 /* mac80211 allocates memory for this device instance, including
1608 * space for this driver's private structure */
1609 struct ieee80211_hw *hw;
1610
Emmanuel Grumbachd0f76d62012-02-09 16:08:15 +02001611 hw = ieee80211_alloc_hw(sizeof(struct iwl_priv) +
1612 sizeof(struct iwl_op_mode), &iwlagn_hw_ops);
Wey-Yi Guy73356132011-11-10 06:55:11 -08001613 if (!hw)
1614 goto out;
1615
Emmanuel Grumbachd0f76d62012-02-09 16:08:15 +02001616 op_mode = hw->priv;
1617 priv = IWL_OP_MODE_GET_DVM(op_mode);
Wey-Yi Guy73356132011-11-10 06:55:11 -08001618 priv->hw = hw;
1619
1620out:
1621 return hw;
1622}