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