Linux Audio

Check our new training course

Linux BSP development engineering services

Need help to port Linux and bootloaders to your hardware?
Loading...
v6.8
   1// SPDX-License-Identifier: GPL-2.0-only
   2/******************************************************************************
   3 *
   4 * Copyright(c) 2003 - 2014 Intel Corporation. All rights reserved.
   5 * Copyright(c) 2015 Intel Deutschland GmbH
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
   6 *****************************************************************************/
   7
   8#include <linux/etherdevice.h>
   9#include "iwl-trans.h"
  10#include "iwl-modparams.h"
  11#include "dev.h"
  12#include "agn.h"
  13#include "calib.h"
  14
  15/*
  16 * initialize rxon structure with default values from eeprom
  17 */
  18void iwl_connection_init_rx_config(struct iwl_priv *priv,
  19				   struct iwl_rxon_context *ctx)
  20{
  21	memset(&ctx->staging, 0, sizeof(ctx->staging));
  22
  23	if (!ctx->vif) {
  24		ctx->staging.dev_type = ctx->unused_devtype;
  25	} else
  26	switch (ctx->vif->type) {
  27	case NL80211_IFTYPE_AP:
  28		ctx->staging.dev_type = ctx->ap_devtype;
  29		break;
  30
  31	case NL80211_IFTYPE_STATION:
  32		ctx->staging.dev_type = ctx->station_devtype;
  33		ctx->staging.filter_flags = RXON_FILTER_ACCEPT_GRP_MSK;
  34		break;
  35
  36	case NL80211_IFTYPE_ADHOC:
  37		ctx->staging.dev_type = ctx->ibss_devtype;
  38		ctx->staging.flags = RXON_FLG_SHORT_PREAMBLE_MSK;
  39		ctx->staging.filter_flags = RXON_FILTER_BCON_AWARE_MSK |
  40						  RXON_FILTER_ACCEPT_GRP_MSK;
  41		break;
  42
  43	case NL80211_IFTYPE_MONITOR:
  44		ctx->staging.dev_type = RXON_DEV_TYPE_SNIFFER;
  45		break;
  46
  47	default:
  48		IWL_ERR(priv, "Unsupported interface type %d\n",
  49			ctx->vif->type);
  50		break;
  51	}
  52
  53#if 0
  54	/* TODO:  Figure out when short_preamble would be set and cache from
  55	 * that */
  56	if (!hw_to_local(priv->hw)->short_preamble)
  57		ctx->staging.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
  58	else
  59		ctx->staging.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
  60#endif
  61
  62	ctx->staging.channel =
  63		cpu_to_le16(priv->hw->conf.chandef.chan->hw_value);
  64	priv->band = priv->hw->conf.chandef.chan->band;
  65
  66	iwl_set_flags_for_band(priv, ctx, priv->band, ctx->vif);
  67
  68	/* clear both MIX and PURE40 mode flag */
  69	ctx->staging.flags &= ~(RXON_FLG_CHANNEL_MODE_MIXED |
  70					RXON_FLG_CHANNEL_MODE_PURE_40);
  71	if (ctx->vif)
  72		memcpy(ctx->staging.node_addr, ctx->vif->addr, ETH_ALEN);
  73
  74	ctx->staging.ofdm_ht_single_stream_basic_rates = 0xff;
  75	ctx->staging.ofdm_ht_dual_stream_basic_rates = 0xff;
  76	ctx->staging.ofdm_ht_triple_stream_basic_rates = 0xff;
  77}
  78
  79static int iwlagn_disable_bss(struct iwl_priv *priv,
  80			      struct iwl_rxon_context *ctx,
  81			      struct iwl_rxon_cmd *send)
  82{
  83	__le32 old_filter = send->filter_flags;
  84	int ret;
  85
  86	send->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
  87	ret = iwl_dvm_send_cmd_pdu(priv, ctx->rxon_cmd,
  88				0, sizeof(*send), send);
  89
  90	send->filter_flags = old_filter;
  91
  92	if (ret)
  93		IWL_DEBUG_QUIET_RFKILL(priv,
  94			"Error clearing ASSOC_MSK on BSS (%d)\n", ret);
  95
  96	return ret;
  97}
  98
  99static int iwlagn_disable_pan(struct iwl_priv *priv,
 100			      struct iwl_rxon_context *ctx,
 101			      struct iwl_rxon_cmd *send)
 102{
 103	struct iwl_notification_wait disable_wait;
 104	__le32 old_filter = send->filter_flags;
 105	u8 old_dev_type = send->dev_type;
 106	int ret;
 107	static const u16 deactivate_cmd[] = {
 108		REPLY_WIPAN_DEACTIVATION_COMPLETE
 109	};
 110
 111	iwl_init_notification_wait(&priv->notif_wait, &disable_wait,
 112				   deactivate_cmd, ARRAY_SIZE(deactivate_cmd),
 113				   NULL, NULL);
 114
 115	send->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
 116	send->dev_type = RXON_DEV_TYPE_P2P;
 117	ret = iwl_dvm_send_cmd_pdu(priv, ctx->rxon_cmd,
 118				0, sizeof(*send), send);
 119
 120	send->filter_flags = old_filter;
 121	send->dev_type = old_dev_type;
 122
 123	if (ret) {
 124		IWL_ERR(priv, "Error disabling PAN (%d)\n", ret);
 125		iwl_remove_notification(&priv->notif_wait, &disable_wait);
 126	} else {
 127		ret = iwl_wait_notification(&priv->notif_wait,
 128					    &disable_wait, HZ);
 129		if (ret)
 130			IWL_ERR(priv, "Timed out waiting for PAN disable\n");
 131	}
 132
 133	return ret;
 134}
 135
 136static int iwlagn_disconn_pan(struct iwl_priv *priv,
 137			      struct iwl_rxon_context *ctx,
 138			      struct iwl_rxon_cmd *send)
 139{
 140	__le32 old_filter = send->filter_flags;
 141	int ret;
 142
 143	send->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
 144	ret = iwl_dvm_send_cmd_pdu(priv, ctx->rxon_cmd, 0,
 145				sizeof(*send), send);
 146
 147	send->filter_flags = old_filter;
 148
 149	return ret;
 150}
 151
 152static void iwlagn_update_qos(struct iwl_priv *priv,
 153			      struct iwl_rxon_context *ctx)
 154{
 155	int ret;
 156
 157	if (!ctx->is_active)
 158		return;
 159
 160	ctx->qos_data.def_qos_parm.qos_flags = 0;
 161
 162	if (ctx->qos_data.qos_active)
 163		ctx->qos_data.def_qos_parm.qos_flags |=
 164			QOS_PARAM_FLG_UPDATE_EDCA_MSK;
 165
 166	if (ctx->ht.enabled)
 167		ctx->qos_data.def_qos_parm.qos_flags |= QOS_PARAM_FLG_TGN_MSK;
 168
 169	IWL_DEBUG_INFO(priv, "send QoS cmd with Qos active=%d FLAGS=0x%X\n",
 170		      ctx->qos_data.qos_active,
 171		      ctx->qos_data.def_qos_parm.qos_flags);
 172
 173	ret = iwl_dvm_send_cmd_pdu(priv, ctx->qos_cmd, 0,
 174			       sizeof(struct iwl_qosparam_cmd),
 175			       &ctx->qos_data.def_qos_parm);
 176	if (ret)
 177		IWL_DEBUG_QUIET_RFKILL(priv, "Failed to update QoS\n");
 178}
 179
 180static int iwlagn_update_beacon(struct iwl_priv *priv,
 181				struct ieee80211_vif *vif)
 182{
 183	lockdep_assert_held(&priv->mutex);
 184
 185	dev_kfree_skb(priv->beacon_skb);
 186	priv->beacon_skb = ieee80211_beacon_get(priv->hw, vif, 0);
 187	if (!priv->beacon_skb)
 188		return -ENOMEM;
 189	return iwlagn_send_beacon_cmd(priv);
 190}
 191
 192static int iwlagn_send_rxon_assoc(struct iwl_priv *priv,
 193				  struct iwl_rxon_context *ctx)
 194{
 195	int ret = 0;
 196	struct iwl_rxon_assoc_cmd rxon_assoc;
 197	const struct iwl_rxon_cmd *rxon1 = &ctx->staging;
 198	const struct iwl_rxon_cmd *rxon2 = &ctx->active;
 199
 200	if ((rxon1->flags == rxon2->flags) &&
 201	    (rxon1->filter_flags == rxon2->filter_flags) &&
 202	    (rxon1->cck_basic_rates == rxon2->cck_basic_rates) &&
 203	    (rxon1->ofdm_ht_single_stream_basic_rates ==
 204	     rxon2->ofdm_ht_single_stream_basic_rates) &&
 205	    (rxon1->ofdm_ht_dual_stream_basic_rates ==
 206	     rxon2->ofdm_ht_dual_stream_basic_rates) &&
 207	    (rxon1->ofdm_ht_triple_stream_basic_rates ==
 208	     rxon2->ofdm_ht_triple_stream_basic_rates) &&
 209	    (rxon1->acquisition_data == rxon2->acquisition_data) &&
 210	    (rxon1->rx_chain == rxon2->rx_chain) &&
 211	    (rxon1->ofdm_basic_rates == rxon2->ofdm_basic_rates)) {
 212		IWL_DEBUG_INFO(priv, "Using current RXON_ASSOC.  Not resending.\n");
 213		return 0;
 214	}
 215
 216	rxon_assoc.flags = ctx->staging.flags;
 217	rxon_assoc.filter_flags = ctx->staging.filter_flags;
 218	rxon_assoc.ofdm_basic_rates = ctx->staging.ofdm_basic_rates;
 219	rxon_assoc.cck_basic_rates = ctx->staging.cck_basic_rates;
 220	rxon_assoc.reserved1 = 0;
 221	rxon_assoc.reserved2 = 0;
 222	rxon_assoc.reserved3 = 0;
 223	rxon_assoc.ofdm_ht_single_stream_basic_rates =
 224	    ctx->staging.ofdm_ht_single_stream_basic_rates;
 225	rxon_assoc.ofdm_ht_dual_stream_basic_rates =
 226	    ctx->staging.ofdm_ht_dual_stream_basic_rates;
 227	rxon_assoc.rx_chain_select_flags = ctx->staging.rx_chain;
 228	rxon_assoc.ofdm_ht_triple_stream_basic_rates =
 229		 ctx->staging.ofdm_ht_triple_stream_basic_rates;
 230	rxon_assoc.acquisition_data = ctx->staging.acquisition_data;
 231
 232	ret = iwl_dvm_send_cmd_pdu(priv, ctx->rxon_assoc_cmd,
 233				CMD_ASYNC, sizeof(rxon_assoc), &rxon_assoc);
 234	return ret;
 235}
 236
 237static u16 iwl_adjust_beacon_interval(u16 beacon_val, u16 max_beacon_val)
 238{
 239	u16 new_val;
 240	u16 beacon_factor;
 241
 242	/*
 243	 * If mac80211 hasn't given us a beacon interval, program
 244	 * the default into the device (not checking this here
 245	 * would cause the adjustment below to return the maximum
 246	 * value, which may break PAN.)
 247	 */
 248	if (!beacon_val)
 249		return DEFAULT_BEACON_INTERVAL;
 250
 251	/*
 252	 * If the beacon interval we obtained from the peer
 253	 * is too large, we'll have to wake up more often
 254	 * (and in IBSS case, we'll beacon too much)
 255	 *
 256	 * For example, if max_beacon_val is 4096, and the
 257	 * requested beacon interval is 7000, we'll have to
 258	 * use 3500 to be able to wake up on the beacons.
 259	 *
 260	 * This could badly influence beacon detection stats.
 261	 */
 262
 263	beacon_factor = (beacon_val + max_beacon_val) / max_beacon_val;
 264	new_val = beacon_val / beacon_factor;
 265
 266	if (!new_val)
 267		new_val = max_beacon_val;
 268
 269	return new_val;
 270}
 271
 272static int iwl_send_rxon_timing(struct iwl_priv *priv,
 273				struct iwl_rxon_context *ctx)
 274{
 275	u64 tsf;
 276	s32 interval_tm, rem;
 277	struct ieee80211_conf *conf = NULL;
 278	u16 beacon_int;
 279	struct ieee80211_vif *vif = ctx->vif;
 280
 281	conf = &priv->hw->conf;
 282
 283	lockdep_assert_held(&priv->mutex);
 284
 285	memset(&ctx->timing, 0, sizeof(struct iwl_rxon_time_cmd));
 286
 287	ctx->timing.timestamp = cpu_to_le64(priv->timestamp);
 288	ctx->timing.listen_interval = cpu_to_le16(conf->listen_interval);
 289
 290	beacon_int = vif ? vif->bss_conf.beacon_int : 0;
 291
 292	/*
 293	 * TODO: For IBSS we need to get atim_window from mac80211,
 294	 *	 for now just always use 0
 295	 */
 296	ctx->timing.atim_window = 0;
 297
 298	if (ctx->ctxid == IWL_RXON_CTX_PAN &&
 299	    (!ctx->vif || ctx->vif->type != NL80211_IFTYPE_STATION) &&
 300	    iwl_is_associated(priv, IWL_RXON_CTX_BSS) &&
 301	    priv->contexts[IWL_RXON_CTX_BSS].vif &&
 302	    priv->contexts[IWL_RXON_CTX_BSS].vif->bss_conf.beacon_int) {
 303		ctx->timing.beacon_interval =
 304			priv->contexts[IWL_RXON_CTX_BSS].timing.beacon_interval;
 305		beacon_int = le16_to_cpu(ctx->timing.beacon_interval);
 306	} else if (ctx->ctxid == IWL_RXON_CTX_BSS &&
 307		   iwl_is_associated(priv, IWL_RXON_CTX_PAN) &&
 308		   priv->contexts[IWL_RXON_CTX_PAN].vif &&
 309		   priv->contexts[IWL_RXON_CTX_PAN].vif->bss_conf.beacon_int &&
 310		   (!iwl_is_associated_ctx(ctx) || !ctx->vif ||
 311		    !ctx->vif->bss_conf.beacon_int)) {
 312		ctx->timing.beacon_interval =
 313			priv->contexts[IWL_RXON_CTX_PAN].timing.beacon_interval;
 314		beacon_int = le16_to_cpu(ctx->timing.beacon_interval);
 315	} else {
 316		beacon_int = iwl_adjust_beacon_interval(beacon_int,
 317			IWL_MAX_UCODE_BEACON_INTERVAL * TIME_UNIT);
 318		ctx->timing.beacon_interval = cpu_to_le16(beacon_int);
 319	}
 320
 321	ctx->beacon_int = beacon_int;
 322
 323	tsf = priv->timestamp; /* tsf is modifed by do_div: copy it */
 324	interval_tm = beacon_int * TIME_UNIT;
 325	rem = do_div(tsf, interval_tm);
 326	ctx->timing.beacon_init_val = cpu_to_le32(interval_tm - rem);
 327
 328	ctx->timing.dtim_period = vif ? (vif->bss_conf.dtim_period ?: 1) : 1;
 329
 330	IWL_DEBUG_ASSOC(priv,
 331			"beacon interval %d beacon timer %d beacon tim %d\n",
 332			le16_to_cpu(ctx->timing.beacon_interval),
 333			le32_to_cpu(ctx->timing.beacon_init_val),
 334			le16_to_cpu(ctx->timing.atim_window));
 335
 336	return iwl_dvm_send_cmd_pdu(priv, ctx->rxon_timing_cmd,
 337				0, sizeof(ctx->timing), &ctx->timing);
 338}
 339
 340static int iwlagn_rxon_disconn(struct iwl_priv *priv,
 341			       struct iwl_rxon_context *ctx)
 342{
 343	int ret;
 344	struct iwl_rxon_cmd *active = (void *)&ctx->active;
 345
 346	if (ctx->ctxid == IWL_RXON_CTX_BSS) {
 347		ret = iwlagn_disable_bss(priv, ctx, &ctx->staging);
 348	} else {
 349		ret = iwlagn_disable_pan(priv, ctx, &ctx->staging);
 350		if (ret)
 351			return ret;
 352		if (ctx->vif) {
 353			ret = iwl_send_rxon_timing(priv, ctx);
 354			if (ret) {
 355				IWL_ERR(priv, "Failed to send timing (%d)!\n", ret);
 356				return ret;
 357			}
 358			ret = iwlagn_disconn_pan(priv, ctx, &ctx->staging);
 359		}
 360	}
 361	if (ret)
 362		return ret;
 363
 364	/*
 365	 * Un-assoc RXON clears the station table and WEP
 366	 * keys, so we have to restore those afterwards.
 367	 */
 368	iwl_clear_ucode_stations(priv, ctx);
 369	/* update -- might need P2P now */
 370	iwl_update_bcast_station(priv, ctx);
 371	iwl_restore_stations(priv, ctx);
 372	ret = iwl_restore_default_wep_keys(priv, ctx);
 373	if (ret) {
 374		IWL_ERR(priv, "Failed to restore WEP keys (%d)\n", ret);
 375		return ret;
 376	}
 377
 378	memcpy(active, &ctx->staging, sizeof(*active));
 379	return 0;
 380}
 381
 382static int iwl_set_tx_power(struct iwl_priv *priv, s8 tx_power, bool force)
 383{
 384	int ret;
 385	s8 prev_tx_power;
 386	bool defer;
 387	struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
 388
 389	if (priv->calib_disabled & IWL_TX_POWER_CALIB_DISABLED)
 390		return 0;
 391
 392	lockdep_assert_held(&priv->mutex);
 393
 394	if (priv->tx_power_user_lmt == tx_power && !force)
 395		return 0;
 396
 397	if (tx_power < IWLAGN_TX_POWER_TARGET_POWER_MIN) {
 398		IWL_WARN(priv,
 399			 "Requested user TXPOWER %d below lower limit %d.\n",
 400			 tx_power,
 401			 IWLAGN_TX_POWER_TARGET_POWER_MIN);
 402		return -EINVAL;
 403	}
 404
 405	if (tx_power > DIV_ROUND_UP(priv->nvm_data->max_tx_pwr_half_dbm, 2)) {
 406		IWL_WARN(priv,
 407			"Requested user TXPOWER %d above upper limit %d.\n",
 408			 tx_power, priv->nvm_data->max_tx_pwr_half_dbm);
 409		return -EINVAL;
 410	}
 411
 412	if (!iwl_is_ready_rf(priv))
 413		return -EIO;
 414
 415	/* scan complete and commit_rxon use tx_power_next value,
 416	 * it always need to be updated for newest request */
 417	priv->tx_power_next = tx_power;
 418
 419	/* do not set tx power when scanning or channel changing */
 420	defer = test_bit(STATUS_SCANNING, &priv->status) ||
 421		memcmp(&ctx->active, &ctx->staging, sizeof(ctx->staging));
 422	if (defer && !force) {
 423		IWL_DEBUG_INFO(priv, "Deferring tx power set\n");
 424		return 0;
 425	}
 426
 427	prev_tx_power = priv->tx_power_user_lmt;
 428	priv->tx_power_user_lmt = tx_power;
 429
 430	ret = iwlagn_send_tx_power(priv);
 431
 432	/* if fail to set tx_power, restore the orig. tx power */
 433	if (ret) {
 434		priv->tx_power_user_lmt = prev_tx_power;
 435		priv->tx_power_next = prev_tx_power;
 436	}
 437	return ret;
 438}
 439
 440static int iwlagn_rxon_connect(struct iwl_priv *priv,
 441			       struct iwl_rxon_context *ctx)
 442{
 443	int ret;
 444	struct iwl_rxon_cmd *active = (void *)&ctx->active;
 445
 446	/* RXON timing must be before associated RXON */
 447	if (ctx->ctxid == IWL_RXON_CTX_BSS) {
 448		ret = iwl_send_rxon_timing(priv, ctx);
 449		if (ret) {
 450			IWL_ERR(priv, "Failed to send timing (%d)!\n", ret);
 451			return ret;
 452		}
 453	}
 454	/* QoS info may be cleared by previous un-assoc RXON */
 455	iwlagn_update_qos(priv, ctx);
 456
 457	/*
 458	 * We'll run into this code path when beaconing is
 459	 * enabled, but then we also need to send the beacon
 460	 * to the device.
 461	 */
 462	if (ctx->vif && (ctx->vif->type == NL80211_IFTYPE_AP)) {
 463		ret = iwlagn_update_beacon(priv, ctx->vif);
 464		if (ret) {
 465			IWL_ERR(priv,
 466				"Error sending required beacon (%d)!\n",
 467				ret);
 468			return ret;
 469		}
 470	}
 471
 472	priv->start_calib = 0;
 473	/*
 474	 * Apply the new configuration.
 475	 *
 476	 * Associated RXON doesn't clear the station table in uCode,
 477	 * so we don't need to restore stations etc. after this.
 478	 */
 479	ret = iwl_dvm_send_cmd_pdu(priv, ctx->rxon_cmd, 0,
 480		      sizeof(struct iwl_rxon_cmd), &ctx->staging);
 481	if (ret) {
 482		IWL_ERR(priv, "Error setting new RXON (%d)\n", ret);
 483		return ret;
 484	}
 485	memcpy(active, &ctx->staging, sizeof(*active));
 486
 487	/* IBSS beacon needs to be sent after setting assoc */
 488	if (ctx->vif && (ctx->vif->type == NL80211_IFTYPE_ADHOC))
 489		if (iwlagn_update_beacon(priv, ctx->vif))
 490			IWL_ERR(priv, "Error sending IBSS beacon\n");
 491	iwl_init_sensitivity(priv);
 492
 493	/*
 494	 * If we issue a new RXON command which required a tune then
 495	 * we must send a new TXPOWER command or we won't be able to
 496	 * Tx any frames.
 497	 *
 498	 * It's expected we set power here if channel is changing.
 499	 */
 500	ret = iwl_set_tx_power(priv, priv->tx_power_next, true);
 501	if (ret) {
 502		IWL_ERR(priv, "Error sending TX power (%d)\n", ret);
 503		return ret;
 504	}
 505
 
 
 
 
 
 506	return 0;
 507}
 508
 509int iwlagn_set_pan_params(struct iwl_priv *priv)
 510{
 511	struct iwl_wipan_params_cmd cmd;
 512	struct iwl_rxon_context *ctx_bss, *ctx_pan;
 513	int slot0 = 300, slot1 = 0;
 514	int ret;
 515
 516	if (priv->valid_contexts == BIT(IWL_RXON_CTX_BSS))
 517		return 0;
 518
 519	BUILD_BUG_ON(NUM_IWL_RXON_CTX != 2);
 520
 521	lockdep_assert_held(&priv->mutex);
 522
 523	ctx_bss = &priv->contexts[IWL_RXON_CTX_BSS];
 524	ctx_pan = &priv->contexts[IWL_RXON_CTX_PAN];
 525
 526	/*
 527	 * If the PAN context is inactive, then we don't need
 528	 * to update the PAN parameters, the last thing we'll
 529	 * have done before it goes inactive is making the PAN
 530	 * parameters be WLAN-only.
 531	 */
 532	if (!ctx_pan->is_active)
 533		return 0;
 534
 535	memset(&cmd, 0, sizeof(cmd));
 536
 537	/* only 2 slots are currently allowed */
 538	cmd.num_slots = 2;
 539
 540	cmd.slots[0].type = 0; /* BSS */
 541	cmd.slots[1].type = 1; /* PAN */
 542
 543	if (ctx_bss->vif && ctx_pan->vif) {
 544		int bcnint = ctx_pan->beacon_int;
 545		int dtim = ctx_pan->vif->bss_conf.dtim_period ?: 1;
 546
 547		/* should be set, but seems unused?? */
 548		cmd.flags |= cpu_to_le16(IWL_WIPAN_PARAMS_FLG_SLOTTED_MODE);
 549
 550		if (ctx_pan->vif->type == NL80211_IFTYPE_AP &&
 551		    bcnint &&
 552		    bcnint != ctx_bss->beacon_int) {
 553			IWL_ERR(priv,
 554				"beacon intervals don't match (%d, %d)\n",
 555				ctx_bss->beacon_int, ctx_pan->beacon_int);
 556		} else
 557			bcnint = max_t(int, bcnint,
 558				       ctx_bss->beacon_int);
 559		if (!bcnint)
 560			bcnint = DEFAULT_BEACON_INTERVAL;
 561		slot0 = bcnint / 2;
 562		slot1 = bcnint - slot0;
 563
 564		if (test_bit(STATUS_SCAN_HW, &priv->status) ||
 565		    (!ctx_bss->vif->cfg.idle &&
 566		     !ctx_bss->vif->cfg.assoc)) {
 567			slot0 = dtim * bcnint * 3 - IWL_MIN_SLOT_TIME;
 568			slot1 = IWL_MIN_SLOT_TIME;
 569		} else if (!ctx_pan->vif->cfg.idle &&
 570			   !ctx_pan->vif->cfg.assoc) {
 571			slot1 = dtim * bcnint * 3 - IWL_MIN_SLOT_TIME;
 572			slot0 = IWL_MIN_SLOT_TIME;
 573		}
 574	} else if (ctx_pan->vif) {
 575		slot0 = 0;
 576		slot1 = max_t(int, 1, ctx_pan->vif->bss_conf.dtim_period) *
 577					ctx_pan->beacon_int;
 578		slot1 = max_t(int, DEFAULT_BEACON_INTERVAL, slot1);
 579
 580		if (test_bit(STATUS_SCAN_HW, &priv->status)) {
 581			slot0 = slot1 * 3 - IWL_MIN_SLOT_TIME;
 582			slot1 = IWL_MIN_SLOT_TIME;
 583		}
 584	}
 585
 586	cmd.slots[0].width = cpu_to_le16(slot0);
 587	cmd.slots[1].width = cpu_to_le16(slot1);
 588
 589	ret = iwl_dvm_send_cmd_pdu(priv, REPLY_WIPAN_PARAMS, 0,
 590			sizeof(cmd), &cmd);
 591	if (ret)
 592		IWL_ERR(priv, "Error setting PAN parameters (%d)\n", ret);
 593
 594	return ret;
 595}
 596
 597static void _iwl_set_rxon_ht(struct iwl_priv *priv,
 598			     struct iwl_ht_config *ht_conf,
 599			     struct iwl_rxon_context *ctx)
 600{
 601	struct iwl_rxon_cmd *rxon = &ctx->staging;
 602
 603	if (!ctx->ht.enabled) {
 604		rxon->flags &= ~(RXON_FLG_CHANNEL_MODE_MSK |
 605			RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK |
 606			RXON_FLG_HT40_PROT_MSK |
 607			RXON_FLG_HT_PROT_MSK);
 608		return;
 609	}
 610
 611	/* FIXME: if the definition of ht.protection changed, the "translation"
 612	 * will be needed for rxon->flags
 613	 */
 614	rxon->flags |= cpu_to_le32(ctx->ht.protection <<
 615				   RXON_FLG_HT_OPERATING_MODE_POS);
 616
 617	/* Set up channel bandwidth:
 618	 * 20 MHz only, 20/40 mixed or pure 40 if ht40 ok */
 619	/* clear the HT channel mode before set the mode */
 620	rxon->flags &= ~(RXON_FLG_CHANNEL_MODE_MSK |
 621			 RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK);
 622	if (iwl_is_ht40_tx_allowed(priv, ctx, NULL)) {
 623		/* pure ht40 */
 624		if (ctx->ht.protection ==
 625		    IEEE80211_HT_OP_MODE_PROTECTION_20MHZ) {
 626			rxon->flags |= RXON_FLG_CHANNEL_MODE_PURE_40;
 627			/*
 628			 * Note: control channel is opposite of extension
 629			 * channel
 630			 */
 631			switch (ctx->ht.extension_chan_offset) {
 632			case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
 633				rxon->flags &=
 634					~RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK;
 635				break;
 636			case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
 637				rxon->flags |=
 638					RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK;
 639				break;
 640			}
 641		} else {
 642			/*
 643			 * Note: control channel is opposite of extension
 644			 * channel
 645			 */
 646			switch (ctx->ht.extension_chan_offset) {
 647			case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
 648				rxon->flags &=
 649					~(RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK);
 650				rxon->flags |= RXON_FLG_CHANNEL_MODE_MIXED;
 651				break;
 652			case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
 653				rxon->flags |= RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK;
 654				rxon->flags |= RXON_FLG_CHANNEL_MODE_MIXED;
 655				break;
 656			case IEEE80211_HT_PARAM_CHA_SEC_NONE:
 657			default:
 658				/*
 659				 * channel location only valid if in Mixed
 660				 * mode
 661				 */
 662				IWL_ERR(priv,
 663					"invalid extension channel offset\n");
 664				break;
 665			}
 666		}
 667	} else {
 668		rxon->flags |= RXON_FLG_CHANNEL_MODE_LEGACY;
 669	}
 670
 671	iwlagn_set_rxon_chain(priv, ctx);
 672
 673	IWL_DEBUG_ASSOC(priv, "rxon flags 0x%X operation mode :0x%X "
 674			"extension channel offset 0x%x\n",
 675			le32_to_cpu(rxon->flags), ctx->ht.protection,
 676			ctx->ht.extension_chan_offset);
 677}
 678
 679void iwl_set_rxon_ht(struct iwl_priv *priv, struct iwl_ht_config *ht_conf)
 680{
 681	struct iwl_rxon_context *ctx;
 682
 683	for_each_context(priv, ctx)
 684		_iwl_set_rxon_ht(priv, ht_conf, ctx);
 685}
 686
 687/*
 688 * iwl_set_rxon_channel - Set the band and channel values in staging RXON
 689 * @ch: requested channel as a pointer to struct ieee80211_channel
 690
 691 * NOTE:  Does not commit to the hardware; it sets appropriate bit fields
 692 * in the staging RXON flag structure based on the ch->band
 693 */
 694void iwl_set_rxon_channel(struct iwl_priv *priv, struct ieee80211_channel *ch,
 695			 struct iwl_rxon_context *ctx)
 696{
 697	enum nl80211_band band = ch->band;
 698	u16 channel = ch->hw_value;
 699
 700	if ((le16_to_cpu(ctx->staging.channel) == channel) &&
 701	    (priv->band == band))
 702		return;
 703
 704	ctx->staging.channel = cpu_to_le16(channel);
 705	if (band == NL80211_BAND_5GHZ)
 706		ctx->staging.flags &= ~RXON_FLG_BAND_24G_MSK;
 707	else
 708		ctx->staging.flags |= RXON_FLG_BAND_24G_MSK;
 709
 710	priv->band = band;
 711
 712	IWL_DEBUG_INFO(priv, "Staging channel set to %d [%d]\n", channel, band);
 713
 714}
 715
 716void iwl_set_flags_for_band(struct iwl_priv *priv,
 717			    struct iwl_rxon_context *ctx,
 718			    enum nl80211_band band,
 719			    struct ieee80211_vif *vif)
 720{
 721	if (band == NL80211_BAND_5GHZ) {
 722		ctx->staging.flags &=
 723		    ~(RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK
 724		      | RXON_FLG_CCK_MSK);
 725		ctx->staging.flags |= RXON_FLG_SHORT_SLOT_MSK;
 726	} else {
 727		/* Copied from iwl_post_associate() */
 728		if (vif && vif->bss_conf.use_short_slot)
 729			ctx->staging.flags |= RXON_FLG_SHORT_SLOT_MSK;
 730		else
 731			ctx->staging.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
 732
 733		ctx->staging.flags |= RXON_FLG_BAND_24G_MSK;
 734		ctx->staging.flags |= RXON_FLG_AUTO_DETECT_MSK;
 735		ctx->staging.flags &= ~RXON_FLG_CCK_MSK;
 736	}
 737}
 738
 739static void iwl_set_rxon_hwcrypto(struct iwl_priv *priv,
 740				  struct iwl_rxon_context *ctx, int hw_decrypt)
 741{
 742	struct iwl_rxon_cmd *rxon = &ctx->staging;
 743
 744	if (hw_decrypt)
 745		rxon->filter_flags &= ~RXON_FILTER_DIS_DECRYPT_MSK;
 746	else
 747		rxon->filter_flags |= RXON_FILTER_DIS_DECRYPT_MSK;
 748
 749}
 750
 751/* validate RXON structure is valid */
 752static int iwl_check_rxon_cmd(struct iwl_priv *priv,
 753			      struct iwl_rxon_context *ctx)
 754{
 755	struct iwl_rxon_cmd *rxon = &ctx->staging;
 756	u32 errors = 0;
 757
 758	if (rxon->flags & RXON_FLG_BAND_24G_MSK) {
 759		if (rxon->flags & RXON_FLG_TGJ_NARROW_BAND_MSK) {
 760			IWL_WARN(priv, "check 2.4G: wrong narrow\n");
 761			errors |= BIT(0);
 762		}
 763		if (rxon->flags & RXON_FLG_RADAR_DETECT_MSK) {
 764			IWL_WARN(priv, "check 2.4G: wrong radar\n");
 765			errors |= BIT(1);
 766		}
 767	} else {
 768		if (!(rxon->flags & RXON_FLG_SHORT_SLOT_MSK)) {
 769			IWL_WARN(priv, "check 5.2G: not short slot!\n");
 770			errors |= BIT(2);
 771		}
 772		if (rxon->flags & RXON_FLG_CCK_MSK) {
 773			IWL_WARN(priv, "check 5.2G: CCK!\n");
 774			errors |= BIT(3);
 775		}
 776	}
 777	if ((rxon->node_addr[0] | rxon->bssid_addr[0]) & 0x1) {
 778		IWL_WARN(priv, "mac/bssid mcast!\n");
 779		errors |= BIT(4);
 780	}
 781
 782	/* make sure basic rates 6Mbps and 1Mbps are supported */
 783	if ((rxon->ofdm_basic_rates & IWL_RATE_6M_MASK) == 0 &&
 784	    (rxon->cck_basic_rates & IWL_RATE_1M_MASK) == 0) {
 785		IWL_WARN(priv, "neither 1 nor 6 are basic\n");
 786		errors |= BIT(5);
 787	}
 788
 789	if (le16_to_cpu(rxon->assoc_id) > 2007) {
 790		IWL_WARN(priv, "aid > 2007\n");
 791		errors |= BIT(6);
 792	}
 793
 794	if ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK))
 795			== (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK)) {
 796		IWL_WARN(priv, "CCK and short slot\n");
 797		errors |= BIT(7);
 798	}
 799
 800	if ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK))
 801			== (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK)) {
 802		IWL_WARN(priv, "CCK and auto detect\n");
 803		errors |= BIT(8);
 804	}
 805
 806	if ((rxon->flags & (RXON_FLG_AUTO_DETECT_MSK |
 807			    RXON_FLG_TGG_PROTECT_MSK)) ==
 808			    RXON_FLG_TGG_PROTECT_MSK) {
 809		IWL_WARN(priv, "TGg but no auto-detect\n");
 810		errors |= BIT(9);
 811	}
 812
 813	if (rxon->channel == 0) {
 814		IWL_WARN(priv, "zero channel is invalid\n");
 815		errors |= BIT(10);
 816	}
 817
 818	WARN(errors, "Invalid RXON (%#x), channel %d",
 819	     errors, le16_to_cpu(rxon->channel));
 820
 821	return errors ? -EINVAL : 0;
 822}
 823
 824/*
 825 * iwl_full_rxon_required - check if full RXON (vs RXON_ASSOC) cmd is needed
 826 * @priv: staging_rxon is compared to active_rxon
 827 *
 828 * If the RXON structure is changing enough to require a new tune,
 829 * or is clearing the RXON_FILTER_ASSOC_MSK, then return 1 to indicate that
 830 * a new tune (full RXON command, rather than RXON_ASSOC cmd) is required.
 831 */
 832static int iwl_full_rxon_required(struct iwl_priv *priv,
 833				  struct iwl_rxon_context *ctx)
 834{
 835	const struct iwl_rxon_cmd *staging = &ctx->staging;
 836	const struct iwl_rxon_cmd *active = &ctx->active;
 837
 838#define CHK(cond)							\
 839	if ((cond)) {							\
 840		IWL_DEBUG_INFO(priv, "need full RXON - " #cond "\n");	\
 841		return 1;						\
 842	}
 843
 844#define CHK_NEQ(c1, c2)						\
 845	if ((c1) != (c2)) {					\
 846		IWL_DEBUG_INFO(priv, "need full RXON - "	\
 847			       #c1 " != " #c2 " - %d != %d\n",	\
 848			       (c1), (c2));			\
 849		return 1;					\
 850	}
 851
 852	/* These items are only settable from the full RXON command */
 853	CHK(!iwl_is_associated_ctx(ctx));
 854	CHK(!ether_addr_equal(staging->bssid_addr, active->bssid_addr));
 855	CHK(!ether_addr_equal(staging->node_addr, active->node_addr));
 856	CHK(!ether_addr_equal(staging->wlap_bssid_addr,
 857			      active->wlap_bssid_addr));
 858	CHK_NEQ(staging->dev_type, active->dev_type);
 859	CHK_NEQ(staging->channel, active->channel);
 860	CHK_NEQ(staging->air_propagation, active->air_propagation);
 861	CHK_NEQ(staging->ofdm_ht_single_stream_basic_rates,
 862		active->ofdm_ht_single_stream_basic_rates);
 863	CHK_NEQ(staging->ofdm_ht_dual_stream_basic_rates,
 864		active->ofdm_ht_dual_stream_basic_rates);
 865	CHK_NEQ(staging->ofdm_ht_triple_stream_basic_rates,
 866		active->ofdm_ht_triple_stream_basic_rates);
 867	CHK_NEQ(staging->assoc_id, active->assoc_id);
 868
 869	/* flags, filter_flags, ofdm_basic_rates, and cck_basic_rates can
 870	 * be updated with the RXON_ASSOC command -- however only some
 871	 * flag transitions are allowed using RXON_ASSOC */
 872
 873	/* Check if we are not switching bands */
 874	CHK_NEQ(staging->flags & RXON_FLG_BAND_24G_MSK,
 875		active->flags & RXON_FLG_BAND_24G_MSK);
 876
 877	/* Check if we are switching association toggle */
 878	CHK_NEQ(staging->filter_flags & RXON_FILTER_ASSOC_MSK,
 879		active->filter_flags & RXON_FILTER_ASSOC_MSK);
 880
 881#undef CHK
 882#undef CHK_NEQ
 883
 884	return 0;
 885}
 886
 887#ifdef CONFIG_IWLWIFI_DEBUG
 888void iwl_print_rx_config_cmd(struct iwl_priv *priv,
 889			     enum iwl_rxon_context_id ctxid)
 890{
 891	struct iwl_rxon_context *ctx = &priv->contexts[ctxid];
 892	struct iwl_rxon_cmd *rxon = &ctx->staging;
 893
 894	IWL_DEBUG_RADIO(priv, "RX CONFIG:\n");
 895	iwl_print_hex_dump(priv, IWL_DL_RADIO, (u8 *) rxon, sizeof(*rxon));
 896	IWL_DEBUG_RADIO(priv, "u16 channel: 0x%x\n",
 897			le16_to_cpu(rxon->channel));
 898	IWL_DEBUG_RADIO(priv, "u32 flags: 0x%08X\n",
 899			le32_to_cpu(rxon->flags));
 900	IWL_DEBUG_RADIO(priv, "u32 filter_flags: 0x%08x\n",
 901			le32_to_cpu(rxon->filter_flags));
 902	IWL_DEBUG_RADIO(priv, "u8 dev_type: 0x%x\n", rxon->dev_type);
 903	IWL_DEBUG_RADIO(priv, "u8 ofdm_basic_rates: 0x%02x\n",
 904			rxon->ofdm_basic_rates);
 905	IWL_DEBUG_RADIO(priv, "u8 cck_basic_rates: 0x%02x\n",
 906			rxon->cck_basic_rates);
 907	IWL_DEBUG_RADIO(priv, "u8[6] node_addr: %pM\n", rxon->node_addr);
 908	IWL_DEBUG_RADIO(priv, "u8[6] bssid_addr: %pM\n", rxon->bssid_addr);
 909	IWL_DEBUG_RADIO(priv, "u16 assoc_id: 0x%x\n",
 910			le16_to_cpu(rxon->assoc_id));
 911}
 912#endif
 913
 914static void iwl_calc_basic_rates(struct iwl_priv *priv,
 915				 struct iwl_rxon_context *ctx)
 916{
 917	int lowest_present_ofdm = 100;
 918	int lowest_present_cck = 100;
 919	u8 cck = 0;
 920	u8 ofdm = 0;
 921
 922	if (ctx->vif) {
 923		struct ieee80211_supported_band *sband;
 924		unsigned long basic = ctx->vif->bss_conf.basic_rates;
 925		int i;
 926
 927		sband = priv->hw->wiphy->bands[priv->hw->conf.chandef.chan->band];
 928
 929		for_each_set_bit(i, &basic, BITS_PER_LONG) {
 930			int hw = sband->bitrates[i].hw_value;
 931			if (hw >= IWL_FIRST_OFDM_RATE) {
 932				ofdm |= BIT(hw - IWL_FIRST_OFDM_RATE);
 933				if (lowest_present_ofdm > hw)
 934					lowest_present_ofdm = hw;
 935			} else {
 936				BUILD_BUG_ON(IWL_FIRST_CCK_RATE != 0);
 937
 938				cck |= BIT(hw);
 939				if (lowest_present_cck > hw)
 940					lowest_present_cck = hw;
 941			}
 942		}
 943	}
 944
 945	/*
 946	 * Now we've got the basic rates as bitmaps in the ofdm and cck
 947	 * variables. This isn't sufficient though, as there might not
 948	 * be all the right rates in the bitmap. E.g. if the only basic
 949	 * rates are 5.5 Mbps and 11 Mbps, we still need to add 1 Mbps
 950	 * and 6 Mbps because the 802.11-2007 standard says in 9.6:
 951	 *
 952	 *    [...] a STA responding to a received frame shall transmit
 953	 *    its Control Response frame [...] at the highest rate in the
 954	 *    BSSBasicRateSet parameter that is less than or equal to the
 955	 *    rate of the immediately previous frame in the frame exchange
 956	 *    sequence ([...]) and that is of the same modulation class
 957	 *    ([...]) as the received frame. If no rate contained in the
 958	 *    BSSBasicRateSet parameter meets these conditions, then the
 959	 *    control frame sent in response to a received frame shall be
 960	 *    transmitted at the highest mandatory rate of the PHY that is
 961	 *    less than or equal to the rate of the received frame, and
 962	 *    that is of the same modulation class as the received frame.
 963	 *
 964	 * As a consequence, we need to add all mandatory rates that are
 965	 * lower than all of the basic rates to these bitmaps.
 966	 */
 967
 968	if (IWL_RATE_24M_INDEX < lowest_present_ofdm)
 969		ofdm |= IWL_RATE_24M_MASK >> IWL_FIRST_OFDM_RATE;
 970	if (IWL_RATE_12M_INDEX < lowest_present_ofdm)
 971		ofdm |= IWL_RATE_12M_MASK >> IWL_FIRST_OFDM_RATE;
 972	/* 6M already there or needed so always add */
 973	ofdm |= IWL_RATE_6M_MASK >> IWL_FIRST_OFDM_RATE;
 974
 975	/*
 976	 * CCK is a bit more complex with DSSS vs. HR/DSSS vs. ERP.
 977	 * Note, however:
 978	 *  - if no CCK rates are basic, it must be ERP since there must
 979	 *    be some basic rates at all, so they're OFDM => ERP PHY
 980	 *    (or we're in 5 GHz, and the cck bitmap will never be used)
 981	 *  - if 11M is a basic rate, it must be ERP as well, so add 5.5M
 982	 *  - if 5.5M is basic, 1M and 2M are mandatory
 983	 *  - if 2M is basic, 1M is mandatory
 984	 *  - if 1M is basic, that's the only valid ACK rate.
 985	 * As a consequence, it's not as complicated as it sounds, just add
 986	 * any lower rates to the ACK rate bitmap.
 987	 */
 988	if (IWL_RATE_11M_INDEX < lowest_present_cck)
 989		cck |= IWL_RATE_11M_MASK >> IWL_FIRST_CCK_RATE;
 990	if (IWL_RATE_5M_INDEX < lowest_present_cck)
 991		cck |= IWL_RATE_5M_MASK >> IWL_FIRST_CCK_RATE;
 992	if (IWL_RATE_2M_INDEX < lowest_present_cck)
 993		cck |= IWL_RATE_2M_MASK >> IWL_FIRST_CCK_RATE;
 994	/* 1M already there or needed so always add */
 995	cck |= IWL_RATE_1M_MASK >> IWL_FIRST_CCK_RATE;
 996
 997	IWL_DEBUG_RATE(priv, "Set basic rates cck:0x%.2x ofdm:0x%.2x\n",
 998		       cck, ofdm);
 999
1000	/* "basic_rates" is a misnomer here -- should be called ACK rates */
1001	ctx->staging.cck_basic_rates = cck;
1002	ctx->staging.ofdm_basic_rates = ofdm;
1003}
1004
1005/*
1006 * iwlagn_commit_rxon - commit staging_rxon to hardware
1007 *
1008 * The RXON command in staging_rxon is committed to the hardware and
1009 * the active_rxon structure is updated with the new data.  This
1010 * function correctly transitions out of the RXON_ASSOC_MSK state if
1011 * a HW tune is required based on the RXON structure changes.
1012 *
1013 * The connect/disconnect flow should be as the following:
1014 *
1015 * 1. make sure send RXON command with association bit unset if not connect
1016 *	this should include the channel and the band for the candidate
1017 *	to be connected to
1018 * 2. Add Station before RXON association with the AP
1019 * 3. RXON_timing has to send before RXON for connection
1020 * 4. full RXON command - associated bit set
1021 * 5. use RXON_ASSOC command to update any flags changes
1022 */
1023int iwlagn_commit_rxon(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
1024{
1025	/* cast away the const for active_rxon in this function */
1026	struct iwl_rxon_cmd *active = (void *)&ctx->active;
1027	bool new_assoc = !!(ctx->staging.filter_flags & RXON_FILTER_ASSOC_MSK);
1028	int ret;
1029
1030	lockdep_assert_held(&priv->mutex);
1031
1032	if (!iwl_is_alive(priv))
1033		return -EBUSY;
1034
1035	/* This function hardcodes a bunch of dual-mode assumptions */
1036	BUILD_BUG_ON(NUM_IWL_RXON_CTX != 2);
1037
1038	if (!ctx->is_active)
1039		return 0;
1040
1041	/* always get timestamp with Rx frame */
1042	ctx->staging.flags |= RXON_FLG_TSF2HOST_MSK;
1043
1044	/* recalculate basic rates */
1045	iwl_calc_basic_rates(priv, ctx);
1046
1047	/*
1048	 * force CTS-to-self frames protection if RTS-CTS is not preferred
1049	 * one aggregation protection method
1050	 */
1051	if (!priv->hw_params.use_rts_for_aggregation)
1052		ctx->staging.flags |= RXON_FLG_SELF_CTS_EN;
1053
1054	if ((ctx->vif && ctx->vif->bss_conf.use_short_slot) ||
1055	    !(ctx->staging.flags & RXON_FLG_BAND_24G_MSK))
1056		ctx->staging.flags |= RXON_FLG_SHORT_SLOT_MSK;
1057	else
1058		ctx->staging.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
1059
1060	iwl_print_rx_config_cmd(priv, ctx->ctxid);
1061	ret = iwl_check_rxon_cmd(priv, ctx);
1062	if (ret) {
1063		IWL_ERR(priv, "Invalid RXON configuration. Not committing.\n");
1064		return -EINVAL;
1065	}
1066
1067	/*
1068	 * receive commit_rxon request
1069	 * abort any previous channel switch if still in process
1070	 */
1071	if (test_bit(STATUS_CHANNEL_SWITCH_PENDING, &priv->status) &&
1072	    (priv->switch_channel != ctx->staging.channel)) {
1073		IWL_DEBUG_11H(priv, "abort channel switch on %d\n",
1074			      le16_to_cpu(priv->switch_channel));
1075		iwl_chswitch_done(priv, false);
1076	}
1077
1078	/*
1079	 * If we don't need to send a full RXON, we can use
1080	 * iwl_rxon_assoc_cmd which is used to reconfigure filter
1081	 * and other flags for the current radio configuration.
1082	 */
1083	if (!iwl_full_rxon_required(priv, ctx)) {
1084		ret = iwlagn_send_rxon_assoc(priv, ctx);
1085		if (ret) {
1086			IWL_ERR(priv, "Error setting RXON_ASSOC (%d)\n", ret);
1087			return ret;
1088		}
1089
1090		memcpy(active, &ctx->staging, sizeof(*active));
1091		/*
1092		 * We do not commit tx power settings while channel changing,
1093		 * do it now if after settings changed.
1094		 */
1095		iwl_set_tx_power(priv, priv->tx_power_next, false);
1096
1097		/* make sure we are in the right PS state */
1098		iwl_power_update_mode(priv, true);
1099
1100		return 0;
1101	}
1102
1103	iwl_set_rxon_hwcrypto(priv, ctx, !iwlwifi_mod_params.swcrypto);
1104
1105	IWL_DEBUG_INFO(priv,
1106		       "Going to commit RXON\n"
1107		       "  * with%s RXON_FILTER_ASSOC_MSK\n"
1108		       "  * channel = %d\n"
1109		       "  * bssid = %pM\n",
1110		       (new_assoc ? "" : "out"),
1111		       le16_to_cpu(ctx->staging.channel),
1112		       ctx->staging.bssid_addr);
1113
1114	/*
1115	 * Always clear associated first, but with the correct config.
1116	 * This is required as for example station addition for the
1117	 * AP station must be done after the BSSID is set to correctly
1118	 * set up filters in the device.
1119	 */
1120	ret = iwlagn_rxon_disconn(priv, ctx);
1121	if (ret)
1122		return ret;
1123
1124	ret = iwlagn_set_pan_params(priv);
1125	if (ret)
1126		return ret;
1127
1128	if (new_assoc)
1129		return iwlagn_rxon_connect(priv, ctx);
1130
1131	return 0;
1132}
1133
1134void iwlagn_config_ht40(struct ieee80211_conf *conf,
1135			struct iwl_rxon_context *ctx)
1136{
1137	if (conf_is_ht40_minus(conf)) {
1138		ctx->ht.extension_chan_offset =
1139			IEEE80211_HT_PARAM_CHA_SEC_BELOW;
1140		ctx->ht.is_40mhz = true;
1141	} else if (conf_is_ht40_plus(conf)) {
1142		ctx->ht.extension_chan_offset =
1143			IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
1144		ctx->ht.is_40mhz = true;
1145	} else {
1146		ctx->ht.extension_chan_offset =
1147			IEEE80211_HT_PARAM_CHA_SEC_NONE;
1148		ctx->ht.is_40mhz = false;
1149	}
1150}
1151
1152int iwlagn_mac_config(struct ieee80211_hw *hw, u32 changed)
1153{
1154	struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
1155	struct iwl_rxon_context *ctx;
1156	struct ieee80211_conf *conf = &hw->conf;
1157	struct ieee80211_channel *channel = conf->chandef.chan;
1158	int ret = 0;
1159
1160	IWL_DEBUG_MAC80211(priv, "enter: changed %#x\n", changed);
1161
1162	mutex_lock(&priv->mutex);
1163
1164	if (unlikely(test_bit(STATUS_SCANNING, &priv->status))) {
1165		IWL_DEBUG_MAC80211(priv, "leave - scanning\n");
1166		goto out;
1167	}
1168
1169	if (!iwl_is_ready(priv)) {
1170		IWL_DEBUG_MAC80211(priv, "leave - not ready\n");
1171		goto out;
1172	}
1173
1174	if (changed & (IEEE80211_CONF_CHANGE_SMPS |
1175		       IEEE80211_CONF_CHANGE_CHANNEL)) {
1176		/* mac80211 uses static for non-HT which is what we want */
1177		priv->current_ht_config.smps = conf->smps_mode;
1178
1179		/*
1180		 * Recalculate chain counts.
1181		 *
1182		 * If monitor mode is enabled then mac80211 will
1183		 * set up the SM PS mode to OFF if an HT channel is
1184		 * configured.
1185		 */
1186		for_each_context(priv, ctx)
1187			iwlagn_set_rxon_chain(priv, ctx);
1188	}
1189
1190	if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
1191		for_each_context(priv, ctx) {
1192			/* Configure HT40 channels */
1193			if (ctx->ht.enabled != conf_is_ht(conf))
1194				ctx->ht.enabled = conf_is_ht(conf);
1195
1196			if (ctx->ht.enabled) {
1197				/* if HT40 is used, it should not change
1198				 * after associated except channel switch */
1199				if (!ctx->ht.is_40mhz ||
1200						!iwl_is_associated_ctx(ctx))
1201					iwlagn_config_ht40(conf, ctx);
1202			} else
1203				ctx->ht.is_40mhz = false;
1204
1205			/*
1206			 * Default to no protection. Protection mode will
1207			 * later be set from BSS config in iwl_ht_conf
1208			 */
1209			ctx->ht.protection = IEEE80211_HT_OP_MODE_PROTECTION_NONE;
1210
1211			/* if we are switching from ht to 2.4 clear flags
1212			 * from any ht related info since 2.4 does not
1213			 * support ht */
1214			if (le16_to_cpu(ctx->staging.channel) !=
1215			    channel->hw_value)
1216				ctx->staging.flags = 0;
1217
1218			iwl_set_rxon_channel(priv, channel, ctx);
1219			iwl_set_rxon_ht(priv, &priv->current_ht_config);
1220
1221			iwl_set_flags_for_band(priv, ctx, channel->band,
1222					       ctx->vif);
1223		}
1224
1225		iwl_update_bcast_stations(priv);
1226	}
1227
1228	if (changed & (IEEE80211_CONF_CHANGE_PS |
1229			IEEE80211_CONF_CHANGE_IDLE)) {
1230		ret = iwl_power_update_mode(priv, false);
1231		if (ret)
1232			IWL_DEBUG_MAC80211(priv, "Error setting sleep level\n");
1233	}
1234
1235	if (changed & IEEE80211_CONF_CHANGE_POWER) {
1236		IWL_DEBUG_MAC80211(priv, "TX Power old=%d new=%d\n",
1237			priv->tx_power_user_lmt, conf->power_level);
1238
1239		iwl_set_tx_power(priv, conf->power_level, false);
1240	}
1241
1242	for_each_context(priv, ctx) {
1243		if (!memcmp(&ctx->staging, &ctx->active, sizeof(ctx->staging)))
1244			continue;
1245		iwlagn_commit_rxon(priv, ctx);
1246	}
1247 out:
1248	mutex_unlock(&priv->mutex);
1249	IWL_DEBUG_MAC80211(priv, "leave\n");
1250
1251	return ret;
1252}
1253
1254static void iwlagn_check_needed_chains(struct iwl_priv *priv,
1255				       struct iwl_rxon_context *ctx,
1256				       struct ieee80211_bss_conf *bss_conf)
1257{
1258	struct ieee80211_vif *vif = ctx->vif;
1259	struct iwl_rxon_context *tmp;
1260	struct ieee80211_sta *sta;
1261	struct iwl_ht_config *ht_conf = &priv->current_ht_config;
1262	struct ieee80211_sta_ht_cap *ht_cap;
1263	bool need_multiple;
1264
1265	lockdep_assert_held(&priv->mutex);
1266
1267	switch (vif->type) {
1268	case NL80211_IFTYPE_STATION:
1269		rcu_read_lock();
1270		sta = ieee80211_find_sta(vif, bss_conf->bssid);
1271		if (!sta) {
1272			/*
1273			 * If at all, this can only happen through a race
1274			 * when the AP disconnects us while we're still
1275			 * setting up the connection, in that case mac80211
1276			 * will soon tell us about that.
1277			 */
1278			need_multiple = false;
1279			rcu_read_unlock();
1280			break;
1281		}
1282
1283		ht_cap = &sta->deflink.ht_cap;
1284
1285		need_multiple = true;
1286
1287		/*
1288		 * If the peer advertises no support for receiving 2 and 3
1289		 * stream MCS rates, it can't be transmitting them either.
1290		 */
1291		if (ht_cap->mcs.rx_mask[1] == 0 &&
1292		    ht_cap->mcs.rx_mask[2] == 0) {
1293			need_multiple = false;
1294		} else if (!(ht_cap->mcs.tx_params &
1295						IEEE80211_HT_MCS_TX_DEFINED)) {
1296			/* If it can't TX MCS at all ... */
1297			need_multiple = false;
1298		} else if (ht_cap->mcs.tx_params &
1299						IEEE80211_HT_MCS_TX_RX_DIFF) {
1300			int maxstreams;
1301
1302			/*
1303			 * But if it can receive them, it might still not
1304			 * be able to transmit them, which is what we need
1305			 * to check here -- so check the number of streams
1306			 * it advertises for TX (if different from RX).
1307			 */
1308
1309			maxstreams = (ht_cap->mcs.tx_params &
1310				 IEEE80211_HT_MCS_TX_MAX_STREAMS_MASK);
1311			maxstreams >>=
1312				IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT;
1313			maxstreams += 1;
1314
1315			if (maxstreams <= 1)
1316				need_multiple = false;
1317		}
1318
1319		rcu_read_unlock();
1320		break;
1321	case NL80211_IFTYPE_ADHOC:
1322		/* currently */
1323		need_multiple = false;
1324		break;
1325	default:
1326		/* only AP really */
1327		need_multiple = true;
1328		break;
1329	}
1330
1331	ctx->ht_need_multiple_chains = need_multiple;
1332
1333	if (!need_multiple) {
1334		/* check all contexts */
1335		for_each_context(priv, tmp) {
1336			if (!tmp->vif)
1337				continue;
1338			if (tmp->ht_need_multiple_chains) {
1339				need_multiple = true;
1340				break;
1341			}
1342		}
1343	}
1344
1345	ht_conf->single_chain_sufficient = !need_multiple;
1346}
1347
1348static void iwlagn_chain_noise_reset(struct iwl_priv *priv)
1349{
1350	struct iwl_chain_noise_data *data = &priv->chain_noise_data;
1351	int ret;
1352
1353	if (priv->calib_disabled & IWL_CHAIN_NOISE_CALIB_DISABLED)
1354		return;
1355
1356	if ((data->state == IWL_CHAIN_NOISE_ALIVE) &&
1357	    iwl_is_any_associated(priv)) {
1358		struct iwl_calib_chain_noise_reset_cmd cmd;
1359
1360		/* clear data for chain noise calibration algorithm */
1361		data->chain_noise_a = 0;
1362		data->chain_noise_b = 0;
1363		data->chain_noise_c = 0;
1364		data->chain_signal_a = 0;
1365		data->chain_signal_b = 0;
1366		data->chain_signal_c = 0;
1367		data->beacon_count = 0;
1368
1369		memset(&cmd, 0, sizeof(cmd));
1370		iwl_set_calib_hdr(&cmd.hdr,
1371			priv->phy_calib_chain_noise_reset_cmd);
1372		ret = iwl_dvm_send_cmd_pdu(priv,
1373					REPLY_PHY_CALIBRATION_CMD,
1374					0, sizeof(cmd), &cmd);
1375		if (ret)
1376			IWL_ERR(priv,
1377				"Could not send REPLY_PHY_CALIBRATION_CMD\n");
1378		data->state = IWL_CHAIN_NOISE_ACCUMULATE;
1379		IWL_DEBUG_CALIB(priv, "Run chain_noise_calibrate\n");
1380	}
1381}
1382
1383void iwlagn_bss_info_changed(struct ieee80211_hw *hw,
1384			     struct ieee80211_vif *vif,
1385			     struct ieee80211_bss_conf *bss_conf,
1386			     u64 changes)
1387{
1388	struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
1389	struct iwl_rxon_context *ctx = iwl_rxon_ctx_from_vif(vif);
1390	int ret;
1391	bool force = false;
1392
1393	mutex_lock(&priv->mutex);
1394
1395	if (changes & BSS_CHANGED_IDLE && vif->cfg.idle) {
1396		/*
1397		 * If we go idle, then clearly no "passive-no-rx"
1398		 * workaround is needed any more, this is a reset.
1399		 */
1400		iwlagn_lift_passive_no_rx(priv);
1401	}
1402
1403	if (unlikely(!iwl_is_ready(priv))) {
1404		IWL_DEBUG_MAC80211(priv, "leave - not ready\n");
1405		mutex_unlock(&priv->mutex);
1406		return;
1407        }
1408
1409	if (unlikely(!ctx->vif)) {
1410		IWL_DEBUG_MAC80211(priv, "leave - vif is NULL\n");
1411		mutex_unlock(&priv->mutex);
1412		return;
1413	}
1414
1415	if (changes & BSS_CHANGED_BEACON_INT)
1416		force = true;
1417
1418	if (changes & BSS_CHANGED_QOS) {
1419		ctx->qos_data.qos_active = bss_conf->qos;
1420		iwlagn_update_qos(priv, ctx);
1421	}
1422
1423	ctx->staging.assoc_id = cpu_to_le16(vif->cfg.aid);
1424	if (vif->bss_conf.use_short_preamble)
1425		ctx->staging.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
1426	else
1427		ctx->staging.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
1428
1429	if (changes & BSS_CHANGED_ASSOC) {
1430		if (vif->cfg.assoc) {
1431			priv->timestamp = bss_conf->sync_tsf;
1432			ctx->staging.filter_flags |= RXON_FILTER_ASSOC_MSK;
1433		} else {
1434			ctx->staging.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
1435
1436			if (ctx->ctxid == IWL_RXON_CTX_BSS)
1437				priv->have_rekey_data = false;
1438		}
1439
1440		iwlagn_bt_coex_rssi_monitor(priv);
1441	}
1442
1443	if (ctx->ht.enabled) {
1444		ctx->ht.protection = bss_conf->ht_operation_mode &
1445					IEEE80211_HT_OP_MODE_PROTECTION;
1446		ctx->ht.non_gf_sta_present = !!(bss_conf->ht_operation_mode &
1447					IEEE80211_HT_OP_MODE_NON_GF_STA_PRSNT);
1448		iwlagn_check_needed_chains(priv, ctx, bss_conf);
1449		iwl_set_rxon_ht(priv, &priv->current_ht_config);
1450	}
1451
1452	iwlagn_set_rxon_chain(priv, ctx);
1453
1454	if (bss_conf->use_cts_prot && (priv->band != NL80211_BAND_5GHZ))
1455		ctx->staging.flags |= RXON_FLG_TGG_PROTECT_MSK;
1456	else
1457		ctx->staging.flags &= ~RXON_FLG_TGG_PROTECT_MSK;
1458
1459	if (bss_conf->use_cts_prot)
1460		ctx->staging.flags |= RXON_FLG_SELF_CTS_EN;
1461	else
1462		ctx->staging.flags &= ~RXON_FLG_SELF_CTS_EN;
1463
1464	memcpy(ctx->staging.bssid_addr, bss_conf->bssid, ETH_ALEN);
1465
1466	if (vif->type == NL80211_IFTYPE_AP ||
1467	    vif->type == NL80211_IFTYPE_ADHOC) {
1468		if (vif->bss_conf.enable_beacon) {
1469			ctx->staging.filter_flags |= RXON_FILTER_ASSOC_MSK;
1470			priv->beacon_ctx = ctx;
1471		} else {
1472			ctx->staging.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
1473			priv->beacon_ctx = NULL;
1474		}
1475	}
1476
1477	/*
1478	 * If the ucode decides to do beacon filtering before
1479	 * association, it will lose beacons that are needed
1480	 * before sending frames out on passive channels. This
1481	 * causes association failures on those channels. Enable
1482	 * receiving beacons in such cases.
1483	 */
1484
1485	if (vif->type == NL80211_IFTYPE_STATION) {
1486		if (!vif->cfg.assoc)
1487			ctx->staging.filter_flags |= RXON_FILTER_BCON_AWARE_MSK;
1488		else
1489			ctx->staging.filter_flags &=
1490						    ~RXON_FILTER_BCON_AWARE_MSK;
1491	}
1492
1493	if (force || memcmp(&ctx->staging, &ctx->active, sizeof(ctx->staging)))
1494		iwlagn_commit_rxon(priv, ctx);
1495
1496	if (changes & BSS_CHANGED_ASSOC && vif->cfg.assoc) {
1497		/*
1498		 * The chain noise calibration will enable PM upon
1499		 * completion. If calibration has already been run
1500		 * then we need to enable power management here.
1501		 */
1502		if (priv->chain_noise_data.state == IWL_CHAIN_NOISE_DONE)
1503			iwl_power_update_mode(priv, false);
1504
1505		/* Enable RX differential gain and sensitivity calibrations */
1506		iwlagn_chain_noise_reset(priv);
1507		priv->start_calib = 1;
1508	}
1509
1510	if (changes & BSS_CHANGED_IBSS) {
1511		ret = iwlagn_manage_ibss_station(priv, vif,
1512						 vif->cfg.ibss_joined);
1513		if (ret)
1514			IWL_ERR(priv, "failed to %s IBSS station %pM\n",
1515				vif->cfg.ibss_joined ? "add" : "remove",
1516				bss_conf->bssid);
1517	}
1518
1519	if (changes & BSS_CHANGED_BEACON && priv->beacon_ctx == ctx) {
1520		if (iwlagn_update_beacon(priv, vif))
1521			IWL_ERR(priv, "Error updating beacon\n");
1522	}
1523
1524	mutex_unlock(&priv->mutex);
1525}
1526
1527void iwlagn_post_scan(struct iwl_priv *priv)
1528{
1529	struct iwl_rxon_context *ctx;
1530
1531	/*
1532	 * We do not commit power settings while scan is pending,
1533	 * do it now if the settings changed.
1534	 */
1535	iwl_power_set_mode(priv, &priv->power_data.sleep_cmd_next, false);
1536	iwl_set_tx_power(priv, priv->tx_power_next, false);
1537
1538	/*
1539	 * Since setting the RXON may have been deferred while
1540	 * performing the scan, fire one off if needed
1541	 */
1542	for_each_context(priv, ctx)
1543		if (memcmp(&ctx->staging, &ctx->active, sizeof(ctx->staging)))
1544			iwlagn_commit_rxon(priv, ctx);
1545
1546	iwlagn_set_pan_params(priv);
1547}
v4.6
 
   1/******************************************************************************
   2 *
   3 * Copyright(c) 2003 - 2014 Intel Corporation. All rights reserved.
   4 * Copyright(c) 2015 Intel Deutschland GmbH
   5 *
   6 * This program is free software; you can redistribute it and/or modify it
   7 * under the terms of version 2 of the GNU General Public License as
   8 * published by the Free Software Foundation.
   9 *
  10 * This program is distributed in the hope that it will be useful, but WITHOUT
  11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
  13 * more details.
  14 *
  15 * You should have received a copy of the GNU General Public License along with
  16 * this program; if not, write to the Free Software Foundation, Inc.,
  17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
  18 *
  19 * The full GNU General Public License is included in this distribution in the
  20 * file called LICENSE.
  21 *
  22 * Contact Information:
  23 * Intel Linux Wireless <linuxwifi@intel.com>
  24 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  25 *
  26 *****************************************************************************/
  27
  28#include <linux/etherdevice.h>
  29#include "iwl-trans.h"
  30#include "iwl-modparams.h"
  31#include "dev.h"
  32#include "agn.h"
  33#include "calib.h"
  34
  35/*
  36 * initialize rxon structure with default values from eeprom
  37 */
  38void iwl_connection_init_rx_config(struct iwl_priv *priv,
  39				   struct iwl_rxon_context *ctx)
  40{
  41	memset(&ctx->staging, 0, sizeof(ctx->staging));
  42
  43	if (!ctx->vif) {
  44		ctx->staging.dev_type = ctx->unused_devtype;
  45	} else
  46	switch (ctx->vif->type) {
  47	case NL80211_IFTYPE_AP:
  48		ctx->staging.dev_type = ctx->ap_devtype;
  49		break;
  50
  51	case NL80211_IFTYPE_STATION:
  52		ctx->staging.dev_type = ctx->station_devtype;
  53		ctx->staging.filter_flags = RXON_FILTER_ACCEPT_GRP_MSK;
  54		break;
  55
  56	case NL80211_IFTYPE_ADHOC:
  57		ctx->staging.dev_type = ctx->ibss_devtype;
  58		ctx->staging.flags = RXON_FLG_SHORT_PREAMBLE_MSK;
  59		ctx->staging.filter_flags = RXON_FILTER_BCON_AWARE_MSK |
  60						  RXON_FILTER_ACCEPT_GRP_MSK;
  61		break;
  62
  63	case NL80211_IFTYPE_MONITOR:
  64		ctx->staging.dev_type = RXON_DEV_TYPE_SNIFFER;
  65		break;
  66
  67	default:
  68		IWL_ERR(priv, "Unsupported interface type %d\n",
  69			ctx->vif->type);
  70		break;
  71	}
  72
  73#if 0
  74	/* TODO:  Figure out when short_preamble would be set and cache from
  75	 * that */
  76	if (!hw_to_local(priv->hw)->short_preamble)
  77		ctx->staging.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
  78	else
  79		ctx->staging.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
  80#endif
  81
  82	ctx->staging.channel =
  83		cpu_to_le16(priv->hw->conf.chandef.chan->hw_value);
  84	priv->band = priv->hw->conf.chandef.chan->band;
  85
  86	iwl_set_flags_for_band(priv, ctx, priv->band, ctx->vif);
  87
  88	/* clear both MIX and PURE40 mode flag */
  89	ctx->staging.flags &= ~(RXON_FLG_CHANNEL_MODE_MIXED |
  90					RXON_FLG_CHANNEL_MODE_PURE_40);
  91	if (ctx->vif)
  92		memcpy(ctx->staging.node_addr, ctx->vif->addr, ETH_ALEN);
  93
  94	ctx->staging.ofdm_ht_single_stream_basic_rates = 0xff;
  95	ctx->staging.ofdm_ht_dual_stream_basic_rates = 0xff;
  96	ctx->staging.ofdm_ht_triple_stream_basic_rates = 0xff;
  97}
  98
  99static int iwlagn_disable_bss(struct iwl_priv *priv,
 100			      struct iwl_rxon_context *ctx,
 101			      struct iwl_rxon_cmd *send)
 102{
 103	__le32 old_filter = send->filter_flags;
 104	int ret;
 105
 106	send->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
 107	ret = iwl_dvm_send_cmd_pdu(priv, ctx->rxon_cmd,
 108				0, sizeof(*send), send);
 109
 110	send->filter_flags = old_filter;
 111
 112	if (ret)
 113		IWL_DEBUG_QUIET_RFKILL(priv,
 114			"Error clearing ASSOC_MSK on BSS (%d)\n", ret);
 115
 116	return ret;
 117}
 118
 119static int iwlagn_disable_pan(struct iwl_priv *priv,
 120			      struct iwl_rxon_context *ctx,
 121			      struct iwl_rxon_cmd *send)
 122{
 123	struct iwl_notification_wait disable_wait;
 124	__le32 old_filter = send->filter_flags;
 125	u8 old_dev_type = send->dev_type;
 126	int ret;
 127	static const u16 deactivate_cmd[] = {
 128		REPLY_WIPAN_DEACTIVATION_COMPLETE
 129	};
 130
 131	iwl_init_notification_wait(&priv->notif_wait, &disable_wait,
 132				   deactivate_cmd, ARRAY_SIZE(deactivate_cmd),
 133				   NULL, NULL);
 134
 135	send->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
 136	send->dev_type = RXON_DEV_TYPE_P2P;
 137	ret = iwl_dvm_send_cmd_pdu(priv, ctx->rxon_cmd,
 138				0, sizeof(*send), send);
 139
 140	send->filter_flags = old_filter;
 141	send->dev_type = old_dev_type;
 142
 143	if (ret) {
 144		IWL_ERR(priv, "Error disabling PAN (%d)\n", ret);
 145		iwl_remove_notification(&priv->notif_wait, &disable_wait);
 146	} else {
 147		ret = iwl_wait_notification(&priv->notif_wait,
 148					    &disable_wait, HZ);
 149		if (ret)
 150			IWL_ERR(priv, "Timed out waiting for PAN disable\n");
 151	}
 152
 153	return ret;
 154}
 155
 156static int iwlagn_disconn_pan(struct iwl_priv *priv,
 157			      struct iwl_rxon_context *ctx,
 158			      struct iwl_rxon_cmd *send)
 159{
 160	__le32 old_filter = send->filter_flags;
 161	int ret;
 162
 163	send->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
 164	ret = iwl_dvm_send_cmd_pdu(priv, ctx->rxon_cmd, 0,
 165				sizeof(*send), send);
 166
 167	send->filter_flags = old_filter;
 168
 169	return ret;
 170}
 171
 172static void iwlagn_update_qos(struct iwl_priv *priv,
 173			      struct iwl_rxon_context *ctx)
 174{
 175	int ret;
 176
 177	if (!ctx->is_active)
 178		return;
 179
 180	ctx->qos_data.def_qos_parm.qos_flags = 0;
 181
 182	if (ctx->qos_data.qos_active)
 183		ctx->qos_data.def_qos_parm.qos_flags |=
 184			QOS_PARAM_FLG_UPDATE_EDCA_MSK;
 185
 186	if (ctx->ht.enabled)
 187		ctx->qos_data.def_qos_parm.qos_flags |= QOS_PARAM_FLG_TGN_MSK;
 188
 189	IWL_DEBUG_INFO(priv, "send QoS cmd with Qos active=%d FLAGS=0x%X\n",
 190		      ctx->qos_data.qos_active,
 191		      ctx->qos_data.def_qos_parm.qos_flags);
 192
 193	ret = iwl_dvm_send_cmd_pdu(priv, ctx->qos_cmd, 0,
 194			       sizeof(struct iwl_qosparam_cmd),
 195			       &ctx->qos_data.def_qos_parm);
 196	if (ret)
 197		IWL_DEBUG_QUIET_RFKILL(priv, "Failed to update QoS\n");
 198}
 199
 200static int iwlagn_update_beacon(struct iwl_priv *priv,
 201				struct ieee80211_vif *vif)
 202{
 203	lockdep_assert_held(&priv->mutex);
 204
 205	dev_kfree_skb(priv->beacon_skb);
 206	priv->beacon_skb = ieee80211_beacon_get(priv->hw, vif);
 207	if (!priv->beacon_skb)
 208		return -ENOMEM;
 209	return iwlagn_send_beacon_cmd(priv);
 210}
 211
 212static int iwlagn_send_rxon_assoc(struct iwl_priv *priv,
 213				  struct iwl_rxon_context *ctx)
 214{
 215	int ret = 0;
 216	struct iwl_rxon_assoc_cmd rxon_assoc;
 217	const struct iwl_rxon_cmd *rxon1 = &ctx->staging;
 218	const struct iwl_rxon_cmd *rxon2 = &ctx->active;
 219
 220	if ((rxon1->flags == rxon2->flags) &&
 221	    (rxon1->filter_flags == rxon2->filter_flags) &&
 222	    (rxon1->cck_basic_rates == rxon2->cck_basic_rates) &&
 223	    (rxon1->ofdm_ht_single_stream_basic_rates ==
 224	     rxon2->ofdm_ht_single_stream_basic_rates) &&
 225	    (rxon1->ofdm_ht_dual_stream_basic_rates ==
 226	     rxon2->ofdm_ht_dual_stream_basic_rates) &&
 227	    (rxon1->ofdm_ht_triple_stream_basic_rates ==
 228	     rxon2->ofdm_ht_triple_stream_basic_rates) &&
 229	    (rxon1->acquisition_data == rxon2->acquisition_data) &&
 230	    (rxon1->rx_chain == rxon2->rx_chain) &&
 231	    (rxon1->ofdm_basic_rates == rxon2->ofdm_basic_rates)) {
 232		IWL_DEBUG_INFO(priv, "Using current RXON_ASSOC.  Not resending.\n");
 233		return 0;
 234	}
 235
 236	rxon_assoc.flags = ctx->staging.flags;
 237	rxon_assoc.filter_flags = ctx->staging.filter_flags;
 238	rxon_assoc.ofdm_basic_rates = ctx->staging.ofdm_basic_rates;
 239	rxon_assoc.cck_basic_rates = ctx->staging.cck_basic_rates;
 240	rxon_assoc.reserved1 = 0;
 241	rxon_assoc.reserved2 = 0;
 242	rxon_assoc.reserved3 = 0;
 243	rxon_assoc.ofdm_ht_single_stream_basic_rates =
 244	    ctx->staging.ofdm_ht_single_stream_basic_rates;
 245	rxon_assoc.ofdm_ht_dual_stream_basic_rates =
 246	    ctx->staging.ofdm_ht_dual_stream_basic_rates;
 247	rxon_assoc.rx_chain_select_flags = ctx->staging.rx_chain;
 248	rxon_assoc.ofdm_ht_triple_stream_basic_rates =
 249		 ctx->staging.ofdm_ht_triple_stream_basic_rates;
 250	rxon_assoc.acquisition_data = ctx->staging.acquisition_data;
 251
 252	ret = iwl_dvm_send_cmd_pdu(priv, ctx->rxon_assoc_cmd,
 253				CMD_ASYNC, sizeof(rxon_assoc), &rxon_assoc);
 254	return ret;
 255}
 256
 257static u16 iwl_adjust_beacon_interval(u16 beacon_val, u16 max_beacon_val)
 258{
 259	u16 new_val;
 260	u16 beacon_factor;
 261
 262	/*
 263	 * If mac80211 hasn't given us a beacon interval, program
 264	 * the default into the device (not checking this here
 265	 * would cause the adjustment below to return the maximum
 266	 * value, which may break PAN.)
 267	 */
 268	if (!beacon_val)
 269		return DEFAULT_BEACON_INTERVAL;
 270
 271	/*
 272	 * If the beacon interval we obtained from the peer
 273	 * is too large, we'll have to wake up more often
 274	 * (and in IBSS case, we'll beacon too much)
 275	 *
 276	 * For example, if max_beacon_val is 4096, and the
 277	 * requested beacon interval is 7000, we'll have to
 278	 * use 3500 to be able to wake up on the beacons.
 279	 *
 280	 * This could badly influence beacon detection stats.
 281	 */
 282
 283	beacon_factor = (beacon_val + max_beacon_val) / max_beacon_val;
 284	new_val = beacon_val / beacon_factor;
 285
 286	if (!new_val)
 287		new_val = max_beacon_val;
 288
 289	return new_val;
 290}
 291
 292static int iwl_send_rxon_timing(struct iwl_priv *priv,
 293				struct iwl_rxon_context *ctx)
 294{
 295	u64 tsf;
 296	s32 interval_tm, rem;
 297	struct ieee80211_conf *conf = NULL;
 298	u16 beacon_int;
 299	struct ieee80211_vif *vif = ctx->vif;
 300
 301	conf = &priv->hw->conf;
 302
 303	lockdep_assert_held(&priv->mutex);
 304
 305	memset(&ctx->timing, 0, sizeof(struct iwl_rxon_time_cmd));
 306
 307	ctx->timing.timestamp = cpu_to_le64(priv->timestamp);
 308	ctx->timing.listen_interval = cpu_to_le16(conf->listen_interval);
 309
 310	beacon_int = vif ? vif->bss_conf.beacon_int : 0;
 311
 312	/*
 313	 * TODO: For IBSS we need to get atim_window from mac80211,
 314	 *	 for now just always use 0
 315	 */
 316	ctx->timing.atim_window = 0;
 317
 318	if (ctx->ctxid == IWL_RXON_CTX_PAN &&
 319	    (!ctx->vif || ctx->vif->type != NL80211_IFTYPE_STATION) &&
 320	    iwl_is_associated(priv, IWL_RXON_CTX_BSS) &&
 321	    priv->contexts[IWL_RXON_CTX_BSS].vif &&
 322	    priv->contexts[IWL_RXON_CTX_BSS].vif->bss_conf.beacon_int) {
 323		ctx->timing.beacon_interval =
 324			priv->contexts[IWL_RXON_CTX_BSS].timing.beacon_interval;
 325		beacon_int = le16_to_cpu(ctx->timing.beacon_interval);
 326	} else if (ctx->ctxid == IWL_RXON_CTX_BSS &&
 327		   iwl_is_associated(priv, IWL_RXON_CTX_PAN) &&
 328		   priv->contexts[IWL_RXON_CTX_PAN].vif &&
 329		   priv->contexts[IWL_RXON_CTX_PAN].vif->bss_conf.beacon_int &&
 330		   (!iwl_is_associated_ctx(ctx) || !ctx->vif ||
 331		    !ctx->vif->bss_conf.beacon_int)) {
 332		ctx->timing.beacon_interval =
 333			priv->contexts[IWL_RXON_CTX_PAN].timing.beacon_interval;
 334		beacon_int = le16_to_cpu(ctx->timing.beacon_interval);
 335	} else {
 336		beacon_int = iwl_adjust_beacon_interval(beacon_int,
 337			IWL_MAX_UCODE_BEACON_INTERVAL * TIME_UNIT);
 338		ctx->timing.beacon_interval = cpu_to_le16(beacon_int);
 339	}
 340
 341	ctx->beacon_int = beacon_int;
 342
 343	tsf = priv->timestamp; /* tsf is modifed by do_div: copy it */
 344	interval_tm = beacon_int * TIME_UNIT;
 345	rem = do_div(tsf, interval_tm);
 346	ctx->timing.beacon_init_val = cpu_to_le32(interval_tm - rem);
 347
 348	ctx->timing.dtim_period = vif ? (vif->bss_conf.dtim_period ?: 1) : 1;
 349
 350	IWL_DEBUG_ASSOC(priv,
 351			"beacon interval %d beacon timer %d beacon tim %d\n",
 352			le16_to_cpu(ctx->timing.beacon_interval),
 353			le32_to_cpu(ctx->timing.beacon_init_val),
 354			le16_to_cpu(ctx->timing.atim_window));
 355
 356	return iwl_dvm_send_cmd_pdu(priv, ctx->rxon_timing_cmd,
 357				0, sizeof(ctx->timing), &ctx->timing);
 358}
 359
 360static int iwlagn_rxon_disconn(struct iwl_priv *priv,
 361			       struct iwl_rxon_context *ctx)
 362{
 363	int ret;
 364	struct iwl_rxon_cmd *active = (void *)&ctx->active;
 365
 366	if (ctx->ctxid == IWL_RXON_CTX_BSS) {
 367		ret = iwlagn_disable_bss(priv, ctx, &ctx->staging);
 368	} else {
 369		ret = iwlagn_disable_pan(priv, ctx, &ctx->staging);
 370		if (ret)
 371			return ret;
 372		if (ctx->vif) {
 373			ret = iwl_send_rxon_timing(priv, ctx);
 374			if (ret) {
 375				IWL_ERR(priv, "Failed to send timing (%d)!\n", ret);
 376				return ret;
 377			}
 378			ret = iwlagn_disconn_pan(priv, ctx, &ctx->staging);
 379		}
 380	}
 381	if (ret)
 382		return ret;
 383
 384	/*
 385	 * Un-assoc RXON clears the station table and WEP
 386	 * keys, so we have to restore those afterwards.
 387	 */
 388	iwl_clear_ucode_stations(priv, ctx);
 389	/* update -- might need P2P now */
 390	iwl_update_bcast_station(priv, ctx);
 391	iwl_restore_stations(priv, ctx);
 392	ret = iwl_restore_default_wep_keys(priv, ctx);
 393	if (ret) {
 394		IWL_ERR(priv, "Failed to restore WEP keys (%d)\n", ret);
 395		return ret;
 396	}
 397
 398	memcpy(active, &ctx->staging, sizeof(*active));
 399	return 0;
 400}
 401
 402static int iwl_set_tx_power(struct iwl_priv *priv, s8 tx_power, bool force)
 403{
 404	int ret;
 405	s8 prev_tx_power;
 406	bool defer;
 407	struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
 408
 409	if (priv->calib_disabled & IWL_TX_POWER_CALIB_DISABLED)
 410		return 0;
 411
 412	lockdep_assert_held(&priv->mutex);
 413
 414	if (priv->tx_power_user_lmt == tx_power && !force)
 415		return 0;
 416
 417	if (tx_power < IWLAGN_TX_POWER_TARGET_POWER_MIN) {
 418		IWL_WARN(priv,
 419			 "Requested user TXPOWER %d below lower limit %d.\n",
 420			 tx_power,
 421			 IWLAGN_TX_POWER_TARGET_POWER_MIN);
 422		return -EINVAL;
 423	}
 424
 425	if (tx_power > DIV_ROUND_UP(priv->nvm_data->max_tx_pwr_half_dbm, 2)) {
 426		IWL_WARN(priv,
 427			"Requested user TXPOWER %d above upper limit %d.\n",
 428			 tx_power, priv->nvm_data->max_tx_pwr_half_dbm);
 429		return -EINVAL;
 430	}
 431
 432	if (!iwl_is_ready_rf(priv))
 433		return -EIO;
 434
 435	/* scan complete and commit_rxon use tx_power_next value,
 436	 * it always need to be updated for newest request */
 437	priv->tx_power_next = tx_power;
 438
 439	/* do not set tx power when scanning or channel changing */
 440	defer = test_bit(STATUS_SCANNING, &priv->status) ||
 441		memcmp(&ctx->active, &ctx->staging, sizeof(ctx->staging));
 442	if (defer && !force) {
 443		IWL_DEBUG_INFO(priv, "Deferring tx power set\n");
 444		return 0;
 445	}
 446
 447	prev_tx_power = priv->tx_power_user_lmt;
 448	priv->tx_power_user_lmt = tx_power;
 449
 450	ret = iwlagn_send_tx_power(priv);
 451
 452	/* if fail to set tx_power, restore the orig. tx power */
 453	if (ret) {
 454		priv->tx_power_user_lmt = prev_tx_power;
 455		priv->tx_power_next = prev_tx_power;
 456	}
 457	return ret;
 458}
 459
 460static int iwlagn_rxon_connect(struct iwl_priv *priv,
 461			       struct iwl_rxon_context *ctx)
 462{
 463	int ret;
 464	struct iwl_rxon_cmd *active = (void *)&ctx->active;
 465
 466	/* RXON timing must be before associated RXON */
 467	if (ctx->ctxid == IWL_RXON_CTX_BSS) {
 468		ret = iwl_send_rxon_timing(priv, ctx);
 469		if (ret) {
 470			IWL_ERR(priv, "Failed to send timing (%d)!\n", ret);
 471			return ret;
 472		}
 473	}
 474	/* QoS info may be cleared by previous un-assoc RXON */
 475	iwlagn_update_qos(priv, ctx);
 476
 477	/*
 478	 * We'll run into this code path when beaconing is
 479	 * enabled, but then we also need to send the beacon
 480	 * to the device.
 481	 */
 482	if (ctx->vif && (ctx->vif->type == NL80211_IFTYPE_AP)) {
 483		ret = iwlagn_update_beacon(priv, ctx->vif);
 484		if (ret) {
 485			IWL_ERR(priv,
 486				"Error sending required beacon (%d)!\n",
 487				ret);
 488			return ret;
 489		}
 490	}
 491
 492	priv->start_calib = 0;
 493	/*
 494	 * Apply the new configuration.
 495	 *
 496	 * Associated RXON doesn't clear the station table in uCode,
 497	 * so we don't need to restore stations etc. after this.
 498	 */
 499	ret = iwl_dvm_send_cmd_pdu(priv, ctx->rxon_cmd, 0,
 500		      sizeof(struct iwl_rxon_cmd), &ctx->staging);
 501	if (ret) {
 502		IWL_ERR(priv, "Error setting new RXON (%d)\n", ret);
 503		return ret;
 504	}
 505	memcpy(active, &ctx->staging, sizeof(*active));
 506
 507	/* IBSS beacon needs to be sent after setting assoc */
 508	if (ctx->vif && (ctx->vif->type == NL80211_IFTYPE_ADHOC))
 509		if (iwlagn_update_beacon(priv, ctx->vif))
 510			IWL_ERR(priv, "Error sending IBSS beacon\n");
 511	iwl_init_sensitivity(priv);
 512
 513	/*
 514	 * If we issue a new RXON command which required a tune then
 515	 * we must send a new TXPOWER command or we won't be able to
 516	 * Tx any frames.
 517	 *
 518	 * It's expected we set power here if channel is changing.
 519	 */
 520	ret = iwl_set_tx_power(priv, priv->tx_power_next, true);
 521	if (ret) {
 522		IWL_ERR(priv, "Error sending TX power (%d)\n", ret);
 523		return ret;
 524	}
 525
 526	if (ctx->vif && ctx->vif->type == NL80211_IFTYPE_STATION &&
 527	    priv->cfg->ht_params && priv->cfg->ht_params->smps_mode)
 528		ieee80211_request_smps(ctx->vif,
 529				       priv->cfg->ht_params->smps_mode);
 530
 531	return 0;
 532}
 533
 534int iwlagn_set_pan_params(struct iwl_priv *priv)
 535{
 536	struct iwl_wipan_params_cmd cmd;
 537	struct iwl_rxon_context *ctx_bss, *ctx_pan;
 538	int slot0 = 300, slot1 = 0;
 539	int ret;
 540
 541	if (priv->valid_contexts == BIT(IWL_RXON_CTX_BSS))
 542		return 0;
 543
 544	BUILD_BUG_ON(NUM_IWL_RXON_CTX != 2);
 545
 546	lockdep_assert_held(&priv->mutex);
 547
 548	ctx_bss = &priv->contexts[IWL_RXON_CTX_BSS];
 549	ctx_pan = &priv->contexts[IWL_RXON_CTX_PAN];
 550
 551	/*
 552	 * If the PAN context is inactive, then we don't need
 553	 * to update the PAN parameters, the last thing we'll
 554	 * have done before it goes inactive is making the PAN
 555	 * parameters be WLAN-only.
 556	 */
 557	if (!ctx_pan->is_active)
 558		return 0;
 559
 560	memset(&cmd, 0, sizeof(cmd));
 561
 562	/* only 2 slots are currently allowed */
 563	cmd.num_slots = 2;
 564
 565	cmd.slots[0].type = 0; /* BSS */
 566	cmd.slots[1].type = 1; /* PAN */
 567
 568	if (ctx_bss->vif && ctx_pan->vif) {
 569		int bcnint = ctx_pan->beacon_int;
 570		int dtim = ctx_pan->vif->bss_conf.dtim_period ?: 1;
 571
 572		/* should be set, but seems unused?? */
 573		cmd.flags |= cpu_to_le16(IWL_WIPAN_PARAMS_FLG_SLOTTED_MODE);
 574
 575		if (ctx_pan->vif->type == NL80211_IFTYPE_AP &&
 576		    bcnint &&
 577		    bcnint != ctx_bss->beacon_int) {
 578			IWL_ERR(priv,
 579				"beacon intervals don't match (%d, %d)\n",
 580				ctx_bss->beacon_int, ctx_pan->beacon_int);
 581		} else
 582			bcnint = max_t(int, bcnint,
 583				       ctx_bss->beacon_int);
 584		if (!bcnint)
 585			bcnint = DEFAULT_BEACON_INTERVAL;
 586		slot0 = bcnint / 2;
 587		slot1 = bcnint - slot0;
 588
 589		if (test_bit(STATUS_SCAN_HW, &priv->status) ||
 590		    (!ctx_bss->vif->bss_conf.idle &&
 591		     !ctx_bss->vif->bss_conf.assoc)) {
 592			slot0 = dtim * bcnint * 3 - IWL_MIN_SLOT_TIME;
 593			slot1 = IWL_MIN_SLOT_TIME;
 594		} else if (!ctx_pan->vif->bss_conf.idle &&
 595			   !ctx_pan->vif->bss_conf.assoc) {
 596			slot1 = dtim * bcnint * 3 - IWL_MIN_SLOT_TIME;
 597			slot0 = IWL_MIN_SLOT_TIME;
 598		}
 599	} else if (ctx_pan->vif) {
 600		slot0 = 0;
 601		slot1 = max_t(int, 1, ctx_pan->vif->bss_conf.dtim_period) *
 602					ctx_pan->beacon_int;
 603		slot1 = max_t(int, DEFAULT_BEACON_INTERVAL, slot1);
 604
 605		if (test_bit(STATUS_SCAN_HW, &priv->status)) {
 606			slot0 = slot1 * 3 - IWL_MIN_SLOT_TIME;
 607			slot1 = IWL_MIN_SLOT_TIME;
 608		}
 609	}
 610
 611	cmd.slots[0].width = cpu_to_le16(slot0);
 612	cmd.slots[1].width = cpu_to_le16(slot1);
 613
 614	ret = iwl_dvm_send_cmd_pdu(priv, REPLY_WIPAN_PARAMS, 0,
 615			sizeof(cmd), &cmd);
 616	if (ret)
 617		IWL_ERR(priv, "Error setting PAN parameters (%d)\n", ret);
 618
 619	return ret;
 620}
 621
 622static void _iwl_set_rxon_ht(struct iwl_priv *priv,
 623			     struct iwl_ht_config *ht_conf,
 624			     struct iwl_rxon_context *ctx)
 625{
 626	struct iwl_rxon_cmd *rxon = &ctx->staging;
 627
 628	if (!ctx->ht.enabled) {
 629		rxon->flags &= ~(RXON_FLG_CHANNEL_MODE_MSK |
 630			RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK |
 631			RXON_FLG_HT40_PROT_MSK |
 632			RXON_FLG_HT_PROT_MSK);
 633		return;
 634	}
 635
 636	/* FIXME: if the definition of ht.protection changed, the "translation"
 637	 * will be needed for rxon->flags
 638	 */
 639	rxon->flags |= cpu_to_le32(ctx->ht.protection <<
 640				   RXON_FLG_HT_OPERATING_MODE_POS);
 641
 642	/* Set up channel bandwidth:
 643	 * 20 MHz only, 20/40 mixed or pure 40 if ht40 ok */
 644	/* clear the HT channel mode before set the mode */
 645	rxon->flags &= ~(RXON_FLG_CHANNEL_MODE_MSK |
 646			 RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK);
 647	if (iwl_is_ht40_tx_allowed(priv, ctx, NULL)) {
 648		/* pure ht40 */
 649		if (ctx->ht.protection ==
 650		    IEEE80211_HT_OP_MODE_PROTECTION_20MHZ) {
 651			rxon->flags |= RXON_FLG_CHANNEL_MODE_PURE_40;
 652			/*
 653			 * Note: control channel is opposite of extension
 654			 * channel
 655			 */
 656			switch (ctx->ht.extension_chan_offset) {
 657			case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
 658				rxon->flags &=
 659					~RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK;
 660				break;
 661			case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
 662				rxon->flags |=
 663					RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK;
 664				break;
 665			}
 666		} else {
 667			/*
 668			 * Note: control channel is opposite of extension
 669			 * channel
 670			 */
 671			switch (ctx->ht.extension_chan_offset) {
 672			case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
 673				rxon->flags &=
 674					~(RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK);
 675				rxon->flags |= RXON_FLG_CHANNEL_MODE_MIXED;
 676				break;
 677			case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
 678				rxon->flags |= RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK;
 679				rxon->flags |= RXON_FLG_CHANNEL_MODE_MIXED;
 680				break;
 681			case IEEE80211_HT_PARAM_CHA_SEC_NONE:
 682			default:
 683				/*
 684				 * channel location only valid if in Mixed
 685				 * mode
 686				 */
 687				IWL_ERR(priv,
 688					"invalid extension channel offset\n");
 689				break;
 690			}
 691		}
 692	} else {
 693		rxon->flags |= RXON_FLG_CHANNEL_MODE_LEGACY;
 694	}
 695
 696	iwlagn_set_rxon_chain(priv, ctx);
 697
 698	IWL_DEBUG_ASSOC(priv, "rxon flags 0x%X operation mode :0x%X "
 699			"extension channel offset 0x%x\n",
 700			le32_to_cpu(rxon->flags), ctx->ht.protection,
 701			ctx->ht.extension_chan_offset);
 702}
 703
 704void iwl_set_rxon_ht(struct iwl_priv *priv, struct iwl_ht_config *ht_conf)
 705{
 706	struct iwl_rxon_context *ctx;
 707
 708	for_each_context(priv, ctx)
 709		_iwl_set_rxon_ht(priv, ht_conf, ctx);
 710}
 711
 712/**
 713 * iwl_set_rxon_channel - Set the band and channel values in staging RXON
 714 * @ch: requested channel as a pointer to struct ieee80211_channel
 715
 716 * NOTE:  Does not commit to the hardware; it sets appropriate bit fields
 717 * in the staging RXON flag structure based on the ch->band
 718 */
 719void iwl_set_rxon_channel(struct iwl_priv *priv, struct ieee80211_channel *ch,
 720			 struct iwl_rxon_context *ctx)
 721{
 722	enum ieee80211_band band = ch->band;
 723	u16 channel = ch->hw_value;
 724
 725	if ((le16_to_cpu(ctx->staging.channel) == channel) &&
 726	    (priv->band == band))
 727		return;
 728
 729	ctx->staging.channel = cpu_to_le16(channel);
 730	if (band == IEEE80211_BAND_5GHZ)
 731		ctx->staging.flags &= ~RXON_FLG_BAND_24G_MSK;
 732	else
 733		ctx->staging.flags |= RXON_FLG_BAND_24G_MSK;
 734
 735	priv->band = band;
 736
 737	IWL_DEBUG_INFO(priv, "Staging channel set to %d [%d]\n", channel, band);
 738
 739}
 740
 741void iwl_set_flags_for_band(struct iwl_priv *priv,
 742			    struct iwl_rxon_context *ctx,
 743			    enum ieee80211_band band,
 744			    struct ieee80211_vif *vif)
 745{
 746	if (band == IEEE80211_BAND_5GHZ) {
 747		ctx->staging.flags &=
 748		    ~(RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK
 749		      | RXON_FLG_CCK_MSK);
 750		ctx->staging.flags |= RXON_FLG_SHORT_SLOT_MSK;
 751	} else {
 752		/* Copied from iwl_post_associate() */
 753		if (vif && vif->bss_conf.use_short_slot)
 754			ctx->staging.flags |= RXON_FLG_SHORT_SLOT_MSK;
 755		else
 756			ctx->staging.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
 757
 758		ctx->staging.flags |= RXON_FLG_BAND_24G_MSK;
 759		ctx->staging.flags |= RXON_FLG_AUTO_DETECT_MSK;
 760		ctx->staging.flags &= ~RXON_FLG_CCK_MSK;
 761	}
 762}
 763
 764static void iwl_set_rxon_hwcrypto(struct iwl_priv *priv,
 765				  struct iwl_rxon_context *ctx, int hw_decrypt)
 766{
 767	struct iwl_rxon_cmd *rxon = &ctx->staging;
 768
 769	if (hw_decrypt)
 770		rxon->filter_flags &= ~RXON_FILTER_DIS_DECRYPT_MSK;
 771	else
 772		rxon->filter_flags |= RXON_FILTER_DIS_DECRYPT_MSK;
 773
 774}
 775
 776/* validate RXON structure is valid */
 777static int iwl_check_rxon_cmd(struct iwl_priv *priv,
 778			      struct iwl_rxon_context *ctx)
 779{
 780	struct iwl_rxon_cmd *rxon = &ctx->staging;
 781	u32 errors = 0;
 782
 783	if (rxon->flags & RXON_FLG_BAND_24G_MSK) {
 784		if (rxon->flags & RXON_FLG_TGJ_NARROW_BAND_MSK) {
 785			IWL_WARN(priv, "check 2.4G: wrong narrow\n");
 786			errors |= BIT(0);
 787		}
 788		if (rxon->flags & RXON_FLG_RADAR_DETECT_MSK) {
 789			IWL_WARN(priv, "check 2.4G: wrong radar\n");
 790			errors |= BIT(1);
 791		}
 792	} else {
 793		if (!(rxon->flags & RXON_FLG_SHORT_SLOT_MSK)) {
 794			IWL_WARN(priv, "check 5.2G: not short slot!\n");
 795			errors |= BIT(2);
 796		}
 797		if (rxon->flags & RXON_FLG_CCK_MSK) {
 798			IWL_WARN(priv, "check 5.2G: CCK!\n");
 799			errors |= BIT(3);
 800		}
 801	}
 802	if ((rxon->node_addr[0] | rxon->bssid_addr[0]) & 0x1) {
 803		IWL_WARN(priv, "mac/bssid mcast!\n");
 804		errors |= BIT(4);
 805	}
 806
 807	/* make sure basic rates 6Mbps and 1Mbps are supported */
 808	if ((rxon->ofdm_basic_rates & IWL_RATE_6M_MASK) == 0 &&
 809	    (rxon->cck_basic_rates & IWL_RATE_1M_MASK) == 0) {
 810		IWL_WARN(priv, "neither 1 nor 6 are basic\n");
 811		errors |= BIT(5);
 812	}
 813
 814	if (le16_to_cpu(rxon->assoc_id) > 2007) {
 815		IWL_WARN(priv, "aid > 2007\n");
 816		errors |= BIT(6);
 817	}
 818
 819	if ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK))
 820			== (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK)) {
 821		IWL_WARN(priv, "CCK and short slot\n");
 822		errors |= BIT(7);
 823	}
 824
 825	if ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK))
 826			== (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK)) {
 827		IWL_WARN(priv, "CCK and auto detect\n");
 828		errors |= BIT(8);
 829	}
 830
 831	if ((rxon->flags & (RXON_FLG_AUTO_DETECT_MSK |
 832			    RXON_FLG_TGG_PROTECT_MSK)) ==
 833			    RXON_FLG_TGG_PROTECT_MSK) {
 834		IWL_WARN(priv, "TGg but no auto-detect\n");
 835		errors |= BIT(9);
 836	}
 837
 838	if (rxon->channel == 0) {
 839		IWL_WARN(priv, "zero channel is invalid\n");
 840		errors |= BIT(10);
 841	}
 842
 843	WARN(errors, "Invalid RXON (%#x), channel %d",
 844	     errors, le16_to_cpu(rxon->channel));
 845
 846	return errors ? -EINVAL : 0;
 847}
 848
 849/**
 850 * iwl_full_rxon_required - check if full RXON (vs RXON_ASSOC) cmd is needed
 851 * @priv: staging_rxon is compared to active_rxon
 852 *
 853 * If the RXON structure is changing enough to require a new tune,
 854 * or is clearing the RXON_FILTER_ASSOC_MSK, then return 1 to indicate that
 855 * a new tune (full RXON command, rather than RXON_ASSOC cmd) is required.
 856 */
 857static int iwl_full_rxon_required(struct iwl_priv *priv,
 858				  struct iwl_rxon_context *ctx)
 859{
 860	const struct iwl_rxon_cmd *staging = &ctx->staging;
 861	const struct iwl_rxon_cmd *active = &ctx->active;
 862
 863#define CHK(cond)							\
 864	if ((cond)) {							\
 865		IWL_DEBUG_INFO(priv, "need full RXON - " #cond "\n");	\
 866		return 1;						\
 867	}
 868
 869#define CHK_NEQ(c1, c2)						\
 870	if ((c1) != (c2)) {					\
 871		IWL_DEBUG_INFO(priv, "need full RXON - "	\
 872			       #c1 " != " #c2 " - %d != %d\n",	\
 873			       (c1), (c2));			\
 874		return 1;					\
 875	}
 876
 877	/* These items are only settable from the full RXON command */
 878	CHK(!iwl_is_associated_ctx(ctx));
 879	CHK(!ether_addr_equal(staging->bssid_addr, active->bssid_addr));
 880	CHK(!ether_addr_equal(staging->node_addr, active->node_addr));
 881	CHK(!ether_addr_equal(staging->wlap_bssid_addr,
 882			      active->wlap_bssid_addr));
 883	CHK_NEQ(staging->dev_type, active->dev_type);
 884	CHK_NEQ(staging->channel, active->channel);
 885	CHK_NEQ(staging->air_propagation, active->air_propagation);
 886	CHK_NEQ(staging->ofdm_ht_single_stream_basic_rates,
 887		active->ofdm_ht_single_stream_basic_rates);
 888	CHK_NEQ(staging->ofdm_ht_dual_stream_basic_rates,
 889		active->ofdm_ht_dual_stream_basic_rates);
 890	CHK_NEQ(staging->ofdm_ht_triple_stream_basic_rates,
 891		active->ofdm_ht_triple_stream_basic_rates);
 892	CHK_NEQ(staging->assoc_id, active->assoc_id);
 893
 894	/* flags, filter_flags, ofdm_basic_rates, and cck_basic_rates can
 895	 * be updated with the RXON_ASSOC command -- however only some
 896	 * flag transitions are allowed using RXON_ASSOC */
 897
 898	/* Check if we are not switching bands */
 899	CHK_NEQ(staging->flags & RXON_FLG_BAND_24G_MSK,
 900		active->flags & RXON_FLG_BAND_24G_MSK);
 901
 902	/* Check if we are switching association toggle */
 903	CHK_NEQ(staging->filter_flags & RXON_FILTER_ASSOC_MSK,
 904		active->filter_flags & RXON_FILTER_ASSOC_MSK);
 905
 906#undef CHK
 907#undef CHK_NEQ
 908
 909	return 0;
 910}
 911
 912#ifdef CONFIG_IWLWIFI_DEBUG
 913void iwl_print_rx_config_cmd(struct iwl_priv *priv,
 914			     enum iwl_rxon_context_id ctxid)
 915{
 916	struct iwl_rxon_context *ctx = &priv->contexts[ctxid];
 917	struct iwl_rxon_cmd *rxon = &ctx->staging;
 918
 919	IWL_DEBUG_RADIO(priv, "RX CONFIG:\n");
 920	iwl_print_hex_dump(priv, IWL_DL_RADIO, (u8 *) rxon, sizeof(*rxon));
 921	IWL_DEBUG_RADIO(priv, "u16 channel: 0x%x\n",
 922			le16_to_cpu(rxon->channel));
 923	IWL_DEBUG_RADIO(priv, "u32 flags: 0x%08X\n",
 924			le32_to_cpu(rxon->flags));
 925	IWL_DEBUG_RADIO(priv, "u32 filter_flags: 0x%08x\n",
 926			le32_to_cpu(rxon->filter_flags));
 927	IWL_DEBUG_RADIO(priv, "u8 dev_type: 0x%x\n", rxon->dev_type);
 928	IWL_DEBUG_RADIO(priv, "u8 ofdm_basic_rates: 0x%02x\n",
 929			rxon->ofdm_basic_rates);
 930	IWL_DEBUG_RADIO(priv, "u8 cck_basic_rates: 0x%02x\n",
 931			rxon->cck_basic_rates);
 932	IWL_DEBUG_RADIO(priv, "u8[6] node_addr: %pM\n", rxon->node_addr);
 933	IWL_DEBUG_RADIO(priv, "u8[6] bssid_addr: %pM\n", rxon->bssid_addr);
 934	IWL_DEBUG_RADIO(priv, "u16 assoc_id: 0x%x\n",
 935			le16_to_cpu(rxon->assoc_id));
 936}
 937#endif
 938
 939static void iwl_calc_basic_rates(struct iwl_priv *priv,
 940				 struct iwl_rxon_context *ctx)
 941{
 942	int lowest_present_ofdm = 100;
 943	int lowest_present_cck = 100;
 944	u8 cck = 0;
 945	u8 ofdm = 0;
 946
 947	if (ctx->vif) {
 948		struct ieee80211_supported_band *sband;
 949		unsigned long basic = ctx->vif->bss_conf.basic_rates;
 950		int i;
 951
 952		sband = priv->hw->wiphy->bands[priv->hw->conf.chandef.chan->band];
 953
 954		for_each_set_bit(i, &basic, BITS_PER_LONG) {
 955			int hw = sband->bitrates[i].hw_value;
 956			if (hw >= IWL_FIRST_OFDM_RATE) {
 957				ofdm |= BIT(hw - IWL_FIRST_OFDM_RATE);
 958				if (lowest_present_ofdm > hw)
 959					lowest_present_ofdm = hw;
 960			} else {
 961				BUILD_BUG_ON(IWL_FIRST_CCK_RATE != 0);
 962
 963				cck |= BIT(hw);
 964				if (lowest_present_cck > hw)
 965					lowest_present_cck = hw;
 966			}
 967		}
 968	}
 969
 970	/*
 971	 * Now we've got the basic rates as bitmaps in the ofdm and cck
 972	 * variables. This isn't sufficient though, as there might not
 973	 * be all the right rates in the bitmap. E.g. if the only basic
 974	 * rates are 5.5 Mbps and 11 Mbps, we still need to add 1 Mbps
 975	 * and 6 Mbps because the 802.11-2007 standard says in 9.6:
 976	 *
 977	 *    [...] a STA responding to a received frame shall transmit
 978	 *    its Control Response frame [...] at the highest rate in the
 979	 *    BSSBasicRateSet parameter that is less than or equal to the
 980	 *    rate of the immediately previous frame in the frame exchange
 981	 *    sequence ([...]) and that is of the same modulation class
 982	 *    ([...]) as the received frame. If no rate contained in the
 983	 *    BSSBasicRateSet parameter meets these conditions, then the
 984	 *    control frame sent in response to a received frame shall be
 985	 *    transmitted at the highest mandatory rate of the PHY that is
 986	 *    less than or equal to the rate of the received frame, and
 987	 *    that is of the same modulation class as the received frame.
 988	 *
 989	 * As a consequence, we need to add all mandatory rates that are
 990	 * lower than all of the basic rates to these bitmaps.
 991	 */
 992
 993	if (IWL_RATE_24M_INDEX < lowest_present_ofdm)
 994		ofdm |= IWL_RATE_24M_MASK >> IWL_FIRST_OFDM_RATE;
 995	if (IWL_RATE_12M_INDEX < lowest_present_ofdm)
 996		ofdm |= IWL_RATE_12M_MASK >> IWL_FIRST_OFDM_RATE;
 997	/* 6M already there or needed so always add */
 998	ofdm |= IWL_RATE_6M_MASK >> IWL_FIRST_OFDM_RATE;
 999
1000	/*
1001	 * CCK is a bit more complex with DSSS vs. HR/DSSS vs. ERP.
1002	 * Note, however:
1003	 *  - if no CCK rates are basic, it must be ERP since there must
1004	 *    be some basic rates at all, so they're OFDM => ERP PHY
1005	 *    (or we're in 5 GHz, and the cck bitmap will never be used)
1006	 *  - if 11M is a basic rate, it must be ERP as well, so add 5.5M
1007	 *  - if 5.5M is basic, 1M and 2M are mandatory
1008	 *  - if 2M is basic, 1M is mandatory
1009	 *  - if 1M is basic, that's the only valid ACK rate.
1010	 * As a consequence, it's not as complicated as it sounds, just add
1011	 * any lower rates to the ACK rate bitmap.
1012	 */
1013	if (IWL_RATE_11M_INDEX < lowest_present_cck)
1014		cck |= IWL_RATE_11M_MASK >> IWL_FIRST_CCK_RATE;
1015	if (IWL_RATE_5M_INDEX < lowest_present_cck)
1016		cck |= IWL_RATE_5M_MASK >> IWL_FIRST_CCK_RATE;
1017	if (IWL_RATE_2M_INDEX < lowest_present_cck)
1018		cck |= IWL_RATE_2M_MASK >> IWL_FIRST_CCK_RATE;
1019	/* 1M already there or needed so always add */
1020	cck |= IWL_RATE_1M_MASK >> IWL_FIRST_CCK_RATE;
1021
1022	IWL_DEBUG_RATE(priv, "Set basic rates cck:0x%.2x ofdm:0x%.2x\n",
1023		       cck, ofdm);
1024
1025	/* "basic_rates" is a misnomer here -- should be called ACK rates */
1026	ctx->staging.cck_basic_rates = cck;
1027	ctx->staging.ofdm_basic_rates = ofdm;
1028}
1029
1030/**
1031 * iwlagn_commit_rxon - commit staging_rxon to hardware
1032 *
1033 * The RXON command in staging_rxon is committed to the hardware and
1034 * the active_rxon structure is updated with the new data.  This
1035 * function correctly transitions out of the RXON_ASSOC_MSK state if
1036 * a HW tune is required based on the RXON structure changes.
1037 *
1038 * The connect/disconnect flow should be as the following:
1039 *
1040 * 1. make sure send RXON command with association bit unset if not connect
1041 *	this should include the channel and the band for the candidate
1042 *	to be connected to
1043 * 2. Add Station before RXON association with the AP
1044 * 3. RXON_timing has to send before RXON for connection
1045 * 4. full RXON command - associated bit set
1046 * 5. use RXON_ASSOC command to update any flags changes
1047 */
1048int iwlagn_commit_rxon(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
1049{
1050	/* cast away the const for active_rxon in this function */
1051	struct iwl_rxon_cmd *active = (void *)&ctx->active;
1052	bool new_assoc = !!(ctx->staging.filter_flags & RXON_FILTER_ASSOC_MSK);
1053	int ret;
1054
1055	lockdep_assert_held(&priv->mutex);
1056
1057	if (!iwl_is_alive(priv))
1058		return -EBUSY;
1059
1060	/* This function hardcodes a bunch of dual-mode assumptions */
1061	BUILD_BUG_ON(NUM_IWL_RXON_CTX != 2);
1062
1063	if (!ctx->is_active)
1064		return 0;
1065
1066	/* always get timestamp with Rx frame */
1067	ctx->staging.flags |= RXON_FLG_TSF2HOST_MSK;
1068
1069	/* recalculate basic rates */
1070	iwl_calc_basic_rates(priv, ctx);
1071
1072	/*
1073	 * force CTS-to-self frames protection if RTS-CTS is not preferred
1074	 * one aggregation protection method
1075	 */
1076	if (!priv->hw_params.use_rts_for_aggregation)
1077		ctx->staging.flags |= RXON_FLG_SELF_CTS_EN;
1078
1079	if ((ctx->vif && ctx->vif->bss_conf.use_short_slot) ||
1080	    !(ctx->staging.flags & RXON_FLG_BAND_24G_MSK))
1081		ctx->staging.flags |= RXON_FLG_SHORT_SLOT_MSK;
1082	else
1083		ctx->staging.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
1084
1085	iwl_print_rx_config_cmd(priv, ctx->ctxid);
1086	ret = iwl_check_rxon_cmd(priv, ctx);
1087	if (ret) {
1088		IWL_ERR(priv, "Invalid RXON configuration. Not committing.\n");
1089		return -EINVAL;
1090	}
1091
1092	/*
1093	 * receive commit_rxon request
1094	 * abort any previous channel switch if still in process
1095	 */
1096	if (test_bit(STATUS_CHANNEL_SWITCH_PENDING, &priv->status) &&
1097	    (priv->switch_channel != ctx->staging.channel)) {
1098		IWL_DEBUG_11H(priv, "abort channel switch on %d\n",
1099			      le16_to_cpu(priv->switch_channel));
1100		iwl_chswitch_done(priv, false);
1101	}
1102
1103	/*
1104	 * If we don't need to send a full RXON, we can use
1105	 * iwl_rxon_assoc_cmd which is used to reconfigure filter
1106	 * and other flags for the current radio configuration.
1107	 */
1108	if (!iwl_full_rxon_required(priv, ctx)) {
1109		ret = iwlagn_send_rxon_assoc(priv, ctx);
1110		if (ret) {
1111			IWL_ERR(priv, "Error setting RXON_ASSOC (%d)\n", ret);
1112			return ret;
1113		}
1114
1115		memcpy(active, &ctx->staging, sizeof(*active));
1116		/*
1117		 * We do not commit tx power settings while channel changing,
1118		 * do it now if after settings changed.
1119		 */
1120		iwl_set_tx_power(priv, priv->tx_power_next, false);
1121
1122		/* make sure we are in the right PS state */
1123		iwl_power_update_mode(priv, true);
1124
1125		return 0;
1126	}
1127
1128	iwl_set_rxon_hwcrypto(priv, ctx, !iwlwifi_mod_params.sw_crypto);
1129
1130	IWL_DEBUG_INFO(priv,
1131		       "Going to commit RXON\n"
1132		       "  * with%s RXON_FILTER_ASSOC_MSK\n"
1133		       "  * channel = %d\n"
1134		       "  * bssid = %pM\n",
1135		       (new_assoc ? "" : "out"),
1136		       le16_to_cpu(ctx->staging.channel),
1137		       ctx->staging.bssid_addr);
1138
1139	/*
1140	 * Always clear associated first, but with the correct config.
1141	 * This is required as for example station addition for the
1142	 * AP station must be done after the BSSID is set to correctly
1143	 * set up filters in the device.
1144	 */
1145	ret = iwlagn_rxon_disconn(priv, ctx);
1146	if (ret)
1147		return ret;
1148
1149	ret = iwlagn_set_pan_params(priv);
1150	if (ret)
1151		return ret;
1152
1153	if (new_assoc)
1154		return iwlagn_rxon_connect(priv, ctx);
1155
1156	return 0;
1157}
1158
1159void iwlagn_config_ht40(struct ieee80211_conf *conf,
1160			struct iwl_rxon_context *ctx)
1161{
1162	if (conf_is_ht40_minus(conf)) {
1163		ctx->ht.extension_chan_offset =
1164			IEEE80211_HT_PARAM_CHA_SEC_BELOW;
1165		ctx->ht.is_40mhz = true;
1166	} else if (conf_is_ht40_plus(conf)) {
1167		ctx->ht.extension_chan_offset =
1168			IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
1169		ctx->ht.is_40mhz = true;
1170	} else {
1171		ctx->ht.extension_chan_offset =
1172			IEEE80211_HT_PARAM_CHA_SEC_NONE;
1173		ctx->ht.is_40mhz = false;
1174	}
1175}
1176
1177int iwlagn_mac_config(struct ieee80211_hw *hw, u32 changed)
1178{
1179	struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
1180	struct iwl_rxon_context *ctx;
1181	struct ieee80211_conf *conf = &hw->conf;
1182	struct ieee80211_channel *channel = conf->chandef.chan;
1183	int ret = 0;
1184
1185	IWL_DEBUG_MAC80211(priv, "enter: changed %#x\n", changed);
1186
1187	mutex_lock(&priv->mutex);
1188
1189	if (unlikely(test_bit(STATUS_SCANNING, &priv->status))) {
1190		IWL_DEBUG_MAC80211(priv, "leave - scanning\n");
1191		goto out;
1192	}
1193
1194	if (!iwl_is_ready(priv)) {
1195		IWL_DEBUG_MAC80211(priv, "leave - not ready\n");
1196		goto out;
1197	}
1198
1199	if (changed & (IEEE80211_CONF_CHANGE_SMPS |
1200		       IEEE80211_CONF_CHANGE_CHANNEL)) {
1201		/* mac80211 uses static for non-HT which is what we want */
1202		priv->current_ht_config.smps = conf->smps_mode;
1203
1204		/*
1205		 * Recalculate chain counts.
1206		 *
1207		 * If monitor mode is enabled then mac80211 will
1208		 * set up the SM PS mode to OFF if an HT channel is
1209		 * configured.
1210		 */
1211		for_each_context(priv, ctx)
1212			iwlagn_set_rxon_chain(priv, ctx);
1213	}
1214
1215	if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
1216		for_each_context(priv, ctx) {
1217			/* Configure HT40 channels */
1218			if (ctx->ht.enabled != conf_is_ht(conf))
1219				ctx->ht.enabled = conf_is_ht(conf);
1220
1221			if (ctx->ht.enabled) {
1222				/* if HT40 is used, it should not change
1223				 * after associated except channel switch */
1224				if (!ctx->ht.is_40mhz ||
1225						!iwl_is_associated_ctx(ctx))
1226					iwlagn_config_ht40(conf, ctx);
1227			} else
1228				ctx->ht.is_40mhz = false;
1229
1230			/*
1231			 * Default to no protection. Protection mode will
1232			 * later be set from BSS config in iwl_ht_conf
1233			 */
1234			ctx->ht.protection = IEEE80211_HT_OP_MODE_PROTECTION_NONE;
1235
1236			/* if we are switching from ht to 2.4 clear flags
1237			 * from any ht related info since 2.4 does not
1238			 * support ht */
1239			if (le16_to_cpu(ctx->staging.channel) !=
1240			    channel->hw_value)
1241				ctx->staging.flags = 0;
1242
1243			iwl_set_rxon_channel(priv, channel, ctx);
1244			iwl_set_rxon_ht(priv, &priv->current_ht_config);
1245
1246			iwl_set_flags_for_band(priv, ctx, channel->band,
1247					       ctx->vif);
1248		}
1249
1250		iwl_update_bcast_stations(priv);
1251	}
1252
1253	if (changed & (IEEE80211_CONF_CHANGE_PS |
1254			IEEE80211_CONF_CHANGE_IDLE)) {
1255		ret = iwl_power_update_mode(priv, false);
1256		if (ret)
1257			IWL_DEBUG_MAC80211(priv, "Error setting sleep level\n");
1258	}
1259
1260	if (changed & IEEE80211_CONF_CHANGE_POWER) {
1261		IWL_DEBUG_MAC80211(priv, "TX Power old=%d new=%d\n",
1262			priv->tx_power_user_lmt, conf->power_level);
1263
1264		iwl_set_tx_power(priv, conf->power_level, false);
1265	}
1266
1267	for_each_context(priv, ctx) {
1268		if (!memcmp(&ctx->staging, &ctx->active, sizeof(ctx->staging)))
1269			continue;
1270		iwlagn_commit_rxon(priv, ctx);
1271	}
1272 out:
1273	mutex_unlock(&priv->mutex);
1274	IWL_DEBUG_MAC80211(priv, "leave\n");
1275
1276	return ret;
1277}
1278
1279static void iwlagn_check_needed_chains(struct iwl_priv *priv,
1280				       struct iwl_rxon_context *ctx,
1281				       struct ieee80211_bss_conf *bss_conf)
1282{
1283	struct ieee80211_vif *vif = ctx->vif;
1284	struct iwl_rxon_context *tmp;
1285	struct ieee80211_sta *sta;
1286	struct iwl_ht_config *ht_conf = &priv->current_ht_config;
1287	struct ieee80211_sta_ht_cap *ht_cap;
1288	bool need_multiple;
1289
1290	lockdep_assert_held(&priv->mutex);
1291
1292	switch (vif->type) {
1293	case NL80211_IFTYPE_STATION:
1294		rcu_read_lock();
1295		sta = ieee80211_find_sta(vif, bss_conf->bssid);
1296		if (!sta) {
1297			/*
1298			 * If at all, this can only happen through a race
1299			 * when the AP disconnects us while we're still
1300			 * setting up the connection, in that case mac80211
1301			 * will soon tell us about that.
1302			 */
1303			need_multiple = false;
1304			rcu_read_unlock();
1305			break;
1306		}
1307
1308		ht_cap = &sta->ht_cap;
1309
1310		need_multiple = true;
1311
1312		/*
1313		 * If the peer advertises no support for receiving 2 and 3
1314		 * stream MCS rates, it can't be transmitting them either.
1315		 */
1316		if (ht_cap->mcs.rx_mask[1] == 0 &&
1317		    ht_cap->mcs.rx_mask[2] == 0) {
1318			need_multiple = false;
1319		} else if (!(ht_cap->mcs.tx_params &
1320						IEEE80211_HT_MCS_TX_DEFINED)) {
1321			/* If it can't TX MCS at all ... */
1322			need_multiple = false;
1323		} else if (ht_cap->mcs.tx_params &
1324						IEEE80211_HT_MCS_TX_RX_DIFF) {
1325			int maxstreams;
1326
1327			/*
1328			 * But if it can receive them, it might still not
1329			 * be able to transmit them, which is what we need
1330			 * to check here -- so check the number of streams
1331			 * it advertises for TX (if different from RX).
1332			 */
1333
1334			maxstreams = (ht_cap->mcs.tx_params &
1335				 IEEE80211_HT_MCS_TX_MAX_STREAMS_MASK);
1336			maxstreams >>=
1337				IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT;
1338			maxstreams += 1;
1339
1340			if (maxstreams <= 1)
1341				need_multiple = false;
1342		}
1343
1344		rcu_read_unlock();
1345		break;
1346	case NL80211_IFTYPE_ADHOC:
1347		/* currently */
1348		need_multiple = false;
1349		break;
1350	default:
1351		/* only AP really */
1352		need_multiple = true;
1353		break;
1354	}
1355
1356	ctx->ht_need_multiple_chains = need_multiple;
1357
1358	if (!need_multiple) {
1359		/* check all contexts */
1360		for_each_context(priv, tmp) {
1361			if (!tmp->vif)
1362				continue;
1363			if (tmp->ht_need_multiple_chains) {
1364				need_multiple = true;
1365				break;
1366			}
1367		}
1368	}
1369
1370	ht_conf->single_chain_sufficient = !need_multiple;
1371}
1372
1373static void iwlagn_chain_noise_reset(struct iwl_priv *priv)
1374{
1375	struct iwl_chain_noise_data *data = &priv->chain_noise_data;
1376	int ret;
1377
1378	if (priv->calib_disabled & IWL_CHAIN_NOISE_CALIB_DISABLED)
1379		return;
1380
1381	if ((data->state == IWL_CHAIN_NOISE_ALIVE) &&
1382	    iwl_is_any_associated(priv)) {
1383		struct iwl_calib_chain_noise_reset_cmd cmd;
1384
1385		/* clear data for chain noise calibration algorithm */
1386		data->chain_noise_a = 0;
1387		data->chain_noise_b = 0;
1388		data->chain_noise_c = 0;
1389		data->chain_signal_a = 0;
1390		data->chain_signal_b = 0;
1391		data->chain_signal_c = 0;
1392		data->beacon_count = 0;
1393
1394		memset(&cmd, 0, sizeof(cmd));
1395		iwl_set_calib_hdr(&cmd.hdr,
1396			priv->phy_calib_chain_noise_reset_cmd);
1397		ret = iwl_dvm_send_cmd_pdu(priv,
1398					REPLY_PHY_CALIBRATION_CMD,
1399					0, sizeof(cmd), &cmd);
1400		if (ret)
1401			IWL_ERR(priv,
1402				"Could not send REPLY_PHY_CALIBRATION_CMD\n");
1403		data->state = IWL_CHAIN_NOISE_ACCUMULATE;
1404		IWL_DEBUG_CALIB(priv, "Run chain_noise_calibrate\n");
1405	}
1406}
1407
1408void iwlagn_bss_info_changed(struct ieee80211_hw *hw,
1409			     struct ieee80211_vif *vif,
1410			     struct ieee80211_bss_conf *bss_conf,
1411			     u32 changes)
1412{
1413	struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
1414	struct iwl_rxon_context *ctx = iwl_rxon_ctx_from_vif(vif);
1415	int ret;
1416	bool force = false;
1417
1418	mutex_lock(&priv->mutex);
1419
1420	if (changes & BSS_CHANGED_IDLE && bss_conf->idle) {
1421		/*
1422		 * If we go idle, then clearly no "passive-no-rx"
1423		 * workaround is needed any more, this is a reset.
1424		 */
1425		iwlagn_lift_passive_no_rx(priv);
1426	}
1427
1428	if (unlikely(!iwl_is_ready(priv))) {
1429		IWL_DEBUG_MAC80211(priv, "leave - not ready\n");
1430		mutex_unlock(&priv->mutex);
1431		return;
1432        }
1433
1434	if (unlikely(!ctx->vif)) {
1435		IWL_DEBUG_MAC80211(priv, "leave - vif is NULL\n");
1436		mutex_unlock(&priv->mutex);
1437		return;
1438	}
1439
1440	if (changes & BSS_CHANGED_BEACON_INT)
1441		force = true;
1442
1443	if (changes & BSS_CHANGED_QOS) {
1444		ctx->qos_data.qos_active = bss_conf->qos;
1445		iwlagn_update_qos(priv, ctx);
1446	}
1447
1448	ctx->staging.assoc_id = cpu_to_le16(vif->bss_conf.aid);
1449	if (vif->bss_conf.use_short_preamble)
1450		ctx->staging.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
1451	else
1452		ctx->staging.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
1453
1454	if (changes & BSS_CHANGED_ASSOC) {
1455		if (bss_conf->assoc) {
1456			priv->timestamp = bss_conf->sync_tsf;
1457			ctx->staging.filter_flags |= RXON_FILTER_ASSOC_MSK;
1458		} else {
1459			ctx->staging.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
1460
1461			if (ctx->ctxid == IWL_RXON_CTX_BSS)
1462				priv->have_rekey_data = false;
1463		}
1464
1465		iwlagn_bt_coex_rssi_monitor(priv);
1466	}
1467
1468	if (ctx->ht.enabled) {
1469		ctx->ht.protection = bss_conf->ht_operation_mode &
1470					IEEE80211_HT_OP_MODE_PROTECTION;
1471		ctx->ht.non_gf_sta_present = !!(bss_conf->ht_operation_mode &
1472					IEEE80211_HT_OP_MODE_NON_GF_STA_PRSNT);
1473		iwlagn_check_needed_chains(priv, ctx, bss_conf);
1474		iwl_set_rxon_ht(priv, &priv->current_ht_config);
1475	}
1476
1477	iwlagn_set_rxon_chain(priv, ctx);
1478
1479	if (bss_conf->use_cts_prot && (priv->band != IEEE80211_BAND_5GHZ))
1480		ctx->staging.flags |= RXON_FLG_TGG_PROTECT_MSK;
1481	else
1482		ctx->staging.flags &= ~RXON_FLG_TGG_PROTECT_MSK;
1483
1484	if (bss_conf->use_cts_prot)
1485		ctx->staging.flags |= RXON_FLG_SELF_CTS_EN;
1486	else
1487		ctx->staging.flags &= ~RXON_FLG_SELF_CTS_EN;
1488
1489	memcpy(ctx->staging.bssid_addr, bss_conf->bssid, ETH_ALEN);
1490
1491	if (vif->type == NL80211_IFTYPE_AP ||
1492	    vif->type == NL80211_IFTYPE_ADHOC) {
1493		if (vif->bss_conf.enable_beacon) {
1494			ctx->staging.filter_flags |= RXON_FILTER_ASSOC_MSK;
1495			priv->beacon_ctx = ctx;
1496		} else {
1497			ctx->staging.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
1498			priv->beacon_ctx = NULL;
1499		}
1500	}
1501
1502	/*
1503	 * If the ucode decides to do beacon filtering before
1504	 * association, it will lose beacons that are needed
1505	 * before sending frames out on passive channels. This
1506	 * causes association failures on those channels. Enable
1507	 * receiving beacons in such cases.
1508	 */
1509
1510	if (vif->type == NL80211_IFTYPE_STATION) {
1511		if (!bss_conf->assoc)
1512			ctx->staging.filter_flags |= RXON_FILTER_BCON_AWARE_MSK;
1513		else
1514			ctx->staging.filter_flags &=
1515						    ~RXON_FILTER_BCON_AWARE_MSK;
1516	}
1517
1518	if (force || memcmp(&ctx->staging, &ctx->active, sizeof(ctx->staging)))
1519		iwlagn_commit_rxon(priv, ctx);
1520
1521	if (changes & BSS_CHANGED_ASSOC && bss_conf->assoc) {
1522		/*
1523		 * The chain noise calibration will enable PM upon
1524		 * completion. If calibration has already been run
1525		 * then we need to enable power management here.
1526		 */
1527		if (priv->chain_noise_data.state == IWL_CHAIN_NOISE_DONE)
1528			iwl_power_update_mode(priv, false);
1529
1530		/* Enable RX differential gain and sensitivity calibrations */
1531		iwlagn_chain_noise_reset(priv);
1532		priv->start_calib = 1;
1533	}
1534
1535	if (changes & BSS_CHANGED_IBSS) {
1536		ret = iwlagn_manage_ibss_station(priv, vif,
1537						 bss_conf->ibss_joined);
1538		if (ret)
1539			IWL_ERR(priv, "failed to %s IBSS station %pM\n",
1540				bss_conf->ibss_joined ? "add" : "remove",
1541				bss_conf->bssid);
1542	}
1543
1544	if (changes & BSS_CHANGED_BEACON && priv->beacon_ctx == ctx) {
1545		if (iwlagn_update_beacon(priv, vif))
1546			IWL_ERR(priv, "Error updating beacon\n");
1547	}
1548
1549	mutex_unlock(&priv->mutex);
1550}
1551
1552void iwlagn_post_scan(struct iwl_priv *priv)
1553{
1554	struct iwl_rxon_context *ctx;
1555
1556	/*
1557	 * We do not commit power settings while scan is pending,
1558	 * do it now if the settings changed.
1559	 */
1560	iwl_power_set_mode(priv, &priv->power_data.sleep_cmd_next, false);
1561	iwl_set_tx_power(priv, priv->tx_power_next, false);
1562
1563	/*
1564	 * Since setting the RXON may have been deferred while
1565	 * performing the scan, fire one off if needed
1566	 */
1567	for_each_context(priv, ctx)
1568		if (memcmp(&ctx->staging, &ctx->active, sizeof(ctx->staging)))
1569			iwlagn_commit_rxon(priv, ctx);
1570
1571	iwlagn_set_pan_params(priv);
1572}