]> err.no Git - linux-2.6/blob - drivers/net/wireless/iwlwifi/iwl-sta.c
d39ac1cffb57b9c50d6b1918481942e77bb70b93
[linux-2.6] / drivers / net / wireless / iwlwifi / iwl-sta.c
1 /******************************************************************************
2  *
3  * Copyright(c) 2003 - 2008 Intel Corporation. All rights reserved.
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  * James P. Ketrenos <ipw2100-admin@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 #include <linux/etherdevice.h>
32
33 #include "iwl-eeprom.h"
34 #include "iwl-4965.h"
35 #include "iwl-core.h"
36 #include "iwl-sta.h"
37 #include "iwl-io.h"
38 #include "iwl-helpers.h"
39 #include "iwl-4965.h"
40 #include "iwl-sta.h"
41
42 u8 iwl_find_station(struct iwl_priv *priv, const u8 *addr)
43 {
44         int i;
45         int start = 0;
46         int ret = IWL_INVALID_STATION;
47         unsigned long flags;
48         DECLARE_MAC_BUF(mac);
49
50         if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) ||
51             (priv->iw_mode == IEEE80211_IF_TYPE_AP))
52                 start = IWL_STA_ID;
53
54         if (is_broadcast_ether_addr(addr))
55                 return priv->hw_params.bcast_sta_id;
56
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,
61                                          addr))) {
62                         ret = i;
63                         goto out;
64                 }
65
66         IWL_DEBUG_ASSOC_LIMIT("can not find STA %s total %d\n",
67                               print_mac(mac, addr), priv->num_stations);
68
69  out:
70         spin_unlock_irqrestore(&priv->sta_lock, flags);
71         return ret;
72 }
73 EXPORT_SYMBOL(iwl_find_station);
74
75
76 int iwl_get_free_ucode_key_index(struct iwl_priv *priv)
77 {
78         int i;
79
80         for (i = 0; i < STA_KEY_MAX_NUM; i++)
81                 if (!test_and_set_bit(i, &priv->ucode_key_table))
82                         return i;
83
84         return -1;
85 }
86
87 int iwl_send_static_wepkey_cmd(struct iwl_priv *priv, u8 send_if_empty)
88 {
89         int i, not_empty = 0;
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 = {
95                 .id = REPLY_WEPKEY,
96                 .data = wep_cmd,
97                 .meta.flags = CMD_ASYNC,
98         };
99
100         memset(wep_cmd, 0, cmd_size +
101                         (sizeof(struct iwl_wep_key) * WEP_KEYS_MAX));
102
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;
107                         not_empty = 1;
108                 } else {
109                         wep_cmd->key[i].key_offset = WEP_INVALID_OFFSET;
110                 }
111
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);
115         }
116
117         wep_cmd->global_key_type = WEP_KEY_WEP_TYPE;
118         wep_cmd->num_keys = WEP_KEYS_MAX;
119
120         cmd_size += sizeof(struct iwl_wep_key) * WEP_KEYS_MAX;
121
122         cmd.len = cmd_size;
123
124         if (not_empty || send_if_empty)
125                 return iwl_send_cmd(priv, &cmd);
126         else
127                 return 0;
128 }
129
130 int iwl_remove_default_wep_key(struct iwl_priv *priv,
131                                struct ieee80211_key_conf *keyconf)
132 {
133         int ret;
134         unsigned long flags;
135
136         spin_lock_irqsave(&priv->sta_lock, flags);
137
138         if (!test_and_clear_bit(keyconf->keyidx, &priv->ucode_key_table))
139                 IWL_ERROR("index %d not used in uCode key table.\n",
140                           keyconf->keyidx);
141
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);
146
147         return ret;
148 }
149
150 int iwl_set_default_wep_key(struct iwl_priv *priv,
151                             struct ieee80211_key_conf *keyconf)
152 {
153         int ret;
154         unsigned long flags;
155
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;
159
160         spin_lock_irqsave(&priv->sta_lock, flags);
161         priv->default_wep_key++;
162
163         if (test_and_set_bit(keyconf->keyidx, &priv->ucode_key_table))
164                 IWL_ERROR("index %d already used in uCode key table.\n",
165                         keyconf->keyidx);
166
167         priv->wep_keys[keyconf->keyidx].key_size = keyconf->keylen;
168         memcpy(&priv->wep_keys[keyconf->keyidx].key, &keyconf->key,
169                                                         keyconf->keylen);
170
171         ret = iwl_send_static_wepkey_cmd(priv, 0);
172         spin_unlock_irqrestore(&priv->sta_lock, flags);
173
174         return ret;
175 }
176
177 static int iwl_set_wep_dynamic_key_info(struct iwl_priv *priv,
178                                 struct ieee80211_key_conf *keyconf,
179                                 u8 sta_id)
180 {
181         unsigned long flags;
182         __le16 key_flags = 0;
183         int ret;
184
185         keyconf->flags &= ~IEEE80211_KEY_FLAG_GENERATE_IV;
186         keyconf->hw_key_idx = keyconf->keyidx;
187
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;
191
192         if (keyconf->keylen == WEP_KEY_LEN_128)
193                 key_flags |= STA_KEY_FLG_KEY_SIZE_MSK;
194
195         if (sta_id == priv->hw_params.bcast_sta_id)
196                 key_flags |= STA_KEY_MULTICAST_MSK;
197
198         spin_lock_irqsave(&priv->sta_lock, flags);
199
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;
203
204         memcpy(priv->stations[sta_id].keyinfo.key,
205                                 keyconf->key, keyconf->keylen);
206
207         memcpy(&priv->stations[sta_id].sta.key.key[3],
208                                 keyconf->key, keyconf->keylen);
209
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
215          * in uCode. */
216
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;
220
221         ret = iwl4965_send_add_station(priv,
222                 &priv->stations[sta_id].sta, CMD_ASYNC);
223
224         spin_unlock_irqrestore(&priv->sta_lock, flags);
225
226         return ret;
227 }
228
229 static int iwl_set_ccmp_dynamic_key_info(struct iwl_priv *priv,
230                                    struct ieee80211_key_conf *keyconf,
231                                    u8 sta_id)
232 {
233         unsigned long flags;
234         __le16 key_flags = 0;
235
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;
239
240         if (sta_id == priv->hw_params.bcast_sta_id)
241                 key_flags |= STA_KEY_MULTICAST_MSK;
242
243         keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
244         keyconf->hw_key_idx = keyconf->keyidx;
245
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;
249
250         memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key,
251                keyconf->keylen);
252
253         memcpy(priv->stations[sta_id].sta.key.key, keyconf->key,
254                keyconf->keylen);
255
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
261          * in uCode. */
262
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;
266
267         spin_unlock_irqrestore(&priv->sta_lock, flags);
268
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);
272 }
273
274 static int iwl_set_tkip_dynamic_key_info(struct iwl_priv *priv,
275                                    struct ieee80211_key_conf *keyconf,
276                                    u8 sta_id)
277 {
278         unsigned long flags;
279         int ret = 0;
280
281         keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
282         keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
283         keyconf->hw_key_idx = keyconf->keyidx;
284
285         spin_lock_irqsave(&priv->sta_lock, flags);
286
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;
290
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
296          * in uCode. */
297
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);
300
301         memcpy(priv->stations[sta_id].sta.key.key, keyconf->key, 16);
302
303         spin_unlock_irqrestore(&priv->sta_lock, flags);
304
305         return ret;
306 }
307
308 int iwl_remove_dynamic_key(struct iwl_priv *priv,
309                                 struct ieee80211_key_conf *keyconf,
310                                 u8 sta_id)
311 {
312         unsigned long flags;
313         int ret = 0;
314         u16 key_flags;
315         u8 keyidx;
316
317         priv->key_mapping_key = 0;
318
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;
322
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
328                  */
329                 spin_unlock_irqrestore(&priv->sta_lock, flags);
330                 return 0;
331         }
332
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;
346
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);
350         return ret;
351 }
352
353 int iwl_set_dynamic_key(struct iwl_priv *priv,
354                                 struct ieee80211_key_conf *key, u8 sta_id)
355 {
356         int ret;
357
358         priv->key_mapping_key = 1;
359
360         switch (key->alg) {
361         case ALG_CCMP:
362                 ret = iwl_set_ccmp_dynamic_key_info(priv, key, sta_id);
363                 break;
364         case ALG_TKIP:
365                 ret = iwl_set_tkip_dynamic_key_info(priv, key, sta_id);
366                 break;
367         case ALG_WEP:
368                 ret = iwl_set_wep_dynamic_key_info(priv, key, sta_id);
369                 break;
370         default:
371                 IWL_ERROR("Unknown alg: %s alg = %d\n", __func__, key->alg);
372                 ret = -EINVAL;
373         }
374
375         return ret;
376 }
377
378 #ifdef CONFIG_IWLWIFI_DEBUG
379 static void iwl_dump_lq_cmd(struct iwl_priv *priv,
380                            struct iwl_link_quality_cmd *lq)
381 {
382         int i;
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);
387
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);
391 }
392 #else
393 static inline void iwl_dump_lq_cmd(struct iwl_priv *priv,
394                                    struct iwl_link_quality_cmd *lq)
395 {
396 }
397 #endif
398
399 int iwl_send_lq_cmd(struct iwl_priv *priv,
400                     struct iwl_link_quality_cmd *lq, u8 flags)
401 {
402         struct iwl_host_cmd cmd = {
403                 .id = REPLY_TX_LINK_QUALITY_CMD,
404                 .len = sizeof(struct iwl_link_quality_cmd),
405                 .meta.flags = flags,
406                 .data = lq,
407         };
408
409         if ((lq->sta_id == 0xFF) &&
410             (priv->iw_mode == IEEE80211_IF_TYPE_IBSS))
411                 return -EINVAL;
412
413         if (lq->sta_id == 0xFF)
414                 lq->sta_id = IWL_AP_ID;
415
416         iwl_dump_lq_cmd(priv,lq);
417
418         if (iwl_is_associated(priv) && priv->assoc_station_added &&
419             priv->lq_mngr.lq_ready)
420                 return  iwl_send_cmd(priv, &cmd);
421
422         return 0;
423 }
424 EXPORT_SYMBOL(iwl_send_lq_cmd);
425