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