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 priv->stations[sta_id].sta.key.key_offset =
211 iwl_get_free_ucode_key_index(priv);
212 priv->stations[sta_id].sta.key.key_flags = key_flags;
214 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
215 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
217 ret = iwl4965_send_add_station(priv,
218 &priv->stations[sta_id].sta, CMD_ASYNC);
220 spin_unlock_irqrestore(&priv->sta_lock, flags);
225 static int iwl_set_ccmp_dynamic_key_info(struct iwl_priv *priv,
226 struct ieee80211_key_conf *keyconf,
230 __le16 key_flags = 0;
232 key_flags |= (STA_KEY_FLG_CCMP | STA_KEY_FLG_MAP_KEY_MSK);
233 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
234 key_flags &= ~STA_KEY_FLG_INVALID;
236 if (sta_id == priv->hw_params.bcast_sta_id)
237 key_flags |= STA_KEY_MULTICAST_MSK;
239 keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
240 keyconf->hw_key_idx = keyconf->keyidx;
242 spin_lock_irqsave(&priv->sta_lock, flags);
243 priv->stations[sta_id].keyinfo.alg = keyconf->alg;
244 priv->stations[sta_id].keyinfo.keylen = keyconf->keylen;
246 memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key,
249 memcpy(priv->stations[sta_id].sta.key.key, keyconf->key,
252 priv->stations[sta_id].sta.key.key_offset =
253 iwl_get_free_ucode_key_index(priv);
254 priv->stations[sta_id].sta.key.key_flags = key_flags;
255 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
256 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
258 spin_unlock_irqrestore(&priv->sta_lock, flags);
260 IWL_DEBUG_INFO("hwcrypto: modify ucode station key info\n");
261 return iwl4965_send_add_station(priv,
262 &priv->stations[sta_id].sta, CMD_ASYNC);
265 static int iwl_set_tkip_dynamic_key_info(struct iwl_priv *priv,
266 struct ieee80211_key_conf *keyconf,
272 keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
273 keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
274 keyconf->hw_key_idx = keyconf->keyidx;
276 spin_lock_irqsave(&priv->sta_lock, flags);
278 priv->stations[sta_id].keyinfo.alg = keyconf->alg;
279 priv->stations[sta_id].keyinfo.conf = keyconf;
280 priv->stations[sta_id].keyinfo.keylen = 16;
281 priv->stations[sta_id].sta.key.key_offset =
282 iwl_get_free_ucode_key_index(priv);
284 /* This copy is acutally not needed: we get the key with each TX */
285 memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key, 16);
287 memcpy(priv->stations[sta_id].sta.key.key, keyconf->key, 16);
289 spin_unlock_irqrestore(&priv->sta_lock, flags);
294 int iwl_remove_dynamic_key(struct iwl_priv *priv, u8 sta_id)
298 priv->key_mapping_key = 0;
300 spin_lock_irqsave(&priv->sta_lock, flags);
301 if (!test_and_clear_bit(priv->stations[sta_id].sta.key.key_offset,
302 &priv->ucode_key_table))
303 IWL_ERROR("index %d not used in uCode key table.\n",
304 priv->stations[sta_id].sta.key.key_offset);
305 memset(&priv->stations[sta_id].keyinfo, 0,
306 sizeof(struct iwl4965_hw_key));
307 memset(&priv->stations[sta_id].sta.key, 0,
308 sizeof(struct iwl4965_keyinfo));
309 priv->stations[sta_id].sta.key.key_flags = STA_KEY_FLG_NO_ENC;
310 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
311 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
312 spin_unlock_irqrestore(&priv->sta_lock, flags);
314 IWL_DEBUG_INFO("hwcrypto: clear ucode station key info\n");
315 return iwl4965_send_add_station(priv, &priv->stations[sta_id].sta, 0);
318 int iwl_set_dynamic_key(struct iwl_priv *priv,
319 struct ieee80211_key_conf *key, u8 sta_id)
323 priv->key_mapping_key = 1;
327 ret = iwl_set_ccmp_dynamic_key_info(priv, key, sta_id);
330 ret = iwl_set_tkip_dynamic_key_info(priv, key, sta_id);
333 ret = iwl_set_wep_dynamic_key_info(priv, key, sta_id);
336 IWL_ERROR("Unknown alg: %s alg = %d\n", __func__, key->alg);
343 #ifdef CONFIG_IWLWIFI_DEBUG
344 static void iwl_dump_lq_cmd(struct iwl_priv *priv,
345 struct iwl_link_quality_cmd *lq)
348 IWL_DEBUG_RATE("lq station id 0x%x\n", lq->sta_id);
349 IWL_DEBUG_RATE("lq dta 0x%X 0x%X\n",
350 lq->general_params.single_stream_ant_msk,
351 lq->general_params.dual_stream_ant_msk);
353 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
354 IWL_DEBUG_RATE("lq index %d 0x%X\n",
355 i, lq->rs_table[i].rate_n_flags);
358 static inline void iwl_dump_lq_cmd(struct iwl_priv *priv,
359 struct iwl_link_quality_cmd *lq)
364 int iwl_send_lq_cmd(struct iwl_priv *priv,
365 struct iwl_link_quality_cmd *lq, u8 flags)
367 struct iwl_host_cmd cmd = {
368 .id = REPLY_TX_LINK_QUALITY_CMD,
369 .len = sizeof(struct iwl_link_quality_cmd),
374 if ((lq->sta_id == 0xFF) &&
375 (priv->iw_mode == IEEE80211_IF_TYPE_IBSS))
378 if (lq->sta_id == 0xFF)
379 lq->sta_id = IWL_AP_ID;
381 iwl_dump_lq_cmd(priv,lq);
383 if (iwl_is_associated(priv) && priv->assoc_station_added &&
384 priv->lq_mngr.lq_ready)
385 return iwl_send_cmd(priv, &cmd);
389 EXPORT_SYMBOL(iwl_send_lq_cmd);