1 /******************************************************************************
3 * Copyright(c) 2003 - 2008 Intel Corporation. All rights reserved.
5 * Portions of this file are derived from the ipw3945 project, as well
6 * as portions of the ieee80211 subsystem header files.
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.
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
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
21 * The full GNU General Public License is included in this distribution in the
22 * file called LICENSE.
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
28 *****************************************************************************/
30 #include <net/mac80211.h>
31 #include <linux/etherdevice.h>
36 #include "iwl-helpers.h"
39 #define IWL_STA_DRIVER_ACTIVE BIT(0) /* driver entry is active */
40 #define IWL_STA_UCODE_ACTIVE BIT(1) /* ucode entry is active */
42 u8 iwl_find_station(struct iwl_priv *priv, const u8 *addr)
46 int ret = IWL_INVALID_STATION;
50 if ((priv->iw_mode == NL80211_IFTYPE_ADHOC) ||
51 (priv->iw_mode == NL80211_IFTYPE_AP))
54 if (is_broadcast_ether_addr(addr))
55 return priv->hw_params.bcast_sta_id;
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,
66 IWL_DEBUG_ASSOC_LIMIT("can not find STA %s total %d\n",
67 print_mac(mac, addr), priv->num_stations);
70 spin_unlock_irqrestore(&priv->sta_lock, flags);
73 EXPORT_SYMBOL(iwl_find_station);
75 int iwl_get_ra_sta_id(struct iwl_priv *priv, struct ieee80211_hdr *hdr)
77 if (priv->iw_mode == NL80211_IFTYPE_STATION) {
80 u8 *da = ieee80211_get_DA(hdr);
81 return iwl_find_station(priv, da);
84 EXPORT_SYMBOL(iwl_get_ra_sta_id);
86 static void iwl_sta_ucode_activate(struct iwl_priv *priv, u8 sta_id)
91 spin_lock_irqsave(&priv->sta_lock, flags);
93 if (!(priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE))
94 IWL_ERROR("ACTIVATE a non DRIVER active station %d\n", sta_id);
96 priv->stations[sta_id].used |= IWL_STA_UCODE_ACTIVE;
97 IWL_DEBUG_ASSOC("Added STA to Ucode: %s\n",
98 print_mac(mac, priv->stations[sta_id].sta.sta.addr));
100 spin_unlock_irqrestore(&priv->sta_lock, flags);
103 static int iwl_add_sta_callback(struct iwl_priv *priv,
104 struct iwl_cmd *cmd, struct sk_buff *skb)
106 struct iwl_rx_packet *res = NULL;
107 u8 sta_id = cmd->cmd.addsta.sta.sta_id;
110 IWL_ERROR("Error: Response NULL in REPLY_ADD_STA.\n");
114 res = (struct iwl_rx_packet *)skb->data;
115 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
116 IWL_ERROR("Bad return from REPLY_ADD_STA (0x%08X)\n",
121 switch (res->u.add_sta.status) {
122 case ADD_STA_SUCCESS_MSK:
123 iwl_sta_ucode_activate(priv, sta_id);
126 IWL_DEBUG_HC("Received REPLY_ADD_STA:(0x%08X)\n",
127 res->u.add_sta.status);
131 /* We didn't cache the SKB; let the caller free it */
135 int iwl_send_add_sta(struct iwl_priv *priv,
136 struct iwl_addsta_cmd *sta, u8 flags)
138 struct iwl_rx_packet *res = NULL;
140 u8 data[sizeof(*sta)];
141 struct iwl_host_cmd cmd = {
147 if (flags & CMD_ASYNC)
148 cmd.meta.u.callback = iwl_add_sta_callback;
150 cmd.meta.flags |= CMD_WANT_SKB;
152 cmd.len = priv->cfg->ops->utils->build_addsta_hcmd(sta, data);
153 ret = iwl_send_cmd(priv, &cmd);
155 if (ret || (flags & CMD_ASYNC))
158 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
159 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
160 IWL_ERROR("Bad return from REPLY_ADD_STA (0x%08X)\n",
166 switch (res->u.add_sta.status) {
167 case ADD_STA_SUCCESS_MSK:
168 iwl_sta_ucode_activate(priv, sta->sta.sta_id);
169 IWL_DEBUG_INFO("REPLY_ADD_STA PASSED\n");
173 IWL_WARNING("REPLY_ADD_STA failed\n");
178 priv->alloc_rxb_skb--;
179 dev_kfree_skb_any(cmd.meta.u.skb);
183 EXPORT_SYMBOL(iwl_send_add_sta);
185 static void iwl_set_ht_add_station(struct iwl_priv *priv, u8 index,
186 struct ieee80211_ht_info *sta_ht_inf)
191 if (!sta_ht_inf || !sta_ht_inf->ht_supported)
194 mimo_ps_mode = (sta_ht_inf->cap & IEEE80211_HT_CAP_SM_PS) >> 2;
196 sta_flags = priv->stations[index].sta.station_flags;
198 sta_flags &= ~(STA_FLG_RTS_MIMO_PROT_MSK | STA_FLG_MIMO_DIS_MSK);
200 switch (mimo_ps_mode) {
201 case WLAN_HT_CAP_SM_PS_STATIC:
202 sta_flags |= STA_FLG_MIMO_DIS_MSK;
204 case WLAN_HT_CAP_SM_PS_DYNAMIC:
205 sta_flags |= STA_FLG_RTS_MIMO_PROT_MSK;
207 case WLAN_HT_CAP_SM_PS_DISABLED:
210 IWL_WARNING("Invalid MIMO PS mode %d\n", mimo_ps_mode);
214 sta_flags |= cpu_to_le32(
215 (u32)sta_ht_inf->ampdu_factor << STA_FLG_MAX_AGG_SIZE_POS);
217 sta_flags |= cpu_to_le32(
218 (u32)sta_ht_inf->ampdu_density << STA_FLG_AGG_MPDU_DENSITY_POS);
220 if (iwl_is_fat_tx_allowed(priv, sta_ht_inf))
221 sta_flags |= STA_FLG_FAT_EN_MSK;
223 sta_flags &= ~STA_FLG_FAT_EN_MSK;
225 priv->stations[index].sta.station_flags = sta_flags;
231 * iwl_add_station_flags - Add station to tables in driver and device
233 u8 iwl_add_station_flags(struct iwl_priv *priv, const u8 *addr, int is_ap,
234 u8 flags, struct ieee80211_ht_info *ht_info)
237 int sta_id = IWL_INVALID_STATION;
238 struct iwl_station_entry *station;
239 unsigned long flags_spin;
240 DECLARE_MAC_BUF(mac);
242 spin_lock_irqsave(&priv->sta_lock, flags_spin);
245 else if (is_broadcast_ether_addr(addr))
246 sta_id = priv->hw_params.bcast_sta_id;
248 for (i = IWL_STA_ID; i < priv->hw_params.max_stations; i++) {
249 if (!compare_ether_addr(priv->stations[i].sta.sta.addr,
255 if (!priv->stations[i].used &&
256 sta_id == IWL_INVALID_STATION)
260 /* These two conditions have the same outcome, but keep them separate
261 since they have different meanings */
262 if (unlikely(sta_id == IWL_INVALID_STATION)) {
263 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
267 if (priv->stations[sta_id].used &&
268 !compare_ether_addr(priv->stations[sta_id].sta.sta.addr, addr)) {
269 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
274 station = &priv->stations[sta_id];
275 station->used = IWL_STA_DRIVER_ACTIVE;
276 IWL_DEBUG_ASSOC("Add STA to driver ID %d: %s\n",
277 sta_id, print_mac(mac, addr));
278 priv->num_stations++;
280 /* Set up the REPLY_ADD_STA command to send to device */
281 memset(&station->sta, 0, sizeof(struct iwl_addsta_cmd));
282 memcpy(station->sta.sta.addr, addr, ETH_ALEN);
283 station->sta.mode = 0;
284 station->sta.sta.sta_id = sta_id;
285 station->sta.station_flags = 0;
287 /* BCAST station and IBSS stations do not work in HT mode */
288 if (sta_id != priv->hw_params.bcast_sta_id &&
289 priv->iw_mode != NL80211_IFTYPE_ADHOC)
290 iwl_set_ht_add_station(priv, sta_id, ht_info);
292 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
294 /* Add station to device's station table */
295 iwl_send_add_sta(priv, &station->sta, flags);
299 EXPORT_SYMBOL(iwl_add_station_flags);
301 static void iwl_sta_ucode_deactivate(struct iwl_priv *priv, const char *addr)
304 DECLARE_MAC_BUF(mac);
306 u8 sta_id = iwl_find_station(priv, addr);
308 BUG_ON(sta_id == IWL_INVALID_STATION);
310 IWL_DEBUG_ASSOC("Removed STA from Ucode: %s\n",
311 print_mac(mac, addr));
313 spin_lock_irqsave(&priv->sta_lock, flags);
315 /* Ucode must be active and driver must be non active */
316 if (priv->stations[sta_id].used != IWL_STA_UCODE_ACTIVE)
317 IWL_ERROR("removed non active STA %d\n", sta_id);
319 priv->stations[sta_id].used &= ~IWL_STA_UCODE_ACTIVE;
321 memset(&priv->stations[sta_id], 0, sizeof(struct iwl_station_entry));
322 spin_unlock_irqrestore(&priv->sta_lock, flags);
325 static int iwl_remove_sta_callback(struct iwl_priv *priv,
326 struct iwl_cmd *cmd, struct sk_buff *skb)
328 struct iwl_rx_packet *res = NULL;
329 const char *addr = cmd->cmd.rm_sta.addr;
332 IWL_ERROR("Error: Response NULL in REPLY_REMOVE_STA.\n");
336 res = (struct iwl_rx_packet *)skb->data;
337 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
338 IWL_ERROR("Bad return from REPLY_REMOVE_STA (0x%08X)\n",
343 switch (res->u.rem_sta.status) {
344 case REM_STA_SUCCESS_MSK:
345 iwl_sta_ucode_deactivate(priv, addr);
348 IWL_ERROR("REPLY_REMOVE_STA failed\n");
352 /* We didn't cache the SKB; let the caller free it */
356 static int iwl_send_remove_station(struct iwl_priv *priv, const u8 *addr,
359 struct iwl_rx_packet *res = NULL;
362 struct iwl_rem_sta_cmd rm_sta_cmd;
364 struct iwl_host_cmd cmd = {
365 .id = REPLY_REMOVE_STA,
366 .len = sizeof(struct iwl_rem_sta_cmd),
371 memset(&rm_sta_cmd, 0, sizeof(rm_sta_cmd));
372 rm_sta_cmd.num_sta = 1;
373 memcpy(&rm_sta_cmd.addr, addr , ETH_ALEN);
375 if (flags & CMD_ASYNC)
376 cmd.meta.u.callback = iwl_remove_sta_callback;
378 cmd.meta.flags |= CMD_WANT_SKB;
379 ret = iwl_send_cmd(priv, &cmd);
381 if (ret || (flags & CMD_ASYNC))
384 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
385 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
386 IWL_ERROR("Bad return from REPLY_REMOVE_STA (0x%08X)\n",
392 switch (res->u.rem_sta.status) {
393 case REM_STA_SUCCESS_MSK:
394 iwl_sta_ucode_deactivate(priv, addr);
395 IWL_DEBUG_ASSOC("REPLY_REMOVE_STA PASSED\n");
399 IWL_ERROR("REPLY_REMOVE_STA failed\n");
404 priv->alloc_rxb_skb--;
405 dev_kfree_skb_any(cmd.meta.u.skb);
411 * iwl_remove_station - Remove driver's knowledge of station.
413 int iwl_remove_station(struct iwl_priv *priv, const u8 *addr, int is_ap)
415 int sta_id = IWL_INVALID_STATION;
416 int i, ret = -EINVAL;
418 DECLARE_MAC_BUF(mac);
420 spin_lock_irqsave(&priv->sta_lock, flags);
424 else if (is_broadcast_ether_addr(addr))
425 sta_id = priv->hw_params.bcast_sta_id;
427 for (i = IWL_STA_ID; i < priv->hw_params.max_stations; i++)
428 if (priv->stations[i].used &&
429 !compare_ether_addr(priv->stations[i].sta.sta.addr,
435 if (unlikely(sta_id == IWL_INVALID_STATION))
438 IWL_DEBUG_ASSOC("Removing STA from driver:%d %s\n",
439 sta_id, print_mac(mac, addr));
441 if (!(priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE)) {
442 IWL_ERROR("Removing %s but non DRIVER active\n",
443 print_mac(mac, addr));
447 if (!(priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE)) {
448 IWL_ERROR("Removing %s but non UCODE active\n",
449 print_mac(mac, addr));
454 priv->stations[sta_id].used &= ~IWL_STA_DRIVER_ACTIVE;
456 priv->num_stations--;
458 BUG_ON(priv->num_stations < 0);
460 spin_unlock_irqrestore(&priv->sta_lock, flags);
462 ret = iwl_send_remove_station(priv, addr, CMD_ASYNC);
465 spin_unlock_irqrestore(&priv->sta_lock, flags);
468 EXPORT_SYMBOL(iwl_remove_station);
470 static int iwl_get_free_ucode_key_index(struct iwl_priv *priv)
474 for (i = 0; i < STA_KEY_MAX_NUM; i++)
475 if (!test_and_set_bit(i, &priv->ucode_key_table))
478 return WEP_INVALID_OFFSET;
481 int iwl_send_static_wepkey_cmd(struct iwl_priv *priv, u8 send_if_empty)
483 int i, not_empty = 0;
484 u8 buff[sizeof(struct iwl_wep_cmd) +
485 sizeof(struct iwl_wep_key) * WEP_KEYS_MAX];
486 struct iwl_wep_cmd *wep_cmd = (struct iwl_wep_cmd *)buff;
487 size_t cmd_size = sizeof(struct iwl_wep_cmd);
488 struct iwl_host_cmd cmd = {
491 .meta.flags = CMD_ASYNC,
494 memset(wep_cmd, 0, cmd_size +
495 (sizeof(struct iwl_wep_key) * WEP_KEYS_MAX));
497 for (i = 0; i < WEP_KEYS_MAX ; i++) {
498 wep_cmd->key[i].key_index = i;
499 if (priv->wep_keys[i].key_size) {
500 wep_cmd->key[i].key_offset = i;
503 wep_cmd->key[i].key_offset = WEP_INVALID_OFFSET;
506 wep_cmd->key[i].key_size = priv->wep_keys[i].key_size;
507 memcpy(&wep_cmd->key[i].key[3], priv->wep_keys[i].key,
508 priv->wep_keys[i].key_size);
511 wep_cmd->global_key_type = WEP_KEY_WEP_TYPE;
512 wep_cmd->num_keys = WEP_KEYS_MAX;
514 cmd_size += sizeof(struct iwl_wep_key) * WEP_KEYS_MAX;
518 if (not_empty || send_if_empty)
519 return iwl_send_cmd(priv, &cmd);
523 EXPORT_SYMBOL(iwl_send_static_wepkey_cmd);
525 int iwl_remove_default_wep_key(struct iwl_priv *priv,
526 struct ieee80211_key_conf *keyconf)
531 spin_lock_irqsave(&priv->sta_lock, flags);
533 if (!test_and_clear_bit(keyconf->keyidx, &priv->ucode_key_table))
534 IWL_ERROR("index %d not used in uCode key table.\n",
537 priv->default_wep_key--;
538 memset(&priv->wep_keys[keyconf->keyidx], 0, sizeof(priv->wep_keys[0]));
539 ret = iwl_send_static_wepkey_cmd(priv, 1);
540 IWL_DEBUG_WEP("Remove default WEP key: idx=%d ret=%d\n",
541 keyconf->keyidx, ret);
542 spin_unlock_irqrestore(&priv->sta_lock, flags);
546 EXPORT_SYMBOL(iwl_remove_default_wep_key);
548 int iwl_set_default_wep_key(struct iwl_priv *priv,
549 struct ieee80211_key_conf *keyconf)
554 if (keyconf->keylen != WEP_KEY_LEN_128 &&
555 keyconf->keylen != WEP_KEY_LEN_64) {
556 IWL_DEBUG_WEP("Bad WEP key length %d\n", keyconf->keylen);
560 keyconf->flags &= ~IEEE80211_KEY_FLAG_GENERATE_IV;
561 keyconf->hw_key_idx = HW_KEY_DEFAULT;
562 priv->stations[IWL_AP_ID].keyinfo.alg = ALG_WEP;
564 spin_lock_irqsave(&priv->sta_lock, flags);
565 priv->default_wep_key++;
567 if (test_and_set_bit(keyconf->keyidx, &priv->ucode_key_table))
568 IWL_ERROR("index %d already used in uCode key table.\n",
571 priv->wep_keys[keyconf->keyidx].key_size = keyconf->keylen;
572 memcpy(&priv->wep_keys[keyconf->keyidx].key, &keyconf->key,
575 ret = iwl_send_static_wepkey_cmd(priv, 0);
576 IWL_DEBUG_WEP("Set default WEP key: len=%d idx=%d ret=%d\n",
577 keyconf->keylen, keyconf->keyidx, ret);
578 spin_unlock_irqrestore(&priv->sta_lock, flags);
582 EXPORT_SYMBOL(iwl_set_default_wep_key);
584 static int iwl_set_wep_dynamic_key_info(struct iwl_priv *priv,
585 struct ieee80211_key_conf *keyconf,
589 __le16 key_flags = 0;
592 keyconf->flags &= ~IEEE80211_KEY_FLAG_GENERATE_IV;
594 key_flags |= (STA_KEY_FLG_WEP | STA_KEY_FLG_MAP_KEY_MSK);
595 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
596 key_flags &= ~STA_KEY_FLG_INVALID;
598 if (keyconf->keylen == WEP_KEY_LEN_128)
599 key_flags |= STA_KEY_FLG_KEY_SIZE_MSK;
601 if (sta_id == priv->hw_params.bcast_sta_id)
602 key_flags |= STA_KEY_MULTICAST_MSK;
604 spin_lock_irqsave(&priv->sta_lock, flags);
606 priv->stations[sta_id].keyinfo.alg = keyconf->alg;
607 priv->stations[sta_id].keyinfo.keylen = keyconf->keylen;
608 priv->stations[sta_id].keyinfo.keyidx = keyconf->keyidx;
610 memcpy(priv->stations[sta_id].keyinfo.key,
611 keyconf->key, keyconf->keylen);
613 memcpy(&priv->stations[sta_id].sta.key.key[3],
614 keyconf->key, keyconf->keylen);
616 if ((priv->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
617 == STA_KEY_FLG_NO_ENC)
618 priv->stations[sta_id].sta.key.key_offset =
619 iwl_get_free_ucode_key_index(priv);
620 /* else, we are overriding an existing key => no need to allocated room
623 WARN(priv->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
624 "no space for new kew");
626 priv->stations[sta_id].sta.key.key_flags = key_flags;
627 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
628 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
630 ret = iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
632 spin_unlock_irqrestore(&priv->sta_lock, flags);
637 static int iwl_set_ccmp_dynamic_key_info(struct iwl_priv *priv,
638 struct ieee80211_key_conf *keyconf,
642 __le16 key_flags = 0;
645 key_flags |= (STA_KEY_FLG_CCMP | STA_KEY_FLG_MAP_KEY_MSK);
646 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
647 key_flags &= ~STA_KEY_FLG_INVALID;
649 if (sta_id == priv->hw_params.bcast_sta_id)
650 key_flags |= STA_KEY_MULTICAST_MSK;
652 keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
654 spin_lock_irqsave(&priv->sta_lock, flags);
655 priv->stations[sta_id].keyinfo.alg = keyconf->alg;
656 priv->stations[sta_id].keyinfo.keylen = keyconf->keylen;
658 memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key,
661 memcpy(priv->stations[sta_id].sta.key.key, keyconf->key,
664 if ((priv->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
665 == STA_KEY_FLG_NO_ENC)
666 priv->stations[sta_id].sta.key.key_offset =
667 iwl_get_free_ucode_key_index(priv);
668 /* else, we are overriding an existing key => no need to allocated room
671 WARN(priv->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
672 "no space for new kew");
674 priv->stations[sta_id].sta.key.key_flags = key_flags;
675 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
676 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
678 ret = iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
680 spin_unlock_irqrestore(&priv->sta_lock, flags);
685 static int iwl_set_tkip_dynamic_key_info(struct iwl_priv *priv,
686 struct ieee80211_key_conf *keyconf,
692 keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
693 keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
695 spin_lock_irqsave(&priv->sta_lock, flags);
697 priv->stations[sta_id].keyinfo.alg = keyconf->alg;
698 priv->stations[sta_id].keyinfo.keylen = 16;
700 if ((priv->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
701 == STA_KEY_FLG_NO_ENC)
702 priv->stations[sta_id].sta.key.key_offset =
703 iwl_get_free_ucode_key_index(priv);
704 /* else, we are overriding an existing key => no need to allocated room
707 WARN(priv->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
708 "no space for new kew");
710 /* This copy is acutally not needed: we get the key with each TX */
711 memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key, 16);
713 memcpy(priv->stations[sta_id].sta.key.key, keyconf->key, 16);
715 spin_unlock_irqrestore(&priv->sta_lock, flags);
720 int iwl_remove_dynamic_key(struct iwl_priv *priv,
721 struct ieee80211_key_conf *keyconf,
729 priv->key_mapping_key--;
731 spin_lock_irqsave(&priv->sta_lock, flags);
732 key_flags = le16_to_cpu(priv->stations[sta_id].sta.key.key_flags);
733 keyidx = (key_flags >> STA_KEY_FLG_KEYID_POS) & 0x3;
735 IWL_DEBUG_WEP("Remove dynamic key: idx=%d sta=%d\n",
736 keyconf->keyidx, sta_id);
738 if (keyconf->keyidx != keyidx) {
739 /* We need to remove a key with index different that the one
740 * in the uCode. This means that the key we need to remove has
741 * been replaced by another one with different index.
742 * Don't do anything and return ok
744 spin_unlock_irqrestore(&priv->sta_lock, flags);
748 if (priv->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET) {
749 IWL_WARNING("Removing wrong key %d 0x%x\n",
750 keyconf->keyidx, key_flags);
751 spin_unlock_irqrestore(&priv->sta_lock, flags);
755 if (!test_and_clear_bit(priv->stations[sta_id].sta.key.key_offset,
756 &priv->ucode_key_table))
757 IWL_ERROR("index %d not used in uCode key table.\n",
758 priv->stations[sta_id].sta.key.key_offset);
759 memset(&priv->stations[sta_id].keyinfo, 0,
760 sizeof(struct iwl_hw_key));
761 memset(&priv->stations[sta_id].sta.key, 0,
762 sizeof(struct iwl4965_keyinfo));
763 priv->stations[sta_id].sta.key.key_flags =
764 STA_KEY_FLG_NO_ENC | STA_KEY_FLG_INVALID;
765 priv->stations[sta_id].sta.key.key_offset = WEP_INVALID_OFFSET;
766 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
767 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
769 ret = iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
770 spin_unlock_irqrestore(&priv->sta_lock, flags);
773 EXPORT_SYMBOL(iwl_remove_dynamic_key);
775 int iwl_set_dynamic_key(struct iwl_priv *priv,
776 struct ieee80211_key_conf *keyconf, u8 sta_id)
780 priv->key_mapping_key++;
781 keyconf->hw_key_idx = HW_KEY_DYNAMIC;
783 switch (keyconf->alg) {
785 ret = iwl_set_ccmp_dynamic_key_info(priv, keyconf, sta_id);
788 ret = iwl_set_tkip_dynamic_key_info(priv, keyconf, sta_id);
791 ret = iwl_set_wep_dynamic_key_info(priv, keyconf, sta_id);
794 IWL_ERROR("Unknown alg: %s alg = %d\n", __func__, keyconf->alg);
798 IWL_DEBUG_WEP("Set dynamic key: alg= %d len=%d idx=%d sta=%d ret=%d\n",
799 keyconf->alg, keyconf->keylen, keyconf->keyidx,
804 EXPORT_SYMBOL(iwl_set_dynamic_key);
806 #ifdef CONFIG_IWLWIFI_DEBUG
807 static void iwl_dump_lq_cmd(struct iwl_priv *priv,
808 struct iwl_link_quality_cmd *lq)
811 IWL_DEBUG_RATE("lq station id 0x%x\n", lq->sta_id);
812 IWL_DEBUG_RATE("lq dta 0x%X 0x%X\n",
813 lq->general_params.single_stream_ant_msk,
814 lq->general_params.dual_stream_ant_msk);
816 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
817 IWL_DEBUG_RATE("lq index %d 0x%X\n",
818 i, lq->rs_table[i].rate_n_flags);
821 static inline void iwl_dump_lq_cmd(struct iwl_priv *priv,
822 struct iwl_link_quality_cmd *lq)
827 int iwl_send_lq_cmd(struct iwl_priv *priv,
828 struct iwl_link_quality_cmd *lq, u8 flags)
830 struct iwl_host_cmd cmd = {
831 .id = REPLY_TX_LINK_QUALITY_CMD,
832 .len = sizeof(struct iwl_link_quality_cmd),
837 if ((lq->sta_id == 0xFF) &&
838 (priv->iw_mode == NL80211_IFTYPE_ADHOC))
841 if (lq->sta_id == 0xFF)
842 lq->sta_id = IWL_AP_ID;
844 iwl_dump_lq_cmd(priv, lq);
846 if (iwl_is_associated(priv) && priv->assoc_station_added)
847 return iwl_send_cmd(priv, &cmd);
851 EXPORT_SYMBOL(iwl_send_lq_cmd);
854 * iwl_sta_init_lq - Initialize a station's hardware rate table
856 * The uCode's station table contains a table of fallback rates
857 * for automatic fallback during transmission.
859 * NOTE: This sets up a default set of values. These will be replaced later
860 * if the driver's iwl-agn-rs rate scaling algorithm is used, instead of
863 * NOTE: Run REPLY_ADD_STA command to set up station table entry, before
864 * calling this function (which runs REPLY_TX_LINK_QUALITY_CMD,
865 * which requires station table entry to exist).
867 static void iwl_sta_init_lq(struct iwl_priv *priv, const u8 *addr, int is_ap)
870 struct iwl_link_quality_cmd link_cmd = {
875 /* Set up the rate scaling to start at selected rate, fall back
876 * all the way down to 1M in IEEE order, and then spin on 1M */
878 r = IWL_RATE_54M_INDEX;
879 else if (priv->band == IEEE80211_BAND_5GHZ)
880 r = IWL_RATE_6M_INDEX;
882 r = IWL_RATE_1M_INDEX;
884 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
886 if (r >= IWL_FIRST_CCK_RATE && r <= IWL_LAST_CCK_RATE)
887 rate_flags |= RATE_MCS_CCK_MSK;
889 /* Use Tx antenna B only */
890 rate_flags |= RATE_MCS_ANT_B_MSK; /*FIXME:RS*/
892 link_cmd.rs_table[i].rate_n_flags =
893 iwl_hw_set_rate_n_flags(iwl_rates[r].plcp, rate_flags);
894 r = iwl4965_get_prev_ieee_rate(r);
897 link_cmd.general_params.single_stream_ant_msk = 2;
898 link_cmd.general_params.dual_stream_ant_msk = 3;
899 link_cmd.agg_params.agg_dis_start_th = 3;
900 link_cmd.agg_params.agg_time_limit = cpu_to_le16(4000);
902 /* Update the rate scaling for control frame Tx to AP */
903 link_cmd.sta_id = is_ap ? IWL_AP_ID : priv->hw_params.bcast_sta_id;
905 iwl_send_cmd_pdu_async(priv, REPLY_TX_LINK_QUALITY_CMD,
906 sizeof(link_cmd), &link_cmd, NULL);
910 * iwl_rxon_add_station - add station into station table.
912 * there is only one AP station with id= IWL_AP_ID
913 * NOTE: mutex must be held before calling this fnction
915 int iwl_rxon_add_station(struct iwl_priv *priv, const u8 *addr, int is_ap)
919 /* Add station to device's station table */
920 struct ieee80211_conf *conf = &priv->hw->conf;
921 struct ieee80211_ht_info *cur_ht_config = &conf->ht_conf;
924 (conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) &&
925 (priv->iw_mode == NL80211_IFTYPE_STATION))
926 sta_id = iwl_add_station_flags(priv, addr, is_ap,
929 sta_id = iwl_add_station_flags(priv, addr, is_ap,
932 /* Set up default rate scaling table in device's station table */
933 iwl_sta_init_lq(priv, addr, is_ap);
937 EXPORT_SYMBOL(iwl_rxon_add_station);
940 * iwl_get_sta_id - Find station's index within station table
942 * If new IBSS station, create new entry in station table
944 int iwl_get_sta_id(struct iwl_priv *priv, struct ieee80211_hdr *hdr)
947 u16 fc = le16_to_cpu(hdr->frame_control);
948 DECLARE_MAC_BUF(mac);
950 /* If this frame is broadcast or management, use broadcast station id */
951 if (((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA) ||
952 is_multicast_ether_addr(hdr->addr1))
953 return priv->hw_params.bcast_sta_id;
955 switch (priv->iw_mode) {
957 /* If we are a client station in a BSS network, use the special
958 * AP station entry (that's the only station we communicate with) */
959 case NL80211_IFTYPE_STATION:
962 /* If we are an AP, then find the station, or use BCAST */
963 case NL80211_IFTYPE_AP:
964 sta_id = iwl_find_station(priv, hdr->addr1);
965 if (sta_id != IWL_INVALID_STATION)
967 return priv->hw_params.bcast_sta_id;
969 /* If this frame is going out to an IBSS network, find the station,
970 * or create a new station table entry */
971 case NL80211_IFTYPE_ADHOC:
972 sta_id = iwl_find_station(priv, hdr->addr1);
973 if (sta_id != IWL_INVALID_STATION)
976 /* Create new station table entry */
977 sta_id = iwl_add_station_flags(priv, hdr->addr1,
980 if (sta_id != IWL_INVALID_STATION)
983 IWL_DEBUG_DROP("Station %s not in station map. "
984 "Defaulting to broadcast...\n",
985 print_mac(mac, hdr->addr1));
986 iwl_print_hex_dump(priv, IWL_DL_DROP, (u8 *) hdr, sizeof(*hdr));
987 return priv->hw_params.bcast_sta_id;
989 /* If we are in monitor mode, use BCAST. This is required for
990 * packet injection. */
991 case NL80211_IFTYPE_MONITOR:
992 return priv->hw_params.bcast_sta_id;
995 IWL_WARNING("Unknown mode of operation: %d\n", priv->iw_mode);
996 return priv->hw_params.bcast_sta_id;
999 EXPORT_SYMBOL(iwl_get_sta_id);
1002 * iwl_sta_modify_enable_tid_tx - Enable Tx for this TID in station table
1004 void iwl_sta_modify_enable_tid_tx(struct iwl_priv *priv, int sta_id, int tid)
1006 unsigned long flags;
1008 /* Remove "disable" flag, to enable Tx for this TID */
1009 spin_lock_irqsave(&priv->sta_lock, flags);
1010 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_TID_DISABLE_TX;
1011 priv->stations[sta_id].sta.tid_disable_tx &= cpu_to_le16(~(1 << tid));
1012 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1013 spin_unlock_irqrestore(&priv->sta_lock, flags);
1015 iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
1017 EXPORT_SYMBOL(iwl_sta_modify_enable_tid_tx);