mac80211.c 42.6 KB
Newer Older
1 2
/******************************************************************************
 *
W
Wey-Yi Guy 已提交
3
 * Copyright(c) 2003 - 2012 Intel Corporation. All rights reserved.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
 *
 * Portions of this file are derived from the ipw3945 project, as well
 * as portions of the ieee80211 subsystem header files.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of version 2 of the GNU General Public License as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
 *
 * The full GNU General Public License is included in this distribution in the
 * file called LICENSE.
 *
 * Contact Information:
 *  Intel Linux Wireless <ilw@linux.intel.com>
 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
 *
 *****************************************************************************/
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/dma-mapping.h>
#include <linux/delay.h>
#include <linux/sched.h>
#include <linux/skbuff.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/if_arp.h>

41
#include <net/ieee80211_radiotap.h>
42 43 44 45 46 47
#include <net/mac80211.h>

#include <asm/div64.h>

#include "iwl-io.h"
#include "iwl-trans.h"
48
#include "iwl-op-mode.h"
49
#include "iwl-modparams.h"
50

51 52 53 54
#include "dev.h"
#include "calib.h"
#include "agn.h"

55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
/*****************************************************************************
 *
 * mac80211 entry point functions
 *
 *****************************************************************************/

static const struct ieee80211_iface_limit iwlagn_sta_ap_limits[] = {
	{
		.max = 1,
		.types = BIT(NL80211_IFTYPE_STATION),
	},
	{
		.max = 1,
		.types = BIT(NL80211_IFTYPE_AP),
	},
};

static const struct ieee80211_iface_limit iwlagn_2sta_limits[] = {
	{
		.max = 2,
		.types = BIT(NL80211_IFTYPE_STATION),
	},
};

static const struct ieee80211_iface_limit iwlagn_p2p_sta_go_limits[] = {
	{
		.max = 1,
		.types = BIT(NL80211_IFTYPE_STATION),
	},
	{
		.max = 1,
		.types = BIT(NL80211_IFTYPE_P2P_GO) |
			 BIT(NL80211_IFTYPE_AP),
	},
};

static const struct ieee80211_iface_limit iwlagn_p2p_2sta_limits[] = {
	{
		.max = 2,
		.types = BIT(NL80211_IFTYPE_STATION),
	},
	{
		.max = 1,
		.types = BIT(NL80211_IFTYPE_P2P_CLIENT),
	},
};

static const struct ieee80211_iface_combination
iwlagn_iface_combinations_dualmode[] = {
	{ .num_different_channels = 1,
	  .max_interfaces = 2,
	  .beacon_int_infra_match = true,
	  .limits = iwlagn_sta_ap_limits,
	  .n_limits = ARRAY_SIZE(iwlagn_sta_ap_limits),
	},
	{ .num_different_channels = 1,
	  .max_interfaces = 2,
	  .limits = iwlagn_2sta_limits,
	  .n_limits = ARRAY_SIZE(iwlagn_2sta_limits),
	},
};

static const struct ieee80211_iface_combination
iwlagn_iface_combinations_p2p[] = {
	{ .num_different_channels = 1,
	  .max_interfaces = 2,
	  .beacon_int_infra_match = true,
	  .limits = iwlagn_p2p_sta_go_limits,
	  .n_limits = ARRAY_SIZE(iwlagn_p2p_sta_go_limits),
	},
	{ .num_different_channels = 1,
	  .max_interfaces = 2,
	  .limits = iwlagn_p2p_2sta_limits,
	  .n_limits = ARRAY_SIZE(iwlagn_p2p_2sta_limits),
	},
};

/*
 * Not a mac80211 entry point function, but it fits in with all the
 * other mac80211 functions grouped here.
 */
int iwlagn_mac_setup_register(struct iwl_priv *priv,
137
			      const struct iwl_ucode_capabilities *capa)
138 139 140 141 142 143 144 145 146 147 148 149
{
	int ret;
	struct ieee80211_hw *hw = priv->hw;
	struct iwl_rxon_context *ctx;

	hw->rate_control_algorithm = "iwl-agn-rs";

	/* Tell mac80211 our characteristics */
	hw->flags = IEEE80211_HW_SIGNAL_DBM |
		    IEEE80211_HW_AMPDU_AGGREGATION |
		    IEEE80211_HW_NEED_DTIM_PERIOD |
		    IEEE80211_HW_SPECTRUM_MGMT |
150 151 152 153
		    IEEE80211_HW_REPORTS_TX_ACK_STATUS |
		    IEEE80211_HW_QUEUE_CONTROL |
		    IEEE80211_HW_SUPPORTS_PS |
		    IEEE80211_HW_SUPPORTS_DYNAMIC_PS |
154
		    IEEE80211_HW_WANT_MONITOR_VIF |
155 156 157
		    IEEE80211_HW_SCAN_WHILE_IDLE;

	hw->offchannel_tx_hw_queue = IWL_AUX_QUEUE;
158
	hw->radiotap_mcs_details |= IEEE80211_RADIOTAP_MCS_HAVE_FMT;
159 160 161 162 163 164 165 166

	/*
	 * Including the following line will crash some AP's.  This
	 * workaround removes the stimulus which causes the crash until
	 * the AP software can be fixed.
	hw->max_tx_aggregation_subframes = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
	 */

167
	if (priv->nvm_data->sku_cap_11n_enable)
168 169 170
		hw->flags |= IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS |
			     IEEE80211_HW_SUPPORTS_STATIC_SMPS;

171 172 173 174 175 176 177
	/*
	 * Enable 11w if advertised by firmware and software crypto
	 * is not enabled (as the firmware will interpret some mgmt
	 * packets, so enabling it with software crypto isn't safe)
	 */
	if (priv->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_MFP &&
	    !iwlwifi_mod_params.sw_crypto)
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
		hw->flags |= IEEE80211_HW_MFP_CAPABLE;

	hw->sta_data_size = sizeof(struct iwl_station_priv);
	hw->vif_data_size = sizeof(struct iwl_vif_priv);

	for_each_context(priv, ctx) {
		hw->wiphy->interface_modes |= ctx->interface_modes;
		hw->wiphy->interface_modes |= ctx->exclusive_interface_modes;
	}

	BUILD_BUG_ON(NUM_IWL_RXON_CTX != 2);

	if (hw->wiphy->interface_modes & BIT(NL80211_IFTYPE_P2P_CLIENT)) {
		hw->wiphy->iface_combinations = iwlagn_iface_combinations_p2p;
		hw->wiphy->n_iface_combinations =
			ARRAY_SIZE(iwlagn_iface_combinations_p2p);
	} else if (hw->wiphy->interface_modes & BIT(NL80211_IFTYPE_AP)) {
		hw->wiphy->iface_combinations =
			iwlagn_iface_combinations_dualmode;
		hw->wiphy->n_iface_combinations =
			ARRAY_SIZE(iwlagn_iface_combinations_dualmode);
	}

201
	hw->wiphy->max_remain_on_channel_duration = 500;
202 203 204 205 206

	hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY |
			    WIPHY_FLAG_DISABLE_BEACON_HINTS |
			    WIPHY_FLAG_IBSS_RSN;

207
#ifdef CONFIG_PM_SLEEP
D
David Spinadel 已提交
208
	if (priv->fw->img[IWL_UCODE_WOWLAN].sec[0].len &&
209 210
	    priv->trans->ops->wowlan_suspend &&
	    device_can_wakeup(priv->trans->dev)) {
211 212 213 214
		hw->wiphy->wowlan.flags = WIPHY_WOWLAN_MAGIC_PKT |
					  WIPHY_WOWLAN_DISCONNECT |
					  WIPHY_WOWLAN_EAP_IDENTITY_REQ |
					  WIPHY_WOWLAN_RFKILL_RELEASE;
215
		if (!iwlwifi_mod_params.sw_crypto)
216 217 218 219 220 221 222 223 224 225
			hw->wiphy->wowlan.flags |=
				WIPHY_WOWLAN_SUPPORTS_GTK_REKEY |
				WIPHY_WOWLAN_GTK_REKEY_FAILURE;

		hw->wiphy->wowlan.n_patterns = IWLAGN_WOWLAN_MAX_PATTERNS;
		hw->wiphy->wowlan.pattern_min_len =
					IWLAGN_WOWLAN_MIN_PATTERN_LEN;
		hw->wiphy->wowlan.pattern_max_len =
					IWLAGN_WOWLAN_MAX_PATTERN_LEN;
	}
226
#endif
227

228
	if (iwlwifi_mod_params.power_save)
229 230 231 232 233
		hw->wiphy->flags |= WIPHY_FLAG_PS_ON_BY_DEFAULT;
	else
		hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;

	hw->wiphy->max_scan_ssids = PROBE_OPTION_MAX;
234 235
	/* we create the 802.11 header and a max-length SSID element */
	hw->wiphy->max_scan_ie_len = capa->max_probe_length - 24 - 34;
236

237 238 239 240 241
	/*
	 * We don't use all queues: 4 and 9 are unused and any
	 * aggregation queue gets mapped down to the AC queue.
	 */
	hw->queues = IWLAGN_FIRST_AMPDU_QUEUE;
242 243 244

	hw->max_listen_interval = IWL_CONN_MAX_LISTEN_INTERVAL;

245
	if (priv->nvm_data->bands[IEEE80211_BAND_2GHZ].n_channels)
246
		priv->hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
247 248
			&priv->nvm_data->bands[IEEE80211_BAND_2GHZ];
	if (priv->nvm_data->bands[IEEE80211_BAND_5GHZ].n_channels)
249
		priv->hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
250
			&priv->nvm_data->bands[IEEE80211_BAND_5GHZ];
251

252
	hw->wiphy->hw_version = priv->trans->hw_id;
W
Wey-Yi Guy 已提交
253

254 255 256 257 258
	iwl_leds_init(priv);

	ret = ieee80211_register_hw(priv->hw);
	if (ret) {
		IWL_ERR(priv, "Failed to register hw (error %d)\n", ret);
259
		iwl_leds_exit(priv);
260 261 262 263 264 265 266
		return ret;
	}
	priv->mac80211_registered = 1;

	return 0;
}

