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