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