267 268 269 270 271 272 273 274 275
void iwlagn_mac_unregister(struct iwl_priv *priv)
{
	if (!priv->mac80211_registered)
		return;
	iwl_leds_exit(priv);
	ieee80211_unregister_hw(priv->hw);
	priv->mac80211_registered = 0;
}

276 277 278 279 280
static int __iwl_up(struct iwl_priv *priv)
{
	struct iwl_rxon_context *ctx;
	int ret;

281
	lockdep_assert_held(&priv->mutex);
282

D
Don Fry 已提交
283
	if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
284 285 286 287 288 289 290 291 292 293 294 295
		IWL_WARN(priv, "Exit pending; will not bring the NIC up\n");
		return -EIO;
	}

	for_each_context(priv, ctx) {
		ret = iwlagn_alloc_bcast_station(priv, ctx);
		if (ret) {
			iwl_dealloc_bcast_stations(priv);
			return ret;
		}
	}

296
	ret = iwl_run_init_ucode(priv);
297 298 299 300 301
	if (ret) {
		IWL_ERR(priv, "Failed to run INIT ucode: %d\n", ret);
		goto error;
	}

302
	ret = iwl_load_ucode_wait_alive(priv, IWL_UCODE_REGULAR);
303 304 305 306 307 308 309 310 311 312 313
	if (ret) {
		IWL_ERR(priv, "Failed to start RT ucode: %d\n", ret);
		goto error;
	}

	ret = iwl_alive_start(priv);
	if (ret)
		goto error;
	return 0;

 error:
D
Don Fry 已提交
314
	set_bit(STATUS_EXIT_PENDING, &priv->status);
315
	iwl_down(priv);
D
Don Fry 已提交
316
	clear_bit(STATUS_EXIT_PENDING, &priv->status);
317 318 319 320 321 322 323

	IWL_ERR(priv, "Unable to initialize device.\n");
	return ret;
}

static int iwlagn_mac_start(struct ieee80211_hw *hw)
{
324
	struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
325 326 327 328 329
	int ret;

	IWL_DEBUG_MAC80211(priv, "enter\n");

	/* we should be verifying the device is ready to be opened */
330
	mutex_lock(&priv->mutex);
331
	ret = __iwl_up(priv);
332
	mutex_unlock(&priv->mutex);
333 334 335 336 337 338
	if (ret)
		return ret;

	IWL_DEBUG_INFO(priv, "Start UP work done.\n");

	/* Now we should be done, and the READY bit should be set. */
D
Don Fry 已提交
339
	if (WARN_ON(!test_bit(STATUS_READY, &priv->status)))
340 341 342 343 344 345 346 347 348
		ret = -EIO;

	iwlagn_led_enable(priv);

	priv->is_open = 1;
	IWL_DEBUG_MAC80211(priv, "leave\n");
	return 0;
}

349
static void iwlagn_mac_stop(struct ieee80211_hw *hw)
350
{
351
	struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
352 353 354 355 356 357 358 359

	IWL_DEBUG_MAC80211(priv, "enter\n");

	if (!priv->is_open)
		return;

	priv->is_open = 0;

360
	mutex_lock(&priv->mutex);
361
	iwl_down(priv);
362
	mutex_unlock(&priv->mutex);
363 364

	iwl_cancel_deferred_work(priv);
365

J
Johannes Berg 已提交
366
	flush_workqueue(priv->workqueue);
367 368

	/* User space software may expect getting rfkill changes
369 370 371
	 * even if interface is down, trans->down will leave the RF
	 * kill interrupt enabled
	 */
372
	iwl_trans_stop_hw(priv->trans, false);
373 374 375 376

	IWL_DEBUG_MAC80211(priv, "leave\n");
}

377 378 379
static void iwlagn_mac_set_rekey_data(struct ieee80211_hw *hw,
				      struct ieee80211_vif *vif,
				      struct cfg80211_gtk_rekey_data *data)
380
{
381
	struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
382

383
	if (iwlwifi_mod_params.sw_crypto)
384 385 386
		return;

	IWL_DEBUG_MAC80211(priv, "enter\n");
387
	mutex_lock(&priv->mutex);
388 389 390 391 392 393 394 395 396 397 398

	if (priv->contexts[IWL_RXON_CTX_BSS].vif != vif)
		goto out;

	memcpy(priv->kek, data->kek, NL80211_KEK_LEN);
	memcpy(priv->kck, data->kck, NL80211_KCK_LEN);
	priv->replay_ctr =
		cpu_to_le64(be64_to_cpup((__be64 *)&data->replay_ctr));
	priv->have_rekey_data = true;

 out:
399
	mutex_unlock(&priv->mutex);
400 401 402 403 404
	IWL_DEBUG_MAC80211(priv, "leave\n");
}

#ifdef CONFIG_PM_SLEEP

405 406
static int iwlagn_mac_suspend(struct ieee80211_hw *hw,
			      struct cfg80211_wowlan *wowlan)
407
{
408
	struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
409
	struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
410
	int ret;
411 412 413 414 415

	if (WARN_ON(!wowlan))
		return -EINVAL;

	IWL_DEBUG_MAC80211(priv, "enter\n");
416
	mutex_lock(&priv->mutex);
417 418 419 420 421 422 423 424

	/* Don't attempt WoWLAN when not associated, tear down instead. */
	if (!ctx->vif || ctx->vif->type != NL80211_IFTYPE_STATION ||
	    !iwl_is_associated_ctx(ctx)) {
		ret = 1;
		goto out;
	}

425
	ret = iwlagn_suspend(priv, wowlan);
426 427 428
	if (ret)
		goto error;

429
	iwl_trans_wowlan_suspend(priv->trans);
430 431 432 433

	goto out;

 error:
434
	priv->wowlan = false;
435 436 437
	iwlagn_prepare_restart(priv);
	ieee80211_restart_hw(priv->hw);
 out:
438
	mutex_unlock(&priv->mutex);
439 440 441 442 443 444 445
	IWL_DEBUG_MAC80211(priv, "leave\n");

	return ret;
}

