iwlwifi: move find station to iwl-sta.c
[safe/jmp/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         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;
213
214         priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
215         priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
216
217         ret = iwl4965_send_add_station(priv,
218                 &priv->stations[sta_id].sta, CMD_ASYNC);
219
220         spin_unlock_irqrestore(&priv->sta_lock, flags);
221
222         return ret;
223 }
224
225 static int iwl_set_ccmp_dynamic_key_info(struct iwl_priv *priv,
226                                    struct ieee80211_key_conf *keyconf,
227                                    u8 sta_id)
228 {
229         unsigned long flags;
230         __le16 key_flags = 0;
231
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;
235
236         if (sta_id == priv->hw_params.bcast_sta_id)
237                 key_flags |= STA_KEY_MULTICAST_MSK;
238
239         keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
240         keyconf->hw_key_idx = keyconf->keyidx;
241
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;
245
246         memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key,
247                keyconf->keylen);
248
249         memcpy(priv->stations[sta_id].sta.key.key, keyconf->key,
250                keyconf->keylen);
251
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;
257
258         spin_unlock_irqrestore(&priv->sta_lock, flags);
259
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);
263 }
264
265 static int iwl_set_tkip_dynamic_key_info(struct iwl_priv *priv,
266                                    struct ieee80211_key_conf *keyconf,
267                                    u8 sta_id)
268 {
269         unsigned long flags;
270         int ret = 0;
271
272         keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
273         keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
274         keyconf->hw_key_idx = keyconf->keyidx;
275
276         spin_lock_irqsave(&priv->sta_lock, flags);
277
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);
283
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);
286
287         memcpy(priv->stations[sta_id].sta.key.key, keyconf->key, 16);
288
289         spin_unlock_irqrestore(&priv->sta_lock, flags);
290
291         return ret;
292 }
293
294 int iwl_remove_dynamic_key(struct iwl_priv *priv, u8 sta_id)
295 {
296         unsigned long flags;
297
298         priv->key_mapping_key = 0;
299
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);
313
314         IWL_DEBUG_INFO("hwcrypto: clear ucode station key info\n");
315         return iwl4965_send_add_station(priv, &priv->stations[sta_id].sta, 0);
316 }
317
318 int iwl_set_dynamic_key(struct iwl_priv *priv,
319                                 struct ieee80211_key_conf *key, u8 sta_id)
320 {
321         int ret;
322
323         priv->key_mapping_key = 1;
324
325         switch (key->alg) {
326         case ALG_CCMP:
327                 ret = iwl_set_ccmp_dynamic_key_info(priv, key, sta_id);
328                 break;
329         case ALG_TKIP:
330                 ret = iwl_set_tkip_dynamic_key_info(priv, key, sta_id);
331                 break;
332         case ALG_WEP:
333                 ret = iwl_set_wep_dynamic_key_info(priv, key, sta_id);
334                 break;
335         default:
336                 IWL_ERROR("Unknown alg: %s alg = %d\n", __func__, key->alg);
337                 ret = -EINVAL;
338         }
339
340         return ret;
341 }
342
343 #ifdef CONFIG_IWLWIFI_DEBUG
344 static void iwl_dump_lq_cmd(struct iwl_priv *priv,
345                            struct iwl_link_quality_cmd *lq)
346 {
347         int i;
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);
352
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);
356 }
357 #else
358 static inline void iwl_dump_lq_cmd(struct iwl_priv *priv,
359                                    struct iwl_link_quality_cmd *lq)
360 {
361 }
362 #endif
363
364 int iwl_send_lq_cmd(struct iwl_priv *priv,
365                     struct iwl_link_quality_cmd *lq, u8 flags)
366 {
367         struct iwl_host_cmd cmd = {
368                 .id = REPLY_TX_LINK_QUALITY_CMD,
369                 .len = sizeof(struct iwl_link_quality_cmd),
370                 .meta.flags = flags,
371                 .data = lq,
372         };
373
374         if ((lq->sta_id == 0xFF) &&
375             (priv->iw_mode == IEEE80211_IF_TYPE_IBSS))
376                 return -EINVAL;
377
378         if (lq->sta_id == 0xFF)
379                 lq->sta_id = IWL_AP_ID;
380
381         iwl_dump_lq_cmd(priv,lq);
382
383         if (iwl_is_associated(priv) && priv->assoc_station_added &&
384             priv->lq_mngr.lq_ready)
385                 return  iwl_send_cmd(priv, &cmd);
386
387         return 0;
388 }
389 EXPORT_SYMBOL(iwl_send_lq_cmd);
390