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