blob: 7a2babe0dd4e3cc446e37c955110977e47170187 [file] [log] [blame]
Johannes Berga30e3112010-09-22 18:02:01 +02001/******************************************************************************
2 *
Wey-Yi Guy901069c2011-04-05 09:42:00 -07003 * Copyright(c) 2003 - 2011 Intel Corporation. All rights reserved.
Johannes Berga30e3112010-09-22 18:02:01 +02004 *
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 *****************************************************************************/
29
30#include <net/mac80211.h>
31
32#include "iwl-dev.h"
33#include "iwl-core.h"
34#include "iwl-sta.h"
35#include "iwl-agn.h"
Emmanuel Grumbachbdfbf092011-07-08 08:46:16 -070036#include "iwl-trans.h"
Johannes Berga30e3112010-09-22 18:02:01 +020037
38static struct iwl_link_quality_cmd *
Johannes Bergf775aa02011-06-22 06:34:09 -070039iwl_sta_alloc_lq(struct iwl_priv *priv, struct iwl_rxon_context *ctx, u8 sta_id)
Johannes Berga30e3112010-09-22 18:02:01 +020040{
41 int i, r;
42 struct iwl_link_quality_cmd *link_cmd;
43 u32 rate_flags = 0;
44 __le32 rate_n_flags;
45
46 link_cmd = kzalloc(sizeof(struct iwl_link_quality_cmd), GFP_KERNEL);
47 if (!link_cmd) {
48 IWL_ERR(priv, "Unable to allocate memory for LQ cmd.\n");
49 return NULL;
50 }
Johannes Bergf775aa02011-06-22 06:34:09 -070051
52 lockdep_assert_held(&priv->mutex);
53
Johannes Berga30e3112010-09-22 18:02:01 +020054 /* Set up the rate scaling to start at selected rate, fall back
55 * all the way down to 1M in IEEE order, and then spin on 1M */
56 if (priv->band == IEEE80211_BAND_5GHZ)
57 r = IWL_RATE_6M_INDEX;
Johannes Bergf775aa02011-06-22 06:34:09 -070058 else if (ctx && ctx->vif && ctx->vif->p2p)
59 r = IWL_RATE_6M_INDEX;
Johannes Berga30e3112010-09-22 18:02:01 +020060 else
61 r = IWL_RATE_1M_INDEX;
62
63 if (r >= IWL_FIRST_CCK_RATE && r <= IWL_LAST_CCK_RATE)
64 rate_flags |= RATE_MCS_CCK_MSK;
65
66 rate_flags |= first_antenna(priv->hw_params.valid_tx_ant) <<
67 RATE_MCS_ANT_POS;
68 rate_n_flags = iwl_hw_set_rate_n_flags(iwl_rates[r].plcp, rate_flags);
69 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
70 link_cmd->rs_table[i].rate_n_flags = rate_n_flags;
71
72 link_cmd->general_params.single_stream_ant_msk =
73 first_antenna(priv->hw_params.valid_tx_ant);
74
75 link_cmd->general_params.dual_stream_ant_msk =
76 priv->hw_params.valid_tx_ant &
77 ~first_antenna(priv->hw_params.valid_tx_ant);
78 if (!link_cmd->general_params.dual_stream_ant_msk) {
79 link_cmd->general_params.dual_stream_ant_msk = ANT_AB;
80 } else if (num_of_ant(priv->hw_params.valid_tx_ant) == 2) {
81 link_cmd->general_params.dual_stream_ant_msk =
82 priv->hw_params.valid_tx_ant;
83 }
84
85 link_cmd->agg_params.agg_dis_start_th = LINK_QUAL_AGG_DISABLE_START_DEF;
86 link_cmd->agg_params.agg_time_limit =
87 cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
88
89 link_cmd->sta_id = sta_id;
90
91 return link_cmd;
92}
93
94/*
95 * iwlagn_add_bssid_station - Add the special IBSS BSSID station
96 *
97 * Function sleeps.
98 */
99int iwlagn_add_bssid_station(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
100 const u8 *addr, u8 *sta_id_r)
101{
102 int ret;
103 u8 sta_id;
104 struct iwl_link_quality_cmd *link_cmd;
105 unsigned long flags;
106
107 if (sta_id_r)
108 *sta_id_r = IWL_INVALID_STATION;
109
110 ret = iwl_add_station_common(priv, ctx, addr, 0, NULL, &sta_id);
111 if (ret) {
112 IWL_ERR(priv, "Unable to add station %pM\n", addr);
113 return ret;
114 }
115
116 if (sta_id_r)
117 *sta_id_r = sta_id;
118
119 spin_lock_irqsave(&priv->sta_lock, flags);
120 priv->stations[sta_id].used |= IWL_STA_LOCAL;
121 spin_unlock_irqrestore(&priv->sta_lock, flags);
122
123 /* Set up default rate scaling table in device's station table */
Johannes Bergf775aa02011-06-22 06:34:09 -0700124 link_cmd = iwl_sta_alloc_lq(priv, ctx, sta_id);
Johannes Berga30e3112010-09-22 18:02:01 +0200125 if (!link_cmd) {
126 IWL_ERR(priv, "Unable to initialize rate scaling for station %pM.\n",
127 addr);
128 return -ENOMEM;
129 }
130
131 ret = iwl_send_lq_cmd(priv, ctx, link_cmd, CMD_SYNC, true);
132 if (ret)
133 IWL_ERR(priv, "Link quality command failed (%d)\n", ret);
134
135 spin_lock_irqsave(&priv->sta_lock, flags);
136 priv->stations[sta_id].lq = link_cmd;
137 spin_unlock_irqrestore(&priv->sta_lock, flags);
138
139 return 0;
140}
141
Johannes Berg5a3d9882011-07-15 13:03:12 -0700142/*
143 * static WEP keys
144 *
145 * For each context, the device has a table of 4 static WEP keys
146 * (one for each key index) that is updated with the following
147 * commands.
148 */
149
Johannes Berga30e3112010-09-22 18:02:01 +0200150static int iwl_send_static_wepkey_cmd(struct iwl_priv *priv,
151 struct iwl_rxon_context *ctx,
152 bool send_if_empty)
153{
154 int i, not_empty = 0;
155 u8 buff[sizeof(struct iwl_wep_cmd) +
156 sizeof(struct iwl_wep_key) * WEP_KEYS_MAX];
157 struct iwl_wep_cmd *wep_cmd = (struct iwl_wep_cmd *)buff;
158 size_t cmd_size = sizeof(struct iwl_wep_cmd);
159 struct iwl_host_cmd cmd = {
160 .id = ctx->wep_key_cmd,
Johannes Berg3fa50732011-05-04 07:50:38 -0700161 .data = { wep_cmd, },
Johannes Berga30e3112010-09-22 18:02:01 +0200162 .flags = CMD_SYNC,
163 };
164
165 might_sleep();
166
167 memset(wep_cmd, 0, cmd_size +
168 (sizeof(struct iwl_wep_key) * WEP_KEYS_MAX));
169
170 for (i = 0; i < WEP_KEYS_MAX ; i++) {
171 wep_cmd->key[i].key_index = i;
172 if (ctx->wep_keys[i].key_size) {
173 wep_cmd->key[i].key_offset = i;
174 not_empty = 1;
175 } else {
176 wep_cmd->key[i].key_offset = WEP_INVALID_OFFSET;
177 }
178
179 wep_cmd->key[i].key_size = ctx->wep_keys[i].key_size;
180 memcpy(&wep_cmd->key[i].key[3], ctx->wep_keys[i].key,
181 ctx->wep_keys[i].key_size);
182 }
183
184 wep_cmd->global_key_type = WEP_KEY_WEP_TYPE;
185 wep_cmd->num_keys = WEP_KEYS_MAX;
186
187 cmd_size += sizeof(struct iwl_wep_key) * WEP_KEYS_MAX;
188
Johannes Berg3fa50732011-05-04 07:50:38 -0700189 cmd.len[0] = cmd_size;
Johannes Berga30e3112010-09-22 18:02:01 +0200190
191 if (not_empty || send_if_empty)
Emmanuel Grumbach41c50542011-07-11 08:51:04 +0300192 return trans_send_cmd(&priv->trans, &cmd);
Johannes Berga30e3112010-09-22 18:02:01 +0200193 else
194 return 0;
195}
196
197int iwl_restore_default_wep_keys(struct iwl_priv *priv,
198 struct iwl_rxon_context *ctx)
199{
200 lockdep_assert_held(&priv->mutex);
201
202 return iwl_send_static_wepkey_cmd(priv, ctx, false);
203}
204
205int iwl_remove_default_wep_key(struct iwl_priv *priv,
206 struct iwl_rxon_context *ctx,
207 struct ieee80211_key_conf *keyconf)
208{
209 int ret;
210
211 lockdep_assert_held(&priv->mutex);
212
213 IWL_DEBUG_WEP(priv, "Removing default WEP key: idx=%d\n",
214 keyconf->keyidx);
215
216 memset(&ctx->wep_keys[keyconf->keyidx], 0, sizeof(ctx->wep_keys[0]));
217 if (iwl_is_rfkill(priv)) {
218 IWL_DEBUG_WEP(priv, "Not sending REPLY_WEPKEY command due to RFKILL.\n");
219 /* but keys in device are clear anyway so return success */
220 return 0;
221 }
222 ret = iwl_send_static_wepkey_cmd(priv, ctx, 1);
223 IWL_DEBUG_WEP(priv, "Remove default WEP key: idx=%d ret=%d\n",
224 keyconf->keyidx, ret);
225
226 return ret;
227}
228
229int iwl_set_default_wep_key(struct iwl_priv *priv,
230 struct iwl_rxon_context *ctx,
231 struct ieee80211_key_conf *keyconf)
232{
233 int ret;
234
235 lockdep_assert_held(&priv->mutex);
236
237 if (keyconf->keylen != WEP_KEY_LEN_128 &&
238 keyconf->keylen != WEP_KEY_LEN_64) {
239 IWL_DEBUG_WEP(priv, "Bad WEP key length %d\n", keyconf->keylen);
240 return -EINVAL;
241 }
242
Johannes Berg5a3d9882011-07-15 13:03:12 -0700243 keyconf->hw_key_idx = IWLAGN_HW_KEY_DEFAULT;
Johannes Berga30e3112010-09-22 18:02:01 +0200244
245 ctx->wep_keys[keyconf->keyidx].key_size = keyconf->keylen;
246 memcpy(&ctx->wep_keys[keyconf->keyidx].key, &keyconf->key,
247 keyconf->keylen);
248
249 ret = iwl_send_static_wepkey_cmd(priv, ctx, false);
250 IWL_DEBUG_WEP(priv, "Set default WEP key: len=%d idx=%d ret=%d\n",
251 keyconf->keylen, keyconf->keyidx, ret);
252
253 return ret;
254}
255
Johannes Berg5a3d9882011-07-15 13:03:12 -0700256/*
257 * dynamic (per-station) keys
258 *
259 * The dynamic keys are a little more complicated. The device has
260 * a key cache of up to STA_KEY_MAX_NUM/STA_KEY_MAX_NUM_PAN keys.
261 * These are linked to stations by a table that contains an index
262 * into the key table for each station/key index/{mcast,unicast},
263 * i.e. it's basically an array of pointers like this:
264 * key_offset_t key_mapping[NUM_STATIONS][4][2];
265 * (it really works differently, but you can think of it as such)
266 *
267 * The key uploading and linking happens in the same command, the
268 * add station command with STA_MODIFY_KEY_MASK.
269 */
270
271static u8 iwlagn_key_sta_id(struct iwl_priv *priv,
272 struct ieee80211_vif *vif,
273 struct ieee80211_sta *sta)
274{
275 struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
276 u8 sta_id = IWL_INVALID_STATION;
277
278 if (sta)
279 sta_id = iwl_sta_id(sta);
280
281 /*
282 * The device expects GTKs for station interfaces to be
283 * installed as GTKs for the AP station. If we have no
284 * station ID, then use the ap_sta_id in that case.
285 */
286 if (!sta && vif && vif_priv->ctx) {
287 switch (vif->type) {
288 case NL80211_IFTYPE_STATION:
289 sta_id = vif_priv->ctx->ap_sta_id;
290 break;
291 default:
292 /*
293 * In all other cases, the key will be
294 * used either for TX only or is bound
295 * to a station already.
296 */
297 break;
298 }
299 }
300
301 return sta_id;
302}
303
304static int iwlagn_set_dynamic_key(struct iwl_priv *priv,
305 struct ieee80211_key_conf *keyconf,
306 u8 sta_id, u32 tkip_iv32, u16 *tkip_p1k,
307 u32 cmd_flags)
Johannes Berga30e3112010-09-22 18:02:01 +0200308{
309 unsigned long flags;
Johannes Berg5a3d9882011-07-15 13:03:12 -0700310 __le16 key_flags;
Johannes Berga30e3112010-09-22 18:02:01 +0200311 struct iwl_addsta_cmd sta_cmd;
Johannes Berg5a3d9882011-07-15 13:03:12 -0700312 int i;
313 spin_lock_irqsave(&priv->sta_lock, flags);
314 memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(sta_cmd));
315 spin_unlock_irqrestore(&priv->sta_lock, flags);
Johannes Berga30e3112010-09-22 18:02:01 +0200316
Johannes Berg5a3d9882011-07-15 13:03:12 -0700317 key_flags = cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
318 key_flags |= STA_KEY_FLG_MAP_KEY_MSK;
Johannes Berga30e3112010-09-22 18:02:01 +0200319
Johannes Berg5a3d9882011-07-15 13:03:12 -0700320 switch (keyconf->cipher) {
321 case WLAN_CIPHER_SUITE_CCMP:
322 key_flags |= STA_KEY_FLG_CCMP;
323 memcpy(sta_cmd.key.key, keyconf->key, keyconf->keylen);
324 break;
325 case WLAN_CIPHER_SUITE_TKIP:
326 key_flags |= STA_KEY_FLG_TKIP;
327 sta_cmd.key.tkip_rx_tsc_byte2 = tkip_iv32;
328 for (i = 0; i < 5; i++)
329 sta_cmd.key.tkip_rx_ttak[i] = cpu_to_le16(tkip_p1k[i]);
330 memcpy(sta_cmd.key.key, keyconf->key, keyconf->keylen);
331 break;
332 case WLAN_CIPHER_SUITE_WEP104:
Johannes Berga30e3112010-09-22 18:02:01 +0200333 key_flags |= STA_KEY_FLG_KEY_SIZE_MSK;
Johannes Berg5a3d9882011-07-15 13:03:12 -0700334 /* fall through */
335 case WLAN_CIPHER_SUITE_WEP40:
336 key_flags |= STA_KEY_FLG_WEP;
337 memcpy(&sta_cmd.key.key[3], keyconf->key, keyconf->keylen);
338 break;
339 default:
340 WARN_ON(1);
341 return -EINVAL;
342 }
Johannes Berga30e3112010-09-22 18:02:01 +0200343
Johannes Berg5a3d9882011-07-15 13:03:12 -0700344 if (!(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE))
Johannes Berga30e3112010-09-22 18:02:01 +0200345 key_flags |= STA_KEY_MULTICAST_MSK;
346
Johannes Berg5a3d9882011-07-15 13:03:12 -0700347 /* key pointer (offset) */
348 sta_cmd.key.key_offset = keyconf->hw_key_idx;
Johannes Berga30e3112010-09-22 18:02:01 +0200349
Johannes Berg5a3d9882011-07-15 13:03:12 -0700350 sta_cmd.key.key_flags = key_flags;
351 sta_cmd.mode = STA_CONTROL_MODIFY_MSK;
352 sta_cmd.sta.modify_mask = STA_MODIFY_KEY_MASK;
Johannes Berga30e3112010-09-22 18:02:01 +0200353
Johannes Berg5a3d9882011-07-15 13:03:12 -0700354 return iwl_send_add_sta(priv, &sta_cmd, cmd_flags);
Johannes Berga30e3112010-09-22 18:02:01 +0200355}
356
357void iwl_update_tkip_key(struct iwl_priv *priv,
Johannes Berg5a3d9882011-07-15 13:03:12 -0700358 struct ieee80211_vif *vif,
Johannes Berga30e3112010-09-22 18:02:01 +0200359 struct ieee80211_key_conf *keyconf,
360 struct ieee80211_sta *sta, u32 iv32, u16 *phase1key)
361{
Johannes Berg5a3d9882011-07-15 13:03:12 -0700362 u8 sta_id = iwlagn_key_sta_id(priv, vif, sta);
363
364 if (sta_id == IWL_INVALID_STATION)
365 return;
Johannes Berga30e3112010-09-22 18:02:01 +0200366
367 if (iwl_scan_cancel(priv)) {
368 /* cancel scan failed, just live w/ bad key and rely
369 briefly on SW decryption */
370 return;
371 }
372
Johannes Berg5a3d9882011-07-15 13:03:12 -0700373 iwlagn_set_dynamic_key(priv, keyconf, sta_id,
374 iv32, phase1key, CMD_ASYNC);
Johannes Berga30e3112010-09-22 18:02:01 +0200375}
376
377int iwl_remove_dynamic_key(struct iwl_priv *priv,
378 struct iwl_rxon_context *ctx,
379 struct ieee80211_key_conf *keyconf,
Johannes Berg5a3d9882011-07-15 13:03:12 -0700380 struct ieee80211_sta *sta)
Johannes Berga30e3112010-09-22 18:02:01 +0200381{
382 unsigned long flags;
Johannes Berga30e3112010-09-22 18:02:01 +0200383 struct iwl_addsta_cmd sta_cmd;
Johannes Berg5a3d9882011-07-15 13:03:12 -0700384 u8 sta_id = iwlagn_key_sta_id(priv, ctx->vif, sta);
385
386 /* if station isn't there, neither is the key */
387 if (sta_id == IWL_INVALID_STATION)
388 return -ENOENT;
389
390 spin_lock_irqsave(&priv->sta_lock, flags);
391 memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(sta_cmd));
392 if (!(priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE))
393 sta_id = IWL_INVALID_STATION;
394 spin_unlock_irqrestore(&priv->sta_lock, flags);
395
396 if (sta_id == IWL_INVALID_STATION)
397 return 0;
Johannes Berga30e3112010-09-22 18:02:01 +0200398
399 lockdep_assert_held(&priv->mutex);
400
401 ctx->key_mapping_keys--;
402
Johannes Berga30e3112010-09-22 18:02:01 +0200403 IWL_DEBUG_WEP(priv, "Remove dynamic key: idx=%d sta=%d\n",
404 keyconf->keyidx, sta_id);
405
Johannes Berg5a3d9882011-07-15 13:03:12 -0700406 if (!test_and_clear_bit(keyconf->hw_key_idx, &priv->ucode_key_table))
407 IWL_ERR(priv, "offset %d not used in uCode key table.\n",
408 keyconf->hw_key_idx);
Johannes Berga30e3112010-09-22 18:02:01 +0200409
Johannes Berg5a3d9882011-07-15 13:03:12 -0700410 sta_cmd.key.key_flags = STA_KEY_FLG_NO_ENC | STA_KEY_FLG_INVALID;
411 sta_cmd.key.key_offset = WEP_INVALID_OFFSET;
412 sta_cmd.sta.modify_mask = STA_MODIFY_KEY_MASK;
413 sta_cmd.mode = STA_CONTROL_MODIFY_MSK;
Johannes Berga30e3112010-09-22 18:02:01 +0200414
415 return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
416}
417
Johannes Berg5a3d9882011-07-15 13:03:12 -0700418int iwl_set_dynamic_key(struct iwl_priv *priv,
419 struct iwl_rxon_context *ctx,
420 struct ieee80211_key_conf *keyconf,
421 struct ieee80211_sta *sta)
Johannes Berga30e3112010-09-22 18:02:01 +0200422{
Johannes Berg5a3d9882011-07-15 13:03:12 -0700423 struct ieee80211_key_seq seq;
424 u16 p1k[5];
Johannes Berga30e3112010-09-22 18:02:01 +0200425 int ret;
Johannes Berg5a3d9882011-07-15 13:03:12 -0700426 u8 sta_id = iwlagn_key_sta_id(priv, ctx->vif, sta);
427 const u8 *addr;
428
429 if (sta_id == IWL_INVALID_STATION)
430 return -EINVAL;
Johannes Berga30e3112010-09-22 18:02:01 +0200431
432 lockdep_assert_held(&priv->mutex);
433
Johannes Berg5a3d9882011-07-15 13:03:12 -0700434 keyconf->hw_key_idx = iwl_get_free_ucode_key_offset(priv);
435 if (keyconf->hw_key_idx == WEP_INVALID_OFFSET)
436 return -ENOSPC;
437
Johannes Berga30e3112010-09-22 18:02:01 +0200438 ctx->key_mapping_keys++;
Johannes Berga30e3112010-09-22 18:02:01 +0200439
440 switch (keyconf->cipher) {
Johannes Berga30e3112010-09-22 18:02:01 +0200441 case WLAN_CIPHER_SUITE_TKIP:
Johannes Berg5a3d9882011-07-15 13:03:12 -0700442 keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
443 keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
444
445 if (sta)
446 addr = sta->addr;
447 else /* station mode case only */
448 addr = ctx->active.bssid_addr;
449
450 /* pre-fill phase 1 key into device cache */
451 ieee80211_get_key_rx_seq(keyconf, 0, &seq);
452 ieee80211_get_tkip_rx_p1k(keyconf, addr, seq.tkip.iv32, p1k);
453 ret = iwlagn_set_dynamic_key(priv, keyconf, sta_id,
454 seq.tkip.iv32, p1k, CMD_SYNC);
Johannes Berga30e3112010-09-22 18:02:01 +0200455 break;
Johannes Berg5a3d9882011-07-15 13:03:12 -0700456 case WLAN_CIPHER_SUITE_CCMP:
457 keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
458 /* fall through */
Johannes Berga30e3112010-09-22 18:02:01 +0200459 case WLAN_CIPHER_SUITE_WEP40:
460 case WLAN_CIPHER_SUITE_WEP104:
Johannes Berg5a3d9882011-07-15 13:03:12 -0700461 ret = iwlagn_set_dynamic_key(priv, keyconf, sta_id,
462 0, NULL, CMD_SYNC);
Johannes Berga30e3112010-09-22 18:02:01 +0200463 break;
464 default:
Johannes Berg5a3d9882011-07-15 13:03:12 -0700465 IWL_ERR(priv, "Unknown cipher %x\n", keyconf->cipher);
Johannes Berga30e3112010-09-22 18:02:01 +0200466 ret = -EINVAL;
467 }
468
Johannes Berg5a3d9882011-07-15 13:03:12 -0700469 if (ret) {
470 ctx->key_mapping_keys--;
471 clear_bit(keyconf->hw_key_idx, &priv->ucode_key_table);
472 }
473
474 IWL_DEBUG_WEP(priv, "Set dynamic key: cipher=%x len=%d idx=%d sta=%pM ret=%d\n",
Johannes Berga30e3112010-09-22 18:02:01 +0200475 keyconf->cipher, keyconf->keylen, keyconf->keyidx,
Johannes Berg5a3d9882011-07-15 13:03:12 -0700476 sta ? sta->addr : NULL, ret);
Johannes Berga30e3112010-09-22 18:02:01 +0200477
478 return ret;
479}
480
481/**
482 * iwlagn_alloc_bcast_station - add broadcast station into driver's station table.
483 *
484 * This adds the broadcast station into the driver's station table
485 * and marks it driver active, so that it will be restored to the
486 * device at the next best time.
487 */
488int iwlagn_alloc_bcast_station(struct iwl_priv *priv,
489 struct iwl_rxon_context *ctx)
490{
491 struct iwl_link_quality_cmd *link_cmd;
492 unsigned long flags;
493 u8 sta_id;
494
495 spin_lock_irqsave(&priv->sta_lock, flags);
496 sta_id = iwl_prep_station(priv, ctx, iwl_bcast_addr, false, NULL);
497 if (sta_id == IWL_INVALID_STATION) {
498 IWL_ERR(priv, "Unable to prepare broadcast station\n");
499 spin_unlock_irqrestore(&priv->sta_lock, flags);
500
501 return -EINVAL;
502 }
503
504 priv->stations[sta_id].used |= IWL_STA_DRIVER_ACTIVE;
505 priv->stations[sta_id].used |= IWL_STA_BCAST;
506 spin_unlock_irqrestore(&priv->sta_lock, flags);
507
Johannes Bergf775aa02011-06-22 06:34:09 -0700508 link_cmd = iwl_sta_alloc_lq(priv, ctx, sta_id);
Johannes Berga30e3112010-09-22 18:02:01 +0200509 if (!link_cmd) {
510 IWL_ERR(priv,
511 "Unable to initialize rate scaling for bcast station.\n");
512 return -ENOMEM;
513 }
514
515 spin_lock_irqsave(&priv->sta_lock, flags);
516 priv->stations[sta_id].lq = link_cmd;
517 spin_unlock_irqrestore(&priv->sta_lock, flags);
518
519 return 0;
520}
521
522/**
523 * iwl_update_bcast_station - update broadcast station's LQ command
524 *
525 * Only used by iwlagn. Placed here to have all bcast station management
526 * code together.
527 */
Johannes Bergf775aa02011-06-22 06:34:09 -0700528int iwl_update_bcast_station(struct iwl_priv *priv,
529 struct iwl_rxon_context *ctx)
Johannes Berga30e3112010-09-22 18:02:01 +0200530{
531 unsigned long flags;
532 struct iwl_link_quality_cmd *link_cmd;
533 u8 sta_id = ctx->bcast_sta_id;
534
Johannes Bergf775aa02011-06-22 06:34:09 -0700535 link_cmd = iwl_sta_alloc_lq(priv, ctx, sta_id);
Johannes Berga30e3112010-09-22 18:02:01 +0200536 if (!link_cmd) {
537 IWL_ERR(priv, "Unable to initialize rate scaling for bcast station.\n");
538 return -ENOMEM;
539 }
540
541 spin_lock_irqsave(&priv->sta_lock, flags);
542 if (priv->stations[sta_id].lq)
543 kfree(priv->stations[sta_id].lq);
544 else
545 IWL_DEBUG_INFO(priv, "Bcast station rate scaling has not been initialized yet.\n");
546 priv->stations[sta_id].lq = link_cmd;
547 spin_unlock_irqrestore(&priv->sta_lock, flags);
548
549 return 0;
550}
551
552int iwl_update_bcast_stations(struct iwl_priv *priv)
553{
554 struct iwl_rxon_context *ctx;
555 int ret = 0;
556
557 for_each_context(priv, ctx) {
558 ret = iwl_update_bcast_station(priv, ctx);
559 if (ret)
560 break;
561 }
562
563 return ret;
564}
565
566/**
567 * iwl_sta_tx_modify_enable_tid - Enable Tx for this TID in station table
568 */
569int iwl_sta_tx_modify_enable_tid(struct iwl_priv *priv, int sta_id, int tid)
570{
571 unsigned long flags;
572 struct iwl_addsta_cmd sta_cmd;
573
574 lockdep_assert_held(&priv->mutex);
575
576 /* Remove "disable" flag, to enable Tx for this TID */
577 spin_lock_irqsave(&priv->sta_lock, flags);
578 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_TID_DISABLE_TX;
579 priv->stations[sta_id].sta.tid_disable_tx &= cpu_to_le16(~(1 << tid));
580 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
581 memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
582 spin_unlock_irqrestore(&priv->sta_lock, flags);
583
584 return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
585}
586
587int iwl_sta_rx_agg_start(struct iwl_priv *priv, struct ieee80211_sta *sta,
588 int tid, u16 ssn)
589{
590 unsigned long flags;
591 int sta_id;
592 struct iwl_addsta_cmd sta_cmd;
593
594 lockdep_assert_held(&priv->mutex);
595
596 sta_id = iwl_sta_id(sta);
597 if (sta_id == IWL_INVALID_STATION)
598 return -ENXIO;
599
600 spin_lock_irqsave(&priv->sta_lock, flags);
601 priv->stations[sta_id].sta.station_flags_msk = 0;
602 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_ADDBA_TID_MSK;
603 priv->stations[sta_id].sta.add_immediate_ba_tid = (u8)tid;
604 priv->stations[sta_id].sta.add_immediate_ba_ssn = cpu_to_le16(ssn);
605 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
606 memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
607 spin_unlock_irqrestore(&priv->sta_lock, flags);
608
609 return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
610}
611
612int iwl_sta_rx_agg_stop(struct iwl_priv *priv, struct ieee80211_sta *sta,
613 int tid)
614{
615 unsigned long flags;
616 int sta_id;
617 struct iwl_addsta_cmd sta_cmd;
618
619 lockdep_assert_held(&priv->mutex);
620
621 sta_id = iwl_sta_id(sta);
622 if (sta_id == IWL_INVALID_STATION) {
623 IWL_ERR(priv, "Invalid station for AGG tid %d\n", tid);
624 return -ENXIO;
625 }
626
627 spin_lock_irqsave(&priv->sta_lock, flags);
628 priv->stations[sta_id].sta.station_flags_msk = 0;
629 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_DELBA_TID_MSK;
630 priv->stations[sta_id].sta.remove_immediate_ba_tid = (u8)tid;
631 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
632 memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
633 spin_unlock_irqrestore(&priv->sta_lock, flags);
634
635 return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
636}
637
Johannes Bergae79d232010-11-10 09:56:38 -0800638static void iwl_sta_modify_ps_wake(struct iwl_priv *priv, int sta_id)
Johannes Berga30e3112010-09-22 18:02:01 +0200639{
640 unsigned long flags;
641
642 spin_lock_irqsave(&priv->sta_lock, flags);
643 priv->stations[sta_id].sta.station_flags &= ~STA_FLG_PWR_SAVE_MSK;
644 priv->stations[sta_id].sta.station_flags_msk = STA_FLG_PWR_SAVE_MSK;
645 priv->stations[sta_id].sta.sta.modify_mask = 0;
646 priv->stations[sta_id].sta.sleep_tx_count = 0;
647 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
648 iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
649 spin_unlock_irqrestore(&priv->sta_lock, flags);
650
651}
652
653void iwl_sta_modify_sleep_tx_count(struct iwl_priv *priv, int sta_id, int cnt)
654{
655 unsigned long flags;
656
657 spin_lock_irqsave(&priv->sta_lock, flags);
658 priv->stations[sta_id].sta.station_flags |= STA_FLG_PWR_SAVE_MSK;
659 priv->stations[sta_id].sta.station_flags_msk = STA_FLG_PWR_SAVE_MSK;
660 priv->stations[sta_id].sta.sta.modify_mask =
661 STA_MODIFY_SLEEP_TX_COUNT_MSK;
662 priv->stations[sta_id].sta.sleep_tx_count = cpu_to_le16(cnt);
663 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
664 iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
665 spin_unlock_irqrestore(&priv->sta_lock, flags);
666
667}
Johannes Bergae79d232010-11-10 09:56:38 -0800668
669void iwlagn_mac_sta_notify(struct ieee80211_hw *hw,
670 struct ieee80211_vif *vif,
671 enum sta_notify_cmd cmd,
672 struct ieee80211_sta *sta)
673{
674 struct iwl_priv *priv = hw->priv;
675 struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
676 int sta_id;
677
678 switch (cmd) {
679 case STA_NOTIFY_SLEEP:
680 WARN_ON(!sta_priv->client);
681 sta_priv->asleep = true;
682 if (atomic_read(&sta_priv->pending_frames) > 0)
683 ieee80211_sta_block_awake(hw, sta, true);
684 break;
685 case STA_NOTIFY_AWAKE:
686 WARN_ON(!sta_priv->client);
687 if (!sta_priv->asleep)
688 break;
689 sta_priv->asleep = false;
690 sta_id = iwl_sta_id(sta);
691 if (sta_id != IWL_INVALID_STATION)
692 iwl_sta_modify_ps_wake(priv, sta_id);
693 break;
694 default:
695 break;
696 }
697}