static int iwlagn_mac_resume(struct ieee80211_hw *hw)
{
446
	struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
447 448 449 450 451 452 453
	struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
	struct ieee80211_vif *vif;
	unsigned long flags;
	u32 base, status = 0xffffffff;
	int ret = -EIO;

	IWL_DEBUG_MAC80211(priv, "enter\n");
454
	mutex_lock(&priv->mutex);
455

456
	iwl_write32(priv->trans, CSR_UCODE_DRV_GP1_CLR,
457 458
			  CSR_UCODE_DRV_GP1_BIT_D3_CFG_COMPLETE);

459
	base = priv->device_pointers.error_event_table;
460
	if (iwlagn_hw_valid_rtc_data_addr(base)) {
461
		spin_lock_irqsave(&priv->trans->reg_lock, flags);
462
		ret = iwl_trans_grab_nic_access(priv->trans, true);
463
		if (likely(ret == 0)) {
464 465
			iwl_write32(priv->trans, HBUS_TARG_MEM_RADDR, base);
			status = iwl_read32(priv->trans, HBUS_TARG_MEM_RDAT);
466
			iwl_trans_release_nic_access(priv->trans);
467
		}
468
		spin_unlock_irqrestore(&priv->trans->reg_lock, flags);
469 470 471

#ifdef CONFIG_IWLWIFI_DEBUGFS
		if (ret == 0) {
472 473
			const struct fw_img *img;

D
David Spinadel 已提交
474 475
			img = &(priv->fw->img[IWL_UCODE_WOWLAN]);
			if (!priv->wowlan_sram) {
476
				priv->wowlan_sram =
D
David Spinadel 已提交
477
				   kzalloc(img->sec[IWL_UCODE_SECTION_DATA].len,
478
						GFP_KERNEL);
D
David Spinadel 已提交
479
			}
480 481

			if (priv->wowlan_sram)
482
				_iwl_read_targ_mem_dwords(
483
				      priv->trans, 0x800000,
D
David Spinadel 已提交
484 485
				      priv->wowlan_sram,
				      img->sec[IWL_UCODE_SECTION_DATA].len / 4);
486 487 488 489 490 491 492
		}
#endif
	}

	/* we'll clear ctx->vif during iwlagn_prepare_restart() */
	vif = ctx->vif;

493
	priv->wowlan = false;
494 495 496 497 498 499 500

	iwlagn_prepare_restart(priv);

	memset((void *)&ctx->active, 0, sizeof(ctx->active));
	iwl_connection_init_rx_config(priv, ctx);
	iwlagn_set_rxon_chain(priv, ctx);

501
	mutex_unlock(&priv->mutex);
502 503 504 505 506 507 508
	IWL_DEBUG_MAC80211(priv, "leave\n");

	ieee80211_resume_disconnect(vif);

	return 1;
}

509 510 511 512 513 514
static void iwlagn_mac_set_wakeup(struct ieee80211_hw *hw, bool enabled)
{
	struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);

	device_set_wakeup_enable(priv->trans->dev, enabled);
}
515 516
#endif

517 518 519
static void iwlagn_mac_tx(struct ieee80211_hw *hw,
			  struct ieee80211_tx_control *control,
			  struct sk_buff *skb)
520
{
521
	struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
522 523 524 525

	IWL_DEBUG_TX(priv, "dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
		     ieee80211_get_tx_rate(hw, IEEE80211_SKB_CB(skb))->bitrate);

526
	if (iwlagn_tx_skb(priv, control->sta, skb))
J
Johannes Berg 已提交
527
		ieee80211_free_txskb(hw, skb);
528 529
}

530 531 532 533 534
static void iwlagn_mac_update_tkip_key(struct ieee80211_hw *hw,
				       struct ieee80211_vif *vif,
				       struct ieee80211_key_conf *keyconf,
				       struct ieee80211_sta *sta,
				       u32 iv32, u16 *phase1key)
535
{
536
	struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
537 538 539 540

	iwl_update_tkip_key(priv, vif, keyconf, sta, iv32, phase1key);
}

541 542 543 544
static int iwlagn_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
			      struct ieee80211_vif *vif,
			      struct ieee80211_sta *sta,
			      struct ieee80211_key_conf *key)
545
{
546
	struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
547 548 549 550 551 552 553
	struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
	struct iwl_rxon_context *ctx = vif_priv->ctx;
	int ret;
	bool is_default_wep_key = false;

	IWL_DEBUG_MAC80211(priv, "enter\n");

554
	if (iwlwifi_mod_params.sw_crypto) {
555 556 557 558
		IWL_DEBUG_MAC80211(priv, "leave - hwcrypto disabled\n");
		return -EOPNOTSUPP;
	}

559 560 561 562 563 564 565 566 567 568 569
	switch (key->cipher) {
	case WLAN_CIPHER_SUITE_TKIP:
		key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
		/* fall through */
	case WLAN_CIPHER_SUITE_CCMP:
		key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
		break;
	default:
		break;
	}

570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586
	/*
	 * We could program these keys into the hardware as well, but we
	 * don't expect much multicast traffic in IBSS and having keys
	 * for more stations is probably more useful.
	 *
	 * Mark key TX-only and return 0.
	 */
	if (vif->type == NL80211_IFTYPE_ADHOC &&
	    !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
		key->hw_key_idx = WEP_INVALID_OFFSET;
		return 0;
	}

	/* If they key was TX-only, accept deletion */
	if (cmd == DISABLE_KEY && key->hw_key_idx == WEP_INVALID_OFFSET)
		return 0;

587
	mutex_lock(&priv->mutex);
588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637
	iwl_scan_cancel_timeout(priv, 100);

	BUILD_BUG_ON(WEP_INVALID_OFFSET == IWLAGN_HW_KEY_DEFAULT);

	/*
	 * If we are getting WEP group key and we didn't receive any key mapping
	 * so far, we are in legacy wep mode (group key only), otherwise we are
	 * in 1X mode.
	 * In legacy wep mode, we use another host command to the uCode.
	 */
	if ((key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
	     key->cipher == WLAN_CIPHER_SUITE_WEP104) && !sta) {
		if (cmd == SET_KEY)
			is_default_wep_key = !ctx->key_mapping_keys;
		else
			is_default_wep_key =
				key->hw_key_idx == IWLAGN_HW_KEY_DEFAULT;
	}


	switch (cmd) {
	case SET_KEY:
		if (is_default_wep_key) {
			ret = iwl_set_default_wep_key(priv, vif_priv->ctx, key);
			break;
		}
		ret = iwl_set_dynamic_key(priv, vif_priv->ctx, key, sta);
		if (ret) {
			/*
			 * can't add key for RX, but we don't need it
			 * in the device for TX so still return 0
			 */
			ret = 0;
			key->hw_key_idx = WEP_INVALID_OFFSET;
		}

		IWL_DEBUG_MAC80211(priv, "enable hwcrypto key\n");
		break;
	case DISABLE_KEY:
		if (is_default_wep_key)
			ret = iwl_remove_default_wep_key(priv, ctx, key);
		else
			ret = iwl_remove_dynamic_key(priv, ctx, key, sta);

		IWL_DEBUG_MAC80211(priv, "disable hwcrypto key\n");
		break;
	default:
		ret = -EINVAL;
	}

638
	mutex_unlock(&priv->mutex);
639 640 641 642 643
	IWL_DEBUG_MAC80211(priv, "leave\n");

	return ret;
}

