blob: dd1c639074c1770dfd66096de130f40cb855e447 [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>
Reinette Chatrefe6b23d2010-02-22 16:24:47 -080032#include <linux/sched.h>
Emmanuel Grumbach6974e362008-04-14 21:16:06 -070033
Tomas Winkler3e0d4cb2008-04-24 11:55:38 -070034#include "iwl-dev.h"
Emmanuel Grumbach6974e362008-04-14 21:16:06 -070035#include "iwl-core.h"
36#include "iwl-sta.h"
Tomas Winkler7a999bf2008-05-29 16:35:02 +080037
Tomas Winkler947b13a2008-04-16 16:34:48 -070038u8 iwl_find_station(struct iwl_priv *priv, const u8 *addr)
39{
40 int i;
41 int start = 0;
42 int ret = IWL_INVALID_STATION;
43 unsigned long flags;
Tomas Winkler947b13a2008-04-16 16:34:48 -070044
Johannes Berg05c914f2008-09-11 00:01:58 +020045 if ((priv->iw_mode == NL80211_IFTYPE_ADHOC) ||
46 (priv->iw_mode == NL80211_IFTYPE_AP))
Tomas Winkler947b13a2008-04-16 16:34:48 -070047 start = IWL_STA_ID;
48
49 if (is_broadcast_ether_addr(addr))
50 return priv->hw_params.bcast_sta_id;
51
52 spin_lock_irqsave(&priv->sta_lock, flags);
53 for (i = start; i < priv->hw_params.max_stations; i++)
54 if (priv->stations[i].used &&
55 (!compare_ether_addr(priv->stations[i].sta.sta.addr,
56 addr))) {
57 ret = i;
58 goto out;
59 }
60
Tomas Winklere1623442009-01-27 14:27:56 -080061 IWL_DEBUG_ASSOC_LIMIT(priv, "can not find STA %pM total %d\n",
Johannes Berge1749612008-10-27 15:59:26 -070062 addr, priv->num_stations);
Tomas Winkler947b13a2008-04-16 16:34:48 -070063
64 out:
Reinette Chatrefe6b23d2010-02-22 16:24:47 -080065 /*
66 * It may be possible that more commands interacting with stations
67 * arrive before we completed processing the adding of
68 * station
69 */
70 if (ret != IWL_INVALID_STATION &&
71 (!(priv->stations[ret].used & IWL_STA_UCODE_ACTIVE) ||
72 ((priv->stations[ret].used & IWL_STA_UCODE_ACTIVE) &&
73 (priv->stations[ret].used & IWL_STA_UCODE_INPROGRESS)))) {
Frans Pop91dd6c22010-03-24 14:19:58 -070074 IWL_ERR(priv, "Requested station info for sta %d before ready.\n",
Reinette Chatrefe6b23d2010-02-22 16:24:47 -080075 ret);
76 ret = IWL_INVALID_STATION;
77 }
Tomas Winkler947b13a2008-04-16 16:34:48 -070078 spin_unlock_irqrestore(&priv->sta_lock, flags);
79 return ret;
80}
81EXPORT_SYMBOL(iwl_find_station);
82
Reinette Chatre1fa97aa2010-01-22 14:22:48 -080083/* priv->sta_lock must be held */
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +080084static void iwl_sta_ucode_activate(struct iwl_priv *priv, u8 sta_id)
85{
Reinette Chatre1fa97aa2010-01-22 14:22:48 -080086
87 if (!(priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE))
88 IWL_ERR(priv, "ACTIVATE a non DRIVER active station id %u addr %pM\n",
89 sta_id, priv->stations[sta_id].sta.sta.addr);
90
91 if (priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE) {
92 IWL_DEBUG_ASSOC(priv,
93 "STA id %u addr %pM already present in uCode (according to driver)\n",
94 sta_id, priv->stations[sta_id].sta.sta.addr);
95 } else {
96 priv->stations[sta_id].used |= IWL_STA_UCODE_ACTIVE;
97 IWL_DEBUG_ASSOC(priv, "Added STA id %u addr %pM to uCode\n",
98 sta_id, priv->stations[sta_id].sta.sta.addr);
99 }
100}
101
102static void iwl_process_add_sta_resp(struct iwl_priv *priv,
103 struct iwl_addsta_cmd *addsta,
104 struct iwl_rx_packet *pkt,
105 bool sync)
106{
107 u8 sta_id = addsta->sta.sta_id;
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800108 unsigned long flags;
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800109
Reinette Chatre1fa97aa2010-01-22 14:22:48 -0800110 if (pkt->hdr.flags & IWL_CMD_FAILED_MSK) {
111 IWL_ERR(priv, "Bad return from REPLY_ADD_STA (0x%08X)\n",
112 pkt->hdr.flags);
113 return;
114 }
115
116 IWL_DEBUG_INFO(priv, "Processing response for adding station %u\n",
117 sta_id);
118
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800119 spin_lock_irqsave(&priv->sta_lock, flags);
120
Reinette Chatre1fa97aa2010-01-22 14:22:48 -0800121 switch (pkt->u.add_sta.status) {
122 case ADD_STA_SUCCESS_MSK:
123 IWL_DEBUG_INFO(priv, "REPLY_ADD_STA PASSED\n");
124 iwl_sta_ucode_activate(priv, sta_id);
125 break;
126 case ADD_STA_NO_ROOM_IN_TABLE:
127 IWL_ERR(priv, "Adding station %d failed, no room in table.\n",
Winkler, Tomas15b16872008-12-19 10:37:33 +0800128 sta_id);
Reinette Chatre1fa97aa2010-01-22 14:22:48 -0800129 break;
130 case ADD_STA_NO_BLOCK_ACK_RESOURCE:
131 IWL_ERR(priv, "Adding station %d failed, no block ack resource.\n",
132 sta_id);
133 break;
134 case ADD_STA_MODIFY_NON_EXIST_STA:
Frans Pop91dd6c22010-03-24 14:19:58 -0700135 IWL_ERR(priv, "Attempting to modify non-existing station %d\n",
Reinette Chatre1fa97aa2010-01-22 14:22:48 -0800136 sta_id);
137 break;
138 default:
139 IWL_DEBUG_ASSOC(priv, "Received REPLY_ADD_STA:(0x%08X)\n",
140 pkt->u.add_sta.status);
141 break;
142 }
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800143
Reinette Chatre1fa97aa2010-01-22 14:22:48 -0800144 IWL_DEBUG_INFO(priv, "%s station id %u addr %pM\n",
145 priv->stations[sta_id].sta.mode ==
146 STA_CONTROL_MODIFY_MSK ? "Modified" : "Added",
147 sta_id, priv->stations[sta_id].sta.sta.addr);
148
149 /*
150 * XXX: The MAC address in the command buffer is often changed from
151 * the original sent to the device. That is, the MAC address
152 * written to the command buffer often is not the same MAC adress
153 * read from the command buffer when the command returns. This
154 * issue has not yet been resolved and this debugging is left to
155 * observe the problem.
156 */
157 IWL_DEBUG_INFO(priv, "%s station according to cmd buffer %pM\n",
158 priv->stations[sta_id].sta.mode ==
159 STA_CONTROL_MODIFY_MSK ? "Modified" : "Added",
160 addsta->sta.addr);
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800161 spin_unlock_irqrestore(&priv->sta_lock, flags);
162}
163
Johannes Berg5696aea2009-07-24 11:13:06 -0700164static void iwl_add_sta_callback(struct iwl_priv *priv,
165 struct iwl_device_cmd *cmd,
Zhu Yi2f301222009-10-09 17:19:45 +0800166 struct iwl_rx_packet *pkt)
Tomas Winkler42132bc2008-05-29 16:35:03 +0800167{
Tomas Winkler3257e5d2008-10-14 12:32:43 -0700168 struct iwl_addsta_cmd *addsta =
169 (struct iwl_addsta_cmd *)cmd->cmd.payload;
Tomas Winkler42132bc2008-05-29 16:35:03 +0800170
Reinette Chatre1fa97aa2010-01-22 14:22:48 -0800171 iwl_process_add_sta_resp(priv, addsta, pkt, false);
Tomas Winkler42132bc2008-05-29 16:35:03 +0800172
Tomas Winkler42132bc2008-05-29 16:35:03 +0800173}
174
Samuel Ortiz17f841c2009-01-23 13:45:20 -0800175int iwl_send_add_sta(struct iwl_priv *priv,
Tomas Winkler133636d2008-05-05 10:22:34 +0800176 struct iwl_addsta_cmd *sta, u8 flags)
177{
Zhu Yi2f301222009-10-09 17:19:45 +0800178 struct iwl_rx_packet *pkt = NULL;
Tomas Winkler133636d2008-05-05 10:22:34 +0800179 int ret = 0;
180 u8 data[sizeof(*sta)];
181 struct iwl_host_cmd cmd = {
182 .id = REPLY_ADD_STA,
Johannes Bergc2acea82009-07-24 11:13:05 -0700183 .flags = flags,
Tomas Winkler133636d2008-05-05 10:22:34 +0800184 .data = data,
185 };
Reinette Chatref875f512010-04-05 10:43:10 -0700186 u8 sta_id __maybe_unused = sta->sta.sta_id;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800187
188 IWL_DEBUG_INFO(priv, "Adding sta %u (%pM) %ssynchronously\n",
189 sta_id, sta->sta.addr, flags & CMD_ASYNC ? "a" : "");
Tomas Winkler133636d2008-05-05 10:22:34 +0800190
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/**
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800263 * iwl_prep_station - Prepare station information for addition
264 *
265 * should be called with sta_lock held
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800266 */
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800267static u8 iwl_prep_station(struct iwl_priv *priv, const u8 *addr,
268 bool is_ap,
269 struct ieee80211_sta_ht_cap *ht_info)
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800270{
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800271 struct iwl_station_entry *station;
Tomas Winklerc587de02009-06-03 11:44:07 -0700272 int i;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800273 u8 sta_id = IWL_INVALID_STATION;
Tomas Winklerc587de02009-06-03 11:44:07 -0700274 u16 rate;
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800275
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800276 if (is_ap)
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800277 sta_id = IWL_AP_ID;
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800278 else if (is_broadcast_ether_addr(addr))
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800279 sta_id = priv->hw_params.bcast_sta_id;
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800280 else
281 for (i = IWL_STA_ID; i < priv->hw_params.max_stations; i++) {
282 if (!compare_ether_addr(priv->stations[i].sta.sta.addr,
283 addr)) {
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800284 sta_id = i;
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800285 break;
286 }
287
288 if (!priv->stations[i].used &&
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800289 sta_id == IWL_INVALID_STATION)
290 sta_id = i;
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800291 }
292
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800293 /*
294 * These two conditions have the same outcome, but keep them
295 * separate
296 */
297 if (unlikely(sta_id == IWL_INVALID_STATION))
298 return sta_id;
299
300 /*
301 * uCode is not able to deal with multiple requests to add a
302 * station. Keep track if one is in progress so that we do not send
303 * another.
304 */
305 if (priv->stations[sta_id].used & IWL_STA_UCODE_INPROGRESS) {
306 IWL_DEBUG_INFO(priv, "STA %d already in process of being added.\n",
307 sta_id);
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800308 return sta_id;
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800309 }
310
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800311 if ((priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE) &&
312 (priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE) &&
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800313 !compare_ether_addr(priv->stations[sta_id].sta.sta.addr, addr)) {
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800314 IWL_DEBUG_ASSOC(priv, "STA %d (%pM) already added, not adding again.\n",
315 sta_id, addr);
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800316 return sta_id;
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800317 }
318
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800319 station = &priv->stations[sta_id];
320 station->used = IWL_STA_DRIVER_ACTIVE;
Tomas Winklere1623442009-01-27 14:27:56 -0800321 IWL_DEBUG_ASSOC(priv, "Add STA to driver ID %d: %pM\n",
Johannes Berge1749612008-10-27 15:59:26 -0700322 sta_id, addr);
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800323 priv->num_stations++;
324
325 /* Set up the REPLY_ADD_STA command to send to device */
326 memset(&station->sta, 0, sizeof(struct iwl_addsta_cmd));
327 memcpy(station->sta.sta.addr, addr, ETH_ALEN);
328 station->sta.mode = 0;
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800329 station->sta.sta.sta_id = sta_id;
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800330 station->sta.station_flags = 0;
331
332 /* BCAST station and IBSS stations do not work in HT mode */
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800333 if (sta_id != priv->hw_params.bcast_sta_id &&
Johannes Berg05c914f2008-09-11 00:01:58 +0200334 priv->iw_mode != NL80211_IFTYPE_ADHOC)
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800335 iwl_set_ht_add_station(priv, sta_id, ht_info);
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800336
Tomas Winklerc587de02009-06-03 11:44:07 -0700337 /* 3945 only */
338 rate = (priv->band == IEEE80211_BAND_5GHZ) ?
339 IWL_RATE_6M_PLCP : IWL_RATE_1M_PLCP;
340 /* Turn on both antennas for the station... */
341 station->sta.rate_n_flags = cpu_to_le16(rate | RATE_MCS_ANT_AB_MSK);
342
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800343 return sta_id;
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800344
345}
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800346
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800347#define STA_WAIT_TIMEOUT (HZ/2)
348
349/**
350 * iwl_add_station_common -
351 */
352int iwl_add_station_common(struct iwl_priv *priv, const u8 *addr,
353 bool is_ap,
354 struct ieee80211_sta_ht_cap *ht_info,
355 u8 *sta_id_r)
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800356{
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800357 struct iwl_station_entry *station;
358 unsigned long flags_spin;
359 int ret = 0;
360 u8 sta_id;
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800361
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800362 *sta_id_r = 0;
363 spin_lock_irqsave(&priv->sta_lock, flags_spin);
364 sta_id = iwl_prep_station(priv, addr, is_ap, ht_info);
365 if (sta_id == IWL_INVALID_STATION) {
366 IWL_ERR(priv, "Unable to prepare station %pM for addition\n",
367 addr);
368 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
369 return -EINVAL;
370 }
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800371
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800372 /*
373 * uCode is not able to deal with multiple requests to add a
374 * station. Keep track if one is in progress so that we do not send
375 * another.
376 */
377 if (priv->stations[sta_id].used & IWL_STA_UCODE_INPROGRESS) {
378 IWL_DEBUG_INFO(priv, "STA %d already in process of being added.\n",
379 sta_id);
380 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
381 return -EEXIST;
382 }
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800383
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800384 if ((priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE) &&
385 (priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE)) {
386 IWL_DEBUG_ASSOC(priv, "STA %d (%pM) already added, not adding again.\n",
387 sta_id, addr);
388 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
389 return -EEXIST;
390 }
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800391
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800392 priv->stations[sta_id].used |= IWL_STA_UCODE_INPROGRESS;
393 station = &priv->stations[sta_id];
394 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
395
396 /* Add station to device's station table */
397 ret = iwl_send_add_sta(priv, &station->sta, CMD_SYNC);
398 if (ret) {
399 IWL_ERR(priv, "Adding station %pM failed.\n", station->sta.sta.addr);
400 spin_lock_irqsave(&priv->sta_lock, flags_spin);
401 priv->stations[sta_id].used &= ~IWL_STA_DRIVER_ACTIVE;
402 priv->stations[sta_id].used &= ~IWL_STA_UCODE_INPROGRESS;
403 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
404 }
405 *sta_id_r = sta_id;
406 return ret;
407}
408EXPORT_SYMBOL(iwl_add_station_common);
409
Reinette Chatred2e210a2010-04-23 10:33:33 -0700410static struct iwl_link_quality_cmd *iwl_sta_init_lq(struct iwl_priv *priv,
Johannes Berg156b70d2010-04-28 14:44:45 -0700411 u8 sta_id)
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800412{
413 int i, r;
Reinette Chatred2e210a2010-04-23 10:33:33 -0700414 struct iwl_link_quality_cmd *link_cmd;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800415 u32 rate_flags;
Wey-Yi Guy57e40d32010-03-09 15:36:10 -0800416 int ret = 0;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800417
Reinette Chatred2e210a2010-04-23 10:33:33 -0700418 link_cmd = kzalloc(sizeof(struct iwl_link_quality_cmd), GFP_KERNEL);
419 if (!link_cmd) {
420 IWL_ERR(priv, "Unable to allocate memory for LQ cmd.\n");
421 return NULL;
422 }
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800423 /* Set up the rate scaling to start at selected rate, fall back
424 * all the way down to 1M in IEEE order, and then spin on 1M */
Johannes Berg156b70d2010-04-28 14:44:45 -0700425 if (priv->band == IEEE80211_BAND_5GHZ)
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800426 r = IWL_RATE_6M_INDEX;
427 else
428 r = IWL_RATE_1M_INDEX;
429
430 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
431 rate_flags = 0;
432 if (r >= IWL_FIRST_CCK_RATE && r <= IWL_LAST_CCK_RATE)
433 rate_flags |= RATE_MCS_CCK_MSK;
434
435 rate_flags |= first_antenna(priv->hw_params.valid_tx_ant) <<
436 RATE_MCS_ANT_POS;
437
Reinette Chatred2e210a2010-04-23 10:33:33 -0700438 link_cmd->rs_table[i].rate_n_flags =
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800439 iwl_hw_set_rate_n_flags(iwl_rates[r].plcp, rate_flags);
440 r = iwl_get_prev_ieee_rate(r);
441 }
442
Reinette Chatred2e210a2010-04-23 10:33:33 -0700443 link_cmd->general_params.single_stream_ant_msk =
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800444 first_antenna(priv->hw_params.valid_tx_ant);
Wey-Yi Guy3a23d692010-04-03 16:44:39 -0700445
Reinette Chatred2e210a2010-04-23 10:33:33 -0700446 link_cmd->general_params.dual_stream_ant_msk =
Wey-Yi Guy3a23d692010-04-03 16:44:39 -0700447 priv->hw_params.valid_tx_ant &
448 ~first_antenna(priv->hw_params.valid_tx_ant);
Reinette Chatred2e210a2010-04-23 10:33:33 -0700449 if (!link_cmd->general_params.dual_stream_ant_msk) {
450 link_cmd->general_params.dual_stream_ant_msk = ANT_AB;
Wey-Yi Guy3a23d692010-04-03 16:44:39 -0700451 } else if (num_of_ant(priv->hw_params.valid_tx_ant) == 2) {
Reinette Chatred2e210a2010-04-23 10:33:33 -0700452 link_cmd->general_params.dual_stream_ant_msk =
Wey-Yi Guy3a23d692010-04-03 16:44:39 -0700453 priv->hw_params.valid_tx_ant;
454 }
455
Reinette Chatred2e210a2010-04-23 10:33:33 -0700456 link_cmd->agg_params.agg_dis_start_th = LINK_QUAL_AGG_DISABLE_START_DEF;
457 link_cmd->agg_params.agg_time_limit =
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800458 cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
459
Johannes Berg156b70d2010-04-28 14:44:45 -0700460 link_cmd->sta_id = sta_id;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800461
Reinette Chatred2e210a2010-04-23 10:33:33 -0700462 ret = iwl_send_lq_cmd(priv, link_cmd, CMD_SYNC, true);
Wey-Yi Guy57e40d32010-03-09 15:36:10 -0800463 if (ret)
Reinette Chatred2e210a2010-04-23 10:33:33 -0700464 IWL_ERR(priv, "Link quality command failed (%d)\n", ret);
465
466 return link_cmd;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800467}
468
469/*
470 * iwl_add_local_stations - Add stations not requested by mac80211
471 *
472 * This will be either the broadcast station or the bssid station needed by
473 * ad-hoc.
474 *
475 * Function sleeps.
476 */
477int iwl_add_local_station(struct iwl_priv *priv, const u8 *addr, bool init_rs)
478{
479 int ret;
480 u8 sta_id;
Reinette Chatred2e210a2010-04-23 10:33:33 -0700481 struct iwl_link_quality_cmd *link_cmd;
482 unsigned long flags;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800483
484 ret = iwl_add_station_common(priv, addr, 0, NULL, &sta_id);
485 if (ret) {
486 IWL_ERR(priv, "Unable to add station %pM\n", addr);
487 return ret;
488 }
489
Reinette Chatred2e210a2010-04-23 10:33:33 -0700490 spin_lock_irqsave(&priv->sta_lock, flags);
491 priv->stations[sta_id].used |= IWL_STA_LOCAL;
492 spin_unlock_irqrestore(&priv->sta_lock, flags);
493
494 if (init_rs) {
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800495 /* Set up default rate scaling table in device's station table */
Johannes Berg156b70d2010-04-28 14:44:45 -0700496 link_cmd = iwl_sta_init_lq(priv, sta_id);
Reinette Chatred2e210a2010-04-23 10:33:33 -0700497 if (!link_cmd) {
498 IWL_ERR(priv, "Unable to initialize rate scaling for station %pM.\n",
499 addr);
500 return -ENOMEM;
501 }
502 spin_lock_irqsave(&priv->sta_lock, flags);
503 priv->stations[sta_id].lq = link_cmd;
504 spin_unlock_irqrestore(&priv->sta_lock, flags);
505 }
506
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800507 return 0;
508}
509EXPORT_SYMBOL(iwl_add_local_station);
510
511/**
512 * iwl_sta_ucode_deactivate - deactivate ucode status for a station
513 *
514 * priv->sta_lock must be held
515 */
516static void iwl_sta_ucode_deactivate(struct iwl_priv *priv, u8 sta_id)
517{
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800518 /* Ucode must be active and driver must be non active */
Reinette Chatred2e210a2010-04-23 10:33:33 -0700519 if ((priv->stations[sta_id].used &
520 (IWL_STA_UCODE_ACTIVE | IWL_STA_DRIVER_ACTIVE)) != IWL_STA_UCODE_ACTIVE)
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800521 IWL_ERR(priv, "removed non active STA %u\n", sta_id);
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800522
523 priv->stations[sta_id].used &= ~IWL_STA_UCODE_ACTIVE;
524
525 memset(&priv->stations[sta_id], 0, sizeof(struct iwl_station_entry));
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800526 IWL_DEBUG_ASSOC(priv, "Removed STA %u\n", sta_id);
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800527}
528
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800529static int iwl_send_remove_station(struct iwl_priv *priv,
530 struct iwl_station_entry *station)
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800531{
Zhu Yi2f301222009-10-09 17:19:45 +0800532 struct iwl_rx_packet *pkt;
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800533 int ret;
534
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800535 unsigned long flags_spin;
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800536 struct iwl_rem_sta_cmd rm_sta_cmd;
537
538 struct iwl_host_cmd cmd = {
539 .id = REPLY_REMOVE_STA,
540 .len = sizeof(struct iwl_rem_sta_cmd),
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800541 .flags = CMD_SYNC,
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800542 .data = &rm_sta_cmd,
543 };
544
545 memset(&rm_sta_cmd, 0, sizeof(rm_sta_cmd));
546 rm_sta_cmd.num_sta = 1;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800547 memcpy(&rm_sta_cmd.addr, &station->sta.sta.addr , ETH_ALEN);
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800548
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800549 cmd.flags |= CMD_WANT_SKB;
550
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800551 ret = iwl_send_cmd(priv, &cmd);
552
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800553 if (ret)
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800554 return ret;
555
Zhu Yi2f301222009-10-09 17:19:45 +0800556 pkt = (struct iwl_rx_packet *)cmd.reply_page;
557 if (pkt->hdr.flags & IWL_CMD_FAILED_MSK) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800558 IWL_ERR(priv, "Bad return from REPLY_REMOVE_STA (0x%08X)\n",
Zhu Yi2f301222009-10-09 17:19:45 +0800559 pkt->hdr.flags);
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800560 ret = -EIO;
561 }
562
563 if (!ret) {
Zhu Yi2f301222009-10-09 17:19:45 +0800564 switch (pkt->u.rem_sta.status) {
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800565 case REM_STA_SUCCESS_MSK:
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800566 spin_lock_irqsave(&priv->sta_lock, flags_spin);
567 iwl_sta_ucode_deactivate(priv, station->sta.sta.sta_id);
568 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
Tomas Winklere1623442009-01-27 14:27:56 -0800569 IWL_DEBUG_ASSOC(priv, "REPLY_REMOVE_STA PASSED\n");
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800570 break;
571 default:
572 ret = -EIO;
Winkler, Tomas15b16872008-12-19 10:37:33 +0800573 IWL_ERR(priv, "REPLY_REMOVE_STA failed\n");
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800574 break;
575 }
576 }
Zhu Yi64a76b52009-12-10 14:37:21 -0800577 iwl_free_pages(priv, cmd.reply_page);
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800578
579 return ret;
580}
Emmanuel Grumbachbe1f3ab62008-06-12 09:47:18 +0800581
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800582/**
583 * iwl_remove_station - Remove driver's knowledge of station.
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800584 */
Johannes Berg1fa61b22010-04-28 08:44:52 -0700585int iwl_remove_station(struct iwl_priv *priv, const u8 *addr)
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800586{
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800587 int sta_id = IWL_INVALID_STATION;
588 int i, ret = -EINVAL;
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800589 unsigned long flags;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800590 bool is_ap = priv->iw_mode == NL80211_IFTYPE_STATION;
591 struct iwl_station_entry *station;
592
593 if (!iwl_is_ready(priv)) {
594 IWL_DEBUG_INFO(priv,
Frans Pop91dd6c22010-03-24 14:19:58 -0700595 "Unable to remove station %pM, device not ready.\n",
Johannes Bergc0222df2010-04-28 08:44:51 -0700596 addr);
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800597 /*
598 * It is typical for stations to be removed when we are
599 * going down. Return success since device will be down
600 * soon anyway
601 */
602 return 0;
603 }
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800604
605 spin_lock_irqsave(&priv->sta_lock, flags);
606
607 if (is_ap)
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800608 sta_id = IWL_AP_ID;
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800609 else
610 for (i = IWL_STA_ID; i < priv->hw_params.max_stations; i++)
611 if (priv->stations[i].used &&
612 !compare_ether_addr(priv->stations[i].sta.sta.addr,
Johannes Bergc0222df2010-04-28 08:44:51 -0700613 addr)) {
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800614 sta_id = i;
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800615 break;
616 }
617
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800618 if (unlikely(sta_id == IWL_INVALID_STATION))
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800619 goto out;
620
Tomas Winklere1623442009-01-27 14:27:56 -0800621 IWL_DEBUG_ASSOC(priv, "Removing STA from driver:%d %pM\n",
Johannes Bergc0222df2010-04-28 08:44:51 -0700622 sta_id, addr);
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800623
624 if (!(priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE)) {
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800625 IWL_DEBUG_INFO(priv, "Removing %pM but non DRIVER active\n",
Johannes Bergc0222df2010-04-28 08:44:51 -0700626 addr);
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800627 goto out;
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800628 }
629
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800630 if (!(priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE)) {
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800631 IWL_DEBUG_INFO(priv, "Removing %pM but non UCODE active\n",
Johannes Bergc0222df2010-04-28 08:44:51 -0700632 addr);
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800633 goto out;
634 }
635
Johannes Berg1fa61b22010-04-28 08:44:52 -0700636 if (priv->stations[sta_id].used & IWL_STA_LOCAL) {
637 kfree(priv->stations[sta_id].lq);
638 priv->stations[sta_id].lq = NULL;
639 }
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800640
641 priv->stations[sta_id].used &= ~IWL_STA_DRIVER_ACTIVE;
642
643 priv->num_stations--;
644
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800645 BUG_ON(priv->num_stations < 0);
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800646
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800647 station = &priv->stations[sta_id];
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800648 spin_unlock_irqrestore(&priv->sta_lock, flags);
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800649
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800650 ret = iwl_send_remove_station(priv, station);
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800651 return ret;
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800652out:
653 spin_unlock_irqrestore(&priv->sta_lock, flags);
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800654 return ret;
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800655}
Johannes Berg1fa61b22010-04-28 08:44:52 -0700656EXPORT_SYMBOL_GPL(iwl_remove_station);
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800657
Winkler, Tomas83dde8c2008-11-19 15:32:23 -0800658/**
Reinette Chatre7e246192010-02-18 22:58:32 -0800659 * iwl_clear_ucode_stations() - clear entire station table driver and/or ucode
660 * @priv:
661 * @force: If set then the uCode station table needs to be cleared here. If
662 * not set then the uCode station table has already been cleared,
663 * for example after sending it a RXON command without ASSOC bit
664 * set, and we just need to change driver state here.
Winkler, Tomas83dde8c2008-11-19 15:32:23 -0800665 */
Reinette Chatre7e246192010-02-18 22:58:32 -0800666void iwl_clear_ucode_stations(struct iwl_priv *priv, bool force)
Winkler, Tomas83dde8c2008-11-19 15:32:23 -0800667{
Mohamed Abbas48676eb2009-03-11 11:17:59 -0700668 int i;
Reinette Chatre7e246192010-02-18 22:58:32 -0800669 unsigned long flags_spin;
670 bool cleared = false;
Winkler, Tomas83dde8c2008-11-19 15:32:23 -0800671
Reinette Chatre7e246192010-02-18 22:58:32 -0800672 IWL_DEBUG_INFO(priv, "Clearing ucode stations in driver%s\n",
673 force ? " and ucode" : "");
Winkler, Tomas83dde8c2008-11-19 15:32:23 -0800674
Reinette Chatre7e246192010-02-18 22:58:32 -0800675 if (force) {
676 if (!iwl_is_ready(priv)) {
677 /*
678 * If device is not ready at this point the station
679 * table is likely already empty (uCode not ready
680 * to receive station requests) or will soon be
681 * due to interface going down.
682 */
683 IWL_DEBUG_INFO(priv, "Unable to remove stations from device - device not ready\n");
684 } else {
685 iwl_send_cmd_pdu_async(priv, REPLY_REMOVE_ALL_STA, 0, NULL, NULL);
686 }
Mohamed Abbas48676eb2009-03-11 11:17:59 -0700687 }
688
Reinette Chatre7e246192010-02-18 22:58:32 -0800689 spin_lock_irqsave(&priv->sta_lock, flags_spin);
690 if (force) {
691 IWL_DEBUG_INFO(priv, "Clearing all station information in driver\n");
Reinette Chatred2e210a2010-04-23 10:33:33 -0700692 /*
693 * The station entry contains a link to the LQ command. For
694 * all stations managed by mac80211 this memory will be
695 * managed by it also. For local stations (broadcast and
696 * bssid station when in adhoc mode) we need to maintain
697 * this lq command separately. This memory is created when
698 * these stations are added.
699 */
700 for (i = 0; i < priv->hw_params.max_stations; i++) {
701 if (priv->stations[i].used & IWL_STA_LOCAL) {
702 kfree(priv->stations[i].lq);
703 priv->stations[i].lq = NULL;
704 }
705 }
Reinette Chatre7e246192010-02-18 22:58:32 -0800706 priv->num_stations = 0;
707 memset(priv->stations, 0, sizeof(priv->stations));
Reinette Chatred2e210a2010-04-23 10:33:33 -0700708 cleared = true;
Reinette Chatre7e246192010-02-18 22:58:32 -0800709 } else {
710 for (i = 0; i < priv->hw_params.max_stations; i++) {
711 if (priv->stations[i].used & IWL_STA_UCODE_ACTIVE) {
Frans Pop91dd6c22010-03-24 14:19:58 -0700712 IWL_DEBUG_INFO(priv, "Clearing ucode active for station %d\n", i);
Reinette Chatre7e246192010-02-18 22:58:32 -0800713 priv->stations[i].used &= ~IWL_STA_UCODE_ACTIVE;
714 cleared = true;
715 }
716 }
717 }
718 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
719
720 if (!cleared)
721 IWL_DEBUG_INFO(priv, "No active stations found to be cleared\n");
Winkler, Tomas83dde8c2008-11-19 15:32:23 -0800722}
Reinette Chatre7e246192010-02-18 22:58:32 -0800723EXPORT_SYMBOL(iwl_clear_ucode_stations);
724
725/**
726 * iwl_restore_stations() - Restore driver known stations to device
727 *
728 * All stations considered active by driver, but not present in ucode, is
729 * restored.
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800730 *
731 * Function sleeps.
Reinette Chatre7e246192010-02-18 22:58:32 -0800732 */
733void iwl_restore_stations(struct iwl_priv *priv)
734{
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800735 struct iwl_station_entry *station;
Reinette Chatre7e246192010-02-18 22:58:32 -0800736 unsigned long flags_spin;
737 int i;
738 bool found = false;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800739 int ret;
Reinette Chatre7e246192010-02-18 22:58:32 -0800740
741 if (!iwl_is_ready(priv)) {
742 IWL_DEBUG_INFO(priv, "Not ready yet, not restoring any stations.\n");
743 return;
744 }
745
746 IWL_DEBUG_ASSOC(priv, "Restoring all known stations ... start.\n");
747 spin_lock_irqsave(&priv->sta_lock, flags_spin);
748 for (i = 0; i < priv->hw_params.max_stations; i++) {
749 if ((priv->stations[i].used & IWL_STA_DRIVER_ACTIVE) &&
750 !(priv->stations[i].used & IWL_STA_UCODE_ACTIVE)) {
751 IWL_DEBUG_ASSOC(priv, "Restoring sta %pM\n",
752 priv->stations[i].sta.sta.addr);
753 priv->stations[i].sta.mode = 0;
754 priv->stations[i].used |= IWL_STA_UCODE_INPROGRESS;
755 found = true;
756 }
757 }
758
759 for (i = 0; i < priv->hw_params.max_stations; i++) {
760 if ((priv->stations[i].used & IWL_STA_UCODE_INPROGRESS)) {
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800761 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
762 station = &priv->stations[i];
763 ret = iwl_send_add_sta(priv, &priv->stations[i].sta, CMD_SYNC);
764 if (ret) {
765 IWL_ERR(priv, "Adding station %pM failed.\n",
766 station->sta.sta.addr);
767 spin_lock_irqsave(&priv->sta_lock, flags_spin);
768 priv->stations[i].used &= ~IWL_STA_DRIVER_ACTIVE;
769 priv->stations[i].used &= ~IWL_STA_UCODE_INPROGRESS;
770 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
771 }
772 /*
773 * Rate scaling has already been initialized, send
774 * current LQ command
775 */
776 if (station->lq)
777 iwl_send_lq_cmd(priv, station->lq, CMD_SYNC, true);
778 spin_lock_irqsave(&priv->sta_lock, flags_spin);
Reinette Chatre7e246192010-02-18 22:58:32 -0800779 priv->stations[i].used &= ~IWL_STA_UCODE_INPROGRESS;
780 }
781 }
782
783 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
784 if (!found)
785 IWL_DEBUG_INFO(priv, "Restoring all known stations .... no stations to be restored.\n");
786 else
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800787 IWL_DEBUG_INFO(priv, "Restoring all known stations .... complete.\n");
Reinette Chatre7e246192010-02-18 22:58:32 -0800788}
789EXPORT_SYMBOL(iwl_restore_stations);
Winkler, Tomas83dde8c2008-11-19 15:32:23 -0800790
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800791int iwl_get_free_ucode_key_index(struct iwl_priv *priv)
Emmanuel Grumbach80fb47a2008-04-14 21:16:08 -0700792{
793 int i;
794
795 for (i = 0; i < STA_KEY_MAX_NUM; i++)
Emmanuel Grumbach77bab602008-04-15 16:01:44 -0700796 if (!test_and_set_bit(i, &priv->ucode_key_table))
Emmanuel Grumbach80fb47a2008-04-14 21:16:08 -0700797 return i;
798
Tomas Winkler40a9a822008-11-25 23:29:03 +0200799 return WEP_INVALID_OFFSET;
Emmanuel Grumbach80fb47a2008-04-14 21:16:08 -0700800}
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800801EXPORT_SYMBOL(iwl_get_free_ucode_key_index);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700802
Johannes Berg335348b2010-03-30 10:11:46 -0700803static int iwl_send_static_wepkey_cmd(struct iwl_priv *priv, u8 send_if_empty)
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700804{
805 int i, not_empty = 0;
806 u8 buff[sizeof(struct iwl_wep_cmd) +
807 sizeof(struct iwl_wep_key) * WEP_KEYS_MAX];
808 struct iwl_wep_cmd *wep_cmd = (struct iwl_wep_cmd *)buff;
809 size_t cmd_size = sizeof(struct iwl_wep_cmd);
810 struct iwl_host_cmd cmd = {
811 .id = REPLY_WEPKEY,
812 .data = wep_cmd,
Johannes Berg72e15d72010-02-19 11:42:32 -0800813 .flags = CMD_SYNC,
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700814 };
815
Johannes Berg72e15d72010-02-19 11:42:32 -0800816 might_sleep();
817
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700818 memset(wep_cmd, 0, cmd_size +
819 (sizeof(struct iwl_wep_key) * WEP_KEYS_MAX));
820
821 for (i = 0; i < WEP_KEYS_MAX ; i++) {
822 wep_cmd->key[i].key_index = i;
823 if (priv->wep_keys[i].key_size) {
824 wep_cmd->key[i].key_offset = i;
825 not_empty = 1;
826 } else {
827 wep_cmd->key[i].key_offset = WEP_INVALID_OFFSET;
828 }
829
830 wep_cmd->key[i].key_size = priv->wep_keys[i].key_size;
831 memcpy(&wep_cmd->key[i].key[3], priv->wep_keys[i].key,
832 priv->wep_keys[i].key_size);
833 }
834
835 wep_cmd->global_key_type = WEP_KEY_WEP_TYPE;
836 wep_cmd->num_keys = WEP_KEYS_MAX;
837
838 cmd_size += sizeof(struct iwl_wep_key) * WEP_KEYS_MAX;
839
840 cmd.len = cmd_size;
841
842 if (not_empty || send_if_empty)
843 return iwl_send_cmd(priv, &cmd);
844 else
845 return 0;
846}
Johannes Berg335348b2010-03-30 10:11:46 -0700847
848int iwl_restore_default_wep_keys(struct iwl_priv *priv)
849{
850 WARN_ON(!mutex_is_locked(&priv->mutex));
851
852 return iwl_send_static_wepkey_cmd(priv, 0);
853}
854EXPORT_SYMBOL(iwl_restore_default_wep_keys);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700855
856int iwl_remove_default_wep_key(struct iwl_priv *priv,
Emmanuel Grumbach80fb47a2008-04-14 21:16:08 -0700857 struct ieee80211_key_conf *keyconf)
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700858{
859 int ret;
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700860
Johannes Berg72e15d72010-02-19 11:42:32 -0800861 WARN_ON(!mutex_is_locked(&priv->mutex));
862
Reinette Chatre2d1bb9e2009-07-17 09:30:18 -0700863 IWL_DEBUG_WEP(priv, "Removing default WEP key: idx=%d\n",
864 keyconf->keyidx);
Emmanuel Grumbach80fb47a2008-04-14 21:16:08 -0700865
Emmanuel Grumbach80fb47a2008-04-14 21:16:08 -0700866 memset(&priv->wep_keys[keyconf->keyidx], 0, sizeof(priv->wep_keys[0]));
Reinette Chatre2d1bb9e2009-07-17 09:30:18 -0700867 if (iwl_is_rfkill(priv)) {
868 IWL_DEBUG_WEP(priv, "Not sending REPLY_WEPKEY command due to RFKILL.\n");
Johannes Berg72e15d72010-02-19 11:42:32 -0800869 /* but keys in device are clear anyway so return success */
Reinette Chatre2d1bb9e2009-07-17 09:30:18 -0700870 return 0;
871 }
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700872 ret = iwl_send_static_wepkey_cmd(priv, 1);
Tomas Winklere1623442009-01-27 14:27:56 -0800873 IWL_DEBUG_WEP(priv, "Remove default WEP key: idx=%d ret=%d\n",
Emmanuel Grumbach4564ce82008-06-12 09:47:09 +0800874 keyconf->keyidx, ret);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700875
876 return ret;
877}
Tomas Winkler27aaba02008-05-05 10:22:44 +0800878EXPORT_SYMBOL(iwl_remove_default_wep_key);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700879
880int iwl_set_default_wep_key(struct iwl_priv *priv,
881 struct ieee80211_key_conf *keyconf)
882{
883 int ret;
Johannes Berg72e15d72010-02-19 11:42:32 -0800884
885 WARN_ON(!mutex_is_locked(&priv->mutex));
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700886
Emmanuel Grumbach4564ce82008-06-12 09:47:09 +0800887 if (keyconf->keylen != WEP_KEY_LEN_128 &&
888 keyconf->keylen != WEP_KEY_LEN_64) {
Tomas Winklere1623442009-01-27 14:27:56 -0800889 IWL_DEBUG_WEP(priv, "Bad WEP key length %d\n", keyconf->keylen);
Emmanuel Grumbach4564ce82008-06-12 09:47:09 +0800890 return -EINVAL;
891 }
892
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700893 keyconf->flags &= ~IEEE80211_KEY_FLAG_GENERATE_IV;
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +0800894 keyconf->hw_key_idx = HW_KEY_DEFAULT;
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700895 priv->stations[IWL_AP_ID].keyinfo.alg = ALG_WEP;
896
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700897 priv->wep_keys[keyconf->keyidx].key_size = keyconf->keylen;
898 memcpy(&priv->wep_keys[keyconf->keyidx].key, &keyconf->key,
899 keyconf->keylen);
900
901 ret = iwl_send_static_wepkey_cmd(priv, 0);
Tomas Winklere1623442009-01-27 14:27:56 -0800902 IWL_DEBUG_WEP(priv, "Set default WEP key: len=%d idx=%d ret=%d\n",
Emmanuel Grumbach4564ce82008-06-12 09:47:09 +0800903 keyconf->keylen, keyconf->keyidx, ret);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700904
905 return ret;
906}
Tomas Winkler27aaba02008-05-05 10:22:44 +0800907EXPORT_SYMBOL(iwl_set_default_wep_key);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700908
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700909static int iwl_set_wep_dynamic_key_info(struct iwl_priv *priv,
Emmanuel Grumbach0211ddd2008-04-14 21:16:07 -0700910 struct ieee80211_key_conf *keyconf,
911 u8 sta_id)
912{
913 unsigned long flags;
914 __le16 key_flags = 0;
915 int ret;
916
917 keyconf->flags &= ~IEEE80211_KEY_FLAG_GENERATE_IV;
Emmanuel Grumbach0211ddd2008-04-14 21:16:07 -0700918
919 key_flags |= (STA_KEY_FLG_WEP | STA_KEY_FLG_MAP_KEY_MSK);
920 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
921 key_flags &= ~STA_KEY_FLG_INVALID;
922
923 if (keyconf->keylen == WEP_KEY_LEN_128)
924 key_flags |= STA_KEY_FLG_KEY_SIZE_MSK;
925
Tomas Winkler5425e492008-04-15 16:01:38 -0700926 if (sta_id == priv->hw_params.bcast_sta_id)
Emmanuel Grumbach0211ddd2008-04-14 21:16:07 -0700927 key_flags |= STA_KEY_MULTICAST_MSK;
928
929 spin_lock_irqsave(&priv->sta_lock, flags);
930
931 priv->stations[sta_id].keyinfo.alg = keyconf->alg;
932 priv->stations[sta_id].keyinfo.keylen = keyconf->keylen;
933 priv->stations[sta_id].keyinfo.keyidx = keyconf->keyidx;
934
935 memcpy(priv->stations[sta_id].keyinfo.key,
936 keyconf->key, keyconf->keylen);
937
938 memcpy(&priv->stations[sta_id].sta.key.key[3],
939 keyconf->key, keyconf->keylen);
940
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -0700941 if ((priv->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
942 == STA_KEY_FLG_NO_ENC)
943 priv->stations[sta_id].sta.key.key_offset =
Emmanuel Grumbach80fb47a2008-04-14 21:16:08 -0700944 iwl_get_free_ucode_key_index(priv);
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -0700945 /* else, we are overriding an existing key => no need to allocated room
946 * in uCode. */
Emmanuel Grumbach0211ddd2008-04-14 21:16:07 -0700947
Tomas Winkler40a9a822008-11-25 23:29:03 +0200948 WARN(priv->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
Jiri Slabye724b8f2009-01-05 17:06:06 +0100949 "no space for a new key");
Tomas Winkler40a9a822008-11-25 23:29:03 +0200950
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -0700951 priv->stations[sta_id].sta.key.key_flags = key_flags;
Emmanuel Grumbach0211ddd2008-04-14 21:16:07 -0700952 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
953 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
954
Tomas Winkler133636d2008-05-05 10:22:34 +0800955 ret = iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
Emmanuel Grumbach0211ddd2008-04-14 21:16:07 -0700956
957 spin_unlock_irqrestore(&priv->sta_lock, flags);
958
959 return ret;
960}
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700961
962static int iwl_set_ccmp_dynamic_key_info(struct iwl_priv *priv,
963 struct ieee80211_key_conf *keyconf,
964 u8 sta_id)
965{
966 unsigned long flags;
967 __le16 key_flags = 0;
Tomas Winkler40a9a822008-11-25 23:29:03 +0200968 int ret;
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700969
970 key_flags |= (STA_KEY_FLG_CCMP | STA_KEY_FLG_MAP_KEY_MSK);
971 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
972 key_flags &= ~STA_KEY_FLG_INVALID;
973
Tomas Winkler5425e492008-04-15 16:01:38 -0700974 if (sta_id == priv->hw_params.bcast_sta_id)
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700975 key_flags |= STA_KEY_MULTICAST_MSK;
976
977 keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700978
979 spin_lock_irqsave(&priv->sta_lock, flags);
980 priv->stations[sta_id].keyinfo.alg = keyconf->alg;
981 priv->stations[sta_id].keyinfo.keylen = keyconf->keylen;
982
983 memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key,
984 keyconf->keylen);
985
986 memcpy(priv->stations[sta_id].sta.key.key, keyconf->key,
987 keyconf->keylen);
988
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -0700989 if ((priv->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
990 == STA_KEY_FLG_NO_ENC)
991 priv->stations[sta_id].sta.key.key_offset =
992 iwl_get_free_ucode_key_index(priv);
993 /* else, we are overriding an existing key => no need to allocated room
994 * in uCode. */
995
Tomas Winkler40a9a822008-11-25 23:29:03 +0200996 WARN(priv->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
Jiri Slabye724b8f2009-01-05 17:06:06 +0100997 "no space for a new key");
Tomas Winkler40a9a822008-11-25 23:29:03 +0200998
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700999 priv->stations[sta_id].sta.key.key_flags = key_flags;
1000 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
1001 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1002
Tomas Winkler40a9a822008-11-25 23:29:03 +02001003 ret = iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
1004
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001005 spin_unlock_irqrestore(&priv->sta_lock, flags);
1006
Tomas Winkler40a9a822008-11-25 23:29:03 +02001007 return ret;
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001008}
1009
1010static int iwl_set_tkip_dynamic_key_info(struct iwl_priv *priv,
1011 struct ieee80211_key_conf *keyconf,
1012 u8 sta_id)
1013{
1014 unsigned long flags;
1015 int ret = 0;
Reinette Chatre299f5462009-04-30 13:56:31 -07001016 __le16 key_flags = 0;
1017
1018 key_flags |= (STA_KEY_FLG_TKIP | STA_KEY_FLG_MAP_KEY_MSK);
1019 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
1020 key_flags &= ~STA_KEY_FLG_INVALID;
1021
1022 if (sta_id == priv->hw_params.bcast_sta_id)
1023 key_flags |= STA_KEY_MULTICAST_MSK;
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001024
1025 keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
1026 keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001027
1028 spin_lock_irqsave(&priv->sta_lock, flags);
1029
1030 priv->stations[sta_id].keyinfo.alg = keyconf->alg;
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001031 priv->stations[sta_id].keyinfo.keylen = 16;
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -07001032
1033 if ((priv->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
1034 == STA_KEY_FLG_NO_ENC)
1035 priv->stations[sta_id].sta.key.key_offset =
Emmanuel Grumbach77bab602008-04-15 16:01:44 -07001036 iwl_get_free_ucode_key_index(priv);
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -07001037 /* else, we are overriding an existing key => no need to allocated room
1038 * in uCode. */
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001039
Tomas Winkler40a9a822008-11-25 23:29:03 +02001040 WARN(priv->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
Jiri Slabye724b8f2009-01-05 17:06:06 +01001041 "no space for a new key");
Tomas Winkler40a9a822008-11-25 23:29:03 +02001042
Reinette Chatre299f5462009-04-30 13:56:31 -07001043 priv->stations[sta_id].sta.key.key_flags = key_flags;
1044
1045
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001046 /* This copy is acutally not needed: we get the key with each TX */
1047 memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key, 16);
1048
1049 memcpy(priv->stations[sta_id].sta.key.key, keyconf->key, 16);
1050
1051 spin_unlock_irqrestore(&priv->sta_lock, flags);
1052
1053 return ret;
1054}
1055
Tomas Winkler9f586712008-11-12 13:14:05 -08001056void iwl_update_tkip_key(struct iwl_priv *priv,
1057 struct ieee80211_key_conf *keyconf,
1058 const u8 *addr, u32 iv32, u16 *phase1key)
1059{
1060 u8 sta_id = IWL_INVALID_STATION;
1061 unsigned long flags;
Tomas Winkler9f586712008-11-12 13:14:05 -08001062 int i;
Tomas Winkler9f586712008-11-12 13:14:05 -08001063
Tomas Winklerc587de02009-06-03 11:44:07 -07001064 sta_id = iwl_find_station(priv, addr);
Tomas Winkler9f586712008-11-12 13:14:05 -08001065 if (sta_id == IWL_INVALID_STATION) {
Tomas Winklere1623442009-01-27 14:27:56 -08001066 IWL_DEBUG_MAC80211(priv, "leave - %pM not in station map.\n",
Tomas Winkler9f586712008-11-12 13:14:05 -08001067 addr);
1068 return;
1069 }
1070
1071 if (iwl_scan_cancel(priv)) {
1072 /* cancel scan failed, just live w/ bad key and rely
1073 briefly on SW decryption */
1074 return;
1075 }
1076
Tomas Winkler9f586712008-11-12 13:14:05 -08001077 spin_lock_irqsave(&priv->sta_lock, flags);
1078
Tomas Winkler9f586712008-11-12 13:14:05 -08001079 priv->stations[sta_id].sta.key.tkip_rx_tsc_byte2 = (u8) iv32;
1080
1081 for (i = 0; i < 5; i++)
1082 priv->stations[sta_id].sta.key.tkip_rx_ttak[i] =
1083 cpu_to_le16(phase1key[i]);
1084
1085 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
1086 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1087
1088 iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
1089
1090 spin_unlock_irqrestore(&priv->sta_lock, flags);
1091
1092}
1093EXPORT_SYMBOL(iwl_update_tkip_key);
1094
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -07001095int iwl_remove_dynamic_key(struct iwl_priv *priv,
1096 struct ieee80211_key_conf *keyconf,
1097 u8 sta_id)
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001098{
1099 unsigned long flags;
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -07001100 int ret = 0;
1101 u16 key_flags;
1102 u8 keyidx;
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001103
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +08001104 priv->key_mapping_key--;
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001105
1106 spin_lock_irqsave(&priv->sta_lock, flags);
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -07001107 key_flags = le16_to_cpu(priv->stations[sta_id].sta.key.key_flags);
1108 keyidx = (key_flags >> STA_KEY_FLG_KEYID_POS) & 0x3;
1109
Tomas Winklere1623442009-01-27 14:27:56 -08001110 IWL_DEBUG_WEP(priv, "Remove dynamic key: idx=%d sta=%d\n",
Emmanuel Grumbach4564ce82008-06-12 09:47:09 +08001111 keyconf->keyidx, sta_id);
1112
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -07001113 if (keyconf->keyidx != keyidx) {
1114 /* We need to remove a key with index different that the one
1115 * in the uCode. This means that the key we need to remove has
1116 * been replaced by another one with different index.
1117 * Don't do anything and return ok
1118 */
1119 spin_unlock_irqrestore(&priv->sta_lock, flags);
1120 return 0;
1121 }
1122
Tomas Winkler40a9a822008-11-25 23:29:03 +02001123 if (priv->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08001124 IWL_WARN(priv, "Removing wrong key %d 0x%x\n",
Tomas Winkler40a9a822008-11-25 23:29:03 +02001125 keyconf->keyidx, key_flags);
1126 spin_unlock_irqrestore(&priv->sta_lock, flags);
1127 return 0;
1128 }
1129
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001130 if (!test_and_clear_bit(priv->stations[sta_id].sta.key.key_offset,
1131 &priv->ucode_key_table))
Winkler, Tomas15b16872008-12-19 10:37:33 +08001132 IWL_ERR(priv, "index %d not used in uCode key table.\n",
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001133 priv->stations[sta_id].sta.key.key_offset);
1134 memset(&priv->stations[sta_id].keyinfo, 0,
Tomas Winkler6def9762008-05-05 10:22:31 +08001135 sizeof(struct iwl_hw_key));
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001136 memset(&priv->stations[sta_id].sta.key, 0,
1137 sizeof(struct iwl4965_keyinfo));
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -07001138 priv->stations[sta_id].sta.key.key_flags =
1139 STA_KEY_FLG_NO_ENC | STA_KEY_FLG_INVALID;
1140 priv->stations[sta_id].sta.key.key_offset = WEP_INVALID_OFFSET;
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001141 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
1142 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001143
Reinette Chatre2d1bb9e2009-07-17 09:30:18 -07001144 if (iwl_is_rfkill(priv)) {
Frans Pop91dd6c22010-03-24 14:19:58 -07001145 IWL_DEBUG_WEP(priv, "Not sending REPLY_ADD_STA command because RFKILL enabled.\n");
Reinette Chatre2d1bb9e2009-07-17 09:30:18 -07001146 spin_unlock_irqrestore(&priv->sta_lock, flags);
1147 return 0;
1148 }
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +08001149 ret = iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -07001150 spin_unlock_irqrestore(&priv->sta_lock, flags);
1151 return ret;
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001152}
Tomas Winkler27aaba02008-05-05 10:22:44 +08001153EXPORT_SYMBOL(iwl_remove_dynamic_key);
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001154
1155int iwl_set_dynamic_key(struct iwl_priv *priv,
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +08001156 struct ieee80211_key_conf *keyconf, u8 sta_id)
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001157{
1158 int ret;
1159
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +08001160 priv->key_mapping_key++;
1161 keyconf->hw_key_idx = HW_KEY_DYNAMIC;
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001162
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +08001163 switch (keyconf->alg) {
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001164 case ALG_CCMP:
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +08001165 ret = iwl_set_ccmp_dynamic_key_info(priv, keyconf, sta_id);
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001166 break;
1167 case ALG_TKIP:
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +08001168 ret = iwl_set_tkip_dynamic_key_info(priv, keyconf, sta_id);
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001169 break;
1170 case ALG_WEP:
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +08001171 ret = iwl_set_wep_dynamic_key_info(priv, keyconf, sta_id);
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001172 break;
1173 default:
Winkler, Tomas15b16872008-12-19 10:37:33 +08001174 IWL_ERR(priv,
1175 "Unknown alg: %s alg = %d\n", __func__, keyconf->alg);
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001176 ret = -EINVAL;
1177 }
1178
Tomas Winklere1623442009-01-27 14:27:56 -08001179 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 +08001180 keyconf->alg, keyconf->keylen, keyconf->keyidx,
1181 sta_id, ret);
1182
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001183 return ret;
1184}
Tomas Winkler27aaba02008-05-05 10:22:44 +08001185EXPORT_SYMBOL(iwl_set_dynamic_key);
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001186
Tomas Winkler66c73db2008-04-15 16:01:40 -07001187#ifdef CONFIG_IWLWIFI_DEBUG
1188static void iwl_dump_lq_cmd(struct iwl_priv *priv,
1189 struct iwl_link_quality_cmd *lq)
1190{
1191 int i;
Tomas Winklere1623442009-01-27 14:27:56 -08001192 IWL_DEBUG_RATE(priv, "lq station id 0x%x\n", lq->sta_id);
1193 IWL_DEBUG_RATE(priv, "lq ant 0x%X 0x%X\n",
Tomas Winkler66c73db2008-04-15 16:01:40 -07001194 lq->general_params.single_stream_ant_msk,
1195 lq->general_params.dual_stream_ant_msk);
1196
1197 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
Tomas Winklere1623442009-01-27 14:27:56 -08001198 IWL_DEBUG_RATE(priv, "lq index %d 0x%X\n",
Tomas Winkler66c73db2008-04-15 16:01:40 -07001199 i, lq->rs_table[i].rate_n_flags);
1200}
1201#else
1202static inline void iwl_dump_lq_cmd(struct iwl_priv *priv,
1203 struct iwl_link_quality_cmd *lq)
1204{
1205}
1206#endif
1207
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08001208/**
1209 * iwl_send_lq_cmd() - Send link quality command
1210 * @init: This command is sent as part of station initialization right
1211 * after station has been added.
1212 *
1213 * The link quality command is sent as the last step of station creation.
1214 * This is the special case in which init is set and we call a callback in
1215 * this case to clear the state indicating that station creation is in
1216 * progress.
1217 */
Tomas Winkler66c73db2008-04-15 16:01:40 -07001218int iwl_send_lq_cmd(struct iwl_priv *priv,
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08001219 struct iwl_link_quality_cmd *lq, u8 flags, bool init)
Tomas Winkler66c73db2008-04-15 16:01:40 -07001220{
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08001221 int ret = 0;
1222 unsigned long flags_spin;
1223
Tomas Winkler66c73db2008-04-15 16:01:40 -07001224 struct iwl_host_cmd cmd = {
1225 .id = REPLY_TX_LINK_QUALITY_CMD,
1226 .len = sizeof(struct iwl_link_quality_cmd),
Johannes Bergc2acea82009-07-24 11:13:05 -07001227 .flags = flags,
Tomas Winkler66c73db2008-04-15 16:01:40 -07001228 .data = lq,
1229 };
1230
Johannes Berg76c3c692010-03-30 02:46:29 -07001231 if (WARN_ON(lq->sta_id == IWL_INVALID_STATION))
Tomas Winkler66c73db2008-04-15 16:01:40 -07001232 return -EINVAL;
1233
Tomas Winkler3ac7f142008-07-21 02:40:14 +03001234 iwl_dump_lq_cmd(priv, lq);
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08001235 BUG_ON(init && (cmd.flags & CMD_ASYNC));
Tomas Winkler66c73db2008-04-15 16:01:40 -07001236
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08001237 ret = iwl_send_cmd(priv, &cmd);
Reinette Chatred2e210a2010-04-23 10:33:33 -07001238
1239 if (cmd.flags & CMD_ASYNC)
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08001240 return ret;
Tomas Winkler66c73db2008-04-15 16:01:40 -07001241
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08001242 if (init) {
Frans Pop91dd6c22010-03-24 14:19:58 -07001243 IWL_DEBUG_INFO(priv, "init LQ command complete, clearing sta addition status for sta %d\n",
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08001244 lq->sta_id);
1245 spin_lock_irqsave(&priv->sta_lock, flags_spin);
1246 priv->stations[lq->sta_id].used &= ~IWL_STA_UCODE_INPROGRESS;
1247 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
1248 }
Reinette Chatred2e210a2010-04-23 10:33:33 -07001249 return ret;
Tomas Winkler66c73db2008-04-15 16:01:40 -07001250}
1251EXPORT_SYMBOL(iwl_send_lq_cmd);
1252
Tomas Winkler4f40e4d2008-05-15 13:54:04 +08001253/**
Reinette Chatre9a9ca652009-10-30 14:36:12 -07001254 * iwl_add_bcast_station - add broadcast station into station table.
1255 */
Reinette Chatred2e210a2010-04-23 10:33:33 -07001256int iwl_add_bcast_station(struct iwl_priv *priv)
Reinette Chatre9a9ca652009-10-30 14:36:12 -07001257{
Reinette Chatre3459ab52010-01-22 14:22:49 -08001258 IWL_DEBUG_INFO(priv, "Adding broadcast station to station table\n");
Reinette Chatred2e210a2010-04-23 10:33:33 -07001259 return iwl_add_local_station(priv, iwl_bcast_addr, true);
Reinette Chatre9a9ca652009-10-30 14:36:12 -07001260}
1261EXPORT_SYMBOL(iwl_add_bcast_station);
1262
1263/**
Reinette Chatre3459ab52010-01-22 14:22:49 -08001264 * iwl3945_add_bcast_station - add broadcast station into station table.
1265 */
Reinette Chatred2e210a2010-04-23 10:33:33 -07001266int iwl3945_add_bcast_station(struct iwl_priv *priv)
Reinette Chatre3459ab52010-01-22 14:22:49 -08001267{
Reinette Chatred2e210a2010-04-23 10:33:33 -07001268 int ret;
1269
Reinette Chatre3459ab52010-01-22 14:22:49 -08001270 IWL_DEBUG_INFO(priv, "Adding broadcast station to station table\n");
Reinette Chatred2e210a2010-04-23 10:33:33 -07001271 ret = iwl_add_local_station(priv, iwl_bcast_addr, false);
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08001272 /*
1273 * It is assumed that when station is added more initialization
1274 * needs to be done, but for 3945 it is not the case and we can
1275 * just release station table access right here.
1276 */
1277 priv->stations[priv->hw_params.bcast_sta_id].used &= ~IWL_STA_UCODE_INPROGRESS;
Reinette Chatred2e210a2010-04-23 10:33:33 -07001278 return ret;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08001279
Reinette Chatre3459ab52010-01-22 14:22:49 -08001280}
1281EXPORT_SYMBOL(iwl3945_add_bcast_station);
1282
1283/**
Tomas Winkler4f40e4d2008-05-15 13:54:04 +08001284 * iwl_get_sta_id - Find station's index within station table
1285 *
1286 * If new IBSS station, create new entry in station table
1287 */
1288int iwl_get_sta_id(struct iwl_priv *priv, struct ieee80211_hdr *hdr)
1289{
1290 int sta_id;
Luis R. Rodriguez943ab702009-07-14 20:14:07 -04001291 __le16 fc = hdr->frame_control;
Tomas Winkler4f40e4d2008-05-15 13:54:04 +08001292
1293 /* If this frame is broadcast or management, use broadcast station id */
Luis R. Rodriguez943ab702009-07-14 20:14:07 -04001294 if (!ieee80211_is_data(fc) || is_multicast_ether_addr(hdr->addr1))
Tomas Winkler4f40e4d2008-05-15 13:54:04 +08001295 return priv->hw_params.bcast_sta_id;
1296
1297 switch (priv->iw_mode) {
1298
1299 /* If we are a client station in a BSS network, use the special
1300 * AP station entry (that's the only station we communicate with) */
Johannes Berg05c914f2008-09-11 00:01:58 +02001301 case NL80211_IFTYPE_STATION:
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08001302 /*
1303 * If addition of station not complete yet, which means
1304 * that rate scaling has not been initialized, then return
1305 * the broadcast station.
1306 */
1307 if (!(priv->stations[IWL_AP_ID].used & IWL_STA_UCODE_ACTIVE))
1308 return priv->hw_params.bcast_sta_id;
Tomas Winkler4f40e4d2008-05-15 13:54:04 +08001309 return IWL_AP_ID;
1310
1311 /* If we are an AP, then find the station, or use BCAST */
Johannes Berg05c914f2008-09-11 00:01:58 +02001312 case NL80211_IFTYPE_AP:
Tomas Winklerc587de02009-06-03 11:44:07 -07001313 sta_id = iwl_find_station(priv, hdr->addr1);
Tomas Winkler4f40e4d2008-05-15 13:54:04 +08001314 if (sta_id != IWL_INVALID_STATION)
1315 return sta_id;
1316 return priv->hw_params.bcast_sta_id;
1317
1318 /* If this frame is going out to an IBSS network, find the station,
1319 * or create a new station table entry */
Johannes Berg05c914f2008-09-11 00:01:58 +02001320 case NL80211_IFTYPE_ADHOC:
Tomas Winklerc587de02009-06-03 11:44:07 -07001321 sta_id = iwl_find_station(priv, hdr->addr1);
Tomas Winkler4f40e4d2008-05-15 13:54:04 +08001322 if (sta_id != IWL_INVALID_STATION)
1323 return sta_id;
1324
Tomas Winklere1623442009-01-27 14:27:56 -08001325 IWL_DEBUG_DROP(priv, "Station %pM not in station map. "
Tomas Winkler4f40e4d2008-05-15 13:54:04 +08001326 "Defaulting to broadcast...\n",
Johannes Berge1749612008-10-27 15:59:26 -07001327 hdr->addr1);
Reinette Chatre3d816c72009-08-07 15:41:37 -07001328 iwl_print_hex_dump(priv, IWL_DL_DROP, (u8 *) hdr, sizeof(*hdr));
Tomas Winkler4f40e4d2008-05-15 13:54:04 +08001329 return priv->hw_params.bcast_sta_id;
1330
1331 default:
Winkler, Tomas39aadf82008-12-19 10:37:32 +08001332 IWL_WARN(priv, "Unknown mode of operation: %d\n",
1333 priv->iw_mode);
Tomas Winkler4f40e4d2008-05-15 13:54:04 +08001334 return priv->hw_params.bcast_sta_id;
1335 }
1336}
1337EXPORT_SYMBOL(iwl_get_sta_id);
1338
Tomas Winkler5083e562008-05-29 16:35:15 +08001339/**
Tomas Winkler9f586712008-11-12 13:14:05 -08001340 * iwl_sta_tx_modify_enable_tid - Enable Tx for this TID in station table
Tomas Winkler5083e562008-05-29 16:35:15 +08001341 */
Tomas Winkler9f586712008-11-12 13:14:05 -08001342void iwl_sta_tx_modify_enable_tid(struct iwl_priv *priv, int sta_id, int tid)
Tomas Winkler5083e562008-05-29 16:35:15 +08001343{
1344 unsigned long flags;
1345
1346 /* Remove "disable" flag, to enable Tx for this TID */
1347 spin_lock_irqsave(&priv->sta_lock, flags);
1348 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_TID_DISABLE_TX;
1349 priv->stations[sta_id].sta.tid_disable_tx &= cpu_to_le16(~(1 << tid));
1350 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1351 spin_unlock_irqrestore(&priv->sta_lock, flags);
1352
1353 iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
1354}
Tomas Winkler9f586712008-11-12 13:14:05 -08001355EXPORT_SYMBOL(iwl_sta_tx_modify_enable_tid);
1356
1357int iwl_sta_rx_agg_start(struct iwl_priv *priv,
1358 const u8 *addr, int tid, u16 ssn)
1359{
1360 unsigned long flags;
1361 int sta_id;
1362
Tomas Winklerc587de02009-06-03 11:44:07 -07001363 sta_id = iwl_find_station(priv, addr);
Tomas Winkler9f586712008-11-12 13:14:05 -08001364 if (sta_id == IWL_INVALID_STATION)
1365 return -ENXIO;
1366
1367 spin_lock_irqsave(&priv->sta_lock, flags);
1368 priv->stations[sta_id].sta.station_flags_msk = 0;
1369 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_ADDBA_TID_MSK;
1370 priv->stations[sta_id].sta.add_immediate_ba_tid = (u8)tid;
1371 priv->stations[sta_id].sta.add_immediate_ba_ssn = cpu_to_le16(ssn);
1372 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1373 spin_unlock_irqrestore(&priv->sta_lock, flags);
1374
1375 return iwl_send_add_sta(priv, &priv->stations[sta_id].sta,
1376 CMD_ASYNC);
1377}
1378EXPORT_SYMBOL(iwl_sta_rx_agg_start);
1379
1380int iwl_sta_rx_agg_stop(struct iwl_priv *priv, const u8 *addr, int tid)
1381{
1382 unsigned long flags;
1383 int sta_id;
1384
Tomas Winklerc587de02009-06-03 11:44:07 -07001385 sta_id = iwl_find_station(priv, addr);
Wey-Yi Guya2f1cbe2009-03-17 21:51:52 -07001386 if (sta_id == IWL_INVALID_STATION) {
1387 IWL_ERR(priv, "Invalid station for AGG tid %d\n", tid);
Tomas Winkler9f586712008-11-12 13:14:05 -08001388 return -ENXIO;
Wey-Yi Guya2f1cbe2009-03-17 21:51:52 -07001389 }
Tomas Winkler9f586712008-11-12 13:14:05 -08001390
1391 spin_lock_irqsave(&priv->sta_lock, flags);
1392 priv->stations[sta_id].sta.station_flags_msk = 0;
1393 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_DELBA_TID_MSK;
1394 priv->stations[sta_id].sta.remove_immediate_ba_tid = (u8)tid;
1395 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1396 spin_unlock_irqrestore(&priv->sta_lock, flags);
1397
1398 return iwl_send_add_sta(priv, &priv->stations[sta_id].sta,
1399 CMD_ASYNC);
1400}
1401EXPORT_SYMBOL(iwl_sta_rx_agg_stop);
1402
Johannes Berg6ab10ff2009-11-13 11:56:37 -08001403void iwl_sta_modify_ps_wake(struct iwl_priv *priv, int sta_id)
Tomas Winkler9f586712008-11-12 13:14:05 -08001404{
1405 unsigned long flags;
1406
1407 spin_lock_irqsave(&priv->sta_lock, flags);
1408 priv->stations[sta_id].sta.station_flags &= ~STA_FLG_PWR_SAVE_MSK;
1409 priv->stations[sta_id].sta.station_flags_msk = STA_FLG_PWR_SAVE_MSK;
1410 priv->stations[sta_id].sta.sta.modify_mask = 0;
Johannes Berg6ab10ff2009-11-13 11:56:37 -08001411 priv->stations[sta_id].sta.sleep_tx_count = 0;
Tomas Winkler9f586712008-11-12 13:14:05 -08001412 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1413 spin_unlock_irqrestore(&priv->sta_lock, flags);
1414
1415 iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
1416}
Johannes Berg6ab10ff2009-11-13 11:56:37 -08001417EXPORT_SYMBOL(iwl_sta_modify_ps_wake);
Tomas Winkler9f586712008-11-12 13:14:05 -08001418
Johannes Berg6ab10ff2009-11-13 11:56:37 -08001419void iwl_sta_modify_sleep_tx_count(struct iwl_priv *priv, int sta_id, int cnt)
Tomas Winkler9f586712008-11-12 13:14:05 -08001420{
Johannes Berg6ab10ff2009-11-13 11:56:37 -08001421 unsigned long flags;
Tomas Winkler9f586712008-11-12 13:14:05 -08001422
Johannes Berg6ab10ff2009-11-13 11:56:37 -08001423 spin_lock_irqsave(&priv->sta_lock, flags);
1424 priv->stations[sta_id].sta.station_flags |= STA_FLG_PWR_SAVE_MSK;
1425 priv->stations[sta_id].sta.station_flags_msk = STA_FLG_PWR_SAVE_MSK;
1426 priv->stations[sta_id].sta.sta.modify_mask =
1427 STA_MODIFY_SLEEP_TX_COUNT_MSK;
1428 priv->stations[sta_id].sta.sleep_tx_count = cpu_to_le16(cnt);
1429 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1430 spin_unlock_irqrestore(&priv->sta_lock, flags);
Tomas Winkler9f586712008-11-12 13:14:05 -08001431
Johannes Berg6ab10ff2009-11-13 11:56:37 -08001432 iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
Tomas Winkler9f586712008-11-12 13:14:05 -08001433}
Wey-Yi Guy74bcdb32010-03-17 13:34:34 -07001434EXPORT_SYMBOL(iwl_sta_modify_sleep_tx_count);
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08001435
1436int iwl_mac_sta_remove(struct ieee80211_hw *hw,
1437 struct ieee80211_vif *vif,
1438 struct ieee80211_sta *sta)
1439{
1440 int ret;
1441 struct iwl_priv *priv = hw->priv;
1442 IWL_DEBUG_INFO(priv, "received request to remove station %pM\n",
1443 sta->addr);
Johannes Bergc0222df2010-04-28 08:44:51 -07001444 ret = iwl_remove_station(priv, sta->addr);
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08001445 if (ret)
1446 IWL_ERR(priv, "Error removing station %pM\n",
1447 sta->addr);
1448 return ret;
1449}
1450EXPORT_SYMBOL(iwl_mac_sta_remove);