]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/net/wireless/iwlwifi/iwl-sta.c
iwlwifi: track station IDs
[net-next-2.6.git] / drivers / net / wireless / iwlwifi / iwl-sta.c
CommitLineData
6974e363
EG
1/******************************************************************************
2 *
1f447808 3 * Copyright(c) 2003 - 2010 Intel Corporation. All rights reserved.
6974e363
EG
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:
759ef89f 25 * Intel Linux Wireless <ilw@linux.intel.com>
6974e363
EG
26 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
27 *
28 *****************************************************************************/
29
30#include <net/mac80211.h>
947b13a7 31#include <linux/etherdevice.h>
fe6b23dd 32#include <linux/sched.h>
6974e363 33
3e0d4cb1 34#include "iwl-dev.h"
6974e363
EG
35#include "iwl-core.h"
36#include "iwl-sta.h"
7a999bf0 37
947b13a7
TW
38u8 iwl_find_station(struct iwl_priv *priv, const u8 *addr)
39{
40 int i;
41 int start = 0;
42 int ret = IWL_INVALID_STATION;
43 unsigned long flags;
947b13a7 44
05c914fe
JB
45 if ((priv->iw_mode == NL80211_IFTYPE_ADHOC) ||
46 (priv->iw_mode == NL80211_IFTYPE_AP))
947b13a7
TW
47 start = IWL_STA_ID;
48
49 if (is_broadcast_ether_addr(addr))
50 return priv->hw_params.bcast_sta_id;
51
52 spin_lock_irqsave(&priv->sta_lock, flags);
53 for (i = start; i < priv->hw_params.max_stations; i++)
54 if (priv->stations[i].used &&
55 (!compare_ether_addr(priv->stations[i].sta.sta.addr,
56 addr))) {
57 ret = i;
58 goto out;
59 }
60
e1623446 61 IWL_DEBUG_ASSOC_LIMIT(priv, "can not find STA %pM total %d\n",
e174961c 62 addr, priv->num_stations);
947b13a7
TW
63
64 out:
fe6b23dd
RC
65 /*
66 * It may be possible that more commands interacting with stations
67 * arrive before we completed processing the adding of
68 * station
69 */
70 if (ret != IWL_INVALID_STATION &&
71 (!(priv->stations[ret].used & IWL_STA_UCODE_ACTIVE) ||
72 ((priv->stations[ret].used & IWL_STA_UCODE_ACTIVE) &&
73 (priv->stations[ret].used & IWL_STA_UCODE_INPROGRESS)))) {
91dd6c27 74 IWL_ERR(priv, "Requested station info for sta %d before ready.\n",
fe6b23dd
RC
75 ret);
76 ret = IWL_INVALID_STATION;
77 }
947b13a7
TW
78 spin_unlock_irqrestore(&priv->sta_lock, flags);
79 return ret;
80}
81EXPORT_SYMBOL(iwl_find_station);
82
1fa97aae 83/* priv->sta_lock must be held */
24e5c401
EG
84static void iwl_sta_ucode_activate(struct iwl_priv *priv, u8 sta_id)
85{
24e5c401 86
c587de0b 87 if (!(priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE))
1fa97aae
RC
88 IWL_ERR(priv, "ACTIVATE a non DRIVER active station id %u addr %pM\n",
89 sta_id, priv->stations[sta_id].sta.sta.addr);
24e5c401 90
1fa97aae
RC
91 if (priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE) {
92 IWL_DEBUG_ASSOC(priv,
93 "STA id %u addr %pM already present in uCode (according to driver)\n",
94 sta_id, priv->stations[sta_id].sta.sta.addr);
95 } else {
96 priv->stations[sta_id].used |= IWL_STA_UCODE_ACTIVE;
97 IWL_DEBUG_ASSOC(priv, "Added STA id %u addr %pM to uCode\n",
98 sta_id, priv->stations[sta_id].sta.sta.addr);
99 }
24e5c401
EG
100}
101
1fa97aae
RC
102static void iwl_process_add_sta_resp(struct iwl_priv *priv,
103 struct iwl_addsta_cmd *addsta,
104 struct iwl_rx_packet *pkt,
105 bool sync)
42132bce 106{
3257e5d4 107 u8 sta_id = addsta->sta.sta_id;
1fa97aae 108 unsigned long flags;
42132bce 109
2f301227 110 if (pkt->hdr.flags & IWL_CMD_FAILED_MSK) {
15b1687c 111 IWL_ERR(priv, "Bad return from REPLY_ADD_STA (0x%08X)\n",
1fa97aae 112 pkt->hdr.flags);
5696aea6 113 return;
42132bce
TW
114 }
115
1fa97aae
RC
116 IWL_DEBUG_INFO(priv, "Processing response for adding station %u\n",
117 sta_id);
118
119 spin_lock_irqsave(&priv->sta_lock, flags);
120
2f301227 121 switch (pkt->u.add_sta.status) {
42132bce 122 case ADD_STA_SUCCESS_MSK:
1fa97aae 123 IWL_DEBUG_INFO(priv, "REPLY_ADD_STA PASSED\n");
24e5c401 124 iwl_sta_ucode_activate(priv, sta_id);
1fa97aae
RC
125 break;
126 case ADD_STA_NO_ROOM_IN_TABLE:
127 IWL_ERR(priv, "Adding station %d failed, no room in table.\n",
128 sta_id);
129 break;
130 case ADD_STA_NO_BLOCK_ACK_RESOURCE:
131 IWL_ERR(priv, "Adding station %d failed, no block ack resource.\n",
132 sta_id);
133 break;
134 case ADD_STA_MODIFY_NON_EXIST_STA:
91dd6c27 135 IWL_ERR(priv, "Attempting to modify non-existing station %d\n",
1fa97aae
RC
136 sta_id);
137 break;
42132bce 138 default:
1fa97aae
RC
139 IWL_DEBUG_ASSOC(priv, "Received REPLY_ADD_STA:(0x%08X)\n",
140 pkt->u.add_sta.status);
42132bce
TW
141 break;
142 }
1fa97aae
RC
143
144 IWL_DEBUG_INFO(priv, "%s station id %u addr %pM\n",
145 priv->stations[sta_id].sta.mode ==
146 STA_CONTROL_MODIFY_MSK ? "Modified" : "Added",
147 sta_id, priv->stations[sta_id].sta.sta.addr);
148
149 /*
150 * XXX: The MAC address in the command buffer is often changed from
151 * the original sent to the device. That is, the MAC address
152 * written to the command buffer often is not the same MAC adress
153 * read from the command buffer when the command returns. This
154 * issue has not yet been resolved and this debugging is left to
155 * observe the problem.
156 */
157 IWL_DEBUG_INFO(priv, "%s station according to cmd buffer %pM\n",
158 priv->stations[sta_id].sta.mode ==
159 STA_CONTROL_MODIFY_MSK ? "Modified" : "Added",
160 addsta->sta.addr);
1fa97aae
RC
161 spin_unlock_irqrestore(&priv->sta_lock, flags);
162}
163
164static void iwl_add_sta_callback(struct iwl_priv *priv,
165 struct iwl_device_cmd *cmd,
166 struct iwl_rx_packet *pkt)
167{
168 struct iwl_addsta_cmd *addsta =
169 (struct iwl_addsta_cmd *)cmd->cmd.payload;
170
171 iwl_process_add_sta_resp(priv, addsta, pkt, false);
172
42132bce
TW
173}
174
17f841cd 175int iwl_send_add_sta(struct iwl_priv *priv,
133636de
TW
176 struct iwl_addsta_cmd *sta, u8 flags)
177{
2f301227 178 struct iwl_rx_packet *pkt = NULL;
133636de
TW
179 int ret = 0;
180 u8 data[sizeof(*sta)];
181 struct iwl_host_cmd cmd = {
182 .id = REPLY_ADD_STA,
c2acea8e 183 .flags = flags,
133636de
TW
184 .data = data,
185 };
f875f518 186 u8 sta_id __maybe_unused = sta->sta.sta_id;
fe6b23dd
RC
187
188 IWL_DEBUG_INFO(priv, "Adding sta %u (%pM) %ssynchronously\n",
189 sta_id, sta->sta.addr, flags & CMD_ASYNC ? "a" : "");
133636de 190
42132bce 191 if (flags & CMD_ASYNC)
c2acea8e 192 cmd.callback = iwl_add_sta_callback;
42132bce 193 else
c2acea8e 194 cmd.flags |= CMD_WANT_SKB;
133636de
TW
195
196 cmd.len = priv->cfg->ops->utils->build_addsta_hcmd(sta, data);
197 ret = iwl_send_cmd(priv, &cmd);
198
199 if (ret || (flags & CMD_ASYNC))
200 return ret;
201
133636de 202 if (ret == 0) {
1fa97aae
RC
203 pkt = (struct iwl_rx_packet *)cmd.reply_page;
204 iwl_process_add_sta_resp(priv, sta, pkt, true);
133636de 205 }
64a76b50 206 iwl_free_pages(priv, cmd.reply_page);
133636de
TW
207
208 return ret;
209}
17f841cd 210EXPORT_SYMBOL(iwl_send_add_sta);
947b13a7 211
4f40e4d9 212static void iwl_set_ht_add_station(struct iwl_priv *priv, u8 index,
d9fe60de 213 struct ieee80211_sta_ht_cap *sta_ht_inf)
4f40e4d9
TW
214{
215 __le32 sta_flags;
216 u8 mimo_ps_mode;
217
218 if (!sta_ht_inf || !sta_ht_inf->ht_supported)
219 goto done;
220
00c5ae2f 221 mimo_ps_mode = (sta_ht_inf->cap & IEEE80211_HT_CAP_SM_PS) >> 2;
3f3e0376
WYG
222 IWL_DEBUG_ASSOC(priv, "spatial multiplexing power save mode: %s\n",
223 (mimo_ps_mode == WLAN_HT_CAP_SM_PS_STATIC) ?
224 "static" :
225 (mimo_ps_mode == WLAN_HT_CAP_SM_PS_DYNAMIC) ?
226 "dynamic" : "disabled");
4f40e4d9
TW
227
228 sta_flags = priv->stations[index].sta.station_flags;
229
230 sta_flags &= ~(STA_FLG_RTS_MIMO_PROT_MSK | STA_FLG_MIMO_DIS_MSK);
231
232 switch (mimo_ps_mode) {
00c5ae2f 233 case WLAN_HT_CAP_SM_PS_STATIC:
4f40e4d9
TW
234 sta_flags |= STA_FLG_MIMO_DIS_MSK;
235 break;
00c5ae2f 236 case WLAN_HT_CAP_SM_PS_DYNAMIC:
4f40e4d9
TW
237 sta_flags |= STA_FLG_RTS_MIMO_PROT_MSK;
238 break;
00c5ae2f 239 case WLAN_HT_CAP_SM_PS_DISABLED:
4f40e4d9
TW
240 break;
241 default:
39aadf8c 242 IWL_WARN(priv, "Invalid MIMO PS mode %d\n", mimo_ps_mode);
4f40e4d9
TW
243 break;
244 }
245
246 sta_flags |= cpu_to_le32(
247 (u32)sta_ht_inf->ampdu_factor << STA_FLG_MAX_AGG_SIZE_POS);
248
249 sta_flags |= cpu_to_le32(
250 (u32)sta_ht_inf->ampdu_density << STA_FLG_AGG_MPDU_DENSITY_POS);
251
7aafef1c
WYG
252 if (iwl_is_ht40_tx_allowed(priv, sta_ht_inf))
253 sta_flags |= STA_FLG_HT40_EN_MSK;
4f40e4d9 254 else
7aafef1c 255 sta_flags &= ~STA_FLG_HT40_EN_MSK;
4f40e4d9
TW
256
257 priv->stations[index].sta.station_flags = sta_flags;
258 done:
259 return;
260}
4f40e4d9
TW
261
262/**
fe6b23dd
RC
263 * iwl_prep_station - Prepare station information for addition
264 *
265 * should be called with sta_lock held
4f40e4d9 266 */
fe6b23dd
RC
267static u8 iwl_prep_station(struct iwl_priv *priv, const u8 *addr,
268 bool is_ap,
269 struct ieee80211_sta_ht_cap *ht_info)
4f40e4d9 270{
4f40e4d9 271 struct iwl_station_entry *station;
c587de0b 272 int i;
fe6b23dd 273 u8 sta_id = IWL_INVALID_STATION;
c587de0b 274 u16 rate;
4f40e4d9 275
4f40e4d9 276 if (is_ap)
24e5c401 277 sta_id = IWL_AP_ID;
4f40e4d9 278 else if (is_broadcast_ether_addr(addr))
24e5c401 279 sta_id = priv->hw_params.bcast_sta_id;
4f40e4d9
TW
280 else
281 for (i = IWL_STA_ID; i < priv->hw_params.max_stations; i++) {
282 if (!compare_ether_addr(priv->stations[i].sta.sta.addr,
283 addr)) {
24e5c401 284 sta_id = i;
4f40e4d9
TW
285 break;
286 }
287
288 if (!priv->stations[i].used &&
24e5c401
EG
289 sta_id == IWL_INVALID_STATION)
290 sta_id = i;
4f40e4d9
TW
291 }
292
fe6b23dd
RC
293 /*
294 * These two conditions have the same outcome, but keep them
295 * separate
296 */
297 if (unlikely(sta_id == IWL_INVALID_STATION))
298 return sta_id;
299
300 /*
301 * uCode is not able to deal with multiple requests to add a
302 * station. Keep track if one is in progress so that we do not send
303 * another.
304 */
305 if (priv->stations[sta_id].used & IWL_STA_UCODE_INPROGRESS) {
306 IWL_DEBUG_INFO(priv, "STA %d already in process of being added.\n",
307 sta_id);
24e5c401 308 return sta_id;
4f40e4d9
TW
309 }
310
fe6b23dd
RC
311 if ((priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE) &&
312 (priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE) &&
24e5c401 313 !compare_ether_addr(priv->stations[sta_id].sta.sta.addr, addr)) {
fe6b23dd
RC
314 IWL_DEBUG_ASSOC(priv, "STA %d (%pM) already added, not adding again.\n",
315 sta_id, addr);
24e5c401 316 return sta_id;
4f40e4d9
TW
317 }
318
24e5c401
EG
319 station = &priv->stations[sta_id];
320 station->used = IWL_STA_DRIVER_ACTIVE;
e1623446 321 IWL_DEBUG_ASSOC(priv, "Add STA to driver ID %d: %pM\n",
e174961c 322 sta_id, addr);
4f40e4d9
TW
323 priv->num_stations++;
324
325 /* Set up the REPLY_ADD_STA command to send to device */
326 memset(&station->sta, 0, sizeof(struct iwl_addsta_cmd));
327 memcpy(station->sta.sta.addr, addr, ETH_ALEN);
328 station->sta.mode = 0;
24e5c401 329 station->sta.sta.sta_id = sta_id;
4f40e4d9
TW
330 station->sta.station_flags = 0;
331
332 /* BCAST station and IBSS stations do not work in HT mode */
24e5c401 333 if (sta_id != priv->hw_params.bcast_sta_id &&
05c914fe 334 priv->iw_mode != NL80211_IFTYPE_ADHOC)
24e5c401 335 iwl_set_ht_add_station(priv, sta_id, ht_info);
4f40e4d9 336
c587de0b
TW
337 /* 3945 only */
338 rate = (priv->band == IEEE80211_BAND_5GHZ) ?
339 IWL_RATE_6M_PLCP : IWL_RATE_1M_PLCP;
340 /* Turn on both antennas for the station... */
341 station->sta.rate_n_flags = cpu_to_le16(rate | RATE_MCS_ANT_AB_MSK);
342
fe6b23dd
RC
343 return sta_id;
344
345}
346
347#define STA_WAIT_TIMEOUT (HZ/2)
348
349/**
350 * iwl_add_station_common -
351 */
352int iwl_add_station_common(struct iwl_priv *priv, const u8 *addr,
353 bool is_ap,
354 struct ieee80211_sta_ht_cap *ht_info,
355 u8 *sta_id_r)
356{
357 struct iwl_station_entry *station;
358 unsigned long flags_spin;
359 int ret = 0;
360 u8 sta_id;
361
362 *sta_id_r = 0;
363 spin_lock_irqsave(&priv->sta_lock, flags_spin);
364 sta_id = iwl_prep_station(priv, addr, is_ap, ht_info);
365 if (sta_id == IWL_INVALID_STATION) {
366 IWL_ERR(priv, "Unable to prepare station %pM for addition\n",
367 addr);
368 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
369 return -EINVAL;
370 }
371
372 /*
373 * uCode is not able to deal with multiple requests to add a
374 * station. Keep track if one is in progress so that we do not send
375 * another.
376 */
377 if (priv->stations[sta_id].used & IWL_STA_UCODE_INPROGRESS) {
378 IWL_DEBUG_INFO(priv, "STA %d already in process of being added.\n",
379 sta_id);
380 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
381 return -EEXIST;
382 }
383
384 if ((priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE) &&
385 (priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE)) {
386 IWL_DEBUG_ASSOC(priv, "STA %d (%pM) already added, not adding again.\n",
387 sta_id, addr);
388 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
389 return -EEXIST;
390 }
391
392 priv->stations[sta_id].used |= IWL_STA_UCODE_INPROGRESS;
393 station = &priv->stations[sta_id];
4f40e4d9
TW
394 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
395
396 /* Add station to device's station table */
fe6b23dd
RC
397 ret = iwl_send_add_sta(priv, &station->sta, CMD_SYNC);
398 if (ret) {
399 IWL_ERR(priv, "Adding station %pM failed.\n", station->sta.sta.addr);
400 spin_lock_irqsave(&priv->sta_lock, flags_spin);
401 priv->stations[sta_id].used &= ~IWL_STA_DRIVER_ACTIVE;
402 priv->stations[sta_id].used &= ~IWL_STA_UCODE_INPROGRESS;
403 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
404 }
405 *sta_id_r = sta_id;
406 return ret;
4f40e4d9 407}
fe6b23dd 408EXPORT_SYMBOL(iwl_add_station_common);
4f40e4d9 409
a6a0345c
JB
410static struct iwl_link_quality_cmd *iwl_sta_alloc_lq(struct iwl_priv *priv,
411 u8 sta_id)
7a999bf0 412{
fe6b23dd 413 int i, r;
d2e210ae 414 struct iwl_link_quality_cmd *link_cmd;
fe6b23dd
RC
415 u32 rate_flags;
416
d2e210ae
RC
417 link_cmd = kzalloc(sizeof(struct iwl_link_quality_cmd), GFP_KERNEL);
418 if (!link_cmd) {
419 IWL_ERR(priv, "Unable to allocate memory for LQ cmd.\n");
420 return NULL;
421 }
fe6b23dd
RC
422 /* Set up the rate scaling to start at selected rate, fall back
423 * all the way down to 1M in IEEE order, and then spin on 1M */
156b70d1 424 if (priv->band == IEEE80211_BAND_5GHZ)
fe6b23dd
RC
425 r = IWL_RATE_6M_INDEX;
426 else
427 r = IWL_RATE_1M_INDEX;
24e5c401 428
fe6b23dd
RC
429 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
430 rate_flags = 0;
431 if (r >= IWL_FIRST_CCK_RATE && r <= IWL_LAST_CCK_RATE)
432 rate_flags |= RATE_MCS_CCK_MSK;
24e5c401 433
fe6b23dd
RC
434 rate_flags |= first_antenna(priv->hw_params.valid_tx_ant) <<
435 RATE_MCS_ANT_POS;
24e5c401 436
d2e210ae 437 link_cmd->rs_table[i].rate_n_flags =
fe6b23dd
RC
438 iwl_hw_set_rate_n_flags(iwl_rates[r].plcp, rate_flags);
439 r = iwl_get_prev_ieee_rate(r);
440 }
24e5c401 441
d2e210ae 442 link_cmd->general_params.single_stream_ant_msk =
fe6b23dd 443 first_antenna(priv->hw_params.valid_tx_ant);
3a23d695 444
d2e210ae 445 link_cmd->general_params.dual_stream_ant_msk =
3a23d695
WYG
446 priv->hw_params.valid_tx_ant &
447 ~first_antenna(priv->hw_params.valid_tx_ant);
d2e210ae
RC
448 if (!link_cmd->general_params.dual_stream_ant_msk) {
449 link_cmd->general_params.dual_stream_ant_msk = ANT_AB;
3a23d695 450 } else if (num_of_ant(priv->hw_params.valid_tx_ant) == 2) {
d2e210ae 451 link_cmd->general_params.dual_stream_ant_msk =
3a23d695
WYG
452 priv->hw_params.valid_tx_ant;
453 }
454
d2e210ae
RC
455 link_cmd->agg_params.agg_dis_start_th = LINK_QUAL_AGG_DISABLE_START_DEF;
456 link_cmd->agg_params.agg_time_limit =
fe6b23dd 457 cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
24e5c401 458
156b70d1 459 link_cmd->sta_id = sta_id;
24e5c401 460
d2e210ae 461 return link_cmd;
7a999bf0
TW
462}
463
fe6b23dd 464/*
fd1af15d 465 * iwl_add_local_station - Add stations not requested by mac80211
fe6b23dd
RC
466 *
467 * This will be either the broadcast station or the bssid station needed by
468 * ad-hoc.
469 *
470 * Function sleeps.
471 */
fd1af15d
JB
472int iwl_add_local_station(struct iwl_priv *priv, const u8 *addr, bool init_rs,
473 u8 *sta_id_r)
7a999bf0 474{
fe6b23dd
RC
475 int ret;
476 u8 sta_id;
d2e210ae
RC
477 struct iwl_link_quality_cmd *link_cmd;
478 unsigned long flags;
7a999bf0 479
fd1af15d
JB
480 if (*sta_id_r)
481 *sta_id_r = IWL_INVALID_STATION;
482
fe6b23dd
RC
483 ret = iwl_add_station_common(priv, addr, 0, NULL, &sta_id);
484 if (ret) {
485 IWL_ERR(priv, "Unable to add station %pM\n", addr);
486 return ret;
7a999bf0
TW
487 }
488
fd1af15d
JB
489 if (sta_id_r)
490 *sta_id_r = sta_id;
491
d2e210ae
RC
492 spin_lock_irqsave(&priv->sta_lock, flags);
493 priv->stations[sta_id].used |= IWL_STA_LOCAL;
494 spin_unlock_irqrestore(&priv->sta_lock, flags);
495
496 if (init_rs) {
fe6b23dd 497 /* Set up default rate scaling table in device's station table */
a6a0345c 498 link_cmd = iwl_sta_alloc_lq(priv, sta_id);
d2e210ae
RC
499 if (!link_cmd) {
500 IWL_ERR(priv, "Unable to initialize rate scaling for station %pM.\n",
501 addr);
502 return -ENOMEM;
503 }
a6a0345c
JB
504
505 ret = iwl_send_lq_cmd(priv, link_cmd, CMD_SYNC, true);
506 if (ret)
507 IWL_ERR(priv, "Link quality command failed (%d)\n", ret);
508
d2e210ae
RC
509 spin_lock_irqsave(&priv->sta_lock, flags);
510 priv->stations[sta_id].lq = link_cmd;
511 spin_unlock_irqrestore(&priv->sta_lock, flags);
512 }
513
fe6b23dd
RC
514 return 0;
515}
516EXPORT_SYMBOL(iwl_add_local_station);
517
518/**
519 * iwl_sta_ucode_deactivate - deactivate ucode status for a station
520 *
521 * priv->sta_lock must be held
522 */
523static void iwl_sta_ucode_deactivate(struct iwl_priv *priv, u8 sta_id)
524{
525 /* Ucode must be active and driver must be non active */
d2e210ae
RC
526 if ((priv->stations[sta_id].used &
527 (IWL_STA_UCODE_ACTIVE | IWL_STA_DRIVER_ACTIVE)) != IWL_STA_UCODE_ACTIVE)
fe6b23dd
RC
528 IWL_ERR(priv, "removed non active STA %u\n", sta_id);
529
530 priv->stations[sta_id].used &= ~IWL_STA_UCODE_ACTIVE;
531
532 memset(&priv->stations[sta_id], 0, sizeof(struct iwl_station_entry));
533 IWL_DEBUG_ASSOC(priv, "Removed STA %u\n", sta_id);
7a999bf0
TW
534}
535
fe6b23dd
RC
536static int iwl_send_remove_station(struct iwl_priv *priv,
537 struct iwl_station_entry *station)
7a999bf0 538{
2f301227 539 struct iwl_rx_packet *pkt;
7a999bf0
TW
540 int ret;
541
fe6b23dd 542 unsigned long flags_spin;
7a999bf0
TW
543 struct iwl_rem_sta_cmd rm_sta_cmd;
544
545 struct iwl_host_cmd cmd = {
546 .id = REPLY_REMOVE_STA,
547 .len = sizeof(struct iwl_rem_sta_cmd),
fe6b23dd 548 .flags = CMD_SYNC,
7a999bf0
TW
549 .data = &rm_sta_cmd,
550 };
551
552 memset(&rm_sta_cmd, 0, sizeof(rm_sta_cmd));
553 rm_sta_cmd.num_sta = 1;
fe6b23dd
RC
554 memcpy(&rm_sta_cmd.addr, &station->sta.sta.addr , ETH_ALEN);
555
556 cmd.flags |= CMD_WANT_SKB;
7a999bf0 557
7a999bf0
TW
558 ret = iwl_send_cmd(priv, &cmd);
559
fe6b23dd 560 if (ret)
7a999bf0
TW
561 return ret;
562
2f301227
ZY
563 pkt = (struct iwl_rx_packet *)cmd.reply_page;
564 if (pkt->hdr.flags & IWL_CMD_FAILED_MSK) {
15b1687c 565 IWL_ERR(priv, "Bad return from REPLY_REMOVE_STA (0x%08X)\n",
2f301227 566 pkt->hdr.flags);
7a999bf0
TW
567 ret = -EIO;
568 }
569
570 if (!ret) {
2f301227 571 switch (pkt->u.rem_sta.status) {
7a999bf0 572 case REM_STA_SUCCESS_MSK:
fe6b23dd
RC
573 spin_lock_irqsave(&priv->sta_lock, flags_spin);
574 iwl_sta_ucode_deactivate(priv, station->sta.sta.sta_id);
575 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
e1623446 576 IWL_DEBUG_ASSOC(priv, "REPLY_REMOVE_STA PASSED\n");
7a999bf0
TW
577 break;
578 default:
579 ret = -EIO;
15b1687c 580 IWL_ERR(priv, "REPLY_REMOVE_STA failed\n");
7a999bf0
TW
581 break;
582 }
583 }
64a76b50 584 iwl_free_pages(priv, cmd.reply_page);
7a999bf0
TW
585
586 return ret;
587}
be1f3ab6 588
7a999bf0
TW
589/**
590 * iwl_remove_station - Remove driver's knowledge of station.
7a999bf0 591 */
fd1af15d
JB
592int iwl_remove_station(struct iwl_priv *priv, const u8 sta_id,
593 const u8 *addr)
7a999bf0 594{
fe6b23dd 595 struct iwl_station_entry *station;
fd1af15d 596 unsigned long flags;
fe6b23dd
RC
597
598 if (!iwl_is_ready(priv)) {
599 IWL_DEBUG_INFO(priv,
91dd6c27 600 "Unable to remove station %pM, device not ready.\n",
c0222df8 601 addr);
fe6b23dd
RC
602 /*
603 * It is typical for stations to be removed when we are
604 * going down. Return success since device will be down
605 * soon anyway
606 */
607 return 0;
608 }
7a999bf0 609
fd1af15d
JB
610 IWL_DEBUG_ASSOC(priv, "Removing STA from driver:%d %pM\n",
611 sta_id, addr);
7a999bf0 612
fd1af15d
JB
613 if (WARN_ON(sta_id == IWL_INVALID_STATION))
614 return -EINVAL;
7a999bf0 615
fd1af15d 616 spin_lock_irqsave(&priv->sta_lock, flags);
24e5c401
EG
617
618 if (!(priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE)) {
fe6b23dd 619 IWL_DEBUG_INFO(priv, "Removing %pM but non DRIVER active\n",
c0222df8 620 addr);
fd1af15d 621 goto out_err;
24e5c401
EG
622 }
623
624 if (!(priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE)) {
fe6b23dd 625 IWL_DEBUG_INFO(priv, "Removing %pM but non UCODE active\n",
c0222df8 626 addr);
fd1af15d 627 goto out_err;
7a999bf0
TW
628 }
629
1fa61b2e
JB
630 if (priv->stations[sta_id].used & IWL_STA_LOCAL) {
631 kfree(priv->stations[sta_id].lq);
632 priv->stations[sta_id].lq = NULL;
633 }
24e5c401
EG
634
635 priv->stations[sta_id].used &= ~IWL_STA_DRIVER_ACTIVE;
636
637 priv->num_stations--;
638
7a999bf0 639 BUG_ON(priv->num_stations < 0);
24e5c401 640
fe6b23dd 641 station = &priv->stations[sta_id];
7a999bf0 642 spin_unlock_irqrestore(&priv->sta_lock, flags);
24e5c401 643
fd1af15d
JB
644 return iwl_send_remove_station(priv, station);
645out_err:
7a999bf0 646 spin_unlock_irqrestore(&priv->sta_lock, flags);
fd1af15d 647 return -EINVAL;
7a999bf0 648}
1fa61b2e 649EXPORT_SYMBOL_GPL(iwl_remove_station);
24e5c401 650
83dde8c9 651/**
2c810ccd
JB
652 * iwl_clear_ucode_stations - clear ucode station table bits
653 *
654 * This function clears all the bits in the driver indicating
655 * which stations are active in the ucode. Call when something
656 * other than explicit station management would cause this in
657 * the ucode, e.g. unassociated RXON.
83dde8c9 658 */
2c810ccd 659void iwl_clear_ucode_stations(struct iwl_priv *priv)
83dde8c9 660{
48676eb3 661 int i;
7e246191
RC
662 unsigned long flags_spin;
663 bool cleared = false;
664
2c810ccd 665 IWL_DEBUG_INFO(priv, "Clearing ucode stations in driver\n");
83dde8c9 666
7e246191 667 spin_lock_irqsave(&priv->sta_lock, flags_spin);
2c810ccd
JB
668 for (i = 0; i < priv->hw_params.max_stations; i++) {
669 if (priv->stations[i].used & IWL_STA_UCODE_ACTIVE) {
670 IWL_DEBUG_INFO(priv, "Clearing ucode active for station %d\n", i);
671 priv->stations[i].used &= ~IWL_STA_UCODE_ACTIVE;
672 cleared = true;
7e246191
RC
673 }
674 }
675 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
676
677 if (!cleared)
678 IWL_DEBUG_INFO(priv, "No active stations found to be cleared\n");
679}
680EXPORT_SYMBOL(iwl_clear_ucode_stations);
83dde8c9 681
7e246191
RC
682/**
683 * iwl_restore_stations() - Restore driver known stations to device
684 *
685 * All stations considered active by driver, but not present in ucode, is
686 * restored.
fe6b23dd
RC
687 *
688 * Function sleeps.
7e246191
RC
689 */
690void iwl_restore_stations(struct iwl_priv *priv)
691{
fe6b23dd 692 struct iwl_station_entry *station;
7e246191
RC
693 unsigned long flags_spin;
694 int i;
695 bool found = false;
fe6b23dd 696 int ret;
83dde8c9 697
7e246191
RC
698 if (!iwl_is_ready(priv)) {
699 IWL_DEBUG_INFO(priv, "Not ready yet, not restoring any stations.\n");
700 return;
701 }
83dde8c9 702
7e246191
RC
703 IWL_DEBUG_ASSOC(priv, "Restoring all known stations ... start.\n");
704 spin_lock_irqsave(&priv->sta_lock, flags_spin);
705 for (i = 0; i < priv->hw_params.max_stations; i++) {
706 if ((priv->stations[i].used & IWL_STA_DRIVER_ACTIVE) &&
707 !(priv->stations[i].used & IWL_STA_UCODE_ACTIVE)) {
708 IWL_DEBUG_ASSOC(priv, "Restoring sta %pM\n",
709 priv->stations[i].sta.sta.addr);
710 priv->stations[i].sta.mode = 0;
711 priv->stations[i].used |= IWL_STA_UCODE_INPROGRESS;
712 found = true;
713 }
714 }
5e46882e 715
7e246191
RC
716 for (i = 0; i < priv->hw_params.max_stations; i++) {
717 if ((priv->stations[i].used & IWL_STA_UCODE_INPROGRESS)) {
fe6b23dd
RC
718 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
719 station = &priv->stations[i];
720 ret = iwl_send_add_sta(priv, &priv->stations[i].sta, CMD_SYNC);
721 if (ret) {
722 IWL_ERR(priv, "Adding station %pM failed.\n",
723 station->sta.sta.addr);
724 spin_lock_irqsave(&priv->sta_lock, flags_spin);
725 priv->stations[i].used &= ~IWL_STA_DRIVER_ACTIVE;
726 priv->stations[i].used &= ~IWL_STA_UCODE_INPROGRESS;
727 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
728 }
729 /*
730 * Rate scaling has already been initialized, send
731 * current LQ command
732 */
733 if (station->lq)
734 iwl_send_lq_cmd(priv, station->lq, CMD_SYNC, true);
735 spin_lock_irqsave(&priv->sta_lock, flags_spin);
7e246191
RC
736 priv->stations[i].used &= ~IWL_STA_UCODE_INPROGRESS;
737 }
48676eb3
MA
738 }
739
7e246191
RC
740 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
741 if (!found)
742 IWL_DEBUG_INFO(priv, "Restoring all known stations .... no stations to be restored.\n");
743 else
fe6b23dd 744 IWL_DEBUG_INFO(priv, "Restoring all known stations .... complete.\n");
83dde8c9 745}
7e246191 746EXPORT_SYMBOL(iwl_restore_stations);
83dde8c9 747
6e21f15c 748int iwl_get_free_ucode_key_index(struct iwl_priv *priv)
80fb47a1
EG
749{
750 int i;
751
752 for (i = 0; i < STA_KEY_MAX_NUM; i++)
77bab602 753 if (!test_and_set_bit(i, &priv->ucode_key_table))
80fb47a1
EG
754 return i;
755
40a9a829 756 return WEP_INVALID_OFFSET;
80fb47a1 757}
6e21f15c 758EXPORT_SYMBOL(iwl_get_free_ucode_key_index);
6974e363 759
335348b1 760static int iwl_send_static_wepkey_cmd(struct iwl_priv *priv, u8 send_if_empty)
6974e363
EG
761{
762 int i, not_empty = 0;
763 u8 buff[sizeof(struct iwl_wep_cmd) +
764 sizeof(struct iwl_wep_key) * WEP_KEYS_MAX];
765 struct iwl_wep_cmd *wep_cmd = (struct iwl_wep_cmd *)buff;
766 size_t cmd_size = sizeof(struct iwl_wep_cmd);
767 struct iwl_host_cmd cmd = {
768 .id = REPLY_WEPKEY,
769 .data = wep_cmd,
72e15d71 770 .flags = CMD_SYNC,
6974e363
EG
771 };
772
72e15d71
JB
773 might_sleep();
774
6974e363
EG
775 memset(wep_cmd, 0, cmd_size +
776 (sizeof(struct iwl_wep_key) * WEP_KEYS_MAX));
777
778 for (i = 0; i < WEP_KEYS_MAX ; i++) {
779 wep_cmd->key[i].key_index = i;
780 if (priv->wep_keys[i].key_size) {
781 wep_cmd->key[i].key_offset = i;
782 not_empty = 1;
783 } else {
784 wep_cmd->key[i].key_offset = WEP_INVALID_OFFSET;
785 }
786
787 wep_cmd->key[i].key_size = priv->wep_keys[i].key_size;
788 memcpy(&wep_cmd->key[i].key[3], priv->wep_keys[i].key,
789 priv->wep_keys[i].key_size);
790 }
791
792 wep_cmd->global_key_type = WEP_KEY_WEP_TYPE;
793 wep_cmd->num_keys = WEP_KEYS_MAX;
794
795 cmd_size += sizeof(struct iwl_wep_key) * WEP_KEYS_MAX;
796
797 cmd.len = cmd_size;
798
799 if (not_empty || send_if_empty)
800 return iwl_send_cmd(priv, &cmd);
801 else
802 return 0;
803}
335348b1
JB
804
805int iwl_restore_default_wep_keys(struct iwl_priv *priv)
806{
807 WARN_ON(!mutex_is_locked(&priv->mutex));
808
809 return iwl_send_static_wepkey_cmd(priv, 0);
810}
811EXPORT_SYMBOL(iwl_restore_default_wep_keys);
6974e363
EG
812
813int iwl_remove_default_wep_key(struct iwl_priv *priv,
80fb47a1 814 struct ieee80211_key_conf *keyconf)
6974e363
EG
815{
816 int ret;
6974e363 817
72e15d71
JB
818 WARN_ON(!mutex_is_locked(&priv->mutex));
819
2d1bb9e5
RC
820 IWL_DEBUG_WEP(priv, "Removing default WEP key: idx=%d\n",
821 keyconf->keyidx);
80fb47a1 822
80fb47a1 823 memset(&priv->wep_keys[keyconf->keyidx], 0, sizeof(priv->wep_keys[0]));
2d1bb9e5
RC
824 if (iwl_is_rfkill(priv)) {
825 IWL_DEBUG_WEP(priv, "Not sending REPLY_WEPKEY command due to RFKILL.\n");
72e15d71 826 /* but keys in device are clear anyway so return success */
2d1bb9e5
RC
827 return 0;
828 }
6974e363 829 ret = iwl_send_static_wepkey_cmd(priv, 1);
e1623446 830 IWL_DEBUG_WEP(priv, "Remove default WEP key: idx=%d ret=%d\n",
4564ce8b 831 keyconf->keyidx, ret);
6974e363
EG
832
833 return ret;
834}
27aaba0c 835EXPORT_SYMBOL(iwl_remove_default_wep_key);
6974e363
EG
836
837int iwl_set_default_wep_key(struct iwl_priv *priv,
838 struct ieee80211_key_conf *keyconf)
839{
840 int ret;
72e15d71
JB
841
842 WARN_ON(!mutex_is_locked(&priv->mutex));
6974e363 843
4564ce8b
EG
844 if (keyconf->keylen != WEP_KEY_LEN_128 &&
845 keyconf->keylen != WEP_KEY_LEN_64) {
e1623446 846 IWL_DEBUG_WEP(priv, "Bad WEP key length %d\n", keyconf->keylen);
4564ce8b
EG
847 return -EINVAL;
848 }
849
6974e363 850 keyconf->flags &= ~IEEE80211_KEY_FLAG_GENERATE_IV;
ccc038ab 851 keyconf->hw_key_idx = HW_KEY_DEFAULT;
6974e363
EG
852 priv->stations[IWL_AP_ID].keyinfo.alg = ALG_WEP;
853
6974e363
EG
854 priv->wep_keys[keyconf->keyidx].key_size = keyconf->keylen;
855 memcpy(&priv->wep_keys[keyconf->keyidx].key, &keyconf->key,
856 keyconf->keylen);
857
858 ret = iwl_send_static_wepkey_cmd(priv, 0);
e1623446 859 IWL_DEBUG_WEP(priv, "Set default WEP key: len=%d idx=%d ret=%d\n",
4564ce8b 860 keyconf->keylen, keyconf->keyidx, ret);
6974e363
EG
861
862 return ret;
863}
27aaba0c 864EXPORT_SYMBOL(iwl_set_default_wep_key);
6974e363 865
7480513f 866static int iwl_set_wep_dynamic_key_info(struct iwl_priv *priv,
0211ddda
EG
867 struct ieee80211_key_conf *keyconf,
868 u8 sta_id)
869{
870 unsigned long flags;
871 __le16 key_flags = 0;
872 int ret;
873
874 keyconf->flags &= ~IEEE80211_KEY_FLAG_GENERATE_IV;
0211ddda
EG
875
876 key_flags |= (STA_KEY_FLG_WEP | STA_KEY_FLG_MAP_KEY_MSK);
877 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
878 key_flags &= ~STA_KEY_FLG_INVALID;
879
880 if (keyconf->keylen == WEP_KEY_LEN_128)
881 key_flags |= STA_KEY_FLG_KEY_SIZE_MSK;
882
5425e490 883 if (sta_id == priv->hw_params.bcast_sta_id)
0211ddda
EG
884 key_flags |= STA_KEY_MULTICAST_MSK;
885
886 spin_lock_irqsave(&priv->sta_lock, flags);
887
888 priv->stations[sta_id].keyinfo.alg = keyconf->alg;
889 priv->stations[sta_id].keyinfo.keylen = keyconf->keylen;
890 priv->stations[sta_id].keyinfo.keyidx = keyconf->keyidx;
891
892 memcpy(priv->stations[sta_id].keyinfo.key,
893 keyconf->key, keyconf->keylen);
894
895 memcpy(&priv->stations[sta_id].sta.key.key[3],
896 keyconf->key, keyconf->keylen);
897
3ec47732
EG
898 if ((priv->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
899 == STA_KEY_FLG_NO_ENC)
900 priv->stations[sta_id].sta.key.key_offset =
80fb47a1 901 iwl_get_free_ucode_key_index(priv);
3ec47732
EG
902 /* else, we are overriding an existing key => no need to allocated room
903 * in uCode. */
0211ddda 904
40a9a829 905 WARN(priv->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
e724b8fe 906 "no space for a new key");
40a9a829 907
3ec47732 908 priv->stations[sta_id].sta.key.key_flags = key_flags;
0211ddda
EG
909 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
910 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
911
133636de 912 ret = iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
0211ddda
EG
913
914 spin_unlock_irqrestore(&priv->sta_lock, flags);
915
916 return ret;
917}
7480513f
EG
918
919static int iwl_set_ccmp_dynamic_key_info(struct iwl_priv *priv,
920 struct ieee80211_key_conf *keyconf,
921 u8 sta_id)
922{
923 unsigned long flags;
924 __le16 key_flags = 0;
40a9a829 925 int ret;
7480513f
EG
926
927 key_flags |= (STA_KEY_FLG_CCMP | STA_KEY_FLG_MAP_KEY_MSK);
928 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
929 key_flags &= ~STA_KEY_FLG_INVALID;
930
5425e490 931 if (sta_id == priv->hw_params.bcast_sta_id)
7480513f
EG
932 key_flags |= STA_KEY_MULTICAST_MSK;
933
934 keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
7480513f
EG
935
936 spin_lock_irqsave(&priv->sta_lock, flags);
937 priv->stations[sta_id].keyinfo.alg = keyconf->alg;
938 priv->stations[sta_id].keyinfo.keylen = keyconf->keylen;
939
940 memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key,
941 keyconf->keylen);
942
943 memcpy(priv->stations[sta_id].sta.key.key, keyconf->key,
944 keyconf->keylen);
945
3ec47732
EG
946 if ((priv->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
947 == STA_KEY_FLG_NO_ENC)
948 priv->stations[sta_id].sta.key.key_offset =
949 iwl_get_free_ucode_key_index(priv);
950 /* else, we are overriding an existing key => no need to allocated room
951 * in uCode. */
952
40a9a829 953 WARN(priv->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
e724b8fe 954 "no space for a new key");
40a9a829 955
7480513f
EG
956 priv->stations[sta_id].sta.key.key_flags = key_flags;
957 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
958 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
959
40a9a829
TW
960 ret = iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
961
7480513f
EG
962 spin_unlock_irqrestore(&priv->sta_lock, flags);
963
40a9a829 964 return ret;
7480513f
EG
965}
966
967static int iwl_set_tkip_dynamic_key_info(struct iwl_priv *priv,
968 struct ieee80211_key_conf *keyconf,
969 u8 sta_id)
970{
971 unsigned long flags;
972 int ret = 0;
299f5462
RC
973 __le16 key_flags = 0;
974
975 key_flags |= (STA_KEY_FLG_TKIP | STA_KEY_FLG_MAP_KEY_MSK);
976 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
977 key_flags &= ~STA_KEY_FLG_INVALID;
978
979 if (sta_id == priv->hw_params.bcast_sta_id)
980 key_flags |= STA_KEY_MULTICAST_MSK;
7480513f
EG
981
982 keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
983 keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
7480513f
EG
984
985 spin_lock_irqsave(&priv->sta_lock, flags);
986
987 priv->stations[sta_id].keyinfo.alg = keyconf->alg;
7480513f 988 priv->stations[sta_id].keyinfo.keylen = 16;
3ec47732
EG
989
990 if ((priv->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
991 == STA_KEY_FLG_NO_ENC)
992 priv->stations[sta_id].sta.key.key_offset =
77bab602 993 iwl_get_free_ucode_key_index(priv);
3ec47732
EG
994 /* else, we are overriding an existing key => no need to allocated room
995 * in uCode. */
7480513f 996
40a9a829 997 WARN(priv->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
e724b8fe 998 "no space for a new key");
40a9a829 999
299f5462
RC
1000 priv->stations[sta_id].sta.key.key_flags = key_flags;
1001
1002
7480513f
EG
1003 /* This copy is acutally not needed: we get the key with each TX */
1004 memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key, 16);
1005
1006 memcpy(priv->stations[sta_id].sta.key.key, keyconf->key, 16);
1007
1008 spin_unlock_irqrestore(&priv->sta_lock, flags);
1009
1010 return ret;
1011}
1012
9f58671e
TW
1013void iwl_update_tkip_key(struct iwl_priv *priv,
1014 struct ieee80211_key_conf *keyconf,
1015 const u8 *addr, u32 iv32, u16 *phase1key)
1016{
1017 u8 sta_id = IWL_INVALID_STATION;
1018 unsigned long flags;
9f58671e 1019 int i;
9f58671e 1020
c587de0b 1021 sta_id = iwl_find_station(priv, addr);
9f58671e 1022 if (sta_id == IWL_INVALID_STATION) {
e1623446 1023 IWL_DEBUG_MAC80211(priv, "leave - %pM not in station map.\n",
9f58671e
TW
1024 addr);
1025 return;
1026 }
1027
1028 if (iwl_scan_cancel(priv)) {
1029 /* cancel scan failed, just live w/ bad key and rely
1030 briefly on SW decryption */
1031 return;
1032 }
1033
9f58671e
TW
1034 spin_lock_irqsave(&priv->sta_lock, flags);
1035
9f58671e
TW
1036 priv->stations[sta_id].sta.key.tkip_rx_tsc_byte2 = (u8) iv32;
1037
1038 for (i = 0; i < 5; i++)
1039 priv->stations[sta_id].sta.key.tkip_rx_ttak[i] =
1040 cpu_to_le16(phase1key[i]);
1041
1042 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
1043 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1044
1045 iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
1046
1047 spin_unlock_irqrestore(&priv->sta_lock, flags);
1048
1049}
1050EXPORT_SYMBOL(iwl_update_tkip_key);
1051
3ec47732
EG
1052int iwl_remove_dynamic_key(struct iwl_priv *priv,
1053 struct ieee80211_key_conf *keyconf,
1054 u8 sta_id)
7480513f
EG
1055{
1056 unsigned long flags;
3ec47732
EG
1057 int ret = 0;
1058 u16 key_flags;
1059 u8 keyidx;
7480513f 1060
ccc038ab 1061 priv->key_mapping_key--;
7480513f
EG
1062
1063 spin_lock_irqsave(&priv->sta_lock, flags);
3ec47732
EG
1064 key_flags = le16_to_cpu(priv->stations[sta_id].sta.key.key_flags);
1065 keyidx = (key_flags >> STA_KEY_FLG_KEYID_POS) & 0x3;
1066
e1623446 1067 IWL_DEBUG_WEP(priv, "Remove dynamic key: idx=%d sta=%d\n",
4564ce8b
EG
1068 keyconf->keyidx, sta_id);
1069
3ec47732
EG
1070 if (keyconf->keyidx != keyidx) {
1071 /* We need to remove a key with index different that the one
1072 * in the uCode. This means that the key we need to remove has
1073 * been replaced by another one with different index.
1074 * Don't do anything and return ok
1075 */
1076 spin_unlock_irqrestore(&priv->sta_lock, flags);
1077 return 0;
1078 }
1079
40a9a829 1080 if (priv->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET) {
39aadf8c 1081 IWL_WARN(priv, "Removing wrong key %d 0x%x\n",
40a9a829
TW
1082 keyconf->keyidx, key_flags);
1083 spin_unlock_irqrestore(&priv->sta_lock, flags);
1084 return 0;
1085 }
1086
7480513f
EG
1087 if (!test_and_clear_bit(priv->stations[sta_id].sta.key.key_offset,
1088 &priv->ucode_key_table))
15b1687c 1089 IWL_ERR(priv, "index %d not used in uCode key table.\n",
7480513f
EG
1090 priv->stations[sta_id].sta.key.key_offset);
1091 memset(&priv->stations[sta_id].keyinfo, 0,
6def9761 1092 sizeof(struct iwl_hw_key));
7480513f
EG
1093 memset(&priv->stations[sta_id].sta.key, 0,
1094 sizeof(struct iwl4965_keyinfo));
3ec47732
EG
1095 priv->stations[sta_id].sta.key.key_flags =
1096 STA_KEY_FLG_NO_ENC | STA_KEY_FLG_INVALID;
1097 priv->stations[sta_id].sta.key.key_offset = WEP_INVALID_OFFSET;
7480513f
EG
1098 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
1099 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
7480513f 1100
2d1bb9e5 1101 if (iwl_is_rfkill(priv)) {
91dd6c27 1102 IWL_DEBUG_WEP(priv, "Not sending REPLY_ADD_STA command because RFKILL enabled.\n");
2d1bb9e5
RC
1103 spin_unlock_irqrestore(&priv->sta_lock, flags);
1104 return 0;
1105 }
ccc038ab 1106 ret = iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
3ec47732
EG
1107 spin_unlock_irqrestore(&priv->sta_lock, flags);
1108 return ret;
7480513f 1109}
27aaba0c 1110EXPORT_SYMBOL(iwl_remove_dynamic_key);
7480513f
EG
1111
1112int iwl_set_dynamic_key(struct iwl_priv *priv,
ccc038ab 1113 struct ieee80211_key_conf *keyconf, u8 sta_id)
7480513f
EG
1114{
1115 int ret;
1116
ccc038ab
EG
1117 priv->key_mapping_key++;
1118 keyconf->hw_key_idx = HW_KEY_DYNAMIC;
7480513f 1119
ccc038ab 1120 switch (keyconf->alg) {
7480513f 1121 case ALG_CCMP:
ccc038ab 1122 ret = iwl_set_ccmp_dynamic_key_info(priv, keyconf, sta_id);
7480513f
EG
1123 break;
1124 case ALG_TKIP:
ccc038ab 1125 ret = iwl_set_tkip_dynamic_key_info(priv, keyconf, sta_id);
7480513f
EG
1126 break;
1127 case ALG_WEP:
ccc038ab 1128 ret = iwl_set_wep_dynamic_key_info(priv, keyconf, sta_id);
7480513f
EG
1129 break;
1130 default:
15b1687c
WT
1131 IWL_ERR(priv,
1132 "Unknown alg: %s alg = %d\n", __func__, keyconf->alg);
7480513f
EG
1133 ret = -EINVAL;
1134 }
1135
e1623446 1136 IWL_DEBUG_WEP(priv, "Set dynamic key: alg= %d len=%d idx=%d sta=%d ret=%d\n",
4564ce8b
EG
1137 keyconf->alg, keyconf->keylen, keyconf->keyidx,
1138 sta_id, ret);
1139
7480513f
EG
1140 return ret;
1141}
27aaba0c 1142EXPORT_SYMBOL(iwl_set_dynamic_key);
7480513f 1143
66c73db7
TW
1144#ifdef CONFIG_IWLWIFI_DEBUG
1145static void iwl_dump_lq_cmd(struct iwl_priv *priv,
1146 struct iwl_link_quality_cmd *lq)
1147{
1148 int i;
e1623446
TW
1149 IWL_DEBUG_RATE(priv, "lq station id 0x%x\n", lq->sta_id);
1150 IWL_DEBUG_RATE(priv, "lq ant 0x%X 0x%X\n",
66c73db7
TW
1151 lq->general_params.single_stream_ant_msk,
1152 lq->general_params.dual_stream_ant_msk);
1153
1154 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
e1623446 1155 IWL_DEBUG_RATE(priv, "lq index %d 0x%X\n",
66c73db7
TW
1156 i, lq->rs_table[i].rate_n_flags);
1157}
1158#else
1159static inline void iwl_dump_lq_cmd(struct iwl_priv *priv,
1160 struct iwl_link_quality_cmd *lq)
1161{
1162}
1163#endif
1164
3bce6066
RC
1165/**
1166 * is_lq_table_valid() - Test one aspect of LQ cmd for validity
1167 *
1168 * It sometimes happens when a HT rate has been in use and we
1169 * loose connectivity with AP then mac80211 will first tell us that the
1170 * current channel is not HT anymore before removing the station. In such a
1171 * scenario the RXON flags will be updated to indicate we are not
1172 * communicating HT anymore, but the LQ command may still contain HT rates.
1173 * Test for this to prevent driver from sending LQ command between the time
1174 * RXON flags are updated and when LQ command is updated.
1175 */
1176static bool is_lq_table_valid(struct iwl_priv *priv,
1177 struct iwl_link_quality_cmd *lq)
1178{
1179 int i;
1180 struct iwl_ht_config *ht_conf = &priv->current_ht_config;
1181
1182 if (ht_conf->is_ht)
1183 return true;
1184
1185 IWL_DEBUG_INFO(priv, "Channel %u is not an HT channel\n",
1186 priv->active_rxon.channel);
1187 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
1188 if (le32_to_cpu(lq->rs_table[i].rate_n_flags) & RATE_MCS_HT_MSK) {
1189 IWL_DEBUG_INFO(priv,
1190 "index %d of LQ expects HT channel\n",
1191 i);
1192 return false;
1193 }
1194 }
1195 return true;
1196}
1197
fe6b23dd
RC
1198/**
1199 * iwl_send_lq_cmd() - Send link quality command
1200 * @init: This command is sent as part of station initialization right
1201 * after station has been added.
1202 *
1203 * The link quality command is sent as the last step of station creation.
1204 * This is the special case in which init is set and we call a callback in
1205 * this case to clear the state indicating that station creation is in
1206 * progress.
1207 */
66c73db7 1208int iwl_send_lq_cmd(struct iwl_priv *priv,
fe6b23dd 1209 struct iwl_link_quality_cmd *lq, u8 flags, bool init)
66c73db7 1210{
fe6b23dd
RC
1211 int ret = 0;
1212 unsigned long flags_spin;
1213
66c73db7
TW
1214 struct iwl_host_cmd cmd = {
1215 .id = REPLY_TX_LINK_QUALITY_CMD,
1216 .len = sizeof(struct iwl_link_quality_cmd),
c2acea8e 1217 .flags = flags,
66c73db7
TW
1218 .data = lq,
1219 };
1220
76c3c698 1221 if (WARN_ON(lq->sta_id == IWL_INVALID_STATION))
66c73db7
TW
1222 return -EINVAL;
1223
3ac7f146 1224 iwl_dump_lq_cmd(priv, lq);
fe6b23dd 1225 BUG_ON(init && (cmd.flags & CMD_ASYNC));
66c73db7 1226
3bce6066
RC
1227 if (is_lq_table_valid(priv, lq))
1228 ret = iwl_send_cmd(priv, &cmd);
1229 else
1230 ret = -EINVAL;
d2e210ae
RC
1231
1232 if (cmd.flags & CMD_ASYNC)
fe6b23dd 1233 return ret;
66c73db7 1234
fe6b23dd 1235 if (init) {
91dd6c27 1236 IWL_DEBUG_INFO(priv, "init LQ command complete, clearing sta addition status for sta %d\n",
fe6b23dd
RC
1237 lq->sta_id);
1238 spin_lock_irqsave(&priv->sta_lock, flags_spin);
1239 priv->stations[lq->sta_id].used &= ~IWL_STA_UCODE_INPROGRESS;
1240 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
1241 }
d2e210ae 1242 return ret;
66c73db7
TW
1243}
1244EXPORT_SYMBOL(iwl_send_lq_cmd);
1245
9a9ca65f 1246/**
2c810ccd
JB
1247 * iwl_alloc_bcast_station - add broadcast station into driver's station table.
1248 *
1249 * This adds the broadcast station into the driver's station table
1250 * and marks it driver active, so that it will be restored to the
1251 * device at the next best time.
9a9ca65f 1252 */
2c810ccd 1253int iwl_alloc_bcast_station(struct iwl_priv *priv, bool init_lq)
9a9ca65f 1254{
2c810ccd
JB
1255 struct iwl_link_quality_cmd *link_cmd;
1256 unsigned long flags;
1257 u8 sta_id;
1258
1259 spin_lock_irqsave(&priv->sta_lock, flags);
1260 sta_id = iwl_prep_station(priv, iwl_bcast_addr, false, NULL);
1261 if (sta_id == IWL_INVALID_STATION) {
1262 IWL_ERR(priv, "Unable to prepare broadcast station\n");
1263 spin_unlock_irqrestore(&priv->sta_lock, flags);
1264
1265 return -EINVAL;
1266 }
1267
1268 priv->stations[sta_id].used |= IWL_STA_DRIVER_ACTIVE;
1269 priv->stations[sta_id].used |= IWL_STA_BCAST;
1270 spin_unlock_irqrestore(&priv->sta_lock, flags);
1271
1272 if (init_lq) {
1273 link_cmd = iwl_sta_alloc_lq(priv, sta_id);
1274 if (!link_cmd) {
1275 IWL_ERR(priv,
1276 "Unable to initialize rate scaling for bcast station.\n");
1277 return -ENOMEM;
1278 }
1279
1280 spin_lock_irqsave(&priv->sta_lock, flags);
1281 priv->stations[sta_id].lq = link_cmd;
1282 spin_unlock_irqrestore(&priv->sta_lock, flags);
1283 }
1284
1285 return 0;
9a9ca65f 1286}
2c810ccd 1287EXPORT_SYMBOL_GPL(iwl_alloc_bcast_station);
9a9ca65f 1288
2c810ccd 1289void iwl_dealloc_bcast_station(struct iwl_priv *priv)
3459ab5a 1290{
2c810ccd
JB
1291 unsigned long flags;
1292 int i;
fe6b23dd 1293
2c810ccd
JB
1294 spin_lock_irqsave(&priv->sta_lock, flags);
1295 for (i = 0; i < priv->hw_params.max_stations; i++) {
1296 if (!(priv->stations[i].used & IWL_STA_BCAST))
1297 continue;
1298
1299 priv->stations[i].used &= ~IWL_STA_UCODE_ACTIVE;
1300 priv->num_stations--;
1301 BUG_ON(priv->num_stations < 0);
1302 kfree(priv->stations[i].lq);
1303 priv->stations[i].lq = NULL;
1304 }
1305 spin_unlock_irqrestore(&priv->sta_lock, flags);
3459ab5a 1306}
2c810ccd 1307EXPORT_SYMBOL_GPL(iwl_dealloc_bcast_station);
3459ab5a 1308
4f40e4d9
TW
1309/**
1310 * iwl_get_sta_id - Find station's index within station table
1311 *
1312 * If new IBSS station, create new entry in station table
1313 */
1314int iwl_get_sta_id(struct iwl_priv *priv, struct ieee80211_hdr *hdr)
1315{
1316 int sta_id;
943ab70f 1317 __le16 fc = hdr->frame_control;
4f40e4d9
TW
1318
1319 /* If this frame is broadcast or management, use broadcast station id */
943ab70f 1320 if (!ieee80211_is_data(fc) || is_multicast_ether_addr(hdr->addr1))
4f40e4d9
TW
1321 return priv->hw_params.bcast_sta_id;
1322
1323 switch (priv->iw_mode) {
1324
1325 /* If we are a client station in a BSS network, use the special
1326 * AP station entry (that's the only station we communicate with) */
05c914fe 1327 case NL80211_IFTYPE_STATION:
fe6b23dd
RC
1328 /*
1329 * If addition of station not complete yet, which means
1330 * that rate scaling has not been initialized, then return
1331 * the broadcast station.
1332 */
1333 if (!(priv->stations[IWL_AP_ID].used & IWL_STA_UCODE_ACTIVE))
1334 return priv->hw_params.bcast_sta_id;
4f40e4d9
TW
1335 return IWL_AP_ID;
1336
1337 /* If we are an AP, then find the station, or use BCAST */
05c914fe 1338 case NL80211_IFTYPE_AP:
c587de0b 1339 sta_id = iwl_find_station(priv, hdr->addr1);
4f40e4d9
TW
1340 if (sta_id != IWL_INVALID_STATION)
1341 return sta_id;
1342 return priv->hw_params.bcast_sta_id;
1343
1344 /* If this frame is going out to an IBSS network, find the station,
1345 * or create a new station table entry */
05c914fe 1346 case NL80211_IFTYPE_ADHOC:
c587de0b 1347 sta_id = iwl_find_station(priv, hdr->addr1);
4f40e4d9
TW
1348 if (sta_id != IWL_INVALID_STATION)
1349 return sta_id;
1350
e1623446 1351 IWL_DEBUG_DROP(priv, "Station %pM not in station map. "
4f40e4d9 1352 "Defaulting to broadcast...\n",
e174961c 1353 hdr->addr1);
3d816c77 1354 iwl_print_hex_dump(priv, IWL_DL_DROP, (u8 *) hdr, sizeof(*hdr));
4f40e4d9
TW
1355 return priv->hw_params.bcast_sta_id;
1356
1357 default:
39aadf8c
WT
1358 IWL_WARN(priv, "Unknown mode of operation: %d\n",
1359 priv->iw_mode);
4f40e4d9
TW
1360 return priv->hw_params.bcast_sta_id;
1361 }
1362}
1363EXPORT_SYMBOL(iwl_get_sta_id);
1364
5083e563 1365/**
9f58671e 1366 * iwl_sta_tx_modify_enable_tid - Enable Tx for this TID in station table
5083e563 1367 */
9f58671e 1368void iwl_sta_tx_modify_enable_tid(struct iwl_priv *priv, int sta_id, int tid)
5083e563
TW
1369{
1370 unsigned long flags;
1371
1372 /* Remove "disable" flag, to enable Tx for this TID */
1373 spin_lock_irqsave(&priv->sta_lock, flags);
1374 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_TID_DISABLE_TX;
1375 priv->stations[sta_id].sta.tid_disable_tx &= cpu_to_le16(~(1 << tid));
1376 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1377 spin_unlock_irqrestore(&priv->sta_lock, flags);
1378
1379 iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
1380}
9f58671e
TW
1381EXPORT_SYMBOL(iwl_sta_tx_modify_enable_tid);
1382
1383int iwl_sta_rx_agg_start(struct iwl_priv *priv,
1384 const u8 *addr, int tid, u16 ssn)
1385{
1386 unsigned long flags;
1387 int sta_id;
1388
c587de0b 1389 sta_id = iwl_find_station(priv, addr);
9f58671e
TW
1390 if (sta_id == IWL_INVALID_STATION)
1391 return -ENXIO;
1392
1393 spin_lock_irqsave(&priv->sta_lock, flags);
1394 priv->stations[sta_id].sta.station_flags_msk = 0;
1395 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_ADDBA_TID_MSK;
1396 priv->stations[sta_id].sta.add_immediate_ba_tid = (u8)tid;
1397 priv->stations[sta_id].sta.add_immediate_ba_ssn = cpu_to_le16(ssn);
1398 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1399 spin_unlock_irqrestore(&priv->sta_lock, flags);
1400
1401 return iwl_send_add_sta(priv, &priv->stations[sta_id].sta,
1402 CMD_ASYNC);
1403}
1404EXPORT_SYMBOL(iwl_sta_rx_agg_start);
1405
1406int iwl_sta_rx_agg_stop(struct iwl_priv *priv, const u8 *addr, int tid)
1407{
1408 unsigned long flags;
1409 int sta_id;
1410
c587de0b 1411 sta_id = iwl_find_station(priv, addr);
a2f1cbeb
WYG
1412 if (sta_id == IWL_INVALID_STATION) {
1413 IWL_ERR(priv, "Invalid station for AGG tid %d\n", tid);
9f58671e 1414 return -ENXIO;
a2f1cbeb 1415 }
9f58671e
TW
1416
1417 spin_lock_irqsave(&priv->sta_lock, flags);
1418 priv->stations[sta_id].sta.station_flags_msk = 0;
1419 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_DELBA_TID_MSK;
1420 priv->stations[sta_id].sta.remove_immediate_ba_tid = (u8)tid;
1421 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1422 spin_unlock_irqrestore(&priv->sta_lock, flags);
1423
1424 return iwl_send_add_sta(priv, &priv->stations[sta_id].sta,
1425 CMD_ASYNC);
1426}
1427EXPORT_SYMBOL(iwl_sta_rx_agg_stop);
1428
6ab10ff8 1429void iwl_sta_modify_ps_wake(struct iwl_priv *priv, int sta_id)
9f58671e
TW
1430{
1431 unsigned long flags;
1432
1433 spin_lock_irqsave(&priv->sta_lock, flags);
1434 priv->stations[sta_id].sta.station_flags &= ~STA_FLG_PWR_SAVE_MSK;
1435 priv->stations[sta_id].sta.station_flags_msk = STA_FLG_PWR_SAVE_MSK;
1436 priv->stations[sta_id].sta.sta.modify_mask = 0;
6ab10ff8 1437 priv->stations[sta_id].sta.sleep_tx_count = 0;
9f58671e
TW
1438 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1439 spin_unlock_irqrestore(&priv->sta_lock, flags);
1440
1441 iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
1442}
6ab10ff8 1443EXPORT_SYMBOL(iwl_sta_modify_ps_wake);
9f58671e 1444
6ab10ff8 1445void iwl_sta_modify_sleep_tx_count(struct iwl_priv *priv, int sta_id, int cnt)
9f58671e 1446{
6ab10ff8 1447 unsigned long flags;
9f58671e 1448
6ab10ff8
JB
1449 spin_lock_irqsave(&priv->sta_lock, flags);
1450 priv->stations[sta_id].sta.station_flags |= STA_FLG_PWR_SAVE_MSK;
1451 priv->stations[sta_id].sta.station_flags_msk = STA_FLG_PWR_SAVE_MSK;
1452 priv->stations[sta_id].sta.sta.modify_mask =
1453 STA_MODIFY_SLEEP_TX_COUNT_MSK;
1454 priv->stations[sta_id].sta.sleep_tx_count = cpu_to_le16(cnt);
1455 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1456 spin_unlock_irqrestore(&priv->sta_lock, flags);
9f58671e 1457
6ab10ff8 1458 iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
9f58671e 1459}
74bcdb33 1460EXPORT_SYMBOL(iwl_sta_modify_sleep_tx_count);
fe6b23dd
RC
1461
1462int iwl_mac_sta_remove(struct ieee80211_hw *hw,
fd1af15d
JB
1463 struct ieee80211_vif *vif,
1464 struct ieee80211_sta *sta)
fe6b23dd 1465{
fe6b23dd 1466 struct iwl_priv *priv = hw->priv;
fd1af15d
JB
1467 struct iwl_station_priv_common *sta_common = (void *)sta->drv_priv;
1468 int ret;
1469
fe6b23dd
RC
1470 IWL_DEBUG_INFO(priv, "received request to remove station %pM\n",
1471 sta->addr);
fd1af15d 1472 ret = iwl_remove_station(priv, sta_common->sta_id, sta->addr);
fe6b23dd
RC
1473 if (ret)
1474 IWL_ERR(priv, "Error removing station %pM\n",
1475 sta->addr);
1476 return ret;
1477}
1478EXPORT_SYMBOL(iwl_mac_sta_remove);