644 645 646 647 648
static int iwlagn_mac_ampdu_action(struct ieee80211_hw *hw,
				   struct ieee80211_vif *vif,
				   enum ieee80211_ampdu_mlme_action action,
				   struct ieee80211_sta *sta, u16 tid, u16 *ssn,
				   u8 buf_size)
649
{
650
	struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
651 652 653 654 655 656
	int ret = -EINVAL;
	struct iwl_station_priv *sta_priv = (void *) sta->drv_priv;

	IWL_DEBUG_HT(priv, "A-MPDU action on addr %pM tid %d\n",
		     sta->addr, tid);

657
	if (!(priv->nvm_data->sku_cap_11n_enable))
658 659 660
		return -EACCES;

	IWL_DEBUG_MAC80211(priv, "enter\n");
661
	mutex_lock(&priv->mutex);
662 663 664

	switch (action) {
	case IEEE80211_AMPDU_RX_START:
665
		if (iwlwifi_mod_params.disable_11n & IWL_DISABLE_HT_RXAGG)
J
Johannes Berg 已提交
666
			break;
667 668 669 670 671 672 673 674
		IWL_DEBUG_HT(priv, "start Rx\n");
		ret = iwl_sta_rx_agg_start(priv, sta, tid, *ssn);
		break;
	case IEEE80211_AMPDU_RX_STOP:
		IWL_DEBUG_HT(priv, "stop Rx\n");
		ret = iwl_sta_rx_agg_stop(priv, sta, tid);
		break;
	case IEEE80211_AMPDU_TX_START:
675
		if (!priv->trans->ops->txq_enable)
676
			break;
677
		if (iwlwifi_mod_params.disable_11n & IWL_DISABLE_HT_TXAGG)
J
Johannes Berg 已提交
678
			break;
679 680 681 682 683 684 685 686 687 688 689
		IWL_DEBUG_HT(priv, "start Tx\n");
		ret = iwlagn_tx_agg_start(priv, vif, sta, tid, ssn);
		break;
	case IEEE80211_AMPDU_TX_STOP:
		IWL_DEBUG_HT(priv, "stop Tx\n");
		ret = iwlagn_tx_agg_stop(priv, vif, sta, tid);
		if ((ret == 0) && (priv->agg_tids_count > 0)) {
			priv->agg_tids_count--;
			IWL_DEBUG_HT(priv, "priv->agg_tids_count = %u\n",
				     priv->agg_tids_count);
		}
690
		if (!priv->agg_tids_count &&
691
		    priv->hw_params.use_rts_for_aggregation) {
692 693 694 695 696 697 698 699 700 701
			/*
			 * switch off RTS/CTS if it was previously enabled
			 */
			sta_priv->lq_sta.lq.general_params.flags &=
				~LINK_QUAL_FLAGS_SET_STA_TLC_RTS_MSK;
			iwl_send_lq_cmd(priv, iwl_rxon_ctx_from_vif(vif),
					&sta_priv->lq_sta.lq, CMD_ASYNC, false);
		}
		break;
	case IEEE80211_AMPDU_TX_OPERATIONAL:
702
		ret = iwlagn_tx_agg_oper(priv, vif, sta, tid, buf_size);
703 704
		break;
	}
705
	mutex_unlock(&priv->mutex);
706 707 708 709 710 711 712 713
	IWL_DEBUG_MAC80211(priv, "leave\n");
	return ret;
}

static int iwlagn_mac_sta_add(struct ieee80211_hw *hw,
			      struct ieee80211_vif *vif,
			      struct ieee80211_sta *sta)
{
714
	struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
715 716 717
	struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
	struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
	bool is_ap = vif->type == NL80211_IFTYPE_STATION;
718
	int ret;
719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734
	u8 sta_id;

	IWL_DEBUG_INFO(priv, "proceeding to add station %pM\n",
			sta->addr);
	sta_priv->sta_id = IWL_INVALID_STATION;

	atomic_set(&sta_priv->pending_frames, 0);
	if (vif->type == NL80211_IFTYPE_AP)
		sta_priv->client = true;

	ret = iwl_add_station_common(priv, vif_priv->ctx, sta->addr,
				     is_ap, sta, &sta_id);
	if (ret) {
		IWL_ERR(priv, "Unable to add station %pM (%d)\n",
			sta->addr, ret);
		/* Should we return success if return code is EEXIST ? */
735
		return ret;
736 737 738 739
	}

	sta_priv->sta_id = sta_id;

740 741 742 743 744 745 746 747 748 749 750
	return 0;
}

static int iwlagn_mac_sta_remove(struct ieee80211_hw *hw,
				 struct ieee80211_vif *vif,
				 struct ieee80211_sta *sta)
{
	struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
	struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
	int ret;

751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766
	IWL_DEBUG_INFO(priv, "proceeding to remove station %pM\n", sta->addr);

	if (vif->type == NL80211_IFTYPE_STATION) {
		/*
		 * Station will be removed from device when the RXON
		 * is set to unassociated -- just deactivate it here
		 * to avoid re-programming it.
		 */
		ret = 0;
		iwl_deactivate_station(priv, sta_priv->sta_id, sta->addr);
	} else {
		ret = iwl_remove_station(priv, sta_priv->sta_id, sta->addr);
		if (ret)
			IWL_DEBUG_QUIET_RFKILL(priv,
				"Error removing station %pM\n", sta->addr);
	}
767 768 769
	return ret;
}

770 771 772 773 774
static int iwlagn_mac_sta_state(struct ieee80211_hw *hw,
				struct ieee80211_vif *vif,
				struct ieee80211_sta *sta,
				enum ieee80211_sta_state old_state,
				enum ieee80211_sta_state new_state)
775 776
{
	struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
777
	struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
778
	enum {
779
		NONE, ADD, REMOVE, HT_RATE_INIT, ADD_RATE_INIT,
780 781 782 783 784 785
	} op = NONE;
	int ret;

	IWL_DEBUG_MAC80211(priv, "station %pM state change %d->%d\n",
			   sta->addr, old_state, new_state);

786
	mutex_lock(&priv->mutex);
787 788 789 790 791 792 793 794 795
	if (vif->type == NL80211_IFTYPE_STATION) {
		if (old_state == IEEE80211_STA_NOTEXIST &&
		    new_state == IEEE80211_STA_NONE)
			op = ADD;
		else if (old_state == IEEE80211_STA_NONE &&
			 new_state == IEEE80211_STA_NOTEXIST)
			op = REMOVE;
		else if (old_state == IEEE80211_STA_AUTH &&
			 new_state == IEEE80211_STA_ASSOC)
796
			op = HT_RATE_INIT;
797 798 799 800 801 802 803 804 805 806 807 808
	} else {
		if (old_state == IEEE80211_STA_AUTH &&
		    new_state == IEEE80211_STA_ASSOC)
			op = ADD_RATE_INIT;
		else if (old_state == IEEE80211_STA_ASSOC &&
			 new_state == IEEE80211_STA_AUTH)
			op = REMOVE;
	}

	switch (op) {
	case ADD:
		ret = iwlagn_mac_sta_add(hw, vif, sta);
809 810 811 812 813 814 815 816 817 818 819 820
		if (ret)
			break;
		/*
		 * Clear the in-progress flag, the AP station entry was added
		 * but we'll initialize LQ only when we've associated (which
		 * would also clear the in-progress flag). This is necessary
		 * in case we never initialize LQ because association fails.
		 */
		spin_lock_bh(&priv->sta_lock);
		priv->stations[iwl_sta_id(sta)].used &=
			~IWL_STA_UCODE_INPROGRESS;
		spin_unlock_bh(&priv->sta_lock);
821 822 823 824 825 826 827 828 829 830 831 832 833 834 835
		break;
	case REMOVE:
		ret = iwlagn_mac_sta_remove(hw, vif, sta);
		break;
	case ADD_RATE_INIT:
		ret = iwlagn_mac_sta_add(hw, vif, sta);
		if (ret)
			break;
		/* Initialize rate scaling */
		IWL_DEBUG_INFO(priv,
			       "Initializing rate scaling for station %pM\n",
			       sta->addr);
		iwl_rs_rate_init(priv, sta, iwl_sta_id(sta));
		ret = 0;
		break;
836 837 838 839 840 841 842 843 844 845 846
	case HT_RATE_INIT:
		/* Initialize rate scaling */
		ret = iwl_sta_update_ht(priv, vif_priv->ctx, sta);
		if (ret)
			break;
		IWL_DEBUG_INFO(priv,
			       "Initializing rate scaling for station %pM\n",
			       sta->addr);
		iwl_rs_rate_init(priv, sta, iwl_sta_id(sta));
		ret = 0;
		break;
847 848 849 850 851 852 853 854 855
	default:
		ret = 0;
		break;
	}

	/*
	 * mac80211 might WARN if we fail, but due the way we
	 * (badly) handle hard rfkill, we might fail here
	 */
D
Don Fry 已提交
856
	if (iwl_is_rfkill(priv))
857 858
		ret = 0;

859
	mutex_unlock(&priv->mutex);
860 861 862 863 864
	IWL_DEBUG_MAC80211(priv, "leave\n");

	return ret;
}

