1 /******************************************************************************
3 * Copyright(c) 2003 - 2008 Intel Corporation. All rights reserved.
5 * Portions of this file are derived from the ipw3945 project, as well
6 * as portions of the ieee80211 subsystem header files.
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.
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
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
21 * The full GNU General Public License is included in this distribution in the
22 * file called LICENSE.
24 * Contact Information:
25 * James P. Ketrenos <ipw2100-admin@linux.intel.com>
26 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
28 *****************************************************************************/
30 #include <net/mac80211.h>
31 #include <linux/etherdevice.h>
33 #include "iwl-eeprom.h"
38 #include "iwl-helpers.h"
42 u8 iwl_find_station(struct iwl_priv *priv, const u8 *addr)
46 int ret = IWL_INVALID_STATION;
50 if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) ||
51 (priv->iw_mode == IEEE80211_IF_TYPE_AP))
54 if (is_broadcast_ether_addr(addr))
55 return priv->hw_params.bcast_sta_id;
57 spin_lock_irqsave(&priv->sta_lock, flags);
58 for (i = start; i < priv->hw_params.max_stations; i++)
59 if (priv->stations[i].used &&
60 (!compare_ether_addr(priv->stations[i].sta.sta.addr,
66 IWL_DEBUG_ASSOC_LIMIT("can not find STA %s total %d\n",
67 print_mac(mac, addr), priv->num_stations);
70 spin_unlock_irqrestore(&priv->sta_lock, flags);
73 EXPORT_SYMBOL(iwl_find_station);
76 int iwl_get_free_ucode_key_index(struct iwl_priv *priv)
80 for (i = 0; i < STA_KEY_MAX_NUM; i++)
81 if (!test_and_set_bit(i, &priv->ucode_key_table))
87 int iwl_send_static_wepkey_cmd(struct iwl_priv *priv, u8 send_if_empty)
90 u8 buff[sizeof(struct iwl_wep_cmd) +
91 sizeof(struct iwl_wep_key) * WEP_KEYS_MAX];
92 struct iwl_wep_cmd *wep_cmd = (struct iwl_wep_cmd *)buff;
93 size_t cmd_size = sizeof(struct iwl_wep_cmd);
94 struct iwl_host_cmd cmd = {
97 .meta.flags = CMD_ASYNC,
100 memset(wep_cmd, 0, cmd_size +
101 (sizeof(struct iwl_wep_key) * WEP_KEYS_MAX));
103 for (i = 0; i < WEP_KEYS_MAX ; i++) {
104 wep_cmd->key[i].key_index = i;
105 if (priv->wep_keys[i].key_size) {
106 wep_cmd->key[i].key_offset = i;
109 wep_cmd->key[i].key_offset = WEP_INVALID_OFFSET;
112 wep_cmd->key[i].key_size = priv->wep_keys[i].key_size;
113 memcpy(&wep_cmd->key[i].key[3], priv->wep_keys[i].key,
114 priv->wep_keys[i].key_size);
117 wep_cmd->global_key_type = WEP_KEY_WEP_TYPE;
118 wep_cmd->num_keys = WEP_KEYS_MAX;
120 cmd_size += sizeof(struct iwl_wep_key) * WEP_KEYS_MAX;
124 if (not_empty || send_if_empty)
125 return iwl_send_cmd(priv, &cmd);
130 int iwl_remove_default_wep_key(struct iwl_priv *priv,
131 struct ieee80211_key_conf *keyconf)
136 spin_lock_irqsave(&priv->sta_lock, flags);
138 if (!test_and_clear_bit(keyconf->keyidx, &priv->ucode_key_table))
139 IWL_ERROR("index %d not used in uCode key table.\n",
142 priv->default_wep_key--;
143 memset(&priv->wep_keys[keyconf->keyidx], 0, sizeof(priv->wep_keys[0]));
144 ret = iwl_send_static_wepkey_cmd(priv, 1);
145 spin_unlock_irqrestore(&priv->sta_lock, flags);
150 int iwl_set_default_wep_key(struct iwl_priv *priv,
151 struct ieee80211_key_conf *keyconf)
156 keyconf->flags &= ~IEEE80211_KEY_FLAG_GENERATE_IV;
157 keyconf->hw_key_idx = keyconf->keyidx;
158 priv->stations[IWL_AP_ID].keyinfo.alg = ALG_WEP;
160 spin_lock_irqsave(&priv->sta_lock, flags);
161 priv->default_wep_key++;
163 if (test_and_set_bit(keyconf->keyidx, &priv->ucode_key_table))
164 IWL_ERROR("index %d already used in uCode key table.\n",
167 priv->wep_keys[keyconf->keyidx].key_size = keyconf->keylen;
168 memcpy(&priv->wep_keys[keyconf->keyidx].key, &keyconf->key,
171 ret = iwl_send_static_wepkey_cmd(priv, 0);
172 spin_unlock_irqrestore(&priv->sta_lock, flags);
177 static int iwl_set_wep_dynamic_key_info(struct iwl_priv *priv,
178 struct ieee80211_key_conf *keyconf,
182 __le16 key_flags = 0;
185 keyconf->flags &= ~IEEE80211_KEY_FLAG_GENERATE_IV;
186 keyconf->hw_key_idx = keyconf->keyidx;
188 key_flags |= (STA_KEY_FLG_WEP | STA_KEY_FLG_MAP_KEY_MSK);
189 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
190 key_flags &= ~STA_KEY_FLG_INVALID;
192 if (keyconf->keylen == WEP_KEY_LEN_128)
193 key_flags |= STA_KEY_FLG_KEY_SIZE_MSK;
195 if (sta_id == priv->hw_params.bcast_sta_id)
196 key_flags |= STA_KEY_MULTICAST_MSK;
198 spin_lock_irqsave(&priv->sta_lock, flags);
200 priv->stations[sta_id].keyinfo.alg = keyconf->alg;
201 priv->stations[sta_id].keyinfo.keylen = keyconf->keylen;
202 priv->stations[sta_id].keyinfo.keyidx = keyconf->keyidx;
204 memcpy(priv->stations[sta_id].keyinfo.key,
205 keyconf->key, keyconf->keylen);
207 memcpy(&priv->stations[sta_id].sta.key.key[3],
208 keyconf->key, keyconf->keylen);
210 if ((priv->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
211 == STA_KEY_FLG_NO_ENC)
212 priv->stations[sta_id].sta.key.key_offset =
213 iwl_get_free_ucode_key_index(priv);
214 /* else, we are overriding an existing key => no need to allocated room
217 priv->stations[sta_id].sta.key.key_flags = key_flags;
218 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
219 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
221 ret = iwl4965_send_add_station(priv,
222 &priv->stations[sta_id].sta, CMD_ASYNC);
224 spin_unlock_irqrestore(&priv->sta_lock, flags);
229 static int iwl_set_ccmp_dynamic_key_info(struct iwl_priv *priv,
230 struct ieee80211_key_conf *keyconf,
234 __le16 key_flags = 0;
236 key_flags |= (STA_KEY_FLG_CCMP | STA_KEY_FLG_MAP_KEY_MSK);
237 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
238 key_flags &= ~STA_KEY_FLG_INVALID;
240 if (sta_id == priv->hw_params.bcast_sta_id)
241 key_flags |= STA_KEY_MULTICAST_MSK;
243 keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
244 keyconf->hw_key_idx = keyconf->keyidx;
246 spin_lock_irqsave(&priv->sta_lock, flags);
247 priv->stations[sta_id].keyinfo.alg = keyconf->alg;
248 priv->stations[sta_id].keyinfo.keylen = keyconf->keylen;
250 memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key,
253 memcpy(priv->stations[sta_id].sta.key.key, keyconf->key,
256 if ((priv->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
257 == STA_KEY_FLG_NO_ENC)
258 priv->stations[sta_id].sta.key.key_offset =
259 iwl_get_free_ucode_key_index(priv);
260 /* else, we are overriding an existing key => no need to allocated room
263 priv->stations[sta_id].sta.key.key_flags = key_flags;
264 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
265 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
267 spin_unlock_irqrestore(&priv->sta_lock, flags);
269 IWL_DEBUG_INFO("hwcrypto: modify ucode station key info\n");
270 return iwl4965_send_add_station(priv,
271 &priv->stations[sta_id].sta, CMD_ASYNC);
274 static int iwl_set_tkip_dynamic_key_info(struct iwl_priv *priv,
275 struct ieee80211_key_conf *keyconf,
281 keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
282 keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
283 keyconf->hw_key_idx = keyconf->keyidx;
285 spin_lock_irqsave(&priv->sta_lock, flags);
287 priv->stations[sta_id].keyinfo.alg = keyconf->alg;
288 priv->stations[sta_id].keyinfo.conf = keyconf;
289 priv->stations[sta_id].keyinfo.keylen = 16;
291 if ((priv->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
292 == STA_KEY_FLG_NO_ENC)
293 priv->stations[sta_id].sta.key.key_offset =
294 iwl_get_free_ucode_key_index(priv);
295 /* else, we are overriding an existing key => no need to allocated room
298 /* This copy is acutally not needed: we get the key with each TX */
299 memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key, 16);
301 memcpy(priv->stations[sta_id].sta.key.key, keyconf->key, 16);
303 spin_unlock_irqrestore(&priv->sta_lock, flags);
308 int iwl_remove_dynamic_key(struct iwl_priv *priv,
309 struct ieee80211_key_conf *keyconf,
317 priv->key_mapping_key = 0;
319 spin_lock_irqsave(&priv->sta_lock, flags);
320 key_flags = le16_to_cpu(priv->stations[sta_id].sta.key.key_flags);
321 keyidx = (key_flags >> STA_KEY_FLG_KEYID_POS) & 0x3;
323 if (keyconf->keyidx != keyidx) {
324 /* We need to remove a key with index different that the one
325 * in the uCode. This means that the key we need to remove has
326 * been replaced by another one with different index.
327 * Don't do anything and return ok
329 spin_unlock_irqrestore(&priv->sta_lock, flags);
333 if (!test_and_clear_bit(priv->stations[sta_id].sta.key.key_offset,
334 &priv->ucode_key_table))
335 IWL_ERROR("index %d not used in uCode key table.\n",
336 priv->stations[sta_id].sta.key.key_offset);
337 memset(&priv->stations[sta_id].keyinfo, 0,
338 sizeof(struct iwl4965_hw_key));
339 memset(&priv->stations[sta_id].sta.key, 0,
340 sizeof(struct iwl4965_keyinfo));
341 priv->stations[sta_id].sta.key.key_flags =
342 STA_KEY_FLG_NO_ENC | STA_KEY_FLG_INVALID;
343 priv->stations[sta_id].sta.key.key_offset = WEP_INVALID_OFFSET;
344 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
345 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
347 IWL_DEBUG_INFO("hwcrypto: clear ucode station key info\n");
348 ret = iwl4965_send_add_station(priv, &priv->stations[sta_id].sta, 0);
349 spin_unlock_irqrestore(&priv->sta_lock, flags);
353 int iwl_set_dynamic_key(struct iwl_priv *priv,
354 struct ieee80211_key_conf *key, u8 sta_id)
358 priv->key_mapping_key = 1;
362 ret = iwl_set_ccmp_dynamic_key_info(priv, key, sta_id);
365 ret = iwl_set_tkip_dynamic_key_info(priv, key, sta_id);
368 ret = iwl_set_wep_dynamic_key_info(priv, key, sta_id);
371 IWL_ERROR("Unknown alg: %s alg = %d\n", __func__, key->alg);
378 #ifdef CONFIG_IWLWIFI_DEBUG
379 static void iwl_dump_lq_cmd(struct iwl_priv *priv,
380 struct iwl_link_quality_cmd *lq)
383 IWL_DEBUG_RATE("lq station id 0x%x\n", lq->sta_id);
384 IWL_DEBUG_RATE("lq dta 0x%X 0x%X\n",
385 lq->general_params.single_stream_ant_msk,
386 lq->general_params.dual_stream_ant_msk);
388 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
389 IWL_DEBUG_RATE("lq index %d 0x%X\n",
390 i, lq->rs_table[i].rate_n_flags);
393 static inline void iwl_dump_lq_cmd(struct iwl_priv *priv,
394 struct iwl_link_quality_cmd *lq)
399 int iwl_send_lq_cmd(struct iwl_priv *priv,
400 struct iwl_link_quality_cmd *lq, u8 flags)
402 struct iwl_host_cmd cmd = {
403 .id = REPLY_TX_LINK_QUALITY_CMD,
404 .len = sizeof(struct iwl_link_quality_cmd),
409 if ((lq->sta_id == 0xFF) &&
410 (priv->iw_mode == IEEE80211_IF_TYPE_IBSS))
413 if (lq->sta_id == 0xFF)
414 lq->sta_id = IWL_AP_ID;
416 iwl_dump_lq_cmd(priv,lq);
418 if (iwl_is_associated(priv) && priv->assoc_station_added &&
419 priv->lq_mngr.lq_ready)
420 return iwl_send_cmd(priv, &cmd);
424 EXPORT_SYMBOL(iwl_send_lq_cmd);