blob: 11ec408f99c52c673d16fc9671982f4c495ab0c8 [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 Winkler133636d2008-05-05 10:22:34 +080077int iwl_send_add_sta(struct iwl_priv *priv,
78 struct iwl_addsta_cmd *sta, u8 flags)
79{
80 struct iwl_rx_packet *res = NULL;
81 int ret = 0;
82 u8 data[sizeof(*sta)];
83 struct iwl_host_cmd cmd = {
84 .id = REPLY_ADD_STA,
85 .meta.flags = flags,
86 .data = data,
87 };
88
89 if (!(flags & CMD_ASYNC))
90 cmd.meta.flags |= CMD_WANT_SKB;
91
92 cmd.len = priv->cfg->ops->utils->build_addsta_hcmd(sta, data);
93 ret = iwl_send_cmd(priv, &cmd);
94
95 if (ret || (flags & CMD_ASYNC))
96 return ret;
97
98 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
99 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
100 IWL_ERROR("Bad return from REPLY_ADD_STA (0x%08X)\n",
101 res->hdr.flags);
102 ret = -EIO;
103 }
104
105 if (ret == 0) {
106 switch (res->u.add_sta.status) {
107 case ADD_STA_SUCCESS_MSK:
108 IWL_DEBUG_INFO("REPLY_ADD_STA PASSED\n");
109 break;
110 default:
111 ret = -EIO;
112 IWL_WARNING("REPLY_ADD_STA failed\n");
113 break;
114 }
115 }
116
117 priv->alloc_rxb_skb--;
118 dev_kfree_skb_any(cmd.meta.u.skb);
119
120 return ret;
121}
122EXPORT_SYMBOL(iwl_send_add_sta);
Tomas Winkler947b13a2008-04-16 16:34:48 -0700123
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800124#ifdef CONFIG_IWL4965_HT
125
126static void iwl_set_ht_add_station(struct iwl_priv *priv, u8 index,
127 struct ieee80211_ht_info *sta_ht_inf)
128{
129 __le32 sta_flags;
130 u8 mimo_ps_mode;
131
132 if (!sta_ht_inf || !sta_ht_inf->ht_supported)
133 goto done;
134
135 mimo_ps_mode = (sta_ht_inf->cap & IEEE80211_HT_CAP_MIMO_PS) >> 2;
136
137 sta_flags = priv->stations[index].sta.station_flags;
138
139 sta_flags &= ~(STA_FLG_RTS_MIMO_PROT_MSK | STA_FLG_MIMO_DIS_MSK);
140
141 switch (mimo_ps_mode) {
142 case WLAN_HT_CAP_MIMO_PS_STATIC:
143 sta_flags |= STA_FLG_MIMO_DIS_MSK;
144 break;
145 case WLAN_HT_CAP_MIMO_PS_DYNAMIC:
146 sta_flags |= STA_FLG_RTS_MIMO_PROT_MSK;
147 break;
148 case WLAN_HT_CAP_MIMO_PS_DISABLED:
149 break;
150 default:
151 IWL_WARNING("Invalid MIMO PS mode %d", mimo_ps_mode);
152 break;
153 }
154
155 sta_flags |= cpu_to_le32(
156 (u32)sta_ht_inf->ampdu_factor << STA_FLG_MAX_AGG_SIZE_POS);
157
158 sta_flags |= cpu_to_le32(
159 (u32)sta_ht_inf->ampdu_density << STA_FLG_AGG_MPDU_DENSITY_POS);
160
161 if (iwl_is_fat_tx_allowed(priv, sta_ht_inf))
162 sta_flags |= STA_FLG_FAT_EN_MSK;
163 else
164 sta_flags &= ~STA_FLG_FAT_EN_MSK;
165
166 priv->stations[index].sta.station_flags = sta_flags;
167 done:
168 return;
169}
170#else
171static inline void iwl_set_ht_add_station(struct iwl_priv *priv, u8 index,
172 struct ieee80211_ht_info *sta_ht_info)
173{
174}
175#endif
176
177/**
178 * iwl_add_station_flags - Add station to tables in driver and device
179 */
180u8 iwl_add_station_flags(struct iwl_priv *priv, const u8 *addr, int is_ap,
181 u8 flags, struct ieee80211_ht_info *ht_info)
182{
183 int i;
184 int index = IWL_INVALID_STATION;
185 struct iwl_station_entry *station;
186 unsigned long flags_spin;
187 DECLARE_MAC_BUF(mac);
188
189 spin_lock_irqsave(&priv->sta_lock, flags_spin);
190 if (is_ap)
191 index = IWL_AP_ID;
192 else if (is_broadcast_ether_addr(addr))
193 index = priv->hw_params.bcast_sta_id;
194 else
195 for (i = IWL_STA_ID; i < priv->hw_params.max_stations; i++) {
196 if (!compare_ether_addr(priv->stations[i].sta.sta.addr,
197 addr)) {
198 index = i;
199 break;
200 }
201
202 if (!priv->stations[i].used &&
203 index == IWL_INVALID_STATION)
204 index = i;
205 }
206
207
208 /* These two conditions have the same outcome, but keep them separate
209 since they have different meanings */
210 if (unlikely(index == IWL_INVALID_STATION)) {
211 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
212 return index;
213 }
214
215 if (priv->stations[index].used &&
216 !compare_ether_addr(priv->stations[index].sta.sta.addr, addr)) {
217 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
218 return index;
219 }
220
221
222 IWL_DEBUG_ASSOC("Add STA ID %d: %s\n", index, print_mac(mac, addr));
223 station = &priv->stations[index];
224 station->used = 1;
225 priv->num_stations++;
226
227 /* Set up the REPLY_ADD_STA command to send to device */
228 memset(&station->sta, 0, sizeof(struct iwl_addsta_cmd));
229 memcpy(station->sta.sta.addr, addr, ETH_ALEN);
230 station->sta.mode = 0;
231 station->sta.sta.sta_id = index;
232 station->sta.station_flags = 0;
233
234 /* BCAST station and IBSS stations do not work in HT mode */
235 if (index != priv->hw_params.bcast_sta_id &&
236 priv->iw_mode != IEEE80211_IF_TYPE_IBSS)
237 iwl_set_ht_add_station(priv, index, ht_info);
238
239 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
240
241 /* Add station to device's station table */
242 iwl_send_add_sta(priv, &station->sta, flags);
243 return index;
244
245}
246EXPORT_SYMBOL(iwl_add_station_flags);
247
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800248
249static int iwl_sta_ucode_deactivate(struct iwl_priv *priv, const char *addr)
250{
251 unsigned long flags;
252 u8 sta_id;
253 DECLARE_MAC_BUF(mac);
254
255 sta_id = iwl_find_station(priv, addr);
256 if (sta_id != IWL_INVALID_STATION) {
257 IWL_DEBUG_ASSOC("Removed STA from Ucode: %s\n",
258 print_mac(mac, addr));
259 spin_lock_irqsave(&priv->sta_lock, flags);
260 priv->stations[sta_id].used &= ~IWL_STA_UCODE_ACTIVE;
261 memset(&priv->stations[sta_id], 0,
262 sizeof(struct iwl_station_entry));
263 spin_unlock_irqrestore(&priv->sta_lock, flags);
264 return 0;
265 }
266 return -EINVAL;
267}
268
269static int iwl_remove_sta_callback(struct iwl_priv *priv,
270 struct iwl_cmd *cmd, struct sk_buff *skb)
271{
272 struct iwl_rx_packet *res = NULL;
273 const char *addr = cmd->cmd.rm_sta.addr;
274
275 if (!skb) {
276 IWL_ERROR("Error: Response NULL in REPLY_REMOVE_STA.\n");
277 return 1;
278 }
279
280 res = (struct iwl_rx_packet *)skb->data;
281 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
282 IWL_ERROR("Bad return from REPLY_REMOVE_STA (0x%08X)\n",
283 res->hdr.flags);
284 return 1;
285 }
286
287 switch (res->u.rem_sta.status) {
288 case REM_STA_SUCCESS_MSK:
289 iwl_sta_ucode_deactivate(priv, addr);
290 break;
291 default:
292 break;
293 }
294
295 /* We didn't cache the SKB; let the caller free it */
296 return 1;
297}
298
299static int iwl_send_remove_station(struct iwl_priv *priv, const u8 *addr,
300 u8 flags)
301{
302 struct iwl_rx_packet *res = NULL;
303 int ret;
304
305 struct iwl_rem_sta_cmd rm_sta_cmd;
306
307 struct iwl_host_cmd cmd = {
308 .id = REPLY_REMOVE_STA,
309 .len = sizeof(struct iwl_rem_sta_cmd),
310 .meta.flags = flags,
311 .data = &rm_sta_cmd,
312 };
313
314 memset(&rm_sta_cmd, 0, sizeof(rm_sta_cmd));
315 rm_sta_cmd.num_sta = 1;
316 memcpy(&rm_sta_cmd.addr, addr , ETH_ALEN);
317
318 if (flags & CMD_ASYNC)
319 cmd.meta.u.callback = iwl_remove_sta_callback;
320 else
321 cmd.meta.flags |= CMD_WANT_SKB;
322 ret = iwl_send_cmd(priv, &cmd);
323
324 if (ret || (flags & CMD_ASYNC))
325 return ret;
326
327 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
328 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
329 IWL_ERROR("Bad return from REPLY_REMOVE_STA (0x%08X)\n",
330 res->hdr.flags);
331 ret = -EIO;
332 }
333
334 if (!ret) {
335 switch (res->u.rem_sta.status) {
336 case REM_STA_SUCCESS_MSK:
337 iwl_sta_ucode_deactivate(priv, addr);
338 IWL_DEBUG_ASSOC("REPLY_REMOVE_STA PASSED\n");
339 break;
340 default:
341 ret = -EIO;
342 IWL_ERROR("REPLY_REMOVE_STA failed\n");
343 break;
344 }
345 }
346
347 priv->alloc_rxb_skb--;
348 dev_kfree_skb_any(cmd.meta.u.skb);
349
350 return ret;
351}
352/**
353 * iwl_remove_station - Remove driver's knowledge of station.
354 *
355 */
356u8 iwl_remove_station(struct iwl_priv *priv, const u8 *addr, int is_ap)
357{
358 int index = IWL_INVALID_STATION;
359 int i;
360 unsigned long flags;
361
362 spin_lock_irqsave(&priv->sta_lock, flags);
363
364 if (is_ap)
365 index = IWL_AP_ID;
366 else if (is_broadcast_ether_addr(addr))
367 index = priv->hw_params.bcast_sta_id;
368 else
369 for (i = IWL_STA_ID; i < priv->hw_params.max_stations; i++)
370 if (priv->stations[i].used &&
371 !compare_ether_addr(priv->stations[i].sta.sta.addr,
372 addr)) {
373 index = i;
374 break;
375 }
376
377 if (unlikely(index == IWL_INVALID_STATION))
378 goto out;
379
380 if (priv->stations[index].used) {
381 priv->stations[index].used = 0;
382 priv->num_stations--;
383 }
384
385 BUG_ON(priv->num_stations < 0);
386 spin_unlock_irqrestore(&priv->sta_lock, flags);
387 iwl_send_remove_station(priv, addr, CMD_ASYNC);
388 return index;
389out:
390 spin_unlock_irqrestore(&priv->sta_lock, flags);
391 return 0;
392}
393EXPORT_SYMBOL(iwl_remove_station);
Emmanuel Grumbach80fb47a2008-04-14 21:16:08 -0700394int iwl_get_free_ucode_key_index(struct iwl_priv *priv)
395{
396 int i;
397
398 for (i = 0; i < STA_KEY_MAX_NUM; i++)
Emmanuel Grumbach77bab602008-04-15 16:01:44 -0700399 if (!test_and_set_bit(i, &priv->ucode_key_table))
Emmanuel Grumbach80fb47a2008-04-14 21:16:08 -0700400 return i;
401
402 return -1;
403}
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700404
405int iwl_send_static_wepkey_cmd(struct iwl_priv *priv, u8 send_if_empty)
406{
407 int i, not_empty = 0;
408 u8 buff[sizeof(struct iwl_wep_cmd) +
409 sizeof(struct iwl_wep_key) * WEP_KEYS_MAX];
410 struct iwl_wep_cmd *wep_cmd = (struct iwl_wep_cmd *)buff;
411 size_t cmd_size = sizeof(struct iwl_wep_cmd);
412 struct iwl_host_cmd cmd = {
413 .id = REPLY_WEPKEY,
414 .data = wep_cmd,
415 .meta.flags = CMD_ASYNC,
416 };
417
418 memset(wep_cmd, 0, cmd_size +
419 (sizeof(struct iwl_wep_key) * WEP_KEYS_MAX));
420
421 for (i = 0; i < WEP_KEYS_MAX ; i++) {
422 wep_cmd->key[i].key_index = i;
423 if (priv->wep_keys[i].key_size) {
424 wep_cmd->key[i].key_offset = i;
425 not_empty = 1;
426 } else {
427 wep_cmd->key[i].key_offset = WEP_INVALID_OFFSET;
428 }
429
430 wep_cmd->key[i].key_size = priv->wep_keys[i].key_size;
431 memcpy(&wep_cmd->key[i].key[3], priv->wep_keys[i].key,
432 priv->wep_keys[i].key_size);
433 }
434
435 wep_cmd->global_key_type = WEP_KEY_WEP_TYPE;
436 wep_cmd->num_keys = WEP_KEYS_MAX;
437
438 cmd_size += sizeof(struct iwl_wep_key) * WEP_KEYS_MAX;
439
440 cmd.len = cmd_size;
441
442 if (not_empty || send_if_empty)
443 return iwl_send_cmd(priv, &cmd);
444 else
445 return 0;
446}
Tomas Winkler27aaba02008-05-05 10:22:44 +0800447EXPORT_SYMBOL(iwl_send_static_wepkey_cmd);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700448
449int iwl_remove_default_wep_key(struct iwl_priv *priv,
Emmanuel Grumbach80fb47a2008-04-14 21:16:08 -0700450 struct ieee80211_key_conf *keyconf)
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700451{
452 int ret;
453 unsigned long flags;
454
455 spin_lock_irqsave(&priv->sta_lock, flags);
Emmanuel Grumbach80fb47a2008-04-14 21:16:08 -0700456
457 if (!test_and_clear_bit(keyconf->keyidx, &priv->ucode_key_table))
458 IWL_ERROR("index %d not used in uCode key table.\n",
459 keyconf->keyidx);
460
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700461 priv->default_wep_key--;
Emmanuel Grumbach80fb47a2008-04-14 21:16:08 -0700462 memset(&priv->wep_keys[keyconf->keyidx], 0, sizeof(priv->wep_keys[0]));
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700463 ret = iwl_send_static_wepkey_cmd(priv, 1);
464 spin_unlock_irqrestore(&priv->sta_lock, flags);
465
466 return ret;
467}
Tomas Winkler27aaba02008-05-05 10:22:44 +0800468EXPORT_SYMBOL(iwl_remove_default_wep_key);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700469
470int iwl_set_default_wep_key(struct iwl_priv *priv,
471 struct ieee80211_key_conf *keyconf)
472{
473 int ret;
474 unsigned long flags;
475
476 keyconf->flags &= ~IEEE80211_KEY_FLAG_GENERATE_IV;
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +0800477 keyconf->hw_key_idx = HW_KEY_DEFAULT;
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700478 priv->stations[IWL_AP_ID].keyinfo.alg = ALG_WEP;
479
480 spin_lock_irqsave(&priv->sta_lock, flags);
481 priv->default_wep_key++;
482
Emmanuel Grumbach80fb47a2008-04-14 21:16:08 -0700483 if (test_and_set_bit(keyconf->keyidx, &priv->ucode_key_table))
484 IWL_ERROR("index %d already used in uCode key table.\n",
485 keyconf->keyidx);
486
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700487 priv->wep_keys[keyconf->keyidx].key_size = keyconf->keylen;
488 memcpy(&priv->wep_keys[keyconf->keyidx].key, &keyconf->key,
489 keyconf->keylen);
490
491 ret = iwl_send_static_wepkey_cmd(priv, 0);
492 spin_unlock_irqrestore(&priv->sta_lock, flags);
493
494 return ret;
495}
Tomas Winkler27aaba02008-05-05 10:22:44 +0800496EXPORT_SYMBOL(iwl_set_default_wep_key);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700497
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700498static int iwl_set_wep_dynamic_key_info(struct iwl_priv *priv,
Emmanuel Grumbach0211ddd2008-04-14 21:16:07 -0700499 struct ieee80211_key_conf *keyconf,
500 u8 sta_id)
501{
502 unsigned long flags;
503 __le16 key_flags = 0;
504 int ret;
505
506 keyconf->flags &= ~IEEE80211_KEY_FLAG_GENERATE_IV;
Emmanuel Grumbach0211ddd2008-04-14 21:16:07 -0700507
508 key_flags |= (STA_KEY_FLG_WEP | STA_KEY_FLG_MAP_KEY_MSK);
509 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
510 key_flags &= ~STA_KEY_FLG_INVALID;
511
512 if (keyconf->keylen == WEP_KEY_LEN_128)
513 key_flags |= STA_KEY_FLG_KEY_SIZE_MSK;
514
Tomas Winkler5425e492008-04-15 16:01:38 -0700515 if (sta_id == priv->hw_params.bcast_sta_id)
Emmanuel Grumbach0211ddd2008-04-14 21:16:07 -0700516 key_flags |= STA_KEY_MULTICAST_MSK;
517
518 spin_lock_irqsave(&priv->sta_lock, flags);
519
520 priv->stations[sta_id].keyinfo.alg = keyconf->alg;
521 priv->stations[sta_id].keyinfo.keylen = keyconf->keylen;
522 priv->stations[sta_id].keyinfo.keyidx = keyconf->keyidx;
523
524 memcpy(priv->stations[sta_id].keyinfo.key,
525 keyconf->key, keyconf->keylen);
526
527 memcpy(&priv->stations[sta_id].sta.key.key[3],
528 keyconf->key, keyconf->keylen);
529
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -0700530 if ((priv->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
531 == STA_KEY_FLG_NO_ENC)
532 priv->stations[sta_id].sta.key.key_offset =
Emmanuel Grumbach80fb47a2008-04-14 21:16:08 -0700533 iwl_get_free_ucode_key_index(priv);
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -0700534 /* else, we are overriding an existing key => no need to allocated room
535 * in uCode. */
Emmanuel Grumbach0211ddd2008-04-14 21:16:07 -0700536
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -0700537 priv->stations[sta_id].sta.key.key_flags = key_flags;
Emmanuel Grumbach0211ddd2008-04-14 21:16:07 -0700538 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
539 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
540
Tomas Winkler133636d2008-05-05 10:22:34 +0800541 ret = iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
Emmanuel Grumbach0211ddd2008-04-14 21:16:07 -0700542
543 spin_unlock_irqrestore(&priv->sta_lock, flags);
544
545 return ret;
546}
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700547
548static int iwl_set_ccmp_dynamic_key_info(struct iwl_priv *priv,
549 struct ieee80211_key_conf *keyconf,
550 u8 sta_id)
551{
552 unsigned long flags;
553 __le16 key_flags = 0;
554
555 key_flags |= (STA_KEY_FLG_CCMP | STA_KEY_FLG_MAP_KEY_MSK);
556 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
557 key_flags &= ~STA_KEY_FLG_INVALID;
558
Tomas Winkler5425e492008-04-15 16:01:38 -0700559 if (sta_id == priv->hw_params.bcast_sta_id)
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700560 key_flags |= STA_KEY_MULTICAST_MSK;
561
562 keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700563
564 spin_lock_irqsave(&priv->sta_lock, flags);
565 priv->stations[sta_id].keyinfo.alg = keyconf->alg;
566 priv->stations[sta_id].keyinfo.keylen = keyconf->keylen;
567
568 memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key,
569 keyconf->keylen);
570
571 memcpy(priv->stations[sta_id].sta.key.key, keyconf->key,
572 keyconf->keylen);
573
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -0700574 if ((priv->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
575 == STA_KEY_FLG_NO_ENC)
576 priv->stations[sta_id].sta.key.key_offset =
577 iwl_get_free_ucode_key_index(priv);
578 /* else, we are overriding an existing key => no need to allocated room
579 * in uCode. */
580
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700581 priv->stations[sta_id].sta.key.key_flags = key_flags;
582 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
583 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
584
585 spin_unlock_irqrestore(&priv->sta_lock, flags);
586
587 IWL_DEBUG_INFO("hwcrypto: modify ucode station key info\n");
Tomas Winkler133636d2008-05-05 10:22:34 +0800588 return iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700589}
590
591static int iwl_set_tkip_dynamic_key_info(struct iwl_priv *priv,
592 struct ieee80211_key_conf *keyconf,
593 u8 sta_id)
594{
595 unsigned long flags;
596 int ret = 0;
597
598 keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
599 keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700600
601 spin_lock_irqsave(&priv->sta_lock, flags);
602
603 priv->stations[sta_id].keyinfo.alg = keyconf->alg;
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700604 priv->stations[sta_id].keyinfo.keylen = 16;
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -0700605
606 if ((priv->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
607 == STA_KEY_FLG_NO_ENC)
608 priv->stations[sta_id].sta.key.key_offset =
Emmanuel Grumbach77bab602008-04-15 16:01:44 -0700609 iwl_get_free_ucode_key_index(priv);
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -0700610 /* else, we are overriding an existing key => no need to allocated room
611 * in uCode. */
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700612
613 /* This copy is acutally not needed: we get the key with each TX */
614 memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key, 16);
615
616 memcpy(priv->stations[sta_id].sta.key.key, keyconf->key, 16);
617
618 spin_unlock_irqrestore(&priv->sta_lock, flags);
619
620 return ret;
621}
622
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -0700623int iwl_remove_dynamic_key(struct iwl_priv *priv,
624 struct ieee80211_key_conf *keyconf,
625 u8 sta_id)
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700626{
627 unsigned long flags;
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -0700628 int ret = 0;
629 u16 key_flags;
630 u8 keyidx;
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700631
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +0800632 priv->key_mapping_key--;
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700633
634 spin_lock_irqsave(&priv->sta_lock, flags);
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -0700635 key_flags = le16_to_cpu(priv->stations[sta_id].sta.key.key_flags);
636 keyidx = (key_flags >> STA_KEY_FLG_KEYID_POS) & 0x3;
637
638 if (keyconf->keyidx != keyidx) {
639 /* We need to remove a key with index different that the one
640 * in the uCode. This means that the key we need to remove has
641 * been replaced by another one with different index.
642 * Don't do anything and return ok
643 */
644 spin_unlock_irqrestore(&priv->sta_lock, flags);
645 return 0;
646 }
647
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700648 if (!test_and_clear_bit(priv->stations[sta_id].sta.key.key_offset,
649 &priv->ucode_key_table))
650 IWL_ERROR("index %d not used in uCode key table.\n",
651 priv->stations[sta_id].sta.key.key_offset);
652 memset(&priv->stations[sta_id].keyinfo, 0,
Tomas Winkler6def9762008-05-05 10:22:31 +0800653 sizeof(struct iwl_hw_key));
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700654 memset(&priv->stations[sta_id].sta.key, 0,
655 sizeof(struct iwl4965_keyinfo));
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -0700656 priv->stations[sta_id].sta.key.key_flags =
657 STA_KEY_FLG_NO_ENC | STA_KEY_FLG_INVALID;
658 priv->stations[sta_id].sta.key.key_offset = WEP_INVALID_OFFSET;
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700659 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
660 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700661
662 IWL_DEBUG_INFO("hwcrypto: clear ucode station key info\n");
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +0800663 ret = iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -0700664 spin_unlock_irqrestore(&priv->sta_lock, flags);
665 return ret;
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700666}
Tomas Winkler27aaba02008-05-05 10:22:44 +0800667EXPORT_SYMBOL(iwl_remove_dynamic_key);
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700668
669int iwl_set_dynamic_key(struct iwl_priv *priv,
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +0800670 struct ieee80211_key_conf *keyconf, u8 sta_id)
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700671{
672 int ret;
673
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +0800674 priv->key_mapping_key++;
675 keyconf->hw_key_idx = HW_KEY_DYNAMIC;
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700676
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +0800677 switch (keyconf->alg) {
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700678 case ALG_CCMP:
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +0800679 ret = iwl_set_ccmp_dynamic_key_info(priv, keyconf, sta_id);
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700680 break;
681 case ALG_TKIP:
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +0800682 ret = iwl_set_tkip_dynamic_key_info(priv, keyconf, sta_id);
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700683 break;
684 case ALG_WEP:
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +0800685 ret = iwl_set_wep_dynamic_key_info(priv, keyconf, sta_id);
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700686 break;
687 default:
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +0800688 IWL_ERROR("Unknown alg: %s alg = %d\n", __func__, keyconf->alg);
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700689 ret = -EINVAL;
690 }
691
692 return ret;
693}
Tomas Winkler27aaba02008-05-05 10:22:44 +0800694EXPORT_SYMBOL(iwl_set_dynamic_key);
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700695
Tomas Winkler66c73db2008-04-15 16:01:40 -0700696#ifdef CONFIG_IWLWIFI_DEBUG
697static void iwl_dump_lq_cmd(struct iwl_priv *priv,
698 struct iwl_link_quality_cmd *lq)
699{
700 int i;
701 IWL_DEBUG_RATE("lq station id 0x%x\n", lq->sta_id);
702 IWL_DEBUG_RATE("lq dta 0x%X 0x%X\n",
703 lq->general_params.single_stream_ant_msk,
704 lq->general_params.dual_stream_ant_msk);
705
706 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
707 IWL_DEBUG_RATE("lq index %d 0x%X\n",
708 i, lq->rs_table[i].rate_n_flags);
709}
710#else
711static inline void iwl_dump_lq_cmd(struct iwl_priv *priv,
712 struct iwl_link_quality_cmd *lq)
713{
714}
715#endif
716
717int iwl_send_lq_cmd(struct iwl_priv *priv,
718 struct iwl_link_quality_cmd *lq, u8 flags)
719{
720 struct iwl_host_cmd cmd = {
721 .id = REPLY_TX_LINK_QUALITY_CMD,
722 .len = sizeof(struct iwl_link_quality_cmd),
723 .meta.flags = flags,
724 .data = lq,
725 };
726
727 if ((lq->sta_id == 0xFF) &&
728 (priv->iw_mode == IEEE80211_IF_TYPE_IBSS))
729 return -EINVAL;
730
731 if (lq->sta_id == 0xFF)
732 lq->sta_id = IWL_AP_ID;
733
734 iwl_dump_lq_cmd(priv,lq);
735
736 if (iwl_is_associated(priv) && priv->assoc_station_added &&
737 priv->lq_mngr.lq_ready)
738 return iwl_send_cmd(priv, &cmd);
739
740 return 0;
741}
742EXPORT_SYMBOL(iwl_send_lq_cmd);
743
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800744/**
745 * iwl_sta_init_lq - Initialize a station's hardware rate table
746 *
747 * The uCode's station table contains a table of fallback rates
748 * for automatic fallback during transmission.
749 *
750 * NOTE: This sets up a default set of values. These will be replaced later
751 * if the driver's iwl-4965-rs rate scaling algorithm is used, instead of
752 * rc80211_simple.
753 *
754 * NOTE: Run REPLY_ADD_STA command to set up station table entry, before
755 * calling this function (which runs REPLY_TX_LINK_QUALITY_CMD,
756 * which requires station table entry to exist).
757 */
758static void iwl_sta_init_lq(struct iwl_priv *priv, const u8 *addr, int is_ap)
759{
760 int i, r;
761 struct iwl_link_quality_cmd link_cmd = {
762 .reserved1 = 0,
763 };
764 u16 rate_flags;
765
766 /* Set up the rate scaling to start at selected rate, fall back
767 * all the way down to 1M in IEEE order, and then spin on 1M */
768 if (is_ap)
769 r = IWL_RATE_54M_INDEX;
770 else if (priv->band == IEEE80211_BAND_5GHZ)
771 r = IWL_RATE_6M_INDEX;
772 else
773 r = IWL_RATE_1M_INDEX;
774
775 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
776 rate_flags = 0;
777 if (r >= IWL_FIRST_CCK_RATE && r <= IWL_LAST_CCK_RATE)
778 rate_flags |= RATE_MCS_CCK_MSK;
779
780 /* Use Tx antenna B only */
781 rate_flags |= RATE_MCS_ANT_B_MSK; /*FIXME:RS*/
782
783 link_cmd.rs_table[i].rate_n_flags =
784 iwl4965_hw_set_rate_n_flags(iwl_rates[r].plcp, rate_flags);
785 r = iwl4965_get_prev_ieee_rate(r);
786 }
787
788 link_cmd.general_params.single_stream_ant_msk = 2;
789 link_cmd.general_params.dual_stream_ant_msk = 3;
790 link_cmd.agg_params.agg_dis_start_th = 3;
791 link_cmd.agg_params.agg_time_limit = cpu_to_le16(4000);
792
793 /* Update the rate scaling for control frame Tx to AP */
794 link_cmd.sta_id = is_ap ? IWL_AP_ID : priv->hw_params.bcast_sta_id;
795
796 iwl_send_cmd_pdu_async(priv, REPLY_TX_LINK_QUALITY_CMD,
797 sizeof(link_cmd), &link_cmd, NULL);
798}
799/**
800 * iwl_rxon_add_station - add station into station table.
801 *
802 * there is only one AP station with id= IWL_AP_ID
803 * NOTE: mutex must be held before calling this fnction
804 */
805int iwl_rxon_add_station(struct iwl_priv *priv, const u8 *addr, int is_ap)
806{
807 u8 sta_id;
808
809 /* Add station to device's station table */
810#ifdef CONFIG_IWL4965_HT
811 struct ieee80211_conf *conf = &priv->hw->conf;
812 struct ieee80211_ht_info *cur_ht_config = &conf->ht_conf;
813
814 if ((is_ap) &&
815 (conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) &&
816 (priv->iw_mode == IEEE80211_IF_TYPE_STA))
817 sta_id = iwl_add_station_flags(priv, addr, is_ap,
818 0, cur_ht_config);
819 else
820#endif /* CONFIG_IWL4965_HT */
821 sta_id = iwl_add_station_flags(priv, addr, is_ap,
822 0, NULL);
823
824 /* Set up default rate scaling table in device's station table */
825 iwl_sta_init_lq(priv, addr, is_ap);
826
827 return sta_id;
828}
829EXPORT_SYMBOL(iwl_rxon_add_station);
830
831
832/**
833 * iwl_get_sta_id - Find station's index within station table
834 *
835 * If new IBSS station, create new entry in station table
836 */
837int iwl_get_sta_id(struct iwl_priv *priv, struct ieee80211_hdr *hdr)
838{
839 int sta_id;
840 u16 fc = le16_to_cpu(hdr->frame_control);
841 DECLARE_MAC_BUF(mac);
842
843 /* If this frame is broadcast or management, use broadcast station id */
844 if (((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA) ||
845 is_multicast_ether_addr(hdr->addr1))
846 return priv->hw_params.bcast_sta_id;
847
848 switch (priv->iw_mode) {
849
850 /* If we are a client station in a BSS network, use the special
851 * AP station entry (that's the only station we communicate with) */
852 case IEEE80211_IF_TYPE_STA:
853 return IWL_AP_ID;
854
855 /* If we are an AP, then find the station, or use BCAST */
856 case IEEE80211_IF_TYPE_AP:
857 sta_id = iwl_find_station(priv, hdr->addr1);
858 if (sta_id != IWL_INVALID_STATION)
859 return sta_id;
860 return priv->hw_params.bcast_sta_id;
861
862 /* If this frame is going out to an IBSS network, find the station,
863 * or create a new station table entry */
864 case IEEE80211_IF_TYPE_IBSS:
865 sta_id = iwl_find_station(priv, hdr->addr1);
866 if (sta_id != IWL_INVALID_STATION)
867 return sta_id;
868
869 /* Create new station table entry */
870 sta_id = iwl_add_station_flags(priv, hdr->addr1,
871 0, CMD_ASYNC, NULL);
872
873 if (sta_id != IWL_INVALID_STATION)
874 return sta_id;
875
876 IWL_DEBUG_DROP("Station %s not in station map. "
877 "Defaulting to broadcast...\n",
878 print_mac(mac, hdr->addr1));
879 iwl_print_hex_dump(priv, IWL_DL_DROP, (u8 *) hdr, sizeof(*hdr));
880 return priv->hw_params.bcast_sta_id;
881
882 default:
883 IWL_WARNING("Unknown mode of operation: %d", priv->iw_mode);
884 return priv->hw_params.bcast_sta_id;
885 }
886}
887EXPORT_SYMBOL(iwl_get_sta_id);
888