Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/linville/wirel...
[deliverable/linux.git] / drivers / net / wireless / iwlwifi / iwl-agn-sta.c
CommitLineData
a30e3112
JB
1/******************************************************************************
2 *
fb4961db 3 * Copyright(c) 2003 - 2012 Intel Corporation. All rights reserved.
a30e3112
JB
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 *****************************************************************************/
e7a0d0c4 29#include <linux/etherdevice.h>
a30e3112
JB
30#include <net/mac80211.h>
31
32#include "iwl-dev.h"
33#include "iwl-core.h"
a30e3112 34#include "iwl-agn.h"
bdfbf092 35#include "iwl-trans.h"
a30e3112 36
f32cadfc
MV
37const u8 iwl_bcast_addr[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
38
2da424b0 39static int iwl_sta_ucode_activate(struct iwl_priv *priv, u8 sta_id)
c745f55b 40{
fa23cb04
JB
41 lockdep_assert_held(&priv->sta_lock);
42
2da424b0
WYG
43 if (sta_id >= IWLAGN_STATION_COUNT) {
44 IWL_ERR(priv, "invalid sta_id %u", sta_id);
45 return -EINVAL;
46 }
c745f55b
WYG
47 if (!(priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE))
48 IWL_ERR(priv, "ACTIVATE a non DRIVER active station id %u "
49 "addr %pM\n",
50 sta_id, priv->stations[sta_id].sta.sta.addr);
51
52 if (priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE) {
53 IWL_DEBUG_ASSOC(priv,
54 "STA id %u addr %pM already present in uCode "
55 "(according to driver)\n",
56 sta_id, priv->stations[sta_id].sta.sta.addr);
57 } else {
58 priv->stations[sta_id].used |= IWL_STA_UCODE_ACTIVE;
59 IWL_DEBUG_ASSOC(priv, "Added STA id %u addr %pM to uCode\n",
60 sta_id, priv->stations[sta_id].sta.sta.addr);
61 }
2da424b0 62 return 0;
c745f55b
WYG
63}
64
65static int iwl_process_add_sta_resp(struct iwl_priv *priv,
66 struct iwl_addsta_cmd *addsta,
67 struct iwl_rx_packet *pkt)
68{
f8d7c1a1 69 struct iwl_add_sta_resp *add_sta_resp = (void *)pkt->data;
c745f55b 70 u8 sta_id = addsta->sta.sta_id;
c745f55b
WYG
71 int ret = -EIO;
72
73 if (pkt->hdr.flags & IWL_CMD_FAILED_MSK) {
74 IWL_ERR(priv, "Bad return from REPLY_ADD_STA (0x%08X)\n",
75 pkt->hdr.flags);
76 return ret;
77 }
78
79 IWL_DEBUG_INFO(priv, "Processing response for adding station %u\n",
80 sta_id);
81
fa23cb04 82 spin_lock(&priv->sta_lock);
c745f55b 83
f8d7c1a1 84 switch (add_sta_resp->status) {
c745f55b
WYG
85 case ADD_STA_SUCCESS_MSK:
86 IWL_DEBUG_INFO(priv, "REPLY_ADD_STA PASSED\n");
2da424b0 87 ret = iwl_sta_ucode_activate(priv, sta_id);
c745f55b
WYG
88 break;
89 case ADD_STA_NO_ROOM_IN_TABLE:
90 IWL_ERR(priv, "Adding station %d failed, no room in table.\n",
91 sta_id);
92 break;
93 case ADD_STA_NO_BLOCK_ACK_RESOURCE:
94 IWL_ERR(priv, "Adding station %d failed, no block ack "
95 "resource.\n", sta_id);
96 break;
97 case ADD_STA_MODIFY_NON_EXIST_STA:
98 IWL_ERR(priv, "Attempting to modify non-existing station %d\n",
99 sta_id);
100 break;
101 default:
102 IWL_DEBUG_ASSOC(priv, "Received REPLY_ADD_STA:(0x%08X)\n",
f8d7c1a1 103 add_sta_resp->status);
c745f55b
WYG
104 break;
105 }
106
107 IWL_DEBUG_INFO(priv, "%s station id %u addr %pM\n",
108 priv->stations[sta_id].sta.mode ==
109 STA_CONTROL_MODIFY_MSK ? "Modified" : "Added",
110 sta_id, priv->stations[sta_id].sta.sta.addr);
111
112 /*
113 * XXX: The MAC address in the command buffer is often changed from
114 * the original sent to the device. That is, the MAC address
115 * written to the command buffer often is not the same MAC address
116 * read from the command buffer when the command returns. This
117 * issue has not yet been resolved and this debugging is left to
118 * observe the problem.
119 */
120 IWL_DEBUG_INFO(priv, "%s station according to cmd buffer %pM\n",
121 priv->stations[sta_id].sta.mode ==
122 STA_CONTROL_MODIFY_MSK ? "Modified" : "Added",
123 addsta->sta.addr);
fa23cb04 124 spin_unlock(&priv->sta_lock);
c745f55b
WYG
125
126 return ret;
127}
128
48a2d66f 129int iwl_add_sta_callback(struct iwl_priv *priv, struct iwl_rx_cmd_buffer *rxb,
c745f55b
WYG
130 struct iwl_device_cmd *cmd)
131{
132 struct iwl_rx_packet *pkt = rxb_addr(rxb);
133 struct iwl_addsta_cmd *addsta =
134 (struct iwl_addsta_cmd *) cmd->payload;
135
136 return iwl_process_add_sta_resp(priv, addsta, pkt);
137}
138
c745f55b
WYG
139int iwl_send_add_sta(struct iwl_priv *priv,
140 struct iwl_addsta_cmd *sta, u8 flags)
141{
142 int ret = 0;
c745f55b
WYG
143 struct iwl_host_cmd cmd = {
144 .id = REPLY_ADD_STA,
145 .flags = flags,
69b172f7
JB
146 .data = { sta, },
147 .len = { sizeof(*sta), },
c745f55b
WYG
148 };
149 u8 sta_id __maybe_unused = sta->sta.sta_id;
150
151 IWL_DEBUG_INFO(priv, "Adding sta %u (%pM) %ssynchronously\n",
152 sta_id, sta->sta.addr, flags & CMD_ASYNC ? "a" : "");
153
154 if (!(flags & CMD_ASYNC)) {
155 cmd.flags |= CMD_WANT_SKB;
156 might_sleep();
157 }
158
e10a0533 159 ret = iwl_dvm_send_cmd(priv, &cmd);
c745f55b
WYG
160
161 if (ret || (flags & CMD_ASYNC))
162 return ret;
163 /*else the command was successfully sent in SYNC mode, need to free
164 * the reply page */
165
65b94a4a 166 iwl_free_resp(&cmd);
c745f55b
WYG
167
168 if (cmd.handler_status)
169 IWL_ERR(priv, "%s - error in the CMD response %d", __func__,
170 cmd.handler_status);
171
172 return cmd.handler_status;
173}
174
5edc565d
MV
175static bool iwl_is_channel_extension(struct iwl_priv *priv,
176 enum ieee80211_band band,
177 u16 channel, u8 extension_chan_offset)
178{
179 const struct iwl_channel_info *ch_info;
180
181 ch_info = iwl_get_channel_info(priv, band, channel);
182 if (!is_channel_valid(ch_info))
183 return false;
184
185 if (extension_chan_offset == IEEE80211_HT_PARAM_CHA_SEC_ABOVE)
186 return !(ch_info->ht40_extension_channel &
187 IEEE80211_CHAN_NO_HT40PLUS);
188 else if (extension_chan_offset == IEEE80211_HT_PARAM_CHA_SEC_BELOW)
189 return !(ch_info->ht40_extension_channel &
190 IEEE80211_CHAN_NO_HT40MINUS);
191
192 return false;
193}
194
195bool iwl_is_ht40_tx_allowed(struct iwl_priv *priv,
196 struct iwl_rxon_context *ctx,
197 struct ieee80211_sta_ht_cap *ht_cap)
198{
199 if (!ctx->ht.enabled || !ctx->ht.is_40mhz)
200 return false;
201
202 /*
203 * We do not check for IEEE80211_HT_CAP_SUP_WIDTH_20_40
204 * the bit will not set if it is pure 40MHz case
205 */
206 if (ht_cap && !ht_cap->ht_supported)
207 return false;
208
209#ifdef CONFIG_IWLWIFI_DEBUGFS
210 if (priv->disable_ht40)
211 return false;
212#endif
213
214 return iwl_is_channel_extension(priv, priv->band,
215 le16_to_cpu(ctx->staging.channel),
216 ctx->ht.extension_chan_offset);
217}
218
ab0bd5b3
JB
219static void iwl_sta_calc_ht_flags(struct iwl_priv *priv,
220 struct ieee80211_sta *sta,
221 struct iwl_rxon_context *ctx,
222 __le32 *flags, __le32 *mask)
c745f55b
WYG
223{
224 struct ieee80211_sta_ht_cap *sta_ht_inf = &sta->ht_cap;
c745f55b
WYG
225 u8 mimo_ps_mode;
226
ab0bd5b3
JB
227 *mask = STA_FLG_RTS_MIMO_PROT_MSK |
228 STA_FLG_MIMO_DIS_MSK |
229 STA_FLG_HT40_EN_MSK |
230 STA_FLG_MAX_AGG_SIZE_MSK |
231 STA_FLG_AGG_MPDU_DENSITY_MSK;
232 *flags = 0;
233
c745f55b 234 if (!sta || !sta_ht_inf->ht_supported)
ab0bd5b3 235 return;
c745f55b
WYG
236
237 mimo_ps_mode = (sta_ht_inf->cap & IEEE80211_HT_CAP_SM_PS) >> 2;
ab0bd5b3
JB
238
239 IWL_DEBUG_INFO(priv, "STA %pM SM PS mode: %s\n",
c745f55b
WYG
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
c745f55b
WYG
245 switch (mimo_ps_mode) {
246 case WLAN_HT_CAP_SM_PS_STATIC:
ab0bd5b3 247 *flags |= STA_FLG_MIMO_DIS_MSK;
c745f55b
WYG
248 break;
249 case WLAN_HT_CAP_SM_PS_DYNAMIC:
ab0bd5b3 250 *flags |= STA_FLG_RTS_MIMO_PROT_MSK;
c745f55b
WYG
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
ab0bd5b3
JB
259 *flags |= cpu_to_le32(
260 (u32)sta_ht_inf->ampdu_factor << STA_FLG_MAX_AGG_SIZE_POS);
c745f55b 261
ab0bd5b3
JB
262 *flags |= cpu_to_le32(
263 (u32)sta_ht_inf->ampdu_density << STA_FLG_AGG_MPDU_DENSITY_POS);
c745f55b
WYG
264
265 if (iwl_is_ht40_tx_allowed(priv, ctx, &sta->ht_cap))
ab0bd5b3
JB
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;
c745f55b 278
ab0bd5b3
JB
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;
c745f55b
WYG
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 (!compare_ether_addr(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 !compare_ether_addr(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{
c745f55b
WYG
404 int ret = 0;
405 u8 sta_id;
406 struct iwl_addsta_cmd sta_cmd;
407
408 *sta_id_r = 0;
fa23cb04 409 spin_lock_bh(&priv->sta_lock);
c745f55b
WYG
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);
fa23cb04 414 spin_unlock_bh(&priv->sta_lock);
c745f55b
WYG
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);
fa23cb04 426 spin_unlock_bh(&priv->sta_lock);
c745f55b
WYG
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);
fa23cb04 434 spin_unlock_bh(&priv->sta_lock);
c745f55b
WYG
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));
fa23cb04 441 spin_unlock_bh(&priv->sta_lock);
c745f55b
WYG
442
443 /* Add station to device's station table */
444 ret = iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
445 if (ret) {
fa23cb04 446 spin_lock_bh(&priv->sta_lock);
c745f55b
WYG
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;
fa23cb04 451 spin_unlock_bh(&priv->sta_lock);
c745f55b
WYG
452 }
453 *sta_id_r = sta_id;
454 return ret;
455}
456
457/**
458 * iwl_sta_ucode_deactivate - deactivate ucode status for a station
c745f55b
WYG
459 */
460static void iwl_sta_ucode_deactivate(struct iwl_priv *priv, u8 sta_id)
461{
fa23cb04
JB
462 lockdep_assert_held(&priv->sta_lock);
463
c745f55b
WYG
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;
c745f55b
WYG
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
e10a0533 497 ret = iwl_dvm_send_cmd(priv, &cmd);
c745f55b
WYG
498
499 if (ret)
500 return ret;
501
65b94a4a 502 pkt = cmd.resp_pkt;
c745f55b
WYG
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) {
f8d7c1a1
JB
510 struct iwl_rem_sta_resp *rem_sta_resp = (void *)pkt->data;
511 switch (rem_sta_resp->status) {
c745f55b
WYG
512 case REM_STA_SUCCESS_MSK:
513 if (!temporary) {
fa23cb04 514 spin_lock_bh(&priv->sta_lock);
c745f55b 515 iwl_sta_ucode_deactivate(priv, sta_id);
fa23cb04 516 spin_unlock_bh(&priv->sta_lock);
c745f55b
WYG
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 }
65b94a4a 526 iwl_free_resp(&cmd);
c745f55b
WYG
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{
855c2ee8 537 u8 tid;
c745f55b 538
83626404 539 if (!iwl_is_ready(priv)) {
c745f55b
WYG
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
fa23cb04 557 spin_lock_bh(&priv->sta_lock);
c745f55b
WYG
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
855c2ee8
EG
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
c745f55b
WYG
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
fa23cb04 587 spin_unlock_bh(&priv->sta_lock);
c745f55b
WYG
588
589 return iwl_send_remove_station(priv, addr, sta_id, false);
590out_err:
fa23cb04 591 spin_unlock_bh(&priv->sta_lock);
c745f55b
WYG
592 return -EINVAL;
593}
594
97420515
JB
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
4dcba6d3
JB
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
c745f55b
WYG
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;
c745f55b
WYG
692 bool cleared = false;
693
694 IWL_DEBUG_INFO(priv, "Clearing ucode stations in driver\n");
695
fa23cb04 696 spin_lock_bh(&priv->sta_lock);
c745f55b
WYG
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 }
fa23cb04 708 spin_unlock_bh(&priv->sta_lock);
c745f55b
WYG
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;
c745f55b
WYG
727 int i;
728 bool found = false;
729 int ret;
730 bool send_lq;
731
83626404 732 if (!iwl_is_ready(priv)) {
c745f55b
WYG
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");
fa23cb04 739 spin_lock_bh(&priv->sta_lock);
c745f55b
WYG
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) {
15b86bff 759 if (priv->wowlan)
c745f55b
WYG
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 }
fa23cb04 766 spin_unlock_bh(&priv->sta_lock);
c745f55b
WYG
767 ret = iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
768 if (ret) {
fa23cb04 769 spin_lock_bh(&priv->sta_lock);
c745f55b
WYG
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;
fa23cb04 776 spin_unlock_bh(&priv->sta_lock);
c745f55b
WYG
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);
fa23cb04 785 spin_lock_bh(&priv->sta_lock);
c745f55b
WYG
786 priv->stations[i].used &= ~IWL_STA_UCODE_INPROGRESS;
787 }
788 }
789
fa23cb04 790 spin_unlock_bh(&priv->sta_lock);
c745f55b
WYG
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
c745f55b
WYG
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{
c745f55b
WYG
812 int i;
813
fa23cb04 814 spin_lock_bh(&priv->sta_lock);
c745f55b
WYG
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 }
fa23cb04 826 spin_unlock_bh(&priv->sta_lock);
c745f55b
WYG
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;
c745f55b
WYG
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
fa23cb04 909 spin_lock_bh(&priv->sta_lock);
c745f55b 910 if (!(priv->stations[lq->sta_id].used & IWL_STA_DRIVER_ACTIVE)) {
fa23cb04 911 spin_unlock_bh(&priv->sta_lock);
c745f55b
WYG
912 return -EINVAL;
913 }
fa23cb04 914 spin_unlock_bh(&priv->sta_lock);
c745f55b
WYG
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))
e10a0533 921 ret = iwl_dvm_send_cmd(priv, &cmd);
c745f55b
WYG
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);
fa23cb04 932 spin_lock_bh(&priv->sta_lock);
c745f55b 933 priv->stations[lq->sta_id].used &= ~IWL_STA_UCODE_INPROGRESS;
fa23cb04 934 spin_unlock_bh(&priv->sta_lock);
c745f55b
WYG
935 }
936 return ret;
937}
938
c745f55b 939
c079166e 940static struct iwl_link_quality_cmd *
c745f55b
WYG
941iwl_sta_alloc_lq(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
942 u8 sta_id)
c079166e
JB
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);
a30e3112
JB
953
954 return link_cmd;
955}
956
957/*
958 * iwlagn_add_bssid_station - Add the special IBSS BSSID station
959 *
960 * Function sleeps.
961 */
c745f55b
WYG
962int iwlagn_add_bssid_station(struct iwl_priv *priv,
963 struct iwl_rxon_context *ctx,
a30e3112
JB
964 const u8 *addr, u8 *sta_id_r)
965{
966 int ret;
967 u8 sta_id;
968 struct iwl_link_quality_cmd *link_cmd;
a30e3112
JB
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
fa23cb04 982 spin_lock_bh(&priv->sta_lock);
a30e3112 983 priv->stations[sta_id].used |= IWL_STA_LOCAL;
fa23cb04 984 spin_unlock_bh(&priv->sta_lock);
a30e3112
JB
985
986 /* Set up default rate scaling table in device's station table */
f775aa06 987 link_cmd = iwl_sta_alloc_lq(priv, ctx, sta_id);
a30e3112 988 if (!link_cmd) {
c745f55b
WYG
989 IWL_ERR(priv,
990 "Unable to initialize rate scaling for station %pM.\n",
a30e3112
JB
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
fa23cb04 999 spin_lock_bh(&priv->sta_lock);
a30e3112 1000 priv->stations[sta_id].lq = link_cmd;
fa23cb04 1001 spin_unlock_bh(&priv->sta_lock);
a30e3112
JB
1002
1003 return 0;
1004}
1005
5a3d9882
JB
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
a30e3112
JB
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,
3fa50738 1025 .data = { wep_cmd, },
a30e3112
JB
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
3fa50738 1053 cmd.len[0] = cmd_size;
a30e3112
JB
1054
1055 if (not_empty || send_if_empty)
e10a0533 1056 return iwl_dvm_send_cmd(priv, &cmd);
a30e3112
JB
1057 else
1058 return 0;
1059}
1060
1061int iwl_restore_default_wep_keys(struct iwl_priv *priv,
1062 struct iwl_rxon_context *ctx)
1063{
b1eea297 1064 lockdep_assert_held(&priv->mutex);
a30e3112
JB
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
b1eea297 1075 lockdep_assert_held(&priv->mutex);
a30e3112
JB
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]));
83626404 1081 if (iwl_is_rfkill(priv)) {
c745f55b
WYG
1082 IWL_DEBUG_WEP(priv,
1083 "Not sending REPLY_WEPKEY command due to RFKILL.\n");
a30e3112
JB
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
b1eea297 1100 lockdep_assert_held(&priv->mutex);
a30e3112
JB
1101
1102 if (keyconf->keylen != WEP_KEY_LEN_128 &&
1103 keyconf->keylen != WEP_KEY_LEN_64) {
c745f55b
WYG
1104 IWL_DEBUG_WEP(priv,
1105 "Bad WEP key length %d\n", keyconf->keylen);
a30e3112
JB
1106 return -EINVAL;
1107 }
1108
5a3d9882 1109 keyconf->hw_key_idx = IWLAGN_HW_KEY_DEFAULT;
a30e3112
JB
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
5a3d9882
JB
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 */
a30e3112 1136
5a3d9882
JB
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;
5a3d9882
JB
1142
1143 if (sta)
40e4e686 1144 return iwl_sta_id(sta);
5a3d9882
JB
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 */
40e4e686
JB
1151 if (vif->type == NL80211_IFTYPE_STATION && vif_priv->ctx)
1152 return vif_priv->ctx->ap_sta_id;
a30e3112 1153
40e4e686 1154 return IWL_INVALID_STATION;
a30e3112
JB
1155}
1156
e1b1c087
JB
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)
a30e3112 1161{
5a3d9882 1162 __le16 key_flags;
a30e3112 1163 struct iwl_addsta_cmd sta_cmd;
5a3d9882 1164 int i;
e1b1c087 1165
fa23cb04 1166 spin_lock_bh(&priv->sta_lock);
5a3d9882 1167 memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(sta_cmd));
fa23cb04 1168 spin_unlock_bh(&priv->sta_lock);
a30e3112 1169
5a3d9882
JB
1170 key_flags = cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
1171 key_flags |= STA_KEY_FLG_MAP_KEY_MSK;
a30e3112 1172
5a3d9882
JB
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 }
a30e3112 1196
5a3d9882 1197 if (!(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE))
a30e3112
JB
1198 key_flags |= STA_KEY_MULTICAST_MSK;
1199
5a3d9882
JB
1200 /* key pointer (offset) */
1201 sta_cmd.key.key_offset = keyconf->hw_key_idx;
a30e3112 1202
5a3d9882
JB
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;
a30e3112 1206
5a3d9882 1207 return iwl_send_add_sta(priv, &sta_cmd, cmd_flags);
a30e3112
JB
1208}
1209
1210void iwl_update_tkip_key(struct iwl_priv *priv,
5a3d9882 1211 struct ieee80211_vif *vif,
a30e3112
JB
1212 struct ieee80211_key_conf *keyconf,
1213 struct ieee80211_sta *sta, u32 iv32, u16 *phase1key)
1214{
5a3d9882
JB
1215 u8 sta_id = iwlagn_key_sta_id(priv, vif, sta);
1216
1217 if (sta_id == IWL_INVALID_STATION)
1218 return;
a30e3112
JB
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
e1b1c087
JB
1226 iwlagn_send_sta_key(priv, keyconf, sta_id,
1227 iv32, phase1key, CMD_ASYNC);
a30e3112
JB
1228}
1229
1230int iwl_remove_dynamic_key(struct iwl_priv *priv,
1231 struct iwl_rxon_context *ctx,
1232 struct ieee80211_key_conf *keyconf,
5a3d9882 1233 struct ieee80211_sta *sta)
a30e3112 1234{
a30e3112 1235 struct iwl_addsta_cmd sta_cmd;
5a3d9882 1236 u8 sta_id = iwlagn_key_sta_id(priv, ctx->vif, sta);
5dcbf480 1237 __le16 key_flags;
5a3d9882
JB
1238
1239 /* if station isn't there, neither is the key */
1240 if (sta_id == IWL_INVALID_STATION)
1241 return -ENOENT;
1242
fa23cb04 1243 spin_lock_bh(&priv->sta_lock);
5a3d9882
JB
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;
fa23cb04 1247 spin_unlock_bh(&priv->sta_lock);
5a3d9882
JB
1248
1249 if (sta_id == IWL_INVALID_STATION)
1250 return 0;
a30e3112 1251
b1eea297 1252 lockdep_assert_held(&priv->mutex);
a30e3112
JB
1253
1254 ctx->key_mapping_keys--;
1255
a30e3112
JB
1256 IWL_DEBUG_WEP(priv, "Remove dynamic key: idx=%d sta=%d\n",
1257 keyconf->keyidx, sta_id);
1258
5a3d9882
JB
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);
a30e3112 1262
5dcbf480
JB
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;
5a3d9882
JB
1271 sta_cmd.key.key_offset = WEP_INVALID_OFFSET;
1272 sta_cmd.sta.modify_mask = STA_MODIFY_KEY_MASK;
1273 sta_cmd.mode = STA_CONTROL_MODIFY_MSK;
a30e3112
JB
1274
1275 return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
1276}
1277
5a3d9882
JB
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)
a30e3112 1282{
5a3d9882
JB
1283 struct ieee80211_key_seq seq;
1284 u16 p1k[5];
a30e3112 1285 int ret;
5a3d9882
JB
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;
a30e3112 1291
b1eea297 1292 lockdep_assert_held(&priv->mutex);
a30e3112 1293
5a3d9882
JB
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
a30e3112 1298 ctx->key_mapping_keys++;
a30e3112
JB
1299
1300 switch (keyconf->cipher) {
a30e3112 1301 case WLAN_CIPHER_SUITE_TKIP:
5a3d9882
JB
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);
e1b1c087
JB
1310 ret = iwlagn_send_sta_key(priv, keyconf, sta_id,
1311 seq.tkip.iv32, p1k, CMD_SYNC);
a30e3112 1312 break;
5a3d9882 1313 case WLAN_CIPHER_SUITE_CCMP:
a30e3112
JB
1314 case WLAN_CIPHER_SUITE_WEP40:
1315 case WLAN_CIPHER_SUITE_WEP104:
e1b1c087
JB
1316 ret = iwlagn_send_sta_key(priv, keyconf, sta_id,
1317 0, NULL, CMD_SYNC);
a30e3112
JB
1318 break;
1319 default:
5a3d9882 1320 IWL_ERR(priv, "Unknown cipher %x\n", keyconf->cipher);
a30e3112
JB
1321 ret = -EINVAL;
1322 }
1323
5a3d9882
JB
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",
a30e3112 1330 keyconf->cipher, keyconf->keylen, keyconf->keyidx,
5a3d9882 1331 sta ? sta->addr : NULL, ret);
a30e3112
JB
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;
a30e3112
JB
1347 u8 sta_id;
1348
fa23cb04 1349 spin_lock_bh(&priv->sta_lock);
a30e3112
JB
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");
fa23cb04 1353 spin_unlock_bh(&priv->sta_lock);
a30e3112
JB
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;
fa23cb04 1360 spin_unlock_bh(&priv->sta_lock);
a30e3112 1361
f775aa06 1362 link_cmd = iwl_sta_alloc_lq(priv, ctx, sta_id);
a30e3112
JB
1363 if (!link_cmd) {
1364 IWL_ERR(priv,
1365 "Unable to initialize rate scaling for bcast station.\n");
1366 return -ENOMEM;
1367 }
1368
fa23cb04 1369 spin_lock_bh(&priv->sta_lock);
a30e3112 1370 priv->stations[sta_id].lq = link_cmd;
fa23cb04 1371 spin_unlock_bh(&priv->sta_lock);
a30e3112
JB
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 */
f775aa06
JB
1382int iwl_update_bcast_station(struct iwl_priv *priv,
1383 struct iwl_rxon_context *ctx)
a30e3112 1384{
a30e3112
JB
1385 struct iwl_link_quality_cmd *link_cmd;
1386 u8 sta_id = ctx->bcast_sta_id;
1387
f775aa06 1388 link_cmd = iwl_sta_alloc_lq(priv, ctx, sta_id);
a30e3112
JB
1389 if (!link_cmd) {
1390 IWL_ERR(priv, "Unable to initialize rate scaling for bcast station.\n");
1391 return -ENOMEM;
1392 }
1393
fa23cb04 1394 spin_lock_bh(&priv->sta_lock);
a30e3112
JB
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;
fa23cb04 1400 spin_unlock_bh(&priv->sta_lock);
a30e3112
JB
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{
a30e3112
JB
1424 struct iwl_addsta_cmd sta_cmd;
1425
b1eea297 1426 lockdep_assert_held(&priv->mutex);
a30e3112
JB
1427
1428 /* Remove "disable" flag, to enable Tx for this TID */
fa23cb04 1429 spin_lock_bh(&priv->sta_lock);
a30e3112
JB
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));
fa23cb04 1434 spin_unlock_bh(&priv->sta_lock);
a30e3112
JB
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{
a30e3112
JB
1442 int sta_id;
1443 struct iwl_addsta_cmd sta_cmd;
1444
b1eea297 1445 lockdep_assert_held(&priv->mutex);
a30e3112
JB
1446
1447 sta_id = iwl_sta_id(sta);
1448 if (sta_id == IWL_INVALID_STATION)
1449 return -ENXIO;
1450
fa23cb04 1451 spin_lock_bh(&priv->sta_lock);
a30e3112
JB
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));
fa23cb04 1458 spin_unlock_bh(&priv->sta_lock);
a30e3112
JB
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{
a30e3112
JB
1466 int sta_id;
1467 struct iwl_addsta_cmd sta_cmd;
1468
b1eea297 1469 lockdep_assert_held(&priv->mutex);
a30e3112
JB
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
fa23cb04 1477 spin_lock_bh(&priv->sta_lock);
a30e3112
JB
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));
fa23cb04 1483 spin_unlock_bh(&priv->sta_lock);
a30e3112
JB
1484
1485 return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
1486}
1487
a30e3112 1488
a30e3112
JB
1489
1490void iwl_sta_modify_sleep_tx_count(struct iwl_priv *priv, int sta_id, int cnt)
1491{
9451ca1a
JB
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 };
a30e3112 1500
9451ca1a 1501 iwl_send_add_sta(priv, &cmd, CMD_ASYNC);
a30e3112 1502}
This page took 0.338677 seconds and 5 git commands to generate.