blob: ed07566f22ee7a31eb66197cdba4b1ac8c1f84b7 [file] [log] [blame]
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07001/******************************************************************************
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>
Tomas Winkler947b13a2008-04-16 16:34:48 -070031#include <linux/etherdevice.h>
Emmanuel Grumbach6974e362008-04-14 21:16:06 -070032
33#include "iwl-eeprom.h"
Tomas Winkler3e0d4cb2008-04-24 11:55:38 -070034#include "iwl-dev.h"
Emmanuel Grumbach6974e362008-04-14 21:16:06 -070035#include "iwl-core.h"
36#include "iwl-sta.h"
37#include "iwl-io.h"
38#include "iwl-helpers.h"
Emmanuel Grumbach80fb47a2008-04-14 21:16:08 -070039
Tomas Winkler7a999bf2008-05-29 16:35:02 +080040
41#define IWL_STA_DRIVER_ACTIVE 0x1 /* ucode entry is active */
42#define IWL_STA_UCODE_ACTIVE 0x2 /* ucode entry is active */
43
Tomas Winkler947b13a2008-04-16 16:34:48 -070044u8 iwl_find_station(struct iwl_priv *priv, const u8 *addr)
45{
46 int i;
47 int start = 0;
48 int ret = IWL_INVALID_STATION;
49 unsigned long flags;
50 DECLARE_MAC_BUF(mac);
51
52 if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) ||
53 (priv->iw_mode == IEEE80211_IF_TYPE_AP))
54 start = IWL_STA_ID;
55
56 if (is_broadcast_ether_addr(addr))
57 return priv->hw_params.bcast_sta_id;
58
59 spin_lock_irqsave(&priv->sta_lock, flags);
60 for (i = start; i < priv->hw_params.max_stations; i++)
61 if (priv->stations[i].used &&
62 (!compare_ether_addr(priv->stations[i].sta.sta.addr,
63 addr))) {
64 ret = i;
65 goto out;
66 }
67
68 IWL_DEBUG_ASSOC_LIMIT("can not find STA %s total %d\n",
69 print_mac(mac, addr), priv->num_stations);
70
71 out:
72 spin_unlock_irqrestore(&priv->sta_lock, flags);
73 return ret;
74}
75EXPORT_SYMBOL(iwl_find_station);
76
Tomas Winkler42132bc2008-05-29 16:35:03 +080077static int iwl_add_sta_callback(struct iwl_priv *priv,
78 struct iwl_cmd *cmd, struct sk_buff *skb)
79{
80 struct iwl_rx_packet *res = NULL;
81
82 if (!skb) {
83 IWL_ERROR("Error: Response NULL in REPLY_ADD_STA.\n");
84 return 1;
85 }
86
87 res = (struct iwl_rx_packet *)skb->data;
88 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
89 IWL_ERROR("Bad return from REPLY_ADD_STA (0x%08X)\n",
90 res->hdr.flags);
91 return 1;
92 }
93
94 switch (res->u.add_sta.status) {
95 case ADD_STA_SUCCESS_MSK:
96 /* FIXME: implement iwl_sta_ucode_activate(priv, addr); */
97 /* fail through */
98 default:
99 IWL_DEBUG_HC("Received REPLY_ADD_STA:(0x%08X)\n",
100 res->u.add_sta.status);
101 break;
102 }
103
104 /* We didn't cache the SKB; let the caller free it */
105 return 1;
106}
107
108
109
Tomas Winkler133636d2008-05-05 10:22:34 +0800110int iwl_send_add_sta(struct iwl_priv *priv,
111 struct iwl_addsta_cmd *sta, u8 flags)
112{
113 struct iwl_rx_packet *res = NULL;
114 int ret = 0;
115 u8 data[sizeof(*sta)];
116 struct iwl_host_cmd cmd = {
117 .id = REPLY_ADD_STA,
118 .meta.flags = flags,
119 .data = data,
120 };
121
Tomas Winkler42132bc2008-05-29 16:35:03 +0800122 if (flags & CMD_ASYNC)
123 cmd.meta.u.callback = iwl_add_sta_callback;
124 else
Tomas Winkler133636d2008-05-05 10:22:34 +0800125 cmd.meta.flags |= CMD_WANT_SKB;
126
127 cmd.len = priv->cfg->ops->utils->build_addsta_hcmd(sta, data);
128 ret = iwl_send_cmd(priv, &cmd);
129
130 if (ret || (flags & CMD_ASYNC))
131 return ret;
132
133 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
134 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
135 IWL_ERROR("Bad return from REPLY_ADD_STA (0x%08X)\n",
136 res->hdr.flags);
137 ret = -EIO;
138 }
139
140 if (ret == 0) {
141 switch (res->u.add_sta.status) {
142 case ADD_STA_SUCCESS_MSK:
143 IWL_DEBUG_INFO("REPLY_ADD_STA PASSED\n");
144 break;
145 default:
146 ret = -EIO;
147 IWL_WARNING("REPLY_ADD_STA failed\n");
148 break;
149 }
150 }
151
152 priv->alloc_rxb_skb--;
153 dev_kfree_skb_any(cmd.meta.u.skb);
154
155 return ret;
156}
157EXPORT_SYMBOL(iwl_send_add_sta);
Tomas Winkler947b13a2008-04-16 16:34:48 -0700158
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800159#ifdef CONFIG_IWL4965_HT
160
161static void iwl_set_ht_add_station(struct iwl_priv *priv, u8 index,
162 struct ieee80211_ht_info *sta_ht_inf)
163{
164 __le32 sta_flags;
165 u8 mimo_ps_mode;
166
167 if (!sta_ht_inf || !sta_ht_inf->ht_supported)
168 goto done;
169
170 mimo_ps_mode = (sta_ht_inf->cap & IEEE80211_HT_CAP_MIMO_PS) >> 2;
171
172 sta_flags = priv->stations[index].sta.station_flags;
173
174 sta_flags &= ~(STA_FLG_RTS_MIMO_PROT_MSK | STA_FLG_MIMO_DIS_MSK);
175
176 switch (mimo_ps_mode) {
177 case WLAN_HT_CAP_MIMO_PS_STATIC:
178 sta_flags |= STA_FLG_MIMO_DIS_MSK;
179 break;
180 case WLAN_HT_CAP_MIMO_PS_DYNAMIC:
181 sta_flags |= STA_FLG_RTS_MIMO_PROT_MSK;
182 break;
183 case WLAN_HT_CAP_MIMO_PS_DISABLED:
184 break;
185 default:
186 IWL_WARNING("Invalid MIMO PS mode %d", mimo_ps_mode);
187 break;
188 }
189
190 sta_flags |= cpu_to_le32(
191 (u32)sta_ht_inf->ampdu_factor << STA_FLG_MAX_AGG_SIZE_POS);
192
193 sta_flags |= cpu_to_le32(
194 (u32)sta_ht_inf->ampdu_density << STA_FLG_AGG_MPDU_DENSITY_POS);
195
196 if (iwl_is_fat_tx_allowed(priv, sta_ht_inf))
197 sta_flags |= STA_FLG_FAT_EN_MSK;
198 else
199 sta_flags &= ~STA_FLG_FAT_EN_MSK;
200
201 priv->stations[index].sta.station_flags = sta_flags;
202 done:
203 return;
204}
205#else
206static inline void iwl_set_ht_add_station(struct iwl_priv *priv, u8 index,
207 struct ieee80211_ht_info *sta_ht_info)
208{
209}
210#endif
211
212/**
213 * iwl_add_station_flags - Add station to tables in driver and device
214 */
215u8 iwl_add_station_flags(struct iwl_priv *priv, const u8 *addr, int is_ap,
216 u8 flags, struct ieee80211_ht_info *ht_info)
217{
218 int i;
219 int index = IWL_INVALID_STATION;
220 struct iwl_station_entry *station;
221 unsigned long flags_spin;
222 DECLARE_MAC_BUF(mac);
223
224 spin_lock_irqsave(&priv->sta_lock, flags_spin);
225 if (is_ap)
226 index = IWL_AP_ID;
227 else if (is_broadcast_ether_addr(addr))
228 index = priv->hw_params.bcast_sta_id;
229 else
230 for (i = IWL_STA_ID; i < priv->hw_params.max_stations; i++) {
231 if (!compare_ether_addr(priv->stations[i].sta.sta.addr,
232 addr)) {
233 index = i;
234 break;
235 }
236
237 if (!priv->stations[i].used &&
238 index == IWL_INVALID_STATION)
239 index = i;
240 }
241
242
243 /* These two conditions have the same outcome, but keep them separate
244 since they have different meanings */
245 if (unlikely(index == IWL_INVALID_STATION)) {
246 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
247 return index;
248 }
249
250 if (priv->stations[index].used &&
251 !compare_ether_addr(priv->stations[index].sta.sta.addr, addr)) {
252 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
253 return index;
254 }
255
256
257 IWL_DEBUG_ASSOC("Add STA ID %d: %s\n", index, print_mac(mac, addr));
258 station = &priv->stations[index];
259 station->used = 1;
260 priv->num_stations++;
261
262 /* Set up the REPLY_ADD_STA command to send to device */
263 memset(&station->sta, 0, sizeof(struct iwl_addsta_cmd));
264 memcpy(station->sta.sta.addr, addr, ETH_ALEN);
265 station->sta.mode = 0;
266 station->sta.sta.sta_id = index;
267 station->sta.station_flags = 0;
268
269 /* BCAST station and IBSS stations do not work in HT mode */
270 if (index != priv->hw_params.bcast_sta_id &&
271 priv->iw_mode != IEEE80211_IF_TYPE_IBSS)
272 iwl_set_ht_add_station(priv, index, ht_info);
273
274 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
275
276 /* Add station to device's station table */
277 iwl_send_add_sta(priv, &station->sta, flags);
278 return index;
279
280}
281EXPORT_SYMBOL(iwl_add_station_flags);
282
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800283
284static int iwl_sta_ucode_deactivate(struct iwl_priv *priv, const char *addr)
285{
286 unsigned long flags;
287 u8 sta_id;
288 DECLARE_MAC_BUF(mac);
289
290 sta_id = iwl_find_station(priv, addr);
291 if (sta_id != IWL_INVALID_STATION) {
292 IWL_DEBUG_ASSOC("Removed STA from Ucode: %s\n",
293 print_mac(mac, addr));
294 spin_lock_irqsave(&priv->sta_lock, flags);
295 priv->stations[sta_id].used &= ~IWL_STA_UCODE_ACTIVE;
296 memset(&priv->stations[sta_id], 0,
297 sizeof(struct iwl_station_entry));
298 spin_unlock_irqrestore(&priv->sta_lock, flags);
299 return 0;
300 }
301 return -EINVAL;
302}
303
304static int iwl_remove_sta_callback(struct iwl_priv *priv,
305 struct iwl_cmd *cmd, struct sk_buff *skb)
306{
307 struct iwl_rx_packet *res = NULL;
308 const char *addr = cmd->cmd.rm_sta.addr;
309
310 if (!skb) {
311 IWL_ERROR("Error: Response NULL in REPLY_REMOVE_STA.\n");
312 return 1;
313 }
314
315 res = (struct iwl_rx_packet *)skb->data;
316 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
317 IWL_ERROR("Bad return from REPLY_REMOVE_STA (0x%08X)\n",
318 res->hdr.flags);
319 return 1;
320 }
321
322 switch (res->u.rem_sta.status) {
323 case REM_STA_SUCCESS_MSK:
324 iwl_sta_ucode_deactivate(priv, addr);
325 break;
326 default:
327 break;
328 }
329
330 /* We didn't cache the SKB; let the caller free it */
331 return 1;
332}
333
334static int iwl_send_remove_station(struct iwl_priv *priv, const u8 *addr,
335 u8 flags)
336{
337 struct iwl_rx_packet *res = NULL;
338 int ret;
339
340 struct iwl_rem_sta_cmd rm_sta_cmd;
341
342 struct iwl_host_cmd cmd = {
343 .id = REPLY_REMOVE_STA,
344 .len = sizeof(struct iwl_rem_sta_cmd),
345 .meta.flags = flags,
346 .data = &rm_sta_cmd,
347 };
348
349 memset(&rm_sta_cmd, 0, sizeof(rm_sta_cmd));
350 rm_sta_cmd.num_sta = 1;
351 memcpy(&rm_sta_cmd.addr, addr , ETH_ALEN);
352
353 if (flags & CMD_ASYNC)
354 cmd.meta.u.callback = iwl_remove_sta_callback;
355 else
356 cmd.meta.flags |= CMD_WANT_SKB;
357 ret = iwl_send_cmd(priv, &cmd);
358
359 if (ret || (flags & CMD_ASYNC))
360 return ret;
361
362 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
363 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
364 IWL_ERROR("Bad return from REPLY_REMOVE_STA (0x%08X)\n",
365 res->hdr.flags);
366 ret = -EIO;
367 }
368
369 if (!ret) {
370 switch (res->u.rem_sta.status) {
371 case REM_STA_SUCCESS_MSK:
372 iwl_sta_ucode_deactivate(priv, addr);
373 IWL_DEBUG_ASSOC("REPLY_REMOVE_STA PASSED\n");
374 break;
375 default:
376 ret = -EIO;
377 IWL_ERROR("REPLY_REMOVE_STA failed\n");
378 break;
379 }
380 }
381
382 priv->alloc_rxb_skb--;
383 dev_kfree_skb_any(cmd.meta.u.skb);
384
385 return ret;
386}
387/**
388 * iwl_remove_station - Remove driver's knowledge of station.
389 *
390 */
391u8 iwl_remove_station(struct iwl_priv *priv, const u8 *addr, int is_ap)
392{
393 int index = IWL_INVALID_STATION;
394 int i;
395 unsigned long flags;
396
397 spin_lock_irqsave(&priv->sta_lock, flags);
398
399 if (is_ap)
400 index = IWL_AP_ID;
401 else if (is_broadcast_ether_addr(addr))
402 index = priv->hw_params.bcast_sta_id;
403 else
404 for (i = IWL_STA_ID; i < priv->hw_params.max_stations; i++)
405 if (priv->stations[i].used &&
406 !compare_ether_addr(priv->stations[i].sta.sta.addr,
407 addr)) {
408 index = i;
409 break;
410 }
411
412 if (unlikely(index == IWL_INVALID_STATION))
413 goto out;
414
415 if (priv->stations[index].used) {
416 priv->stations[index].used = 0;
417 priv->num_stations--;
418 }
419
420 BUG_ON(priv->num_stations < 0);
421 spin_unlock_irqrestore(&priv->sta_lock, flags);
422 iwl_send_remove_station(priv, addr, CMD_ASYNC);
423 return index;
424out:
425 spin_unlock_irqrestore(&priv->sta_lock, flags);
426 return 0;
427}
428EXPORT_SYMBOL(iwl_remove_station);
Emmanuel Grumbach80fb47a2008-04-14 21:16:08 -0700429int iwl_get_free_ucode_key_index(struct iwl_priv *priv)
430{
431 int i;
432
433 for (i = 0; i < STA_KEY_MAX_NUM; i++)
Emmanuel Grumbach77bab602008-04-15 16:01:44 -0700434 if (!test_and_set_bit(i, &priv->ucode_key_table))
Emmanuel Grumbach80fb47a2008-04-14 21:16:08 -0700435 return i;
436
437 return -1;
438}
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700439
440int iwl_send_static_wepkey_cmd(struct iwl_priv *priv, u8 send_if_empty)
441{
442 int i, not_empty = 0;
443 u8 buff[sizeof(struct iwl_wep_cmd) +
444 sizeof(struct iwl_wep_key) * WEP_KEYS_MAX];
445 struct iwl_wep_cmd *wep_cmd = (struct iwl_wep_cmd *)buff;
446 size_t cmd_size = sizeof(struct iwl_wep_cmd);
447 struct iwl_host_cmd cmd = {
448 .id = REPLY_WEPKEY,
449 .data = wep_cmd,
450 .meta.flags = CMD_ASYNC,
451 };
452
453 memset(wep_cmd, 0, cmd_size +
454 (sizeof(struct iwl_wep_key) * WEP_KEYS_MAX));
455
456 for (i = 0; i < WEP_KEYS_MAX ; i++) {
457 wep_cmd->key[i].key_index = i;
458 if (priv->wep_keys[i].key_size) {
459 wep_cmd->key[i].key_offset = i;
460 not_empty = 1;
461 } else {
462 wep_cmd->key[i].key_offset = WEP_INVALID_OFFSET;
463 }
464
465 wep_cmd->key[i].key_size = priv->wep_keys[i].key_size;
466 memcpy(&wep_cmd->key[i].key[3], priv->wep_keys[i].key,
467 priv->wep_keys[i].key_size);
468 }
469
470 wep_cmd->global_key_type = WEP_KEY_WEP_TYPE;
471 wep_cmd->num_keys = WEP_KEYS_MAX;
472
473 cmd_size += sizeof(struct iwl_wep_key) * WEP_KEYS_MAX;
474
475 cmd.len = cmd_size;
476
477 if (not_empty || send_if_empty)
478 return iwl_send_cmd(priv, &cmd);
479 else
480 return 0;
481}
Tomas Winkler27aaba02008-05-05 10:22:44 +0800482EXPORT_SYMBOL(iwl_send_static_wepkey_cmd);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700483
484int iwl_remove_default_wep_key(struct iwl_priv *priv,
Emmanuel Grumbach80fb47a2008-04-14 21:16:08 -0700485 struct ieee80211_key_conf *keyconf)
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700486{
487 int ret;
488 unsigned long flags;
489
490 spin_lock_irqsave(&priv->sta_lock, flags);
Emmanuel Grumbach80fb47a2008-04-14 21:16:08 -0700491
492 if (!test_and_clear_bit(keyconf->keyidx, &priv->ucode_key_table))
493 IWL_ERROR("index %d not used in uCode key table.\n",
494 keyconf->keyidx);
495
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700496 priv->default_wep_key--;
Emmanuel Grumbach80fb47a2008-04-14 21:16:08 -0700497 memset(&priv->wep_keys[keyconf->keyidx], 0, sizeof(priv->wep_keys[0]));
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700498 ret = iwl_send_static_wepkey_cmd(priv, 1);
499 spin_unlock_irqrestore(&priv->sta_lock, flags);
500
501 return ret;
502}
Tomas Winkler27aaba02008-05-05 10:22:44 +0800503EXPORT_SYMBOL(iwl_remove_default_wep_key);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700504
505int iwl_set_default_wep_key(struct iwl_priv *priv,
506 struct ieee80211_key_conf *keyconf)
507{
508 int ret;
509 unsigned long flags;
510
511 keyconf->flags &= ~IEEE80211_KEY_FLAG_GENERATE_IV;
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +0800512 keyconf->hw_key_idx = HW_KEY_DEFAULT;
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700513 priv->stations[IWL_AP_ID].keyinfo.alg = ALG_WEP;
514
515 spin_lock_irqsave(&priv->sta_lock, flags);
516 priv->default_wep_key++;
517
Emmanuel Grumbach80fb47a2008-04-14 21:16:08 -0700518 if (test_and_set_bit(keyconf->keyidx, &priv->ucode_key_table))
519 IWL_ERROR("index %d already used in uCode key table.\n",
520 keyconf->keyidx);
521
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700522 priv->wep_keys[keyconf->keyidx].key_size = keyconf->keylen;
523 memcpy(&priv->wep_keys[keyconf->keyidx].key, &keyconf->key,
524 keyconf->keylen);
525
526 ret = iwl_send_static_wepkey_cmd(priv, 0);
527 spin_unlock_irqrestore(&priv->sta_lock, flags);
528
529 return ret;
530}
Tomas Winkler27aaba02008-05-05 10:22:44 +0800531EXPORT_SYMBOL(iwl_set_default_wep_key);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700532
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700533static int iwl_set_wep_dynamic_key_info(struct iwl_priv *priv,
Emmanuel Grumbach0211ddd2008-04-14 21:16:07 -0700534 struct ieee80211_key_conf *keyconf,
535 u8 sta_id)
536{
537 unsigned long flags;
538 __le16 key_flags = 0;
539 int ret;
540
541 keyconf->flags &= ~IEEE80211_KEY_FLAG_GENERATE_IV;
Emmanuel Grumbach0211ddd2008-04-14 21:16:07 -0700542
543 key_flags |= (STA_KEY_FLG_WEP | STA_KEY_FLG_MAP_KEY_MSK);
544 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
545 key_flags &= ~STA_KEY_FLG_INVALID;
546
547 if (keyconf->keylen == WEP_KEY_LEN_128)
548 key_flags |= STA_KEY_FLG_KEY_SIZE_MSK;
549
Tomas Winkler5425e492008-04-15 16:01:38 -0700550 if (sta_id == priv->hw_params.bcast_sta_id)
Emmanuel Grumbach0211ddd2008-04-14 21:16:07 -0700551 key_flags |= STA_KEY_MULTICAST_MSK;
552
553 spin_lock_irqsave(&priv->sta_lock, flags);
554
555 priv->stations[sta_id].keyinfo.alg = keyconf->alg;
556 priv->stations[sta_id].keyinfo.keylen = keyconf->keylen;
557 priv->stations[sta_id].keyinfo.keyidx = keyconf->keyidx;
558
559 memcpy(priv->stations[sta_id].keyinfo.key,
560 keyconf->key, keyconf->keylen);
561
562 memcpy(&priv->stations[sta_id].sta.key.key[3],
563 keyconf->key, keyconf->keylen);
564
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -0700565 if ((priv->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
566 == STA_KEY_FLG_NO_ENC)
567 priv->stations[sta_id].sta.key.key_offset =
Emmanuel Grumbach80fb47a2008-04-14 21:16:08 -0700568 iwl_get_free_ucode_key_index(priv);
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -0700569 /* else, we are overriding an existing key => no need to allocated room
570 * in uCode. */
Emmanuel Grumbach0211ddd2008-04-14 21:16:07 -0700571
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -0700572 priv->stations[sta_id].sta.key.key_flags = key_flags;
Emmanuel Grumbach0211ddd2008-04-14 21:16:07 -0700573 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
574 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
575
Tomas Winkler133636d2008-05-05 10:22:34 +0800576 ret = iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
Emmanuel Grumbach0211ddd2008-04-14 21:16:07 -0700577
578 spin_unlock_irqrestore(&priv->sta_lock, flags);
579
580 return ret;
581}
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700582
583static int iwl_set_ccmp_dynamic_key_info(struct iwl_priv *priv,
584 struct ieee80211_key_conf *keyconf,
585 u8 sta_id)
586{
587 unsigned long flags;
588 __le16 key_flags = 0;
589
590 key_flags |= (STA_KEY_FLG_CCMP | STA_KEY_FLG_MAP_KEY_MSK);
591 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
592 key_flags &= ~STA_KEY_FLG_INVALID;
593
Tomas Winkler5425e492008-04-15 16:01:38 -0700594 if (sta_id == priv->hw_params.bcast_sta_id)
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700595 key_flags |= STA_KEY_MULTICAST_MSK;
596
597 keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700598
599 spin_lock_irqsave(&priv->sta_lock, flags);
600 priv->stations[sta_id].keyinfo.alg = keyconf->alg;
601 priv->stations[sta_id].keyinfo.keylen = keyconf->keylen;
602
603 memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key,
604 keyconf->keylen);
605
606 memcpy(priv->stations[sta_id].sta.key.key, keyconf->key,
607 keyconf->keylen);
608
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -0700609 if ((priv->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
610 == STA_KEY_FLG_NO_ENC)
611 priv->stations[sta_id].sta.key.key_offset =
612 iwl_get_free_ucode_key_index(priv);
613 /* else, we are overriding an existing key => no need to allocated room
614 * in uCode. */
615
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700616 priv->stations[sta_id].sta.key.key_flags = key_flags;
617 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
618 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
619
620 spin_unlock_irqrestore(&priv->sta_lock, flags);
621
622 IWL_DEBUG_INFO("hwcrypto: modify ucode station key info\n");
Tomas Winkler133636d2008-05-05 10:22:34 +0800623 return iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700624}
625
626static int iwl_set_tkip_dynamic_key_info(struct iwl_priv *priv,
627 struct ieee80211_key_conf *keyconf,
628 u8 sta_id)
629{
630 unsigned long flags;
631 int ret = 0;
632
633 keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
634 keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700635
636 spin_lock_irqsave(&priv->sta_lock, flags);
637
638 priv->stations[sta_id].keyinfo.alg = keyconf->alg;
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700639 priv->stations[sta_id].keyinfo.keylen = 16;
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -0700640
641 if ((priv->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
642 == STA_KEY_FLG_NO_ENC)
643 priv->stations[sta_id].sta.key.key_offset =
Emmanuel Grumbach77bab602008-04-15 16:01:44 -0700644 iwl_get_free_ucode_key_index(priv);
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -0700645 /* else, we are overriding an existing key => no need to allocated room
646 * in uCode. */
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700647
648 /* This copy is acutally not needed: we get the key with each TX */
649 memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key, 16);
650
651 memcpy(priv->stations[sta_id].sta.key.key, keyconf->key, 16);
652
653 spin_unlock_irqrestore(&priv->sta_lock, flags);
654
655 return ret;
656}
657
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -0700658int iwl_remove_dynamic_key(struct iwl_priv *priv,
659 struct ieee80211_key_conf *keyconf,
660 u8 sta_id)
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700661{
662 unsigned long flags;
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -0700663 int ret = 0;
664 u16 key_flags;
665 u8 keyidx;
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700666
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +0800667 priv->key_mapping_key--;
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700668
669 spin_lock_irqsave(&priv->sta_lock, flags);
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -0700670 key_flags = le16_to_cpu(priv->stations[sta_id].sta.key.key_flags);
671 keyidx = (key_flags >> STA_KEY_FLG_KEYID_POS) & 0x3;
672
673 if (keyconf->keyidx != keyidx) {
674 /* We need to remove a key with index different that the one
675 * in the uCode. This means that the key we need to remove has
676 * been replaced by another one with different index.
677 * Don't do anything and return ok
678 */
679 spin_unlock_irqrestore(&priv->sta_lock, flags);
680 return 0;
681 }
682
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700683 if (!test_and_clear_bit(priv->stations[sta_id].sta.key.key_offset,
684 &priv->ucode_key_table))
685 IWL_ERROR("index %d not used in uCode key table.\n",
686 priv->stations[sta_id].sta.key.key_offset);
687 memset(&priv->stations[sta_id].keyinfo, 0,
Tomas Winkler6def9762008-05-05 10:22:31 +0800688 sizeof(struct iwl_hw_key));
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700689 memset(&priv->stations[sta_id].sta.key, 0,
690 sizeof(struct iwl4965_keyinfo));
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -0700691 priv->stations[sta_id].sta.key.key_flags =
692 STA_KEY_FLG_NO_ENC | STA_KEY_FLG_INVALID;
693 priv->stations[sta_id].sta.key.key_offset = WEP_INVALID_OFFSET;
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700694 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
695 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700696
697 IWL_DEBUG_INFO("hwcrypto: clear ucode station key info\n");
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +0800698 ret = iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -0700699 spin_unlock_irqrestore(&priv->sta_lock, flags);
700 return ret;
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700701}
Tomas Winkler27aaba02008-05-05 10:22:44 +0800702EXPORT_SYMBOL(iwl_remove_dynamic_key);
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700703
704int iwl_set_dynamic_key(struct iwl_priv *priv,
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +0800705 struct ieee80211_key_conf *keyconf, u8 sta_id)
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700706{
707 int ret;
708
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +0800709 priv->key_mapping_key++;
710 keyconf->hw_key_idx = HW_KEY_DYNAMIC;
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700711
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +0800712 switch (keyconf->alg) {
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700713 case ALG_CCMP:
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +0800714 ret = iwl_set_ccmp_dynamic_key_info(priv, keyconf, sta_id);
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700715 break;
716 case ALG_TKIP:
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +0800717 ret = iwl_set_tkip_dynamic_key_info(priv, keyconf, sta_id);
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700718 break;
719 case ALG_WEP:
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +0800720 ret = iwl_set_wep_dynamic_key_info(priv, keyconf, sta_id);
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700721 break;
722 default:
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +0800723 IWL_ERROR("Unknown alg: %s alg = %d\n", __func__, keyconf->alg);
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700724 ret = -EINVAL;
725 }
726
727 return ret;
728}
Tomas Winkler27aaba02008-05-05 10:22:44 +0800729EXPORT_SYMBOL(iwl_set_dynamic_key);
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700730
Tomas Winkler66c73db2008-04-15 16:01:40 -0700731#ifdef CONFIG_IWLWIFI_DEBUG
732static void iwl_dump_lq_cmd(struct iwl_priv *priv,
733 struct iwl_link_quality_cmd *lq)
734{
735 int i;
736 IWL_DEBUG_RATE("lq station id 0x%x\n", lq->sta_id);
737 IWL_DEBUG_RATE("lq dta 0x%X 0x%X\n",
738 lq->general_params.single_stream_ant_msk,
739 lq->general_params.dual_stream_ant_msk);
740
741 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
742 IWL_DEBUG_RATE("lq index %d 0x%X\n",
743 i, lq->rs_table[i].rate_n_flags);
744}
745#else
746static inline void iwl_dump_lq_cmd(struct iwl_priv *priv,
747 struct iwl_link_quality_cmd *lq)
748{
749}
750#endif
751
752int iwl_send_lq_cmd(struct iwl_priv *priv,
753 struct iwl_link_quality_cmd *lq, u8 flags)
754{
755 struct iwl_host_cmd cmd = {
756 .id = REPLY_TX_LINK_QUALITY_CMD,
757 .len = sizeof(struct iwl_link_quality_cmd),
758 .meta.flags = flags,
759 .data = lq,
760 };
761
762 if ((lq->sta_id == 0xFF) &&
763 (priv->iw_mode == IEEE80211_IF_TYPE_IBSS))
764 return -EINVAL;
765
766 if (lq->sta_id == 0xFF)
767 lq->sta_id = IWL_AP_ID;
768
769 iwl_dump_lq_cmd(priv,lq);
770
771 if (iwl_is_associated(priv) && priv->assoc_station_added &&
772 priv->lq_mngr.lq_ready)
773 return iwl_send_cmd(priv, &cmd);
774
775 return 0;
776}
777EXPORT_SYMBOL(iwl_send_lq_cmd);
778
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800779/**
780 * iwl_sta_init_lq - Initialize a station's hardware rate table
781 *
782 * The uCode's station table contains a table of fallback rates
783 * for automatic fallback during transmission.
784 *
785 * NOTE: This sets up a default set of values. These will be replaced later
786 * if the driver's iwl-4965-rs rate scaling algorithm is used, instead of
787 * rc80211_simple.
788 *
789 * NOTE: Run REPLY_ADD_STA command to set up station table entry, before
790 * calling this function (which runs REPLY_TX_LINK_QUALITY_CMD,
791 * which requires station table entry to exist).
792 */
793static void iwl_sta_init_lq(struct iwl_priv *priv, const u8 *addr, int is_ap)
794{
795 int i, r;
796 struct iwl_link_quality_cmd link_cmd = {
797 .reserved1 = 0,
798 };
799 u16 rate_flags;
800
801 /* Set up the rate scaling to start at selected rate, fall back
802 * all the way down to 1M in IEEE order, and then spin on 1M */
803 if (is_ap)
804 r = IWL_RATE_54M_INDEX;
805 else if (priv->band == IEEE80211_BAND_5GHZ)
806 r = IWL_RATE_6M_INDEX;
807 else
808 r = IWL_RATE_1M_INDEX;
809
810 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
811 rate_flags = 0;
812 if (r >= IWL_FIRST_CCK_RATE && r <= IWL_LAST_CCK_RATE)
813 rate_flags |= RATE_MCS_CCK_MSK;
814
815 /* Use Tx antenna B only */
816 rate_flags |= RATE_MCS_ANT_B_MSK; /*FIXME:RS*/
817
818 link_cmd.rs_table[i].rate_n_flags =
819 iwl4965_hw_set_rate_n_flags(iwl_rates[r].plcp, rate_flags);
820 r = iwl4965_get_prev_ieee_rate(r);
821 }
822
823 link_cmd.general_params.single_stream_ant_msk = 2;
824 link_cmd.general_params.dual_stream_ant_msk = 3;
825 link_cmd.agg_params.agg_dis_start_th = 3;
826 link_cmd.agg_params.agg_time_limit = cpu_to_le16(4000);
827
828 /* Update the rate scaling for control frame Tx to AP */
829 link_cmd.sta_id = is_ap ? IWL_AP_ID : priv->hw_params.bcast_sta_id;
830
831 iwl_send_cmd_pdu_async(priv, REPLY_TX_LINK_QUALITY_CMD,
832 sizeof(link_cmd), &link_cmd, NULL);
833}
834/**
835 * iwl_rxon_add_station - add station into station table.
836 *
837 * there is only one AP station with id= IWL_AP_ID
838 * NOTE: mutex must be held before calling this fnction
839 */
840int iwl_rxon_add_station(struct iwl_priv *priv, const u8 *addr, int is_ap)
841{
842 u8 sta_id;
843
844 /* Add station to device's station table */
845#ifdef CONFIG_IWL4965_HT
846 struct ieee80211_conf *conf = &priv->hw->conf;
847 struct ieee80211_ht_info *cur_ht_config = &conf->ht_conf;
848
849 if ((is_ap) &&
850 (conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) &&
851 (priv->iw_mode == IEEE80211_IF_TYPE_STA))
852 sta_id = iwl_add_station_flags(priv, addr, is_ap,
853 0, cur_ht_config);
854 else
855#endif /* CONFIG_IWL4965_HT */
856 sta_id = iwl_add_station_flags(priv, addr, is_ap,
857 0, NULL);
858
859 /* Set up default rate scaling table in device's station table */
860 iwl_sta_init_lq(priv, addr, is_ap);
861
862 return sta_id;
863}
864EXPORT_SYMBOL(iwl_rxon_add_station);
865
866
867/**
868 * iwl_get_sta_id - Find station's index within station table
869 *
870 * If new IBSS station, create new entry in station table
871 */
872int iwl_get_sta_id(struct iwl_priv *priv, struct ieee80211_hdr *hdr)
873{
874 int sta_id;
875 u16 fc = le16_to_cpu(hdr->frame_control);
876 DECLARE_MAC_BUF(mac);
877
878 /* If this frame is broadcast or management, use broadcast station id */
879 if (((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA) ||
880 is_multicast_ether_addr(hdr->addr1))
881 return priv->hw_params.bcast_sta_id;
882
883 switch (priv->iw_mode) {
884
885 /* If we are a client station in a BSS network, use the special
886 * AP station entry (that's the only station we communicate with) */
887 case IEEE80211_IF_TYPE_STA:
888 return IWL_AP_ID;
889
890 /* If we are an AP, then find the station, or use BCAST */
891 case IEEE80211_IF_TYPE_AP:
892 sta_id = iwl_find_station(priv, hdr->addr1);
893 if (sta_id != IWL_INVALID_STATION)
894 return sta_id;
895 return priv->hw_params.bcast_sta_id;
896
897 /* If this frame is going out to an IBSS network, find the station,
898 * or create a new station table entry */
899 case IEEE80211_IF_TYPE_IBSS:
900 sta_id = iwl_find_station(priv, hdr->addr1);
901 if (sta_id != IWL_INVALID_STATION)
902 return sta_id;
903
904 /* Create new station table entry */
905 sta_id = iwl_add_station_flags(priv, hdr->addr1,
906 0, CMD_ASYNC, NULL);
907
908 if (sta_id != IWL_INVALID_STATION)
909 return sta_id;
910
911 IWL_DEBUG_DROP("Station %s not in station map. "
912 "Defaulting to broadcast...\n",
913 print_mac(mac, hdr->addr1));
914 iwl_print_hex_dump(priv, IWL_DL_DROP, (u8 *) hdr, sizeof(*hdr));
915 return priv->hw_params.bcast_sta_id;
916
917 default:
918 IWL_WARNING("Unknown mode of operation: %d", priv->iw_mode);
919 return priv->hw_params.bcast_sta_id;
920 }
921}
922EXPORT_SYMBOL(iwl_get_sta_id);
923