865 866
static void iwlagn_mac_channel_switch(struct ieee80211_hw *hw,
				      struct ieee80211_channel_switch *ch_switch)
867
{
868
	struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884
	struct ieee80211_conf *conf = &hw->conf;
	struct ieee80211_channel *channel = ch_switch->channel;
	struct iwl_ht_config *ht_conf = &priv->current_ht_config;
	/*
	 * MULTI-FIXME
	 * When we add support for multiple interfaces, we need to
	 * revisit this. The channel switch command in the device
	 * only affects the BSS context, but what does that really
	 * mean? And what if we get a CSA on the second interface?
	 * This needs a lot of work.
	 */
	struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
	u16 ch;

	IWL_DEBUG_MAC80211(priv, "enter\n");

885
	mutex_lock(&priv->mutex);
886

D
Don Fry 已提交
887
	if (iwl_is_rfkill(priv))
888 889
		goto out;

D
Don Fry 已提交
890 891 892
	if (test_bit(STATUS_EXIT_PENDING, &priv->status) ||
	    test_bit(STATUS_SCANNING, &priv->status) ||
	    test_bit(STATUS_CHANNEL_SWITCH_PENDING, &priv->status))
893 894 895 896 897
		goto out;

	if (!iwl_is_associated_ctx(ctx))
		goto out;

898
	if (!priv->lib->set_channel_switch)
899 900 901 902 903 904 905 906 907 908
		goto out;

	ch = channel->hw_value;
	if (le16_to_cpu(ctx->active.channel) == ch)
		goto out;

	priv->current_ht_config.smps = conf->smps_mode;

	/* Configure HT40 channels */
	ctx->ht.enabled = conf_is_ht(conf);
909 910 911
	if (ctx->ht.enabled)
		iwlagn_config_ht40(conf, ctx);
	else
912 913 914 915 916 917 918 919 920 921 922 923 924
		ctx->ht.is_40mhz = false;

	if ((le16_to_cpu(ctx->staging.channel) != ch))
		ctx->staging.flags = 0;

	iwl_set_rxon_channel(priv, channel, ctx);
	iwl_set_rxon_ht(priv, ht_conf);
	iwl_set_flags_for_band(priv, ctx, channel->band, ctx->vif);

	/*
	 * at this point, staging_rxon has the
	 * configuration for channel switch
	 */
D
Don Fry 已提交
925
	set_bit(STATUS_CHANNEL_SWITCH_PENDING, &priv->status);
926
	priv->switch_channel = cpu_to_le16(ch);
927
	if (priv->lib->set_channel_switch(priv, ch_switch)) {
D
Don Fry 已提交
928
		clear_bit(STATUS_CHANNEL_SWITCH_PENDING, &priv->status);
929 930 931 932 933
		priv->switch_channel = 0;
		ieee80211_chswitch_done(ctx->vif, false);
	}

out:
934
	mutex_unlock(&priv->mutex);
935 936 937
	IWL_DEBUG_MAC80211(priv, "leave\n");
}

938 939 940 941 942 943 944 945 946 947 948 949 950 951 952
void iwl_chswitch_done(struct iwl_priv *priv, bool is_success)
{
	/*
	 * MULTI-FIXME
	 * See iwlagn_mac_channel_switch.
	 */
	struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];

	if (test_bit(STATUS_EXIT_PENDING, &priv->status))
		return;

	if (test_and_clear_bit(STATUS_CHANNEL_SWITCH_PENDING, &priv->status))
		ieee80211_chswitch_done(ctx->vif, is_success);
}

953 954 955 956
static void iwlagn_configure_filter(struct ieee80211_hw *hw,
				    unsigned int changed_flags,
				    unsigned int *total_flags,
				    u64 multicast)
957
{
958
	struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978
	__le32 filter_or = 0, filter_nand = 0;
	struct iwl_rxon_context *ctx;

#define CHK(test, flag)	do { \
	if (*total_flags & (test))		\
		filter_or |= (flag);		\
	else					\
		filter_nand |= (flag);		\
	} while (0)

	IWL_DEBUG_MAC80211(priv, "Enter: changed: 0x%x, total: 0x%x\n",
			changed_flags, *total_flags);

	CHK(FIF_OTHER_BSS | FIF_PROMISC_IN_BSS, RXON_FILTER_PROMISC_MSK);
	/* Setting _just_ RXON_FILTER_CTL2HOST_MSK causes FH errors */
	CHK(FIF_CONTROL, RXON_FILTER_CTL2HOST_MSK | RXON_FILTER_PROMISC_MSK);
	CHK(FIF_BCN_PRBRESP_PROMISC, RXON_FILTER_BCON_AWARE_MSK);

#undef CHK

979
	mutex_lock(&priv->mutex);
980 981 982 983 984 985 986 987 988 989 990

	for_each_context(priv, ctx) {
		ctx->staging.filter_flags &= ~filter_nand;
		ctx->staging.filter_flags |= filter_or;

		/*
		 * Not committing directly because hardware can perform a scan,
		 * but we'll eventually commit the filter flags change anyway.
		 */
	}

991
	mutex_unlock(&priv->mutex);
992 993 994 995 996 997 998 999 1000 1001 1002

	/*
	 * Receiving all multicast frames is always enabled by the
	 * default flags setup in iwl_connection_init_rx_config()
	 * since we currently do not support programming multicast
	 * filters into the device.
	 */
	*total_flags &= FIF_OTHER_BSS | FIF_ALLMULTI | FIF_PROMISC_IN_BSS |
			FIF_BCN_PRBRESP_PROMISC | FIF_CONTROL;
}

1003
static void iwlagn_mac_flush(struct ieee80211_hw *hw, bool drop)
1004
{
1005
	struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
1006

1007
	mutex_lock(&priv->mutex);
1008 1009
	IWL_DEBUG_MAC80211(priv, "enter\n");

D
Don Fry 已提交
1010
	if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
1011 1012 1013
		IWL_DEBUG_TX(priv, "Aborting flush due to device shutdown\n");
		goto done;
	}
D
Don Fry 已提交
1014
	if (iwl_is_rfkill(priv)) {
1015 1016 1017 1018 1019 1020 1021 1022 1023 1024
		IWL_DEBUG_TX(priv, "Aborting flush due to RF Kill\n");
		goto done;
	}

	/*
	 * mac80211 will not push any more frames for transmit
	 * until the flush is completed
	 */
	if (drop) {
		IWL_DEBUG_MAC80211(priv, "send flush command\n");
J
Johannes Berg 已提交
1025
		if (iwlagn_txfifo_flush(priv)) {
1026 1027 1028 1029 1030
			IWL_ERR(priv, "flush request fail\n");
			goto done;
		}
	}
	IWL_DEBUG_MAC80211(priv, "wait transmit/flush all frames\n");
