blob: c1807fa1d17199214382b58f14a331016bac1cfb [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
Johannes Bergc079166e2011-10-10 07:26:54 -070038void iwl_sta_fill_lq(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
39 u8 sta_id, struct iwl_link_quality_cmd *link_cmd)
Johannes Berga30e3112010-09-22 18:02:01 +020040{
41 int i, r;
Johannes Berga30e3112010-09-22 18:02:01 +020042 u32 rate_flags = 0;
43 __le32 rate_n_flags;
44
Emmanuel Grumbach6ac2f832011-08-25 23:10:44 -070045 lockdep_assert_held(&priv->shrd->mutex);
Johannes Bergf775aa02011-06-22 06:34:09 -070046
Johannes Bergc079166e2011-10-10 07:26:54 -070047 memset(link_cmd, 0, sizeof(*link_cmd));
48
Johannes Berga30e3112010-09-22 18:02:01 +020049 /* Set up the rate scaling to start at selected rate, fall back
50 * all the way down to 1M in IEEE order, and then spin on 1M */
51 if (priv->band == IEEE80211_BAND_5GHZ)
52 r = IWL_RATE_6M_INDEX;
Johannes Bergf775aa02011-06-22 06:34:09 -070053 else if (ctx && ctx->vif && ctx->vif->p2p)
54 r = IWL_RATE_6M_INDEX;
Johannes Berga30e3112010-09-22 18:02:01 +020055 else
56 r = IWL_RATE_1M_INDEX;
57
58 if (r >= IWL_FIRST_CCK_RATE && r <= IWL_LAST_CCK_RATE)
59 rate_flags |= RATE_MCS_CCK_MSK;
60
Emmanuel Grumbachd6189122011-08-25 23:10:39 -070061 rate_flags |= first_antenna(hw_params(priv).valid_tx_ant) <<
Johannes Berga30e3112010-09-22 18:02:01 +020062 RATE_MCS_ANT_POS;
63 rate_n_flags = iwl_hw_set_rate_n_flags(iwl_rates[r].plcp, rate_flags);
64 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
65 link_cmd->rs_table[i].rate_n_flags = rate_n_flags;
66
67 link_cmd->general_params.single_stream_ant_msk =
Emmanuel Grumbachd6189122011-08-25 23:10:39 -070068 first_antenna(hw_params(priv).valid_tx_ant);
Johannes Berga30e3112010-09-22 18:02:01 +020069
70 link_cmd->general_params.dual_stream_ant_msk =
Emmanuel Grumbachd6189122011-08-25 23:10:39 -070071 hw_params(priv).valid_tx_ant &
72 ~first_antenna(hw_params(priv).valid_tx_ant);
Johannes Berga30e3112010-09-22 18:02:01 +020073 if (!link_cmd->general_params.dual_stream_ant_msk) {
74 link_cmd->general_params.dual_stream_ant_msk = ANT_AB;
Emmanuel Grumbachd6189122011-08-25 23:10:39 -070075 } else if (num_of_ant(hw_params(priv).valid_tx_ant) == 2) {
Johannes Berga30e3112010-09-22 18:02:01 +020076 link_cmd->general_params.dual_stream_ant_msk =
Emmanuel Grumbachd6189122011-08-25 23:10:39 -070077 hw_params(priv).valid_tx_ant;
Johannes Berga30e3112010-09-22 18:02:01 +020078 }
79
80 link_cmd->agg_params.agg_dis_start_th = LINK_QUAL_AGG_DISABLE_START_DEF;
81 link_cmd->agg_params.agg_time_limit =
82 cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
83
84 link_cmd->sta_id = sta_id;
Johannes Bergc079166e2011-10-10 07:26:54 -070085}
86
87static struct iwl_link_quality_cmd *
88iwl_sta_alloc_lq(struct iwl_priv *priv, struct iwl_rxon_context *ctx, u8 sta_id)
89{
90 struct iwl_link_quality_cmd *link_cmd;
91
92 link_cmd = kzalloc(sizeof(struct iwl_link_quality_cmd), GFP_KERNEL);
93 if (!link_cmd) {
94 IWL_ERR(priv, "Unable to allocate memory for LQ cmd.\n");
95 return NULL;
96 }
97
98 iwl_sta_fill_lq(priv, ctx, sta_id, link_cmd);
Johannes Berga30e3112010-09-22 18:02:01 +020099
100 return link_cmd;
101}
102
103/*
104 * iwlagn_add_bssid_station - Add the special IBSS BSSID station
105 *
106 * Function sleeps.
107 */
108int iwlagn_add_bssid_station(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
109 const u8 *addr, u8 *sta_id_r)
110{
111 int ret;
112 u8 sta_id;
113 struct iwl_link_quality_cmd *link_cmd;
114 unsigned long flags;
115
116 if (sta_id_r)
117 *sta_id_r = IWL_INVALID_STATION;
118
119 ret = iwl_add_station_common(priv, ctx, addr, 0, NULL, &sta_id);
120 if (ret) {
121 IWL_ERR(priv, "Unable to add station %pM\n", addr);
122 return ret;
123 }
124
125 if (sta_id_r)
126 *sta_id_r = sta_id;
127
Emmanuel Grumbachf39c95e2011-08-25 23:10:47 -0700128 spin_lock_irqsave(&priv->shrd->sta_lock, flags);
Johannes Berga30e3112010-09-22 18:02:01 +0200129 priv->stations[sta_id].used |= IWL_STA_LOCAL;
Emmanuel Grumbachf39c95e2011-08-25 23:10:47 -0700130 spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
Johannes Berga30e3112010-09-22 18:02:01 +0200131
132 /* Set up default rate scaling table in device's station table */
Johannes Bergf775aa02011-06-22 06:34:09 -0700133 link_cmd = iwl_sta_alloc_lq(priv, ctx, sta_id);
Johannes Berga30e3112010-09-22 18:02:01 +0200134 if (!link_cmd) {
135 IWL_ERR(priv, "Unable to initialize rate scaling for station %pM.\n",
136 addr);
137 return -ENOMEM;
138 }
139
140 ret = iwl_send_lq_cmd(priv, ctx, link_cmd, CMD_SYNC, true);
141 if (ret)
142 IWL_ERR(priv, "Link quality command failed (%d)\n", ret);
143
Emmanuel Grumbachf39c95e2011-08-25 23:10:47 -0700144 spin_lock_irqsave(&priv->shrd->sta_lock, flags);
Johannes Berga30e3112010-09-22 18:02:01 +0200145 priv->stations[sta_id].lq = link_cmd;
Emmanuel Grumbachf39c95e2011-08-25 23:10:47 -0700146 spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
Johannes Berga30e3112010-09-22 18:02:01 +0200147
148 return 0;
149}
150
Johannes Berg5a3d9882011-07-15 13:03:12 -0700151/*
152 * static WEP keys
153 *
154 * For each context, the device has a table of 4 static WEP keys
155 * (one for each key index) that is updated with the following
156 * commands.
157 */
158
Johannes Berga30e3112010-09-22 18:02:01 +0200159static int iwl_send_static_wepkey_cmd(struct iwl_priv *priv,
160 struct iwl_rxon_context *ctx,
161 bool send_if_empty)
162{
163 int i, not_empty = 0;
164 u8 buff[sizeof(struct iwl_wep_cmd) +
165 sizeof(struct iwl_wep_key) * WEP_KEYS_MAX];
166 struct iwl_wep_cmd *wep_cmd = (struct iwl_wep_cmd *)buff;
167 size_t cmd_size = sizeof(struct iwl_wep_cmd);
168 struct iwl_host_cmd cmd = {
169 .id = ctx->wep_key_cmd,
Johannes Berg3fa50732011-05-04 07:50:38 -0700170 .data = { wep_cmd, },
Johannes Berga30e3112010-09-22 18:02:01 +0200171 .flags = CMD_SYNC,
172 };
173
174 might_sleep();
175
176 memset(wep_cmd, 0, cmd_size +
177 (sizeof(struct iwl_wep_key) * WEP_KEYS_MAX));
178
179 for (i = 0; i < WEP_KEYS_MAX ; i++) {
180 wep_cmd->key[i].key_index = i;
181 if (ctx->wep_keys[i].key_size) {
182 wep_cmd->key[i].key_offset = i;
183 not_empty = 1;
184 } else {
185 wep_cmd->key[i].key_offset = WEP_INVALID_OFFSET;
186 }
187
188 wep_cmd->key[i].key_size = ctx->wep_keys[i].key_size;
189 memcpy(&wep_cmd->key[i].key[3], ctx->wep_keys[i].key,
190 ctx->wep_keys[i].key_size);
191 }
192
193 wep_cmd->global_key_type = WEP_KEY_WEP_TYPE;
194 wep_cmd->num_keys = WEP_KEYS_MAX;
195
196 cmd_size += sizeof(struct iwl_wep_key) * WEP_KEYS_MAX;
197
Johannes Berg3fa50732011-05-04 07:50:38 -0700198 cmd.len[0] = cmd_size;
Johannes Berga30e3112010-09-22 18:02:01 +0200199
200 if (not_empty || send_if_empty)
Emmanuel Grumbache6bb4c92011-08-25 23:10:48 -0700201 return iwl_trans_send_cmd(trans(priv), &cmd);
Johannes Berga30e3112010-09-22 18:02:01 +0200202 else
203 return 0;
204}
205
206int iwl_restore_default_wep_keys(struct iwl_priv *priv,
207 struct iwl_rxon_context *ctx)
208{
Emmanuel Grumbach6ac2f832011-08-25 23:10:44 -0700209 lockdep_assert_held(&priv->shrd->mutex);
Johannes Berga30e3112010-09-22 18:02:01 +0200210
211 return iwl_send_static_wepkey_cmd(priv, ctx, false);
212}
213
214int iwl_remove_default_wep_key(struct iwl_priv *priv,
215 struct iwl_rxon_context *ctx,
216 struct ieee80211_key_conf *keyconf)
217{
218 int ret;
219
Emmanuel Grumbach6ac2f832011-08-25 23:10:44 -0700220 lockdep_assert_held(&priv->shrd->mutex);
Johannes Berga30e3112010-09-22 18:02:01 +0200221
222 IWL_DEBUG_WEP(priv, "Removing default WEP key: idx=%d\n",
223 keyconf->keyidx);
224
225 memset(&ctx->wep_keys[keyconf->keyidx], 0, sizeof(ctx->wep_keys[0]));
Emmanuel Grumbach845a9c02011-08-25 23:11:04 -0700226 if (iwl_is_rfkill(priv->shrd)) {
Johannes Berga30e3112010-09-22 18:02:01 +0200227 IWL_DEBUG_WEP(priv, "Not sending REPLY_WEPKEY command due to RFKILL.\n");
228 /* but keys in device are clear anyway so return success */
229 return 0;
230 }
231 ret = iwl_send_static_wepkey_cmd(priv, ctx, 1);
232 IWL_DEBUG_WEP(priv, "Remove default WEP key: idx=%d ret=%d\n",
233 keyconf->keyidx, ret);
234
235 return ret;
236}
237
238int iwl_set_default_wep_key(struct iwl_priv *priv,
239 struct iwl_rxon_context *ctx,
240 struct ieee80211_key_conf *keyconf)
241{
242 int ret;
243
Emmanuel Grumbach6ac2f832011-08-25 23:10:44 -0700244 lockdep_assert_held(&priv->shrd->mutex);
Johannes Berga30e3112010-09-22 18:02:01 +0200245
246 if (keyconf->keylen != WEP_KEY_LEN_128 &&
247 keyconf->keylen != WEP_KEY_LEN_64) {
248 IWL_DEBUG_WEP(priv, "Bad WEP key length %d\n", keyconf->keylen);
249 return -EINVAL;
250 }
251
Johannes Berg5a3d9882011-07-15 13:03:12 -0700252 keyconf->hw_key_idx = IWLAGN_HW_KEY_DEFAULT;
Johannes Berga30e3112010-09-22 18:02:01 +0200253
254 ctx->wep_keys[keyconf->keyidx].key_size = keyconf->keylen;
255 memcpy(&ctx->wep_keys[keyconf->keyidx].key, &keyconf->key,
256 keyconf->keylen);
257
258 ret = iwl_send_static_wepkey_cmd(priv, ctx, false);
259 IWL_DEBUG_WEP(priv, "Set default WEP key: len=%d idx=%d ret=%d\n",
260 keyconf->keylen, keyconf->keyidx, ret);
261
262 return ret;
263}
264
Johannes Berg5a3d9882011-07-15 13:03:12 -0700265/*
266 * dynamic (per-station) keys
267 *
268 * The dynamic keys are a little more complicated. The device has
269 * a key cache of up to STA_KEY_MAX_NUM/STA_KEY_MAX_NUM_PAN keys.
270 * These are linked to stations by a table that contains an index
271 * into the key table for each station/key index/{mcast,unicast},
272 * i.e. it's basically an array of pointers like this:
273 * key_offset_t key_mapping[NUM_STATIONS][4][2];
274 * (it really works differently, but you can think of it as such)
275 *
276 * The key uploading and linking happens in the same command, the
277 * add station command with STA_MODIFY_KEY_MASK.
278 */
279
280static u8 iwlagn_key_sta_id(struct iwl_priv *priv,
281 struct ieee80211_vif *vif,
282 struct ieee80211_sta *sta)
283{
284 struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
285 u8 sta_id = IWL_INVALID_STATION;
286
287 if (sta)
288 sta_id = iwl_sta_id(sta);
289
290 /*
291 * The device expects GTKs for station interfaces to be
292 * installed as GTKs for the AP station. If we have no
293 * station ID, then use the ap_sta_id in that case.
294 */
295 if (!sta && vif && vif_priv->ctx) {
296 switch (vif->type) {
297 case NL80211_IFTYPE_STATION:
298 sta_id = vif_priv->ctx->ap_sta_id;
299 break;
300 default:
301 /*
302 * In all other cases, the key will be
303 * used either for TX only or is bound
304 * to a station already.
305 */
306 break;
307 }
308 }
309
310 return sta_id;
311}
312
Johannes Berge1b1c082011-07-17 01:44:11 -0700313static int iwlagn_send_sta_key(struct iwl_priv *priv,
314 struct ieee80211_key_conf *keyconf,
315 u8 sta_id, u32 tkip_iv32, u16 *tkip_p1k,
316 u32 cmd_flags)
Johannes Berga30e3112010-09-22 18:02:01 +0200317{
318 unsigned long flags;
Johannes Berg5a3d9882011-07-15 13:03:12 -0700319 __le16 key_flags;
Johannes Berga30e3112010-09-22 18:02:01 +0200320 struct iwl_addsta_cmd sta_cmd;
Johannes Berg5a3d9882011-07-15 13:03:12 -0700321 int i;
Johannes Berge1b1c082011-07-17 01:44:11 -0700322
Emmanuel Grumbachf39c95e2011-08-25 23:10:47 -0700323 spin_lock_irqsave(&priv->shrd->sta_lock, flags);
Johannes Berg5a3d9882011-07-15 13:03:12 -0700324 memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(sta_cmd));
Emmanuel Grumbachf39c95e2011-08-25 23:10:47 -0700325 spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
Johannes Berga30e3112010-09-22 18:02:01 +0200326
Johannes Berg5a3d9882011-07-15 13:03:12 -0700327 key_flags = cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
328 key_flags |= STA_KEY_FLG_MAP_KEY_MSK;
Johannes Berga30e3112010-09-22 18:02:01 +0200329
Johannes Berg5a3d9882011-07-15 13:03:12 -0700330 switch (keyconf->cipher) {
331 case WLAN_CIPHER_SUITE_CCMP:
332 key_flags |= STA_KEY_FLG_CCMP;
333 memcpy(sta_cmd.key.key, keyconf->key, keyconf->keylen);
334 break;
335 case WLAN_CIPHER_SUITE_TKIP:
336 key_flags |= STA_KEY_FLG_TKIP;
337 sta_cmd.key.tkip_rx_tsc_byte2 = tkip_iv32;
338 for (i = 0; i < 5; i++)
339 sta_cmd.key.tkip_rx_ttak[i] = cpu_to_le16(tkip_p1k[i]);
340 memcpy(sta_cmd.key.key, keyconf->key, keyconf->keylen);
341 break;
342 case WLAN_CIPHER_SUITE_WEP104:
Johannes Berga30e3112010-09-22 18:02:01 +0200343 key_flags |= STA_KEY_FLG_KEY_SIZE_MSK;
Johannes Berg5a3d9882011-07-15 13:03:12 -0700344 /* fall through */
345 case WLAN_CIPHER_SUITE_WEP40:
346 key_flags |= STA_KEY_FLG_WEP;
347 memcpy(&sta_cmd.key.key[3], keyconf->key, keyconf->keylen);
348 break;
349 default:
350 WARN_ON(1);
351 return -EINVAL;
352 }
Johannes Berga30e3112010-09-22 18:02:01 +0200353
Johannes Berg5a3d9882011-07-15 13:03:12 -0700354 if (!(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE))
Johannes Berga30e3112010-09-22 18:02:01 +0200355 key_flags |= STA_KEY_MULTICAST_MSK;
356
Johannes Berg5a3d9882011-07-15 13:03:12 -0700357 /* key pointer (offset) */
358 sta_cmd.key.key_offset = keyconf->hw_key_idx;
Johannes Berga30e3112010-09-22 18:02:01 +0200359
Johannes Berg5a3d9882011-07-15 13:03:12 -0700360 sta_cmd.key.key_flags = key_flags;
361 sta_cmd.mode = STA_CONTROL_MODIFY_MSK;
362 sta_cmd.sta.modify_mask = STA_MODIFY_KEY_MASK;
Johannes Berga30e3112010-09-22 18:02:01 +0200363
Johannes Berg5a3d9882011-07-15 13:03:12 -0700364 return iwl_send_add_sta(priv, &sta_cmd, cmd_flags);
Johannes Berga30e3112010-09-22 18:02:01 +0200365}
366
367void iwl_update_tkip_key(struct iwl_priv *priv,
Johannes Berg5a3d9882011-07-15 13:03:12 -0700368 struct ieee80211_vif *vif,
Johannes Berga30e3112010-09-22 18:02:01 +0200369 struct ieee80211_key_conf *keyconf,
370 struct ieee80211_sta *sta, u32 iv32, u16 *phase1key)
371{
Johannes Berg5a3d9882011-07-15 13:03:12 -0700372 u8 sta_id = iwlagn_key_sta_id(priv, vif, sta);
373
374 if (sta_id == IWL_INVALID_STATION)
375 return;
Johannes Berga30e3112010-09-22 18:02:01 +0200376
377 if (iwl_scan_cancel(priv)) {
378 /* cancel scan failed, just live w/ bad key and rely
379 briefly on SW decryption */
380 return;
381 }
382
Johannes Berge1b1c082011-07-17 01:44:11 -0700383 iwlagn_send_sta_key(priv, keyconf, sta_id,
384 iv32, phase1key, CMD_ASYNC);
Johannes Berga30e3112010-09-22 18:02:01 +0200385}
386
387int iwl_remove_dynamic_key(struct iwl_priv *priv,
388 struct iwl_rxon_context *ctx,
389 struct ieee80211_key_conf *keyconf,
Johannes Berg5a3d9882011-07-15 13:03:12 -0700390 struct ieee80211_sta *sta)
Johannes Berga30e3112010-09-22 18:02:01 +0200391{
392 unsigned long flags;
Johannes Berga30e3112010-09-22 18:02:01 +0200393 struct iwl_addsta_cmd sta_cmd;
Johannes Berg5a3d9882011-07-15 13:03:12 -0700394 u8 sta_id = iwlagn_key_sta_id(priv, ctx->vif, sta);
395
396 /* if station isn't there, neither is the key */
397 if (sta_id == IWL_INVALID_STATION)
398 return -ENOENT;
399
Emmanuel Grumbachf39c95e2011-08-25 23:10:47 -0700400 spin_lock_irqsave(&priv->shrd->sta_lock, flags);
Johannes Berg5a3d9882011-07-15 13:03:12 -0700401 memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(sta_cmd));
402 if (!(priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE))
403 sta_id = IWL_INVALID_STATION;
Emmanuel Grumbachf39c95e2011-08-25 23:10:47 -0700404 spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
Johannes Berg5a3d9882011-07-15 13:03:12 -0700405
406 if (sta_id == IWL_INVALID_STATION)
407 return 0;
Johannes Berga30e3112010-09-22 18:02:01 +0200408
Emmanuel Grumbach6ac2f832011-08-25 23:10:44 -0700409 lockdep_assert_held(&priv->shrd->mutex);
Johannes Berga30e3112010-09-22 18:02:01 +0200410
411 ctx->key_mapping_keys--;
412
Johannes Berga30e3112010-09-22 18:02:01 +0200413 IWL_DEBUG_WEP(priv, "Remove dynamic key: idx=%d sta=%d\n",
414 keyconf->keyidx, sta_id);
415
Johannes Berg5a3d9882011-07-15 13:03:12 -0700416 if (!test_and_clear_bit(keyconf->hw_key_idx, &priv->ucode_key_table))
417 IWL_ERR(priv, "offset %d not used in uCode key table.\n",
418 keyconf->hw_key_idx);
Johannes Berga30e3112010-09-22 18:02:01 +0200419
Johannes Berg5a3d9882011-07-15 13:03:12 -0700420 sta_cmd.key.key_flags = STA_KEY_FLG_NO_ENC | STA_KEY_FLG_INVALID;
421 sta_cmd.key.key_offset = WEP_INVALID_OFFSET;
422 sta_cmd.sta.modify_mask = STA_MODIFY_KEY_MASK;
423 sta_cmd.mode = STA_CONTROL_MODIFY_MSK;
Johannes Berga30e3112010-09-22 18:02:01 +0200424
425 return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
426}
427
Johannes Berg5a3d9882011-07-15 13:03:12 -0700428int iwl_set_dynamic_key(struct iwl_priv *priv,
429 struct iwl_rxon_context *ctx,
430 struct ieee80211_key_conf *keyconf,
431 struct ieee80211_sta *sta)
Johannes Berga30e3112010-09-22 18:02:01 +0200432{
Johannes Berg5a3d9882011-07-15 13:03:12 -0700433 struct ieee80211_key_seq seq;
434 u16 p1k[5];
Johannes Berga30e3112010-09-22 18:02:01 +0200435 int ret;
Johannes Berg5a3d9882011-07-15 13:03:12 -0700436 u8 sta_id = iwlagn_key_sta_id(priv, ctx->vif, sta);
437 const u8 *addr;
438
439 if (sta_id == IWL_INVALID_STATION)
440 return -EINVAL;
Johannes Berga30e3112010-09-22 18:02:01 +0200441
Emmanuel Grumbach6ac2f832011-08-25 23:10:44 -0700442 lockdep_assert_held(&priv->shrd->mutex);
Johannes Berga30e3112010-09-22 18:02:01 +0200443
Johannes Berg5a3d9882011-07-15 13:03:12 -0700444 keyconf->hw_key_idx = iwl_get_free_ucode_key_offset(priv);
445 if (keyconf->hw_key_idx == WEP_INVALID_OFFSET)
446 return -ENOSPC;
447
Johannes Berga30e3112010-09-22 18:02:01 +0200448 ctx->key_mapping_keys++;
Johannes Berga30e3112010-09-22 18:02:01 +0200449
450 switch (keyconf->cipher) {
Johannes Berga30e3112010-09-22 18:02:01 +0200451 case WLAN_CIPHER_SUITE_TKIP:
Johannes Berg5a3d9882011-07-15 13:03:12 -0700452 keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
453 keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
454
455 if (sta)
456 addr = sta->addr;
457 else /* station mode case only */
458 addr = ctx->active.bssid_addr;
459
460 /* pre-fill phase 1 key into device cache */
461 ieee80211_get_key_rx_seq(keyconf, 0, &seq);
462 ieee80211_get_tkip_rx_p1k(keyconf, addr, seq.tkip.iv32, p1k);
Johannes Berge1b1c082011-07-17 01:44:11 -0700463 ret = iwlagn_send_sta_key(priv, keyconf, sta_id,
464 seq.tkip.iv32, p1k, CMD_SYNC);
Johannes Berga30e3112010-09-22 18:02:01 +0200465 break;
Johannes Berg5a3d9882011-07-15 13:03:12 -0700466 case WLAN_CIPHER_SUITE_CCMP:
467 keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
468 /* fall through */
Johannes Berga30e3112010-09-22 18:02:01 +0200469 case WLAN_CIPHER_SUITE_WEP40:
470 case WLAN_CIPHER_SUITE_WEP104:
Johannes Berge1b1c082011-07-17 01:44:11 -0700471 ret = iwlagn_send_sta_key(priv, keyconf, sta_id,
472 0, NULL, CMD_SYNC);
Johannes Berga30e3112010-09-22 18:02:01 +0200473 break;
474 default:
Johannes Berg5a3d9882011-07-15 13:03:12 -0700475 IWL_ERR(priv, "Unknown cipher %x\n", keyconf->cipher);
Johannes Berga30e3112010-09-22 18:02:01 +0200476 ret = -EINVAL;
477 }
478
Johannes Berg5a3d9882011-07-15 13:03:12 -0700479 if (ret) {
480 ctx->key_mapping_keys--;
481 clear_bit(keyconf->hw_key_idx, &priv->ucode_key_table);
482 }
483
484 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 +0200485 keyconf->cipher, keyconf->keylen, keyconf->keyidx,
Johannes Berg5a3d9882011-07-15 13:03:12 -0700486 sta ? sta->addr : NULL, ret);
Johannes Berga30e3112010-09-22 18:02:01 +0200487
488 return ret;
489}
490
491/**
492 * iwlagn_alloc_bcast_station - add broadcast station into driver's station table.
493 *
494 * This adds the broadcast station into the driver's station table
495 * and marks it driver active, so that it will be restored to the
496 * device at the next best time.
497 */
498int iwlagn_alloc_bcast_station(struct iwl_priv *priv,
499 struct iwl_rxon_context *ctx)
500{
501 struct iwl_link_quality_cmd *link_cmd;
502 unsigned long flags;
503 u8 sta_id;
504
Emmanuel Grumbachf39c95e2011-08-25 23:10:47 -0700505 spin_lock_irqsave(&priv->shrd->sta_lock, flags);
Johannes Berga30e3112010-09-22 18:02:01 +0200506 sta_id = iwl_prep_station(priv, ctx, iwl_bcast_addr, false, NULL);
507 if (sta_id == IWL_INVALID_STATION) {
508 IWL_ERR(priv, "Unable to prepare broadcast station\n");
Emmanuel Grumbachf39c95e2011-08-25 23:10:47 -0700509 spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
Johannes Berga30e3112010-09-22 18:02:01 +0200510
511 return -EINVAL;
512 }
513
514 priv->stations[sta_id].used |= IWL_STA_DRIVER_ACTIVE;
515 priv->stations[sta_id].used |= IWL_STA_BCAST;
Emmanuel Grumbachf39c95e2011-08-25 23:10:47 -0700516 spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
Johannes Berga30e3112010-09-22 18:02:01 +0200517
Johannes Bergf775aa02011-06-22 06:34:09 -0700518 link_cmd = iwl_sta_alloc_lq(priv, ctx, sta_id);
Johannes Berga30e3112010-09-22 18:02:01 +0200519 if (!link_cmd) {
520 IWL_ERR(priv,
521 "Unable to initialize rate scaling for bcast station.\n");
522 return -ENOMEM;
523 }
524
Emmanuel Grumbachf39c95e2011-08-25 23:10:47 -0700525 spin_lock_irqsave(&priv->shrd->sta_lock, flags);
Johannes Berga30e3112010-09-22 18:02:01 +0200526 priv->stations[sta_id].lq = link_cmd;
Emmanuel Grumbachf39c95e2011-08-25 23:10:47 -0700527 spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
Johannes Berga30e3112010-09-22 18:02:01 +0200528
529 return 0;
530}
531
532/**
533 * iwl_update_bcast_station - update broadcast station's LQ command
534 *
535 * Only used by iwlagn. Placed here to have all bcast station management
536 * code together.
537 */
Johannes Bergf775aa02011-06-22 06:34:09 -0700538int iwl_update_bcast_station(struct iwl_priv *priv,
539 struct iwl_rxon_context *ctx)
Johannes Berga30e3112010-09-22 18:02:01 +0200540{
541 unsigned long flags;
542 struct iwl_link_quality_cmd *link_cmd;
543 u8 sta_id = ctx->bcast_sta_id;
544
Johannes Bergf775aa02011-06-22 06:34:09 -0700545 link_cmd = iwl_sta_alloc_lq(priv, ctx, sta_id);
Johannes Berga30e3112010-09-22 18:02:01 +0200546 if (!link_cmd) {
547 IWL_ERR(priv, "Unable to initialize rate scaling for bcast station.\n");
548 return -ENOMEM;
549 }
550
Emmanuel Grumbachf39c95e2011-08-25 23:10:47 -0700551 spin_lock_irqsave(&priv->shrd->sta_lock, flags);
Johannes Berga30e3112010-09-22 18:02:01 +0200552 if (priv->stations[sta_id].lq)
553 kfree(priv->stations[sta_id].lq);
554 else
555 IWL_DEBUG_INFO(priv, "Bcast station rate scaling has not been initialized yet.\n");
556 priv->stations[sta_id].lq = link_cmd;
Emmanuel Grumbachf39c95e2011-08-25 23:10:47 -0700557 spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
Johannes Berga30e3112010-09-22 18:02:01 +0200558
559 return 0;
560}
561
562int iwl_update_bcast_stations(struct iwl_priv *priv)
563{
564 struct iwl_rxon_context *ctx;
565 int ret = 0;
566
567 for_each_context(priv, ctx) {
568 ret = iwl_update_bcast_station(priv, ctx);
569 if (ret)
570 break;
571 }
572
573 return ret;
574}
575
576/**
577 * iwl_sta_tx_modify_enable_tid - Enable Tx for this TID in station table
578 */
579int iwl_sta_tx_modify_enable_tid(struct iwl_priv *priv, int sta_id, int tid)
580{
581 unsigned long flags;
582 struct iwl_addsta_cmd sta_cmd;
583
Emmanuel Grumbach6ac2f832011-08-25 23:10:44 -0700584 lockdep_assert_held(&priv->shrd->mutex);
Johannes Berga30e3112010-09-22 18:02:01 +0200585
586 /* Remove "disable" flag, to enable Tx for this TID */
Emmanuel Grumbachf39c95e2011-08-25 23:10:47 -0700587 spin_lock_irqsave(&priv->shrd->sta_lock, flags);
Johannes Berga30e3112010-09-22 18:02:01 +0200588 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_TID_DISABLE_TX;
589 priv->stations[sta_id].sta.tid_disable_tx &= cpu_to_le16(~(1 << tid));
590 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
591 memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
Emmanuel Grumbachf39c95e2011-08-25 23:10:47 -0700592 spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
Johannes Berga30e3112010-09-22 18:02:01 +0200593
594 return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
595}
596
597int iwl_sta_rx_agg_start(struct iwl_priv *priv, struct ieee80211_sta *sta,
598 int tid, u16 ssn)
599{
600 unsigned long flags;
601 int sta_id;
602 struct iwl_addsta_cmd sta_cmd;
603
Emmanuel Grumbach6ac2f832011-08-25 23:10:44 -0700604 lockdep_assert_held(&priv->shrd->mutex);
Johannes Berga30e3112010-09-22 18:02:01 +0200605
606 sta_id = iwl_sta_id(sta);
607 if (sta_id == IWL_INVALID_STATION)
608 return -ENXIO;
609
Emmanuel Grumbachf39c95e2011-08-25 23:10:47 -0700610 spin_lock_irqsave(&priv->shrd->sta_lock, flags);
Johannes Berga30e3112010-09-22 18:02:01 +0200611 priv->stations[sta_id].sta.station_flags_msk = 0;
612 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_ADDBA_TID_MSK;
613 priv->stations[sta_id].sta.add_immediate_ba_tid = (u8)tid;
614 priv->stations[sta_id].sta.add_immediate_ba_ssn = cpu_to_le16(ssn);
615 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
616 memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
Emmanuel Grumbachf39c95e2011-08-25 23:10:47 -0700617 spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
Johannes Berga30e3112010-09-22 18:02:01 +0200618
619 return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
620}
621
622int iwl_sta_rx_agg_stop(struct iwl_priv *priv, struct ieee80211_sta *sta,
623 int tid)
624{
625 unsigned long flags;
626 int sta_id;
627 struct iwl_addsta_cmd sta_cmd;
628
Emmanuel Grumbach6ac2f832011-08-25 23:10:44 -0700629 lockdep_assert_held(&priv->shrd->mutex);
Johannes Berga30e3112010-09-22 18:02:01 +0200630
631 sta_id = iwl_sta_id(sta);
632 if (sta_id == IWL_INVALID_STATION) {
633 IWL_ERR(priv, "Invalid station for AGG tid %d\n", tid);
634 return -ENXIO;
635 }
636
Emmanuel Grumbachf39c95e2011-08-25 23:10:47 -0700637 spin_lock_irqsave(&priv->shrd->sta_lock, flags);
Johannes Berga30e3112010-09-22 18:02:01 +0200638 priv->stations[sta_id].sta.station_flags_msk = 0;
639 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_DELBA_TID_MSK;
640 priv->stations[sta_id].sta.remove_immediate_ba_tid = (u8)tid;
641 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
642 memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
Emmanuel Grumbachf39c95e2011-08-25 23:10:47 -0700643 spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
Johannes Berga30e3112010-09-22 18:02:01 +0200644
645 return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
646}
647
Johannes Bergae79d232010-11-10 09:56:38 -0800648static void iwl_sta_modify_ps_wake(struct iwl_priv *priv, int sta_id)
Johannes Berga30e3112010-09-22 18:02:01 +0200649{
650 unsigned long flags;
651
Emmanuel Grumbachf39c95e2011-08-25 23:10:47 -0700652 spin_lock_irqsave(&priv->shrd->sta_lock, flags);
Johannes Berga30e3112010-09-22 18:02:01 +0200653 priv->stations[sta_id].sta.station_flags &= ~STA_FLG_PWR_SAVE_MSK;
654 priv->stations[sta_id].sta.station_flags_msk = STA_FLG_PWR_SAVE_MSK;
655 priv->stations[sta_id].sta.sta.modify_mask = 0;
656 priv->stations[sta_id].sta.sleep_tx_count = 0;
657 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
658 iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
Emmanuel Grumbachf39c95e2011-08-25 23:10:47 -0700659 spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
Johannes Berga30e3112010-09-22 18:02:01 +0200660
661}
662
663void iwl_sta_modify_sleep_tx_count(struct iwl_priv *priv, int sta_id, int cnt)
664{
665 unsigned long flags;
666
Emmanuel Grumbachf39c95e2011-08-25 23:10:47 -0700667 spin_lock_irqsave(&priv->shrd->sta_lock, flags);
Johannes Berga30e3112010-09-22 18:02:01 +0200668 priv->stations[sta_id].sta.station_flags |= STA_FLG_PWR_SAVE_MSK;
669 priv->stations[sta_id].sta.station_flags_msk = STA_FLG_PWR_SAVE_MSK;
670 priv->stations[sta_id].sta.sta.modify_mask =
671 STA_MODIFY_SLEEP_TX_COUNT_MSK;
672 priv->stations[sta_id].sta.sleep_tx_count = cpu_to_le16(cnt);
673 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
674 iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
Emmanuel Grumbachf39c95e2011-08-25 23:10:47 -0700675 spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
Johannes Berga30e3112010-09-22 18:02:01 +0200676
677}
Johannes Bergae79d232010-11-10 09:56:38 -0800678
679void iwlagn_mac_sta_notify(struct ieee80211_hw *hw,
680 struct ieee80211_vif *vif,
681 enum sta_notify_cmd cmd,
682 struct ieee80211_sta *sta)
683{
684 struct iwl_priv *priv = hw->priv;
685 struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
686 int sta_id;
687
Wey-Yi Guy770c72c2011-10-10 07:27:10 -0700688 IWL_DEBUG_MAC80211(priv, "enter\n");
689
Johannes Bergae79d232010-11-10 09:56:38 -0800690 switch (cmd) {
691 case STA_NOTIFY_SLEEP:
692 WARN_ON(!sta_priv->client);
693 sta_priv->asleep = true;
694 if (atomic_read(&sta_priv->pending_frames) > 0)
695 ieee80211_sta_block_awake(hw, sta, true);
696 break;
697 case STA_NOTIFY_AWAKE:
698 WARN_ON(!sta_priv->client);
699 if (!sta_priv->asleep)
700 break;
701 sta_priv->asleep = false;
702 sta_id = iwl_sta_id(sta);
703 if (sta_id != IWL_INVALID_STATION)
704 iwl_sta_modify_ps_wake(priv, sta_id);
705 break;
706 default:
707 break;
708 }
Wey-Yi Guy770c72c2011-10-10 07:27:10 -0700709 IWL_DEBUG_MAC80211(priv, "leave\n");
Johannes Bergae79d232010-11-10 09:56:38 -0800710}