blob: 312099099ed3b52a2e5987ee4342e3d3b1926905 [file] [log] [blame]
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07001/******************************************************************************
2 *
Reinette Chatre1f447802010-01-15 13:43:41 -08003 * Copyright(c) 2003 - 2010 Intel Corporation. All rights reserved.
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07004 *
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:
Winkler, Tomas759ef892008-12-09 11:28:58 -080025 * Intel Linux Wireless <ilw@linux.intel.com>
Emmanuel Grumbach6974e362008-04-14 21:16:06 -070026 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
27 *
28 *****************************************************************************/
29
30#include <net/mac80211.h>
Tomas Winkler947b13a2008-04-16 16:34:48 -070031#include <linux/etherdevice.h>
Emmanuel Grumbach6974e362008-04-14 21:16:06 -070032
Tomas Winkler3e0d4cb2008-04-24 11:55:38 -070033#include "iwl-dev.h"
Emmanuel Grumbach6974e362008-04-14 21:16:06 -070034#include "iwl-core.h"
35#include "iwl-sta.h"
Tomas Winkler7a999bf2008-05-29 16:35:02 +080036
Tomas Winkler947b13a2008-04-16 16:34:48 -070037u8 iwl_find_station(struct iwl_priv *priv, const u8 *addr)
38{
39 int i;
40 int start = 0;
41 int ret = IWL_INVALID_STATION;
42 unsigned long flags;
Tomas Winkler947b13a2008-04-16 16:34:48 -070043
Johannes Berg05c914f2008-09-11 00:01:58 +020044 if ((priv->iw_mode == NL80211_IFTYPE_ADHOC) ||
45 (priv->iw_mode == NL80211_IFTYPE_AP))
Tomas Winkler947b13a2008-04-16 16:34:48 -070046 start = IWL_STA_ID;
47
48 if (is_broadcast_ether_addr(addr))
49 return priv->hw_params.bcast_sta_id;
50
51 spin_lock_irqsave(&priv->sta_lock, flags);
52 for (i = start; i < priv->hw_params.max_stations; i++)
53 if (priv->stations[i].used &&
54 (!compare_ether_addr(priv->stations[i].sta.sta.addr,
55 addr))) {
56 ret = i;
57 goto out;
58 }
59
Tomas Winklere1623442009-01-27 14:27:56 -080060 IWL_DEBUG_ASSOC_LIMIT(priv, "can not find STA %pM total %d\n",
Johannes Berge1749612008-10-27 15:59:26 -070061 addr, priv->num_stations);
Tomas Winkler947b13a2008-04-16 16:34:48 -070062
63 out:
64 spin_unlock_irqrestore(&priv->sta_lock, flags);
65 return ret;
66}
67EXPORT_SYMBOL(iwl_find_station);
68
Emmanuel Grumbachbe1f3ab62008-06-12 09:47:18 +080069int iwl_get_ra_sta_id(struct iwl_priv *priv, struct ieee80211_hdr *hdr)
70{
Johannes Berg05c914f2008-09-11 00:01:58 +020071 if (priv->iw_mode == NL80211_IFTYPE_STATION) {
Emmanuel Grumbachbe1f3ab62008-06-12 09:47:18 +080072 return IWL_AP_ID;
73 } else {
74 u8 *da = ieee80211_get_DA(hdr);
Tomas Winklerc587de02009-06-03 11:44:07 -070075 return iwl_find_station(priv, da);
Emmanuel Grumbachbe1f3ab62008-06-12 09:47:18 +080076 }
77}
78EXPORT_SYMBOL(iwl_get_ra_sta_id);
79
Reinette Chatre1fa97aa2010-01-22 14:22:48 -080080/* priv->sta_lock must be held */
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +080081static void iwl_sta_ucode_activate(struct iwl_priv *priv, u8 sta_id)
82{
Reinette Chatre1fa97aa2010-01-22 14:22:48 -080083
84 if (!(priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE))
85 IWL_ERR(priv, "ACTIVATE a non DRIVER active station id %u addr %pM\n",
86 sta_id, priv->stations[sta_id].sta.sta.addr);
87
88 if (priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE) {
89 IWL_DEBUG_ASSOC(priv,
90 "STA id %u addr %pM already present in uCode (according to driver)\n",
91 sta_id, priv->stations[sta_id].sta.sta.addr);
92 } else {
93 priv->stations[sta_id].used |= IWL_STA_UCODE_ACTIVE;
94 IWL_DEBUG_ASSOC(priv, "Added STA id %u addr %pM to uCode\n",
95 sta_id, priv->stations[sta_id].sta.sta.addr);
96 }
97}
98
99static void iwl_process_add_sta_resp(struct iwl_priv *priv,
100 struct iwl_addsta_cmd *addsta,
101 struct iwl_rx_packet *pkt,
102 bool sync)
103{
104 u8 sta_id = addsta->sta.sta_id;
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800105 unsigned long flags;
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800106
Reinette Chatre1fa97aa2010-01-22 14:22:48 -0800107 if (pkt->hdr.flags & IWL_CMD_FAILED_MSK) {
108 IWL_ERR(priv, "Bad return from REPLY_ADD_STA (0x%08X)\n",
109 pkt->hdr.flags);
110 return;
111 }
112
113 IWL_DEBUG_INFO(priv, "Processing response for adding station %u\n",
114 sta_id);
115
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800116 spin_lock_irqsave(&priv->sta_lock, flags);
117
Reinette Chatre1fa97aa2010-01-22 14:22:48 -0800118 switch (pkt->u.add_sta.status) {
119 case ADD_STA_SUCCESS_MSK:
120 IWL_DEBUG_INFO(priv, "REPLY_ADD_STA PASSED\n");
121 iwl_sta_ucode_activate(priv, sta_id);
122 break;
123 case ADD_STA_NO_ROOM_IN_TABLE:
124 IWL_ERR(priv, "Adding station %d failed, no room in table.\n",
Winkler, Tomas15b16872008-12-19 10:37:33 +0800125 sta_id);
Reinette Chatre1fa97aa2010-01-22 14:22:48 -0800126 break;
127 case ADD_STA_NO_BLOCK_ACK_RESOURCE:
128 IWL_ERR(priv, "Adding station %d failed, no block ack resource.\n",
129 sta_id);
130 break;
131 case ADD_STA_MODIFY_NON_EXIST_STA:
132 IWL_ERR(priv, "Attempting to modify non-existing station %d \n",
133 sta_id);
134 break;
135 default:
136 IWL_DEBUG_ASSOC(priv, "Received REPLY_ADD_STA:(0x%08X)\n",
137 pkt->u.add_sta.status);
138 break;
139 }
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800140
Reinette Chatre1fa97aa2010-01-22 14:22:48 -0800141 IWL_DEBUG_INFO(priv, "%s station id %u addr %pM\n",
142 priv->stations[sta_id].sta.mode ==
143 STA_CONTROL_MODIFY_MSK ? "Modified" : "Added",
144 sta_id, priv->stations[sta_id].sta.sta.addr);
145
146 /*
147 * XXX: The MAC address in the command buffer is often changed from
148 * the original sent to the device. That is, the MAC address
149 * written to the command buffer often is not the same MAC adress
150 * read from the command buffer when the command returns. This
151 * issue has not yet been resolved and this debugging is left to
152 * observe the problem.
153 */
154 IWL_DEBUG_INFO(priv, "%s station according to cmd buffer %pM\n",
155 priv->stations[sta_id].sta.mode ==
156 STA_CONTROL_MODIFY_MSK ? "Modified" : "Added",
157 addsta->sta.addr);
158
159 /*
160 * Determine if we wanted to modify or add a station,
161 * if adding a station succeeded we have some more initialization
162 * to do when using station notification. TODO
163 */
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800164
165 spin_unlock_irqrestore(&priv->sta_lock, flags);
166}
167
Johannes Berg5696aea2009-07-24 11:13:06 -0700168static void iwl_add_sta_callback(struct iwl_priv *priv,
169 struct iwl_device_cmd *cmd,
Zhu Yi2f301222009-10-09 17:19:45 +0800170 struct iwl_rx_packet *pkt)
Tomas Winkler42132bc2008-05-29 16:35:03 +0800171{
Tomas Winkler3257e5d2008-10-14 12:32:43 -0700172 struct iwl_addsta_cmd *addsta =
173 (struct iwl_addsta_cmd *)cmd->cmd.payload;
Tomas Winkler42132bc2008-05-29 16:35:03 +0800174
Reinette Chatre1fa97aa2010-01-22 14:22:48 -0800175 iwl_process_add_sta_resp(priv, addsta, pkt, false);
Tomas Winkler42132bc2008-05-29 16:35:03 +0800176
Tomas Winkler42132bc2008-05-29 16:35:03 +0800177}
178
Samuel Ortiz17f841c2009-01-23 13:45:20 -0800179int iwl_send_add_sta(struct iwl_priv *priv,
Tomas Winkler133636d2008-05-05 10:22:34 +0800180 struct iwl_addsta_cmd *sta, u8 flags)
181{
Zhu Yi2f301222009-10-09 17:19:45 +0800182 struct iwl_rx_packet *pkt = NULL;
Tomas Winkler133636d2008-05-05 10:22:34 +0800183 int ret = 0;
184 u8 data[sizeof(*sta)];
185 struct iwl_host_cmd cmd = {
186 .id = REPLY_ADD_STA,
Johannes Bergc2acea82009-07-24 11:13:05 -0700187 .flags = flags,
Tomas Winkler133636d2008-05-05 10:22:34 +0800188 .data = data,
189 };
190
Tomas Winkler42132bc2008-05-29 16:35:03 +0800191 if (flags & CMD_ASYNC)
Johannes Bergc2acea82009-07-24 11:13:05 -0700192 cmd.callback = iwl_add_sta_callback;
Tomas Winkler42132bc2008-05-29 16:35:03 +0800193 else
Johannes Bergc2acea82009-07-24 11:13:05 -0700194 cmd.flags |= CMD_WANT_SKB;
Tomas Winkler133636d2008-05-05 10:22:34 +0800195
196 cmd.len = priv->cfg->ops->utils->build_addsta_hcmd(sta, data);
197 ret = iwl_send_cmd(priv, &cmd);
198
199 if (ret || (flags & CMD_ASYNC))
200 return ret;
201
Tomas Winkler133636d2008-05-05 10:22:34 +0800202 if (ret == 0) {
Reinette Chatre1fa97aa2010-01-22 14:22:48 -0800203 pkt = (struct iwl_rx_packet *)cmd.reply_page;
204 iwl_process_add_sta_resp(priv, sta, pkt, true);
Tomas Winkler133636d2008-05-05 10:22:34 +0800205 }
Zhu Yi64a76b52009-12-10 14:37:21 -0800206 iwl_free_pages(priv, cmd.reply_page);
Tomas Winkler133636d2008-05-05 10:22:34 +0800207
208 return ret;
209}
Samuel Ortiz17f841c2009-01-23 13:45:20 -0800210EXPORT_SYMBOL(iwl_send_add_sta);
Tomas Winkler947b13a2008-04-16 16:34:48 -0700211
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800212static void iwl_set_ht_add_station(struct iwl_priv *priv, u8 index,
Johannes Bergd9fe60d2008-10-09 12:13:49 +0200213 struct ieee80211_sta_ht_cap *sta_ht_inf)
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800214{
215 __le32 sta_flags;
216 u8 mimo_ps_mode;
217
218 if (!sta_ht_inf || !sta_ht_inf->ht_supported)
219 goto done;
220
Tomas Winkler00c5ae22008-09-03 11:26:42 +0800221 mimo_ps_mode = (sta_ht_inf->cap & IEEE80211_HT_CAP_SM_PS) >> 2;
Wey-Yi Guy3f3e0372009-10-30 14:36:17 -0700222 IWL_DEBUG_ASSOC(priv, "spatial multiplexing power save mode: %s\n",
223 (mimo_ps_mode == WLAN_HT_CAP_SM_PS_STATIC) ?
224 "static" :
225 (mimo_ps_mode == WLAN_HT_CAP_SM_PS_DYNAMIC) ?
226 "dynamic" : "disabled");
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800227
228 sta_flags = priv->stations[index].sta.station_flags;
229
230 sta_flags &= ~(STA_FLG_RTS_MIMO_PROT_MSK | STA_FLG_MIMO_DIS_MSK);
231
232 switch (mimo_ps_mode) {
Tomas Winkler00c5ae22008-09-03 11:26:42 +0800233 case WLAN_HT_CAP_SM_PS_STATIC:
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800234 sta_flags |= STA_FLG_MIMO_DIS_MSK;
235 break;
Tomas Winkler00c5ae22008-09-03 11:26:42 +0800236 case WLAN_HT_CAP_SM_PS_DYNAMIC:
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800237 sta_flags |= STA_FLG_RTS_MIMO_PROT_MSK;
238 break;
Tomas Winkler00c5ae22008-09-03 11:26:42 +0800239 case WLAN_HT_CAP_SM_PS_DISABLED:
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800240 break;
241 default:
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800242 IWL_WARN(priv, "Invalid MIMO PS mode %d\n", mimo_ps_mode);
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800243 break;
244 }
245
246 sta_flags |= cpu_to_le32(
247 (u32)sta_ht_inf->ampdu_factor << STA_FLG_MAX_AGG_SIZE_POS);
248
249 sta_flags |= cpu_to_le32(
250 (u32)sta_ht_inf->ampdu_density << STA_FLG_AGG_MPDU_DENSITY_POS);
251
Wey-Yi Guy7aafef12009-08-07 15:41:38 -0700252 if (iwl_is_ht40_tx_allowed(priv, sta_ht_inf))
253 sta_flags |= STA_FLG_HT40_EN_MSK;
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800254 else
Wey-Yi Guy7aafef12009-08-07 15:41:38 -0700255 sta_flags &= ~STA_FLG_HT40_EN_MSK;
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800256
257 priv->stations[index].sta.station_flags = sta_flags;
258 done:
259 return;
260}
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800261
262/**
Tomas Winklerc587de02009-06-03 11:44:07 -0700263 * iwl_add_station - Add station to tables in driver and device
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800264 */
Tomas Winklerc587de02009-06-03 11:44:07 -0700265u8 iwl_add_station(struct iwl_priv *priv, const u8 *addr, bool is_ap, u8 flags,
266 struct ieee80211_sta_ht_cap *ht_info)
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800267{
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800268 struct iwl_station_entry *station;
269 unsigned long flags_spin;
Tomas Winklerc587de02009-06-03 11:44:07 -0700270 int i;
271 int sta_id = IWL_INVALID_STATION;
272 u16 rate;
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800273
274 spin_lock_irqsave(&priv->sta_lock, flags_spin);
275 if (is_ap)
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800276 sta_id = IWL_AP_ID;
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800277 else if (is_broadcast_ether_addr(addr))
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800278 sta_id = priv->hw_params.bcast_sta_id;
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800279 else
280 for (i = IWL_STA_ID; i < priv->hw_params.max_stations; i++) {
281 if (!compare_ether_addr(priv->stations[i].sta.sta.addr,
282 addr)) {
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800283 sta_id = i;
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800284 break;
285 }
286
287 if (!priv->stations[i].used &&
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800288 sta_id == IWL_INVALID_STATION)
289 sta_id = i;
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800290 }
291
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800292 /* These two conditions have the same outcome, but keep them separate
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800293 since they have different meanings */
294 if (unlikely(sta_id == IWL_INVALID_STATION)) {
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800295 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800296 return sta_id;
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800297 }
298
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800299 if (priv->stations[sta_id].used &&
300 !compare_ether_addr(priv->stations[sta_id].sta.sta.addr, addr)) {
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800301 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800302 return sta_id;
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800303 }
304
305
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800306 station = &priv->stations[sta_id];
307 station->used = IWL_STA_DRIVER_ACTIVE;
Tomas Winklere1623442009-01-27 14:27:56 -0800308 IWL_DEBUG_ASSOC(priv, "Add STA to driver ID %d: %pM\n",
Johannes Berge1749612008-10-27 15:59:26 -0700309 sta_id, addr);
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800310 priv->num_stations++;
311
312 /* Set up the REPLY_ADD_STA command to send to device */
313 memset(&station->sta, 0, sizeof(struct iwl_addsta_cmd));
314 memcpy(station->sta.sta.addr, addr, ETH_ALEN);
315 station->sta.mode = 0;
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800316 station->sta.sta.sta_id = sta_id;
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800317 station->sta.station_flags = 0;
318
319 /* BCAST station and IBSS stations do not work in HT mode */
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800320 if (sta_id != priv->hw_params.bcast_sta_id &&
Johannes Berg05c914f2008-09-11 00:01:58 +0200321 priv->iw_mode != NL80211_IFTYPE_ADHOC)
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800322 iwl_set_ht_add_station(priv, sta_id, ht_info);
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800323
Tomas Winklerc587de02009-06-03 11:44:07 -0700324 /* 3945 only */
325 rate = (priv->band == IEEE80211_BAND_5GHZ) ?
326 IWL_RATE_6M_PLCP : IWL_RATE_1M_PLCP;
327 /* Turn on both antennas for the station... */
328 station->sta.rate_n_flags = cpu_to_le16(rate | RATE_MCS_ANT_AB_MSK);
329
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800330 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
331
332 /* Add station to device's station table */
333 iwl_send_add_sta(priv, &station->sta, flags);
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800334 return sta_id;
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800335
336}
Tomas Winklerc587de02009-06-03 11:44:07 -0700337EXPORT_SYMBOL(iwl_add_station);
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800338
Johannes Berg0b5d9b22010-01-22 14:22:34 -0800339static void iwl_sta_ucode_deactivate(struct iwl_priv *priv, const u8 *addr)
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800340{
341 unsigned long flags;
Tomas Winklerc587de02009-06-03 11:44:07 -0700342 u8 sta_id = iwl_find_station(priv, addr);
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800343
344 BUG_ON(sta_id == IWL_INVALID_STATION);
345
Tomas Winklere1623442009-01-27 14:27:56 -0800346 IWL_DEBUG_ASSOC(priv, "Removed STA from Ucode: %pM\n", addr);
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800347
348 spin_lock_irqsave(&priv->sta_lock, flags);
349
350 /* Ucode must be active and driver must be non active */
351 if (priv->stations[sta_id].used != IWL_STA_UCODE_ACTIVE)
Winkler, Tomas15b16872008-12-19 10:37:33 +0800352 IWL_ERR(priv, "removed non active STA %d\n", sta_id);
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800353
354 priv->stations[sta_id].used &= ~IWL_STA_UCODE_ACTIVE;
355
356 memset(&priv->stations[sta_id], 0, sizeof(struct iwl_station_entry));
357 spin_unlock_irqrestore(&priv->sta_lock, flags);
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800358}
359
Johannes Berg5696aea2009-07-24 11:13:06 -0700360static void iwl_remove_sta_callback(struct iwl_priv *priv,
361 struct iwl_device_cmd *cmd,
Zhu Yi2f301222009-10-09 17:19:45 +0800362 struct iwl_rx_packet *pkt)
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800363{
Tomas Winkler3257e5d2008-10-14 12:32:43 -0700364 struct iwl_rem_sta_cmd *rm_sta =
Zhu Yi2f301222009-10-09 17:19:45 +0800365 (struct iwl_rem_sta_cmd *)cmd->cmd.payload;
Johannes Berg0b5d9b22010-01-22 14:22:34 -0800366 const u8 *addr = rm_sta->addr;
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800367
Zhu Yi2f301222009-10-09 17:19:45 +0800368 if (pkt->hdr.flags & IWL_CMD_FAILED_MSK) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800369 IWL_ERR(priv, "Bad return from REPLY_REMOVE_STA (0x%08X)\n",
Zhu Yi2f301222009-10-09 17:19:45 +0800370 pkt->hdr.flags);
Johannes Berg5696aea2009-07-24 11:13:06 -0700371 return;
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800372 }
373
Zhu Yi2f301222009-10-09 17:19:45 +0800374 switch (pkt->u.rem_sta.status) {
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800375 case REM_STA_SUCCESS_MSK:
376 iwl_sta_ucode_deactivate(priv, addr);
377 break;
378 default:
Winkler, Tomas15b16872008-12-19 10:37:33 +0800379 IWL_ERR(priv, "REPLY_REMOVE_STA failed\n");
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800380 break;
381 }
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800382}
383
384static int iwl_send_remove_station(struct iwl_priv *priv, const u8 *addr,
385 u8 flags)
386{
Zhu Yi2f301222009-10-09 17:19:45 +0800387 struct iwl_rx_packet *pkt;
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800388 int ret;
389
390 struct iwl_rem_sta_cmd rm_sta_cmd;
391
392 struct iwl_host_cmd cmd = {
393 .id = REPLY_REMOVE_STA,
394 .len = sizeof(struct iwl_rem_sta_cmd),
Johannes Bergc2acea82009-07-24 11:13:05 -0700395 .flags = flags,
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800396 .data = &rm_sta_cmd,
397 };
398
399 memset(&rm_sta_cmd, 0, sizeof(rm_sta_cmd));
400 rm_sta_cmd.num_sta = 1;
401 memcpy(&rm_sta_cmd.addr, addr , ETH_ALEN);
402
403 if (flags & CMD_ASYNC)
Johannes Bergc2acea82009-07-24 11:13:05 -0700404 cmd.callback = iwl_remove_sta_callback;
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800405 else
Johannes Bergc2acea82009-07-24 11:13:05 -0700406 cmd.flags |= CMD_WANT_SKB;
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800407 ret = iwl_send_cmd(priv, &cmd);
408
409 if (ret || (flags & CMD_ASYNC))
410 return ret;
411
Zhu Yi2f301222009-10-09 17:19:45 +0800412 pkt = (struct iwl_rx_packet *)cmd.reply_page;
413 if (pkt->hdr.flags & IWL_CMD_FAILED_MSK) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800414 IWL_ERR(priv, "Bad return from REPLY_REMOVE_STA (0x%08X)\n",
Zhu Yi2f301222009-10-09 17:19:45 +0800415 pkt->hdr.flags);
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800416 ret = -EIO;
417 }
418
419 if (!ret) {
Zhu Yi2f301222009-10-09 17:19:45 +0800420 switch (pkt->u.rem_sta.status) {
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800421 case REM_STA_SUCCESS_MSK:
422 iwl_sta_ucode_deactivate(priv, addr);
Tomas Winklere1623442009-01-27 14:27:56 -0800423 IWL_DEBUG_ASSOC(priv, "REPLY_REMOVE_STA PASSED\n");
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800424 break;
425 default:
426 ret = -EIO;
Winkler, Tomas15b16872008-12-19 10:37:33 +0800427 IWL_ERR(priv, "REPLY_REMOVE_STA failed\n");
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800428 break;
429 }
430 }
Zhu Yi64a76b52009-12-10 14:37:21 -0800431 iwl_free_pages(priv, cmd.reply_page);
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800432
433 return ret;
434}
Emmanuel Grumbachbe1f3ab62008-06-12 09:47:18 +0800435
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800436/**
437 * iwl_remove_station - Remove driver's knowledge of station.
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800438 */
Tomas Winklerc587de02009-06-03 11:44:07 -0700439int iwl_remove_station(struct iwl_priv *priv, const u8 *addr, bool is_ap)
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800440{
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800441 int sta_id = IWL_INVALID_STATION;
442 int i, ret = -EINVAL;
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800443 unsigned long flags;
444
445 spin_lock_irqsave(&priv->sta_lock, flags);
446
447 if (is_ap)
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800448 sta_id = IWL_AP_ID;
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800449 else if (is_broadcast_ether_addr(addr))
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800450 sta_id = priv->hw_params.bcast_sta_id;
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800451 else
452 for (i = IWL_STA_ID; i < priv->hw_params.max_stations; i++)
453 if (priv->stations[i].used &&
454 !compare_ether_addr(priv->stations[i].sta.sta.addr,
455 addr)) {
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800456 sta_id = i;
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800457 break;
458 }
459
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800460 if (unlikely(sta_id == IWL_INVALID_STATION))
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800461 goto out;
462
Tomas Winklere1623442009-01-27 14:27:56 -0800463 IWL_DEBUG_ASSOC(priv, "Removing STA from driver:%d %pM\n",
Johannes Berge1749612008-10-27 15:59:26 -0700464 sta_id, addr);
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800465
466 if (!(priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800467 IWL_ERR(priv, "Removing %pM but non DRIVER active\n",
Johannes Berge1749612008-10-27 15:59:26 -0700468 addr);
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800469 goto out;
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800470 }
471
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800472 if (!(priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800473 IWL_ERR(priv, "Removing %pM but non UCODE active\n",
Johannes Berge1749612008-10-27 15:59:26 -0700474 addr);
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800475 goto out;
476 }
477
478
479 priv->stations[sta_id].used &= ~IWL_STA_DRIVER_ACTIVE;
480
481 priv->num_stations--;
482
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800483 BUG_ON(priv->num_stations < 0);
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800484
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800485 spin_unlock_irqrestore(&priv->sta_lock, flags);
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800486
487 ret = iwl_send_remove_station(priv, addr, CMD_ASYNC);
488 return ret;
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800489out:
490 spin_unlock_irqrestore(&priv->sta_lock, flags);
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800491 return ret;
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800492}
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800493
Winkler, Tomas83dde8c2008-11-19 15:32:23 -0800494/**
Reinette Chatre7e246192010-02-18 22:58:32 -0800495 * iwl_clear_ucode_stations() - clear entire station table driver and/or ucode
496 * @priv:
497 * @force: If set then the uCode station table needs to be cleared here. If
498 * not set then the uCode station table has already been cleared,
499 * for example after sending it a RXON command without ASSOC bit
500 * set, and we just need to change driver state here.
Winkler, Tomas83dde8c2008-11-19 15:32:23 -0800501 */
Reinette Chatre7e246192010-02-18 22:58:32 -0800502void iwl_clear_ucode_stations(struct iwl_priv *priv, bool force)
Winkler, Tomas83dde8c2008-11-19 15:32:23 -0800503{
Mohamed Abbas48676eb2009-03-11 11:17:59 -0700504 int i;
Reinette Chatre7e246192010-02-18 22:58:32 -0800505 unsigned long flags_spin;
506 bool cleared = false;
Winkler, Tomas83dde8c2008-11-19 15:32:23 -0800507
Reinette Chatre7e246192010-02-18 22:58:32 -0800508 IWL_DEBUG_INFO(priv, "Clearing ucode stations in driver%s\n",
509 force ? " and ucode" : "");
Winkler, Tomas83dde8c2008-11-19 15:32:23 -0800510
Reinette Chatre7e246192010-02-18 22:58:32 -0800511 if (force) {
512 if (!iwl_is_ready(priv)) {
513 /*
514 * If device is not ready at this point the station
515 * table is likely already empty (uCode not ready
516 * to receive station requests) or will soon be
517 * due to interface going down.
518 */
519 IWL_DEBUG_INFO(priv, "Unable to remove stations from device - device not ready\n");
520 } else {
521 iwl_send_cmd_pdu_async(priv, REPLY_REMOVE_ALL_STA, 0, NULL, NULL);
522 }
Mohamed Abbas48676eb2009-03-11 11:17:59 -0700523 }
524
Reinette Chatre7e246192010-02-18 22:58:32 -0800525 spin_lock_irqsave(&priv->sta_lock, flags_spin);
526 if (force) {
527 IWL_DEBUG_INFO(priv, "Clearing all station information in driver\n");
528 priv->num_stations = 0;
529 memset(priv->stations, 0, sizeof(priv->stations));
530 } else {
531 for (i = 0; i < priv->hw_params.max_stations; i++) {
532 if (priv->stations[i].used & IWL_STA_UCODE_ACTIVE) {
533 IWL_DEBUG_INFO(priv, "Clearing ucode active for station %d \n", i);
534 priv->stations[i].used &= ~IWL_STA_UCODE_ACTIVE;
535 cleared = true;
536 }
537 }
538 }
539 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
540
541 if (!cleared)
542 IWL_DEBUG_INFO(priv, "No active stations found to be cleared\n");
Winkler, Tomas83dde8c2008-11-19 15:32:23 -0800543}
Reinette Chatre7e246192010-02-18 22:58:32 -0800544EXPORT_SYMBOL(iwl_clear_ucode_stations);
545
546/**
547 * iwl_restore_stations() - Restore driver known stations to device
548 *
549 * All stations considered active by driver, but not present in ucode, is
550 * restored.
551 */
552void iwl_restore_stations(struct iwl_priv *priv)
553{
554 unsigned long flags_spin;
555 int i;
556 bool found = false;
557
558 if (!iwl_is_ready(priv)) {
559 IWL_DEBUG_INFO(priv, "Not ready yet, not restoring any stations.\n");
560 return;
561 }
562
563 IWL_DEBUG_ASSOC(priv, "Restoring all known stations ... start.\n");
564 spin_lock_irqsave(&priv->sta_lock, flags_spin);
565 for (i = 0; i < priv->hw_params.max_stations; i++) {
566 if ((priv->stations[i].used & IWL_STA_DRIVER_ACTIVE) &&
567 !(priv->stations[i].used & IWL_STA_UCODE_ACTIVE)) {
568 IWL_DEBUG_ASSOC(priv, "Restoring sta %pM\n",
569 priv->stations[i].sta.sta.addr);
570 priv->stations[i].sta.mode = 0;
571 priv->stations[i].used |= IWL_STA_UCODE_INPROGRESS;
572 found = true;
573 }
574 }
575
576 for (i = 0; i < priv->hw_params.max_stations; i++) {
577 if ((priv->stations[i].used & IWL_STA_UCODE_INPROGRESS)) {
578 iwl_send_add_sta(priv, &priv->stations[i].sta,
579 CMD_ASYNC);
580 priv->stations[i].used &= ~IWL_STA_UCODE_INPROGRESS;
581 }
582 }
583
584 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
585 if (!found)
586 IWL_DEBUG_INFO(priv, "Restoring all known stations .... no stations to be restored.\n");
587 else
588 IWL_DEBUG_INFO(priv, "Restoring all known stations .... in progress.\n");
589}
590EXPORT_SYMBOL(iwl_restore_stations);
Winkler, Tomas83dde8c2008-11-19 15:32:23 -0800591
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800592int iwl_get_free_ucode_key_index(struct iwl_priv *priv)
Emmanuel Grumbach80fb47a2008-04-14 21:16:08 -0700593{
594 int i;
595
596 for (i = 0; i < STA_KEY_MAX_NUM; i++)
Emmanuel Grumbach77bab602008-04-15 16:01:44 -0700597 if (!test_and_set_bit(i, &priv->ucode_key_table))
Emmanuel Grumbach80fb47a2008-04-14 21:16:08 -0700598 return i;
599
Tomas Winkler40a9a822008-11-25 23:29:03 +0200600 return WEP_INVALID_OFFSET;
Emmanuel Grumbach80fb47a2008-04-14 21:16:08 -0700601}
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800602EXPORT_SYMBOL(iwl_get_free_ucode_key_index);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700603
604int iwl_send_static_wepkey_cmd(struct iwl_priv *priv, u8 send_if_empty)
605{
606 int i, not_empty = 0;
607 u8 buff[sizeof(struct iwl_wep_cmd) +
608 sizeof(struct iwl_wep_key) * WEP_KEYS_MAX];
609 struct iwl_wep_cmd *wep_cmd = (struct iwl_wep_cmd *)buff;
610 size_t cmd_size = sizeof(struct iwl_wep_cmd);
611 struct iwl_host_cmd cmd = {
612 .id = REPLY_WEPKEY,
613 .data = wep_cmd,
Johannes Berg72e15d72010-02-19 11:42:32 -0800614 .flags = CMD_SYNC,
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700615 };
616
Johannes Berg72e15d72010-02-19 11:42:32 -0800617 might_sleep();
618
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700619 memset(wep_cmd, 0, cmd_size +
620 (sizeof(struct iwl_wep_key) * WEP_KEYS_MAX));
621
622 for (i = 0; i < WEP_KEYS_MAX ; i++) {
623 wep_cmd->key[i].key_index = i;
624 if (priv->wep_keys[i].key_size) {
625 wep_cmd->key[i].key_offset = i;
626 not_empty = 1;
627 } else {
628 wep_cmd->key[i].key_offset = WEP_INVALID_OFFSET;
629 }
630
631 wep_cmd->key[i].key_size = priv->wep_keys[i].key_size;
632 memcpy(&wep_cmd->key[i].key[3], priv->wep_keys[i].key,
633 priv->wep_keys[i].key_size);
634 }
635
636 wep_cmd->global_key_type = WEP_KEY_WEP_TYPE;
637 wep_cmd->num_keys = WEP_KEYS_MAX;
638
639 cmd_size += sizeof(struct iwl_wep_key) * WEP_KEYS_MAX;
640
641 cmd.len = cmd_size;
642
643 if (not_empty || send_if_empty)
644 return iwl_send_cmd(priv, &cmd);
645 else
646 return 0;
647}
Tomas Winkler27aaba02008-05-05 10:22:44 +0800648EXPORT_SYMBOL(iwl_send_static_wepkey_cmd);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700649
650int iwl_remove_default_wep_key(struct iwl_priv *priv,
Emmanuel Grumbach80fb47a2008-04-14 21:16:08 -0700651 struct ieee80211_key_conf *keyconf)
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700652{
653 int ret;
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700654
Johannes Berg72e15d72010-02-19 11:42:32 -0800655 WARN_ON(!mutex_is_locked(&priv->mutex));
656
Reinette Chatre2d1bb9e2009-07-17 09:30:18 -0700657 IWL_DEBUG_WEP(priv, "Removing default WEP key: idx=%d\n",
658 keyconf->keyidx);
Emmanuel Grumbach80fb47a2008-04-14 21:16:08 -0700659
660 if (!test_and_clear_bit(keyconf->keyidx, &priv->ucode_key_table))
Winkler, Tomas15b16872008-12-19 10:37:33 +0800661 IWL_ERR(priv, "index %d not used in uCode key table.\n",
Emmanuel Grumbach80fb47a2008-04-14 21:16:08 -0700662 keyconf->keyidx);
663
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700664 priv->default_wep_key--;
Emmanuel Grumbach80fb47a2008-04-14 21:16:08 -0700665 memset(&priv->wep_keys[keyconf->keyidx], 0, sizeof(priv->wep_keys[0]));
Reinette Chatre2d1bb9e2009-07-17 09:30:18 -0700666 if (iwl_is_rfkill(priv)) {
667 IWL_DEBUG_WEP(priv, "Not sending REPLY_WEPKEY command due to RFKILL.\n");
Johannes Berg72e15d72010-02-19 11:42:32 -0800668 /* but keys in device are clear anyway so return success */
Reinette Chatre2d1bb9e2009-07-17 09:30:18 -0700669 return 0;
670 }
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700671 ret = iwl_send_static_wepkey_cmd(priv, 1);
Tomas Winklere1623442009-01-27 14:27:56 -0800672 IWL_DEBUG_WEP(priv, "Remove default WEP key: idx=%d ret=%d\n",
Emmanuel Grumbach4564ce82008-06-12 09:47:09 +0800673 keyconf->keyidx, ret);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700674
675 return ret;
676}
Tomas Winkler27aaba02008-05-05 10:22:44 +0800677EXPORT_SYMBOL(iwl_remove_default_wep_key);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700678
679int iwl_set_default_wep_key(struct iwl_priv *priv,
680 struct ieee80211_key_conf *keyconf)
681{
682 int ret;
Johannes Berg72e15d72010-02-19 11:42:32 -0800683
684 WARN_ON(!mutex_is_locked(&priv->mutex));
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700685
Emmanuel Grumbach4564ce82008-06-12 09:47:09 +0800686 if (keyconf->keylen != WEP_KEY_LEN_128 &&
687 keyconf->keylen != WEP_KEY_LEN_64) {
Tomas Winklere1623442009-01-27 14:27:56 -0800688 IWL_DEBUG_WEP(priv, "Bad WEP key length %d\n", keyconf->keylen);
Emmanuel Grumbach4564ce82008-06-12 09:47:09 +0800689 return -EINVAL;
690 }
691
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700692 keyconf->flags &= ~IEEE80211_KEY_FLAG_GENERATE_IV;
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +0800693 keyconf->hw_key_idx = HW_KEY_DEFAULT;
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700694 priv->stations[IWL_AP_ID].keyinfo.alg = ALG_WEP;
695
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700696 priv->default_wep_key++;
697
Emmanuel Grumbach80fb47a2008-04-14 21:16:08 -0700698 if (test_and_set_bit(keyconf->keyidx, &priv->ucode_key_table))
Winkler, Tomas15b16872008-12-19 10:37:33 +0800699 IWL_ERR(priv, "index %d already used in uCode key table.\n",
Johannes Berg72e15d72010-02-19 11:42:32 -0800700 keyconf->keyidx);
Emmanuel Grumbach80fb47a2008-04-14 21:16:08 -0700701
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700702 priv->wep_keys[keyconf->keyidx].key_size = keyconf->keylen;
703 memcpy(&priv->wep_keys[keyconf->keyidx].key, &keyconf->key,
704 keyconf->keylen);
705
706 ret = iwl_send_static_wepkey_cmd(priv, 0);
Tomas Winklere1623442009-01-27 14:27:56 -0800707 IWL_DEBUG_WEP(priv, "Set default WEP key: len=%d idx=%d ret=%d\n",
Emmanuel Grumbach4564ce82008-06-12 09:47:09 +0800708 keyconf->keylen, keyconf->keyidx, ret);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700709
710 return ret;
711}
Tomas Winkler27aaba02008-05-05 10:22:44 +0800712EXPORT_SYMBOL(iwl_set_default_wep_key);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700713
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700714static int iwl_set_wep_dynamic_key_info(struct iwl_priv *priv,
Emmanuel Grumbach0211ddd2008-04-14 21:16:07 -0700715 struct ieee80211_key_conf *keyconf,
716 u8 sta_id)
717{
718 unsigned long flags;
719 __le16 key_flags = 0;
720 int ret;
721
722 keyconf->flags &= ~IEEE80211_KEY_FLAG_GENERATE_IV;
Emmanuel Grumbach0211ddd2008-04-14 21:16:07 -0700723
724 key_flags |= (STA_KEY_FLG_WEP | STA_KEY_FLG_MAP_KEY_MSK);
725 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
726 key_flags &= ~STA_KEY_FLG_INVALID;
727
728 if (keyconf->keylen == WEP_KEY_LEN_128)
729 key_flags |= STA_KEY_FLG_KEY_SIZE_MSK;
730
Tomas Winkler5425e492008-04-15 16:01:38 -0700731 if (sta_id == priv->hw_params.bcast_sta_id)
Emmanuel Grumbach0211ddd2008-04-14 21:16:07 -0700732 key_flags |= STA_KEY_MULTICAST_MSK;
733
734 spin_lock_irqsave(&priv->sta_lock, flags);
735
736 priv->stations[sta_id].keyinfo.alg = keyconf->alg;
737 priv->stations[sta_id].keyinfo.keylen = keyconf->keylen;
738 priv->stations[sta_id].keyinfo.keyidx = keyconf->keyidx;
739
740 memcpy(priv->stations[sta_id].keyinfo.key,
741 keyconf->key, keyconf->keylen);
742
743 memcpy(&priv->stations[sta_id].sta.key.key[3],
744 keyconf->key, keyconf->keylen);
745
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -0700746 if ((priv->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
747 == STA_KEY_FLG_NO_ENC)
748 priv->stations[sta_id].sta.key.key_offset =
Emmanuel Grumbach80fb47a2008-04-14 21:16:08 -0700749 iwl_get_free_ucode_key_index(priv);
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -0700750 /* else, we are overriding an existing key => no need to allocated room
751 * in uCode. */
Emmanuel Grumbach0211ddd2008-04-14 21:16:07 -0700752
Tomas Winkler40a9a822008-11-25 23:29:03 +0200753 WARN(priv->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
Jiri Slabye724b8f2009-01-05 17:06:06 +0100754 "no space for a new key");
Tomas Winkler40a9a822008-11-25 23:29:03 +0200755
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -0700756 priv->stations[sta_id].sta.key.key_flags = key_flags;
Emmanuel Grumbach0211ddd2008-04-14 21:16:07 -0700757 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
758 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
759
Tomas Winkler133636d2008-05-05 10:22:34 +0800760 ret = iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
Emmanuel Grumbach0211ddd2008-04-14 21:16:07 -0700761
762 spin_unlock_irqrestore(&priv->sta_lock, flags);
763
764 return ret;
765}
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700766
767static int iwl_set_ccmp_dynamic_key_info(struct iwl_priv *priv,
768 struct ieee80211_key_conf *keyconf,
769 u8 sta_id)
770{
771 unsigned long flags;
772 __le16 key_flags = 0;
Tomas Winkler40a9a822008-11-25 23:29:03 +0200773 int ret;
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700774
775 key_flags |= (STA_KEY_FLG_CCMP | STA_KEY_FLG_MAP_KEY_MSK);
776 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
777 key_flags &= ~STA_KEY_FLG_INVALID;
778
Tomas Winkler5425e492008-04-15 16:01:38 -0700779 if (sta_id == priv->hw_params.bcast_sta_id)
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700780 key_flags |= STA_KEY_MULTICAST_MSK;
781
782 keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700783
784 spin_lock_irqsave(&priv->sta_lock, flags);
785 priv->stations[sta_id].keyinfo.alg = keyconf->alg;
786 priv->stations[sta_id].keyinfo.keylen = keyconf->keylen;
787
788 memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key,
789 keyconf->keylen);
790
791 memcpy(priv->stations[sta_id].sta.key.key, keyconf->key,
792 keyconf->keylen);
793
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -0700794 if ((priv->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
795 == STA_KEY_FLG_NO_ENC)
796 priv->stations[sta_id].sta.key.key_offset =
797 iwl_get_free_ucode_key_index(priv);
798 /* else, we are overriding an existing key => no need to allocated room
799 * in uCode. */
800
Tomas Winkler40a9a822008-11-25 23:29:03 +0200801 WARN(priv->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
Jiri Slabye724b8f2009-01-05 17:06:06 +0100802 "no space for a new key");
Tomas Winkler40a9a822008-11-25 23:29:03 +0200803
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700804 priv->stations[sta_id].sta.key.key_flags = key_flags;
805 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
806 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
807
Tomas Winkler40a9a822008-11-25 23:29:03 +0200808 ret = iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
809
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700810 spin_unlock_irqrestore(&priv->sta_lock, flags);
811
Tomas Winkler40a9a822008-11-25 23:29:03 +0200812 return ret;
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700813}
814
815static int iwl_set_tkip_dynamic_key_info(struct iwl_priv *priv,
816 struct ieee80211_key_conf *keyconf,
817 u8 sta_id)
818{
819 unsigned long flags;
820 int ret = 0;
Reinette Chatre299f5462009-04-30 13:56:31 -0700821 __le16 key_flags = 0;
822
823 key_flags |= (STA_KEY_FLG_TKIP | STA_KEY_FLG_MAP_KEY_MSK);
824 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
825 key_flags &= ~STA_KEY_FLG_INVALID;
826
827 if (sta_id == priv->hw_params.bcast_sta_id)
828 key_flags |= STA_KEY_MULTICAST_MSK;
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700829
830 keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
831 keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700832
833 spin_lock_irqsave(&priv->sta_lock, flags);
834
835 priv->stations[sta_id].keyinfo.alg = keyconf->alg;
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700836 priv->stations[sta_id].keyinfo.keylen = 16;
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -0700837
838 if ((priv->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
839 == STA_KEY_FLG_NO_ENC)
840 priv->stations[sta_id].sta.key.key_offset =
Emmanuel Grumbach77bab602008-04-15 16:01:44 -0700841 iwl_get_free_ucode_key_index(priv);
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -0700842 /* else, we are overriding an existing key => no need to allocated room
843 * in uCode. */
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700844
Tomas Winkler40a9a822008-11-25 23:29:03 +0200845 WARN(priv->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
Jiri Slabye724b8f2009-01-05 17:06:06 +0100846 "no space for a new key");
Tomas Winkler40a9a822008-11-25 23:29:03 +0200847
Reinette Chatre299f5462009-04-30 13:56:31 -0700848 priv->stations[sta_id].sta.key.key_flags = key_flags;
849
850
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700851 /* This copy is acutally not needed: we get the key with each TX */
852 memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key, 16);
853
854 memcpy(priv->stations[sta_id].sta.key.key, keyconf->key, 16);
855
856 spin_unlock_irqrestore(&priv->sta_lock, flags);
857
858 return ret;
859}
860
Tomas Winkler9f586712008-11-12 13:14:05 -0800861void iwl_update_tkip_key(struct iwl_priv *priv,
862 struct ieee80211_key_conf *keyconf,
863 const u8 *addr, u32 iv32, u16 *phase1key)
864{
865 u8 sta_id = IWL_INVALID_STATION;
866 unsigned long flags;
Tomas Winkler9f586712008-11-12 13:14:05 -0800867 int i;
Tomas Winkler9f586712008-11-12 13:14:05 -0800868
Tomas Winklerc587de02009-06-03 11:44:07 -0700869 sta_id = iwl_find_station(priv, addr);
Tomas Winkler9f586712008-11-12 13:14:05 -0800870 if (sta_id == IWL_INVALID_STATION) {
Tomas Winklere1623442009-01-27 14:27:56 -0800871 IWL_DEBUG_MAC80211(priv, "leave - %pM not in station map.\n",
Tomas Winkler9f586712008-11-12 13:14:05 -0800872 addr);
873 return;
874 }
875
876 if (iwl_scan_cancel(priv)) {
877 /* cancel scan failed, just live w/ bad key and rely
878 briefly on SW decryption */
879 return;
880 }
881
Tomas Winkler9f586712008-11-12 13:14:05 -0800882 spin_lock_irqsave(&priv->sta_lock, flags);
883
Tomas Winkler9f586712008-11-12 13:14:05 -0800884 priv->stations[sta_id].sta.key.tkip_rx_tsc_byte2 = (u8) iv32;
885
886 for (i = 0; i < 5; i++)
887 priv->stations[sta_id].sta.key.tkip_rx_ttak[i] =
888 cpu_to_le16(phase1key[i]);
889
890 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
891 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
892
893 iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
894
895 spin_unlock_irqrestore(&priv->sta_lock, flags);
896
897}
898EXPORT_SYMBOL(iwl_update_tkip_key);
899
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -0700900int iwl_remove_dynamic_key(struct iwl_priv *priv,
901 struct ieee80211_key_conf *keyconf,
902 u8 sta_id)
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700903{
904 unsigned long flags;
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -0700905 int ret = 0;
906 u16 key_flags;
907 u8 keyidx;
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700908
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +0800909 priv->key_mapping_key--;
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700910
911 spin_lock_irqsave(&priv->sta_lock, flags);
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -0700912 key_flags = le16_to_cpu(priv->stations[sta_id].sta.key.key_flags);
913 keyidx = (key_flags >> STA_KEY_FLG_KEYID_POS) & 0x3;
914
Tomas Winklere1623442009-01-27 14:27:56 -0800915 IWL_DEBUG_WEP(priv, "Remove dynamic key: idx=%d sta=%d\n",
Emmanuel Grumbach4564ce82008-06-12 09:47:09 +0800916 keyconf->keyidx, sta_id);
917
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -0700918 if (keyconf->keyidx != keyidx) {
919 /* We need to remove a key with index different that the one
920 * in the uCode. This means that the key we need to remove has
921 * been replaced by another one with different index.
922 * Don't do anything and return ok
923 */
924 spin_unlock_irqrestore(&priv->sta_lock, flags);
925 return 0;
926 }
927
Tomas Winkler40a9a822008-11-25 23:29:03 +0200928 if (priv->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800929 IWL_WARN(priv, "Removing wrong key %d 0x%x\n",
Tomas Winkler40a9a822008-11-25 23:29:03 +0200930 keyconf->keyidx, key_flags);
931 spin_unlock_irqrestore(&priv->sta_lock, flags);
932 return 0;
933 }
934
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700935 if (!test_and_clear_bit(priv->stations[sta_id].sta.key.key_offset,
936 &priv->ucode_key_table))
Winkler, Tomas15b16872008-12-19 10:37:33 +0800937 IWL_ERR(priv, "index %d not used in uCode key table.\n",
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700938 priv->stations[sta_id].sta.key.key_offset);
939 memset(&priv->stations[sta_id].keyinfo, 0,
Tomas Winkler6def9762008-05-05 10:22:31 +0800940 sizeof(struct iwl_hw_key));
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700941 memset(&priv->stations[sta_id].sta.key, 0,
942 sizeof(struct iwl4965_keyinfo));
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -0700943 priv->stations[sta_id].sta.key.key_flags =
944 STA_KEY_FLG_NO_ENC | STA_KEY_FLG_INVALID;
945 priv->stations[sta_id].sta.key.key_offset = WEP_INVALID_OFFSET;
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700946 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
947 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700948
Reinette Chatre2d1bb9e2009-07-17 09:30:18 -0700949 if (iwl_is_rfkill(priv)) {
950 IWL_DEBUG_WEP(priv, "Not sending REPLY_ADD_STA command because RFKILL enabled. \n");
951 spin_unlock_irqrestore(&priv->sta_lock, flags);
952 return 0;
953 }
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +0800954 ret = iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -0700955 spin_unlock_irqrestore(&priv->sta_lock, flags);
956 return ret;
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700957}
Tomas Winkler27aaba02008-05-05 10:22:44 +0800958EXPORT_SYMBOL(iwl_remove_dynamic_key);
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700959
960int iwl_set_dynamic_key(struct iwl_priv *priv,
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +0800961 struct ieee80211_key_conf *keyconf, u8 sta_id)
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700962{
963 int ret;
964
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +0800965 priv->key_mapping_key++;
966 keyconf->hw_key_idx = HW_KEY_DYNAMIC;
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700967
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +0800968 switch (keyconf->alg) {
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700969 case ALG_CCMP:
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +0800970 ret = iwl_set_ccmp_dynamic_key_info(priv, keyconf, sta_id);
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700971 break;
972 case ALG_TKIP:
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +0800973 ret = iwl_set_tkip_dynamic_key_info(priv, keyconf, sta_id);
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700974 break;
975 case ALG_WEP:
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +0800976 ret = iwl_set_wep_dynamic_key_info(priv, keyconf, sta_id);
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700977 break;
978 default:
Winkler, Tomas15b16872008-12-19 10:37:33 +0800979 IWL_ERR(priv,
980 "Unknown alg: %s alg = %d\n", __func__, keyconf->alg);
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700981 ret = -EINVAL;
982 }
983
Tomas Winklere1623442009-01-27 14:27:56 -0800984 IWL_DEBUG_WEP(priv, "Set dynamic key: alg= %d len=%d idx=%d sta=%d ret=%d\n",
Emmanuel Grumbach4564ce82008-06-12 09:47:09 +0800985 keyconf->alg, keyconf->keylen, keyconf->keyidx,
986 sta_id, ret);
987
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700988 return ret;
989}
Tomas Winkler27aaba02008-05-05 10:22:44 +0800990EXPORT_SYMBOL(iwl_set_dynamic_key);
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700991
Tomas Winkler66c73db2008-04-15 16:01:40 -0700992#ifdef CONFIG_IWLWIFI_DEBUG
993static void iwl_dump_lq_cmd(struct iwl_priv *priv,
994 struct iwl_link_quality_cmd *lq)
995{
996 int i;
Tomas Winklere1623442009-01-27 14:27:56 -0800997 IWL_DEBUG_RATE(priv, "lq station id 0x%x\n", lq->sta_id);
998 IWL_DEBUG_RATE(priv, "lq ant 0x%X 0x%X\n",
Tomas Winkler66c73db2008-04-15 16:01:40 -0700999 lq->general_params.single_stream_ant_msk,
1000 lq->general_params.dual_stream_ant_msk);
1001
1002 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
Tomas Winklere1623442009-01-27 14:27:56 -08001003 IWL_DEBUG_RATE(priv, "lq index %d 0x%X\n",
Tomas Winkler66c73db2008-04-15 16:01:40 -07001004 i, lq->rs_table[i].rate_n_flags);
1005}
1006#else
1007static inline void iwl_dump_lq_cmd(struct iwl_priv *priv,
1008 struct iwl_link_quality_cmd *lq)
1009{
1010}
1011#endif
1012
1013int iwl_send_lq_cmd(struct iwl_priv *priv,
1014 struct iwl_link_quality_cmd *lq, u8 flags)
1015{
1016 struct iwl_host_cmd cmd = {
1017 .id = REPLY_TX_LINK_QUALITY_CMD,
1018 .len = sizeof(struct iwl_link_quality_cmd),
Johannes Bergc2acea82009-07-24 11:13:05 -07001019 .flags = flags,
Tomas Winkler66c73db2008-04-15 16:01:40 -07001020 .data = lq,
1021 };
1022
1023 if ((lq->sta_id == 0xFF) &&
Johannes Berg05c914f2008-09-11 00:01:58 +02001024 (priv->iw_mode == NL80211_IFTYPE_ADHOC))
Tomas Winkler66c73db2008-04-15 16:01:40 -07001025 return -EINVAL;
1026
1027 if (lq->sta_id == 0xFF)
1028 lq->sta_id = IWL_AP_ID;
1029
Tomas Winkler3ac7f142008-07-21 02:40:14 +03001030 iwl_dump_lq_cmd(priv, lq);
Tomas Winkler66c73db2008-04-15 16:01:40 -07001031
Ron Rindjunsky36e1f162008-06-30 17:23:13 +08001032 if (iwl_is_associated(priv) && priv->assoc_station_added)
Tomas Winkler66c73db2008-04-15 16:01:40 -07001033 return iwl_send_cmd(priv, &cmd);
1034
1035 return 0;
1036}
1037EXPORT_SYMBOL(iwl_send_lq_cmd);
1038
Tomas Winkler4f40e4d2008-05-15 13:54:04 +08001039/**
1040 * iwl_sta_init_lq - Initialize a station's hardware rate table
1041 *
1042 * The uCode's station table contains a table of fallback rates
1043 * for automatic fallback during transmission.
1044 *
1045 * NOTE: This sets up a default set of values. These will be replaced later
Tomas Winklere227cea2008-07-18 13:53:05 +08001046 * if the driver's iwl-agn-rs rate scaling algorithm is used, instead of
Tomas Winkler4f40e4d2008-05-15 13:54:04 +08001047 * rc80211_simple.
1048 *
1049 * NOTE: Run REPLY_ADD_STA command to set up station table entry, before
1050 * calling this function (which runs REPLY_TX_LINK_QUALITY_CMD,
1051 * which requires station table entry to exist).
1052 */
Tomas Winklerc587de02009-06-03 11:44:07 -07001053static void iwl_sta_init_lq(struct iwl_priv *priv, const u8 *addr, bool is_ap)
Tomas Winkler4f40e4d2008-05-15 13:54:04 +08001054{
1055 int i, r;
1056 struct iwl_link_quality_cmd link_cmd = {
1057 .reserved1 = 0,
1058 };
Tomas Winkler5d664a42008-10-08 09:37:29 +08001059 u32 rate_flags;
Tomas Winkler4f40e4d2008-05-15 13:54:04 +08001060
1061 /* Set up the rate scaling to start at selected rate, fall back
1062 * all the way down to 1M in IEEE order, and then spin on 1M */
1063 if (is_ap)
1064 r = IWL_RATE_54M_INDEX;
1065 else if (priv->band == IEEE80211_BAND_5GHZ)
1066 r = IWL_RATE_6M_INDEX;
1067 else
1068 r = IWL_RATE_1M_INDEX;
1069
1070 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
1071 rate_flags = 0;
1072 if (r >= IWL_FIRST_CCK_RATE && r <= IWL_LAST_CCK_RATE)
1073 rate_flags |= RATE_MCS_CCK_MSK;
1074
Tomas Winkler5d664a42008-10-08 09:37:29 +08001075 rate_flags |= first_antenna(priv->hw_params.valid_tx_ant) <<
1076 RATE_MCS_ANT_POS;
Tomas Winkler4f40e4d2008-05-15 13:54:04 +08001077
1078 link_cmd.rs_table[i].rate_n_flags =
Tomas Winklere7d326a2008-06-12 09:47:11 +08001079 iwl_hw_set_rate_n_flags(iwl_rates[r].plcp, rate_flags);
Winkler, Tomaseb4779c2008-10-29 14:05:44 -07001080 r = iwl_get_prev_ieee_rate(r);
Tomas Winkler4f40e4d2008-05-15 13:54:04 +08001081 }
1082
Tomas Winkler5d664a42008-10-08 09:37:29 +08001083 link_cmd.general_params.single_stream_ant_msk =
1084 first_antenna(priv->hw_params.valid_tx_ant);
Tomas Winkler4f40e4d2008-05-15 13:54:04 +08001085 link_cmd.general_params.dual_stream_ant_msk = 3;
Wey-Yi Guy13c33a02009-06-03 11:44:11 -07001086 link_cmd.agg_params.agg_dis_start_th = LINK_QUAL_AGG_DISABLE_START_DEF;
1087 link_cmd.agg_params.agg_time_limit =
1088 cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
Tomas Winkler4f40e4d2008-05-15 13:54:04 +08001089
1090 /* Update the rate scaling for control frame Tx to AP */
1091 link_cmd.sta_id = is_ap ? IWL_AP_ID : priv->hw_params.bcast_sta_id;
1092
1093 iwl_send_cmd_pdu_async(priv, REPLY_TX_LINK_QUALITY_CMD,
1094 sizeof(link_cmd), &link_cmd, NULL);
1095}
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +08001096
Tomas Winkler4f40e4d2008-05-15 13:54:04 +08001097/**
1098 * iwl_rxon_add_station - add station into station table.
1099 *
1100 * there is only one AP station with id= IWL_AP_ID
Tomas Winklera96a27f2008-10-23 23:48:56 -07001101 * NOTE: mutex must be held before calling this function
Tomas Winkler4f40e4d2008-05-15 13:54:04 +08001102 */
Tomas Winklerc587de02009-06-03 11:44:07 -07001103int iwl_rxon_add_station(struct iwl_priv *priv, const u8 *addr, bool is_ap)
Tomas Winkler4f40e4d2008-05-15 13:54:04 +08001104{
Johannes Bergae5eb022008-10-14 16:58:37 +02001105 struct ieee80211_sta *sta;
1106 struct ieee80211_sta_ht_cap ht_config;
1107 struct ieee80211_sta_ht_cap *cur_ht_config = NULL;
Tomas Winkler4f40e4d2008-05-15 13:54:04 +08001108 u8 sta_id;
1109
Johannes Bergae5eb022008-10-14 16:58:37 +02001110 /*
Daniel Halperinff27fab2010-01-22 14:22:58 -08001111 * Set HT capabilities. It is ok to set this struct even if not using
1112 * HT config: the priv->current_ht_config.is_ht flag will just be false
Johannes Bergae5eb022008-10-14 16:58:37 +02001113 */
Daniel Halperinff27fab2010-01-22 14:22:58 -08001114 rcu_read_lock();
1115 sta = ieee80211_find_sta(priv->vif, addr);
1116 if (sta) {
1117 memcpy(&ht_config, &sta->ht_cap, sizeof(ht_config));
1118 cur_ht_config = &ht_config;
Johannes Bergae5eb022008-10-14 16:58:37 +02001119 }
Daniel Halperinff27fab2010-01-22 14:22:58 -08001120 rcu_read_unlock();
Johannes Bergae5eb022008-10-14 16:58:37 +02001121
Daniel Halperinff27fab2010-01-22 14:22:58 -08001122 /* Add station to device's station table */
Tomas Winklerc587de02009-06-03 11:44:07 -07001123 sta_id = iwl_add_station(priv, addr, is_ap, CMD_SYNC, cur_ht_config);
Tomas Winkler4f40e4d2008-05-15 13:54:04 +08001124
1125 /* Set up default rate scaling table in device's station table */
1126 iwl_sta_init_lq(priv, addr, is_ap);
1127
1128 return sta_id;
1129}
1130EXPORT_SYMBOL(iwl_rxon_add_station);
1131
Tomas Winkler4f40e4d2008-05-15 13:54:04 +08001132/**
Reinette Chatre9a9ca652009-10-30 14:36:12 -07001133 * iwl_sta_init_bcast_lq - Initialize a bcast station's hardware rate table
1134 *
1135 * NOTE: Run REPLY_ADD_STA command to set up station table entry, before
1136 * calling this function (which runs REPLY_TX_LINK_QUALITY_CMD,
1137 * which requires station table entry to exist).
1138 */
1139static void iwl_sta_init_bcast_lq(struct iwl_priv *priv)
1140{
1141 int i, r;
1142 struct iwl_link_quality_cmd link_cmd = {
1143 .reserved1 = 0,
1144 };
1145 u32 rate_flags;
1146
1147 /* Set up the rate scaling to start at selected rate, fall back
1148 * all the way down to 1M in IEEE order, and then spin on 1M */
1149 if (priv->band == IEEE80211_BAND_5GHZ)
1150 r = IWL_RATE_6M_INDEX;
1151 else
1152 r = IWL_RATE_1M_INDEX;
1153
1154 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
1155 rate_flags = 0;
1156 if (r >= IWL_FIRST_CCK_RATE && r <= IWL_LAST_CCK_RATE)
1157 rate_flags |= RATE_MCS_CCK_MSK;
1158
1159 rate_flags |= first_antenna(priv->hw_params.valid_tx_ant) <<
1160 RATE_MCS_ANT_POS;
1161
1162 link_cmd.rs_table[i].rate_n_flags =
1163 iwl_hw_set_rate_n_flags(iwl_rates[r].plcp, rate_flags);
1164 r = iwl_get_prev_ieee_rate(r);
1165 }
1166
1167 link_cmd.general_params.single_stream_ant_msk =
1168 first_antenna(priv->hw_params.valid_tx_ant);
1169 link_cmd.general_params.dual_stream_ant_msk = 3;
1170 link_cmd.agg_params.agg_dis_start_th = LINK_QUAL_AGG_DISABLE_START_DEF;
1171 link_cmd.agg_params.agg_time_limit =
1172 cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
1173
1174 /* Update the rate scaling for control frame Tx to AP */
1175 link_cmd.sta_id = priv->hw_params.bcast_sta_id;
1176
1177 iwl_send_cmd_pdu_async(priv, REPLY_TX_LINK_QUALITY_CMD,
1178 sizeof(link_cmd), &link_cmd, NULL);
1179}
1180
1181
1182/**
1183 * iwl_add_bcast_station - add broadcast station into station table.
1184 */
1185void iwl_add_bcast_station(struct iwl_priv *priv)
1186{
Reinette Chatre3459ab52010-01-22 14:22:49 -08001187 IWL_DEBUG_INFO(priv, "Adding broadcast station to station table\n");
Reinette Chatre9a9ca652009-10-30 14:36:12 -07001188 iwl_add_station(priv, iwl_bcast_addr, false, CMD_SYNC, NULL);
1189
1190 /* Set up default rate scaling table in device's station table */
1191 iwl_sta_init_bcast_lq(priv);
1192}
1193EXPORT_SYMBOL(iwl_add_bcast_station);
1194
1195/**
Reinette Chatre3459ab52010-01-22 14:22:49 -08001196 * iwl3945_add_bcast_station - add broadcast station into station table.
1197 */
1198void iwl3945_add_bcast_station(struct iwl_priv *priv)
1199{
1200 IWL_DEBUG_INFO(priv, "Adding broadcast station to station table\n");
1201 iwl_add_station(priv, iwl_bcast_addr, false, CMD_SYNC, NULL);
1202}
1203EXPORT_SYMBOL(iwl3945_add_bcast_station);
1204
1205/**
Tomas Winkler4f40e4d2008-05-15 13:54:04 +08001206 * iwl_get_sta_id - Find station's index within station table
1207 *
1208 * If new IBSS station, create new entry in station table
1209 */
1210int iwl_get_sta_id(struct iwl_priv *priv, struct ieee80211_hdr *hdr)
1211{
1212 int sta_id;
Luis R. Rodriguez943ab702009-07-14 20:14:07 -04001213 __le16 fc = hdr->frame_control;
Tomas Winkler4f40e4d2008-05-15 13:54:04 +08001214
1215 /* If this frame is broadcast or management, use broadcast station id */
Luis R. Rodriguez943ab702009-07-14 20:14:07 -04001216 if (!ieee80211_is_data(fc) || is_multicast_ether_addr(hdr->addr1))
Tomas Winkler4f40e4d2008-05-15 13:54:04 +08001217 return priv->hw_params.bcast_sta_id;
1218
1219 switch (priv->iw_mode) {
1220
1221 /* If we are a client station in a BSS network, use the special
1222 * AP station entry (that's the only station we communicate with) */
Johannes Berg05c914f2008-09-11 00:01:58 +02001223 case NL80211_IFTYPE_STATION:
Tomas Winkler4f40e4d2008-05-15 13:54:04 +08001224 return IWL_AP_ID;
1225
1226 /* If we are an AP, then find the station, or use BCAST */
Johannes Berg05c914f2008-09-11 00:01:58 +02001227 case NL80211_IFTYPE_AP:
Tomas Winklerc587de02009-06-03 11:44:07 -07001228 sta_id = iwl_find_station(priv, hdr->addr1);
Tomas Winkler4f40e4d2008-05-15 13:54:04 +08001229 if (sta_id != IWL_INVALID_STATION)
1230 return sta_id;
1231 return priv->hw_params.bcast_sta_id;
1232
1233 /* If this frame is going out to an IBSS network, find the station,
1234 * or create a new station table entry */
Johannes Berg05c914f2008-09-11 00:01:58 +02001235 case NL80211_IFTYPE_ADHOC:
Tomas Winklerc587de02009-06-03 11:44:07 -07001236 sta_id = iwl_find_station(priv, hdr->addr1);
Tomas Winkler4f40e4d2008-05-15 13:54:04 +08001237 if (sta_id != IWL_INVALID_STATION)
1238 return sta_id;
1239
1240 /* Create new station table entry */
Tomas Winklerc587de02009-06-03 11:44:07 -07001241 sta_id = iwl_add_station(priv, hdr->addr1, false,
1242 CMD_ASYNC, NULL);
Tomas Winkler4f40e4d2008-05-15 13:54:04 +08001243
1244 if (sta_id != IWL_INVALID_STATION)
1245 return sta_id;
1246
Tomas Winklere1623442009-01-27 14:27:56 -08001247 IWL_DEBUG_DROP(priv, "Station %pM not in station map. "
Tomas Winkler4f40e4d2008-05-15 13:54:04 +08001248 "Defaulting to broadcast...\n",
Johannes Berge1749612008-10-27 15:59:26 -07001249 hdr->addr1);
Reinette Chatre3d816c72009-08-07 15:41:37 -07001250 iwl_print_hex_dump(priv, IWL_DL_DROP, (u8 *) hdr, sizeof(*hdr));
Tomas Winkler4f40e4d2008-05-15 13:54:04 +08001251 return priv->hw_params.bcast_sta_id;
1252
1253 default:
Winkler, Tomas39aadf82008-12-19 10:37:32 +08001254 IWL_WARN(priv, "Unknown mode of operation: %d\n",
1255 priv->iw_mode);
Tomas Winkler4f40e4d2008-05-15 13:54:04 +08001256 return priv->hw_params.bcast_sta_id;
1257 }
1258}
1259EXPORT_SYMBOL(iwl_get_sta_id);
1260
Tomas Winkler5083e562008-05-29 16:35:15 +08001261/**
Tomas Winkler9f586712008-11-12 13:14:05 -08001262 * iwl_sta_tx_modify_enable_tid - Enable Tx for this TID in station table
Tomas Winkler5083e562008-05-29 16:35:15 +08001263 */
Tomas Winkler9f586712008-11-12 13:14:05 -08001264void iwl_sta_tx_modify_enable_tid(struct iwl_priv *priv, int sta_id, int tid)
Tomas Winkler5083e562008-05-29 16:35:15 +08001265{
1266 unsigned long flags;
1267
1268 /* Remove "disable" flag, to enable Tx for this TID */
1269 spin_lock_irqsave(&priv->sta_lock, flags);
1270 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_TID_DISABLE_TX;
1271 priv->stations[sta_id].sta.tid_disable_tx &= cpu_to_le16(~(1 << tid));
1272 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1273 spin_unlock_irqrestore(&priv->sta_lock, flags);
1274
1275 iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
1276}
Tomas Winkler9f586712008-11-12 13:14:05 -08001277EXPORT_SYMBOL(iwl_sta_tx_modify_enable_tid);
1278
1279int iwl_sta_rx_agg_start(struct iwl_priv *priv,
1280 const u8 *addr, int tid, u16 ssn)
1281{
1282 unsigned long flags;
1283 int sta_id;
1284
Tomas Winklerc587de02009-06-03 11:44:07 -07001285 sta_id = iwl_find_station(priv, addr);
Tomas Winkler9f586712008-11-12 13:14:05 -08001286 if (sta_id == IWL_INVALID_STATION)
1287 return -ENXIO;
1288
1289 spin_lock_irqsave(&priv->sta_lock, flags);
1290 priv->stations[sta_id].sta.station_flags_msk = 0;
1291 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_ADDBA_TID_MSK;
1292 priv->stations[sta_id].sta.add_immediate_ba_tid = (u8)tid;
1293 priv->stations[sta_id].sta.add_immediate_ba_ssn = cpu_to_le16(ssn);
1294 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1295 spin_unlock_irqrestore(&priv->sta_lock, flags);
1296
1297 return iwl_send_add_sta(priv, &priv->stations[sta_id].sta,
1298 CMD_ASYNC);
1299}
1300EXPORT_SYMBOL(iwl_sta_rx_agg_start);
1301
1302int iwl_sta_rx_agg_stop(struct iwl_priv *priv, const u8 *addr, int tid)
1303{
1304 unsigned long flags;
1305 int sta_id;
1306
Tomas Winklerc587de02009-06-03 11:44:07 -07001307 sta_id = iwl_find_station(priv, addr);
Wey-Yi Guya2f1cbe2009-03-17 21:51:52 -07001308 if (sta_id == IWL_INVALID_STATION) {
1309 IWL_ERR(priv, "Invalid station for AGG tid %d\n", tid);
Tomas Winkler9f586712008-11-12 13:14:05 -08001310 return -ENXIO;
Wey-Yi Guya2f1cbe2009-03-17 21:51:52 -07001311 }
Tomas Winkler9f586712008-11-12 13:14:05 -08001312
1313 spin_lock_irqsave(&priv->sta_lock, flags);
1314 priv->stations[sta_id].sta.station_flags_msk = 0;
1315 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_DELBA_TID_MSK;
1316 priv->stations[sta_id].sta.remove_immediate_ba_tid = (u8)tid;
1317 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1318 spin_unlock_irqrestore(&priv->sta_lock, flags);
1319
1320 return iwl_send_add_sta(priv, &priv->stations[sta_id].sta,
1321 CMD_ASYNC);
1322}
1323EXPORT_SYMBOL(iwl_sta_rx_agg_stop);
1324
Johannes Berg6ab10ff2009-11-13 11:56:37 -08001325void iwl_sta_modify_ps_wake(struct iwl_priv *priv, int sta_id)
Tomas Winkler9f586712008-11-12 13:14:05 -08001326{
1327 unsigned long flags;
1328
1329 spin_lock_irqsave(&priv->sta_lock, flags);
1330 priv->stations[sta_id].sta.station_flags &= ~STA_FLG_PWR_SAVE_MSK;
1331 priv->stations[sta_id].sta.station_flags_msk = STA_FLG_PWR_SAVE_MSK;
1332 priv->stations[sta_id].sta.sta.modify_mask = 0;
Johannes Berg6ab10ff2009-11-13 11:56:37 -08001333 priv->stations[sta_id].sta.sleep_tx_count = 0;
Tomas Winkler9f586712008-11-12 13:14:05 -08001334 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1335 spin_unlock_irqrestore(&priv->sta_lock, flags);
1336
1337 iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
1338}
Johannes Berg6ab10ff2009-11-13 11:56:37 -08001339EXPORT_SYMBOL(iwl_sta_modify_ps_wake);
Tomas Winkler9f586712008-11-12 13:14:05 -08001340
Johannes Berg6ab10ff2009-11-13 11:56:37 -08001341void iwl_sta_modify_sleep_tx_count(struct iwl_priv *priv, int sta_id, int cnt)
Tomas Winkler9f586712008-11-12 13:14:05 -08001342{
Johannes Berg6ab10ff2009-11-13 11:56:37 -08001343 unsigned long flags;
Tomas Winkler9f586712008-11-12 13:14:05 -08001344
Johannes Berg6ab10ff2009-11-13 11:56:37 -08001345 spin_lock_irqsave(&priv->sta_lock, flags);
1346 priv->stations[sta_id].sta.station_flags |= STA_FLG_PWR_SAVE_MSK;
1347 priv->stations[sta_id].sta.station_flags_msk = STA_FLG_PWR_SAVE_MSK;
1348 priv->stations[sta_id].sta.sta.modify_mask =
1349 STA_MODIFY_SLEEP_TX_COUNT_MSK;
1350 priv->stations[sta_id].sta.sleep_tx_count = cpu_to_le16(cnt);
1351 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1352 spin_unlock_irqrestore(&priv->sta_lock, flags);
Tomas Winkler9f586712008-11-12 13:14:05 -08001353
Johannes Berg6ab10ff2009-11-13 11:56:37 -08001354 iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
Tomas Winkler9f586712008-11-12 13:14:05 -08001355}