1031
	iwl_trans_wait_tx_queue_empty(priv->trans);
1032
done:
1033
	mutex_unlock(&priv->mutex);
1034 1035 1036 1037
	IWL_DEBUG_MAC80211(priv, "leave\n");
}

static int iwlagn_mac_remain_on_channel(struct ieee80211_hw *hw,
1038
				     struct ieee80211_vif *vif,
1039 1040 1041
				     struct ieee80211_channel *channel,
				     int duration)
{
1042
	struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
1043 1044 1045
	struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_PAN];
	int err = 0;

1046
	if (!(priv->valid_contexts & BIT(IWL_RXON_CTX_PAN)))
1047 1048 1049 1050 1051 1052
		return -EOPNOTSUPP;

	if (!(ctx->interface_modes & BIT(NL80211_IFTYPE_P2P_CLIENT)))
		return -EOPNOTSUPP;

	IWL_DEBUG_MAC80211(priv, "enter\n");
1053
	mutex_lock(&priv->mutex);
1054

D
Don Fry 已提交
1055
	if (test_bit(STATUS_SCAN_HW, &priv->status)) {
1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067
		/* mac80211 should not scan while ROC or ROC while scanning */
		if (WARN_ON_ONCE(priv->scan_type != IWL_SCAN_RADIO_RESET)) {
			err = -EBUSY;
			goto out;
		}

		iwl_scan_cancel_timeout(priv, 100);

		if (test_bit(STATUS_SCAN_HW, &priv->status)) {
			err = -EBUSY;
			goto out;
		}
1068 1069 1070
	}

	priv->hw_roc_channel = channel;
1071 1072
	/* convert from ms to TU */
	priv->hw_roc_duration = DIV_ROUND_UP(1000 * duration, 1024);
1073 1074 1075 1076
	priv->hw_roc_start_notified = false;
	cancel_delayed_work(&priv->hw_roc_disable_work);

	if (!ctx->is_active) {
1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103
		static const struct iwl_qos_info default_qos_data = {
			.def_qos_parm = {
				.ac[0] = {
					.cw_min = cpu_to_le16(3),
					.cw_max = cpu_to_le16(7),
					.aifsn = 2,
					.edca_txop = cpu_to_le16(1504),
				},
				.ac[1] = {
					.cw_min = cpu_to_le16(7),
					.cw_max = cpu_to_le16(15),
					.aifsn = 2,
					.edca_txop = cpu_to_le16(3008),
				},
				.ac[2] = {
					.cw_min = cpu_to_le16(15),
					.cw_max = cpu_to_le16(1023),
					.aifsn = 3,
				},
				.ac[3] = {
					.cw_min = cpu_to_le16(15),
					.cw_max = cpu_to_le16(1023),
					.aifsn = 7,
				},
			},
		};

1104
		ctx->is_active = true;
1105
		ctx->qos_data = default_qos_data;
1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132
		ctx->staging.dev_type = RXON_DEV_TYPE_P2P;
		memcpy(ctx->staging.node_addr,
		       priv->contexts[IWL_RXON_CTX_BSS].staging.node_addr,
		       ETH_ALEN);
		memcpy(ctx->staging.bssid_addr,
		       priv->contexts[IWL_RXON_CTX_BSS].staging.node_addr,
		       ETH_ALEN);
		err = iwlagn_commit_rxon(priv, ctx);
		if (err)
			goto out;
		ctx->staging.filter_flags |= RXON_FILTER_ASSOC_MSK |
					     RXON_FILTER_PROMISC_MSK |
					     RXON_FILTER_CTL2HOST_MSK;

		err = iwlagn_commit_rxon(priv, ctx);
		if (err) {
			iwlagn_disable_roc(priv);
			goto out;
		}
		priv->hw_roc_setup = true;
	}

	err = iwl_scan_initiate(priv, ctx->vif, IWL_SCAN_ROC, channel->band);
	if (err)
		iwlagn_disable_roc(priv);

 out:
1133
	mutex_unlock(&priv->mutex);
1134 1135 1136 1137 1138
	IWL_DEBUG_MAC80211(priv, "leave\n");

	return err;
}

1139
static int iwlagn_mac_cancel_remain_on_channel(struct ieee80211_hw *hw)
1140
{
1141
	struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
1142

1143
	if (!(priv->valid_contexts & BIT(IWL_RXON_CTX_PAN)))
1144 1145 1146
		return -EOPNOTSUPP;

	IWL_DEBUG_MAC80211(priv, "enter\n");
1147
	mutex_lock(&priv->mutex);
1148 1149
	iwl_scan_cancel_timeout(priv, priv->hw_roc_duration);
	iwlagn_disable_roc(priv);
1150
	mutex_unlock(&priv->mutex);
1151 1152 1153 1154 1155
	IWL_DEBUG_MAC80211(priv, "leave\n");

	return 0;
}

1156 1157
static void iwlagn_mac_rssi_callback(struct ieee80211_hw *hw,
				     enum ieee80211_rssi_event rssi_event)
1158
{
1159
	struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
1160 1161

	IWL_DEBUG_MAC80211(priv, "enter\n");
1162
	mutex_lock(&priv->mutex);
1163

1164 1165
	if (priv->cfg->bt_params &&
			priv->cfg->bt_params->advanced_bt_coexist) {
1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176
		if (rssi_event == RSSI_EVENT_LOW)
			priv->bt_enable_pspoll = true;
		else if (rssi_event == RSSI_EVENT_HIGH)
			priv->bt_enable_pspoll = false;

		iwlagn_send_advance_bt_config(priv);
	} else {
		IWL_DEBUG_MAC80211(priv, "Advanced BT coex disabled,"
				"ignoring RSSI callback\n");
	}

1177
	mutex_unlock(&priv->mutex);
1178 1179 1180
	IWL_DEBUG_MAC80211(priv, "leave\n");
}

1181 1182
static int iwlagn_mac_set_tim(struct ieee80211_hw *hw,
			      struct ieee80211_sta *sta, bool set)
1183
{
1184
	struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
1185

J
Johannes Berg 已提交
1186
	queue_work(priv->workqueue, &priv->beacon_update);
1187 1188 1189 1190

	return 0;
}

1191 1192 1193
static int iwlagn_mac_conf_tx(struct ieee80211_hw *hw,
			      struct ieee80211_vif *vif, u16 queue,
			      const struct ieee80211_tx_queue_params *params)
1194
{
1195
	struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
1196 1197 1198 1199 1200 1201 1202 1203 1204
	struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
	struct iwl_rxon_context *ctx = vif_priv->ctx;
	int q;

	if (WARN_ON(!ctx))
		return -EINVAL;

	IWL_DEBUG_MAC80211(priv, "enter\n");

D
Don Fry 已提交
1205
	if (!iwl_is_ready_rf(priv)) {
1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216
		IWL_DEBUG_MAC80211(priv, "leave - RF not ready\n");
		return -EIO;
	}

	if (queue >= AC_NUM) {
		IWL_DEBUG_MAC80211(priv, "leave - queue >= AC_NUM %d\n", queue);
		return 0;
	}

	q = AC_NUM - 1 - queue;

1217
	mutex_lock(&priv->mutex);
1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228

	ctx->qos_data.def_qos_parm.ac[q].cw_min =
		cpu_to_le16(params->cw_min);
	ctx->qos_data.def_qos_parm.ac[q].cw_max =
		cpu_to_le16(params->cw_max);
	ctx->qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
	ctx->qos_data.def_qos_parm.ac[q].edca_txop =
			cpu_to_le16((params->txop * 32));

	ctx->qos_data.def_qos_parm.ac[q].reserved1 = 0;

1229
	mutex_unlock(&priv->mutex);
1230 1231 1232 1233 1234

	IWL_DEBUG_MAC80211(priv, "leave\n");
	return 0;
}

