blob: da689582af635cd8e220e54520b938c444fe8da3 [file] [log] [blame]
Wey-Yi Guy73356132011-11-10 06:55:11 -08001/******************************************************************************
2 *
3 * Copyright(c) 2003 - 2011 Intel Corporation. All rights reserved.
4 *
5 * Portions of this file are derived from the ipw3945 project, as well
6 * as portions of the ieee80211 subsystem header files.
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of version 2 of the GNU General Public License as
10 * published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
20 *
21 * The full GNU General Public License is included in this distribution in the
22 * file called LICENSE.
23 *
24 * Contact Information:
25 * Intel Linux Wireless <ilw@linux.intel.com>
26 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
27 *
28 *****************************************************************************/
29#include <linux/kernel.h>
30#include <linux/module.h>
31#include <linux/init.h>
32#include <linux/slab.h>
33#include <linux/dma-mapping.h>
34#include <linux/delay.h>
35#include <linux/sched.h>
36#include <linux/skbuff.h>
37#include <linux/netdevice.h>
38#include <linux/firmware.h>
39#include <linux/etherdevice.h>
40#include <linux/if_arp.h>
41
42#include <net/mac80211.h>
43
44#include <asm/div64.h>
45
46#include "iwl-eeprom.h"
Don Fry69a679b2011-12-07 08:50:46 -080047#include "iwl-wifi.h"
Wey-Yi Guy73356132011-11-10 06:55:11 -080048#include "iwl-dev.h"
49#include "iwl-core.h"
50#include "iwl-io.h"
51#include "iwl-agn-calib.h"
52#include "iwl-agn.h"
53#include "iwl-shared.h"
54#include "iwl-bus.h"
55#include "iwl-trans.h"
56
57/*****************************************************************************
58 *
59 * mac80211 entry point functions
60 *
61 *****************************************************************************/
62
63static const struct ieee80211_iface_limit iwlagn_sta_ap_limits[] = {
64 {
65 .max = 1,
66 .types = BIT(NL80211_IFTYPE_STATION),
67 },
68 {
69 .max = 1,
70 .types = BIT(NL80211_IFTYPE_AP),
71 },
72};
73
74static const struct ieee80211_iface_limit iwlagn_2sta_limits[] = {
75 {
76 .max = 2,
77 .types = BIT(NL80211_IFTYPE_STATION),
78 },
79};
80
81static const struct ieee80211_iface_limit iwlagn_p2p_sta_go_limits[] = {
82 {
83 .max = 1,
84 .types = BIT(NL80211_IFTYPE_STATION),
85 },
86 {
87 .max = 1,
88 .types = BIT(NL80211_IFTYPE_P2P_GO) |
89 BIT(NL80211_IFTYPE_AP),
90 },
91};
92
93static const struct ieee80211_iface_limit iwlagn_p2p_2sta_limits[] = {
94 {
95 .max = 2,
96 .types = BIT(NL80211_IFTYPE_STATION),
97 },
98 {
99 .max = 1,
100 .types = BIT(NL80211_IFTYPE_P2P_CLIENT),
101 },
102};
103
104static const struct ieee80211_iface_combination
105iwlagn_iface_combinations_dualmode[] = {
106 { .num_different_channels = 1,
107 .max_interfaces = 2,
108 .beacon_int_infra_match = true,
109 .limits = iwlagn_sta_ap_limits,
110 .n_limits = ARRAY_SIZE(iwlagn_sta_ap_limits),
111 },
112 { .num_different_channels = 1,
113 .max_interfaces = 2,
114 .limits = iwlagn_2sta_limits,
115 .n_limits = ARRAY_SIZE(iwlagn_2sta_limits),
116 },
117};
118
119static const struct ieee80211_iface_combination
120iwlagn_iface_combinations_p2p[] = {
121 { .num_different_channels = 1,
122 .max_interfaces = 2,
123 .beacon_int_infra_match = true,
124 .limits = iwlagn_p2p_sta_go_limits,
125 .n_limits = ARRAY_SIZE(iwlagn_p2p_sta_go_limits),
126 },
127 { .num_different_channels = 1,
128 .max_interfaces = 2,
129 .limits = iwlagn_p2p_2sta_limits,
130 .n_limits = ARRAY_SIZE(iwlagn_p2p_2sta_limits),
131 },
132};
133
134/*
135 * Not a mac80211 entry point function, but it fits in with all the
136 * other mac80211 functions grouped here.
137 */
138int iwlagn_mac_setup_register(struct iwl_priv *priv,
139 struct iwlagn_ucode_capabilities *capa)
140{
141 int ret;
142 struct ieee80211_hw *hw = priv->hw;
143 struct iwl_rxon_context *ctx;
144
145 hw->rate_control_algorithm = "iwl-agn-rs";
146
147 /* Tell mac80211 our characteristics */
148 hw->flags = IEEE80211_HW_SIGNAL_DBM |
149 IEEE80211_HW_AMPDU_AGGREGATION |
150 IEEE80211_HW_NEED_DTIM_PERIOD |
151 IEEE80211_HW_SPECTRUM_MGMT |
152 IEEE80211_HW_REPORTS_TX_ACK_STATUS;
153
154 /*
155 * Including the following line will crash some AP's. This
156 * workaround removes the stimulus which causes the crash until
157 * the AP software can be fixed.
158 hw->max_tx_aggregation_subframes = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
159 */
160
161 hw->flags |= IEEE80211_HW_SUPPORTS_PS |
162 IEEE80211_HW_SUPPORTS_DYNAMIC_PS;
163
Don Fry38622412011-12-16 07:07:36 -0800164 if (cfg(priv)->sku & EEPROM_SKU_CAP_11N_ENABLE)
Wey-Yi Guy73356132011-11-10 06:55:11 -0800165 hw->flags |= IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS |
166 IEEE80211_HW_SUPPORTS_STATIC_SMPS;
167
168 if (capa->flags & IWL_UCODE_TLV_FLAGS_MFP)
169 hw->flags |= IEEE80211_HW_MFP_CAPABLE;
170
171 hw->sta_data_size = sizeof(struct iwl_station_priv);
172 hw->vif_data_size = sizeof(struct iwl_vif_priv);
173
174 for_each_context(priv, ctx) {
175 hw->wiphy->interface_modes |= ctx->interface_modes;
176 hw->wiphy->interface_modes |= ctx->exclusive_interface_modes;
177 }
178
179 BUILD_BUG_ON(NUM_IWL_RXON_CTX != 2);
180
181 if (hw->wiphy->interface_modes & BIT(NL80211_IFTYPE_P2P_CLIENT)) {
182 hw->wiphy->iface_combinations = iwlagn_iface_combinations_p2p;
183 hw->wiphy->n_iface_combinations =
184 ARRAY_SIZE(iwlagn_iface_combinations_p2p);
185 } else if (hw->wiphy->interface_modes & BIT(NL80211_IFTYPE_AP)) {
186 hw->wiphy->iface_combinations =
187 iwlagn_iface_combinations_dualmode;
188 hw->wiphy->n_iface_combinations =
189 ARRAY_SIZE(iwlagn_iface_combinations_dualmode);
190 }
191
192 hw->wiphy->max_remain_on_channel_duration = 1000;
193
194 hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY |
195 WIPHY_FLAG_DISABLE_BEACON_HINTS |
196 WIPHY_FLAG_IBSS_RSN;
197
198 if (trans(priv)->ucode_wowlan.code.len &&
199 device_can_wakeup(bus(priv)->dev)) {
200 hw->wiphy->wowlan.flags = WIPHY_WOWLAN_MAGIC_PKT |
201 WIPHY_WOWLAN_DISCONNECT |
202 WIPHY_WOWLAN_EAP_IDENTITY_REQ |
203 WIPHY_WOWLAN_RFKILL_RELEASE;
204 if (!iwlagn_mod_params.sw_crypto)
205 hw->wiphy->wowlan.flags |=
206 WIPHY_WOWLAN_SUPPORTS_GTK_REKEY |
207 WIPHY_WOWLAN_GTK_REKEY_FAILURE;
208
209 hw->wiphy->wowlan.n_patterns = IWLAGN_WOWLAN_MAX_PATTERNS;
210 hw->wiphy->wowlan.pattern_min_len =
211 IWLAGN_WOWLAN_MIN_PATTERN_LEN;
212 hw->wiphy->wowlan.pattern_max_len =
213 IWLAGN_WOWLAN_MAX_PATTERN_LEN;
214 }
215
216 if (iwlagn_mod_params.power_save)
217 hw->wiphy->flags |= WIPHY_FLAG_PS_ON_BY_DEFAULT;
218 else
219 hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
220
221 hw->wiphy->max_scan_ssids = PROBE_OPTION_MAX;
222 /* we create the 802.11 header and a zero-length SSID element */
223 hw->wiphy->max_scan_ie_len = capa->max_probe_length - 24 - 2;
224
225 /* Default value; 4 EDCA QOS priorities */
226 hw->queues = 4;
227
228 hw->max_listen_interval = IWL_CONN_MAX_LISTEN_INTERVAL;
229
230 if (priv->bands[IEEE80211_BAND_2GHZ].n_channels)
231 priv->hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
232 &priv->bands[IEEE80211_BAND_2GHZ];
233 if (priv->bands[IEEE80211_BAND_5GHZ].n_channels)
234 priv->hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
235 &priv->bands[IEEE80211_BAND_5GHZ];
236
237 iwl_leds_init(priv);
238
239 ret = ieee80211_register_hw(priv->hw);
240 if (ret) {
241 IWL_ERR(priv, "Failed to register hw (error %d)\n", ret);
242 return ret;
243 }
244 priv->mac80211_registered = 1;
245
246 return 0;
247}
248
Don Fry09af1402011-12-09 10:07:38 -0800249void iwlagn_mac_unregister(struct iwl_priv *priv)
250{
251 if (!priv->mac80211_registered)
252 return;
253 iwl_leds_exit(priv);
254 ieee80211_unregister_hw(priv->hw);
255 priv->mac80211_registered = 0;
256}
257
Wey-Yi Guy73356132011-11-10 06:55:11 -0800258static int __iwl_up(struct iwl_priv *priv)
259{
260 struct iwl_rxon_context *ctx;
261 int ret;
262
263 lockdep_assert_held(&priv->shrd->mutex);
264
265 if (test_bit(STATUS_EXIT_PENDING, &priv->shrd->status)) {
266 IWL_WARN(priv, "Exit pending; will not bring the NIC up\n");
267 return -EIO;
268 }
269
270 for_each_context(priv, ctx) {
271 ret = iwlagn_alloc_bcast_station(priv, ctx);
272 if (ret) {
273 iwl_dealloc_bcast_stations(priv);
274 return ret;
275 }
276 }
277
Don Fry69a679b2011-12-07 08:50:46 -0800278 ret = iwl_run_init_ucode(trans(priv));
Wey-Yi Guy73356132011-11-10 06:55:11 -0800279 if (ret) {
280 IWL_ERR(priv, "Failed to run INIT ucode: %d\n", ret);
281 goto error;
282 }
283
Don Fry69a679b2011-12-07 08:50:46 -0800284 ret = iwl_load_ucode_wait_alive(trans(priv), IWL_UCODE_REGULAR);
Wey-Yi Guy73356132011-11-10 06:55:11 -0800285 if (ret) {
286 IWL_ERR(priv, "Failed to start RT ucode: %d\n", ret);
287 goto error;
288 }
289
290 ret = iwl_alive_start(priv);
291 if (ret)
292 goto error;
293 return 0;
294
295 error:
296 set_bit(STATUS_EXIT_PENDING, &priv->shrd->status);
297 __iwl_down(priv);
298 clear_bit(STATUS_EXIT_PENDING, &priv->shrd->status);
299
300 IWL_ERR(priv, "Unable to initialize device.\n");
301 return ret;
302}
303
304static int iwlagn_mac_start(struct ieee80211_hw *hw)
305{
306 struct iwl_priv *priv = hw->priv;
307 int ret;
308
309 IWL_DEBUG_MAC80211(priv, "enter\n");
310
311 /* we should be verifying the device is ready to be opened */
312 mutex_lock(&priv->shrd->mutex);
313 ret = __iwl_up(priv);
314 mutex_unlock(&priv->shrd->mutex);
315 if (ret)
316 return ret;
317
318 IWL_DEBUG_INFO(priv, "Start UP work done.\n");
319
320 /* Now we should be done, and the READY bit should be set. */
321 if (WARN_ON(!test_bit(STATUS_READY, &priv->shrd->status)))
322 ret = -EIO;
323
324 iwlagn_led_enable(priv);
325
326 priv->is_open = 1;
327 IWL_DEBUG_MAC80211(priv, "leave\n");
328 return 0;
329}
330
331static void iwlagn_mac_stop(struct ieee80211_hw *hw)
332{
333 struct iwl_priv *priv = hw->priv;
334
335 IWL_DEBUG_MAC80211(priv, "enter\n");
336
337 if (!priv->is_open)
338 return;
339
340 priv->is_open = 0;
341
342 iwl_down(priv);
343
344 flush_workqueue(priv->shrd->workqueue);
345
346 /* User space software may expect getting rfkill changes
347 * even if interface is down */
348 iwl_write32(bus(priv), CSR_INT, 0xFFFFFFFF);
349 iwl_enable_rfkill_int(priv);
350
351 IWL_DEBUG_MAC80211(priv, "leave\n");
352}
353
354static void iwlagn_mac_set_rekey_data(struct ieee80211_hw *hw,
355 struct ieee80211_vif *vif,
356 struct cfg80211_gtk_rekey_data *data)
357{
358 struct iwl_priv *priv = hw->priv;
359
360 if (iwlagn_mod_params.sw_crypto)
361 return;
362
363 IWL_DEBUG_MAC80211(priv, "enter\n");
364 mutex_lock(&priv->shrd->mutex);
365
366 if (priv->contexts[IWL_RXON_CTX_BSS].vif != vif)
367 goto out;
368
369 memcpy(priv->kek, data->kek, NL80211_KEK_LEN);
370 memcpy(priv->kck, data->kck, NL80211_KCK_LEN);
371 priv->replay_ctr =
372 cpu_to_le64(be64_to_cpup((__be64 *)&data->replay_ctr));
373 priv->have_rekey_data = true;
374
375 out:
376 mutex_unlock(&priv->shrd->mutex);
377 IWL_DEBUG_MAC80211(priv, "leave\n");
378}
379
380#ifdef CONFIG_PM_SLEEP
Wey-Yi Guy73356132011-11-10 06:55:11 -0800381
382static int iwlagn_mac_suspend(struct ieee80211_hw *hw,
383 struct cfg80211_wowlan *wowlan)
384{
385 struct iwl_priv *priv = hw->priv;
Wey-Yi Guy73356132011-11-10 06:55:11 -0800386 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
Wey-Yi Guy023ca582011-11-10 06:55:17 -0800387 int ret;
Wey-Yi Guy73356132011-11-10 06:55:11 -0800388
389 if (WARN_ON(!wowlan))
390 return -EINVAL;
391
392 IWL_DEBUG_MAC80211(priv, "enter\n");
393 mutex_lock(&priv->shrd->mutex);
394
395 /* Don't attempt WoWLAN when not associated, tear down instead. */
396 if (!ctx->vif || ctx->vif->type != NL80211_IFTYPE_STATION ||
397 !iwl_is_associated_ctx(ctx)) {
398 ret = 1;
399 goto out;
400 }
401
Wey-Yi Guy023ca582011-11-10 06:55:17 -0800402 ret = iwlagn_suspend(priv, hw, wowlan);
Wey-Yi Guy73356132011-11-10 06:55:11 -0800403 if (ret)
404 goto error;
405
406 device_set_wakeup_enable(bus(priv)->dev, true);
407
408 /* Now let the ucode operate on its own */
409 iwl_write32(bus(priv), CSR_UCODE_DRV_GP1_SET,
410 CSR_UCODE_DRV_GP1_BIT_D3_CFG_COMPLETE);
411
412 goto out;
413
414 error:
415 priv->shrd->wowlan = false;
416 iwlagn_prepare_restart(priv);
417 ieee80211_restart_hw(priv->hw);
418 out:
419 mutex_unlock(&priv->shrd->mutex);
Wey-Yi Guy73356132011-11-10 06:55:11 -0800420 IWL_DEBUG_MAC80211(priv, "leave\n");
421
422 return ret;
423}
424
425static int iwlagn_mac_resume(struct ieee80211_hw *hw)
426{
427 struct iwl_priv *priv = hw->priv;
428 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
429 struct ieee80211_vif *vif;
430 unsigned long flags;
431 u32 base, status = 0xffffffff;
432 int ret = -EIO;
433
434 IWL_DEBUG_MAC80211(priv, "enter\n");
435 mutex_lock(&priv->shrd->mutex);
436
437 iwl_write32(bus(priv), CSR_UCODE_DRV_GP1_CLR,
438 CSR_UCODE_DRV_GP1_BIT_D3_CFG_COMPLETE);
439
Don Fryae6130f2011-11-30 16:12:59 -0800440 base = priv->shrd->device_pointers.error_event_table;
Wey-Yi Guy73356132011-11-10 06:55:11 -0800441 if (iwlagn_hw_valid_rtc_data_addr(base)) {
442 spin_lock_irqsave(&bus(priv)->reg_lock, flags);
443 ret = iwl_grab_nic_access_silent(bus(priv));
444 if (ret == 0) {
445 iwl_write32(bus(priv), HBUS_TARG_MEM_RADDR, base);
446 status = iwl_read32(bus(priv), HBUS_TARG_MEM_RDAT);
447 iwl_release_nic_access(bus(priv));
448 }
449 spin_unlock_irqrestore(&bus(priv)->reg_lock, flags);
450
451#ifdef CONFIG_IWLWIFI_DEBUGFS
452 if (ret == 0) {
453 struct iwl_trans *trans = trans(priv);
454 if (!priv->wowlan_sram)
455 priv->wowlan_sram =
456 kzalloc(trans->ucode_wowlan.data.len,
457 GFP_KERNEL);
458
459 if (priv->wowlan_sram)
460 _iwl_read_targ_mem_words(
461 bus(priv), 0x800000, priv->wowlan_sram,
462 trans->ucode_wowlan.data.len / 4);
463 }
464#endif
465 }
466
467 /* we'll clear ctx->vif during iwlagn_prepare_restart() */
468 vif = ctx->vif;
469
470 priv->shrd->wowlan = false;
471
472 device_set_wakeup_enable(bus(priv)->dev, false);
473
474 iwlagn_prepare_restart(priv);
475
476 memset((void *)&ctx->active, 0, sizeof(ctx->active));
477 iwl_connection_init_rx_config(priv, ctx);
478 iwlagn_set_rxon_chain(priv, ctx);
479
480 mutex_unlock(&priv->shrd->mutex);
481 IWL_DEBUG_MAC80211(priv, "leave\n");
482
483 ieee80211_resume_disconnect(vif);
484
485 return 1;
486}
487
488#endif
489
490static void iwlagn_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
491{
492 struct iwl_priv *priv = hw->priv;
493
Wey-Yi Guy73356132011-11-10 06:55:11 -0800494 IWL_DEBUG_TX(priv, "dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
495 ieee80211_get_tx_rate(hw, IEEE80211_SKB_CB(skb))->bitrate);
496
497 if (iwlagn_tx_skb(priv, skb))
498 dev_kfree_skb_any(skb);
Wey-Yi Guy73356132011-11-10 06:55:11 -0800499}
500
501static void iwlagn_mac_update_tkip_key(struct ieee80211_hw *hw,
502 struct ieee80211_vif *vif,
503 struct ieee80211_key_conf *keyconf,
504 struct ieee80211_sta *sta,
505 u32 iv32, u16 *phase1key)
506{
507 struct iwl_priv *priv = hw->priv;
508
509 iwl_update_tkip_key(priv, vif, keyconf, sta, iv32, phase1key);
510}
511
512static int iwlagn_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
513 struct ieee80211_vif *vif,
514 struct ieee80211_sta *sta,
515 struct ieee80211_key_conf *key)
516{
517 struct iwl_priv *priv = hw->priv;
518 struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
519 struct iwl_rxon_context *ctx = vif_priv->ctx;
520 int ret;
521 bool is_default_wep_key = false;
522
523 IWL_DEBUG_MAC80211(priv, "enter\n");
524
525 if (iwlagn_mod_params.sw_crypto) {
526 IWL_DEBUG_MAC80211(priv, "leave - hwcrypto disabled\n");
527 return -EOPNOTSUPP;
528 }
529
Johannes Berga7e12c82011-11-25 03:20:09 -0800530 switch (key->cipher) {
531 case WLAN_CIPHER_SUITE_TKIP:
532 key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
533 /* fall through */
534 case WLAN_CIPHER_SUITE_CCMP:
535 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
536 break;
537 default:
538 break;
539 }
540
Wey-Yi Guy73356132011-11-10 06:55:11 -0800541 /*
542 * We could program these keys into the hardware as well, but we
543 * don't expect much multicast traffic in IBSS and having keys
544 * for more stations is probably more useful.
545 *
546 * Mark key TX-only and return 0.
547 */
548 if (vif->type == NL80211_IFTYPE_ADHOC &&
549 !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
550 key->hw_key_idx = WEP_INVALID_OFFSET;
551 return 0;
552 }
553
554 /* If they key was TX-only, accept deletion */
555 if (cmd == DISABLE_KEY && key->hw_key_idx == WEP_INVALID_OFFSET)
556 return 0;
557
558 mutex_lock(&priv->shrd->mutex);
559 iwl_scan_cancel_timeout(priv, 100);
560
561 BUILD_BUG_ON(WEP_INVALID_OFFSET == IWLAGN_HW_KEY_DEFAULT);
562
563 /*
564 * If we are getting WEP group key and we didn't receive any key mapping
565 * so far, we are in legacy wep mode (group key only), otherwise we are
566 * in 1X mode.
567 * In legacy wep mode, we use another host command to the uCode.
568 */
569 if ((key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
570 key->cipher == WLAN_CIPHER_SUITE_WEP104) && !sta) {
571 if (cmd == SET_KEY)
572 is_default_wep_key = !ctx->key_mapping_keys;
573 else
574 is_default_wep_key =
575 key->hw_key_idx == IWLAGN_HW_KEY_DEFAULT;
576 }
577
578
579 switch (cmd) {
580 case SET_KEY:
581 if (is_default_wep_key) {
582 ret = iwl_set_default_wep_key(priv, vif_priv->ctx, key);
583 break;
584 }
585 ret = iwl_set_dynamic_key(priv, vif_priv->ctx, key, sta);
586 if (ret) {
587 /*
588 * can't add key for RX, but we don't need it
589 * in the device for TX so still return 0
590 */
591 ret = 0;
592 key->hw_key_idx = WEP_INVALID_OFFSET;
593 }
594
595 IWL_DEBUG_MAC80211(priv, "enable hwcrypto key\n");
596 break;
597 case DISABLE_KEY:
598 if (is_default_wep_key)
599 ret = iwl_remove_default_wep_key(priv, ctx, key);
600 else
601 ret = iwl_remove_dynamic_key(priv, ctx, key, sta);
602
603 IWL_DEBUG_MAC80211(priv, "disable hwcrypto key\n");
604 break;
605 default:
606 ret = -EINVAL;
607 }
608
609 mutex_unlock(&priv->shrd->mutex);
610 IWL_DEBUG_MAC80211(priv, "leave\n");
611
612 return ret;
613}
614
615static int iwlagn_mac_ampdu_action(struct ieee80211_hw *hw,
616 struct ieee80211_vif *vif,
617 enum ieee80211_ampdu_mlme_action action,
618 struct ieee80211_sta *sta, u16 tid, u16 *ssn,
619 u8 buf_size)
620{
621 struct iwl_priv *priv = hw->priv;
622 int ret = -EINVAL;
623 struct iwl_station_priv *sta_priv = (void *) sta->drv_priv;
Wey-Yi Guy73356132011-11-10 06:55:11 -0800624
625 IWL_DEBUG_HT(priv, "A-MPDU action on addr %pM tid %d\n",
626 sta->addr, tid);
627
Don Fry38622412011-12-16 07:07:36 -0800628 if (!(cfg(priv)->sku & EEPROM_SKU_CAP_11N_ENABLE))
Wey-Yi Guy73356132011-11-10 06:55:11 -0800629 return -EACCES;
630
631 IWL_DEBUG_MAC80211(priv, "enter\n");
632 mutex_lock(&priv->shrd->mutex);
633
634 switch (action) {
635 case IEEE80211_AMPDU_RX_START:
636 IWL_DEBUG_HT(priv, "start Rx\n");
637 ret = iwl_sta_rx_agg_start(priv, sta, tid, *ssn);
638 break;
639 case IEEE80211_AMPDU_RX_STOP:
640 IWL_DEBUG_HT(priv, "stop Rx\n");
641 ret = iwl_sta_rx_agg_stop(priv, sta, tid);
642 if (test_bit(STATUS_EXIT_PENDING, &priv->shrd->status))
643 ret = 0;
644 break;
645 case IEEE80211_AMPDU_TX_START:
646 IWL_DEBUG_HT(priv, "start Tx\n");
647 ret = iwlagn_tx_agg_start(priv, vif, sta, tid, ssn);
648 break;
649 case IEEE80211_AMPDU_TX_STOP:
650 IWL_DEBUG_HT(priv, "stop Tx\n");
651 ret = iwlagn_tx_agg_stop(priv, vif, sta, tid);
652 if ((ret == 0) && (priv->agg_tids_count > 0)) {
653 priv->agg_tids_count--;
654 IWL_DEBUG_HT(priv, "priv->agg_tids_count = %u\n",
655 priv->agg_tids_count);
656 }
657 if (test_bit(STATUS_EXIT_PENDING, &priv->shrd->status))
658 ret = 0;
Don Fry38622412011-12-16 07:07:36 -0800659 if (!priv->agg_tids_count && cfg(priv)->ht_params &&
660 cfg(priv)->ht_params->use_rts_for_aggregation) {
Wey-Yi Guy73356132011-11-10 06:55:11 -0800661 /*
662 * switch off RTS/CTS if it was previously enabled
663 */
664 sta_priv->lq_sta.lq.general_params.flags &=
665 ~LINK_QUAL_FLAGS_SET_STA_TLC_RTS_MSK;
666 iwl_send_lq_cmd(priv, iwl_rxon_ctx_from_vif(vif),
667 &sta_priv->lq_sta.lq, CMD_ASYNC, false);
668 }
669 break;
670 case IEEE80211_AMPDU_TX_OPERATIONAL:
Emmanuel Grumbach822e8b22011-11-21 13:25:31 +0200671 ret = iwlagn_tx_agg_oper(priv, vif, sta, tid, buf_size);
Wey-Yi Guy73356132011-11-10 06:55:11 -0800672 break;
673 }
674 mutex_unlock(&priv->shrd->mutex);
675 IWL_DEBUG_MAC80211(priv, "leave\n");
676 return ret;
677}
678
679static int iwlagn_mac_sta_add(struct ieee80211_hw *hw,
680 struct ieee80211_vif *vif,
681 struct ieee80211_sta *sta)
682{
683 struct iwl_priv *priv = hw->priv;
684 struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
685 struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
686 bool is_ap = vif->type == NL80211_IFTYPE_STATION;
687 int ret = 0;
688 u8 sta_id;
689
690 IWL_DEBUG_MAC80211(priv, "received request to add station %pM\n",
691 sta->addr);
692 mutex_lock(&priv->shrd->mutex);
693 IWL_DEBUG_INFO(priv, "proceeding to add station %pM\n",
694 sta->addr);
695 sta_priv->sta_id = IWL_INVALID_STATION;
696
697 atomic_set(&sta_priv->pending_frames, 0);
698 if (vif->type == NL80211_IFTYPE_AP)
699 sta_priv->client = true;
700
701 ret = iwl_add_station_common(priv, vif_priv->ctx, sta->addr,
702 is_ap, sta, &sta_id);
703 if (ret) {
704 IWL_ERR(priv, "Unable to add station %pM (%d)\n",
705 sta->addr, ret);
706 /* Should we return success if return code is EEXIST ? */
707 goto out;
708 }
709
710 sta_priv->sta_id = sta_id;
711
712 /* Initialize rate scaling */
713 IWL_DEBUG_INFO(priv, "Initializing rate scaling for station %pM\n",
714 sta->addr);
715 iwl_rs_rate_init(priv, sta, sta_id);
716 out:
717 mutex_unlock(&priv->shrd->mutex);
718 IWL_DEBUG_MAC80211(priv, "leave\n");
719
720 return ret;
721}
722
723static void iwlagn_mac_channel_switch(struct ieee80211_hw *hw,
724 struct ieee80211_channel_switch *ch_switch)
725{
726 struct iwl_priv *priv = hw->priv;
727 const struct iwl_channel_info *ch_info;
728 struct ieee80211_conf *conf = &hw->conf;
729 struct ieee80211_channel *channel = ch_switch->channel;
730 struct iwl_ht_config *ht_conf = &priv->current_ht_config;
731 /*
732 * MULTI-FIXME
733 * When we add support for multiple interfaces, we need to
734 * revisit this. The channel switch command in the device
735 * only affects the BSS context, but what does that really
736 * mean? And what if we get a CSA on the second interface?
737 * This needs a lot of work.
738 */
739 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
740 u16 ch;
741
742 IWL_DEBUG_MAC80211(priv, "enter\n");
743
744 mutex_lock(&priv->shrd->mutex);
745
746 if (iwl_is_rfkill(priv->shrd))
747 goto out;
748
749 if (test_bit(STATUS_EXIT_PENDING, &priv->shrd->status) ||
750 test_bit(STATUS_SCANNING, &priv->shrd->status) ||
751 test_bit(STATUS_CHANNEL_SWITCH_PENDING, &priv->shrd->status))
752 goto out;
753
754 if (!iwl_is_associated_ctx(ctx))
755 goto out;
756
Don Fry38622412011-12-16 07:07:36 -0800757 if (!cfg(priv)->lib->set_channel_switch)
Wey-Yi Guy73356132011-11-10 06:55:11 -0800758 goto out;
759
760 ch = channel->hw_value;
761 if (le16_to_cpu(ctx->active.channel) == ch)
762 goto out;
763
764 ch_info = iwl_get_channel_info(priv, channel->band, ch);
765 if (!is_channel_valid(ch_info)) {
766 IWL_DEBUG_MAC80211(priv, "invalid channel\n");
767 goto out;
768 }
769
770 spin_lock_irq(&priv->shrd->lock);
771
772 priv->current_ht_config.smps = conf->smps_mode;
773
774 /* Configure HT40 channels */
775 ctx->ht.enabled = conf_is_ht(conf);
Wey-Yi Guy137ce792011-11-22 16:23:35 -0800776 if (ctx->ht.enabled)
777 iwlagn_config_ht40(conf, ctx);
778 else
Wey-Yi Guy73356132011-11-10 06:55:11 -0800779 ctx->ht.is_40mhz = false;
780
781 if ((le16_to_cpu(ctx->staging.channel) != ch))
782 ctx->staging.flags = 0;
783
784 iwl_set_rxon_channel(priv, channel, ctx);
785 iwl_set_rxon_ht(priv, ht_conf);
786 iwl_set_flags_for_band(priv, ctx, channel->band, ctx->vif);
787
788 spin_unlock_irq(&priv->shrd->lock);
789
790 iwl_set_rate(priv);
791 /*
792 * at this point, staging_rxon has the
793 * configuration for channel switch
794 */
795 set_bit(STATUS_CHANNEL_SWITCH_PENDING, &priv->shrd->status);
796 priv->switch_channel = cpu_to_le16(ch);
Don Fry38622412011-12-16 07:07:36 -0800797 if (cfg(priv)->lib->set_channel_switch(priv, ch_switch)) {
Wey-Yi Guy73356132011-11-10 06:55:11 -0800798 clear_bit(STATUS_CHANNEL_SWITCH_PENDING, &priv->shrd->status);
799 priv->switch_channel = 0;
800 ieee80211_chswitch_done(ctx->vif, false);
801 }
802
803out:
804 mutex_unlock(&priv->shrd->mutex);
805 IWL_DEBUG_MAC80211(priv, "leave\n");
806}
807
808static void iwlagn_configure_filter(struct ieee80211_hw *hw,
809 unsigned int changed_flags,
810 unsigned int *total_flags,
811 u64 multicast)
812{
813 struct iwl_priv *priv = hw->priv;
814 __le32 filter_or = 0, filter_nand = 0;
815 struct iwl_rxon_context *ctx;
816
817#define CHK(test, flag) do { \
818 if (*total_flags & (test)) \
819 filter_or |= (flag); \
820 else \
821 filter_nand |= (flag); \
822 } while (0)
823
824 IWL_DEBUG_MAC80211(priv, "Enter: changed: 0x%x, total: 0x%x\n",
825 changed_flags, *total_flags);
826
827 CHK(FIF_OTHER_BSS | FIF_PROMISC_IN_BSS, RXON_FILTER_PROMISC_MSK);
828 /* Setting _just_ RXON_FILTER_CTL2HOST_MSK causes FH errors */
829 CHK(FIF_CONTROL, RXON_FILTER_CTL2HOST_MSK | RXON_FILTER_PROMISC_MSK);
830 CHK(FIF_BCN_PRBRESP_PROMISC, RXON_FILTER_BCON_AWARE_MSK);
831
832#undef CHK
833
834 mutex_lock(&priv->shrd->mutex);
835
836 for_each_context(priv, ctx) {
837 ctx->staging.filter_flags &= ~filter_nand;
838 ctx->staging.filter_flags |= filter_or;
839
840 /*
841 * Not committing directly because hardware can perform a scan,
842 * but we'll eventually commit the filter flags change anyway.
843 */
844 }
845
846 mutex_unlock(&priv->shrd->mutex);
847
848 /*
849 * Receiving all multicast frames is always enabled by the
850 * default flags setup in iwl_connection_init_rx_config()
851 * since we currently do not support programming multicast
852 * filters into the device.
853 */
854 *total_flags &= FIF_OTHER_BSS | FIF_ALLMULTI | FIF_PROMISC_IN_BSS |
855 FIF_BCN_PRBRESP_PROMISC | FIF_CONTROL;
856}
857
858static void iwlagn_mac_flush(struct ieee80211_hw *hw, bool drop)
859{
860 struct iwl_priv *priv = hw->priv;
861
862 mutex_lock(&priv->shrd->mutex);
863 IWL_DEBUG_MAC80211(priv, "enter\n");
864
865 if (test_bit(STATUS_EXIT_PENDING, &priv->shrd->status)) {
866 IWL_DEBUG_TX(priv, "Aborting flush due to device shutdown\n");
867 goto done;
868 }
869 if (iwl_is_rfkill(priv->shrd)) {
870 IWL_DEBUG_TX(priv, "Aborting flush due to RF Kill\n");
871 goto done;
872 }
873
874 /*
875 * mac80211 will not push any more frames for transmit
876 * until the flush is completed
877 */
878 if (drop) {
879 IWL_DEBUG_MAC80211(priv, "send flush command\n");
880 if (iwlagn_txfifo_flush(priv, IWL_DROP_ALL)) {
881 IWL_ERR(priv, "flush request fail\n");
882 goto done;
883 }
884 }
885 IWL_DEBUG_MAC80211(priv, "wait transmit/flush all frames\n");
886 iwl_trans_wait_tx_queue_empty(trans(priv));
887done:
888 mutex_unlock(&priv->shrd->mutex);
889 IWL_DEBUG_MAC80211(priv, "leave\n");
890}
891
892static int iwlagn_mac_remain_on_channel(struct ieee80211_hw *hw,
893 struct ieee80211_channel *channel,
894 enum nl80211_channel_type channel_type,
895 int duration)
896{
897 struct iwl_priv *priv = hw->priv;
898 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_PAN];
899 int err = 0;
900
901 if (!(priv->shrd->valid_contexts & BIT(IWL_RXON_CTX_PAN)))
902 return -EOPNOTSUPP;
903
904 if (!(ctx->interface_modes & BIT(NL80211_IFTYPE_P2P_CLIENT)))
905 return -EOPNOTSUPP;
906
907 IWL_DEBUG_MAC80211(priv, "enter\n");
908 mutex_lock(&priv->shrd->mutex);
909
910 if (test_bit(STATUS_SCAN_HW, &priv->shrd->status)) {
911 err = -EBUSY;
912 goto out;
913 }
914
915 priv->hw_roc_channel = channel;
916 priv->hw_roc_chantype = channel_type;
Johannes Berg3ddf6be2011-11-10 06:55:21 -0800917 /* convert from ms to TU */
918 priv->hw_roc_duration = DIV_ROUND_UP(1000 * duration, 1024);
Wey-Yi Guy73356132011-11-10 06:55:11 -0800919 priv->hw_roc_start_notified = false;
920 cancel_delayed_work(&priv->hw_roc_disable_work);
921
922 if (!ctx->is_active) {
Johannes Berga69cd042011-11-10 06:55:13 -0800923 static const struct iwl_qos_info default_qos_data = {
924 .def_qos_parm = {
925 .ac[0] = {
926 .cw_min = cpu_to_le16(3),
927 .cw_max = cpu_to_le16(7),
928 .aifsn = 2,
929 .edca_txop = cpu_to_le16(1504),
930 },
931 .ac[1] = {
932 .cw_min = cpu_to_le16(7),
933 .cw_max = cpu_to_le16(15),
934 .aifsn = 2,
935 .edca_txop = cpu_to_le16(3008),
936 },
937 .ac[2] = {
938 .cw_min = cpu_to_le16(15),
939 .cw_max = cpu_to_le16(1023),
940 .aifsn = 3,
941 },
942 .ac[3] = {
943 .cw_min = cpu_to_le16(15),
944 .cw_max = cpu_to_le16(1023),
945 .aifsn = 7,
946 },
947 },
948 };
949
Wey-Yi Guy73356132011-11-10 06:55:11 -0800950 ctx->is_active = true;
Johannes Berga69cd042011-11-10 06:55:13 -0800951 ctx->qos_data = default_qos_data;
Wey-Yi Guy73356132011-11-10 06:55:11 -0800952 ctx->staging.dev_type = RXON_DEV_TYPE_P2P;
953 memcpy(ctx->staging.node_addr,
954 priv->contexts[IWL_RXON_CTX_BSS].staging.node_addr,
955 ETH_ALEN);
956 memcpy(ctx->staging.bssid_addr,
957 priv->contexts[IWL_RXON_CTX_BSS].staging.node_addr,
958 ETH_ALEN);
959 err = iwlagn_commit_rxon(priv, ctx);
960 if (err)
961 goto out;
962 ctx->staging.filter_flags |= RXON_FILTER_ASSOC_MSK |
963 RXON_FILTER_PROMISC_MSK |
964 RXON_FILTER_CTL2HOST_MSK;
965
966 err = iwlagn_commit_rxon(priv, ctx);
967 if (err) {
968 iwlagn_disable_roc(priv);
969 goto out;
970 }
971 priv->hw_roc_setup = true;
972 }
973
974 err = iwl_scan_initiate(priv, ctx->vif, IWL_SCAN_ROC, channel->band);
975 if (err)
976 iwlagn_disable_roc(priv);
977
978 out:
979 mutex_unlock(&priv->shrd->mutex);
980 IWL_DEBUG_MAC80211(priv, "leave\n");
981
982 return err;
983}
984
985static int iwlagn_mac_cancel_remain_on_channel(struct ieee80211_hw *hw)
986{
987 struct iwl_priv *priv = hw->priv;
988
989 if (!(priv->shrd->valid_contexts & BIT(IWL_RXON_CTX_PAN)))
990 return -EOPNOTSUPP;
991
992 IWL_DEBUG_MAC80211(priv, "enter\n");
993 mutex_lock(&priv->shrd->mutex);
994 iwl_scan_cancel_timeout(priv, priv->hw_roc_duration);
995 iwlagn_disable_roc(priv);
996 mutex_unlock(&priv->shrd->mutex);
997 IWL_DEBUG_MAC80211(priv, "leave\n");
998
999 return 0;
1000}
1001
1002static int iwlagn_mac_tx_sync(struct ieee80211_hw *hw,
1003 struct ieee80211_vif *vif,
1004 const u8 *bssid,
1005 enum ieee80211_tx_sync_type type)
1006{
1007 struct iwl_priv *priv = hw->priv;
1008 struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
1009 struct iwl_rxon_context *ctx = vif_priv->ctx;
1010 int ret;
1011 u8 sta_id;
1012
John W. Linville5d22df22011-12-14 14:35:41 -05001013 if (ctx->ctxid != IWL_RXON_CTX_PAN)
1014 return 0;
1015
Wey-Yi Guy73356132011-11-10 06:55:11 -08001016 IWL_DEBUG_MAC80211(priv, "enter\n");
1017 mutex_lock(&priv->shrd->mutex);
1018
1019 if (iwl_is_associated_ctx(ctx)) {
1020 ret = 0;
1021 goto out;
1022 }
1023
1024 if (ctx->preauth_bssid || test_bit(STATUS_SCAN_HW,
1025 &priv->shrd->status)) {
1026 ret = -EBUSY;
1027 goto out;
1028 }
1029
1030 ret = iwl_add_station_common(priv, ctx, bssid, true, NULL, &sta_id);
1031 if (ret)
1032 goto out;
1033
1034 if (WARN_ON(sta_id != ctx->ap_sta_id)) {
1035 ret = -EIO;
1036 goto out_remove_sta;
1037 }
1038
1039 memcpy(ctx->bssid, bssid, ETH_ALEN);
1040 ctx->preauth_bssid = true;
1041
1042 ret = iwlagn_commit_rxon(priv, ctx);
1043
1044 if (ret == 0)
1045 goto out;
1046
1047 out_remove_sta:
1048 iwl_remove_station(priv, sta_id, bssid);
1049 out:
1050 mutex_unlock(&priv->shrd->mutex);
1051 IWL_DEBUG_MAC80211(priv, "leave\n");
1052
1053 return ret;
1054}
1055
1056static void iwlagn_mac_finish_tx_sync(struct ieee80211_hw *hw,
1057 struct ieee80211_vif *vif,
1058 const u8 *bssid,
1059 enum ieee80211_tx_sync_type type)
1060{
1061 struct iwl_priv *priv = hw->priv;
1062 struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
1063 struct iwl_rxon_context *ctx = vif_priv->ctx;
1064
John W. Linville5d22df22011-12-14 14:35:41 -05001065 if (ctx->ctxid != IWL_RXON_CTX_PAN)
1066 return;
1067
Wey-Yi Guy73356132011-11-10 06:55:11 -08001068 IWL_DEBUG_MAC80211(priv, "enter\n");
1069 mutex_lock(&priv->shrd->mutex);
1070
1071 if (iwl_is_associated_ctx(ctx))
1072 goto out;
1073
1074 iwl_remove_station(priv, ctx->ap_sta_id, bssid);
1075 ctx->preauth_bssid = false;
1076 /* no need to commit */
1077 out:
1078 mutex_unlock(&priv->shrd->mutex);
1079 IWL_DEBUG_MAC80211(priv, "leave\n");
1080}
1081
1082static void iwlagn_mac_rssi_callback(struct ieee80211_hw *hw,
1083 enum ieee80211_rssi_event rssi_event)
1084{
1085 struct iwl_priv *priv = hw->priv;
1086
1087 IWL_DEBUG_MAC80211(priv, "enter\n");
1088 mutex_lock(&priv->shrd->mutex);
1089
Don Fry38622412011-12-16 07:07:36 -08001090 if (cfg(priv)->bt_params &&
1091 cfg(priv)->bt_params->advanced_bt_coexist) {
Wey-Yi Guy73356132011-11-10 06:55:11 -08001092 if (rssi_event == RSSI_EVENT_LOW)
1093 priv->bt_enable_pspoll = true;
1094 else if (rssi_event == RSSI_EVENT_HIGH)
1095 priv->bt_enable_pspoll = false;
1096
1097 iwlagn_send_advance_bt_config(priv);
1098 } else {
1099 IWL_DEBUG_MAC80211(priv, "Advanced BT coex disabled,"
1100 "ignoring RSSI callback\n");
1101 }
1102
1103 mutex_unlock(&priv->shrd->mutex);
1104 IWL_DEBUG_MAC80211(priv, "leave\n");
1105}
1106
1107static int iwlagn_mac_set_tim(struct ieee80211_hw *hw,
1108 struct ieee80211_sta *sta, bool set)
1109{
1110 struct iwl_priv *priv = hw->priv;
1111
1112 queue_work(priv->shrd->workqueue, &priv->beacon_update);
1113
1114 return 0;
1115}
1116
Wey-Yi Guy0b7a4c72011-11-10 06:55:14 -08001117static int iwlagn_mac_conf_tx(struct ieee80211_hw *hw,
1118 struct ieee80211_vif *vif, u16 queue,
1119 const struct ieee80211_tx_queue_params *params)
1120{
1121 struct iwl_priv *priv = hw->priv;
1122 struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
1123 struct iwl_rxon_context *ctx = vif_priv->ctx;
1124 unsigned long flags;
1125 int q;
1126
1127 if (WARN_ON(!ctx))
1128 return -EINVAL;
1129
1130 IWL_DEBUG_MAC80211(priv, "enter\n");
1131
1132 if (!iwl_is_ready_rf(priv->shrd)) {
1133 IWL_DEBUG_MAC80211(priv, "leave - RF not ready\n");
1134 return -EIO;
1135 }
1136
1137 if (queue >= AC_NUM) {
1138 IWL_DEBUG_MAC80211(priv, "leave - queue >= AC_NUM %d\n", queue);
1139 return 0;
1140 }
1141
1142 q = AC_NUM - 1 - queue;
1143
1144 spin_lock_irqsave(&priv->shrd->lock, flags);
1145
1146 ctx->qos_data.def_qos_parm.ac[q].cw_min =
1147 cpu_to_le16(params->cw_min);
1148 ctx->qos_data.def_qos_parm.ac[q].cw_max =
1149 cpu_to_le16(params->cw_max);
1150 ctx->qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
1151 ctx->qos_data.def_qos_parm.ac[q].edca_txop =
1152 cpu_to_le16((params->txop * 32));
1153
1154 ctx->qos_data.def_qos_parm.ac[q].reserved1 = 0;
1155
1156 spin_unlock_irqrestore(&priv->shrd->lock, flags);
1157
1158 IWL_DEBUG_MAC80211(priv, "leave\n");
1159 return 0;
1160}
1161
1162static int iwlagn_mac_tx_last_beacon(struct ieee80211_hw *hw)
1163{
1164 struct iwl_priv *priv = hw->priv;
1165
1166 return priv->ibss_manager == IWL_IBSS_MANAGER;
1167}
1168
1169static int iwl_set_mode(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
1170{
1171 iwl_connection_init_rx_config(priv, ctx);
1172
1173 iwlagn_set_rxon_chain(priv, ctx);
1174
1175 return iwlagn_commit_rxon(priv, ctx);
1176}
1177
1178static int iwl_setup_interface(struct iwl_priv *priv,
1179 struct iwl_rxon_context *ctx)
1180{
1181 struct ieee80211_vif *vif = ctx->vif;
1182 int err;
1183
1184 lockdep_assert_held(&priv->shrd->mutex);
1185
1186 /*
1187 * This variable will be correct only when there's just
1188 * a single context, but all code using it is for hardware
1189 * that supports only one context.
1190 */
1191 priv->iw_mode = vif->type;
1192
1193 ctx->is_active = true;
1194
1195 err = iwl_set_mode(priv, ctx);
1196 if (err) {
1197 if (!ctx->always_active)
1198 ctx->is_active = false;
1199 return err;
1200 }
1201
Don Fry38622412011-12-16 07:07:36 -08001202 if (cfg(priv)->bt_params && cfg(priv)->bt_params->advanced_bt_coexist &&
Wey-Yi Guy0b7a4c72011-11-10 06:55:14 -08001203 vif->type == NL80211_IFTYPE_ADHOC) {
1204 /*
1205 * pretend to have high BT traffic as long as we
1206 * are operating in IBSS mode, as this will cause
1207 * the rate scaling etc. to behave as intended.
1208 */
1209 priv->bt_traffic_load = IWL_BT_COEX_TRAFFIC_LOAD_HIGH;
1210 }
1211
1212 return 0;
1213}
1214
1215static int iwlagn_mac_add_interface(struct ieee80211_hw *hw,
1216 struct ieee80211_vif *vif)
1217{
1218 struct iwl_priv *priv = hw->priv;
1219 struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
1220 struct iwl_rxon_context *tmp, *ctx = NULL;
1221 int err;
1222 enum nl80211_iftype viftype = ieee80211_vif_type_p2p(vif);
1223
1224 IWL_DEBUG_MAC80211(priv, "enter: type %d, addr %pM\n",
1225 viftype, vif->addr);
1226
1227 cancel_delayed_work_sync(&priv->hw_roc_disable_work);
1228
1229 mutex_lock(&priv->shrd->mutex);
1230
1231 iwlagn_disable_roc(priv);
1232
1233 if (!iwl_is_ready_rf(priv->shrd)) {
1234 IWL_WARN(priv, "Try to add interface when device not ready\n");
1235 err = -EINVAL;
1236 goto out;
1237 }
1238
1239 for_each_context(priv, tmp) {
1240 u32 possible_modes =
1241 tmp->interface_modes | tmp->exclusive_interface_modes;
1242
1243 if (tmp->vif) {
1244 /* check if this busy context is exclusive */
1245 if (tmp->exclusive_interface_modes &
1246 BIT(tmp->vif->type)) {
1247 err = -EINVAL;
1248 goto out;
1249 }
1250 continue;
1251 }
1252
1253 if (!(possible_modes & BIT(viftype)))
1254 continue;
1255
1256 /* have maybe usable context w/o interface */
1257 ctx = tmp;
1258 break;
1259 }
1260
1261 if (!ctx) {
1262 err = -EOPNOTSUPP;
1263 goto out;
1264 }
1265
1266 vif_priv->ctx = ctx;
1267 ctx->vif = vif;
1268
1269 err = iwl_setup_interface(priv, ctx);
1270 if (!err)
1271 goto out;
1272
1273 ctx->vif = NULL;
1274 priv->iw_mode = NL80211_IFTYPE_STATION;
1275 out:
1276 mutex_unlock(&priv->shrd->mutex);
1277
1278 IWL_DEBUG_MAC80211(priv, "leave\n");
1279 return err;
1280}
1281
1282static void iwl_teardown_interface(struct iwl_priv *priv,
1283 struct ieee80211_vif *vif,
1284 bool mode_change)
1285{
1286 struct iwl_rxon_context *ctx = iwl_rxon_ctx_from_vif(vif);
1287
1288 lockdep_assert_held(&priv->shrd->mutex);
1289
1290 if (priv->scan_vif == vif) {
1291 iwl_scan_cancel_timeout(priv, 200);
1292 iwl_force_scan_end(priv);
1293 }
1294
1295 if (!mode_change) {
1296 iwl_set_mode(priv, ctx);
1297 if (!ctx->always_active)
1298 ctx->is_active = false;
1299 }
1300
1301 /*
1302 * When removing the IBSS interface, overwrite the
1303 * BT traffic load with the stored one from the last
1304 * notification, if any. If this is a device that
1305 * doesn't implement this, this has no effect since
1306 * both values are the same and zero.
1307 */
1308 if (vif->type == NL80211_IFTYPE_ADHOC)
1309 priv->bt_traffic_load = priv->last_bt_traffic_load;
1310}
1311
1312static void iwlagn_mac_remove_interface(struct ieee80211_hw *hw,
1313 struct ieee80211_vif *vif)
1314{
1315 struct iwl_priv *priv = hw->priv;
1316 struct iwl_rxon_context *ctx = iwl_rxon_ctx_from_vif(vif);
1317
1318 IWL_DEBUG_MAC80211(priv, "enter\n");
1319
1320 mutex_lock(&priv->shrd->mutex);
1321
1322 if (WARN_ON(ctx->vif != vif)) {
1323 struct iwl_rxon_context *tmp;
1324 IWL_ERR(priv, "ctx->vif = %p, vif = %p\n", ctx->vif, vif);
1325 for_each_context(priv, tmp)
1326 IWL_ERR(priv, "\tID = %d:\tctx = %p\tctx->vif = %p\n",
1327 tmp->ctxid, tmp, tmp->vif);
1328 }
1329 ctx->vif = NULL;
1330
1331 iwl_teardown_interface(priv, vif, false);
1332
1333 mutex_unlock(&priv->shrd->mutex);
1334
1335 IWL_DEBUG_MAC80211(priv, "leave\n");
1336
1337}
1338
1339static int iwlagn_mac_change_interface(struct ieee80211_hw *hw,
1340 struct ieee80211_vif *vif,
1341 enum nl80211_iftype newtype, bool newp2p)
1342{
1343 struct iwl_priv *priv = hw->priv;
1344 struct iwl_rxon_context *ctx = iwl_rxon_ctx_from_vif(vif);
1345 struct iwl_rxon_context *bss_ctx = &priv->contexts[IWL_RXON_CTX_BSS];
1346 struct iwl_rxon_context *tmp;
1347 enum nl80211_iftype newviftype = newtype;
1348 u32 interface_modes;
1349 int err;
1350
1351 IWL_DEBUG_MAC80211(priv, "enter\n");
1352
1353 newtype = ieee80211_iftype_p2p(newtype, newp2p);
1354
1355 mutex_lock(&priv->shrd->mutex);
1356
1357 if (!ctx->vif || !iwl_is_ready_rf(priv->shrd)) {
1358 /*
1359 * Huh? But wait ... this can maybe happen when
1360 * we're in the middle of a firmware restart!
1361 */
1362 err = -EBUSY;
1363 goto out;
1364 }
1365
1366 interface_modes = ctx->interface_modes | ctx->exclusive_interface_modes;
1367
1368 if (!(interface_modes & BIT(newtype))) {
1369 err = -EBUSY;
1370 goto out;
1371 }
1372
1373 /*
1374 * Refuse a change that should be done by moving from the PAN
1375 * context to the BSS context instead, if the BSS context is
1376 * available and can support the new interface type.
1377 */
1378 if (ctx->ctxid == IWL_RXON_CTX_PAN && !bss_ctx->vif &&
1379 (bss_ctx->interface_modes & BIT(newtype) ||
1380 bss_ctx->exclusive_interface_modes & BIT(newtype))) {
1381 BUILD_BUG_ON(NUM_IWL_RXON_CTX != 2);
1382 err = -EBUSY;
1383 goto out;
1384 }
1385
1386 if (ctx->exclusive_interface_modes & BIT(newtype)) {
1387 for_each_context(priv, tmp) {
1388 if (ctx == tmp)
1389 continue;
1390
1391 if (!tmp->vif)
1392 continue;
1393
1394 /*
1395 * The current mode switch would be exclusive, but
1396 * another context is active ... refuse the switch.
1397 */
1398 err = -EBUSY;
1399 goto out;
1400 }
1401 }
1402
1403 /* success */
1404 iwl_teardown_interface(priv, vif, true);
1405 vif->type = newviftype;
1406 vif->p2p = newp2p;
1407 err = iwl_setup_interface(priv, ctx);
1408 WARN_ON(err);
1409 /*
1410 * We've switched internally, but submitting to the
1411 * device may have failed for some reason. Mask this
1412 * error, because otherwise mac80211 will not switch
1413 * (and set the interface type back) and we'll be
1414 * out of sync with it.
1415 */
1416 err = 0;
1417
1418 out:
1419 mutex_unlock(&priv->shrd->mutex);
1420 IWL_DEBUG_MAC80211(priv, "leave\n");
1421
1422 return err;
1423}
1424
Wey-Yi Guyba4c5312011-11-10 06:55:15 -08001425static int iwlagn_mac_hw_scan(struct ieee80211_hw *hw,
1426 struct ieee80211_vif *vif,
1427 struct cfg80211_scan_request *req)
1428{
1429 struct iwl_priv *priv = hw->priv;
1430 int ret;
1431
1432 IWL_DEBUG_MAC80211(priv, "enter\n");
1433
1434 if (req->n_channels == 0)
1435 return -EINVAL;
1436
1437 mutex_lock(&priv->shrd->mutex);
1438
1439 /*
1440 * If an internal scan is in progress, just set
1441 * up the scan_request as per above.
1442 */
1443 if (priv->scan_type != IWL_SCAN_NORMAL) {
1444 IWL_DEBUG_SCAN(priv,
1445 "SCAN request during internal scan - defer\n");
1446 priv->scan_request = req;
1447 priv->scan_vif = vif;
1448 ret = 0;
1449 } else {
1450 priv->scan_request = req;
1451 priv->scan_vif = vif;
1452 /*
1453 * mac80211 will only ask for one band at a time
1454 * so using channels[0] here is ok
1455 */
1456 ret = iwl_scan_initiate(priv, vif, IWL_SCAN_NORMAL,
1457 req->channels[0]->band);
1458 if (ret) {
1459 priv->scan_request = NULL;
1460 priv->scan_vif = NULL;
1461 }
1462 }
1463
1464 IWL_DEBUG_MAC80211(priv, "leave\n");
1465
1466 mutex_unlock(&priv->shrd->mutex);
1467
1468 return ret;
1469}
1470
Wey-Yi Guy76b29332011-11-10 06:55:16 -08001471static int iwlagn_mac_sta_remove(struct ieee80211_hw *hw,
1472 struct ieee80211_vif *vif,
1473 struct ieee80211_sta *sta)
1474{
1475 struct iwl_priv *priv = hw->priv;
1476 struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
1477 int ret;
1478
1479 IWL_DEBUG_MAC80211(priv, "enter: received request to remove "
1480 "station %pM\n", sta->addr);
1481 mutex_lock(&priv->shrd->mutex);
1482 IWL_DEBUG_INFO(priv, "proceeding to remove station %pM\n",
1483 sta->addr);
1484 ret = iwl_remove_station(priv, sta_priv->sta_id, sta->addr);
1485 if (ret)
1486 IWL_DEBUG_QUIET_RFKILL(priv, "Error removing station %pM\n",
1487 sta->addr);
1488 mutex_unlock(&priv->shrd->mutex);
1489 IWL_DEBUG_MAC80211(priv, "leave\n");
1490
1491 return ret;
1492}
1493
1494static void iwl_sta_modify_ps_wake(struct iwl_priv *priv, int sta_id)
1495{
1496 unsigned long flags;
1497
1498 spin_lock_irqsave(&priv->shrd->sta_lock, flags);
1499 priv->stations[sta_id].sta.station_flags &= ~STA_FLG_PWR_SAVE_MSK;
1500 priv->stations[sta_id].sta.station_flags_msk = STA_FLG_PWR_SAVE_MSK;
1501 priv->stations[sta_id].sta.sta.modify_mask = 0;
1502 priv->stations[sta_id].sta.sleep_tx_count = 0;
1503 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1504 iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
1505 spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
1506
1507}
1508
1509static void iwlagn_mac_sta_notify(struct ieee80211_hw *hw,
1510 struct ieee80211_vif *vif,
1511 enum sta_notify_cmd cmd,
1512 struct ieee80211_sta *sta)
1513{
1514 struct iwl_priv *priv = hw->priv;
1515 struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
1516 int sta_id;
1517
1518 IWL_DEBUG_MAC80211(priv, "enter\n");
1519
1520 switch (cmd) {
1521 case STA_NOTIFY_SLEEP:
1522 WARN_ON(!sta_priv->client);
1523 sta_priv->asleep = true;
1524 if (atomic_read(&sta_priv->pending_frames) > 0)
1525 ieee80211_sta_block_awake(hw, sta, true);
1526 break;
1527 case STA_NOTIFY_AWAKE:
1528 WARN_ON(!sta_priv->client);
1529 if (!sta_priv->asleep)
1530 break;
1531 sta_priv->asleep = false;
1532 sta_id = iwl_sta_id(sta);
1533 if (sta_id != IWL_INVALID_STATION)
1534 iwl_sta_modify_ps_wake(priv, sta_id);
1535 break;
1536 default:
1537 break;
1538 }
1539 IWL_DEBUG_MAC80211(priv, "leave\n");
1540}
1541
Wey-Yi Guy73356132011-11-10 06:55:11 -08001542struct ieee80211_ops iwlagn_hw_ops = {
1543 .tx = iwlagn_mac_tx,
1544 .start = iwlagn_mac_start,
1545 .stop = iwlagn_mac_stop,
1546#ifdef CONFIG_PM_SLEEP
1547 .suspend = iwlagn_mac_suspend,
1548 .resume = iwlagn_mac_resume,
1549#endif
1550 .add_interface = iwlagn_mac_add_interface,
1551 .remove_interface = iwlagn_mac_remove_interface,
1552 .change_interface = iwlagn_mac_change_interface,
1553 .config = iwlagn_mac_config,
1554 .configure_filter = iwlagn_configure_filter,
1555 .set_key = iwlagn_mac_set_key,
1556 .update_tkip_key = iwlagn_mac_update_tkip_key,
1557 .set_rekey_data = iwlagn_mac_set_rekey_data,
1558 .conf_tx = iwlagn_mac_conf_tx,
1559 .bss_info_changed = iwlagn_bss_info_changed,
1560 .ampdu_action = iwlagn_mac_ampdu_action,
1561 .hw_scan = iwlagn_mac_hw_scan,
1562 .sta_notify = iwlagn_mac_sta_notify,
1563 .sta_add = iwlagn_mac_sta_add,
1564 .sta_remove = iwlagn_mac_sta_remove,
1565 .channel_switch = iwlagn_mac_channel_switch,
1566 .flush = iwlagn_mac_flush,
1567 .tx_last_beacon = iwlagn_mac_tx_last_beacon,
1568 .remain_on_channel = iwlagn_mac_remain_on_channel,
1569 .cancel_remain_on_channel = iwlagn_mac_cancel_remain_on_channel,
1570 .rssi_callback = iwlagn_mac_rssi_callback,
1571 CFG80211_TESTMODE_CMD(iwlagn_mac_testmode_cmd)
1572 CFG80211_TESTMODE_DUMP(iwlagn_mac_testmode_dump)
1573 .tx_sync = iwlagn_mac_tx_sync,
1574 .finish_tx_sync = iwlagn_mac_finish_tx_sync,
1575 .set_tim = iwlagn_mac_set_tim,
1576};
1577
1578/* This function both allocates and initializes hw and priv. */
1579struct ieee80211_hw *iwl_alloc_all(void)
1580{
1581 struct iwl_priv *priv;
1582 /* mac80211 allocates memory for this device instance, including
1583 * space for this driver's private structure */
1584 struct ieee80211_hw *hw;
1585
1586 hw = ieee80211_alloc_hw(sizeof(struct iwl_priv), &iwlagn_hw_ops);
1587 if (!hw)
1588 goto out;
1589
1590 priv = hw->priv;
1591 priv->hw = hw;
1592
1593out:
1594 return hw;
1595}