iwlwifi: move add sta handler to iwl-sta.c
[safe/jmp/linux-2.6] / drivers / net / wireless / iwlwifi / iwl-sta.c
1 /******************************************************************************
2  *
3  * Copyright(c) 2003 - 2008 Intel Corporation. All rights reserved.
4  *
5  * Portions of this file are derived from the ipw3945 project, as well
6  * as portions of the ieee80211 subsystem header files.
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms of version 2 of the GNU General Public License as
10  * published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
15  * more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
20  *
21  * The full GNU General Public License is included in this distribution in the
22  * file called LICENSE.
23  *
24  * Contact Information:
25  * James P. Ketrenos <ipw2100-admin@linux.intel.com>
26  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
27  *
28  *****************************************************************************/
29
30 #include <net/mac80211.h>
31 #include <linux/etherdevice.h>
32
33 #include "iwl-eeprom.h"
34 #include "iwl-dev.h"
35 #include "iwl-core.h"
36 #include "iwl-sta.h"
37 #include "iwl-io.h"
38 #include "iwl-helpers.h"
39
40
41 #define IWL_STA_DRIVER_ACTIVE           0x1     /* ucode entry is active */
42 #define IWL_STA_UCODE_ACTIVE            0x2     /* ucode entry is active */
43
44 u8 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 }
75 EXPORT_SYMBOL(iwl_find_station);
76
77 static 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
110 int 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
122         if (flags & CMD_ASYNC)
123                 cmd.meta.u.callback = iwl_add_sta_callback;
124         else
125                 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 }
157 EXPORT_SYMBOL(iwl_send_add_sta);
158
159 #ifdef CONFIG_IWL4965_HT
160
161 static 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
206 static 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  */
215 u8 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 }
281 EXPORT_SYMBOL(iwl_add_station_flags);
282
283
284 static 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
304 static 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
334 static 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  */
391 u8 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;
424 out:
425         spin_unlock_irqrestore(&priv->sta_lock, flags);
426         return 0;
427 }
428 EXPORT_SYMBOL(iwl_remove_station);
429 int 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++)
434                 if (!test_and_set_bit(i, &priv->ucode_key_table))
435                         return i;
436
437         return -1;
438 }
439
440 int 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 }
482 EXPORT_SYMBOL(iwl_send_static_wepkey_cmd);
483
484 int iwl_remove_default_wep_key(struct iwl_priv *priv,
485                                struct ieee80211_key_conf *keyconf)
486 {
487         int ret;
488         unsigned long flags;
489
490         spin_lock_irqsave(&priv->sta_lock, flags);
491
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
496         priv->default_wep_key--;
497         memset(&priv->wep_keys[keyconf->keyidx], 0, sizeof(priv->wep_keys[0]));
498         ret = iwl_send_static_wepkey_cmd(priv, 1);
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         keyconf->flags &= ~IEEE80211_KEY_FLAG_GENERATE_IV;
512         keyconf->hw_key_idx = HW_KEY_DEFAULT;
513         priv->stations[IWL_AP_ID].keyinfo.alg = ALG_WEP;
514
515         spin_lock_irqsave(&priv->sta_lock, flags);
516         priv->default_wep_key++;
517
518         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
522         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 }
531 EXPORT_SYMBOL(iwl_set_default_wep_key);
532
533 static int iwl_set_wep_dynamic_key_info(struct iwl_priv *priv,
534                                 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;
542
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
550         if (sta_id == priv->hw_params.bcast_sta_id)
551                 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
565         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 =
568                                  iwl_get_free_ucode_key_index(priv);
569         /* else, we are overriding an existing key => no need to allocated room
570          * in uCode. */
571
572         priv->stations[sta_id].sta.key.key_flags = key_flags;
573         priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
574         priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
575
576         ret = iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
577
578         spin_unlock_irqrestore(&priv->sta_lock, flags);
579
580         return ret;
581 }
582
583 static 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
594         if (sta_id == priv->hw_params.bcast_sta_id)
595                 key_flags |= STA_KEY_MULTICAST_MSK;
596
597         keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
598
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
609         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
616         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");
623         return iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
624 }
625
626 static 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;
635
636         spin_lock_irqsave(&priv->sta_lock, flags);
637
638         priv->stations[sta_id].keyinfo.alg = keyconf->alg;
639         priv->stations[sta_id].keyinfo.keylen = 16;
640
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 =
644                                  iwl_get_free_ucode_key_index(priv);
645         /* else, we are overriding an existing key => no need to allocated room
646          * in uCode. */
647
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
658 int iwl_remove_dynamic_key(struct iwl_priv *priv,
659                                 struct ieee80211_key_conf *keyconf,
660                                 u8 sta_id)
661 {
662         unsigned long flags;
663         int ret = 0;
664         u16 key_flags;
665         u8 keyidx;
666
667         priv->key_mapping_key--;
668
669         spin_lock_irqsave(&priv->sta_lock, flags);
670         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
683         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,
688                                         sizeof(struct iwl_hw_key));
689         memset(&priv->stations[sta_id].sta.key, 0,
690                                         sizeof(struct iwl4965_keyinfo));
691         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;
694         priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
695         priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
696
697         IWL_DEBUG_INFO("hwcrypto: clear ucode station key info\n");
698         ret =  iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
699         spin_unlock_irqrestore(&priv->sta_lock, flags);
700         return ret;
701 }
702 EXPORT_SYMBOL(iwl_remove_dynamic_key);
703
704 int iwl_set_dynamic_key(struct iwl_priv *priv,
705                                 struct ieee80211_key_conf *keyconf, u8 sta_id)
706 {
707         int ret;
708
709         priv->key_mapping_key++;
710         keyconf->hw_key_idx = HW_KEY_DYNAMIC;
711
712         switch (keyconf->alg) {
713         case ALG_CCMP:
714                 ret = iwl_set_ccmp_dynamic_key_info(priv, keyconf, sta_id);
715                 break;
716         case ALG_TKIP:
717                 ret = iwl_set_tkip_dynamic_key_info(priv, keyconf, sta_id);
718                 break;
719         case ALG_WEP:
720                 ret = iwl_set_wep_dynamic_key_info(priv, keyconf, sta_id);
721                 break;
722         default:
723                 IWL_ERROR("Unknown alg: %s alg = %d\n", __func__, keyconf->alg);
724                 ret = -EINVAL;
725         }
726
727         return ret;
728 }
729 EXPORT_SYMBOL(iwl_set_dynamic_key);
730
731 #ifdef CONFIG_IWLWIFI_DEBUG
732 static 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
746 static inline void iwl_dump_lq_cmd(struct iwl_priv *priv,
747                                    struct iwl_link_quality_cmd *lq)
748 {
749 }
750 #endif
751
752 int 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 }
777 EXPORT_SYMBOL(iwl_send_lq_cmd);
778
779 /**
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  */
793 static 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  */
840 int 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 }
864 EXPORT_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  */
872 int 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 }
922 EXPORT_SYMBOL(iwl_get_sta_id);
923