]> err.no Git - linux-2.6/blob - drivers/net/wireless/iwlwifi/iwl-sta.c
Merge branch 'master' of master.kernel.org:/pub/scm/linux/kernel/git/davem/net-2.6
[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-dev.h"
34 #include "iwl-core.h"
35 #include "iwl-sta.h"
36 #include "iwl-helpers.h"
37
38
39 #define IWL_STA_DRIVER_ACTIVE           0x1     /* ucode entry is active */
40 #define IWL_STA_UCODE_ACTIVE            0x2     /* ucode entry is active */
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 int iwl_get_ra_sta_id(struct iwl_priv *priv, struct ieee80211_hdr *hdr)
76 {
77         if (priv->iw_mode == IEEE80211_IF_TYPE_STA) {
78                 return IWL_AP_ID;
79         } else {
80                 u8 *da = ieee80211_get_DA(hdr);
81                 return iwl_find_station(priv, da);
82         }
83 }
84 EXPORT_SYMBOL(iwl_get_ra_sta_id);
85
86 static int iwl_add_sta_callback(struct iwl_priv *priv,
87                                    struct iwl_cmd *cmd, struct sk_buff *skb)
88 {
89         struct iwl_rx_packet *res = NULL;
90
91         if (!skb) {
92                 IWL_ERROR("Error: Response NULL in REPLY_ADD_STA.\n");
93                 return 1;
94         }
95
96         res = (struct iwl_rx_packet *)skb->data;
97         if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
98                 IWL_ERROR("Bad return from REPLY_ADD_STA (0x%08X)\n",
99                           res->hdr.flags);
100                 return 1;
101         }
102
103         switch (res->u.add_sta.status) {
104         case ADD_STA_SUCCESS_MSK:
105                 /* FIXME: implement iwl_sta_ucode_activate(priv, addr); */
106                 /* fail through */
107         default:
108                 IWL_DEBUG_HC("Received REPLY_ADD_STA:(0x%08X)\n",
109                              res->u.add_sta.status);
110                 break;
111         }
112
113         /* We didn't cache the SKB; let the caller free it */
114         return 1;
115 }
116
117 int iwl_send_add_sta(struct iwl_priv *priv,
118                      struct iwl_addsta_cmd *sta, u8 flags)
119 {
120         struct iwl_rx_packet *res = NULL;
121         int ret = 0;
122         u8 data[sizeof(*sta)];
123         struct iwl_host_cmd cmd = {
124                 .id = REPLY_ADD_STA,
125                 .meta.flags = flags,
126                 .data = data,
127         };
128
129         if (flags & CMD_ASYNC)
130                 cmd.meta.u.callback = iwl_add_sta_callback;
131         else
132                 cmd.meta.flags |= CMD_WANT_SKB;
133
134         cmd.len = priv->cfg->ops->utils->build_addsta_hcmd(sta, data);
135         ret = iwl_send_cmd(priv, &cmd);
136
137         if (ret || (flags & CMD_ASYNC))
138                 return ret;
139
140         res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
141         if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
142                 IWL_ERROR("Bad return from REPLY_ADD_STA (0x%08X)\n",
143                           res->hdr.flags);
144                 ret = -EIO;
145         }
146
147         if (ret == 0) {
148                 switch (res->u.add_sta.status) {
149                 case ADD_STA_SUCCESS_MSK:
150                         IWL_DEBUG_INFO("REPLY_ADD_STA PASSED\n");
151                         break;
152                 default:
153                         ret = -EIO;
154                         IWL_WARNING("REPLY_ADD_STA failed\n");
155                         break;
156                 }
157         }
158
159         priv->alloc_rxb_skb--;
160         dev_kfree_skb_any(cmd.meta.u.skb);
161
162         return ret;
163 }
164 EXPORT_SYMBOL(iwl_send_add_sta);
165
166 static void iwl_set_ht_add_station(struct iwl_priv *priv, u8 index,
167                                    struct ieee80211_ht_info *sta_ht_inf)
168 {
169         __le32 sta_flags;
170         u8 mimo_ps_mode;
171
172         if (!sta_ht_inf || !sta_ht_inf->ht_supported)
173                 goto done;
174
175         mimo_ps_mode = (sta_ht_inf->cap & IEEE80211_HT_CAP_MIMO_PS) >> 2;
176
177         sta_flags = priv->stations[index].sta.station_flags;
178
179         sta_flags &= ~(STA_FLG_RTS_MIMO_PROT_MSK | STA_FLG_MIMO_DIS_MSK);
180
181         switch (mimo_ps_mode) {
182         case WLAN_HT_CAP_MIMO_PS_STATIC:
183                 sta_flags |= STA_FLG_MIMO_DIS_MSK;
184                 break;
185         case WLAN_HT_CAP_MIMO_PS_DYNAMIC:
186                 sta_flags |= STA_FLG_RTS_MIMO_PROT_MSK;
187                 break;
188         case WLAN_HT_CAP_MIMO_PS_DISABLED:
189                 break;
190         default:
191                 IWL_WARNING("Invalid MIMO PS mode %d", mimo_ps_mode);
192                 break;
193         }
194
195         sta_flags |= cpu_to_le32(
196               (u32)sta_ht_inf->ampdu_factor << STA_FLG_MAX_AGG_SIZE_POS);
197
198         sta_flags |= cpu_to_le32(
199               (u32)sta_ht_inf->ampdu_density << STA_FLG_AGG_MPDU_DENSITY_POS);
200
201         if (iwl_is_fat_tx_allowed(priv, sta_ht_inf))
202                 sta_flags |= STA_FLG_FAT_EN_MSK;
203         else
204                 sta_flags &= ~STA_FLG_FAT_EN_MSK;
205
206         priv->stations[index].sta.station_flags = sta_flags;
207  done:
208         return;
209 }
210
211 /**
212  * iwl_add_station_flags - Add station to tables in driver and device
213  */
214 u8 iwl_add_station_flags(struct iwl_priv *priv, const u8 *addr, int is_ap,
215                          u8 flags, struct ieee80211_ht_info *ht_info)
216 {
217         int i;
218         int index = IWL_INVALID_STATION;
219         struct iwl_station_entry *station;
220         unsigned long flags_spin;
221         DECLARE_MAC_BUF(mac);
222
223         spin_lock_irqsave(&priv->sta_lock, flags_spin);
224         if (is_ap)
225                 index = IWL_AP_ID;
226         else if (is_broadcast_ether_addr(addr))
227                 index = priv->hw_params.bcast_sta_id;
228         else
229                 for (i = IWL_STA_ID; i < priv->hw_params.max_stations; i++) {
230                         if (!compare_ether_addr(priv->stations[i].sta.sta.addr,
231                                                 addr)) {
232                                 index = i;
233                                 break;
234                         }
235
236                         if (!priv->stations[i].used &&
237                             index == IWL_INVALID_STATION)
238                                 index = i;
239                 }
240
241
242         /* These two conditions have the same outcome, but keep them separate
243           since they have different meanings */
244         if (unlikely(index == IWL_INVALID_STATION)) {
245                 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
246                 return index;
247         }
248
249         if (priv->stations[index].used &&
250             !compare_ether_addr(priv->stations[index].sta.sta.addr, addr)) {
251                 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
252                 return index;
253         }
254
255
256         IWL_DEBUG_ASSOC("Add STA ID %d: %s\n", index, print_mac(mac, addr));
257         station = &priv->stations[index];
258         station->used = 1;
259         priv->num_stations++;
260
261         /* Set up the REPLY_ADD_STA command to send to device */
262         memset(&station->sta, 0, sizeof(struct iwl_addsta_cmd));
263         memcpy(station->sta.sta.addr, addr, ETH_ALEN);
264         station->sta.mode = 0;
265         station->sta.sta.sta_id = index;
266         station->sta.station_flags = 0;
267
268         /* BCAST station and IBSS stations do not work in HT mode */
269         if (index != priv->hw_params.bcast_sta_id &&
270             priv->iw_mode != IEEE80211_IF_TYPE_IBSS)
271                 iwl_set_ht_add_station(priv, index, ht_info);
272
273         spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
274
275         /* Add station to device's station table */
276         iwl_send_add_sta(priv, &station->sta, flags);
277         return index;
278
279 }
280 EXPORT_SYMBOL(iwl_add_station_flags);
281
282 static int iwl_sta_ucode_deactivate(struct iwl_priv *priv, const char *addr)
283 {
284         unsigned long flags;
285         u8 sta_id;
286         DECLARE_MAC_BUF(mac);
287
288         sta_id = iwl_find_station(priv, addr);
289         if (sta_id != IWL_INVALID_STATION) {
290                 IWL_DEBUG_ASSOC("Removed STA from Ucode: %s\n",
291                                 print_mac(mac, addr));
292                 spin_lock_irqsave(&priv->sta_lock, flags);
293                 priv->stations[sta_id].used &= ~IWL_STA_UCODE_ACTIVE;
294                 memset(&priv->stations[sta_id], 0,
295                         sizeof(struct iwl_station_entry));
296                 spin_unlock_irqrestore(&priv->sta_lock, flags);
297                 return 0;
298         }
299         return -EINVAL;
300 }
301
302 static int iwl_remove_sta_callback(struct iwl_priv *priv,
303                                    struct iwl_cmd *cmd, struct sk_buff *skb)
304 {
305         struct iwl_rx_packet *res = NULL;
306         const char *addr = cmd->cmd.rm_sta.addr;
307
308         if (!skb) {
309                 IWL_ERROR("Error: Response NULL in REPLY_REMOVE_STA.\n");
310                 return 1;
311         }
312
313         res = (struct iwl_rx_packet *)skb->data;
314         if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
315                 IWL_ERROR("Bad return from REPLY_REMOVE_STA (0x%08X)\n",
316                 res->hdr.flags);
317                 return 1;
318         }
319
320         switch (res->u.rem_sta.status) {
321         case REM_STA_SUCCESS_MSK:
322                 iwl_sta_ucode_deactivate(priv, addr);
323                 break;
324         default:
325                 break;
326         }
327
328         /* We didn't cache the SKB; let the caller free it */
329         return 1;
330 }
331
332 static int iwl_send_remove_station(struct iwl_priv *priv, const u8 *addr,
333                                    u8 flags)
334 {
335         struct iwl_rx_packet *res = NULL;
336         int ret;
337
338         struct iwl_rem_sta_cmd rm_sta_cmd;
339
340         struct iwl_host_cmd cmd = {
341                 .id = REPLY_REMOVE_STA,
342                 .len = sizeof(struct iwl_rem_sta_cmd),
343                 .meta.flags = flags,
344                 .data = &rm_sta_cmd,
345         };
346
347         memset(&rm_sta_cmd, 0, sizeof(rm_sta_cmd));
348         rm_sta_cmd.num_sta = 1;
349         memcpy(&rm_sta_cmd.addr, addr , ETH_ALEN);
350
351         if (flags & CMD_ASYNC)
352                 cmd.meta.u.callback = iwl_remove_sta_callback;
353         else
354                 cmd.meta.flags |= CMD_WANT_SKB;
355         ret = iwl_send_cmd(priv, &cmd);
356
357         if (ret || (flags & CMD_ASYNC))
358                 return ret;
359
360         res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
361         if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
362                 IWL_ERROR("Bad return from REPLY_REMOVE_STA (0x%08X)\n",
363                           res->hdr.flags);
364                 ret = -EIO;
365         }
366
367         if (!ret) {
368                 switch (res->u.rem_sta.status) {
369                 case REM_STA_SUCCESS_MSK:
370                         iwl_sta_ucode_deactivate(priv, addr);
371                         IWL_DEBUG_ASSOC("REPLY_REMOVE_STA PASSED\n");
372                         break;
373                 default:
374                         ret = -EIO;
375                         IWL_ERROR("REPLY_REMOVE_STA failed\n");
376                         break;
377                 }
378         }
379
380         priv->alloc_rxb_skb--;
381         dev_kfree_skb_any(cmd.meta.u.skb);
382
383         return ret;
384 }
385
386 /**
387  * iwl_remove_station - Remove driver's knowledge of station.
388  */
389 u8 iwl_remove_station(struct iwl_priv *priv, const u8 *addr, int is_ap)
390 {
391         int index = IWL_INVALID_STATION;
392         int i;
393         unsigned long flags;
394
395         spin_lock_irqsave(&priv->sta_lock, flags);
396
397         if (is_ap)
398                 index = IWL_AP_ID;
399         else if (is_broadcast_ether_addr(addr))
400                 index = priv->hw_params.bcast_sta_id;
401         else
402                 for (i = IWL_STA_ID; i < priv->hw_params.max_stations; i++)
403                         if (priv->stations[i].used &&
404                             !compare_ether_addr(priv->stations[i].sta.sta.addr,
405                                                 addr)) {
406                                 index = i;
407                                 break;
408                         }
409
410         if (unlikely(index == IWL_INVALID_STATION))
411                 goto out;
412
413         if (priv->stations[index].used) {
414                 priv->stations[index].used = 0;
415                 priv->num_stations--;
416         }
417
418         BUG_ON(priv->num_stations < 0);
419         spin_unlock_irqrestore(&priv->sta_lock, flags);
420         iwl_send_remove_station(priv, addr, CMD_ASYNC);
421         return index;
422 out:
423         spin_unlock_irqrestore(&priv->sta_lock, flags);
424         return 0;
425 }
426 EXPORT_SYMBOL(iwl_remove_station);
427 static int iwl_get_free_ucode_key_index(struct iwl_priv *priv)
428 {
429         int i;
430
431         for (i = 0; i < STA_KEY_MAX_NUM; i++)
432                 if (!test_and_set_bit(i, &priv->ucode_key_table))
433                         return i;
434
435         return -1;
436 }
437
438 int iwl_send_static_wepkey_cmd(struct iwl_priv *priv, u8 send_if_empty)
439 {
440         int i, not_empty = 0;
441         u8 buff[sizeof(struct iwl_wep_cmd) +
442                 sizeof(struct iwl_wep_key) * WEP_KEYS_MAX];
443         struct iwl_wep_cmd *wep_cmd = (struct iwl_wep_cmd *)buff;
444         size_t cmd_size  = sizeof(struct iwl_wep_cmd);
445         struct iwl_host_cmd cmd = {
446                 .id = REPLY_WEPKEY,
447                 .data = wep_cmd,
448                 .meta.flags = CMD_ASYNC,
449         };
450
451         memset(wep_cmd, 0, cmd_size +
452                         (sizeof(struct iwl_wep_key) * WEP_KEYS_MAX));
453
454         for (i = 0; i < WEP_KEYS_MAX ; i++) {
455                 wep_cmd->key[i].key_index = i;
456                 if (priv->wep_keys[i].key_size) {
457                         wep_cmd->key[i].key_offset = i;
458                         not_empty = 1;
459                 } else {
460                         wep_cmd->key[i].key_offset = WEP_INVALID_OFFSET;
461                 }
462
463                 wep_cmd->key[i].key_size = priv->wep_keys[i].key_size;
464                 memcpy(&wep_cmd->key[i].key[3], priv->wep_keys[i].key,
465                                 priv->wep_keys[i].key_size);
466         }
467
468         wep_cmd->global_key_type = WEP_KEY_WEP_TYPE;
469         wep_cmd->num_keys = WEP_KEYS_MAX;
470
471         cmd_size += sizeof(struct iwl_wep_key) * WEP_KEYS_MAX;
472
473         cmd.len = cmd_size;
474
475         if (not_empty || send_if_empty)
476                 return iwl_send_cmd(priv, &cmd);
477         else
478                 return 0;
479 }
480 EXPORT_SYMBOL(iwl_send_static_wepkey_cmd);
481
482 int iwl_remove_default_wep_key(struct iwl_priv *priv,
483                                struct ieee80211_key_conf *keyconf)
484 {
485         int ret;
486         unsigned long flags;
487
488         spin_lock_irqsave(&priv->sta_lock, flags);
489
490         if (!test_and_clear_bit(keyconf->keyidx, &priv->ucode_key_table))
491                 IWL_ERROR("index %d not used in uCode key table.\n",
492                           keyconf->keyidx);
493
494         priv->default_wep_key--;
495         memset(&priv->wep_keys[keyconf->keyidx], 0, sizeof(priv->wep_keys[0]));
496         ret = iwl_send_static_wepkey_cmd(priv, 1);
497         IWL_DEBUG_WEP("Remove default WEP key: idx=%d ret=%d\n",
498                       keyconf->keyidx, ret);
499         spin_unlock_irqrestore(&priv->sta_lock, flags);
500
501         return ret;
502 }
503 EXPORT_SYMBOL(iwl_remove_default_wep_key);
504
505 int 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         if (keyconf->keylen != WEP_KEY_LEN_128 &&
512             keyconf->keylen != WEP_KEY_LEN_64) {
513                 IWL_DEBUG_WEP("Bad WEP key length %d\n", keyconf->keylen);
514                 return -EINVAL;
515         }
516
517         keyconf->flags &= ~IEEE80211_KEY_FLAG_GENERATE_IV;
518         keyconf->hw_key_idx = HW_KEY_DEFAULT;
519         priv->stations[IWL_AP_ID].keyinfo.alg = ALG_WEP;
520
521         spin_lock_irqsave(&priv->sta_lock, flags);
522         priv->default_wep_key++;
523
524         if (test_and_set_bit(keyconf->keyidx, &priv->ucode_key_table))
525                 IWL_ERROR("index %d already used in uCode key table.\n",
526                         keyconf->keyidx);
527
528         priv->wep_keys[keyconf->keyidx].key_size = keyconf->keylen;
529         memcpy(&priv->wep_keys[keyconf->keyidx].key, &keyconf->key,
530                                                         keyconf->keylen);
531
532         ret = iwl_send_static_wepkey_cmd(priv, 0);
533         IWL_DEBUG_WEP("Set default WEP key: len=%d idx=%d ret=%d\n",
534                 keyconf->keylen, keyconf->keyidx, ret);
535         spin_unlock_irqrestore(&priv->sta_lock, flags);
536
537         return ret;
538 }
539 EXPORT_SYMBOL(iwl_set_default_wep_key);
540
541 static int iwl_set_wep_dynamic_key_info(struct iwl_priv *priv,
542                                 struct ieee80211_key_conf *keyconf,
543                                 u8 sta_id)
544 {
545         unsigned long flags;
546         __le16 key_flags = 0;
547         int ret;
548
549         keyconf->flags &= ~IEEE80211_KEY_FLAG_GENERATE_IV;
550
551         key_flags |= (STA_KEY_FLG_WEP | STA_KEY_FLG_MAP_KEY_MSK);
552         key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
553         key_flags &= ~STA_KEY_FLG_INVALID;
554
555         if (keyconf->keylen == WEP_KEY_LEN_128)
556                 key_flags |= STA_KEY_FLG_KEY_SIZE_MSK;
557
558         if (sta_id == priv->hw_params.bcast_sta_id)
559                 key_flags |= STA_KEY_MULTICAST_MSK;
560
561         spin_lock_irqsave(&priv->sta_lock, flags);
562
563         priv->stations[sta_id].keyinfo.alg = keyconf->alg;
564         priv->stations[sta_id].keyinfo.keylen = keyconf->keylen;
565         priv->stations[sta_id].keyinfo.keyidx = keyconf->keyidx;
566
567         memcpy(priv->stations[sta_id].keyinfo.key,
568                                 keyconf->key, keyconf->keylen);
569
570         memcpy(&priv->stations[sta_id].sta.key.key[3],
571                                 keyconf->key, keyconf->keylen);
572
573         if ((priv->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
574                         == STA_KEY_FLG_NO_ENC)
575                 priv->stations[sta_id].sta.key.key_offset =
576                                  iwl_get_free_ucode_key_index(priv);
577         /* else, we are overriding an existing key => no need to allocated room
578          * in uCode. */
579
580         priv->stations[sta_id].sta.key.key_flags = key_flags;
581         priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
582         priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
583
584         ret = iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
585
586         spin_unlock_irqrestore(&priv->sta_lock, flags);
587
588         return ret;
589 }
590
591 static int iwl_set_ccmp_dynamic_key_info(struct iwl_priv *priv,
592                                    struct ieee80211_key_conf *keyconf,
593                                    u8 sta_id)
594 {
595         unsigned long flags;
596         __le16 key_flags = 0;
597
598         key_flags |= (STA_KEY_FLG_CCMP | STA_KEY_FLG_MAP_KEY_MSK);
599         key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
600         key_flags &= ~STA_KEY_FLG_INVALID;
601
602         if (sta_id == priv->hw_params.bcast_sta_id)
603                 key_flags |= STA_KEY_MULTICAST_MSK;
604
605         keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
606
607         spin_lock_irqsave(&priv->sta_lock, flags);
608         priv->stations[sta_id].keyinfo.alg = keyconf->alg;
609         priv->stations[sta_id].keyinfo.keylen = keyconf->keylen;
610
611         memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key,
612                keyconf->keylen);
613
614         memcpy(priv->stations[sta_id].sta.key.key, keyconf->key,
615                keyconf->keylen);
616
617         if ((priv->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
618                         == STA_KEY_FLG_NO_ENC)
619                 priv->stations[sta_id].sta.key.key_offset =
620                                  iwl_get_free_ucode_key_index(priv);
621         /* else, we are overriding an existing key => no need to allocated room
622          * in uCode. */
623
624         priv->stations[sta_id].sta.key.key_flags = key_flags;
625         priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
626         priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
627
628         spin_unlock_irqrestore(&priv->sta_lock, flags);
629
630         IWL_DEBUG_INFO("hwcrypto: modify ucode station key info\n");
631         return iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
632 }
633
634 static int iwl_set_tkip_dynamic_key_info(struct iwl_priv *priv,
635                                    struct ieee80211_key_conf *keyconf,
636                                    u8 sta_id)
637 {
638         unsigned long flags;
639         int ret = 0;
640
641         keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
642         keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
643
644         spin_lock_irqsave(&priv->sta_lock, flags);
645
646         priv->stations[sta_id].keyinfo.alg = keyconf->alg;
647         priv->stations[sta_id].keyinfo.keylen = 16;
648
649         if ((priv->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
650                         == STA_KEY_FLG_NO_ENC)
651                 priv->stations[sta_id].sta.key.key_offset =
652                                  iwl_get_free_ucode_key_index(priv);
653         /* else, we are overriding an existing key => no need to allocated room
654          * in uCode. */
655
656         /* This copy is acutally not needed: we get the key with each TX */
657         memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key, 16);
658
659         memcpy(priv->stations[sta_id].sta.key.key, keyconf->key, 16);
660
661         spin_unlock_irqrestore(&priv->sta_lock, flags);
662
663         return ret;
664 }
665
666 int iwl_remove_dynamic_key(struct iwl_priv *priv,
667                                 struct ieee80211_key_conf *keyconf,
668                                 u8 sta_id)
669 {
670         unsigned long flags;
671         int ret = 0;
672         u16 key_flags;
673         u8 keyidx;
674
675         priv->key_mapping_key--;
676
677         spin_lock_irqsave(&priv->sta_lock, flags);
678         key_flags = le16_to_cpu(priv->stations[sta_id].sta.key.key_flags);
679         keyidx = (key_flags >> STA_KEY_FLG_KEYID_POS) & 0x3;
680
681         IWL_DEBUG_WEP("Remove dynamic key: idx=%d sta=%d\n",
682                       keyconf->keyidx, sta_id);
683
684         if (keyconf->keyidx != keyidx) {
685                 /* We need to remove a key with index different that the one
686                  * in the uCode. This means that the key we need to remove has
687                  * been replaced by another one with different index.
688                  * Don't do anything and return ok
689                  */
690                 spin_unlock_irqrestore(&priv->sta_lock, flags);
691                 return 0;
692         }
693
694         if (!test_and_clear_bit(priv->stations[sta_id].sta.key.key_offset,
695                 &priv->ucode_key_table))
696                 IWL_ERROR("index %d not used in uCode key table.\n",
697                         priv->stations[sta_id].sta.key.key_offset);
698         memset(&priv->stations[sta_id].keyinfo, 0,
699                                         sizeof(struct iwl_hw_key));
700         memset(&priv->stations[sta_id].sta.key, 0,
701                                         sizeof(struct iwl4965_keyinfo));
702         priv->stations[sta_id].sta.key.key_flags =
703                         STA_KEY_FLG_NO_ENC | STA_KEY_FLG_INVALID;
704         priv->stations[sta_id].sta.key.key_offset = WEP_INVALID_OFFSET;
705         priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
706         priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
707
708         ret =  iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
709         spin_unlock_irqrestore(&priv->sta_lock, flags);
710         return ret;
711 }
712 EXPORT_SYMBOL(iwl_remove_dynamic_key);
713
714 int iwl_set_dynamic_key(struct iwl_priv *priv,
715                                 struct ieee80211_key_conf *keyconf, u8 sta_id)
716 {
717         int ret;
718
719         priv->key_mapping_key++;
720         keyconf->hw_key_idx = HW_KEY_DYNAMIC;
721
722         switch (keyconf->alg) {
723         case ALG_CCMP:
724                 ret = iwl_set_ccmp_dynamic_key_info(priv, keyconf, sta_id);
725                 break;
726         case ALG_TKIP:
727                 ret = iwl_set_tkip_dynamic_key_info(priv, keyconf, sta_id);
728                 break;
729         case ALG_WEP:
730                 ret = iwl_set_wep_dynamic_key_info(priv, keyconf, sta_id);
731                 break;
732         default:
733                 IWL_ERROR("Unknown alg: %s alg = %d\n", __func__, keyconf->alg);
734                 ret = -EINVAL;
735         }
736
737         IWL_DEBUG_WEP("Set dynamic key: alg= %d len=%d idx=%d sta=%d ret=%d\n",
738                       keyconf->alg, keyconf->keylen, keyconf->keyidx,
739                       sta_id, ret);
740
741         return ret;
742 }
743 EXPORT_SYMBOL(iwl_set_dynamic_key);
744
745 #ifdef CONFIG_IWLWIFI_DEBUG
746 static void iwl_dump_lq_cmd(struct iwl_priv *priv,
747                            struct iwl_link_quality_cmd *lq)
748 {
749         int i;
750         IWL_DEBUG_RATE("lq station id 0x%x\n", lq->sta_id);
751         IWL_DEBUG_RATE("lq dta 0x%X 0x%X\n",
752                        lq->general_params.single_stream_ant_msk,
753                        lq->general_params.dual_stream_ant_msk);
754
755         for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
756                 IWL_DEBUG_RATE("lq index %d 0x%X\n",
757                                i, lq->rs_table[i].rate_n_flags);
758 }
759 #else
760 static inline void iwl_dump_lq_cmd(struct iwl_priv *priv,
761                                    struct iwl_link_quality_cmd *lq)
762 {
763 }
764 #endif
765
766 int iwl_send_lq_cmd(struct iwl_priv *priv,
767                     struct iwl_link_quality_cmd *lq, u8 flags)
768 {
769         struct iwl_host_cmd cmd = {
770                 .id = REPLY_TX_LINK_QUALITY_CMD,
771                 .len = sizeof(struct iwl_link_quality_cmd),
772                 .meta.flags = flags,
773                 .data = lq,
774         };
775
776         if ((lq->sta_id == 0xFF) &&
777             (priv->iw_mode == IEEE80211_IF_TYPE_IBSS))
778                 return -EINVAL;
779
780         if (lq->sta_id == 0xFF)
781                 lq->sta_id = IWL_AP_ID;
782
783         iwl_dump_lq_cmd(priv,lq);
784
785         if (iwl_is_associated(priv) && priv->assoc_station_added &&
786             priv->lq_mngr.lq_ready)
787                 return  iwl_send_cmd(priv, &cmd);
788
789         return 0;
790 }
791 EXPORT_SYMBOL(iwl_send_lq_cmd);
792
793 /**
794  * iwl_sta_init_lq - Initialize a station's hardware rate table
795  *
796  * The uCode's station table contains a table of fallback rates
797  * for automatic fallback during transmission.
798  *
799  * NOTE: This sets up a default set of values.  These will be replaced later
800  *       if the driver's iwl-4965-rs rate scaling algorithm is used, instead of
801  *       rc80211_simple.
802  *
803  * NOTE: Run REPLY_ADD_STA command to set up station table entry, before
804  *       calling this function (which runs REPLY_TX_LINK_QUALITY_CMD,
805  *       which requires station table entry to exist).
806  */
807 static void iwl_sta_init_lq(struct iwl_priv *priv, const u8 *addr, int is_ap)
808 {
809         int i, r;
810         struct iwl_link_quality_cmd link_cmd = {
811                 .reserved1 = 0,
812         };
813         u16 rate_flags;
814
815         /* Set up the rate scaling to start at selected rate, fall back
816          * all the way down to 1M in IEEE order, and then spin on 1M */
817         if (is_ap)
818                 r = IWL_RATE_54M_INDEX;
819         else if (priv->band == IEEE80211_BAND_5GHZ)
820                 r = IWL_RATE_6M_INDEX;
821         else
822                 r = IWL_RATE_1M_INDEX;
823
824         for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
825                 rate_flags = 0;
826                 if (r >= IWL_FIRST_CCK_RATE && r <= IWL_LAST_CCK_RATE)
827                         rate_flags |= RATE_MCS_CCK_MSK;
828
829                 /* Use Tx antenna B only */
830                 rate_flags |= RATE_MCS_ANT_B_MSK; /*FIXME:RS*/
831
832                 link_cmd.rs_table[i].rate_n_flags =
833                         iwl_hw_set_rate_n_flags(iwl_rates[r].plcp, rate_flags);
834                 r = iwl4965_get_prev_ieee_rate(r);
835         }
836
837         link_cmd.general_params.single_stream_ant_msk = 2;
838         link_cmd.general_params.dual_stream_ant_msk = 3;
839         link_cmd.agg_params.agg_dis_start_th = 3;
840         link_cmd.agg_params.agg_time_limit = cpu_to_le16(4000);
841
842         /* Update the rate scaling for control frame Tx to AP */
843         link_cmd.sta_id = is_ap ? IWL_AP_ID : priv->hw_params.bcast_sta_id;
844
845         iwl_send_cmd_pdu_async(priv, REPLY_TX_LINK_QUALITY_CMD,
846                                sizeof(link_cmd), &link_cmd, NULL);
847 }
848 /**
849  * iwl_rxon_add_station - add station into station table.
850  *
851  * there is only one AP station with id= IWL_AP_ID
852  * NOTE: mutex must be held before calling this fnction
853  */
854 int iwl_rxon_add_station(struct iwl_priv *priv, const u8 *addr, int is_ap)
855 {
856         u8 sta_id;
857
858         /* Add station to device's station table */
859         struct ieee80211_conf *conf = &priv->hw->conf;
860         struct ieee80211_ht_info *cur_ht_config = &conf->ht_conf;
861
862         if ((is_ap) &&
863             (conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) &&
864             (priv->iw_mode == IEEE80211_IF_TYPE_STA))
865                 sta_id = iwl_add_station_flags(priv, addr, is_ap,
866                                                    0, cur_ht_config);
867         else
868                 sta_id = iwl_add_station_flags(priv, addr, is_ap,
869                                                    0, NULL);
870
871         /* Set up default rate scaling table in device's station table */
872         iwl_sta_init_lq(priv, addr, is_ap);
873
874         return sta_id;
875 }
876 EXPORT_SYMBOL(iwl_rxon_add_station);
877
878 /**
879  * iwl_get_sta_id - Find station's index within station table
880  *
881  * If new IBSS station, create new entry in station table
882  */
883 int iwl_get_sta_id(struct iwl_priv *priv, struct ieee80211_hdr *hdr)
884 {
885         int sta_id;
886         u16 fc = le16_to_cpu(hdr->frame_control);
887         DECLARE_MAC_BUF(mac);
888
889         /* If this frame is broadcast or management, use broadcast station id */
890         if (((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA) ||
891             is_multicast_ether_addr(hdr->addr1))
892                 return priv->hw_params.bcast_sta_id;
893
894         switch (priv->iw_mode) {
895
896         /* If we are a client station in a BSS network, use the special
897          * AP station entry (that's the only station we communicate with) */
898         case IEEE80211_IF_TYPE_STA:
899                 return IWL_AP_ID;
900
901         /* If we are an AP, then find the station, or use BCAST */
902         case IEEE80211_IF_TYPE_AP:
903                 sta_id = iwl_find_station(priv, hdr->addr1);
904                 if (sta_id != IWL_INVALID_STATION)
905                         return sta_id;
906                 return priv->hw_params.bcast_sta_id;
907
908         /* If this frame is going out to an IBSS network, find the station,
909          * or create a new station table entry */
910         case IEEE80211_IF_TYPE_IBSS:
911                 sta_id = iwl_find_station(priv, hdr->addr1);
912                 if (sta_id != IWL_INVALID_STATION)
913                         return sta_id;
914
915                 /* Create new station table entry */
916                 sta_id = iwl_add_station_flags(priv, hdr->addr1,
917                                                    0, CMD_ASYNC, NULL);
918
919                 if (sta_id != IWL_INVALID_STATION)
920                         return sta_id;
921
922                 IWL_DEBUG_DROP("Station %s not in station map. "
923                                "Defaulting to broadcast...\n",
924                                print_mac(mac, hdr->addr1));
925                 iwl_print_hex_dump(priv, IWL_DL_DROP, (u8 *) hdr, sizeof(*hdr));
926                 return priv->hw_params.bcast_sta_id;
927
928         default:
929                 IWL_WARNING("Unknown mode of operation: %d", priv->iw_mode);
930                 return priv->hw_params.bcast_sta_id;
931         }
932 }
933 EXPORT_SYMBOL(iwl_get_sta_id);
934
935 /**
936  * iwl_sta_modify_enable_tid_tx - Enable Tx for this TID in station table
937  */
938 void iwl_sta_modify_enable_tid_tx(struct iwl_priv *priv, int sta_id, int tid)
939 {
940         unsigned long flags;
941
942         /* Remove "disable" flag, to enable Tx for this TID */
943         spin_lock_irqsave(&priv->sta_lock, flags);
944         priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_TID_DISABLE_TX;
945         priv->stations[sta_id].sta.tid_disable_tx &= cpu_to_le16(~(1 << tid));
946         priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
947         spin_unlock_irqrestore(&priv->sta_lock, flags);
948
949         iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
950 }
951 EXPORT_SYMBOL(iwl_sta_modify_enable_tid_tx);
952