1235
static int iwlagn_mac_tx_last_beacon(struct ieee80211_hw *hw)
1236
{
1237
	struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250

	return priv->ibss_manager == IWL_IBSS_MANAGER;
}

static int iwl_set_mode(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
{
	iwl_connection_init_rx_config(priv, ctx);

	iwlagn_set_rxon_chain(priv, ctx);

	return iwlagn_commit_rxon(priv, ctx);
}

1251 1252
static int iwl_setup_interface(struct iwl_priv *priv,
			       struct iwl_rxon_context *ctx)
1253 1254
{
	struct ieee80211_vif *vif = ctx->vif;
1255
	int err, ac;
1256

1257
	lockdep_assert_held(&priv->mutex);
1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274

	/*
	 * This variable will be correct only when there's just
	 * a single context, but all code using it is for hardware
	 * that supports only one context.
	 */
	priv->iw_mode = vif->type;

	ctx->is_active = true;

	err = iwl_set_mode(priv, ctx);
	if (err) {
		if (!ctx->always_active)
			ctx->is_active = false;
		return err;
	}

1275
	if (priv->cfg->bt_params && priv->cfg->bt_params->advanced_bt_coexist &&
1276 1277 1278 1279 1280 1281 1282 1283 1284
	    vif->type == NL80211_IFTYPE_ADHOC) {
		/*
		 * pretend to have high BT traffic as long as we
		 * are operating in IBSS mode, as this will cause
		 * the rate scaling etc. to behave as intended.
		 */
		priv->bt_traffic_load = IWL_BT_COEX_TRAFFIC_LOAD_HIGH;
	}

1285 1286 1287 1288 1289 1290 1291 1292 1293
	/* set up queue mappings */
	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
		vif->hw_queue[ac] = ctx->ac_to_queue[ac];

	if (vif->type == NL80211_IFTYPE_AP)
		vif->cab_queue = ctx->mcast_queue;
	else
		vif->cab_queue = IEEE80211_INVAL_HW_QUEUE;

1294 1295 1296 1297
	return 0;
}

static int iwlagn_mac_add_interface(struct ieee80211_hw *hw,
1298
				    struct ieee80211_vif *vif)
1299
{
1300
	struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
1301 1302 1303 1304
	struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
	struct iwl_rxon_context *tmp, *ctx = NULL;
	int err;
	enum nl80211_iftype viftype = ieee80211_vif_type_p2p(vif);
1305
	bool reset = false;
1306 1307 1308 1309 1310 1311

	IWL_DEBUG_MAC80211(priv, "enter: type %d, addr %pM\n",
			   viftype, vif->addr);

	cancel_delayed_work_sync(&priv->hw_roc_disable_work);

1312
	mutex_lock(&priv->mutex);
1313 1314 1315

	iwlagn_disable_roc(priv);

D
Don Fry 已提交
1316
	if (!iwl_is_ready_rf(priv)) {
1317 1318 1319 1320 1321 1322 1323 1324 1325 1326
		IWL_WARN(priv, "Try to add interface when device not ready\n");
		err = -EINVAL;
		goto out;
	}

	for_each_context(priv, tmp) {
		u32 possible_modes =
			tmp->interface_modes | tmp->exclusive_interface_modes;

		if (tmp->vif) {
1327 1328 1329 1330 1331 1332 1333
			/* On reset we need to add the same interface again */
			if (tmp->vif == vif) {
				reset = true;
				ctx = tmp;
				break;
			}

1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358
			/* check if this busy context is exclusive */
			if (tmp->exclusive_interface_modes &
						BIT(tmp->vif->type)) {
				err = -EINVAL;
				goto out;
			}
			continue;
		}

		if (!(possible_modes & BIT(viftype)))
			continue;

		/* have maybe usable context w/o interface */
		ctx = tmp;
		break;
	}

	if (!ctx) {
		err = -EOPNOTSUPP;
		goto out;
	}

	vif_priv->ctx = ctx;
	ctx->vif = vif;

J
Johannes Berg 已提交
1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372
	/*
	 * In SNIFFER device type, the firmware reports the FCS to
	 * the host, rather than snipping it off. Unfortunately,
	 * mac80211 doesn't (yet) provide a per-packet flag for
	 * this, so that we have to set the hardware flag based
	 * on the interfaces added. As the monitor interface can
	 * only be present by itself, and will be removed before
	 * other interfaces are added, this is safe.
	 */
	if (vif->type == NL80211_IFTYPE_MONITOR)
		priv->hw->flags |= IEEE80211_HW_RX_INCLUDES_FCS;
	else
		priv->hw->flags &= ~IEEE80211_HW_RX_INCLUDES_FCS;

1373
	err = iwl_setup_interface(priv, ctx);
1374
	if (!err || reset)
1375 1376 1377 1378 1379
		goto out;

	ctx->vif = NULL;
	priv->iw_mode = NL80211_IFTYPE_STATION;
 out:
1380
	mutex_unlock(&priv->mutex);
1381 1382 1383 1384 1385

	IWL_DEBUG_MAC80211(priv, "leave\n");
	return err;
}

1386 1387 1388
static void iwl_teardown_interface(struct iwl_priv *priv,
				   struct ieee80211_vif *vif,
				   bool mode_change)
1389 1390 1391
{
	struct iwl_rxon_context *ctx = iwl_rxon_ctx_from_vif(vif);

1392
	lockdep_assert_held(&priv->mutex);
1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418

	if (priv->scan_vif == vif) {
		iwl_scan_cancel_timeout(priv, 200);
		iwl_force_scan_end(priv);
	}

	if (!mode_change) {
		iwl_set_mode(priv, ctx);
		if (!ctx->always_active)
			ctx->is_active = false;
	}

	/*
	 * When removing the IBSS interface, overwrite the
	 * BT traffic load with the stored one from the last
	 * notification, if any. If this is a device that
	 * doesn't implement this, this has no effect since
	 * both values are the same and zero.
	 */
	if (vif->type == NL80211_IFTYPE_ADHOC)
		priv->bt_traffic_load = priv->last_bt_traffic_load;
}

static void iwlagn_mac_remove_interface(struct ieee80211_hw *hw,
			      struct ieee80211_vif *vif)
{
1419
	struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
1420 1421 1422 1423
	struct iwl_rxon_context *ctx = iwl_rxon_ctx_from_vif(vif);

	IWL_DEBUG_MAC80211(priv, "enter\n");

1424
	mutex_lock(&priv->mutex);
1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436

	if (WARN_ON(ctx->vif != vif)) {
		struct iwl_rxon_context *tmp;
		IWL_ERR(priv, "ctx->vif = %p, vif = %p\n", ctx->vif, vif);
		for_each_context(priv, tmp)
			IWL_ERR(priv, "\tID = %d:\tctx = %p\tctx->vif = %p\n",
				tmp->ctxid, tmp, tmp->vif);
	}
	ctx->vif = NULL;

	iwl_teardown_interface(priv, vif, false);

1437
	mutex_unlock(&priv->mutex);
1438 1439 1440 1441 1442 1443

	IWL_DEBUG_MAC80211(priv, "leave\n");

}

static int iwlagn_mac_change_interface(struct ieee80211_hw *hw,
1444 1445
				       struct ieee80211_vif *vif,
				       enum nl80211_iftype newtype, bool newp2p)
1446
{
1447
	struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
1448
	struct iwl_rxon_context *ctx, *tmp;
1449 1450 1451 1452 1453 1454 1455 1456
	enum nl80211_iftype newviftype = newtype;
	u32 interface_modes;
	int err;

	IWL_DEBUG_MAC80211(priv, "enter\n");

	newtype = ieee80211_iftype_p2p(newtype, newp2p);

1457
	mutex_lock(&priv->mutex);
1458

1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470
	ctx = iwl_rxon_ctx_from_vif(vif);

	/*
	 * To simplify this code, only support changes on the
	 * BSS context. The PAN context is usually reassigned
	 * by creating/removing P2P interfaces anyway.
	 */
	if (ctx->ctxid != IWL_RXON_CTX_BSS) {
		err = -EBUSY;
		goto out;
	}

D
Don Fry 已提交
1471
	if (!ctx->vif || !iwl_is_ready_rf(priv)) {
1472 1473 1474 1475 1476 1477 1478 1479
		/*
		 * Huh? But wait ... this can maybe happen when
		 * we're in the middle of a firmware restart!
		 */
		err = -EBUSY;
		goto out;
	}

1480
	/* Check if the switch is supported in the same context */
1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491
	interface_modes = ctx->interface_modes | ctx->exclusive_interface_modes;
	if (!(interface_modes & BIT(newtype))) {
		err = -EBUSY;
		goto out;
	}

	if (ctx->exclusive_interface_modes & BIT(newtype)) {
		for_each_context(priv, tmp) {
			if (ctx == tmp)
				continue;

1492
			if (!tmp->is_active)
1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519
				continue;

			/*
			 * The current mode switch would be exclusive, but
			 * another context is active ... refuse the switch.
			 */
			err = -EBUSY;
			goto out;
		}
	}

	/* success */
	iwl_teardown_interface(priv, vif, true);
	vif->type = newviftype;
	vif->p2p = newp2p;
	err = iwl_setup_interface(priv, ctx);
	WARN_ON(err);
	/*
	 * We've switched internally, but submitting to the
	 * device may have failed for some reason. Mask this
	 * error, because otherwise mac80211 will not switch
	 * (and set the interface type back) and we'll be
	 * out of sync with it.
	 */
	err = 0;

 out:
1520
	mutex_unlock(&priv->mutex);
1521 1522 1523 1524 1525
	IWL_DEBUG_MAC80211(priv, "leave\n");

	return err;
}

1526 1527 1528
static int iwlagn_mac_hw_scan(struct ieee80211_hw *hw,
			      struct ieee80211_vif *vif,
			      struct cfg80211_scan_request *req)
1529
{
1530
	struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
1531 1532 1533 1534 1535 1536 1537
	int ret;

	IWL_DEBUG_MAC80211(priv, "enter\n");

	if (req->n_channels == 0)
		return -EINVAL;

1538
	mutex_lock(&priv->mutex);
1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566

	/*
	 * If an internal scan is in progress, just set
	 * up the scan_request as per above.
	 */
	if (priv->scan_type != IWL_SCAN_NORMAL) {
		IWL_DEBUG_SCAN(priv,
			       "SCAN request during internal scan - defer\n");
		priv->scan_request = req;
		priv->scan_vif = vif;
		ret = 0;
	} else {
		priv->scan_request = req;
		priv->scan_vif = vif;
		/*
		 * mac80211 will only ask for one band at a time
		 * so using channels[0] here is ok
		 */
		ret = iwl_scan_initiate(priv, vif, IWL_SCAN_NORMAL,
					req->channels[0]->band);
		if (ret) {
			priv->scan_request = NULL;
			priv->scan_vif = NULL;
		}
	}

	IWL_DEBUG_MAC80211(priv, "leave\n");

1567
	mutex_unlock(&priv->mutex);
1568 1569 1570 1571

	return ret;
}

1572 1573
static void iwl_sta_modify_ps_wake(struct iwl_priv *priv, int sta_id)
{
1574 1575 1576 1577 1578
	struct iwl_addsta_cmd cmd = {
		.mode = STA_CONTROL_MODIFY_MSK,
		.station_flags_msk = STA_FLG_PWR_SAVE_MSK,
		.sta.sta_id = sta_id,
	};
1579

1580
	iwl_send_add_sta(priv, &cmd, CMD_ASYNC);
1581 1582
}

1583 1584 1585 1586
static void iwlagn_mac_sta_notify(struct ieee80211_hw *hw,
				  struct ieee80211_vif *vif,
				  enum sta_notify_cmd cmd,
				  struct ieee80211_sta *sta)
1587
{
1588
	struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615
	struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
	int sta_id;

	IWL_DEBUG_MAC80211(priv, "enter\n");

	switch (cmd) {
	case STA_NOTIFY_SLEEP:
		WARN_ON(!sta_priv->client);
		sta_priv->asleep = true;
		if (atomic_read(&sta_priv->pending_frames) > 0)
			ieee80211_sta_block_awake(hw, sta, true);
		break;
	case STA_NOTIFY_AWAKE:
		WARN_ON(!sta_priv->client);
		if (!sta_priv->asleep)
			break;
		sta_priv->asleep = false;
		sta_id = iwl_sta_id(sta);
		if (sta_id != IWL_INVALID_STATION)
			iwl_sta_modify_ps_wake(priv, sta_id);
		break;
	default:
		break;
	}
	IWL_DEBUG_MAC80211(priv, "leave\n");
}

1616 1617 1618 1619 1620 1621 1622
struct ieee80211_ops iwlagn_hw_ops = {
	.tx = iwlagn_mac_tx,
	.start = iwlagn_mac_start,
	.stop = iwlagn_mac_stop,
#ifdef CONFIG_PM_SLEEP
	.suspend = iwlagn_mac_suspend,
	.resume = iwlagn_mac_resume,
1623
	.set_wakeup = iwlagn_mac_set_wakeup,
1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637
#endif
	.add_interface = iwlagn_mac_add_interface,
	.remove_interface = iwlagn_mac_remove_interface,
	.change_interface = iwlagn_mac_change_interface,
	.config = iwlagn_mac_config,
	.configure_filter = iwlagn_configure_filter,
	.set_key = iwlagn_mac_set_key,
	.update_tkip_key = iwlagn_mac_update_tkip_key,
	.set_rekey_data = iwlagn_mac_set_rekey_data,
	.conf_tx = iwlagn_mac_conf_tx,
	.bss_info_changed = iwlagn_bss_info_changed,
	.ampdu_action = iwlagn_mac_ampdu_action,
	.hw_scan = iwlagn_mac_hw_scan,
	.sta_notify = iwlagn_mac_sta_notify,
1638
	.sta_state = iwlagn_mac_sta_state,
1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653
	.channel_switch = iwlagn_mac_channel_switch,
	.flush = iwlagn_mac_flush,
	.tx_last_beacon = iwlagn_mac_tx_last_beacon,
	.remain_on_channel = iwlagn_mac_remain_on_channel,
	.cancel_remain_on_channel = iwlagn_mac_cancel_remain_on_channel,
	.rssi_callback = iwlagn_mac_rssi_callback,
	CFG80211_TESTMODE_CMD(iwlagn_mac_testmode_cmd)
	CFG80211_TESTMODE_DUMP(iwlagn_mac_testmode_dump)
	.set_tim = iwlagn_mac_set_tim,
};

/* This function both allocates and initializes hw and priv. */
struct ieee80211_hw *iwl_alloc_all(void)
{
	struct iwl_priv *priv;
1654
	struct iwl_op_mode *op_mode;
1655 1656 1657 1658
	/* mac80211 allocates memory for this device instance, including
	 *   space for this driver's private structure */
	struct ieee80211_hw *hw;

1659 1660
	hw = ieee80211_alloc_hw(sizeof(struct iwl_priv) +
				sizeof(struct iwl_op_mode), &iwlagn_hw_ops);
1661 1662 1663
	if (!hw)
		goto out;

1664 1665
	op_mode = hw->priv;
	priv = IWL_OP_MODE_GET_DVM(op_mode);
1666 1667 1668 1669 1670
	priv->hw = hw;

out:
	return hw;
}