blob: 41e9013c742785ef9c7b48d8a866fb99df096d29 [file] [log] [blame]
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07001/******************************************************************************
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:
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
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +080037#define IWL_STA_DRIVER_ACTIVE BIT(0) /* driver entry is active */
38#define IWL_STA_UCODE_ACTIVE BIT(1) /* ucode entry is active */
Tomas Winkler7a999bf2008-05-29 16:35:02 +080039
Tomas Winkler947b13a2008-04-16 16:34:48 -070040u8 iwl_find_station(struct iwl_priv *priv, const u8 *addr)
41{
42 int i;
43 int start = 0;
44 int ret = IWL_INVALID_STATION;
45 unsigned long flags;
Tomas Winkler947b13a2008-04-16 16:34:48 -070046
Johannes Berg05c914f2008-09-11 00:01:58 +020047 if ((priv->iw_mode == NL80211_IFTYPE_ADHOC) ||
48 (priv->iw_mode == NL80211_IFTYPE_AP))
Tomas Winkler947b13a2008-04-16 16:34:48 -070049 start = IWL_STA_ID;
50
51 if (is_broadcast_ether_addr(addr))
52 return priv->hw_params.bcast_sta_id;
53
54 spin_lock_irqsave(&priv->sta_lock, flags);
55 for (i = start; i < priv->hw_params.max_stations; i++)
56 if (priv->stations[i].used &&
57 (!compare_ether_addr(priv->stations[i].sta.sta.addr,
58 addr))) {
59 ret = i;
60 goto out;
61 }
62
Johannes Berge1749612008-10-27 15:59:26 -070063 IWL_DEBUG_ASSOC_LIMIT("can not find STA %pM total %d\n",
64 addr, priv->num_stations);
Tomas Winkler947b13a2008-04-16 16:34:48 -070065
66 out:
67 spin_unlock_irqrestore(&priv->sta_lock, flags);
68 return ret;
69}
70EXPORT_SYMBOL(iwl_find_station);
71
Emmanuel Grumbachbe1f3ab62008-06-12 09:47:18 +080072int iwl_get_ra_sta_id(struct iwl_priv *priv, struct ieee80211_hdr *hdr)
73{
Johannes Berg05c914f2008-09-11 00:01:58 +020074 if (priv->iw_mode == NL80211_IFTYPE_STATION) {
Emmanuel Grumbachbe1f3ab62008-06-12 09:47:18 +080075 return IWL_AP_ID;
76 } else {
77 u8 *da = ieee80211_get_DA(hdr);
78 return iwl_find_station(priv, da);
79 }
80}
81EXPORT_SYMBOL(iwl_get_ra_sta_id);
82
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +080083static void iwl_sta_ucode_activate(struct iwl_priv *priv, u8 sta_id)
84{
85 unsigned long flags;
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +080086
87 spin_lock_irqsave(&priv->sta_lock, flags);
88
89 if (!(priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE))
90 IWL_ERROR("ACTIVATE a non DRIVER active station %d\n", sta_id);
91
92 priv->stations[sta_id].used |= IWL_STA_UCODE_ACTIVE;
Johannes Berge1749612008-10-27 15:59:26 -070093 IWL_DEBUG_ASSOC("Added STA to Ucode: %pM\n",
94 priv->stations[sta_id].sta.sta.addr);
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +080095
96 spin_unlock_irqrestore(&priv->sta_lock, flags);
97}
98
Tomas Winkler42132bc2008-05-29 16:35:03 +080099static int iwl_add_sta_callback(struct iwl_priv *priv,
100 struct iwl_cmd *cmd, struct sk_buff *skb)
101{
102 struct iwl_rx_packet *res = NULL;
Tomas Winkler3257e5d2008-10-14 12:32:43 -0700103 struct iwl_addsta_cmd *addsta =
104 (struct iwl_addsta_cmd *)cmd->cmd.payload;
105 u8 sta_id = addsta->sta.sta_id;
Tomas Winkler42132bc2008-05-29 16:35:03 +0800106
107 if (!skb) {
108 IWL_ERROR("Error: Response NULL in REPLY_ADD_STA.\n");
109 return 1;
110 }
111
112 res = (struct iwl_rx_packet *)skb->data;
113 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
114 IWL_ERROR("Bad return from REPLY_ADD_STA (0x%08X)\n",
115 res->hdr.flags);
116 return 1;
117 }
118
119 switch (res->u.add_sta.status) {
120 case ADD_STA_SUCCESS_MSK:
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800121 iwl_sta_ucode_activate(priv, sta_id);
122 /* fall through */
Tomas Winkler42132bc2008-05-29 16:35:03 +0800123 default:
124 IWL_DEBUG_HC("Received REPLY_ADD_STA:(0x%08X)\n",
125 res->u.add_sta.status);
126 break;
127 }
128
129 /* We didn't cache the SKB; let the caller free it */
130 return 1;
131}
132
Tomas Winkler9f586712008-11-12 13:14:05 -0800133static int iwl_send_add_sta(struct iwl_priv *priv,
Tomas Winkler133636d2008-05-05 10:22:34 +0800134 struct iwl_addsta_cmd *sta, u8 flags)
135{
136 struct iwl_rx_packet *res = NULL;
137 int ret = 0;
138 u8 data[sizeof(*sta)];
139 struct iwl_host_cmd cmd = {
140 .id = REPLY_ADD_STA,
141 .meta.flags = flags,
142 .data = data,
143 };
144
Tomas Winkler42132bc2008-05-29 16:35:03 +0800145 if (flags & CMD_ASYNC)
146 cmd.meta.u.callback = iwl_add_sta_callback;
147 else
Tomas Winkler133636d2008-05-05 10:22:34 +0800148 cmd.meta.flags |= CMD_WANT_SKB;
149
150 cmd.len = priv->cfg->ops->utils->build_addsta_hcmd(sta, data);
151 ret = iwl_send_cmd(priv, &cmd);
152
153 if (ret || (flags & CMD_ASYNC))
154 return ret;
155
156 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
157 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
158 IWL_ERROR("Bad return from REPLY_ADD_STA (0x%08X)\n",
159 res->hdr.flags);
160 ret = -EIO;
161 }
162
163 if (ret == 0) {
164 switch (res->u.add_sta.status) {
165 case ADD_STA_SUCCESS_MSK:
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800166 iwl_sta_ucode_activate(priv, sta->sta.sta_id);
Tomas Winkler133636d2008-05-05 10:22:34 +0800167 IWL_DEBUG_INFO("REPLY_ADD_STA PASSED\n");
168 break;
169 default:
170 ret = -EIO;
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800171 IWL_WARN(priv, "REPLY_ADD_STA failed\n");
Tomas Winkler133636d2008-05-05 10:22:34 +0800172 break;
173 }
174 }
175
176 priv->alloc_rxb_skb--;
177 dev_kfree_skb_any(cmd.meta.u.skb);
178
179 return ret;
180}
Tomas Winkler947b13a2008-04-16 16:34:48 -0700181
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800182static void iwl_set_ht_add_station(struct iwl_priv *priv, u8 index,
Johannes Bergd9fe60d2008-10-09 12:13:49 +0200183 struct ieee80211_sta_ht_cap *sta_ht_inf)
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800184{
185 __le32 sta_flags;
186 u8 mimo_ps_mode;
187
188 if (!sta_ht_inf || !sta_ht_inf->ht_supported)
189 goto done;
190
Tomas Winkler00c5ae22008-09-03 11:26:42 +0800191 mimo_ps_mode = (sta_ht_inf->cap & IEEE80211_HT_CAP_SM_PS) >> 2;
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800192
193 sta_flags = priv->stations[index].sta.station_flags;
194
195 sta_flags &= ~(STA_FLG_RTS_MIMO_PROT_MSK | STA_FLG_MIMO_DIS_MSK);
196
197 switch (mimo_ps_mode) {
Tomas Winkler00c5ae22008-09-03 11:26:42 +0800198 case WLAN_HT_CAP_SM_PS_STATIC:
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800199 sta_flags |= STA_FLG_MIMO_DIS_MSK;
200 break;
Tomas Winkler00c5ae22008-09-03 11:26:42 +0800201 case WLAN_HT_CAP_SM_PS_DYNAMIC:
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800202 sta_flags |= STA_FLG_RTS_MIMO_PROT_MSK;
203 break;
Tomas Winkler00c5ae22008-09-03 11:26:42 +0800204 case WLAN_HT_CAP_SM_PS_DISABLED:
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800205 break;
206 default:
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800207 IWL_WARN(priv, "Invalid MIMO PS mode %d\n", mimo_ps_mode);
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800208 break;
209 }
210
211 sta_flags |= cpu_to_le32(
212 (u32)sta_ht_inf->ampdu_factor << STA_FLG_MAX_AGG_SIZE_POS);
213
214 sta_flags |= cpu_to_le32(
215 (u32)sta_ht_inf->ampdu_density << STA_FLG_AGG_MPDU_DENSITY_POS);
216
217 if (iwl_is_fat_tx_allowed(priv, sta_ht_inf))
218 sta_flags |= STA_FLG_FAT_EN_MSK;
219 else
220 sta_flags &= ~STA_FLG_FAT_EN_MSK;
221
222 priv->stations[index].sta.station_flags = sta_flags;
223 done:
224 return;
225}
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800226
227/**
228 * iwl_add_station_flags - Add station to tables in driver and device
229 */
230u8 iwl_add_station_flags(struct iwl_priv *priv, const u8 *addr, int is_ap,
Johannes Bergd9fe60d2008-10-09 12:13:49 +0200231 u8 flags, struct ieee80211_sta_ht_cap *ht_info)
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800232{
233 int i;
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800234 int sta_id = IWL_INVALID_STATION;
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800235 struct iwl_station_entry *station;
236 unsigned long flags_spin;
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800237
238 spin_lock_irqsave(&priv->sta_lock, flags_spin);
239 if (is_ap)
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800240 sta_id = IWL_AP_ID;
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800241 else if (is_broadcast_ether_addr(addr))
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800242 sta_id = priv->hw_params.bcast_sta_id;
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800243 else
244 for (i = IWL_STA_ID; i < priv->hw_params.max_stations; i++) {
245 if (!compare_ether_addr(priv->stations[i].sta.sta.addr,
246 addr)) {
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800247 sta_id = i;
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800248 break;
249 }
250
251 if (!priv->stations[i].used &&
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800252 sta_id == IWL_INVALID_STATION)
253 sta_id = i;
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800254 }
255
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800256 /* These two conditions have the same outcome, but keep them separate
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800257 since they have different meanings */
258 if (unlikely(sta_id == IWL_INVALID_STATION)) {
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800259 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800260 return sta_id;
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800261 }
262
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800263 if (priv->stations[sta_id].used &&
264 !compare_ether_addr(priv->stations[sta_id].sta.sta.addr, addr)) {
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800265 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800266 return sta_id;
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800267 }
268
269
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800270 station = &priv->stations[sta_id];
271 station->used = IWL_STA_DRIVER_ACTIVE;
Johannes Berge1749612008-10-27 15:59:26 -0700272 IWL_DEBUG_ASSOC("Add STA to driver ID %d: %pM\n",
273 sta_id, addr);
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800274 priv->num_stations++;
275
276 /* Set up the REPLY_ADD_STA command to send to device */
277 memset(&station->sta, 0, sizeof(struct iwl_addsta_cmd));
278 memcpy(station->sta.sta.addr, addr, ETH_ALEN);
279 station->sta.mode = 0;
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800280 station->sta.sta.sta_id = sta_id;
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800281 station->sta.station_flags = 0;
282
283 /* BCAST station and IBSS stations do not work in HT mode */
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800284 if (sta_id != priv->hw_params.bcast_sta_id &&
Johannes Berg05c914f2008-09-11 00:01:58 +0200285 priv->iw_mode != NL80211_IFTYPE_ADHOC)
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800286 iwl_set_ht_add_station(priv, sta_id, ht_info);
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800287
288 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
289
290 /* Add station to device's station table */
291 iwl_send_add_sta(priv, &station->sta, flags);
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800292 return sta_id;
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800293
294}
295EXPORT_SYMBOL(iwl_add_station_flags);
296
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800297static void iwl_sta_ucode_deactivate(struct iwl_priv *priv, const char *addr)
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800298{
299 unsigned long flags;
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800300 u8 sta_id = iwl_find_station(priv, addr);
301
302 BUG_ON(sta_id == IWL_INVALID_STATION);
303
Johannes Berge1749612008-10-27 15:59:26 -0700304 IWL_DEBUG_ASSOC("Removed STA from Ucode: %pM\n", addr);
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800305
306 spin_lock_irqsave(&priv->sta_lock, flags);
307
308 /* Ucode must be active and driver must be non active */
309 if (priv->stations[sta_id].used != IWL_STA_UCODE_ACTIVE)
310 IWL_ERROR("removed non active STA %d\n", sta_id);
311
312 priv->stations[sta_id].used &= ~IWL_STA_UCODE_ACTIVE;
313
314 memset(&priv->stations[sta_id], 0, sizeof(struct iwl_station_entry));
315 spin_unlock_irqrestore(&priv->sta_lock, flags);
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800316}
317
318static int iwl_remove_sta_callback(struct iwl_priv *priv,
319 struct iwl_cmd *cmd, struct sk_buff *skb)
320{
321 struct iwl_rx_packet *res = NULL;
Tomas Winkler3257e5d2008-10-14 12:32:43 -0700322 struct iwl_rem_sta_cmd *rm_sta =
323 (struct iwl_rem_sta_cmd *)cmd->cmd.payload;
324 const char *addr = rm_sta->addr;
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800325
326 if (!skb) {
327 IWL_ERROR("Error: Response NULL in REPLY_REMOVE_STA.\n");
328 return 1;
329 }
330
331 res = (struct iwl_rx_packet *)skb->data;
332 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
333 IWL_ERROR("Bad return from REPLY_REMOVE_STA (0x%08X)\n",
334 res->hdr.flags);
335 return 1;
336 }
337
338 switch (res->u.rem_sta.status) {
339 case REM_STA_SUCCESS_MSK:
340 iwl_sta_ucode_deactivate(priv, addr);
341 break;
342 default:
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800343 IWL_ERROR("REPLY_REMOVE_STA failed\n");
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800344 break;
345 }
346
347 /* We didn't cache the SKB; let the caller free it */
348 return 1;
349}
350
351static int iwl_send_remove_station(struct iwl_priv *priv, const u8 *addr,
352 u8 flags)
353{
354 struct iwl_rx_packet *res = NULL;
355 int ret;
356
357 struct iwl_rem_sta_cmd rm_sta_cmd;
358
359 struct iwl_host_cmd cmd = {
360 .id = REPLY_REMOVE_STA,
361 .len = sizeof(struct iwl_rem_sta_cmd),
362 .meta.flags = flags,
363 .data = &rm_sta_cmd,
364 };
365
366 memset(&rm_sta_cmd, 0, sizeof(rm_sta_cmd));
367 rm_sta_cmd.num_sta = 1;
368 memcpy(&rm_sta_cmd.addr, addr , ETH_ALEN);
369
370 if (flags & CMD_ASYNC)
371 cmd.meta.u.callback = iwl_remove_sta_callback;
372 else
373 cmd.meta.flags |= CMD_WANT_SKB;
374 ret = iwl_send_cmd(priv, &cmd);
375
376 if (ret || (flags & CMD_ASYNC))
377 return ret;
378
379 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
380 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
381 IWL_ERROR("Bad return from REPLY_REMOVE_STA (0x%08X)\n",
382 res->hdr.flags);
383 ret = -EIO;
384 }
385
386 if (!ret) {
387 switch (res->u.rem_sta.status) {
388 case REM_STA_SUCCESS_MSK:
389 iwl_sta_ucode_deactivate(priv, addr);
390 IWL_DEBUG_ASSOC("REPLY_REMOVE_STA PASSED\n");
391 break;
392 default:
393 ret = -EIO;
394 IWL_ERROR("REPLY_REMOVE_STA failed\n");
395 break;
396 }
397 }
398
399 priv->alloc_rxb_skb--;
400 dev_kfree_skb_any(cmd.meta.u.skb);
401
402 return ret;
403}
Emmanuel Grumbachbe1f3ab62008-06-12 09:47:18 +0800404
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800405/**
406 * iwl_remove_station - Remove driver's knowledge of station.
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800407 */
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800408int iwl_remove_station(struct iwl_priv *priv, const u8 *addr, int is_ap)
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800409{
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800410 int sta_id = IWL_INVALID_STATION;
411 int i, ret = -EINVAL;
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800412 unsigned long flags;
413
414 spin_lock_irqsave(&priv->sta_lock, flags);
415
416 if (is_ap)
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800417 sta_id = IWL_AP_ID;
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800418 else if (is_broadcast_ether_addr(addr))
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800419 sta_id = priv->hw_params.bcast_sta_id;
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800420 else
421 for (i = IWL_STA_ID; i < priv->hw_params.max_stations; i++)
422 if (priv->stations[i].used &&
423 !compare_ether_addr(priv->stations[i].sta.sta.addr,
424 addr)) {
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800425 sta_id = i;
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800426 break;
427 }
428
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800429 if (unlikely(sta_id == IWL_INVALID_STATION))
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800430 goto out;
431
Johannes Berge1749612008-10-27 15:59:26 -0700432 IWL_DEBUG_ASSOC("Removing STA from driver:%d %pM\n",
433 sta_id, addr);
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800434
435 if (!(priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE)) {
Johannes Berge1749612008-10-27 15:59:26 -0700436 IWL_ERROR("Removing %pM but non DRIVER active\n",
437 addr);
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800438 goto out;
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800439 }
440
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800441 if (!(priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE)) {
Johannes Berge1749612008-10-27 15:59:26 -0700442 IWL_ERROR("Removing %pM but non UCODE active\n",
443 addr);
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800444 goto out;
445 }
446
447
448 priv->stations[sta_id].used &= ~IWL_STA_DRIVER_ACTIVE;
449
450 priv->num_stations--;
451
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800452 BUG_ON(priv->num_stations < 0);
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800453
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800454 spin_unlock_irqrestore(&priv->sta_lock, flags);
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800455
456 ret = iwl_send_remove_station(priv, addr, CMD_ASYNC);
457 return ret;
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800458out:
459 spin_unlock_irqrestore(&priv->sta_lock, flags);
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800460 return ret;
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800461}
462EXPORT_SYMBOL(iwl_remove_station);
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800463
Winkler, Tomas83dde8c2008-11-19 15:32:23 -0800464/**
465 * iwl_clear_stations_table - Clear the driver's station table
466 *
467 * NOTE: This does not clear or otherwise alter the device's station table.
468 */
469void iwl_clear_stations_table(struct iwl_priv *priv)
470{
471 unsigned long flags;
472
473 spin_lock_irqsave(&priv->sta_lock, flags);
474
475 if (iwl_is_alive(priv) &&
476 !test_bit(STATUS_EXIT_PENDING, &priv->status) &&
477 iwl_send_cmd_pdu_async(priv, REPLY_REMOVE_ALL_STA, 0, NULL, NULL))
478 IWL_ERROR("Couldn't clear the station table\n");
479
480 priv->num_stations = 0;
481 memset(priv->stations, 0, sizeof(priv->stations));
482
483 spin_unlock_irqrestore(&priv->sta_lock, flags);
484}
485EXPORT_SYMBOL(iwl_clear_stations_table);
486
Emmanuel Grumbachbe1f3ab62008-06-12 09:47:18 +0800487static int iwl_get_free_ucode_key_index(struct iwl_priv *priv)
Emmanuel Grumbach80fb47a2008-04-14 21:16:08 -0700488{
489 int i;
490
491 for (i = 0; i < STA_KEY_MAX_NUM; i++)
Emmanuel Grumbach77bab602008-04-15 16:01:44 -0700492 if (!test_and_set_bit(i, &priv->ucode_key_table))
Emmanuel Grumbach80fb47a2008-04-14 21:16:08 -0700493 return i;
494
Tomas Winkler40a9a822008-11-25 23:29:03 +0200495 return WEP_INVALID_OFFSET;
Emmanuel Grumbach80fb47a2008-04-14 21:16:08 -0700496}
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700497
498int iwl_send_static_wepkey_cmd(struct iwl_priv *priv, u8 send_if_empty)
499{
500 int i, not_empty = 0;
501 u8 buff[sizeof(struct iwl_wep_cmd) +
502 sizeof(struct iwl_wep_key) * WEP_KEYS_MAX];
503 struct iwl_wep_cmd *wep_cmd = (struct iwl_wep_cmd *)buff;
504 size_t cmd_size = sizeof(struct iwl_wep_cmd);
505 struct iwl_host_cmd cmd = {
506 .id = REPLY_WEPKEY,
507 .data = wep_cmd,
508 .meta.flags = CMD_ASYNC,
509 };
510
511 memset(wep_cmd, 0, cmd_size +
512 (sizeof(struct iwl_wep_key) * WEP_KEYS_MAX));
513
514 for (i = 0; i < WEP_KEYS_MAX ; i++) {
515 wep_cmd->key[i].key_index = i;
516 if (priv->wep_keys[i].key_size) {
517 wep_cmd->key[i].key_offset = i;
518 not_empty = 1;
519 } else {
520 wep_cmd->key[i].key_offset = WEP_INVALID_OFFSET;
521 }
522
523 wep_cmd->key[i].key_size = priv->wep_keys[i].key_size;
524 memcpy(&wep_cmd->key[i].key[3], priv->wep_keys[i].key,
525 priv->wep_keys[i].key_size);
526 }
527
528 wep_cmd->global_key_type = WEP_KEY_WEP_TYPE;
529 wep_cmd->num_keys = WEP_KEYS_MAX;
530
531 cmd_size += sizeof(struct iwl_wep_key) * WEP_KEYS_MAX;
532
533 cmd.len = cmd_size;
534
535 if (not_empty || send_if_empty)
536 return iwl_send_cmd(priv, &cmd);
537 else
538 return 0;
539}
Tomas Winkler27aaba02008-05-05 10:22:44 +0800540EXPORT_SYMBOL(iwl_send_static_wepkey_cmd);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700541
542int iwl_remove_default_wep_key(struct iwl_priv *priv,
Emmanuel Grumbach80fb47a2008-04-14 21:16:08 -0700543 struct ieee80211_key_conf *keyconf)
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700544{
545 int ret;
546 unsigned long flags;
547
548 spin_lock_irqsave(&priv->sta_lock, flags);
Emmanuel Grumbach80fb47a2008-04-14 21:16:08 -0700549
550 if (!test_and_clear_bit(keyconf->keyidx, &priv->ucode_key_table))
551 IWL_ERROR("index %d not used in uCode key table.\n",
552 keyconf->keyidx);
553
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700554 priv->default_wep_key--;
Emmanuel Grumbach80fb47a2008-04-14 21:16:08 -0700555 memset(&priv->wep_keys[keyconf->keyidx], 0, sizeof(priv->wep_keys[0]));
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700556 ret = iwl_send_static_wepkey_cmd(priv, 1);
Emmanuel Grumbach4564ce82008-06-12 09:47:09 +0800557 IWL_DEBUG_WEP("Remove default WEP key: idx=%d ret=%d\n",
558 keyconf->keyidx, ret);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700559 spin_unlock_irqrestore(&priv->sta_lock, flags);
560
561 return ret;
562}
Tomas Winkler27aaba02008-05-05 10:22:44 +0800563EXPORT_SYMBOL(iwl_remove_default_wep_key);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700564
565int iwl_set_default_wep_key(struct iwl_priv *priv,
566 struct ieee80211_key_conf *keyconf)
567{
568 int ret;
569 unsigned long flags;
570
Emmanuel Grumbach4564ce82008-06-12 09:47:09 +0800571 if (keyconf->keylen != WEP_KEY_LEN_128 &&
572 keyconf->keylen != WEP_KEY_LEN_64) {
573 IWL_DEBUG_WEP("Bad WEP key length %d\n", keyconf->keylen);
574 return -EINVAL;
575 }
576
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700577 keyconf->flags &= ~IEEE80211_KEY_FLAG_GENERATE_IV;
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +0800578 keyconf->hw_key_idx = HW_KEY_DEFAULT;
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700579 priv->stations[IWL_AP_ID].keyinfo.alg = ALG_WEP;
580
581 spin_lock_irqsave(&priv->sta_lock, flags);
582 priv->default_wep_key++;
583
Emmanuel Grumbach80fb47a2008-04-14 21:16:08 -0700584 if (test_and_set_bit(keyconf->keyidx, &priv->ucode_key_table))
585 IWL_ERROR("index %d already used in uCode key table.\n",
586 keyconf->keyidx);
587
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700588 priv->wep_keys[keyconf->keyidx].key_size = keyconf->keylen;
589 memcpy(&priv->wep_keys[keyconf->keyidx].key, &keyconf->key,
590 keyconf->keylen);
591
592 ret = iwl_send_static_wepkey_cmd(priv, 0);
Emmanuel Grumbach4564ce82008-06-12 09:47:09 +0800593 IWL_DEBUG_WEP("Set default WEP key: len=%d idx=%d ret=%d\n",
594 keyconf->keylen, keyconf->keyidx, ret);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700595 spin_unlock_irqrestore(&priv->sta_lock, flags);
596
597 return ret;
598}
Tomas Winkler27aaba02008-05-05 10:22:44 +0800599EXPORT_SYMBOL(iwl_set_default_wep_key);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700600
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700601static int iwl_set_wep_dynamic_key_info(struct iwl_priv *priv,
Emmanuel Grumbach0211ddd2008-04-14 21:16:07 -0700602 struct ieee80211_key_conf *keyconf,
603 u8 sta_id)
604{
605 unsigned long flags;
606 __le16 key_flags = 0;
607 int ret;
608
609 keyconf->flags &= ~IEEE80211_KEY_FLAG_GENERATE_IV;
Emmanuel Grumbach0211ddd2008-04-14 21:16:07 -0700610
611 key_flags |= (STA_KEY_FLG_WEP | STA_KEY_FLG_MAP_KEY_MSK);
612 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
613 key_flags &= ~STA_KEY_FLG_INVALID;
614
615 if (keyconf->keylen == WEP_KEY_LEN_128)
616 key_flags |= STA_KEY_FLG_KEY_SIZE_MSK;
617
Tomas Winkler5425e492008-04-15 16:01:38 -0700618 if (sta_id == priv->hw_params.bcast_sta_id)
Emmanuel Grumbach0211ddd2008-04-14 21:16:07 -0700619 key_flags |= STA_KEY_MULTICAST_MSK;
620
621 spin_lock_irqsave(&priv->sta_lock, flags);
622
623 priv->stations[sta_id].keyinfo.alg = keyconf->alg;
624 priv->stations[sta_id].keyinfo.keylen = keyconf->keylen;
625 priv->stations[sta_id].keyinfo.keyidx = keyconf->keyidx;
626
627 memcpy(priv->stations[sta_id].keyinfo.key,
628 keyconf->key, keyconf->keylen);
629
630 memcpy(&priv->stations[sta_id].sta.key.key[3],
631 keyconf->key, keyconf->keylen);
632
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -0700633 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 =
Emmanuel Grumbach80fb47a2008-04-14 21:16:08 -0700636 iwl_get_free_ucode_key_index(priv);
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -0700637 /* else, we are overriding an existing key => no need to allocated room
638 * in uCode. */
Emmanuel Grumbach0211ddd2008-04-14 21:16:07 -0700639
Tomas Winkler40a9a822008-11-25 23:29:03 +0200640 WARN(priv->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
641 "no space for new kew");
642
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -0700643 priv->stations[sta_id].sta.key.key_flags = key_flags;
Emmanuel Grumbach0211ddd2008-04-14 21:16:07 -0700644 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
645 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
646
Tomas Winkler133636d2008-05-05 10:22:34 +0800647 ret = iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
Emmanuel Grumbach0211ddd2008-04-14 21:16:07 -0700648
649 spin_unlock_irqrestore(&priv->sta_lock, flags);
650
651 return ret;
652}
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700653
654static int iwl_set_ccmp_dynamic_key_info(struct iwl_priv *priv,
655 struct ieee80211_key_conf *keyconf,
656 u8 sta_id)
657{
658 unsigned long flags;
659 __le16 key_flags = 0;
Tomas Winkler40a9a822008-11-25 23:29:03 +0200660 int ret;
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700661
662 key_flags |= (STA_KEY_FLG_CCMP | STA_KEY_FLG_MAP_KEY_MSK);
663 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
664 key_flags &= ~STA_KEY_FLG_INVALID;
665
Tomas Winkler5425e492008-04-15 16:01:38 -0700666 if (sta_id == priv->hw_params.bcast_sta_id)
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700667 key_flags |= STA_KEY_MULTICAST_MSK;
668
669 keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700670
671 spin_lock_irqsave(&priv->sta_lock, flags);
672 priv->stations[sta_id].keyinfo.alg = keyconf->alg;
673 priv->stations[sta_id].keyinfo.keylen = keyconf->keylen;
674
675 memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key,
676 keyconf->keylen);
677
678 memcpy(priv->stations[sta_id].sta.key.key, keyconf->key,
679 keyconf->keylen);
680
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -0700681 if ((priv->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
682 == STA_KEY_FLG_NO_ENC)
683 priv->stations[sta_id].sta.key.key_offset =
684 iwl_get_free_ucode_key_index(priv);
685 /* else, we are overriding an existing key => no need to allocated room
686 * in uCode. */
687
Tomas Winkler40a9a822008-11-25 23:29:03 +0200688 WARN(priv->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
689 "no space for new kew");
690
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700691 priv->stations[sta_id].sta.key.key_flags = key_flags;
692 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
693 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
694
Tomas Winkler40a9a822008-11-25 23:29:03 +0200695 ret = iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
696
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700697 spin_unlock_irqrestore(&priv->sta_lock, flags);
698
Tomas Winkler40a9a822008-11-25 23:29:03 +0200699 return ret;
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700700}
701
702static int iwl_set_tkip_dynamic_key_info(struct iwl_priv *priv,
703 struct ieee80211_key_conf *keyconf,
704 u8 sta_id)
705{
706 unsigned long flags;
707 int ret = 0;
708
709 keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
710 keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700711
712 spin_lock_irqsave(&priv->sta_lock, flags);
713
714 priv->stations[sta_id].keyinfo.alg = keyconf->alg;
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700715 priv->stations[sta_id].keyinfo.keylen = 16;
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -0700716
717 if ((priv->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
718 == STA_KEY_FLG_NO_ENC)
719 priv->stations[sta_id].sta.key.key_offset =
Emmanuel Grumbach77bab602008-04-15 16:01:44 -0700720 iwl_get_free_ucode_key_index(priv);
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -0700721 /* else, we are overriding an existing key => no need to allocated room
722 * in uCode. */
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700723
Tomas Winkler40a9a822008-11-25 23:29:03 +0200724 WARN(priv->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
725 "no space for new kew");
726
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700727 /* This copy is acutally not needed: we get the key with each TX */
728 memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key, 16);
729
730 memcpy(priv->stations[sta_id].sta.key.key, keyconf->key, 16);
731
732 spin_unlock_irqrestore(&priv->sta_lock, flags);
733
734 return ret;
735}
736
Tomas Winkler9f586712008-11-12 13:14:05 -0800737void iwl_update_tkip_key(struct iwl_priv *priv,
738 struct ieee80211_key_conf *keyconf,
739 const u8 *addr, u32 iv32, u16 *phase1key)
740{
741 u8 sta_id = IWL_INVALID_STATION;
742 unsigned long flags;
743 __le16 key_flags = 0;
744 int i;
745 DECLARE_MAC_BUF(mac);
746
747 sta_id = iwl_find_station(priv, addr);
748 if (sta_id == IWL_INVALID_STATION) {
749 IWL_DEBUG_MAC80211("leave - %pM not in station map.\n",
750 addr);
751 return;
752 }
753
754 if (iwl_scan_cancel(priv)) {
755 /* cancel scan failed, just live w/ bad key and rely
756 briefly on SW decryption */
757 return;
758 }
759
760 key_flags |= (STA_KEY_FLG_TKIP | STA_KEY_FLG_MAP_KEY_MSK);
761 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
762 key_flags &= ~STA_KEY_FLG_INVALID;
763
764 if (sta_id == priv->hw_params.bcast_sta_id)
765 key_flags |= STA_KEY_MULTICAST_MSK;
766
767 spin_lock_irqsave(&priv->sta_lock, flags);
768
769 priv->stations[sta_id].sta.key.key_flags = key_flags;
770 priv->stations[sta_id].sta.key.tkip_rx_tsc_byte2 = (u8) iv32;
771
772 for (i = 0; i < 5; i++)
773 priv->stations[sta_id].sta.key.tkip_rx_ttak[i] =
774 cpu_to_le16(phase1key[i]);
775
776 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
777 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
778
779 iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
780
781 spin_unlock_irqrestore(&priv->sta_lock, flags);
782
783}
784EXPORT_SYMBOL(iwl_update_tkip_key);
785
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -0700786int iwl_remove_dynamic_key(struct iwl_priv *priv,
787 struct ieee80211_key_conf *keyconf,
788 u8 sta_id)
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700789{
790 unsigned long flags;
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -0700791 int ret = 0;
792 u16 key_flags;
793 u8 keyidx;
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700794
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +0800795 priv->key_mapping_key--;
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700796
797 spin_lock_irqsave(&priv->sta_lock, flags);
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -0700798 key_flags = le16_to_cpu(priv->stations[sta_id].sta.key.key_flags);
799 keyidx = (key_flags >> STA_KEY_FLG_KEYID_POS) & 0x3;
800
Emmanuel Grumbach4564ce82008-06-12 09:47:09 +0800801 IWL_DEBUG_WEP("Remove dynamic key: idx=%d sta=%d\n",
802 keyconf->keyidx, sta_id);
803
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -0700804 if (keyconf->keyidx != keyidx) {
805 /* We need to remove a key with index different that the one
806 * in the uCode. This means that the key we need to remove has
807 * been replaced by another one with different index.
808 * Don't do anything and return ok
809 */
810 spin_unlock_irqrestore(&priv->sta_lock, flags);
811 return 0;
812 }
813
Tomas Winkler40a9a822008-11-25 23:29:03 +0200814 if (priv->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800815 IWL_WARN(priv, "Removing wrong key %d 0x%x\n",
Tomas Winkler40a9a822008-11-25 23:29:03 +0200816 keyconf->keyidx, key_flags);
817 spin_unlock_irqrestore(&priv->sta_lock, flags);
818 return 0;
819 }
820
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700821 if (!test_and_clear_bit(priv->stations[sta_id].sta.key.key_offset,
822 &priv->ucode_key_table))
823 IWL_ERROR("index %d not used in uCode key table.\n",
824 priv->stations[sta_id].sta.key.key_offset);
825 memset(&priv->stations[sta_id].keyinfo, 0,
Tomas Winkler6def9762008-05-05 10:22:31 +0800826 sizeof(struct iwl_hw_key));
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700827 memset(&priv->stations[sta_id].sta.key, 0,
828 sizeof(struct iwl4965_keyinfo));
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -0700829 priv->stations[sta_id].sta.key.key_flags =
830 STA_KEY_FLG_NO_ENC | STA_KEY_FLG_INVALID;
831 priv->stations[sta_id].sta.key.key_offset = WEP_INVALID_OFFSET;
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700832 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
833 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700834
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +0800835 ret = iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -0700836 spin_unlock_irqrestore(&priv->sta_lock, flags);
837 return ret;
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700838}
Tomas Winkler27aaba02008-05-05 10:22:44 +0800839EXPORT_SYMBOL(iwl_remove_dynamic_key);
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700840
841int iwl_set_dynamic_key(struct iwl_priv *priv,
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +0800842 struct ieee80211_key_conf *keyconf, u8 sta_id)
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700843{
844 int ret;
845
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +0800846 priv->key_mapping_key++;
847 keyconf->hw_key_idx = HW_KEY_DYNAMIC;
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700848
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +0800849 switch (keyconf->alg) {
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700850 case ALG_CCMP:
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +0800851 ret = iwl_set_ccmp_dynamic_key_info(priv, keyconf, sta_id);
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700852 break;
853 case ALG_TKIP:
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +0800854 ret = iwl_set_tkip_dynamic_key_info(priv, keyconf, sta_id);
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700855 break;
856 case ALG_WEP:
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +0800857 ret = iwl_set_wep_dynamic_key_info(priv, keyconf, sta_id);
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700858 break;
859 default:
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +0800860 IWL_ERROR("Unknown alg: %s alg = %d\n", __func__, keyconf->alg);
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700861 ret = -EINVAL;
862 }
863
Emmanuel Grumbach4564ce82008-06-12 09:47:09 +0800864 IWL_DEBUG_WEP("Set dynamic key: alg= %d len=%d idx=%d sta=%d ret=%d\n",
865 keyconf->alg, keyconf->keylen, keyconf->keyidx,
866 sta_id, ret);
867
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700868 return ret;
869}
Tomas Winkler27aaba02008-05-05 10:22:44 +0800870EXPORT_SYMBOL(iwl_set_dynamic_key);
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700871
Tomas Winkler66c73db2008-04-15 16:01:40 -0700872#ifdef CONFIG_IWLWIFI_DEBUG
873static void iwl_dump_lq_cmd(struct iwl_priv *priv,
874 struct iwl_link_quality_cmd *lq)
875{
876 int i;
877 IWL_DEBUG_RATE("lq station id 0x%x\n", lq->sta_id);
Tomas Winklera96a27f2008-10-23 23:48:56 -0700878 IWL_DEBUG_RATE("lq ant 0x%X 0x%X\n",
Tomas Winkler66c73db2008-04-15 16:01:40 -0700879 lq->general_params.single_stream_ant_msk,
880 lq->general_params.dual_stream_ant_msk);
881
882 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
883 IWL_DEBUG_RATE("lq index %d 0x%X\n",
884 i, lq->rs_table[i].rate_n_flags);
885}
886#else
887static inline void iwl_dump_lq_cmd(struct iwl_priv *priv,
888 struct iwl_link_quality_cmd *lq)
889{
890}
891#endif
892
893int iwl_send_lq_cmd(struct iwl_priv *priv,
894 struct iwl_link_quality_cmd *lq, u8 flags)
895{
896 struct iwl_host_cmd cmd = {
897 .id = REPLY_TX_LINK_QUALITY_CMD,
898 .len = sizeof(struct iwl_link_quality_cmd),
899 .meta.flags = flags,
900 .data = lq,
901 };
902
903 if ((lq->sta_id == 0xFF) &&
Johannes Berg05c914f2008-09-11 00:01:58 +0200904 (priv->iw_mode == NL80211_IFTYPE_ADHOC))
Tomas Winkler66c73db2008-04-15 16:01:40 -0700905 return -EINVAL;
906
907 if (lq->sta_id == 0xFF)
908 lq->sta_id = IWL_AP_ID;
909
Tomas Winkler3ac7f142008-07-21 02:40:14 +0300910 iwl_dump_lq_cmd(priv, lq);
Tomas Winkler66c73db2008-04-15 16:01:40 -0700911
Ron Rindjunsky36e1f162008-06-30 17:23:13 +0800912 if (iwl_is_associated(priv) && priv->assoc_station_added)
Tomas Winkler66c73db2008-04-15 16:01:40 -0700913 return iwl_send_cmd(priv, &cmd);
914
915 return 0;
916}
917EXPORT_SYMBOL(iwl_send_lq_cmd);
918
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800919/**
920 * iwl_sta_init_lq - Initialize a station's hardware rate table
921 *
922 * The uCode's station table contains a table of fallback rates
923 * for automatic fallback during transmission.
924 *
925 * NOTE: This sets up a default set of values. These will be replaced later
Tomas Winklere227cea2008-07-18 13:53:05 +0800926 * if the driver's iwl-agn-rs rate scaling algorithm is used, instead of
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800927 * rc80211_simple.
928 *
929 * NOTE: Run REPLY_ADD_STA command to set up station table entry, before
930 * calling this function (which runs REPLY_TX_LINK_QUALITY_CMD,
931 * which requires station table entry to exist).
932 */
933static void iwl_sta_init_lq(struct iwl_priv *priv, const u8 *addr, int is_ap)
934{
935 int i, r;
936 struct iwl_link_quality_cmd link_cmd = {
937 .reserved1 = 0,
938 };
Tomas Winkler5d664a42008-10-08 09:37:29 +0800939 u32 rate_flags;
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800940
941 /* Set up the rate scaling to start at selected rate, fall back
942 * all the way down to 1M in IEEE order, and then spin on 1M */
943 if (is_ap)
944 r = IWL_RATE_54M_INDEX;
945 else if (priv->band == IEEE80211_BAND_5GHZ)
946 r = IWL_RATE_6M_INDEX;
947 else
948 r = IWL_RATE_1M_INDEX;
949
950 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
951 rate_flags = 0;
952 if (r >= IWL_FIRST_CCK_RATE && r <= IWL_LAST_CCK_RATE)
953 rate_flags |= RATE_MCS_CCK_MSK;
954
Tomas Winkler5d664a42008-10-08 09:37:29 +0800955 rate_flags |= first_antenna(priv->hw_params.valid_tx_ant) <<
956 RATE_MCS_ANT_POS;
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800957
958 link_cmd.rs_table[i].rate_n_flags =
Tomas Winklere7d326a2008-06-12 09:47:11 +0800959 iwl_hw_set_rate_n_flags(iwl_rates[r].plcp, rate_flags);
Winkler, Tomaseb4779c2008-10-29 14:05:44 -0700960 r = iwl_get_prev_ieee_rate(r);
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800961 }
962
Tomas Winkler5d664a42008-10-08 09:37:29 +0800963 link_cmd.general_params.single_stream_ant_msk =
964 first_antenna(priv->hw_params.valid_tx_ant);
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800965 link_cmd.general_params.dual_stream_ant_msk = 3;
966 link_cmd.agg_params.agg_dis_start_th = 3;
967 link_cmd.agg_params.agg_time_limit = cpu_to_le16(4000);
968
969 /* Update the rate scaling for control frame Tx to AP */
970 link_cmd.sta_id = is_ap ? IWL_AP_ID : priv->hw_params.bcast_sta_id;
971
972 iwl_send_cmd_pdu_async(priv, REPLY_TX_LINK_QUALITY_CMD,
973 sizeof(link_cmd), &link_cmd, NULL);
974}
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800975
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800976/**
977 * iwl_rxon_add_station - add station into station table.
978 *
979 * there is only one AP station with id= IWL_AP_ID
Tomas Winklera96a27f2008-10-23 23:48:56 -0700980 * NOTE: mutex must be held before calling this function
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800981 */
982int iwl_rxon_add_station(struct iwl_priv *priv, const u8 *addr, int is_ap)
983{
Johannes Bergae5eb022008-10-14 16:58:37 +0200984 struct ieee80211_sta *sta;
985 struct ieee80211_sta_ht_cap ht_config;
986 struct ieee80211_sta_ht_cap *cur_ht_config = NULL;
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800987 u8 sta_id;
988
989 /* Add station to device's station table */
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800990
Johannes Bergae5eb022008-10-14 16:58:37 +0200991 /*
992 * XXX: This check is definitely not correct, if we're an AP
993 * it'll always be false which is not what we want, but
994 * it doesn't look like iwlagn is prepared to be an HT
995 * AP anyway.
996 */
997 if (priv->current_ht_config.is_ht) {
998 rcu_read_lock();
999 sta = ieee80211_find_sta(priv->hw, addr);
1000 if (sta) {
1001 memcpy(&ht_config, &sta->ht_cap, sizeof(ht_config));
1002 cur_ht_config = &ht_config;
1003 }
1004 rcu_read_unlock();
1005 }
1006
1007 sta_id = iwl_add_station_flags(priv, addr, is_ap,
1008 0, cur_ht_config);
Tomas Winkler4f40e4d2008-05-15 13:54:04 +08001009
1010 /* Set up default rate scaling table in device's station table */
1011 iwl_sta_init_lq(priv, addr, is_ap);
1012
1013 return sta_id;
1014}
1015EXPORT_SYMBOL(iwl_rxon_add_station);
1016
Tomas Winkler4f40e4d2008-05-15 13:54:04 +08001017/**
1018 * iwl_get_sta_id - Find station's index within station table
1019 *
1020 * If new IBSS station, create new entry in station table
1021 */
1022int iwl_get_sta_id(struct iwl_priv *priv, struct ieee80211_hdr *hdr)
1023{
1024 int sta_id;
1025 u16 fc = le16_to_cpu(hdr->frame_control);
Tomas Winkler4f40e4d2008-05-15 13:54:04 +08001026
1027 /* If this frame is broadcast or management, use broadcast station id */
1028 if (((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA) ||
1029 is_multicast_ether_addr(hdr->addr1))
1030 return priv->hw_params.bcast_sta_id;
1031
1032 switch (priv->iw_mode) {
1033
1034 /* If we are a client station in a BSS network, use the special
1035 * AP station entry (that's the only station we communicate with) */
Johannes Berg05c914f2008-09-11 00:01:58 +02001036 case NL80211_IFTYPE_STATION:
Tomas Winkler4f40e4d2008-05-15 13:54:04 +08001037 return IWL_AP_ID;
1038
1039 /* If we are an AP, then find the station, or use BCAST */
Johannes Berg05c914f2008-09-11 00:01:58 +02001040 case NL80211_IFTYPE_AP:
Tomas Winkler4f40e4d2008-05-15 13:54:04 +08001041 sta_id = iwl_find_station(priv, hdr->addr1);
1042 if (sta_id != IWL_INVALID_STATION)
1043 return sta_id;
1044 return priv->hw_params.bcast_sta_id;
1045
1046 /* If this frame is going out to an IBSS network, find the station,
1047 * or create a new station table entry */
Johannes Berg05c914f2008-09-11 00:01:58 +02001048 case NL80211_IFTYPE_ADHOC:
Tomas Winkler4f40e4d2008-05-15 13:54:04 +08001049 sta_id = iwl_find_station(priv, hdr->addr1);
1050 if (sta_id != IWL_INVALID_STATION)
1051 return sta_id;
1052
1053 /* Create new station table entry */
1054 sta_id = iwl_add_station_flags(priv, hdr->addr1,
1055 0, CMD_ASYNC, NULL);
1056
1057 if (sta_id != IWL_INVALID_STATION)
1058 return sta_id;
1059
Johannes Berge1749612008-10-27 15:59:26 -07001060 IWL_DEBUG_DROP("Station %pM not in station map. "
Tomas Winkler4f40e4d2008-05-15 13:54:04 +08001061 "Defaulting to broadcast...\n",
Johannes Berge1749612008-10-27 15:59:26 -07001062 hdr->addr1);
Tomas Winkler4f40e4d2008-05-15 13:54:04 +08001063 iwl_print_hex_dump(priv, IWL_DL_DROP, (u8 *) hdr, sizeof(*hdr));
1064 return priv->hw_params.bcast_sta_id;
1065
Stefanik Gábord10c4ec2008-09-03 11:26:59 +08001066 /* If we are in monitor mode, use BCAST. This is required for
1067 * packet injection. */
Johannes Berg05c914f2008-09-11 00:01:58 +02001068 case NL80211_IFTYPE_MONITOR:
Stefanik Gábord10c4ec2008-09-03 11:26:59 +08001069 return priv->hw_params.bcast_sta_id;
1070
Tomas Winkler4f40e4d2008-05-15 13:54:04 +08001071 default:
Winkler, Tomas39aadf82008-12-19 10:37:32 +08001072 IWL_WARN(priv, "Unknown mode of operation: %d\n",
1073 priv->iw_mode);
Tomas Winkler4f40e4d2008-05-15 13:54:04 +08001074 return priv->hw_params.bcast_sta_id;
1075 }
1076}
1077EXPORT_SYMBOL(iwl_get_sta_id);
1078
Tomas Winkler5083e562008-05-29 16:35:15 +08001079/**
Tomas Winkler9f586712008-11-12 13:14:05 -08001080 * iwl_sta_tx_modify_enable_tid - Enable Tx for this TID in station table
Tomas Winkler5083e562008-05-29 16:35:15 +08001081 */
Tomas Winkler9f586712008-11-12 13:14:05 -08001082void iwl_sta_tx_modify_enable_tid(struct iwl_priv *priv, int sta_id, int tid)
Tomas Winkler5083e562008-05-29 16:35:15 +08001083{
1084 unsigned long flags;
1085
1086 /* Remove "disable" flag, to enable Tx for this TID */
1087 spin_lock_irqsave(&priv->sta_lock, flags);
1088 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_TID_DISABLE_TX;
1089 priv->stations[sta_id].sta.tid_disable_tx &= cpu_to_le16(~(1 << tid));
1090 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1091 spin_unlock_irqrestore(&priv->sta_lock, flags);
1092
1093 iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
1094}
Tomas Winkler9f586712008-11-12 13:14:05 -08001095EXPORT_SYMBOL(iwl_sta_tx_modify_enable_tid);
1096
1097int iwl_sta_rx_agg_start(struct iwl_priv *priv,
1098 const u8 *addr, int tid, u16 ssn)
1099{
1100 unsigned long flags;
1101 int sta_id;
1102
1103 sta_id = iwl_find_station(priv, addr);
1104 if (sta_id == IWL_INVALID_STATION)
1105 return -ENXIO;
1106
1107 spin_lock_irqsave(&priv->sta_lock, flags);
1108 priv->stations[sta_id].sta.station_flags_msk = 0;
1109 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_ADDBA_TID_MSK;
1110 priv->stations[sta_id].sta.add_immediate_ba_tid = (u8)tid;
1111 priv->stations[sta_id].sta.add_immediate_ba_ssn = cpu_to_le16(ssn);
1112 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1113 spin_unlock_irqrestore(&priv->sta_lock, flags);
1114
1115 return iwl_send_add_sta(priv, &priv->stations[sta_id].sta,
1116 CMD_ASYNC);
1117}
1118EXPORT_SYMBOL(iwl_sta_rx_agg_start);
1119
1120int iwl_sta_rx_agg_stop(struct iwl_priv *priv, const u8 *addr, int tid)
1121{
1122 unsigned long flags;
1123 int sta_id;
1124
1125 sta_id = iwl_find_station(priv, addr);
1126 if (sta_id == IWL_INVALID_STATION)
1127 return -ENXIO;
1128
1129 spin_lock_irqsave(&priv->sta_lock, flags);
1130 priv->stations[sta_id].sta.station_flags_msk = 0;
1131 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_DELBA_TID_MSK;
1132 priv->stations[sta_id].sta.remove_immediate_ba_tid = (u8)tid;
1133 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1134 spin_unlock_irqrestore(&priv->sta_lock, flags);
1135
1136 return iwl_send_add_sta(priv, &priv->stations[sta_id].sta,
1137 CMD_ASYNC);
1138}
1139EXPORT_SYMBOL(iwl_sta_rx_agg_stop);
1140
1141static void iwl_sta_modify_ps_wake(struct iwl_priv *priv, int sta_id)
1142{
1143 unsigned long flags;
1144
1145 spin_lock_irqsave(&priv->sta_lock, flags);
1146 priv->stations[sta_id].sta.station_flags &= ~STA_FLG_PWR_SAVE_MSK;
1147 priv->stations[sta_id].sta.station_flags_msk = STA_FLG_PWR_SAVE_MSK;
1148 priv->stations[sta_id].sta.sta.modify_mask = 0;
1149 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1150 spin_unlock_irqrestore(&priv->sta_lock, flags);
1151
1152 iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
1153}
1154
1155void iwl_update_ps_mode(struct iwl_priv *priv, u16 ps_bit, u8 *addr)
1156{
1157 /* FIXME: need locking over ps_status ??? */
1158 u8 sta_id = iwl_find_station(priv, addr);
1159
1160 if (sta_id != IWL_INVALID_STATION) {
1161 u8 sta_awake = priv->stations[sta_id].
1162 ps_status == STA_PS_STATUS_WAKE;
1163
1164 if (sta_awake && ps_bit)
1165 priv->stations[sta_id].ps_status = STA_PS_STATUS_SLEEP;
1166 else if (!sta_awake && !ps_bit) {
1167 iwl_sta_modify_ps_wake(priv, sta_id);
1168 priv->stations[sta_id].ps_status = STA_PS_STATUS_WAKE;
1169 }
1170 }
1171}
Tomas Winkler5083e562008-05-29 16:35:15 +08001172