Linux Audio

Check our new training course

Real-Time Linux with PREEMPT_RT training

Feb 18-20, 2025
Register
Loading...
Note: File does not exist in v6.8.
   1/******************************************************************************
   2 *
   3 * Copyright(c) 2003 - 2012 Intel Corporation. All rights reserved.
   4 *
   5 * Portions of this file are derived from the ipw3945 project, as well
   6 * as portions of the ieee80211 subsystem header files.
   7 *
   8 * This program is free software; you can redistribute it and/or modify it
   9 * under the terms of version 2 of the GNU General Public License as
  10 * published by the Free Software Foundation.
  11 *
  12 * This program is distributed in the hope that it will be useful, but WITHOUT
  13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
  15 * more details.
  16 *
  17 * You should have received a copy of the GNU General Public License along with
  18 * this program; if not, write to the Free Software Foundation, Inc.,
  19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
  20 *
  21 * The full GNU General Public License is included in this distribution in the
  22 * file called LICENSE.
  23 *
  24 * Contact Information:
  25 *  Intel Linux Wireless <ilw@linux.intel.com>
  26 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  27 *
  28 *****************************************************************************/
  29#include <linux/etherdevice.h>
  30#include <net/mac80211.h>
  31
  32#include "iwl-dev.h"
  33#include "iwl-agn.h"
  34#include "iwl-trans.h"
  35
  36const u8 iwl_bcast_addr[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
  37
  38static int iwl_sta_ucode_activate(struct iwl_priv *priv, u8 sta_id)
  39{
  40	lockdep_assert_held(&priv->sta_lock);
  41
  42	if (sta_id >= IWLAGN_STATION_COUNT) {
  43		IWL_ERR(priv, "invalid sta_id %u", sta_id);
  44		return -EINVAL;
  45	}
  46	if (!(priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE))
  47		IWL_ERR(priv, "ACTIVATE a non DRIVER active station id %u "
  48			"addr %pM\n",
  49			sta_id, priv->stations[sta_id].sta.sta.addr);
  50
  51	if (priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE) {
  52		IWL_DEBUG_ASSOC(priv,
  53				"STA id %u addr %pM already present in uCode "
  54				"(according to driver)\n",
  55				sta_id, priv->stations[sta_id].sta.sta.addr);
  56	} else {
  57		priv->stations[sta_id].used |= IWL_STA_UCODE_ACTIVE;
  58		IWL_DEBUG_ASSOC(priv, "Added STA id %u addr %pM to uCode\n",
  59				sta_id, priv->stations[sta_id].sta.sta.addr);
  60	}
  61	return 0;
  62}
  63
  64static int iwl_process_add_sta_resp(struct iwl_priv *priv,
  65				    struct iwl_addsta_cmd *addsta,
  66				    struct iwl_rx_packet *pkt)
  67{
  68	struct iwl_add_sta_resp *add_sta_resp = (void *)pkt->data;
  69	u8 sta_id = addsta->sta.sta_id;
  70	int ret = -EIO;
  71
  72	if (pkt->hdr.flags & IWL_CMD_FAILED_MSK) {
  73		IWL_ERR(priv, "Bad return from REPLY_ADD_STA (0x%08X)\n",
  74			pkt->hdr.flags);
  75		return ret;
  76	}
  77
  78	IWL_DEBUG_INFO(priv, "Processing response for adding station %u\n",
  79		       sta_id);
  80
  81	spin_lock(&priv->sta_lock);
  82
  83	switch (add_sta_resp->status) {
  84	case ADD_STA_SUCCESS_MSK:
  85		IWL_DEBUG_INFO(priv, "REPLY_ADD_STA PASSED\n");
  86		ret = iwl_sta_ucode_activate(priv, sta_id);
  87		break;
  88	case ADD_STA_NO_ROOM_IN_TABLE:
  89		IWL_ERR(priv, "Adding station %d failed, no room in table.\n",
  90			sta_id);
  91		break;
  92	case ADD_STA_NO_BLOCK_ACK_RESOURCE:
  93		IWL_ERR(priv, "Adding station %d failed, no block ack "
  94			"resource.\n", sta_id);
  95		break;
  96	case ADD_STA_MODIFY_NON_EXIST_STA:
  97		IWL_ERR(priv, "Attempting to modify non-existing station %d\n",
  98			sta_id);
  99		break;
 100	default:
 101		IWL_DEBUG_ASSOC(priv, "Received REPLY_ADD_STA:(0x%08X)\n",
 102				add_sta_resp->status);
 103		break;
 104	}
 105
 106	IWL_DEBUG_INFO(priv, "%s station id %u addr %pM\n",
 107		       priv->stations[sta_id].sta.mode ==
 108		       STA_CONTROL_MODIFY_MSK ?  "Modified" : "Added",
 109		       sta_id, priv->stations[sta_id].sta.sta.addr);
 110
 111	/*
 112	 * XXX: The MAC address in the command buffer is often changed from
 113	 * the original sent to the device. That is, the MAC address
 114	 * written to the command buffer often is not the same MAC address
 115	 * read from the command buffer when the command returns. This
 116	 * issue has not yet been resolved and this debugging is left to
 117	 * observe the problem.
 118	 */
 119	IWL_DEBUG_INFO(priv, "%s station according to cmd buffer %pM\n",
 120		       priv->stations[sta_id].sta.mode ==
 121		       STA_CONTROL_MODIFY_MSK ? "Modified" : "Added",
 122		       addsta->sta.addr);
 123	spin_unlock(&priv->sta_lock);
 124
 125	return ret;
 126}
 127
 128int iwl_add_sta_callback(struct iwl_priv *priv, struct iwl_rx_cmd_buffer *rxb,
 129			       struct iwl_device_cmd *cmd)
 130{
 131	struct iwl_rx_packet *pkt = rxb_addr(rxb);
 132	struct iwl_addsta_cmd *addsta =
 133		(struct iwl_addsta_cmd *) cmd->payload;
 134
 135	return iwl_process_add_sta_resp(priv, addsta, pkt);
 136}
 137
 138int iwl_send_add_sta(struct iwl_priv *priv,
 139		     struct iwl_addsta_cmd *sta, u8 flags)
 140{
 141	int ret = 0;
 142	struct iwl_host_cmd cmd = {
 143		.id = REPLY_ADD_STA,
 144		.flags = flags,
 145		.data = { sta, },
 146		.len = { sizeof(*sta), },
 147	};
 148	u8 sta_id __maybe_unused = sta->sta.sta_id;
 149
 150	IWL_DEBUG_INFO(priv, "Adding sta %u (%pM) %ssynchronously\n",
 151		       sta_id, sta->sta.addr, flags & CMD_ASYNC ?  "a" : "");
 152
 153	if (!(flags & CMD_ASYNC)) {
 154		cmd.flags |= CMD_WANT_SKB;
 155		might_sleep();
 156	}
 157
 158	ret = iwl_dvm_send_cmd(priv, &cmd);
 159
 160	if (ret || (flags & CMD_ASYNC))
 161		return ret;
 162	/*else the command was successfully sent in SYNC mode, need to free
 163	 * the reply page */
 164
 165	iwl_free_resp(&cmd);
 166
 167	if (cmd.handler_status)
 168		IWL_ERR(priv, "%s - error in the CMD response %d", __func__,
 169			cmd.handler_status);
 170
 171	return cmd.handler_status;
 172}
 173
 174static bool iwl_is_channel_extension(struct iwl_priv *priv,
 175				     enum ieee80211_band band,
 176				     u16 channel, u8 extension_chan_offset)
 177{
 178	const struct iwl_channel_info *ch_info;
 179
 180	ch_info = iwl_get_channel_info(priv, band, channel);
 181	if (!is_channel_valid(ch_info))
 182		return false;
 183
 184	if (extension_chan_offset == IEEE80211_HT_PARAM_CHA_SEC_ABOVE)
 185		return !(ch_info->ht40_extension_channel &
 186					IEEE80211_CHAN_NO_HT40PLUS);
 187	else if (extension_chan_offset == IEEE80211_HT_PARAM_CHA_SEC_BELOW)
 188		return !(ch_info->ht40_extension_channel &
 189					IEEE80211_CHAN_NO_HT40MINUS);
 190
 191	return false;
 192}
 193
 194bool iwl_is_ht40_tx_allowed(struct iwl_priv *priv,
 195			    struct iwl_rxon_context *ctx,
 196			    struct ieee80211_sta_ht_cap *ht_cap)
 197{
 198	if (!ctx->ht.enabled || !ctx->ht.is_40mhz)
 199		return false;
 200
 201	/*
 202	 * We do not check for IEEE80211_HT_CAP_SUP_WIDTH_20_40
 203	 * the bit will not set if it is pure 40MHz case
 204	 */
 205	if (ht_cap && !ht_cap->ht_supported)
 206		return false;
 207
 208#ifdef CONFIG_IWLWIFI_DEBUGFS
 209	if (priv->disable_ht40)
 210		return false;
 211#endif
 212
 213	return iwl_is_channel_extension(priv, priv->band,
 214			le16_to_cpu(ctx->staging.channel),
 215			ctx->ht.extension_chan_offset);
 216}
 217
 218static void iwl_sta_calc_ht_flags(struct iwl_priv *priv,
 219				  struct ieee80211_sta *sta,
 220				  struct iwl_rxon_context *ctx,
 221				  __le32 *flags, __le32 *mask)
 222{
 223	struct ieee80211_sta_ht_cap *sta_ht_inf = &sta->ht_cap;
 224	u8 mimo_ps_mode;
 225
 226	*mask = STA_FLG_RTS_MIMO_PROT_MSK |
 227		STA_FLG_MIMO_DIS_MSK |
 228		STA_FLG_HT40_EN_MSK |
 229		STA_FLG_MAX_AGG_SIZE_MSK |
 230		STA_FLG_AGG_MPDU_DENSITY_MSK;
 231	*flags = 0;
 232
 233	if (!sta || !sta_ht_inf->ht_supported)
 234		return;
 235
 236	mimo_ps_mode = (sta_ht_inf->cap & IEEE80211_HT_CAP_SM_PS) >> 2;
 237
 238	IWL_DEBUG_INFO(priv, "STA %pM SM PS mode: %s\n",
 239			sta->addr,
 240			(mimo_ps_mode == WLAN_HT_CAP_SM_PS_STATIC) ?
 241			"static" :
 242			(mimo_ps_mode == WLAN_HT_CAP_SM_PS_DYNAMIC) ?
 243			"dynamic" : "disabled");
 244
 245	switch (mimo_ps_mode) {
 246	case WLAN_HT_CAP_SM_PS_STATIC:
 247		*flags |= STA_FLG_MIMO_DIS_MSK;
 248		break;
 249	case WLAN_HT_CAP_SM_PS_DYNAMIC:
 250		*flags |= STA_FLG_RTS_MIMO_PROT_MSK;
 251		break;
 252	case WLAN_HT_CAP_SM_PS_DISABLED:
 253		break;
 254	default:
 255		IWL_WARN(priv, "Invalid MIMO PS mode %d\n", mimo_ps_mode);
 256		break;
 257	}
 258
 259	*flags |= cpu_to_le32(
 260		(u32)sta_ht_inf->ampdu_factor << STA_FLG_MAX_AGG_SIZE_POS);
 261
 262	*flags |= cpu_to_le32(
 263		(u32)sta_ht_inf->ampdu_density << STA_FLG_AGG_MPDU_DENSITY_POS);
 264
 265	if (iwl_is_ht40_tx_allowed(priv, ctx, &sta->ht_cap))
 266		*flags |= STA_FLG_HT40_EN_MSK;
 267}
 268
 269int iwl_sta_update_ht(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
 270		      struct ieee80211_sta *sta)
 271{
 272	u8 sta_id = iwl_sta_id(sta);
 273	__le32 flags, mask;
 274	struct iwl_addsta_cmd cmd;
 275
 276	if (WARN_ON_ONCE(sta_id == IWL_INVALID_STATION))
 277		return -EINVAL;
 278
 279	iwl_sta_calc_ht_flags(priv, sta, ctx, &flags, &mask);
 280
 281	spin_lock_bh(&priv->sta_lock);
 282	priv->stations[sta_id].sta.station_flags &= ~mask;
 283	priv->stations[sta_id].sta.station_flags |= flags;
 284	spin_unlock_bh(&priv->sta_lock);
 285
 286	memset(&cmd, 0, sizeof(cmd));
 287	cmd.mode = STA_CONTROL_MODIFY_MSK;
 288	cmd.station_flags_msk = mask;
 289	cmd.station_flags = flags;
 290	cmd.sta.sta_id = sta_id;
 291
 292	return iwl_send_add_sta(priv, &cmd, CMD_SYNC);
 293}
 294
 295static void iwl_set_ht_add_station(struct iwl_priv *priv, u8 index,
 296				   struct ieee80211_sta *sta,
 297				   struct iwl_rxon_context *ctx)
 298{
 299	__le32 flags, mask;
 300
 301	iwl_sta_calc_ht_flags(priv, sta, ctx, &flags, &mask);
 302
 303	lockdep_assert_held(&priv->sta_lock);
 304	priv->stations[index].sta.station_flags &= ~mask;
 305	priv->stations[index].sta.station_flags |= flags;
 306}
 307
 308/**
 309 * iwl_prep_station - Prepare station information for addition
 310 *
 311 * should be called with sta_lock held
 312 */
 313u8 iwl_prep_station(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
 314		    const u8 *addr, bool is_ap, struct ieee80211_sta *sta)
 315{
 316	struct iwl_station_entry *station;
 317	int i;
 318	u8 sta_id = IWL_INVALID_STATION;
 319
 320	if (is_ap)
 321		sta_id = ctx->ap_sta_id;
 322	else if (is_broadcast_ether_addr(addr))
 323		sta_id = ctx->bcast_sta_id;
 324	else
 325		for (i = IWL_STA_ID; i < IWLAGN_STATION_COUNT; i++) {
 326			if (ether_addr_equal(priv->stations[i].sta.sta.addr,
 327					     addr)) {
 328				sta_id = i;
 329				break;
 330			}
 331
 332			if (!priv->stations[i].used &&
 333			    sta_id == IWL_INVALID_STATION)
 334				sta_id = i;
 335		}
 336
 337	/*
 338	 * These two conditions have the same outcome, but keep them
 339	 * separate
 340	 */
 341	if (unlikely(sta_id == IWL_INVALID_STATION))
 342		return sta_id;
 343
 344	/*
 345	 * uCode is not able to deal with multiple requests to add a
 346	 * station. Keep track if one is in progress so that we do not send
 347	 * another.
 348	 */
 349	if (priv->stations[sta_id].used & IWL_STA_UCODE_INPROGRESS) {
 350		IWL_DEBUG_INFO(priv, "STA %d already in process of being "
 351			       "added.\n", sta_id);
 352		return sta_id;
 353	}
 354
 355	if ((priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE) &&
 356	    (priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE) &&
 357	    ether_addr_equal(priv->stations[sta_id].sta.sta.addr, addr)) {
 358		IWL_DEBUG_ASSOC(priv, "STA %d (%pM) already added, not "
 359				"adding again.\n", sta_id, addr);
 360		return sta_id;
 361	}
 362
 363	station = &priv->stations[sta_id];
 364	station->used = IWL_STA_DRIVER_ACTIVE;
 365	IWL_DEBUG_ASSOC(priv, "Add STA to driver ID %d: %pM\n",
 366			sta_id, addr);
 367	priv->num_stations++;
 368
 369	/* Set up the REPLY_ADD_STA command to send to device */
 370	memset(&station->sta, 0, sizeof(struct iwl_addsta_cmd));
 371	memcpy(station->sta.sta.addr, addr, ETH_ALEN);
 372	station->sta.mode = 0;
 373	station->sta.sta.sta_id = sta_id;
 374	station->sta.station_flags = ctx->station_flags;
 375	station->ctxid = ctx->ctxid;
 376
 377	if (sta) {
 378		struct iwl_station_priv *sta_priv;
 379
 380		sta_priv = (void *)sta->drv_priv;
 381		sta_priv->ctx = ctx;
 382	}
 383
 384	/*
 385	 * OK to call unconditionally, since local stations (IBSS BSSID
 386	 * STA and broadcast STA) pass in a NULL sta, and mac80211
 387	 * doesn't allow HT IBSS.
 388	 */
 389	iwl_set_ht_add_station(priv, sta_id, sta, ctx);
 390
 391	return sta_id;
 392
 393}
 394
 395#define STA_WAIT_TIMEOUT (HZ/2)
 396
 397/**
 398 * iwl_add_station_common -
 399 */
 400int iwl_add_station_common(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
 401			   const u8 *addr, bool is_ap,
 402			   struct ieee80211_sta *sta, u8 *sta_id_r)
 403{
 404	int ret = 0;
 405	u8 sta_id;
 406	struct iwl_addsta_cmd sta_cmd;
 407
 408	*sta_id_r = 0;
 409	spin_lock_bh(&priv->sta_lock);
 410	sta_id = iwl_prep_station(priv, ctx, addr, is_ap, sta);
 411	if (sta_id == IWL_INVALID_STATION) {
 412		IWL_ERR(priv, "Unable to prepare station %pM for addition\n",
 413			addr);
 414		spin_unlock_bh(&priv->sta_lock);
 415		return -EINVAL;
 416	}
 417
 418	/*
 419	 * uCode is not able to deal with multiple requests to add a
 420	 * station. Keep track if one is in progress so that we do not send
 421	 * another.
 422	 */
 423	if (priv->stations[sta_id].used & IWL_STA_UCODE_INPROGRESS) {
 424		IWL_DEBUG_INFO(priv, "STA %d already in process of being "
 425			       "added.\n", sta_id);
 426		spin_unlock_bh(&priv->sta_lock);
 427		return -EEXIST;
 428	}
 429
 430	if ((priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE) &&
 431	    (priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE)) {
 432		IWL_DEBUG_ASSOC(priv, "STA %d (%pM) already added, not "
 433				"adding again.\n", sta_id, addr);
 434		spin_unlock_bh(&priv->sta_lock);
 435		return -EEXIST;
 436	}
 437
 438	priv->stations[sta_id].used |= IWL_STA_UCODE_INPROGRESS;
 439	memcpy(&sta_cmd, &priv->stations[sta_id].sta,
 440	       sizeof(struct iwl_addsta_cmd));
 441	spin_unlock_bh(&priv->sta_lock);
 442
 443	/* Add station to device's station table */
 444	ret = iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
 445	if (ret) {
 446		spin_lock_bh(&priv->sta_lock);
 447		IWL_ERR(priv, "Adding station %pM failed.\n",
 448			priv->stations[sta_id].sta.sta.addr);
 449		priv->stations[sta_id].used &= ~IWL_STA_DRIVER_ACTIVE;
 450		priv->stations[sta_id].used &= ~IWL_STA_UCODE_INPROGRESS;
 451		spin_unlock_bh(&priv->sta_lock);
 452	}
 453	*sta_id_r = sta_id;
 454	return ret;
 455}
 456
 457/**
 458 * iwl_sta_ucode_deactivate - deactivate ucode status for a station
 459 */
 460static void iwl_sta_ucode_deactivate(struct iwl_priv *priv, u8 sta_id)
 461{
 462	lockdep_assert_held(&priv->sta_lock);
 463
 464	/* Ucode must be active and driver must be non active */
 465	if ((priv->stations[sta_id].used &
 466	     (IWL_STA_UCODE_ACTIVE | IWL_STA_DRIVER_ACTIVE)) !=
 467	      IWL_STA_UCODE_ACTIVE)
 468		IWL_ERR(priv, "removed non active STA %u\n", sta_id);
 469
 470	priv->stations[sta_id].used &= ~IWL_STA_UCODE_ACTIVE;
 471
 472	memset(&priv->stations[sta_id], 0, sizeof(struct iwl_station_entry));
 473	IWL_DEBUG_ASSOC(priv, "Removed STA %u\n", sta_id);
 474}
 475
 476static int iwl_send_remove_station(struct iwl_priv *priv,
 477				   const u8 *addr, int sta_id,
 478				   bool temporary)
 479{
 480	struct iwl_rx_packet *pkt;
 481	int ret;
 482	struct iwl_rem_sta_cmd rm_sta_cmd;
 483
 484	struct iwl_host_cmd cmd = {
 485		.id = REPLY_REMOVE_STA,
 486		.len = { sizeof(struct iwl_rem_sta_cmd), },
 487		.flags = CMD_SYNC,
 488		.data = { &rm_sta_cmd, },
 489	};
 490
 491	memset(&rm_sta_cmd, 0, sizeof(rm_sta_cmd));
 492	rm_sta_cmd.num_sta = 1;
 493	memcpy(&rm_sta_cmd.addr, addr, ETH_ALEN);
 494
 495	cmd.flags |= CMD_WANT_SKB;
 496
 497	ret = iwl_dvm_send_cmd(priv, &cmd);
 498
 499	if (ret)
 500		return ret;
 501
 502	pkt = cmd.resp_pkt;
 503	if (pkt->hdr.flags & IWL_CMD_FAILED_MSK) {
 504		IWL_ERR(priv, "Bad return from REPLY_REMOVE_STA (0x%08X)\n",
 505			  pkt->hdr.flags);
 506		ret = -EIO;
 507	}
 508
 509	if (!ret) {
 510		struct iwl_rem_sta_resp *rem_sta_resp = (void *)pkt->data;
 511		switch (rem_sta_resp->status) {
 512		case REM_STA_SUCCESS_MSK:
 513			if (!temporary) {
 514				spin_lock_bh(&priv->sta_lock);
 515				iwl_sta_ucode_deactivate(priv, sta_id);
 516				spin_unlock_bh(&priv->sta_lock);
 517			}
 518			IWL_DEBUG_ASSOC(priv, "REPLY_REMOVE_STA PASSED\n");
 519			break;
 520		default:
 521			ret = -EIO;
 522			IWL_ERR(priv, "REPLY_REMOVE_STA failed\n");
 523			break;
 524		}
 525	}
 526	iwl_free_resp(&cmd);
 527
 528	return ret;
 529}
 530
 531/**
 532 * iwl_remove_station - Remove driver's knowledge of station.
 533 */
 534int iwl_remove_station(struct iwl_priv *priv, const u8 sta_id,
 535		       const u8 *addr)
 536{
 537	u8 tid;
 538
 539	if (!iwl_is_ready(priv)) {
 540		IWL_DEBUG_INFO(priv,
 541			"Unable to remove station %pM, device not ready.\n",
 542			addr);
 543		/*
 544		 * It is typical for stations to be removed when we are
 545		 * going down. Return success since device will be down
 546		 * soon anyway
 547		 */
 548		return 0;
 549	}
 550
 551	IWL_DEBUG_ASSOC(priv, "Removing STA from driver:%d  %pM\n",
 552			sta_id, addr);
 553
 554	if (WARN_ON(sta_id == IWL_INVALID_STATION))
 555		return -EINVAL;
 556
 557	spin_lock_bh(&priv->sta_lock);
 558
 559	if (!(priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE)) {
 560		IWL_DEBUG_INFO(priv, "Removing %pM but non DRIVER active\n",
 561				addr);
 562		goto out_err;
 563	}
 564
 565	if (!(priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE)) {
 566		IWL_DEBUG_INFO(priv, "Removing %pM but non UCODE active\n",
 567				addr);
 568		goto out_err;
 569	}
 570
 571	if (priv->stations[sta_id].used & IWL_STA_LOCAL) {
 572		kfree(priv->stations[sta_id].lq);
 573		priv->stations[sta_id].lq = NULL;
 574	}
 575
 576	for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++)
 577		memset(&priv->tid_data[sta_id][tid], 0,
 578			sizeof(priv->tid_data[sta_id][tid]));
 579
 580	priv->stations[sta_id].used &= ~IWL_STA_DRIVER_ACTIVE;
 581
 582	priv->num_stations--;
 583
 584	if (WARN_ON(priv->num_stations < 0))
 585		priv->num_stations = 0;
 586
 587	spin_unlock_bh(&priv->sta_lock);
 588
 589	return iwl_send_remove_station(priv, addr, sta_id, false);
 590out_err:
 591	spin_unlock_bh(&priv->sta_lock);
 592	return -EINVAL;
 593}
 594
 595void iwl_deactivate_station(struct iwl_priv *priv, const u8 sta_id,
 596			    const u8 *addr)
 597{
 598	u8 tid;
 599
 600	if (!iwl_is_ready(priv)) {
 601		IWL_DEBUG_INFO(priv,
 602			"Unable to remove station %pM, device not ready.\n",
 603			addr);
 604		return;
 605	}
 606
 607	IWL_DEBUG_ASSOC(priv, "Deactivating STA: %pM (%d)\n", addr, sta_id);
 608
 609	if (WARN_ON_ONCE(sta_id == IWL_INVALID_STATION))
 610		return;
 611
 612	spin_lock_bh(&priv->sta_lock);
 613
 614	WARN_ON_ONCE(!(priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE));
 615
 616	for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++)
 617		memset(&priv->tid_data[sta_id][tid], 0,
 618			sizeof(priv->tid_data[sta_id][tid]));
 619
 620	priv->stations[sta_id].used &= ~IWL_STA_DRIVER_ACTIVE;
 621
 622	priv->num_stations--;
 623
 624	if (WARN_ON_ONCE(priv->num_stations < 0))
 625		priv->num_stations = 0;
 626
 627	spin_unlock_bh(&priv->sta_lock);
 628}
 629
 630static void iwl_sta_fill_lq(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
 631			    u8 sta_id, struct iwl_link_quality_cmd *link_cmd)
 632{
 633	int i, r;
 634	u32 rate_flags = 0;
 635	__le32 rate_n_flags;
 636
 637	lockdep_assert_held(&priv->mutex);
 638
 639	memset(link_cmd, 0, sizeof(*link_cmd));
 640
 641	/* Set up the rate scaling to start at selected rate, fall back
 642	 * all the way down to 1M in IEEE order, and then spin on 1M */
 643	if (priv->band == IEEE80211_BAND_5GHZ)
 644		r = IWL_RATE_6M_INDEX;
 645	else if (ctx && ctx->vif && ctx->vif->p2p)
 646		r = IWL_RATE_6M_INDEX;
 647	else
 648		r = IWL_RATE_1M_INDEX;
 649
 650	if (r >= IWL_FIRST_CCK_RATE && r <= IWL_LAST_CCK_RATE)
 651		rate_flags |= RATE_MCS_CCK_MSK;
 652
 653	rate_flags |= first_antenna(priv->hw_params.valid_tx_ant) <<
 654				RATE_MCS_ANT_POS;
 655	rate_n_flags = iwl_hw_set_rate_n_flags(iwl_rates[r].plcp, rate_flags);
 656	for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
 657		link_cmd->rs_table[i].rate_n_flags = rate_n_flags;
 658
 659	link_cmd->general_params.single_stream_ant_msk =
 660			first_antenna(priv->hw_params.valid_tx_ant);
 661
 662	link_cmd->general_params.dual_stream_ant_msk =
 663		priv->hw_params.valid_tx_ant &
 664		~first_antenna(priv->hw_params.valid_tx_ant);
 665	if (!link_cmd->general_params.dual_stream_ant_msk) {
 666		link_cmd->general_params.dual_stream_ant_msk = ANT_AB;
 667	} else if (num_of_ant(priv->hw_params.valid_tx_ant) == 2) {
 668		link_cmd->general_params.dual_stream_ant_msk =
 669			priv->hw_params.valid_tx_ant;
 670	}
 671
 672	link_cmd->agg_params.agg_dis_start_th =
 673		LINK_QUAL_AGG_DISABLE_START_DEF;
 674	link_cmd->agg_params.agg_time_limit =
 675		cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
 676
 677	link_cmd->sta_id = sta_id;
 678}
 679
 680/**
 681 * iwl_clear_ucode_stations - clear ucode station table bits
 682 *
 683 * This function clears all the bits in the driver indicating
 684 * which stations are active in the ucode. Call when something
 685 * other than explicit station management would cause this in
 686 * the ucode, e.g. unassociated RXON.
 687 */
 688void iwl_clear_ucode_stations(struct iwl_priv *priv,
 689			      struct iwl_rxon_context *ctx)
 690{
 691	int i;
 692	bool cleared = false;
 693
 694	IWL_DEBUG_INFO(priv, "Clearing ucode stations in driver\n");
 695
 696	spin_lock_bh(&priv->sta_lock);
 697	for (i = 0; i < IWLAGN_STATION_COUNT; i++) {
 698		if (ctx && ctx->ctxid != priv->stations[i].ctxid)
 699			continue;
 700
 701		if (priv->stations[i].used & IWL_STA_UCODE_ACTIVE) {
 702			IWL_DEBUG_INFO(priv,
 703				"Clearing ucode active for station %d\n", i);
 704			priv->stations[i].used &= ~IWL_STA_UCODE_ACTIVE;
 705			cleared = true;
 706		}
 707	}
 708	spin_unlock_bh(&priv->sta_lock);
 709
 710	if (!cleared)
 711		IWL_DEBUG_INFO(priv,
 712			       "No active stations found to be cleared\n");
 713}
 714
 715/**
 716 * iwl_restore_stations() - Restore driver known stations to device
 717 *
 718 * All stations considered active by driver, but not present in ucode, is
 719 * restored.
 720 *
 721 * Function sleeps.
 722 */
 723void iwl_restore_stations(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
 724{
 725	struct iwl_addsta_cmd sta_cmd;
 726	struct iwl_link_quality_cmd lq;
 727	int i;
 728	bool found = false;
 729	int ret;
 730	bool send_lq;
 731
 732	if (!iwl_is_ready(priv)) {
 733		IWL_DEBUG_INFO(priv,
 734			       "Not ready yet, not restoring any stations.\n");
 735		return;
 736	}
 737
 738	IWL_DEBUG_ASSOC(priv, "Restoring all known stations ... start.\n");
 739	spin_lock_bh(&priv->sta_lock);
 740	for (i = 0; i < IWLAGN_STATION_COUNT; i++) {
 741		if (ctx->ctxid != priv->stations[i].ctxid)
 742			continue;
 743		if ((priv->stations[i].used & IWL_STA_DRIVER_ACTIVE) &&
 744			    !(priv->stations[i].used & IWL_STA_UCODE_ACTIVE)) {
 745			IWL_DEBUG_ASSOC(priv, "Restoring sta %pM\n",
 746					priv->stations[i].sta.sta.addr);
 747			priv->stations[i].sta.mode = 0;
 748			priv->stations[i].used |= IWL_STA_UCODE_INPROGRESS;
 749			found = true;
 750		}
 751	}
 752
 753	for (i = 0; i < IWLAGN_STATION_COUNT; i++) {
 754		if ((priv->stations[i].used & IWL_STA_UCODE_INPROGRESS)) {
 755			memcpy(&sta_cmd, &priv->stations[i].sta,
 756			       sizeof(struct iwl_addsta_cmd));
 757			send_lq = false;
 758			if (priv->stations[i].lq) {
 759				if (priv->wowlan)
 760					iwl_sta_fill_lq(priv, ctx, i, &lq);
 761				else
 762					memcpy(&lq, priv->stations[i].lq,
 763					       sizeof(struct iwl_link_quality_cmd));
 764				send_lq = true;
 765			}
 766			spin_unlock_bh(&priv->sta_lock);
 767			ret = iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
 768			if (ret) {
 769				spin_lock_bh(&priv->sta_lock);
 770				IWL_ERR(priv, "Adding station %pM failed.\n",
 771					priv->stations[i].sta.sta.addr);
 772				priv->stations[i].used &=
 773						~IWL_STA_DRIVER_ACTIVE;
 774				priv->stations[i].used &=
 775						~IWL_STA_UCODE_INPROGRESS;
 776				continue;
 777			}
 778			/*
 779			 * Rate scaling has already been initialized, send
 780			 * current LQ command
 781			 */
 782			if (send_lq)
 783				iwl_send_lq_cmd(priv, ctx, &lq,
 784						CMD_SYNC, true);
 785			spin_lock_bh(&priv->sta_lock);
 786			priv->stations[i].used &= ~IWL_STA_UCODE_INPROGRESS;
 787		}
 788	}
 789
 790	spin_unlock_bh(&priv->sta_lock);
 791	if (!found)
 792		IWL_DEBUG_INFO(priv, "Restoring all known stations .... "
 793			"no stations to be restored.\n");
 794	else
 795		IWL_DEBUG_INFO(priv, "Restoring all known stations .... "
 796			"complete.\n");
 797}
 798
 799int iwl_get_free_ucode_key_offset(struct iwl_priv *priv)
 800{
 801	int i;
 802
 803	for (i = 0; i < priv->sta_key_max_num; i++)
 804		if (!test_and_set_bit(i, &priv->ucode_key_table))
 805			return i;
 806
 807	return WEP_INVALID_OFFSET;
 808}
 809
 810void iwl_dealloc_bcast_stations(struct iwl_priv *priv)
 811{
 812	int i;
 813
 814	spin_lock_bh(&priv->sta_lock);
 815	for (i = 0; i < IWLAGN_STATION_COUNT; i++) {
 816		if (!(priv->stations[i].used & IWL_STA_BCAST))
 817			continue;
 818
 819		priv->stations[i].used &= ~IWL_STA_UCODE_ACTIVE;
 820		priv->num_stations--;
 821		if (WARN_ON(priv->num_stations < 0))
 822			priv->num_stations = 0;
 823		kfree(priv->stations[i].lq);
 824		priv->stations[i].lq = NULL;
 825	}
 826	spin_unlock_bh(&priv->sta_lock);
 827}
 828
 829#ifdef CONFIG_IWLWIFI_DEBUG
 830static void iwl_dump_lq_cmd(struct iwl_priv *priv,
 831			   struct iwl_link_quality_cmd *lq)
 832{
 833	int i;
 834	IWL_DEBUG_RATE(priv, "lq station id 0x%x\n", lq->sta_id);
 835	IWL_DEBUG_RATE(priv, "lq ant 0x%X 0x%X\n",
 836		       lq->general_params.single_stream_ant_msk,
 837		       lq->general_params.dual_stream_ant_msk);
 838
 839	for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
 840		IWL_DEBUG_RATE(priv, "lq index %d 0x%X\n",
 841			       i, lq->rs_table[i].rate_n_flags);
 842}
 843#else
 844static inline void iwl_dump_lq_cmd(struct iwl_priv *priv,
 845				   struct iwl_link_quality_cmd *lq)
 846{
 847}
 848#endif
 849
 850/**
 851 * is_lq_table_valid() - Test one aspect of LQ cmd for validity
 852 *
 853 * It sometimes happens when a HT rate has been in use and we
 854 * loose connectivity with AP then mac80211 will first tell us that the
 855 * current channel is not HT anymore before removing the station. In such a
 856 * scenario the RXON flags will be updated to indicate we are not
 857 * communicating HT anymore, but the LQ command may still contain HT rates.
 858 * Test for this to prevent driver from sending LQ command between the time
 859 * RXON flags are updated and when LQ command is updated.
 860 */
 861static bool is_lq_table_valid(struct iwl_priv *priv,
 862			      struct iwl_rxon_context *ctx,
 863			      struct iwl_link_quality_cmd *lq)
 864{
 865	int i;
 866
 867	if (ctx->ht.enabled)
 868		return true;
 869
 870	IWL_DEBUG_INFO(priv, "Channel %u is not an HT channel\n",
 871		       ctx->active.channel);
 872	for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
 873		if (le32_to_cpu(lq->rs_table[i].rate_n_flags) &
 874		    RATE_MCS_HT_MSK) {
 875			IWL_DEBUG_INFO(priv,
 876				       "index %d of LQ expects HT channel\n",
 877				       i);
 878			return false;
 879		}
 880	}
 881	return true;
 882}
 883
 884/**
 885 * iwl_send_lq_cmd() - Send link quality command
 886 * @init: This command is sent as part of station initialization right
 887 *        after station has been added.
 888 *
 889 * The link quality command is sent as the last step of station creation.
 890 * This is the special case in which init is set and we call a callback in
 891 * this case to clear the state indicating that station creation is in
 892 * progress.
 893 */
 894int iwl_send_lq_cmd(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
 895		    struct iwl_link_quality_cmd *lq, u8 flags, bool init)
 896{
 897	int ret = 0;
 898	struct iwl_host_cmd cmd = {
 899		.id = REPLY_TX_LINK_QUALITY_CMD,
 900		.len = { sizeof(struct iwl_link_quality_cmd), },
 901		.flags = flags,
 902		.data = { lq, },
 903	};
 904
 905	if (WARN_ON(lq->sta_id == IWL_INVALID_STATION))
 906		return -EINVAL;
 907
 908
 909	spin_lock_bh(&priv->sta_lock);
 910	if (!(priv->stations[lq->sta_id].used & IWL_STA_DRIVER_ACTIVE)) {
 911		spin_unlock_bh(&priv->sta_lock);
 912		return -EINVAL;
 913	}
 914	spin_unlock_bh(&priv->sta_lock);
 915
 916	iwl_dump_lq_cmd(priv, lq);
 917	if (WARN_ON(init && (cmd.flags & CMD_ASYNC)))
 918		return -EINVAL;
 919
 920	if (is_lq_table_valid(priv, ctx, lq))
 921		ret = iwl_dvm_send_cmd(priv, &cmd);
 922	else
 923		ret = -EINVAL;
 924
 925	if (cmd.flags & CMD_ASYNC)
 926		return ret;
 927
 928	if (init) {
 929		IWL_DEBUG_INFO(priv, "init LQ command complete, "
 930			       "clearing sta addition status for sta %d\n",
 931			       lq->sta_id);
 932		spin_lock_bh(&priv->sta_lock);
 933		priv->stations[lq->sta_id].used &= ~IWL_STA_UCODE_INPROGRESS;
 934		spin_unlock_bh(&priv->sta_lock);
 935	}
 936	return ret;
 937}
 938
 939
 940static struct iwl_link_quality_cmd *
 941iwl_sta_alloc_lq(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
 942		 u8 sta_id)
 943{
 944	struct iwl_link_quality_cmd *link_cmd;
 945
 946	link_cmd = kzalloc(sizeof(struct iwl_link_quality_cmd), GFP_KERNEL);
 947	if (!link_cmd) {
 948		IWL_ERR(priv, "Unable to allocate memory for LQ cmd.\n");
 949		return NULL;
 950	}
 951
 952	iwl_sta_fill_lq(priv, ctx, sta_id, link_cmd);
 953
 954	return link_cmd;
 955}
 956
 957/*
 958 * iwlagn_add_bssid_station - Add the special IBSS BSSID station
 959 *
 960 * Function sleeps.
 961 */
 962int iwlagn_add_bssid_station(struct iwl_priv *priv,
 963			     struct iwl_rxon_context *ctx,
 964			     const u8 *addr, u8 *sta_id_r)
 965{
 966	int ret;
 967	u8 sta_id;
 968	struct iwl_link_quality_cmd *link_cmd;
 969
 970	if (sta_id_r)
 971		*sta_id_r = IWL_INVALID_STATION;
 972
 973	ret = iwl_add_station_common(priv, ctx, addr, 0, NULL, &sta_id);
 974	if (ret) {
 975		IWL_ERR(priv, "Unable to add station %pM\n", addr);
 976		return ret;
 977	}
 978
 979	if (sta_id_r)
 980		*sta_id_r = sta_id;
 981
 982	spin_lock_bh(&priv->sta_lock);
 983	priv->stations[sta_id].used |= IWL_STA_LOCAL;
 984	spin_unlock_bh(&priv->sta_lock);
 985
 986	/* Set up default rate scaling table in device's station table */
 987	link_cmd = iwl_sta_alloc_lq(priv, ctx, sta_id);
 988	if (!link_cmd) {
 989		IWL_ERR(priv,
 990			"Unable to initialize rate scaling for station %pM.\n",
 991			addr);
 992		return -ENOMEM;
 993	}
 994
 995	ret = iwl_send_lq_cmd(priv, ctx, link_cmd, CMD_SYNC, true);
 996	if (ret)
 997		IWL_ERR(priv, "Link quality command failed (%d)\n", ret);
 998
 999	spin_lock_bh(&priv->sta_lock);
1000	priv->stations[sta_id].lq = link_cmd;
1001	spin_unlock_bh(&priv->sta_lock);
1002
1003	return 0;
1004}
1005
1006/*
1007 * static WEP keys
1008 *
1009 * For each context, the device has a table of 4 static WEP keys
1010 * (one for each key index) that is updated with the following
1011 * commands.
1012 */
1013
1014static int iwl_send_static_wepkey_cmd(struct iwl_priv *priv,
1015				      struct iwl_rxon_context *ctx,
1016				      bool send_if_empty)
1017{
1018	int i, not_empty = 0;
1019	u8 buff[sizeof(struct iwl_wep_cmd) +
1020		sizeof(struct iwl_wep_key) * WEP_KEYS_MAX];
1021	struct iwl_wep_cmd *wep_cmd = (struct iwl_wep_cmd *)buff;
1022	size_t cmd_size  = sizeof(struct iwl_wep_cmd);
1023	struct iwl_host_cmd cmd = {
1024		.id = ctx->wep_key_cmd,
1025		.data = { wep_cmd, },
1026		.flags = CMD_SYNC,
1027	};
1028
1029	might_sleep();
1030
1031	memset(wep_cmd, 0, cmd_size +
1032			(sizeof(struct iwl_wep_key) * WEP_KEYS_MAX));
1033
1034	for (i = 0; i < WEP_KEYS_MAX ; i++) {
1035		wep_cmd->key[i].key_index = i;
1036		if (ctx->wep_keys[i].key_size) {
1037			wep_cmd->key[i].key_offset = i;
1038			not_empty = 1;
1039		} else {
1040			wep_cmd->key[i].key_offset = WEP_INVALID_OFFSET;
1041		}
1042
1043		wep_cmd->key[i].key_size = ctx->wep_keys[i].key_size;
1044		memcpy(&wep_cmd->key[i].key[3], ctx->wep_keys[i].key,
1045				ctx->wep_keys[i].key_size);
1046	}
1047
1048	wep_cmd->global_key_type = WEP_KEY_WEP_TYPE;
1049	wep_cmd->num_keys = WEP_KEYS_MAX;
1050
1051	cmd_size += sizeof(struct iwl_wep_key) * WEP_KEYS_MAX;
1052
1053	cmd.len[0] = cmd_size;
1054
1055	if (not_empty || send_if_empty)
1056		return iwl_dvm_send_cmd(priv, &cmd);
1057	else
1058		return 0;
1059}
1060
1061int iwl_restore_default_wep_keys(struct iwl_priv *priv,
1062				 struct iwl_rxon_context *ctx)
1063{
1064	lockdep_assert_held(&priv->mutex);
1065
1066	return iwl_send_static_wepkey_cmd(priv, ctx, false);
1067}
1068
1069int iwl_remove_default_wep_key(struct iwl_priv *priv,
1070			       struct iwl_rxon_context *ctx,
1071			       struct ieee80211_key_conf *keyconf)
1072{
1073	int ret;
1074
1075	lockdep_assert_held(&priv->mutex);
1076
1077	IWL_DEBUG_WEP(priv, "Removing default WEP key: idx=%d\n",
1078		      keyconf->keyidx);
1079
1080	memset(&ctx->wep_keys[keyconf->keyidx], 0, sizeof(ctx->wep_keys[0]));
1081	if (iwl_is_rfkill(priv)) {
1082		IWL_DEBUG_WEP(priv,
1083			"Not sending REPLY_WEPKEY command due to RFKILL.\n");
1084		/* but keys in device are clear anyway so return success */
1085		return 0;
1086	}
1087	ret = iwl_send_static_wepkey_cmd(priv, ctx, 1);
1088	IWL_DEBUG_WEP(priv, "Remove default WEP key: idx=%d ret=%d\n",
1089		      keyconf->keyidx, ret);
1090
1091	return ret;
1092}
1093
1094int iwl_set_default_wep_key(struct iwl_priv *priv,
1095			    struct iwl_rxon_context *ctx,
1096			    struct ieee80211_key_conf *keyconf)
1097{
1098	int ret;
1099
1100	lockdep_assert_held(&priv->mutex);
1101
1102	if (keyconf->keylen != WEP_KEY_LEN_128 &&
1103	    keyconf->keylen != WEP_KEY_LEN_64) {
1104		IWL_DEBUG_WEP(priv,
1105			      "Bad WEP key length %d\n", keyconf->keylen);
1106		return -EINVAL;
1107	}
1108
1109	keyconf->hw_key_idx = IWLAGN_HW_KEY_DEFAULT;
1110
1111	ctx->wep_keys[keyconf->keyidx].key_size = keyconf->keylen;
1112	memcpy(&ctx->wep_keys[keyconf->keyidx].key, &keyconf->key,
1113							keyconf->keylen);
1114
1115	ret = iwl_send_static_wepkey_cmd(priv, ctx, false);
1116	IWL_DEBUG_WEP(priv, "Set default WEP key: len=%d idx=%d ret=%d\n",
1117		keyconf->keylen, keyconf->keyidx, ret);
1118
1119	return ret;
1120}
1121
1122/*
1123 * dynamic (per-station) keys
1124 *
1125 * The dynamic keys are a little more complicated. The device has
1126 * a key cache of up to STA_KEY_MAX_NUM/STA_KEY_MAX_NUM_PAN keys.
1127 * These are linked to stations by a table that contains an index
1128 * into the key table for each station/key index/{mcast,unicast},
1129 * i.e. it's basically an array of pointers like this:
1130 *	key_offset_t key_mapping[NUM_STATIONS][4][2];
1131 * (it really works differently, but you can think of it as such)
1132 *
1133 * The key uploading and linking happens in the same command, the
1134 * add station command with STA_MODIFY_KEY_MASK.
1135 */
1136
1137static u8 iwlagn_key_sta_id(struct iwl_priv *priv,
1138			    struct ieee80211_vif *vif,
1139			    struct ieee80211_sta *sta)
1140{
1141	struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
1142
1143	if (sta)
1144		return iwl_sta_id(sta);
1145
1146	/*
1147	 * The device expects GTKs for station interfaces to be
1148	 * installed as GTKs for the AP station. If we have no
1149	 * station ID, then use the ap_sta_id in that case.
1150	 */
1151	if (vif->type == NL80211_IFTYPE_STATION && vif_priv->ctx)
1152		return vif_priv->ctx->ap_sta_id;
1153
1154	return IWL_INVALID_STATION;
1155}
1156
1157static int iwlagn_send_sta_key(struct iwl_priv *priv,
1158			       struct ieee80211_key_conf *keyconf,
1159			       u8 sta_id, u32 tkip_iv32, u16 *tkip_p1k,
1160			       u32 cmd_flags)
1161{
1162	__le16 key_flags;
1163	struct iwl_addsta_cmd sta_cmd;
1164	int i;
1165
1166	spin_lock_bh(&priv->sta_lock);
1167	memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(sta_cmd));
1168	spin_unlock_bh(&priv->sta_lock);
1169
1170	key_flags = cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
1171	key_flags |= STA_KEY_FLG_MAP_KEY_MSK;
1172
1173	switch (keyconf->cipher) {
1174	case WLAN_CIPHER_SUITE_CCMP:
1175		key_flags |= STA_KEY_FLG_CCMP;
1176		memcpy(sta_cmd.key.key, keyconf->key, keyconf->keylen);
1177		break;
1178	case WLAN_CIPHER_SUITE_TKIP:
1179		key_flags |= STA_KEY_FLG_TKIP;
1180		sta_cmd.key.tkip_rx_tsc_byte2 = tkip_iv32;
1181		for (i = 0; i < 5; i++)
1182			sta_cmd.key.tkip_rx_ttak[i] = cpu_to_le16(tkip_p1k[i]);
1183		memcpy(sta_cmd.key.key, keyconf->key, keyconf->keylen);
1184		break;
1185	case WLAN_CIPHER_SUITE_WEP104:
1186		key_flags |= STA_KEY_FLG_KEY_SIZE_MSK;
1187		/* fall through */
1188	case WLAN_CIPHER_SUITE_WEP40:
1189		key_flags |= STA_KEY_FLG_WEP;
1190		memcpy(&sta_cmd.key.key[3], keyconf->key, keyconf->keylen);
1191		break;
1192	default:
1193		WARN_ON(1);
1194		return -EINVAL;
1195	}
1196
1197	if (!(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE))
1198		key_flags |= STA_KEY_MULTICAST_MSK;
1199
1200	/* key pointer (offset) */
1201	sta_cmd.key.key_offset = keyconf->hw_key_idx;
1202
1203	sta_cmd.key.key_flags = key_flags;
1204	sta_cmd.mode = STA_CONTROL_MODIFY_MSK;
1205	sta_cmd.sta.modify_mask = STA_MODIFY_KEY_MASK;
1206
1207	return iwl_send_add_sta(priv, &sta_cmd, cmd_flags);
1208}
1209
1210void iwl_update_tkip_key(struct iwl_priv *priv,
1211			 struct ieee80211_vif *vif,
1212			 struct ieee80211_key_conf *keyconf,
1213			 struct ieee80211_sta *sta, u32 iv32, u16 *phase1key)
1214{
1215	u8 sta_id = iwlagn_key_sta_id(priv, vif, sta);
1216
1217	if (sta_id == IWL_INVALID_STATION)
1218		return;
1219
1220	if (iwl_scan_cancel(priv)) {
1221		/* cancel scan failed, just live w/ bad key and rely
1222		   briefly on SW decryption */
1223		return;
1224	}
1225
1226	iwlagn_send_sta_key(priv, keyconf, sta_id,
1227			    iv32, phase1key, CMD_ASYNC);
1228}
1229
1230int iwl_remove_dynamic_key(struct iwl_priv *priv,
1231			   struct iwl_rxon_context *ctx,
1232			   struct ieee80211_key_conf *keyconf,
1233			   struct ieee80211_sta *sta)
1234{
1235	struct iwl_addsta_cmd sta_cmd;
1236	u8 sta_id = iwlagn_key_sta_id(priv, ctx->vif, sta);
1237	__le16 key_flags;
1238
1239	/* if station isn't there, neither is the key */
1240	if (sta_id == IWL_INVALID_STATION)
1241		return -ENOENT;
1242
1243	spin_lock_bh(&priv->sta_lock);
1244	memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(sta_cmd));
1245	if (!(priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE))
1246		sta_id = IWL_INVALID_STATION;
1247	spin_unlock_bh(&priv->sta_lock);
1248
1249	if (sta_id == IWL_INVALID_STATION)
1250		return 0;
1251
1252	lockdep_assert_held(&priv->mutex);
1253
1254	ctx->key_mapping_keys--;
1255
1256	IWL_DEBUG_WEP(priv, "Remove dynamic key: idx=%d sta=%d\n",
1257		      keyconf->keyidx, sta_id);
1258
1259	if (!test_and_clear_bit(keyconf->hw_key_idx, &priv->ucode_key_table))
1260		IWL_ERR(priv, "offset %d not used in uCode key table.\n",
1261			keyconf->hw_key_idx);
1262
1263	key_flags = cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
1264	key_flags |= STA_KEY_FLG_MAP_KEY_MSK | STA_KEY_FLG_NO_ENC |
1265		     STA_KEY_FLG_INVALID;
1266
1267	if (!(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE))
1268		key_flags |= STA_KEY_MULTICAST_MSK;
1269
1270	sta_cmd.key.key_flags = key_flags;
1271	sta_cmd.key.key_offset = keyconf->hw_key_idx;
1272	sta_cmd.sta.modify_mask = STA_MODIFY_KEY_MASK;
1273	sta_cmd.mode = STA_CONTROL_MODIFY_MSK;
1274
1275	return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
1276}
1277
1278int iwl_set_dynamic_key(struct iwl_priv *priv,
1279			struct iwl_rxon_context *ctx,
1280			struct ieee80211_key_conf *keyconf,
1281			struct ieee80211_sta *sta)
1282{
1283	struct ieee80211_key_seq seq;
1284	u16 p1k[5];
1285	int ret;
1286	u8 sta_id = iwlagn_key_sta_id(priv, ctx->vif, sta);
1287	const u8 *addr;
1288
1289	if (sta_id == IWL_INVALID_STATION)
1290		return -EINVAL;
1291
1292	lockdep_assert_held(&priv->mutex);
1293
1294	keyconf->hw_key_idx = iwl_get_free_ucode_key_offset(priv);
1295	if (keyconf->hw_key_idx == WEP_INVALID_OFFSET)
1296		return -ENOSPC;
1297
1298	ctx->key_mapping_keys++;
1299
1300	switch (keyconf->cipher) {
1301	case WLAN_CIPHER_SUITE_TKIP:
1302		if (sta)
1303			addr = sta->addr;
1304		else /* station mode case only */
1305			addr = ctx->active.bssid_addr;
1306
1307		/* pre-fill phase 1 key into device cache */
1308		ieee80211_get_key_rx_seq(keyconf, 0, &seq);
1309		ieee80211_get_tkip_rx_p1k(keyconf, addr, seq.tkip.iv32, p1k);
1310		ret = iwlagn_send_sta_key(priv, keyconf, sta_id,
1311					  seq.tkip.iv32, p1k, CMD_SYNC);
1312		break;
1313	case WLAN_CIPHER_SUITE_CCMP:
1314	case WLAN_CIPHER_SUITE_WEP40:
1315	case WLAN_CIPHER_SUITE_WEP104:
1316		ret = iwlagn_send_sta_key(priv, keyconf, sta_id,
1317					  0, NULL, CMD_SYNC);
1318		break;
1319	default:
1320		IWL_ERR(priv, "Unknown cipher %x\n", keyconf->cipher);
1321		ret = -EINVAL;
1322	}
1323
1324	if (ret) {
1325		ctx->key_mapping_keys--;
1326		clear_bit(keyconf->hw_key_idx, &priv->ucode_key_table);
1327	}
1328
1329	IWL_DEBUG_WEP(priv, "Set dynamic key: cipher=%x len=%d idx=%d sta=%pM ret=%d\n",
1330		      keyconf->cipher, keyconf->keylen, keyconf->keyidx,
1331		      sta ? sta->addr : NULL, ret);
1332
1333	return ret;
1334}
1335
1336/**
1337 * iwlagn_alloc_bcast_station - add broadcast station into driver's station table.
1338 *
1339 * This adds the broadcast station into the driver's station table
1340 * and marks it driver active, so that it will be restored to the
1341 * device at the next best time.
1342 */
1343int iwlagn_alloc_bcast_station(struct iwl_priv *priv,
1344			       struct iwl_rxon_context *ctx)
1345{
1346	struct iwl_link_quality_cmd *link_cmd;
1347	u8 sta_id;
1348
1349	spin_lock_bh(&priv->sta_lock);
1350	sta_id = iwl_prep_station(priv, ctx, iwl_bcast_addr, false, NULL);
1351	if (sta_id == IWL_INVALID_STATION) {
1352		IWL_ERR(priv, "Unable to prepare broadcast station\n");
1353		spin_unlock_bh(&priv->sta_lock);
1354
1355		return -EINVAL;
1356	}
1357
1358	priv->stations[sta_id].used |= IWL_STA_DRIVER_ACTIVE;
1359	priv->stations[sta_id].used |= IWL_STA_BCAST;
1360	spin_unlock_bh(&priv->sta_lock);
1361
1362	link_cmd = iwl_sta_alloc_lq(priv, ctx, sta_id);
1363	if (!link_cmd) {
1364		IWL_ERR(priv,
1365			"Unable to initialize rate scaling for bcast station.\n");
1366		return -ENOMEM;
1367	}
1368
1369	spin_lock_bh(&priv->sta_lock);
1370	priv->stations[sta_id].lq = link_cmd;
1371	spin_unlock_bh(&priv->sta_lock);
1372
1373	return 0;
1374}
1375
1376/**
1377 * iwl_update_bcast_station - update broadcast station's LQ command
1378 *
1379 * Only used by iwlagn. Placed here to have all bcast station management
1380 * code together.
1381 */
1382int iwl_update_bcast_station(struct iwl_priv *priv,
1383			     struct iwl_rxon_context *ctx)
1384{
1385	struct iwl_link_quality_cmd *link_cmd;
1386	u8 sta_id = ctx->bcast_sta_id;
1387
1388	link_cmd = iwl_sta_alloc_lq(priv, ctx, sta_id);
1389	if (!link_cmd) {
1390		IWL_ERR(priv, "Unable to initialize rate scaling for bcast station.\n");
1391		return -ENOMEM;
1392	}
1393
1394	spin_lock_bh(&priv->sta_lock);
1395	if (priv->stations[sta_id].lq)
1396		kfree(priv->stations[sta_id].lq);
1397	else
1398		IWL_DEBUG_INFO(priv, "Bcast station rate scaling has not been initialized yet.\n");
1399	priv->stations[sta_id].lq = link_cmd;
1400	spin_unlock_bh(&priv->sta_lock);
1401
1402	return 0;
1403}
1404
1405int iwl_update_bcast_stations(struct iwl_priv *priv)
1406{
1407	struct iwl_rxon_context *ctx;
1408	int ret = 0;
1409
1410	for_each_context(priv, ctx) {
1411		ret = iwl_update_bcast_station(priv, ctx);
1412		if (ret)
1413			break;
1414	}
1415
1416	return ret;
1417}
1418
1419/**
1420 * iwl_sta_tx_modify_enable_tid - Enable Tx for this TID in station table
1421 */
1422int iwl_sta_tx_modify_enable_tid(struct iwl_priv *priv, int sta_id, int tid)
1423{
1424	struct iwl_addsta_cmd sta_cmd;
1425
1426	lockdep_assert_held(&priv->mutex);
1427
1428	/* Remove "disable" flag, to enable Tx for this TID */
1429	spin_lock_bh(&priv->sta_lock);
1430	priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_TID_DISABLE_TX;
1431	priv->stations[sta_id].sta.tid_disable_tx &= cpu_to_le16(~(1 << tid));
1432	priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1433	memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
1434	spin_unlock_bh(&priv->sta_lock);
1435
1436	return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
1437}
1438
1439int iwl_sta_rx_agg_start(struct iwl_priv *priv, struct ieee80211_sta *sta,
1440			 int tid, u16 ssn)
1441{
1442	int sta_id;
1443	struct iwl_addsta_cmd sta_cmd;
1444
1445	lockdep_assert_held(&priv->mutex);
1446
1447	sta_id = iwl_sta_id(sta);
1448	if (sta_id == IWL_INVALID_STATION)
1449		return -ENXIO;
1450
1451	spin_lock_bh(&priv->sta_lock);
1452	priv->stations[sta_id].sta.station_flags_msk = 0;
1453	priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_ADDBA_TID_MSK;
1454	priv->stations[sta_id].sta.add_immediate_ba_tid = (u8)tid;
1455	priv->stations[sta_id].sta.add_immediate_ba_ssn = cpu_to_le16(ssn);
1456	priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1457	memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
1458	spin_unlock_bh(&priv->sta_lock);
1459
1460	return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
1461}
1462
1463int iwl_sta_rx_agg_stop(struct iwl_priv *priv, struct ieee80211_sta *sta,
1464			int tid)
1465{
1466	int sta_id;
1467	struct iwl_addsta_cmd sta_cmd;
1468
1469	lockdep_assert_held(&priv->mutex);
1470
1471	sta_id = iwl_sta_id(sta);
1472	if (sta_id == IWL_INVALID_STATION) {
1473		IWL_ERR(priv, "Invalid station for AGG tid %d\n", tid);
1474		return -ENXIO;
1475	}
1476
1477	spin_lock_bh(&priv->sta_lock);
1478	priv->stations[sta_id].sta.station_flags_msk = 0;
1479	priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_DELBA_TID_MSK;
1480	priv->stations[sta_id].sta.remove_immediate_ba_tid = (u8)tid;
1481	priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1482	memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
1483	spin_unlock_bh(&priv->sta_lock);
1484
1485	return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
1486}
1487
1488
1489
1490void iwl_sta_modify_sleep_tx_count(struct iwl_priv *priv, int sta_id, int cnt)
1491{
1492	struct iwl_addsta_cmd cmd = {
1493		.mode = STA_CONTROL_MODIFY_MSK,
1494		.station_flags = STA_FLG_PWR_SAVE_MSK,
1495		.station_flags_msk = STA_FLG_PWR_SAVE_MSK,
1496		.sta.sta_id = sta_id,
1497		.sta.modify_mask = STA_MODIFY_SLEEP_TX_COUNT_MSK,
1498		.sleep_tx_count = cpu_to_le16(cnt),
1499	};
1500
1501	iwl_send_add_sta(priv, &cmd, CMD_ASYNC);
1502}