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